diff --git a/clang/lib/AST/ASTImporterLookupTable.cpp b/clang/lib/AST/ASTImporterLookupTable.cpp --- a/clang/lib/AST/ASTImporterLookupTable.cpp +++ b/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(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(D->getDeclContext())) + add(&TU, Ns); + add(D->getDeclContext(), D); + } } void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) { diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -8137,6 +8137,42 @@ EXPECT_FALSE(SharedStatePtr->isNewDecl(ToBar)); } +TEST_P(ASTImporterOptionSpecificTestBase, VaListC) { + Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_C99); + + auto *FromVaList = FirstDeclMatcher().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_C99); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher().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().match( + FromTU, typedefDecl(hasName("va_list"))); + ASSERT_TRUE(FromVaList); + + auto *ToVaList = Import(FromVaList, Lang_CXX03); + ASSERT_TRUE(ToVaList); + + auto *ToBuiltinVaList = FirstDeclMatcher().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);