diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -560,6 +560,8 @@ /// /// \param ModuleName The name of the module we're looking for. /// + /// \param ImportLoc Location of the module include/import. + /// /// \param AllowSearch Whether we are allowed to search in the various /// search directories to produce a module definition. If not, this lookup /// will only return an already-known module. @@ -568,7 +570,8 @@ /// in subdirectories. /// /// \returns The module with the given name. - Module *lookupModule(StringRef ModuleName, bool AllowSearch = true, + Module *lookupModule(StringRef ModuleName, SourceLocation ImportLoc, + bool AllowSearch = true, bool AllowExtraModuleMapSearch = false); /// Try to find a module map file in the given directory, returning @@ -638,11 +641,14 @@ /// but for compatibility with some buggy frameworks, additional attempts /// may be made to find the module under a related-but-different search-name. /// + /// \param ImportLoc Location of the module include/import. + /// /// \param AllowExtraModuleMapSearch Whether we allow to search modulemaps /// in subdirectories. /// /// \returns The module named ModuleName. Module *lookupModule(StringRef ModuleName, StringRef SearchName, + SourceLocation ImportLoc, bool AllowExtraModuleMapSearch = false); /// Retrieve the name of the (to-be-)cached module file that should diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1770,7 +1770,8 @@ SourceLocation ModuleNameLoc, bool IsInclusionDirective) { // Search for a module with the given name. HeaderSearch &HS = PP->getHeaderSearchInfo(); - Module *M = HS.lookupModule(ModuleName, true, !IsInclusionDirective); + Module *M = + HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective); // Select the source and filename for loading the named module. std::string ModuleFilename; @@ -1829,7 +1830,7 @@ // A prebuilt module is indexed as a ModuleFile; the Module does not exist // until the first call to ReadAST. Look it up now. - M = HS.lookupModule(ModuleName, true, !IsInclusionDirective); + M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective); // Check whether M refers to the file in the prebuilt module path. if (M && M->getASTFile()) @@ -1952,7 +1953,7 @@ } else if (ModuleName == getLangOpts().CurrentModule) { // This is the module we're building. Module = PP->getHeaderSearchInfo().lookupModule( - ModuleName, /*AllowSearch*/ true, + ModuleName, ImportLoc, /*AllowSearch*/ true, /*AllowExtraModuleMapSearch*/ !IsInclusionDirective); /// FIXME: perhaps we should (a) look for a module using the module name // to file map (PrebuiltModuleFiles) and (b) diagnose if still not found? @@ -2001,8 +2002,8 @@ PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID()); PrivPath.push_back(std::make_pair(&II, Path[0].second)); - if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, true, - !IsInclusionDirective)) + if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, + true, !IsInclusionDirective)) Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective); if (Sub) { diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -144,7 +144,7 @@ Module *FrontendAction::getCurrentModule() const { CompilerInstance &CI = getCompilerInstance(); return CI.getPreprocessor().getHeaderSearchInfo().lookupModule( - CI.getLangOpts().CurrentModule, /*AllowSearch*/false); + CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false); } std::unique_ptr @@ -472,7 +472,7 @@ // Dig out the module definition. HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); - Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, + Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch=*/true); if (!M) { CI.getDiagnostics().Report(diag::err_missing_module) @@ -630,7 +630,8 @@ if (Kind.getFormat() == InputKind::ModuleMap) { Module *ASTModule = AST->getPreprocessor().getHeaderSearchInfo().lookupModule( - AST->getLangOpts().CurrentModule, /*AllowSearch*/ false); + AST->getLangOpts().CurrentModule, SourceLocation(), + /*AllowSearch*/ false); assert(ASTModule && "module file does not define its own module"); Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind); } else { diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -229,7 +229,8 @@ return Result.str().str(); } -Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch, +Module *HeaderSearch::lookupModule(StringRef ModuleName, + SourceLocation ImportLoc, bool AllowSearch, bool AllowExtraModuleMapSearch) { // Look in the module map to determine if there is a module by this name. Module *Module = ModMap.findModule(ModuleName); @@ -237,7 +238,8 @@ return Module; StringRef SearchName = ModuleName; - Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch); + Module = lookupModule(ModuleName, SearchName, ImportLoc, + AllowExtraModuleMapSearch); // The facility for "private modules" -- adjacent, optional module maps named // module.private.modulemap that are supposed to define private submodules -- @@ -248,13 +250,16 @@ // could force building unwanted dependencies into the parent module and cause // dependency cycles. if (!Module && SearchName.consume_back("_Private")) - Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch); + Module = lookupModule(ModuleName, SearchName, ImportLoc, + AllowExtraModuleMapSearch); if (!Module && SearchName.consume_back("Private")) - Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch); + Module = lookupModule(ModuleName, SearchName, ImportLoc, + AllowExtraModuleMapSearch); return Module; } Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName, + SourceLocation ImportLoc, bool AllowExtraModuleMapSearch) { Module *Module = nullptr; diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -745,7 +745,7 @@ // to the current module, if there is one. return getLangOpts().CurrentModule.empty() ? nullptr - : HeaderInfo.lookupModule(getLangOpts().CurrentModule); + : HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc); } const FileEntry * diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -1752,7 +1752,7 @@ // Find the module we're entering. We require that a module map for it // be loaded or implicitly loadable. auto &HSI = PP.getHeaderSearchInfo(); - Module *M = HSI.lookupModule(Current); + Module *M = HSI.lookupModule(Current, ModuleName.front().second); if (!M) { PP.Diag(ModuleName.front().second, diag::err_pp_module_begin_no_module_map) << Current; diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -518,7 +518,8 @@ if (!getLangOpts().isCompilingModule()) return nullptr; - return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); + return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule, + SourceLocation()); } //===----------------------------------------------------------------------===// diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -556,7 +556,8 @@ StringRef ModuleName = TopImport->ModuleName; assert(!ModuleName.empty() && "diagnostic options read before module name"); - Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName); + Module *M = + PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc); assert(M && "missing module"); return M; } @@ -2923,7 +2924,7 @@ // If we've already loaded a module map file covering this module, we may // have a better path for it (relative to the current build). Module *M = PP.getHeaderSearchInfo().lookupModule( - F.ModuleName, /*AllowSearch*/ true, + F.ModuleName, F.ImportLoc, /*AllowSearch*/ true, /*AllowExtraModuleMapSearch*/ true); if (M && M->Directory) { // If we're implicitly loading a module, the base directory can't @@ -3909,7 +3910,8 @@ if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) { // An implicitly-loaded module file should have its module listed in some // module map file that we've already loaded. - Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName); + Module *M = + PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc); auto &Map = PP.getHeaderSearchInfo().getModuleMap(); const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr; // Don't emit module relocation error if we have -fno-validate-pch diff --git a/clang/lib/Serialization/GeneratePCH.cpp b/clang/lib/Serialization/GeneratePCH.cpp --- a/clang/lib/Serialization/GeneratePCH.cpp +++ b/clang/lib/Serialization/GeneratePCH.cpp @@ -50,7 +50,8 @@ Module *Module = nullptr; if (PP.getLangOpts().isCompilingModule()) { Module = PP.getHeaderSearchInfo().lookupModule( - PP.getLangOpts().CurrentModule, /*AllowSearch*/ false); + PP.getLangOpts().CurrentModule, SourceLocation(), + /*AllowSearch*/ false); if (!Module) { assert(hasErrors && "emitting module but current module doesn't exist"); return;