[clang] b092f41 - [clang] Use `FileEntryRef` in modular header search (part 1/2)
Jan Svoboda via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 1 10:38:15 PDT 2023
Author: Jan Svoboda
Date: 2023-06-01T10:38:07-07:00
New Revision: b092f417db211b5316b2222c29bc651eaaa86ce1
URL: https://github.com/llvm/llvm-project/commit/b092f417db211b5316b2222c29bc651eaaa86ce1
DIFF: https://github.com/llvm/llvm-project/commit/b092f417db211b5316b2222c29bc651eaaa86ce1.diff
LOG: [clang] Use `FileEntryRef` in modular header search (part 1/2)
This patch removes some deprecated uses of `{File,Directory}Entry::getName()`. No functional change indended.
Depends on D151853.
Reviewed By: benlangmuir
Differential Revision: https://reviews.llvm.org/D151854
Added:
Modified:
clang/include/clang/Lex/HeaderSearch.h
clang/include/clang/Lex/ModuleMap.h
clang/lib/Lex/HeaderSearch.cpp
clang/lib/Lex/ModuleMap.cpp
clang/tools/libclang/CIndex.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h
index 947fb8308908e..131dbdcc20007 100644
--- a/clang/include/clang/Lex/HeaderSearch.h
+++ b/clang/include/clang/Lex/HeaderSearch.h
@@ -659,7 +659,7 @@ class HeaderSearch {
///
/// \param File The header that we wish to map to a module.
/// \param AllowTextual Whether we want to find textual headers too.
- ModuleMap::KnownHeader findModuleForHeader(const FileEntry *File,
+ ModuleMap::KnownHeader findModuleForHeader(FileEntryRef File,
bool AllowTextual = false,
bool AllowExcluded = false) const;
@@ -670,7 +670,7 @@ class HeaderSearch {
///
/// \ref findModuleForHeader should typically be used instead of this.
ArrayRef<ModuleMap::KnownHeader>
- findAllModulesForHeader(const FileEntry *File) const;
+ findAllModulesForHeader(FileEntryRef File) const;
/// Like \ref findAllModulesForHeader, but do not attempt to infer module
/// ownership from umbrella headers if we've not already done so.
@@ -760,8 +760,7 @@ class HeaderSearch {
///
/// \return \c true if the file can be used, \c false if we are not permitted to
/// find this file due to requirements from \p RequestingModule.
- bool findUsableModuleForHeader(const FileEntry *File,
- const DirectoryEntry *Root,
+ bool findUsableModuleForHeader(FileEntryRef File, const DirectoryEntry *Root,
Module *RequestingModule,
ModuleMap::KnownHeader *SuggestedModule,
bool IsSystemHeaderDir);
@@ -772,7 +771,7 @@ class HeaderSearch {
/// \return \c true if the file can be used, \c false if we are not permitted to
/// find this file due to requirements from \p RequestingModule.
bool findUsableModuleForFrameworkHeader(
- const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
+ FileEntryRef File, StringRef FrameworkName, Module *RequestingModule,
ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework);
/// Look up the file with the specified name and determine its owning
diff --git a/clang/include/clang/Lex/ModuleMap.h b/clang/include/clang/Lex/ModuleMap.h
index 55797a31bbf83..83c227b38e1bb 100644
--- a/clang/include/clang/Lex/ModuleMap.h
+++ b/clang/include/clang/Lex/ModuleMap.h
@@ -364,17 +364,17 @@ class ModuleMap {
///
/// \param IntermediateDirs On success, contains the set of directories
/// searched before finding \p File.
- KnownHeader findHeaderInUmbrellaDirs(const FileEntry *File,
- SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs);
+ KnownHeader findHeaderInUmbrellaDirs(
+ FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs);
/// Given that \p File is not in the Headers map, look it up within
/// umbrella directories and find or create a module for it.
- KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File);
+ KnownHeader findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File);
/// A convenience method to determine if \p File is (possibly nested)
/// in an umbrella directory.
- bool isHeaderInUmbrellaDirs(const FileEntry *File) {
- SmallVector<const DirectoryEntry *, 2> IntermediateDirs;
+ bool isHeaderInUmbrellaDirs(FileEntryRef File) {
+ SmallVector<DirectoryEntryRef, 2> IntermediateDirs;
return static_cast<bool>(findHeaderInUmbrellaDirs(File, IntermediateDirs));
}
@@ -437,8 +437,7 @@ class ModuleMap {
/// \returns The module KnownHeader, which provides the module that owns the
/// given header file. The KnownHeader is default constructed to indicate
/// that no module owns this header file.
- KnownHeader findModuleForHeader(const FileEntry *File,
- bool AllowTextual = false,
+ KnownHeader findModuleForHeader(FileEntryRef File, bool AllowTextual = false,
bool AllowExcluded = false);
/// Retrieve all the modules that contain the given header file. Note that
@@ -448,7 +447,7 @@ class ModuleMap {
///
/// Typically, \ref findModuleForHeader should be used instead, as it picks
/// the preferred module for the header.
- ArrayRef<KnownHeader> findAllModulesForHeader(const FileEntry *File);
+ ArrayRef<KnownHeader> findAllModulesForHeader(FileEntryRef File);
/// Like \ref findAllModulesForHeader, but do not attempt to infer module
/// ownership from umbrella headers if we've not already done so.
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp
index 0ee61aa288906..d94fe09274ed6 100644
--- a/clang/lib/Lex/HeaderSearch.cpp
+++ b/clang/lib/Lex/HeaderSearch.cpp
@@ -436,8 +436,8 @@ OptionalFileEntryRef HeaderSearch::getFileAndSuggestModule(
// If there is a module that corresponds to this header, suggest it.
if (!findUsableModuleForHeader(
- &File->getFileEntry(), Dir ? Dir : File->getFileEntry().getDir(),
- RequestingModule, SuggestedModule, IsSystemHeaderDir))
+ *File, Dir ? Dir : File->getFileEntry().getDir(), RequestingModule,
+ SuggestedModule, IsSystemHeaderDir))
return std::nullopt;
return *File;
@@ -491,7 +491,7 @@ OptionalFileEntryRef DirectoryLookup::LookupFile(
IsInHeaderMap = true;
auto FixupSearchPathAndFindUsableModule =
- [&](auto File) -> OptionalFileEntryRef {
+ [&](FileEntryRef File) -> OptionalFileEntryRef {
if (SearchPath) {
StringRef SearchPathRef(getName());
SearchPath->clear();
@@ -501,9 +501,9 @@ OptionalFileEntryRef DirectoryLookup::LookupFile(
RelativePath->clear();
RelativePath->append(Filename.begin(), Filename.end());
}
- if (!HS.findUsableModuleForHeader(
- &File.getFileEntry(), File.getFileEntry().getDir(),
- RequestingModule, SuggestedModule, isSystemHeaderDirectory())) {
+ if (!HS.findUsableModuleForHeader(File, File.getFileEntry().getDir(),
+ RequestingModule, SuggestedModule,
+ isSystemHeaderDirectory())) {
return std::nullopt;
}
return File;
@@ -713,14 +713,13 @@ OptionalFileEntryRef DirectoryLookup::DoFrameworkLookup(
bool IsSystem = getDirCharacteristic() != SrcMgr::C_User;
if (FoundFramework) {
- if (!HS.findUsableModuleForFrameworkHeader(
- &File->getFileEntry(), FrameworkPath, RequestingModule,
- SuggestedModule, IsSystem))
+ if (!HS.findUsableModuleForFrameworkHeader(*File, FrameworkPath,
+ RequestingModule,
+ SuggestedModule, IsSystem))
return std::nullopt;
} else {
- if (!HS.findUsableModuleForHeader(&File->getFileEntry(), getDir(),
- RequestingModule, SuggestedModule,
- IsSystem))
+ if (!HS.findUsableModuleForHeader(*File, getDir(), RequestingModule,
+ SuggestedModule, IsSystem))
return std::nullopt;
}
}
@@ -1279,7 +1278,7 @@ OptionalFileEntryRef HeaderSearch::LookupSubframeworkHeader(
getFileInfo(&File->getFileEntry()).DirInfo = DirInfo;
FrameworkName.pop_back(); // remove the trailing '/'
- if (!findUsableModuleForFrameworkHeader(&File->getFileEntry(), FrameworkName,
+ if (!findUsableModuleForFrameworkHeader(*File, FrameworkName,
RequestingModule, SuggestedModule,
/*IsSystem*/ false))
return std::nullopt;
@@ -1559,7 +1558,7 @@ bool HeaderSearch::hasModuleMap(StringRef FileName,
}
ModuleMap::KnownHeader
-HeaderSearch::findModuleForHeader(const FileEntry *File, bool AllowTextual,
+HeaderSearch::findModuleForHeader(FileEntryRef File, bool AllowTextual,
bool AllowExcluded) const {
if (ExternalSource) {
// Make sure the external source has handled header info about this file,
@@ -1570,7 +1569,7 @@ HeaderSearch::findModuleForHeader(const FileEntry *File, bool AllowTextual,
}
ArrayRef<ModuleMap::KnownHeader>
-HeaderSearch::findAllModulesForHeader(const FileEntry *File) const {
+HeaderSearch::findAllModulesForHeader(FileEntryRef File) const {
if (ExternalSource) {
// Make sure the external source has handled header info about this file,
// which includes whether the file is part of a module.
@@ -1589,7 +1588,7 @@ HeaderSearch::findResolvedModulesForHeader(const FileEntry *File) const {
return ModMap.findResolvedModulesForHeader(File);
}
-static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
+static bool suggestModule(HeaderSearch &HS, FileEntryRef File,
Module *RequestingModule,
ModuleMap::KnownHeader *SuggestedModule) {
ModuleMap::KnownHeader Module =
@@ -1625,18 +1624,18 @@ static bool suggestModule(HeaderSearch &HS, const FileEntry *File,
}
bool HeaderSearch::findUsableModuleForHeader(
- const FileEntry *File, const DirectoryEntry *Root, Module *RequestingModule,
+ FileEntryRef File, const DirectoryEntry *Root, Module *RequestingModule,
ModuleMap::KnownHeader *SuggestedModule, bool IsSystemHeaderDir) {
- if (File && needModuleLookup(RequestingModule, SuggestedModule)) {
+ if (needModuleLookup(RequestingModule, SuggestedModule)) {
// If there is a module that corresponds to this header, suggest it.
- hasModuleMap(File->getName(), Root, IsSystemHeaderDir);
+ hasModuleMap(File.getName(), Root, IsSystemHeaderDir);
return suggestModule(*this, File, RequestingModule, SuggestedModule);
}
return true;
}
bool HeaderSearch::findUsableModuleForFrameworkHeader(
- const FileEntry *File, StringRef FrameworkName, Module *RequestingModule,
+ FileEntryRef File, StringRef FrameworkName, Module *RequestingModule,
ModuleMap::KnownHeader *SuggestedModule, bool IsSystemFramework) {
// If we're supposed to suggest a module, look for one now.
if (needModuleLookup(RequestingModule, SuggestedModule)) {
diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index 7fc810fda5ba5..c620eb7769742 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -409,29 +409,27 @@ ModuleMap::findKnownHeader(const FileEntry *File) {
return Known;
}
-ModuleMap::KnownHeader
-ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
- SmallVectorImpl<const DirectoryEntry *> &IntermediateDirs) {
+ModuleMap::KnownHeader ModuleMap::findHeaderInUmbrellaDirs(
+ FileEntryRef File, SmallVectorImpl<DirectoryEntryRef> &IntermediateDirs) {
if (UmbrellaDirs.empty())
return {};
- const DirectoryEntry *Dir = File->getDir();
- assert(Dir && "file in no directory");
+ OptionalDirectoryEntryRef Dir = File.getDir();
// Note: as an egregious but useful hack we use the real path here, because
// frameworks moving from top-level frameworks to embedded frameworks tend
// to be symlinked from the top-level location to the embedded location,
// and we need to resolve lookups as if we had found the embedded location.
- StringRef DirName = SourceMgr.getFileManager().getCanonicalName(Dir);
+ StringRef DirName = SourceMgr.getFileManager().getCanonicalName(*Dir);
// Keep walking up the directory hierarchy, looking for a directory with
// an umbrella header.
do {
- auto KnownDir = UmbrellaDirs.find(Dir);
+ auto KnownDir = UmbrellaDirs.find(*Dir);
if (KnownDir != UmbrellaDirs.end())
return KnownHeader(KnownDir->second, NormalHeader);
- IntermediateDirs.push_back(Dir);
+ IntermediateDirs.push_back(*Dir);
// Retrieve our parent path.
DirName = llvm::sys::path::parent_path(DirName);
@@ -439,10 +437,7 @@ ModuleMap::findHeaderInUmbrellaDirs(const FileEntry *File,
break;
// Resolve the parent path to a directory entry.
- if (auto DirEntry = SourceMgr.getFileManager().getDirectory(DirName))
- Dir = *DirEntry;
- else
- Dir = nullptr;
+ Dir = SourceMgr.getFileManager().getOptionalDirectoryRef(DirName);
} while (Dir);
return {};
}
@@ -582,7 +577,7 @@ static bool isBetterKnownHeader(const ModuleMap::KnownHeader &New,
return false;
}
-ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
+ModuleMap::KnownHeader ModuleMap::findModuleForHeader(FileEntryRef File,
bool AllowTextual,
bool AllowExcluded) {
auto MakeResult = [&](ModuleMap::KnownHeader R) -> ModuleMap::KnownHeader {
@@ -612,10 +607,10 @@ ModuleMap::KnownHeader ModuleMap::findModuleForHeader(const FileEntry *File,
}
ModuleMap::KnownHeader
-ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
+ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(FileEntryRef File) {
assert(!Headers.count(File) && "already have a module for this header");
- SmallVector<const DirectoryEntry *, 2> SkippedDirs;
+ SmallVector<DirectoryEntryRef, 2> SkippedDirs;
KnownHeader H = findHeaderInUmbrellaDirs(File, SkippedDirs);
if (H) {
Module *Result = H.getModule();
@@ -635,11 +630,11 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
// the actual header is located.
bool Explicit = UmbrellaModule->InferExplicitSubmodules;
- for (const DirectoryEntry *SkippedDir : llvm::reverse(SkippedDirs)) {
+ for (DirectoryEntryRef SkippedDir : llvm::reverse(SkippedDirs)) {
// Find or create the module that corresponds to this directory name.
SmallString<32> NameBuf;
StringRef Name = sanitizeFilenameAsIdentifier(
- llvm::sys::path::stem(SkippedDir->getName()), NameBuf);
+ llvm::sys::path::stem(SkippedDir.getName()), NameBuf);
Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
Explicit).first;
InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
@@ -657,7 +652,7 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
// Infer a submodule with the same name as this header file.
SmallString<32> NameBuf;
StringRef Name = sanitizeFilenameAsIdentifier(
- llvm::sys::path::stem(File->getName()), NameBuf);
+ llvm::sys::path::stem(File.getName()), NameBuf);
Result = findOrCreateModule(Name, Result, /*IsFramework=*/false,
Explicit).first;
InferredModuleAllowedBy[Result] = UmbrellaModuleMap;
@@ -684,7 +679,7 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) {
}
ArrayRef<ModuleMap::KnownHeader>
-ModuleMap::findAllModulesForHeader(const FileEntry *File) {
+ModuleMap::findAllModulesForHeader(FileEntryRef File) {
HeadersMap::iterator Known = findKnownHeader(File);
if (Known != Headers.end())
return Known->second;
diff --git a/clang/tools/libclang/CIndex.cpp b/clang/tools/libclang/CIndex.cpp
index ed2ca6ae2cfe1..f7321f0efe6ef 100644
--- a/clang/tools/libclang/CIndex.cpp
+++ b/clang/tools/libclang/CIndex.cpp
@@ -8799,7 +8799,8 @@ CXModule clang_getModuleForFile(CXTranslationUnit TU, CXFile File) {
ASTUnit &Unit = *cxtu::getASTUnit(TU);
HeaderSearch &HS = Unit.getPreprocessor().getHeaderSearchInfo();
- ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE);
+ // TODO: Make CXFile a FileEntryRef.
+ ModuleMap::KnownHeader Header = HS.findModuleForHeader(FE->getLastRef());
return Header.getModule();
}
More information about the cfe-commits
mailing list