Skip to content

Commit 978c40b

Browse files
authored
[clang][ASTImporter][NFC] add unittests for unnamed EnumDecl (#100545)
These tests are for multiple anonymous EnumDecls structural eq test & importing. We found the anonymous enums importing issue a few days ago and tried to fix it but 0a6233a already did this. I think these tests are still useful for regressions.
1 parent 5bf0859 commit 978c40b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9783,6 +9783,43 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExistingEmptyAnonymousEnums) {
97839783
EXPECT_EQ(ImportedE2, ToE1);
97849784
}
97859785

9786+
TEST_P(ASTImporterOptionSpecificTestBase, ImportMultipleAnonymousEnumDecls) {
9787+
Decl *ToTU = getToTuDecl("", Lang_CXX03);
9788+
Decl *FromTU = getTuDecl(
9789+
R"(
9790+
struct foo {
9791+
enum { A };
9792+
enum { B };
9793+
};
9794+
)",
9795+
Lang_CXX03);
9796+
9797+
auto EnumConstA = enumConstantDecl(hasName("A"));
9798+
auto EnumConstB = enumConstantDecl(hasName("B"));
9799+
9800+
auto *FromA = FirstDeclMatcher<EnumConstantDecl>().match(FromTU, EnumConstA);
9801+
auto *FromB = FirstDeclMatcher<EnumConstantDecl>().match(FromTU, EnumConstB);
9802+
9803+
auto *ToA = Import(FromA, Lang_CXX03);
9804+
auto *ToB = Import(FromB, Lang_CXX03);
9805+
9806+
ASSERT_TRUE(ToA);
9807+
ASSERT_TRUE(ToB);
9808+
9809+
auto *ToFooA = FirstDeclMatcher<CXXRecordDecl>().match(
9810+
ToTU, tagDecl(has(enumDecl(has(EnumConstA)))));
9811+
auto *ToFooB = FirstDeclMatcher<CXXRecordDecl>().match(
9812+
ToTU, tagDecl(has(enumDecl(has(EnumConstB)))));
9813+
ASSERT_EQ(ToFooA, ToFooB);
9814+
9815+
// different EnumDecl
9816+
auto *ToEnumDeclA =
9817+
FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(has(EnumConstA)));
9818+
auto *ToEnumDeclB =
9819+
FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(has(EnumConstB)));
9820+
ASSERT_NE(ToEnumDeclA, ToEnumDeclB);
9821+
}
9822+
97869823
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
97879824
DefaultTestValuesForRunOptions);
97889825

clang/unittests/AST/StructuralEquivalenceTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,20 @@ TEST_F(StructuralEquivalenceEnumTest, EnumsWithDifferentBody) {
11091109
EXPECT_FALSE(testStructuralMatch(t));
11101110
}
11111111

1112+
TEST_F(StructuralEquivalenceEnumTest, AnonymousEnumsWithSameConsts) {
1113+
// field x is required to trigger comparison of the anonymous enum
1114+
auto t = makeNamedDecls("struct foo { enum { A } x; };",
1115+
"struct foo { enum { A } x;};", Lang_CXX11);
1116+
EXPECT_TRUE(testStructuralMatch(t));
1117+
}
1118+
1119+
TEST_F(StructuralEquivalenceEnumTest, AnonymousEnumsWithDiffConsts) {
1120+
// field x is required to trigger comparison of the anonymous enum
1121+
auto t = makeNamedDecls("struct foo { enum { A } x; };",
1122+
"struct foo { enum { B } x;};", Lang_CXX11);
1123+
EXPECT_FALSE(testStructuralMatch(t));
1124+
}
1125+
11121126
struct StructuralEquivalenceEnumConstantTest : StructuralEquivalenceTest {};
11131127

11141128
TEST_F(StructuralEquivalenceEnumConstantTest, EnumConstantsWithSameValues) {

0 commit comments

Comments
 (0)