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

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 14 05:09:17 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

<details>
<summary>Changes</summary>

Fixed #<!-- -->69610.

---
Full diff: https://github.com/llvm/llvm-project/pull/72251.diff


2 Files Affected:

- (modified) clang/lib/Format/QualifierAlignmentFixer.cpp (+5) 
- (modified) clang/unittests/Format/QualifierFixerTest.cpp (+10) 


``````````diff
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);
 

``````````

</details>


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


More information about the cfe-commits mailing list