[clang] [clang-format] Make some binary operations imply requires clause (PR #110942)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Oct 2 17:18:48 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Emilia Kond (rymiel)
<details>
<summary>Changes</summary>
This patch adjusts the requires clause/expression lookahead heuristic to assume that some binary operation mean that the requires keyword refers to a clause.
This partially reverts the removed case clauses from 9b68c095d6b52d6ec0390c653528f65c42e5f570, with an additional check that we're not in a type parameter.
This is quite a band-aid fix, it may be worth it to rewrite that whole heuristic to track more state in the future, instead of just blindly marching forward across multiple unrelated definitions, since right now, the definition following the one with the requires clause can influence whether the heuristic chooses clause or expression.
Fixes https://github.com/llvm/llvm-project/issues/110485
---
Full diff: https://github.com/llvm/llvm-project/pull/110942.diff
2 Files Affected:
- (modified) clang/lib/Format/UnwrappedLineParser.cpp (+11)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+15)
``````````diff
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 40f77266fabdca..98078fb50dcfaa 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3528,6 +3528,17 @@ bool UnwrappedLineParser::parseRequires() {
return false;
}
break;
+ case tok::equalequal:
+ case tok::greaterequal:
+ case tok::lessequal:
+ case tok::r_paren:
+ case tok::pipepipe:
+ if (OpenAngles == 0) {
+ FormatTok = Tokens->setPosition(StoredPosition);
+ parseRequiresClause(RequiresToken);
+ return true;
+ }
+ break;
case tok::eof:
// Break out of the loop.
Lookahead = 50;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1884d41a5f23f5..6f1b1df0bda655 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1296,6 +1296,21 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
Tokens = annotate("bool x = t && requires(Foo<C1 || C2> x) { x.foo(); };");
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
+
+ // Second function definition is required due to lookahead
+ Tokens = annotate("void f() & requires(n == 1) {}\nvoid g();");
+ ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
+
+ Tokens = annotate("void f() & requires(n || h) {}\nvoid g();");
+ ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
+ EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
+
+ Tokens = annotate("bool x = t && requires(F<n == 1> x) { x.foo(); };");
+ ASSERT_EQ(Tokens.size(), 25u) << Tokens;
+ EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
}
TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/110942
More information about the cfe-commits
mailing list