[clang][deps] NFC: Move entry initialization into member functions

This is a prep-patch for making `CachedFileSystemEntry` initialization more lazy.
This commit is contained in:
Jan Svoboda 2021-12-14 12:36:59 +01:00
parent d83dc4c648
commit da920c3bcc
2 changed files with 29 additions and 53 deletions

View File

@ -36,20 +36,17 @@ public:
/// Creates an uninitialized entry.
CachedFileSystemEntry() : MaybeStat(llvm::vfs::Status()) {}
CachedFileSystemEntry(std::error_code Error) : MaybeStat(std::move(Error)) {}
/// Initialize the cached file system entry.
void init(llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus, StringRef Filename,
llvm::vfs::FileSystem &FS, bool ShouldMinimize = true);
/// Create an entry that represents an opened source file with minimized or
/// original contents.
/// Initialize the entry as file with minimized or original contents.
///
/// The filesystem opens the file even for `stat` calls open to avoid the
/// issues with stat + open of minimized files that might lead to a
/// mismatching size of the file.
static CachedFileSystemEntry createFileEntry(StringRef Filename,
llvm::vfs::FileSystem &FS,
bool Minimize = true);
/// Create an entry that represents a directory on the filesystem.
static CachedFileSystemEntry createDirectoryEntry(llvm::vfs::Status &&Stat);
llvm::ErrorOr<llvm::vfs::Status>
initFile(StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize = true);
/// \returns True if the entry is initialized.
bool isInitialized() const {
@ -203,11 +200,6 @@ private:
llvm::ErrorOr<const CachedFileSystemEntry *>
getOrCreateFileSystemEntry(const StringRef Filename);
/// Create a cached file system entry based on the initial status result.
CachedFileSystemEntry
createFileSystemEntry(llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus,
StringRef Filename, bool ShouldMinimize);
/// The global cache shared between worker threads.
DependencyScanningFilesystemSharedCache &SharedCache;
/// The local cache is used by the worker thread to cache file system queries

View File

@ -16,20 +16,21 @@ using namespace clang;
using namespace tooling;
using namespace dependencies;
CachedFileSystemEntry CachedFileSystemEntry::createFileEntry(
StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize) {
llvm::ErrorOr<llvm::vfs::Status>
CachedFileSystemEntry::initFile(StringRef Filename, llvm::vfs::FileSystem &FS,
bool Minimize) {
// Load the file and its content from the file system.
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> MaybeFile =
FS.openFileForRead(Filename);
if (!MaybeFile)
return MaybeFile.getError();
llvm::ErrorOr<llvm::vfs::Status> Stat = (*MaybeFile)->status();
if (!Stat)
return Stat.getError();
llvm::vfs::File &F = **MaybeFile;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MaybeBuffer =
F.getBuffer(Stat->getName());
(*MaybeFile)->getBuffer(Stat->getName());
if (!MaybeBuffer)
return MaybeBuffer.getError();
@ -42,22 +43,18 @@ CachedFileSystemEntry CachedFileSystemEntry::createFileEntry(
// Use the original file unless requested otherwise, or
// if the minimization failed.
// FIXME: Propage the diagnostic if desired by the client.
CachedFileSystemEntry Result;
Result.MaybeStat = std::move(*Stat);
Result.Contents = std::move(*MaybeBuffer);
return Result;
Contents = std::move(*MaybeBuffer);
return Stat;
}
CachedFileSystemEntry Result;
size_t Size = MinimizedFileContents.size();
Result.MaybeStat = llvm::vfs::Status(Stat->getName(), Stat->getUniqueID(),
Stat->getLastModificationTime(),
Stat->getUser(), Stat->getGroup(), Size,
Stat = llvm::vfs::Status(Stat->getName(), Stat->getUniqueID(),
Stat->getLastModificationTime(), Stat->getUser(),
Stat->getGroup(), MinimizedFileContents.size(),
Stat->getType(), Stat->getPermissions());
// The contents produced by the minimizer must be null terminated.
assert(MinimizedFileContents.data()[MinimizedFileContents.size()] == '\0' &&
"not null terminated contents");
Result.Contents = std::make_unique<llvm::SmallVectorMemoryBuffer>(
Contents = std::make_unique<llvm::SmallVectorMemoryBuffer>(
std::move(MinimizedFileContents));
// Compute the skipped PP ranges that speedup skipping over inactive
@ -76,17 +73,8 @@ CachedFileSystemEntry CachedFileSystemEntry::createFileEntry(
}
Mapping[Range.Offset] = Range.Length;
}
Result.PPSkippedRangeMapping = std::move(Mapping);
return Result;
}
CachedFileSystemEntry
CachedFileSystemEntry::createDirectoryEntry(llvm::vfs::Status &&Stat) {
assert(Stat.isDirectory() && "not a directory!");
auto Result = CachedFileSystemEntry();
Result.MaybeStat = std::move(Stat);
return Result;
PPSkippedRangeMapping = std::move(Mapping);
return Stat;
}
DependencyScanningFilesystemSharedCache::SingleCache::SingleCache() {
@ -157,17 +145,13 @@ bool DependencyScanningWorkerFilesystem::shouldMinimize(StringRef RawFilename) {
return !NotToBeMinimized.contains(Filename);
}
CachedFileSystemEntry DependencyScanningWorkerFilesystem::createFileSystemEntry(
llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus, StringRef Filename,
void CachedFileSystemEntry::init(llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus,
StringRef Filename, llvm::vfs::FileSystem &FS,
bool ShouldMinimize) {
if (!MaybeStatus)
return CachedFileSystemEntry(MaybeStatus.getError());
if (MaybeStatus->isDirectory())
return CachedFileSystemEntry::createDirectoryEntry(std::move(*MaybeStatus));
return CachedFileSystemEntry::createFileEntry(Filename, getUnderlyingFS(),
ShouldMinimize);
if (!MaybeStatus || MaybeStatus->isDirectory())
MaybeStat = std::move(MaybeStatus);
else
MaybeStat = initFile(Filename, FS, ShouldMinimize);
}
llvm::ErrorOr<const CachedFileSystemEntry *>
@ -196,7 +180,7 @@ DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry(
// files before building them, and then looks for them again. If we
// cache the stat failure, it won't see them the second time.
return MaybeStatus.getError();
CacheEntry = createFileSystemEntry(std::move(MaybeStatus), Filename,
CacheEntry.init(std::move(MaybeStatus), Filename, getUnderlyingFS(),
ShouldMinimize);
}