From 5a93df278914a6f7dc87522ff45d33c95bcc46f9 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 13 Oct 2015 00:23:25 +0000 Subject: [PATCH] [modules] Fix merging of __va_list_tag's implicit special member functions. We model predefined declarations as not being from AST files, but in most ways they act as if they come from some implicit prebuilt module file imported before all others. Therefore, if we see an update to the predefined 'struct __va_list_tag' declaration (and we've already loaded any modules), it needs a corresponding update record, even though it didn't technically come from an AST file. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@250134 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Serialization/ASTWriter.cpp | 37 ++++++++++++++++---- test/Modules/Inputs/va_list/left.h | 7 ++++ test/Modules/Inputs/va_list/module.modulemap | 3 ++ test/Modules/Inputs/va_list/right.h | 7 ++++ test/Modules/Inputs/va_list/top.h | 1 + test/Modules/va_list.cpp | 8 +++++ 6 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 test/Modules/Inputs/va_list/left.h create mode 100644 test/Modules/Inputs/va_list/right.h create mode 100644 test/Modules/Inputs/va_list/top.h create mode 100644 test/Modules/va_list.cpp diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index 95c1620fcb..331fa38590 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -5630,27 +5630,52 @@ void ASTWriter::CompletedTagDefinition(const TagDecl *D) { } } +static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) { + if (D->isFromASTFile()) + return true; + + // If we've not loaded any modules, this can't be imported. + if (!Chain || !Chain->getModuleManager().size()) + return false; + + // The predefined __va_list_tag struct is imported if we imported any decls. + // FIXME: This is a gross hack. + return D == D->getASTContext().getVaListTagDecl(); +} + void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) { // TU and namespaces are handled elsewhere. if (isa(DC) || isa(DC)) return; - if (!(!D->isFromASTFile() && cast(DC)->isFromASTFile())) - return; // Not a source decl added to a DeclContext from PCH. + // We're only interested in cases where a local declaration is added to an + // imported context. + if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast(DC))) + return; assert(DC == DC->getPrimaryContext() && "added to non-primary context"); assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!"); assert(!WritingAST && "Already writing the AST!"); - UpdatedDeclContexts.insert(DC); + if (UpdatedDeclContexts.insert(DC) && !cast(DC)->isFromASTFile()) { + // We're adding a visible declaration to a predefined decl context. Ensure + // that we write out all of its lookup results so we don't get a nasty + // surprise when we try to emit its lookup table. + for (auto *Child : DC->decls()) + UpdatingVisibleDecls.push_back(Child); + } UpdatingVisibleDecls.push_back(D); } void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) { assert(D->isImplicit()); - if (!(!D->isFromASTFile() && RD->isFromASTFile())) - return; // Not a source member added to a class from PCH. + + // We're only interested in cases where a local declaration is added to an + // imported context. + if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD)) + return; + if (!isa(D)) - return; // We are interested in lazily declared implicit methods. + return; // A decl coming from PCH was modified. assert(RD->isCompleteDefinition()); diff --git a/test/Modules/Inputs/va_list/left.h b/test/Modules/Inputs/va_list/left.h new file mode 100644 index 0000000000..6842f9f7d0 --- /dev/null +++ b/test/Modules/Inputs/va_list/left.h @@ -0,0 +1,7 @@ +@import top; + +template +void f(int k, ...) { + va_list va; + __builtin_va_start(va, k); +} diff --git a/test/Modules/Inputs/va_list/module.modulemap b/test/Modules/Inputs/va_list/module.modulemap index 870f38bb0e..bd9c61456f 100644 --- a/test/Modules/Inputs/va_list/module.modulemap +++ b/test/Modules/Inputs/va_list/module.modulemap @@ -1,2 +1,5 @@ module va_list_a { header "va_list_a.h" } module va_list_b { header "va_list_b.h" } +module top { header "top.h" } +module left { header "left.h" } +module right { header "right.h" } diff --git a/test/Modules/Inputs/va_list/right.h b/test/Modules/Inputs/va_list/right.h new file mode 100644 index 0000000000..6842f9f7d0 --- /dev/null +++ b/test/Modules/Inputs/va_list/right.h @@ -0,0 +1,7 @@ +@import top; + +template +void f(int k, ...) { + va_list va; + __builtin_va_start(va, k); +} diff --git a/test/Modules/Inputs/va_list/top.h b/test/Modules/Inputs/va_list/top.h new file mode 100644 index 0000000000..5660b87655 --- /dev/null +++ b/test/Modules/Inputs/va_list/top.h @@ -0,0 +1 @@ +typedef __builtin_va_list va_list; diff --git a/test/Modules/va_list.cpp b/test/Modules/va_list.cpp new file mode 100644 index 0000000000..35694cdec6 --- /dev/null +++ b/test/Modules/va_list.cpp @@ -0,0 +1,8 @@ +// RUN: rm -rf %t +// RUN: %clang_cc1 -x objective-c++ -fmodules-cache-path=%t -fmodules -fimplicit-module-maps -I %S/Inputs/va_list %s -verify +// expected-no-diagnostics + +@import left; +@import right; + +void g(int k, ...) { f(k); } -- 2.40.0