[PATCH] D129940: [clang-format] Fix misannotation of colon in presence of requires clause

Björn Schäpers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 18 12:42:51 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG2b04c41b2832: [clang-format] Fix misannotation of colon in presence of requires clause (authored by HazardyKnusperkeks).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D129940/new/

https://reviews.llvm.org/D129940

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -802,6 +802,70 @@
           << I;
     }
   }
+
+  BaseTokens = annotate("constexpr Foo(Foo const &other)\n"
+                        "    : value{other.value} {\n"
+                        "  do_magic();\n"
+                        "  do_more_magic();\n"
+                        "}");
+
+  ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n"
+                               "  requires std::is_copy_constructible<T>\n"
+                               "    : value{other.value} {\n"
+                               "  do_magic();\n"
+                               "  do_more_magic();\n"
+                               "}");
+
+  NumberOfBaseTokens = 26u;
+  NumberOfAdditionalRequiresClauseTokens = 7u;
+  NumberOfTokensBeforeRequires = 8u;
+
+  ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
+  ASSERT_EQ(ConstrainedTokens.size(),
+            NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
+      << ConstrainedTokens;
+
+  for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
+    if (I < NumberOfTokensBeforeRequires) {
+      EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
+    } else {
+      EXPECT_EQ(*BaseTokens[I],
+                *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
+          << I;
+    }
+  }
+
+  BaseTokens = annotate("constexpr Foo(Foo const &other)\n"
+                        "    : value{other.value} {\n"
+                        "  do_magic();\n"
+                        "  do_more_magic();\n"
+                        "}");
+
+  ConstrainedTokens = annotate("constexpr Foo(Foo const &other)\n"
+                               "  requires (std::is_copy_constructible<T>)\n"
+                               "    : value{other.value} {\n"
+                               "  do_magic();\n"
+                               "  do_more_magic();\n"
+                               "}");
+
+  NumberOfBaseTokens = 26u;
+  NumberOfAdditionalRequiresClauseTokens = 9u;
+  NumberOfTokensBeforeRequires = 8u;
+
+  ASSERT_EQ(BaseTokens.size(), NumberOfBaseTokens) << BaseTokens;
+  ASSERT_EQ(ConstrainedTokens.size(),
+            NumberOfBaseTokens + NumberOfAdditionalRequiresClauseTokens)
+      << ConstrainedTokens;
+
+  for (auto I = 0u; I < NumberOfBaseTokens; ++I) {
+    if (I < NumberOfTokensBeforeRequires) {
+      EXPECT_EQ(*BaseTokens[I], *ConstrainedTokens[I]) << I;
+    } else {
+      EXPECT_EQ(*BaseTokens[I],
+                *ConstrainedTokens[I + NumberOfAdditionalRequiresClauseTokens])
+          << I;
+    }
+  }
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsAsm) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -999,7 +999,8 @@
         FormatToken *Prev = Tok->getPreviousNonComment();
         if (!Prev)
           break;
-        if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept)) {
+        if (Prev->isOneOf(tok::r_paren, tok::kw_noexcept) ||
+            Prev->ClosesRequiresClause) {
           Tok->setType(TT_CtorInitializerColon);
         } else if (Prev->is(tok::kw_try)) {
           // Member initializer list within function try block.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129940.445599.patch
Type: text/x-patch
Size: 3439 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220718/ab6287a4/attachment-0001.bin>


More information about the cfe-commits mailing list