[clang] 46f6c83 - [clang-format] Fix QualifierOrder breaking the code with requires clause.

Marek Kurdej via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 24 01:16:14 PST 2022


Author: Marek Kurdej
Date: 2022-02-24T10:16:10+01:00
New Revision: 46f6c834d9f95d99e0a85aa0c6dc07419aa6dee2

URL: https://github.com/llvm/llvm-project/commit/46f6c834d9f95d99e0a85aa0c6dc07419aa6dee2
DIFF: https://github.com/llvm/llvm-project/commit/46f6c834d9f95d99e0a85aa0c6dc07419aa6dee2.diff

LOG: [clang-format] Fix QualifierOrder breaking the code with requires clause.

Fixes https://github.com/llvm/llvm-project/issues/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

Added: 
    

Modified: 
    clang/lib/Format/QualifierAlignmentFixer.cpp
    clang/unittests/Format/QualifierFixerTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 233b081a95f6d..aff5562dd9721 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -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;
@@ -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;

diff  --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index 14f09d875e6be..0c81c831e1f18 100755
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -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;


        


More information about the cfe-commits mailing list