[clang] 873308f - [Format] Fix incorrect pointer/reference detection
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 17 01:35:10 PDT 2021
Author: Yilong Guo
Date: 2021-06-17T09:34:06+01:00
New Revision: 873308fd8c96a54f0024251425daac1b78f70119
URL: https://github.com/llvm/llvm-project/commit/873308fd8c96a54f0024251425daac1b78f70119
DIFF: https://github.com/llvm/llvm-project/commit/873308fd8c96a54f0024251425daac1b78f70119.diff
LOG: [Format] Fix incorrect pointer/reference detection
https://llvm.org/PR50568
When an overloaded operator is called, its argument must be an
expression.
Before:
void f() { a.operator()(a *a); }
After:
void f() { a.operator()(a * a); }
Reviewed By: HazardyKnusperkeks, curdeius, MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D103678
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a3d863745297..48309af24aa8 100755
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -229,7 +229,19 @@ class AnnotatingParser {
}
if (Left->is(TT_OverloadedOperatorLParen)) {
- Contexts.back().IsExpression = false;
+ // Find the previous kw_operator token.
+ FormatToken *Prev = Left;
+ while (!Prev->is(tok::kw_operator)) {
+ Prev = Prev->Previous;
+ assert(Prev && "Expect a kw_operator prior to the OperatorLParen!");
+ }
+
+ // 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;
} else if (Style.Language == FormatStyle::LK_JavaScript &&
(Line.startsWith(Keywords.kw_type, tok::identifier) ||
Line.startsWith(tok::kw_export, Keywords.kw_type,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 3df5d23a930f..6a2c0d007589 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -8274,6 +8274,16 @@ TEST_F(FormatTest, UnderstandsOverloadedOperators) {
verifyFormat("using A::operator+;");
verifyFormat("inline A operator^(const A &lhs, const A &rhs) {}\n"
"int i;");
+
+ // Calling an operator as a member function.
+ verifyFormat("void f() { a.operator*(); }");
+ verifyFormat("void f() { a.operator*(b & b); }");
+ verifyFormat("void f() { a->operator&(a * b); }");
+ verifyFormat("void f() { NS::a.operator+(*b * *b); }");
+ // TODO: Calling an operator as a non-member function is hard to distinguish.
+ // https://llvm.org/PR50629
+ // verifyFormat("void f() { operator*(a & a); }");
+ // verifyFormat("void f() { operator&(a, b * b); }");
}
TEST_F(FormatTest, UnderstandsFunctionRefQualification) {
@@ -8766,6 +8776,13 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
"operator()() && {}");
verifyGoogleFormat("template <typename T>\n"
"auto x() & -> int {}");
+
+ // Should be binary operators when used as an argument expression (overloaded
+ // operator invoked as a member function).
+ verifyFormat("void f() { a.operator()(a * a); }");
+ verifyFormat("void f() { a->operator()(a & a); }");
+ verifyFormat("void f() { a.operator()(*a & *a); }");
+ verifyFormat("void f() { a->operator()(*a * *a); }");
}
TEST_F(FormatTest, UnderstandsAttributes) {
More information about the cfe-commits
mailing list