Skip to content

Commit 8992d27

Browse files
author
Eric Liu
committed
[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 llvm-svn: 332731
1 parent 29b8df6 commit 8992d27

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

clang/include/clang/Tooling/Tooling.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,15 @@ bool runToolOnCodeWithArgs(
187187
std::make_shared<PCHContainerOperations>(),
188188
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
189189

190+
// Similar to the overload except this takes a VFS.
191+
bool runToolOnCodeWithArgs(
192+
FrontendAction *ToolAction, const Twine &Code,
193+
llvm::IntrusiveRefCntPtr<vfs::FileSystem> VFS,
194+
const std::vector<std::string> &Args, const Twine &FileName = "input.cc",
195+
const Twine &ToolName = "clang-tool",
196+
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
197+
std::make_shared<PCHContainerOperations>());
198+
190199
/// Builds an AST for 'Code'.
191200
///
192201
/// \param Code C++ code.

clang/lib/Tooling/Tooling.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,27 +155,37 @@ namespace tooling {
155155

156156
bool runToolOnCodeWithArgs(
157157
FrontendAction *ToolAction, const Twine &Code,
158+
llvm::IntrusiveRefCntPtr<vfs::FileSystem> VFS,
158159
const std::vector<std::string> &Args, const Twine &FileName,
159160
const Twine &ToolName,
160-
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
161-
const FileContentMappings &VirtualMappedFiles) {
161+
std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
162162
SmallString<16> FileNameStorage;
163163
StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
164-
llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
165-
new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
166-
llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
167-
new vfs::InMemoryFileSystem);
168-
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
164+
169165
llvm::IntrusiveRefCntPtr<FileManager> Files(
170-
new FileManager(FileSystemOptions(), OverlayFileSystem));
166+
new FileManager(FileSystemOptions(), VFS));
171167
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster();
172168
ToolInvocation Invocation(
173169
getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
174170
ToolAction, Files.get(),
175171
std::move(PCHContainerOps));
172+
return Invocation.run();
173+
}
174+
175+
bool runToolOnCodeWithArgs(
176+
FrontendAction *ToolAction, const Twine &Code,
177+
const std::vector<std::string> &Args, const Twine &FileName,
178+
const Twine &ToolName,
179+
std::shared_ptr<PCHContainerOperations> PCHContainerOps,
180+
const FileContentMappings &VirtualMappedFiles) {
181+
llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem(
182+
new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
183+
llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
184+
new vfs::InMemoryFileSystem);
185+
OverlayFileSystem->pushOverlay(InMemoryFileSystem);
176186

177187
SmallString<1024> CodeStorage;
178-
InMemoryFileSystem->addFile(FileNameRef, 0,
188+
InMemoryFileSystem->addFile(FileName, 0,
179189
llvm::MemoryBuffer::getMemBuffer(
180190
Code.toNullTerminatedStringRef(CodeStorage)));
181191

@@ -185,7 +195,8 @@ bool runToolOnCodeWithArgs(
185195
llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
186196
}
187197

188-
return Invocation.run();
198+
return runToolOnCodeWithArgs(ToolAction, Code, OverlayFileSystem, Args,
199+
FileName, ToolName);
189200
}
190201

191202
std::string getAbsolutePath(StringRef File) {

0 commit comments

Comments
 (0)