Skip to content

Commit aea60ab

Browse files
authored
[clang-format] Make bitwise and imply requires clause (#110942)
This patch adjusts the requires clause/expression parser to imply a requires clause if it is preceded by a bitwise and operator `&`, and assume it is a reference qualifier. The justification is that bitwise operations should not be used for requires expressions. This is a band-aid fix. The real problems lie in the lookahead heuristic in the same method. 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 #110485
1 parent 11c8188 commit aea60ab

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3484,10 +3484,10 @@ bool UnwrappedLineParser::parseRequires() {
34843484
case tok::r_paren:
34853485
case tok::kw_noexcept:
34863486
case tok::kw_const:
3487+
case tok::amp:
34873488
// This is a requires clause.
34883489
parseRequiresClause(RequiresToken);
34893490
return true;
3490-
case tok::amp:
34913491
case tok::ampamp: {
34923492
// This can be either:
34933493
// if (... && requires (T t) ...)

clang/unittests/Format/TokenAnnotatorTest.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1318,6 +1318,15 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) {
13181318
Tokens = annotate("bool x = t && requires(Foo<C1 || C2> x) { x.foo(); };");
13191319
ASSERT_EQ(Tokens.size(), 25u) << Tokens;
13201320
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresExpression);
1321+
1322+
// Second function definition is required due to lookahead
1323+
Tokens = annotate("void f() &\n"
1324+
" requires(n == 1)\n"
1325+
"{}\n"
1326+
"void g();");
1327+
ASSERT_EQ(Tokens.size(), 19u) << Tokens;
1328+
EXPECT_TOKEN(Tokens[4], tok::amp, TT_PointerOrReference);
1329+
EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause);
13211330
}
13221331

13231332
TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {

0 commit comments

Comments
 (0)