AllTUsExecution.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- AllTUsExecution.h - Execute actions on all TUs. -*- 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 a tool executor that runs given actions on all TUs in the
  15. // compilation database. Tool results are deuplicated by the result key.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
  19. #define LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
  20. #include "clang/Tooling/ArgumentsAdjusters.h"
  21. #include "clang/Tooling/Execution.h"
  22. #include <optional>
  23. namespace clang {
  24. namespace tooling {
  25. /// Executes given frontend actions on all files/TUs in the compilation
  26. /// database.
  27. class AllTUsToolExecutor : public ToolExecutor {
  28. public:
  29. static const char *ExecutorName;
  30. /// Init with \p CompilationDatabase.
  31. /// This uses \p ThreadCount threads to exececute the actions on all files in
  32. /// parallel. If \p ThreadCount is 0, this uses `llvm::hardware_concurrency`.
  33. AllTUsToolExecutor(const CompilationDatabase &Compilations,
  34. unsigned ThreadCount,
  35. std::shared_ptr<PCHContainerOperations> PCHContainerOps =
  36. std::make_shared<PCHContainerOperations>());
  37. /// Init with \p CommonOptionsParser. This is expected to be used by
  38. /// `createExecutorFromCommandLineArgs` based on commandline options.
  39. ///
  40. /// The executor takes ownership of \p Options.
  41. AllTUsToolExecutor(CommonOptionsParser Options, unsigned ThreadCount,
  42. std::shared_ptr<PCHContainerOperations> PCHContainerOps =
  43. std::make_shared<PCHContainerOperations>());
  44. StringRef getExecutorName() const override { return ExecutorName; }
  45. using ToolExecutor::execute;
  46. llvm::Error
  47. execute(llvm::ArrayRef<
  48. std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
  49. Actions) override;
  50. ExecutionContext *getExecutionContext() override { return &Context; };
  51. ToolResults *getToolResults() override { return Results.get(); }
  52. void mapVirtualFile(StringRef FilePath, StringRef Content) override {
  53. OverlayFiles[FilePath] = std::string(Content);
  54. }
  55. private:
  56. // Used to store the parser when the executor is initialized with parser.
  57. std::optional<CommonOptionsParser> OptionsParser;
  58. const CompilationDatabase &Compilations;
  59. std::unique_ptr<ToolResults> Results;
  60. ExecutionContext Context;
  61. llvm::StringMap<std::string> OverlayFiles;
  62. unsigned ThreadCount;
  63. };
  64. extern llvm::cl::opt<unsigned> ExecutorConcurrency;
  65. extern llvm::cl::opt<std::string> Filter;
  66. } // end namespace tooling
  67. } // end namespace clang
  68. #endif // LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
  69. #ifdef __GNUC__
  70. #pragma GCC diagnostic pop
  71. #endif