/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Driver/Driver.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- Driver.h - Clang GCC Compatible Driver -----------------*- 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_DRIVER_H |
10 | | #define LLVM_CLANG_DRIVER_DRIVER_H |
11 | | |
12 | | #include "clang/Basic/Diagnostic.h" |
13 | | #include "clang/Basic/LLVM.h" |
14 | | #include "clang/Driver/Action.h" |
15 | | #include "clang/Driver/DriverDiagnostic.h" |
16 | | #include "clang/Driver/InputInfo.h" |
17 | | #include "clang/Driver/Options.h" |
18 | | #include "clang/Driver/Phases.h" |
19 | | #include "clang/Driver/ToolChain.h" |
20 | | #include "clang/Driver/Types.h" |
21 | | #include "clang/Driver/Util.h" |
22 | | #include "llvm/ADT/StringMap.h" |
23 | | #include "llvm/ADT/StringRef.h" |
24 | | #include "llvm/Option/Arg.h" |
25 | | #include "llvm/Option/ArgList.h" |
26 | | #include "llvm/Support/StringSaver.h" |
27 | | |
28 | | #include <list> |
29 | | #include <map> |
30 | | #include <string> |
31 | | |
32 | | namespace llvm { |
33 | | class Triple; |
34 | | namespace vfs { |
35 | | class FileSystem; |
36 | | } |
37 | | } // namespace llvm |
38 | | |
39 | | namespace clang { |
40 | | |
41 | | namespace driver { |
42 | | |
43 | | typedef SmallVector<InputInfo, 4> InputInfoList; |
44 | | |
45 | | class Command; |
46 | | class Compilation; |
47 | | class JobList; |
48 | | class JobAction; |
49 | | class SanitizerArgs; |
50 | | class ToolChain; |
51 | | |
52 | | /// Describes the kind of LTO mode selected via -f(no-)?lto(=.*)? options. |
53 | | enum LTOKind { |
54 | | LTOK_None, |
55 | | LTOK_Full, |
56 | | LTOK_Thin, |
57 | | LTOK_Unknown |
58 | | }; |
59 | | |
60 | | /// Whether headers used to construct C++20 module units should be looked |
61 | | /// up by the path supplied on the command line, or in the user or system |
62 | | /// search paths. |
63 | | enum ModuleHeaderMode { |
64 | | HeaderMode_None, |
65 | | HeaderMode_Default, |
66 | | HeaderMode_User, |
67 | | HeaderMode_System |
68 | | }; |
69 | | |
70 | | /// Driver - Encapsulate logic for constructing compilation processes |
71 | | /// from a set of gcc-driver-like command line arguments. |
72 | | class Driver { |
73 | | DiagnosticsEngine &Diags; |
74 | | |
75 | | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS; |
76 | | |
77 | | enum DriverMode { |
78 | | GCCMode, |
79 | | GXXMode, |
80 | | CPPMode, |
81 | | CLMode, |
82 | | FlangMode, |
83 | | DXCMode |
84 | | } Mode; |
85 | | |
86 | | enum SaveTempsMode { |
87 | | SaveTempsNone, |
88 | | SaveTempsCwd, |
89 | | SaveTempsObj |
90 | | } SaveTemps; |
91 | | |
92 | | enum BitcodeEmbedMode { |
93 | | EmbedNone, |
94 | | EmbedMarker, |
95 | | EmbedBitcode |
96 | | } BitcodeEmbed; |
97 | | |
98 | | enum OffloadMode { |
99 | | OffloadHostDevice, |
100 | | OffloadHost, |
101 | | OffloadDevice, |
102 | | } Offload; |
103 | | |
104 | | /// Header unit mode set by -fmodule-header={user,system}. |
105 | | ModuleHeaderMode CXX20HeaderType; |
106 | | |
107 | | /// Set if we should process inputs and jobs with C++20 module |
108 | | /// interpretation. |
109 | | bool ModulesModeCXX20; |
110 | | |
111 | | /// LTO mode selected via -f(no-)?lto(=.*)? options. |
112 | | LTOKind LTOMode; |
113 | | |
114 | | /// LTO mode selected via -f(no-offload-)?lto(=.*)? options. |
115 | | LTOKind OffloadLTOMode; |
116 | | |
117 | | public: |
118 | | enum OpenMPRuntimeKind { |
119 | | /// An unknown OpenMP runtime. We can't generate effective OpenMP code |
120 | | /// without knowing what runtime to target. |
121 | | OMPRT_Unknown, |
122 | | |
123 | | /// The LLVM OpenMP runtime. When completed and integrated, this will become |
124 | | /// the default for Clang. |
125 | | OMPRT_OMP, |
126 | | |
127 | | /// The GNU OpenMP runtime. Clang doesn't support generating OpenMP code for |
128 | | /// this runtime but can swallow the pragmas, and find and link against the |
129 | | /// runtime library itself. |
130 | | OMPRT_GOMP, |
131 | | |
132 | | /// The legacy name for the LLVM OpenMP runtime from when it was the Intel |
133 | | /// OpenMP runtime. We support this mode for users with existing |
134 | | /// dependencies on this runtime library name. |
135 | | OMPRT_IOMP5 |
136 | | }; |
137 | | |
138 | | // Diag - Forwarding function for diagnostics. |
139 | 7.00k | DiagnosticBuilder Diag(unsigned DiagID) const { |
140 | 7.00k | return Diags.Report(DiagID); |
141 | 7.00k | } |
142 | | |
143 | | // FIXME: Privatize once interface is stable. |
144 | | public: |
145 | | /// The name the driver was invoked as. |
146 | | std::string Name; |
147 | | |
148 | | /// The path the driver executable was in, as invoked from the |
149 | | /// command line. |
150 | | std::string Dir; |
151 | | |
152 | | /// The original path to the clang executable. |
153 | | std::string ClangExecutable; |
154 | | |
155 | | /// Target and driver mode components extracted from clang executable name. |
156 | | ParsedClangName ClangNameParts; |
157 | | |
158 | | /// The path to the installed clang directory, if any. |
159 | | std::string InstalledDir; |
160 | | |
161 | | /// The path to the compiler resource directory. |
162 | | std::string ResourceDir; |
163 | | |
164 | | /// System directory for config files. |
165 | | std::string SystemConfigDir; |
166 | | |
167 | | /// User directory for config files. |
168 | | std::string UserConfigDir; |
169 | | |
170 | | /// A prefix directory used to emulate a limited subset of GCC's '-Bprefix' |
171 | | /// functionality. |
172 | | /// FIXME: This type of customization should be removed in favor of the |
173 | | /// universal driver when it is ready. |
174 | | typedef SmallVector<std::string, 4> prefix_list; |
175 | | prefix_list PrefixDirs; |
176 | | |
177 | | /// sysroot, if present |
178 | | std::string SysRoot; |
179 | | |
180 | | /// Dynamic loader prefix, if present |
181 | | std::string DyldPrefix; |
182 | | |
183 | | /// Driver title to use with help. |
184 | | std::string DriverTitle; |
185 | | |
186 | | /// Information about the host which can be overridden by the user. |
187 | | std::string HostBits, HostMachine, HostSystem, HostRelease; |
188 | | |
189 | | /// The file to log CC_PRINT_PROC_STAT_FILE output to, if enabled. |
190 | | std::string CCPrintStatReportFilename; |
191 | | |
192 | | /// The file to log CC_PRINT_OPTIONS output to, if enabled. |
193 | | std::string CCPrintOptionsFilename; |
194 | | |
195 | | /// The file to log CC_PRINT_HEADERS output to, if enabled. |
196 | | std::string CCPrintHeadersFilename; |
197 | | |
198 | | /// The file to log CC_LOG_DIAGNOSTICS output to, if enabled. |
199 | | std::string CCLogDiagnosticsFilename; |
200 | | |
201 | | /// An input type and its arguments. |
202 | | using InputTy = std::pair<types::ID, const llvm::opt::Arg *>; |
203 | | |
204 | | /// A list of inputs and their types for the given arguments. |
205 | | using InputList = SmallVector<InputTy, 16>; |
206 | | |
207 | | /// Whether the driver should follow g++ like behavior. |
208 | 113k | bool CCCIsCXX() const { return Mode == GXXMode; } |
209 | | |
210 | | /// Whether the driver is just the preprocessor. |
211 | 189k | bool CCCIsCPP() const { return Mode == CPPMode; } |
212 | | |
213 | | /// Whether the driver should follow gcc like behavior. |
214 | | bool CCCIsCC() const { return Mode == GCCMode; } |
215 | | |
216 | | /// Whether the driver should follow cl.exe like behavior. |
217 | 396k | bool IsCLMode() const { return Mode == CLMode; } |
218 | | |
219 | | /// Whether the driver should invoke flang for fortran inputs. |
220 | | /// Other modes fall back to calling gcc which in turn calls gfortran. |
221 | 171k | bool IsFlangMode() const { return Mode == FlangMode; } |
222 | | |
223 | | /// Whether the driver should follow dxc.exe like behavior. |
224 | 146k | bool IsDXCMode() const { return Mode == DXCMode; } |
225 | | |
226 | | /// Only print tool bindings, don't build any jobs. |
227 | | unsigned CCCPrintBindings : 1; |
228 | | |
229 | | /// Set CC_PRINT_OPTIONS mode, which is like -v but logs the commands to |
230 | | /// CCPrintOptionsFilename or to stderr. |
231 | | unsigned CCPrintOptions : 1; |
232 | | |
233 | | /// Set CC_PRINT_HEADERS mode, which causes the frontend to log header include |
234 | | /// information to CCPrintHeadersFilename or to stderr. |
235 | | unsigned CCPrintHeaders : 1; |
236 | | |
237 | | /// Set CC_LOG_DIAGNOSTICS mode, which causes the frontend to log diagnostics |
238 | | /// to CCLogDiagnosticsFilename or to stderr, in a stable machine readable |
239 | | /// format. |
240 | | unsigned CCLogDiagnostics : 1; |
241 | | |
242 | | /// Whether the driver is generating diagnostics for debugging purposes. |
243 | | unsigned CCGenDiagnostics : 1; |
244 | | |
245 | | /// Set CC_PRINT_PROC_STAT mode, which causes the driver to dump |
246 | | /// performance report to CC_PRINT_PROC_STAT_FILE or to stdout. |
247 | | unsigned CCPrintProcessStats : 1; |
248 | | |
249 | | /// Pointer to the ExecuteCC1Tool function, if available. |
250 | | /// When the clangDriver lib is used through clang.exe, this provides a |
251 | | /// shortcut for executing the -cc1 command-line directly, in the same |
252 | | /// process. |
253 | | typedef int (*CC1ToolFunc)(SmallVectorImpl<const char *> &ArgV); |
254 | | CC1ToolFunc CC1Main = nullptr; |
255 | | |
256 | | private: |
257 | | /// Raw target triple. |
258 | | std::string TargetTriple; |
259 | | |
260 | | /// Name to use when invoking gcc/g++. |
261 | | std::string CCCGenericGCCName; |
262 | | |
263 | | /// Name of configuration file if used. |
264 | | std::string ConfigFile; |
265 | | |
266 | | /// Allocator for string saver. |
267 | | llvm::BumpPtrAllocator Alloc; |
268 | | |
269 | | /// Object that stores strings read from configuration file. |
270 | | llvm::StringSaver Saver; |
271 | | |
272 | | /// Arguments originated from configuration file. |
273 | | std::unique_ptr<llvm::opt::InputArgList> CfgOptions; |
274 | | |
275 | | /// Arguments originated from command line. |
276 | | std::unique_ptr<llvm::opt::InputArgList> CLOptions; |
277 | | |
278 | | /// Whether to check that input files exist when constructing compilation |
279 | | /// jobs. |
280 | | unsigned CheckInputsExist : 1; |
281 | | /// Whether to probe for PCH files on disk, in order to upgrade |
282 | | /// -include foo.h to -include-pch foo.h.pch. |
283 | | unsigned ProbePrecompiled : 1; |
284 | | |
285 | | public: |
286 | | // getFinalPhase - Determine which compilation mode we are in and record |
287 | | // which option we used to determine the final phase. |
288 | | // TODO: Much of what getFinalPhase returns are not actually true compiler |
289 | | // modes. Fold this functionality into Types::getCompilationPhases and |
290 | | // handleArguments. |
291 | | phases::ID getFinalPhase(const llvm::opt::DerivedArgList &DAL, |
292 | | llvm::opt::Arg **FinalPhaseArg = nullptr) const; |
293 | | |
294 | | private: |
295 | | /// Certain options suppress the 'no input files' warning. |
296 | | unsigned SuppressMissingInputWarning : 1; |
297 | | |
298 | | /// Cache of all the ToolChains in use by the driver. |
299 | | /// |
300 | | /// This maps from the string representation of a triple to a ToolChain |
301 | | /// created targeting that triple. The driver owns all the ToolChain objects |
302 | | /// stored in it, and will clean them up when torn down. |
303 | | mutable llvm::StringMap<std::unique_ptr<ToolChain>> ToolChains; |
304 | | |
305 | | /// Cache of known offloading architectures for the ToolChain already derived. |
306 | | /// This should only be modified when we first initialize the offloading |
307 | | /// toolchains. |
308 | | llvm::DenseMap<const ToolChain *, llvm::DenseSet<llvm::StringRef>> KnownArchs; |
309 | | |
310 | | private: |
311 | | /// TranslateInputArgs - Create a new derived argument list from the input |
312 | | /// arguments, after applying the standard argument translations. |
313 | | llvm::opt::DerivedArgList * |
314 | | TranslateInputArgs(const llvm::opt::InputArgList &Args) const; |
315 | | |
316 | | // handleArguments - All code related to claiming and printing diagnostics |
317 | | // related to arguments to the driver are done here. |
318 | | void handleArguments(Compilation &C, llvm::opt::DerivedArgList &Args, |
319 | | const InputList &Inputs, ActionList &Actions) const; |
320 | | |
321 | | // Before executing jobs, sets up response files for commands that need them. |
322 | | void setUpResponseFiles(Compilation &C, Command &Cmd); |
323 | | |
324 | | void generatePrefixedToolNames(StringRef Tool, const ToolChain &TC, |
325 | | SmallVectorImpl<std::string> &Names) const; |
326 | | |
327 | | /// Find the appropriate .crash diagonostic file for the child crash |
328 | | /// under this driver and copy it out to a temporary destination with the |
329 | | /// other reproducer related files (.sh, .cache, etc). If not found, suggest a |
330 | | /// directory for the user to look at. |
331 | | /// |
332 | | /// \param ReproCrashFilename The file path to copy the .crash to. |
333 | | /// \param CrashDiagDir The suggested directory for the user to look at |
334 | | /// in case the search or copy fails. |
335 | | /// |
336 | | /// \returns If the .crash is found and successfully copied return true, |
337 | | /// otherwise false and return the suggested directory in \p CrashDiagDir. |
338 | | bool getCrashDiagnosticFile(StringRef ReproCrashFilename, |
339 | | SmallString<128> &CrashDiagDir); |
340 | | |
341 | | public: |
342 | | |
343 | | /// Takes the path to a binary that's either in bin/ or lib/ and returns |
344 | | /// the path to clang's resource directory. |
345 | | static std::string GetResourcesPath(StringRef BinaryPath, |
346 | | StringRef CustomResourceDir = ""); |
347 | | |
348 | | Driver(StringRef ClangExecutable, StringRef TargetTriple, |
349 | | DiagnosticsEngine &Diags, std::string Title = "clang LLVM compiler", |
350 | | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr); |
351 | | |
352 | | /// @name Accessors |
353 | | /// @{ |
354 | | |
355 | | /// Name to use when invoking gcc/g++. |
356 | 437 | const std::string &getCCCGenericGCCName() const { return CCCGenericGCCName; } |
357 | | |
358 | 0 | const std::string &getConfigFile() const { return ConfigFile; } |
359 | | |
360 | 228k | const llvm::opt::OptTable &getOpts() const { return getDriverOptTable(); } |
361 | | |
362 | 45.7k | DiagnosticsEngine &getDiags() const { return Diags; } |
363 | | |
364 | 635k | llvm::vfs::FileSystem &getVFS() const { return *VFS; } |
365 | | |
366 | 49.2k | bool getCheckInputsExist() const { return CheckInputsExist; } |
367 | | |
368 | 4.96k | void setCheckInputsExist(bool Value) { CheckInputsExist = Value; } |
369 | | |
370 | 3.49k | bool getProbePrecompiled() const { return ProbePrecompiled; } |
371 | 4.74k | void setProbePrecompiled(bool Value) { ProbePrecompiled = Value; } |
372 | | |
373 | 19.8k | void setTargetAndMode(const ParsedClangName &TM) { ClangNameParts = TM; } |
374 | | |
375 | 0 | const std::string &getTitle() { return DriverTitle; } |
376 | 25.3k | void setTitle(std::string Value) { DriverTitle = std::move(Value); } |
377 | | |
378 | 2.03k | std::string getTargetTriple() const { return TargetTriple; } |
379 | | |
380 | | /// Get the path to the main clang executable. |
381 | 95.2k | const char *getClangProgramPath() const { |
382 | 95.2k | return ClangExecutable.c_str(); |
383 | 95.2k | } |
384 | | |
385 | | /// Get the path to where the clang executable was installed. |
386 | 162k | const char *getInstalledDir() const { |
387 | 162k | if (!InstalledDir.empty()) |
388 | 63.3k | return InstalledDir.c_str(); |
389 | 99.5k | return Dir.c_str(); |
390 | 162k | } |
391 | 19.8k | void setInstalledDir(StringRef Value) { InstalledDir = std::string(Value); } |
392 | | |
393 | 203k | bool isSaveTempsEnabled() const { return SaveTemps != SaveTempsNone; } |
394 | 404 | bool isSaveTempsObj() const { return SaveTemps == SaveTempsObj; } |
395 | | |
396 | 15.0k | bool embedBitcodeEnabled() const { return BitcodeEmbed != EmbedNone; } |
397 | 100k | bool embedBitcodeInObject() const { return (BitcodeEmbed == EmbedBitcode); } |
398 | 56.8k | bool embedBitcodeMarkerOnly() const { return (BitcodeEmbed == EmbedMarker); } |
399 | | |
400 | 397 | bool offloadHostOnly() const { return Offload == OffloadHost; } |
401 | 371 | bool offloadDeviceOnly() const { return Offload == OffloadDevice; } |
402 | | |
403 | | /// Compute the desired OpenMP runtime from the flags provided. |
404 | | OpenMPRuntimeKind getOpenMPRuntime(const llvm::opt::ArgList &Args) const; |
405 | | |
406 | | /// @} |
407 | | /// @name Primary Functionality |
408 | | /// @{ |
409 | | |
410 | | /// CreateOffloadingDeviceToolChains - create all the toolchains required to |
411 | | /// support offloading devices given the programming models specified in the |
412 | | /// current compilation. Also, update the host tool chain kind accordingly. |
413 | | void CreateOffloadingDeviceToolChains(Compilation &C, InputList &Inputs); |
414 | | |
415 | | /// BuildCompilation - Construct a compilation object for a command |
416 | | /// line argument vector. |
417 | | /// |
418 | | /// \return A compilation, or 0 if none was built for the given |
419 | | /// argument vector. A null return value does not necessarily |
420 | | /// indicate an error condition, the diagnostics should be queried |
421 | | /// to determine if an error occurred. |
422 | | Compilation *BuildCompilation(ArrayRef<const char *> Args); |
423 | | |
424 | | /// ParseArgStrings - Parse the given list of strings into an |
425 | | /// ArgList. |
426 | | llvm::opt::InputArgList ParseArgStrings(ArrayRef<const char *> Args, |
427 | | bool IsClCompatMode, |
428 | | bool &ContainsError); |
429 | | |
430 | | /// BuildInputs - Construct the list of inputs and their types from |
431 | | /// the given arguments. |
432 | | /// |
433 | | /// \param TC - The default host tool chain. |
434 | | /// \param Args - The input arguments. |
435 | | /// \param Inputs - The list to store the resulting compilation |
436 | | /// inputs onto. |
437 | | void BuildInputs(const ToolChain &TC, llvm::opt::DerivedArgList &Args, |
438 | | InputList &Inputs) const; |
439 | | |
440 | | /// BuildActions - Construct the list of actions to perform for the |
441 | | /// given arguments, which are only done for a single architecture. |
442 | | /// |
443 | | /// \param C - The compilation that is being built. |
444 | | /// \param Args - The input arguments. |
445 | | /// \param Actions - The list to store the resulting actions onto. |
446 | | void BuildActions(Compilation &C, llvm::opt::DerivedArgList &Args, |
447 | | const InputList &Inputs, ActionList &Actions) const; |
448 | | |
449 | | /// BuildUniversalActions - Construct the list of actions to perform |
450 | | /// for the given arguments, which may require a universal build. |
451 | | /// |
452 | | /// \param C - The compilation that is being built. |
453 | | /// \param TC - The default host tool chain. |
454 | | void BuildUniversalActions(Compilation &C, const ToolChain &TC, |
455 | | const InputList &BAInputs) const; |
456 | | |
457 | | /// BuildOffloadingActions - Construct the list of actions to perform for the |
458 | | /// offloading toolchain that will be embedded in the host. |
459 | | /// |
460 | | /// \param C - The compilation that is being built. |
461 | | /// \param Args - The input arguments. |
462 | | /// \param Input - The input type and arguments |
463 | | /// \param HostAction - The host action used in the offloading toolchain. |
464 | | Action *BuildOffloadingActions(Compilation &C, |
465 | | llvm::opt::DerivedArgList &Args, |
466 | | const InputTy &Input, |
467 | | Action *HostAction) const; |
468 | | |
469 | | /// Returns the set of bound architectures active for this offload kind. |
470 | | /// If there are no bound architctures we return a set containing only the |
471 | | /// empty string. |
472 | | llvm::DenseSet<StringRef> |
473 | | getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args, |
474 | | Action::OffloadKind Kind, const ToolChain *TC) const; |
475 | | |
476 | | /// Check that the file referenced by Value exists. If it doesn't, |
477 | | /// issue a diagnostic and return false. |
478 | | /// If TypoCorrect is true and the file does not exist, see if it looks |
479 | | /// like a likely typo for a flag and if so print a "did you mean" blurb. |
480 | | bool DiagnoseInputExistence(const llvm::opt::DerivedArgList &Args, |
481 | | StringRef Value, types::ID Ty, |
482 | | bool TypoCorrect) const; |
483 | | |
484 | | /// BuildJobs - Bind actions to concrete tools and translate |
485 | | /// arguments to form the list of jobs to run. |
486 | | /// |
487 | | /// \param C - The compilation that is being built. |
488 | | void BuildJobs(Compilation &C) const; |
489 | | |
490 | | /// ExecuteCompilation - Execute the compilation according to the command line |
491 | | /// arguments and return an appropriate exit code. |
492 | | /// |
493 | | /// This routine handles additional processing that must be done in addition |
494 | | /// to just running the subprocesses, for example reporting errors, setting |
495 | | /// up response files, removing temporary files, etc. |
496 | | int ExecuteCompilation(Compilation &C, |
497 | | SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands); |
498 | | |
499 | | /// Contains the files in the compilation diagnostic report generated by |
500 | | /// generateCompilationDiagnostics. |
501 | | struct CompilationDiagnosticReport { |
502 | | llvm::SmallVector<std::string, 4> TemporaryFiles; |
503 | | }; |
504 | | |
505 | | /// generateCompilationDiagnostics - Generate diagnostics information |
506 | | /// including preprocessed source file(s). |
507 | | /// |
508 | | void generateCompilationDiagnostics( |
509 | | Compilation &C, const Command &FailingCommand, |
510 | | StringRef AdditionalInformation = "", |
511 | | CompilationDiagnosticReport *GeneratedReport = nullptr); |
512 | | |
513 | | enum class CommandStatus { |
514 | | Crash = 1, |
515 | | Error, |
516 | | Ok, |
517 | | }; |
518 | | |
519 | | enum class ReproLevel { |
520 | | Off = 0, |
521 | | OnCrash = static_cast<int>(CommandStatus::Crash), |
522 | | OnError = static_cast<int>(CommandStatus::Error), |
523 | | Always = static_cast<int>(CommandStatus::Ok), |
524 | | }; |
525 | | |
526 | | bool maybeGenerateCompilationDiagnostics( |
527 | | CommandStatus CS, ReproLevel Level, Compilation &C, |
528 | | const Command &FailingCommand, StringRef AdditionalInformation = "", |
529 | 18.2k | CompilationDiagnosticReport *GeneratedReport = nullptr) { |
530 | 18.2k | if (static_cast<int>(CS) > static_cast<int>(Level)) |
531 | 18.1k | return false; |
532 | 38 | if (CS != CommandStatus::Crash) |
533 | 19 | Diags.Report(diag::err_drv_force_crash) |
534 | 19 | << !::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"); |
535 | | // Hack to ensure that diagnostic notes get emitted. |
536 | 38 | Diags.setLastDiagnosticIgnored(false); |
537 | 38 | generateCompilationDiagnostics(C, FailingCommand, AdditionalInformation, |
538 | 38 | GeneratedReport); |
539 | 38 | return true; |
540 | 18.2k | } |
541 | | |
542 | | /// @} |
543 | | /// @name Helper Methods |
544 | | /// @{ |
545 | | |
546 | | /// PrintActions - Print the list of actions. |
547 | | void PrintActions(const Compilation &C) const; |
548 | | |
549 | | /// PrintHelp - Print the help text. |
550 | | /// |
551 | | /// \param ShowHidden - Show hidden options. |
552 | | void PrintHelp(bool ShowHidden) const; |
553 | | |
554 | | /// PrintVersion - Print the driver version. |
555 | | void PrintVersion(const Compilation &C, raw_ostream &OS) const; |
556 | | |
557 | | /// GetFilePath - Lookup \p Name in the list of file search paths. |
558 | | /// |
559 | | /// \param TC - The tool chain for additional information on |
560 | | /// directories to search. |
561 | | // |
562 | | // FIXME: This should be in CompilationInfo. |
563 | | std::string GetFilePath(StringRef Name, const ToolChain &TC) const; |
564 | | |
565 | | /// GetProgramPath - Lookup \p Name in the list of program search paths. |
566 | | /// |
567 | | /// \param TC - The provided tool chain for additional information on |
568 | | /// directories to search. |
569 | | // |
570 | | // FIXME: This should be in CompilationInfo. |
571 | | std::string GetProgramPath(StringRef Name, const ToolChain &TC) const; |
572 | | |
573 | | /// HandleAutocompletions - Handle --autocomplete by searching and printing |
574 | | /// possible flags, descriptions, and its arguments. |
575 | | void HandleAutocompletions(StringRef PassedFlags) const; |
576 | | |
577 | | /// HandleImmediateArgs - Handle any arguments which should be |
578 | | /// treated before building actions or binding tools. |
579 | | /// |
580 | | /// \return Whether any compilation should be built for this |
581 | | /// invocation. |
582 | | bool HandleImmediateArgs(const Compilation &C); |
583 | | |
584 | | /// ConstructAction - Construct the appropriate action to do for |
585 | | /// \p Phase on the \p Input, taking in to account arguments |
586 | | /// like -fsyntax-only or --analyze. |
587 | | Action *ConstructPhaseAction( |
588 | | Compilation &C, const llvm::opt::ArgList &Args, phases::ID Phase, |
589 | | Action *Input, |
590 | | Action::OffloadKind TargetDeviceOffloadKind = Action::OFK_None) const; |
591 | | |
592 | | /// BuildJobsForAction - Construct the jobs to perform for the action \p A and |
593 | | /// return an InputInfo for the result of running \p A. Will only construct |
594 | | /// jobs for a given (Action, ToolChain, BoundArch, DeviceKind) tuple once. |
595 | | InputInfoList BuildJobsForAction( |
596 | | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
597 | | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
598 | | std::map<std::pair<const Action *, std::string>, InputInfoList> |
599 | | &CachedResults, |
600 | | Action::OffloadKind TargetDeviceOffloadKind) const; |
601 | | |
602 | | /// Returns the default name for linked images (e.g., "a.out"). |
603 | | const char *getDefaultImageName() const; |
604 | | |
605 | | /// GetNamedOutputPath - Return the name to use for the output of |
606 | | /// the action \p JA. The result is appended to the compilation's |
607 | | /// list of temporary or result files, as appropriate. |
608 | | /// |
609 | | /// \param C - The compilation. |
610 | | /// \param JA - The action of interest. |
611 | | /// \param BaseInput - The original input file that this action was |
612 | | /// triggered by. |
613 | | /// \param BoundArch - The bound architecture. |
614 | | /// \param AtTopLevel - Whether this is a "top-level" action. |
615 | | /// \param MultipleArchs - Whether multiple -arch options were supplied. |
616 | | /// \param NormalizedTriple - The normalized triple of the relevant target. |
617 | | const char *GetNamedOutputPath(Compilation &C, const JobAction &JA, |
618 | | const char *BaseInput, StringRef BoundArch, |
619 | | bool AtTopLevel, bool MultipleArchs, |
620 | | StringRef NormalizedTriple) const; |
621 | | |
622 | | /// GetTemporaryPath - Return the pathname of a temporary file to use |
623 | | /// as part of compilation; the file will have the given prefix and suffix. |
624 | | /// |
625 | | /// GCC goes to extra lengths here to be a bit more robust. |
626 | | std::string GetTemporaryPath(StringRef Prefix, StringRef Suffix) const; |
627 | | |
628 | | /// GetTemporaryDirectory - Return the pathname of a temporary directory to |
629 | | /// use as part of compilation; the directory will have the given prefix. |
630 | | std::string GetTemporaryDirectory(StringRef Prefix) const; |
631 | | |
632 | | /// Return the pathname of the pch file in clang-cl mode. |
633 | | std::string GetClPchPath(Compilation &C, StringRef BaseName) const; |
634 | | |
635 | | /// ShouldUseClangCompiler - Should the clang compiler be used to |
636 | | /// handle this action. |
637 | | bool ShouldUseClangCompiler(const JobAction &JA) const; |
638 | | |
639 | | /// ShouldUseFlangCompiler - Should the flang compiler be used to |
640 | | /// handle this action. |
641 | | bool ShouldUseFlangCompiler(const JobAction &JA) const; |
642 | | |
643 | | /// ShouldEmitStaticLibrary - Should the linker emit a static library. |
644 | | bool ShouldEmitStaticLibrary(const llvm::opt::ArgList &Args) const; |
645 | | |
646 | | /// Returns true if the user has indicated a C++20 header unit mode. |
647 | 46 | bool hasHeaderMode() const { return CXX20HeaderType != HeaderMode_None; } |
648 | | |
649 | | /// Get the mode for handling headers as set by fmodule-header{=}. |
650 | 0 | ModuleHeaderMode getModuleHeaderMode() const { return CXX20HeaderType; } |
651 | | |
652 | | /// Returns true if we are performing any kind of LTO. |
653 | 77.8k | bool isUsingLTO(bool IsOffload = false) const { |
654 | 77.8k | return getLTOMode(IsOffload) != LTOK_None; |
655 | 77.8k | } |
656 | | |
657 | | /// Get the specific kind of LTO being performed. |
658 | 123k | LTOKind getLTOMode(bool IsOffload = false) const { |
659 | 123k | return IsOffload ? OffloadLTOMode14.4k : LTOMode109k ; |
660 | 123k | } |
661 | | |
662 | | private: |
663 | | |
664 | | /// Tries to load options from configuration file. |
665 | | /// |
666 | | /// \returns true if error occurred. |
667 | | bool loadConfigFile(); |
668 | | |
669 | | /// Read options from the specified file. |
670 | | /// |
671 | | /// \param [in] FileName File to read. |
672 | | /// \returns true, if error occurred while reading. |
673 | | bool readConfigFile(StringRef FileName); |
674 | | |
675 | | /// Set the driver mode (cl, gcc, etc) from the value of the `--driver-mode` |
676 | | /// option. |
677 | | void setDriverMode(StringRef DriverModeValue); |
678 | | |
679 | | /// Parse the \p Args list for LTO options and record the type of LTO |
680 | | /// compilation based on which -f(no-)?lto(=.*)? option occurs last. |
681 | | void setLTOMode(const llvm::opt::ArgList &Args); |
682 | | |
683 | | /// Retrieves a ToolChain for a particular \p Target triple. |
684 | | /// |
685 | | /// Will cache ToolChains for the life of the driver object, and create them |
686 | | /// on-demand. |
687 | | const ToolChain &getToolChain(const llvm::opt::ArgList &Args, |
688 | | const llvm::Triple &Target) const; |
689 | | |
690 | | /// @} |
691 | | |
692 | | /// Retrieves a ToolChain for a particular device \p Target triple |
693 | | /// |
694 | | /// \param[in] HostTC is the host ToolChain paired with the device |
695 | | /// |
696 | | /// \param[in] TargetDeviceOffloadKind (e.g. OFK_Cuda/OFK_OpenMP/OFK_SYCL) is |
697 | | /// an Offloading action that is optionally passed to a ToolChain (used by |
698 | | /// CUDA, to specify if it's used in conjunction with OpenMP) |
699 | | /// |
700 | | /// Will cache ToolChains for the life of the driver object, and create them |
701 | | /// on-demand. |
702 | | const ToolChain &getOffloadingDeviceToolChain( |
703 | | const llvm::opt::ArgList &Args, const llvm::Triple &Target, |
704 | | const ToolChain &HostTC, |
705 | | const Action::OffloadKind &TargetDeviceOffloadKind) const; |
706 | | |
707 | | /// Get bitmasks for which option flags to include and exclude based on |
708 | | /// the driver mode. |
709 | | std::pair<unsigned, unsigned> getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const; |
710 | | |
711 | | /// Helper used in BuildJobsForAction. Doesn't use the cache when building |
712 | | /// jobs specifically for the given action, but will use the cache when |
713 | | /// building jobs for the Action's inputs. |
714 | | InputInfoList BuildJobsForActionNoCache( |
715 | | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
716 | | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
717 | | std::map<std::pair<const Action *, std::string>, InputInfoList> |
718 | | &CachedResults, |
719 | | Action::OffloadKind TargetDeviceOffloadKind) const; |
720 | | |
721 | | public: |
722 | | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and |
723 | | /// return the grouped values as integers. Numbers which are not |
724 | | /// provided are set to 0. |
725 | | /// |
726 | | /// \return True if the entire string was parsed (9.2), or all |
727 | | /// groups were parsed (10.3.5extrastuff). HadExtra is true if all |
728 | | /// groups were parsed but extra characters remain at the end. |
729 | | static bool GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
730 | | unsigned &Micro, bool &HadExtra); |
731 | | |
732 | | /// Parse digits from a string \p Str and fulfill \p Digits with |
733 | | /// the parsed numbers. This method assumes that the max number of |
734 | | /// digits to look for is equal to Digits.size(). |
735 | | /// |
736 | | /// \return True if the entire string was parsed and there are |
737 | | /// no extra characters remaining at the end. |
738 | | static bool GetReleaseVersion(StringRef Str, |
739 | | MutableArrayRef<unsigned> Digits); |
740 | | /// Compute the default -fmodule-cache-path. |
741 | | /// \return True if the system provides a default cache directory. |
742 | | static bool getDefaultModuleCachePath(SmallVectorImpl<char> &Result); |
743 | | }; |
744 | | |
745 | | /// \return True if the last defined optimization level is -Ofast. |
746 | | /// And False otherwise. |
747 | | bool isOptimizationLevelFast(const llvm::opt::ArgList &Args); |
748 | | |
749 | | /// \return True if the argument combination will end up generating remarks. |
750 | | bool willEmitRemarks(const llvm::opt::ArgList &Args); |
751 | | |
752 | | /// Returns the driver mode option's value, i.e. `X` in `--driver-mode=X`. If \p |
753 | | /// Args doesn't mention one explicitly, tries to deduce from `ProgName`. |
754 | | /// Returns empty on failure. |
755 | | /// Common values are "gcc", "g++", "cpp", "cl" and "flang". Returned value need |
756 | | /// not be one of these. |
757 | | llvm::StringRef getDriverMode(StringRef ProgName, ArrayRef<const char *> Args); |
758 | | |
759 | | /// Checks whether the value produced by getDriverMode is for CL mode. |
760 | | bool IsClangCL(StringRef DriverMode); |
761 | | |
762 | | } // end namespace driver |
763 | | } // end namespace clang |
764 | | |
765 | | #endif |