From 638c673a8c3e27b6532049ac17ac9bc96640fc45 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Tue, 12 Oct 2021 09:04:37 +0200 Subject: [PATCH] [clang][modules] NFC: Propagate import `SourceLocation` into `HeaderSearch::lookupModule` This patch propagates the import `SourceLocation` into `HeaderSearch::lookupModule`. This enables remarks on search path usage (implemented in D102923) to point to the source code that initiated header search. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D111557 --- clang/include/clang/Lex/HeaderSearch.h | 8 +++++++- clang/lib/Frontend/CompilerInstance.cpp | 11 ++++++----- clang/lib/Frontend/FrontendAction.cpp | 7 ++++--- clang/lib/Lex/HeaderSearch.cpp | 13 +++++++++---- clang/lib/Lex/PPDirectives.cpp | 2 +- clang/lib/Lex/Pragma.cpp | 2 +- clang/lib/Lex/Preprocessor.cpp | 3 ++- clang/lib/Serialization/ASTReader.cpp | 8 +++++--- clang/lib/Serialization/GeneratePCH.cpp | 3 ++- 9 files changed, 37 insertions(+), 20 deletions(-) diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h index 7df1127f4273..7b19e0da87ec 100644 --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -560,6 +560,8 @@ public: /// /// \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 @@ public: /// 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 @@ private: /// 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 index a9c756616356..3e0a0f312c55 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -1770,7 +1770,8 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST( 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 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST( // 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 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, } 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 @@ CompilerInstance::loadModule(SourceLocation ImportLoc, 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 index 0bb62a96e02c..6b0a55d79039 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -144,7 +144,7 @@ void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput, 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 @@ static Module *prepareToBuildModule(CompilerInstance &CI, // 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 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, 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 index 8bf61a2ba597..539e0d880fe3 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -229,7 +229,8 @@ std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName, 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 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch, 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 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch, // 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 index e77b3898e0df..425d68cc7a41 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -745,7 +745,7 @@ Module *Preprocessor::getModuleForLocation(SourceLocation Loc) { // 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 index 3cbe16e58017..67daa5841983 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -1752,7 +1752,7 @@ struct PragmaModuleBeginHandler : public PragmaHandler { // 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 index bf0b85e038f0..5aa831aa4eda 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -518,7 +518,8 @@ Module *Preprocessor::getCurrentModule() { 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 index b015ca10dc3a..ebb58d623348 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -556,7 +556,8 @@ static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr, 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 @@ ASTReader::ReadControlBlock(ModuleFile &F, // 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 @@ ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F, 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 index d869796b82c1..6ec5c42e8b82 100644 --- a/clang/lib/Serialization/GeneratePCH.cpp +++ b/clang/lib/Serialization/GeneratePCH.cpp @@ -50,7 +50,8 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { 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;