[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
This commit is contained in:
Jan Svoboda 2021-10-12 09:04:37 +02:00
parent 5371968e48
commit 638c673a8c
9 changed files with 37 additions and 20 deletions

View File

@ -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

View File

@ -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) {

View File

@ -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<ASTConsumer>
@ -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 {

View File

@ -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;

View File

@ -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 *

View File

@ -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;

View File

@ -518,7 +518,8 @@ Module *Preprocessor::getCurrentModule() {
if (!getLangOpts().isCompilingModule())
return nullptr;
return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule,
SourceLocation());
}
//===----------------------------------------------------------------------===//

View File

@ -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

View File

@ -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;