LLVM 24.0.0git
Driver.h
Go to the documentation of this file.
1//===- Driver.h -------------------------------------------------*- 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#ifndef LLVM_SUPPORT_DRIVER_H
10#define LLVM_SUPPORT_DRIVER_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
16
17#include <functional>
18#include <memory>
19
20namespace llvm {
21
22class ToolSession;
23class ToolContext;
24
25using ToolMainFn = std::function<int(int, char **, const ToolContext &)>;
26
27/// An LLVM command-line tool that can be invoked without creating a process.
31
32 explicit operator bool() const { return static_cast<bool>(Main); }
33};
34
35/// Describes how a tool was invoked and provides access to its host session.
37 ToolSession *Session = nullptr;
38
39 friend class ToolSession;
40
41public:
42 const char *Path;
43 const char *PrependArg;
44 // PrependArg will be added unconditionally by the llvm-driver, but
45 // NeedsPrependArg will be false if Path is adequate to reinvoke the tool.
46 // This is useful if realpath is ever called on Path, in which case it will
47 // point to the llvm-driver executable, where PrependArg will be needed to
48 // invoke the correct tool.
50
53
54 /// Finds a tool registered with the session that owns this context.
56
57 /// Invokes another tool registered with the same host session.
59};
60
61/// Owns LLVM process initialization and an in-process tool registry.
62///
63/// A long-lived host constructs one session and uses it for every embedded
64/// tool invocation. The individual tools borrow a ToolContext and therefore do
65/// not initialize or shut down LLVM themselves.
66///
67/// LLVM tools may use process-global state. Tool invocations must be externally
68/// serialized; concurrent calls are not supported.
70public:
71 ToolSession(int &Argc, char **&Argv, ArrayRef<CallableTool> Tools,
72 bool InstallPipeSignalExitHandler = true,
73 bool NeedsPOSIXUtilitySignalHandling = false);
75
76 ToolSession(const ToolSession &) = delete;
77 ToolSession &operator=(const ToolSession &) = delete;
78
79 /// Invokes the tool named by Args[0]. Args may instead contain a
80 /// process-style argv beginning with the session executable or an LLVM
81 /// multicall name.
83
84private:
85 struct Impl;
86 std::unique_ptr<Impl> PImpl;
87
88 ErrorOr<CallableTool> findTool(StringRef Name) const;
89 ToolContext makeContext(StringRef RegisteredName, const char *PrependArg);
90
91 friend class ToolContext;
92};
93
94} // namespace llvm
95
96#endif
#define LLVM_ABI
Definition Compiler.h:215
Provides ErrorOr<T> smart pointer.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Represents either an error or a value T.
Definition ErrorOr.h:56
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Describes how a tool was invoked and provides access to its host session.
Definition Driver.h:36
LLVM_ABI ErrorOr< int > callTool(ArrayRef< const char * > Args) const
Invokes another tool registered with the same host session.
Definition Driver.cpp:130
ToolContext(const char *Path, const char *PrependArg, bool NeedsPrependArg)
Definition Driver.h:51
LLVM_ABI ErrorOr< CallableTool > getCallableTool(StringRef Name) const
Finds a tool registered with the session that owns this context.
Definition Driver.cpp:124
bool NeedsPrependArg
Definition Driver.h:49
friend class ToolSession
Definition Driver.h:39
const char * PrependArg
Definition Driver.h:43
const char * Path
Definition Driver.h:42
Owns LLVM process initialization and an in-process tool registry.
Definition Driver.h:69
ToolSession(int &Argc, char **&Argv, ArrayRef< CallableTool > Tools, bool InstallPipeSignalExitHandler=true, bool NeedsPOSIXUtilitySignalHandling=false)
Definition Driver.cpp:58
ErrorOr< int > callTool(ArrayRef< const char * > Args)
Invokes the tool named by Args[0].
Definition Driver.cpp:95
ToolSession(const ToolSession &)=delete
friend class ToolContext
Definition Driver.h:91
ToolSession & operator=(const ToolSession &)=delete
This is an optimization pass for GlobalISel generic memory operations.
std::function< int(int, char **, const ToolContext &)> ToolMainFn
Definition Driver.h:25
An LLVM command-line tool that can be invoked without creating a process.
Definition Driver.h:28
ToolMainFn Main
Definition Driver.h:30
StringRef Name
Definition Driver.h:29