Author: Ding Fei Date: 2024-07-26T09:34:56+08:00 New Revision: 978c40b4482c7f87a5d087ade85cee53089146d4
URL: https://github.com/llvm/llvm-project/commit/978c40b4482c7f87a5d087ade85cee53089146d4 DIFF: https://github.com/llvm/llvm-project/commit/978c40b4482c7f87a5d087ade85cee53089146d4.diff LOG: [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 0a6233a68c7b575d05bca0f0c708b7e97cc710d1 already did this. I think these tests are still useful for regressions. Added: Modified: clang/unittests/AST/ASTImporterTest.cpp clang/unittests/AST/StructuralEquivalenceTest.cpp Removed: ################################################################################ diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 6d987cc7e9ec6..9b12caa37cf79 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -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); + + // diff erent 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); diff --git a/clang/unittests/AST/StructuralEquivalenceTest.cpp b/clang/unittests/AST/StructuralEquivalenceTest.cpp index 952c83be0cb64..e994086c99d04 100644 --- a/clang/unittests/AST/StructuralEquivalenceTest.cpp +++ b/clang/unittests/AST/StructuralEquivalenceTest.cpp @@ -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) { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits