Skip to content

[clang-format] Preprocessor directives lead to exessive indentation and other issues #60843

Closed
@Flamefire

Description

@Flamefire

Given the following (reduced) .clangformat style file:

---
Language:        Cpp
AllowShortFunctionsOnASingleLine: Inline
BasedOnStyle:    'LLVM'
IndentPPDirectives: AfterHash
IndentWidth:     4
NamespaceIndentation: Inner
CompactNamespaces: True
...

I am reformatting the following C++ file:

#ifdef TRIGGER_CLANGFORMAT_15+
#    pragma suppress warning
#endif
namespace a { namespace b { namespace c {

    class Foo {
#ifdef TRIGGER_CLANGFORMAT_14
#    pragma suppress warning
#endif
        std::string bar_;

      public:
        const std::string &bar() const { return bar_; }
    };

}}} // namespace a::b::c

As you can see no changes are expected. However clang-format-15 up to 17 indent the whole class by an additional 4 spaces.
clang-format-14 does not do that but it breaks the member function into separate lines although AllowShortFunctionsOnASingleLine is set.

A bit more debugging:

  • Removing IndentPPDirectives or the preprocessor directives fixes the behavior
  • The value of IndentPPDirectives does not matter, i.e. Before/AfterHash both produce the bug (None is the same as removal, i.e. works)
  • The bug on clang-format 15+ is triggered by the first preprocessor directive, the one on 14 by the 2nd. No effect of the first on 14 or 2nd on 15+ (i.e. that works) and the names/content seem to be irrelevant.
  • The value of PPIndentWidth does not matter
  • When disabling CompactNamespaces the 3 namespace declarations are put on separate lines but namespace c is already indented despite NamespaceIndentation: Inner (happens consistently on version 14-17) so might be a related issue

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions