Skip to content

Commit 3542168

Browse files
authored
[clang][AST] fix lack comparison of declRefExpr in ASTStructuralEquivalence (#66041)
Fixed #66047 Before fix,the following testcase expected true. ```cpp TEST_F(StructuralEquivalenceStmtTest, DeclRefENoEq) { std::string Prefix = "enum Test { AAA, BBB };"; auto t = makeStmts( Prefix + "void foo(int i) {if (i > 0) {i = AAA;} else {i = BBB;}}", Prefix + "void foo(int i) {if (i > 0) {i = BBB;} else {i = AAA;}}", Lang_CXX03, ifStmt()); EXPECT_FALSE(testStructuralMatch(t)); // EXPECT_TRUE } ```
1 parent dc925be commit 3542168

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ Bug Fixes in This Version
244244
(`#65156 <https://github.com/llvm/llvm-project/issues/65156>`_)
245245
- Clang no longer considers the loss of ``__unaligned`` qualifier from objects as
246246
an invalid conversion during method function overload resolution.
247+
- Fix lack of comparison of declRefExpr in ASTStructuralEquivalence
248+
(`#66047 <https://github.com/llvm/llvm-project/issues/66047>`_)
247249
- Fix parser crash when dealing with ill-formed objective C++ header code. Fixes
248250
(`#64836 <https://github.com/llvm/llvm-project/issues/64836>`_)
249251
- Clang now allows an ``_Atomic`` qualified integer in a switch statement. Fixes

clang/lib/AST/ASTStructuralEquivalence.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ class StmtComparer {
214214
return E1->size() == E2->size();
215215
}
216216

217+
bool IsStmtEquivalent(const DeclRefExpr *DRE1, const DeclRefExpr *DRE2) {
218+
const ValueDecl *Decl1 = DRE1->getDecl();
219+
const ValueDecl *Decl2 = DRE2->getDecl();
220+
if (!Decl1 || !Decl2)
221+
return false;
222+
return IsStructurallyEquivalent(Context, const_cast<ValueDecl *>(Decl1),
223+
const_cast<ValueDecl *>(Decl2));
224+
}
225+
217226
bool IsStmtEquivalent(const DependentScopeDeclRefExpr *DE1,
218227
const DependentScopeDeclRefExpr *DE2) {
219228
if (!IsStructurallyEquivalent(Context, DE1->getDeclName(),

clang/unittests/AST/StructuralEquivalenceTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,5 +2393,14 @@ TEST_F(StructuralEquivalenceCacheTest, GotoStmtNoEq) {
23932393
EXPECT_FALSE(testStructuralMatch(S));
23942394
}
23952395

2396+
TEST_F(StructuralEquivalenceStmtTest, DeclRefExpr) {
2397+
std::string Prefix = "enum Test { AAA, BBB };";
2398+
auto t = makeStmts(
2399+
Prefix + "void foo(int i) {if (i > 0) {i = AAA;} else {i = BBB;}}",
2400+
Prefix + "void foo(int i) {if (i > 0) {i = BBB;} else {i = AAA;}}",
2401+
Lang_CXX03, ifStmt());
2402+
EXPECT_FALSE(testStructuralMatch(t));
2403+
}
2404+
23962405
} // end namespace ast_matchers
23972406
} // end namespace clang

0 commit comments

Comments
 (0)