[clang] [clang-format] Handle constrained auto in QualifierAlignment (PR #72251)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 15:33:40 PST 2023


https://github.com/owenca updated https://github.com/llvm/llvm-project/pull/72251

>From 8e61a470fc7e4cf11a6cf285668a7f9eca72702c Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 14 Nov 2023 05:05:14 -0800
Subject: [PATCH 1/2] [clang-format] Handle constrained auto in
 QualifierAlignment

Fixed #69610.
---
 clang/lib/Format/QualifierAlignmentFixer.cpp  |  5 +++++
 clang/unittests/Format/QualifierFixerTest.cpp | 10 ++++++++++
 2 files changed, 15 insertions(+)

diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 7167e50ec724eab..72464d367153934 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -345,6 +345,8 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
         TypeToken = Next->getNextNonComment()->getNextNonComment();
       }
     }
+    if (Next->is(tok::kw_auto))
+      TypeToken = Next;
 
     // Place the Qualifier at the end of the list of qualifiers.
     while (isQualifier(TypeToken->getNextNonComment())) {
@@ -446,6 +448,9 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeLeft(
         return false;
       }
 
+      if (Tok->endsSequence(tok::kw_auto, tok::identifier))
+        return false;
+
       return true;
     };
 
diff --git a/clang/unittests/Format/QualifierFixerTest.cpp b/clang/unittests/Format/QualifierFixerTest.cpp
index 73814e7414f5e7d..a56323a88f4a04d 100644
--- a/clang/unittests/Format/QualifierFixerTest.cpp
+++ b/clang/unittests/Format/QualifierFixerTest.cpp
@@ -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);
 
@@ -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);
 

>From 0a5a39d757400328f95ad5a851c9d247bdc8321b Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Tue, 14 Nov 2023 15:33:34 -0800
Subject: [PATCH 2/2] Update clang/lib/Format/QualifierAlignmentFixer.cpp

---
 clang/lib/Format/QualifierAlignmentFixer.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/lib/Format/QualifierAlignmentFixer.cpp b/clang/lib/Format/QualifierAlignmentFixer.cpp
index 72464d367153934..e2fab1c1e3c2a37 100644
--- a/clang/lib/Format/QualifierAlignmentFixer.cpp
+++ b/clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -345,6 +345,7 @@ const FormatToken *LeftRightQualifierAlignmentFixer::analyzeRight(
         TypeToken = Next->getNextNonComment()->getNextNonComment();
       }
     }
+
     if (Next->is(tok::kw_auto))
       TypeToken = Next;
 



More information about the cfe-commits mailing list