Skip to content

Commit

Permalink
[clang][ASTImporter][NFC] add unittests for unnamed EnumDecl (#100545)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
danix800 authored Jul 26, 2024
1 parent 5bf0859 commit 978c40b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
37 changes: 37 additions & 0 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9783,6 +9783,43 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportExistingEmptyAnonymousEnums) {
EXPECT_EQ(ImportedE2, ToE1);
}

TEST_P(ASTImporterOptionSpecificTestBase, ImportMultipleAnonymousEnumDecls) {
Decl *ToTU = getToTuDecl("", Lang_CXX03);
Decl *FromTU = getTuDecl(
R"(
struct foo {
enum { A };
enum { B };
};
)",
Lang_CXX03);

auto EnumConstA = enumConstantDecl(hasName("A"));
auto EnumConstB = enumConstantDecl(hasName("B"));

auto *FromA = FirstDeclMatcher<EnumConstantDecl>().match(FromTU, EnumConstA);
auto *FromB = FirstDeclMatcher<EnumConstantDecl>().match(FromTU, EnumConstB);

auto *ToA = Import(FromA, Lang_CXX03);
auto *ToB = Import(FromB, Lang_CXX03);

ASSERT_TRUE(ToA);
ASSERT_TRUE(ToB);

auto *ToFooA = FirstDeclMatcher<CXXRecordDecl>().match(
ToTU, tagDecl(has(enumDecl(has(EnumConstA)))));
auto *ToFooB = FirstDeclMatcher<CXXRecordDecl>().match(
ToTU, tagDecl(has(enumDecl(has(EnumConstB)))));
ASSERT_EQ(ToFooA, ToFooB);

// different EnumDecl
auto *ToEnumDeclA =
FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(has(EnumConstA)));
auto *ToEnumDeclB =
FirstDeclMatcher<EnumDecl>().match(ToTU, enumDecl(has(EnumConstB)));
ASSERT_NE(ToEnumDeclA, ToEnumDeclB);
}

INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
DefaultTestValuesForRunOptions);

Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/AST/StructuralEquivalenceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,20 @@ TEST_F(StructuralEquivalenceEnumTest, EnumsWithDifferentBody) {
EXPECT_FALSE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceEnumTest, AnonymousEnumsWithSameConsts) {
// field x is required to trigger comparison of the anonymous enum
auto t = makeNamedDecls("struct foo { enum { A } x; };",
"struct foo { enum { A } x;};", Lang_CXX11);
EXPECT_TRUE(testStructuralMatch(t));
}

TEST_F(StructuralEquivalenceEnumTest, AnonymousEnumsWithDiffConsts) {
// field x is required to trigger comparison of the anonymous enum
auto t = makeNamedDecls("struct foo { enum { A } x; };",
"struct foo { enum { B } x;};", Lang_CXX11);
EXPECT_FALSE(testStructuralMatch(t));
}

struct StructuralEquivalenceEnumConstantTest : StructuralEquivalenceTest {};

TEST_F(StructuralEquivalenceEnumConstantTest, EnumConstantsWithSameValues) {
Expand Down

0 comments on commit 978c40b

Please sign in to comment.