|
|
|||
File indexing completed on 2026-05-10 08:37:12
0001 //===--- Execution.h - Executing clang frontend actions -*- C++ ---------*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file defines framework for executing clang frontend actions. 0010 // 0011 // The framework can be extended to support different execution plans including 0012 // standalone execution on the given TUs or parallel execution on all TUs in 0013 // the codebase. 0014 // 0015 // In order to enable multiprocessing execution, tool actions are expected to 0016 // output result into the ToolResults provided by the executor. The 0017 // `ToolResults` is an interface that abstracts how results are stored e.g. 0018 // in-memory for standalone execution or on-disk for large-scale execution. 0019 // 0020 // New executors can be registered as ToolExecutorPlugins via the 0021 // `ToolExecutorPluginRegistry`. CLI tools can use 0022 // `createExecutorFromCommandLineArgs` to create a specific registered executor 0023 // according to the command-line arguments. 0024 // 0025 //===----------------------------------------------------------------------===// 0026 0027 #ifndef LLVM_CLANG_TOOLING_EXECUTION_H 0028 #define LLVM_CLANG_TOOLING_EXECUTION_H 0029 0030 #include "clang/Tooling/CommonOptionsParser.h" 0031 #include "clang/Tooling/Tooling.h" 0032 #include "llvm/Support/Error.h" 0033 #include "llvm/Support/Registry.h" 0034 #include "llvm/Support/StringSaver.h" 0035 0036 namespace clang { 0037 namespace tooling { 0038 0039 extern llvm::cl::opt<std::string> ExecutorName; 0040 0041 /// An abstraction for the result of a tool execution. For example, the 0042 /// underlying result can be in-memory or on-disk. 0043 /// 0044 /// Results should be string key-value pairs. For example, a refactoring tool 0045 /// can use source location as key and a replacement in YAML format as value. 0046 class ToolResults { 0047 public: 0048 virtual ~ToolResults() = default; 0049 virtual void addResult(StringRef Key, StringRef Value) = 0; 0050 virtual std::vector<std::pair<llvm::StringRef, llvm::StringRef>> 0051 AllKVResults() = 0; 0052 virtual void forEachResult( 0053 llvm::function_ref<void(StringRef Key, StringRef Value)> Callback) = 0; 0054 }; 0055 0056 /// Stores the key-value results in memory. It maintains the lifetime of 0057 /// the result. Clang tools using this class are expected to generate a small 0058 /// set of different results, or a large set of duplicated results. 0059 class InMemoryToolResults : public ToolResults { 0060 public: 0061 InMemoryToolResults() : Strings(Arena) {} 0062 void addResult(StringRef Key, StringRef Value) override; 0063 std::vector<std::pair<llvm::StringRef, llvm::StringRef>> 0064 AllKVResults() override; 0065 void forEachResult(llvm::function_ref<void(StringRef Key, StringRef Value)> 0066 Callback) override; 0067 0068 private: 0069 llvm::BumpPtrAllocator Arena; 0070 llvm::UniqueStringSaver Strings; 0071 0072 std::vector<std::pair<llvm::StringRef, llvm::StringRef>> KVResults; 0073 }; 0074 0075 /// The context of an execution, including the information about 0076 /// compilation and results. 0077 class ExecutionContext { 0078 public: 0079 virtual ~ExecutionContext() {} 0080 0081 /// Initializes a context. This does not take ownership of `Results`. 0082 explicit ExecutionContext(ToolResults *Results) : Results(Results) {} 0083 0084 /// Adds a KV pair to the result container of this execution. 0085 void reportResult(StringRef Key, StringRef Value); 0086 0087 // Returns the source control system's revision number if applicable. 0088 // Otherwise returns an empty string. 0089 virtual std::string getRevision() { return ""; } 0090 0091 // Returns the corpus being analyzed, e.g. "llvm" for the LLVM codebase, if 0092 // applicable. 0093 virtual std::string getCorpus() { return ""; } 0094 0095 // Returns the currently processed compilation unit if available. 0096 virtual std::string getCurrentCompilationUnit() { return ""; } 0097 0098 private: 0099 ToolResults *Results; 0100 }; 0101 0102 /// Interface for executing clang frontend actions. 0103 /// 0104 /// This can be extended to support running tool actions in different 0105 /// execution mode, e.g. on a specific set of TUs or many TUs in parallel. 0106 /// 0107 /// New executors can be registered as ToolExecutorPlugins via the 0108 /// `ToolExecutorPluginRegistry`. CLI tools can use 0109 /// `createExecutorFromCommandLineArgs` to create a specific registered 0110 /// executor according to the command-line arguments. 0111 class ToolExecutor { 0112 public: 0113 virtual ~ToolExecutor() {} 0114 0115 /// Returns the name of a specific executor. 0116 virtual StringRef getExecutorName() const = 0; 0117 0118 /// Executes each action with a corresponding arguments adjuster. 0119 virtual llvm::Error 0120 execute(llvm::ArrayRef< 0121 std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>> 0122 Actions) = 0; 0123 0124 /// Convenient functions for the above `execute`. 0125 llvm::Error execute(std::unique_ptr<FrontendActionFactory> Action); 0126 /// Executes an action with an argument adjuster. 0127 llvm::Error execute(std::unique_ptr<FrontendActionFactory> Action, 0128 ArgumentsAdjuster Adjuster); 0129 0130 /// Returns a reference to the execution context. 0131 /// 0132 /// This should be passed to tool callbacks, and tool callbacks should report 0133 /// results via the returned context. 0134 virtual ExecutionContext *getExecutionContext() = 0; 0135 0136 /// Returns a reference to the result container. 0137 /// 0138 /// NOTE: This should only be used after the execution finishes. Tool 0139 /// callbacks should report results via `ExecutionContext` instead. 0140 virtual ToolResults *getToolResults() = 0; 0141 0142 /// Map a virtual file to be used while running the tool. 0143 /// 0144 /// \param FilePath The path at which the content will be mapped. 0145 /// \param Content A buffer of the file's content. 0146 virtual void mapVirtualFile(StringRef FilePath, StringRef Content) = 0; 0147 }; 0148 0149 /// Interface for factories that create specific executors. This is also 0150 /// used as a plugin to be registered into ToolExecutorPluginRegistry. 0151 class ToolExecutorPlugin { 0152 public: 0153 virtual ~ToolExecutorPlugin() {} 0154 0155 /// Create an `ToolExecutor`. 0156 /// 0157 /// `OptionsParser` can be consumed (e.g. moved) if the creation succeeds. 0158 virtual llvm::Expected<std::unique_ptr<ToolExecutor>> 0159 create(CommonOptionsParser &OptionsParser) = 0; 0160 }; 0161 0162 /// This creates a ToolExecutor that is in the global registry based on 0163 /// commandline arguments. 0164 /// 0165 /// This picks the right executor based on the `--executor` option. This parses 0166 /// the commandline arguments with `CommonOptionsParser`, so caller does not 0167 /// need to parse again. 0168 /// 0169 /// By default, this creates a `StandaloneToolExecutor` ("standalone") if 0170 /// `--executor` is not provided. 0171 llvm::Expected<std::unique_ptr<ToolExecutor>> 0172 createExecutorFromCommandLineArgs(int &argc, const char **argv, 0173 llvm::cl::OptionCategory &Category, 0174 const char *Overview = nullptr); 0175 0176 namespace internal { 0177 llvm::Expected<std::unique_ptr<ToolExecutor>> 0178 createExecutorFromCommandLineArgsImpl(int &argc, const char **argv, 0179 llvm::cl::OptionCategory &Category, 0180 const char *Overview = nullptr); 0181 } // end namespace internal 0182 0183 } // end namespace tooling 0184 } // end namespace clang 0185 0186 #endif // LLVM_CLANG_TOOLING_EXECUTION_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|