1e3365aa452673f7ea3244bb6638551f13ab7570dNick Lewycky//===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===//
23ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar//
33ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar//                     The LLVM Compiler Infrastructure
43ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar//
53ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar// This file is distributed under the University of Illinois Open Source
63ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar// License. See LICENSE.TXT for details.
73ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar//
83ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar//===----------------------------------------------------------------------===//
93ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar
103ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar#include "clang/Driver/Driver.h"
1155fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "InputInfo.h"
1255fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "ToolChains.h"
1355fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Basic/Version.h"
14ef8225444452a1486bd721f3285301fe84643b00Stephen Hines#include "clang/Config/config.h"
1553ec55215075c8f4ddd47ca6ed7d382f16beb670Daniel Dunbar#include "clang/Driver/Action.h"
161b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbar#include "clang/Driver/Compilation.h"
174ad4b3ebbe5769143389dccfcfadb666a4ba5940Daniel Dunbar#include "clang/Driver/DriverDiagnostic.h"
18f353c8cc2ee1cc16ff194b399a8d951f707fb129Daniel Dunbar#include "clang/Driver/Job.h"
191b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbar#include "clang/Driver/Options.h"
20f353c8cc2ee1cc16ff194b399a8d951f707fb129Daniel Dunbar#include "clang/Driver/Tool.h"
21f353c8cc2ee1cc16ff194b399a8d951f707fb129Daniel Dunbar#include "clang/Driver/ToolChain.h"
227f9fc3f7ce076645cb6aefc99c64d9446caf13d6Chris Lattner#include "llvm/ADT/ArrayRef.h"
236981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg#include "llvm/ADT/STLExtras.h"
24ef8225444452a1486bd721f3285301fe84643b00Stephen Hines#include "llvm/ADT/StringExtras.h"
2555fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/ADT/StringSet.h"
2676b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg#include "llvm/ADT/StringSwitch.h"
27b1e25a1bc03292dc538d336573e0be1490223171Reid Kleckner#include "llvm/Option/Arg.h"
28b1e25a1bc03292dc538d336573e0be1490223171Reid Kleckner#include "llvm/Option/ArgList.h"
29651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include "llvm/Option/OptSpecifier.h"
30b1e25a1bc03292dc538d336573e0be1490223171Reid Kleckner#include "llvm/Option/OptTable.h"
31b1e25a1bc03292dc538d336573e0be1490223171Reid Kleckner#include "llvm/Option/Option.h"
32c706c8e440abf61910c042380e19c67932998395Eric Christopher#include "llvm/Support/Debug.h"
33548f6c8e8010b181cb151df92f0a989363cb993eDavid Blaikie#include "llvm/Support/ErrorHandling.h"
34256053b31e697fdf0cc48f17d621c82fc3b8dff0Michael J. Spencer#include "llvm/Support/FileSystem.h"
3503013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Path.h"
3655fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/PrettyStackTrace.h"
37ef8225444452a1486bd721f3285301fe84643b00Stephen Hines#include "llvm/Support/Process.h"
3803013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer#include "llvm/Support/Program.h"
3955fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "llvm/Support/raw_ostream.h"
40f2462be34a49e2108c9b4151f8e6d3fe1b916179Dylan Noblesmith#include <map>
41651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines#include <memory>
4269d3b4f7c9fa818d2536b4f1b220b942ae392128Dylan Noblesmith
431b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbarusing namespace clang::driver;
4492b369928b8e9ad156fb6eec2bf9f21d657bd69bChris Lattnerusing namespace clang;
45b1e25a1bc03292dc538d336573e0be1490223171Reid Klecknerusing namespace llvm::opt;
461b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbar
475f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerDriver::Driver(StringRef ClangExecutable,
489606a57b6f2c43bf438a958fdffea1de60f6b2b7Sebastian Pop               StringRef DefaultTargetTriple,
49d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie               DiagnosticsEngine &Diags)
5033530b8e99085f736b0af5e1bc8fedaf80b5c90eHans Wennborg  : Opts(createDriverOptTable()), Diags(Diags), Mode(GCCMode),
514762a2da74875d2ae94e0d77d38ed964816cce36Sebastian Pop    ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT),
524762a2da74875d2ae94e0d77d38ed964816cce36Sebastian Pop    UseStdLib(true), DefaultTargetTriple(DefaultTargetTriple),
536bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    DefaultImageName("a.out"),
546f85b1e7906f7a03cbede1f730ba19b28a5f85e8Bob Wilson    DriverTitle("clang LLVM compiler"),
556bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr),
566bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    CCLogDiagnosticsFilename(nullptr),
577d3240d3c949b8d0697a23a202cceba4d1a2d21fRafael Espindola    CCCPrintBindings(false),
58a673dbda6f67dbeb06aa2f99312200c8f5034cabRafael Espindola    CCPrintHeaders(false), CCLogDiagnostics(false),
592b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    CCGenDiagnostics(false), CCCGenericGCCName(""), CheckInputsExist(true),
6064c4f29ff3b98d412e0235504364096e997b04bbBob Wilson    CCCUsePCH(true), SuppressMissingInputWarning(false) {
61225c41706dc0f3682236e351820d339e14390e00Daniel Dunbar
62d5b08bee799d4f77f1a25fa5977ca77b983ab031Michael J. Spencer  Name = llvm::sys::path::stem(ClangExecutable);
63d5b08bee799d4f77f1a25fa5977ca77b983ab031Michael J. Spencer  Dir  = llvm::sys::path::parent_path(ClangExecutable);
644a792960940bf19acced0123eae08bd39b15138bBob Wilson
654a792960940bf19acced0123eae08bd39b15138bBob Wilson  // Compute the path to the resource directory.
664a792960940bf19acced0123eae08bd39b15138bBob Wilson  StringRef ClangResourceDir(CLANG_RESOURCE_DIR);
674a792960940bf19acced0123eae08bd39b15138bBob Wilson  SmallString<128> P(Dir);
684a792960940bf19acced0123eae08bd39b15138bBob Wilson  if (ClangResourceDir != "")
694a792960940bf19acced0123eae08bd39b15138bBob Wilson    llvm::sys::path::append(P, ClangResourceDir);
704a792960940bf19acced0123eae08bd39b15138bBob Wilson  else
714a792960940bf19acced0123eae08bd39b15138bBob Wilson    llvm::sys::path::append(P, "..", "lib", "clang", CLANG_VERSION_STRING);
724a792960940bf19acced0123eae08bd39b15138bBob Wilson  ResourceDir = P.str();
733ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar}
743ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar
753ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel DunbarDriver::~Driver() {
761b3bb6efc59a21f794b534078f9ae7e95393f510Daniel Dunbar  delete Opts;
7718d7f3af752c41a197552a1ff25ddd639224b4bbChandler Carruth
78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  llvm::DeleteContainerSeconds(ToolChains);
793ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar}
803ede8d0a7d1813f678ccc6011a99a0834b1b6116Daniel Dunbar
8176b86c2e79a58910a641d449f229889d671d8a38Hans Wennborgvoid Driver::ParseDriverMode(ArrayRef<const char *> Args) {
8276b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  const std::string OptName =
8376b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    getOpts().getOption(options::OPT_driver_mode).getPrefixedName();
8476b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg
8576b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  for (size_t I = 0, E = Args.size(); I != E; ++I) {
8676b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    const StringRef Arg = Args[I];
8776b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    if (!Arg.startswith(OptName))
8876b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg      continue;
8976b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg
9076b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    const StringRef Value = Arg.drop_front(OptName.size());
9176b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    const unsigned M = llvm::StringSwitch<unsigned>(Value)
9276b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg        .Case("gcc", GCCMode)
9376b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg        .Case("g++", GXXMode)
9476b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg        .Case("cpp", CPPMode)
95c2f531a1300cc7a79cb8dde12cb993da82beba1bHans Wennborg        .Case("cl",  CLMode)
9676b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg        .Default(~0U);
9776b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg
9876b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    if (M != ~0U)
9976b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg      Mode = static_cast<DriverMode>(M);
10076b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg    else
10176b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg      Diag(diag::err_drv_unsupported_option_argument) << OptName << Value;
10276b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  }
10376b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg}
10476b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg
1052d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris LattnerInputArgList *Driver::ParseArgStrings(ArrayRef<const char *> ArgList) {
1068f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar  llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
1076981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg
1086981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  unsigned IncludedFlagsBitmask;
1096981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  unsigned ExcludedFlagsBitmask;
110651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
1116981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg    getIncludeExcludeOptionFlagMasks();
1126981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg
113847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar  unsigned MissingArgIndex, MissingArgCount;
1147f9fc3f7ce076645cb6aefc99c64d9446caf13d6Chris Lattner  InputArgList *Args = getOpts().ParseArgs(ArgList.begin(), ArgList.end(),
1156981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg                                           MissingArgIndex, MissingArgCount,
1166981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg                                           IncludedFlagsBitmask,
1176981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg                                           ExcludedFlagsBitmask);
118847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar
119847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar  // Check for missing argument error.
120847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar  if (MissingArgCount)
121847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar    Diag(clang::diag::err_drv_missing_argument)
122847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar      << Args->getArgString(MissingArgIndex) << MissingArgCount;
123847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar
124847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar  // Check for unsupported options.
125847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar  for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
126847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar       it != ie; ++it) {
127847abaa62883b6c20005bdd3346ebbd44e46dbacDaniel Dunbar    Arg *A = *it;
12891e06dab9335ed6b4474a74353549c708cf3d936Michael J. Spencer    if (A->getOption().hasFlag(options::Unsupported)) {
129b0c4df5c4df69a003f26b378eb95961bc7c486e5Daniel Dunbar      Diag(clang::diag::err_drv_unsupported_opt) << A->getAsString(*Args);
130b0c4df5c4df69a003f26b378eb95961bc7c486e5Daniel Dunbar      continue;
131b0c4df5c4df69a003f26b378eb95961bc7c486e5Daniel Dunbar    }
1322dd17a15ddc0184790d790b6128d79ffddae4964Chad Rosier
1332dd17a15ddc0184790d790b6128d79ffddae4964Chad Rosier    // Warn about -mcpu= without an argument.
1346467c9bf4f07f54789d3ed902f04048f4d80ad47Chad Rosier    if (A->getOption().matches(options::OPT_mcpu_EQ) &&
1352dd17a15ddc0184790d790b6128d79ffddae4964Chad Rosier        A->containsValue("")) {
1366467c9bf4f07f54789d3ed902f04048f4d80ad47Chad Rosier      Diag(clang::diag::warn_drv_empty_joined_argument) <<
1376467c9bf4f07f54789d3ed902f04048f4d80ad47Chad Rosier        A->getAsString(*Args);
1382dd17a15ddc0184790d790b6128d79ffddae4964Chad Rosier    }
1390648262df75d97b464c2be0ed867da3615659785Daniel Dunbar  }
1400648262df75d97b464c2be0ed867da3615659785Daniel Dunbar
14106b52b41a98fff69c1c68baf24316eafd8d5a47bRafael Espindola  for (arg_iterator it = Args->filtered_begin(options::OPT_UNKNOWN),
14206b52b41a98fff69c1c68baf24316eafd8d5a47bRafael Espindola         ie = Args->filtered_end(); it != ie; ++it) {
14306b52b41a98fff69c1c68baf24316eafd8d5a47bRafael Espindola    Diags.Report(diag::err_drv_unknown_argument) << (*it) ->getAsString(*Args);
14406b52b41a98fff69c1c68baf24316eafd8d5a47bRafael Espindola  }
14506b52b41a98fff69c1c68baf24316eafd8d5a47bRafael Espindola
1460648262df75d97b464c2be0ed867da3615659785Daniel Dunbar  return Args;
1470648262df75d97b464c2be0ed867da3615659785Daniel Dunbar}
1480648262df75d97b464c2be0ed867da3615659785Daniel Dunbar
1491fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier// Determine which compilation mode we are in. We look for options which
1501fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier// affect the phase, starting with the earliest phases, and record which
1511fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier// option we used to determine the final phase.
1521fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosierphases::ID Driver::getFinalPhase(const DerivedArgList &DAL, Arg **FinalPhaseArg)
1531fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosierconst {
1546bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Arg *PhaseArg = nullptr;
1551fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  phases::ID FinalPhase;
15659f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher
157ef8225444452a1486bd721f3285301fe84643b00Stephen Hines  // -{E,EP,P,M,MM} only run the preprocessor.
15876b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  if (CCCIsCPP() ||
1591fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier      (PhaseArg = DAL.getLastArg(options::OPT_E)) ||
160ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) ||
161651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) ||
162651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) {
1631fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    FinalPhase = phases::Preprocess;
16459f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher
1651fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    // -{fsyntax-only,-analyze,emit-ast,S} only run up to the compiler.
1661fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) ||
167c544ba09695e300f31355af342258bd57619e737Douglas Gregor             (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) ||
168651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines             (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) ||
1691fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier             (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) ||
170582b395ea4d5dfe353e2132a470d39efe2f84a54Fariborz Jahanian             (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) ||
17130660a898545416f0fea2d717f16f75640001e38Ted Kremenek             (PhaseArg = DAL.getLastArg(options::OPT__migrate)) ||
1721fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier             (PhaseArg = DAL.getLastArg(options::OPT__analyze,
17353cb2b47596b0ca893a449fa81e0912ded658759Chad Rosier                                        options::OPT__analyze_auto)) ||
1741fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier             (PhaseArg = DAL.getLastArg(options::OPT_emit_ast)) ||
1751fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier             (PhaseArg = DAL.getLastArg(options::OPT_S))) {
1761fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    FinalPhase = phases::Compile;
1771fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier
1781fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    // -c only runs up to the assembler.
1791fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) {
1801fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    FinalPhase = phases::Assemble;
1811fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier
1821fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    // Otherwise do everything.
1831fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  } else
1841fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    FinalPhase = phases::Link;
1851fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier
1861fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  if (FinalPhaseArg)
1871fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier    *FinalPhaseArg = PhaseArg;
1881fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier
1891fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier  return FinalPhase;
1901fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier}
1911fc1de48cdf9a2f241e0f2c7b9e9db2322f1b794Chad Rosier
1926bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstatic Arg* MakeInputArg(DerivedArgList &Args, OptTable *Opts,
19373052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg                         StringRef Value) {
19473052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg  Arg *A = new Arg(Opts->getOption(options::OPT_INPUT), Value,
19573052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg                   Args.getBaseArgs().MakeIndex(Value), Value.data());
1966bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines  Args.AddSynthesizedArg(A);
19773052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg  A->claim();
19873052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg  return A;
19973052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg}
20073052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg
201279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel DunbarDerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const {
202279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar  DerivedArgList *DAL = new DerivedArgList(Args);
203279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar
204e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar  bool HasNostdlib = Args.hasArg(options::OPT_nostdlib);
205279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar  for (ArgList::const_iterator it = Args.begin(),
206f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar         ie = Args.end(); it != ie; ++it) {
207f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar    const Arg *A = *it;
208f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar
209f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar    // Unfortunately, we have to parse some forwarding options (-Xassembler,
210f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar    // -Xlinker, -Xpreprocessor) because we either integrate their functionality
211f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar    // (assembler and preprocessor), or bypass a previous driver ('collect2').
212eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar
213eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    // Rewrite linker options, to replace --no-demangle with a custom internal
214eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    // option.
215eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    if ((A->getOption().matches(options::OPT_Wl_COMMA) ||
216eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar         A->getOption().matches(options::OPT_Xlinker)) &&
217eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar        A->containsValue("--no-demangle")) {
218f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar      // Add the rewritten no-demangle argument.
219f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar      DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle));
220f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar
221f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar      // Add the remaining values as Xlinker arguments.
222f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar      for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
2231d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith        if (StringRef(A->getValue(i)) != "--no-demangle")
224f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar          DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker),
2251d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith                              A->getValue(i));
226f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar
227f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar      continue;
228f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar    }
229f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar
230eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by
231eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    // some build systems. We don't try to be complete here because we don't
232eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    // care to encourage this usage model.
233eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    if (A->getOption().matches(options::OPT_Wp_COMMA) &&
2341d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith        (A->getValue(0) == StringRef("-MD") ||
2351d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith         A->getValue(0) == StringRef("-MMD"))) {
236212df32d955c34dbb8b976bc0316e8eb83ac579eDaniel Dunbar      // Rewrite to -MD/-MMD along with -MF.
2371d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith      if (A->getValue(0) == StringRef("-MD"))
238212df32d955c34dbb8b976bc0316e8eb83ac579eDaniel Dunbar        DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD));
239212df32d955c34dbb8b976bc0316e8eb83ac579eDaniel Dunbar      else
240212df32d955c34dbb8b976bc0316e8eb83ac579eDaniel Dunbar        DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD));
241da05df7ac964abcd18956ffaf6d66ff13a203a11Michael J. Spencer      if (A->getNumValues() == 2)
242da05df7ac964abcd18956ffaf6d66ff13a203a11Michael J. Spencer        DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF),
243da05df7ac964abcd18956ffaf6d66ff13a203a11Michael J. Spencer                            A->getValue(1));
244eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar      continue;
245eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar    }
246eda3f707fd53f90f4232b0e694d3f5514237aba5Daniel Dunbar
2477433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen    // Rewrite reserved library names.
2487433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen    if (A->getOption().matches(options::OPT_l)) {
2491d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith      StringRef Value = A->getValue();
250e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar
2517433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen      // Rewrite unless -nostdlib is present.
2527433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen      if (!HasNostdlib && Value == "stdc++") {
253e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar        DAL->AddFlagArg(A, Opts->getOption(
254e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar                              options::OPT_Z_reserved_lib_stdcxx));
255e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar        continue;
256e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar      }
2577433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen
2587433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen      // Rewrite unconditionally.
2597433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen      if (Value == "cc_kext") {
2607433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen        DAL->AddFlagArg(A, Opts->getOption(
2617433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen                              options::OPT_Z_reserved_lib_cckext));
2627433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen        continue;
2637433fedce98a58341d0f30c2e12e8d53f3bba575Shantonu Sen      }
264e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar    }
265e5a37f48388c7e4cc081f44d13a4910bfa56cd35Daniel Dunbar
26673052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg    // Pick up inputs via the -- option.
26773052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg    if (A->getOption().matches(options::OPT__DASH_DASH)) {
26873052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg      A->claim();
26973052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg      for (unsigned i = 0, e = A->getNumValues(); i != e; ++i)
27073052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg        DAL->append(MakeInputArg(*DAL, Opts, A->getValue(i)));
27173052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg      continue;
27273052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg    }
27373052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg
274279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar    DAL->append(*it);
275f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar  }
276279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar
277a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar  // Add a default value of -mlinker-version=, if one was given and the user
278a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar  // didn't specify one.
279a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar#if defined(HOST_LINK_VERSION)
280a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar  if (!Args.hasArg(options::OPT_mlinker_version_EQ)) {
281a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar    DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ),
282a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar                      HOST_LINK_VERSION);
283c326b64a4d14ad89d6a5d227d2460050149d5461Daniel Dunbar    DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim();
284a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar  }
285a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar#endif
286a77a723489b0cff63bf90f23416e10b8b6fc1f55Daniel Dunbar
287279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar  return DAL;
288279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar}
289279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar
2902d3ba4f5a923a90c3fc290ddfba5e36c2d0a9b46Chris LattnerCompilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
2918f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar  llvm::PrettyStackTraceString CrashInfo("Compilation construction");
2928f25c79e59d88df41c5a3cabba1c58035d384a6eDaniel Dunbar
29359f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher  // FIXME: Handle environment options which affect driver behavior, somewhere
294e8cb554c36c94e31e80ea893d9ad528afb491bdfBill Wendling  // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS.
295815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier
296815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier  if (char *env = ::getenv("COMPILER_PATH")) {
297815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier    StringRef CompilerPath = env;
298815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier    while (!CompilerPath.empty()) {
29904ee66e1fee44e72ee57845c1405a7f498a9daf7NAKAMURA Takumi      std::pair<StringRef, StringRef> Split
300fa09383ce9d6ea81a8fe86b2f26519c7a1c165fbRafael Espindola        = CompilerPath.split(llvm::sys::EnvPathSeparator);
301815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier      PrefixDirs.push_back(Split.first);
302815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier      CompilerPath = Split.second;
303815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier    }
304815eb6b80739ffca9879b9833208932dee55d0cbChad Rosier  }
305cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
30676b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  // We look for the driver mode option early, because the mode can affect
30776b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  // how other options are parsed.
30876b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  ParseDriverMode(ArgList.slice(1));
30976b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg
310cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar  // FIXME: What are we going to do with -V and -b?
311cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
312a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // FIXME: This stuff needs to go into the Compilation, not the driver.
313a673dbda6f67dbeb06aa2f99312200c8f5034cabRafael Espindola  bool CCCPrintActions;
3140648262df75d97b464c2be0ed867da3615659785Daniel Dunbar
3157f9fc3f7ce076645cb6aefc99c64d9446caf13d6Chris Lattner  InputArgList *Args = ParseArgStrings(ArgList.slice(1));
3168477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar
3177ca7987669fcd14d91b4227a9ddcf7fdd8b1f8c2Rafael Espindola  // -no-canonical-prefixes is used very early in main.
3187ca7987669fcd14d91b4227a9ddcf7fdd8b1f8c2Rafael Espindola  Args->ClaimAllArgs(options::OPT_no_canonical_prefixes);
3197ca7987669fcd14d91b4227a9ddcf7fdd8b1f8c2Rafael Espindola
320c19a12dc3d441bec62eed55e312b76c12d6d9022Daniel Dunbar  // Ignore -pipe.
321c19a12dc3d441bec62eed55e312b76c12d6d9022Daniel Dunbar  Args->ClaimAllArgs(options::OPT_pipe);
322c19a12dc3d441bec62eed55e312b76c12d6d9022Daniel Dunbar
3238477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  // Extract -ccc args.
324365c02f65be026f90e67a8e00e7b827cee60e228Daniel Dunbar  //
325a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // FIXME: We need to figure out where this behavior should live. Most of it
326a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // should be outside in the client; the parts that aren't should have proper
327a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // options, either by introducing new ones or by overloading gcc ones like -V
328a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // or -b.
3298477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  CCCPrintActions = Args->hasArg(options::OPT_ccc_print_phases);
3308477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  CCCPrintBindings = Args->hasArg(options::OPT_ccc_print_bindings);
3318477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  if (const Arg *A = Args->getLastArg(options::OPT_ccc_gcc_name))
3321d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    CCCGenericGCCName = A->getValue();
3338477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  CCCUsePCH = Args->hasFlag(options::OPT_ccc_pch_is_pch,
3348477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar                            options::OPT_ccc_pch_is_pth);
3358a988c32b8ac18eb252ef4a58ed56f898255524dJoerg Sonnenberger  // FIXME: DefaultTargetTriple is used by the target-prefixed calls to as/ld
3368a988c32b8ac18eb252ef4a58ed56f898255524dJoerg Sonnenberger  // and getToolChain is const.
3375db9527a2e582c17bc835e6e2a5a8178efa5a1e8Hans Wennborg  if (IsCLMode()) {
338651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    // clang-cl targets MSVC-style Win32.
3395db9527a2e582c17bc835e6e2a5a8178efa5a1e8Hans Wennborg    llvm::Triple T(DefaultTargetTriple);
340651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    T.setOS(llvm::Triple::Win32);
341651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    T.setEnvironment(llvm::Triple::MSVC);
3425db9527a2e582c17bc835e6e2a5a8178efa5a1e8Hans Wennborg    DefaultTargetTriple = T.str();
3435db9527a2e582c17bc835e6e2a5a8178efa5a1e8Hans Wennborg  }
3448a988c32b8ac18eb252ef4a58ed56f898255524dJoerg Sonnenberger  if (const Arg *A = Args->getLastArg(options::OPT_target))
3451d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    DefaultTargetTriple = A->getValue();
3468477ee93c9d81d1b5fe321706ff95bcd219c744bDaniel Dunbar  if (const Arg *A = Args->getLastArg(options::OPT_ccc_install_dir))
3471d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    Dir = InstalledDir = A->getValue();
34809982cec0029bc495591067fa89967b6b4d1fa19Benjamin Kramer  for (arg_iterator it = Args->filtered_begin(options::OPT_B),
34909982cec0029bc495591067fa89967b6b4d1fa19Benjamin Kramer         ie = Args->filtered_end(); it != ie; ++it) {
35009982cec0029bc495591067fa89967b6b4d1fa19Benjamin Kramer    const Arg *A = *it;
35109982cec0029bc495591067fa89967b6b4d1fa19Benjamin Kramer    A->claim();
3521d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    PrefixDirs.push_back(A->getValue(0));
35309982cec0029bc495591067fa89967b6b4d1fa19Benjamin Kramer  }
3548ab2bdcc4f397e100c684ede8e8ca6e226e1380aJoerg Sonnenberger  if (const Arg *A = Args->getLastArg(options::OPT__sysroot_EQ))
3551d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    SysRoot = A->getValue();
356bdaa13418851be5cc1ca3be88fbd82b15efecde7Peter Collingbourne  if (const Arg *A = Args->getLastArg(options::OPT__dyld_prefix_EQ))
357bdaa13418851be5cc1ca3be88fbd82b15efecde7Peter Collingbourne    DyldPrefix = A->getValue();
35805e5930166333e382522b942f00d08abc6c0a28eJoerg Sonnenberger  if (Args->hasArg(options::OPT_nostdlib))
35905e5930166333e382522b942f00d08abc6c0a28eJoerg Sonnenberger    UseStdLib = false;
360365c02f65be026f90e67a8e00e7b827cee60e228Daniel Dunbar
36176e6e1377ff837047fb8e03a199c7f286c53897bRichard Smith  if (const Arg *A = Args->getLastArg(options::OPT_resource_dir))
3624a792960940bf19acced0123eae08bd39b15138bBob Wilson    ResourceDir = A->getValue();
36361d16c1f491f5ad6d4a254dcca8868acb5f150ccJim Grosbach
364279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar  // Perform the default argument translations.
365279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar  DerivedArgList *TranslatedArgs = TranslateInputArgs(*Args);
366279c1dbebf37cd128f3c73c70741a6b8c35ad025Daniel Dunbar
36708386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth  // Owned by the host.
36818d7f3af752c41a197552a1ff25ddd639224b4bbChandler Carruth  const ToolChain &TC = getToolChain(*Args);
36908386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth
37021549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  // The compilation takes ownership of Args.
37108386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth  Compilation *C = new Compilation(*this, TC, Args, TranslatedArgs);
37221549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar
37321549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  if (!HandleImmediateArgs(*C))
37421549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar    return C;
375cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
376be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier  // Construct the list of inputs.
377be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier  InputList Inputs;
37873052bf669d3fe4c202cff2792cfe215cf5727beHans Wennborg  BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs);
379be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier
3804a04d0b4aa33e98ea9e7b3e787f0490585ce9b6fChandler Carruth  // Construct the list of abstract actions to perform for this compilation. On
381651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  // MachO targets this uses the driver-driver and universal actions.
382651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TC.getTriple().isOSBinFormatMachO())
38365f7165676854bf75494f5b73a620d7237db5d41Joerg Sonnenberger    BuildUniversalActions(C->getDefaultToolChain(), C->getArgs(),
384be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier                          Inputs, C->getActions());
38553ec55215075c8f4ddd47ca6ed7d382f16beb670Daniel Dunbar  else
386be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier    BuildActions(C->getDefaultToolChain(), C->getArgs(), Inputs,
387be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier                 C->getActions());
38853ec55215075c8f4ddd47ca6ed7d382f16beb670Daniel Dunbar
38953ec55215075c8f4ddd47ca6ed7d382f16beb670Daniel Dunbar  if (CCCPrintActions) {
39010ffa9a4887d9376e3eb3598e40523d1b58773c9Daniel Dunbar    PrintActions(*C);
39121549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar    return C;
39253ec55215075c8f4ddd47ca6ed7d382f16beb670Daniel Dunbar  }
393ab835430b4f758dd49903251c7c7f21b95933c89Daniel Dunbar
39421549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  BuildJobs(*C);
3958d2554a2c3201aa664cbf2108cf9d57aa0aa4b0aDaniel Dunbar
3968d2554a2c3201aa664cbf2108cf9d57aa0aa4b0aDaniel Dunbar  return C;
397365c02f65be026f90e67a8e00e7b827cee60e228Daniel Dunbar}
398365c02f65be026f90e67a8e00e7b827cee60e228Daniel Dunbar
39959f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher// When clang crashes, produce diagnostic information including the fully
40059f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher// preprocessed source file(s).  Request that the developer attach the
4012b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier// diagnostic information to a bug report.
4022b81910618f63e4ce2373c926a26e76b4b91373fChad Rosiervoid Driver::generateCompilationDiagnostics(Compilation &C,
4032b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier                                            const Command *FailingCommand) {
4042639ac628cff5dedb4fa60169082b2a1a9dd8694Chad Rosier  if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics))
4056467c9bf4f07f54789d3ed902f04048f4d80ad47Chad Rosier    return;
4068ba9a6244c56b19bc2a24de5d0c32ff37d50177bChad Rosier
40775dbc717c21a662b7836ed34cc4e7da7b8fa33c0Chad Rosier  // Don't try to generate diagnostics for link or dsymutil jobs.
40875dbc717c21a662b7836ed34cc4e7da7b8fa33c0Chad Rosier  if (FailingCommand && (FailingCommand->getCreator().isLinkJob() ||
40975dbc717c21a662b7836ed34cc4e7da7b8fa33c0Chad Rosier                         FailingCommand->getCreator().isDsymutilJob()))
4102639ac628cff5dedb4fa60169082b2a1a9dd8694Chad Rosier    return;
4112639ac628cff5dedb4fa60169082b2a1a9dd8694Chad Rosier
4124f6a4b47675a30e621251fcda1d192a2c17c6eeaChad Rosier  // Print the version of the compiler.
4134f6a4b47675a30e621251fcda1d192a2c17c6eeaChad Rosier  PrintVersion(C, llvm::errs());
4144f6a4b47675a30e621251fcda1d192a2c17c6eeaChad Rosier
4152b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  Diag(clang::diag::note_drv_command_failed_diag_msg)
41613223073a94f8df6527193827192a326d50877fbChad Rosier    << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the "
41713223073a94f8df6527193827192a326d50877fbChad Rosier    "crash backtrace, preprocessed source, and associated run script.";
4182b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
4192b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  // Suppress driver output and emit preprocessor output to temp file.
42076b86c2e79a58910a641d449f229889d671d8a38Hans Wennborg  Mode = CPPMode;
4212b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  CCGenDiagnostics = true;
4222b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
423ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier  // Save the original job command(s).
424ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier  std::string Cmd;
425ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier  llvm::raw_string_ostream OS(Cmd);
42653d2409a223a14d063faf06e4360ede09c41ed80Chad Rosier  if (FailingCommand)
427fc3389701ead32709ba84825e4c06651065da2c0Hans Wennborg    FailingCommand->Print(OS, "\n", /*Quote*/ false, /*CrashReport*/ true);
42853d2409a223a14d063faf06e4360ede09c41ed80Chad Rosier  else
4296467c9bf4f07f54789d3ed902f04048f4d80ad47Chad Rosier    // Crash triggered by FORCE_CLANG_DIAGNOSTICS_CRASH, which doesn't have an
43053d2409a223a14d063faf06e4360ede09c41ed80Chad Rosier    // associated FailingCommand, so just pass all jobs.
431fc3389701ead32709ba84825e4c06651065da2c0Hans Wennborg    C.getJobs().Print(OS, "\n", /*Quote*/ false, /*CrashReport*/ true);
432ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier  OS.flush();
433ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier
4345b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  // Keep track of whether we produce any errors while trying to produce
4355b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  // preprocessed sources.
4365b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  DiagnosticErrorTrap Trap(Diags);
4375b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith
4385b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  // Suppress tool output.
4392b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  C.initCompilationForDiagnostics();
440be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier
441be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier  // Construct the list of inputs.
442be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier  InputList Inputs;
443be69f6092668b0586c8729855c01ca2eea22e6feChad Rosier  BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs);
4442b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
445137a20b5f9360311dd0b6d2500e3365ab2cdf40eChad Rosier  for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) {
44690c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier    bool IgnoreInput = false;
44790c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier
44890c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier    // Ignore input from stdin or any inputs that cannot be preprocessed.
4496bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    // Check type first as not all linker inputs have a value.
4506bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines   if (types::getPreprocessedType(it->first) == types::TY_INVALID) {
4516bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines      IgnoreInput = true;
4526bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines    } else if (!strcmp(it->second->getValue(), "-")) {
45390c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier      Diag(clang::diag::note_drv_command_failed_diag_msg)
45490c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier        << "Error generating preprocessed source(s) - ignoring input from stdin"
45590c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier        ".";
45690c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier      IgnoreInput = true;
45790c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier    }
45890c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier
45990c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier    if (IgnoreInput) {
460137a20b5f9360311dd0b6d2500e3365ab2cdf40eChad Rosier      it = Inputs.erase(it);
461137a20b5f9360311dd0b6d2500e3365ab2cdf40eChad Rosier      ie = Inputs.end();
4623060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    } else {
463137a20b5f9360311dd0b6d2500e3365ab2cdf40eChad Rosier      ++it;
4643060178ad9df29789505c1e6debcfc80a3a13587Chad Rosier    }
465137a20b5f9360311dd0b6d2500e3365ab2cdf40eChad Rosier  }
46690c88023fd4cb709c8645aa24e9eac420f733b5dChad Rosier
4678425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier  if (Inputs.empty()) {
4688425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
4698425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier      << "Error generating preprocessed source(s) - no preprocessable inputs.";
4708425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier    return;
4718425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier  }
4728425a5413b437a0b6ac04c475e7cf54cc9977880Chad Rosier
47346e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier  // Don't attempt to generate preprocessed files if multiple -arch options are
474c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier  // used, unless they're all duplicates.
475c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier  llvm::StringSet<> ArchNames;
47646e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier  for (ArgList::const_iterator it = C.getArgs().begin(), ie = C.getArgs().end();
47746e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier       it != ie; ++it) {
47846e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier    Arg *A = *it;
47946e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier    if (A->getOption().matches(options::OPT_arch)) {
4801d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith      StringRef ArchName = A->getValue();
481c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier      ArchNames.insert(ArchName);
48246e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier    }
48346e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier  }
484c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier  if (ArchNames.size() > 1) {
485c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
486c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier      << "Error generating preprocessed source(s) - cannot generate "
487c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier      "preprocessed source with multiple -arch options.";
488c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier    return;
489c56389189e7a8b99d7db3a6f60c896adb2b092bfChad Rosier  }
49046e3908af1ba2af6d7497e8e501f5c9a01acde7eChad Rosier
4914a04d0b4aa33e98ea9e7b3e787f0490585ce9b6fChandler Carruth  // Construct the list of abstract actions to perform for this compilation. On
4924a04d0b4aa33e98ea9e7b3e787f0490585ce9b6fChandler Carruth  // Darwin OSes this uses the driver-driver and builds universal actions.
49308386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth  const ToolChain &TC = C.getDefaultToolChain();
494651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (TC.getTriple().isOSBinFormatMachO())
49508386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth    BuildUniversalActions(TC, C.getArgs(), Inputs, C.getActions());
4962b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  else
49708386a9769575bd8290024aaaecb2dbad5bfd59dChandler Carruth    BuildActions(TC, C.getArgs(), Inputs, C.getActions());
4982b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
4992b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  BuildJobs(C);
5002b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
5012b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  // If there were errors building the compilation, quit now.
5025b9268f26ac53a74d2c504279fe577d988d5615dRichard Smith  if (Trap.hasErrorOccurred()) {
5032b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
5042b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier      << "Error generating preprocessed source(s).";
5052b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    return;
5062b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  }
5072b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
5082b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  // Generate preprocessed output.
509a16355c31878403443f99077cc8df8318457faf5Chad Rosier  SmallVector<std::pair<int, const Command *>, 4> FailingCommands;
510a16355c31878403443f99077cc8df8318457faf5Chad Rosier  C.ExecuteJob(C.getJobs(), FailingCommands);
5112b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
5122b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  // If the command succeeded, we are done.
513a16355c31878403443f99077cc8df8318457faf5Chad Rosier  if (FailingCommands.empty()) {
5142b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
51513223073a94f8df6527193827192a326d50877fbChad Rosier      << "\n********************\n\n"
51613223073a94f8df6527193827192a326d50877fbChad Rosier      "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n"
51713223073a94f8df6527193827192a326d50877fbChad Rosier      "Preprocessed source(s) and associated run script(s) are located at:";
5182b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    ArgStringList Files = C.getTempFiles();
51959f9b26bcb59bfb4d66c6ea129172795dcd1cb6bEric Christopher    for (ArgStringList::const_iterator it = Files.begin(), ie = Files.end();
520ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier         it != ie; ++it) {
5212b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier      Diag(clang::diag::note_drv_command_failed_diag_msg) << *it;
522ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      std::string Script = StringRef(*it).rsplit('.').first;
523ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      // In some cases (modules) we'll dump extra data to help with reproducing
524ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      // the crash into a directory next to the output.
525ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      SmallString<128> VFS;
526ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      if (llvm::sys::fs::exists(Script + ".cache")) {
527ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        Diag(clang::diag::note_drv_command_failed_diag_msg)
528ef8225444452a1486bd721f3285301fe84643b00Stephen Hines            << Script + ".cache";
529ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        VFS = llvm::sys::path::filename(Script + ".cache");
530ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        llvm::sys::path::append(VFS, "vfs", "vfs.yaml");
531ef8225444452a1486bd721f3285301fe84643b00Stephen Hines      }
532ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier
533ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier      std::string Err;
534ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier      Script += ".sh";
535651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      llvm::raw_fd_ostream ScriptOS(Script.c_str(), Err, llvm::sys::fs::F_Excl);
536ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier      if (!Err.empty()) {
537ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier        Diag(clang::diag::note_drv_command_failed_diag_msg)
538ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier          << "Error generating run script: " + Script + " " + Err;
539ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier      } else {
540ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        // Replace the original filename with the preprocessed one.
54144727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        size_t I, E;
54244727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        I = Cmd.find("-main-file-name ");
54344727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        assert (I != std::string::npos && "Expected to find -main-file-name");
54444727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        I += 16;
54544727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        E = Cmd.find(" ", I);
54644727629b766bf34bc2299c40f5ee8e4c7a71391Chad Rosier        assert (E != std::string::npos && "-main-file-name missing argument?");
5478775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        StringRef OldFilename = StringRef(Cmd).slice(I, E);
5488775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        StringRef NewFilename = llvm::sys::path::filename(*it);
5498775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        I = StringRef(Cmd).rfind(OldFilename);
5508775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        E = I + OldFilename.size();
5518775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        I = Cmd.rfind(" ", I) + 1;
5528775485350b2419cb9953518ace61cf68b1c9c18Chad Rosier        Cmd.replace(I, E - I, NewFilename.data(), NewFilename.size());
553ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        if (!VFS.empty()) {
554ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          // Add the VFS overlay to the reproduction script.
555ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          I += NewFilename.size();
556ef8225444452a1486bd721f3285301fe84643b00Stephen Hines          Cmd.insert(I, std::string(" -ivfsoverlay ") + VFS.c_str());
557ef8225444452a1486bd721f3285301fe84643b00Stephen Hines        }
558ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier        ScriptOS << Cmd;
559ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier        Diag(clang::diag::note_drv_command_failed_diag_msg) << Script;
560ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier      }
561ce50c55f8cb569cc1b71770fd62327a883fdb7baChad Rosier    }
56213223073a94f8df6527193827192a326d50877fbChad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
56313223073a94f8df6527193827192a326d50877fbChad Rosier      << "\n\n********************";
5642b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  } else {
5652b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    // Failure, remove preprocessed files.
5661c18759b45140c85e62ac6367fbee51f363e3c81Chad Rosier    if (!C.getArgs().hasArg(options::OPT_save_temps))
5672b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier      C.CleanupFileList(C.getTempFiles(), true);
5682b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
5692b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier    Diag(clang::diag::note_drv_command_failed_diag_msg)
5702b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier      << "Error generating preprocessed source(s).";
5712b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  }
5722b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier}
5732b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier
5742b81910618f63e4ce2373c926a26e76b4b91373fChad Rosierint Driver::ExecuteCompilation(const Compilation &C,
575a16355c31878403443f99077cc8df8318457faf5Chad Rosier    SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) const {
576c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  // Just print if -### was present.
577c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
578fc3389701ead32709ba84825e4c06651065da2c0Hans Wennborg    C.getJobs().Print(llvm::errs(), "\n", true);
579c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar    return 0;
580c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  }
581c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
582c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  // If there were errors building the compilation, quit now.
5832b81910618f63e4ce2373c926a26e76b4b91373fChad Rosier  if (Diags.hasErrorOccurred())
584c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar    return 1;
585c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
586a16355c31878403443f99077cc8df8318457faf5Chad Rosier  C.ExecuteJob(C.getJobs(), FailingCommands);
587a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar
588c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  // Remove temp files.
589c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar  C.CleanupFileList(C.getTempFiles());
590c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
5919fcbc050a704cc5bb46e1c493940aca7c79aebf9Daniel Dunbar  // If the command succeeded, we are done.
592a16355c31878403443f99077cc8df8318457faf5Chad Rosier  if (FailingCommands.empty())
593a16355c31878403443f99077cc8df8318457faf5Chad Rosier    return 0;
5949fcbc050a704cc5bb46e1c493940aca7c79aebf9Daniel Dunbar
595a16355c31878403443f99077cc8df8318457faf5Chad Rosier  // Otherwise, remove result files and print extra information about abnormal
596a16355c31878403443f99077cc8df8318457faf5Chad Rosier  // failures.
597a16355c31878403443f99077cc8df8318457faf5Chad Rosier  for (SmallVectorImpl< std::pair<int, const Command *> >::iterator it =
598a16355c31878403443f99077cc8df8318457faf5Chad Rosier         FailingCommands.begin(), ie = FailingCommands.end(); it != ie; ++it) {
599a16355c31878403443f99077cc8df8318457faf5Chad Rosier    int Res = it->first;
600a16355c31878403443f99077cc8df8318457faf5Chad Rosier    const Command *FailingCommand = it->second;
601c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
602a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // Remove result files if we're not saving temps.
603a16355c31878403443f99077cc8df8318457faf5Chad Rosier    if (!C.getArgs().hasArg(options::OPT_save_temps)) {
604a16355c31878403443f99077cc8df8318457faf5Chad Rosier      const JobAction *JA = cast<JobAction>(&FailingCommand->getSource());
605a16355c31878403443f99077cc8df8318457faf5Chad Rosier      C.CleanupFileMap(C.getResultFiles(), JA, true);
6065d4d9807a65291cbe6ab88d4a4f74cb7fff01bdePeter Collingbourne
607a16355c31878403443f99077cc8df8318457faf5Chad Rosier      // Failure result files are valid unless we crashed.
608a16355c31878403443f99077cc8df8318457faf5Chad Rosier      if (Res < 0)
609a16355c31878403443f99077cc8df8318457faf5Chad Rosier        C.CleanupFileMap(C.getFailureResultFiles(), JA, true);
610a16355c31878403443f99077cc8df8318457faf5Chad Rosier    }
611c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
612a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // Print extra information about abnormal failures, if possible.
613a16355c31878403443f99077cc8df8318457faf5Chad Rosier    //
614a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // This is ad-hoc, but we don't want to be excessively noisy. If the result
615ef8225444452a1486bd721f3285301fe84643b00Stephen Hines    // status was 1, assume the command failed normally. In particular, if it
616a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // was the compiler then assume it gave a reasonable error code. Failures
617a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // in other tools are less common, and they generally have worse
618a16355c31878403443f99077cc8df8318457faf5Chad Rosier    // diagnostics, so always print the diagnostic there.
619a16355c31878403443f99077cc8df8318457faf5Chad Rosier    const Tool &FailingTool = FailingCommand->getCreator();
620a16355c31878403443f99077cc8df8318457faf5Chad Rosier
621a16355c31878403443f99077cc8df8318457faf5Chad Rosier    if (!FailingCommand->getCreator().hasGoodDiagnostics() || Res != 1) {
622a16355c31878403443f99077cc8df8318457faf5Chad Rosier      // FIXME: See FIXME above regarding result code interpretation.
623a16355c31878403443f99077cc8df8318457faf5Chad Rosier      if (Res < 0)
624a16355c31878403443f99077cc8df8318457faf5Chad Rosier        Diag(clang::diag::err_drv_command_signalled)
625a16355c31878403443f99077cc8df8318457faf5Chad Rosier          << FailingTool.getShortName();
626a16355c31878403443f99077cc8df8318457faf5Chad Rosier      else
627a16355c31878403443f99077cc8df8318457faf5Chad Rosier        Diag(clang::diag::err_drv_command_failed)
628a16355c31878403443f99077cc8df8318457faf5Chad Rosier          << FailingTool.getShortName() << Res;
629a16355c31878403443f99077cc8df8318457faf5Chad Rosier    }
630a16355c31878403443f99077cc8df8318457faf5Chad Rosier  }
631a16355c31878403443f99077cc8df8318457faf5Chad Rosier  return 0;
632c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar}
633c88a88f6f7672b6bb831dce9da4acfa0c846975fDaniel Dunbar
634c35d71f1e0a0c72fd43a73ddecd408bf43d501deDaniel Dunbarvoid Driver::PrintHelp(bool ShowHidden) const {
6356981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  unsigned IncludedFlagsBitmask;
6366981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  unsigned ExcludedFlagsBitmask;
637651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) =
6386981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg    getIncludeExcludeOptionFlagMasks();
6396981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg
6406981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  ExcludedFlagsBitmask |= options::NoDriverOption;
6416981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  if (!ShowHidden)
6426981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg    ExcludedFlagsBitmask |= HelpHidden;
6436981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg
6446981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg  getOpts().PrintHelp(llvm::outs(), Name.c_str(), DriverTitle.c_str(),
6456981330cc231e4e2ccbd38679209e04b776483ebHans Wennborg                      IncludedFlagsBitmask, ExcludedFlagsBitmask);
64691e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar}
64791e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar
6485f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnervoid Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const {
649a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // FIXME: The following handlers should use a callback mechanism, we don't
650a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // know what the client would like to do.
651a18f1b88a7ef3d2c944bb1d3b6586a2860ce2baaTed Kremenek  OS << getClangFullVersion() << '\n';
65270c8db1003af49d3627aeef41fcdd7c6c6a7df79Daniel Dunbar  const ToolChain &TC = C.getDefaultToolChain();
653793007242ba209b68ce14ec7547ac70ee981303aDaniel Dunbar  OS << "Target: " << TC.getTripleString() << '\n';
6543ee96baa3fff269e145855a6f7526c36e59a7f4bDaniel Dunbar
6553ee96baa3fff269e145855a6f7526c36e59a7f4bDaniel Dunbar  // Print the threading model.
6563ee96baa3fff269e145855a6f7526c36e59a7f4bDaniel Dunbar  //
6573ee96baa3fff269e145855a6f7526c36e59a7f4bDaniel Dunbar  // FIXME: Implement correctly.
658793007242ba209b68ce14ec7547ac70ee981303aDaniel Dunbar  OS << "Thread model: " << "posix" << '\n';
659cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar}
660cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
661c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner/// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
662c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner/// option.
6635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattnerstatic void PrintDiagnosticCategories(raw_ostream &OS) {
664477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis  // Skip the empty category.
665477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis  for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories();
666477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis       i != max; ++i)
667477aab6782795e7472055a54108d2df270ce1a89Argyrios Kyrtzidis    OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
668c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner}
669c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner
67021549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbarbool Driver::HandleImmediateArgs(const Compilation &C) {
671e82ec0bead14ae262cddbe958b77767a3f6aebcdDaniel Dunbar  // The order these options are handled in gcc is all over the place, but we
672a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // don't expect inconsistencies w.r.t. that to matter in practice.
67391e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar
674d829950b2b75d50fca2772a20129323a186993a5Daniel Dunbar  if (C.getArgs().hasArg(options::OPT_dumpmachine)) {
675d829950b2b75d50fca2772a20129323a186993a5Daniel Dunbar    llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n';
676d829950b2b75d50fca2772a20129323a186993a5Daniel Dunbar    return false;
677d829950b2b75d50fca2772a20129323a186993a5Daniel Dunbar  }
678d829950b2b75d50fca2772a20129323a186993a5Daniel Dunbar
679e06dc219a869d73b6c9a398a2dee0aa2d4e2dcf0Daniel Dunbar  if (C.getArgs().hasArg(options::OPT_dumpversion)) {
68095a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    // Since -dumpversion is only implemented for pedantic GCC compatibility, we
68195a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    // return an answer which matches our definition of __VERSION__.
68295a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    //
68395a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    // If we want to return a more correct answer some day, then we should
68495a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    // introduce a non-pedantically GCC compatible mode to Clang in which we
68595a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    // provide sensible definitions for -dumpversion, __VERSION__, etc.
68695a907fc0f2f717dca38a379c1d2353bfea06d4fDaniel Dunbar    llvm::outs() << "4.2.1\n";
687e06dc219a869d73b6c9a398a2dee0aa2d4e2dcf0Daniel Dunbar    return false;
688e06dc219a869d73b6c9a398a2dee0aa2d4e2dcf0Daniel Dunbar  }
689f78925f633e949f06521d9ffd937019e59f35efcDaniel Dunbar
690c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner  if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) {
691c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner    PrintDiagnosticCategories(llvm::outs());
692c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner    return false;
693c3d26cc4eea5187ce13ed180b479ff8ef41015a7Chris Lattner  }
694e06dc219a869d73b6c9a398a2dee0aa2d4e2dcf0Daniel Dunbar
695bfd7a525bcf372004787cb641b8c1566b9e8aba5James Molloy  if (C.getArgs().hasArg(options::OPT_help) ||
696c35d71f1e0a0c72fd43a73ddecd408bf43d501deDaniel Dunbar      C.getArgs().hasArg(options::OPT__help_hidden)) {
697c35d71f1e0a0c72fd43a73ddecd408bf43d501deDaniel Dunbar    PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden));
69891e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar    return false;
69991e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar  }
70091e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar
7016cc73de6ebb3eb00af7dd5149f6778318f1fb76fDaniel Dunbar  if (C.getArgs().hasArg(options::OPT__version)) {
702a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar    // Follow gcc behavior and use stdout for --version and stderr for -v.
703793007242ba209b68ce14ec7547ac70ee981303aDaniel Dunbar    PrintVersion(C, llvm::outs());
7046cc73de6ebb3eb00af7dd5149f6778318f1fb76fDaniel Dunbar    return false;
7056cc73de6ebb3eb00af7dd5149f6778318f1fb76fDaniel Dunbar  }
7066cc73de6ebb3eb00af7dd5149f6778318f1fb76fDaniel Dunbar
707a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  if (C.getArgs().hasArg(options::OPT_v) ||
70821549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar      C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
709793007242ba209b68ce14ec7547ac70ee981303aDaniel Dunbar    PrintVersion(C, llvm::errs());
710cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar    SuppressMissingInputWarning = true;
711cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar  }
712cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
71321549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  const ToolChain &TC = C.getDefaultToolChain();
7146365ab91a0cf2f20c73ea9c39b55c14ab438018eChandler Carruth
7156365ab91a0cf2f20c73ea9c39b55c14ab438018eChandler Carruth  if (C.getArgs().hasArg(options::OPT_v))
7166365ab91a0cf2f20c73ea9c39b55c14ab438018eChandler Carruth    TC.printVerboseInfo(llvm::errs());
7176365ab91a0cf2f20c73ea9c39b55c14ab438018eChandler Carruth
718ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar  if (C.getArgs().hasArg(options::OPT_print_search_dirs)) {
719ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    llvm::outs() << "programs: =";
720ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    for (ToolChain::path_list::const_iterator it = TC.getProgramPaths().begin(),
721ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar           ie = TC.getProgramPaths().end(); it != ie; ++it) {
722ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar      if (it != TC.getProgramPaths().begin())
723ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar        llvm::outs() << ':';
724ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar      llvm::outs() << *it;
725ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    }
726ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    llvm::outs() << "\n";
72741ee7a371780d45f43251ee68542b8a9f0cd0fbaPeter Collingbourne    llvm::outs() << "libraries: =" << ResourceDir;
72859c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger
7294762a2da74875d2ae94e0d77d38ed964816cce36Sebastian Pop    StringRef sysroot = C.getSysRoot();
73059c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger
731a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar    for (ToolChain::path_list::const_iterator it = TC.getFilePaths().begin(),
732ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar           ie = TC.getFilePaths().end(); it != ie; ++it) {
73341ee7a371780d45f43251ee68542b8a9f0cd0fbaPeter Collingbourne      llvm::outs() << ':';
73459c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger      const char *path = it->c_str();
73559c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger      if (path[0] == '=')
73659c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger        llvm::outs() << sysroot << path + 1;
73759c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger      else
73859c8457f01f908e75d0e61ec3bd99cebcd672ceeJoerg Sonnenberger        llvm::outs() << path;
739ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    }
740ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar    llvm::outs() << "\n";
74191e28afe3503893c69afd71877f11e3bf03fa4ceDaniel Dunbar    return false;
742ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar  }
743ca3459e95f552cfae6c9c5fb4ed161e3a662c25eDaniel Dunbar
744a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // FIXME: The following handlers should use a callback mechanism, we don't
745a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar  // know what the client would like to do.
74621549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) {
7471d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    llvm::outs() << GetFilePath(A->getValue(), TC) << "\n";
748cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar    return false;
749cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar  }
750cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
75121549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) {
7521d489cf4a04ad0ad8ac2696e4eed0995f3a67288Richard Smith    llvm::outs() << GetProgramPath(A->getValue(), TC) << "\n";
753cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar    return false;
754cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar  }
755cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
75621549237f14505cfc2a18a06416372a36229d0ceDaniel Dunbar  if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
7575ed34f4c58b6ad4e21038d713c1fae31a6146ff5Daniel Dunbar    llvm::outs() << GetFilePath("libgcc.a", TC) << "\n";
758cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar    return false;
759cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar  }
760cb881672c2c46142ec1bdfa401c9818ae805db0fDaniel Dunbar
76112cfe036d809173a25af0104844d4bb66a92252bDaniel Dunbar  if (C.getArgs().hasArg(options::OPT_print_multi_lib)) {
762651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    const MultilibSet &Multilibs = TC.getMultilibs();
763a82318332c9e9b61d3d17371e675ca2859877ee5Daniel Dunbar
764651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (MultilibSet::const_iterator I = Multilibs.begin(), E = Multilibs.end();
765651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         I != E; ++I) {
766651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      llvm::outs() << *I << "\n";
76712cfe036d809173a25af0104844d4bb66a92252bDaniel Dunbar    }
76812cfe036d809173a25af0104844d4bb66a92252bDaniel Dunbar    return false;
76912cfe036d809173a25af0104844d4bb66a92252bDaniel Dunbar  }
77012cfe036d809173a25af0104844d4bb66a92252bDaniel Dunbar
771651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  if (C.getArgs().hasArg(options::OPT_print_multi_directory)) {
772651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    const MultilibSet &Multilibs = TC.getMultilibs();
773651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines    for (MultilibSet::const_iterator I = Multilibs.begin(), E = Multilibs.end();
774651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines         I != E; ++I) {
775651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines      if (I->gccSuffix().empty())
776651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines        llvm::