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