StandaloneExecution.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===- lib/Tooling/Execution.cpp - Standalone clang action execution. -----===//
  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. #include "clang/Tooling/StandaloneExecution.h"
  9. #include "clang/Tooling/ToolExecutorPluginRegistry.h"
  10. namespace clang {
  11. namespace tooling {
  12. static llvm::Error make_string_error(const llvm::Twine &Message) {
  13. return llvm::make_error<llvm::StringError>(Message,
  14. llvm::inconvertibleErrorCode());
  15. }
  16. const char *StandaloneToolExecutor::ExecutorName = "StandaloneToolExecutor";
  17. static ArgumentsAdjuster getDefaultArgumentsAdjusters() {
  18. return combineAdjusters(
  19. getClangStripOutputAdjuster(),
  20. combineAdjusters(getClangSyntaxOnlyAdjuster(),
  21. getClangStripDependencyFileAdjuster()));
  22. }
  23. StandaloneToolExecutor::StandaloneToolExecutor(
  24. const CompilationDatabase &Compilations,
  25. llvm::ArrayRef<std::string> SourcePaths,
  26. IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
  27. std::shared_ptr<PCHContainerOperations> PCHContainerOps)
  28. : Tool(Compilations, SourcePaths, std::move(PCHContainerOps),
  29. std::move(BaseFS)),
  30. Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
  31. // Use self-defined default argument adjusters instead of the default
  32. // adjusters that come with the old `ClangTool`.
  33. Tool.clearArgumentsAdjusters();
  34. }
  35. StandaloneToolExecutor::StandaloneToolExecutor(
  36. CommonOptionsParser Options,
  37. std::shared_ptr<PCHContainerOperations> PCHContainerOps)
  38. : OptionsParser(std::move(Options)),
  39. Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList(),
  40. std::move(PCHContainerOps)),
  41. Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
  42. Tool.clearArgumentsAdjusters();
  43. }
  44. llvm::Error StandaloneToolExecutor::execute(
  45. llvm::ArrayRef<
  46. std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
  47. Actions) {
  48. if (Actions.empty())
  49. return make_string_error("No action to execute.");
  50. if (Actions.size() != 1)
  51. return make_string_error(
  52. "Only support executing exactly 1 action at this point.");
  53. auto &Action = Actions.front();
  54. Tool.appendArgumentsAdjuster(Action.second);
  55. Tool.appendArgumentsAdjuster(ArgsAdjuster);
  56. if (Tool.run(Action.first.get()))
  57. return make_string_error("Failed to run action.");
  58. return llvm::Error::success();
  59. }
  60. class StandaloneToolExecutorPlugin : public ToolExecutorPlugin {
  61. public:
  62. llvm::Expected<std::unique_ptr<ToolExecutor>>
  63. create(CommonOptionsParser &OptionsParser) override {
  64. if (OptionsParser.getSourcePathList().empty())
  65. return make_string_error(
  66. "[StandaloneToolExecutorPlugin] No positional argument found.");
  67. return std::make_unique<StandaloneToolExecutor>(std::move(OptionsParser));
  68. }
  69. };
  70. static ToolExecutorPluginRegistry::Add<StandaloneToolExecutorPlugin>
  71. X("standalone", "Runs FrontendActions on a set of files provided "
  72. "via positional arguments.");
  73. // This anchor is used to force the linker to link in the generated object file
  74. // and thus register the plugin.
  75. volatile int StandaloneToolExecutorAnchorSource = 0;
  76. } // end namespace tooling
  77. } // end namespace clang