Revert "Lex: Migrate HeaderSearch::LoadedModuleMaps to FileEntryRef"

This reverts commit a40db5502b.
and follow-up d636b881bb

Somewhat speculative, likely broke check-clang on Windows:
https://reviews.llvm.org/D92975#2453482
This commit is contained in:
Nico Weber 2020-12-14 22:04:41 -05:00
parent c9ede6f336
commit 7799ef7121
7 changed files with 53 additions and 49 deletions

View File

@ -258,22 +258,22 @@ std::error_code ModularizeUtilities::loadProblemHeaderList(
std::error_code ModularizeUtilities::loadModuleMap( std::error_code ModularizeUtilities::loadModuleMap(
llvm::StringRef InputPath) { llvm::StringRef InputPath) {
// Get file entry for module.modulemap file. // Get file entry for module.modulemap file.
auto ExpectedModuleMapEntry = auto ModuleMapEntryOrErr =
SourceMgr->getFileManager().getFileRef(InputPath); SourceMgr->getFileManager().getFile(InputPath);
// return error if not found. // return error if not found.
if (!ExpectedModuleMapEntry) { if (!ModuleMapEntryOrErr) {
llvm::errs() << "error: File \"" << InputPath << "\" not found.\n"; llvm::errs() << "error: File \"" << InputPath << "\" not found.\n";
return errorToErrorCode(ExpectedModuleMapEntry.takeError()); return ModuleMapEntryOrErr.getError();
} }
FileEntryRef ModuleMapEntry = *ExpectedModuleMapEntry; const FileEntry *ModuleMapEntry = *ModuleMapEntryOrErr;
// Because the module map parser uses a ForwardingDiagnosticConsumer, // Because the module map parser uses a ForwardingDiagnosticConsumer,
// which doesn't forward the BeginSourceFile call, we do it explicitly here. // which doesn't forward the BeginSourceFile call, we do it explicitly here.
DC.BeginSourceFile(*LangOpts, nullptr); DC.BeginSourceFile(*LangOpts, nullptr);
// Figure out the home directory for the module map file. // Figure out the home directory for the module map file.
const DirectoryEntry *Dir = ModuleMapEntry.getDir(); const DirectoryEntry *Dir = ModuleMapEntry->getDir();
StringRef DirName(Dir->getName()); StringRef DirName(Dir->getName());
if (llvm::sys::path::filename(DirName) == "Modules") { if (llvm::sys::path::filename(DirName) == "Modules") {
DirName = llvm::sys::path::parent_path(DirName); DirName = llvm::sys::path::parent_path(DirName);

View File

@ -239,7 +239,7 @@ class HeaderSearch {
/// Set of module map files we've already loaded, and a flag indicating /// Set of module map files we've already loaded, and a flag indicating
/// whether they were valid or not. /// whether they were valid or not.
llvm::DenseMap<FileEntryRef, bool> LoadedModuleMaps; llvm::DenseMap<const FileEntry *, bool> LoadedModuleMaps;
/// Uniqued set of framework names, which is used to track which /// Uniqued set of framework names, which is used to track which
/// headers were included as framework headers. /// headers were included as framework headers.
@ -560,8 +560,8 @@ public:
/// Try to find a module map file in the given directory, returning /// Try to find a module map file in the given directory, returning
/// \c nullptr if none is found. /// \c nullptr if none is found.
Optional<FileEntryRef> lookupModuleMapFile(const DirectoryEntry *Dir, const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
bool IsFramework); bool IsFramework);
/// Determine whether there is a module map that may map the header /// Determine whether there is a module map that may map the header
/// with the given file name to a (sub)module. /// with the given file name to a (sub)module.
@ -603,8 +603,8 @@ public:
/// used to resolve paths within the module (this is required when /// used to resolve paths within the module (this is required when
/// building the module from preprocessed source). /// building the module from preprocessed source).
/// \returns true if an error occurred, false otherwise. /// \returns true if an error occurred, false otherwise.
bool loadModuleMapFile(FileEntryRef File, bool IsSystem, FileID ID = FileID(), bool loadModuleMapFile(const FileEntry *File, bool IsSystem,
unsigned *Offset = nullptr, FileID ID = FileID(), unsigned *Offset = nullptr,
StringRef OriginalModuleMapFile = StringRef()); StringRef OriginalModuleMapFile = StringRef());
/// Collect the set of all known, top-level modules. /// Collect the set of all known, top-level modules.
@ -794,7 +794,8 @@ private:
LMM_InvalidModuleMap LMM_InvalidModuleMap
}; };
LoadModuleMapResult loadModuleMapFileImpl(FileEntryRef File, bool IsSystem, LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
bool IsSystem,
const DirectoryEntry *Dir, const DirectoryEntry *Dir,
FileID ID = FileID(), FileID ID = FileID(),
unsigned *Offset = nullptr); unsigned *Offset = nullptr);

View File

@ -684,9 +684,9 @@ public:
/// that caused us to load this module map file, if any. /// that caused us to load this module map file, if any.
/// ///
/// \returns true if an error occurred, false otherwise. /// \returns true if an error occurred, false otherwise.
bool parseModuleMapFile(FileEntryRef File, bool IsSystem, bool parseModuleMapFile(const FileEntry *File, bool IsSystem,
const DirectoryEntry *HomeDir, FileID ID = FileID(), const DirectoryEntry *HomeDir,
unsigned *Offset = nullptr, FileID ID = FileID(), unsigned *Offset = nullptr,
SourceLocation ExternModuleLoc = SourceLocation()); SourceLocation ExternModuleLoc = SourceLocation());
/// Dump the contents of the module map, for debugging purposes. /// Dump the contents of the module map, for debugging purposes.

View File

@ -432,7 +432,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
// Map the current input to a file. // Map the current input to a file.
FileID ModuleMapID = SrcMgr.getMainFileID(); FileID ModuleMapID = SrcMgr.getMainFileID();
Optional<FileEntryRef> ModuleMap = SrcMgr.getFileEntryRefForID(ModuleMapID); const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
// If the module map is preprocessed, handle the initial line marker; // If the module map is preprocessed, handle the initial line marker;
// line directives are not part of the module map syntax in general. // line directives are not part of the module map syntax in general.
@ -445,7 +445,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
} }
// Load the module map file. // Load the module map file.
if (HS.loadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset, if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
PresumedModuleMapFile)) PresumedModuleMapFile))
return true; return true;
@ -807,7 +807,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,
// If we were asked to load any module map files, do so now. // If we were asked to load any module map files, do so now.
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) { for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
if (auto File = CI.getFileManager().getOptionalFileRef(Filename)) if (auto File = CI.getFileManager().getFile(Filename))
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile( CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
*File, /*IsSystem*/false); *File, /*IsSystem*/false);
else else

View File

@ -1499,20 +1499,22 @@ bool HeaderSearch::findUsableModuleForFrameworkHeader(
return true; return true;
} }
static Optional<FileEntryRef> getPrivateModuleMap(FileEntryRef File, static const FileEntry *getPrivateModuleMap(const FileEntry *File,
FileManager &FileMgr) { FileManager &FileMgr) {
StringRef Filename = llvm::sys::path::filename(File.getName()); StringRef Filename = llvm::sys::path::filename(File->getName());
SmallString<128> PrivateFilename(File.getDir().getName()); SmallString<128> PrivateFilename(File->getDir()->getName());
if (Filename == "module.map") if (Filename == "module.map")
llvm::sys::path::append(PrivateFilename, "module_private.map"); llvm::sys::path::append(PrivateFilename, "module_private.map");
else if (Filename == "module.modulemap") else if (Filename == "module.modulemap")
llvm::sys::path::append(PrivateFilename, "module.private.modulemap"); llvm::sys::path::append(PrivateFilename, "module.private.modulemap");
else else
return None; return nullptr;
return FileMgr.getOptionalFileRef(PrivateFilename); if (auto File = FileMgr.getFile(PrivateFilename))
return *File;
return nullptr;
} }
bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem, bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
FileID ID, unsigned *Offset, FileID ID, unsigned *Offset,
StringRef OriginalModuleMapFile) { StringRef OriginalModuleMapFile) {
// Find the directory for the module. For frameworks, that may require going // Find the directory for the module. For frameworks, that may require going
@ -1534,7 +1536,7 @@ bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem,
Dir = FakeFile->getDir(); Dir = FakeFile->getDir();
} }
} else { } else {
Dir = File.getDir(); Dir = File->getDir();
} }
StringRef DirName(Dir->getName()); StringRef DirName(Dir->getName());
@ -1561,9 +1563,11 @@ bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem,
} }
HeaderSearch::LoadModuleMapResult HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem, HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
const DirectoryEntry *Dir, FileID ID, const DirectoryEntry *Dir, FileID ID,
unsigned *Offset) { unsigned *Offset) {
assert(File && "expected FileEntry");
// Check whether we've already loaded this module map, and mark it as being // Check whether we've already loaded this module map, and mark it as being
// loaded in case we recursively try to load it from itself. // loaded in case we recursively try to load it from itself.
auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true)); auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
@ -1576,8 +1580,8 @@ HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
} }
// Try to load a corresponding private module map. // Try to load a corresponding private module map.
if (Optional<FileEntryRef> PMMFile = getPrivateModuleMap(File, FileMgr)) { if (const FileEntry *PMMFile = getPrivateModuleMap(File, FileMgr)) {
if (ModMap.parseModuleMapFile(*PMMFile, IsSystem, Dir)) { if (ModMap.parseModuleMapFile(PMMFile, IsSystem, Dir)) {
LoadedModuleMaps[File] = false; LoadedModuleMaps[File] = false;
return LMM_InvalidModuleMap; return LMM_InvalidModuleMap;
} }
@ -1587,24 +1591,24 @@ HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
return LMM_NewlyLoaded; return LMM_NewlyLoaded;
} }
Optional<FileEntryRef> const FileEntry *
HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) { HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
if (!HSOpts->ImplicitModuleMaps) if (!HSOpts->ImplicitModuleMaps)
return None; return nullptr;
// For frameworks, the preferred spelling is Modules/module.modulemap, but // For frameworks, the preferred spelling is Modules/module.modulemap, but
// module.map at the framework root is also accepted. // module.map at the framework root is also accepted.
SmallString<128> ModuleMapFileName(Dir->getName()); SmallString<128> ModuleMapFileName(Dir->getName());
if (IsFramework) if (IsFramework)
llvm::sys::path::append(ModuleMapFileName, "Modules"); llvm::sys::path::append(ModuleMapFileName, "Modules");
llvm::sys::path::append(ModuleMapFileName, "module.modulemap"); llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName)) if (auto F = FileMgr.getFile(ModuleMapFileName))
return F; return *F;
// Continue to allow module.map // Continue to allow module.map
ModuleMapFileName = Dir->getName(); ModuleMapFileName = Dir->getName();
llvm::sys::path::append(ModuleMapFileName, "module.map"); llvm::sys::path::append(ModuleMapFileName, "module.map");
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName)) if (auto F = FileMgr.getFile(ModuleMapFileName))
return F; return *F;
// For frameworks, allow to have a private module map with a preferred // For frameworks, allow to have a private module map with a preferred
// spelling when a public module map is absent. // spelling when a public module map is absent.
@ -1612,10 +1616,10 @@ HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
ModuleMapFileName = Dir->getName(); ModuleMapFileName = Dir->getName();
llvm::sys::path::append(ModuleMapFileName, "Modules", llvm::sys::path::append(ModuleMapFileName, "Modules",
"module.private.modulemap"); "module.private.modulemap");
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName)) if (auto F = FileMgr.getFile(ModuleMapFileName))
return F; return *F;
} }
return None; return nullptr;
} }
Module *HeaderSearch::loadFrameworkModule(StringRef Name, Module *HeaderSearch::loadFrameworkModule(StringRef Name,
@ -1659,10 +1663,9 @@ HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
if (KnownDir != DirectoryHasModuleMap.end()) if (KnownDir != DirectoryHasModuleMap.end())
return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap; return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;
if (Optional<FileEntryRef> ModuleMapFile = if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
lookupModuleMapFile(Dir, IsFramework)) {
LoadModuleMapResult Result = LoadModuleMapResult Result =
loadModuleMapFileImpl(*ModuleMapFile, IsSystem, Dir); loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
// Add Dir explicitly in case ModuleMapFile is in a subdirectory. // Add Dir explicitly in case ModuleMapFile is in a subdirectory.
// E.g. Foo.framework/Modules/module.modulemap // E.g. Foo.framework/Modules/module.modulemap
// ^Dir ^ModuleMapFile // ^Dir ^ModuleMapFile

View File

@ -973,9 +973,9 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
// We haven't looked here before. Load a module map, if there is // We haven't looked here before. Load a module map, if there is
// one. // one.
bool IsFrameworkDir = Parent.endswith(".framework"); bool IsFrameworkDir = Parent.endswith(".framework");
if (Optional<FileEntryRef> ModMapFile = if (const FileEntry *ModMapFile =
HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) { HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
parseModuleMapFile(*ModMapFile, Attrs.IsSystem, *ParentDir); parseModuleMapFile(ModMapFile, Attrs.IsSystem, *ParentDir);
inferred = InferredDirectories.find(*ParentDir); inferred = InferredDirectories.find(*ParentDir);
} }
@ -2163,12 +2163,12 @@ void ModuleMapParser::parseExternModuleDecl() {
llvm::sys::path::append(ModuleMapFileName, FileName); llvm::sys::path::append(ModuleMapFileName, FileName);
FileNameRef = ModuleMapFileName; FileNameRef = ModuleMapFileName;
} }
if (auto File = SourceMgr.getFileManager().getOptionalFileRef(FileNameRef)) if (auto File = SourceMgr.getFileManager().getFile(FileNameRef))
Map.parseModuleMapFile( Map.parseModuleMapFile(
*File, /*IsSystem=*/false, *File, /*IsSystem=*/false,
Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd Map.HeaderInfo.getHeaderSearchOpts().ModuleMapFileHomeIsCwd
? Directory ? Directory
: File->getDir(), : (*File)->getDir(),
FileID(), nullptr, ExternLoc); FileID(), nullptr, ExternLoc);
} }
@ -2984,7 +2984,7 @@ bool ModuleMapParser::parseModuleMapFile() {
} while (true); } while (true);
} }
bool ModuleMap::parseModuleMapFile(FileEntryRef File, bool IsSystem, bool ModuleMap::parseModuleMapFile(const FileEntry *File, bool IsSystem,
const DirectoryEntry *Dir, FileID ID, const DirectoryEntry *Dir, FileID ID,
unsigned *Offset, unsigned *Offset,
SourceLocation ExternModuleLoc) { SourceLocation ExternModuleLoc) {

View File

@ -278,10 +278,10 @@ bool ClangModulesDeclVendorImpl::AddModule(const SourceModule &module,
HS.getFileMgr().getDirectory(module.search_path.GetStringRef()); HS.getFileMgr().getDirectory(module.search_path.GetStringRef());
if (!dir) if (!dir)
return error(); return error();
auto file = HS.lookupModuleMapFile(*dir, is_framework); auto *file = HS.lookupModuleMapFile(*dir, is_framework);
if (!file) if (!file)
return error(); return error();
if (!HS.loadModuleMapFile(*file, is_system)) if (!HS.loadModuleMapFile(file, is_system))
return error(); return error();
} }
} }