clang  10.0.0svn
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 
19 namespace clang {
20 namespace tooling {
21 
22 /// A standalone executor that runs FrontendActions on a given set of
23 /// TUs in sequence.
24 ///
25 /// By default, this executor uses the following arguments adjusters (as defined
26 /// in `clang/Tooling/ArgumentsAdjusters.h`):
27 /// - `getClangStripOutputAdjuster()`
28 /// - `getClangSyntaxOnlyAdjuster()`
29 /// - `getClangStripDependencyFileAdjuster()`
31 public:
32  static const char *ExecutorName;
33 
34  /// Init with \p CompilationDatabase and the paths of all files to be
35  /// proccessed.
37  const CompilationDatabase &Compilations,
38  llvm::ArrayRef<std::string> SourcePaths,
40  llvm::vfs::getRealFileSystem(),
41  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
42  std::make_shared<PCHContainerOperations>());
43 
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.
49  CommonOptionsParser Options,
50  std::shared_ptr<PCHContainerOperations> PCHContainerOps =
51  std::make_shared<PCHContainerOperations>());
52 
53  StringRef getExecutorName() const override { return ExecutorName; }
54 
56 
59  std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
60  Actions) override;
61 
62  /// Set a \c DiagnosticConsumer to use during parsing.
64  Tool.setDiagnosticConsumer(DiagConsumer);
65  }
66 
67  ExecutionContext *getExecutionContext() override { return &Context; };
68 
69  ToolResults *getToolResults() override { return &Results; }
70 
72  return Tool.getSourcePaths();
73  }
74 
75  void mapVirtualFile(StringRef FilePath, StringRef Content) override {
76  Tool.mapVirtualFile(FilePath, Content);
77  }
78 
79  /// Returns the file manager used in the tool.
80  ///
81  /// The file manager is shared between all translation units.
82  FileManager &getFiles() { return Tool.getFiles(); }
83 
84 private:
85  // Used to store the parser when the executor is initialized with parser.
87  // FIXME: The standalone executor is currently just a wrapper of `ClangTool`.
88  // Merge `ClangTool` implementation into the this.
89  ClangTool Tool;
90  ExecutionContext Context;
91  InMemoryToolResults Results;
92  ArgumentsAdjuster ArgsAdjuster;
93 };
94 
95 } // end namespace tooling
96 } // end namespace clang
97 
98 #endif // LLVM_CLANG_TOOLING_STANDALONEEXECUTION_H
virtual llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions)=0
Executes each action with a corresponding arguments adjuster.
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer)
Set a DiagnosticConsumer to use during parsing.
Definition: Tooling.h:328
ExecutionContext * getExecutionContext() override
Returns a reference to the execution context.
Implements support for file system lookup, file system caching, and directory search management...
Definition: FileManager.h:171
llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions) override
Executes each action with a corresponding arguments adjuster.
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:303
FileManager & getFiles()
Returns the file manager used in the tool.
Abstract interface, implemented by clients of the front-end, which formats and prints fully processed...
Definition: Diagnostic.h:1489
The context of an execution, including the information about compilation and results.
Definition: Execution.h:77
Interface for executing clang frontend actions.
Definition: Execution.h:111
FileManager & getFiles()
Returns the file manager used in the tool.
Definition: Tooling.h:371
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
llvm::ArrayRef< std::string > getSourcePaths() const
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.
Interface for compilation databases.
A parser for options common to all command-line Clang tools.
void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer)
Set a DiagnosticConsumer to use during parsing.
StringRef getExecutorName() const override
Returns the name of a specific executor.
void mapVirtualFile(StringRef FilePath, StringRef Content)
Map a virtual file to be used while running the tool.
Definition: Tooling.cpp:424
Dataflow Directional Tag Classes.
An abstraction for the result of a tool execution.
Definition: Execution.h:46
llvm::ArrayRef< std::string > getSourcePaths() const
Definition: Tooling.h:373
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.
ToolResults * getToolResults() override
Returns a reference to the result container.