[clang] [clang-format] Fix requires misannotation with comma (PR #65908)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 10 12:56:14 PDT 2023


llvmbot wrote:

@llvm/pr-subscribers-clang

<details>
<summary>Changes</summary>

clang-format uses a heuristic to determine if a requires() is either a requires clause or requires expression, based on what is in the parentheses. Part of this heuristic assumed that a requires clause can never contain a comma, however this is not the case if said comma is in the template argument of a type.

This patch allows commas to appear in a requires clause if an angle bracket `<` has been opened.

Fixes https://github.com/llvm/llvm-project/issues/65904
--
Full diff: https://github.com/llvm/llvm-project/pull/65908.diff

2 Files Affected:

- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+7-3) 
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+8) 


<pre>
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 0ff0656a92d7222..7af9a0cba221458 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3369,9 +3369,13 @@ bool clang::format::UnwrappedLineParser::parseRequires() {
     case tok::kw_volatile:
     case tok::kw_const:
     case tok::comma:
-      FormatTok = Tokens->setPosition(StoredPosition);
-      parseRequiresExpression(RequiresToken);
-      return false;
+      if (OpenAngles == 0) {
+        FormatTok = Tokens->setPosition(StoredPosition);
+        parseRequiresExpression(RequiresToken);
+        return false;
+      } else {
+        break;
+      }
     case tok::r_paren:
     case tok::pipepipe:
       FormatTok = Tokens->setPosition(StoredPosition);
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 434852983712940..f3cdb8a8b2cc1b4 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1016,6 +1016,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
   ASSERT_EQ(Tokens.size(), 22u) << Tokens;
   EXPECT_TOKEN(Tokens[4], tok::kw_requires, TT_RequiresClause);
   EXPECT_TOKEN(Tokens[10], tok::ampamp, TT_PointerOrReference);
+
+  Tokens = annotate("void f() & requires(true) {}");
+  EXPECT_EQ(Tokens.size(), 12u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
+
+  Tokens = annotate("void f() & requires(C<true, true>) {}");
+  EXPECT_EQ(Tokens.size(), 17u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
</pre>

</details>

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


More information about the cfe-commits mailing list