Skip to content

Commit 638c673

Browse files
committed
[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
1 parent 5371968 commit 638c673

File tree

9 files changed

+37
-20
lines changed

9 files changed

+37
-20
lines changed

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ class HeaderSearch {
560560
///
561561
/// \param ModuleName The name of the module we're looking for.
562562
///
563+
/// \param ImportLoc Location of the module include/import.
564+
///
563565
/// \param AllowSearch Whether we are allowed to search in the various
564566
/// search directories to produce a module definition. If not, this lookup
565567
/// will only return an already-known module.
@@ -568,7 +570,8 @@ class HeaderSearch {
568570
/// in subdirectories.
569571
///
570572
/// \returns The module with the given name.
571-
Module *lookupModule(StringRef ModuleName, bool AllowSearch = true,
573+
Module *lookupModule(StringRef ModuleName, SourceLocation ImportLoc,
574+
bool AllowSearch = true,
572575
bool AllowExtraModuleMapSearch = false);
573576

574577
/// Try to find a module map file in the given directory, returning
@@ -638,11 +641,14 @@ class HeaderSearch {
638641
/// but for compatibility with some buggy frameworks, additional attempts
639642
/// may be made to find the module under a related-but-different search-name.
640643
///
644+
/// \param ImportLoc Location of the module include/import.
645+
///
641646
/// \param AllowExtraModuleMapSearch Whether we allow to search modulemaps
642647
/// in subdirectories.
643648
///
644649
/// \returns The module named ModuleName.
645650
Module *lookupModule(StringRef ModuleName, StringRef SearchName,
651+
SourceLocation ImportLoc,
646652
bool AllowExtraModuleMapSearch = false);
647653

648654
/// Retrieve the name of the (to-be-)cached module file that should

clang/lib/Frontend/CompilerInstance.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,7 +1770,8 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
17701770
SourceLocation ModuleNameLoc, bool IsInclusionDirective) {
17711771
// Search for a module with the given name.
17721772
HeaderSearch &HS = PP->getHeaderSearchInfo();
1773-
Module *M = HS.lookupModule(ModuleName, true, !IsInclusionDirective);
1773+
Module *M =
1774+
HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
17741775

17751776
// Select the source and filename for loading the named module.
17761777
std::string ModuleFilename;
@@ -1829,7 +1830,7 @@ ModuleLoadResult CompilerInstance::findOrCompileModuleAndReadAST(
18291830

18301831
// A prebuilt module is indexed as a ModuleFile; the Module does not exist
18311832
// until the first call to ReadAST. Look it up now.
1832-
M = HS.lookupModule(ModuleName, true, !IsInclusionDirective);
1833+
M = HS.lookupModule(ModuleName, ImportLoc, true, !IsInclusionDirective);
18331834

18341835
// Check whether M refers to the file in the prebuilt module path.
18351836
if (M && M->getASTFile())
@@ -1952,7 +1953,7 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
19521953
} else if (ModuleName == getLangOpts().CurrentModule) {
19531954
// This is the module we're building.
19541955
Module = PP->getHeaderSearchInfo().lookupModule(
1955-
ModuleName, /*AllowSearch*/ true,
1956+
ModuleName, ImportLoc, /*AllowSearch*/ true,
19561957
/*AllowExtraModuleMapSearch*/ !IsInclusionDirective);
19571958
/// FIXME: perhaps we should (a) look for a module using the module name
19581959
// to file map (PrebuiltModuleFiles) and (b) diagnose if still not found?
@@ -2001,8 +2002,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
20012002
PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
20022003
PrivPath.push_back(std::make_pair(&II, Path[0].second));
20032004

2004-
if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, true,
2005-
!IsInclusionDirective))
2005+
if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc,
2006+
true, !IsInclusionDirective))
20062007
Sub =
20072008
loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
20082009
if (Sub) {

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ void FrontendAction::setCurrentInput(const FrontendInputFile &CurrentInput,
144144
Module *FrontendAction::getCurrentModule() const {
145145
CompilerInstance &CI = getCompilerInstance();
146146
return CI.getPreprocessor().getHeaderSearchInfo().lookupModule(
147-
CI.getLangOpts().CurrentModule, /*AllowSearch*/false);
147+
CI.getLangOpts().CurrentModule, SourceLocation(), /*AllowSearch*/false);
148148
}
149149

150150
std::unique_ptr<ASTConsumer>
@@ -472,7 +472,7 @@ static Module *prepareToBuildModule(CompilerInstance &CI,
472472

473473
// Dig out the module definition.
474474
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
475-
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
475+
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule, SourceLocation(),
476476
/*AllowSearch=*/true);
477477
if (!M) {
478478
CI.getDiagnostics().Report(diag::err_missing_module)
@@ -630,7 +630,8 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
630630
if (Kind.getFormat() == InputKind::ModuleMap) {
631631
Module *ASTModule =
632632
AST->getPreprocessor().getHeaderSearchInfo().lookupModule(
633-
AST->getLangOpts().CurrentModule, /*AllowSearch*/ false);
633+
AST->getLangOpts().CurrentModule, SourceLocation(),
634+
/*AllowSearch*/ false);
634635
assert(ASTModule && "module file does not define its own module");
635636
Input = FrontendInputFile(ASTModule->PresumedModuleMapFile, Kind);
636637
} else {

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,17 @@ std::string HeaderSearch::getCachedModuleFileNameImpl(StringRef ModuleName,
229229
return Result.str().str();
230230
}
231231

232-
Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch,
232+
Module *HeaderSearch::lookupModule(StringRef ModuleName,
233+
SourceLocation ImportLoc, bool AllowSearch,
233234
bool AllowExtraModuleMapSearch) {
234235
// Look in the module map to determine if there is a module by this name.
235236
Module *Module = ModMap.findModule(ModuleName);
236237
if (Module || !AllowSearch || !HSOpts->ImplicitModuleMaps)
237238
return Module;
238239

239240
StringRef SearchName = ModuleName;
240-
Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
241+
Module = lookupModule(ModuleName, SearchName, ImportLoc,
242+
AllowExtraModuleMapSearch);
241243

242244
// The facility for "private modules" -- adjacent, optional module maps named
243245
// module.private.modulemap that are supposed to define private submodules --
@@ -248,13 +250,16 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, bool AllowSearch,
248250
// could force building unwanted dependencies into the parent module and cause
249251
// dependency cycles.
250252
if (!Module && SearchName.consume_back("_Private"))
251-
Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
253+
Module = lookupModule(ModuleName, SearchName, ImportLoc,
254+
AllowExtraModuleMapSearch);
252255
if (!Module && SearchName.consume_back("Private"))
253-
Module = lookupModule(ModuleName, SearchName, AllowExtraModuleMapSearch);
256+
Module = lookupModule(ModuleName, SearchName, ImportLoc,
257+
AllowExtraModuleMapSearch);
254258
return Module;
255259
}
256260

257261
Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
262+
SourceLocation ImportLoc,
258263
bool AllowExtraModuleMapSearch) {
259264
Module *Module = nullptr;
260265

clang/lib/Lex/PPDirectives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ Module *Preprocessor::getModuleForLocation(SourceLocation Loc) {
745745
// to the current module, if there is one.
746746
return getLangOpts().CurrentModule.empty()
747747
? nullptr
748-
: HeaderInfo.lookupModule(getLangOpts().CurrentModule);
748+
: HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc);
749749
}
750750

751751
const FileEntry *

clang/lib/Lex/Pragma.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ struct PragmaModuleBeginHandler : public PragmaHandler {
17521752
// Find the module we're entering. We require that a module map for it
17531753
// be loaded or implicitly loadable.
17541754
auto &HSI = PP.getHeaderSearchInfo();
1755-
Module *M = HSI.lookupModule(Current);
1755+
Module *M = HSI.lookupModule(Current, ModuleName.front().second);
17561756
if (!M) {
17571757
PP.Diag(ModuleName.front().second,
17581758
diag::err_pp_module_begin_no_module_map) << Current;

clang/lib/Lex/Preprocessor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ Module *Preprocessor::getCurrentModule() {
518518
if (!getLangOpts().isCompilingModule())
519519
return nullptr;
520520

521-
return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
521+
return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule,
522+
SourceLocation());
522523
}
523524

524525
//===----------------------------------------------------------------------===//

clang/lib/Serialization/ASTReader.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,8 @@ static Module *getTopImportImplicitModule(ModuleManager &ModuleMgr,
556556
StringRef ModuleName = TopImport->ModuleName;
557557
assert(!ModuleName.empty() && "diagnostic options read before module name");
558558

559-
Module *M = PP.getHeaderSearchInfo().lookupModule(ModuleName);
559+
Module *M =
560+
PP.getHeaderSearchInfo().lookupModule(ModuleName, TopImport->ImportLoc);
560561
assert(M && "missing module");
561562
return M;
562563
}
@@ -2923,7 +2924,7 @@ ASTReader::ReadControlBlock(ModuleFile &F,
29232924
// If we've already loaded a module map file covering this module, we may
29242925
// have a better path for it (relative to the current build).
29252926
Module *M = PP.getHeaderSearchInfo().lookupModule(
2926-
F.ModuleName, /*AllowSearch*/ true,
2927+
F.ModuleName, F.ImportLoc, /*AllowSearch*/ true,
29272928
/*AllowExtraModuleMapSearch*/ true);
29282929
if (M && M->Directory) {
29292930
// If we're implicitly loading a module, the base directory can't
@@ -3909,7 +3910,8 @@ ASTReader::ReadModuleMapFileBlock(RecordData &Record, ModuleFile &F,
39093910
if (F.Kind == MK_ImplicitModule && ModuleMgr.begin()->Kind != MK_MainFile) {
39103911
// An implicitly-loaded module file should have its module listed in some
39113912
// module map file that we've already loaded.
3912-
Module *M = PP.getHeaderSearchInfo().lookupModule(F.ModuleName);
3913+
Module *M =
3914+
PP.getHeaderSearchInfo().lookupModule(F.ModuleName, F.ImportLoc);
39133915
auto &Map = PP.getHeaderSearchInfo().getModuleMap();
39143916
const FileEntry *ModMap = M ? Map.getModuleMapFileForUniquing(M) : nullptr;
39153917
// Don't emit module relocation error if we have -fno-validate-pch

clang/lib/Serialization/GeneratePCH.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
5050
Module *Module = nullptr;
5151
if (PP.getLangOpts().isCompilingModule()) {
5252
Module = PP.getHeaderSearchInfo().lookupModule(
53-
PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
53+
PP.getLangOpts().CurrentModule, SourceLocation(),
54+
/*AllowSearch*/ false);
5455
if (!Module) {
5556
assert(hasErrors && "emitting module but current module doesn't exist");
5657
return;

0 commit comments

Comments
 (0)