1 //===--- Compilation.cpp - Compilation Task Implementation ----------------===//
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/Compilation.h"
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/ArgList.h"
14 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/DriverDiagnostic.h"
16 #include "clang/Driver/Options.h"
17 #include "clang/Driver/ToolChain.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Program.h"
25 using namespace clang::driver;
26 using namespace clang;
28 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
29 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
30 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
31 TranslatedArgs(_TranslatedArgs), Redirects(0) {
34 Compilation::~Compilation() {
35 delete TranslatedArgs;
38 // Free any derived arg lists.
39 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
40 DerivedArgList*>::iterator it = TCArgs.begin(),
41 ie = TCArgs.end(); it != ie; ++it)
42 if (it->second != TranslatedArgs)
45 // Free the actions, if built.
46 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
50 // Free redirections of stdout/stderr.
58 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
59 const char *BoundArch) {
61 TC = &DefaultToolChain;
63 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
65 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
67 Entry = TranslatedArgs;
73 void Compilation::PrintJob(raw_ostream &OS, const Job &J,
74 const char *Terminator, bool Quote) const {
75 if (const Command *C = dyn_cast<Command>(&J)) {
76 OS << " \"" << C->getExecutable() << '"';
77 for (ArgStringList::const_iterator it = C->getArguments().begin(),
78 ie = C->getArguments().end(); it != ie; ++it) {
80 if (!Quote && !std::strpbrk(*it, " \"\\$")) {
85 // Quote the argument and escape shell special characters; this isn't
86 // really complete but is good enough.
88 for (const char *s = *it; *s; ++s) {
89 if (*s == '"' || *s == '\\' || *s == '$')
97 const JobList *Jobs = cast<JobList>(&J);
98 for (JobList::const_iterator
99 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
100 PrintJob(OS, **it, Terminator, Quote);
104 bool Compilation::CleanupFileList(const ArgStringList &Files,
105 bool IssueErrors) const {
108 for (ArgStringList::const_iterator
109 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
111 llvm::sys::Path P(*it);
114 // Don't try to remove files which we don't have write access to (but may be
115 // able to remove). Underlying tools may have intentionally not overwritten
120 if (P.eraseFromDisk(false, &Error)) {
121 // Failure is only failure if the file exists and is "regular". There is
122 // a race condition here due to the limited interface of
123 // llvm::sys::Path, we want to know if the removal gave ENOENT.
125 // FIXME: Grumble, P.exists() is broken. PR3837.
127 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
130 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
140 int Compilation::ExecuteCommand(const Command &C,
141 const Command *&FailingCommand) const {
142 llvm::sys::Path Prog(C.getExecutable());
143 const char **Argv = new const char*[C.getArguments().size() + 2];
144 Argv[0] = C.getExecutable();
145 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
146 Argv[C.getArguments().size() + 1] = 0;
148 if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
149 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
150 raw_ostream *OS = &llvm::errs();
152 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
154 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
156 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
158 llvm::raw_fd_ostream::F_Append);
159 if (!Error.empty()) {
160 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
168 if (getDriver().CCPrintOptions)
169 *OS << "[Logging clang options]";
171 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
173 if (OS != &llvm::errs())
179 llvm::sys::Program::ExecuteAndWait(Prog, Argv,
181 /*secondsToWait*/0, /*memoryLimit*/0,
183 if (!Error.empty()) {
184 assert(Res && "Error string set with 0 result code!");
185 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
195 int Compilation::ExecuteJob(const Job &J,
196 const Command *&FailingCommand) const {
197 if (const Command *C = dyn_cast<Command>(&J)) {
198 return ExecuteCommand(*C, FailingCommand);
200 const JobList *Jobs = cast<JobList>(&J);
201 for (JobList::const_iterator
202 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
203 if (int Res = ExecuteJob(**it, FailingCommand))
209 void Compilation::initCompilationForDiagnostics(void) {
210 // Free actions and jobs.
211 DeleteContainerPointers(Actions);
214 // Clear temporary/results file lists.
218 // Remove any user specified output. Claim any unclaimed arguments, so as
219 // to avoid emitting warnings about unused args.
220 if (TranslatedArgs->hasArg(options::OPT_o))
221 TranslatedArgs->eraseArg(options::OPT_o);
222 TranslatedArgs->ClaimAllArgs();
224 // Redirect stdout/stderr to /dev/null.
225 Redirects = new const llvm::sys::Path*[3]();
226 Redirects[1] = new const llvm::sys::Path();
227 Redirects[2] = new const llvm::sys::Path();