Skip to content

Commit a40db55

Browse files
committed
Lex: Migrate HeaderSearch::LoadedModuleMaps to FileEntryRef
Migrate `HeaderSearch::LoadedModuleMaps` and a number of APIs over to `FileEntryRef`. This should have no functionality change. Note that two `FileEntryRef`s hash the same if they point at the same `FileEntry`. Differential Revision: https://reviews.llvm.org/D92975
1 parent 25067f1 commit a40db55

File tree

6 files changed

+47
-51
lines changed

6 files changed

+47
-51
lines changed

clang-tools-extra/modularize/ModularizeUtilities.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,22 +258,22 @@ std::error_code ModularizeUtilities::loadProblemHeaderList(
258258
std::error_code ModularizeUtilities::loadModuleMap(
259259
llvm::StringRef InputPath) {
260260
// Get file entry for module.modulemap file.
261-
auto ModuleMapEntryOrErr =
262-
SourceMgr->getFileManager().getFile(InputPath);
261+
auto ExpectedModuleMapEntry =
262+
SourceMgr->getFileManager().getFileRef(InputPath);
263263

264264
// return error if not found.
265-
if (!ModuleMapEntryOrErr) {
265+
if (!ExpectedModuleMapEntry) {
266266
llvm::errs() << "error: File \"" << InputPath << "\" not found.\n";
267-
return ModuleMapEntryOrErr.getError();
267+
return errorToErrorCode(ExpectedModuleMapEntry.takeError());
268268
}
269-
const FileEntry *ModuleMapEntry = *ModuleMapEntryOrErr;
269+
FileEntryRef ModuleMapEntry = *ExpectedModuleMapEntry;
270270

271271
// Because the module map parser uses a ForwardingDiagnosticConsumer,
272272
// which doesn't forward the BeginSourceFile call, we do it explicitly here.
273273
DC.BeginSourceFile(*LangOpts, nullptr);
274274

275275
// Figure out the home directory for the module map file.
276-
const DirectoryEntry *Dir = ModuleMapEntry->getDir();
276+
const DirectoryEntry *Dir = ModuleMapEntry.getDir();
277277
StringRef DirName(Dir->getName());
278278
if (llvm::sys::path::filename(DirName) == "Modules") {
279279
DirName = llvm::sys::path::parent_path(DirName);

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class HeaderSearch {
239239

240240
/// Set of module map files we've already loaded, and a flag indicating
241241
/// whether they were valid or not.
242-
llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
242+
llvm::DenseMap<FileEntryRef, bool> LoadedModuleMaps;
243243

244244
/// Uniqued set of framework names, which is used to track which
245245
/// headers were included as framework headers.
@@ -560,8 +560,8 @@ class HeaderSearch {
560560

561561
/// Try to find a module map file in the given directory, returning
562562
/// \c nullptr if none is found.
563-
const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
564-
bool IsFramework);
563+
Optional<FileEntryRef> lookupModuleMapFile(const DirectoryEntry *Dir,
564+
bool IsFramework);
565565

566566
/// Determine whether there is a module map that may map the header
567567
/// with the given file name to a (sub)module.
@@ -603,8 +603,8 @@ class HeaderSearch {
603603
/// used to resolve paths within the module (this is required when
604604
/// building the module from preprocessed source).
605605
/// \returns true if an error occurred, false otherwise.
606-
bool loadModuleMapFile(const FileEntry *File, bool IsSystem,
607-
FileID ID = FileID(), unsigned *Offset = nullptr,
606+
bool loadModuleMapFile(FileEntryRef File, bool IsSystem, FileID ID = FileID(),
607+
unsigned *Offset = nullptr,
608608
StringRef OriginalModuleMapFile = StringRef());
609609

610610
/// Collect the set of all known, top-level modules.
@@ -794,8 +794,7 @@ class HeaderSearch {
794794
LMM_InvalidModuleMap
795795
};
796796

797-
LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
798-
bool IsSystem,
797+
LoadModuleMapResult loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
799798
const DirectoryEntry *Dir,
800799
FileID ID = FileID(),
801800
unsigned *Offset = nullptr);

clang/include/clang/Lex/ModuleMap.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,9 +684,9 @@ class ModuleMap {
684684
/// that caused us to load this module map file, if any.
685685
///
686686
/// \returns true if an error occurred, false otherwise.
687-
bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
688-
const DirectoryEntry *HomeDir,
689-
FileID ID = FileID(), unsigned *Offset = nullptr,
687+
bool parseModuleMapFile(FileEntryRef File, bool IsSystem,
688+
const DirectoryEntry *HomeDir, FileID ID = FileID(),
689+
unsigned *Offset = nullptr,
690690
SourceLocation ExternModuleLoc = SourceLocation());
691691

692692
/// Dump the contents of the module map, for debugging purposes.

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
432432

433433
// Map the current input to a file.
434434
FileID ModuleMapID = SrcMgr.getMainFileID();
435-
const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
435+
Optional<FileEntryRef> ModuleMap = SrcMgr.getFileEntryRefForID(ModuleMapID);
436436

437437
// If the module map is preprocessed, handle the initial line marker;
438438
// line directives are not part of the module map syntax in general.
@@ -445,7 +445,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
445445
}
446446

447447
// Load the module map file.
448-
if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
448+
if (HS.loadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset,
449449
PresumedModuleMapFile))
450450
return true;
451451

@@ -807,7 +807,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
807807

808808
// If we were asked to load any module map files, do so now.
809809
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
810-
if (auto File = CI.getFileManager().getFile(Filename))
810+
if (auto File = CI.getFileManager().getOptionalFileRef(Filename))
811811
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
812812
*File, /*IsSystem*/false);
813813
else

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,22 +1499,20 @@ bool HeaderSearch::findUsableModuleForFrameworkHeader(
14991499
return true;
15001500
}
15011501

1502-
static const FileEntry *getPrivateModuleMap(const FileEntry *File,
1503-
FileManager &FileMgr) {
1504-
StringRef Filename = llvm::sys::path::filename(File->getName());
1505-
SmallString<128> PrivateFilename(File->getDir()->getName());
1502+
static Optional<FileEntryRef> getPrivateModuleMap(FileEntryRef File,
1503+
FileManager &FileMgr) {
1504+
StringRef Filename = llvm::sys::path::filename(File.getName());
1505+
SmallString<128> PrivateFilename(File.getDir().getName());
15061506
if (Filename == "module.map")
15071507
llvm::sys::path::append(PrivateFilename, "module_private.map");
15081508
else if (Filename == "module.modulemap")
15091509
llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
15101510
else
1511-
return nullptr;
1512-
if (auto File = FileMgr.getFile(PrivateFilename))
1513-
return *File;
1514-
return nullptr;
1511+
return None;
1512+
return FileMgr.getOptionalFileRef(PrivateFilename);
15151513
}
15161514

1517-
bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
1515+
bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem,
15181516
FileID ID, unsigned *Offset,
15191517
StringRef OriginalModuleMapFile) {
15201518
// Find the directory for the module. For frameworks, that may require going
@@ -1536,7 +1534,7 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
15361534
Dir = FakeFile->getDir();
15371535
}
15381536
} else {
1539-
Dir = File->getDir();
1537+
Dir = File.getDir();
15401538
}
15411539

15421540
StringRef DirName(Dir->getName());
@@ -1563,11 +1561,9 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
15631561
}
15641562

15651563
HeaderSearch::LoadModuleMapResult
1566-
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1564+
HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
15671565
const DirectoryEntry *Dir, FileID ID,
15681566
unsigned *Offset) {
1569-
assert(File && "expected FileEntry");
1570-
15711567
// Check whether we've already loaded this module map, and mark it as being
15721568
// loaded in case we recursively try to load it from itself.
15731569
auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
@@ -1580,8 +1576,8 @@ HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
15801576
}
15811577

15821578
// Try to load a corresponding private module map.
1583-
if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
1584-
if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
1579+
if (Optional<FileEntryRef> PMMFile = getPrivateModuleMap(File, FileMgr)) {
1580+
if (ModMap.parseModuleMapFile(*PMMFile, IsSystem, Dir)) {
15851581
LoadedModuleMaps[File] = false;
15861582
return LMM_InvalidModuleMap;
15871583
}
@@ -1591,35 +1587,35 @@ HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
15911587
return LMM_NewlyLoaded;
15921588
}
15931589

1594-
const FileEntry *
1590+
Optional<FileEntryRef>
15951591
HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
15961592
if (!HSOpts->ImplicitModuleMaps)
1597-
return nullptr;
1593+
return None;
15981594
// For frameworks, the preferred spelling is Modules/module.modulemap, but
15991595
// module.map at the framework root is also accepted.
16001596
SmallString<128> ModuleMapFileName(Dir->getName());
16011597
if (IsFramework)
16021598
llvm::sys::path::append(ModuleMapFileName, "Modules");
16031599
llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
1604-
if (auto F = FileMgr.getFile(ModuleMapFileName))
1605-
return *F;
1600+
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1601+
return F;
16061602

16071603
// Continue to allow module.map
16081604
ModuleMapFileName = Dir->getName();
16091605
llvm::sys::path::append(ModuleMapFileName, "module.map");
1610-
if (auto F = FileMgr.getFile(ModuleMapFileName))
1611-
return *F;
1606+
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1607+
return F;
16121608

16131609
// For frameworks, allow to have a private module map with a preferred
16141610
// spelling when a public module map is absent.
16151611
if (IsFramework) {
16161612
ModuleMapFileName = Dir->getName();
16171613
llvm::sys::path::append(ModuleMapFileName, "Modules",
16181614
"module.private.modulemap");
1619-
if (auto F = FileMgr.getFile(ModuleMapFileName))
1620-
return *F;
1615+
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
1616+
return F;
16211617
}
1622-
return nullptr;
1618+
return None;
16231619
}
16241620

16251621
Module *HeaderSearch::loadFrameworkModule(StringRef Name,
@@ -1663,9 +1659,10 @@ HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
16631659
if (KnownDir != DirectoryHasModuleMap.end())
16641660
return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
16651661

1666-
if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
1662+
if (Optional<FileEntryRef> ModuleMapFile =
1663+
lookupModuleMapFile(Dir, IsFramework)) {
16671664
LoadModuleMapResult Result =
1668-
loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
1665+
loadModuleMapFileImpl(*ModuleMapFile, IsSystem, Dir);
16691666
// Add Dir explicitly in case ModuleMapFile is in a subdirectory.
16701667
// E.g. Foo.framework/Modules/module.modulemap
16711668
// ^Dir ^ModuleMapFile

clang/lib/Lex/ModuleMap.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,9 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
973973
// We haven't looked here before. Load a module map, if there is
974974
// one.
975975
bool IsFrameworkDir = Parent.endswith(".framework");
976-
if (const FileEntry *ModMapFile =
977-
HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
978-
parseModuleMapFile(ModMapFile, Attrs.IsSystem, *ParentDir);
976+
if (Optional<FileEntryRef> ModMapFile =
977+
HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
978+
parseModuleMapFile(*ModMapFile, Attrs.IsSystem, *ParentDir);
979979
inferred = InferredDirectories.find(*ParentDir);
980980
}
981981

@@ -2163,12 +2163,12 @@ void ModuleMapParser::parseExternModuleDecl() {
21632163
llvm::sys::path::append(ModuleMapFileName, FileName);
21642164
FileNameRef = ModuleMapFileName;
21652165
}
2166-
if (auto File = SourceMgr.getFileManager().getFile(FileNameRef))
2166+
if (auto File = SourceMgr.getFileManager().getOptionalFileRef(FileNameRef))
21672167
Map.parseModuleMapFile(
21682168
*File, /*IsSystem=*/false,
21692169
Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
21702170
? Directory
2171-
: (*File)->getDir(),
2171+
: File->getDir(),
21722172
FileID(), nullptr, ExternLoc);
21732173
}
21742174

@@ -2984,7 +2984,7 @@ bool ModuleMapParser::parseModuleMapFile() {
29842984
} while (true);
29852985
}
29862986

2987-
bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
2987+
bool ModuleMap::parseModuleMapFile(FileEntryRef File, bool IsSystem,
29882988
const DirectoryEntry *Dir, FileID ID,
29892989
unsigned *Offset,
29902990
SourceLocation ExternModuleLoc) {

0 commit comments

Comments
 (0)