Skip to content

Commit dd471db

Browse files
committed
[libTooling] Extend buildASTFromCodeWithArgs to take files argument.
Summary: Adds an optional parameter to `buildASTFromCodeWithArgs` that allows the user to pass additional files that the main code needs to compile. This change makes `buildASTFromCodeWithArgs` consistent with `runToolOnCodeWithArgs`. Patch by Alexey Eremin. Reviewers: gribozavr Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D70175
1 parent 6e418de commit dd471db

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

clang/include/clang/Tooling/Tooling.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
224224
StringRef FileName = "input.cc", StringRef ToolName = "clang-tool",
225225
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
226226
std::make_shared<PCHContainerOperations>(),
227-
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster());
227+
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
228+
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
228229

229230
/// Utility to run a FrontendAction in a single clang invocation.
230231
class ToolInvocation {

clang/lib/Tooling/Tooling.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ buildASTFromCode(StringRef Code, StringRef FileName,
619619
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
620620
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
621621
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
622-
ArgumentsAdjuster Adjuster) {
622+
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles) {
623623
std::vector<std::unique_ptr<ASTUnit>> ASTs;
624624
ASTBuilderAction Action(ASTs);
625625
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
@@ -636,6 +636,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
636636

637637
InMemoryFileSystem->addFile(FileName, 0,
638638
llvm::MemoryBuffer::getMemBufferCopy(Code));
639+
for (auto &FilenameWithContent : VirtualMappedFiles) {
640+
InMemoryFileSystem->addFile(
641+
FilenameWithContent.first, 0,
642+
llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
643+
}
644+
639645
if (!Invocation.run())
640646
return nullptr;
641647

0 commit comments

Comments
 (0)