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

Marek Kurdej via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 22 01:02:35 PST 2022


curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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`).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120309

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
@@ -858,6 +858,17 @@
                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);
+}
+
 TEST_F(QualifierFixerTest, DisableRegions) {
   FormatStyle Style = getLLVMStyle();
   Style.QualifierAlignment = FormatStyle::QAS_Custom;
Index: clang/lib/Format/QualifierAlignmentFixer.cpp
===================================================================
--- clang/lib/Format/QualifierAlignmentFixer.cpp
+++ clang/lib/Format/QualifierAlignmentFixer.cpp
@@ -328,14 +328,17 @@
       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 @@
     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;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120309.410470.patch
Type: text/x-patch
Size: 2136 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220222/cb5a72cf/attachment.bin>


More information about the cfe-commits mailing list