[clang] 29a4ed8 - [clang][ASTImporter] Add VaList declaration to lookup table.

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 6 00:32:55 PST 2023


Author: Balázs Kéri
Date: 2023-03-06T09:32:02+01:00
New Revision: 29a4ed80bb4c8aadc76718924f80a5f532b1ffce

URL: https://github.com/llvm/llvm-project/commit/29a4ed80bb4c8aadc76718924f80a5f532b1ffce
DIFF: https://github.com/llvm/llvm-project/commit/29a4ed80bb4c8aadc76718924f80a5f532b1ffce.diff

LOG: [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

Added: 
    clang/test/Import/cxx-valist/Inputs/I1.cpp
    clang/test/Import/cxx-valist/test.cpp

Modified: 
    clang/lib/AST/ASTImporterLookupTable.cpp
    clang/unittests/AST/ASTImporterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTImporterLookupTable.cpp b/clang/lib/AST/ASTImporterLookupTable.cpp
index a193e97bbf5a0..07d39dcee2583 100644
--- a/clang/lib/AST/ASTImporterLookupTable.cpp
+++ b/clang/lib/AST/ASTImporterLookupTable.cpp
@@ -87,6 +87,18 @@ struct Builder : RecursiveASTVisitor<Builder> {
 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) {

diff  --git a/clang/test/Import/cxx-valist/Inputs/I1.cpp b/clang/test/Import/cxx-valist/Inputs/I1.cpp
new file mode 100644
index 0000000000000..ea1b79e43be65
--- /dev/null
+++ b/clang/test/Import/cxx-valist/Inputs/I1.cpp
@@ -0,0 +1,3 @@
+int f(int N) {
+  return N;
+}

diff  --git a/clang/test/Import/cxx-valist/test.cpp b/clang/test/Import/cxx-valist/test.cpp
new file mode 100644
index 0000000000000..fe011ed8ec17a
--- /dev/null
+++ b/clang/test/Import/cxx-valist/test.cpp
@@ -0,0 +1,7 @@
+// RUN: clang-import-test -import %S/Inputs/I1.cpp -expression %s
+
+void expr() {
+  f(0);
+}
+
+int std = 17;

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 3c9b804a92e13..fecfac4c5914e 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -8438,6 +8438,42 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplateName) {
   EXPECT_EQ(Templ1, Templ2);
 }
 
+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);
 


        


More information about the cfe-commits mailing list