Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply only one clang-format action?

I want to use clang-format to align my comments, but nothing else.

The option for that is: AlignTrailingComments (bool).

But when I run the following:

clang-format-3.6 -i -style='{AlignTrailingComments: true}' <file> 

It performs all kinds of other formatting options that I suppose have a default when unspecified.

How can I execute just one clang formatting rule on the codebase?

Having all of these defaults make it difficult to see the full effect that a single formatting option has on the code. I have to parse through the diff of all these other changes and decide if it was the option I specified that actually did it.


I noticed that there is a DisableFormat option, but no matter how I use it, it stops any formatting from happening at all.

clang-format-3.6 -i -style='{AlignTrailingComments: true, DisableFormat: true}'  clang-format-3.6 -i -style='{DisableFormat: true, AlignTrailingComments: true}' 

Both cause clang-format to not make any code anywhere.

like image 375
Trevor Hickey Avatar asked Jun 10 '15 17:06

Trevor Hickey


People also ask

How do I apply a clang file format?

You can install clang-format and git-clang-format via npm install -g clang-format . To automatically format a file according to Electron C++ code style, run clang-format -i path/to/electron/file.cc . It should work on macOS/Linux/Windows.

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.

How do I turn off clang-format?

Disabling Formatting on a Piece of Code The code between a comment // clang-format off or /* clang-format off */ up to a comment // clang-format on or /* clang-format on */ will not be formatted.

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.


1 Answers

I think clang format was simply not designed for this. Rules are not things it applies incrementally, the program is instead built around, parsing your entire program and forgetting (most of) the old whitespace, and generating new whitespace based on the rules you select.

You can see some overview of the architecture here: http://www.llvm.org/devmtg/2013-04/jasper-slides.pdf

First it runs clang lexer and parser, then it divides groups of tokens into "unwrapped lines" which are "tokens we would like to put together on a single line if there was no column limit". Then a layouter determines the formatting of each unwrapped line based on the various constraints and optimizing for the various penalties.

So, I don't think "one clang-format action" is actually a thing, the design looks pretty much monolithic to me.

like image 192
Chris Beck Avatar answered Oct 09 '22 22:10

Chris Beck