Compilation.cpp revision 22807868ad852806fd19e005945a910d3e9ab9ef
1//===--- Compilation.cpp - Compilation Task Implementation ----------------===// 2// 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" 11 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" 18 19#include "llvm/ADT/STLExtras.h" 20#include "llvm/Support/raw_ostream.h" 21#include "llvm/Support/Program.h" 22#include <sys/stat.h> 23#include <errno.h> 24 25using namespace clang::driver; 26using namespace clang; 27 28Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, 29 InputArgList *_Args, DerivedArgList *_TranslatedArgs) 30 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args), 31 TranslatedArgs(_TranslatedArgs), Redirects(0) { 32} 33 34Compilation::~Compilation() { 35 delete TranslatedArgs; 36 delete Args; 37 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) 43 delete it->second; 44 45 // Free the actions, if built. 46 for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); 47 it != ie; ++it) 48 delete *it; 49 50 // Free redirections of stdout/stderr. 51 if (Redirects) { 52 delete Redirects[1]; 53 delete Redirects[2]; 54 delete [] Redirects; 55 } 56} 57 58const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC, 59 const char *BoundArch) { 60 if (!TC) 61 TC = &DefaultToolChain; 62 63 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)]; 64 if (!Entry) { 65 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch); 66 if (!Entry) 67 Entry = TranslatedArgs; 68 } 69 70 return *Entry; 71} 72 73static bool needsQuote(const char *s) { 74 for (const char *c = s; *c; ++c) 75 if (*c == ' ' || *c == '"' || *c == '\\' || *c == '$') 76 return true; 77 return false; 78} 79 80void Compilation::PrintJob(raw_ostream &OS, const Job &J, 81 const char *Terminator, bool Quote) const { 82 if (const Command *C = dyn_cast<Command>(&J)) { 83 OS << " \"" << C->getExecutable() << '"'; 84 for (ArgStringList::const_iterator it = C->getArguments().begin(), 85 ie = C->getArguments().end(); it != ie; ++it) { 86 OS << ' '; 87 if (!Quote && !needsQuote(*it)) { 88 OS << *it; 89 continue; 90 } 91 92 // Quote the argument and escape shell special characters; this isn't 93 // really complete but is good enough. 94 OS << '"'; 95 for (const char *s = *it; *s; ++s) { 96 if (*s == '"' || *s == '\\' || *s == '$') 97 OS << '\\'; 98 OS << *s; 99 } 100 OS << '"'; 101 } 102 OS << Terminator; 103 } else { 104 const JobList *Jobs = cast<JobList>(&J); 105 for (JobList::const_iterator 106 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) 107 PrintJob(OS, **it, Terminator, Quote); 108 } 109} 110 111bool Compilation::CleanupFileList(const ArgStringList &Files, 112 bool IssueErrors) const { 113 bool Success = true; 114 115 for (ArgStringList::const_iterator 116 it = Files.begin(), ie = Files.end(); it != ie; ++it) { 117 118 llvm::sys::Path P(*it); 119 std::string Error; 120 121 // Don't try to remove files which we don't have write access to (but may be 122 // able to remove). Underlying tools may have intentionally not overwritten 123 // them. 124 if (!P.canWrite()) 125 continue; 126 127 if (P.eraseFromDisk(false, &Error)) { 128 // Failure is only failure if the file exists and is "regular". There is 129 // a race condition here due to the limited interface of 130 // llvm::sys::Path, we want to know if the removal gave ENOENT. 131 132 // FIXME: Grumble, P.exists() is broken. PR3837. 133 struct stat buf; 134 if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG : 135 (errno != ENOENT)) { 136 if (IssueErrors) 137 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) 138 << Error; 139 Success = false; 140 } 141 } 142 } 143 144 return Success; 145} 146 147int Compilation::ExecuteCommand(const Command &C, 148 const Command *&FailingCommand) const { 149 llvm::sys::Path Prog(C.getExecutable()); 150 const char **Argv = new const char*[C.getArguments().size() + 2]; 151 Argv[0] = C.getExecutable(); 152 std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1); 153 Argv[C.getArguments().size() + 1] = 0; 154 155 if ((getDriver().CCCEcho || getDriver().CCPrintOptions || 156 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) { 157 raw_ostream *OS = &llvm::errs(); 158 159 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the 160 // output stream. 161 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { 162 std::string Error; 163 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, 164 Error, 165 llvm::raw_fd_ostream::F_Append); 166 if (!Error.empty()) { 167 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) 168 << Error; 169 FailingCommand = &C; 170 delete OS; 171 return 1; 172 } 173 } 174 175 if (getDriver().CCPrintOptions) 176 *OS << "[Logging clang options]"; 177 178 PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions); 179 180 if (OS != &llvm::errs()) 181 delete OS; 182 } 183 184 std::string Error; 185 int Res = 186 llvm::sys::Program::ExecuteAndWait(Prog, Argv, 187 /*env*/0, Redirects, 188 /*secondsToWait*/0, /*memoryLimit*/0, 189 &Error); 190 if (!Error.empty()) { 191 assert(Res && "Error string set with 0 result code!"); 192 getDriver().Diag(clang::diag::err_drv_command_failure) << Error; 193 } 194 195 if (Res) 196 FailingCommand = &C; 197 198 delete[] Argv; 199 return Res; 200} 201 202int Compilation::ExecuteJob(const Job &J, 203 const Command *&FailingCommand) const { 204 if (const Command *C = dyn_cast<Command>(&J)) { 205 return ExecuteCommand(*C, FailingCommand); 206 } else { 207 const JobList *Jobs = cast<JobList>(&J); 208 for (JobList::const_iterator 209 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) 210 if (int Res = ExecuteJob(**it, FailingCommand)) 211 return Res; 212 return 0; 213 } 214} 215 216void Compilation::initCompilationForDiagnostics(void) { 217 // Free actions and jobs. 218 DeleteContainerPointers(Actions); 219 Jobs.clear(); 220 221 // Clear temporary/results file lists. 222 TempFiles.clear(); 223 ResultFiles.clear(); 224 225 // Remove any user specified output. Claim any unclaimed arguments, so as 226 // to avoid emitting warnings about unused args. 227 if (TranslatedArgs->hasArg(options::OPT_o)) 228 TranslatedArgs->eraseArg(options::OPT_o); 229 TranslatedArgs->ClaimAllArgs(); 230 231 // Redirect stdout/stderr to /dev/null. 232 Redirects = new const llvm::sys::Path*[3](); 233 Redirects[1] = new const llvm::sys::Path(); 234 Redirects[2] = new const llvm::sys::Path(); 235} 236