Skip to content

Commit 9512d6d

Browse files
authored
[clang-format] Fix parsing of operator<() {} (#75144)
Fixes #74876. During the parsing of `operator<(Foo&) {}`, there was no handling for the operator<, so it called `consumeToken()` again, causing the `AnnotationParser::Scopes` to have one additional left brace each time it tried to parse it, leaving it unbalanced. Because of this, in the following code: ```cpp class Foo { void operator<(Foo&) {} Foo& f; }; ``` The `&` in the reference member, was being interpreted as `TT_BinaryOperator` instead of `TT_PointerOrReference`.
1 parent ddd13b6 commit 9512d6d

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ class AnnotatingParser {
164164
TT_OverloadedOperatorLParen))) {
165165
return false;
166166
}
167+
if (Previous.Previous->is(tok::kw_operator) &&
168+
CurrentToken->is(tok::l_paren)) {
169+
return false;
170+
}
167171
}
168172

169173
FormatToken *Left = CurrentToken->Previous;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,17 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
298298
ASSERT_EQ(Tokens.size(), 12u) << Tokens;
299299
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_TypeName);
300300
EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
301+
302+
Tokens = annotate("class Foo {\n"
303+
" void operator<() {}\n"
304+
" Foo &f;\n"
305+
"};");
306+
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
307+
EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
308+
EXPECT_TOKEN(Tokens[5], tok::less, TT_OverloadedOperator);
309+
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
310+
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
311+
EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
301312
}
302313

303314
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {

0 commit comments

Comments
 (0)