diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h index e64be07d9ab4..ea3b0e042140 100644 --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -296,10 +296,14 @@ class ClangTool { /// not found in Compilations, it is skipped. /// \param PCHContainerOps The PCHContainerOperations for loading and creating /// clang modules. + /// \param BaseFS VFS used for all underlying file accesses when running the + /// tool. ClangTool(const CompilationDatabase &Compilations, ArrayRef SourcePaths, std::shared_ptr PCHContainerOps = - std::make_shared()); + std::make_shared(), + IntrusiveRefCntPtr BaseFS = + vfs::getRealFileSystem()); ~ClangTool(); diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp index 4fbfa4f00473..b6185086176a 100644 --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -328,10 +328,11 @@ bool FrontendActionFactory::runInvocation( ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef SourcePaths, - std::shared_ptr PCHContainerOps) + std::shared_ptr PCHContainerOps, + IntrusiveRefCntPtr BaseFS) : Compilations(Compilations), SourcePaths(SourcePaths), PCHContainerOps(std::move(PCHContainerOps)), - OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())), + OverlayFileSystem(new vfs::OverlayFileSystem(BaseFS)), InMemoryFileSystem(new vfs::InMemoryFileSystem), Files(new FileManager(FileSystemOptions(), OverlayFileSystem)), DiagConsumer(nullptr) { diff --git a/clang/unittests/Tooling/ToolingTest.cpp b/clang/unittests/Tooling/ToolingTest.cpp index 891907a4d081..6dd0e53c4ddc 100644 --- a/clang/unittests/Tooling/ToolingTest.cpp +++ b/clang/unittests/Tooling/ToolingTest.cpp @@ -402,6 +402,24 @@ TEST(ClangToolTest, ArgumentAdjusters) { EXPECT_FALSE(Found); } +TEST(ClangToolTest, BaseVirtualFileSystemUsage) { + FixedCompilationDatabase Compilations("/", std::vector()); + llvm::IntrusiveRefCntPtr OverlayFileSystem( + new vfs::OverlayFileSystem(vfs::getRealFileSystem())); + llvm::IntrusiveRefCntPtr InMemoryFileSystem( + new vfs::InMemoryFileSystem); + OverlayFileSystem->pushOverlay(InMemoryFileSystem); + + InMemoryFileSystem->addFile( + "a.cpp", 0, llvm::MemoryBuffer::getMemBuffer("int main() {}")); + + ClangTool Tool(Compilations, std::vector(1, "a.cpp"), + std::make_shared(), OverlayFileSystem); + std::unique_ptr Action( + newFrontendActionFactory()); + EXPECT_EQ(0, Tool.run(Action.get())); +} + // Check getClangStripDependencyFileAdjuster doesn't strip args after -MD/-MMD. TEST(ClangToolTest, StripDependencyFileAdjuster) { FixedCompilationDatabase Compilations("/", {"-MD", "-c", "-MMD", "-w"});