xref: /freebsd/contrib/llvm-project/clang/tools/driver/driver.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
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 
58 using namespace clang;
59 using namespace clang::driver;
60 using namespace llvm::opt;
61 
GetExecutablePath(const char * Argv0,bool CanonicalPrefixes)62 std::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(ExecutablePath))
67       if (llvm::ErrorOr<std::string> P =
68               llvm::sys::findProgramByName(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, P);
77 }
78 
GetStableCStr(llvm::StringSet<> & SavedStrings,StringRef S)79 static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) {
80   return SavedStrings.insert(S).first->getKeyData();
81 }
82 
83 extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
84                     void *MainAddr);
85 extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
86                       void *MainAddr);
87 extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
88                                   const char *Argv0, void *MainAddr,
89                                   const llvm::ToolContext &);
90 
insertTargetAndModeArgs(const ParsedClangName & NameParts,SmallVectorImpl<const char * > & ArgVector,llvm::StringSet<> & SavedStrings)91 static 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(ArgVector.begin() + InsertionPoint,
104                      GetStableCStr(SavedStrings, NameParts.DriverMode));
105   }
106 
107   if (NameParts.TargetIsValid) {
108     const char *arr[] = {"-target", GetStableCStr(SavedStrings,
109                                                   NameParts.TargetPrefix)};
110     ArgVector.insert(ArgVector.begin() + InsertionPoint,
111                      std::begin(arr), std::end(arr));
112   }
113 }
114 
getCLEnvVarOptions(std::string & EnvValue,llvm::StringSaver & Saver,SmallVectorImpl<const char * > & Opts)115 static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
116                                SmallVectorImpl<const char *> &Opts) {
117   llvm::cl::TokenizeWindowsCommandLine(EnvValue, Saver, 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(Opt, '#')))
121       *NumberSignPtr = '=';
122 }
123 
124 template <class T>
checkEnvVar(const char * EnvOptSet,const char * EnvOptFile,std::string & OptFile)125 static T checkEnvVar(const char *EnvOptSet, const char *EnvOptFile,
126                      std::string &OptFile) {
127   const char *Str = ::getenv(EnvOptSet);
128   if (!Str)
129     return T{};
130 
131   T OptVal = Str;
132   if (const char *Var = ::getenv(EnvOptFile))
133     OptFile = Var;
134   return OptVal;
135 }
136 
SetBackdoorDriverOutputsFromEnvVars(Driver & TheDriver)137 static bool SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
138   TheDriver.CCPrintOptions =
139       checkEnvVar<bool>("CC_PRINT_OPTIONS", "CC_PRINT_OPTIONS_FILE",
140                         TheDriver.CCPrintOptionsFilename);
141   if (checkEnvVar<bool>("CC_PRINT_HEADERS", "CC_PRINT_HEADERS_FILE",
142                         TheDriver.CCPrintHeadersFilename)) {
143     TheDriver.CCPrintHeadersFormat = HIFMT_Textual;
144     TheDriver.CCPrintHeadersFiltering = HIFIL_None;
145   } else {
146     std::string EnvVar = checkEnvVar<std::string>(
147         "CC_PRINT_HEADERS_FORMAT", "CC_PRINT_HEADERS_FILE",
148         TheDriver.CCPrintHeadersFilename);
149     if (!EnvVar.empty()) {
150       TheDriver.CCPrintHeadersFormat =
151           stringToHeaderIncludeFormatKind(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("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(FilteringStr, 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>("CC_LOG_DIAGNOSTICS", "CC_LOG_DIAGNOSTICS_FILE",
185                         TheDriver.CCLogDiagnosticsFilename);
186   TheDriver.CCPrintProcessStats =
187       checkEnvVar<bool>("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
188                         TheDriver.CCPrintStatReportFilename);
189   TheDriver.CCPrintInternalStats =
190       checkEnvVar<bool>("CC_PRINT_INTERNAL_STAT", "CC_PRINT_INTERNAL_STAT_FILE",
191                         TheDriver.CCPrintInternalStatReportFilename);
192 
193   return true;
194 }
195 
FixupDiagPrefixExeName(TextDiagnosticPrinter * DiagClient,const std::string & Path)196 static 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));
201   if (ExeBasename.equals_insensitive("cl"))
202     ExeBasename = "clang-cl";
203   DiagClient->setPrefix(std::string(ExeBasename));
204 }
205 
ExecuteCC1Tool(SmallVectorImpl<const char * > & ArgV,const llvm::ToolContext & ToolContext)206 static 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)) {
217     llvm::errs() << toString(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(ArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
224   if (Tool == "-cc1as")
225     return cc1as_main(ArrayRef(ArgV).slice(2), ArgV[0], GetExecutablePathVP);
226   if (Tool == "-cc1gen-reproducer")
227     return cc1gen_reproducer_main(ArrayRef(ArgV).slice(2), ArgV[0],
228                                   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 
clang_main(int Argc,char ** Argv,const llvm::ToolContext & ToolContext)236 int 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(getDriverMode(ProgName, llvm::ArrayRef(Args).slice(1)));
256 
257   if (llvm::Error Err = expandResponseFiles(Args, ClangCLMode, A)) {
258     llvm::errs() << toString(std::move(Err)) << '\n';
259     return 1;
260   }
261 
262   // Handle -cc1 integrated tools.
263   if (Args.size() >= 2 && StringRef(Args[1]).starts_with("-cc1"))
264     return ExecuteCC1Tool(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("CL");
284     if (OptCL) {
285       SmallVector<const char *, 8> PrependedOpts;
286       getCLEnvVarOptions(*OptCL, Saver, PrependedOpts);
287 
288       // Insert right after the program name to prepend to the argument list.
289       Args.insert(Args.begin() + 1, PrependedOpts.begin(), PrependedOpts.end());
290     }
291     // Arguments in "_CL_" are appended.
292     std::optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
293     if (Opt_CL_) {
294       SmallVector<const char *, 8> AppendedOpts;
295       getCLEnvVarOptions(*Opt_CL_, Saver, AppendedOpts);
296 
297       // Insert at the end of the argument list to append.
298       Args.append(AppendedOpts.begin(), 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("CCC_OVERRIDE_OPTIONS")) {
306     // FIXME: Driver shouldn't take extra initial argument.
307     driver::applyOverrideOptions(Args, OverrideStr, SavedStrings,
308                                  "CCC_OVERRIDE_OPTIONS", &llvm::errs());
309   }
310 
311   std::string Path = GetExecutablePath(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("-fno-integrated-cc1", true)
321                            .Case("-fintegrated-cc1", false)
322                            .Default(UseNewCC1Process);
323 
324   std::unique_ptr<DiagnosticOptions> DiagOpts = CreateAndPopulateDiagOpts(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, 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(DiagOpts->DiagnosticSerializationFile,
341                                         *DiagOpts, /*MergeChildRecords=*/true);
342     Diags.setClient(new ChainedDiagnosticConsumer(
343         Diags.takeClient(), std::move(SerializedConsumer)));
344   }
345 
346   auto VFS = llvm::vfs::getRealFileSystem();
347   ProcessWarningOptions(Diags, *DiagOpts, *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(TargetAndMode, 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("off", Driver::ReproLevel::Off)
383             .Case("crash", Driver::ReproLevel::OnCrash)
384             .Case("error", Driver::ReproLevel::OnError)
385             .Case("always", Driver::ReproLevel::Always)
386             .Default(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("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, 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("FORCE_CLANG_DIAGNOSTICS_CRASH"))
439     llvm::dbgs() << llvm::getBugReportMsg();
440   if (FailingCommand != nullptr &&
441     TheDriver.maybeGenerateCompilationDiagnostics(CommandStatus, ReproLevel,
442                                                   *C, *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(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(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