Execution.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===- lib/Tooling/Execution.cpp - Implements tool execution framework. ---===//
  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/Execution.h"
  9. #include "clang/Tooling/ToolExecutorPluginRegistry.h"
  10. #include "clang/Tooling/Tooling.h"
  11. LLVM_INSTANTIATE_REGISTRY(clang::tooling::ToolExecutorPluginRegistry)
  12. namespace clang {
  13. namespace tooling {
  14. llvm::cl::opt<std::string>
  15. ExecutorName("executor", llvm::cl::desc("The name of the executor to use."),
  16. llvm::cl::init("standalone"));
  17. void InMemoryToolResults::addResult(StringRef Key, StringRef Value) {
  18. KVResults.push_back({Strings.save(Key), Strings.save(Value)});
  19. }
  20. std::vector<std::pair<llvm::StringRef, llvm::StringRef>>
  21. InMemoryToolResults::AllKVResults() {
  22. return KVResults;
  23. }
  24. void InMemoryToolResults::forEachResult(
  25. llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) {
  26. for (const auto &KV : KVResults) {
  27. Callback(KV.first, KV.second);
  28. }
  29. }
  30. void ExecutionContext::reportResult(StringRef Key, StringRef Value) {
  31. Results->addResult(Key, Value);
  32. }
  33. llvm::Error
  34. ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action) {
  35. return execute(std::move(Action), ArgumentsAdjuster());
  36. }
  37. llvm::Error ToolExecutor::execute(std::unique_ptr<FrontendActionFactory> Action,
  38. ArgumentsAdjuster Adjuster) {
  39. std::vector<
  40. std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
  41. Actions;
  42. Actions.emplace_back(std::move(Action), std::move(Adjuster));
  43. return execute(Actions);
  44. }
  45. namespace internal {
  46. llvm::Expected<std::unique_ptr<ToolExecutor>>
  47. createExecutorFromCommandLineArgsImpl(int &argc, const char **argv,
  48. llvm::cl::OptionCategory &Category,
  49. const char *Overview) {
  50. auto OptionsParser =
  51. CommonOptionsParser::create(argc, argv, Category, llvm::cl::ZeroOrMore,
  52. /*Overview=*/Overview);
  53. if (!OptionsParser)
  54. return OptionsParser.takeError();
  55. for (const auto &TEPlugin : ToolExecutorPluginRegistry::entries()) {
  56. if (TEPlugin.getName() != ExecutorName) {
  57. continue;
  58. }
  59. std::unique_ptr<ToolExecutorPlugin> Plugin(TEPlugin.instantiate());
  60. llvm::Expected<std::unique_ptr<ToolExecutor>> Executor =
  61. Plugin->create(*OptionsParser);
  62. if (!Executor) {
  63. return llvm::make_error<llvm::StringError>(
  64. llvm::Twine("Failed to create '") + TEPlugin.getName() +
  65. "': " + llvm::toString(Executor.takeError()) + "\n",
  66. llvm::inconvertibleErrorCode());
  67. }
  68. return std::move(*Executor);
  69. }
  70. return llvm::make_error<llvm::StringError>(
  71. llvm::Twine("Executor \"") + ExecutorName + "\" is not registered.",
  72. llvm::inconvertibleErrorCode());
  73. }
  74. } // end namespace internal
  75. llvm::Expected<std::unique_ptr<ToolExecutor>>
  76. createExecutorFromCommandLineArgs(int &argc, const char **argv,
  77. llvm::cl::OptionCategory &Category,
  78. const char *Overview) {
  79. return internal::createExecutorFromCommandLineArgsImpl(argc, argv, Category,
  80. Overview);
  81. }
  82. // This anchor is used to force the linker to link in the generated object file
  83. // and thus register the StandaloneToolExecutorPlugin etc.
  84. extern volatile int StandaloneToolExecutorAnchorSource;
  85. extern volatile int AllTUsToolExecutorAnchorSource;
  86. static int LLVM_ATTRIBUTE_UNUSED StandaloneToolExecutorAnchorDest =
  87. StandaloneToolExecutorAnchorSource;
  88. static int LLVM_ATTRIBUTE_UNUSED AllTUsToolExecutorAnchorDest =
  89. AllTUsToolExecutorAnchorSource;
  90. } // end namespace tooling
  91. } // end namespace clang