1 //===- Tooling.cpp - Running clang standalone tools -----------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements functions to run clang tools standalone instead 11 // of running them as a plugin. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/Tooling/Tooling.h" 16 #include "clang/Basic/Diagnostic.h" 17 #include "clang/Basic/DiagnosticIDs.h" 18 #include "clang/Basic/DiagnosticOptions.h" 19 #include "clang/Basic/FileManager.h" 20 #include "clang/Basic/FileSystemOptions.h" 21 #include "clang/Basic/LLVM.h" 22 #include "clang/Basic/VirtualFileSystem.h" 23 #include "clang/Driver/Compilation.h" 24 #include "clang/Driver/Driver.h" 25 #include "clang/Driver/Job.h" 26 #include "clang/Driver/Options.h" 27 #include "clang/Driver/Tool.h" 28 #include "clang/Driver/ToolChain.h" 29 #include "clang/Frontend/ASTUnit.h" 30 #include "clang/Frontend/CompilerInstance.h" 31 #include "clang/Frontend/CompilerInvocation.h" 32 #include "clang/Frontend/FrontendDiagnostic.h" 33 #include "clang/Frontend/FrontendOptions.h" 34 #include "clang/Frontend/TextDiagnosticPrinter.h" 35 #include "clang/Lex/HeaderSearchOptions.h" 36 #include "clang/Lex/PreprocessorOptions.h" 37 #include "clang/Tooling/ArgumentsAdjusters.h" 38 #include "clang/Tooling/CompilationDatabase.h" 39 #include "llvm/ADT/ArrayRef.h" 40 #include "llvm/ADT/IntrusiveRefCntPtr.h" 41 #include "llvm/ADT/SmallString.h" 42 #include "llvm/ADT/StringRef.h" 43 #include "llvm/ADT/Twine.h" 44 #include "llvm/Option/ArgList.h" 45 #include "llvm/Option/OptTable.h" 46 #include "llvm/Option/Option.h" 47 #include "llvm/Support/Casting.h" 48 #include "llvm/Support/Debug.h" 49 #include "llvm/Support/ErrorHandling.h" 50 #include "llvm/Support/FileSystem.h" 51 #include "llvm/Support/Host.h" 52 #include "llvm/Support/MemoryBuffer.h" 53 #include "llvm/Support/Path.h" 54 #include "llvm/Support/raw_ostream.h" 55 #include <cassert> 56 #include <cstring> 57 #include <memory> 58 #include <string> 59 #include <system_error> 60 #include <utility> 61 #include <vector> 62 63 #define DEBUG_TYPE "clang-tooling" 64 65 using namespace clang; 66 using namespace tooling; 67 68 ToolAction::~ToolAction() = default; 69 70 FrontendActionFactory::~FrontendActionFactory() = default; 71 72 // FIXME: This file contains structural duplication with other parts of the 73 // code that sets up a compiler to run tools on it, and we should refactor 74 // it to be based on the same framework. 75 76 /// \brief Builds a clang driver initialized for running clang tools. 77 static driver::Driver *newDriver( 78 DiagnosticsEngine *Diagnostics, const char *BinaryName, 79 IntrusiveRefCntPtr<vfs::FileSystem> VFS) { 80 driver::Driver *CompilerDriver = 81 new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(), 82 *Diagnostics, std::move(VFS)); 83 CompilerDriver->setTitle("clang_based_tool"); 84 return CompilerDriver; 85 } 86 87 /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. 88 /// 89 /// Returns nullptr on error. 90 static const llvm::opt::ArgStringList *getCC1Arguments( 91 DiagnosticsEngine *Diagnostics, driver::Compilation *Compilation) { 92 // We expect to get back exactly one Command job, if we didn't something 93 // failed. Extract that job from the Compilation. 94 const driver::JobList &Jobs = Compilation->getJobs(); 95 if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) { 96 SmallString<256> error_msg; 97 llvm::raw_svector_ostream error_stream(error_msg); 98 Jobs.Print(error_stream, "; ", true); 99 Diagnostics->Report(diag::err_fe_expected_compiler_job) 100 << error_stream.str(); 101 return nullptr; 102 } 103 104 // The one job we find should be to invoke clang again. 105 const auto &Cmd = cast<driver::Command>(*Jobs.begin()); 106 if (StringRef(Cmd.getCreator().getName()) != "clang") { 107 Diagnostics->Report(diag::err_fe_expected_clang_command); 108 return nullptr; 109 } 110 111 return &Cmd.getArguments(); 112 } 113 114 namespace clang { 115 namespace tooling { 116 117 /// \brief Returns a clang build invocation initialized from the CC1 flags. 118 CompilerInvocation *newInvocation( 119 DiagnosticsEngine *Diagnostics, const llvm::opt::ArgStringList &CC1Args) { 120 assert(!CC1Args.empty() && "Must at least contain the program name!"); 121 CompilerInvocation *Invocation = new CompilerInvocation; 122 CompilerInvocation::CreateFromArgs( 123 *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), 124 *Diagnostics); 125 Invocation->getFrontendOpts().DisableFree = false; 126 Invocation->getCodeGenOpts().DisableFree = false; 127 return Invocation; 128 } 129 130 bool runToolOnCode(FrontendAction *ToolAction, const Twine &Code, 131 const Twine &FileName, 132 std::shared_ptr<PCHContainerOperations> PCHContainerOps) { 133 return runToolOnCodeWithArgs(ToolAction, Code, std::vector<std::string>(), 134 FileName, "clang-tool", 135 std::move(PCHContainerOps)); 136 } 137 138 } // namespace tooling 139 } // namespace clang 140 141 static std::vector<std::string> 142 getSyntaxOnlyToolArgs(const Twine &ToolName, 143 const std::vector<std::string> &ExtraArgs, 144 StringRef FileName) { 145 std::vector<std::string> Args; 146 Args.push_back(ToolName.str()); 147 Args.push_back("-fsyntax-only"); 148 Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); 149 Args.push_back(FileName.str()); 150 return Args; 151 } 152 153 namespace clang { 154 namespace tooling { 155 156 bool runToolOnCodeWithArgs( 157 FrontendAction *ToolAction, const Twine &Code, 158 const std::vector<std::string> &Args, const Twine &FileName, 159 const Twine &ToolName, 160 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 161 const FileContentMappings &VirtualMappedFiles) { 162 SmallString<16> FileNameStorage; 163 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); 169 llvm::IntrusiveRefCntPtr<FileManager> Files( 170 new FileManager(FileSystemOptions(), OverlayFileSystem)); 171 ArgumentsAdjuster Adjuster = getClangStripDependencyFileAdjuster(); 172 ToolInvocation Invocation( 173 getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), 174 ToolAction, Files.get(), 175 std::move(PCHContainerOps)); 176 177 SmallString<1024> CodeStorage; 178 InMemoryFileSystem->addFile(FileNameRef, 0, 179 llvm::MemoryBuffer::getMemBuffer( 180 Code.toNullTerminatedStringRef(CodeStorage))); 181 182 for (auto &FilenameWithContent : VirtualMappedFiles) { 183 InMemoryFileSystem->addFile( 184 FilenameWithContent.first, 0, 185 llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); 186 } 187 188 return Invocation.run(); 189 } 190 191 std::string getAbsolutePath(StringRef File) { 192 StringRef RelativePath(File); 193 // FIXME: Should '.\\' be accepted on Win32? 194 if (RelativePath.startswith("./")) { 195 RelativePath = RelativePath.substr(strlen("./")); 196 } 197 198 SmallString<1024> AbsolutePath = RelativePath; 199 std::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); 200 assert(!EC); 201 (void)EC; 202 llvm::sys::path::native(AbsolutePath); 203 return AbsolutePath.str(); 204 } 205 206 void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, 207 StringRef InvokedAs) { 208 if (!CommandLine.empty() && !InvokedAs.empty()) { 209 bool AlreadyHasTarget = false; 210 bool AlreadyHasMode = false; 211 // Skip CommandLine[0]. 212 for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); 213 ++Token) { 214 StringRef TokenRef(*Token); 215 AlreadyHasTarget |= 216 (TokenRef == "-target" || TokenRef.startswith("-target=")); 217 AlreadyHasMode |= (TokenRef == "--driver-mode" || 218 TokenRef.startswith("--driver-mode=")); 219 } 220 auto TargetMode = 221 driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs); 222 if (!AlreadyHasMode && TargetMode.DriverMode) { 223 CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode); 224 } 225 if (!AlreadyHasTarget && TargetMode.TargetIsValid) { 226 CommandLine.insert(++CommandLine.begin(), {"-target", 227 TargetMode.TargetPrefix}); 228 } 229 } 230 } 231 232 } // namespace tooling 233 } // namespace clang 234 235 namespace { 236 237 class SingleFrontendActionFactory : public FrontendActionFactory { 238 FrontendAction *Action; 239 240 public: 241 SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} 242 243 FrontendAction *create() override { return Action; } 244 }; 245 246 } // namespace 247 248 ToolInvocation::ToolInvocation( 249 std::vector<std::string> CommandLine, ToolAction *Action, 250 FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) 251 : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false), 252 Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} 253 254 ToolInvocation::ToolInvocation( 255 std::vector<std::string> CommandLine, FrontendAction *FAction, 256 FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps) 257 : CommandLine(std::move(CommandLine)), 258 Action(new SingleFrontendActionFactory(FAction)), OwnsAction(true), 259 Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {} 260 261 ToolInvocation::~ToolInvocation() { 262 if (OwnsAction) 263 delete Action; 264 } 265 266 void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { 267 SmallString<1024> PathStorage; 268 llvm::sys::path::native(FilePath, PathStorage); 269 MappedFileContents[PathStorage] = Content; 270 } 271 272 bool ToolInvocation::run() { 273 std::vector<const char*> Argv; 274 for (const std::string &Str : CommandLine) 275 Argv.push_back(Str.c_str()); 276 const char *const BinaryName = Argv[0]; 277 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); 278 unsigned MissingArgIndex, MissingArgCount; 279 std::unique_ptr<llvm::opt::OptTable> Opts = driver::createDriverOptTable(); 280 llvm::opt::InputArgList ParsedArgs = Opts->ParseArgs( 281 ArrayRef<const char *>(Argv).slice(1), MissingArgIndex, MissingArgCount); 282 ParseDiagnosticArgs(*DiagOpts, ParsedArgs); 283 TextDiagnosticPrinter DiagnosticPrinter( 284 llvm::errs(), &*DiagOpts); 285 DiagnosticsEngine Diagnostics( 286 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, 287 DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); 288 289 const std::unique_ptr<driver::Driver> Driver( 290 newDriver(&Diagnostics, BinaryName, Files->getVirtualFileSystem())); 291 // Since the input might only be virtual, don't check whether it exists. 292 Driver->setCheckInputsExist(false); 293 const std::unique_ptr<driver::Compilation> Compilation( 294 Driver->BuildCompilation(llvm::makeArrayRef(Argv))); 295 if (!Compilation) 296 return false; 297 const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments( 298 &Diagnostics, Compilation.get()); 299 if (!CC1Args) 300 return false; 301 std::unique_ptr<CompilerInvocation> Invocation( 302 newInvocation(&Diagnostics, *CC1Args)); 303 // FIXME: remove this when all users have migrated! 304 for (const auto &It : MappedFileContents) { 305 // Inject the code as the given file name into the preprocessor options. 306 std::unique_ptr<llvm::MemoryBuffer> Input = 307 llvm::MemoryBuffer::getMemBuffer(It.getValue()); 308 Invocation->getPreprocessorOpts().addRemappedFile(It.getKey(), 309 Input.release()); 310 } 311 return runInvocation(BinaryName, Compilation.get(), std::move(Invocation), 312 std::move(PCHContainerOps)); 313 } 314 315 bool ToolInvocation::runInvocation( 316 const char *BinaryName, driver::Compilation *Compilation, 317 std::shared_ptr<CompilerInvocation> Invocation, 318 std::shared_ptr<PCHContainerOperations> PCHContainerOps) { 319 // Show the invocation, with -v. 320 if (Invocation->getHeaderSearchOpts().Verbose) { 321 llvm::errs() << "clang Invocation:\n"; 322 Compilation->getJobs().Print(llvm::errs(), "\n", true); 323 llvm::errs() << "\n"; 324 } 325 326 return Action->runInvocation(std::move(Invocation), Files, 327 std::move(PCHContainerOps), DiagConsumer); 328 } 329 330 bool FrontendActionFactory::runInvocation( 331 std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files, 332 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 333 DiagnosticConsumer *DiagConsumer) { 334 // Create a compiler instance to handle the actual work. 335 CompilerInstance Compiler(std::move(PCHContainerOps)); 336 Compiler.setInvocation(std::move(Invocation)); 337 Compiler.setFileManager(Files); 338 339 // The FrontendAction can have lifetime requirements for Compiler or its 340 // members, and we need to ensure it's deleted earlier than Compiler. So we 341 // pass it to an std::unique_ptr declared after the Compiler variable. 342 std::unique_ptr<FrontendAction> ScopedToolAction(create()); 343 344 // Create the compiler's actual diagnostics engine. 345 Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); 346 if (!Compiler.hasDiagnostics()) 347 return false; 348 349 Compiler.createSourceManager(*Files); 350 351 const bool Success = Compiler.ExecuteAction(*ScopedToolAction); 352 353 Files->clearStatCaches(); 354 return Success; 355 } 356 357 ClangTool::ClangTool(const CompilationDatabase &Compilations, 358 ArrayRef<std::string> SourcePaths, 359 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 360 IntrusiveRefCntPtr<vfs::FileSystem> BaseFS) 361 : Compilations(Compilations), SourcePaths(SourcePaths), 362 PCHContainerOps(std::move(PCHContainerOps)), 363 OverlayFileSystem(new vfs::OverlayFileSystem(std::move(BaseFS))), 364 InMemoryFileSystem(new vfs::InMemoryFileSystem), 365 Files(new FileManager(FileSystemOptions(), OverlayFileSystem)) { 366 OverlayFileSystem->pushOverlay(InMemoryFileSystem); 367 appendArgumentsAdjuster(getClangStripOutputAdjuster()); 368 appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); 369 appendArgumentsAdjuster(getClangStripDependencyFileAdjuster()); 370 } 371 372 ClangTool::~ClangTool() = default; 373 374 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { 375 MappedFileContents.push_back(std::make_pair(FilePath, Content)); 376 } 377 378 void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) { 379 ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster)); 380 } 381 382 void ClangTool::clearArgumentsAdjusters() { 383 ArgsAdjuster = nullptr; 384 } 385 386 static void injectResourceDir(CommandLineArguments &Args, const char *Argv0, 387 void *MainAddr) { 388 // Allow users to override the resource dir. 389 for (StringRef Arg : Args) 390 if (Arg.startswith("-resource-dir")) 391 return; 392 393 // If there's no override in place add our resource dir. 394 Args.push_back("-resource-dir=" + 395 CompilerInvocation::GetResourcesPath(Argv0, MainAddr)); 396 } 397 398 int ClangTool::run(ToolAction *Action) { 399 // Exists solely for the purpose of lookup of the resource path. 400 // This just needs to be some symbol in the binary. 401 static int StaticSymbol; 402 403 llvm::SmallString<128> InitialDirectory; 404 if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) 405 llvm::report_fatal_error("Cannot detect current path: " + 406 Twine(EC.message())); 407 408 // First insert all absolute paths into the in-memory VFS. These are global 409 // for all compile commands. 410 if (SeenWorkingDirectories.insert("/").second) 411 for (const auto &MappedFile : MappedFileContents) 412 if (llvm::sys::path::is_absolute(MappedFile.first)) 413 InMemoryFileSystem->addFile( 414 MappedFile.first, 0, 415 llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); 416 417 bool ProcessingFailed = false; 418 bool FileSkipped = false; 419 for (const auto &SourcePath : SourcePaths) { 420 std::string File(getAbsolutePath(SourcePath)); 421 422 // Currently implementations of CompilationDatabase::getCompileCommands can 423 // change the state of the file system (e.g. prepare generated headers), so 424 // this method needs to run right before we invoke the tool, as the next 425 // file may require a different (incompatible) state of the file system. 426 // 427 // FIXME: Make the compilation database interface more explicit about the 428 // requirements to the order of invocation of its members. 429 std::vector<CompileCommand> CompileCommandsForFile = 430 Compilations.getCompileCommands(File); 431 if (CompileCommandsForFile.empty()) { 432 llvm::errs() << "Skipping " << File << ". Compile command not found.\n"; 433 FileSkipped = true; 434 continue; 435 } 436 for (CompileCommand &CompileCommand : CompileCommandsForFile) { 437 // FIXME: chdir is thread hostile; on the other hand, creating the same 438 // behavior as chdir is complex: chdir resolves the path once, thus 439 // guaranteeing that all subsequent relative path operations work 440 // on the same path the original chdir resulted in. This makes a 441 // difference for example on network filesystems, where symlinks might be 442 // switched during runtime of the tool. Fixing this depends on having a 443 // file system abstraction that allows openat() style interactions. 444 if (OverlayFileSystem->setCurrentWorkingDirectory( 445 CompileCommand.Directory)) 446 llvm::report_fatal_error("Cannot chdir into \"" + 447 Twine(CompileCommand.Directory) + "\n!"); 448 449 // Now fill the in-memory VFS with the relative file mappings so it will 450 // have the correct relative paths. We never remove mappings but that 451 // should be fine. 452 if (SeenWorkingDirectories.insert(CompileCommand.Directory).second) 453 for (const auto &MappedFile : MappedFileContents) 454 if (!llvm::sys::path::is_absolute(MappedFile.first)) 455 InMemoryFileSystem->addFile( 456 MappedFile.first, 0, 457 llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); 458 459 std::vector<std::string> CommandLine = CompileCommand.CommandLine; 460 if (ArgsAdjuster) 461 CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename); 462 assert(!CommandLine.empty()); 463 464 // Add the resource dir based on the binary of this tool. argv[0] in the 465 // compilation database may refer to a different compiler and we want to 466 // pick up the very same standard library that compiler is using. The 467 // builtin headers in the resource dir need to match the exact clang 468 // version the tool is using. 469 // FIXME: On linux, GetMainExecutable is independent of the value of the 470 // first argument, thus allowing ClangTool and runToolOnCode to just 471 // pass in made-up names here. Make sure this works on other platforms. 472 injectResourceDir(CommandLine, "clang_tool", &StaticSymbol); 473 474 // FIXME: We need a callback mechanism for the tool writer to output a 475 // customized message for each file. 476 DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); 477 ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(), 478 PCHContainerOps); 479 Invocation.setDiagnosticConsumer(DiagConsumer); 480 481 if (!Invocation.run()) { 482 // FIXME: Diagnostics should be used instead. 483 llvm::errs() << "Error while processing " << File << ".\n"; 484 ProcessingFailed = true; 485 } 486 // Return to the initial directory to correctly resolve next file by 487 // relative path. 488 if (OverlayFileSystem->setCurrentWorkingDirectory(InitialDirectory.c_str())) 489 llvm::report_fatal_error("Cannot chdir into \"" + 490 Twine(InitialDirectory) + "\n!"); 491 } 492 } 493 return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0); 494 } 495 496 namespace { 497 498 class ASTBuilderAction : public ToolAction { 499 std::vector<std::unique_ptr<ASTUnit>> &ASTs; 500 501 public: 502 ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {} 503 504 bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation, 505 FileManager *Files, 506 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 507 DiagnosticConsumer *DiagConsumer) override { 508 std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( 509 Invocation, std::move(PCHContainerOps), 510 CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), 511 DiagConsumer, 512 /*ShouldOwnClient=*/false), 513 Files); 514 if (!AST) 515 return false; 516 517 ASTs.push_back(std::move(AST)); 518 return true; 519 } 520 }; 521 522 } // namespace 523 524 int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) { 525 ASTBuilderAction Action(ASTs); 526 return run(&Action); 527 } 528 529 namespace clang { 530 namespace tooling { 531 532 std::unique_ptr<ASTUnit> 533 buildASTFromCode(const Twine &Code, const Twine &FileName, 534 std::shared_ptr<PCHContainerOperations> PCHContainerOps) { 535 return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName, 536 "clang-tool", std::move(PCHContainerOps)); 537 } 538 539 std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( 540 const Twine &Code, const std::vector<std::string> &Args, 541 const Twine &FileName, const Twine &ToolName, 542 std::shared_ptr<PCHContainerOperations> PCHContainerOps, 543 ArgumentsAdjuster Adjuster) { 544 SmallString<16> FileNameStorage; 545 StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); 546 547 std::vector<std::unique_ptr<ASTUnit>> ASTs; 548 ASTBuilderAction Action(ASTs); 549 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( 550 new vfs::OverlayFileSystem(vfs::getRealFileSystem())); 551 llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( 552 new vfs::InMemoryFileSystem); 553 OverlayFileSystem->pushOverlay(InMemoryFileSystem); 554 llvm::IntrusiveRefCntPtr<FileManager> Files( 555 new FileManager(FileSystemOptions(), OverlayFileSystem)); 556 557 ToolInvocation Invocation( 558 getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef), 559 &Action, Files.get(), std::move(PCHContainerOps)); 560 561 SmallString<1024> CodeStorage; 562 InMemoryFileSystem->addFile(FileNameRef, 0, 563 llvm::MemoryBuffer::getMemBuffer( 564 Code.toNullTerminatedStringRef(CodeStorage))); 565 if (!Invocation.run()) 566 return nullptr; 567 568 assert(ASTs.size() == 1); 569 return std::move(ASTs[0]); 570 } 571 572 } // namespace tooling 573 } // namespace clang 574