Skip to content

[clang][deps][CAS] Lazy dependency directives #8466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ class DependencyScanningCASFilesystem : public llvm::cas::ThreadSafeFileSystem {
getDirectiveTokens(const Twine &Path);

private:
/// Check whether the file should be scanned for preprocessor directives.
bool shouldScanForDirectives(StringRef Filename);

IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;

struct FileEntry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,11 @@ DependencyScanningCASFilesystem::getOriginal(cas::CASID InputDataID) {
return Blob.takeError();
}

/// Whitelist file extensions that should be minimized, treating no extension as
/// a source file that should be minimized.
///
/// This is kinda hacky, it would be better if we knew what kind of file Clang
/// was expecting instead.
static bool shouldScanForDirectivesBasedOnExtension(StringRef Filename) {
StringRef Ext = llvm::sys::path::extension(Filename);
if (Ext.empty())
return true; // C++ standard library
return llvm::StringSwitch<bool>(Ext)
.CasesLower(".c", ".cc", ".cpp", ".c++", ".cxx", true)
.CasesLower(".h", ".hh", ".hpp", ".h++", ".hxx", true)
.CasesLower(".m", ".mm", true)
.CasesLower(".i", ".ii", ".mi", ".mmi", true)
.CasesLower(".def", ".inc", true)
.Default(false);
}

static bool shouldCacheStatFailures(StringRef Filename) {
StringRef Ext = llvm::sys::path::extension(Filename);
if (Ext.empty())
return false; // This may be the module cache directory.
return shouldScanForDirectivesBasedOnExtension(
Filename); // Only cache stat failures on source files.
}

bool DependencyScanningCASFilesystem::shouldScanForDirectives(
StringRef RawFilename) {
return shouldScanForDirectivesBasedOnExtension(RawFilename);
return true;
}

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

if (shouldScanForDirectives(PathRef))
scanForDirectives(*CAS.getReference(*FileID), PathRef, Entry.DepTokens,
Entry.DepDirectives);

Entry.Buffer = std::move(*Buffer);
Entry.Status = llvm::vfs::Status(
PathRef, MaybeStatus->getUniqueID(),
Expand Down Expand Up @@ -362,7 +334,18 @@ DependencyScanningCASFilesystem::openFileForRead(const Twine &Path) {
std::optional<ArrayRef<dependency_directives_scan::Directive>>
DependencyScanningCASFilesystem::getDirectiveTokens(const Twine &Path) {
LookupPathResult Result = lookupPath(Path);
if (Result.Entry && !Result.Entry->DepDirectives.empty())
return ArrayRef(Result.Entry->DepDirectives);

if (Result.Entry) {
if (Result.Entry->DepDirectives.empty()) {
SmallString<256> PathStorage;
StringRef PathRef = Path.toStringRef(PathStorage);
FileEntry &Entry = const_cast<FileEntry &>(*Result.Entry);
scanForDirectives(*Entry.CASContents, PathRef, Entry.DepTokens,
Entry.DepDirectives);
}

if (!Result.Entry->DepDirectives.empty())
return ArrayRef(Result.Entry->DepDirectives);
}
return std::nullopt;
}