1 //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is the entry point to the clang driver; it is a thin wrapper 11 // for functionality in the Driver clang library. 12 // 13 //===----------------------------------------------------------------------===// 14 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/CompilerInvocation.h" 22 #include "clang/Frontend/TextDiagnosticPrinter.h" 23 #include "clang/Frontend/Utils.h" 24 #include "llvm/ADT/ArrayRef.h" 25 #include "llvm/ADT/STLExtras.h" 26 #include "llvm/ADT/SmallString.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/Config/llvm-config.h" 29 #include "llvm/Option/ArgList.h" 30 #include "llvm/Option/OptTable.h" 31 #include "llvm/Option/Option.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/FileSystem.h" 35 #include "llvm/Support/Host.h" 36 #include "llvm/Support/ManagedStatic.h" 37 #include "llvm/Support/MemoryBuffer.h" 38 #include "llvm/Support/Path.h" 39 #include "llvm/Support/PrettyStackTrace.h" 40 #include "llvm/Support/Process.h" 41 #include "llvm/Support/Program.h" 42 #include "llvm/Support/Regex.h" 43 #include "llvm/Support/Signals.h" 44 #include "llvm/Support/TargetRegistry.h" 45 #include "llvm/Support/TargetSelect.h" 46 #include "llvm/Support/Timer.h" 47 #include "llvm/Support/raw_ostream.h" 48 #include <memory> 49 #include <system_error> 50 using namespace clang; 51 using namespace clang::driver; 52 using namespace llvm::opt; 53 54 std::string GetExecutablePath(const char *Argv0, bool CanonicalPrefixes) { 55 if (!CanonicalPrefixes) 56 return Argv0; 57 58 // This just needs to be some symbol in the binary; C++ doesn't 59 // allow taking the address of ::main however. 60 void *P = (void*) (intptr_t) GetExecutablePath; 61 return llvm::sys::fs::getMainExecutable(Argv0, P); 62 } 63 64 static const char *SaveStringInSet(std::set<std::string> &SavedStrings, 65 StringRef S) { 66 return SavedStrings.insert(S).first->c_str(); 67 } 68 69 /// ApplyQAOverride - Apply a list of edits to the input argument lists. 70 /// 71 /// The input string is a space separate list of edits to perform, 72 /// they are applied in order to the input argument lists. Edits 73 /// should be one of the following forms: 74 /// 75 /// '#': Silence information about the changes to the command line arguments. 76 /// 77 /// '^': Add FOO as a new argument at the beginning of the command line. 78 /// 79 /// '+': Add FOO as a new argument at the end of the command line. 80 /// 81 /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command 82 /// line. 83 /// 84 /// 'xOPTION': Removes all instances of the literal argument OPTION. 85 /// 86 /// 'XOPTION': Removes all instances of the literal argument OPTION, 87 /// and the following argument. 88 /// 89 /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox' 90 /// at the end of the command line. 91 /// 92 /// \param OS - The stream to write edit information to. 93 /// \param Args - The vector of command line arguments. 94 /// \param Edit - The override command to perform. 95 /// \param SavedStrings - Set to use for storing string representations. 96 static void ApplyOneQAOverride(raw_ostream &OS, 97 SmallVectorImpl<const char*> &Args, 98 StringRef Edit, 99 std::set<std::string> &SavedStrings) { 100 // This does not need to be efficient. 101 102 if (Edit[0] == '^') { 103 const char *Str = 104 SaveStringInSet(SavedStrings, Edit.substr(1)); 105 OS << "### Adding argument " << Str << " at beginning\n"; 106 Args.insert(Args.begin() + 1, Str); 107 } else if (Edit[0] == '+') { 108 const char *Str = 109 SaveStringInSet(SavedStrings, Edit.substr(1)); 110 OS << "### Adding argument " << Str << " at end\n"; 111 Args.push_back(Str); 112 } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.endswith("/") && 113 Edit.slice(2, Edit.size()-1).find('/') != StringRef::npos) { 114 StringRef MatchPattern = Edit.substr(2).split('/').first; 115 StringRef ReplPattern = Edit.substr(2).split('/').second; 116 ReplPattern = ReplPattern.slice(0, ReplPattern.size()-1); 117 118 for (unsigned i = 1, e = Args.size(); i != e; ++i) { 119 std::string Repl = llvm::Regex(MatchPattern).sub(ReplPattern, Args[i]); 120 121 if (Repl != Args[i]) { 122 OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n"; 123 Args[i] = SaveStringInSet(SavedStrings, Repl); 124 } 125 } 126 } else if (Edit[0] == 'x' || Edit[0] == 'X') { 127 std::string Option = Edit.substr(1, std::string::npos); 128 for (unsigned i = 1; i < Args.size();) { 129 if (Option == Args[i]) { 130 OS << "### Deleting argument " << Args[i] << '\n'; 131 Args.erase(Args.begin() + i); 132 if (Edit[0] == 'X') { 133 if (i < Args.size()) { 134 OS << "### Deleting argument " << Args[i] << '\n'; 135 Args.erase(Args.begin() + i); 136 } else 137 OS << "### Invalid X edit, end of command line!\n"; 138 } 139 } else 140 ++i; 141 } 142 } else if (Edit[0] == 'O') { 143 for (unsigned i = 1; i < Args.size();) { 144 const char *A = Args[i]; 145 if (A[0] == '-' && A[1] == 'O' && 146 (A[2] == '\0' || 147 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' || 148 ('0' <= A[2] && A[2] <= '9'))))) { 149 OS << "### Deleting argument " << Args[i] << '\n'; 150 Args.erase(Args.begin() + i); 151 } else 152 ++i; 153 } 154 OS << "### Adding argument " << Edit << " at end\n"; 155 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit.str())); 156 } else { 157 OS << "### Unrecognized edit: " << Edit << "\n"; 158 } 159 } 160 161 /// ApplyQAOverride - Apply a comma separate list of edits to the 162 /// input argument lists. See ApplyOneQAOverride. 163 static void ApplyQAOverride(SmallVectorImpl<const char*> &Args, 164 const char *OverrideStr, 165 std::set<std::string> &SavedStrings) { 166 raw_ostream *OS = &llvm::errs(); 167 168 if (OverrideStr[0] == '#') { 169 ++OverrideStr; 170 OS = &llvm::nulls(); 171 } 172 173 *OS << "### CCC_OVERRIDE_OPTIONS: " << OverrideStr << "\n"; 174 175 // This does not need to be efficient. 176 177 const char *S = OverrideStr; 178 while (*S) { 179 const char *End = ::strchr(S, ' '); 180 if (!End) 181 End = S + strlen(S); 182 if (End != S) 183 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings); 184 S = End; 185 if (*S != '\0') 186 ++S; 187 } 188 } 189 190 extern int cc1_main(const char **ArgBegin, const char **ArgEnd, 191 const char *Argv0, void *MainAddr); 192 extern int cc1as_main(const char **ArgBegin, const char **ArgEnd, 193 const char *Argv0, void *MainAddr); 194 195 static void ParseProgName(SmallVectorImpl<const char *> &ArgVector, 196 std::set<std::string> &SavedStrings, 197 Driver &TheDriver) 198 { 199 // Try to infer frontend type and default target from the program name. 200 201 // suffixes[] contains the list of known driver suffixes. 202 // Suffixes are compared against the program name in order. 203 // If there is a match, the frontend type is updated as necessary (CPP/C++). 204 // If there is no match, a second round is done after stripping the last 205 // hyphen and everything following it. This allows using something like 206 // "clang++-2.9". 207 208 // If there is a match in either the first or second round, 209 // the function tries to identify a target as prefix. E.g. 210 // "x86_64-linux-clang" as interpreted as suffix "clang" with 211 // target prefix "x86_64-linux". If such a target prefix is found, 212 // is gets added via -target as implicit first argument. 213 static const struct { 214 const char *Suffix; 215 const char *ModeFlag; 216 } suffixes [] = { 217 { "clang", nullptr }, 218 { "clang++", "--driver-mode=g++" }, 219 { "clang-c++", "--driver-mode=g++" }, 220 { "clang-cc", nullptr }, 221 { "clang-cpp", "--driver-mode=cpp" }, 222 { "clang-g++", "--driver-mode=g++" }, 223 { "clang-gcc", nullptr }, 224 { "clang-cl", "--driver-mode=cl" }, 225 { "cc", nullptr }, 226 { "cpp", "--driver-mode=cpp" }, 227 { "cl" , "--driver-mode=cl" }, 228 { "++", "--driver-mode=g++" }, 229 }; 230 std::string ProgName(llvm::sys::path::stem(ArgVector[0])); 231 #ifdef LLVM_ON_WIN32 232 // Transform to lowercase for case insensitive file systems. 233 std::transform(ProgName.begin(), ProgName.end(), ProgName.begin(), 234 toLowercase); 235 #endif 236 StringRef ProgNameRef(ProgName); 237 StringRef Prefix; 238 239 for (int Components = 2; Components; --Components) { 240 bool FoundMatch = false; 241 size_t i; 242 243 for (i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { 244 if (ProgNameRef.endswith(suffixes[i].Suffix)) { 245 FoundMatch = true; 246 SmallVectorImpl<const char *>::iterator it = ArgVector.begin(); 247 if (it != ArgVector.end()) 248 ++it; 249 if (suffixes[i].ModeFlag) 250 ArgVector.insert(it, suffixes[i].ModeFlag); 251 break; 252 } 253 } 254 255 if (FoundMatch) { 256 StringRef::size_type LastComponent = ProgNameRef.rfind('-', 257 ProgNameRef.size() - strlen(suffixes[i].Suffix)); 258 if (LastComponent != StringRef::npos) 259 Prefix = ProgNameRef.slice(0, LastComponent); 260 break; 261 } 262 263 StringRef::size_type LastComponent = ProgNameRef.rfind('-'); 264 if (LastComponent == StringRef::npos) 265 break; 266 ProgNameRef = ProgNameRef.slice(0, LastComponent); 267 } 268 269 if (Prefix.empty()) 270 return; 271 272 std::string IgnoredError; 273 if (llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) { 274 SmallVectorImpl<const char *>::iterator it = ArgVector.begin(); 275 if (it != ArgVector.end()) 276 ++it; 277 const char* Strings[] = 278 { SaveStringInSet(SavedStrings, std::string("-target")), 279 SaveStringInSet(SavedStrings, Prefix) }; 280 ArgVector.insert(it, Strings, Strings + llvm::array_lengthof(Strings)); 281 } 282 } 283 284 namespace { 285 class StringSetSaver : public llvm::cl::StringSaver { 286 public: 287 StringSetSaver(std::set<std::string> &Storage) : Storage(Storage) {} 288 const char *SaveString(const char *Str) override { 289 return SaveStringInSet(Storage, Str); 290 } 291 private: 292 std::set<std::string> &Storage; 293 }; 294 } 295 296 int main(int argc_, const char **argv_) { 297 llvm::sys::PrintStackTraceOnErrorSignal(); 298 llvm::PrettyStackTraceProgram X(argc_, argv_); 299 300 SmallVector<const char *, 256> argv; 301 llvm::SpecificBumpPtrAllocator<char> ArgAllocator; 302 std::error_code EC = llvm::sys::Process::GetArgumentVector( 303 argv, ArrayRef<const char *>(argv_, argc_), ArgAllocator); 304 if (EC) { 305 llvm::errs() << "error: couldn't get arguments: " << EC.message() << '\n'; 306 return 1; 307 } 308 309 std::set<std::string> SavedStrings; 310 StringSetSaver Saver(SavedStrings); 311 llvm::cl::ExpandResponseFiles(Saver, llvm::cl::TokenizeGNUCommandLine, argv); 312 313 // Handle -cc1 integrated tools. 314 if (argv.size() > 1 && StringRef(argv[1]).startswith("-cc1")) { 315 StringRef Tool = argv[1] + 4; 316 317 if (Tool == "") 318 return cc1_main(argv.data()+2, argv.data()+argv.size(), argv[0], 319 (void*) (intptr_t) GetExecutablePath); 320 if (Tool == "as") 321 return cc1as_main(argv.data()+2, argv.data()+argv.size(), argv[0], 322 (void*) (intptr_t) GetExecutablePath); 323 324 // Reject unknown tools. 325 llvm::errs() << "error: unknown integrated tool '" << Tool << "'\n"; 326 return 1; 327 } 328 329 bool CanonicalPrefixes = true; 330 for (int i = 1, size = argv.size(); i < size; ++i) { 331 if (StringRef(argv[i]) == "-no-canonical-prefixes") { 332 CanonicalPrefixes = false; 333 break; 334 } 335 } 336 337 // Handle CCC_OVERRIDE_OPTIONS, used for editing a command line behind the 338 // scenes. 339 if (const char *OverrideStr = ::getenv("CCC_OVERRIDE_OPTIONS")) { 340 // FIXME: Driver shouldn't take extra initial argument. 341 ApplyQAOverride(argv, OverrideStr, SavedStrings); 342 } 343 344 std::string Path = GetExecutablePath(argv[0], CanonicalPrefixes); 345 346 IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions; 347 { 348 std::unique_ptr<OptTable> Opts(createDriverOptTable()); 349 unsigned MissingArgIndex, MissingArgCount; 350 std::unique_ptr<InputArgList> Args(Opts->ParseArgs( 351 argv.begin() + 1, argv.end(), MissingArgIndex, MissingArgCount)); 352 // We ignore MissingArgCount and the return value of ParseDiagnosticArgs. 353 // Any errors that would be diagnosed here will also be diagnosed later, 354 // when the DiagnosticsEngine actually exists. 355 (void) ParseDiagnosticArgs(*DiagOpts, *Args); 356 } 357 // Now we can create the DiagnosticsEngine with a properly-filled-out 358 // DiagnosticOptions instance. 359 TextDiagnosticPrinter *DiagClient 360 = new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); 361 362 // If the clang binary happens to be named cl.exe for compatibility reasons, 363 // use clang-cl.exe as the prefix to avoid confusion between clang and MSVC. 364 StringRef ExeBasename(llvm::sys::path::filename(Path)); 365 if (ExeBasename.equals_lower("cl.exe")) 366 ExeBasename = "clang-cl.exe"; 367 DiagClient->setPrefix(ExeBasename); 368 369 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); 370 371 DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagClient); 372 ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false); 373 374 Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags); 375 376 // Attempt to find the original path used to invoke the driver, to determine 377 // the installed path. We do this manually, because we want to support that 378 // path being a symlink. 379 { 380 SmallString<128> InstalledPath(argv[0]); 381 382 // Do a PATH lookup, if there are no directory components. 383 if (llvm::sys::path::filename(InstalledPath) == InstalledPath) { 384 std::string Tmp = llvm::sys::FindProgramByName( 385 llvm::sys::path::filename(InstalledPath.str())); 386 if (!Tmp.empty()) 387 InstalledPath = Tmp; 388 } 389 llvm::sys::fs::make_absolute(InstalledPath); 390 InstalledPath = llvm::sys::path::parent_path(InstalledPath); 391 bool exists; 392 if (!llvm::sys::fs::exists(InstalledPath.str(), exists) && exists) 393 TheDriver.setInstalledDir(InstalledPath); 394 } 395 396 llvm::InitializeAllTargets(); 397 ParseProgName(argv, SavedStrings, TheDriver); 398 399 // Handle CC_PRINT_OPTIONS and CC_PRINT_OPTIONS_FILE. 400 TheDriver.CCPrintOptions = !!::getenv("CC_PRINT_OPTIONS"); 401 if (TheDriver.CCPrintOptions) 402 TheDriver.CCPrintOptionsFilename = ::getenv("CC_PRINT_OPTIONS_FILE"); 403 404 // Handle CC_PRINT_HEADERS and CC_PRINT_HEADERS_FILE. 405 TheDriver.CCPrintHeaders = !!::getenv("CC_PRINT_HEADERS"); 406 if (TheDriver.CCPrintHeaders) 407 TheDriver.CCPrintHeadersFilename = ::getenv("CC_PRINT_HEADERS_FILE"); 408 409 // Handle CC_LOG_DIAGNOSTICS and CC_LOG_DIAGNOSTICS_FILE. 410 TheDriver.CCLogDiagnostics = !!::getenv("CC_LOG_DIAGNOSTICS"); 411 if (TheDriver.CCLogDiagnostics) 412 TheDriver.CCLogDiagnosticsFilename = ::getenv("CC_LOG_DIAGNOSTICS_FILE"); 413 414 std::unique_ptr<Compilation> C(TheDriver.BuildCompilation(argv)); 415 int Res = 0; 416 SmallVector<std::pair<int, const Command *>, 4> FailingCommands; 417 if (C.get()) 418 Res = TheDriver.ExecuteCompilation(*C, FailingCommands); 419 420 // Force a crash to test the diagnostics. 421 if (::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")) { 422 Diags.Report(diag::err_drv_force_crash) << "FORCE_CLANG_DIAGNOSTICS_CRASH"; 423 const Command *FailingCommand = nullptr; 424 FailingCommands.push_back(std::make_pair(-1, FailingCommand)); 425 } 426 427 for (SmallVectorImpl< std::pair<int, const Command *> >::iterator it = 428 FailingCommands.begin(), ie = FailingCommands.end(); it != ie; ++it) { 429 int CommandRes = it->first; 430 const Command *FailingCommand = it->second; 431 if (!Res) 432 Res = CommandRes; 433 434 // If result status is < 0, then the driver command signalled an error. 435 // If result status is 70, then the driver command reported a fatal error. 436 // On Windows, abort will return an exit code of 3. In these cases, 437 // generate additional diagnostic information if possible. 438 bool DiagnoseCrash = CommandRes < 0 || CommandRes == 70; 439 #ifdef LLVM_ON_WIN32 440 DiagnoseCrash |= CommandRes == 3; 441 #endif 442 if (DiagnoseCrash) { 443 TheDriver.generateCompilationDiagnostics(*C, FailingCommand); 444 break; 445 } 446 } 447 448 // If any timers were active but haven't been destroyed yet, print their 449 // results now. This happens in -disable-free mode. 450 llvm::TimerGroup::printAll(llvm::errs()); 451 452 llvm::llvm_shutdown(); 453 454 #ifdef LLVM_ON_WIN32 455 // Exit status should not be negative on Win32, unless abnormal termination. 456 // Once abnormal termiation was caught, negative status should not be 457 // propagated. 458 if (Res < 0) 459 Res = 1; 460 #endif 461 462 // If we have multiple failing commands, we return the result of the first 463 // failing command. 464 return Res; 465 } 466