clang  6.0.0svn
StandaloneExecution.cpp
Go to the documentation of this file.
1 //===- lib/Tooling/Execution.cpp - Standalone clang action execution. -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 
13 namespace clang {
14 namespace tooling {
15 
16 static llvm::Error make_string_error(const llvm::Twine &Message) {
17  return llvm::make_error<llvm::StringError>(Message,
18  llvm::inconvertibleErrorCode());
19 }
20 
21 const char *StandaloneToolExecutor::ExecutorName = "StandaloneToolExecutor";
22 
24  return combineAdjusters(
28 }
29 
31  const CompilationDatabase &Compilations,
32  llvm::ArrayRef<std::string> SourcePaths,
33  std::shared_ptr<PCHContainerOperations> PCHContainerOps)
34  : Tool(Compilations, SourcePaths), Context(&Results),
35  ArgsAdjuster(getDefaultArgumentsAdjusters()) {
36  // Use self-defined default argument adjusters instead of the default
37  // adjusters that come with the old `ClangTool`.
39 }
40 
42  CommonOptionsParser Options,
43  std::shared_ptr<PCHContainerOperations> PCHContainerOps)
44  : OptionsParser(std::move(Options)),
45  Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList(),
46  PCHContainerOps),
47  Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
49 }
50 
53  std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
54  Actions) {
55  if (Actions.empty())
56  return make_string_error("No action to execute.");
57 
58  if (Actions.size() != 1)
59  return make_string_error(
60  "Only support executing exactly 1 action at this point.");
61 
62  auto &Action = Actions.front();
63  Tool.appendArgumentsAdjuster(Action.second);
64  Tool.appendArgumentsAdjuster(ArgsAdjuster);
65  if (Tool.run(Action.first.get()))
66  return make_string_error("Failed to run action.");
67 
68  return llvm::Error::success();
69 }
70 
72 public:
74  create(CommonOptionsParser &OptionsParser) override {
75  if (OptionsParser.getSourcePathList().empty())
76  return make_string_error(
77  "[StandaloneToolExecutorPlugin] No positional argument found.");
78  return llvm::make_unique<StandaloneToolExecutor>(std::move(OptionsParser));
79  }
80 };
81 
82 static ToolExecutorPluginRegistry::Add<StandaloneToolExecutorPlugin>
83  X("standalone", "Runs FrontendActions on a set of files provided "
84  "via positional arguments.");
85 
86 // This anchor is used to force the linker to link in the generated object file
87 // and thus register the plugin.
89 
90 } // end namespace tooling
91 } // end namespace clang
static llvm::Error make_string_error(const llvm::Twine &Message)
llvm::Expected< std::unique_ptr< ToolExecutor > > create(CommonOptionsParser &OptionsParser) override
Create an ToolExecutor.
llvm::Error execute(llvm::ArrayRef< std::pair< std::unique_ptr< FrontendActionFactory >, ArgumentsAdjuster >> Actions) override
Executes each action with a corresponding arguments adjuster.
volatile int StandaloneToolExecutorAnchorSource
ArgumentsAdjuster getClangStripDependencyFileAdjuster()
Gets an argument adjuster which removes dependency-file related command line arguments.
ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, ArgumentsAdjuster Second)
Gets an argument adjuster which adjusts the arguments in sequence with the First adjuster and then wi...
void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster)
Append a command line arguments adjuster to the adjuster chain.
Definition: Tooling.cpp:350
Definition: Format.h:1900
void clearArgumentsAdjusters()
Clear the command line arguments adjuster chain.
Definition: Tooling.cpp:354
ArgumentsAdjuster getClangStripOutputAdjuster()
Gets an argument adjuster which removes output-related command line arguments.
std::function< CommandLineArguments(const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster
A prototype of a command line adjuster.
Interface for compilation databases.
A parser for options common to all command-line Clang tools.
static CompilationDatabasePluginRegistry::Add< JSONCompilationDatabasePlugin > X("json-compilation-database", "Reads JSON formatted compilation databases")
Dataflow Directional Tag Classes.
StandaloneToolExecutor(const CompilationDatabase &Compilations, llvm::ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Init with CompilationDatabase and the paths of all files to be proccessed.
Interface for factories that create specific executors.
Definition: Execution.h:140
ArgumentsAdjuster getClangSyntaxOnlyAdjuster()
Gets an argument adjuster that converts input command line arguments to the "syntax check only" varia...
const std::vector< std::string > & getSourcePathList() const
Returns a list of source file paths to process.
int run(ToolAction *Action)
Runs an action over all files specified in the command line.
Definition: Tooling.cpp:370
static ArgumentsAdjuster getDefaultArgumentsAdjusters()