2017-10-26 18:38:14 +08:00
|
|
|
//===- lib/Tooling/Execution.cpp - Standalone clang action execution. -----===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-10-26 18:38:14 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/StandaloneExecution.h"
|
|
|
|
#include "clang/Tooling/ToolExecutorPluginRegistry.h"
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tooling {
|
|
|
|
|
|
|
|
static llvm::Error make_string_error(const llvm::Twine &Message) {
|
|
|
|
return llvm::make_error<llvm::StringError>(Message,
|
|
|
|
llvm::inconvertibleErrorCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *StandaloneToolExecutor::ExecutorName = "StandaloneToolExecutor";
|
|
|
|
|
|
|
|
static ArgumentsAdjuster getDefaultArgumentsAdjusters() {
|
|
|
|
return combineAdjusters(
|
|
|
|
getClangStripOutputAdjuster(),
|
|
|
|
combineAdjusters(getClangSyntaxOnlyAdjuster(),
|
|
|
|
getClangStripDependencyFileAdjuster()));
|
|
|
|
}
|
|
|
|
|
|
|
|
StandaloneToolExecutor::StandaloneToolExecutor(
|
|
|
|
const CompilationDatabase &Compilations,
|
|
|
|
llvm::ArrayRef<std::string> SourcePaths,
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,
|
2017-10-26 18:38:14 +08:00
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
|
2018-07-13 02:32:11 +08:00
|
|
|
: Tool(Compilations, SourcePaths, std::move(PCHContainerOps),
|
|
|
|
std::move(BaseFS)),
|
|
|
|
Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
|
2017-10-26 18:38:14 +08:00
|
|
|
// Use self-defined default argument adjusters instead of the default
|
|
|
|
// adjusters that come with the old `ClangTool`.
|
|
|
|
Tool.clearArgumentsAdjusters();
|
|
|
|
}
|
|
|
|
|
|
|
|
StandaloneToolExecutor::StandaloneToolExecutor(
|
|
|
|
CommonOptionsParser Options,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHContainerOps)
|
|
|
|
: OptionsParser(std::move(Options)),
|
|
|
|
Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList(),
|
2018-07-13 02:32:11 +08:00
|
|
|
std::move(PCHContainerOps)),
|
2017-10-26 18:38:14 +08:00
|
|
|
Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {
|
|
|
|
Tool.clearArgumentsAdjusters();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::Error StandaloneToolExecutor::execute(
|
|
|
|
llvm::ArrayRef<
|
|
|
|
std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>
|
|
|
|
Actions) {
|
|
|
|
if (Actions.empty())
|
|
|
|
return make_string_error("No action to execute.");
|
|
|
|
|
|
|
|
if (Actions.size() != 1)
|
|
|
|
return make_string_error(
|
|
|
|
"Only support executing exactly 1 action at this point.");
|
|
|
|
|
|
|
|
auto &Action = Actions.front();
|
|
|
|
Tool.appendArgumentsAdjuster(Action.second);
|
|
|
|
Tool.appendArgumentsAdjuster(ArgsAdjuster);
|
2017-10-26 21:09:50 +08:00
|
|
|
if (Tool.run(Action.first.get()))
|
2017-10-26 18:38:14 +08:00
|
|
|
return make_string_error("Failed to run action.");
|
|
|
|
|
|
|
|
return llvm::Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
class StandaloneToolExecutorPlugin : public ToolExecutorPlugin {
|
|
|
|
public:
|
|
|
|
llvm::Expected<std::unique_ptr<ToolExecutor>>
|
|
|
|
create(CommonOptionsParser &OptionsParser) override {
|
|
|
|
if (OptionsParser.getSourcePathList().empty())
|
|
|
|
return make_string_error(
|
|
|
|
"[StandaloneToolExecutorPlugin] No positional argument found.");
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<StandaloneToolExecutor>(std::move(OptionsParser));
|
2017-10-26 18:38:14 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static ToolExecutorPluginRegistry::Add<StandaloneToolExecutorPlugin>
|
|
|
|
X("standalone", "Runs FrontendActions on a set of files provided "
|
|
|
|
"via positional arguments.");
|
|
|
|
|
2017-11-03 23:57:27 +08:00
|
|
|
// This anchor is used to force the linker to link in the generated object file
|
|
|
|
// and thus register the plugin.
|
|
|
|
volatile int StandaloneToolExecutorAnchorSource = 0;
|
|
|
|
|
2017-10-26 18:38:14 +08:00
|
|
|
} // end namespace tooling
|
|
|
|
} // end namespace clang
|