StandaloneExecution.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- StandaloneExecution.h - Standalone execution. -*- C++ ----------*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines standalone execution of clang tools.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
  18. #define LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
  19. #include "clang/Tooling/ArgumentsAdjusters.h"
  20. #include "clang/Tooling/Execution.h"
  21. #include <optional>
  22. namespace clang {
  23. namespace tooling {
  24. /// A standalone executor that runs FrontendActions on a given set of
  25. /// TUs in sequence.
  26. ///
  27. /// By default, this executor uses the following arguments adjusters (as defined
  28. /// in `clang/Tooling/ArgumentsAdjusters.h`):
  29. /// - `getClangStripOutputAdjuster()`
  30. /// - `getClangSyntaxOnlyAdjuster()`
  31. /// - `getClangStripDependencyFileAdjuster()`
  32. class StandaloneToolExecutor : public ToolExecutor {
  33. public:
  34. static const char *ExecutorName;
  35. /// Init with \p CompilationDatabase and the paths of all files to be
  36. /// proccessed.
  37. StandaloneToolExecutor(
  38. const CompilationDatabase &Compilations,
  39. llvm::ArrayRef<std::string> SourcePaths,
  40. IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS =
  41. llvm::vfs::getRealFileSystem(),
  42. std::shared_ptr<PCHContainerOperations> PCHContainerOps =
  43. std::make_shared<PCHContainerOperations>());
  44. /// Init with \p CommonOptionsParser. This is expected to be used by
  45. /// `createExecutorFromCommandLineArgs` based on commandline options.
  46. ///
  47. /// The executor takes ownership of \p Options.
  48. StandaloneToolExecutor(
  49. CommonOptionsParser Options,
  50. std::shared_ptr<PCHContainerOperations> PCHContainerOps =
  51. std::make_shared<PCHContainerOperations>());
  52. StringRef getExecutorName() const override { return ExecutorName; }
  53. using ToolExecutor::execute;
  54. llvm::Error
  55. execute(llvm::ArrayRef<
  56. std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
  57. Actions) override;
  58. /// Set a \c DiagnosticConsumer to use during parsing.
  59. void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) {
  60. Tool.setDiagnosticConsumer(DiagConsumer);
  61. }
  62. ExecutionContext *getExecutionContext() override { return &Context; };
  63. ToolResults *getToolResults() override { return &Results; }
  64. llvm::ArrayRef<std::string> getSourcePaths() const {
  65. return Tool.getSourcePaths();
  66. }
  67. void mapVirtualFile(StringRef FilePath, StringRef Content) override {
  68. Tool.mapVirtualFile(FilePath, Content);
  69. }
  70. /// Returns the file manager used in the tool.
  71. ///
  72. /// The file manager is shared between all translation units.
  73. FileManager &getFiles() { return Tool.getFiles(); }
  74. private:
  75. // Used to store the parser when the executor is initialized with parser.
  76. std::optional<CommonOptionsParser> OptionsParser;
  77. // FIXME: The standalone executor is currently just a wrapper of `ClangTool`.
  78. // Merge `ClangTool` implementation into the this.
  79. ClangTool Tool;
  80. ExecutionContext Context;
  81. InMemoryToolResults Results;
  82. ArgumentsAdjuster ArgsAdjuster;
  83. };
  84. } // end namespace tooling
  85. } // end namespace clang
  86. #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
  87. #ifdef __GNUC__
  88. #pragma GCC diagnostic pop
  89. #endif