1 //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This is the entry point to the clang driver; it is a thin wrapper
10 // for functionality in the Driver clang library.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Driver/Driver.h"
15 #include "clang/Basic/DiagnosticOptions.h"
16 #include "clang/Basic/Stack.h"
17 #include "clang/Config/config.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Driver/ToolChain.h"
22 #include "clang/Frontend/ChainedDiagnosticConsumer.h"
23 #include "clang/Frontend/CompilerInvocation.h"
24 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
25 #include "clang/Frontend/TextDiagnosticPrinter.h"
26 #include "clang/Frontend/Utils.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/Option/ArgList.h"
31 #include "llvm/Option/OptTable.h"
32 #include "llvm/Option/Option.h"
33 #include "llvm/Support/BuryPointer.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/CrashRecoveryContext.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/FileSystem.h"
38 #include "llvm/Support/Host.h"
39 #include "llvm/Support/InitLLVM.h"
40 #include "llvm/Support/Path.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/Process.h"
43 #include "llvm/Support/Program.h"
44 #include "llvm/Support/Regex.h"
45 #include "llvm/Support/Signals.h"
46 #include "llvm/Support/StringSaver.h"
47 #include "llvm/Support/TargetSelect.h"
48 #include "llvm/Support/Timer.h"
49 #include "llvm/Support/raw_ostream.h"
52 #include <system_error>
53 using namespace clang;
54 using namespace clang::driver;
55 using namespace llvm::opt;
57 std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
58 if (!CanonicalPrefixes) {
59 SmallString<128> ExecutablePath(Argv0);
60 // Do a PATH lookup if Argv0 isn't a valid path.
61 if (!llvm::sys::fs::exists(ExecutablePath))
62 if (llvm::ErrorOr<std::string> P =
63 llvm::sys::findProgramByName(ExecutablePath))
65 return std::string(ExecutablePath.str());
68 // This just needs to be some symbol in the binary; C++ doesn't
69 // allow taking the address of ::main however.
70 void *P = (void*) (intptr_t) GetExecutablePath;
71 return llvm::sys::fs::getMainExecutable(Argv0, P);
74 static const char *GetStableCStr(std::set<std::string> &SavedStrings,
76 return SavedStrings.insert(std::string(S)).first->c_str();
79 /// ApplyQAOverride - Apply a list of edits to the input argument lists.
81 /// The input string is a space separate list of edits to perform,
82 /// they are applied in order to the input argument lists. Edits
83 /// should be one of the following forms:
85 /// '#': Silence information about the changes to the command line arguments.
87 /// '^': Add FOO as a new argument at the beginning of the command line.
89 /// '+': Add FOO as a new argument at the end of the command line.
91 /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
94 /// 'xOPTION': Removes all instances of the literal argument OPTION.
96 /// 'XOPTION': Removes all instances of the literal argument OPTION,
97 /// and the following argument.
99 /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
100 /// at the end of the command line.
102 /// \param OS - The stream to write edit information to.
103 /// \param Args - The vector of command line arguments.
104 /// \param Edit - The override command to perform.
105 /// \param SavedStrings - Set to use for storing string representations.
106 static void ApplyOneQAOverride(raw_ostream &OS,
107 SmallVectorImpl<const char*> &Args,
109 std::set<std::string> &SavedStrings) {
110 // This does not need to be efficient.
112 if (Edit[0] == '^') {
114 GetStableCStr(SavedStrings, Edit.substr(1));
115 OS << "### Adding argument " << Str << " at beginning\n";
116 Args.insert(Args.begin() + 1, Str);
117 } else if (Edit[0] == '+') {
119 GetStableCStr(SavedStrings, Edit.substr(1));
120 OS << "### Adding argument " << Str << " at end\n";
122 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
123 Edit.slice(2, Edit.size() - 1).contains('/')) {
124 StringRef MatchPattern = Edit.substr(2).split('/').first;
125 StringRef ReplPattern = Edit.substr(2).split('/').second;
126 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
128 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
129 // Ignore end-of-line response file markers
130 if (Args[i] == nullptr)
132 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
134 if (Repl != Args[i]) {
135 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
136 Args[i] = GetStableCStr(SavedStrings, Repl);
139 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
140 auto Option = Edit.substr(1);
141 for (unsigned i = 1; i < Args.size();) {
142 if (Option == Args[i]) {
143 OS << "### Deleting argument " << Args[i] << '\n';
144 Args.erase(Args.begin() + i);
145 if (Edit[0] == 'X') {
146 if (i < Args.size()) {
147 OS << "### Deleting argument " << Args[i] << '\n';
148 Args.erase(Args.begin() + i);
150 OS << "### Invalid X edit, end of command line!\n";
155 } else if (Edit[0] == 'O') {
156 for (unsigned i = 1; i < Args.size();) {
157 const char *A = Args[i];
158 // Ignore end-of-line response file markers
161 if (A[0] == '-' && A[1] == 'O' &&
163 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
164 ('0' <= A[2] && A[2] <= '9'))))) {
165 OS << "### Deleting argument " << Args[i] << '\n';
166 Args.erase(Args.begin() + i);
170 OS << "### Adding argument " << Edit << " at end\n";
171 Args.push_back(GetStableCStr(SavedStrings, '-' + Edit.str()));
173 OS << "### Unrecognized edit: " << Edit << "\n";
177 /// ApplyQAOverride - Apply a comma separate list of edits to the
178 /// input argument lists. See ApplyOneQAOverride.
179 static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
180 const char *OverrideStr,
181 std::set<std::string> &SavedStrings) {
182 raw_ostream *OS = &llvm::errs();
184 if (OverrideStr[0] == '#') {
189 *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
191 // This does not need to be efficient.
193 const char *S = OverrideStr;
195 const char *End = ::strchr(S, ' ');
199 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
206 extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
208 extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
210 extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
211 const char *Argv0, void *MainAddr);
213 static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
214 SmallVectorImpl<const char *> &ArgVector,
215 std::set<std::string> &SavedStrings) {
216 // Put target and mode arguments at the start of argument list so that
217 // arguments specified in command line could override them. Avoid putting
218 // them at index 0, as an option like '-cc1' must remain the first.
219 int InsertionPoint = 0;
220 if (ArgVector.size() > 0)
223 if (NameParts.DriverMode) {
224 // Add the mode flag to the arguments.
225 ArgVector.insert(ArgVector.begin() + InsertionPoint,
226 GetStableCStr(SavedStrings, NameParts.DriverMode));
229 if (NameParts.TargetIsValid) {
230 const char *arr[] = {"-target", GetStableCStr(SavedStrings,
231 NameParts.TargetPrefix)};
232 ArgVector.insert(ArgVector.begin() + InsertionPoint,
233 std::begin(arr), std::end(arr));
237 static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
238 SmallVectorImpl<const char *> &Opts) {
239 llvm::cl::TokenizeWindowsCommandLine(EnvValue, Saver, Opts);
240 // The first instance of '#' should be replaced with '=' in each option.
241 for (const char *Opt : Opts)
242 if (char *NumberSignPtr = const_cast<char *>(::strchr(Opt, '#')))
243 *NumberSignPtr = '=';
246 static void SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
247 auto CheckEnvVar = [](const char *EnvOptSet, const char *EnvOptFile,
248 std::string &OptFile) {
249 bool OptSet = !!::getenv(EnvOptSet);
251 if (const char *Var = ::getenv(EnvOptFile))
257 TheDriver.CCPrintOptions =
258 CheckEnvVar("CC_PRINT_OPTIONS", "CC_PRINT_OPTIONS_FILE",
259 TheDriver.CCPrintOptionsFilename);
260 TheDriver.CCPrintHeaders =
261 CheckEnvVar("CC_PRINT_HEADERS", "CC_PRINT_HEADERS_FILE",
262 TheDriver.CCPrintHeadersFilename);
263 TheDriver.CCLogDiagnostics =
264 CheckEnvVar("CC_LOG_DIAGNOSTICS", "CC_LOG_DIAGNOSTICS_FILE",
265 TheDriver.CCLogDiagnosticsFilename);
266 TheDriver.CCPrintProcessStats =
267 CheckEnvVar("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
268 TheDriver.CCPrintStatReportFilename);
271 static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
272 const std::string &Path) {
273 // If the clang binary happens to be named cl.exe for compatibility reasons,
274 // use clang-cl.exe as the prefix to avoid confusion between clang and MSVC.
275 StringRef ExeBasename(llvm::sys::path::stem(Path));
276 if (ExeBasename.equals_insensitive("cl"))
277 ExeBasename = "clang-cl";
278 DiagClient->setPrefix(std::string(ExeBasename));
281 static void SetInstallDir(SmallVectorImpl<const char *> &argv,
282 Driver &TheDriver, bool CanonicalPrefixes) {
283 // Attempt to find the original path used to invoke the driver, to determine
284 // the installed path. We do this manually, because we want to support that
285 // path being a symlink.
286 SmallString<128> InstalledPath(argv[0]);
288 // Do a PATH lookup, if there are no directory components.
289 if (llvm::sys::path::filename(InstalledPath) == InstalledPath)
290 if (llvm::ErrorOr<std::string> Tmp = llvm::sys::findProgramByName(
291 llvm::sys::path::filename(InstalledPath.str())))
292 InstalledPath = *Tmp;
294 // FIXME: We don't actually canonicalize this, we just make it absolute.
295 if (CanonicalPrefixes)
296 llvm::sys::fs::make_absolute(InstalledPath);
298 StringRef InstalledPathParent(llvm::sys::path::parent_path(InstalledPath));
299 if (llvm::sys::fs::exists(InstalledPathParent))
300 TheDriver.setInstalledDir(InstalledPathParent);
303 static int ExecuteCC1Tool(SmallVectorImpl<const char *> &ArgV) {
304 // If we call the cc1 tool from the clangDriver library (through
305 // Driver::CC1Main), we need to clean up the options usage count. The options
306 // are currently global, and they might have been used previously by the
308 llvm::cl::ResetAllOptionOccurrences();
310 llvm::BumpPtrAllocator A;
311 llvm::cl::ExpansionContext ECtx(A, llvm::cl::TokenizeGNUCommandLine);
312 ECtx.expandResponseFiles(ArgV);
313 StringRef Tool = ArgV[1];
314 void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
316 return cc1_main(makeArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
317 if (Tool == "-cc1as")
318 return cc1as_main(makeArrayRef(ArgV).slice(2), ArgV[0],
319 GetExecutablePathVP);
320 if (Tool == "-cc1gen-reproducer")
321 return cc1gen_reproducer_main(makeArrayRef(ArgV).slice(2), ArgV[0],
322 GetExecutablePathVP);
323 // Reject unknown tools.
324 llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
325 << "Valid tools include '-cc1' and '-cc1as'.\n";
329 int clang_main(int Argc, char **Argv) {
331 llvm::InitLLVM X(Argc, Argv);
332 llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
333 " and include the crash backtrace, preprocessed "
334 "source, and associated run script.\n");
335 SmallVector<const char *, 256> Args(Argv, Argv + Argc);
337 if (llvm::sys::Process::FixupStandardFileDescriptors())
340 llvm::InitializeAllTargets();
342 llvm::BumpPtrAllocator A;
343 llvm::StringSaver Saver(A);
345 // Parse response files using the GNU syntax, unless we're in CL mode. There
346 // are two ways to put clang in CL compatibility mode: Args[0] is either
347 // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
348 // command line parsing can't happen until after response file parsing, so we
349 // have to manually search for a --driver-mode=cl argument the hard way.
350 // Finally, our -cc1 tools don't care which tokenization mode we use because
351 // response files written by clang will tokenize the same way in either mode.
353 IsClangCL(getDriverMode(Args[0], llvm::makeArrayRef(Args).slice(1)));
354 enum { Default, POSIX, Windows } RSPQuoting = Default;
355 for (const char *F : Args) {
356 if (strcmp(F, "--rsp-quoting=posix") == 0)
358 else if (strcmp(F, "--rsp-quoting=windows") == 0)
359 RSPQuoting = Windows;
362 // Determines whether we want nullptr markers in Args to indicate response
363 // files end-of-lines. We only use this for the /LINK driver argument with
364 // clang-cl.exe on Windows.
365 bool MarkEOLs = ClangCLMode;
367 llvm::cl::TokenizerCallback Tokenizer;
368 if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode))
369 Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
371 Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
373 if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).startswith("-cc1"))
375 llvm::cl::ExpansionContext ECtx(A, Tokenizer);
376 ECtx.setMarkEOLs(MarkEOLs).expandResponseFiles(Args);
378 // Handle -cc1 integrated tools, even if -cc1 was expanded from a response
380 auto FirstArg = llvm::find_if(llvm::drop_begin(Args),
381 [](const char *A) { return A != nullptr; });
382 if (FirstArg != Args.end() && StringRef(*FirstArg).startswith("-cc1")) {
383 // If -cc1 came from a response file, remove the EOL sentinels.
385 auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
386 Args.resize(newEnd - Args.begin());
388 return ExecuteCC1Tool(Args);
391 // Handle options that need handling before the real command line parsing in
392 // Driver::BuildCompilation()
393 bool CanonicalPrefixes = true;
394 for (int i = 1, size = Args.size(); i < size; ++i) {
395 // Skip end-of-line response file markers
396 if (Args[i] == nullptr)
398 if (StringRef(Args[i]) == "-canonical-prefixes")
399 CanonicalPrefixes = true;
400 else if (StringRef(Args[i]) == "-no-canonical-prefixes")
401 CanonicalPrefixes = false;
404 // Handle CL and _CL_ which permits additional command line options to be
405 // prepended or appended.
407 // Arguments in "CL" are prepended.
408 llvm::Optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
410 SmallVector<const char *, 8> PrependedOpts;
411 getCLEnvVarOptions(OptCL.value(), Saver, PrependedOpts);
413 // Insert right after the program name to prepend to the argument list.
414 Args.insert(Args.begin() + 1, PrependedOpts.begin(), PrependedOpts.end());
416 // Arguments in "_CL_" are appended.
417 llvm::Optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
419 SmallVector<const char *, 8> AppendedOpts;
420 getCLEnvVarOptions(Opt_CL_.value(), Saver, AppendedOpts);
422 // Insert at the end of the argument list to append.
423 Args.append(AppendedOpts.begin(), AppendedOpts.end());
427 std::set<std::string> SavedStrings;
428 // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
430 if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
431 // FIXME: Driver shouldn't take extra initial argument.
432 ApplyQAOverride(Args, OverrideStr, SavedStrings);
435 std::string Path = GetExecutablePath(Args[0], CanonicalPrefixes);
437 // Whether the cc1 tool should be called inside the current process, or if we
438 // should spawn a new clang subprocess (old behavior).
439 // Not having an additional process saves some execution time of Windows,
440 // and makes debugging and profiling easier.
441 bool UseNewCC1Process = CLANG_SPAWN_CC1;
442 for (const char *Arg : Args)
443 UseNewCC1Process = llvm::StringSwitch<bool>(Arg)
444 .Case("-fno-integrated-cc1", true)
445 .Case("-fintegrated-cc1", false)
446 .Default(UseNewCC1Process);
448 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
449 CreateAndPopulateDiagOpts(Args);
451 TextDiagnosticPrinter *DiagClient
452 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
453 FixupDiagPrefixExeName(DiagClient, Path);
455 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
457 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
459 if (!DiagOpts->DiagnosticSerializationFile.empty()) {
460 auto SerializedConsumer =
461 clang::serialized_diags::create(DiagOpts->DiagnosticSerializationFile,
462 &*DiagOpts, /*MergeChildRecords=*/true);
463 Diags.setClient(new ChainedDiagnosticConsumer(
464 Diags.takeClient(), std::move(SerializedConsumer)));
467 ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
469 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
470 SetInstallDir(Args, TheDriver, CanonicalPrefixes);
471 auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Args[0]);
472 TheDriver.setTargetAndMode(TargetAndMode);
474 insertTargetAndModeArgs(TargetAndMode, Args, SavedStrings);
476 SetBackdoorDriverOutputsFromEnvVars(TheDriver);
478 if (!UseNewCC1Process) {
479 TheDriver.CC1Main = &ExecuteCC1Tool;
480 // Ensure the CC1Command actually catches cc1 crashes
481 llvm::CrashRecoveryContext::Enable();
484 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
486 Driver::ReproLevel ReproLevel = Driver::ReproLevel::OnCrash;
487 if (Arg *A = C->getArgs().getLastArg(options::OPT_gen_reproducer_eq)) {
488 auto Level = llvm::StringSwitch<Optional<Driver::ReproLevel>>(A->getValue())
489 .Case("off", Driver::ReproLevel::Off)
490 .Case("crash", Driver::ReproLevel::OnCrash)
491 .Case("error", Driver::ReproLevel::OnError)
492 .Case("always", Driver::ReproLevel::Always)
495 llvm::errs() << "Unknown value for " << A->getSpelling() << ": '"
496 << A->getValue() << "'\n";
501 if (!!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"))
502 ReproLevel = Driver::ReproLevel::Always;
505 bool IsCrash = false;
506 Driver::CommandStatus CommandStatus = Driver::CommandStatus::Ok;
507 // Pretend the first command failed if ReproStatus is Always.
508 const Command *FailingCommand = nullptr;
509 if (!C->getJobs().empty())
510 FailingCommand = &*C->getJobs().begin();
511 if (C && !C->containsError()) {
512 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
513 Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
515 for (const auto &P : FailingCommands) {
516 int CommandRes = P.first;
517 FailingCommand = P.second;
521 // If result status is < 0, then the driver command signalled an error.
522 // If result status is 70, then the driver command reported a fatal error.
523 // On Windows, abort will return an exit code of 3. In these cases,
524 // generate additional diagnostic information if possible.
525 IsCrash = CommandRes < 0 || CommandRes == 70;
527 IsCrash |= CommandRes == 3;
530 // When running in integrated-cc1 mode, the CrashRecoveryContext returns
531 // the same codes as if the program crashed. See section "Exit Status for
533 // https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
534 IsCrash |= CommandRes > 128;
537 IsCrash ? Driver::CommandStatus::Crash : Driver::CommandStatus::Error;
543 // Print the bug report message that would be printed if we did actually
544 // crash, but only if we're crashing due to FORCE_CLANG_DIAGNOSTICS_CRASH.
545 if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"))
546 llvm::dbgs() << llvm::getBugReportMsg();
547 if (FailingCommand != nullptr &&
548 TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel,
549 *C, *FailingCommand))
552 Diags.getClient()->finish();
554 if (!UseNewCC1Process && IsCrash) {
555 // When crashing in -fintegrated-cc1 mode, bury the timer pointers, because
556 // the internal linked list might point to already released stack frames.
557 llvm::BuryPointer(llvm::TimerGroup::aquireDefaultGroup());
559 // If any timers were active but haven't been destroyed yet, print their
560 // results now. This happens in -disable-free mode.
561 llvm::TimerGroup::printAll(llvm::errs());
562 llvm::TimerGroup::clearAll();
566 // Exit status should not be negative on Win32, unless abnormal termination.
567 // Once abnormal termination was caught, negative status should not be
573 // If we have multiple failing commands, we return the result of the first