[cfe-dev] More clang-format options please ?

Arthur O'Dwyer via cfe-dev cfe-dev at lists.llvm.org
Fri Dec 3 07:31:10 PST 2021


On Fri, Dec 3, 2021 at 4:27 AM Björn Schäpers via cfe-dev <
cfe-dev at lists.llvm.org> wrote:

> Am 03.12.2021 um 00:08 schrieb Timothy Madden via cfe-dev:
> >
> > Is it possible to add a clang-format option for the indent to use inside
> > preprocessing directives please ? So I could use an indent of 1 for the
> PP, and
> > 4 for the rest of the language
>
> Something like PPIndentWidth
> (https://clang.llvm.org/docs/ClangFormatStyleOptions.html) ? :)
>

Yep, this was added to trunk just like a month ago, in the LLVM 14
timeframe.
FWIW, this option was of interest to libc++ (and I believe we turn it on),
but it's still not perfect for our use-cases. It has trouble with the fuzzy
idea of "being in a new context," e.g. it will do this:

#ifndef FOO

#define FOO


int foo();


#if BAR


int bar() {

    baz();

# if BAZ_TWICE

    baz();

# endif // BAZ_TWICE

    return 0;

}


#endif // BAR


#endif // FOO

It understands that the header guard is special and doesn't force-indent
everything inside it; but it isn't currently capable of applying the same
logic to the BAZ_TWICE section: to say "okay, the BAR business is located
entirely outside this function and doesn't affect the stuff inside it."
It's going to treat BAZ_TWICE as "nested by one level," even though a human
programmer wouldn't generally think of it that way.
(FWIW, it makes perfect sense that a *machine* would think of it that way;
I mean I get why clang-format does this. I might even thank it for doing
this, in some pathological cases. But it doesn't result in very
aesthetically pleasing results in the non-pathological cases.)

Further nit-that-maybe-should-be-a-bug-report-instead-of-an-email :) —
It would be helpful if the documentation for `PPIndentWidth` mentioned that
setting `PPIndentWidth` will take effect only if you *also* set
`IndentPPDirectives`; and `IndentPPDirectives` should mention that the
indent level is controlled by (`IndentWidth` and) `PPIndentWidth`.



> > Also is it possible please to add an option for [...]
> > template <typename ElementT>
> >      class vector
> > {
>

FWIW, I'd urge you to take clang-format's lack of support here as a hint,
and reformat your code in a style that's closer to common industry
practice. ;)

my $.02,
Arthur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20211203/a151d8a6/attachment.html>


More information about the cfe-dev mailing list