MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: HazardyKnusperkeks, curdeius.
MyDeveloperDay added projects: clang, clang-format.
MyDeveloperDay requested review of this revision.

https://github.com/llvm/llvm-project/issues/53981

Reorder the qualifiers inside the template argument. This should handle the 
simple cases of

  <const T>
  <T const>

But only by relaxing that single letter capital variables are not possible 
macros

Fixes: #53981


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120710

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


Index: clang/unittests/Format/QualifierFixerTest.cpp
===================================================================
--- clang/unittests/Format/QualifierFixerTest.cpp
+++ clang/unittests/Format/QualifierFixerTest.cpp
@@ -895,5 +895,36 @@
                Style);
 }
 
+TEST_F(QualifierFixerTest, TemplatesRight) {
+  FormatStyle Style = getLLVMStyle();
+  Style.QualifierAlignment = FormatStyle::QAS_Custom;
+  Style.QualifierOrder = {"type", "const"};
+
+  verifyFormat("template <typename T>\n"
+               "  requires Concept<T const>\n"
+               "void f();",
+               "template <typename T>\n"
+               "  requires Concept<const T>\n"
+               "void f();",
+               Style);
+  verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
+}
+
+TEST_F(QualifierFixerTest, TemplatesLeft) {
+  FormatStyle Style = getLLVMStyle();
+  Style.QualifierAlignment = FormatStyle::QAS_Custom;
+  Style.QualifierOrder = {"const", "type"};
+
+  verifyFormat("template <const T> t;", "template <T const> t;", Style);
+  verifyFormat("template <typename T>\n"
+               "  requires Concept<const T>\n"
+               "void f();",
+               "template <typename T>\n"
+               "  requires Concept<T const>\n"
+               "void f();",
+               Style);
+  verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
+}
+
 } // namespace format
 } // namespace clang
Index: clang/lib/Format/QualifierAlignmentFixer.cpp
===================================================================
--- clang/lib/Format/QualifierAlignmentFixer.cpp
+++ clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -223,6 +223,12 @@
   if (LastQual && Qual != LastQual) {
     rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
     Tok = LastQual;
+  } else if (Tok->startsSequence(QualifierType, tok::identifier,
+                                 TT_TemplateCloser)) {
+    FormatToken *Closer = Tok->Next->Next;
+    rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/false);
+    Tok = Closer;
+    return Tok;
   } else if (Tok->startsSequence(QualifierType, tok::identifier,
                                  TT_TemplateOpener)) {
     // Read from the TemplateOpener to
@@ -307,6 +313,11 @@
         rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
         Tok = Tok->Next;
       }
+    } else if (Tok->startsSequence(tok::identifier, QualifierType,
+                                   TT_TemplateCloser)) {
+      FormatToken *Closer = Tok->Next->Next;
+      rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
+      Tok = Closer;
     }
   }
   if (Tok->is(TT_TemplateOpener) && Tok->Next &&
@@ -329,8 +340,12 @@
         Next = Next->getNextNonComment();
       assert(Next->MatchingParen && "Missing template closer");
       Next = Next->MatchingParen;
+
+      // If the template closer is closing the requires clause
+      // then stop and go back to the TemplateOpenener and do whatever is
+      // inside the <>
       if (Next->ClosesRequiresClause)
-        return Next;
+        return Next->MatchingParen;
       Next = Next->Next;
 
       // Move to the end of any template class members e.g.
@@ -460,8 +475,12 @@
     return false;
   if (!Tok->is(tok::identifier))
     return false;
-  if (Tok->TokenText.upper() == Tok->TokenText.str())
+  if (Tok->TokenText.upper() == Tok->TokenText.str()) {
+    // T,K,U,V likely could be template arguments
+    if (Tok->TokenText.size() == 1)
+      return false;
     return true;
+  }
   return false;
 }
 


Index: clang/unittests/Format/QualifierFixerTest.cpp
===================================================================
--- clang/unittests/Format/QualifierFixerTest.cpp
+++ clang/unittests/Format/QualifierFixerTest.cpp
@@ -895,5 +895,36 @@
                Style);
 }
 
+TEST_F(QualifierFixerTest, TemplatesRight) {
+  FormatStyle Style = getLLVMStyle();
+  Style.QualifierAlignment = FormatStyle::QAS_Custom;
+  Style.QualifierOrder = {"type", "const"};
+
+  verifyFormat("template <typename T>\n"
+               "  requires Concept<T const>\n"
+               "void f();",
+               "template <typename T>\n"
+               "  requires Concept<const T>\n"
+               "void f();",
+               Style);
+  verifyFormat("TemplateType<T const> t;", "TemplateType<const T> t;", Style);
+}
+
+TEST_F(QualifierFixerTest, TemplatesLeft) {
+  FormatStyle Style = getLLVMStyle();
+  Style.QualifierAlignment = FormatStyle::QAS_Custom;
+  Style.QualifierOrder = {"const", "type"};
+
+  verifyFormat("template <const T> t;", "template <T const> t;", Style);
+  verifyFormat("template <typename T>\n"
+               "  requires Concept<const T>\n"
+               "void f();",
+               "template <typename T>\n"
+               "  requires Concept<T const>\n"
+               "void f();",
+               Style);
+  verifyFormat("TemplateType<const T> t;", "TemplateType<T const> t;", Style);
+}
+
 } // namespace format
 } // namespace clang
Index: clang/lib/Format/QualifierAlignmentFixer.cpp
===================================================================
--- clang/lib/Format/QualifierAlignmentFixer.cpp
+++ clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -223,6 +223,12 @@
   if (LastQual && Qual != LastQual) {
     rotateTokens(SourceMgr, Fixes, Tok, LastQual, /*Left=*/false);
     Tok = LastQual;
+  } else if (Tok->startsSequence(QualifierType, tok::identifier,
+                                 TT_TemplateCloser)) {
+    FormatToken *Closer = Tok->Next->Next;
+    rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/false);
+    Tok = Closer;
+    return Tok;
   } else if (Tok->startsSequence(QualifierType, tok::identifier,
                                  TT_TemplateOpener)) {
     // Read from the TemplateOpener to
@@ -307,6 +313,11 @@
         rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
         Tok = Tok->Next;
       }
+    } else if (Tok->startsSequence(tok::identifier, QualifierType,
+                                   TT_TemplateCloser)) {
+      FormatToken *Closer = Tok->Next->Next;
+      rotateTokens(SourceMgr, Fixes, Tok, Tok->Next, /*Left=*/true);
+      Tok = Closer;
     }
   }
   if (Tok->is(TT_TemplateOpener) && Tok->Next &&
@@ -329,8 +340,12 @@
         Next = Next->getNextNonComment();
       assert(Next->MatchingParen && "Missing template closer");
       Next = Next->MatchingParen;
+
+      // If the template closer is closing the requires clause
+      // then stop and go back to the TemplateOpenener and do whatever is
+      // inside the <>
       if (Next->ClosesRequiresClause)
-        return Next;
+        return Next->MatchingParen;
       Next = Next->Next;
 
       // Move to the end of any template class members e.g.
@@ -460,8 +475,12 @@
     return false;
   if (!Tok->is(tok::identifier))
     return false;
-  if (Tok->TokenText.upper() == Tok->TokenText.str())
+  if (Tok->TokenText.upper() == Tok->TokenText.str()) {
+    // T,K,U,V likely could be template arguments
+    if (Tok->TokenText.size() == 1)
+      return false;
     return true;
+  }
   return false;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to