[clang] [ASTImport]improve ast comparation (PR #66110)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 12 10:53:48 PDT 2023


https://github.com/jcsxky updated https://github.com/llvm/llvm-project/pull/66110:

>From 98a32795dae4587f64c10f91c6ab47f96881ad4b Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Wed, 13 Sep 2023 00:03:27 +0800
Subject: [PATCH] improve ast comparation     1.VarDecl should not be ignored. 
    2.GotoStmt has no children, it should be handle explicitly.

---
 clang/lib/AST/ASTStructuralEquivalence.cpp    | 26 ++++++++
 .../AST/StructuralEquivalenceTest.cpp         | 59 ++++++++++++++++++-
 2 files changed, 83 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ASTStructuralEquivalence.cpp b/clang/lib/AST/ASTStructuralEquivalence.cpp
index 544420234ef0eb0..74dc44664587b9e 100644
--- a/clang/lib/AST/ASTStructuralEquivalence.cpp
+++ b/clang/lib/AST/ASTStructuralEquivalence.cpp
@@ -277,6 +277,17 @@ class StmtComparer {
 
   bool IsStmtEquivalent(const Stmt *S1, const Stmt *S2) { return true; }
 
+  bool IsStmtEquivalent(const GotoStmt *S1, const GotoStmt *S2) {
+    auto *L1 = S1->getLabel();
+    auto *L2 = S2->getLabel();
+    if (!L1 || !L2) {
+      return false;
+    }
+    IdentifierInfo *Name1 = L1->getIdentifier();
+    IdentifierInfo *Name2 = L2->getIdentifier();
+    return ::IsStructurallyEquivalent(Name1, Name2);
+  }
+
   bool IsStmtEquivalent(const SourceLocExpr *E1, const SourceLocExpr *E2) {
     return E1->getIdentKind() == E2->getIdentKind();
   }
@@ -1295,6 +1306,21 @@ static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
   return true;
 }
 
+static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
+                                     VarDecl *D1, VarDecl *D2) {
+  IdentifierInfo *Name1 = D1->getIdentifier();
+  IdentifierInfo *Name2 = D2->getIdentifier();
+  if (!::IsStructurallyEquivalent(Name1, Name2)) {
+    return false;
+  }
+
+  if (!IsStructurallyEquivalent(Context, D1->getType(), D2->getType())) {
+    return false;
+  }
+
+  return IsStructurallyEquivalent(Context, D1->getInit(), D2->getInit());
+}
+
 static bool IsStructurallyEquivalent(StructuralEquivalenceContext &Context,
                                      FieldDecl *Field1, FieldDecl *Field2,
                                      QualType Owner2Type) {
diff --git a/clang/unittests/AST/StructuralEquivalenceTest.cpp b/clang/unittests/AST/StructuralEquivalenceTest.cpp
index 4e9f476659b9ee6..29b1c95987eaa6d 100644
--- a/clang/unittests/AST/StructuralEquivalenceTest.cpp
+++ b/clang/unittests/AST/StructuralEquivalenceTest.cpp
@@ -1,5 +1,6 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/ASTStructuralEquivalence.h"
+#include "clang/AST/Decl.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Frontend/ASTUnit.h"
@@ -1801,10 +1802,10 @@ TEST_F(StructuralEquivalenceCacheTest, SimpleNonEq) {
 TEST_F(StructuralEquivalenceCacheTest, ReturnStmtNonEq) {
   auto TU = makeTuDecls(
       R"(
-      bool x(){ return true; }
+      bool x() { return true; }
       )",
       R"(
-      bool x(){ return false; }
+      bool x() { return false; }
       )",
       Lang_CXX03);
 
@@ -1817,6 +1818,42 @@ TEST_F(StructuralEquivalenceCacheTest, ReturnStmtNonEq) {
 
 }
 
+TEST_F(StructuralEquivalenceCacheTest, VarDeclNoEq) {
+  auto TU = makeTuDecls(
+      R"(
+      int *p;
+      )",
+      R"(
+      int *q;
+      )",
+      Lang_CXX03);
+
+  StructuralEquivalenceContext Ctx(
+      get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
+      NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);
+
+  auto X = findDeclPair<VarDecl>(TU, varDecl(hasType(isAnyPointer())));
+  EXPECT_FALSE(Ctx.IsEquivalent(X.first, X.second));
+}
+
+TEST_F(StructuralEquivalenceCacheTest, VarDeclWithInitNoEq) {
+  auto TU = makeTuDecls(
+      R"(
+      int *p = (int*)0;
+      )",
+      R"(
+      int *p = (int*)1;
+      )",
+      Lang_CXX03);
+
+  StructuralEquivalenceContext Ctx(
+      get<0>(TU)->getASTContext(), get<1>(TU)->getASTContext(),
+      NonEquivalentDecls, StructuralEquivalenceKind::Default, false, false);
+
+  auto X = findDeclPair<VarDecl>(TU, varDecl(hasType(isAnyPointer())));
+  EXPECT_FALSE(Ctx.IsEquivalent(X.first, X.second));
+}
+
 TEST_F(StructuralEquivalenceCacheTest, SpecialNonEq) {
   auto TU = makeTuDecls(
       R"(
@@ -2320,5 +2357,23 @@ TEST_F(StructuralEquivalenceStmtTest, UnresolvedLookup) {
   EXPECT_TRUE(testStructuralMatch(t));
 }
 
+TEST_F(StructuralEquivalenceCacheTest, GotoStmtNoEq) {
+  auto t = makeStmts(
+      R"(
+      void foo() {
+        goto L1;
+        L1: foo();
+      }
+      )",
+      R"(
+      void foo() {
+        goto L2;
+        L2: foo();
+      }
+      )",
+      Lang_CXX03, gotoStmt());
+  EXPECT_FALSE(testStructuralMatch(t));
+}
+
 } // end namespace ast_matchers
 } // end namespace clang



More information about the cfe-commits mailing list