balazske updated this revision to Diff 501514. balazske added a comment. Add a test.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D144273/new/ https://reviews.llvm.org/D144273 Files: clang/lib/AST/ASTImporterLookupTable.cpp clang/test/Import/cxx-valist/Inputs/I1.cpp clang/test/Import/cxx-valist/Inputs/I2.cpp clang/test/Import/cxx-valist/test.cpp clang/unittests/AST/ASTImporterTest.cpp Index: clang/unittests/AST/ASTImporterTest.cpp =================================================================== --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -8378,6 +8378,42 @@ EXPECT_TRUE(ToCtx.hasSameType(ToInjTypedef, ToInjParmVar)); } +TEST_P(ASTImporterOptionSpecificTestBase, VaListC) { + Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_C99); + + auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_C99); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match( + ToAST->getASTContext().getTranslationUnitDecl(), + typedefDecl(hasName("__builtin_va_list"))); + + ASSERT_TRUE(ToAST->getASTContext().hasSameType( + ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType())); +} + +TEST_P(ASTImporterOptionSpecificTestBase, VaListCpp) { + Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_CXX03); + + auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_CXX03); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match( + ToAST->getASTContext().getTranslationUnitDecl(), + typedefDecl(hasName("__builtin_va_list"))); + + ASSERT_TRUE(ToAST->getASTContext().hasSameType( + ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType())); +} + INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions); Index: clang/test/Import/cxx-valist/test.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/test.cpp @@ -0,0 +1,5 @@ +// RUN: clang-import-test -import %S/Inputs/I1.cpp -import %S/Inputs/I2.cpp -expression %s + +void expr() { + use_new(std); +} Index: clang/test/Import/cxx-valist/Inputs/I2.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/Inputs/I2.cpp @@ -0,0 +1 @@ +int std = 17; Index: clang/test/Import/cxx-valist/Inputs/I1.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/Inputs/I1.cpp @@ -0,0 +1,3 @@ +int *use_new(int N) { + return new int [N]; +} Index: clang/lib/AST/ASTImporterLookupTable.cpp =================================================================== --- clang/lib/AST/ASTImporterLookupTable.cpp +++ clang/lib/AST/ASTImporterLookupTable.cpp @@ -87,6 +87,18 @@ ASTImporterLookupTable::ASTImporterLookupTable(TranslationUnitDecl &TU) { Builder B(*this); B.TraverseDecl(&TU); + // The VaList declaration may be created on demand only or not traversed. + // To ensure it is present and found during import, add it to the table now. + if (auto *D = + dyn_cast_or_null<NamedDecl>(TU.getASTContext().getVaListTagDecl())) { + // On some platforms (AArch64) the VaList declaration can be inside a 'std' + // namespace. This is handled specially and not visible by AST traversal. + // ASTImporter must be able to find this namespace to import the VaList + // declaration (and the namespace) correctly. + if (auto *Ns = dyn_cast<NamespaceDecl>(D->getDeclContext())) + add(&TU, Ns); + add(D->getDeclContext(), D); + } } void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) {
Index: clang/unittests/AST/ASTImporterTest.cpp =================================================================== --- clang/unittests/AST/ASTImporterTest.cpp +++ clang/unittests/AST/ASTImporterTest.cpp @@ -8378,6 +8378,42 @@ EXPECT_TRUE(ToCtx.hasSameType(ToInjTypedef, ToInjParmVar)); } +TEST_P(ASTImporterOptionSpecificTestBase, VaListC) { + Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_C99); + + auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_C99); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match( + ToAST->getASTContext().getTranslationUnitDecl(), + typedefDecl(hasName("__builtin_va_list"))); + + ASSERT_TRUE(ToAST->getASTContext().hasSameType( + ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType())); +} + +TEST_P(ASTImporterOptionSpecificTestBase, VaListCpp) { + Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_CXX03); + + auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_CXX03); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match( + ToAST->getASTContext().getTranslationUnitDecl(), + typedefDecl(hasName("__builtin_va_list"))); + + ASSERT_TRUE(ToAST->getASTContext().hasSameType( + ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType())); +} + INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions); Index: clang/test/Import/cxx-valist/test.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/test.cpp @@ -0,0 +1,5 @@ +// RUN: clang-import-test -import %S/Inputs/I1.cpp -import %S/Inputs/I2.cpp -expression %s + +void expr() { + use_new(std); +} Index: clang/test/Import/cxx-valist/Inputs/I2.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/Inputs/I2.cpp @@ -0,0 +1 @@ +int std = 17; Index: clang/test/Import/cxx-valist/Inputs/I1.cpp =================================================================== --- /dev/null +++ clang/test/Import/cxx-valist/Inputs/I1.cpp @@ -0,0 +1,3 @@ +int *use_new(int N) { + return new int [N]; +} Index: clang/lib/AST/ASTImporterLookupTable.cpp =================================================================== --- clang/lib/AST/ASTImporterLookupTable.cpp +++ clang/lib/AST/ASTImporterLookupTable.cpp @@ -87,6 +87,18 @@ ASTImporterLookupTable::ASTImporterLookupTable(TranslationUnitDecl &TU) { Builder B(*this); B.TraverseDecl(&TU); + // The VaList declaration may be created on demand only or not traversed. + // To ensure it is present and found during import, add it to the table now. + if (auto *D = + dyn_cast_or_null<NamedDecl>(TU.getASTContext().getVaListTagDecl())) { + // On some platforms (AArch64) the VaList declaration can be inside a 'std' + // namespace. This is handled specially and not visible by AST traversal. + // ASTImporter must be able to find this namespace to import the VaList + // declaration (and the namespace) correctly. + if (auto *Ns = dyn_cast<NamespaceDecl>(D->getDeclContext())) + add(&TU, Ns); + add(D->getDeclContext(), D); + } } void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits