clang 22.0.0git
Compilation.h
Go to the documentation of this file.
1//===- Compilation.h - Compilation Task Data Structure ----------*- 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_CLANG_DRIVER_COMPILATION_H
10#define LLVM_CLANG_DRIVER_COMPILATION_H
11
12#include "clang/Basic/LLVM.h"
13#include "clang/Driver/Action.h"
14#include "clang/Driver/Job.h"
15#include "clang/Driver/Util.h"
16#include "llvm/ADT/ArrayRef.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Option/Option.h"
20#include <cassert>
21#include <iterator>
22#include <map>
23#include <memory>
24#include <optional>
25#include <utility>
26#include <vector>
27
28namespace llvm {
29namespace opt {
30
31class DerivedArgList;
32class InputArgList;
33
34} // namespace opt
35} // namespace llvm
36
37namespace clang {
38namespace driver {
39
40class Driver;
41class ToolChain;
42
43/// Compilation - A set of tasks to perform for a single driver
44/// invocation.
46 /// The driver we were created by.
47 const Driver &TheDriver;
48
49 /// The default tool chain.
50 const ToolChain &DefaultToolChain;
51
52 /// A mask of all the programming models the host has to support in the
53 /// current compilation.
54 unsigned ActiveOffloadMask = 0;
55
56 /// Array with the toolchains of offloading host and devices in the order they
57 /// were requested by the user. We are preserving that order in case the code
58 /// generation needs to derive a programming-model-specific semantic out of
59 /// it.
60 std::multimap<Action::OffloadKind, const ToolChain *>
61 OrderedOffloadingToolchains;
62
63 /// The original (untranslated) input argument list.
64 llvm::opt::InputArgList *Args;
65
66 /// The driver translated arguments. Note that toolchains may perform their
67 /// own argument translation.
68 llvm::opt::DerivedArgList *TranslatedArgs;
69
70 /// The list of actions we've created via MakeAction. This is not accessible
71 /// to consumers; it's here just to manage ownership.
72 std::vector<std::unique_ptr<Action>> AllActions;
73
74 /// The list of actions. This is maintained and modified by consumers, via
75 /// getActions().
76 ActionList Actions;
77
78 /// The root list of jobs.
79 JobList Jobs;
80
81 /// Cache of translated arguments for a particular tool chain, bound
82 /// architecture, and device offload kind.
83 struct TCArgsKey final {
84 const ToolChain *TC = nullptr;
85 StringRef BoundArch;
86 Action::OffloadKind DeviceOffloadKind = Action::OFK_None;
87
88 TCArgsKey(const ToolChain *TC, StringRef BoundArch,
89 Action::OffloadKind DeviceOffloadKind)
90 : TC(TC), BoundArch(BoundArch), DeviceOffloadKind(DeviceOffloadKind) {}
91
92 bool operator<(const TCArgsKey &K) const {
93 return std::tie(TC, BoundArch, DeviceOffloadKind) <
94 std::tie(K.TC, K.BoundArch, K.DeviceOffloadKind);
95 }
96 };
97 std::map<TCArgsKey, llvm::opt::DerivedArgList *> TCArgs;
98
99 /// Temporary files which should be removed on exit.
100 llvm::opt::ArgStringList TempFiles;
101
102 /// Result files which should be removed on failure.
103 ArgStringMap ResultFiles;
104
105 /// Result files which are generated correctly on failure, and which should
106 /// only be removed if we crash.
107 ArgStringMap FailureResultFiles;
108
109 /// -ftime-trace result files.
110 ArgStringMap TimeTraceFiles;
111
112 /// Optional redirection for stdin, stdout, stderr.
113 std::vector<std::optional<StringRef>> Redirects;
114
115 /// Callback called after compilation job has been finished.
116 /// Arguments of the callback are the compilation job as an instance of
117 /// class Command and the exit status of the corresponding child process.
118 std::function<void(const Command &, int)> PostCallback;
119
120 /// Whether we're compiling for diagnostic purposes.
121 bool ForDiagnostics = false;
122
123 /// Whether an error during the parsing of the input args.
124 bool ContainsError;
125
126 /// Whether to keep temporary files regardless of -save-temps.
127 bool ForceKeepTempFiles = false;
128
129public:
130 Compilation(const Driver &D, const ToolChain &DefaultToolChain,
131 llvm::opt::InputArgList *Args,
132 llvm::opt::DerivedArgList *TranslatedArgs, bool ContainsError);
133 ~Compilation();
134
135 const Driver &getDriver() const { return TheDriver; }
136
137 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
138
140 return ActiveOffloadMask & Kind;
141 }
142
143 unsigned getActiveOffloadKinds() const { return ActiveOffloadMask; }
144
145 /// Iterator that visits device toolchains of a given kind.
147 const std::multimap<Action::OffloadKind,
148 const ToolChain *>::const_iterator;
152
153 template <Action::OffloadKind Kind>
155 return OrderedOffloadingToolchains.equal_range(Kind);
156 }
157
160 return OrderedOffloadingToolchains.equal_range(Kind);
161 }
162
163 /// Return true if an offloading tool chain of a given kind exists.
164 template <Action::OffloadKind Kind> bool hasOffloadToolChain() const {
165 return OrderedOffloadingToolchains.find(Kind) !=
166 OrderedOffloadingToolchains.end();
167 }
168
169 /// Return an offload toolchain of the provided kind. Only one is expected to
170 /// exist.
171 template <Action::OffloadKind Kind>
173 auto TCs = getOffloadToolChains<Kind>();
174
175 assert(TCs.first != TCs.second &&
176 "No tool chains of the selected kind exist!");
177 assert(std::next(TCs.first) == TCs.second &&
178 "More than one tool chain of the this kind exist.");
179 return TCs.first->second;
180 }
181
182 void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain,
183 Action::OffloadKind OffloadKind) {
184 assert(OffloadKind != Action::OFK_Host && OffloadKind != Action::OFK_None &&
185 "This is not a device tool chain!");
186
187 // Update the host offload kind to also contain this kind.
188 ActiveOffloadMask |= OffloadKind;
189 OrderedOffloadingToolchains.insert(
190 std::make_pair(OffloadKind, DeviceToolChain));
191 }
192
193 const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
194
195 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
196
197 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
198
199 ActionList &getActions() { return Actions; }
200 const ActionList &getActions() const { return Actions; }
201
202 /// Creates a new Action owned by this Compilation.
203 ///
204 /// The new Action is *not* added to the list returned by getActions().
205 template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
206 T *RawPtr = new T(std::forward<Args>(Arg)...);
207 AllActions.push_back(std::unique_ptr<Action>(RawPtr));
208 return RawPtr;
209 }
210
211 JobList &getJobs() { return Jobs; }
212 const JobList &getJobs() const { return Jobs; }
213
214 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
215
216 llvm::opt::ArgStringList &getTempFiles() { return TempFiles; }
217 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
218
219 const ArgStringMap &getResultFiles() const { return ResultFiles; }
220
222 return FailureResultFiles;
223 }
224
225 /// Installs a handler that is executed when a compilation job is finished.
226 /// The arguments of the callback specify the compilation job as an instance
227 /// of class Command and the exit status of the child process executed that
228 /// job.
229 void setPostCallback(const std::function<void(const Command &, int)> &CB) {
230 PostCallback = CB;
231 }
232
233 /// Returns the sysroot path.
234 StringRef getSysRoot() const;
235
236 /// getArgsForToolChain - Return the derived argument list for the
237 /// tool chain \p TC (or the default tool chain, if TC is not specified).
238 /// If a device offloading kind is specified, a translation specific for that
239 /// kind is performed, if any.
240 ///
241 /// \param BoundArch - The bound architecture name, or 0.
242 /// \param DeviceOffloadKind - The offload device kind that should be used in
243 /// the translation, if any.
244 const llvm::opt::DerivedArgList &
245 getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
246 Action::OffloadKind DeviceOffloadKind);
247
248 /// addTempFile - Add a file to remove on exit, and returns its
249 /// argument.
250 const char *addTempFile(const char *Name) {
251 TempFiles.push_back(Name);
252 return Name;
253 }
254
255 /// addResultFile - Add a file to remove on failure, and returns its
256 /// argument.
257 const char *addResultFile(const char *Name, const JobAction *JA) {
258 ResultFiles[JA] = Name;
259 return Name;
260 }
261
262 /// addFailureResultFile - Add a file to remove if we crash, and returns its
263 /// argument.
264 const char *addFailureResultFile(const char *Name, const JobAction *JA) {
265 FailureResultFiles[JA] = Name;
266 return Name;
267 }
268
269 const char *getTimeTraceFile(const JobAction *JA) const {
270 return TimeTraceFiles.lookup(JA);
271 }
272 void addTimeTraceFile(const char *Name, const JobAction *JA) {
273 assert(!TimeTraceFiles.contains(JA));
274 TimeTraceFiles[JA] = Name;
275 }
276
277 /// CleanupFile - Delete a given file.
278 ///
279 /// \param IssueErrors - Report failures as errors.
280 /// \return Whether the file was removed successfully.
281 bool CleanupFile(const char *File, bool IssueErrors = false) const;
282
283 /// CleanupFileList - Remove the files in the given list.
284 ///
285 /// \param IssueErrors - Report failures as errors.
286 /// \return Whether all files were removed successfully.
287 bool CleanupFileList(const llvm::opt::ArgStringList &Files,
288 bool IssueErrors = false) const;
289
290 /// CleanupFileMap - Remove the files in the given map.
291 ///
292 /// \param JA - If specified, only delete the files associated with this
293 /// JobAction. Otherwise, delete all files in the map.
294 /// \param IssueErrors - Report failures as errors.
295 /// \return Whether all files were removed successfully.
296 bool CleanupFileMap(const ArgStringMap &Files,
297 const JobAction *JA,
298 bool IssueErrors = false) const;
299
300 /// ExecuteCommand - Execute an actual command.
301 ///
302 /// \param FailingCommand - For non-zero results, this will be set to the
303 /// Command which failed, if any.
304 /// \param LogOnly - When true, only tries to log the command, not actually
305 /// execute it.
306 /// \return The result code of the subprocess.
307 int ExecuteCommand(const Command &C, const Command *&FailingCommand,
308 bool LogOnly = false) const;
309
310 /// ExecuteJob - Execute a single job.
311 ///
312 /// \param FailingCommands - For non-zero results, this will be a vector of
313 /// failing commands and their associated result code.
314 /// \param LogOnly - When true, only tries to log the command, not actually
315 /// execute it.
316 void
317 ExecuteJobs(const JobList &Jobs,
318 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands,
319 bool LogOnly = false) const;
320
321 /// initCompilationForDiagnostics - Remove stale state and suppress output
322 /// so compilation can be reexecuted to generate additional diagnostic
323 /// information (e.g., preprocessed source(s)).
325
326 /// Return true if we're compiling for diagnostics.
327 bool isForDiagnostics() const { return ForDiagnostics; }
328
329 /// Return whether an error during the parsing of the input args.
330 bool containsError() const { return ContainsError; }
331
332 /// Force driver to fail before toolchain is created. This is necessary when
333 /// error happens in action builder.
334 void setContainsError() { ContainsError = true; }
335
336 /// Redirect - Redirect output of this compilation. Can only be done once.
337 ///
338 /// \param Redirects - array of optional paths. The array should have a size
339 /// of three. The inferior process's stdin(0), stdout(1), and stderr(2) will
340 /// be redirected to the corresponding paths, if provided (not std::nullopt).
341 void Redirect(ArrayRef<std::optional<StringRef>> Redirects);
342};
343
344} // namespace driver
345} // namespace clang
346
347#endif // LLVM_CLANG_DRIVER_COMPILATION_H
const Decl * D
enum clang::sema::@1840::IndirectLocalPathEntry::EntryKind Kind
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Command - An executable path/name and argument vector to execute.
Definition: Job.h:106
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:45
bool hasOffloadToolChain() const
Return true if an offloading tool chain of a given kind exists.
Definition: Compilation.h:164
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind)
getArgsForToolChain - Return the derived argument list for the tool chain TC (or the default tool cha...
Definition: Compilation.cpp:58
const JobList & getJobs() const
Definition: Compilation.h:212
int ExecuteCommand(const Command &C, const Command *&FailingCommand, bool LogOnly=false) const
ExecuteCommand - Execute an actual command.
llvm::opt::ArgStringList & getTempFiles()
Definition: Compilation.h:216
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
const ActionList & getActions() const
Definition: Compilation.h:200
void setPostCallback(const std::function< void(const Command &, int)> &CB)
Installs a handler that is executed when a compilation job is finished.
Definition: Compilation.h:229
ActionList & getActions()
Definition: Compilation.h:199
const_offload_toolchains_range getOffloadToolChains(Action::OffloadKind Kind) const
Definition: Compilation.h:159
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
const ArgStringMap & getFailureResultFiles() const
Definition: Compilation.h:221
llvm::opt::DerivedArgList & getArgs()
Definition: Compilation.h:197
const char * getTimeTraceFile(const JobAction *JA) const
Definition: Compilation.h:269
unsigned isOffloadingHostKind(Action::OffloadKind Kind) const
Definition: Compilation.h:139
T * MakeAction(Args &&... Arg)
Creates a new Action owned by this Compilation.
Definition: Compilation.h:205
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
CleanupFileList - Remove the files in the given list.
const ArgStringMap & getResultFiles() const
Definition: Compilation.h:219
StringRef getSysRoot() const
Returns the sysroot path.
void setContainsError()
Force driver to fail before toolchain is created.
Definition: Compilation.h:334
const_offload_toolchains_range getOffloadToolChains() const
Definition: Compilation.h:154
unsigned getActiveOffloadKinds() const
Definition: Compilation.h:143
const char * addFailureResultFile(const char *Name, const JobAction *JA)
addFailureResultFile - Add a file to remove if we crash, and returns its argument.
Definition: Compilation.h:264
const std::multimap< Action::OffloadKind, const ToolChain * >::const_iterator const_offload_toolchains_iterator
Iterator that visits device toolchains of a given kind.
Definition: Compilation.h:148
void Redirect(ArrayRef< std::optional< StringRef > > Redirects)
Redirect - Redirect output of this compilation.
const ToolChain & getDefaultToolChain() const
Definition: Compilation.h:137
const char * addResultFile(const char *Name, const JobAction *JA)
addResultFile - Add a file to remove on failure, and returns its argument.
Definition: Compilation.h:257
void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain, Action::OffloadKind OffloadKind)
Definition: Compilation.h:182
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:214
const char * addTempFile(const char *Name)
addTempFile - Add a file to remove on exit, and returns its argument.
Definition: Compilation.h:250
const llvm::opt::InputArgList & getInputArgs() const
Definition: Compilation.h:193
const llvm::opt::ArgStringList & getTempFiles() const
Definition: Compilation.h:217
void addTimeTraceFile(const char *Name, const JobAction *JA)
Definition: Compilation.h:272
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:195
std::pair< const_offload_toolchains_iterator, const_offload_toolchains_iterator > const_offload_toolchains_range
Definition: Compilation.h:151
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * > > &FailingCommands, bool LogOnly=false) const
ExecuteJob - Execute a single job.
bool containsError() const
Return whether an error during the parsing of the input args.
Definition: Compilation.h:330
bool isForDiagnostics() const
Return true if we're compiling for diagnostics.
Definition: Compilation.h:327
const ToolChain * getSingleOffloadToolChain() const
Return an offload toolchain of the provided kind.
Definition: Compilation.h:172
const Driver & getDriver() const
Definition: Compilation.h:135
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:99
JobList - A sequence of jobs to perform.
Definition: Job.h:260
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition: Job.h:275
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:92
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:22
The JSON file list parser is used to communicate input to InstallAPI.
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
const FunctionProtoType * T
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
Definition: Wasm.cpp:36
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
int const char * function
Definition: c++config.h:31