Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang-format: always break if params don't fit?

I can't figure out if it's possible to configure clang-format to always break if parameters don't fit, ie:

// Try this first:
SomeCall(aaa, bbb, ccc);

// If doesn't fit, try this:
SomeCall(
     aaa, bbb, ccc);

// If still doesn't fit, do NOT try this:
SomeCall(aaa, bbb,
         ccc);

// and NOT this:
SomeCall(aaa,
         bbb,
         ccc);

// but immediately do this:
SomeCall(
     aaa,
     bbb,
     ccc);

So far I've concluded that it's not possible to do this with clang-format 3.4. Is it correct?

like image 318
Serge Khorun Avatar asked Feb 23 '14 09:02

Serge Khorun


People also ask

Can clang-format break code?

Short answer: YES. The clang-format tool has a -sort-includes option. Changing the order of #include directives can definitely change the behavior of existing code, and may break existing code.

Is clang-format good?

clang-format is a tool to automatically format C/C++/Objective-C code, so that developers don't need to worry about style issues during code reviews. It is highly recommended to format your changed C++ code before opening pull requests, which will save you and the reviewers' time.

How do you customize your clang-format?

clang-format supports two ways to provide custom style options: directly specify style configuration in the -style= command line option or use -style=file and put style configuration in the . clang-format or _clang-format file in the project directory.

What is Clangformat?

Clang-Format is a widely-used C++ code formatter. As it provides an option to define code style options in YAML-formatted files — named . clang-format or _clang-format — these files often become a part of your project where you keep all code style rules.


2 Answers

In newer version of clang-format, this can now be achieved with:

AlignAfterOpenBracket: AlwaysBreak
BinPackArguments: false
BinPackParameters: false

See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for a full explanation of these options.

like image 118
Daniel Kiss Avatar answered Oct 12 '22 02:10

Daniel Kiss


I unfortunately only have access to clang-format 3.8.0 ("clang-format version 3.8.0 (tags/RELEASE_380/final)"), so I can't do testing easily for release 3.4.

There's a manual for the latest release of clang-format available here that I don't know if you've found or not. It links to the list of Clang-Format Style Options. In there, there's a style option that echoes the title of your question: AlignAfterOpenBracket: AlwaysBreak, Always break after an open bracket, if the parameters don’t fit on a single line.

To use this, put the following in your ~/.clang-format file:

AlignAfterOpenBracket: AlwaysBreak

After some testing, it appears that it is doing exactly what you would want it to do, almost.

It formats

SomeCall(aaa, bbb, ccc);

as

SomeCall(
  aaa, bbb,
  ccc);

if aaa, bbb, ccc doesn't fit on one line. It will not break between aaa and bbb until aaa also is too long, in which case bbb and ccc will be on the same line. I.e. it breaks after the opening (, but then tries to fill lines. It doesn't automatically break on all commas.

Looking at corresponding page for clang-format 3.4, it appears as if this configuration option sadly isn't there. This leaves you with two options:

  1. Upgrade to a newer version of clang-format.
  2. Don't.
like image 1
Kusalananda Avatar answered Oct 12 '22 02:10

Kusalananda