blob: bd1a9bd8e3ebadee63922c69a0a0f53687099113 [file] [log] [blame]
Eugene Zelenko5e4511c2018-03-20 21:08:59 +00001//===- Job.cpp - Command to Execute ---------------------------------------===//
Daniel Dunbar313c2912009-03-13 23:36:33 +00002//
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
Mehdi Amini9670f842016-07-18 19:02:11 +000010#include "clang/Driver/Job.h"
Justin Bognerd3371d82015-07-17 03:35:54 +000011#include "InputInfo.h"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000012#include "clang/Basic/LLVM.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000013#include "clang/Driver/Driver.h"
14#include "clang/Driver/DriverDiagnostic.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000015#include "clang/Driver/Tool.h"
16#include "clang/Driver/ToolChain.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000017#include "llvm/ADT/ArrayRef.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000018#include "llvm/ADT/SmallString.h"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000019#include "llvm/ADT/SmallVector.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000020#include "llvm/ADT/StringRef.h"
Reid Kleckner0290c9c2014-09-15 17:45:39 +000021#include "llvm/ADT/StringSet.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000022#include "llvm/ADT/StringSwitch.h"
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000023#include "llvm/Support/FileSystem.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000024#include "llvm/Support/Path.h"
Hans Wennborge8677ef2013-09-12 18:35:08 +000025#include "llvm/Support/Program.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000026#include "llvm/Support/raw_ostream.h"
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000027#include <algorithm>
Daniel Dunbar313c2912009-03-13 23:36:33 +000028#include <cassert>
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000029#include <cstddef>
30#include <string>
31#include <system_error>
32#include <utility>
33
34using namespace clang;
35using namespace driver;
Daniel Dunbar313c2912009-03-13 23:36:33 +000036
Justin Bogner94817612015-07-02 22:52:04 +000037Command::Command(const Action &Source, const Tool &Creator,
Justin Bognerd3371d82015-07-17 03:35:54 +000038 const char *Executable, const ArgStringList &Arguments,
39 ArrayRef<InputInfo> Inputs)
Justin Bogner0cd92482015-07-02 22:52:08 +000040 : Source(Source), Creator(Creator), Executable(Executable),
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000041 Arguments(Arguments) {
Justin Bognerd3371d82015-07-17 03:35:54 +000042 for (const auto &II : Inputs)
43 if (II.isFilename())
44 InputFilenames.push_back(II.getFilename());
45}
Daniel Dunbar313c2912009-03-13 23:36:33 +000046
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000047/// Check if the compiler flag in question should be skipped when
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000048/// emitting a reproducer. Also track how many arguments it has and if the
49/// option is some kind of include path.
50static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
51 bool &IsInclude) {
52 SkipNum = 2;
Hans Wennborgb212b342013-09-12 18:23:34 +000053 // These flags are all of the form -Flag <Arg> and are treated as two
54 // arguments. Therefore, we need to skip the flag and the next argument.
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000055 bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
Bruno Cardoso Lopesb810b022016-04-04 20:26:57 +000056 .Cases("-MF", "-MT", "-MQ", "-serialize-diagnostic-file", true)
Aaron Ballman6c60ed52017-05-01 13:05:04 +000057 .Cases("-o", "-dependency-file", true)
Bruno Cardoso Lopes7aff2bb2016-12-12 19:28:25 +000058 .Cases("-fdebug-compilation-dir", "-diagnostic-log-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000059 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000060 .Default(false);
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000061 if (ShouldSkip)
62 return true;
Hans Wennborgb212b342013-09-12 18:23:34 +000063
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000064 // Some include flags shouldn't be skipped if we have a crash VFS
65 IsInclude = llvm::StringSwitch<bool>(Flag)
66 .Cases("-include", "-header-include-file", true)
67 .Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
68 .Cases("-internal-externc-isystem", "-iprefix", true)
69 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
70 .Cases("-isysroot", "-I", "-F", "-resource-dir", true)
Erich Keane6c483592017-12-11 18:14:51 +000071 .Cases("-iframework", "-include-pch", true)
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000072 .Default(false);
73 if (IsInclude)
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000074 return !HaveCrashVFS;
Hans Wennborgb212b342013-09-12 18:23:34 +000075
76 // The remaining flags are treated as a single argument.
77
78 // These flags are all of the form -Flag and have no second argument.
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000079 ShouldSkip = llvm::StringSwitch<bool>(Flag)
Richard Smithdaae9522017-10-20 00:25:07 +000080 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
81 .Case("-MMD", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000082 .Default(false);
83
84 // Match found.
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000085 SkipNum = 1;
86 if (ShouldSkip)
87 return true;
Hans Wennborgb212b342013-09-12 18:23:34 +000088
89 // These flags are treated as a single argument (e.g., -F<Dir>).
90 StringRef FlagRef(Flag);
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000091 IsInclude = FlagRef.startswith("-F") || FlagRef.startswith("-I");
92 if (IsInclude)
Eugene Zelenko5e4511c2018-03-20 21:08:59 +000093 return !HaveCrashVFS;
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000094 if (FlagRef.startswith("-fmodules-cache-path="))
95 return true;
Hans Wennborgb212b342013-09-12 18:23:34 +000096
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +000097 SkipNum = 0;
98 return false;
Hans Wennborgb212b342013-09-12 18:23:34 +000099}
100
Mehdi Amini5bf825b2016-10-08 01:38:43 +0000101void Command::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
102 const bool Escape = Arg.find_first_of("\"\\$") != StringRef::npos;
Hans Wennborgb212b342013-09-12 18:23:34 +0000103
104 if (!Quote && !Escape) {
105 OS << Arg;
106 return;
107 }
108
109 // Quote and escape. This isn't really complete, but good enough.
110 OS << '"';
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000111 for (const auto c : Arg) {
Hans Wennborgb212b342013-09-12 18:23:34 +0000112 if (c == '"' || c == '\\' || c == '$')
113 OS << '\\';
114 OS << c;
115 }
116 OS << '"';
117}
118
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000119void Command::writeResponseFile(raw_ostream &OS) const {
120 // In a file list, we only write the set of inputs to the response file
121 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000122 for (const auto *Arg : InputFileList) {
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000123 OS << Arg << '\n';
124 }
125 return;
126 }
127
Reid Klecknere2d03442015-07-15 22:42:37 +0000128 // In regular response files, we send all arguments to the response file.
129 // Wrapping all arguments in double quotes ensures that both Unix tools and
130 // Windows tools understand the response file.
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000131 for (const auto *Arg : Arguments) {
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000132 OS << '"';
133
134 for (; *Arg != '\0'; Arg++) {
135 if (*Arg == '\"' || *Arg == '\\') {
136 OS << '\\';
137 }
138 OS << *Arg;
139 }
140
141 OS << "\" ";
142 }
143}
144
145void Command::buildArgvForResponseFile(
146 llvm::SmallVectorImpl<const char *> &Out) const {
147 // When not a file list, all arguments are sent to the response file.
148 // This leaves us to set the argv to a single parameter, requesting the tool
149 // to read the response file.
150 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
151 Out.push_back(Executable);
152 Out.push_back(ResponseFileFlag.c_str());
153 return;
154 }
155
156 llvm::StringSet<> Inputs;
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000157 for (const auto *InputName : InputFileList)
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000158 Inputs.insert(InputName);
159 Out.push_back(Executable);
160 // In a file list, build args vector ignoring parameters that will go in the
161 // response file (elements of the InputFileList vector)
162 bool FirstInput = true;
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000163 for (const auto *Arg : Arguments) {
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000164 if (Inputs.count(Arg) == 0) {
165 Out.push_back(Arg);
166 } else if (FirstInput) {
167 FirstInput = false;
168 Out.push_back(Creator.getResponseFileFlag());
169 Out.push_back(ResponseFile);
170 }
171 }
172}
173
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000174/// Rewrite relative include-like flag paths to absolute ones.
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +0000175static void
176rewriteIncludes(const llvm::ArrayRef<const char *> &Args, size_t Idx,
177 size_t NumArgs,
178 llvm::SmallVectorImpl<llvm::SmallString<128>> &IncFlags) {
179 using namespace llvm;
180 using namespace sys;
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000181
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +0000182 auto getAbsPath = [](StringRef InInc, SmallVectorImpl<char> &OutInc) -> bool {
183 if (path::is_absolute(InInc)) // Nothing to do here...
184 return false;
185 std::error_code EC = fs::current_path(OutInc);
186 if (EC)
187 return false;
188 path::append(OutInc, InInc);
189 return true;
190 };
191
192 SmallString<128> NewInc;
193 if (NumArgs == 1) {
194 StringRef FlagRef(Args[Idx + NumArgs - 1]);
195 assert((FlagRef.startswith("-F") || FlagRef.startswith("-I")) &&
196 "Expecting -I or -F");
197 StringRef Inc = FlagRef.slice(2, StringRef::npos);
198 if (getAbsPath(Inc, NewInc)) {
199 SmallString<128> NewArg(FlagRef.slice(0, 2));
200 NewArg += NewInc;
201 IncFlags.push_back(std::move(NewArg));
202 }
203 return;
204 }
205
206 assert(NumArgs == 2 && "Not expecting more than two arguments");
207 StringRef Inc(Args[Idx + NumArgs - 1]);
208 if (!getAbsPath(Inc, NewInc))
209 return;
210 IncFlags.push_back(SmallString<128>(Args[Idx]));
211 IncFlags.push_back(std::move(NewInc));
212}
213
Hans Wennborgb212b342013-09-12 18:23:34 +0000214void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000215 CrashReportInfo *CrashInfo) const {
Reid Kleckner822434d2014-08-05 20:49:12 +0000216 // Always quote the exe.
Reid Kleckner4f1fc352014-08-07 00:05:00 +0000217 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000218 printArg(OS, Executable, /*Quote=*/true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000219
Eugene Zelenko5e4511c2018-03-20 21:08:59 +0000220 ArrayRef<const char *> Args = Arguments;
221 SmallVector<const char *, 128> ArgsRespFile;
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000222 if (ResponseFile != nullptr) {
223 buildArgvForResponseFile(ArgsRespFile);
224 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
225 }
226
Justin Bogner0cb14752015-03-12 00:52:56 +0000227 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000228 for (size_t i = 0, e = Args.size(); i < e; ++i) {
229 const char *const Arg = Args[i];
Hans Wennborgb212b342013-09-12 18:23:34 +0000230
Justin Bogner25645152014-10-21 17:24:44 +0000231 if (CrashInfo) {
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +0000232 int NumArgs = 0;
233 bool IsInclude = false;
234 if (skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
235 i += NumArgs - 1;
Hans Wennborgb212b342013-09-12 18:23:34 +0000236 continue;
Justin Bognerd3371d82015-07-17 03:35:54 +0000237 }
Bruno Cardoso Lopese3a0aef2016-12-09 02:22:47 +0000238
239 // Relative includes need to be expanded to absolute paths.
240 if (HaveCrashVFS && IsInclude) {
241 SmallVector<SmallString<128>, 2> NewIncFlags;
242 rewriteIncludes(Args, i, NumArgs, NewIncFlags);
243 if (!NewIncFlags.empty()) {
244 for (auto &F : NewIncFlags) {
245 OS << ' ';
246 printArg(OS, F.c_str(), Quote);
247 }
248 i += NumArgs - 1;
249 continue;
250 }
251 }
252
Justin Bognerd3371d82015-07-17 03:35:54 +0000253 auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
254 [&Arg](StringRef IF) { return IF == Arg; });
255 if (Found != InputFilenames.end() &&
256 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
Justin Bogner25645152014-10-21 17:24:44 +0000257 // Replace the input file name with the crashinfo's file name.
258 OS << ' ';
259 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
Malcolm Parsonsf76f6502016-11-02 10:39:27 +0000260 printArg(OS, ShortName.str(), Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000261 continue;
Hans Wennborgb212b342013-09-12 18:23:34 +0000262 }
263 }
264
265 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000266 printArg(OS, Arg, Quote);
Hans Wennborgb212b342013-09-12 18:23:34 +0000267 }
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000268
Justin Bogner0cb14752015-03-12 00:52:56 +0000269 if (CrashInfo && HaveCrashVFS) {
Justin Bogner25645152014-10-21 17:24:44 +0000270 OS << ' ';
Justin Bognered9cbe02015-07-09 06:58:31 +0000271 printArg(OS, "-ivfsoverlay", Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000272 OS << ' ';
Malcolm Parsonsf76f6502016-11-02 10:39:27 +0000273 printArg(OS, CrashInfo->VFSPath.str(), Quote);
Bruno Cardoso Lopesf854b042016-04-01 17:39:08 +0000274
Bruno Cardoso Lopes1c108722016-12-09 03:11:48 +0000275 // The leftover modules from the crash are stored in
276 // <name>.cache/vfs/modules
277 // Leave it untouched for pcm inspection and provide a clean/empty dir
278 // path to contain the future generated module cache:
279 // <name>.cache/vfs/repro-modules
Bruno Cardoso Lopesf854b042016-04-01 17:39:08 +0000280 SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
281 llvm::sys::path::parent_path(CrashInfo->VFSPath));
Bruno Cardoso Lopes1c108722016-12-09 03:11:48 +0000282 llvm::sys::path::append(RelModCacheDir, "repro-modules");
Bruno Cardoso Lopesf854b042016-04-01 17:39:08 +0000283
284 std::string ModCachePath = "-fmodules-cache-path=";
285 ModCachePath.append(RelModCacheDir.c_str());
286
287 OS << ' ';
Malcolm Parsonsf76f6502016-11-02 10:39:27 +0000288 printArg(OS, ModCachePath, Quote);
Justin Bogner25645152014-10-21 17:24:44 +0000289 }
290
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000291 if (ResponseFile != nullptr) {
292 OS << "\n Arguments passed via response file:\n";
293 writeResponseFile(OS);
294 // Avoiding duplicated newline terminator, since FileLists are
295 // newline-separated.
296 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
297 OS << "\n";
298 OS << " (end of response file)";
299 }
300
Hans Wennborgb212b342013-09-12 18:23:34 +0000301 OS << Terminator;
302}
303
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000304void Command::setResponseFile(const char *FileName) {
305 ResponseFile = FileName;
306 ResponseFileFlag = Creator.getResponseFileFlag();
307 ResponseFileFlag += FileName;
308}
309
Zachary Turnera0f96be2017-03-17 16:24:34 +0000310void Command::setEnvironment(llvm::ArrayRef<const char *> NewEnvironment) {
311 Environment.reserve(NewEnvironment.size() + 1);
312 Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
313 Environment.push_back(nullptr);
314}
315
Alexander Kornienko1e531952017-09-13 17:45:51 +0000316int Command::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
Alexander Kornienko9707aa72017-09-13 17:03:58 +0000317 std::string *ErrMsg, bool *ExecutionFailed) const {
Hans Wennborge8677ef2013-09-12 18:35:08 +0000318 SmallVector<const char*, 128> Argv;
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000319
Zachary Turner08426e12018-06-12 17:43:52 +0000320 Optional<ArrayRef<StringRef>> Env;
Zachary Turner4ca94322018-07-03 18:12:39 +0000321 std::vector<StringRef> ArgvVectorStorage;
Zachary Turner08426e12018-06-12 17:43:52 +0000322 if (!Environment.empty()) {
Zachary Turnera0f96be2017-03-17 16:24:34 +0000323 assert(Environment.back() == nullptr &&
324 "Environment vector should be null-terminated by now");
Zachary Turner4ca94322018-07-03 18:12:39 +0000325 ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
326 Env = makeArrayRef(ArgvVectorStorage);
Zachary Turnera0f96be2017-03-17 16:24:34 +0000327 }
328
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000329 if (ResponseFile == nullptr) {
330 Argv.push_back(Executable);
Benjamin Kramerf9890422015-02-17 16:48:30 +0000331 Argv.append(Arguments.begin(), Arguments.end());
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000332 Argv.push_back(nullptr);
333
Zachary Turner08426e12018-06-12 17:43:52 +0000334 auto Args = llvm::toStringRefArray(Argv.data());
Zachary Turnera0f96be2017-03-17 16:24:34 +0000335 return llvm::sys::ExecuteAndWait(
Zachary Turner08426e12018-06-12 17:43:52 +0000336 Executable, Args, Env, Redirects, /*secondsToWait*/ 0,
Zachary Turnera0f96be2017-03-17 16:24:34 +0000337 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000338 }
339
340 // We need to put arguments in a response file (command is too large)
341 // Open stream to store the response file contents
342 std::string RespContents;
343 llvm::raw_string_ostream SS(RespContents);
344
345 // Write file contents and build the Argv vector
346 writeResponseFile(SS);
347 buildArgvForResponseFile(Argv);
Craig Topper92fc2df2014-05-17 16:56:41 +0000348 Argv.push_back(nullptr);
Reid Kleckner0290c9c2014-09-15 17:45:39 +0000349 SS.flush();
350
351 // Save the response file in the appropriate encoding
352 if (std::error_code EC = writeFileWithEncoding(
353 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
354 if (ErrMsg)
355 *ErrMsg = EC.message();
356 if (ExecutionFailed)
357 *ExecutionFailed = true;
358 return -1;
359 }
Hans Wennborge8677ef2013-09-12 18:35:08 +0000360
Zachary Turner08426e12018-06-12 17:43:52 +0000361 auto Args = llvm::toStringRefArray(Argv.data());
362 return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
Zachary Turnera0f96be2017-03-17 16:24:34 +0000363 /*secondsToWait*/ 0,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000364 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
365}
366
Hans Wennborg87cfa712013-09-19 20:32:16 +0000367FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
368 const char *Executable_,
369 const ArgStringList &Arguments_,
Justin Bognerd3371d82015-07-17 03:35:54 +0000370 ArrayRef<InputInfo> Inputs,
David Blaikiec11bf802014-09-04 16:04:28 +0000371 std::unique_ptr<Command> Fallback_)
Justin Bognerd3371d82015-07-17 03:35:54 +0000372 : Command(Source_, Creator_, Executable_, Arguments_, Inputs),
David Blaikiec11bf802014-09-04 16:04:28 +0000373 Fallback(std::move(Fallback_)) {}
Hans Wennborg87cfa712013-09-19 20:32:16 +0000374
375void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
Justin Bogner25645152014-10-21 17:24:44 +0000376 bool Quote, CrashReportInfo *CrashInfo) const {
377 Command::Print(OS, "", Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000378 OS << " ||";
Justin Bogner25645152014-10-21 17:24:44 +0000379 Fallback->Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborg87cfa712013-09-19 20:32:16 +0000380}
381
382static bool ShouldFallback(int ExitCode) {
383 // FIXME: We really just want to fall back for internal errors, such
384 // as when some symbol cannot be mangled, when we should be able to
385 // parse something but can't, etc.
386 return ExitCode != 0;
387}
388
Alexander Kornienko1e531952017-09-13 17:45:51 +0000389int FallbackCommand::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
Alexander Kornienko9707aa72017-09-13 17:03:58 +0000390 std::string *ErrMsg, bool *ExecutionFailed) const {
Hans Wennborg87cfa712013-09-19 20:32:16 +0000391 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
392 if (!ShouldFallback(PrimaryStatus))
393 return PrimaryStatus;
394
395 // Clear ExecutionFailed and ErrMsg before falling back.
396 if (ErrMsg)
397 ErrMsg->clear();
398 if (ExecutionFailed)
399 *ExecutionFailed = false;
400
Hans Wennborgc5afd062014-02-18 21:42:51 +0000401 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000402 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000403
Hans Wennborg87cfa712013-09-19 20:32:16 +0000404 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
405 return SecondaryStatus;
406}
407
Nico Weber2ca4be92016-03-01 23:16:44 +0000408ForceSuccessCommand::ForceSuccessCommand(const Action &Source_,
409 const Tool &Creator_,
410 const char *Executable_,
411 const ArgStringList &Arguments_,
412 ArrayRef<InputInfo> Inputs)
413 : Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
414
415void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
416 bool Quote, CrashReportInfo *CrashInfo) const {
417 Command::Print(OS, "", Quote, CrashInfo);
418 OS << " || (exit 0)" << Terminator;
419}
420
Alexander Kornienko1e531952017-09-13 17:45:51 +0000421int ForceSuccessCommand::Execute(ArrayRef<llvm::Optional<StringRef>> Redirects,
Nico Weber2ca4be92016-03-01 23:16:44 +0000422 std::string *ErrMsg,
423 bool *ExecutionFailed) const {
424 int Status = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
425 (void)Status;
426 if (ExecutionFailed)
427 *ExecutionFailed = false;
428 return 0;
429}
430
Hans Wennborgb212b342013-09-12 18:23:34 +0000431void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
Justin Bogner25645152014-10-21 17:24:44 +0000432 CrashReportInfo *CrashInfo) const {
Justin Bogneraab97922014-10-03 01:04:53 +0000433 for (const auto &Job : *this)
Justin Bogner25645152014-10-21 17:24:44 +0000434 Job.Print(OS, Terminator, Quote, CrashInfo);
Hans Wennborgb212b342013-09-12 18:23:34 +0000435}
436
David Blaikiec11bf802014-09-04 16:04:28 +0000437void JobList::clear() { Jobs.clear(); }