Path: blob/main/contrib/llvm-project/clang/lib/Tooling/StandaloneExecution.cpp
35233 views
//===- lib/Tooling/Execution.cpp - Standalone clang action execution. -----===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Tooling/StandaloneExecution.h"9#include "clang/Tooling/ToolExecutorPluginRegistry.h"1011namespace clang {12namespace tooling {1314static llvm::Error make_string_error(const llvm::Twine &Message) {15return llvm::make_error<llvm::StringError>(Message,16llvm::inconvertibleErrorCode());17}1819const char *StandaloneToolExecutor::ExecutorName = "StandaloneToolExecutor";2021static ArgumentsAdjuster getDefaultArgumentsAdjusters() {22return combineAdjusters(23getClangStripOutputAdjuster(),24combineAdjusters(getClangSyntaxOnlyAdjuster(),25getClangStripDependencyFileAdjuster()));26}2728StandaloneToolExecutor::StandaloneToolExecutor(29const CompilationDatabase &Compilations,30llvm::ArrayRef<std::string> SourcePaths,31IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS,32std::shared_ptr<PCHContainerOperations> PCHContainerOps)33: Tool(Compilations, SourcePaths, std::move(PCHContainerOps),34std::move(BaseFS)),35Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {36// Use self-defined default argument adjusters instead of the default37// adjusters that come with the old `ClangTool`.38Tool.clearArgumentsAdjusters();39}4041StandaloneToolExecutor::StandaloneToolExecutor(42CommonOptionsParser Options,43std::shared_ptr<PCHContainerOperations> PCHContainerOps)44: OptionsParser(std::move(Options)),45Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList(),46std::move(PCHContainerOps)),47Context(&Results), ArgsAdjuster(getDefaultArgumentsAdjusters()) {48Tool.clearArgumentsAdjusters();49}5051llvm::Error StandaloneToolExecutor::execute(52llvm::ArrayRef<53std::pair<std::unique_ptr<FrontendActionFactory>, ArgumentsAdjuster>>54Actions) {55if (Actions.empty())56return make_string_error("No action to execute.");5758if (Actions.size() != 1)59return make_string_error(60"Only support executing exactly 1 action at this point.");6162auto &Action = Actions.front();63Tool.appendArgumentsAdjuster(Action.second);64Tool.appendArgumentsAdjuster(ArgsAdjuster);65if (Tool.run(Action.first.get()))66return make_string_error("Failed to run action.");6768return llvm::Error::success();69}7071class StandaloneToolExecutorPlugin : public ToolExecutorPlugin {72public:73llvm::Expected<std::unique_ptr<ToolExecutor>>74create(CommonOptionsParser &OptionsParser) override {75if (OptionsParser.getSourcePathList().empty())76return make_string_error(77"[StandaloneToolExecutorPlugin] No positional argument found.");78return std::make_unique<StandaloneToolExecutor>(std::move(OptionsParser));79}80};8182static ToolExecutorPluginRegistry::Add<StandaloneToolExecutorPlugin>83X("standalone", "Runs FrontendActions on a set of files provided "84"via positional arguments.");8586// This anchor is used to force the linker to link in the generated object file87// and thus register the plugin.88volatile int StandaloneToolExecutorAnchorSource = 0;8990} // end namespace tooling91} // end namespace clang929394