1 //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the entry point to the clang driver; it is a thin wrapper
11 // for functionality in the Driver clang library.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/DiagnosticOptions.h"
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/Driver.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Driver/Options.h"
21 #include "clang/Frontend/ChainedDiagnosticConsumer.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/SerializedDiagnosticPrinter.h"
24 #include "clang/Frontend/TextDiagnosticPrinter.h"
25 #include "clang/Frontend/Utils.h"
26 #include "llvm/ADT/ArrayRef.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/Option/ArgList.h"
32 #include "llvm/Option/OptTable.h"
33 #include "llvm/Option/Option.h"
34 #include "llvm/Support/CommandLine.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/MemoryBuffer.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/TargetRegistry.h"
47 #include "llvm/Support/TargetSelect.h"
48 #include "llvm/Support/Timer.h"
49 #include "llvm/Support/raw_ostream.h"
51 #include <system_error>
52 using namespace clang;
53 using namespace clang::driver;
54 using namespace llvm::opt;
56 std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
57 if (!CanonicalPrefixes)
60 // This just needs to be some symbol in the binary; C++ doesn't
61 // allow taking the address of ::main however.
62 void *P = (void*) (intptr_t) GetExecutablePath;
63 return llvm::sys::fs::getMainExecutable(Argv0, P);
66 static const char *GetStableCStr(std::set<std::string> &SavedStrings,
68 return SavedStrings.insert(S).first->c_str();
71 /// ApplyQAOverride - Apply a list of edits to the input argument lists.
73 /// The input string is a space separate list of edits to perform,
74 /// they are applied in order to the input argument lists. Edits
75 /// should be one of the following forms:
77 /// '#': Silence information about the changes to the command line arguments.
79 /// '^': Add FOO as a new argument at the beginning of the command line.
81 /// '+': Add FOO as a new argument at the end of the command line.
83 /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
86 /// 'xOPTION': Removes all instances of the literal argument OPTION.
88 /// 'XOPTION': Removes all instances of the literal argument OPTION,
89 /// and the following argument.
91 /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
92 /// at the end of the command line.
94 /// \param OS - The stream to write edit information to.
95 /// \param Args - The vector of command line arguments.
96 /// \param Edit - The override command to perform.
97 /// \param SavedStrings - Set to use for storing string representations.
98 static void ApplyOneQAOverride(raw_ostream &OS,
99 SmallVectorImpl<const char*> &Args,
101 std::set<std::string> &SavedStrings) {
102 // This does not need to be efficient.
104 if (Edit[0] == '^') {
106 GetStableCStr(SavedStrings, Edit.substr(1));
107 OS << "### Adding argument " << Str << " at beginning\n";
108 Args.insert(Args.begin() + 1, Str);
109 } else if (Edit[0] == '+') {
111 GetStableCStr(SavedStrings, Edit.substr(1));
112 OS << "### Adding argument " << Str << " at end\n";
114 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
115 Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) {
116 StringRef MatchPattern = Edit.substr(2).split('/').first;
117 StringRef ReplPattern = Edit.substr(2).split('/').second;
118 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
120 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
121 // Ignore end-of-line response file markers
122 if (Args[i] == nullptr)
124 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
126 if (Repl != Args[i]) {
127 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
128 Args[i] = GetStableCStr(SavedStrings, Repl);
131 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
132 std::string Option = Edit.substr(1, std::string::npos);
133 for (unsigned i = 1; i < Args.size();) {
134 if (Option == Args[i]) {
135 OS << "### Deleting argument " << Args[i] << '\n';
136 Args.erase(Args.begin() + i);
137 if (Edit[0] == 'X') {
138 if (i < Args.size()) {
139 OS << "### Deleting argument " << Args[i] << '\n';
140 Args.erase(Args.begin() + i);
142 OS << "### Invalid X edit, end of command line!\n";
147 } else if (Edit[0] == 'O') {
148 for (unsigned i = 1; i < Args.size();) {
149 const char *A = Args[i];
150 // Ignore end-of-line response file markers
153 if (A[0] == '-' && A[1] == 'O' &&
155 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
156 ('0' <= A[2] && A[2] <= '9'))))) {
157 OS << "### Deleting argument " << Args[i] << '\n';
158 Args.erase(Args.begin() + i);
162 OS << "### Adding argument " << Edit << " at end\n";
163 Args.push_back(GetStableCStr(SavedStrings, '-' + Edit.str()));
165 OS << "### Unrecognized edit: " << Edit << "\n";
169 /// ApplyQAOverride - Apply a comma separate list of edits to the
170 /// input argument lists. See ApplyOneQAOverride.
171 static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
172 const char *OverrideStr,
173 std::set<std::string> &SavedStrings) {
174 raw_ostream *OS = &llvm::errs();
176 if (OverrideStr[0] == '#') {
181 *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n";
183 // This does not need to be efficient.
185 const char *S = OverrideStr;
187 const char *End = ::strchr(S, ' ');
191 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
198 extern int cc1_main(ArrayRef<const char *> Argv, const char *Argv0,
200 extern int cc1as_main(ArrayRef<const char *> Argv, const char *Argv0,
203 struct DriverSuffix {
205 const char *ModeFlag;
208 static const DriverSuffix *FindDriverSuffix(StringRef ProgName) {
209 // A list of known driver suffixes. Suffixes are compared against the
210 // program name in order. If there is a match, the frontend type if updated as
211 // necessary by applying the ModeFlag.
212 static const DriverSuffix DriverSuffixes[] = {
214 {"clang++", "--driver-mode=g++"},
215 {"clang-c++", "--driver-mode=g++"},
216 {"clang-CC", "--driver-mode=g++"},
217 {"clang-cc", nullptr},
218 {"clang-cpp", "--driver-mode=cpp"},
219 {"clang-g++", "--driver-mode=g++"},
220 {"clang-gcc", nullptr},
221 {"clang-cl", "--driver-mode=cl"},
222 {"CC", "--driver-mode=g++"},
224 {"cpp", "--driver-mode=cpp"},
225 {"cl", "--driver-mode=cl"},
226 {"++", "--driver-mode=g++"},
229 for (size_t i = 0; i < llvm::array_lengthof(DriverSuffixes); ++i)
230 if (ProgName.endswith(DriverSuffixes[i].Suffix))
231 return &DriverSuffixes[i];
235 static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
236 std::set<std::string> &SavedStrings) {
237 // Try to infer frontend type and default target from the program name by
238 // comparing it against DriverSuffixes in order.
240 // If there is a match, the function tries to identify a target as prefix.
241 // E.g. "x86_64-linux-clang" as interpreted as suffix "clang" with target
242 // prefix "x86_64-linux". If such a target prefix is found, is gets added via
243 // -target as implicit first argument.
245 std::string ProgName =llvm::sys::path::stem(ArgVector[0]);
247 // Transform to lowercase for case insensitive file systems.
248 ProgName = StringRef(ProgName).lower();
251 StringRef ProgNameRef = ProgName;
252 const DriverSuffix *DS = FindDriverSuffix(ProgNameRef);
255 // Try again after stripping any trailing version number:
256 // clang++3.5 -> clang++
257 ProgNameRef = ProgNameRef.rtrim("0123456789.");
258 DS = FindDriverSuffix(ProgNameRef);
262 // Try again after stripping trailing -component.
263 // clang++-tot -> clang++
264 ProgNameRef = ProgNameRef.slice(0, ProgNameRef.rfind('-'));
265 DS = FindDriverSuffix(ProgNameRef);
269 if (const char *Flag = DS->ModeFlag) {
270 // Add Flag to the arguments.
271 auto it = ArgVector.begin();
272 if (it != ArgVector.end())
274 ArgVector.insert(it, Flag);
277 StringRef::size_type LastComponent = ProgNameRef.rfind(
278 '-', ProgNameRef.size() - strlen(DS->Suffix));
279 if (LastComponent == StringRef::npos)
282 // Infer target from the prefix.
283 StringRef Prefix = ProgNameRef.slice(0, LastComponent);
284 std::string IgnoredError;
285 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
286 auto it = ArgVector.begin();
287 if (it != ArgVector.end())
289 const char *arr[] = { "-target", GetStableCStr(SavedStrings, Prefix) };
290 ArgVector.insert(it, std::begin(arr), std::end(arr));
296 class StringSetSaver : public llvm::cl::StringSaver {
298 StringSetSaver(std::set<std::string> &Storage) : Storage(Storage) {}
299 const char *SaveString(const char *Str) override {
300 return GetStableCStr(Storage, Str);
303 std::set<std::string> &Storage;
307 static void SetBackdoorDriverOutputsFromEnvVars(Driver &TheDriver) {
308 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
309 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
310 if (TheDriver.CCPrintOptions)
311 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
313 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
314 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
315 if (TheDriver.CCPrintHeaders)
316 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
318 // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE.
319 TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS");
320 if (TheDriver.CCLogDiagnostics)
321 TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE");
324 static void FixupDiagPrefixExeName(TextDiagnosticPrinter *DiagClient,
325 const std::string &Path) {
326 // If the clang binary happens to be named cl.exe for compatibility reasons,
327 // use clang-cl.exe as the prefix to avoid confusion between clang and MSVC.
328 StringRef ExeBasename(llvm::sys::path::filename(Path));
329 if (ExeBasename.equals_lower("cl.exe"))
330 ExeBasename = "clang-cl.exe";
331 DiagClient->setPrefix(ExeBasename);
334 // This lets us create the DiagnosticsEngine with a properly-filled-out
335 // DiagnosticOptions instance.
336 static DiagnosticOptions *
337 CreateAndPopulateDiagOpts(SmallVectorImpl<const char *> &argv) {
338 auto *DiagOpts = new DiagnosticOptions;
339 std::unique_ptr<OptTable> Opts(createDriverOptTable());
340 unsigned MissingArgIndex, MissingArgCount;
341 std::unique_ptr<InputArgList> Args(Opts->ParseArgs(
342 argv.begin() + 1, argv.end(), MissingArgIndex, MissingArgCount));
343 // We ignore MissingArgCount and the return value of ParseDiagnosticArgs.
344 // Any errors that would be diagnosed here will also be diagnosed later,
345 // when the DiagnosticsEngine actually exists.
346 (void) ParseDiagnosticArgs(*DiagOpts, *Args);
350 static void SetInstallDir(SmallVectorImpl<const char *> &argv,
352 // Attempt to find the original path used to invoke the driver, to determine
353 // the installed path. We do this manually, because we want to support that
354 // path being a symlink.
355 SmallString<128> InstalledPath(argv[0]);
357 // Do a PATH lookup, if there are no directory components.
358 if (llvm::sys::path::filename(InstalledPath) == InstalledPath)
359 if (llvm::ErrorOr<std::string> Tmp = llvm::sys::findProgramByName(
360 llvm::sys::path::filename(InstalledPath.str())))
361 InstalledPath = *Tmp;
362 llvm::sys::fs::make_absolute(InstalledPath);
363 InstalledPath = llvm::sys::path::parent_path(InstalledPath);
364 if (llvm::sys::fs::exists(InstalledPath.c_str()))
365 TheDriver.setInstalledDir(InstalledPath);
368 static int ExecuteCC1Tool(ArrayRef<const char *> argv, StringRef Tool) {
369 void *GetExecutablePathVP = (void *)(intptr_t) GetExecutablePath;
371 return cc1_main(argv.slice(2), argv[0], GetExecutablePathVP);
373 return cc1as_main(argv.slice(2), argv[0], GetExecutablePathVP);
375 // Reject unknown tools.
376 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
380 int main(int argc_, const char **argv_) {
381 llvm::sys::PrintStackTraceOnErrorSignal();
382 llvm::PrettyStackTraceProgram X(argc_, argv_);
384 if (llvm::sys::Process::FixupStandardFileDescriptors())
387 SmallVector<const char *, 256> argv;
388 llvm::SpecificBumpPtrAllocator<char> ArgAllocator;
389 std::error_code EC = llvm::sys::Process::GetArgumentVector(
390 argv, llvm::makeArrayRef(argv_, argc_), ArgAllocator);
392 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n';
396 std::set<std::string> SavedStrings;
397 StringSetSaver Saver(SavedStrings);
399 // Determines whether we want nullptr markers in argv to indicate response
400 // files end-of-lines. We only use this for the /LINK driver argument.
401 bool MarkEOLs = true;
402 if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1"))
404 llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, argv,
407 // Handle -cc1 integrated tools, even if -cc1 was expanded from a response
409 auto FirstArg = std::find_if(argv.begin() + 1, argv.end(),
410 [](const char *A) { return A != nullptr; });
411 if (FirstArg != argv.end() && StringRef(*FirstArg).startswith("-cc1")) {
412 // If -cc1 came from a response file, remove the EOL sentinels.
414 auto newEnd = std::remove(argv.begin(), argv.end(), nullptr);
415 argv.resize(newEnd - argv.begin());
417 return ExecuteCC1Tool(argv, argv[1] + 4);
420 bool CanonicalPrefixes = true;
421 for (int i = 1, size = argv.size(); i < size; ++i) {
422 // Skip end-of-line response file markers
423 if (argv[i] == nullptr)
425 if (StringRef(argv[i]) == "-no-canonical-prefixes") {
426 CanonicalPrefixes = false;
431 // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the
433 if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) {
434 // FIXME: Driver shouldn't take extra initial argument.
435 ApplyQAOverride(argv, OverrideStr, SavedStrings);
438 std::string Path = GetExecutablePath(argv[0], CanonicalPrefixes);
440 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts =
441 CreateAndPopulateDiagOpts(argv);
443 TextDiagnosticPrinter *DiagClient
444 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
445 FixupDiagPrefixExeName(DiagClient, Path);
447 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
449 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
451 if (!DiagOpts->DiagnosticSerializationFile.empty()) {
452 auto SerializedConsumer =
453 clang::serialized_diags::create(DiagOpts->DiagnosticSerializationFile,
454 &*DiagOpts, /*MergeChildRecords=*/true);
455 Diags.setClient(new ChainedDiagnosticConsumer(
456 Diags.takeClient(), std::move(SerializedConsumer)));
459 ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
461 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags);
462 SetInstallDir(argv, TheDriver);
464 llvm::InitializeAllTargets();
465 ParseProgName(argv, SavedStrings);
467 SetBackdoorDriverOutputsFromEnvVars(TheDriver);
469 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(argv));
471 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
473 Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
475 // Force a crash to test the diagnostics.
476 if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")) {
477 Diags.Report(diag::err_drv_force_crash) << "FORCE_CLANG_DIAGNOSTICS_CRASH";
479 // Pretend that every command failed.
480 FailingCommands.clear();
481 for (const auto &J : C->getJobs())
482 if (const Command *C = dyn_cast<Command>(&J))
483 FailingCommands.push_back(std::make_pair(-1, C));
486 for (const auto &P : FailingCommands) {
487 int CommandRes = P.first;
488 const Command *FailingCommand = P.second;
492 // If result status is < 0, then the driver command signalled an error.
493 // If result status is 70, then the driver command reported a fatal error.
494 // On Windows, abort will return an exit code of 3. In these cases,
495 // generate additional diagnostic information if possible.
496 bool DiagnoseCrash = CommandRes < 0 || CommandRes == 70;
498 DiagnoseCrash |= CommandRes == 3;
501 TheDriver.generateCompilationDiagnostics(*C, *FailingCommand);
506 Diags.getClient()->finish();
508 // If any timers were active but haven't been destroyed yet, print their
509 // results now. This happens in -disable-free mode.
510 llvm::TimerGroup::printAll(llvm::errs());
512 llvm::llvm_shutdown();
515 // Exit status should not be negative on Win32, unless abnormal termination.
516 // Once abnormal termiation was caught, negative status should not be
522 // If we have multiple failing commands, we return the result of the first