Author: rsmith Date: Thu Sep 1 15:15:25 2016 New Revision: 280409 URL: http://llvm.org/viewvc/llvm-project?rev=280409&view=rev Log: When we reach the end of a #include of a header of a local submodule that we textually included, create an ImportDecl just as we would if we reached a #include of any other modular header. This is necessary in order to correctly determine the set of variables to initialize for an imported module.
This should hopefully make the modules selfhost buildbot green again. Added: cfe/trunk/test/Modules/global-init.cpp Modified: cfe/trunk/include/clang/Sema/Sema.h cfe/trunk/lib/Sema/SemaDecl.cpp cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp Modified: cfe/trunk/include/clang/Sema/Sema.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=280409&r1=280408&r2=280409&view=diff ============================================================================== --- cfe/trunk/include/clang/Sema/Sema.h (original) +++ cfe/trunk/include/clang/Sema/Sema.h Thu Sep 1 15:15:25 2016 @@ -1884,6 +1884,7 @@ public: /// \brief The parser has processed a module import translated from a /// #include or similar preprocessing directive. void ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod); + void BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod); /// \brief The parsed has entered a submodule. void ActOnModuleBegin(SourceLocation DirectiveLoc, Module *Mod); Modified: cfe/trunk/lib/Sema/SemaDecl.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=280409&r1=280408&r2=280409&view=diff ============================================================================== --- cfe/trunk/lib/Sema/SemaDecl.cpp (original) +++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu Sep 1 15:15:25 2016 @@ -15312,7 +15312,10 @@ DeclResult Sema::ActOnModuleImport(Sourc void Sema::ActOnModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext, true); + BuildModuleInclude(DirectiveLoc, Mod); +} +void Sema::BuildModuleInclude(SourceLocation DirectiveLoc, Module *Mod) { // Determine whether we're in the #include buffer for a module. The #includes // in that buffer do not qualify as module imports; they're just an // implementation detail of us building the module. @@ -15352,20 +15355,27 @@ void Sema::ActOnModuleBegin(SourceLocati VisibleModules.setVisible(Mod, DirectiveLoc); } -void Sema::ActOnModuleEnd(SourceLocation DirectiveLoc, Module *Mod) { - checkModuleImportContext(*this, Mod, DirectiveLoc, CurContext); +void Sema::ActOnModuleEnd(SourceLocation EofLoc, Module *Mod) { + checkModuleImportContext(*this, Mod, EofLoc, CurContext); if (getLangOpts().ModulesLocalVisibility) { - assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod && - "left the wrong module scope"); VisibleModules = std::move(ModuleScopes.back().OuterVisibleModules); - ModuleScopes.pop_back(); - - VisibleModules.setVisible(Mod, DirectiveLoc); // Leaving a module hides namespace names, so our visible namespace cache // is now out of date. VisibleNamespaceCache.clear(); } + + assert(!ModuleScopes.empty() && ModuleScopes.back().Module == Mod && + "left the wrong module scope"); + ModuleScopes.pop_back(); + + // We got to the end of processing a #include of a local module. Create an + // ImportDecl as we would for an imported module. + FileID File = getSourceManager().getFileID(EofLoc); + assert(File != getSourceManager().getMainFileID() && + "end of submodule in main source file"); + SourceLocation DirectiveLoc = getSourceManager().getIncludeLoc(File); + BuildModuleInclude(DirectiveLoc, Mod); } void Sema::createImplicitModuleImportForErrorRecovery(SourceLocation Loc, Added: cfe/trunk/test/Modules/global-init.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/global-init.cpp?rev=280409&view=auto ============================================================================== --- cfe/trunk/test/Modules/global-init.cpp (added) +++ cfe/trunk/test/Modules/global-init.cpp Thu Sep 1 15:15:25 2016 @@ -0,0 +1,19 @@ +// RUN: rm -rf %t +// RUN: mkdir %t +// +// RUN: echo '#pragma once' > %t/a.h +// RUN: echo 'struct A { A() {} int f() const; } const a;' >> %t/a.h +// +// RUN: echo '#include "a.h"' > %t/b.h +// +// RUN: echo 'module M { module b { header "b.h" export * } module a { header "a.h" export * } }' > %t/map +// +// RUN: %clang_cc1 -fmodules -fmodules-cache-path=%t -fmodule-map-file=%t/map -I%t %s -emit-llvm -o - -triple %itanium_abi_triple | FileCheck %s + +#include "b.h" + +// CHECK: @_ZL1a = internal global +// CHECK: call {{.*}} @_ZN1AC1Ev({{.*}}@_ZL1a +// CHECK: call {{.*}} @_ZNK1A1fEv({{.*}}@_ZL1a +// CHECK: store {{.*}} @x +int x = a.f(); Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp?rev=280409&r1=280408&r2=280409&view=diff ============================================================================== --- cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp (original) +++ cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp Thu Sep 1 15:15:25 2016 @@ -478,6 +478,14 @@ void CXIndexDataConsumer::importedModule if (!Mod) return; + // If the imported module is part of the top-level module that we're + // indexing, it doesn't correspond to an imported AST file. + // FIXME: This assumes that AST files and top-level modules directly + // correspond, which is unlikely to remain true forever. + if (Module *SrcMod = ImportD->getImportedOwningModule()) + if (SrcMod->getTopLevelModule() == Mod->getTopLevelModule()) + return; + CXIdxImportedASTFileInfo Info = { static_cast<CXFile>( const_cast<FileEntry *>(Mod->getASTFile())), _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits