Skip to content

Commit

Permalink
[clang][deps] NFC: Move entry initialization into member functions
Browse files Browse the repository at this point in the history
This is a prep-patch for making `CachedFileSystemEntry` initialization more lazy.
  • Loading branch information
jansvoboda11 committed Dec 15, 2021
1 parent d83dc4c commit da920c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 53 deletions.
Expand Up @@ -36,20 +36,17 @@ class CachedFileSystemEntry {
/// 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 {
Expand Down Expand Up @@ -203,11 +200,6 @@ class DependencyScanningWorkerFilesystem : public llvm::vfs::ProxyFileSystem {
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
Expand Down
Expand Up @@ -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();

Expand All @@ -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->getType(), Stat->getPermissions());
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
Expand All @@ -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() {
Expand Down Expand Up @@ -157,17 +145,13 @@ bool DependencyScanningWorkerFilesystem::shouldMinimize(StringRef RawFilename) {
return !NotToBeMinimized.contains(Filename);
}

CachedFileSystemEntry DependencyScanningWorkerFilesystem::createFileSystemEntry(
llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus, StringRef Filename,
bool ShouldMinimize) {
if (!MaybeStatus)
return CachedFileSystemEntry(MaybeStatus.getError());

if (MaybeStatus->isDirectory())
return CachedFileSystemEntry::createDirectoryEntry(std::move(*MaybeStatus));

return CachedFileSystemEntry::createFileEntry(Filename, getUnderlyingFS(),
ShouldMinimize);
void CachedFileSystemEntry::init(llvm::ErrorOr<llvm::vfs::Status> &&MaybeStatus,
StringRef Filename, llvm::vfs::FileSystem &FS,
bool ShouldMinimize) {
if (!MaybeStatus || MaybeStatus->isDirectory())
MaybeStat = std::move(MaybeStatus);
else
MaybeStat = initFile(Filename, FS, ShouldMinimize);
}

llvm::ErrorOr<const CachedFileSystemEntry *>
Expand Down Expand Up @@ -196,8 +180,8 @@ 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,
ShouldMinimize);
CacheEntry.init(std::move(MaybeStatus), Filename, getUnderlyingFS(),
ShouldMinimize);
}

Result = &CacheEntry;
Expand Down

0 comments on commit da920c3

Please sign in to comment.