Nick Lewycky | 3fdcc6f | 2010-12-31 17:31:54 +0000 | [diff] [blame] | 1 | //===--- Compilation.cpp - Compilation Task Implementation ----------------===// |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 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 | #include "clang/Driver/Compilation.h" |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 11 | #include "clang/Driver/Action.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 12 | #include "clang/Driver/Driver.h" |
| 13 | #include "clang/Driver/DriverDiagnostic.h" |
Daniel Dunbar | 265e9ef | 2009-11-19 04:25:22 +0000 | [diff] [blame] | 14 | #include "clang/Driver/Options.h" |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 15 | #include "clang/Driver/ToolChain.h" |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Chad Rosier | c91b41a | 2012-10-31 18:31:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSwitch.h" |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 18 | #include "llvm/Option/ArgList.h" |
Rafael Espindola | da1f9cb | 2013-06-18 20:58:25 +0000 | [diff] [blame] | 19 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Program.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 24 | |
Daniel Dunbar | 1b3bb6e | 2009-03-04 20:49:20 +0000 | [diff] [blame] | 25 | using namespace clang::driver; |
Francois Pichet | 1c3199a | 2011-07-23 11:36:43 +0000 | [diff] [blame] | 26 | using namespace clang; |
Reid Kleckner | b1e25a1 | 2013-06-14 17:17:23 +0000 | [diff] [blame] | 27 | using namespace llvm::opt; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 28 | |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 29 | Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, |
| 30 | InputArgList *_Args, DerivedArgList *_TranslatedArgs) |
| 31 | : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 32 | TranslatedArgs(_TranslatedArgs), Redirects(0) { |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | Compilation::~Compilation() { |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 36 | delete TranslatedArgs; |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 37 | delete Args; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 38 | |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 39 | // Free any derived arg lists. |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 40 | for (llvm::DenseMap<std::pair<const ToolChain*, const char*>, |
| 41 | DerivedArgList*>::iterator it = TCArgs.begin(), |
| 42 | ie = TCArgs.end(); it != ie; ++it) |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 43 | if (it->second != TranslatedArgs) |
| 44 | delete it->second; |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 45 | |
| 46 | // Free the actions, if built. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); |
Daniel Dunbar | 2154923 | 2009-03-18 02:55:38 +0000 | [diff] [blame] | 48 | it != ie; ++it) |
| 49 | delete *it; |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 50 | |
| 51 | // Free redirections of stdout/stderr. |
| 52 | if (Redirects) { |
| 53 | delete Redirects[1]; |
| 54 | delete Redirects[2]; |
| 55 | delete [] Redirects; |
| 56 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 59 | const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC, |
| 60 | const char *BoundArch) { |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 61 | if (!TC) |
| 62 | TC = &DefaultToolChain; |
| 63 | |
Daniel Dunbar | 4954018 | 2009-09-09 18:36:01 +0000 | [diff] [blame] | 64 | DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)]; |
Daniel Dunbar | 279c1db | 2010-06-11 22:00:26 +0000 | [diff] [blame] | 65 | if (!Entry) { |
| 66 | Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch); |
| 67 | if (!Entry) |
| 68 | Entry = TranslatedArgs; |
| 69 | } |
Daniel Dunbar | 586dc23 | 2009-03-16 06:42:30 +0000 | [diff] [blame] | 70 | |
Daniel Dunbar | aa3e0d2 | 2009-03-18 05:58:45 +0000 | [diff] [blame] | 71 | return *Entry; |
Daniel Dunbar | 3ede8d0 | 2009-03-02 19:59:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 74 | void Compilation::PrintJob(raw_ostream &OS, const Job &J, |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 75 | const char *Terminator, bool Quote) const { |
| 76 | if (const Command *C = dyn_cast<Command>(&J)) { |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 77 | OS << " \"" << C->getExecutable() << '"'; |
| 78 | for (ArgStringList::const_iterator it = C->getArguments().begin(), |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 79 | ie = C->getArguments().end(); it != ie; ++it) { |
Daniel Dunbar | b86c5fc | 2010-03-20 08:01:53 +0000 | [diff] [blame] | 80 | OS << ' '; |
Benjamin Kramer | 5f22614 | 2011-10-06 22:53:35 +0000 | [diff] [blame] | 81 | if (!Quote && !std::strpbrk(*it, " \"\\$")) { |
Daniel Dunbar | b86c5fc | 2010-03-20 08:01:53 +0000 | [diff] [blame] | 82 | OS << *it; |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | // Quote the argument and escape shell special characters; this isn't |
| 87 | // really complete but is good enough. |
| 88 | OS << '"'; |
| 89 | for (const char *s = *it; *s; ++s) { |
| 90 | if (*s == '"' || *s == '\\' || *s == '$') |
| 91 | OS << '\\'; |
| 92 | OS << *s; |
| 93 | } |
| 94 | OS << '"'; |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 95 | } |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 96 | OS << Terminator; |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 97 | } else { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 98 | const JobList *Jobs = cast<JobList>(&J); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | for (JobList::const_iterator |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 100 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 101 | PrintJob(OS, **it, Terminator, Quote); |
Daniel Dunbar | 24b5560 | 2009-03-18 06:49:39 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Chad Rosier | c91b41a | 2012-10-31 18:31:33 +0000 | [diff] [blame] | 105 | static bool skipArg(const char *Flag, bool &SkipNextArg) { |
| 106 | StringRef FlagRef(Flag); |
| 107 | |
| 108 | // Assume we're going to see -Flag <Arg>. |
| 109 | SkipNextArg = true; |
| 110 | |
| 111 | // These flags are all of the form -Flag <Arg> and are treated as two |
| 112 | // arguments. Therefore, we need to skip the flag and the next argument. |
| 113 | bool Res = llvm::StringSwitch<bool>(Flag) |
| 114 | .Cases("-I", "-MF", "-MT", "-MQ", true) |
| 115 | .Cases("-o", "-coverage-file", "-dependency-file", true) |
Douglas Gregor | 250172a | 2013-02-07 22:59:12 +0000 | [diff] [blame] | 116 | .Cases("-fdebug-compilation-dir", "-idirafter", true) |
Chad Rosier | c91b41a | 2012-10-31 18:31:33 +0000 | [diff] [blame] | 117 | .Cases("-include", "-include-pch", "-internal-isystem", true) |
Chad Rosier | 1cd46de | 2012-10-31 21:08:30 +0000 | [diff] [blame] | 118 | .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true) |
Chad Rosier | c91b41a | 2012-10-31 18:31:33 +0000 | [diff] [blame] | 119 | .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true) |
| 120 | .Cases("-resource-dir", "-serialize-diagnostic-file", true) |
| 121 | .Case("-dwarf-debug-flags", true) |
| 122 | .Default(false); |
| 123 | |
| 124 | // Match found. |
| 125 | if (Res) |
| 126 | return Res; |
| 127 | |
| 128 | // The remaining flags are treated as a single argument. |
| 129 | SkipNextArg = false; |
| 130 | |
| 131 | // These flags are all of the form -Flag and have no second argument. |
| 132 | Res = llvm::StringSwitch<bool>(Flag) |
| 133 | .Cases("-M", "-MM", "-MG", "-MP", "-MD", true) |
| 134 | .Case("-MMD", true) |
| 135 | .Default(false); |
| 136 | |
| 137 | // Match found. |
| 138 | if (Res) |
| 139 | return Res; |
| 140 | |
| 141 | // These flags are treated as a single argument (e.g., -F<Dir>). |
| 142 | if (FlagRef.startswith("-F") || FlagRef.startswith("-I")) |
| 143 | return true; |
| 144 | |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | static bool quoteNextArg(const char *flag) { |
| 149 | return llvm::StringSwitch<bool>(flag) |
| 150 | .Case("-D", true) |
| 151 | .Default(false); |
| 152 | } |
| 153 | |
| 154 | void Compilation::PrintDiagnosticJob(raw_ostream &OS, const Job &J) const { |
| 155 | if (const Command *C = dyn_cast<Command>(&J)) { |
| 156 | OS << C->getExecutable(); |
| 157 | unsigned QuoteNextArg = 0; |
| 158 | for (ArgStringList::const_iterator it = C->getArguments().begin(), |
| 159 | ie = C->getArguments().end(); it != ie; ++it) { |
| 160 | |
| 161 | bool SkipNext; |
| 162 | if (skipArg(*it, SkipNext)) { |
| 163 | if (SkipNext) ++it; |
| 164 | continue; |
| 165 | } |
| 166 | |
| 167 | if (!QuoteNextArg) |
| 168 | QuoteNextArg = quoteNextArg(*it) ? 2 : 0; |
| 169 | |
| 170 | OS << ' '; |
| 171 | |
| 172 | if (QuoteNextArg == 1) |
| 173 | OS << '"'; |
| 174 | |
| 175 | if (!std::strpbrk(*it, " \"\\$")) { |
| 176 | OS << *it; |
| 177 | } else { |
| 178 | // Quote the argument and escape shell special characters; this isn't |
| 179 | // really complete but is good enough. |
| 180 | OS << '"'; |
| 181 | for (const char *s = *it; *s; ++s) { |
| 182 | if (*s == '"' || *s == '\\' || *s == '$') |
| 183 | OS << '\\'; |
| 184 | OS << *s; |
| 185 | } |
| 186 | OS << '"'; |
| 187 | } |
| 188 | |
| 189 | if (QuoteNextArg) { |
| 190 | if (QuoteNextArg == 1) |
| 191 | OS << '"'; |
| 192 | --QuoteNextArg; |
| 193 | } |
| 194 | } |
| 195 | OS << '\n'; |
| 196 | } else { |
| 197 | const JobList *Jobs = cast<JobList>(&J); |
| 198 | for (JobList::const_iterator |
| 199 | it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) |
| 200 | PrintDiagnosticJob(OS, **it); |
| 201 | } |
| 202 | } |
| 203 | |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 204 | bool Compilation::CleanupFile(const char *File, bool IssueErrors) const { |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame^] | 205 | std::string P(File); |
| 206 | |
| 207 | // FIXME: Why are we trying to remove files that we have not created? For |
| 208 | // example we should only try to remove a temporary assembly file if |
| 209 | // "clang -cc1" succeed in writing it. Was this a workaround for when |
| 210 | // clang was writing directly to a .s file and sometimes leaving it behind |
| 211 | // during a failure? |
| 212 | |
| 213 | // FIXME: If this is necessary, we can still try to split |
| 214 | // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the |
| 215 | // duplicated stat from is_regular_file. |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 216 | |
| 217 | // Don't try to remove files which we don't have write access to (but may be |
| 218 | // able to remove), or non-regular files. Underlying tools may have |
| 219 | // intentionally not overwritten them. |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame^] | 220 | if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File)) |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 221 | return true; |
| 222 | |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame^] | 223 | if (llvm::error_code EC = llvm::sys::fs::remove(File)) { |
| 224 | // Failure is only failure if the file exists and is "regular". We checked |
| 225 | // for it being regular before, and llvm::sys::fs::remove ignores ENOENT, |
| 226 | // so we don't need to check again. |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 227 | |
Rafael Espindola | de2b523 | 2013-06-24 17:59:44 +0000 | [diff] [blame^] | 228 | if (IssueErrors) |
| 229 | getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) |
| 230 | << EC.message(); |
| 231 | return false; |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 232 | } |
| 233 | return true; |
| 234 | } |
| 235 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | bool Compilation::CleanupFileList(const ArgStringList &Files, |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 237 | bool IssueErrors) const { |
| 238 | bool Success = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 239 | for (ArgStringList::const_iterator |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 240 | it = Files.begin(), ie = Files.end(); it != ie; ++it) |
| 241 | Success &= CleanupFile(*it, IssueErrors); |
| 242 | return Success; |
| 243 | } |
| 244 | |
| 245 | bool Compilation::CleanupFileMap(const ArgStringMap &Files, |
| 246 | const JobAction *JA, |
| 247 | bool IssueErrors) const { |
| 248 | bool Success = true; |
| 249 | for (ArgStringMap::const_iterator |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 250 | it = Files.begin(), ie = Files.end(); it != ie; ++it) { |
Edward O'Callaghan | 56eec2b | 2009-11-24 15:23:21 +0000 | [diff] [blame] | 251 | |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 252 | // If specified, only delete the files associated with the JobAction. |
| 253 | // Otherwise, delete all files in the map. |
| 254 | if (JA && it->first != JA) |
Daniel Dunbar | 8ac127a | 2011-04-25 20:43:05 +0000 | [diff] [blame] | 255 | continue; |
Chad Rosier | 9d71863 | 2013-01-24 19:14:47 +0000 | [diff] [blame] | 256 | Success &= CleanupFile(it->second, IssueErrors); |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 257 | } |
Daniel Dunbar | e530ad4 | 2009-03-18 22:16:03 +0000 | [diff] [blame] | 258 | return Success; |
| 259 | } |
| 260 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 261 | int Compilation::ExecuteCommand(const Command &C, |
| 262 | const Command *&FailingCommand) const { |
Rafael Espindola | 8ce9054 | 2013-06-24 16:46:15 +0000 | [diff] [blame] | 263 | std::string Prog(C.getExecutable()); |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 264 | const char **Argv = new const char*[C.getArguments().size() + 2]; |
| 265 | Argv[0] = C.getExecutable(); |
| 266 | std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1); |
| 267 | Argv[C.getArguments().size() + 1] = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 269 | if ((getDriver().CCCEcho || getDriver().CCPrintOptions || |
| 270 | getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 271 | raw_ostream *OS = &llvm::errs(); |
Daniel Dunbar | 4c00fcd | 2010-03-20 08:01:59 +0000 | [diff] [blame] | 272 | |
| 273 | // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the |
| 274 | // output stream. |
| 275 | if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { |
| 276 | std::string Error; |
| 277 | OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, |
| 278 | Error, |
| 279 | llvm::raw_fd_ostream::F_Append); |
| 280 | if (!Error.empty()) { |
| 281 | getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) |
| 282 | << Error; |
| 283 | FailingCommand = &C; |
| 284 | delete OS; |
| 285 | return 1; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | if (getDriver().CCPrintOptions) |
| 290 | *OS << "[Logging clang options]"; |
| 291 | |
| 292 | PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions); |
| 293 | |
| 294 | if (OS != &llvm::errs()) |
| 295 | delete OS; |
| 296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 298 | std::string Error; |
Chad Rosier | c48d575 | 2013-03-26 23:41:30 +0000 | [diff] [blame] | 299 | bool ExecutionFailed; |
Rafael Espindola | 8ce9054 | 2013-06-24 16:46:15 +0000 | [diff] [blame] | 300 | int Res = llvm::sys::ExecuteAndWait(Prog, Argv, /*env*/ 0, Redirects, |
Rafael Espindola | a603569 | 2013-06-12 20:44:26 +0000 | [diff] [blame] | 301 | /*secondsToWait*/ 0, /*memoryLimit*/ 0, |
| 302 | &Error, &ExecutionFailed); |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 303 | if (!Error.empty()) { |
| 304 | assert(Res && "Error string set with 0 result code!"); |
| 305 | getDriver().Diag(clang::diag::err_drv_command_failure) << Error; |
| 306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Daniel Dunbar | 31c11eb | 2009-07-01 19:14:39 +0000 | [diff] [blame] | 308 | if (Res) |
| 309 | FailingCommand = &C; |
| 310 | |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 311 | delete[] Argv; |
Chad Rosier | c48d575 | 2013-03-26 23:41:30 +0000 | [diff] [blame] | 312 | return ExecutionFailed ? 1 : Res; |
Daniel Dunbar | ceafbc8 | 2009-03-19 08:01:45 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 315 | typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList; |
| 316 | |
| 317 | static bool ActionFailed(const Action *A, |
| 318 | const FailingCommandList &FailingCommands) { |
| 319 | |
| 320 | if (FailingCommands.empty()) |
| 321 | return false; |
| 322 | |
| 323 | for (FailingCommandList::const_iterator CI = FailingCommands.begin(), |
| 324 | CE = FailingCommands.end(); CI != CE; ++CI) |
| 325 | if (A == &(CI->second->getSource())) |
| 326 | return true; |
| 327 | |
| 328 | for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI) |
| 329 | if (ActionFailed(*AI, FailingCommands)) |
| 330 | return true; |
| 331 | |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | static bool InputsOk(const Command &C, |
| 336 | const FailingCommandList &FailingCommands) { |
| 337 | return !ActionFailed(&C.getSource(), FailingCommands); |
| 338 | } |
| 339 | |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 340 | void Compilation::ExecuteJob(const Job &J, |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 341 | FailingCommandList &FailingCommands) const { |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 342 | if (const Command *C = dyn_cast<Command>(&J)) { |
Chad Rosier | 4c4df45 | 2013-02-27 18:46:04 +0000 | [diff] [blame] | 343 | if (!InputsOk(*C, FailingCommands)) |
| 344 | return; |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 345 | const Command *FailingCommand = 0; |
| 346 | if (int Res = ExecuteCommand(*C, FailingCommand)) |
| 347 | FailingCommands.push_back(std::make_pair(Res, FailingCommand)); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 348 | } else { |
| 349 | const JobList *Jobs = cast<JobList>(&J); |
Chad Rosier | a16355c | 2013-01-29 20:15:05 +0000 | [diff] [blame] | 350 | for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end(); |
| 351 | it != ie; ++it) |
| 352 | ExecuteJob(**it, FailingCommands); |
Daniel Dunbar | 49b98e7 | 2009-03-18 22:44:24 +0000 | [diff] [blame] | 353 | } |
| 354 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 355 | |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 356 | void Compilation::initCompilationForDiagnostics() { |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 357 | // Free actions and jobs. |
| 358 | DeleteContainerPointers(Actions); |
| 359 | Jobs.clear(); |
| 360 | |
| 361 | // Clear temporary/results file lists. |
| 362 | TempFiles.clear(); |
| 363 | ResultFiles.clear(); |
Chad Rosier | 8425a54 | 2013-01-29 23:57:10 +0000 | [diff] [blame] | 364 | FailureResultFiles.clear(); |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 365 | |
| 366 | // Remove any user specified output. Claim any unclaimed arguments, so as |
| 367 | // to avoid emitting warnings about unused args. |
Peter Collingbourne | 54db68b | 2011-11-06 00:40:05 +0000 | [diff] [blame] | 368 | OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD, |
| 369 | options::OPT_MMD }; |
Chad Rosier | f9e156c | 2012-05-03 21:25:34 +0000 | [diff] [blame] | 370 | for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) { |
Peter Collingbourne | 54db68b | 2011-11-06 00:40:05 +0000 | [diff] [blame] | 371 | if (TranslatedArgs->hasArg(OutputOpts[i])) |
| 372 | TranslatedArgs->eraseArg(OutputOpts[i]); |
| 373 | } |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 374 | TranslatedArgs->ClaimAllArgs(); |
| 375 | |
| 376 | // Redirect stdout/stderr to /dev/null. |
Rafael Espindola | 57a3bbf | 2013-06-13 20:08:52 +0000 | [diff] [blame] | 377 | Redirects = new const StringRef*[3](); |
| 378 | Redirects[0] = 0; |
| 379 | Redirects[1] = new const StringRef(); |
| 380 | Redirects[2] = new const StringRef(); |
Chad Rosier | 2b81910 | 2011-08-02 17:58:04 +0000 | [diff] [blame] | 381 | } |
Sebastian Pop | 4762a2d | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 382 | |
Dmitri Gribenko | c4a7790 | 2012-11-15 14:28:07 +0000 | [diff] [blame] | 383 | StringRef Compilation::getSysRoot() const { |
Sebastian Pop | 4762a2d | 2012-04-16 04:16:43 +0000 | [diff] [blame] | 384 | return getDriver().SysRoot; |
| 385 | } |