Skip to content

Commit

Permalink
[clang-format] Handle constrained auto in QualifierAlignment (#72251)
Browse files Browse the repository at this point in the history
Fixed #69610.
  • Loading branch information
owenca committed Nov 15, 2023
1 parent 250d9c8 commit b04664b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clang/lib/Format/QualifierAlignmentFixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
}
}

if (Next->is(tok::kw_auto))
TypeToken = Next;

// Place the Qualifier at the end of the list of qualifiers.
while (isQualifier(TypeToken->getNextNonComment())) {
// The case `volatile Foo::iter const` -> `Foo::iter const volatile`
Expand Down Expand Up @@ -446,6 +449,9 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
return false;
}

if (Tok->endsSequence(tok::kw_auto, tok::identifier))
return false;

return true;
};

Expand Down
10 changes: 10 additions & 0 deletions clang/unittests/Format/QualifierFixerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ TEST_F(QualifierFixerTest, RightQualifier) {
verifyFormat("auto const &ir = i;", "const auto &ir = i;", Style);
verifyFormat("auto const *ip = &i;", "const auto *ip = &i;", Style);

verifyFormat("void f(Concept auto const &x);",
"void f(const Concept auto &x);", Style);
verifyFormat("void f(std::integral auto const &x);",
"void f(const std::integral auto &x);", Style);

verifyFormat("Foo<Foo<int> const> P;\n#if 0\n#else\n#endif",
"Foo<const Foo<int>> P;\n#if 0\n#else\n#endif", Style);

Expand Down Expand Up @@ -653,6 +658,11 @@ TEST_F(QualifierFixerTest, LeftQualifier) {
verifyFormat("const auto &ir = i;", "auto const &ir = i;", Style);
verifyFormat("const auto *ip = &i;", "auto const *ip = &i;", Style);

verifyFormat("void f(const Concept auto &x);",
"void f(Concept auto const &x);", Style);
verifyFormat("void f(const std::integral auto &x);",
"void f(std::integral auto const &x);", Style);

verifyFormat("Foo<const Foo<int>> P;\n#if 0\n#else\n#endif",
"Foo<Foo<int> const> P;\n#if 0\n#else\n#endif", Style);

Expand Down

0 comments on commit b04664b

Please sign in to comment.