diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h @@ -46,7 +46,7 @@ /// mismatching size of the file. If file is not minimized, the full file is /// read and copied into memory to ensure that it's not memory mapped to avoid /// running out of file descriptors. - static CachedFileSystemEntry createFileEntry(StringRef Filename, + static CachedFileSystemEntry createFileEntry(llvm::vfs::Status &&Stat, llvm::vfs::FileSystem &FS, bool Minimize = true); @@ -80,6 +80,11 @@ return MaybeStat->getName(); } + std::error_code getError() const { + assert(isValid() && "not initialized"); + return MaybeStat.getError(); + } + /// Return the mapping between location -> distance that is used to speed up /// the block skipping in the preprocessor. const PreprocessorSkippedRangeMapping &getPPSkippedRangeMapping() const { @@ -168,6 +173,9 @@ return It == Cache.end() ? nullptr : It->getValue(); } + llvm::ErrorOr + getOrCreateFileSystemEntry(const StringRef Filename); + DependencyScanningFilesystemSharedCache &SharedCache; /// The local cache is used by the worker thread to cache file system queries /// locally instead of querying the global cache every time. diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningFilesystem.cpp @@ -15,20 +15,19 @@ using namespace tooling; using namespace dependencies; + CachedFileSystemEntry CachedFileSystemEntry::createFileEntry( - StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize) { + llvm::vfs::Status &&Stat, llvm::vfs::FileSystem &FS, bool Minimize) { // Load the file and its content from the file system. + StringRef Filename = Stat.getName(); llvm::ErrorOr> MaybeFile = FS.openFileForRead(Filename); if (!MaybeFile) return MaybeFile.getError(); - llvm::ErrorOr Stat = (*MaybeFile)->status(); - if (!Stat) - return Stat.getError(); llvm::vfs::File &F = **MaybeFile; llvm::ErrorOr> MaybeBuffer = - F.getBuffer(Stat->getName()); + F.getBuffer(Filename); if (!MaybeBuffer) return MaybeBuffer.getError(); @@ -42,7 +41,7 @@ // if the minimization failed. // FIXME: Propage the diagnostic if desired by the client. CachedFileSystemEntry Result; - Result.MaybeStat = std::move(*Stat); + Result.MaybeStat = Stat; Result.Contents.reserve(Buffer->getBufferSize() + 1); Result.Contents.append(Buffer->getBufferStart(), Buffer->getBufferEnd()); // Implicitly null terminate the contents for Clang's lexer. @@ -53,10 +52,10 @@ 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()); + Result.MaybeStat = llvm::vfs::Status(Stat.getName(), Stat.getUniqueID(), + Stat.getLastModificationTime(), + Stat.getUser(), Stat.getGroup(), 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"); @@ -122,14 +121,11 @@ return It.first->getValue(); } -llvm::ErrorOr -DependencyScanningWorkerFilesystem::status(const Twine &Path) { - SmallString<256> OwnedFilename; - StringRef Filename = Path.toStringRef(OwnedFilename); - - // Check the local cache first. - if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename)) - return Entry->getStatus(); +llvm::ErrorOr +DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry(const StringRef Filename) { + if (const CachedFileSystemEntry* Entry = getCachedEntry(Filename)) { + return Entry; + } // FIXME: Handle PCM/PCH files. // FIXME: Handle module map files. @@ -152,7 +148,7 @@ std::move(*MaybeStatus)); else CacheEntry = CachedFileSystemEntry::createFileEntry( - Filename, FS, !KeepOriginalSource); + std::move(*MaybeStatus), FS, !KeepOriginalSource); } Result = &CacheEntry; @@ -160,7 +156,17 @@ // Store the result in the local cache. setCachedEntry(Filename, Result); - return Result->getStatus(); + return Result; +} + +llvm::ErrorOr +DependencyScanningWorkerFilesystem::status(const Twine &Path) { + SmallString<256> OwnedFilename; + StringRef Filename = Path.toStringRef(OwnedFilename); + const llvm::ErrorOr Result = getOrCreateFileSystemEntry(Filename); + if (!Result) + return Result.getError(); + return (*Result)->getStatus(); } namespace { @@ -217,30 +223,8 @@ SmallString<256> OwnedFilename; StringRef Filename = Path.toStringRef(OwnedFilename); - // Check the local cache first. - if (const CachedFileSystemEntry *Entry = getCachedEntry(Filename)) - return createFile(Entry, PPSkipMappings); - - // FIXME: Handle PCM/PCH files. - // FIXME: Handle module map files. - - bool KeepOriginalSource = IgnoredFiles.count(Filename); - DependencyScanningFilesystemSharedCache::SharedFileSystemEntry - &SharedCacheEntry = SharedCache.get(Filename); - const CachedFileSystemEntry *Result; - { - std::unique_lock LockGuard(SharedCacheEntry.ValueLock); - CachedFileSystemEntry &CacheEntry = SharedCacheEntry.Value; - - if (!CacheEntry.isValid()) { - CacheEntry = CachedFileSystemEntry::createFileEntry( - Filename, getUnderlyingFS(), !KeepOriginalSource); - } - - Result = &CacheEntry; - } - - // Store the result in the local cache. - setCachedEntry(Filename, Result); - return createFile(Result, PPSkipMappings); + const llvm::ErrorOr Result = getOrCreateFileSystemEntry(Filename); + if (!Result) + return Result.getError(); + return createFile(Result.get(), PPSkipMappings); } diff --git a/clang/test/ClangScanDeps/Inputs/headerwithdirnamefollowedbyinclude.json b/clang/test/ClangScanDeps/Inputs/headerwithdirnamefollowedbyinclude.json new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/Inputs/headerwithdirnamefollowedbyinclude.json @@ -0,0 +1,7 @@ +[ + { + "directory": "DIR", + "command": "clang -c -IDIR -IInputs DIR/headerwithdirname_input.cpp", + "file": "DIR/headerwithdirname_input.cpp" + } +] diff --git a/clang/test/ClangScanDeps/headerwithdirnamefollowedbyinclude.cpp b/clang/test/ClangScanDeps/headerwithdirnamefollowedbyinclude.cpp new file mode 100644 --- /dev/null +++ b/clang/test/ClangScanDeps/headerwithdirnamefollowedbyinclude.cpp @@ -0,0 +1,21 @@ +// RUN: rm -rf %t.dir +// RUN: rm -rf %t.dir/foodir +// RUN: rm -rf %t.cdb + +// RUN: mkdir -p %t.dir +// RUN: mkdir -p %t.dir/foodir + +// RUN: cp %S/Inputs/header.h %t.dir/foodir/foodirheader.h +// RUN: cp %s %t.dir/headerwithdirname_input.cpp +// RUN: mkdir %t.dir/Inputs +// RUN: cp %S/Inputs/foodir %t.dir/Inputs/foodir +// RUN: sed -e "s|DIR|%/t.dir|g" %S/Inputs/headerwithdirnamefollowedbyinclude.json > %t.cdb +// +// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 | FileCheck %s + +#include +#include "foodir/foodirheader.h" + +// CHECK: headerwithdirname_input.o +// CHECK-NEXT: headerwithdirname_input.cpp +// CHECK-NEXT: Inputs{{/|\\}}foodir