10#include "llvm/Support/MemoryBuffer.h"
11#include "llvm/Support/SmallVectorMemoryBuffer.h"
12#include "llvm/Support/Threading.h"
16using namespace tooling;
17using namespace dependencies;
19llvm::ErrorOr<DependencyScanningWorkerFilesystem::TentativeEntry>
20DependencyScanningWorkerFilesystem::readFile(StringRef
Filename) {
22 auto MaybeFile = getUnderlyingFS().openFileForRead(
Filename);
24 return MaybeFile.getError();
25 auto File = std::move(*MaybeFile);
27 auto MaybeStat =
File->status();
29 return MaybeStat.getError();
30 auto Stat = std::move(*MaybeStat);
32 auto MaybeBuffer =
File->getBuffer(Stat.getName());
34 return MaybeBuffer.getError();
35 auto Buffer = std::move(*MaybeBuffer);
38 if (Stat.getSize() != Buffer->getBufferSize())
39 Stat = llvm::vfs::Status::copyWithNewSize(Stat, Buffer->getBufferSize());
41 return TentativeEntry(Stat, std::move(Buffer));
46 auto &Entry = Ref.Entry;
48 if (Entry.isError() || Entry.isDirectory())
52 assert(Contents &&
"contents not initialized");
58 std::lock_guard<std::mutex> GuardLock(Contents->
ValueLock);
72 Contents->
DepDirectives.store(
new std::optional<DependencyDirectivesTy>());
81 new std::optional<DependencyDirectivesTy>(std::move(Directives)));
93 std::max(2u, llvm::hardware_concurrency().compute_thread_count() / 4);
94 CacheShards = std::make_unique<CacheShard[]>(NumShards);
100 assert(llvm::sys::path::is_absolute_gnu(
Filename));
106 llvm::sys::fs::UniqueID UID)
const {
107 auto Hash = llvm::hash_combine(UID.getDevice(), UID.getFile());
108 return CacheShards[Hash % NumShards];
114 assert(llvm::sys::path::is_absolute_gnu(
Filename));
115 std::lock_guard<std::mutex> LockGuard(
CacheLock);
122 llvm::sys::fs::UniqueID UID)
const {
123 std::lock_guard<std::mutex> LockGuard(CacheLock);
124 auto It = EntriesByUID.find(UID);
125 return It == EntriesByUID.end() ? nullptr : It->getSecond();
131 llvm::ErrorOr<llvm::vfs::Status> Stat) {
132 std::lock_guard<std::mutex> LockGuard(CacheLock);
133 auto Insertion = EntriesByFilename.insert({
Filename,
nullptr});
134 if (Insertion.second)
135 Insertion.first->second =
137 return *Insertion.first->second;
142 llvm::sys::fs::UniqueID UID, llvm::vfs::Status Stat,
143 std::unique_ptr<llvm::MemoryBuffer> Contents) {
144 std::lock_guard<std::mutex> LockGuard(CacheLock);
145 auto Insertion = EntriesByUID.insert({UID,
nullptr});
146 if (Insertion.second) {
149 StoredContents =
new (ContentsStorage.Allocate())
151 Insertion.first->second =
new (EntryStorage.Allocate())
154 return *Insertion.first->second;
161 std::lock_guard<std::mutex> LockGuard(CacheLock);
162 return *EntriesByFilename.insert({
Filename, &Entry}).first->getValue();
166 StringRef Ext = llvm::sys::path::extension(
Filename);
176 llvm::vfs::ProxyFileSystem>(
std::move(FS)),
177 SharedCache(SharedCache),
178 WorkingDirForCacheLookup(
llvm::errc::invalid_argument) {
179 updateWorkingDirForCacheLookup();
183DependencyScanningWorkerFilesystem::getOrEmplaceSharedEntryForUID(
184 TentativeEntry TEntry) {
185 auto &Shard = SharedCache.
getShardForUID(TEntry.Status.getUniqueID());
187 std::move(TEntry.Status),
188 std::move(TEntry.Contents));
192DependencyScanningWorkerFilesystem::findEntryByFilenameWithWriteThrough(
197 if (
const auto *Entry = Shard.findEntryByFilename(
Filename))
202llvm::ErrorOr<const CachedFileSystemEntry &>
203DependencyScanningWorkerFilesystem::computeAndStoreResult(
204 StringRef OriginalFilename, StringRef FilenameForLookup) {
205 llvm::ErrorOr<llvm::vfs::Status> Stat =
206 getUnderlyingFS().status(OriginalFilename);
211 getOrEmplaceSharedEntryForFilename(FilenameForLookup, Stat.getError());
212 return insertLocalEntryForFilename(FilenameForLookup, Entry);
215 if (
const auto *Entry = findSharedEntryByUID(*Stat))
216 return insertLocalEntryForFilename(FilenameForLookup, *Entry);
219 Stat->isDirectory() ? TentativeEntry(*Stat) : readFile(OriginalFilename);
223 const auto &UIDEntry = getOrEmplaceSharedEntryForUID(std::move(*TEntry));
224 return &getOrInsertSharedEntryForFilename(FilenameForLookup, UIDEntry);
226 return &getOrEmplaceSharedEntryForFilename(FilenameForLookup,
230 return insertLocalEntryForFilename(FilenameForLookup, *SharedEntry);
233llvm::ErrorOr<EntryRef>
235 StringRef OriginalFilename) {
236 StringRef FilenameForLookup;
238 if (llvm::sys::path::is_absolute_gnu(OriginalFilename)) {
239 FilenameForLookup = OriginalFilename;
240 }
else if (!WorkingDirForCacheLookup) {
241 return WorkingDirForCacheLookup.getError();
243 StringRef RelFilename = OriginalFilename;
244 RelFilename.consume_front(
"./");
245 PathBuf = *WorkingDirForCacheLookup;
246 llvm::sys::path::append(PathBuf, RelFilename);
247 FilenameForLookup = PathBuf.str();
249 assert(llvm::sys::path::is_absolute_gnu(FilenameForLookup));
250 if (
const auto *Entry =
251 findEntryByFilenameWithWriteThrough(FilenameForLookup))
253 auto MaybeEntry = computeAndStoreResult(OriginalFilename, FilenameForLookup);
255 return MaybeEntry.getError();
259llvm::ErrorOr<llvm::vfs::Status>
262 StringRef
Filename = Path.toStringRef(OwnedFilename);
265 return getUnderlyingFS().status(Path);
270 return Result->getStatus();
277class DepScanFile final :
public llvm::vfs::File {
279 DepScanFile(std::unique_ptr<llvm::MemoryBuffer> Buffer,
280 llvm::vfs::Status Stat)
281 : Buffer(
std::move(Buffer)), Stat(
std::move(Stat)) {}
283 static llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> create(
EntryRef Entry);
285 llvm::ErrorOr<llvm::vfs::Status> status()
override {
return Stat; }
287 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
288 getBuffer(
const Twine &Name, int64_t FileSize,
bool RequiresNullTerminator,
289 bool IsVolatile)
override {
290 return std::move(Buffer);
293 std::error_code close()
override {
return {}; }
296 std::unique_ptr<llvm::MemoryBuffer> Buffer;
297 llvm::vfs::Status Stat;
302llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
303DepScanFile::create(
EntryRef Entry) {
304 assert(!Entry.
isError() &&
"error");
307 return std::make_error_code(std::errc::is_a_directory);
309 auto Result = std::make_unique<DepScanFile>(
310 llvm::MemoryBuffer::getMemBuffer(Entry.
getContents(),
315 return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
316 std::unique_ptr<llvm::vfs::File>(std::move(
Result)));
319llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
322 StringRef
Filename = Path.toStringRef(OwnedFilename);
325 return getUnderlyingFS().openFileForRead(Path);
330 return DepScanFile::create(
Result.get());
335 std::error_code EC = ProxyFileSystem::setCurrentWorkingDirectory(Path);
336 updateWorkingDirForCacheLookup();
340void DependencyScanningWorkerFilesystem::updateWorkingDirForCacheLookup() {
341 llvm::ErrorOr<std::string> CWD =
342 getUnderlyingFS().getCurrentWorkingDirectory();
344 WorkingDirForCacheLookup = CWD.getError();
345 }
else if (!llvm::sys::path::is_absolute_gnu(*CWD)) {
346 WorkingDirForCacheLookup = llvm::errc::invalid_argument;
348 WorkingDirForCacheLookup = *CWD;
350 assert(!WorkingDirForCacheLookup ||
351 llvm::sys::path::is_absolute_gnu(*WorkingDirForCacheLookup));
static bool shouldCacheStatFailures(StringRef Filename)
An in-memory representation of a file system entity that is of interest to the dependency scanning fi...
std::error_code getError() const
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.
The JSON file list parser is used to communicate input to InstallAPI.
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.
YAML serialization mapping.
hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID)
Contents and directive tokens of a cached file entry.
std::mutex ValueLock
The mutex that must be locked before mutating directive tokens.
std::atomic< const std::optional< DependencyDirectivesTy > * > DepDirectives
Accessor to the directive tokens that's atomic to avoid data races.
std::unique_ptr< llvm::MemoryBuffer > Original
Owning storage for the original contents.
SmallVector< dependency_directives_scan::Token, 10 > DepDirectiveTokens