clang  16.0.0git
Tooling.cpp
Go to the documentation of this file.
1 //===- Tooling.cpp - Running clang standalone tools -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements functions to run clang tools standalone instead
10 // of running them as a plugin.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Tooling/Tooling.h"
15 #include "clang/Basic/Diagnostic.h"
20 #include "clang/Basic/LLVM.h"
22 #include "clang/Driver/Driver.h"
23 #include "clang/Driver/Job.h"
24 #include "clang/Driver/Options.h"
25 #include "clang/Driver/Tool.h"
26 #include "clang/Driver/ToolChain.h"
27 #include "clang/Frontend/ASTUnit.h"
37 #include "llvm/ADT/ArrayRef.h"
38 #include "llvm/ADT/IntrusiveRefCntPtr.h"
39 #include "llvm/ADT/SmallString.h"
40 #include "llvm/ADT/StringRef.h"
41 #include "llvm/ADT/Twine.h"
42 #include "llvm/Option/ArgList.h"
43 #include "llvm/Option/OptTable.h"
44 #include "llvm/Option/Option.h"
45 #include "llvm/Support/Casting.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Support/ErrorHandling.h"
48 #include "llvm/Support/FileSystem.h"
49 #include "llvm/Support/Host.h"
50 #include "llvm/Support/MemoryBuffer.h"
51 #include "llvm/Support/Path.h"
52 #include "llvm/Support/VirtualFileSystem.h"
53 #include "llvm/Support/raw_ostream.h"
54 #include <cassert>
55 #include <cstring>
56 #include <memory>
57 #include <string>
58 #include <system_error>
59 #include <utility>
60 #include <vector>
61 
62 #define DEBUG_TYPE "clang-tooling"
63 
64 using namespace clang;
65 using namespace tooling;
66 
67 ToolAction::~ToolAction() = default;
68 
70 
71 // FIXME: This file contains structural duplication with other parts of the
72 // code that sets up a compiler to run tools on it, and we should refactor
73 // it to be based on the same framework.
74 
75 /// Builds a clang driver initialized for running clang tools.
76 static driver::Driver *
77 newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName,
79  driver::Driver *CompilerDriver =
80  new driver::Driver(BinaryName, llvm::sys::getDefaultTargetTriple(),
81  *Diagnostics, "clang LLVM compiler", std::move(VFS));
82  CompilerDriver->setTitle("clang_based_tool");
83  return CompilerDriver;
84 }
85 
86 /// Decide whether extra compiler frontend commands can be ignored.
87 static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation) {
88  const driver::JobList &Jobs = Compilation->getJobs();
89  const driver::ActionList &Actions = Compilation->getActions();
90 
91  bool OffloadCompilation = false;
92 
93  // Jobs and Actions look very different depending on whether the Clang tool
94  // injected -fsyntax-only or not. Try to handle both cases here.
95 
96  for (const auto &Job : Jobs)
97  if (StringRef(Job.getExecutable()) == "clang-offload-bundler")
98  OffloadCompilation = true;
99 
100  if (Jobs.size() > 1) {
101  for (auto *A : Actions){
102  // On MacOSX real actions may end up being wrapped in BindArchAction
103  if (isa<driver::BindArchAction>(A))
104  A = *A->input_begin();
105  if (isa<driver::OffloadAction>(A)) {
106  // Offload compilation has 2 top-level actions, one (at the front) is
107  // the original host compilation and the other is offload action
108  // composed of at least one device compilation. For such case, general
109  // tooling will consider host-compilation only. For tooling on device
110  // compilation, device compilation only option, such as
111  // `--cuda-device-only`, needs specifying.
112  assert(Actions.size() > 1);
113  assert(
114  isa<driver::CompileJobAction>(Actions.front()) ||
115  // On MacOSX real actions may end up being wrapped in
116  // BindArchAction.
117  (isa<driver::BindArchAction>(Actions.front()) &&
118  isa<driver::CompileJobAction>(*Actions.front()->input_begin())));
119  OffloadCompilation = true;
120  break;
121  }
122  }
123  }
124 
125  return OffloadCompilation;
126 }
127 
128 namespace clang {
129 namespace tooling {
130 
131 const llvm::opt::ArgStringList *
133  driver::Compilation *Compilation) {
134  const driver::JobList &Jobs = Compilation->getJobs();
135 
136  auto IsCC1Command = [](const driver::Command &Cmd) {
137  return StringRef(Cmd.getCreator().getName()) == "clang";
138  };
139 
140  auto IsSrcFile = [](const driver::InputInfo &II) {
141  return isSrcFile(II.getType());
142  };
143 
145  for (const driver::Command &Job : Jobs)
146  if (IsCC1Command(Job) && llvm::all_of(Job.getInputInfos(), IsSrcFile))
147  CC1Jobs.push_back(&Job);
148 
149  if (CC1Jobs.empty() ||
150  (CC1Jobs.size() > 1 && !ignoreExtraCC1Commands(Compilation))) {
151  SmallString<256> error_msg;
152  llvm::raw_svector_ostream error_stream(error_msg);
153  Jobs.Print(error_stream, "; ", true);
154  Diagnostics->Report(diag::err_fe_expected_compiler_job)
155  << error_stream.str();
156  return nullptr;
157  }
158 
159  return &CC1Jobs[0]->getArguments();
160 }
161 
162 /// Returns a clang build invocation initialized from the CC1 flags.
164  ArrayRef<const char *> CC1Args,
165  const char *const BinaryName) {
166  assert(!CC1Args.empty() && "Must at least contain the program name!");
167  CompilerInvocation *Invocation = new CompilerInvocation;
168  CompilerInvocation::CreateFromArgs(*Invocation, CC1Args, *Diagnostics,
169  BinaryName);
170  Invocation->getFrontendOpts().DisableFree = false;
171  Invocation->getCodeGenOpts().DisableFree = false;
172  return Invocation;
173 }
174 
175 bool runToolOnCode(std::unique_ptr<FrontendAction> ToolAction,
176  const Twine &Code, const Twine &FileName,
177  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
178  return runToolOnCodeWithArgs(std::move(ToolAction), Code,
179  std::vector<std::string>(), FileName,
180  "clang-tool", std::move(PCHContainerOps));
181 }
182 
183 } // namespace tooling
184 } // namespace clang
185 
186 static std::vector<std::string>
187 getSyntaxOnlyToolArgs(const Twine &ToolName,
188  const std::vector<std::string> &ExtraArgs,
189  StringRef FileName) {
190  std::vector<std::string> Args;
191  Args.push_back(ToolName.str());
192  Args.push_back("-fsyntax-only");
193  Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
194  Args.push_back(FileName.str());
195  return Args;
196 }
197 
198 namespace clang {
199 namespace tooling {
200 
202  std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
204  const std::vector<std::string> &Args, const Twine &FileName,
205  const Twine &ToolName,
206  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
207  SmallString<16> FileNameStorage;
208  StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
209 
211  new FileManager(FileSystemOptions(), VFS));
213  ToolInvocation Invocation(
214  getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileNameRef), FileNameRef),
215  std::move(ToolAction), Files.get(), std::move(PCHContainerOps));
216  return Invocation.run();
217 }
218 
220  std::unique_ptr<FrontendAction> ToolAction, const Twine &Code,
221  const std::vector<std::string> &Args, const Twine &FileName,
222  const Twine &ToolName,
223  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
224  const FileContentMappings &VirtualMappedFiles) {
226  new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
228  new llvm::vfs::InMemoryFileSystem);
229  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
230 
231  SmallString<1024> CodeStorage;
232  InMemoryFileSystem->addFile(FileName, 0,
233  llvm::MemoryBuffer::getMemBuffer(
234  Code.toNullTerminatedStringRef(CodeStorage)));
235 
236  for (auto &FilenameWithContent : VirtualMappedFiles) {
237  InMemoryFileSystem->addFile(
238  FilenameWithContent.first, 0,
239  llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
240  }
241 
242  return runToolOnCodeWithArgs(std::move(ToolAction), Code, OverlayFileSystem,
243  Args, FileName, ToolName);
244 }
245 
247  StringRef File) {
248  StringRef RelativePath(File);
249  // FIXME: Should '.\\' be accepted on Win32?
250  if (RelativePath.startswith("./")) {
251  RelativePath = RelativePath.substr(strlen("./"));
252  }
253 
254  SmallString<1024> AbsolutePath = RelativePath;
255  if (auto EC = FS.makeAbsolute(AbsolutePath))
256  return llvm::errorCodeToError(EC);
257  llvm::sys::path::native(AbsolutePath);
258  return std::string(AbsolutePath.str());
259 }
260 
261 std::string getAbsolutePath(StringRef File) {
262  return llvm::cantFail(getAbsolutePath(*llvm::vfs::getRealFileSystem(), File));
263 }
264 
265 void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine,
266  StringRef InvokedAs) {
267  if (CommandLine.empty() || InvokedAs.empty())
268  return;
269  const auto &Table = driver::getDriverOptTable();
270  // --target=X
271  const std::string TargetOPT =
272  Table.getOption(driver::options::OPT_target).getPrefixedName();
273  // -target X
274  const std::string TargetOPTLegacy =
275  Table.getOption(driver::options::OPT_target_legacy_spelling)
276  .getPrefixedName();
277  // --driver-mode=X
278  const std::string DriverModeOPT =
279  Table.getOption(driver::options::OPT_driver_mode).getPrefixedName();
280  auto TargetMode =
282  // No need to search for target args if we don't have a target/mode to insert.
283  bool ShouldAddTarget = TargetMode.TargetIsValid;
284  bool ShouldAddMode = TargetMode.DriverMode != nullptr;
285  // Skip CommandLine[0].
286  for (auto Token = ++CommandLine.begin(); Token != CommandLine.end();
287  ++Token) {
288  StringRef TokenRef(*Token);
289  ShouldAddTarget = ShouldAddTarget && !TokenRef.startswith(TargetOPT) &&
290  !TokenRef.equals(TargetOPTLegacy);
291  ShouldAddMode = ShouldAddMode && !TokenRef.startswith(DriverModeOPT);
292  }
293  if (ShouldAddMode) {
294  CommandLine.insert(++CommandLine.begin(), TargetMode.DriverMode);
295  }
296  if (ShouldAddTarget) {
297  CommandLine.insert(++CommandLine.begin(),
298  TargetOPT + TargetMode.TargetPrefix);
299  }
300 }
301 
302 } // namespace tooling
303 } // namespace clang
304 
305 namespace {
306 
307 class SingleFrontendActionFactory : public FrontendActionFactory {
308  std::unique_ptr<FrontendAction> Action;
309 
310 public:
311  SingleFrontendActionFactory(std::unique_ptr<FrontendAction> Action)
312  : Action(std::move(Action)) {}
313 
314  std::unique_ptr<FrontendAction> create() override {
315  return std::move(Action);
316  }
317 };
318 
319 } // namespace
320 
322  std::vector<std::string> CommandLine, ToolAction *Action,
323  FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps)
324  : CommandLine(std::move(CommandLine)), Action(Action), OwnsAction(false),
325  Files(Files), PCHContainerOps(std::move(PCHContainerOps)) {}
326 
328  std::vector<std::string> CommandLine,
329  std::unique_ptr<FrontendAction> FAction, FileManager *Files,
330  std::shared_ptr<PCHContainerOperations> PCHContainerOps)
331  : CommandLine(std::move(CommandLine)),
332  Action(new SingleFrontendActionFactory(std::move(FAction))),
333  OwnsAction(true), Files(Files),
334  PCHContainerOps(std::move(PCHContainerOps)) {}
335 
337  if (OwnsAction)
338  delete Action;
339 }
340 
342  llvm::opt::ArgStringList Argv;
343  for (const std::string &Str : CommandLine)
344  Argv.push_back(Str.c_str());
345  const char *const BinaryName = Argv[0];
346 
347  // Parse diagnostic options from the driver command-line only if none were
348  // explicitly set.
350  DiagnosticOptions *DiagOpts = this->DiagOpts;
351  if (!DiagOpts) {
352  ParsedDiagOpts = CreateAndPopulateDiagOpts(Argv);
353  DiagOpts = &*ParsedDiagOpts;
354  }
355 
356  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), DiagOpts);
359  &*DiagOpts, DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false);
360  // Although `Diagnostics` are used only for command-line parsing, the custom
361  // `DiagConsumer` might expect a `SourceManager` to be present.
362  SourceManager SrcMgr(*Diagnostics, *Files);
363  Diagnostics->setSourceManager(&SrcMgr);
364 
365  // We already have a cc1, just create an invocation.
366  if (CommandLine.size() >= 2 && CommandLine[1] == "-cc1") {
367  ArrayRef<const char *> CC1Args = makeArrayRef(Argv).drop_front();
368  std::unique_ptr<CompilerInvocation> Invocation(
369  newInvocation(&*Diagnostics, CC1Args, BinaryName));
370  if (Diagnostics->hasErrorOccurred())
371  return false;
372  return Action->runInvocation(std::move(Invocation), Files,
373  std::move(PCHContainerOps), DiagConsumer);
374  }
375 
376  const std::unique_ptr<driver::Driver> Driver(
377  newDriver(&*Diagnostics, BinaryName, &Files->getVirtualFileSystem()));
378  // The "input file not found" diagnostics from the driver are useful.
379  // The driver is only aware of the VFS working directory, but some clients
380  // change this at the FileManager level instead.
381  // In this case the checks have false positives, so skip them.
382  if (!Files->getFileSystemOpts().WorkingDir.empty())
383  Driver->setCheckInputsExist(false);
384  const std::unique_ptr<driver::Compilation> Compilation(
385  Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
386  if (!Compilation)
387  return false;
388  const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments(
389  &*Diagnostics, Compilation.get());
390  if (!CC1Args)
391  return false;
392  std::unique_ptr<CompilerInvocation> Invocation(
393  newInvocation(&*Diagnostics, *CC1Args, BinaryName));
394  return runInvocation(BinaryName, Compilation.get(), std::move(Invocation),
395  std::move(PCHContainerOps));
396 }
397 
398 bool ToolInvocation::runInvocation(
399  const char *BinaryName, driver::Compilation *Compilation,
400  std::shared_ptr<CompilerInvocation> Invocation,
401  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
402  // Show the invocation, with -v.
403  if (Invocation->getHeaderSearchOpts().Verbose) {
404  llvm::errs() << "clang Invocation:\n";
405  Compilation->getJobs().Print(llvm::errs(), "\n", true);
406  llvm::errs() << "\n";
407  }
408 
409  return Action->runInvocation(std::move(Invocation), Files,
410  std::move(PCHContainerOps), DiagConsumer);
411 }
412 
414  std::shared_ptr<CompilerInvocation> Invocation, FileManager *Files,
415  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
416  DiagnosticConsumer *DiagConsumer) {
417  // Create a compiler instance to handle the actual work.
418  CompilerInstance Compiler(std::move(PCHContainerOps));
419  Compiler.setInvocation(std::move(Invocation));
420  Compiler.setFileManager(Files);
421 
422  // The FrontendAction can have lifetime requirements for Compiler or its
423  // members, and we need to ensure it's deleted earlier than Compiler. So we
424  // pass it to an std::unique_ptr declared after the Compiler variable.
425  std::unique_ptr<FrontendAction> ScopedToolAction(create());
426 
427  // Create the compiler's actual diagnostics engine.
428  Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false);
429  if (!Compiler.hasDiagnostics())
430  return false;
431 
432  Compiler.createSourceManager(*Files);
433 
434  const bool Success = Compiler.ExecuteAction(*ScopedToolAction);
435 
436  Files->clearStatCache();
437  return Success;
438 }
439 
441  ArrayRef<std::string> SourcePaths,
442  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
445  : Compilations(Compilations), SourcePaths(SourcePaths),
446  PCHContainerOps(std::move(PCHContainerOps)),
447  OverlayFileSystem(new llvm::vfs::OverlayFileSystem(std::move(BaseFS))),
448  InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem),
449  Files(Files ? Files
450  : new FileManager(FileSystemOptions(), OverlayFileSystem)) {
451  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
455  if (Files)
456  Files->setVirtualFileSystem(OverlayFileSystem);
457 }
458 
459 ClangTool::~ClangTool() = default;
460 
461 void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
462  MappedFileContents.push_back(std::make_pair(FilePath, Content));
463 }
464 
466  ArgsAdjuster = combineAdjusters(std::move(ArgsAdjuster), std::move(Adjuster));
467 }
468 
470  ArgsAdjuster = nullptr;
471 }
472 
473 static void injectResourceDir(CommandLineArguments &Args, const char *Argv0,
474  void *MainAddr) {
475  // Allow users to override the resource dir.
476  for (StringRef Arg : Args)
477  if (Arg.startswith("-resource-dir"))
478  return;
479 
480  // If there's no override in place add our resource dir.
482  ("-resource-dir=" + CompilerInvocation::GetResourcesPath(Argv0, MainAddr))
483  .c_str())(Args, "");
484 }
485 
487  // Exists solely for the purpose of lookup of the resource path.
488  // This just needs to be some symbol in the binary.
489  static int StaticSymbol;
490 
491  // First insert all absolute paths into the in-memory VFS. These are global
492  // for all compile commands.
493  if (SeenWorkingDirectories.insert("/").second)
494  for (const auto &MappedFile : MappedFileContents)
495  if (llvm::sys::path::is_absolute(MappedFile.first))
496  InMemoryFileSystem->addFile(
497  MappedFile.first, 0,
498  llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
499 
500  bool ProcessingFailed = false;
501  bool FileSkipped = false;
502  // Compute all absolute paths before we run any actions, as those will change
503  // the working directory.
504  std::vector<std::string> AbsolutePaths;
505  AbsolutePaths.reserve(SourcePaths.size());
506  for (const auto &SourcePath : SourcePaths) {
507  auto AbsPath = getAbsolutePath(*OverlayFileSystem, SourcePath);
508  if (!AbsPath) {
509  llvm::errs() << "Skipping " << SourcePath
510  << ". Error while getting an absolute path: "
511  << llvm::toString(AbsPath.takeError()) << "\n";
512  continue;
513  }
514  AbsolutePaths.push_back(std::move(*AbsPath));
515  }
516 
517  // Remember the working directory in case we need to restore it.
518  std::string InitialWorkingDir;
519  if (RestoreCWD) {
520  if (auto CWD = OverlayFileSystem->getCurrentWorkingDirectory()) {
521  InitialWorkingDir = std::move(*CWD);
522  } else {
523  llvm::errs() << "Could not get working directory: "
524  << CWD.getError().message() << "\n";
525  }
526  }
527 
528  for (llvm::StringRef File : AbsolutePaths) {
529  // Currently implementations of CompilationDatabase::getCompileCommands can
530  // change the state of the file system (e.g. prepare generated headers), so
531  // this method needs to run right before we invoke the tool, as the next
532  // file may require a different (incompatible) state of the file system.
533  //
534  // FIXME: Make the compilation database interface more explicit about the
535  // requirements to the order of invocation of its members.
536  std::vector<CompileCommand> CompileCommandsForFile =
537  Compilations.getCompileCommands(File);
538  if (CompileCommandsForFile.empty()) {
539  llvm::errs() << "Skipping " << File << ". Compile command not found.\n";
540  FileSkipped = true;
541  continue;
542  }
543  for (CompileCommand &CompileCommand : CompileCommandsForFile) {
544  // FIXME: chdir is thread hostile; on the other hand, creating the same
545  // behavior as chdir is complex: chdir resolves the path once, thus
546  // guaranteeing that all subsequent relative path operations work
547  // on the same path the original chdir resulted in. This makes a
548  // difference for example on network filesystems, where symlinks might be
549  // switched during runtime of the tool. Fixing this depends on having a
550  // file system abstraction that allows openat() style interactions.
551  if (OverlayFileSystem->setCurrentWorkingDirectory(
553  llvm::report_fatal_error("Cannot chdir into \"" +
554  Twine(CompileCommand.Directory) + "\"!");
555 
556  // Now fill the in-memory VFS with the relative file mappings so it will
557  // have the correct relative paths. We never remove mappings but that
558  // should be fine.
559  if (SeenWorkingDirectories.insert(CompileCommand.Directory).second)
560  for (const auto &MappedFile : MappedFileContents)
561  if (!llvm::sys::path::is_absolute(MappedFile.first))
562  InMemoryFileSystem->addFile(
563  MappedFile.first, 0,
564  llvm::MemoryBuffer::getMemBuffer(MappedFile.second));
565 
566  std::vector<std::string> CommandLine = CompileCommand.CommandLine;
567  if (ArgsAdjuster)
568  CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename);
569  assert(!CommandLine.empty());
570 
571  // Add the resource dir based on the binary of this tool. argv[0] in the
572  // compilation database may refer to a different compiler and we want to
573  // pick up the very same standard library that compiler is using. The
574  // builtin headers in the resource dir need to match the exact clang
575  // version the tool is using.
576  // FIXME: On linux, GetMainExecutable is independent of the value of the
577  // first argument, thus allowing ClangTool and runToolOnCode to just
578  // pass in made-up names here. Make sure this works on other platforms.
579  injectResourceDir(CommandLine, "clang_tool", &StaticSymbol);
580 
581  // FIXME: We need a callback mechanism for the tool writer to output a
582  // customized message for each file.
583  LLVM_DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; });
584  ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(),
585  PCHContainerOps);
586  Invocation.setDiagnosticConsumer(DiagConsumer);
587 
588  if (!Invocation.run()) {
589  // FIXME: Diagnostics should be used instead.
590  if (PrintErrorMessage)
591  llvm::errs() << "Error while processing " << File << ".\n";
592  ProcessingFailed = true;
593  }
594  }
595  }
596 
597  if (!InitialWorkingDir.empty()) {
598  if (auto EC =
599  OverlayFileSystem->setCurrentWorkingDirectory(InitialWorkingDir))
600  llvm::errs() << "Error when trying to restore working dir: "
601  << EC.message() << "\n";
602  }
603  return ProcessingFailed ? 1 : (FileSkipped ? 2 : 0);
604 }
605 
606 namespace {
607 
608 class ASTBuilderAction : public ToolAction {
609  std::vector<std::unique_ptr<ASTUnit>> &ASTs;
610 
611 public:
612  ASTBuilderAction(std::vector<std::unique_ptr<ASTUnit>> &ASTs) : ASTs(ASTs) {}
613 
614  bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
615  FileManager *Files,
616  std::shared_ptr<PCHContainerOperations> PCHContainerOps,
617  DiagnosticConsumer *DiagConsumer) override {
618  std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation(
619  Invocation, std::move(PCHContainerOps),
620  CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(),
621  DiagConsumer,
622  /*ShouldOwnClient=*/false),
623  Files);
624  if (!AST)
625  return false;
626 
627  ASTs.push_back(std::move(AST));
628  return true;
629  }
630 };
631 
632 } // namespace
633 
634 int ClangTool::buildASTs(std::vector<std::unique_ptr<ASTUnit>> &ASTs) {
635  ASTBuilderAction Action(ASTs);
636  return run(&Action);
637 }
638 
639 void ClangTool::setRestoreWorkingDir(bool RestoreCWD) {
640  this->RestoreCWD = RestoreCWD;
641 }
642 
643 void ClangTool::setPrintErrorMessage(bool PrintErrorMessage) {
644  this->PrintErrorMessage = PrintErrorMessage;
645 }
646 
647 namespace clang {
648 namespace tooling {
649 
650 std::unique_ptr<ASTUnit>
651 buildASTFromCode(StringRef Code, StringRef FileName,
652  std::shared_ptr<PCHContainerOperations> PCHContainerOps) {
653  return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName,
654  "clang-tool", std::move(PCHContainerOps));
655 }
656 
657 std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs(
658  StringRef Code, const std::vector<std::string> &Args, StringRef FileName,
659  StringRef ToolName, std::shared_ptr<PCHContainerOperations> PCHContainerOps,
660  ArgumentsAdjuster Adjuster, const FileContentMappings &VirtualMappedFiles,
661  DiagnosticConsumer *DiagConsumer) {
662  std::vector<std::unique_ptr<ASTUnit>> ASTs;
663  ASTBuilderAction Action(ASTs);
665  new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem()));
667  new llvm::vfs::InMemoryFileSystem);
668  OverlayFileSystem->pushOverlay(InMemoryFileSystem);
670  new FileManager(FileSystemOptions(), OverlayFileSystem));
671 
672  ToolInvocation Invocation(
673  getSyntaxOnlyToolArgs(ToolName, Adjuster(Args, FileName), FileName),
674  &Action, Files.get(), std::move(PCHContainerOps));
675  Invocation.setDiagnosticConsumer(DiagConsumer);
676 
677  InMemoryFileSystem->addFile(FileName, 0,
678  llvm::MemoryBuffer::getMemBufferCopy(Code));
679  for (auto &FilenameWithContent : VirtualMappedFiles) {
680  InMemoryFileSystem->addFile(
681  FilenameWithContent.first, 0,
682  llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second));
683  }
684 
685  if (!Invocation.run())
686  return nullptr;
687 
688  assert(ASTs.size() == 1);
689  return std::move(ASTs[0]);
690 }
691 
692 } // namespace tooling
693 } // namespace clang
clang::tooling::ToolInvocation
Utility to run a FrontendAction in a single clang invocation.
Definition: Tooling.h:239
clang::tooling::newInvocation
CompilerInvocation * newInvocation(DiagnosticsEngine *Diagnostics, ArrayRef< const char * > CC1Args, const char *const BinaryName)
Creates a CompilerInvocation.
Definition: Tooling.cpp:163
llvm
YAML serialization mapping.
Definition: Dominators.h:30
clang::CompilerInvocationValueBase::getCodeGenOpts
CodeGenOptions & getCodeGenOpts()
Definition: CompilerInvocation.h:159
clang::DeclaratorContext::File
@ File
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::ToolChain::getTargetAndModeFromProgramName
static ParsedClangName getTargetAndModeFromProgramName(StringRef ProgName)
Return any implicit target and/or mode flag for an invocation of the compiler driver as ProgName.
Definition: ToolChain.cpp:244
clang::tooling::ClangTool::run
int run(ToolAction *Action)
Runs an action over all files specified in the command line.
Definition: Tooling.cpp:486
llvm::SmallVector< Action *, 3 >
clang::tooling::CompilationDatabase
Interface for compilation databases.
Definition: CompilationDatabase.h:87
clang::FileManager::clearStatCache
void clearStatCache()
Removes the FileSystemStatCache object from the manager.
Definition: FileManager.cpp:68
clang::driver::Compilation::getActions
ActionList & getActions()
Definition: Compilation.h:202
clang::tooling::ArgumentsAdjuster
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Definition: ArgumentsAdjusters.h:36
clang::tooling::getInsertArgumentAdjuster
ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, ArgumentInsertPosition Pos)
Gets an argument adjuster which inserts Extra arguments in the specified position.
Definition: ArgumentsAdjusters.cpp:118
clang::tooling::getAbsolutePath
std::string getAbsolutePath(StringRef File)
Returns the absolute path of File, by prepending it with the current directory if File is not absolut...
Definition: Tooling.cpp:261
clang::DiagnosticConsumer
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1739
clang::driver::Compilation::getJobs
JobList & getJobs()
Definition: Compilation.h:214
ArgumentsAdjusters.h
clang::tooling::FrontendActionFactory::~FrontendActionFactory
~FrontendActionFactory() override
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
Job.h
clang::driver::JobList::Print
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:465
clang::tooling::ToolInvocation::setDiagnosticConsumer
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer)
Set a DiagnosticConsumer to use during driver command-line parsing and the action invocation itself.
Definition: Tooling.h:272
clang::tooling::buildASTFromCodeWithArgs
std::unique_ptr< ASTUnit > buildASTFromCodeWithArgs(StringRef Code, const std::vector< std::string > &Args, StringRef FileName="input.cc", StringRef ToolName="clang-tool", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >(), ArgumentsAdjuster Adjuster=getClangStripDependencyFileAdjuster(), const FileContentMappings &VirtualMappedFiles=FileContentMappings(), DiagnosticConsumer *DiagConsumer=nullptr)
Builds an AST for 'Code' with additional flags.
Definition: Tooling.cpp:657
llvm::Expected
Definition: LLVM.h:41
DiagnosticIDs.h
clang::tooling::ClangTool::~ClangTool
~ClangTool()
clang::tooling::getClangStripOutputAdjuster
ArgumentsAdjuster getClangStripOutputAdjuster()
Gets an argument adjuster which removes output-related command line arguments.
Definition: ArgumentsAdjusters.cpp:71
clang::FileSystemOptions::WorkingDir
std::string WorkingDir
If set, paths are resolved as if the working directory was set to the value of WorkingDir.
Definition: FileSystemOptions.h:26
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::tooling::ClangTool::clearArgumentsAdjusters
void clearArgumentsAdjusters()
Clear the command line arguments adjuster chain.
Definition: Tooling.cpp:469
clang::driver::InputInfo
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:22
clang::tooling::ClangTool::ClangTool
ClangTool(const CompilationDatabase &Compilations, ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >(), IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), IntrusiveRefCntPtr< FileManager > Files=nullptr)
Constructs a clang tool to run over a list of files.
Definition: Tooling.cpp:440
CompilerInvocation.h
clang::tooling::ClangTool::appendArgumentsAdjuster
void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster)
Append a command line arguments adjuster to the adjuster chain.
Definition: Tooling.cpp:465
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:636
clang::FileSystemOptions
Keeps track of options that affect how file operations are performed.
Definition: FileSystemOptions.h:22
clang::tooling::CompilationDatabase::getCompileCommands
virtual std::vector< CompileCommand > getCompileCommands(StringRef FilePath) const =0
Returns all compile commands in which the specified file was compiled.
clang::tooling::ToolAction
Interface to process a clang::CompilerInvocation.
Definition: Tooling.h:80
newDriver
static driver::Driver * newDriver(DiagnosticsEngine *Diagnostics, const char *BinaryName, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS)
Builds a clang driver initialized for running clang tools.
Definition: Tooling.cpp:77
clang::CompilerInstance::createDiagnostics
void createDiagnostics(DiagnosticConsumer *Client=nullptr, bool ShouldOwnClient=true)
Create the diagnostics engine using the invocation's diagnostic options and replace any existing one ...
Definition: CompilerInstance.cpp:334
clang::tooling::FileContentMappings
std::vector< std::pair< std::string, std::string > > FileContentMappings
The first part of the pair is the filename, the second part the file-content.
Definition: Tooling.h:171
clang::CompilerInvocationValueBase::getFrontendOpts
FrontendOptions & getFrontendOpts()
Definition: CompilerInvocation.h:176
clang::driver::getDriverOptTable
const llvm::opt::OptTable & getDriverOptTable()
Definition: DriverOptions.cpp:42
Options.h
HeaderSearchOptions.h
clang::FileManager::getVirtualFileSystem
llvm::vfs::FileSystem & getVirtualFileSystem() const
Definition: FileManager.h:245
FrontendOptions.h
clang::CompilerInstance::ExecuteAction
bool ExecuteAction(FrontendAction &Act)
ExecuteAction - Execute the provided action against the compiler's CompilerInvocation object.
Definition: CompilerInstance.cpp:999
ignoreExtraCC1Commands
static bool ignoreExtraCC1Commands(const driver::Compilation *Compilation)
Decide whether extra compiler frontend commands can be ignored.
Definition: Tooling.cpp:87
clang::threadSafety::sx::toString
std::string toString(const til::SExpr *E)
Definition: ThreadSafetyCommon.h:91
clang::tooling::buildASTFromCode
std::unique_ptr< ASTUnit > buildASTFromCode(StringRef Code, StringRef FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Builds an AST for 'Code'.
Definition: Tooling.cpp:651
clang::tooling::CompileCommand
Specifies the working directory and command of a compilation.
Definition: CompilationDatabase.h:44
clang::tooling::CommandLineArguments
std::vector< std::string > CommandLineArguments
A sequence of command line arguments.
Definition: ArgumentsAdjusters.h:29
Diagnostic.h
clang::driver::types::isSrcFile
bool isSrcFile(ID Id)
isSrcFile - Is this a source file, i.e.
Definition: Types.cpp:291
clang::tooling::CompileCommand::Filename
std::string Filename
The source file associated with the command.
Definition: CompilationDatabase.h:55
clang::tooling::addTargetAndModeForProgramName
void addTargetAndModeForProgramName(std::vector< std::string > &CommandLine, StringRef InvokedAs)
Changes CommandLine to contain implicit flags that would have been defined had the compiler driver be...
Definition: Tooling.cpp:265
clang::tooling::ClangTool::setRestoreWorkingDir
void setRestoreWorkingDir(bool RestoreCWD)
Sets whether working directory should be restored after calling run().
Definition: Tooling.cpp:639
clang::tooling::ClangTool::setPrintErrorMessage
void setPrintErrorMessage(bool PrintErrorMessage)
Sets whether an error message should be printed out if an action fails.
Definition: Tooling.cpp:643
Tool.h
clang::driver::JobList
JobList - A sequence of jobs to perform.
Definition: Job.h:272
getSyntaxOnlyToolArgs
static std::vector< std::string > getSyntaxOnlyToolArgs(const Twine &ToolName, const std::vector< std::string > &ExtraArgs, StringRef FileName)
Definition: Tooling.cpp:187
llvm::SmallString
Definition: LLVM.h:37
clang::tooling::CompileCommand::Directory
std::string Directory
The working directory the command was executed from.
Definition: CompilationDatabase.h:52
clang::tooling::ToolAction::runInvocation
virtual bool runInvocation(std::shared_ptr< CompilerInvocation > Invocation, FileManager *Files, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer)=0
Perform an action for an invocation.
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
clang::FileManager::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Returns the current file system options.
Definition: FileManager.h:242
Cmd
CompileCommand Cmd
Definition: InterpolatingCompilationDatabase.cpp:129
clang::CompilerInstance::setInvocation
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
Definition: CompilerInstance.cpp:76
clang::tooling::ClangTool::buildASTs
int buildASTs(std::vector< std::unique_ptr< ASTUnit >> &ASTs)
Create an AST for each file specified in the command line and append them to ASTs.
Definition: Tooling.cpp:634
ToolChain.h
Tooling.h
clang::driver::Command
Command - An executable path/name and argument vector to execute.
Definition: Job.h:106
clang::serialized_diags::create
std::unique_ptr< DiagnosticConsumer > create(StringRef OutputFile, DiagnosticOptions *Diags, bool MergeChildRecords=false)
Returns a DiagnosticConsumer that serializes diagnostics to a bitcode file.
Definition: SerializedDiagnosticPrinter.cpp:301
clang::tooling::runToolOnCode
bool runToolOnCode(std::unique_ptr< FrontendAction > ToolAction, const Twine &Code, const Twine &FileName="input.cc", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag.
Definition: Tooling.cpp:175
false
#define false
Definition: stdbool.h:22
Compilation.h
TextDiagnosticPrinter.h
llvm::ArrayRef< const char * >
clang::tooling::ToolInvocation::run
bool run()
Run the clang invocation.
Definition: Tooling.cpp:341
clang::CompilerInstance::hasDiagnostics
bool hasDiagnostics() const
Definition: CompilerInstance.h:331
clang::tooling::ToolInvocation::~ToolInvocation
~ToolInvocation()
Definition: Tooling.cpp:336
clang::tooling::CompileCommand::CommandLine
std::vector< std::string > CommandLine
The command line that was executed.
Definition: CompilationDatabase.h:58
ASTUnit.h
LLVM.h
clang::driver::Compilation
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
clang::tooling::FrontendActionFactory::create
virtual std::unique_ptr< FrontendAction > create()=0
Returns a new clang::FrontendAction.
clang::tooling::combineAdjusters
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, ArgumentsAdjuster Second)
Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then wi...
Definition: ArgumentsAdjusters.cpp:141
injectResourceDir
static void injectResourceDir(CommandLineArguments &Args, const char *Argv0, void *MainAddr)
Definition: Tooling.cpp:473
clang::tooling::getClangSyntaxOnlyAdjuster
ArgumentsAdjuster getClangSyntaxOnlyAdjuster()
Gets an argument adjuster that converts input command line arguments to the "syntax check only" varia...
Definition: ArgumentsAdjusters.cpp:35
std
Definition: Format.h:4477
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:75
FrontendDiagnostic.h
clang::tooling::ToolInvocation::ToolInvocation
ToolInvocation(std::vector< std::string > CommandLine, std::unique_ptr< FrontendAction > FAction, FileManager *Files, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Create a tool invocation.
clang::CompilerInvocation
Helper class for holding the data necessary to invoke the compiler.
Definition: CompilerInvocation.h:193
clang::tooling::runToolOnCodeWithArgs
bool runToolOnCodeWithArgs(std::unique_ptr< FrontendAction > ToolAction, const Twine &Code, const std::vector< std::string > &Args, const Twine &FileName="input.cc", const Twine &ToolName="clang-tool", std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >(), const FileContentMappings &VirtualMappedFiles=FileContentMappings())
Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and with additional other flags.
Definition: Tooling.cpp:219
clang::tooling::getClangStripDependencyFileAdjuster
ArgumentsAdjuster getClangStripDependencyFileAdjuster()
Gets an argument adjuster which removes dependency-file related command line arguments.
Definition: ArgumentsAdjusters.cpp:89
clang::TextDiagnosticPrinter
Definition: TextDiagnosticPrinter.h:27
clang::CompilerInstance::setFileManager
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
Definition: CompilerInstance.cpp:165
clang::CreateAndPopulateDiagOpts
std::unique_ptr< DiagnosticOptions > CreateAndPopulateDiagOpts(ArrayRef< const char * > Argv)
Definition: CompilerInvocation.cpp:2345
DiagnosticOptions.h
FileSystemOptions.h
CompilerInstance.h
clang::driver::Driver::setTitle
void setTitle(std::string Value)
Definition: Driver.h:381
CompilationDatabase.h
clang::FrontendOptions::DisableFree
unsigned DisableFree
Disable memory freeing on exit.
Definition: FrontendOptions.h:268
clang::CompilerInvocation::GetResourcesPath
static std::string GetResourcesPath(const char *Argv0, void *MainAddr)
Get the directory where the compiler headers reside, relative to the compiler binary (found by the pa...
Definition: CompilerInvocation.cpp:2949
FileManager.h
true
#define true
Definition: stdbool.h:21
clang::tooling::ClangTool::mapVirtualFile
void mapVirtualFile(StringRef FilePath, StringRef Content)
Map a virtual file to be used while running the tool.
Definition: Tooling.cpp:461
PreprocessorOptions.h
clang::tooling::getCC1Arguments
const llvm::opt::ArgStringList * getCC1Arguments(DiagnosticsEngine *Diagnostics, driver::Compilation *Compilation)
Retrieves the flags of the -cc1 job in Compilation that has only source files as its inputs.
Definition: Tooling.cpp:132
clang::tooling::ToolAction::~ToolAction
virtual ~ToolAction()
clang::CompilerInstance::createSourceManager
void createSourceManager(FileManager &FileMgr)
Create the source manager and replace any existing one with it.
Definition: CompilerInstance.cpp:391
clang::CompilerInvocation::CreateFromArgs
static bool CreateFromArgs(CompilerInvocation &Res, ArrayRef< const char * > CommandLineArgs, DiagnosticsEngine &Diags, const char *Argv0=nullptr)
Create a compiler invocation from a list of input options.
Definition: CompilerInvocation.cpp:4562
clang::tooling::FrontendActionFactory::runInvocation
bool runInvocation(std::shared_ptr< CompilerInvocation > Invocation, FileManager *Files, std::shared_ptr< PCHContainerOperations > PCHContainerOps, DiagnosticConsumer *DiagConsumer) override
Invokes the compiler with a FrontendAction created by create().
Definition: Tooling.cpp:413
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem >
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1537