Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/driver/driver.cpp
Line
Count
Source (jump to first uncovered line)
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/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"
50
#include <memory>
51
#include <set>
52
#include <system_error>
53
using namespace clang;
54
using namespace clang::driver;
55
using namespace llvm::opt;
56
57
19.0k
std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
58
19.0k
  if (!CanonicalPrefixes) {
59
1.08k
    SmallString<128> ExecutablePath(Argv0);
60
    // Do a PATH lookup if Argv0 isn't a valid path.
61
1.08k
    if (!llvm::sys::fs::exists(ExecutablePath))
62
0
      if (llvm::ErrorOr<std::string> P =
63
0
              llvm::sys::findProgramByName(ExecutablePath))
64
0
        ExecutablePath = *P;
65
1.08k
    return std::string(ExecutablePath.str());
66
1.08k
  }
67
68
  // This just needs to be some symbol in the binary; C++ doesn't
69
  // allow taking the address of ::main however.
70
17.9k
  void *P = (void*) (intptr_t) GetExecutablePath;
71
17.9k
  return llvm::sys::fs::getMainExecutable(Argv0, P);
72
19.0k
}
73
74
static const char *GetStableCStr(std::set<std::string> &SavedStrings,
75
113
                                 StringRef S) {
76
113
  return SavedStrings.insert(std::string(S)).first->c_str();
77
113
}
78
79
/// ApplyQAOverride - Apply a list of edits to the input argument lists.
80
///
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:
84
///
85
///  '#': Silence information about the changes to the command line arguments.
86
///
87
///  '^': Add FOO as a new argument at the beginning of the command line.
88
///
89
///  '+': Add FOO as a new argument at the end of the command line.
90
///
91
///  's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
92
///  line.
93
///
94
///  'xOPTION': Removes all instances of the literal argument OPTION.
95
///
96
///  'XOPTION': Removes all instances of the literal argument OPTION,
97
///  and the following argument.
98
///
99
///  'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
100
///  at the end of the command line.
101
///
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,
108
                               StringRef Edit,
109
16
                               std::set<std::string> &SavedStrings) {
110
  // This does not need to be efficient.
111
112
16
  if (Edit[0] == '^') {
113
1
    const char *Str =
114
1
      GetStableCStr(SavedStrings, Edit.substr(1));
115
1
    OS << "### Adding argument " << Str << " at beginning\n";
116
1
    Args.insert(Args.begin() + 1, Str);
117
15
  } else if (Edit[0] == '+') {
118
11
    const char *Str =
119
11
      GetStableCStr(SavedStrings, Edit.substr(1));
120
11
    OS << "### Adding argument " << Str << " at end\n";
121
11
    Args.push_back(Str);
122
11
  } else 
if (4
Edit[0] == 's'4
&&
Edit[1] == '/'0
&&
Edit.endswith("/")0
&&
123
4
             
Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos0
) {
124
0
    StringRef MatchPattern = Edit.substr(2).split('/').first;
125
0
    StringRef ReplPattern = Edit.substr(2).split('/').second;
126
0
    ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
127
128
0
    for (unsigned i = 1, e = Args.size(); i != e; ++i) {
129
      // Ignore end-of-line response file markers
130
0
      if (Args[i] == nullptr)
131
0
        continue;
132
0
      std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
133
134
0
      if (Repl != Args[i]) {
135
0
        OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
136
0
        Args[i] = GetStableCStr(SavedStrings, Repl);
137
0
      }
138
0
    }
139
4
  } else if (Edit[0] == 'x' || 
Edit[0] == 'X'2
) {
140
3
    auto Option = Edit.substr(1);
141
34
    for (unsigned i = 1; i < Args.size();) {
142
31
      if (Option == Args[i]) {
143
4
        OS << "### Deleting argument " << Args[i] << '\n';
144
4
        Args.erase(Args.begin() + i);
145
4
        if (Edit[0] == 'X') {
146
1
          if (i < Args.size()) {
147
1
            OS << "### Deleting argument " << Args[i] << '\n';
148
1
            Args.erase(Args.begin() + i);
149
1
          } else
150
0
            OS << "### Invalid X edit, end of command line!\n";
151
1
        }
152
4
      } else
153
27
        ++i;
154
31
    }
155
3
  } else 
if (1
Edit[0] == 'O'1
) {
156
11
    for (unsigned i = 1; i < Args.size();) {
157
10
      const char *A = Args[i];
158
      // Ignore end-of-line response file markers
159
10
      if (A == nullptr)
160
0
        continue;
161
10
      if (A[0] == '-' && 
A[1] == 'O'8
&&
162
10
          
(7
A[2] == '\0'7
||
163
7
           
(6
A[3] == '\0'6
&&
(5
A[2] == 's'5
||
A[2] == 'z'4
||
164
6
                             
(3
'0' <= A[2]3
&&
A[2] <= '9'3
))))) {
165
6
        OS << "### Deleting argument " << Args[i] << '\n';
166
6
        Args.erase(Args.begin() + i);
167
6
      } else
168
4
        ++i;
169
10
    }
170
1
    OS << "### Adding argument " << Edit << " at end\n";
171
1
    Args.push_back(GetStableCStr(SavedStrings, '-' + Edit.str()));
172
1
  } else {
173
0
    OS << "### Unrecognized edit: " << Edit << "\n";
174
0
  }
175
16
}
176
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
4
                            std::set<std::string> &SavedStrings) {
182
4
  raw_ostream *OS = &llvm::errs();
183
184
4
  if (OverrideStr[0] == '#') {
185
1
    ++OverrideStr;
186
1
    OS = &llvm::nulls();
187
1
  }
188
189
4
  *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
190
191
  // This does not need to be efficient.
192
193
4
  const char *S = OverrideStr;
194
21
  while (*S) {
195
17
    const char *End = ::strchr(S, ' ');
196
17
    if (!End)
197
3
      End = S + strlen(S);
198
17
    if (End != S)
199
16
      ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
200
17
    S = End;
201
17
    if (*S != '\0')
202
14
      ++S;
203
17
  }
204
4
}
205
206
extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
207
                    void *MainAddr);
208
extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
209
                      void *MainAddr);
210
extern int cc1gen_reproducer_main(ArrayRef<const char *> Argv,
211
                                  const char *Argv0, void *MainAddr);
212
213
static void insertTargetAndModeArgs(const ParsedClangName &NameParts,
214
                                    SmallVectorImpl<const char *> &ArgVector,
215
19.0k
                                    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
19.0k
  int InsertionPoint = 0;
220
19.0k
  if (ArgVector.size() > 0)
221
19.0k
    ++InsertionPoint;
222
223
19.0k
  if (NameParts.DriverMode) {
224
    // Add the mode flag to the arguments.
225
90
    ArgVector.insert(ArgVector.begin() + InsertionPoint,
226
90
                     GetStableCStr(SavedStrings, NameParts.DriverMode));
227
90
  }
228
229
19.0k
  if (NameParts.TargetIsValid) {
230
10
    const char *arr[] = {"-target", GetStableCStr(SavedStrings,
231
10
                                                  NameParts.TargetPrefix)};
232
10
    ArgVector.insert(ArgVector.begin() + InsertionPoint,
233
10
                     std::begin(arr), std::end(arr));
234
10
  }
235
19.0k
}
236
237
static void getCLEnvVarOptions(std::string &EnvValue, llvm::StringSaver &Saver,
238
3
                               SmallVectorImpl<const char *> &Opts) {
239
3
  llvm::cl::TokenizeWindowsCommandLine(EnvValue, Saver, Opts);
240
  // The first instance of '#' should be replaced with '=' in each option.
241
3
  for (const char *Opt : Opts)
242
5
    if (char *NumberSignPtr = const_cast<char *>(::strchr(Opt, '#')))
243
0
      *NumberSignPtr = '=';
244
3
}
245
246
19.0k
static void SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
247
19.0k
  auto CheckEnvVar = [](const char *EnvOptSet, const char *EnvOptFile,
248
76.0k
                        std::string &OptFile) {
249
76.0k
    bool OptSet = !!::getenv(EnvOptSet);
250
76.0k
    if (OptSet) {
251
16
      if (const char *Var = ::getenv(EnvOptFile))
252
4
        OptFile = Var;
253
16
    }
254
76.0k
    return OptSet;
255
76.0k
  };
256
257
19.0k
  TheDriver.CCPrintOptions =
258
19.0k
      CheckEnvVar("CC_PRINT_OPTIONS", "CC_PRINT_OPTIONS_FILE",
259
19.0k
                  TheDriver.CCPrintOptionsFilename);
260
19.0k
  TheDriver.CCPrintHeaders =
261
19.0k
      CheckEnvVar("CC_PRINT_HEADERS", "CC_PRINT_HEADERS_FILE",
262
19.0k
                  TheDriver.CCPrintHeadersFilename);
263
19.0k
  TheDriver.CCLogDiagnostics =
264
19.0k
      CheckEnvVar("CC_LOG_DIAGNOSTICS", "CC_LOG_DIAGNOSTICS_FILE",
265
19.0k
                  TheDriver.CCLogDiagnosticsFilename);
266
19.0k
  TheDriver.CCPrintProcessStats =
267
19.0k
      CheckEnvVar("CC_PRINT_PROC_STAT", "CC_PRINT_PROC_STAT_FILE",
268
19.0k
                  TheDriver.CCPrintStatReportFilename);
269
19.0k
}
270
271
static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
272
19.0k
                                   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
19.0k
  StringRef ExeBasename(llvm::sys::path::stem(Path));
276
19.0k
  if (ExeBasename.equals_insensitive("cl"))
277
0
    ExeBasename = "clang-cl";
278
19.0k
  DiagClient->setPrefix(std::string(ExeBasename));
279
19.0k
}
280
281
static void SetInstallDir(SmallVectorImpl<const char *> &argv,
282
19.0k
                          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
19.0k
  SmallString<128> InstalledPath(argv[0]);
287
288
  // Do a PATH lookup, if there are no directory components.
289
19.0k
  if (llvm::sys::path::filename(InstalledPath) == InstalledPath)
290
3
    if (llvm::ErrorOr<std::string> Tmp = llvm::sys::findProgramByName(
291
3
            llvm::sys::path::filename(InstalledPath.str())))
292
3
      InstalledPath = *Tmp;
293
294
  // FIXME: We don't actually canonicalize this, we just make it absolute.
295
19.0k
  if (CanonicalPrefixes)
296
17.9k
    llvm::sys::fs::make_absolute(InstalledPath);
297
298
19.0k
  StringRef InstalledPathParent(llvm::sys::path::parent_path(InstalledPath));
299
19.0k
  if (llvm::sys::fs::exists(InstalledPathParent))
300
19.0k
    TheDriver.setInstalledDir(InstalledPathParent);
301
19.0k
}
302
303
40.6k
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
307
  // driver.
308
40.6k
  llvm::cl::ResetAllOptionOccurrences();
309
310
40.6k
  llvm::BumpPtrAllocator A;
311
40.6k
  llvm::StringSaver Saver(A);
312
40.6k
  llvm::cl::ExpandResponseFiles(Saver, &llvm::cl::TokenizeGNUCommandLine, ArgV,
313
40.6k
                                /*MarkEOLs=*/false);
314
40.6k
  StringRef Tool = ArgV[1];
315
40.6k
  void *GetExecutablePathVP = (void *)(intptr_t)GetExecutablePath;
316
40.6k
  if (Tool == "-cc1")
317
40.5k
    return cc1_main(makeArrayRef(ArgV).slice(1), ArgV[0], GetExecutablePathVP);
318
56
  if (Tool == "-cc1as")
319
52
    return cc1as_main(makeArrayRef(ArgV).slice(2), ArgV[0],
320
52
                      GetExecutablePathVP);
321
4
  if (Tool == "-cc1gen-reproducer")
322
3
    return cc1gen_reproducer_main(makeArrayRef(ArgV).slice(2), ArgV[0],
323
3
                                  GetExecutablePathVP);
324
  // Reject unknown tools.
325
1
  llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
326
1
               << "Valid tools include '-cc1' and '-cc1as'.\n";
327
1
  return 1;
328
4
}
329
330
53.9k
int main(int Argc, const char **Argv) {
331
53.9k
  noteBottomOfStack();
332
53.9k
  llvm::InitLLVM X(Argc, Argv);
333
53.9k
  llvm::setBugReportMsg("PLEASE submit a bug report to " BUG_REPORT_URL
334
53.9k
                        " and include the crash backtrace, preprocessed "
335
53.9k
                        "source, and associated run script.\n");
336
53.9k
  SmallVector<const char *, 256> Args(Argv, Argv + Argc);
337
338
53.9k
  if (llvm::sys::Process::FixupStandardFileDescriptors())
339
0
    return 1;
340
341
53.9k
  llvm::InitializeAllTargets();
342
343
53.9k
  llvm::BumpPtrAllocator A;
344
53.9k
  llvm::StringSaver Saver(A);
345
346
  // Parse response files using the GNU syntax, unless we're in CL mode. There
347
  // are two ways to put clang in CL compatibility mode: Args[0] is either
348
  // clang-cl or cl, or --driver-mode=cl is on the command line. The normal
349
  // command line parsing can't happen until after response file parsing, so we
350
  // have to manually search for a --driver-mode=cl argument the hard way.
351
  // Finally, our -cc1 tools don't care which tokenization mode we use because
352
  // response files written by clang will tokenize the same way in either mode.
353
53.9k
  bool ClangCLMode =
354
53.9k
      IsClangCL(getDriverMode(Args[0], llvm::makeArrayRef(Args).slice(1)));
355
53.9k
  enum { Default, POSIX, Windows } RSPQuoting = Default;
356
746k
  for (const char *F : Args) {
357
746k
    if (strcmp(F, "--rsp-quoting=posix") == 0)
358
0
      RSPQuoting = POSIX;
359
746k
    else if (strcmp(F, "--rsp-quoting=windows") == 0)
360
2
      RSPQuoting = Windows;
361
746k
  }
362
363
  // Determines whether we want nullptr markers in Args to indicate response
364
  // files end-of-lines. We only use this for the /LINK driver argument with
365
  // clang-cl.exe on Windows.
366
53.9k
  bool MarkEOLs = ClangCLMode;
367
368
53.9k
  llvm::cl::TokenizerCallback Tokenizer;
369
53.9k
  if (RSPQuoting == Windows || 
(53.9k
RSPQuoting == Default53.9k
&&
ClangCLMode53.9k
))
370
530
    Tokenizer = &llvm::cl::TokenizeWindowsCommandLine;
371
53.4k
  else
372
53.4k
    Tokenizer = &llvm::cl::TokenizeGNUCommandLine;
373
374
53.9k
  if (MarkEOLs && 
Args.size() > 1528
&&
StringRef(Args[1]).startswith("-cc1")528
)
375
0
    MarkEOLs = false;
376
53.9k
  llvm::cl::ExpandResponseFiles(Saver, Tokenizer, Args, MarkEOLs);
377
378
  // Handle -cc1 integrated tools, even if -cc1 was expanded from a response
379
  // file.
380
53.9k
  auto FirstArg = std::find_if(Args.begin() + 1, Args.end(),
381
53.9k
                               [](const char *A) { return A != nullptr; });
382
53.9k
  if (FirstArg != Args.end() && StringRef(*FirstArg).startswith("-cc1")) {
383
    // If -cc1 came from a response file, remove the EOL sentinels.
384
34.9k
    if (MarkEOLs) {
385
0
      auto newEnd = std::remove(Args.begin(), Args.end(), nullptr);
386
0
      Args.resize(newEnd - Args.begin());
387
0
    }
388
34.9k
    return ExecuteCC1Tool(Args);
389
34.9k
  }
390
391
  // Handle options that need handling before the real command line parsing in
392
  // Driver::BuildCompilation()
393
19.0k
  bool CanonicalPrefixes = true;
394
544k
  for (int i = 1, size = Args.size(); i < size; 
++i525k
) {
395
    // Skip end-of-line response file markers
396
525k
    if (Args[i] == nullptr)
397
2
      continue;
398
525k
    if (StringRef(Args[i]) == "-canonical-prefixes")
399
2
      CanonicalPrefixes = true;
400
525k
    else if (StringRef(Args[i]) == "-no-canonical-prefixes")
401
1.08k
      CanonicalPrefixes = false;
402
525k
  }
403
404
  // Handle CL and _CL_ which permits additional command line options to be
405
  // prepended or appended.
406
19.0k
  if (ClangCLMode) {
407
    // Arguments in "CL" are prepended.
408
528
    llvm::Optional<std::string> OptCL = llvm::sys::Process::GetEnv("CL");
409
528
    if (OptCL.hasValue()) {
410
2
      SmallVector<const char *, 8> PrependedOpts;
411
2
      getCLEnvVarOptions(OptCL.getValue(), Saver, PrependedOpts);
412
413
      // Insert right after the program name to prepend to the argument list.
414
2
      Args.insert(Args.begin() + 1, PrependedOpts.begin(), PrependedOpts.end());
415
2
    }
416
    // Arguments in "_CL_" are appended.
417
528
    llvm::Optional<std::string> Opt_CL_ = llvm::sys::Process::GetEnv("_CL_");
418
528
    if (Opt_CL_.hasValue()) {
419
1
      SmallVector<const char *, 8> AppendedOpts;
420
1
      getCLEnvVarOptions(Opt_CL_.getValue(), Saver, AppendedOpts);
421
422
      // Insert at the end of the argument list to append.
423
1
      Args.append(AppendedOpts.begin(), AppendedOpts.end());
424
1
    }
425
528
  }
426
427
19.0k
  std::set<std::string> SavedStrings;
428
  // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
429
  // scenes.
430
19.0k
  if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
431
    // FIXME: Driver shouldn't take extra initial argument.
432
4
    ApplyQAOverride(Args, OverrideStr, SavedStrings);
433
4
  }
434
435
19.0k
  std::string Path = GetExecutablePath(Args[0], CanonicalPrefixes);
436
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
19.0k
  bool UseNewCC1Process = CLANG_SPAWN_CC1;
442
19.0k
  for (const char *Arg : Args)
443
544k
    UseNewCC1Process = llvm::StringSwitch<bool>(Arg)
444
544k
                           .Case("-fno-integrated-cc1", true)
445
544k
                           .Case("-fintegrated-cc1", false)
446
544k
                           .Default(UseNewCC1Process);
447
448
19.0k
  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
449
19.0k
      CreateAndPopulateDiagOpts(Args);
450
451
19.0k
  TextDiagnosticPrinter *DiagClient
452
19.0k
    = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
453
19.0k
  FixupDiagPrefixExeName(DiagClient, Path);
454
455
19.0k
  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
456
457
19.0k
  DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
458
459
19.0k
  if (!DiagOpts->DiagnosticSerializationFile.empty()) {
460
12
    auto SerializedConsumer =
461
12
        clang::serialized_diags::create(DiagOpts->DiagnosticSerializationFile,
462
12
                                        &*DiagOpts, /*MergeChildRecords=*/true);
463
12
    Diags.setClient(new ChainedDiagnosticConsumer(
464
12
        Diags.takeClient(), std::move(SerializedConsumer)));
465
12
  }
466
467
19.0k
  ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
468
469
19.0k
  Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
470
19.0k
  SetInstallDir(Args, TheDriver, CanonicalPrefixes);
471
19.0k
  auto TargetAndMode = ToolChain::getTargetAndModeFromProgramName(Args[0]);
472
19.0k
  TheDriver.setTargetAndMode(TargetAndMode);
473
474
19.0k
  insertTargetAndModeArgs(TargetAndMode, Args, SavedStrings);
475
476
19.0k
  SetBackdoorDriverOutputsFromEnvVars(TheDriver);
477
478
19.0k
  if (!UseNewCC1Process) {
479
18.9k
    TheDriver.CC1Main = &ExecuteCC1Tool;
480
    // Ensure the CC1Command actually catches cc1 crashes
481
18.9k
    llvm::CrashRecoveryContext::Enable();
482
18.9k
  }
483
484
19.0k
  std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(Args));
485
19.0k
  int Res = 1;
486
19.0k
  bool IsCrash = false;
487
19.0k
  if (C && !C->containsError()) {
488
18.9k
    SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
489
18.9k
    Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
490
491
    // Force a crash to test the diagnostics.
492
18.9k
    if (TheDriver.GenReproducer) {
493
14
      Diags.Report(diag::err_drv_force_crash)
494
14
        << !::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH");
495
496
      // Pretend that every command failed.
497
14
      FailingCommands.clear();
498
14
      for (const auto &J : C->getJobs())
499
14
        if (const Command *C = dyn_cast<Command>(&J))
500
14
          FailingCommands.push_back(std::make_pair(-1, C));
501
502
      // Print the bug report message that would be printed if we did actually
503
      // crash, but only if we're crashing due to FORCE_CLANG_DIAGNOSTICS_CRASH.
504
14
      if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH"))
505
13
        llvm::dbgs() << llvm::getBugReportMsg();
506
14
    }
507
508
18.9k
    for (const auto &P : FailingCommands) {
509
307
      int CommandRes = P.first;
510
307
      const Command *FailingCommand = P.second;
511
307
      if (!Res)
512
296
        Res = CommandRes;
513
514
      // If result status is < 0, then the driver command signalled an error.
515
      // If result status is 70, then the driver command reported a fatal error.
516
      // On Windows, abort will return an exit code of 3.  In these cases,
517
      // generate additional diagnostic information if possible.
518
307
      IsCrash = CommandRes < 0 || 
CommandRes == 70293
;
519
#ifdef _WIN32
520
      IsCrash |= CommandRes == 3;
521
#endif
522
307
#if LLVM_ON_UNIX
523
      // When running in integrated-cc1 mode, the CrashRecoveryContext returns
524
      // the same codes as if the program crashed. See section "Exit Status for
525
      // Commands":
526
      // https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html
527
307
      IsCrash |= CommandRes > 128;
528
307
#endif
529
307
      if (IsCrash) {
530
27
        TheDriver.generateCompilationDiagnostics(*C, *FailingCommand);
531
27
        break;
532
27
      }
533
307
    }
534
18.9k
  }
535
536
19.0k
  Diags.getClient()->finish();
537
538
19.0k
  if (!UseNewCC1Process && 
IsCrash18.9k
) {
539
    // When crashing in -fintegrated-cc1 mode, bury the timer pointers, because
540
    // the internal linked list might point to already released stack frames.
541
27
    llvm::BuryPointer(llvm::TimerGroup::aquireDefaultGroup());
542
18.9k
  } else {
543
    // If any timers were active but haven't been destroyed yet, print their
544
    // results now.  This happens in -disable-free mode.
545
18.9k
    llvm::TimerGroup::printAll(llvm::errs());
546
18.9k
    llvm::TimerGroup::clearAll();
547
18.9k
  }
548
549
#ifdef _WIN32
550
  // Exit status should not be negative on Win32, unless abnormal termination.
551
  // Once abnormal termination was caught, negative status should not be
552
  // propagated.
553
  if (Res < 0)
554
    Res = 1;
555
#endif
556
557
  // If we have multiple failing commands, we return the result of the first
558
  // failing command.
559
19.0k
  return Res;
560
53.9k
}