10b57cec5SDimitry Andric //===--- AllTUsExecution.h - Execute actions on all TUs. -*- C++ --------*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file defines a tool executor that runs given actions on all TUs in the
100b57cec5SDimitry Andric //  compilation database. Tool results are deuplicated by the result key.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
150b57cec5SDimitry Andric #define LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "clang/Tooling/ArgumentsAdjusters.h"
180b57cec5SDimitry Andric #include "clang/Tooling/Execution.h"
19bdd1243dSDimitry Andric #include <optional>
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric namespace clang {
220b57cec5SDimitry Andric namespace tooling {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric /// Executes given frontend actions on all files/TUs in the compilation
250b57cec5SDimitry Andric /// database.
260b57cec5SDimitry Andric class AllTUsToolExecutor : public ToolExecutor {
270b57cec5SDimitry Andric public:
280b57cec5SDimitry Andric   static const char *ExecutorName;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric   /// Init with \p CompilationDatabase.
310b57cec5SDimitry Andric   /// This uses \p ThreadCount threads to exececute the actions on all files in
320b57cec5SDimitry Andric   /// parallel. If \p ThreadCount is 0, this uses `llvm::hardware_concurrency`.
330b57cec5SDimitry Andric   AllTUsToolExecutor(const CompilationDatabase &Compilations,
340b57cec5SDimitry Andric                      unsigned ThreadCount,
350b57cec5SDimitry Andric                      std::shared_ptr<PCHContainerOperations> PCHContainerOps =
360b57cec5SDimitry Andric                          std::make_shared<PCHContainerOperations>());
370b57cec5SDimitry Andric 
380b57cec5SDimitry Andric   /// Init with \p CommonOptionsParser. This is expected to be used by
390b57cec5SDimitry Andric   /// `createExecutorFromCommandLineArgs` based on commandline options.
400b57cec5SDimitry Andric   ///
410b57cec5SDimitry Andric   /// The executor takes ownership of \p Options.
420b57cec5SDimitry Andric   AllTUsToolExecutor(CommonOptionsParser Options, unsigned ThreadCount,
430b57cec5SDimitry Andric                      std::shared_ptr<PCHContainerOperations> PCHContainerOps =
440b57cec5SDimitry Andric                          std::make_shared<PCHContainerOperations>());
450b57cec5SDimitry Andric 
getExecutorName()460b57cec5SDimitry Andric   StringRef getExecutorName() const override { return ExecutorName; }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   using ToolExecutor::execute;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   llvm::Error
510b57cec5SDimitry Andric   execute(llvm::ArrayRef<
520b57cec5SDimitry Andric           std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
530b57cec5SDimitry Andric               Actions) override;
540b57cec5SDimitry Andric 
getExecutionContext()550b57cec5SDimitry Andric   ExecutionContext *getExecutionContext() override { return &Context; };
560b57cec5SDimitry Andric 
getToolResults()570b57cec5SDimitry Andric   ToolResults *getToolResults() override { return Results.get(); }
580b57cec5SDimitry Andric 
mapVirtualFile(StringRef FilePath,StringRef Content)590b57cec5SDimitry Andric   void mapVirtualFile(StringRef FilePath, StringRef Content) override {
605ffd83dbSDimitry Andric     OverlayFiles[FilePath] = std::string(Content);
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric private:
640b57cec5SDimitry Andric   // Used to store the parser when the executor is initialized with parser.
65bdd1243dSDimitry Andric   std::optional<CommonOptionsParser> OptionsParser;
660b57cec5SDimitry Andric   const CompilationDatabase &Compilations;
670b57cec5SDimitry Andric   std::unique_ptr<ToolResults> Results;
680b57cec5SDimitry Andric   ExecutionContext Context;
690b57cec5SDimitry Andric   llvm::StringMap<std::string> OverlayFiles;
700b57cec5SDimitry Andric   unsigned ThreadCount;
710b57cec5SDimitry Andric };
720b57cec5SDimitry Andric 
73a7dea167SDimitry Andric extern llvm::cl::opt<unsigned> ExecutorConcurrency;
740b57cec5SDimitry Andric extern llvm::cl::opt<std::string> Filter;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric } // end namespace tooling
770b57cec5SDimitry Andric } // end namespace clang
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric #endif // LLVM_CLANG_TOOLING_ALLTUSEXECUTION_H
80