[clang] [clang] fix lack comparison of declRefExpr in ASTStructuralEquivalence (PR #66041)

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 20:10:53 PDT 2023


https://github.com/mzyKi created https://github.com/llvm/llvm-project/pull/66041:

Before fix,the following testcase expected true. While I think only comparison of declName is not sufficient.Thanks for giving suggestions.

>From 1efbe57a8acd2219228014dd7dce6f179ee777c7 Mon Sep 17 00:00:00 2001
From: miaozhiyuan <miaozhiyuan at feysh.com>
Date: Tue, 12 Sep 2023 10:51:35 +0800
Subject: [PATCH] [clang] fix lack comparison of declRefExpr in
 ASTStructuralEquivalence

---
 clang/lib/AST/ASTStructuralEquivalence.cpp        | 8 ++++++++
 clang/unittests/AST/StructuralEquivalenceTest.cpp | 9 +++++++++
 2 files changed, 17 insertions(+)

diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 544420234ef0eb0..f43706998dc471a 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -214,6 +214,14 @@ class StmtComparer {
     return E1->size() == E2->size();
   }
 
+  bool IsStmtEquivalent(const DeclRefExpr *DRE1, const DeclRefExpr *DRE2) {
+    if (nullptr == DRE1->getDecl() || nullptr == DRE2->getDecl()) {
+      return false;
+    }
+    return IsStructurallyEquivalent(Context, DRE1->getDecl()->getDeclName(),
+                                    DRE2->getDecl()->getDeclName());
+  }
+
   bool IsStmtEquivalent(const DependentScopeDeclRefExpr *DE1,
                         const DependentScopeDeclRefExpr *DE2) {
     if (!IsStructurallyEquivalent(Context, DE1->getDeclName(),
diff --git a/clang/unittests/AST/StructuralEquivalenceTest.cpp b/clang/unittests/AST/StructuralEquivalenceTest.cpp
index 4e9f476659b9ee6..5787fc5a6566617 100644
--- a/clang/unittests/AST/StructuralEquivalenceTest.cpp
+++ b/clang/unittests/AST/StructuralEquivalenceTest.cpp
@@ -2320,5 +2320,14 @@ TEST_F(StructuralEquivalenceStmtTest, UnresolvedLookup) {
   EXPECT_TRUE(testStructuralMatch(t));
 }
 
+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));
+}
+
 } // end namespace ast_matchers
 } // end namespace clang



More information about the cfe-commits mailing list