[clang] [clang][ASTImport] fix issue on anonymous enum import (PR #93923)

Qizhi Hu via cfe-commits cfe-commits at lists.llvm.org
Fri May 31 09:52:28 PDT 2024


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

>From 7e602ae8d3c7ca0c3218d8ce9106510c43b6295b Mon Sep 17 00:00:00 2001
From: huqizhi <huqizhi at feysh.com>
Date: Fri, 31 May 2024 13:12:41 +0800
Subject: [PATCH 1/2] [clang][ASTImport] fix issue on anonymous enum import

---
 clang/lib/AST/ASTImporter.cpp           |  2 +-
 clang/unittests/AST/ASTImporterTest.cpp | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index cab5ee6047956..3b9080e09b331 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -2929,7 +2929,7 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
 
   // We may already have an enum of the same name; try to find and match it.
   EnumDecl *PrevDecl = nullptr;
-  if (!DC->isFunctionOrMethod() && SearchName) {
+  if (!DC->isFunctionOrMethod()) {
     SmallVector<NamedDecl *, 4> ConflictingDecls;
     auto FoundDecls =
         Importer.findDeclsInToCtx(DC, SearchName);
diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 3dc1c336365d1..4cee0b75653a5 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -9674,6 +9674,27 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportInstantiatedFromMember) {
   EXPECT_TRUE(ImportedPartialSpecialization->getInstantiatedFromMember());
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, ImportAnonymousEnum) {
+  const char *ToCode =
+      R"(
+      struct A {
+        enum { E1,E2} x;
+      };
+      )";
+  (void)getToTuDecl(ToCode, Lang_CXX11);
+  const char *Code =
+      R"(
+      struct A {
+        enum { E1,E2} x;
+      };
+      )";
+  Decl *FromTU = getTuDecl(Code, Lang_CXX11);
+  auto *FromE = FirstDeclMatcher<EnumDecl>().match(FromTU, enumDecl());
+  auto *ToE = Import(FromE, Lang_CXX11);
+  ASSERT_TRUE(ToE);
+  EXPECT_FALSE(ToE->enumerators().empty());
+}
+
 INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
                          DefaultTestValuesForRunOptions);
 

>From a32f4f879ef4e080b192ad6a4ee1528591ce2136 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744285 at qq.com>
Date: Sat, 1 Jun 2024 00:52:01 +0800
Subject: [PATCH 2/2] update testcase

---
 clang/unittests/AST/ASTImporterTest.cpp | 27 ++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 4cee0b75653a5..dcf30d7ec1a6f 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -9674,25 +9674,38 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportInstantiatedFromMember) {
   EXPECT_TRUE(ImportedPartialSpecialization->getInstantiatedFromMember());
 }
 
+AST_MATCHER_P(EnumDecl, hasEnumConstName, StringRef, ConstName) {
+  for (EnumConstantDecl *D : Node.enumerators())
+    if (D->getName() == ConstName)
+      return true;
+  return false;
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, ImportAnonymousEnum) {
   const char *ToCode =
       R"(
       struct A {
-        enum { E1,E2} x;
+        enum { E1, E2} x;
+        enum { E3, E4} y;
       };
       )";
-  (void)getToTuDecl(ToCode, Lang_CXX11);
+  Decl *ToTu = getToTuDecl(ToCode, Lang_CXX11);
+  auto *ToE = FirstDeclMatcher<EnumDecl>().match(
+      ToTu, enumDecl(hasEnumConstName("E1")));
   const char *Code =
       R"(
       struct A {
-        enum { E1,E2} x;
+        enum { E1, E2} x;
+        enum { E3, E4} y;
       };
       )";
   Decl *FromTU = getTuDecl(Code, Lang_CXX11);
-  auto *FromE = FirstDeclMatcher<EnumDecl>().match(FromTU, enumDecl());
-  auto *ToE = Import(FromE, Lang_CXX11);
-  ASSERT_TRUE(ToE);
-  EXPECT_FALSE(ToE->enumerators().empty());
+  auto *FromE = FirstDeclMatcher<EnumDecl>().match(
+      FromTU, enumDecl(hasEnumConstName("E1")));
+  auto *ImportedE = Import(FromE, Lang_CXX11);
+  ASSERT_TRUE(ImportedE);
+  EXPECT_EQ(ImportedE, ToE);
+  EXPECT_FALSE(ImportedE->enumerators().empty());
 }
 
 INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,



More information about the cfe-commits mailing list