blob: fdf730d881b0e3d40fe04e2ae9d231f52b1021b7 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +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
Hans Wennborgc5afd062014-02-18 21:42:51 +000010#include "clang/Driver/Driver.h"
11#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000012#include "clang/Driver/Job.h"
Hans Wennborgc5afd062014-02-18 21:42:51 +000013#include "clang/Driver/Tool.h"
14#include "clang/Driver/ToolChain.h"
Chad Rosierbe10f982011-08-02 17:58:04 +000015#include "llvm/ADT/STLExtras.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/StringSwitch.h"
Hans Wennborge8677ef2013-09-12 18:35:08 +000018#include "llvm/Support/Program.h"
Hans Wennborgb212b342013-09-12 18:23:34 +000019#include "llvm/Support/raw_ostream.h"
Daniel Dunbar313c2912009-03-13 23:36:33 +000020#include <cassert>
21using namespace clang::driver;
Hans Wennborgb212b342013-09-12 18:23:34 +000022using llvm::raw_ostream;
23using llvm::StringRef;
Daniel Dunbar313c2912009-03-13 23:36:33 +000024
25Job::~Job() {}
26
Daniel Dunbar120c77e2009-12-02 03:23:25 +000027Command::Command(const Action &_Source, const Tool &_Creator,
Hans Wennborg0daa12252013-09-18 00:21:51 +000028 const char *_Executable,
Hans Wennborge693e842013-09-18 00:41:15 +000029 const ArgStringList &_Arguments)
Reid Kleckner898229a2013-06-14 17:17:23 +000030 : Job(CommandClass), Source(_Source), Creator(_Creator),
31 Executable(_Executable), Arguments(_Arguments) {}
Daniel Dunbar313c2912009-03-13 23:36:33 +000032
Hans Wennborgb212b342013-09-12 18:23:34 +000033static int skipArgs(const char *Flag) {
34 // These flags are all of the form -Flag <Arg> and are treated as two
35 // arguments. Therefore, we need to skip the flag and the next argument.
36 bool Res = llvm::StringSwitch<bool>(Flag)
37 .Cases("-I", "-MF", "-MT", "-MQ", true)
38 .Cases("-o", "-coverage-file", "-dependency-file", true)
39 .Cases("-fdebug-compilation-dir", "-idirafter", true)
40 .Cases("-include", "-include-pch", "-internal-isystem", true)
41 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
42 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
43 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
Justin Bogner61c0e432014-06-22 20:35:10 +000044 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
Hans Wennborgb212b342013-09-12 18:23:34 +000045 .Default(false);
46
47 // Match found.
48 if (Res)
49 return 2;
50
51 // The remaining flags are treated as a single argument.
52
53 // These flags are all of the form -Flag and have no second argument.
54 Res = llvm::StringSwitch<bool>(Flag)
55 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
56 .Case("-MMD", true)
57 .Default(false);
58
59 // Match found.
60 if (Res)
61 return 1;
62
63 // These flags are treated as a single argument (e.g., -F<Dir>).
64 StringRef FlagRef(Flag);
Justin Bognerf893a652014-04-22 21:30:17 +000065 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
66 FlagRef.startswith("-fmodules-cache-path="))
Hans Wennborgb212b342013-09-12 18:23:34 +000067 return 1;
68
69 return 0;
70}
71
72static bool quoteNextArg(const char *flag) {
73 return llvm::StringSwitch<bool>(flag)
74 .Case("-D", true)
75 .Default(false);
76}
77
78static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
79 const bool Escape = std::strpbrk(Arg, "\"\\$");
80
81 if (!Quote && !Escape) {
82 OS << Arg;
83 return;
84 }
85
86 // Quote and escape. This isn't really complete, but good enough.
87 OS << '"';
88 while (const char c = *Arg++) {
89 if (c == '"' || c == '\\' || c == '$')
90 OS << '\\';
91 OS << c;
92 }
93 OS << '"';
94}
95
96void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
97 bool CrashReport) const {
Reid Kleckner822434d2014-08-05 20:49:12 +000098 // Always quote the exe.
99 PrintArg(OS, Executable, /*Quote=*/true);
Hans Wennborgb212b342013-09-12 18:23:34 +0000100
101 for (size_t i = 0, e = Arguments.size(); i < e; ++i) {
102 const char *const Arg = Arguments[i];
103
104 if (CrashReport) {
105 if (int Skip = skipArgs(Arg)) {
106 i += Skip - 1;
107 continue;
108 }
109 }
110
111 OS << ' ';
112 PrintArg(OS, Arg, Quote);
113
114 if (CrashReport && quoteNextArg(Arg) && i + 1 < e) {
115 OS << ' ';
116 PrintArg(OS, Arguments[++i], true);
117 }
118 }
119 OS << Terminator;
120}
121
Hans Wennborge693e842013-09-18 00:41:15 +0000122int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000123 bool *ExecutionFailed) const {
124 SmallVector<const char*, 128> Argv;
125 Argv.push_back(Executable);
126 for (size_t i = 0, e = Arguments.size(); i != e; ++i)
127 Argv.push_back(Arguments[i]);
Craig Topper92fc2df2014-05-17 16:56:41 +0000128 Argv.push_back(nullptr);
Hans Wennborge8677ef2013-09-12 18:35:08 +0000129
Craig Topper92fc2df2014-05-17 16:56:41 +0000130 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
Hans Wennborge8677ef2013-09-12 18:35:08 +0000131 Redirects, /*secondsToWait*/ 0,
132 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
133}
134
Hans Wennborg87cfa712013-09-19 20:32:16 +0000135FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
136 const char *Executable_,
137 const ArgStringList &Arguments_,
138 Command *Fallback_)
139 : Command(Source_, Creator_, Executable_, Arguments_), Fallback(Fallback_) {
140}
141
142void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
143 bool Quote, bool CrashReport) const {
144 Command::Print(OS, "", Quote, CrashReport);
145 OS << " ||";
146 Fallback->Print(OS, Terminator, Quote, CrashReport);
147}
148
149static bool ShouldFallback(int ExitCode) {
150 // FIXME: We really just want to fall back for internal errors, such
151 // as when some symbol cannot be mangled, when we should be able to
152 // parse something but can't, etc.
153 return ExitCode != 0;
154}
155
156int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
157 bool *ExecutionFailed) const {
158 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
159 if (!ShouldFallback(PrimaryStatus))
160 return PrimaryStatus;
161
162 // Clear ExecutionFailed and ErrMsg before falling back.
163 if (ErrMsg)
164 ErrMsg->clear();
165 if (ExecutionFailed)
166 *ExecutionFailed = false;
167
Hans Wennborgc5afd062014-02-18 21:42:51 +0000168 const Driver &D = getCreator().getToolChain().getDriver();
Hans Wennborg897a69b2014-02-19 02:10:19 +0000169 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
Hans Wennborgc5afd062014-02-18 21:42:51 +0000170
Hans Wennborg87cfa712013-09-19 20:32:16 +0000171 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
172 return SecondaryStatus;
173}
174
Daniel Dunbar313c2912009-03-13 23:36:33 +0000175JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar04c4c2c2009-03-18 07:06:02 +0000176
Daniel Dunbar66e276892010-03-11 18:04:49 +0000177JobList::~JobList() {
178 for (iterator it = begin(), ie = end(); it != ie; ++it)
179 delete *it;
180}
181
Hans Wennborgb212b342013-09-12 18:23:34 +0000182void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
183 bool CrashReport) const {
184 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
185 (*it)->Print(OS, Terminator, Quote, CrashReport);
186}
187
Chad Rosierbe10f982011-08-02 17:58:04 +0000188void JobList::clear() {
189 DeleteContainerPointers(Jobs);
190}