[AST] Fix structural inequivalence of operators

Summary: Operators kind was not checked, so we reported e.g. op- to be equal with op+

Reviewers: shafik, a_sidorin, aaron.ballman

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D57902

llvm-svn: 353504
This commit is contained in:
Gabor Marton 2019-02-08 08:55:32 +00:00
parent ee21a66a7b
commit fc638d64e8
2 changed files with 33 additions and 0 deletions

View File

@ -1650,6 +1650,12 @@ bool StructuralEquivalenceContext::CheckKindSpecificEquivalence(
}
} else if (FunctionDecl *FD1 = dyn_cast<FunctionDecl>(D1)) {
if (FunctionDecl *FD2 = dyn_cast<FunctionDecl>(D2)) {
if (FD1->isOverloadedOperator()) {
if (!FD2->isOverloadedOperator())
return false;
if (FD1->getOverloadedOperator() != FD2->getOverloadedOperator())
return false;
}
if (!::IsStructurallyEquivalent(FD1->getIdentifier(),
FD2->getIdentifier()))
return false;

View File

@ -230,6 +230,33 @@ TEST_F(StructuralEquivalenceFunctionTest, TemplateVsNonTemplate) {
EXPECT_FALSE(testStructuralMatch(t));
}
TEST_F(StructuralEquivalenceFunctionTest, DifferentOperators) {
auto t = makeDecls<FunctionDecl>(
"struct X{}; bool operator<(X, X);",
"struct X{}; bool operator==(X, X);", Lang_CXX,
functionDecl(hasOverloadedOperatorName("<")),
functionDecl(hasOverloadedOperatorName("==")));
EXPECT_FALSE(testStructuralMatch(t));
}
TEST_F(StructuralEquivalenceFunctionTest, SameOperators) {
auto t = makeDecls<FunctionDecl>(
"struct X{}; bool operator<(X, X);",
"struct X{}; bool operator<(X, X);", Lang_CXX,
functionDecl(hasOverloadedOperatorName("<")),
functionDecl(hasOverloadedOperatorName("<")));
EXPECT_TRUE(testStructuralMatch(t));
}
TEST_F(StructuralEquivalenceFunctionTest, CtorVsDtor) {
auto t = makeDecls<FunctionDecl>(
"struct X{ X(); };",
"struct X{ ~X(); };", Lang_CXX,
cxxConstructorDecl(),
cxxDestructorDecl());
EXPECT_FALSE(testStructuralMatch(t));
}
TEST_F(StructuralEquivalenceFunctionTest, ParamConstWithRef) {
auto t = makeNamedDecls("void foo(int&);",
"void foo(const int&);", Lang_CXX);