clang  19.0.0git
StandaloneExecution.h
Go to the documentation of this file.
1 //===--- StandaloneExecution.h - Standalone execution. -*- C++ ----------*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines standalone execution of clang tools.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
14 #define LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
15 
18 #include <optional>
19 
20 namespace clang {
21 namespace tooling {
22 
23 /// A standalone executor that runs FrontendActions on a given set of
24 /// TUs in sequence.
25 ///
26 /// By default, this executor uses the following arguments adjusters (as defined
27 /// in `clang/Tooling/ArgumentsAdjusters.h`):
28 /// - `getClangStripOutputAdjuster()`
29 /// - `getClangSyntaxOnlyAdjuster()`
30 /// - `getClangStripDependencyFileAdjuster()`
32 public:
33  static const char *ExecutorName;
34 
35  /// Init with \p CompilationDatabase and the paths of all files to be
36  /// proccessed.
38  const CompilationDatabase &Compilations,
39  llvm::ArrayRef<std::string> SourcePaths,
41  llvm::vfs::getRealFileSystem(),
42  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
43  std::make_shared<PCHContainerOperations>());
44 
45  /// Init with \p CommonOptionsParser. This is expected to be used by
46  /// `createExecutorFromCommandLineArgs` based on commandline options.
47  ///
48  /// The executor takes ownership of \p Options.
50  CommonOptionsParser Options,
51  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
52  std::make_shared<PCHContainerOperations>());
53 
54  StringRef getExecutorName() const override { return ExecutorName; }
55 
57 
58  llvm::Error
60  std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
61  Actions) override;
62 
63  /// Set a \c DiagnosticConsumer to use during parsing.
65  Tool.setDiagnosticConsumer(DiagConsumer);
66  }
67 
68  ExecutionContext *getExecutionContext() override { return &Context; };
69 
70  ToolResults *getToolResults() override { return &Results; }
71 
73  return Tool.getSourcePaths();
74  }
75 
76  void mapVirtualFile(StringRef FilePath, StringRef Content) override {
77  Tool.mapVirtualFile(FilePath, Content);
78  }
79 
80  /// Returns the file manager used in the tool.
81  ///
82  /// The file manager is shared between all translation units.
83  FileManager &getFiles() { return Tool.getFiles(); }
84 
85 private:
86  // Used to store the parser when the executor is initialized with parser.
87  std::optional<CommonOptionsParser> OptionsParser;
88  // FIXME: The standalone executor is currently just a wrapper of `ClangTool`.
89  // Merge `ClangTool` implementation into the this.
90  ClangTool Tool;
91  ExecutionContext Context;
92  InMemoryToolResults Results;
93  ArgumentsAdjuster ArgsAdjuster;
94 };
95 
96 } // end namespace tooling
97 } // end namespace clang
98 
99 #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1751
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:311
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer)
Set a DiagnosticConsumer to use during parsing.
Definition: Tooling.h:336
FileManager & getFiles()
Returns the file manager used in the tool.
Definition: Tooling.h:374
void mapVirtualFile(StringRef FilePath, StringRef Content)
Map a virtual file to be used while running the tool.
Definition: Tooling.cpp:492
llvm::ArrayRef< std::string > getSourcePaths() const
Definition: Tooling.h:376
A parser for options common to all command-line Clang tools.
Interface for compilation databases.
The context of an execution, including the information about compilation and results.
Definition: Execution.h:77
Stores the key-value results in memory.
Definition: Execution.h:59
A standalone executor that runs FrontendActions on a given set of TUs in sequence.
StandaloneToolExecutor(const CompilationDatabase &Compilations, llvm::ArrayRef< std::string > SourcePaths, IntrusiveRefCntPtr< llvm::vfs::FileSystem > BaseFS=llvm::vfs::getRealFileSystem(), std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Init with CompilationDatabase and the paths of all files to be proccessed.
void mapVirtualFile(StringRef FilePath, StringRef Content) override
Map a virtual file to be used while running the tool.
llvm::ArrayRef< std::string > getSourcePaths() const
ExecutionContext * getExecutionContext() override
Returns a reference to the execution context.
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer)
Set a DiagnosticConsumer to use during parsing.
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions)=0
Executes each action with a corresponding arguments adjuster.
StringRef getExecutorName() const override
Returns the name of a specific executor.
FileManager & getFiles()
Returns the file manager used in the tool.
ToolResults * getToolResults() override
Returns a reference to the result container.
Interface for executing clang frontend actions.
Definition: Execution.h:111
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions)=0
Executes each action with a corresponding arguments adjuster.
An abstraction for the result of a tool execution.
Definition: Execution.h:46
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
The JSON file list parser is used to communicate input to InstallAPI.