Skip to content

Commit 786b503

Browse files
committed
[Clang][Lex] Extend HeaderSearch::LookupFile to control OpenFile behavior.
In the case of static compilation the file system is pretty much read-only and taking a snapshot of it usually is sufficient. In the interactive C++ case the compilation is longer and people can create and include files, etc. In that case we often do not want to open files or cache failures unless is absolutely necessary. This patch extends the original API call by forwarding some optional flags, so we can continue use it in the previous way with no breakage. Signed-off-by: Jun Zhang <[email protected]> Differential Revision: https://reviews.llvm.org/D131241
1 parent e99ffe6 commit 786b503

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

clang/include/clang/Lex/DirectoryLookup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ class DirectoryLookup {
186186
SmallVectorImpl<char> *RelativePath, Module *RequestingModule,
187187
ModuleMap::KnownHeader *SuggestedModule,
188188
bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
189-
bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName) const;
189+
bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName,
190+
bool OpenFile = true) const;
190191

191192
private:
192193
Optional<FileEntryRef> DoFrameworkLookup(

clang/include/clang/Lex/HeaderSearch.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ class HeaderSearch {
474474
SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
475475
Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
476476
bool *IsMapped, bool *IsFrameworkFound, bool SkipCache = false,
477-
bool BuildSystemModule = false);
477+
bool BuildSystemModule = false, bool OpenFile = true,
478+
bool CacheFailures = true);
478479

479480
/// Look up a subframework for the specified \#include file.
480481
///
@@ -759,7 +760,8 @@ class HeaderSearch {
759760
getFileAndSuggestModule(StringRef FileName, SourceLocation IncludeLoc,
760761
const DirectoryEntry *Dir, bool IsSystemHeaderDir,
761762
Module *RequestingModule,
762-
ModuleMap::KnownHeader *SuggestedModule);
763+
ModuleMap::KnownHeader *SuggestedModule,
764+
bool OpenFile = true, bool CacheFailures = true);
763765

764766
/// Cache the result of a successful lookup at the given include location
765767
/// using the search path at \c HitIt.

clang/include/clang/Lex/Preprocessor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,8 @@ class Preprocessor {
22282228
ConstSearchDirIterator *CurDir, SmallVectorImpl<char> *SearchPath,
22292229
SmallVectorImpl<char> *RelativePath,
22302230
ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
2231-
bool *IsFrameworkFound, bool SkipCache = false);
2231+
bool *IsFrameworkFound, bool SkipCache = false,
2232+
bool OpenFile = true, bool CacheFailures = true);
22322233

22332234
/// Return true if we're in the top-level file, not in a \#include.
22342235
bool isInPrimaryFile() const;

clang/lib/Lex/HeaderSearch.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ StringRef DirectoryLookup::getName() const {
398398
Optional<FileEntryRef> HeaderSearch::getFileAndSuggestModule(
399399
StringRef FileName, SourceLocation IncludeLoc, const DirectoryEntry *Dir,
400400
bool IsSystemHeaderDir, Module *RequestingModule,
401-
ModuleMap::KnownHeader *SuggestedModule) {
401+
ModuleMap::KnownHeader *SuggestedModule, bool OpenFile /*=true*/,
402+
bool CacheFailures /*=true*/) {
402403
// If we have a module map that might map this header, load it and
403404
// check whether we'll have a suggestion for a module.
404-
auto File = getFileMgr().getFileRef(FileName, /*OpenFile=*/true);
405+
auto File = getFileMgr().getFileRef(FileName, OpenFile, CacheFailures);
405406
if (!File) {
406407
// For rare, surprising errors (e.g. "out of file handles"), diag the EC
407408
// message.
@@ -431,7 +432,8 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
431432
SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
432433
Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
433434
bool &InUserSpecifiedSystemFramework, bool &IsFrameworkFound,
434-
bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName) const {
435+
bool &IsInHeaderMap, SmallVectorImpl<char> &MappedName,
436+
bool OpenFile) const {
435437
InUserSpecifiedSystemFramework = false;
436438
IsInHeaderMap = false;
437439
MappedName.clear();
@@ -451,9 +453,9 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
451453
RelativePath->append(Filename.begin(), Filename.end());
452454
}
453455

454-
return HS.getFileAndSuggestModule(TmpDir, IncludeLoc, getDir(),
455-
isSystemHeaderDirectory(),
456-
RequestingModule, SuggestedModule);
456+
return HS.getFileAndSuggestModule(
457+
TmpDir, IncludeLoc, getDir(), isSystemHeaderDirectory(),
458+
RequestingModule, SuggestedModule, OpenFile);
457459
}
458460

459461
if (isFramework())
@@ -491,7 +493,7 @@ Optional<FileEntryRef> DirectoryLookup::LookupFile(
491493
Dest = HM->lookupFilename(Filename, Path);
492494
}
493495

494-
if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest)) {
496+
if (auto Res = HS.getFileMgr().getOptionalFileRef(Dest, OpenFile)) {
495497
FixupSearchPath();
496498
return *Res;
497499
}
@@ -840,7 +842,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
840842
SmallVectorImpl<char> *SearchPath, SmallVectorImpl<char> *RelativePath,
841843
Module *RequestingModule, ModuleMap::KnownHeader *SuggestedModule,
842844
bool *IsMapped, bool *IsFrameworkFound, bool SkipCache,
843-
bool BuildSystemModule) {
845+
bool BuildSystemModule, bool OpenFile, bool CacheFailures) {
844846
ConstSearchDirIterator CurDirLocal = nullptr;
845847
ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
846848

@@ -869,8 +871,9 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
869871
}
870872
// Otherwise, just return the file.
871873
return getFileAndSuggestModule(Filename, IncludeLoc, nullptr,
872-
/*IsSystemHeaderDir*/false,
873-
RequestingModule, SuggestedModule);
874+
/*IsSystemHeaderDir*/ false,
875+
RequestingModule, SuggestedModule, OpenFile,
876+
CacheFailures);
874877
}
875878

876879
// This is the header that MSVC's header search would have found.
@@ -1010,7 +1013,7 @@ Optional<FileEntryRef> HeaderSearch::LookupFile(
10101013
Optional<FileEntryRef> File = It->LookupFile(
10111014
Filename, *this, IncludeLoc, SearchPath, RelativePath, RequestingModule,
10121015
SuggestedModule, InUserSpecifiedSystemFramework, IsFrameworkFoundInDir,
1013-
IsInHeaderMap, MappedName);
1016+
IsInHeaderMap, MappedName, OpenFile);
10141017
if (!MappedName.empty()) {
10151018
assert(IsInHeaderMap && "MappedName should come from a header map");
10161019
CacheLookup.MappedName =

clang/lib/Lex/PPDirectives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ Optional<FileEntryRef> Preprocessor::LookupFile(
949949
ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath,
950950
SmallVectorImpl<char> *RelativePath,
951951
ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
952-
bool *IsFrameworkFound, bool SkipCache) {
952+
bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) {
953953
ConstSearchDirIterator CurDirLocal = nullptr;
954954
ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
955955

@@ -1028,7 +1028,7 @@ Optional<FileEntryRef> Preprocessor::LookupFile(
10281028
Optional<FileEntryRef> FE = HeaderInfo.LookupFile(
10291029
Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
10301030
RelativePath, RequestingModule, SuggestedModule, IsMapped,
1031-
IsFrameworkFound, SkipCache, BuildSystemModule);
1031+
IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
10321032
if (FE) {
10331033
if (SuggestedModule && !LangOpts.AsmPreprocessor)
10341034
HeaderInfo.getModuleMap().diagnoseHeaderInclusion(

0 commit comments

Comments
 (0)