diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningTool.h @@ -68,7 +68,9 @@ class DependencyScanningTool { public: /// Construct a dependency scanning tool. - DependencyScanningTool(DependencyScanningService &Service); + DependencyScanningTool(DependencyScanningService &Service, + llvm::IntrusiveRefCntPtr FS = + llvm::vfs::createPhysicalFileSystem()); /// Print out the dependency information into a string using the dependency /// file format that is specified in the options (-MD is the default) and diff --git a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h --- a/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h +++ b/clang/include/clang/Tooling/DependencyScanning/DependencyScanningWorker.h @@ -52,7 +52,8 @@ /// using the regular processing run. class DependencyScanningWorker { public: - DependencyScanningWorker(DependencyScanningService &Service); + DependencyScanningWorker(DependencyScanningService &Service, + llvm::IntrusiveRefCntPtr FS); /// Run the dependency scanning tool for a given clang driver command-line, /// and report the discovered dependencies to the provided consumer. If \p diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningTool.cpp @@ -50,8 +50,9 @@ } DependencyScanningTool::DependencyScanningTool( - DependencyScanningService &Service) - : Worker(Service) {} + DependencyScanningService &Service, + llvm::IntrusiveRefCntPtr FS) + : Worker(Service, std::move(FS)) {} llvm::Expected DependencyScanningTool::getDependencyFile( const std::vector &CommandLine, StringRef CWD, diff --git a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp --- a/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp +++ b/clang/lib/Tooling/DependencyScanning/DependencyScanningWorker.cpp @@ -264,7 +264,8 @@ } // end anonymous namespace DependencyScanningWorker::DependencyScanningWorker( - DependencyScanningService &Service) + DependencyScanningService &Service, + llvm::IntrusiveRefCntPtr FS) : Format(Service.getFormat()), OptimizeArgs(Service.canOptimizeArgs()) { PCHContainerOps = std::make_shared(); PCHContainerOps->registerReader( @@ -274,8 +275,8 @@ PCHContainerOps->registerWriter( std::make_unique()); - auto OverlayFS = llvm::makeIntrusiveRefCnt( - llvm::vfs::createPhysicalFileSystem()); + auto OverlayFS = + llvm::makeIntrusiveRefCnt(std::move(FS)); InMemoryFS = llvm::makeIntrusiveRefCnt(); OverlayFS->pushOverlay(InMemoryFS); RealFS = OverlayFS; diff --git a/clang/unittests/Tooling/DependencyScannerTest.cpp b/clang/unittests/Tooling/DependencyScannerTest.cpp --- a/clang/unittests/Tooling/DependencyScannerTest.cpp +++ b/clang/unittests/Tooling/DependencyScannerTest.cpp @@ -14,19 +14,21 @@ #include "clang/Frontend/FrontendAction.h" #include "clang/Frontend/FrontendActions.h" #include "clang/Tooling/CompilationDatabase.h" -#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h" +#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h" #include "clang/Tooling/Tooling.h" #include "llvm/ADT/STLExtras.h" #include "llvm/MC/TargetRegistry.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Path.h" #include "llvm/Support/TargetSelect.h" +#include "llvm/Testing/Support/Error.h" #include "gtest/gtest.h" #include #include using namespace clang; using namespace tooling; +using namespace dependencies; namespace { @@ -203,3 +205,33 @@ EXPECT_EQ(convert_to_slash(Deps[4]), "/root/header.h"); EXPECT_EQ(convert_to_slash(Deps[5]), "/root/symlink.h"); } + +TEST(DependencyScanner, ScanDepsWithFS) { + std::vector CommandLine = {"clang", "-c", "test.cpp", + "-o" + "test.cpp.o"}; + StringRef CWD = "/root"; + + auto VFS = new llvm::vfs::InMemoryFileSystem(); + VFS->setCurrentWorkingDirectory(CWD); + auto Sept = llvm::sys::path::get_separator(); + std::string HeaderPath = + std::string(llvm::formatv("{0}root{0}header.h", Sept)); + std::string TestPath = std::string(llvm::formatv("{0}root{0}test.cpp", Sept)); + + VFS->addFile(HeaderPath, 0, llvm::MemoryBuffer::getMemBuffer("\n")); + VFS->addFile(TestPath, 0, + llvm::MemoryBuffer::getMemBuffer("#include \"header.h\"\n")); + + DependencyScanningService Service(ScanningMode::DependencyDirectivesScan, + ScanningOutputFormat::Make); + DependencyScanningTool ScanTool(Service, VFS); + + std::string DepFile; + ASSERT_THAT_ERROR( + ScanTool.getDependencyFile(CommandLine, CWD).moveInto(DepFile), + llvm::Succeeded()); + using llvm::sys::path::convert_to_slash; + EXPECT_EQ(convert_to_slash(DepFile), + "test.cpp.o: /root/test.cpp /root/header.h\n"); +}