diff --git a/clang/include/clang/Tooling/Tooling.h b/clang/include/clang/Tooling/Tooling.h --- a/clang/include/clang/Tooling/Tooling.h +++ b/clang/include/clang/Tooling/Tooling.h @@ -224,7 +224,8 @@ StringRef FileName = "input.cc", StringRef ToolName = "clang-tool", std::shared_ptr PCHContainerOps = std::make_shared(), - ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster()); + ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(), + const FileContentMappings &VirtualMappedFiles = FileContentMappings()); /// Utility to run a FrontendAction in a single clang invocation. class ToolInvocation { diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp --- a/clang/lib/Tooling/Tooling.cpp +++ b/clang/lib/Tooling/Tooling.cpp @@ -619,7 +619,7 @@ std::unique_ptr buildASTFromCodeWithArgs( StringRef Code, const std::vector &Args, StringRef FileName, StringRef ToolName, std::shared_ptr PCHContainerOps, - ArgumentsAdjuster Adjuster) { + ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles) { std::vector> ASTs; ASTBuilderAction Action(ASTs); llvm::IntrusiveRefCntPtr OverlayFileSystem( @@ -636,6 +636,12 @@ InMemoryFileSystem->addFile(FileName, 0, llvm::MemoryBuffer::getMemBufferCopy(Code)); + for (auto &FilenameWithContent : VirtualMappedFiles) { + InMemoryFileSystem->addFile( + FilenameWithContent.first, 0, + llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); + } + if (!Invocation.run()) return nullptr;