Skip to content

Commit 9c9330f

Browse files
authored
Merge pull request #8466 from apple/jan_svoboda/lazy-dependency-directives
[clang][deps][CAS] Lazy dependency directives The CAS counterpart to the upstream llvm#86347.
2 parents 1b35a94 + 0d92d1c commit 9c9330f

File tree

2 files changed

+14
-34
lines changed

2 files changed

+14
-34
lines changed

clang/include/clang/Tooling/DependencyScanning/DependencyScanningCASFilesystem.h

-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class DependencyScanningCASFilesystem : public llvm::cas::ThreadSafeFileSystem {
7373
getDirectiveTokens(const Twine &Path);
7474

7575
private:
76-
/// Check whether the file should be scanned for preprocessor directives.
77-
bool shouldScanForDirectives(StringRef Filename);
78-
7976
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
8077

8178
struct FileEntry {

clang/lib/Tooling/DependencyScanning/DependencyScanningCASFilesystem.cpp

+14-31
Original file line numberDiff line numberDiff line change
@@ -200,35 +200,11 @@ DependencyScanningCASFilesystem::getOriginal(cas::CASID InputDataID) {
200200
return Blob.takeError();
201201
}
202202

203-
/// Whitelist file extensions that should be minimized, treating no extension as
204-
/// a source file that should be minimized.
205-
///
206-
/// This is kinda hacky, it would be better if we knew what kind of file Clang
207-
/// was expecting instead.
208-
static bool shouldScanForDirectivesBasedOnExtension(StringRef Filename) {
209-
StringRef Ext = llvm::sys::path::extension(Filename);
210-
if (Ext.empty())
211-
return true; // C++ standard library
212-
return llvm::StringSwitch<bool>(Ext)
213-
.CasesLower(".c", ".cc", ".cpp", ".c++", ".cxx", true)
214-
.CasesLower(".h", ".hh", ".hpp", ".h++", ".hxx", true)
215-
.CasesLower(".m", ".mm", true)
216-
.CasesLower(".i", ".ii", ".mi", ".mmi", true)
217-
.CasesLower(".def", ".inc", true)
218-
.Default(false);
219-
}
220-
221203
static bool shouldCacheStatFailures(StringRef Filename) {
222204
StringRef Ext = llvm::sys::path::extension(Filename);
223205
if (Ext.empty())
224206
return false; // This may be the module cache directory.
225-
return shouldScanForDirectivesBasedOnExtension(
226-
Filename); // Only cache stat failures on source files.
227-
}
228-
229-
bool DependencyScanningCASFilesystem::shouldScanForDirectives(
230-
StringRef RawFilename) {
231-
return shouldScanForDirectivesBasedOnExtension(RawFilename);
207+
return true;
232208
}
233209

234210
llvm::cas::CachingOnDiskFileSystem &
@@ -274,10 +250,6 @@ DependencyScanningCASFilesystem::lookupPath(const Twine &Path) {
274250
return LookupPathResult{&Entry, std::error_code()};
275251
}
276252

277-
if (shouldScanForDirectives(PathRef))
278-
scanForDirectives(*CAS.getReference(*FileID), PathRef, Entry.DepTokens,
279-
Entry.DepDirectives);
280-
281253
Entry.Buffer = std::move(*Buffer);
282254
Entry.Status = llvm::vfs::Status(
283255
PathRef, MaybeStatus->getUniqueID(),
@@ -362,7 +334,18 @@ DependencyScanningCASFilesystem::openFileForRead(const Twine &Path) {
362334
std::optional<ArrayRef<dependency_directives_scan::Directive>>
363335
DependencyScanningCASFilesystem::getDirectiveTokens(const Twine &Path) {
364336
LookupPathResult Result = lookupPath(Path);
365-
if (Result.Entry && !Result.Entry->DepDirectives.empty())
366-
return ArrayRef(Result.Entry->DepDirectives);
337+
338+
if (Result.Entry) {
339+
if (Result.Entry->DepDirectives.empty()) {
340+
SmallString<256> PathStorage;
341+
StringRef PathRef = Path.toStringRef(PathStorage);
342+
FileEntry &Entry = const_cast<FileEntry &>(*Result.Entry);
343+
scanForDirectives(*Entry.CASContents, PathRef, Entry.DepTokens,
344+
Entry.DepDirectives);
345+
}
346+
347+
if (!Result.Entry->DepDirectives.empty())
348+
return ArrayRef(Result.Entry->DepDirectives);
349+
}
367350
return std::nullopt;
368351
}

0 commit comments

Comments
 (0)