From: Eric Liu <ioeric@google.com> Date: Fri, 18 May 2018 16:06:19 +0000 (+0000) Subject: [Tooling] Add an overload of runToolOnCodeWithArgs that takes VFS X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=20e9101add0b8e12a1cedfb0c3092f030acaa897;p=clang [Tooling] Add an overload of runToolOnCodeWithArgs that takes VFS Summary: ... to support purely VFS-based tools. Reviewers: bkramer Subscribers: klimek, cfe-commits Differential Revision: https://reviews.llvm.org/D47074 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@332731 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index ac58696c1c..c056894b1a 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -187,6 +187,15 @@ bool runToolOnCodeWithArgs( std::make_shared<PCHContainerOperations>(), const FileContentMappings &VirtualMappedFiles = FileContentMappings()); +// Similar to the overload except this takes a VFS. +bool runToolOnCodeWithArgs( + FrontendAction *ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr<vfs::FileSystem> VFS, + const std::vector<std::string> &Args, const Twine &FileName = "input.cc", + const Twine &ToolName = "clang-tool", + std::shared_ptr<PCHContainerOperations> PCHContainerOps = + std::make_shared<PCHContainerOperations>()); + /// Builds an AST for 'Code'. /// /// \param Code C++ code. diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index 9ef578b362..c22ad93c76 100644 --- a/lib/Tooling/Tooling.cpp +++ b/lib/Tooling/Tooling.cpp @@ -155,27 +155,37 @@ namespace tooling { bool runToolOnCodeWithArgs( FrontendAction *ToolAction, const Twine &Code, + llvm::IntrusiveRefCntPtr<vfs::FileSystem> VFS, const std::vector<std::string> &Args, const Twine &FileName, const Twine &ToolName, - std::shared_ptr<PCHContainerOperations> PCHContainerOps, - const FileContentMappings &VirtualMappedFiles) { + std::shared_ptr<PCHContainerOperations> PCHContainerOps) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( - new vfs::OverlayFileSystem(vfs::getRealFileSystem())); - llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( - new vfs::InMemoryFileSystem); - OverlayFileSystem->pushOverlay(InMemoryFileSystem); + llvm::IntrusiveRefCntPtr<FileManager> 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<std::string> &Args, const Twine &FileName, + const Twine &ToolName, + std::shared_ptr<PCHContainerOperations> PCHContainerOps, + const FileContentMappings &VirtualMappedFiles) { + llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( + new vfs::OverlayFileSystem(vfs::getRealFileSystem())); + llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> 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 @@ bool runToolOnCodeWithArgs( llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); } - return Invocation.run(); + return runToolOnCodeWithArgs(ToolAction, Code, OverlayFileSystem, Args, + FileName, ToolName); } std::string getAbsolutePath(StringRef File) {