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