1//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
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// This is the entry point to the clang driver; it is a thin wrapper
10// for functionality in the Driver clang library.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Driver/Driver.h"
15#include "clang/Basic/DiagnosticOptions.h"
16#include "clang/Basic/HeaderInclude.h"
17#include "clang/Basic/Stack.h"
18#include "clang/Config/config.h"
19#include "clang/Driver/Compilation.h"
20#include "clang/Driver/DriverDiagnostic.h"
21#include "clang/Driver/Options.h"
22#include "clang/Driver/ToolChain.h"
23#include "clang/Frontend/ChainedDiagnosticConsumer.h"
24#include "clang/Frontend/CompilerInvocation.h"
25#include "clang/Frontend/SerializedDiagnosticPrinter.h"
26#include "clang/Frontend/TextDiagnosticPrinter.h"
27#include "clang/Frontend/Utils.h"
28#include "llvm/ADT/ArrayRef.h"
29#include "llvm/ADT/SmallString.h"
30#include "llvm/ADT/SmallVector.h"
31#include "llvm/ADT/StringSet.h"
32#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
33#include "llvm/Option/ArgList.h"
34#include "llvm/Option/OptTable.h"
35#include "llvm/Option/Option.h"
36#include "llvm/Support/BuryPointer.h"
37#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/CrashRecoveryContext.h"
39#include "llvm/Support/ErrorHandling.h"
40#include "llvm/Support/FileSystem.h"
41#include "llvm/Support/LLVMDriver.h"
42#include "llvm/Support/Path.h"
43#include "llvm/Support/PrettyStackTrace.h"
44#include "llvm/Support/Process.h"
45#include "llvm/Support/Program.h"
46#include "llvm/Support/Signals.h"
47#include "llvm/Support/StringSaver.h"
48#include "llvm/Support/TargetSelect.h"
49#include "llvm/Support/Timer.h"
50#include "llvm/Support/VirtualFileSystem.h"
51#include "llvm/Support/raw_ostream.h"
52#include "llvm/TargetParser/Host.h"
53#include <memory>
54#include <optional>
55#include <set>
56#include <system_error>
57
58using namespace clang;
59using namespace clang::driver;
60using namespace llvm::opt;
61
62std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
63 if (!CanonicalPrefixes) {
64 SmallString<128> ExecutablePath(Argv0);
65 // Do a PATH lookup if Argv0 isn't a valid path.
66 if (!llvm::sys::fs::exists(Path: ExecutablePath))
67 if (llvm::ErrorOr<std::string> P =
68 llvm::sys::findProgramByName(Name: ExecutablePath))
69 ExecutablePath = *P;
70 return std::string(ExecutablePath);
71 }
72
73 // This just needs to be some symbol in the binary; C++ doesn't
74 // allow taking the address of ::main however.
75 void *P = (void*) (intptr_t) GetExecutablePath;
76 return llvm::sys::fs::getMainExecutable(argv0: Argv0, MainExecAddr: P);
77}
78
79static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
80 return SavedStrings.insert(key: S).first->getKeyData();
81}
82
83extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
84 void *MainAddr);
85extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
86 void *MainAddr);
87extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
88 const char *Argv0, void *MainAddr,
89 const llvm::ToolContext &);
90
91static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
92 SmallVectorImpl<const char *> &ArgVector,
93 llvm::StringSet<> &SavedStrings) {
94 // Put target and mode arguments at the start of argument list so that
95 // arguments specified in command line could override them. Avoid putting
96 // them at index 0, as an option like '-cc1' must remain the first.
97 int InsertionPoint = 0;
98 if (ArgVector.size() > 0)
99 ++InsertionPoint;
100
101 if (NameParts.DriverMode) {
102 // Add the mode flag to the arguments.
103 ArgVector.insert(I: ArgVector.begin() + InsertionPoint,
104 Elt: GetStableCStr(SavedStrings, S: NameParts.DriverMode));
105 }
106
107 if (NameParts.TargetIsValid) {
108 const char *arr[] = {"-target", GetStableCStr(SavedStrings,
109 S: NameParts.TargetPrefix)};
110 ArgVector.insert(I: ArgVector.begin() + InsertionPoint,
111 From: std::begin(arr&: arr), To: std::end(arr&: arr));
112 }
113}
114
115static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
116 SmallVectorImpl<const char *> &Opts) {
117 llvm::cl::TokenizeWindowsCommandLine(Source: EnvValue, Saver, NewArgv&: Opts);
118 // The first instance of '#' should be replaced with '=' in each option.
119 for (const char *Opt : Opts)
120 if (char *NumberSignPtr = const_cast<char *>(::strchr(s: Opt, c: '#')))
121 *NumberSignPtr = '=';
122}
123
124template <class T>
125static T checkEnvVar(const char *EnvOptSet, const char *EnvOptFile,
126 std::string &OptFile) {
127 const char *Str = ::getenv(name: EnvOptSet);
128 if (!Str)
129 return T{};
130
131 T OptVal = Str;
132 if (const char *Var = ::getenv(name: EnvOptFile))
133 OptFile = Var;
134 return OptVal;
135}
136
137static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
138 TheDriver.CCPrintOptions =
139 checkEnvVar<bool>(EnvOptSet: "CC_PRINT_OPTIONS", EnvOptFile: "CC_PRINT_OPTIONS_FILE",
140 OptFile&: TheDriver.CCPrintOptionsFilename);
141 if (checkEnvVar<bool>(EnvOptSet: "CC_PRINT_HEADERS", EnvOptFile: "CC_PRINT_HEADERS_FILE",
142 OptFile&: TheDriver.CCPrintHeadersFilename)) {
143 TheDriver.CCPrintHeadersFormat = HIFMT_Textual;
144 TheDriver.CCPrintHeadersFiltering = HIFIL_None;
145 } else {
146 std::string EnvVar = checkEnvVar<std::string>(
147 EnvOptSet: "CC_PRINT_HEADERS_FORMAT", EnvOptFile: "CC_PRINT_HEADERS_FILE",
148 OptFile&: TheDriver.CCPrintHeadersFilename);
149 if (!EnvVar.empty()) {
150 TheDriver.CCPrintHeadersFormat =
151 stringToHeaderIncludeFormatKind(Str: EnvVar.c_str());
152 if (!TheDriver.CCPrintHeadersFormat) {
153 TheDriver.Diag(clang::diag::err_drv_print_header_env_var)
154 << 0 << EnvVar;
155 return false;
156 }
157
158 const char *FilteringStr = ::getenv(name: "CC_PRINT_HEADERS_FILTERING");
159 if (!FilteringStr) {
160 TheDriver.Diag(clang::diag::err_drv_print_header_env_var_invalid_format)
161 << EnvVar;
162 return false;
163 }
164 HeaderIncludeFilteringKind Filtering;
165 if (!stringToHeaderIncludeFiltering(Str: FilteringStr, Kind&: Filtering)) {
166 TheDriver.Diag(clang::diag::err_drv_print_header_env_var)
167 << 1 << FilteringStr;
168 return false;
169 }
170
171 if ((TheDriver.CCPrintHeadersFormat == HIFMT_Textual &&
172 Filtering != HIFIL_None) ||
173 (TheDriver.CCPrintHeadersFormat == HIFMT_JSON &&
174 Filtering == HIFIL_None)) {
175 TheDriver.Diag(clang::diag::err_drv_print_header_env_var_combination)
176 << EnvVar << FilteringStr;
177 return false;
178 }
179 TheDriver.CCPrintHeadersFiltering = Filtering;
180 }
181 }
182
183 TheDriver.CCLogDiagnostics =
184 checkEnvVar<bool>(EnvOptSet: "CC_LOG_DIAGNOSTICS", EnvOptFile: "CC_LOG_DIAGNOSTICS_FILE",
185 OptFile&: TheDriver.CCLogDiagnosticsFilename);
186 TheDriver.CCPrintProcessStats =
187 checkEnvVar<bool>(EnvOptSet: "CC_PRINT_PROC_STAT", EnvOptFile: "CC_PRINT_PROC_STAT_FILE",
188 OptFile&: TheDriver.CCPrintStatReportFilename);
189 TheDriver.CCPrintInternalStats =
190 checkEnvVar<bool>(EnvOptSet: "CC_PRINT_INTERNAL_STAT", EnvOptFile: "CC_PRINT_INTERNAL_STAT_FILE",
191 OptFile&: TheDriver.CCPrintInternalStatReportFilename);
192
193 return true;
194}
195
196static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
197 const std::string &Path) {
198 // If the clang binary happens to be named cl.exe for compatibility reasons,
199 // use clang-cl.exe as the prefix to avoid confusion between clang and MSVC.
200 StringRef ExeBasename(llvm::sys::path::stem(path: Path));
201 if (ExeBasename.equals_insensitive(RHS: "cl"))
202 ExeBasename = "clang-cl";
203 DiagClient->setPrefix(std::string(ExeBasename));
204}
205
206static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV,
207 const llvm::ToolContext &ToolContext) {
208 // If we call the cc1 tool from the clangDriver library (through
209 // Driver::CC1Main), we need to clean up the options usage count. The options
210 // are currently global, and they might have been used previously by the
211 // driver.
212 llvm::cl::ResetAllOptionOccurrences();
213
214 llvm::BumpPtrAllocator A;
215 llvm::cl::ExpansionContext ECtx(A, llvm::cl::TokenizeGNUCommandLine);
216 if (llvm::Error Err = ECtx.expandResponseFiles(Argv&: ArgV)) {
217 llvm::errs() << toString(E: std::move(Err)) << '\n';
218 return 1;
219 }
220 StringRef Tool = ArgV[1];
221 void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
222 if (Tool == "-cc1")
223 return cc1_main(Argv: ArrayRef(ArgV).slice(N: 1), Argv0: ArgV[0], MainAddr: GetExecutablePathVP);
224 if (Tool == "-cc1as")
225 return cc1as_main(Argv: ArrayRef(ArgV).slice(N: 2), Argv0: ArgV[0], MainAddr: GetExecutablePathVP);
226 if (Tool == "-cc1gen-reproducer")
227 return cc1gen_reproducer_main(Argv: ArrayRef(ArgV).slice(N: 2), Argv0: ArgV[0],
228 MainAddr: GetExecutablePathVP, ToolContext);
229 // Reject unknown tools.
230 llvm::errs()
231 << "error: unknown integrated tool '" << Tool << "'. "
232 << "Valid tools include '-cc1', '-cc1as' and '-cc1gen-reproducer'.\n";
233 return 1;
234}
235
236int clang_main(int Argc, char **Argv, const llvm::ToolContext &ToolContext) {
237 noteBottomOfStack();
238 llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
239 " and include the crash backtrace, preprocessed "
240 "source, and associated run script.\n");
241 SmallVector<const char *, 256> Args(Argv, Argv + Argc);
242
243 if (llvm::sys::Process::FixupStandardFileDescriptors())
244 return 1;
245
246 llvm::InitializeAllTargets();
247
248 llvm::BumpPtrAllocator A;
249 llvm::StringSaver Saver(A);
250
251 const char *ProgName =
252 ToolContext.NeedsPrependArg ? ToolContext.PrependArg : ToolContext.Path;
253
254 bool ClangCLMode =
255 IsClangCL(DriverMode: getDriverMode(ProgName, Args: llvm::ArrayRef(Args).slice(N: 1)));
256
257 if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, Alloc&: A)) {
258 llvm::errs() << toString(E: std::move(Err)) << '\n';
259 return 1;
260 }
261
262 // Handle -cc1 integrated tools.
263 if (Args.size() >= 2 && StringRef(Args[1]).starts_with(Prefix: "-cc1"))
264 return ExecuteCC1Tool(ArgV&: Args, ToolContext);
265
266 // Handle options that need handling before the real command line parsing in
267 // Driver::BuildCompilation()
268 bool CanonicalPrefixes = true;
269 for (int i = 1, size = Args.size(); i < size; ++i) {
270 // Skip end-of-line response file markers
271 if (Args[i] == nullptr)
272 continue;
273 if (StringRef(Args[i]) == "-canonical-prefixes")
274 CanonicalPrefixes = true;
275 else if (StringRef(Args[i]) == "-no-canonical-prefixes")
276 CanonicalPrefixes = false;
277 }
278
279 // Handle CL and _CL_ which permits additional command line options to be
280 // prepended or appended.
281 if (ClangCLMode) {
282 // Arguments in "CL" are prepended.
283 std::optional<std::string> OptCL = llvm::sys::Process::GetEnv(name: "CL");
284 if (OptCL) {
285 SmallVector<const char *, 8> PrependedOpts;
286 getCLEnvVarOptions(EnvValue&: *OptCL, Saver, Opts&: PrependedOpts);
287
288 // Insert right after the program name to prepend to the argument list.
289 Args.insert(I: Args.begin() + 1, From: PrependedOpts.begin(), To: PrependedOpts.end());
290 }
291 // Arguments in "_CL_" are appended.
292 std::optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv(name: "_CL_");
293 if (Opt_CL_) {
294 SmallVector<const char *, 8> AppendedOpts;
295 getCLEnvVarOptions(EnvValue&: *Opt_CL_, Saver, Opts&: AppendedOpts);
296
297 // Insert at the end of the argument list to append.
298 Args.append(in_start: AppendedOpts.begin(), in_end: AppendedOpts.end());
299 }
300 }
301
302 llvm::StringSet<> SavedStrings;
303 // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
304 // scenes.
305 if (const char *OverrideStr = ::getenv(name: "CCC_OVERRIDE_OPTIONS")) {
306 // FIXME: Driver shouldn't take extra initial argument.
307 driver::applyOverrideOptions(Args, OverrideOpts: OverrideStr, SavedStrings,
308 EnvVar: "CCC_OVERRIDE_OPTIONS", OS: &llvm::errs());
309 }
310
311 std::string Path = GetExecutablePath(Argv0: ToolContext.Path, CanonicalPrefixes);
312
313 // Whether the cc1 tool should be called inside the current process, or if we
314 // should spawn a new clang subprocess (old behavior).
315 // Not having an additional process saves some execution time of Windows,
316 // and makes debugging and profiling easier.
317 bool UseNewCC1Process = CLANG_SPAWN_CC1;
318 for (const char *Arg : Args)
319 UseNewCC1Process = llvm::StringSwitch<bool>(Arg)
320 .Case(S: "-fno-integrated-cc1", Value: true)
321 .Case(S: "-fintegrated-cc1", Value: false)
322 .Default(Value: UseNewCC1Process);
323
324 std::unique_ptr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(Argv: Args);
325 // Driver's diagnostics don't use suppression mappings, so don't bother
326 // parsing them. CC1 still receives full args, so this doesn't impact other
327 // actions.
328 DiagOpts->DiagnosticSuppressionMappingsFile.clear();
329
330 TextDiagnosticPrinter *DiagClient =
331 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);
332 FixupDiagPrefixExeName(DiagClient, Path: ProgName);
333
334 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
335
336 DiagnosticsEngine Diags(DiagID, *DiagOpts, DiagClient);
337
338 if (!DiagOpts->DiagnosticSerializationFile.empty()) {
339 auto SerializedConsumer =
340 clang::serialized_diags::create(OutputFile: DiagOpts->DiagnosticSerializationFile,
341 DiagOpts&: *DiagOpts, /*MergeChildRecords=*/true);
342 Diags.setClient(client: new ChainedDiagnosticConsumer(
343 Diags.takeClient(), std::move(SerializedConsumer)));
344 }
345
346 auto VFS = llvm::vfs::getRealFileSystem();
347 ProcessWarningOptions(Diags, Opts: *DiagOpts, VFS&: *VFS, /*ReportDiags=*/false);
348
349 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags,
350 /*Title=*/"clang LLVM compiler", VFS);
351 auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(ProgName);
352 TheDriver.setTargetAndMode(TargetAndMode);
353 // If -canonical-prefixes is set, GetExecutablePath will have resolved Path
354 // to the llvm driver binary, not clang. In this case, we need to use
355 // PrependArg which should be clang-*. Checking just CanonicalPrefixes is
356 // safe even in the normal case because PrependArg will be null so
357 // setPrependArg will be a no-op.
358 if (ToolContext.NeedsPrependArg || CanonicalPrefixes)
359 TheDriver.setPrependArg(ToolContext.PrependArg);
360
361 insertTargetAndModeArgs(NameParts: TargetAndMode, ArgVector&: Args, SavedStrings);
362
363 if (!SetBackdoorDriverOutputsFromEnvVars(TheDriver))
364 return 1;
365
366 auto ExecuteCC1WithContext =
367 [&ToolContext](SmallVectorImpl<const char *> &ArgV) {
368 return ExecuteCC1Tool(ArgV, ToolContext);
369 };
370 if (!UseNewCC1Process) {
371 TheDriver.CC1Main = ExecuteCC1WithContext;
372 // Ensure the CC1Command actually catches cc1 crashes
373 llvm::CrashRecoveryContext::Enable();
374 }
375
376 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
377
378 Driver::ReproLevel ReproLevel = Driver::ReproLevel::OnCrash;
379 if (Arg *A = C->getArgs().getLastArg(options::OPT_gen_reproducer_eq)) {
380 auto Level =
381 llvm::StringSwitch<std::optional<Driver::ReproLevel>>(A->getValue())
382 .Case(S: "off", Value: Driver::ReproLevel::Off)
383 .Case(S: "crash", Value: Driver::ReproLevel::OnCrash)
384 .Case(S: "error", Value: Driver::ReproLevel::OnError)
385 .Case(S: "always", Value: Driver::ReproLevel::Always)
386 .Default(Value: std::nullopt);
387 if (!Level) {
388 llvm::errs() << "Unknown value for " << A->getSpelling() << ": '"
389 << A->getValue() << "'\n";
390 return 1;
391 }
392 ReproLevel = *Level;
393 }
394 if (!!::getenv(name: "FORCE_CLANG_DIAGNOSTICS_CRASH"))
395 ReproLevel = Driver::ReproLevel::Always;
396
397 int Res = 1;
398 bool IsCrash = false;
399 Driver::CommandStatus CommandStatus = Driver::CommandStatus::Ok;
400 // Pretend the first command failed if ReproStatus is Always.
401 const Command *FailingCommand = nullptr;
402 if (!C->getJobs().empty())
403 FailingCommand = &*C->getJobs().begin();
404 if (C && !C->containsError()) {
405 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
406 Res = TheDriver.ExecuteCompilation(C&: *C, FailingCommands);
407
408 for (const auto &P : FailingCommands) {
409 int CommandRes = P.first;
410 FailingCommand = P.second;
411 if (!Res)
412 Res = CommandRes;
413
414 // If result status is < 0, then the driver command signalled an error.
415 // If result status is 70, then the driver command reported a fatal error.
416 // On Windows, abort will return an exit code of 3. In these cases,
417 // generate additional diagnostic information if possible.
418 IsCrash = CommandRes < 0 || CommandRes == 70;
419#ifdef _WIN32
420 IsCrash |= CommandRes == 3;
421#endif
422#if LLVM_ON_UNIX
423 // When running in integrated-cc1 mode, the CrashRecoveryContext returns
424 // the same codes as if the program crashed. See section "Exit Status for
425 // Commands":
426 // https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
427 IsCrash |= CommandRes > 128;
428#endif
429 CommandStatus =
430 IsCrash ? Driver::CommandStatus::Crash : Driver::CommandStatus::Error;
431 if (IsCrash)
432 break;
433 }
434 }
435
436 // Print the bug report message that would be printed if we did actually
437 // crash, but only if we're crashing due to FORCE_CLANG_DIAGNOSTICS_CRASH.
438 if (::getenv(name: "FORCE_CLANG_DIAGNOSTICS_CRASH"))
439 llvm::dbgs() << llvm::getBugReportMsg();
440 if (FailingCommand != nullptr &&
441 TheDriver.maybeGenerateCompilationDiagnostics(CS: CommandStatus, Level: ReproLevel,
442 C&: *C, FailingCommand: *FailingCommand))
443 Res = 1;
444
445 Diags.getClient()->finish();
446
447 if (!UseNewCC1Process && IsCrash) {
448 // When crashing in -fintegrated-cc1 mode, bury the timer pointers, because
449 // the internal linked list might point to already released stack frames.
450 llvm::BuryPointer(Ptr: llvm::TimerGroup::acquireTimerGlobals());
451 } else {
452 // If any timers were active but haven't been destroyed yet, print their
453 // results now. This happens in -disable-free mode.
454 llvm::TimerGroup::printAll(OS&: llvm::errs());
455 llvm::TimerGroup::clearAll();
456 }
457
458#ifdef _WIN32
459 // Exit status should not be negative on Win32, unless abnormal termination.
460 // Once abnormal termination was caught, negative status should not be
461 // propagated.
462 if (Res < 0)
463 Res = 1;
464#endif
465
466 // If we have multiple failing commands, we return the result of the first
467 // failing command.
468 return Res;
469}
470

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang/tools/driver/driver.cpp