-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[clang][ASTImporter] Improve structural equivalence of overloadable operators. #72242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2252,6 +2252,176 @@ TEST_F(StructuralEquivalenceStmtTest, UnaryOperatorDifferentOps) { | |
EXPECT_FALSE(testStructuralMatch(t)); | ||
} | ||
|
||
TEST_F(StructuralEquivalenceStmtTest, | ||
CXXOperatorCallExprVsUnaryBinaryOperator) { | ||
auto t = makeNamedDecls( | ||
R"( | ||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo( | ||
A<T, x + y>, | ||
A<T, x - y>, | ||
A<T, -x>, | ||
A<T, x * y>, | ||
A<T, *x>, | ||
A<T, x / y>, | ||
A<T, x % y>, | ||
A<T, x ^ y>, | ||
A<T, x & y>, | ||
A<T, &x>, | ||
A<T, x | y>, | ||
A<T, ~x>, | ||
A<T, !x>, | ||
A<T, x < y>, | ||
A<T, (x > y)>, | ||
A<T, x << y>, | ||
A<T, (x >> y)>, | ||
A<T, x == y>, | ||
A<T, x != y>, | ||
A<T, x <= y>, | ||
A<T, x >= y>, | ||
A<T, x <=> y>, | ||
A<T, x && y>, | ||
A<T, x || y>, | ||
A<T, ++x>, | ||
A<T, --x>, | ||
A<T, (x , y)>, | ||
A<T, x ->* y>, | ||
A<T, x -> y> | ||
); | ||
)", | ||
R"( | ||
struct Bar { | ||
Bar& operator=(Bar&); | ||
Bar& operator->(); | ||
}; | ||
|
||
Bar& operator+(Bar&, Bar&); | ||
Bar& operator+(Bar&); | ||
Bar& operator-(Bar&, Bar&); | ||
Bar& operator-(Bar&); | ||
Bar& operator*(Bar&, Bar&); | ||
Bar& operator*(Bar&); | ||
Bar& operator/(Bar&, Bar&); | ||
Bar& operator%(Bar&, Bar&); | ||
Bar& operator^(Bar&, Bar&); | ||
Bar& operator&(Bar&, Bar&); | ||
Bar& operator&(Bar&); | ||
Bar& operator|(Bar&, Bar&); | ||
Bar& operator~(Bar&); | ||
Bar& operator!(Bar&); | ||
Bar& operator<(Bar&, Bar&); | ||
Bar& operator>(Bar&, Bar&); | ||
Bar& operator+=(Bar&, Bar&); | ||
Bar& operator-=(Bar&, Bar&); | ||
Bar& operator*=(Bar&, Bar&); | ||
Bar& operator/=(Bar&, Bar&); | ||
Bar& operator%=(Bar&, Bar&); | ||
Bar& operator^=(Bar&, Bar&); | ||
Bar& operator&=(Bar&, Bar&); | ||
Bar& operator|=(Bar&, Bar&); | ||
Bar& operator<<(Bar&, Bar&); | ||
Bar& operator>>(Bar&, Bar&); | ||
Bar& operator<<=(Bar&, Bar&); | ||
Bar& operator>>=(Bar&, Bar&); | ||
Bar& operator==(Bar&, Bar&); | ||
Bar& operator!=(Bar&, Bar&); | ||
Bar& operator<=(Bar&, Bar&); | ||
Bar& operator>=(Bar&, Bar&); | ||
Bar& operator<=>(Bar&, Bar&); | ||
Bar& operator&&(Bar&, Bar&); | ||
Bar& operator||(Bar&, Bar&); | ||
Bar& operator++(Bar&); | ||
Bar& operator--(Bar&); | ||
Bar& operator,(Bar&, Bar&); | ||
Bar& operator->*(Bar&, Bar&); | ||
|
||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo( | ||
A<T, x + y>, | ||
A<T, x - y>, | ||
A<T, -x>, | ||
A<T, x * y>, | ||
A<T, *x>, | ||
A<T, x / y>, | ||
A<T, x % y>, | ||
A<T, x ^ y>, | ||
A<T, x & y>, | ||
A<T, &x>, | ||
A<T, x | y>, | ||
A<T, ~x>, | ||
A<T, !x>, | ||
A<T, x < y>, | ||
A<T, (x > y)>, | ||
A<T, x << y>, | ||
A<T, (x >> y)>, | ||
A<T, x == y>, | ||
A<T, x != y>, | ||
A<T, x <= y>, | ||
A<T, x >= y>, | ||
A<T, x <=> y>, | ||
A<T, x && y>, | ||
A<T, x || y>, | ||
A<T, ++x>, | ||
A<T, --x>, | ||
A<T, (x , y)>, | ||
A<T, x ->* y>, | ||
A<T, x -> y> | ||
); | ||
)", | ||
Lang_CXX20); | ||
EXPECT_TRUE(testStructuralMatch(t)); | ||
} | ||
|
||
TEST_F(StructuralEquivalenceStmtTest, | ||
CXXOperatorCallExprVsUnaryBinaryOperatorNe) { | ||
auto t = makeNamedDecls( | ||
R"( | ||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo( | ||
A<T, x + y> | ||
); | ||
)", | ||
R"( | ||
struct Bar; | ||
|
||
Bar& operator+(Bar&, Bar&); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the presence of this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not know exactly what was the purpose, but likely it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code is now updated to |
||
|
||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo( | ||
A<T, x - y> | ||
); | ||
)", | ||
Lang_CXX11); | ||
EXPECT_FALSE(testStructuralMatch(t)); | ||
} | ||
|
||
TEST_F(StructuralEquivalenceStmtTest, NonTypeTemplateParm) { | ||
auto t = makeNamedDecls( | ||
R"( | ||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo(A<T, x>); | ||
)", | ||
R"( | ||
template <typename T, T x> | ||
class A; | ||
template <typename T, T x, T y> | ||
void foo(A<T, y>); | ||
)", | ||
Lang_CXX11); | ||
// FIXME: These should not match, | ||
EXPECT_TRUE(testStructuralMatch(t)); | ||
} | ||
|
||
TEST_F(StructuralEquivalenceStmtTest, UnresolvedLookupDifferentName) { | ||
auto t = makeStmts( | ||
R"( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you perhaps elaborate this sentence a bit? Do I understand it correctly that e.g. in
CXXOperatorCallExprVsUnaryBinaryOperator
the presence ofstruct Bar
in the second TU ensured that all the operators are parsed asCXXOperatorCall
s, while in the first TU they are parsed as unary/binary operator expressions?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea was coming from this post:
https://discourse.llvm.org/t/different-ast-translations-for-the-same-body-why/22741
The presence of an operator declaration is needed to change the parsed AST from
BinaryOperator
toCXXOverloadedOperatorCallExpr
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the link, it's really helpful. Perhaps it should be included in the source code comment as well, to help those who'll work on this code in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the updated comment is clear now.