From 4986f3f2f220e4fd2fef4b08e550b399c9f45a9f Mon Sep 17 00:00:00 2001 From: Emilia Kond Date: Thu, 29 Jun 2023 19:46:04 +0300 Subject: [PATCH] [clang-format] Correctly annotate operator free function call 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 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay, Nuullll Differential Revision: https://reviews.llvm.org/D153798 --- clang/lib/Format/TokenAnnotator.cpp | 11 ++++++++--- clang/unittests/Format/FormatTest.cpp | 8 ++++++++ clang/unittests/Format/TokenAnnotatorTest.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 27814b8..735089f 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -310,9 +310,14 @@ private: // 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. - bool OperatorCalledAsMemberFunction = - Prev->Previous && Prev->Previous->isOneOf(tok::period, tok::arrow); - Contexts.back().IsExpression = OperatorCalledAsMemberFunction; + // 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 IsOperatorCallSite = + (Prev->Previous && + Prev->Previous->isOneOf(tok::period, tok::arrow)) || + (!Line.MustBeDeclaration && !Line.InMacroBody); + Contexts.back().IsExpression = IsOperatorCallSite; } else if (OpeningParen.is(TT_VerilogInstancePortLParen)) { Contexts.back().IsExpression = true; Contexts.back().ContextType = Context::VerilogInstancePortList; diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8338799..e1e9719 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -11580,6 +11580,14 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { " }", getLLVMStyleWithColumns(50))); +// FIXME: We should be able to figure out this is an operator call +#if 0 + verifyFormat("#define FOO \\\n" + " void foo() { \\\n" + " operator+(a * b); \\\n" + " }", getLLVMStyleWithColumns(25)); +#endif + // FIXME: We cannot handle this case yet; we might be able to figure out that // foo d > v; doesn't make sense. verifyFormat("foo d> v;"); diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index b7980bb..b6fbe73 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -668,6 +668,33 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) { 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) { -- 2.7.4