Skip to content

Commit 29a4ed8

Browse files
committed
[clang][ASTImporter] Add VaList declaration to lookup table.
The declaration of __builtin_va_list seems to be handled specially by ASTContext and/or Sema. The normal AST traversal probably can not find it, therefore it is not added to ASTImporterLookupTable. If it is not added, errors can occur because a duplicated VaList declaration is created at import. To fix the problem the VaList declaration is added manually to ASTImporterLookupTable at construction. In special cases this declaration is inside a "invisible" 'std' namespace that behaves the same way. This namespace must be added to the table too. Reviewed By: vabridgers, donat.nagy Differential Revision: https://reviews.llvm.org/D144273
1 parent a048df8 commit 29a4ed8

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

clang/lib/AST/ASTImporterLookupTable.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,18 @@ struct Builder : RecursiveASTVisitor<Builder> {
8787
ASTImporterLookupTable::ASTImporterLookupTable(TranslationUnitDecl &TU) {
8888
Builder B(*this);
8989
B.TraverseDecl(&TU);
90+
// The VaList declaration may be created on demand only or not traversed.
91+
// To ensure it is present and found during import, add it to the table now.
92+
if (auto *D =
93+
dyn_cast_or_null<NamedDecl>(TU.getASTContext().getVaListTagDecl())) {
94+
// On some platforms (AArch64) the VaList declaration can be inside a 'std'
95+
// namespace. This is handled specially and not visible by AST traversal.
96+
// ASTImporter must be able to find this namespace to import the VaList
97+
// declaration (and the namespace) correctly.
98+
if (auto *Ns = dyn_cast<NamespaceDecl>(D->getDeclContext()))
99+
add(&TU, Ns);
100+
add(D->getDeclContext(), D);
101+
}
90102
}
91103

92104
void ASTImporterLookupTable::add(DeclContext *DC, NamedDecl *ND) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int f(int N) {
2+
return N;
3+
}

clang/test/Import/cxx-valist/test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: clang-import-test -import %S/Inputs/I1.cpp -expression %s
2+
3+
void expr() {
4+
f(0);
5+
}
6+
7+
int std = 17;

clang/unittests/AST/ASTImporterTest.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8438,6 +8438,42 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplateName) {
84388438
EXPECT_EQ(Templ1, Templ2);
84398439
}
84408440

8441+
TEST_P(ASTImporterOptionSpecificTestBase, VaListC) {
8442+
Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_C99);
8443+
8444+
auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match(
8445+
FromTU, typedefDecl(hasName("va_list")));
8446+
ASSERT_TRUE(FromVaList);
8447+
8448+
auto *ToVaList = Import(FromVaList, Lang_C99);
8449+
ASSERT_TRUE(ToVaList);
8450+
8451+
auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match(
8452+
ToAST->getASTContext().getTranslationUnitDecl(),
8453+
typedefDecl(hasName("__builtin_va_list")));
8454+
8455+
ASSERT_TRUE(ToAST->getASTContext().hasSameType(
8456+
ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType()));
8457+
}
8458+
8459+
TEST_P(ASTImporterOptionSpecificTestBase, VaListCpp) {
8460+
Decl *FromTU = getTuDecl(R"(typedef __builtin_va_list va_list;)", Lang_CXX03);
8461+
8462+
auto *FromVaList = FirstDeclMatcher<TypedefDecl>().match(
8463+
FromTU, typedefDecl(hasName("va_list")));
8464+
ASSERT_TRUE(FromVaList);
8465+
8466+
auto *ToVaList = Import(FromVaList, Lang_CXX03);
8467+
ASSERT_TRUE(ToVaList);
8468+
8469+
auto *ToBuiltinVaList = FirstDeclMatcher<TypedefDecl>().match(
8470+
ToAST->getASTContext().getTranslationUnitDecl(),
8471+
typedefDecl(hasName("__builtin_va_list")));
8472+
8473+
ASSERT_TRUE(ToAST->getASTContext().hasSameType(
8474+
ToVaList->getUnderlyingType(), ToBuiltinVaList->getUnderlyingType()));
8475+
}
8476+
84418477
INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest,
84428478
DefaultTestValuesForRunOptions);
84438479

0 commit comments

Comments
 (0)