Home > Mobile >  Is there any options to make clang-format produce the following effect?
Is there any options to make clang-format produce the following effect?

Time:12-18

I use clang-format to format the following code, but the effect is not what I want:

void f(int, int, std::vector<int>, std::map<int, int>)
{}

int main()
{
    f(
        {
    },
        {}, {1, 2, 3},
        {
            {1, 2},
            {3, 4},
        });
}

Is there any options to make clang-format produce the following effect?

void f(int, int, std::vector<int>, std::map<int, int>)
{}

int main()
{
    f({}, 
      {}, 
      {1, 2, 3},
      {
          {1, 2},
          {3, 4},
      });
}

CodePudding user response:

Yes there is, use the style you want. (it's really that easy!)

clang-format -h tells you about the available options that clang-format has.

  --style=<string>           - Coding style, currently supports:
                                 LLVM, GNU, Google, Chromium, Microsoft, Mozilla, WebKit.
                               Use -style=file to load style configuration from
                               .clang-format file located in one of the parent
                               directories of the source file (or current
                               directory for stdin).

In your case, you probably want something like clang-format --style=LLVM.

But it's not going to be exactly what you want. So, you'll need to write your own style file. It's not that hard. Use clang-format --style=file and put the style definitions in a file called .clang-format in the same, or one of its parent directories.

The documentation explains exactly what you need to do. If you're more of the visual type, this generator might be helpful.

Note, though, that you're probably better off making only minor adjustments to a "common" base style; other programmers might not like it when you do wildly different than other code they're used to. For an example of a modified-as-far-as-necessary-to-deal-with-specific-project-needs style file, see GNU Radio's .clang-format file.

  • Related