blob: 9c7100fb80c4f2a5507c09bfa4550a4638387794 [file] [log] [blame]
Nick Lewycky6da90772010-12-31 17:31:54 +00001//===--- Compilation.cpp - Compilation Task Implementation ----------------===//
Daniel Dunbar544ecd12009-03-02 19:59:07 +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
10#include "clang/Driver/Compilation.h"
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000011#include "clang/Driver/Action.h"
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000012#include "clang/Driver/ArgList.h"
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +000013#include "clang/Driver/Driver.h"
14#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbarda13faf2009-11-19 04:25:22 +000015#include "clang/Driver/Options.h"
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000016#include "clang/Driver/ToolChain.h"
Chad Rosierbe10f982011-08-02 17:58:04 +000017#include "llvm/ADT/STLExtras.h"
Chad Rosier83f16bf2012-10-31 18:31:33 +000018#include "llvm/ADT/StringSwitch.h"
Michael J. Spencer8aaf4992010-11-29 18:12:39 +000019#include "llvm/Support/Program.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000020#include "llvm/Support/raw_ostream.h"
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +000021#include <errno.h>
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include <sys/stat.h>
Francois Pichet24fc69e2011-07-23 11:36:43 +000023
Daniel Dunbarb2cd66b2009-03-04 20:49:20 +000024using namespace clang::driver;
Francois Pichet24fc69e2011-07-23 11:36:43 +000025using namespace clang;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000026
Daniel Dunbar775d4062010-06-11 22:00:26 +000027Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
28 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
29 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
Chad Rosierbe10f982011-08-02 17:58:04 +000030 TranslatedArgs(_TranslatedArgs), Redirects(0) {
Daniel Dunbar544ecd12009-03-02 19:59:07 +000031}
32
Mike Stump11289f42009-09-09 15:08:12 +000033Compilation::~Compilation() {
Daniel Dunbar775d4062010-06-11 22:00:26 +000034 delete TranslatedArgs;
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000035 delete Args;
Mike Stump11289f42009-09-09 15:08:12 +000036
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000037 // Free any derived arg lists.
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000038 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
39 DerivedArgList*>::iterator it = TCArgs.begin(),
40 ie = TCArgs.end(); it != ie; ++it)
Daniel Dunbar775d4062010-06-11 22:00:26 +000041 if (it->second != TranslatedArgs)
42 delete it->second;
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000043
44 // Free the actions, if built.
Mike Stump11289f42009-09-09 15:08:12 +000045 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
Daniel Dunbarf0eddb82009-03-18 02:55:38 +000046 it != ie; ++it)
47 delete *it;
Chad Rosierbe10f982011-08-02 17:58:04 +000048
49 // Free redirections of stdout/stderr.
50 if (Redirects) {
51 delete Redirects[1];
52 delete Redirects[2];
53 delete [] Redirects;
54 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000055}
56
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000057const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
58 const char *BoundArch) {
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000059 if (!TC)
60 TC = &DefaultToolChain;
61
Daniel Dunbarb5d86bb2009-09-09 18:36:01 +000062 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
Daniel Dunbar775d4062010-06-11 22:00:26 +000063 if (!Entry) {
64 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
65 if (!Entry)
66 Entry = TranslatedArgs;
67 }
Daniel Dunbar3ce436d2009-03-16 06:42:30 +000068
Daniel Dunbaraabaac42009-03-18 05:58:45 +000069 return *Entry;
Daniel Dunbar544ecd12009-03-02 19:59:07 +000070}
71
Chris Lattner0e62c1c2011-07-23 10:55:15 +000072void Compilation::PrintJob(raw_ostream &OS, const Job &J,
Daniel Dunbar64316c42009-03-18 22:44:24 +000073 const char *Terminator, bool Quote) const {
74 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbar0928b312009-03-18 06:49:39 +000075 OS << " \"" << C->getExecutable() << '"';
76 for (ArgStringList::const_iterator it = C->getArguments().begin(),
Daniel Dunbar64316c42009-03-18 22:44:24 +000077 ie = C->getArguments().end(); it != ie; ++it) {
Daniel Dunbar7c4de042010-03-20 08:01:53 +000078 OS << ' ';
Benjamin Kramer8c26d442011-10-06 22:53:35 +000079 if (!Quote && !std::strpbrk(*it, " \"\\$")) {
Daniel Dunbar7c4de042010-03-20 08:01:53 +000080 OS << *it;
81 continue;
82 }
83
84 // Quote the argument and escape shell special characters; this isn't
85 // really complete but is good enough.
86 OS << '"';
87 for (const char *s = *it; *s; ++s) {
88 if (*s == '"' || *s == '\\' || *s == '$')
89 OS << '\\';
90 OS << *s;
91 }
92 OS << '"';
Daniel Dunbar64316c42009-03-18 22:44:24 +000093 }
Daniel Dunbar0928b312009-03-18 06:49:39 +000094 OS << Terminator;
Daniel Dunbar0928b312009-03-18 06:49:39 +000095 } else {
Daniel Dunbar64316c42009-03-18 22:44:24 +000096 const JobList *Jobs = cast<JobList>(&J);
Mike Stump11289f42009-09-09 15:08:12 +000097 for (JobList::const_iterator
Daniel Dunbar0928b312009-03-18 06:49:39 +000098 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbar64316c42009-03-18 22:44:24 +000099 PrintJob(OS, **it, Terminator, Quote);
Daniel Dunbar0928b312009-03-18 06:49:39 +0000100 }
101}
102
Chad Rosier83f16bf2012-10-31 18:31:33 +0000103static bool skipArg(const char *Flag, bool &SkipNextArg) {
104 StringRef FlagRef(Flag);
105
106 // Assume we're going to see -Flag <Arg>.
107 SkipNextArg = true;
108
109 // These flags are all of the form -Flag <Arg> and are treated as two
110 // arguments. Therefore, we need to skip the flag and the next argument.
111 bool Res = llvm::StringSwitch<bool>(Flag)
112 .Cases("-I", "-MF", "-MT", "-MQ", true)
113 .Cases("-o", "-coverage-file", "-dependency-file", true)
114 .Cases("-fdebug-compilation-dir", "-fmodule-cache-path", "-idirafter", true)
115 .Cases("-include", "-include-pch", "-internal-isystem", true)
Chad Rosier37756b02012-10-31 21:08:30 +0000116 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
Chad Rosier83f16bf2012-10-31 18:31:33 +0000117 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
118 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
119 .Case("-dwarf-debug-flags", true)
120 .Default(false);
121
122 // Match found.
123 if (Res)
124 return Res;
125
126 // The remaining flags are treated as a single argument.
127 SkipNextArg = false;
128
129 // These flags are all of the form -Flag and have no second argument.
130 Res = llvm::StringSwitch<bool>(Flag)
131 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
132 .Case("-MMD", true)
133 .Default(false);
134
135 // Match found.
136 if (Res)
137 return Res;
138
139 // These flags are treated as a single argument (e.g., -F<Dir>).
140 if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
141 return true;
142
143 return false;
144}
145
146static bool quoteNextArg(const char *flag) {
147 return llvm::StringSwitch<bool>(flag)
148 .Case("-D", true)
149 .Default(false);
150}
151
152void Compilation::PrintDiagnosticJob(raw_ostream &OS, const Job &J) const {
153 if (const Command *C = dyn_cast<Command>(&J)) {
154 OS << C->getExecutable();
155 unsigned QuoteNextArg = 0;
156 for (ArgStringList::const_iterator it = C->getArguments().begin(),
157 ie = C->getArguments().end(); it != ie; ++it) {
158
159 bool SkipNext;
160 if (skipArg(*it, SkipNext)) {
161 if (SkipNext) ++it;
162 continue;
163 }
164
165 if (!QuoteNextArg)
166 QuoteNextArg = quoteNextArg(*it) ? 2 : 0;
167
168 OS << ' ';
169
170 if (QuoteNextArg == 1)
171 OS << '"';
172
173 if (!std::strpbrk(*it, " \"\\$")) {
174 OS << *it;
175 } else {
176 // Quote the argument and escape shell special characters; this isn't
177 // really complete but is good enough.
178 OS << '"';
179 for (const char *s = *it; *s; ++s) {
180 if (*s == '"' || *s == '\\' || *s == '$')
181 OS << '\\';
182 OS << *s;
183 }
184 OS << '"';
185 }
186
187 if (QuoteNextArg) {
188 if (QuoteNextArg == 1)
189 OS << '"';
190 --QuoteNextArg;
191 }
192 }
193 OS << '\n';
194 } else {
195 const JobList *Jobs = cast<JobList>(&J);
196 for (JobList::const_iterator
197 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
198 PrintDiagnosticJob(OS, **it);
199 }
200}
201
Mike Stump11289f42009-09-09 15:08:12 +0000202bool Compilation::CleanupFileList(const ArgStringList &Files,
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000203 bool IssueErrors) const {
204 bool Success = true;
205
Mike Stump11289f42009-09-09 15:08:12 +0000206 for (ArgStringList::const_iterator
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000207 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
Edward O'Callaghandb521ec2009-11-24 15:23:21 +0000208
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000209 llvm::sys::Path P(*it);
210 std::string Error;
211
Daniel Dunbar462e7ed2011-04-25 20:43:05 +0000212 // Don't try to remove files which we don't have write access to (but may be
Daniel Dunbar582b2ab2013-01-23 01:08:23 +0000213 // able to remove), or non-regular files. Underlying tools may have
214 // intentionally not overwritten them.
215 if (!P.canWrite() || !P.isRegularFile())
Daniel Dunbar462e7ed2011-04-25 20:43:05 +0000216 continue;
217
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000218 if (P.eraseFromDisk(false, &Error)) {
Dan Gohmane89c04e2010-10-29 23:26:14 +0000219 // Failure is only failure if the file exists and is "regular". There is
220 // a race condition here due to the limited interface of
221 // llvm::sys::Path, we want to know if the removal gave ENOENT.
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000222
223 // FIXME: Grumble, P.exists() is broken. PR3837.
224 struct stat buf;
Benjamin Kramer7d113822010-10-30 08:28:42 +0000225 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
Dan Gohmane89c04e2010-10-29 23:26:14 +0000226 (errno != ENOENT)) {
Daniel Dunbar6c17bfd2009-03-18 22:16:03 +0000227 if (IssueErrors)
228 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
229 << Error;
230 Success = false;
231 }
232 }
233 }
234
235 return Success;
236}
237
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000238int Compilation::ExecuteCommand(const Command &C,
239 const Command *&FailingCommand) const {
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000240 llvm::sys::Path Prog(C.getExecutable());
241 const char **Argv = new const char*[C.getArguments().size() + 2];
242 Argv[0] = C.getExecutable();
243 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
244 Argv[C.getArguments().size() + 1] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000245
Chad Rosierbe10f982011-08-02 17:58:04 +0000246 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
247 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000248 raw_ostream *OS = &llvm::errs();
Daniel Dunbar6a8efa82010-03-20 08:01:59 +0000249
250 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
251 // output stream.
252 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
253 std::string Error;
254 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
255 Error,
256 llvm::raw_fd_ostream::F_Append);
257 if (!Error.empty()) {
258 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
259 << Error;
260 FailingCommand = &C;
261 delete OS;
262 return 1;
263 }
264 }
265
266 if (getDriver().CCPrintOptions)
267 *OS << "[Logging clang options]";
268
269 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
270
271 if (OS != &llvm::errs())
272 delete OS;
273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000275 std::string Error;
Mike Stump11289f42009-09-09 15:08:12 +0000276 int Res =
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000277 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
Chad Rosierbe10f982011-08-02 17:58:04 +0000278 /*env*/0, Redirects,
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000279 /*secondsToWait*/0, /*memoryLimit*/0,
280 &Error);
281 if (!Error.empty()) {
282 assert(Res && "Error string set with 0 result code!");
283 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
284 }
Mike Stump11289f42009-09-09 15:08:12 +0000285
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000286 if (Res)
287 FailingCommand = &C;
288
Daniel Dunbar1da82ae2009-03-19 08:01:45 +0000289 delete[] Argv;
290 return Res;
291}
292
Mike Stump11289f42009-09-09 15:08:12 +0000293int Compilation::ExecuteJob(const Job &J,
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000294 const Command *&FailingCommand) const {
Daniel Dunbar64316c42009-03-18 22:44:24 +0000295 if (const Command *C = dyn_cast<Command>(&J)) {
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000296 return ExecuteCommand(*C, FailingCommand);
Daniel Dunbar64316c42009-03-18 22:44:24 +0000297 } else {
298 const JobList *Jobs = cast<JobList>(&J);
Mike Stump11289f42009-09-09 15:08:12 +0000299 for (JobList::const_iterator
Daniel Dunbar64316c42009-03-18 22:44:24 +0000300 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
Daniel Dunbaraa246ca2009-07-01 19:14:39 +0000301 if (int Res = ExecuteJob(**it, FailingCommand))
Daniel Dunbar64316c42009-03-18 22:44:24 +0000302 return Res;
303 return 0;
304 }
305}
Chad Rosierbe10f982011-08-02 17:58:04 +0000306
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000307void Compilation::initCompilationForDiagnostics() {
Chad Rosierbe10f982011-08-02 17:58:04 +0000308 // Free actions and jobs.
309 DeleteContainerPointers(Actions);
310 Jobs.clear();
311
312 // Clear temporary/results file lists.
313 TempFiles.clear();
314 ResultFiles.clear();
315
316 // Remove any user specified output. Claim any unclaimed arguments, so as
317 // to avoid emitting warnings about unused args.
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000318 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
319 options::OPT_MMD };
Chad Rosier45cf50f2012-05-03 21:25:34 +0000320 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
Peter Collingbourne9b515cb2011-11-06 00:40:05 +0000321 if (TranslatedArgs->hasArg(OutputOpts[i]))
322 TranslatedArgs->eraseArg(OutputOpts[i]);
323 }
Chad Rosierbe10f982011-08-02 17:58:04 +0000324 TranslatedArgs->ClaimAllArgs();
325
326 // Redirect stdout/stderr to /dev/null.
327 Redirects = new const llvm::sys::Path*[3]();
328 Redirects[1] = new const llvm::sys::Path();
329 Redirects[2] = new const llvm::sys::Path();
330}
Sebastian Pop980920a2012-04-16 04:16:43 +0000331
Dmitri Gribenkob2aa9232012-11-15 14:28:07 +0000332StringRef Compilation::getSysRoot() const {
Sebastian Pop980920a2012-04-16 04:16:43 +0000333 return getDriver().SysRoot;
334}