How can I apply only one clang-format action?
Asked Answered
M

2

47

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.

Madoc answered 10/6, 2015 at 17:58 Comment(11)
i think you should specify BasedOnStyleHugues
you should try it as a plugin (e.g. for Vim , Visual-Studio). It is much handy to use this way.Hugues
@Hugues As a plugin, will it still apply more than one formatting rule? There are many conflicting formatting styles in our codebase, and I don't want to clutter a commit with all of these additional changes that effect other people's portions of code. I just want to apply a single style change so that other people can consume the change and handle merge conflicts more easily. It's not practical to subscribe to an entire code style using BasedOnStyle right now.Madoc
of course. You just specify BasedOnStyle to set values for all options and then you can "override" any number of options. All this via config file. You can even have config files / project.Hugues
@Hugues Ok, so if I set every option to false and 0, clang shouldn't change any code at all. I'll give it a try.Madoc
why would you set every option to false and 0? I don't know try it and read the documentationHugues
@Hugues Because I only want to apply one rule change at at a time. I need a default configuration where clang-format won't make any changes at all. Then I can tweak one at a time, and see a diff of only that format option.Madoc
ok but you should be aware that some rules interact with each otherHugues
@Hugues Right. And a lot of them take integers 0 through N. There's not really a way to turn them off...Madoc
Blegh, came here for this functionality too. Sad it doesn't exist.Archean
So it turns out that manual is the best formatting method in 2021... So much for the automation!Bibliotheca
K
24

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.

Knowledgeable answered 24/8, 2015 at 20:28 Comment(1)
Unfortunately, this seems to be the correct answer.Madoc
S
7

clang-format does not have a mechanism to do this. The palliative I use is to configure what I want changed in two different ways, I run the tool with both configurations and diff them, that tells me the changes that affect what I want, but in general this requires at least some manual work.

clang-format is a good idea with a poor implementation. Some of the many deficiencies it has are that there is no option to have it indicate the reason for a change, the XML output format is not consumable by any other popular tool...

Shem answered 3/9, 2020 at 23:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.