Skip to content

Commit 1d3ba05

Browse files
committed
[clang][lex] NFCI: Use DirectoryEntryRef in HeaderSearch::load*()
This patch removes uses of the deprecated `DirectoryEntry::getName()` from `HeaderSearch::load*()` functions by using `DirectoryEntryRef` instead. Note that we bail out in one case and use the also deprecated `FileEntry::getLastRef()`. That's to prevent this patch from growing, and is addressed in a follow-up. Reviewed By: bnbarham Differential Revision: https://reviews.llvm.org/D123771
1 parent 2cca53c commit 1d3ba05

File tree

2 files changed

+38
-40
lines changed

2 files changed

+38
-40
lines changed

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,7 @@ class HeaderSearch {
738738
/// frameworks.
739739
///
740740
/// \returns The module, if found; otherwise, null.
741-
Module *loadFrameworkModule(StringRef Name,
742-
const DirectoryEntry *Dir,
741+
Module *loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
743742
bool IsSystem);
744743

745744
/// Load all of the module maps within the immediate subdirectories
@@ -888,7 +887,7 @@ class HeaderSearch {
888887

889888
LoadModuleMapResult loadModuleMapFileImpl(const FileEntry *File,
890889
bool IsSystem,
891-
const DirectoryEntry *Dir,
890+
DirectoryEntryRef Dir,
892891
FileID ID = FileID(),
893892
unsigned *Offset = nullptr);
894893

@@ -912,8 +911,8 @@ class HeaderSearch {
912911
///
913912
/// \returns The result of attempting to load the module map file from the
914913
/// named directory.
915-
LoadModuleMapResult loadModuleMapFile(const DirectoryEntry *Dir,
916-
bool IsSystem, bool IsFramework);
914+
LoadModuleMapResult loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
915+
bool IsFramework);
917916
};
918917

919918
/// Apply the header search options to get given HeaderSearch object.

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
319319
SmallString<128> FrameworkDirName;
320320
FrameworkDirName += Dir.getFrameworkDir()->getName();
321321
llvm::sys::path::append(FrameworkDirName, SearchName + ".framework");
322-
if (auto FrameworkDir = FileMgr.getDirectory(FrameworkDirName)) {
322+
if (auto FrameworkDir =
323+
FileMgr.getOptionalDirectoryRef(FrameworkDirName)) {
323324
bool IsSystem = Dir.getDirCharacteristic() != SrcMgr::C_User;
324325
Module = loadFrameworkModule(ModuleName, *FrameworkDir, IsSystem);
325326
if (Module)
@@ -334,8 +335,10 @@ Module *HeaderSearch::lookupModule(StringRef ModuleName, StringRef SearchName,
334335
continue;
335336

336337
bool IsSystem = Dir.isSystemHeaderDirectory();
338+
// Only returns None if not a normal directory, which we just checked
339+
DirectoryEntryRef NormalDir = *Dir.getDirRef();
337340
// Search for a module map file in this directory.
338-
if (loadModuleMapFile(Dir.getDir(), IsSystem,
341+
if (loadModuleMapFile(NormalDir, IsSystem,
339342
/*IsFramework*/false) == LMM_NewlyLoaded) {
340343
// We just loaded a module map file; check whether the module is
341344
// available now.
@@ -507,7 +510,7 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
507510
/// \param DirName The name of the framework directory.
508511
/// \param SubmodulePath Will be populated with the submodule path from the
509512
/// returned top-level module to the originally named framework.
510-
static const DirectoryEntry *
513+
static Optional<DirectoryEntryRef>
511514
getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
512515
SmallVectorImpl<std::string> &SubmodulePath) {
513516
assert(llvm::sys::path::extension(DirName) == ".framework" &&
@@ -527,20 +530,18 @@ getTopFrameworkDir(FileManager &FileMgr, StringRef DirName,
527530
//
528531
// Similar issues occur when a top-level framework has moved into an
529532
// embedded framework.
530-
const DirectoryEntry *TopFrameworkDir = nullptr;
531-
if (auto TopFrameworkDirOrErr = FileMgr.getDirectory(DirName))
532-
TopFrameworkDir = *TopFrameworkDirOrErr;
533+
auto TopFrameworkDir = FileMgr.getOptionalDirectoryRef(DirName);
533534

534535
if (TopFrameworkDir)
535-
DirName = FileMgr.getCanonicalName(TopFrameworkDir);
536+
DirName = FileMgr.getCanonicalName(*TopFrameworkDir);
536537
do {
537538
// Get the parent directory name.
538539
DirName = llvm::sys::path::parent_path(DirName);
539540
if (DirName.empty())
540541
break;
541542

542543
// Determine whether this directory exists.
543-
auto Dir = FileMgr.getDirectory(DirName);
544+
auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
544545
if (!Dir)
545546
break;
546547

@@ -1476,13 +1477,13 @@ bool HeaderSearch::hasModuleMap(StringRef FileName,
14761477
return false;
14771478

14781479
// Determine whether this directory exists.
1479-
auto Dir = FileMgr.getDirectory(DirName);
1480+
auto Dir = FileMgr.getOptionalDirectoryRef(DirName);
14801481
if (!Dir)
14811482
return false;
14821483

14831484
// Try to load the module map file in this directory.
14841485
switch (loadModuleMapFile(*Dir, IsSystem,
1485-
llvm::sys::path::extension((*Dir)->getName()) ==
1486+
llvm::sys::path::extension(Dir->getName()) ==
14861487
".framework")) {
14871488
case LMM_NewlyLoaded:
14881489
case LMM_AlreadyLoaded:
@@ -1579,15 +1580,16 @@ bool HeaderSearch::findUsableModuleForFrameworkHeader(
15791580
if (needModuleLookup(RequestingModule, SuggestedModule)) {
15801581
// Find the top-level framework based on this framework.
15811582
SmallVector<std::string, 4> SubmodulePath;
1582-
const DirectoryEntry *TopFrameworkDir
1583-
= ::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1583+
Optional<DirectoryEntryRef> TopFrameworkDir =
1584+
::getTopFrameworkDir(FileMgr, FrameworkName, SubmodulePath);
1585+
assert(TopFrameworkDir && "Could not find the top-most framework dir");
15841586

15851587
// Determine the name of the top-level framework.
15861588
StringRef ModuleName = llvm::sys::path::stem(TopFrameworkDir->getName());
15871589

15881590
// Load this framework module. If that succeeds, find the suggested module
15891591
// for this header, if any.
1590-
loadFrameworkModule(ModuleName, TopFrameworkDir, IsSystemFramework);
1592+
loadFrameworkModule(ModuleName, *TopFrameworkDir, IsSystemFramework);
15911593

15921594
// FIXME: This can find a module not part of ModuleName, which is
15931595
// important so that we're consistent about whether this header
@@ -1618,39 +1620,38 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
16181620
StringRef OriginalModuleMapFile) {
16191621
// Find the directory for the module. For frameworks, that may require going
16201622
// up from the 'Modules' directory.
1621-
const DirectoryEntry *Dir = nullptr;
1623+
Optional<DirectoryEntryRef> Dir;
16221624
if (getHeaderSearchOpts().ModuleMapFileHomeIsCwd) {
1623-
if (auto DirOrErr = FileMgr.getDirectory("."))
1624-
Dir = *DirOrErr;
1625+
Dir = FileMgr.getOptionalDirectoryRef(".");
16251626
} else {
16261627
if (!OriginalModuleMapFile.empty()) {
16271628
// We're building a preprocessed module map. Find or invent the directory
16281629
// that it originally occupied.
1629-
auto DirOrErr = FileMgr.getDirectory(
1630+
Dir = FileMgr.getOptionalDirectoryRef(
16301631
llvm::sys::path::parent_path(OriginalModuleMapFile));
1631-
if (DirOrErr) {
1632-
Dir = *DirOrErr;
1633-
} else {
1634-
auto *FakeFile = FileMgr.getVirtualFile(OriginalModuleMapFile, 0, 0);
1635-
Dir = FakeFile->getDir();
1632+
if (!Dir) {
1633+
auto FakeFile = FileMgr.getVirtualFileRef(OriginalModuleMapFile, 0, 0);
1634+
Dir = FakeFile.getDir();
16361635
}
16371636
} else {
1638-
Dir = File->getDir();
1637+
Dir = File->getLastRef().getDir();
16391638
}
16401639

1640+
assert(Dir && "parent must exist");
16411641
StringRef DirName(Dir->getName());
16421642
if (llvm::sys::path::filename(DirName) == "Modules") {
16431643
DirName = llvm::sys::path::parent_path(DirName);
16441644
if (DirName.endswith(".framework"))
1645-
if (auto DirOrErr = FileMgr.getDirectory(DirName))
1646-
Dir = *DirOrErr;
1645+
if (auto MaybeDir = FileMgr.getOptionalDirectoryRef(DirName))
1646+
Dir = *MaybeDir;
16471647
// FIXME: This assert can fail if there's a race between the above check
16481648
// and the removal of the directory.
16491649
assert(Dir && "parent must exist");
16501650
}
16511651
}
16521652

1653-
switch (loadModuleMapFileImpl(File, IsSystem, Dir, ID, Offset)) {
1653+
assert(Dir && "module map home directory must exist");
1654+
switch (loadModuleMapFileImpl(File, IsSystem, *Dir, ID, Offset)) {
16541655
case LMM_AlreadyLoaded:
16551656
case LMM_NewlyLoaded:
16561657
return false;
@@ -1663,7 +1664,7 @@ bool HeaderSearch::loadModuleMapFile(const FileEntry *File, bool IsSystem,
16631664

16641665
HeaderSearch::LoadModuleMapResult
16651666
HeaderSearch::loadModuleMapFileImpl(const FileEntry *File, bool IsSystem,
1666-
const DirectoryEntry *Dir, FileID ID,
1667+
DirectoryEntryRef Dir, FileID ID,
16671668
unsigned *Offset) {
16681669
assert(File && "expected FileEntry");
16691670

@@ -1721,8 +1722,7 @@ HeaderSearch::lookupModuleMapFile(const DirectoryEntry *Dir, bool IsFramework) {
17211722
return nullptr;
17221723
}
17231724

1724-
Module *HeaderSearch::loadFrameworkModule(StringRef Name,
1725-
const DirectoryEntry *Dir,
1725+
Module *HeaderSearch::loadFrameworkModule(StringRef Name, DirectoryEntryRef Dir,
17261726
bool IsSystem) {
17271727
if (Module *Module = ModMap.findModule(Name))
17281728
return Module;
@@ -1749,14 +1749,14 @@ Module *HeaderSearch::loadFrameworkModule(StringRef Name,
17491749
HeaderSearch::LoadModuleMapResult
17501750
HeaderSearch::loadModuleMapFile(StringRef DirName, bool IsSystem,
17511751
bool IsFramework) {
1752-
if (auto Dir = FileMgr.getDirectory(DirName))
1752+
if (auto Dir = FileMgr.getOptionalDirectoryRef(DirName))
17531753
return loadModuleMapFile(*Dir, IsSystem, IsFramework);
17541754

17551755
return LMM_NoDirectory;
17561756
}
17571757

17581758
HeaderSearch::LoadModuleMapResult
1759-
HeaderSearch::loadModuleMapFile(const DirectoryEntry *Dir, bool IsSystem,
1759+
HeaderSearch::loadModuleMapFile(DirectoryEntryRef Dir, bool IsSystem,
17601760
bool IsFramework) {
17611761
auto KnownDir = DirectoryHasModuleMap.find(Dir);
17621762
if (KnownDir != DirectoryHasModuleMap.end())
@@ -1797,8 +1797,7 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
17971797
if (llvm::sys::path::extension(Dir->path()) != ".framework")
17981798
continue;
17991799

1800-
auto FrameworkDir =
1801-
FileMgr.getDirectory(Dir->path());
1800+
auto FrameworkDir = FileMgr.getOptionalDirectoryRef(Dir->path());
18021801
if (!FrameworkDir)
18031802
continue;
18041803

@@ -1814,7 +1813,7 @@ void HeaderSearch::collectAllModules(SmallVectorImpl<Module *> &Modules) {
18141813
continue;
18151814

18161815
// Try to load a module map file for the search directory.
1817-
loadModuleMapFile(DL.getDir(), IsSystem, /*IsFramework*/ false);
1816+
loadModuleMapFile(*DL.getDirRef(), IsSystem, /*IsFramework*/ false);
18181817

18191818
// Try to load module map files for immediate subdirectories of this
18201819
// search directory.
@@ -1838,7 +1837,7 @@ void HeaderSearch::loadTopLevelSystemModules() {
18381837
continue;
18391838

18401839
// Try to load a module map file for the search directory.
1841-
loadModuleMapFile(DL.getDir(), DL.isSystemHeaderDirectory(),
1840+
loadModuleMapFile(*DL.getDirRef(), DL.isSystemHeaderDirectory(),
18421841
DL.isFramework());
18431842
}
18441843
}

0 commit comments

Comments
 (0)