Index: include/clang/Tooling/Tooling.h =================================================================== --- include/clang/Tooling/Tooling.h +++ include/clang/Tooling/Tooling.h @@ -187,6 +187,15 @@ std::make_shared(), const FileContentMappings &VirtualMappedFiles = FileContentMappings()); +// Similar to the overload except this takes a VFS. +bool runToolOnCodeWithArgs( + FrontendAction *ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr VFS, + const std::vector &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr PCHContainerOps = + std::make_shared()); + /// Builds an AST for 'Code'. /// /// \param Code C++ code. Index: lib/Tooling/Tooling.cpp =================================================================== --- lib/Tooling/Tooling.cpp +++ lib/Tooling/Tooling.cpp @@ -155,27 +155,37 @@ bool runToolOnCodeWithArgs( FrontendAction *ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr VFS, const std::vector &Args, const Twine &FileName, const Twine &ToolName, - std::shared_ptr PCHContainerOps, - const FileContentMappings &VirtualMappedFiles) { + std::shared_ptr PCHContainerOps) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - llvm::IntrusiveRefCntPtr OverlayFileSystem( - new vfs::OverlayFileSystem(vfs::getRealFileSystem())); - llvm::IntrusiveRefCntPtr InMemoryFileSystem( - new vfs::InMemoryFileSystem); - OverlayFileSystem->pushOverlay(InMemoryFileSystem); + llvm::IntrusiveRefCntPtr Files( - new FileManager(FileSystemOptions(), OverlayFileSystem)); + new FileManager(FileSystemOptions(), VFS)); ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(); ToolInvocation Invocation( getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), ToolAction, Files.get(), std::move(PCHContainerOps)); + return Invocation.run(); +} + +bool runToolOnCodeWithArgs( + FrontendAction *ToolAction, const Twine &Code, + const std::vector &Args, const Twine &FileName, + const Twine &ToolName, + std::shared_ptr PCHContainerOps, + const FileContentMappings &VirtualMappedFiles) { + llvm::IntrusiveRefCntPtr OverlayFileSystem( + new vfs::OverlayFileSystem(vfs::getRealFileSystem())); + llvm::IntrusiveRefCntPtr InMemoryFileSystem( + new vfs::InMemoryFileSystem); + OverlayFileSystem->pushOverlay(InMemoryFileSystem); SmallString<1024> CodeStorage; - InMemoryFileSystem->addFile(FileNameRef, 0, + InMemoryFileSystem->addFile(FileName, 0, llvm::MemoryBuffer::getMemBuffer( Code.toNullTerminatedStringRef(CodeStorage))); @@ -185,7 +195,8 @@ llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); } - return Invocation.run(); + return runToolOnCodeWithArgs(ToolAction, Code, OverlayFileSystem, Args, + FileName, ToolName); } std::string getAbsolutePath(StringRef File) {