Skip to content

Commit

Permalink
[clang-format] Fix QualifierOrder breaking the code with requires cla…
Browse files Browse the repository at this point in the history
…use.

Fixes #53962.

Given the config:
```
BasedOnStyle: LLVM
QualifierAlignment: Custom
QualifierOrder: ['constexpr', 'type']
```

The code:
```
template <typename F>
  requires std::invocable<F>
constexpr constructor();
```
was incorrectly formatted to:
```
template <typename F>
  requires
constexpr std::invocable<F> constructor();
```
because we considered `std::invocable<F> constexpr` as a type, not recognising the requires clause.

This patch avoids moving the qualifier across the boundary of the requires clause (checking `ClosesRequiresClause`).

Reviewed By: HazardyKnusperkeks, owenpan

Differential Revision: https://reviews.llvm.org/D120309
  • Loading branch information
mkurdej committed Feb 24, 2022
1 parent ff3f3a5 commit 46f6c83
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions clang/lib/Format/QualifierAlignmentFixer.cpp
Expand Up @@ -328,14 +328,17 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
if (Next->is(tok::comment) && Next->getNextNonComment())
Next = Next->getNextNonComment();
assert(Next->MatchingParen && "Missing template closer");
Next = Next->MatchingParen->Next;
Next = Next->MatchingParen;
if (Next->ClosesRequiresClause)
return Next;
Next = Next->Next;

// Move to the end of any template class members e.g.
// `Foo<int>::iterator`.
if (Next && Next->startsSequence(tok::coloncolon, tok::identifier))
Next = Next->Next->Next;
if (Next && Next->is(QualifierType)) {
// Remove the const.
// Move the qualifier.
insertQualifierBefore(SourceMgr, Fixes, Tok, Qualifier);
removeToken(SourceMgr, Fixes, Next);
return Next;
Expand All @@ -344,7 +347,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
if (Next && Next->Next &&
Next->Next->isOneOf(tok::amp, tok::ampamp, tok::star)) {
if (Next->is(QualifierType)) {
// Remove the qualifier.
// Move the qualifier.
insertQualifierBefore(SourceMgr, Fixes, Tok, Qualifier);
removeToken(SourceMgr, Fixes, Next);
return Next;
Expand Down
15 changes: 15 additions & 0 deletions clang/unittests/Format/QualifierFixerTest.cpp
Expand Up @@ -858,6 +858,21 @@ TEST_F(QualifierFixerTest, QualifierTemplates) {
Style);
}

TEST_F(QualifierFixerTest, WithConstraints) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Style.QualifierOrder = {"constexpr", "type"};

verifyFormat("template <typename T>\n"
" requires Concept<F>\n"
"constexpr constructor();",
Style);
verifyFormat("template <typename T>\n"
" requires Concept1<F> && Concept2<F>\n"
"constexpr constructor();",
Style);
}

TEST_F(QualifierFixerTest, DisableRegions) {
FormatStyle Style = getLLVMStyle();
Style.QualifierAlignment = FormatStyle::QAS_Custom;
Expand Down

0 comments on commit 46f6c83

Please sign in to comment.