From 20e9101add0b8e12a1cedfb0c3092f030acaa897 Mon Sep 17 00:00:00 2001 From: Eric Liu Date: Fri, 18 May 2018 16:06:19 +0000 Subject: [PATCH] [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 --- include/clang/Tooling/Tooling.h | 9 +++++++++ lib/Tooling/Tooling.cpp | 31 +++++++++++++++++++++---------- 2 files changed, 30 insertions(+), 10 deletions(-) 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(), 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. 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, 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 @@ bool runToolOnCodeWithArgs( llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); } - return Invocation.run(); + return runToolOnCodeWithArgs(ToolAction, Code, OverlayFileSystem, Args, + FileName, ToolName); } std::string getAbsolutePath(StringRef File) { -- 2.40.0