10#include "llvm/Support/MemoryBuffer.h"
11#include "llvm/Support/Threading.h"
17llvm::ErrorOr<DependencyScanningWorkerFilesystem::TentativeEntry>
18DependencyScanningWorkerFilesystem::readFile(StringRef Filename) {
20 auto MaybeFile = getUnderlyingFS().openFileForRead(Filename);
22 return MaybeFile.getError();
23 auto File = std::move(*MaybeFile);
25 auto MaybeStat =
File->status();
27 return MaybeStat.getError();
28 auto Stat = std::move(*MaybeStat);
30 auto MaybeBuffer =
File->getBuffer(Stat.getName());
32 return MaybeBuffer.getError();
33 auto Buffer = std::move(*MaybeBuffer);
36 if (Stat.getSize() != Buffer->getBufferSize())
37 Stat = llvm::vfs::Status::copyWithNewSize(Stat, Buffer->getBufferSize());
39 return TentativeEntry(Stat, std::move(Buffer));
44 auto &Entry = Ref.Entry;
46 if (Entry.isError() || Entry.isDirectory())
50 assert(Contents &&
"contents not initialized");
56 std::lock_guard<std::mutex> GuardLock(Contents->
ValueLock);
70 Contents->
DepDirectives.store(
new std::optional<DependencyDirectivesTy>());
79 new std::optional<DependencyDirectivesTy>(std::move(Directives)));
91 std::max(2u, llvm::hardware_concurrency().compute_thread_count() / 4);
92 CacheShards = std::make_unique<CacheShard[]>(NumShards);
97 StringRef Filename)
const {
98 assert(llvm::sys::path::is_absolute_gnu(Filename));
104 llvm::sys::fs::UniqueID UID)
const {
105 auto Hash = llvm::hash_combine(UID.getDevice(), UID.getFile());
106 return CacheShards[Hash % NumShards];
109std::vector<DependencyScanningFilesystemSharedCache::OutOfDateEntry>
111 llvm::vfs::FileSystem &UnderlyingFS)
const {
113 std::vector<OutOfDateEntry> InvalidDiagInfo;
114 for (
unsigned i = 0; i < NumShards; i++) {
116 std::lock_guard<std::mutex> LockGuard(Shard.
CacheLock);
119 llvm::ErrorOr<llvm::vfs::Status> Status = UnderlyingFS.status(Path);
126 InvalidDiagInfo.emplace_back(Path.data());
128 llvm::vfs::Status CachedStatus = Entry->
getStatus();
129 if (Status->getType() == llvm::sys::fs::file_type::regular_file &&
130 Status->getType() == CachedStatus.getType()) {
138 uint64_t CachedSize = CachedStatus.getSize();
139 uint64_t ActualSize = Status->getSize();
140 if (CachedSize != ActualSize) {
143 InvalidDiagInfo.emplace_back(Path.data(), CachedSize, ActualSize);
150 return InvalidDiagInfo;
155 StringRef Filename)
const {
156 assert(llvm::sys::path::is_absolute_gnu(Filename));
157 std::lock_guard<std::mutex> LockGuard(
CacheLock);
164 llvm::sys::fs::UniqueID UID)
const {
165 std::lock_guard<std::mutex> LockGuard(
CacheLock);
173 llvm::ErrorOr<llvm::vfs::Status> Stat) {
174 std::lock_guard<std::mutex> LockGuard(
CacheLock);
175 auto [It, Inserted] =
CacheByFilename.insert({Filename, {
nullptr,
nullptr}});
180 assert((Inserted ||
CachedRealPath) &&
"existing file with empty pair");
189 llvm::sys::fs::UniqueID UID, llvm::vfs::Status Stat,
190 std::unique_ptr<llvm::MemoryBuffer> Contents) {
191 std::lock_guard<std::mutex> LockGuard(
CacheLock);
193 auto &CachedEntry = It->getSecond();
209 std::lock_guard<std::mutex> LockGuard(
CacheLock);
210 auto [It, Inserted] =
CacheByFilename.insert({Filename, {&Entry,
nullptr}});
212 if (!Inserted || !CachedEntry)
213 CachedEntry = &Entry;
219 StringRef Filename)
const {
220 assert(llvm::sys::path::is_absolute_gnu(Filename));
221 std::lock_guard<std::mutex> LockGuard(
CacheLock);
228 llvm::ErrorOr<llvm::StringRef> RealPath) {
229 std::lock_guard<std::mutex> LockGuard(
CacheLock);
232 if (!StoredRealPath) {
235 return RealPath.getError();
236 return RealPath->str();
243 return *StoredRealPath;
251 SharedCache(SharedCache),
252 WorkingDirForCacheLookup(
llvm::errc::invalid_argument) {
253 updateWorkingDirForCacheLookup();
257DependencyScanningWorkerFilesystem::getOrEmplaceSharedEntryForUID(
258 TentativeEntry TEntry) {
259 auto &Shard = SharedCache.
getShardForUID(TEntry.Status.getUniqueID());
261 std::move(TEntry.Status),
262 std::move(TEntry.Contents));
266DependencyScanningWorkerFilesystem::findEntryByFilenameWithWriteThrough(
267 StringRef Filename) {
271 if (
const auto *Entry = Shard.findEntryByFilename(Filename))
276llvm::ErrorOr<const CachedFileSystemEntry &>
277DependencyScanningWorkerFilesystem::computeAndStoreResult(
278 StringRef OriginalFilename, StringRef FilenameForLookup) {
279 llvm::ErrorOr<llvm::vfs::Status> Stat =
280 getUnderlyingFS().status(OriginalFilename);
283 getOrEmplaceSharedEntryForFilename(FilenameForLookup, Stat.getError());
284 return insertLocalEntryForFilename(FilenameForLookup, Entry);
287 if (
const auto *Entry = findSharedEntryByUID(*Stat))
288 return insertLocalEntryForFilename(FilenameForLookup, *Entry);
291 Stat->isDirectory() ? TentativeEntry(*Stat) : readFile(OriginalFilename);
293 const CachedFileSystemEntry *SharedEntry = [&]() {
295 const auto &UIDEntry = getOrEmplaceSharedEntryForUID(std::move(*TEntry));
296 return &getOrInsertSharedEntryForFilename(FilenameForLookup, UIDEntry);
298 return &getOrEmplaceSharedEntryForFilename(FilenameForLookup,
302 return insertLocalEntryForFilename(FilenameForLookup, *SharedEntry);
305llvm::ErrorOr<EntryRef>
307 StringRef OriginalFilename) {
309 auto FilenameForLookup = tryGetFilenameForLookup(OriginalFilename, PathBuf);
310 if (!FilenameForLookup)
311 return FilenameForLookup.getError();
313 if (
const auto *Entry =
314 findEntryByFilenameWithWriteThrough(*FilenameForLookup))
316 auto MaybeEntry = computeAndStoreResult(OriginalFilename, *FilenameForLookup);
318 return MaybeEntry.getError();
322llvm::ErrorOr<llvm::vfs::Status>
325 StringRef Filename = Path.toStringRef(OwnedFilename);
330 return Result->getStatus();
340 llvm::ErrorOr<llvm::vfs::Status> Status =
status(Path);
341 return Status && Status->exists();
348class DepScanFile final :
public llvm::vfs::File {
350 DepScanFile(std::unique_ptr<llvm::MemoryBuffer> Buffer,
351 llvm::vfs::Status Stat)
352 : Buffer(
std::move(Buffer)), Stat(
std::move(Stat)) {}
354 static llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> create(
EntryRef Entry);
356 llvm::ErrorOr<llvm::vfs::Status> status()
override {
return Stat; }
358 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
359 getBuffer(
const Twine &Name, int64_t FileSize,
bool RequiresNullTerminator,
360 bool IsVolatile)
override {
361 return llvm::MemoryBuffer::getMemBuffer(Buffer->getMemBufferRef(),
362 RequiresNullTerminator);
365 std::error_code close()
override {
return {}; }
368 std::unique_ptr<llvm::MemoryBuffer> Buffer;
369 llvm::vfs::Status Stat;
374llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
375DepScanFile::create(
EntryRef Entry) {
376 assert(!Entry.
isError() &&
"error");
379 return std::make_error_code(std::errc::is_a_directory);
381 auto Result = std::make_unique<DepScanFile>(
382 llvm::MemoryBuffer::getMemBuffer(Entry.
getContents(),
387 return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
388 std::unique_ptr<llvm::vfs::File>(std::move(
Result)));
391llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
394 StringRef Filename = Path.toStringRef(OwnedFilename);
399 return DepScanFile::create(
Result.get());
406 StringRef OriginalFilename = Path.toStringRef(OwnedFilename);
409 auto FilenameForLookup = tryGetFilenameForLookup(OriginalFilename, PathBuf);
410 if (!FilenameForLookup)
411 return FilenameForLookup.getError();
413 auto HandleCachedRealPath =
416 return RealPath.getError();
417 Output.assign(RealPath->begin(), RealPath->end());
422 if (
const auto *RealPath =
423 LocalCache.findRealPathByFilename(*FilenameForLookup))
424 return HandleCachedRealPath(*RealPath);
427 auto &Shard = SharedCache.getShardForFilename(*FilenameForLookup);
428 if (
const auto *ShardRealPath =
429 Shard.findRealPathByFilename(*FilenameForLookup)) {
430 const auto &RealPath = LocalCache.insertRealPathForFilename(
431 *FilenameForLookup, *ShardRealPath);
432 return HandleCachedRealPath(RealPath);
436 std::error_code EC = getUnderlyingFS().getRealPath(OriginalFilename, Output);
437 llvm::ErrorOr<llvm::StringRef> ComputedRealPath = EC;
439 ComputedRealPath = StringRef{Output.data(), Output.size()};
444 const auto &RealPath = Shard.getOrEmplaceRealPathForFilename(
445 *FilenameForLookup, ComputedRealPath);
446 return HandleCachedRealPath(
447 LocalCache.insertRealPathForFilename(*FilenameForLookup, RealPath));
452 std::error_code EC = ProxyFileSystem::setCurrentWorkingDirectory(Path);
453 updateWorkingDirForCacheLookup();
457void DependencyScanningWorkerFilesystem::updateWorkingDirForCacheLookup() {
458 llvm::ErrorOr<std::string> CWD =
459 getUnderlyingFS().getCurrentWorkingDirectory();
461 WorkingDirForCacheLookup = CWD.getError();
462 }
else if (!llvm::sys::path::is_absolute_gnu(*CWD)) {
463 WorkingDirForCacheLookup = llvm::errc::invalid_argument;
465 WorkingDirForCacheLookup = *CWD;
467 assert(!WorkingDirForCacheLookup ||
468 llvm::sys::path::is_absolute_gnu(*WorkingDirForCacheLookup));
471llvm::ErrorOr<StringRef>
472DependencyScanningWorkerFilesystem::tryGetFilenameForLookup(
473 StringRef OriginalFilename, llvm::SmallVectorImpl<char> &PathBuf)
const {
474 StringRef FilenameForLookup;
475 if (llvm::sys::path::is_absolute_gnu(OriginalFilename)) {
476 FilenameForLookup = OriginalFilename;
477 }
else if (!WorkingDirForCacheLookup) {
478 return WorkingDirForCacheLookup.getError();
480 StringRef RelFilename = OriginalFilename;
481 RelFilename.consume_front(
"./");
482 PathBuf.assign(WorkingDirForCacheLookup->begin(),
483 WorkingDirForCacheLookup->end());
484 llvm::sys::path::append(PathBuf, RelFilename);
485 FilenameForLookup = StringRef{PathBuf.begin(), PathBuf.size()};
487 assert(llvm::sys::path::is_absolute_gnu(FilenameForLookup));
488 return FilenameForLookup;
An in-memory representation of a file system entity that is of interest to the dependency scanning fi...
llvm::vfs::Status getStatus() const
std::error_code getError() const
const CachedFileSystemEntry & insertEntryForFilename(StringRef Filename, const CachedFileSystemEntry &Entry)
Associates the given entry with the filename and returns the given entry pointer (for convenience).
const CachedFileSystemEntry * findEntryByFilename(StringRef Filename) const
Returns entry associated with the filename or nullptr if none is found.
This class is a shared cache, that caches the 'stat' and 'open' calls to the underlying real file sys...
CacheShard & getShardForUID(llvm::sys::fs::UniqueID UID) const
CacheShard & getShardForFilename(StringRef Filename) const
Returns shard for the given key.
std::vector< OutOfDateEntry > getOutOfDateEntries(llvm::vfs::FileSystem &UnderlyingFS) const
Visits all cached entries and re-stat an entry using UnderlyingFS to check if the cache contains out-...
DependencyScanningFilesystemSharedCache()
std::error_code getRealPath(const Twine &Path, SmallVectorImpl< char > &Output) override
bool ensureDirectiveTokensArePopulated(EntryRef Entry)
Ensure the directive tokens are populated for this file entry.
bool exists(const Twine &Path) override
Check whether Path exists.
llvm::ErrorOr< EntryRef > getOrCreateFileSystemEntry(StringRef Filename)
Returns entry for the given filename.
llvm::ErrorOr< std::unique_ptr< llvm::vfs::File > > openFileForRead(const Twine &Path) override
DependencyScanningWorkerFilesystem(DependencyScanningFilesystemSharedCache &SharedCache, IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS)
std::error_code setCurrentWorkingDirectory(const Twine &Path) override
llvm::ErrorOr< llvm::vfs::Status > status(const Twine &Path) override
Reference to a CachedFileSystemEntry.
llvm::vfs::Status getStatus() const
StringRef getContents() const
llvm::ErrorOr< EntryRef > unwrapError() const
If the cached entry represents an error, promotes it into ErrorOr.
llvm::ErrorOr< std::string > CachedRealPath
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
bool scanSourceForDependencyDirectives(StringRef Input, SmallVectorImpl< dependency_directives_scan::Token > &Tokens, SmallVectorImpl< dependency_directives_scan::Directive > &Directives, DiagnosticsEngine *Diags=nullptr, SourceLocation InputSourceLoc=SourceLocation())
Scan the input for the preprocessor directives that might have an effect on the dependencies for a co...
@ Result
The result type of a method or function.
Diagnostic wrappers for TextAPI types for error reporting.
hash_code hash_value(const clang::dependencies::ModuleID &ID)
Contents and directive tokens of a cached file entry.
std::unique_ptr< llvm::MemoryBuffer > Original
Owning storage for the original contents.
SmallVector< dependency_directives_scan::Token, 10 > DepDirectiveTokens
std::atomic< const std::optional< DependencyDirectivesTy > * > DepDirectives
Accessor to the directive tokens that's atomic to avoid data races.
std::mutex ValueLock
The mutex that must be locked before mutating directive tokens.
const CachedFileSystemEntry & getOrEmplaceEntryForUID(llvm::sys::fs::UniqueID UID, llvm::vfs::Status Stat, std::unique_ptr< llvm::MemoryBuffer > Contents)
Returns entry associated with the unique ID if there is some.
llvm::SpecificBumpPtrAllocator< CachedFileSystemEntry > EntryStorage
The backing storage for cached entries.
llvm::SpecificBumpPtrAllocator< CachedFileContents > ContentsStorage
The backing storage for cached contents.
llvm::SpecificBumpPtrAllocator< CachedRealPath > RealPathStorage
The backing storage for cached real paths.
const CachedFileSystemEntry * findEntryByUID(llvm::sys::fs::UniqueID UID) const
Returns entry associated with the unique ID or nullptr if none is found.
const CachedRealPath * findRealPathByFilename(StringRef Filename) const
Returns the real path associated with the filename or nullptr if none is found.
std::mutex CacheLock
The mutex that needs to be locked before mutation of any member.
const CachedFileSystemEntry & getOrInsertEntryForFilename(StringRef Filename, const CachedFileSystemEntry &Entry)
Returns entry associated with the filename if there is some.
const CachedFileSystemEntry * findEntryByFilename(StringRef Filename) const
Returns entry associated with the filename or nullptr if none is found.
const CachedFileSystemEntry & getOrEmplaceEntryForFilename(StringRef Filename, llvm::ErrorOr< llvm::vfs::Status > Stat)
Returns entry associated with the filename if there is some.
llvm::StringMap< std::pair< const CachedFileSystemEntry *, const CachedRealPath * >, llvm::BumpPtrAllocator > CacheByFilename
Map from filenames to cached entries and real paths.
const CachedRealPath & getOrEmplaceRealPathForFilename(StringRef Filename, llvm::ErrorOr< StringRef > RealPath)
Returns the real path associated with the filename if there is some.
llvm::DenseMap< llvm::sys::fs::UniqueID, const CachedFileSystemEntry * > EntriesByUID
Map from unique IDs to cached entries.