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/ArgList.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/DriverDiagnostic.h"
21 #include "clang/Driver/OptTable.h"
22 #include "clang/Driver/Option.h"
23 #include "clang/Driver/Options.h"
24 #include "clang/Frontend/CompilerInvocation.h"
25 #include "clang/Frontend/TextDiagnosticPrinter.h"
26 #include "clang/Frontend/Utils.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/OwningPtr.h"
29 #include "llvm/ADT/SmallString.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Host.h"
34 #include "llvm/Support/ManagedStatic.h"
35 #include "llvm/Support/MemoryBuffer.h"
36 #include "llvm/Support/Path.h"
37 #include "llvm/Support/PrettyStackTrace.h"
38 #include "llvm/Support/Program.h"
39 #include "llvm/Support/Regex.h"
40 #include "llvm/Support/Signals.h"
41 #include "llvm/Support/TargetRegistry.h"
42 #include "llvm/Support/TargetSelect.h"
43 #include "llvm/Support/Timer.h"
44 #include "llvm/Support/raw_ostream.h"
45 #include "llvm/Support/system_error.h"
46 using namespace clang;
47 using namespace clang::driver;
49 llvm::sys::Path GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) {
50 if (!CanonicalPrefixes)
51 return llvm::sys::Path(Argv0);
53 // This just needs to be some symbol in the binary; C++ doesn't
54 // allow taking the address of ::main however.
55 void *P = (void*) (intptr_t) GetExecutablePath;
56 return llvm::sys::Path::GetMainExecutable(Argv0, P);
59 static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
61 return SavedStrings.insert(S).first->c_str();
64 /// ApplyQAOverride - Apply a list of edits to the input argument lists.
66 /// The input string is a space separate list of edits to perform,
67 /// they are applied in order to the input argument lists. Edits
68 /// should be one of the following forms:
70 /// '#': Silence information about the changes to the command line arguments.
72 /// '^': Add FOO as a new argument at the beginning of the command line.
74 /// '+': Add FOO as a new argument at the end of the command line.
76 /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command
79 /// 'xOPTION': Removes all instances of the literal argument OPTION.
81 /// 'XOPTION': Removes all instances of the literal argument OPTION,
82 /// and the following argument.
84 /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
85 /// at the end of the command line.
87 /// \param OS - The stream to write edit information to.
88 /// \param Args - The vector of command line arguments.
89 /// \param Edit - The override command to perform.
90 /// \param SavedStrings - Set to use for storing string representations.
91 static void ApplyOneQAOverride(raw_ostream &OS,
92 SmallVectorImpl<const char*> &Args,
94 std::set<std::string> &SavedStrings) {
95 // This does not need to be efficient.
99 SaveStringInSet(SavedStrings, Edit.substr(1));
100 OS << "### Adding argument " << Str << " at beginning\n";
101 Args.insert(Args.begin() + 1, Str);
102 } else if (Edit[0] == '+') {
104 SaveStringInSet(SavedStrings, Edit.substr(1));
105 OS << "### Adding argument " << Str << " at end\n";
107 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") &&
108 Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) {
109 StringRef MatchPattern = Edit.substr(2).split('/').first;
110 StringRef ReplPattern = Edit.substr(2).split('/').second;
111 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1);
113 for (unsigned i = 1, e = Args.size(); i != e; ++i) {
114 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]);
116 if (Repl != Args[i]) {
117 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n";
118 Args[i] = SaveStringInSet(SavedStrings, Repl);
121 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
122 std::string Option = Edit.substr(1, std::string::npos);
123 for (unsigned i = 1; i < Args.size();) {
124 if (Option == Args[i]) {
125 OS << "### Deleting argument " << Args[i] << '\n';
126 Args.erase(Args.begin() + i);
127 if (Edit[0] == 'X') {
128 if (i < Args.size()) {
129 OS << "### Deleting argument " << Args[i] << '\n';
130 Args.erase(Args.begin() + i);
132 OS << "### Invalid X edit, end of command line!\n";
137 } else if (Edit[0] == 'O') {
138 for (unsigned i = 1; i < Args.size();) {
139 const char *A = Args[i];
140 if (A[0] == '-' && A[1] == 'O' &&
142 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
143 ('0' <= A[2] && A[2] <= '9'))))) {
144 OS << "### Deleting argument " << Args[i] << '\n';
145 Args.erase(Args.begin() + i);
149 OS << "### Adding argument " << Edit << " at end\n";
150 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str()));
152 OS << "### Unrecognized edit: " << Edit << "\n";
156 /// ApplyQAOverride - Apply a comma separate list of edits to the
157 /// input argument lists. See ApplyOneQAOverride.
158 static void ApplyQAOverride(SmallVectorImpl<const char*> &Args,
159 const char *OverrideStr,
160 std::set<std::string> &SavedStrings) {
161 raw_ostream *OS = &llvm::errs();
163 if (OverrideStr[0] == '#') {
168 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
170 // This does not need to be efficient.
172 const char *S = OverrideStr;
174 const char *End = ::strchr(S, ' ');
178 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
185 extern int cc1_main(const char **ArgBegin, const char **ArgEnd,
186 const char *Argv0, void *MainAddr);
187 extern int cc1as_main(const char **ArgBegin, const char **ArgEnd,
188 const char *Argv0, void *MainAddr);
190 static void ExpandArgsFromBuf(const char *Arg,
191 SmallVectorImpl<const char*> &ArgVector,
192 std::set<std::string> &SavedStrings) {
193 const char *FName = Arg + 1;
194 OwningPtr<llvm::MemoryBuffer> MemBuf;
195 if (llvm::MemoryBuffer::getFile(FName, MemBuf)) {
196 ArgVector.push_back(SaveStringInSet(SavedStrings, Arg));
200 const char *Buf = MemBuf->getBufferStart();
204 for (const char *P = Buf; ; ++P) {
205 if (*P == '\0' || (isWhitespace(*P) && InQuote == ' ')) {
206 if (!CurArg.empty()) {
208 if (CurArg[0] != '@') {
209 ArgVector.push_back(SaveStringInSet(SavedStrings, CurArg));
211 ExpandArgsFromBuf(CurArg.c_str(), ArgVector, SavedStrings);
222 if (isWhitespace(*P)) {
224 CurArg.push_back(*P);
228 if (*P == '"' || *P == '\'') {
231 else if (InQuote == ' ')
234 CurArg.push_back(*P);
241 CurArg.push_back(*P);
244 CurArg.push_back(*P);
248 static void ExpandArgv(int argc, const char **argv,
249 SmallVectorImpl<const char*> &ArgVector,
250 std::set<std::string> &SavedStrings) {
251 for (int i = 0; i < argc; ++i) {
252 const char *Arg = argv[i];
254 ArgVector.push_back(SaveStringInSet(SavedStrings, std::string(Arg)));
258 ExpandArgsFromBuf(Arg, ArgVector, SavedStrings);
262 static void ParseProgName(SmallVectorImpl<const char *> &ArgVector,
263 std::set<std::string> &SavedStrings,
266 // Try to infer frontend type and default target from the program name.
268 // suffixes[] contains the list of known driver suffixes.
269 // Suffixes are compared against the program name in order.
270 // If there is a match, the frontend type is updated as necessary (CPP/C++).
271 // If there is no match, a second round is done after stripping the last
272 // hyphen and everything following it. This allows using something like
275 // If there is a match in either the first or second round,
276 // the function tries to identify a target as prefix. E.g.
277 // "x86_64-linux-clang" as interpreted as suffix "clang" with
278 // target prefix "x86_64-linux". If such a target prefix is found,
279 // is gets added via -target as implicit first argument.
280 static const struct {
285 { "clang", false, false },
286 { "clang++", true, false },
287 { "clang-c++", true, false },
288 { "clang-cc", false, false },
289 { "clang-cpp", false, true },
290 { "clang-g++", true, false },
291 { "clang-gcc", false, false },
292 { "cc", false, false },
293 { "cpp", false, true },
294 { "++", true, false },
296 std::string ProgName(llvm::sys::path::stem(ArgVector[0]));
297 StringRef ProgNameRef(ProgName);
300 for (int Components = 2; Components; --Components) {
301 bool FoundMatch = false;
304 for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) {
305 if (ProgNameRef.endswith(suffixes[i].Suffix)) {
307 if (suffixes[i].IsCXX)
308 TheDriver.CCCIsCXX = true;
309 if (suffixes[i].IsCPP)
310 TheDriver.CCCIsCPP = true;
316 StringRef::size_type LastComponent = ProgNameRef.rfind('-',
317 ProgNameRef.size() - strlen(suffixes[i].Suffix));
318 if (LastComponent != StringRef::npos)
319 Prefix = ProgNameRef.slice(0, LastComponent);
323 StringRef::size_type LastComponent = ProgNameRef.rfind('-');
324 if (LastComponent == StringRef::npos)
326 ProgNameRef = ProgNameRef.slice(0, LastComponent);
332 std::string IgnoredError;
333 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) {
334 SmallVectorImpl<const char *>::iterator it = ArgVector.begin();
335 if (it != ArgVector.end())
337 ArgVector.insert(it, SaveStringInSet(SavedStrings, Prefix));
339 SaveStringInSet(SavedStrings, std::string("-target")));
343 int main(int argc_, const char **argv_) {
344 llvm::sys::PrintStackTraceOnErrorSignal();
345 llvm::PrettyStackTraceProgram X(argc_, argv_);
347 std::set<std::string> SavedStrings;
348 SmallVector<const char*, 256> argv;
350 ExpandArgv(argc_, argv_, argv, SavedStrings);
352 // Handle -cc1 integrated tools.
353 if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) {
354 StringRef Tool = argv[1] + 4;
357 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0],
358 (void*) (intptr_t) GetExecutablePath);
360 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0],
361 (void*) (intptr_t) GetExecutablePath);
363 // Reject unknown tools.
364 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n";
368 bool CanonicalPrefixes = true;
369 for (int i = 1, size = argv.size(); i < size; ++i) {
370 if (StringRef(argv[i]) == "-no-canonical-prefixes") {
371 CanonicalPrefixes = false;
376 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
377 // command line behind the scenes.
378 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
379 // FIXME: Driver shouldn't take extra initial argument.
380 ApplyQAOverride(argv, OverrideStr, SavedStrings);
381 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
382 // FIXME: Driver shouldn't take extra initial argument.
383 std::vector<const char*> ExtraArgs;
386 const char *Next = strchr(Cur, ',');
389 ExtraArgs.push_back(SaveStringInSet(SavedStrings,
390 std::string(Cur, Next)));
394 ExtraArgs.push_back(SaveStringInSet(SavedStrings, Cur));
399 argv.insert(&argv[1], ExtraArgs.begin(), ExtraArgs.end());
402 llvm::sys::Path Path = GetExecutablePath(argv[0], CanonicalPrefixes);
404 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions;
406 // Note that ParseDiagnosticArgs() uses the cc1 option table.
407 OwningPtr<OptTable> CC1Opts(createDriverOptTable());
408 unsigned MissingArgIndex, MissingArgCount;
409 OwningPtr<InputArgList> Args(CC1Opts->ParseArgs(argv.begin()+1, argv.end(),
410 MissingArgIndex, MissingArgCount));
411 // We ignore MissingArgCount and the return value of ParseDiagnosticArgs.
412 // Any errors that would be diagnosed here will also be diagnosed later,
413 // when the DiagnosticsEngine actually exists.
414 (void) ParseDiagnosticArgs(*DiagOpts, *Args);
416 // Now we can create the DiagnosticsEngine with a properly-filled-out
417 // DiagnosticOptions instance.
418 TextDiagnosticPrinter *DiagClient
419 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
420 DiagClient->setPrefix(llvm::sys::path::filename(Path.str()));
421 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
423 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient);
424 ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false);
426 Driver TheDriver(Path.str(), llvm::sys::getDefaultTargetTriple(),
429 // Attempt to find the original path used to invoke the driver, to determine
430 // the installed path. We do this manually, because we want to support that
431 // path being a symlink.
433 SmallString<128> InstalledPath(argv[0]);
435 // Do a PATH lookup, if there are no directory components.
436 if (llvm::sys::path::filename(InstalledPath) == InstalledPath) {
437 llvm::sys::Path Tmp = llvm::sys::Program::FindProgramByName(
438 llvm::sys::path::filename(InstalledPath.str()));
440 InstalledPath = Tmp.str();
442 llvm::sys::fs::make_absolute(InstalledPath);
443 InstalledPath = llvm::sys::path::parent_path(InstalledPath);
445 if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists)
446 TheDriver.setInstalledDir(InstalledPath);
449 llvm::InitializeAllTargets();
450 ParseProgName(argv, SavedStrings, TheDriver);
452 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE.
453 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS");
454 if (TheDriver.CCPrintOptions)
455 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE");
457 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE.
458 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS");
459 if (TheDriver.CCPrintHeaders)
460 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE");
462 // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE.
463 TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS");
464 if (TheDriver.CCLogDiagnostics)
465 TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE");
467 OwningPtr<Compilation> C(TheDriver.BuildCompilation(argv));
469 SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
471 Res = TheDriver.ExecuteCompilation(*C, FailingCommands);
473 // Force a crash to test the diagnostics.
474 if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")) {
475 Diags.Report(diag::err_drv_force_crash) << "FORCE_CLANG_DIAGNOSTICS_CRASH";
476 const Command *FailingCommand = 0;
477 FailingCommands.push_back(std::make_pair(-1, FailingCommand));
480 for (SmallVectorImpl< std::pair<int, const Command *> >::iterator it =
481 FailingCommands.begin(), ie = FailingCommands.end(); it != ie; ++it) {
482 int CommandRes = it->first;
483 const Command *FailingCommand = it->second;
487 // If result status is < 0, then the driver command signalled an error.
488 // If result status is 70, then the driver command reported a fatal error.
489 // In these cases, generate additional diagnostic information if possible.
490 if (CommandRes < 0 || CommandRes == 70) {
491 TheDriver.generateCompilationDiagnostics(*C, FailingCommand);
496 // If any timers were active but haven't been destroyed yet, print their
497 // results now. This happens in -disable-free mode.
498 llvm::TimerGroup::printAll(llvm::errs());
500 llvm::llvm_shutdown();
503 // Exit status should not be negative on Win32, unless abnormal termination.
504 // Once abnormal termiation was caught, negative status should not be
510 // If we have multiple failing commands, we return the result of the first