Skip to content

Extend IndentPPDirectives with a LeaveAsIs option #38511

@llvmbot

Description

@llvmbot
Bugzilla Link 39163
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor
CC @hartwork

Extended Description

Currently IndentPPDirectives can have values None or AfterHash. There has
also been a suggestion of a BeforeHash option.

I suggest adding another option Leave, which would leave preprocessor directives alone (i.e. not change the indentation at all).

Motivation:

Indenting pre-processor directives can be really tricky with lots of
edge cases. There maybe certain directives you do not want indented,
for example. clang-format already has some detection of include guards
as I understand it, but we also use platform macros around entire
files:

#ifdef OSX
...
#endif

These are not recognized and cause the entire file to be indented.

Further complications arise from interaction between macros and C
code, when you want macros to be indented to the scope of the C code
and the C code to be indented to the scope of the macros, i.e.:

if (x) {
    #ifdef OSX
       bla();
    #endif
}

While it is possible to come up with decent indentation for simple
cases like this, it can get hairy, since the macros don't follow the
C syntax and vice versa. For example, the scopes in C code and the
preprocessor don't have to nest nicely. It may be hard to come up with
good consistent rules for how C/preprocessor interactions should be
handled.

A Leave option would sidestep these thorny issues and leave the
indentation of preprocessor macros to the code author. It should also
be easy to implement.

References:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions