1 //===--- Job.cpp - Command to Execute -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Job.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/Program.h"
15 #include "llvm/Support/raw_ostream.h"
17 using namespace clang::driver;
18 using llvm::raw_ostream;
19 using llvm::StringRef;
23 Command::Command(const Action &_Source, const Tool &_Creator,
24 const char *_Executable,
25 const ArgStringList &_Arguments)
26 : Job(CommandClass), Source(_Source), Creator(_Creator),
27 Executable(_Executable), Arguments(_Arguments) {}
29 static int skipArgs(const char *Flag) {
30 // These flags are all of the form -Flag <Arg> and are treated as two
31 // arguments. Therefore, we need to skip the flag and the next argument.
32 bool Res = llvm::StringSwitch<bool>(Flag)
33 .Cases("-I", "-MF", "-MT", "-MQ", true)
34 .Cases("-o", "-coverage-file", "-dependency-file", true)
35 .Cases("-fdebug-compilation-dir", "-idirafter", true)
36 .Cases("-include", "-include-pch", "-internal-isystem", true)
37 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
38 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
39 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
40 .Case("-dwarf-debug-flags", true)
47 // The remaining flags are treated as a single argument.
49 // These flags are all of the form -Flag and have no second argument.
50 Res = llvm::StringSwitch<bool>(Flag)
51 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
59 // These flags are treated as a single argument (e.g., -F<Dir>).
60 StringRef FlagRef(Flag);
61 if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
67 static bool quoteNextArg(const char *flag) {
68 return llvm::StringSwitch<bool>(flag)
73 static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
74 const bool Escape = std::strpbrk(Arg, "\"\\$");
76 if (!Quote && !Escape) {
81 // Quote and escape. This isn't really complete, but good enough.
83 while (const char c = *Arg++) {
84 if (c == '"' || c == '\\' || c == '$')
91 void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
92 bool CrashReport) const {
93 OS << " \"" << Executable << '"';
95 for (size_t i = 0, e = Arguments.size(); i < e; ++i) {
96 const char *const Arg = Arguments[i];
99 if (int Skip = skipArgs(Arg)) {
106 PrintArg(OS, Arg, Quote);
108 if (CrashReport && quoteNextArg(Arg) && i + 1 < e) {
110 PrintArg(OS, Arguments[++i], true);
116 int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
117 bool *ExecutionFailed) const {
118 SmallVector<const char*, 128> Argv;
119 Argv.push_back(Executable);
120 for (size_t i = 0, e = Arguments.size(); i != e; ++i)
121 Argv.push_back(Arguments[i]);
124 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ 0,
125 Redirects, /*secondsToWait*/ 0,
126 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
129 FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
130 const char *Executable_,
131 const ArgStringList &Arguments_,
133 : Command(Source_, Creator_, Executable_, Arguments_), Fallback(Fallback_) {
136 void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
137 bool Quote, bool CrashReport) const {
138 Command::Print(OS, "", Quote, CrashReport);
140 Fallback->Print(OS, Terminator, Quote, CrashReport);
143 static bool ShouldFallback(int ExitCode) {
144 // FIXME: We really just want to fall back for internal errors, such
145 // as when some symbol cannot be mangled, when we should be able to
146 // parse something but can't, etc.
147 return ExitCode != 0;
150 int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
151 bool *ExecutionFailed) const {
152 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
153 if (!ShouldFallback(PrimaryStatus))
154 return PrimaryStatus;
156 // Clear ExecutionFailed and ErrMsg before falling back.
160 *ExecutionFailed = false;
162 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
163 return SecondaryStatus;
166 JobList::JobList() : Job(JobListClass) {}
168 JobList::~JobList() {
169 for (iterator it = begin(), ie = end(); it != ie; ++it)
173 void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
174 bool CrashReport) const {
175 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
176 (*it)->Print(OS, Terminator, Quote, CrashReport);
179 void JobList::clear() {
180 DeleteContainerPointers(Jobs);