Skip to content

Commit 3b020d5

Browse files
authored
[clang][ASTImport] fix issue on anonymous enum import (#93923)
Don't skip searching in `ToContext` during importing `EnumDecl`. And `IsStructuralMatch` in `StructralEquivalence` can make sure to determine whether the found result is match or not. --------- Co-authored-by: huqizhi <[email protected]>
1 parent 924611b commit 3b020d5

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

clang/lib/AST/ASTImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2929,7 +2929,7 @@ ExpectedDecl ASTNodeImporter::VisitEnumDecl(EnumDecl *D) {
29292929

29302930
// We may already have an enum of the same name; try to find and match it.
29312931
EnumDecl *PrevDecl = nullptr;
2932-
if (!DC->isFunctionOrMethod() && SearchName) {
2932+
if (!DC->isFunctionOrMethod()) {
29332933
SmallVector<NamedDecl *, 4> ConflictingDecls;
29342934
auto FoundDecls =
29352935
Importer.findDeclsInToCtx(DC, SearchName);

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9674,6 +9674,46 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportInstantiatedFromMember) {
96749674
EXPECT_TRUE(ImportedPartialSpecialization->getInstantiatedFromMember());
96759675
}
96769676

9677+
AST_MATCHER_P(EnumDecl, hasEnumConstName, StringRef, ConstName) {
9678+
for (EnumConstantDecl *D : Node.enumerators())
9679+
if (D->getName() == ConstName)
9680+
return true;
9681+
return false;
9682+
}
9683+
9684+
TEST_P(ASTImporterOptionSpecificTestBase, ImportAnonymousEnum) {
9685+
const char *ToCode =
9686+
R"(
9687+
struct A {
9688+
enum { E1, E2} x;
9689+
enum { E3, E4} y;
9690+
};
9691+
)";
9692+
Decl *ToTU = getToTuDecl(ToCode, Lang_CXX11);
9693+
auto *ToE1 = FirstDeclMatcher<EnumDecl>().match(
9694+
ToTU, enumDecl(hasEnumConstName("E1")));
9695+
auto *ToE3 = FirstDeclMatcher<EnumDecl>().match(
9696+
ToTU, enumDecl(hasEnumConstName("E3")));
9697+
const char *Code =
9698+
R"(
9699+
struct A {
9700+
enum { E1, E2} x;
9701+
enum { E3, E4} y;
9702+
};
9703+
)";
9704+
Decl *FromTU = getTuDecl(Code, Lang_CXX11);
9705+
auto *FromE1 = FirstDeclMatcher<EnumDecl>().match(
9706+
FromTU, enumDecl(hasEnumConstName("E1")));
9707+
auto *ImportedE1 = Import(FromE1, Lang_CXX11);
9708+
ASSERT_TRUE(ImportedE1);
9709+
EXPECT_EQ(ImportedE1, ToE1);
9710+
auto *FromE3 = FirstDeclMatcher<EnumDecl>().match(
9711+
FromTU, enumDecl(hasEnumConstName("E3")));
9712+
auto *ImportedE3 = Import(FromE3, Lang_CXX11);
9713+
ASSERT_TRUE(ImportedE3);
9714+
EXPECT_EQ(ImportedE3, ToE3);
9715+
}
9716+
96779717
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
96789718
DefaultTestValuesForRunOptions);
96799719

0 commit comments

Comments
 (0)