[PATCH] D153798: [clang-format] Correctly annotate operator free function call
Emilia Kond via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 26 11:54:32 PDT 2023
rymiel created this revision.
rymiel added reviewers: HazardyKnusperkeks, owenpan, MyDeveloperDay.
Herald added projects: All, clang, clang-format.
Herald added a subscriber: cfe-commits.
rymiel requested review of this revision.
The annotator correctly annotates an overloaded operator call when
called as a member function, like `x.operator+(y)`, however, when called
as a free function, like `operator+(x, y)`, the annotator assumed it was
an overloaded operator function *declaration*, instead of a call.
This patch allows for a free function call to correctly be annotated as
a call, but only if the current like cannot be a declaration, usually
within the bodies of a function.
Fixes https://github.com/llvm/llvm-project/issues/49973
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D153798
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
@@ -657,6 +657,33 @@
EXPECT_TOKEN(Tokens[2], tok::l_paren, TT_OverloadedOperator);
EXPECT_TOKEN(Tokens[3], tok::r_paren, TT_OverloadedOperator);
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_OverloadedOperatorLParen);
+
+ Tokens = annotate("class Foo {\n"
+ " int operator+(a* b);\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 14u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::kw_operator, TT_FunctionDeclarationName);
+ EXPECT_TOKEN(Tokens[5], tok::plus, TT_OverloadedOperator);
+ EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
+ EXPECT_TOKEN(Tokens[8], tok::star, TT_PointerOrReference);
+
+ Tokens = annotate("void foo() {\n"
+ " operator+(a * b);\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 15u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::plus, TT_OverloadedOperator);
+ EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_OverloadedOperatorLParen);
+ EXPECT_TOKEN(Tokens[9], tok::star, TT_BinaryOperator);
+
+ Tokens = annotate("class Foo {\n"
+ " void foo() {\n"
+ " operator+(a * b);\n"
+ " }\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 19u) << Tokens;
+ EXPECT_TOKEN(Tokens[9], tok::plus, TT_OverloadedOperator);
+ EXPECT_TOKEN(Tokens[10], tok::l_paren, TT_OverloadedOperatorLParen);
+ EXPECT_TOKEN(Tokens[12], tok::star, TT_BinaryOperator);
}
TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -310,8 +310,13 @@
// If faced with "a.operator*(argument)" or "a->operator*(argument)",
// i.e. the operator is called as a member function,
// then the argument must be an expression.
+ // If faced with "operator+(argument)", i.e. the operator is called as
+ // a free function, then the argument is an expression only if the current
+ // line can't be a declaration.
bool OperatorCalledAsMemberFunction =
- Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow);
+ (Prev->Previous &&
+ Prev->Previous->isOneOf(tok::period, tok::arrow)) ||
+ (!Line.MustBeDeclaration && !Line.InMacroBody);
Contexts.back().IsExpression = OperatorCalledAsMemberFunction;
} else if (OpeningParen.is(TT_VerilogInstancePortLParen)) {
Contexts.back().IsExpression = true;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153798.534683.patch
Type: text/x-patch
Size: 2757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230626/61e902a0/attachment.bin>
More information about the cfe-commits
mailing list