Skip to content

Commit

Permalink
[clang][lex] NFCI: Use FileEntryRef in ModuleMap::{load,lookup}Module…
Browse files Browse the repository at this point in the history
…Map()

This patch changes the return/argument types of `ModuleMap::{load,lookup}ModuleMap()` from `const FileEntry *` to `FileEntryRef` in order to remove uses of the deprecated `DirectoryEntry::getName()`.

Reviewed By: bnbarham

Differential Revision: https://reviews.llvm.org/D127647
  • Loading branch information
jansvoboda11 committed May 30, 2023
1 parent 9e8a412 commit 769d282
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
13 changes: 6 additions & 7 deletions clang/include/clang/Lex/HeaderSearch.h
Original file line number Diff line number Diff line change
Expand Up @@ -637,9 +637,9 @@ class HeaderSearch {
bool AllowExtraModuleMapSearch = false);

/// Try to find a module map file in the given directory, returning
/// \c nullptr if none is found.
const FileEntry *lookupModuleMapFile(const DirectoryEntry *Dir,
bool IsFramework);
/// \c nullopt if none is found.
OptionalFileEntryRef lookupModuleMapFile(const DirectoryEntry *Dir,
bool IsFramework);

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

/// Collect the set of all known, top-level modules.
Expand Down Expand Up @@ -904,8 +904,7 @@ class HeaderSearch {
LMM_InvalidModuleMap
};

LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
bool IsSystem,
LoadModuleMapResult loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
DirectoryEntryRef Dir,
FileID ID = FileID(),
unsigned *Offset = nullptr);
Expand Down
9 changes: 5 additions & 4 deletions clang/lib/Frontend/FrontendAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,8 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,

// Map the current input to a file.
FileID ModuleMapID = SrcMgr.getMainFileID();
const FileEntry *ModuleMap = SrcMgr.getFileEntryForID(ModuleMapID);
OptionalFileEntryRef ModuleMap = SrcMgr.getFileEntryRefForID(ModuleMapID);
assert(ModuleMap && "MainFileID without FileEntry");

// If the module map is preprocessed, handle the initial line marker;
// line directives are not part of the module map syntax in general.
Expand All @@ -460,7 +461,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,
}

// Load the module map file.
if (HS.loadModuleMapFile(ModuleMap, IsSystem, ModuleMapID, &Offset,
if (HS.loadModuleMapFile(*ModuleMap, IsSystem, ModuleMapID, &Offset,
PresumedModuleMapFile))
return true;

Expand All @@ -469,7 +470,7 @@ static bool loadModuleMapForModuleBuild(CompilerInstance &CI, bool IsSystem,

// Infer framework module if possible.
if (HS.getModuleMap().canInferFrameworkModule(ModuleMap->getDir())) {
SmallString<128> InferredFrameworkPath = ModuleMap->getDir()->getName();
SmallString<128> InferredFrameworkPath = ModuleMap->getDir().getName();
llvm::sys::path::append(InferredFrameworkPath,
CI.getLangOpts().ModuleName + ".framework");
if (auto Dir = CI.getFileManager().getDirectory(InferredFrameworkPath))
Expand Down Expand Up @@ -910,7 +911,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI,

// If we were asked to load any module map files, do so now.
for (const auto &Filename : CI.getFrontendOpts().ModuleMapFiles) {
if (auto File = CI.getFileManager().getFile(Filename))
if (auto File = CI.getFileManager().getOptionalFileRef(Filename))
CI.getPreprocessor().getHeaderSearchInfo().loadModuleMapFile(
*File, /*IsSystem*/false);
else
Expand Down
33 changes: 15 additions & 18 deletions clang/lib/Lex/HeaderSearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1654,10 +1654,10 @@ bool HeaderSearch::findUsableModuleForFrameworkHeader(
return true;
}

static const FileEntry *getPrivateModuleMap(const FileEntry *File,
static const FileEntry *getPrivateModuleMap(FileEntryRef File,
FileManager &FileMgr) {
StringRef Filename = llvm::sys::path::filename(File->getName());
SmallString<128> PrivateFilename(File->getDir()->getName());
StringRef Filename = llvm::sys::path::filename(File.getName());
SmallString<128> PrivateFilename(File.getDir().getName());
if (Filename == "module.map")
llvm::sys::path::append(PrivateFilename, "module_private.map");
else if (Filename == "module.modulemap")
Expand All @@ -1669,7 +1669,7 @@ static const FileEntry *getPrivateModuleMap(const FileEntry *File,
return nullptr;
}

bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
bool HeaderSearch::loadModuleMapFile(FileEntryRef File, bool IsSystem,
FileID ID, unsigned *Offset,
StringRef OriginalModuleMapFile) {
// Find the directory for the module. For frameworks, that may require going
Expand All @@ -1688,9 +1688,7 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
Dir = FakeFile.getDir();
}
} else {
// TODO: Replace with `Dir = File.getDir()` when `File` is switched to
// `FileEntryRef`.
Dir = FileMgr.getOptionalDirectoryRef(File->getDir()->getName());
Dir = File.getDir();
}

assert(Dir && "parent must exist");
Expand Down Expand Up @@ -1719,11 +1717,9 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
}

HeaderSearch::LoadModuleMapResult
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
HeaderSearch::loadModuleMapFileImpl(FileEntryRef File, bool IsSystem,
DirectoryEntryRef Dir, FileID ID,
unsigned *Offset) {
assert(File && "expected FileEntry");

// 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.
auto AddResult = LoadedModuleMaps.insert(std::make_pair(File, true));
Expand All @@ -1747,23 +1743,23 @@ HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
return LMM_NewlyLoaded;
}

const FileEntry *
OptionalFileEntryRef
HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
if (!HSOpts->ImplicitModuleMaps)
return nullptr;
return std::nullopt;
// For frameworks, the preferred spelling is Modules/module.modulemap, but
// module.map at the framework root is also accepted.
SmallString<128> ModuleMapFileName(Dir->getName());
if (IsFramework)
llvm::sys::path::append(ModuleMapFileName, "Modules");
llvm::sys::path::append(ModuleMapFileName, "module.modulemap");
if (auto F = FileMgr.getFile(ModuleMapFileName))
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
return *F;

// Continue to allow module.map
ModuleMapFileName = Dir->getName();
llvm::sys::path::append(ModuleMapFileName, "module.map");
if (auto F = FileMgr.getFile(ModuleMapFileName))
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
return *F;

// For frameworks, allow to have a private module map with a preferred
Expand All @@ -1772,10 +1768,10 @@ HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
ModuleMapFileName = Dir->getName();
llvm::sys::path::append(ModuleMapFileName, "Modules",
"module.private.modulemap");
if (auto F = FileMgr.getFile(ModuleMapFileName))
if (auto F = FileMgr.getOptionalFileRef(ModuleMapFileName))
return *F;
}
return nullptr;
return std::nullopt;
}

Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
Expand Down Expand Up @@ -1818,9 +1814,10 @@ HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
if (KnownDir != DirectoryHasModuleMap.end())
return KnownDir->second ? LMM_AlreadyLoaded : LMM_InvalidModuleMap;

if (const FileEntry *ModuleMapFile = lookupModuleMapFile(Dir, IsFramework)) {
if (OptionalFileEntryRef ModuleMapFile =
lookupModuleMapFile(Dir, IsFramework)) {
LoadModuleMapResult Result =
loadModuleMapFileImpl(ModuleMapFile, IsSystem, Dir);
loadModuleMapFileImpl(*ModuleMapFile, IsSystem, Dir);
// Add Dir explicitly in case ModuleMapFile is in a subdirectory.
// E.g. Foo.framework/Modules/module.modulemap
// ^Dir ^ModuleMapFile
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,9 +1019,9 @@ Module *ModuleMap::inferFrameworkModule(const DirectoryEntry *FrameworkDir,
// We haven't looked here before. Load a module map, if there is
// one.
bool IsFrameworkDir = Parent.endswith(".framework");
if (const FileEntry *ModMapFile =
HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
parseModuleMapFile(ModMapFile, Attrs.IsSystem, *ParentDir);
if (OptionalFileEntryRef ModMapFile =
HeaderInfo.lookupModuleMapFile(*ParentDir, IsFrameworkDir)) {
parseModuleMapFile(*ModMapFile, Attrs.IsSystem, *ParentDir);
inferred = InferredDirectories.find(*ParentDir);
}

Expand Down

0 comments on commit 769d282

Please sign in to comment.