Execution.cpp 3.7 KB

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