[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
This commit is contained in:
Yitzhak Mandelbaum 2019-11-13 09:04:13 -05:00
parent 6e418decd9
commit dd471dbe99
2 changed files with 9 additions and 2 deletions

View File

@ -224,7 +224,8 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
StringRef FileName = "input.cc", StringRef ToolName = "clang-tool",
std::shared_ptr<PCHContainerOperations> PCHContainerOps =
std::make_shared<PCHContainerOperations>(),
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster());
ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(),
const FileContentMappings &VirtualMappedFiles = FileContentMappings());
/// Utility to run a FrontendAction in a single clang invocation.
class ToolInvocation {

View File

@ -619,7 +619,7 @@ buildASTFromCode(StringRef Code, StringRef FileName,
std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
ArgumentsAdjuster Adjuster) {
ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles) {
std::vector<std::unique_ptr<ASTUnit>> ASTs;
ASTBuilderAction Action(ASTs);
llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem(
@ -636,6 +636,12 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
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;