Author: Anutosh Bhat Date: 2026-09-18T20:15:47+05:30 New Revision: fce3facdfe49924687eaba7b2239ee101104b2c3
URL: https://github.com/llvm/llvm-project/commit/fce3facdfe49924687eaba7b2239ee101104b2c3 DIFF: https://github.com/llvm/llvm-project/commit/fce3facdfe49924687eaba7b2239ee101104b2c3.diff LOG: Reapply [Support] ToolSession for in-process tool invocation (#224515) This PR Relands #221996, which was approved and merged but reverted in #224368 after failures in the Fuchsia builders. This PR contains two commits: 1. The original approved commit, reapplied without changes. 2. A small follow-up fixing the reported failures. The problem was that a direct Clang invocation was incorrectly marked as needing a prepended tool name. When Clang launched cc1 out of process, we produced: clang clang -cc1 ... instead of: clang -cc1 ... This affected `cc-print-proc-stat.c` and the macOS runtime compiler checks. The second commit determines whether the session executable already dispatches to the selected tool before setting `NeedsPrependArg`. It also adds regression tests for direct invocation and overlapping tool names. Tested with the ToolSession unit tests, llvm-driver tests, the previously failing Clang test, and forced out-of-process cc1 compilation. Added: llvm/include/llvm/Support/Driver.h llvm/lib/Support/Driver.cpp llvm/test/tools/llvm-driver/session-dispatch.test llvm/unittests/Support/LLVMToolSession/CMakeLists.txt llvm/unittests/Support/LLVMToolSession/LLVMToolSessionTest.cpp Modified: clang/tools/clang-installapi/ClangInstallAPI.cpp clang/tools/clang-scan-deps/ClangScanDeps.cpp clang/tools/driver/cc1gen_reproducer_main.cpp clang/tools/driver/driver.cpp lld/tools/lld/lld.cpp llvm/cmake/modules/llvm-driver-template.cpp.in llvm/lib/Support/CMakeLists.txt llvm/tools/dsymutil/dsymutil.cpp llvm/tools/llvm-ar/llvm-ar.cpp llvm/tools/llvm-cgdata/llvm-cgdata.cpp llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp llvm/tools/llvm-driver/llvm-driver.cpp llvm/tools/llvm-dwp/llvm-dwp.cpp llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp llvm/tools/llvm-ifs/llvm-ifs.cpp llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp llvm/tools/llvm-lipo/llvm-lipo.cpp llvm/tools/llvm-ml/llvm-ml.cpp llvm/tools/llvm-mt/llvm-mt.cpp llvm/tools/llvm-nm/llvm-nm.cpp llvm/tools/llvm-objcopy/llvm-objcopy.cpp llvm/tools/llvm-objdump/llvm-objdump.cpp llvm/tools/llvm-rc/llvm-rc.cpp llvm/tools/llvm-readobj/llvm-readobj.cpp llvm/tools/llvm-readtapi/llvm-readtapi.cpp llvm/tools/llvm-size/llvm-size.cpp llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp llvm/tools/sancov/sancov.cpp llvm/unittests/Support/CMakeLists.txt Removed: llvm/include/llvm/Support/LLVMDriver.h ################################################################################ diff --git a/clang/tools/clang-installapi/ClangInstallAPI.cpp b/clang/tools/clang-installapi/ClangInstallAPI.cpp index baa10ff3e2812..e7251dfb2ded7 100644 --- a/clang/tools/clang-installapi/ClangInstallAPI.cpp +++ b/clang/tools/clang-installapi/ClangInstallAPI.cpp @@ -25,7 +25,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Process.h" diff --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp index 83b4860c130c3..cb4d3f53ff189 100644 --- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp +++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp @@ -19,11 +19,11 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/Format.h" #include "llvm/Support/JSON.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Program.h" #include "llvm/Support/Signals.h" diff --git a/clang/tools/driver/cc1gen_reproducer_main.cpp b/clang/tools/driver/cc1gen_reproducer_main.cpp index d8e4a0d470bc5..810d96363b91b 100644 --- a/clang/tools/driver/cc1gen_reproducer_main.cpp +++ b/clang/tools/driver/cc1gen_reproducer_main.cpp @@ -17,9 +17,9 @@ #include "clang/Driver/Driver.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/IOSandbox.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/YAMLTraits.h" diff --git a/clang/tools/driver/driver.cpp b/clang/tools/driver/driver.cpp index d4d913a8977a4..af96812e9ebd9 100644 --- a/clang/tools/driver/driver.cpp +++ b/clang/tools/driver/driver.cpp @@ -36,10 +36,10 @@ #include "llvm/Support/BuryPointer.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/CrashRecoveryContext.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/IOSandbox.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/Process.h" diff --git a/lld/tools/lld/lld.cpp b/lld/tools/lld/lld.cpp index d6800fa1eea4b..53f0cbc6d9814 100644 --- a/lld/tools/lld/lld.cpp +++ b/lld/tools/lld/lld.cpp @@ -33,7 +33,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/CrashRecoveryContext.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Path.h" #include "llvm/Support/PluginLoader.h" #include "llvm/Support/Process.h" diff --git a/llvm/cmake/modules/llvm-driver-template.cpp.in b/llvm/cmake/modules/llvm-driver-template.cpp.in index d4c385c8cf412..f51b21104afb0 100644 --- a/llvm/cmake/modules/llvm-driver-template.cpp.in +++ b/llvm/cmake/modules/llvm-driver-template.cpp.in @@ -8,7 +8,7 @@ #include "llvm/ADT/ArrayRef.h" #include "llvm/Support/InitLLVM.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" int @TOOL_NAME@_main(int argc, char **, const llvm::ToolContext &); diff --git a/llvm/include/llvm/Support/Driver.h b/llvm/include/llvm/Support/Driver.h new file mode 100644 index 0000000000000..eef1d21cc8e0a --- /dev/null +++ b/llvm/include/llvm/Support/Driver.h @@ -0,0 +1,96 @@ +//===- Driver.h -------------------------------------------------*- C++ -*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_DRIVER_H +#define LLVM_SUPPORT_DRIVER_H + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/ErrorOr.h" + +#include <functional> +#include <memory> + +namespace llvm { + +class ToolSession; +class ToolContext; + +using ToolMainFn = std::function<int(int, char **, const ToolContext &)>; + +/// An LLVM command-line tool that can be invoked without creating a process. +struct CallableTool { + StringRef Name; + ToolMainFn Main; + + explicit operator bool() const { return static_cast<bool>(Main); } +}; + +/// Describes how a tool was invoked and provides access to its host session. +class ToolContext { + ToolSession *Session = nullptr; + + friend class ToolSession; + +public: + const char *Path; + const char *PrependArg; + // PrependArg will be added unconditionally by the llvm-driver, but + // NeedsPrependArg will be false if Path is adequate to reinvoke the tool. + // This is useful if realpath is ever called on Path, in which case it will + // point to the llvm-driver executable, where PrependArg will be needed to + // invoke the correct tool. + bool NeedsPrependArg; + + ToolContext(const char *Path, const char *PrependArg, bool NeedsPrependArg) + : Path(Path), PrependArg(PrependArg), NeedsPrependArg(NeedsPrependArg) {} + + /// Finds a tool registered with the session that owns this context. + LLVM_ABI ErrorOr<CallableTool> getCallableTool(StringRef Name) const; + + /// Invokes another tool registered with the same host session. + LLVM_ABI ErrorOr<int> callTool(ArrayRef<const char *> Args) const; +}; + +/// Owns LLVM process initialization and an in-process tool registry. +/// +/// A long-lived host constructs one session and uses it for every embedded +/// tool invocation. The individual tools borrow a ToolContext and therefore do +/// not initialize or shut down LLVM themselves. +/// +/// LLVM tools may use process-global state. Tool invocations must be externally +/// serialized; concurrent calls are not supported. +class LLVM_ABI ToolSession { +public: + ToolSession(int &Argc, char **&Argv, ArrayRef<CallableTool> Tools, + bool InstallPipeSignalExitHandler = true, + bool NeedsPOSIXUtilitySignalHandling = false); + ~ToolSession(); + + ToolSession(const ToolSession &) = delete; + ToolSession &operator=(const ToolSession &) = delete; + + /// Invokes the tool named by Args[0]. Args may instead contain a + /// process-style argv beginning with the session executable or an LLVM + /// multicall name. + ErrorOr<int> callTool(ArrayRef<const char *> Args); + +private: + struct Impl; + std::unique_ptr<Impl> PImpl; + + ErrorOr<CallableTool> findTool(StringRef Name) const; + ToolContext makeContext(StringRef RegisteredName, const char *PrependArg); + + friend class ToolContext; +}; + +} // namespace llvm + +#endif diff --git a/llvm/include/llvm/Support/LLVMDriver.h b/llvm/include/llvm/Support/LLVMDriver.h deleted file mode 100644 index 0b2e265d50b42..0000000000000 --- a/llvm/include/llvm/Support/LLVMDriver.h +++ /dev/null @@ -1,27 +0,0 @@ -//===- LLVMDriver.h ---------------------------------------------*- C++ -*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_SUPPORT_LLVMDRIVER_H -#define LLVM_SUPPORT_LLVMDRIVER_H - -namespace llvm { - -struct ToolContext { - const char *Path; - const char *PrependArg; - // PrependArg will be added unconditionally by the llvm-driver, but - // NeedsPrependArg will be false if Path is adequate to reinvoke the tool. - // This is useful if realpath is ever called on Path, in which case it will - // point to the llvm-driver executable, where PrependArg will be needed to - // invoke the correct tool. - bool NeedsPrependArg; -}; - -} // namespace llvm - -#endif diff --git a/llvm/lib/Support/CMakeLists.txt b/llvm/lib/Support/CMakeLists.txt index e7dfcb0dcd891..6e4cd15025206 100644 --- a/llvm/lib/Support/CMakeLists.txt +++ b/llvm/lib/Support/CMakeLists.txt @@ -227,6 +227,7 @@ add_llvm_component_library(LLVMSupport KnownFPClass.cpp LEB128.cpp LineIterator.cpp + Driver.cpp Locale.cpp LockFileManager.cpp ManagedStatic.cpp diff --git a/llvm/lib/Support/Driver.cpp b/llvm/lib/Support/Driver.cpp new file mode 100644 index 0000000000000..6a113f1dd8c3e --- /dev/null +++ b/llvm/lib/Support/Driver.cpp @@ -0,0 +1,134 @@ +//===-- Driver.cpp -------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/Driver.h" + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringExtras.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/Path.h" + +#include <cassert> +#include <string> +#include <system_error> +#include <utility> +#include <vector> + +using namespace llvm; + +namespace { + +bool matchesToolName(StringRef RegisteredName, StringRef InvokedName) { + StringRef Stem = sys::path::stem(InvokedName); + StringRef Filename = sys::path::filename(InvokedName); + auto Matches = [RegisteredName](StringRef Candidate) { + size_t Position = Candidate.rfind_insensitive(RegisteredName); + return Position != StringRef::npos && + (Position + RegisteredName.size() == Candidate.size() || + !llvm::isAlnum(Candidate[Position + RegisteredName.size()])); + }; + return Matches(Stem) || Matches(Filename); +} + +bool isMulticallName(StringRef Name) { return matchesToolName("llvm", Name); } + +} // namespace + +struct ToolSession::Impl { + InitLLVM Initialization; + std::string ExecutablePath; + std::vector<std::pair<std::string, ToolMainFn>> Tools; + + Impl(int &Argc, char **&Argv, ArrayRef<CallableTool> RegisteredTools, + bool InstallPipeSignalExitHandler, bool NeedsPOSIXUtilitySignalHandling) + : Initialization(Argc, Argv, InstallPipeSignalExitHandler, + NeedsPOSIXUtilitySignalHandling), + ExecutablePath(Argv[0]) { + Tools.reserve(RegisteredTools.size()); + for (const CallableTool &Tool : RegisteredTools) + Tools.emplace_back(Tool.Name.str(), Tool.Main); + } +}; + +ToolSession::ToolSession(int &Argc, char **&Argv, ArrayRef<CallableTool> Tools, + bool InstallPipeSignalExitHandler, + bool NeedsPOSIXUtilitySignalHandling) { + assert(Argc > 0 && Argv && Argv[0] && "ToolSession requires a valid argv[0]"); + PImpl = + std::make_unique<Impl>(Argc, Argv, Tools, InstallPipeSignalExitHandler, + NeedsPOSIXUtilitySignalHandling); +} + +ToolSession::~ToolSession() = default; + +ErrorOr<CallableTool> ToolSession::findTool(StringRef Name) const { + StringRef Stem = sys::path::stem(Name); + StringRef Filename = sys::path::filename(Name); + for (const auto &[RegisteredName, Main] : PImpl->Tools) + if (Stem.equals_insensitive(RegisteredName) || + Filename.equals_insensitive(RegisteredName)) + return CallableTool{RegisteredName, Main}; + + for (const auto &[RegisteredName, Main] : PImpl->Tools) + if (matchesToolName(RegisteredName, Name)) + return CallableTool{RegisteredName, Main}; + return make_error_code(std::errc::no_such_file_or_directory); +} + +ToolContext ToolSession::makeContext(StringRef RegisteredName, + const char *PrependArg) { + ErrorOr<CallableTool> ExecutableTool = findTool(PImpl->ExecutablePath); + bool NeedsPrependArg = + !ExecutableTool || + !ExecutableTool->Name.equals_insensitive(RegisteredName); + ToolContext Context(PImpl->ExecutablePath.c_str(), PrependArg, + NeedsPrependArg); + Context.Session = this; + return Context; +} + +ErrorOr<int> ToolSession::callTool(ArrayRef<const char *> Args) { + if (Args.empty()) + return make_error_code(std::errc::invalid_argument); + + StringRef InvokedName = Args.front(); + ErrorOr<CallableTool> Tool = findTool(InvokedName); + if (!Tool) { + if (InvokedName != PImpl->ExecutablePath && !isMulticallName(InvokedName)) + return make_error_code(std::errc::no_such_file_or_directory); + Args = Args.drop_front(); + if (Args.empty()) + return make_error_code(std::errc::invalid_argument); + InvokedName = Args.front(); + Tool = findTool(InvokedName); + } + + if (!Tool) + return Tool.getError(); + + std::string PrependArg = sys::path::stem(InvokedName).str(); + ToolContext Context = makeContext(Tool->Name, PrependArg.c_str()); + SmallVector<char *, 16> MutableArgs; + MutableArgs.reserve(Args.size() + 1); + for (const char *Arg : Args) + MutableArgs.push_back(const_cast<char *>(Arg)); + MutableArgs.push_back(nullptr); + return Tool->Main(Args.size(), MutableArgs.data(), Context); +} + +ErrorOr<CallableTool> ToolContext::getCallableTool(StringRef Name) const { + if (!Session) + return make_error_code(std::errc::operation_not_permitted); + return Session->findTool(Name); +} + +ErrorOr<int> ToolContext::callTool(ArrayRef<const char *> Args) const { + if (!Session) + return make_error_code(std::errc::operation_not_permitted); + return Session->callTool(Args); +} diff --git a/llvm/test/tools/llvm-driver/session-dispatch.test b/llvm/test/tools/llvm-driver/session-dispatch.test new file mode 100644 index 0000000000000..a8e627f5a1305 --- /dev/null +++ b/llvm/test/tools/llvm-driver/session-dispatch.test @@ -0,0 +1,9 @@ +# REQUIRES: llvm-driver + +## Exercise a real LLVM tool entry point through ToolSession rather than +## only testing the registry with synthetic callbacks. +# RUN: %llvm cxxfilt _Z3foov | FileCheck %s --check-prefix=CXXFILT +# RUN: %llvm --help | FileCheck %s --check-prefix=HELP + +# CXXFILT: foo() +# HELP: OVERVIEW: llvm toolchain driver diff --git a/llvm/tools/dsymutil/dsymutil.cpp b/llvm/tools/dsymutil/dsymutil.cpp index 76c4d4140488c..244eb0cc9eb39 100644 --- a/llvm/tools/dsymutil/dsymutil.cpp +++ b/llvm/tools/dsymutil/dsymutil.cpp @@ -35,10 +35,10 @@ #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/CrashRecoveryContext.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileCollector.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormatVariadic.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Program.h" diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp index 38f929f63b39f..6ab53375509c8 100644 --- a/llvm/tools/llvm-ar/llvm-ar.cpp +++ b/llvm/tools/llvm-ar/llvm-ar.cpp @@ -22,11 +22,11 @@ #include "llvm/Support/Chrono.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/ConvertUTF.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/FormatVariadic.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" diff --git a/llvm/tools/llvm-cgdata/llvm-cgdata.cpp b/llvm/tools/llvm-cgdata/llvm-cgdata.cpp index 66a7410782922..1212cab0b5e13 100644 --- a/llvm/tools/llvm-cgdata/llvm-cgdata.cpp +++ b/llvm/tools/llvm-cgdata/llvm-cgdata.cpp @@ -21,7 +21,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Path.h" #include "llvm/Support/VirtualFileSystem.h" #include "llvm/Support/WithColor.h" diff --git a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp index 1467093e78c0b..d4b8c23bd02be 100644 --- a/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp +++ b/llvm/tools/llvm-cxxfilt/llvm-cxxfilt.cpp @@ -13,7 +13,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TargetParser/Host.h" diff --git a/llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp b/llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp index 9aeda9f55bb96..9f515d444565a 100644 --- a/llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp +++ b/llvm/tools/llvm-debuginfod-find/llvm-debuginfod-find.cpp @@ -23,8 +23,8 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/InitLLVM.h" -#include "llvm/Support/LLVMDriver.h" using namespace llvm; diff --git a/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp b/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp index c44bf915361f1..e24fa29718887 100644 --- a/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp +++ b/llvm/tools/llvm-debuginfod/llvm-debuginfod.cpp @@ -22,7 +22,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/ThreadPool.h" using namespace llvm; diff --git a/llvm/tools/llvm-driver/llvm-driver.cpp b/llvm/tools/llvm-driver/llvm-driver.cpp index 14ce162faee46..9cbe750fc563b 100644 --- a/llvm/tools/llvm-driver/llvm-driver.cpp +++ b/llvm/tools/llvm-driver/llvm-driver.cpp @@ -6,14 +6,11 @@ // //===----------------------------------------------------------------------===// -#include "llvm/ADT/StringExtras.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/Support/CommandLine.h" -#include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/InitLLVM.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Path.h" -#include "llvm/Support/WithColor.h" +#include "llvm/Support/raw_ostream.h" using namespace llvm; @@ -36,51 +33,26 @@ static void printHelpMessage() { << "OPTIONS:\n\n --help - Display this message\n"; } -static int findTool(int Argc, char **Argv, const char *Argv0) { - if (!Argc) { - printHelpMessage(); - return 1; - } +int main(int Argc, char **Argv) { + const CallableTool Tools[] = { +#define LLVM_DRIVER_TOOL(tool, entry) {tool, entry##_main}, +#include "LLVMDriverTools.def" + }; - StringRef ToolName = Argv[0]; + ToolSession Session(Argc, Argv, Tools); - if (ToolName == "--help") { + StringRef Stem = sys::path::stem(Argv[0]); + if (Stem.equals_insensitive("llvm") && + (Argc == 1 || (Argc == 2 && StringRef(Argv[1]) == "--help"))) { printHelpMessage(); - return 0; + return Argc == 1 ? 1 : 0; } - StringRef Stem = sys::path::stem(ToolName); - auto Is = [=](StringRef Tool) { - auto IsImpl = [=](StringRef Stem) { - auto I = Stem.rfind_insensitive(Tool); - return I != StringRef::npos && (I + Tool.size() == Stem.size() || - !llvm::isAlnum(Stem[I + Tool.size()])); - }; - for (StringRef S : {Stem, sys::path::filename(ToolName)}) - if (IsImpl(S)) - return true; - return false; - }; - - auto MakeDriverArgs = [=]() -> llvm::ToolContext { - if (ToolName != Argv0) - return {Argv0, ToolName.data(), true}; - return {Argv0, sys::path::filename(Argv0).data(), false}; - }; - -#define LLVM_DRIVER_TOOL(tool, entry) \ - if (Is(tool)) \ - return entry##_main(Argc, Argv, MakeDriverArgs()); -#include "LLVMDriverTools.def" - - if (Is("llvm") || Argv0 == Argv[0]) - return findTool(Argc - 1, Argv + 1, Argv0); - - printHelpMessage(); - return 1; -} - -int main(int Argc, char **Argv) { - llvm::InitLLVM X(Argc, Argv); - return findTool(Argc, Argv, Argv[0]); + SmallVector<const char *, 16> Args(Argv, Argv + Argc); + ErrorOr<int> Result = Session.callTool(Args); + if (!Result) { + printHelpMessage(); + return 1; + } + return *Result; } diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp index b8c3f6e3d754a..afe7997cab883 100644 --- a/llvm/tools/llvm-dwp/llvm-dwp.cpp +++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp @@ -16,8 +16,8 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/ToolOutputFile.h" #include <optional> diff --git a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp index ab1eb70e780e9..7aff2cd0481a1 100644 --- a/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp +++ b/llvm/tools/llvm-gsymutil/llvm-gsymutil.cpp @@ -17,11 +17,11 @@ #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" #include "llvm/Support/JSON.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" diff --git a/llvm/tools/llvm-ifs/llvm-ifs.cpp b/llvm/tools/llvm-ifs/llvm-ifs.cpp index 3e9014b7f52f9..e80a507b9f934 100644 --- a/llvm/tools/llvm-ifs/llvm-ifs.cpp +++ b/llvm/tools/llvm-ifs/llvm-ifs.cpp @@ -19,10 +19,10 @@ #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileOutputBuffer.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/VersionTuple.h" diff --git a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp index 40065270fb7ef..25a5d021cfc3c 100644 --- a/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp +++ b/llvm/tools/llvm-libtool-darwin/llvm-libtool-darwin.cpp @@ -23,7 +23,7 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/LLVMDriver.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/VirtualFileSystem.h" diff --git a/llvm/tools/llvm-lipo/llvm-lipo.cpp b/llvm/tools/llvm-lipo/llvm-lipo.cpp index 66ac0eecb8396..0296067f2cf49 100644 --- a/llvm/tools/llvm-lipo/llvm-lipo.cpp +++ b/llvm/tools/llvm-lipo/llvm-lipo.cpp @@ -24,9 +24,9 @@ #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileOutputBuffer.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/WithColor.h" #include "llvm/TargetParser/Triple.h" diff --git a/llvm/tools/llvm-ml/llvm-ml.cpp b/llvm/tools/llvm-ml/llvm-ml.cpp index 21fdb010bab83..b3e53f7eb316c 100644 --- a/llvm/tools/llvm-ml/llvm-ml.cpp +++ b/llvm/tools/llvm-ml/llvm-ml.cpp @@ -31,10 +31,10 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/Compression.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileUtilities.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/FormattedStream.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" diff --git a/llvm/tools/llvm-mt/llvm-mt.cpp b/llvm/tools/llvm-mt/llvm-mt.cpp index 3bd1bc786f86d..4c40a6564e0db 100644 --- a/llvm/tools/llvm-mt/llvm-mt.cpp +++ b/llvm/tools/llvm-mt/llvm-mt.cpp @@ -15,9 +15,9 @@ #include "llvm/Option/Arg.h" #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileOutputBuffer.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index 170bf0b7fc22e..a425e8c5ad4a7 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -42,9 +42,9 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Program.h" #include "llvm/Support/Signals.h" diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp index 482249f67d38c..1b98b4bc1a772 100644 --- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp @@ -27,11 +27,11 @@ #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileUtilities.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Memory.h" #include "llvm/Support/Path.h" #include "llvm/Support/Process.h" diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp index 92c1c2da3d025..2d670fd338309 100644 --- a/llvm/tools/llvm-objdump/llvm-objdump.cpp +++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp @@ -64,10 +64,10 @@ #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/StringSaver.h" diff --git a/llvm/tools/llvm-rc/llvm-rc.cpp b/llvm/tools/llvm-rc/llvm-rc.cpp index 7afecf667fed0..6f10fed300f96 100644 --- a/llvm/tools/llvm-rc/llvm-rc.cpp +++ b/llvm/tools/llvm-rc/llvm-rc.cpp @@ -23,10 +23,10 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/OptTable.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FileUtilities.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/PrettyStackTrace.h" diff --git a/llvm/tools/llvm-readobj/llvm-readobj.cpp b/llvm/tools/llvm-readobj/llvm-readobj.cpp index d06221fbfd5b8..57ad26f0f2a70 100644 --- a/llvm/tools/llvm-readobj/llvm-readobj.cpp +++ b/llvm/tools/llvm-readobj/llvm-readobj.cpp @@ -39,10 +39,10 @@ #include "llvm/Support/CommandLine.h" #include "llvm/Support/DataTypes.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/FormatVariadic.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Path.h" #include "llvm/Support/ScopedPrinter.h" #include "llvm/Support/WithColor.h" diff --git a/llvm/tools/llvm-readtapi/llvm-readtapi.cpp b/llvm/tools/llvm-readtapi/llvm-readtapi.cpp index 312f298a86256..b541e0d7fc214 100644 --- a/llvm/tools/llvm-readtapi/llvm-readtapi.cpp +++ b/llvm/tools/llvm-readtapi/llvm-readtapi.cpp @@ -15,9 +15,9 @@ #include "llvm/Option/ArgList.h" #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" diff --git a/llvm/tools/llvm-size/llvm-size.cpp b/llvm/tools/llvm-size/llvm-size.cpp index 77058368bf4c3..1192dcf101bd4 100644 --- a/llvm/tools/llvm-size/llvm-size.cpp +++ b/llvm/tools/llvm-size/llvm-size.cpp @@ -23,9 +23,9 @@ #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Format.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/WithColor.h" #include "llvm/Support/raw_ostream.h" diff --git a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp index 0496df84fc8eb..b49514823e189 100644 --- a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp +++ b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -32,9 +32,9 @@ #include "llvm/Support/COM.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/Path.h" #include "llvm/Support/StringSaver.h" #include "llvm/Support/WithColor.h" diff --git a/llvm/tools/sancov/sancov.cpp b/llvm/tools/sancov/sancov.cpp index d6fdac2583d5d..450163c5723b3 100644 --- a/llvm/tools/sancov/sancov.cpp +++ b/llvm/tools/sancov/sancov.cpp @@ -34,11 +34,11 @@ #include "llvm/Option/Option.h" #include "llvm/Support/Casting.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" #include "llvm/Support/Errc.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/JSON.h" -#include "llvm/Support/LLVMDriver.h" #include "llvm/Support/MD5.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" diff --git a/llvm/unittests/Support/CMakeLists.txt b/llvm/unittests/Support/CMakeLists.txt index cc88a6c5670ca..f9732ff4e0e4b 100644 --- a/llvm/unittests/Support/CMakeLists.txt +++ b/llvm/unittests/Support/CMakeLists.txt @@ -162,3 +162,4 @@ if(NOT LLVM_INTEGRATED_CRT_ALLOC) endif() add_subdirectory(CommandLineInit) +add_subdirectory(LLVMToolSession) diff --git a/llvm/unittests/Support/LLVMToolSession/CMakeLists.txt b/llvm/unittests/Support/LLVMToolSession/CMakeLists.txt new file mode 100644 index 0000000000000..2c61f3c535615 --- /dev/null +++ b/llvm/unittests/Support/LLVMToolSession/CMakeLists.txt @@ -0,0 +1,20 @@ +set(test_name LLVMToolSessionTests) +set(test_suite UnitTests) + +# This test supplies its own main() so a single ToolSession can own +# InitLLVM for the complete test process. +if (NOT LLVM_BUILD_TESTS) + set(EXCLUDE_FROM_ALL ON) +endif() + +list(APPEND LLVM_LINK_COMPONENTS Support) + +add_llvm_executable(${test_name} + IGNORE_EXTERNALIZE_DEBUGINFO NO_INSTALL_RPATH + LLVMToolSessionTest.cpp) + +target_link_libraries(${test_name} PRIVATE llvm_gtest) +add_dependencies(${test_suite} ${test_name}) + +set(outdir ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}) +set_output_directory(${test_name} BINARY_DIR ${outdir} LIBRARY_DIR ${outdir}) diff --git a/llvm/unittests/Support/LLVMToolSession/LLVMToolSessionTest.cpp b/llvm/unittests/Support/LLVMToolSession/LLVMToolSessionTest.cpp new file mode 100644 index 0000000000000..d1ef91444c2f7 --- /dev/null +++ b/llvm/unittests/Support/LLVMToolSession/LLVMToolSessionTest.cpp @@ -0,0 +1,162 @@ +//===- LLVMToolSessionTest.cpp -------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Driver.h" +#include "llvm/Support/Path.h" +#include "gtest/gtest.h" + +#include <memory> +#include <string> + +using namespace llvm; + +namespace { + +std::unique_ptr<ToolSession> Session; +std::string ExecutablePath; +unsigned CompilerCalls; +unsigned LinkerCalls; +unsigned WrapperCalls; + +int linkerMain(int Argc, char **Argv, const ToolContext &Context) { + ++LinkerCalls; + EXPECT_EQ(Argc, 3); + EXPECT_STREQ(Argv[0], "wasm-ld"); + EXPECT_TRUE(Context.getCallableTool("clang")); + return 0; +} + +int compilerMain(int Argc, char **Argv, const ToolContext &Context) { + ++CompilerCalls; + EXPECT_EQ(Argc, 2); + EXPECT_STREQ(Argv[0], "clang"); + const char *LinkArgs[] = {"wasm-ld", Argv[1], "out.wasm"}; + ErrorOr<int> Result = Context.callTool(LinkArgs); + EXPECT_TRUE(Result); + return Result ? *Result : 1; +} + +int clangWrapperMain(int Argc, char **Argv, const ToolContext &Context) { + ++WrapperCalls; + EXPECT_EQ(Argc, 1); + EXPECT_STREQ(Argv[0], "/tmp/clang-wrapper.exe"); + EXPECT_STREQ(Context.PrependArg, "clang-wrapper"); + return 0; +} + +int resetOptionsMain(int, char **, const ToolContext &) { + cl::ResetAllOptionOccurrences(); + return 0; +} + +int directMain(int Argc, char **Argv, const ToolContext &Context) { + EXPECT_EQ(Argc, 1); + EXPECT_STREQ(Argv[0], ExecutablePath.c_str()); + EXPECT_STREQ(Context.Path, ExecutablePath.c_str()); + EXPECT_FALSE(Context.NeedsPrependArg); + return 0; +} + +int fuzzyMain(int Argc, char **Argv, const ToolContext &Context) { + EXPECT_EQ(Argc, 1); + EXPECT_STREQ(Argv[0], "Tests"); + EXPECT_TRUE(Context.NeedsPrependArg); + return 0; +} + +TEST(LLVMToolSessionTest, SupportsSequentialNestedToolCalls) { + unsigned CompilerCallsBefore = CompilerCalls; + unsigned LinkerCallsBefore = LinkerCalls; + const char *First[] = {"clang", "first.cpp"}; + const char *Second[] = {"clang", "second.cpp"}; + + ErrorOr<int> FirstResult = Session->callTool(First); + ASSERT_TRUE(FirstResult); + EXPECT_EQ(*FirstResult, 0); + ErrorOr<int> SecondResult = Session->callTool(Second); + ASSERT_TRUE(SecondResult); + EXPECT_EQ(*SecondResult, 0); + EXPECT_EQ(CompilerCalls, CompilerCallsBefore + 2); + EXPECT_EQ(LinkerCalls, LinkerCallsBefore + 2); +} + +TEST(LLVMToolSessionTest, ReportsUnknownTools) { + const char *Args[] = {"not-a-tool"}; + ErrorOr<int> Result = Session->callTool(Args); + EXPECT_FALSE(Result); + EXPECT_EQ(Result.getError(), + make_error_code(std::errc::no_such_file_or_directory)); +} + +TEST(LLVMToolSessionTest, SupportsStatefulCallableTools) { + const char *Args[] = {"stateful-tool"}; + ErrorOr<int> Result = Session->callTool(Args); + ASSERT_TRUE(Result); + EXPECT_EQ(*Result, 42); +} + +TEST(LLVMToolSessionTest, PrefersExactToolNameBeforeFuzzyMatch) { + const char *Args[] = {"/tmp/clang-wrapper.exe"}; + ErrorOr<int> Result = Session->callTool(Args); + ASSERT_TRUE(Result); + EXPECT_EQ(*Result, 0); + EXPECT_EQ(WrapperCalls, 1u); +} + +TEST(LLVMToolSessionTest, SurvivesCommandLineOptionReset) { + const char *ResetArgs[] = {"reset-options"}; + ErrorOr<int> ResetResult = Session->callTool(ResetArgs); + ASSERT_TRUE(ResetResult); + EXPECT_EQ(*ResetResult, 0); + + unsigned CompilerCallsBefore = CompilerCalls; + const char *CompilerArgs[] = {"clang", "after-reset.cpp"}; + ErrorOr<int> CompilerResult = Session->callTool(CompilerArgs); + ASSERT_TRUE(CompilerResult); + EXPECT_EQ(*CompilerResult, 0); + EXPECT_EQ(CompilerCalls, CompilerCallsBefore + 1); +} + +TEST(LLVMToolSessionTest, DirectInvocationDoesNotNeedPrependArg) { + const char *Args[] = {ExecutablePath.c_str()}; + ErrorOr<int> Result = Session->callTool(Args); + ASSERT_TRUE(Result); + EXPECT_EQ(*Result, 0); +} + +TEST(LLVMToolSessionTest, DirectInvocationPrefersExactToolName) { + const char *Args[] = {"Tests"}; + ErrorOr<int> Result = Session->callTool(Args); + ASSERT_TRUE(Result); + EXPECT_EQ(*Result, 0); +} + +} // namespace + +int main(int Argc, char **Argv) { + int StatefulResult = 42; + ExecutablePath = Argv[0]; + const CallableTool Tools[] = { + {sys::path::stem(Argv[0]), directMain}, + {"clang", compilerMain}, + {"clang-wrapper", clangWrapperMain}, + {"reset-options", resetOptionsMain}, + {"Tests", fuzzyMain}, + {"stateful-tool", + [&StatefulResult](int, char **, const ToolContext &) { + return StatefulResult; + }}, + {"wasm-ld", linkerMain}, + }; + Session = std::make_unique<ToolSession>(Argc, Argv, Tools); + testing::InitGoogleTest(&Argc, Argv); + int Result = RUN_ALL_TESTS(); + Session.reset(); + return Result; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
