1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
//===--- Compilation.cpp - Compilation Task Implementation ----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/ArgList.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/ToolChain.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <errno.h>
#include <sys/stat.h>
using namespace clang::driver;
using namespace clang;
Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
InputArgList *_Args, DerivedArgList *_TranslatedArgs)
: TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
TranslatedArgs(_TranslatedArgs), Redirects(0) {
}
Compilation::~Compilation() {
delete TranslatedArgs;
delete Args;
// Free any derived arg lists.
for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
DerivedArgList*>::iterator it = TCArgs.begin(),
ie = TCArgs.end(); it != ie; ++it)
if (it->second != TranslatedArgs)
delete it->second;
// Free the actions, if built.
for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
it != ie; ++it)
delete *it;
// Free redirections of stdout/stderr.
if (Redirects) {
delete Redirects[1];
delete Redirects[2];
delete [] Redirects;
}
}
const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
const char *BoundArch) {
if (!TC)
TC = &DefaultToolChain;
DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
if (!Entry) {
Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
if (!Entry)
Entry = TranslatedArgs;
}
return *Entry;
}
void Compilation::PrintJob(raw_ostream &OS, const Job &J,
const char *Terminator, bool Quote) const {
if (const Command *C = dyn_cast<Command>(&J)) {
OS << " \"" << C->getExecutable() << '"';
for (ArgStringList::const_iterator it = C->getArguments().begin(),
ie = C->getArguments().end(); it != ie; ++it) {
OS << ' ';
if (!Quote && !std::strpbrk(*it, " \"\\$")) {
OS << *it;
continue;
}
// Quote the argument and escape shell special characters; this isn't
// really complete but is good enough.
OS << '"';
for (const char *s = *it; *s; ++s) {
if (*s == '"' || *s == '\\' || *s == '$')
OS << '\\';
OS << *s;
}
OS << '"';
}
OS << Terminator;
} else {
const JobList *Jobs = cast<JobList>(&J);
for (JobList::const_iterator
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
PrintJob(OS, **it, Terminator, Quote);
}
}
static bool skipArg(const char *Flag, bool &SkipNextArg) {
StringRef FlagRef(Flag);
// Assume we're going to see -Flag <Arg>.
SkipNextArg = true;
// These flags are all of the form -Flag <Arg> and are treated as two
// arguments. Therefore, we need to skip the flag and the next argument.
bool Res = llvm::StringSwitch<bool>(Flag)
.Cases("-I", "-MF", "-MT", "-MQ", true)
.Cases("-o", "-coverage-file", "-dependency-file", true)
.Cases("-fdebug-compilation-dir", "-idirafter", true)
.Cases("-include", "-include-pch", "-internal-isystem", true)
.Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
.Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
.Cases("-resource-dir", "-serialize-diagnostic-file", true)
.Case("-dwarf-debug-flags", true)
.Default(false);
// Match found.
if (Res)
return Res;
// The remaining flags are treated as a single argument.
SkipNextArg = false;
// These flags are all of the form -Flag and have no second argument.
Res = llvm::StringSwitch<bool>(Flag)
.Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
.Case("-MMD", true)
.Default(false);
// Match found.
if (Res)
return Res;
// These flags are treated as a single argument (e.g., -F<Dir>).
if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
return true;
return false;
}
static bool quoteNextArg(const char *flag) {
return llvm::StringSwitch<bool>(flag)
.Case("-D", true)
.Default(false);
}
void Compilation::PrintDiagnosticJob(raw_ostream &OS, const Job &J) const {
if (const Command *C = dyn_cast<Command>(&J)) {
OS << C->getExecutable();
unsigned QuoteNextArg = 0;
for (ArgStringList::const_iterator it = C->getArguments().begin(),
ie = C->getArguments().end(); it != ie; ++it) {
bool SkipNext;
if (skipArg(*it, SkipNext)) {
if (SkipNext) ++it;
continue;
}
if (!QuoteNextArg)
QuoteNextArg = quoteNextArg(*it) ? 2 : 0;
OS << ' ';
if (QuoteNextArg == 1)
OS << '"';
if (!std::strpbrk(*it, " \"\\$")) {
OS << *it;
} else {
// Quote the argument and escape shell special characters; this isn't
// really complete but is good enough.
OS << '"';
for (const char *s = *it; *s; ++s) {
if (*s == '"' || *s == '\\' || *s == '$')
OS << '\\';
OS << *s;
}
OS << '"';
}
if (QuoteNextArg) {
if (QuoteNextArg == 1)
OS << '"';
--QuoteNextArg;
}
}
OS << '\n';
} else {
const JobList *Jobs = cast<JobList>(&J);
for (JobList::const_iterator
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
PrintDiagnosticJob(OS, **it);
}
}
bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
llvm::sys::Path P(File);
std::string Error;
// Don't try to remove files which we don't have write access to (but may be
// able to remove), or non-regular files. Underlying tools may have
// intentionally not overwritten them.
if (!P.canWrite() || !P.isRegularFile())
return true;
if (P.eraseFromDisk(false, &Error)) {
// Failure is only failure if the file exists and is "regular". There is
// a race condition here due to the limited interface of
// llvm::sys::Path, we want to know if the removal gave ENOENT.
// FIXME: Grumble, P.exists() is broken. PR3837.
struct stat buf;
if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
(errno != ENOENT)) {
if (IssueErrors)
getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
<< Error;
return false;
}
}
return true;
}
bool Compilation::CleanupFileList(const ArgStringList &Files,
bool IssueErrors) const {
bool Success = true;
for (ArgStringList::const_iterator
it = Files.begin(), ie = Files.end(); it != ie; ++it)
Success &= CleanupFile(*it, IssueErrors);
return Success;
}
bool Compilation::CleanupFileMap(const ArgStringMap &Files,
const JobAction *JA,
bool IssueErrors) const {
bool Success = true;
for (ArgStringMap::const_iterator
it = Files.begin(), ie = Files.end(); it != ie; ++it) {
// If specified, only delete the files associated with the JobAction.
// Otherwise, delete all files in the map.
if (JA && it->first != JA)
continue;
Success &= CleanupFile(it->second, IssueErrors);
}
return Success;
}
int Compilation::ExecuteCommand(const Command &C,
const Command *&FailingCommand) const {
llvm::sys::Path Prog(C.getExecutable());
const char **Argv = new const char*[C.getArguments().size() + 2];
Argv[0] = C.getExecutable();
std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
Argv[C.getArguments().size() + 1] = 0;
if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
raw_ostream *OS = &llvm::errs();
// Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
// output stream.
if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
std::string Error;
OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
Error,
llvm::raw_fd_ostream::F_Append);
if (!Error.empty()) {
getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
<< Error;
FailingCommand = &C;
delete OS;
return 1;
}
}
if (getDriver().CCPrintOptions)
*OS << "[Logging clang options]";
PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
if (OS != &llvm::errs())
delete OS;
}
std::string Error;
int Res =
llvm::sys::Program::ExecuteAndWait(Prog, Argv,
/*env*/0, Redirects,
/*secondsToWait*/0, /*memoryLimit*/0,
&Error);
if (!Error.empty()) {
assert(Res && "Error string set with 0 result code!");
getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
}
if (Res)
FailingCommand = &C;
delete[] Argv;
return Res;
}
typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
static bool ActionFailed(const Action *A,
const FailingCommandList &FailingCommands) {
if (FailingCommands.empty())
return false;
for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
CE = FailingCommands.end(); CI != CE; ++CI)
if (A == &(CI->second->getSource()))
return true;
for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
if (ActionFailed(*AI, FailingCommands))
return true;
return false;
}
static bool InputsOk(const Command &C,
const FailingCommandList &FailingCommands) {
return !ActionFailed(&C.getSource(), FailingCommands);
}
void Compilation::ExecuteJob(const Job &J,
FailingCommandList &FailingCommands) const {
if (const Command *C = dyn_cast<Command>(&J)) {
if (!InputsOk(*C, FailingCommands))
return;
const Command *FailingCommand = 0;
if (int Res = ExecuteCommand(*C, FailingCommand))
FailingCommands.push_back(std::make_pair(Res, FailingCommand));
} else {
const JobList *Jobs = cast<JobList>(&J);
for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
it != ie; ++it)
ExecuteJob(**it, FailingCommands);
}
}
void Compilation::initCompilationForDiagnostics() {
// Free actions and jobs.
DeleteContainerPointers(Actions);
Jobs.clear();
// Clear temporary/results file lists.
TempFiles.clear();
ResultFiles.clear();
FailureResultFiles.clear();
// Remove any user specified output. Claim any unclaimed arguments, so as
// to avoid emitting warnings about unused args.
OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
options::OPT_MMD };
for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
if (TranslatedArgs->hasArg(OutputOpts[i]))
TranslatedArgs->eraseArg(OutputOpts[i]);
}
TranslatedArgs->ClaimAllArgs();
// Redirect stdout/stderr to /dev/null.
Redirects = new const llvm::sys::Path*[3]();
Redirects[1] = new const llvm::sys::Path();
Redirects[2] = new const llvm::sys::Path();
}
StringRef Compilation::getSysRoot() const {
return getDriver().SysRoot;
}
|