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 #include "clang/Driver/Action.h" 12 #include "clang/Driver/Driver.h" 13 #include "clang/Driver/DriverDiagnostic.h" 14 #include "clang/Driver/Options.h" 15 #include "clang/Driver/ToolChain.h" 16 #include "llvm/ADT/STLExtras.h" 17 #include "llvm/Option/ArgList.h" 18 #include "llvm/Support/FileSystem.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 using namespace clang::driver; 22 using namespace clang; 23 using namespace llvm::opt; 24 25 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain, 26 InputArgList *_Args, DerivedArgList *_TranslatedArgs) 27 : TheDriver(D), DefaultToolChain(_DefaultToolChain), ActiveOffloadMask(0u), 28 Args(_Args), TranslatedArgs(_TranslatedArgs), Redirects(nullptr), 29 ForDiagnostics(false) { 30 // The offloading host toolchain is the default tool chain. 31 OrderedOffloadingToolchains.insert( 32 std::make_pair(Action::OFK_Host, &DefaultToolChain)); 33 } 34 35 Compilation::~Compilation() { 36 delete TranslatedArgs; 37 delete Args; 38 39 // Free any derived arg lists. 40 for (auto Arg : TCArgs) 41 if (Arg.second != TranslatedArgs) 42 delete Arg.second; 43 44 // Free redirections of stdout/stderr. 45 if (Redirects) { 46 delete Redirects[0]; 47 delete Redirects[1]; 48 delete Redirects[2]; 49 delete [] Redirects; 50 } 51 } 52 53 const DerivedArgList & 54 Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch, 55 Action::OffloadKind DeviceOffloadKind) { 56 if (!TC) 57 TC = &DefaultToolChain; 58 59 DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}]; 60 if (!Entry) { 61 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind); 62 if (!Entry) 63 Entry = TranslatedArgs; 64 } 65 66 return *Entry; 67 } 68 69 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const { 70 // FIXME: Why are we trying to remove files that we have not created? For 71 // example we should only try to remove a temporary assembly file if 72 // "clang -cc1" succeed in writing it. Was this a workaround for when 73 // clang was writing directly to a .s file and sometimes leaving it behind 74 // during a failure? 75 76 // FIXME: If this is necessary, we can still try to split 77 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the 78 // duplicated stat from is_regular_file. 79 80 // Don't try to remove files which we don't have write access to (but may be 81 // able to remove), or non-regular files. Underlying tools may have 82 // intentionally not overwritten them. 83 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File)) 84 return true; 85 86 if (std::error_code EC = llvm::sys::fs::remove(File)) { 87 // Failure is only failure if the file exists and is "regular". We checked 88 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT, 89 // so we don't need to check again. 90 91 if (IssueErrors) 92 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) 93 << EC.message(); 94 return false; 95 } 96 return true; 97 } 98 99 bool Compilation::CleanupFileList(const ArgStringList &Files, 100 bool IssueErrors) const { 101 bool Success = true; 102 for (ArgStringList::const_iterator 103 it = Files.begin(), ie = Files.end(); it != ie; ++it) 104 Success &= CleanupFile(*it, IssueErrors); 105 return Success; 106 } 107 108 bool Compilation::CleanupFileMap(const ArgStringMap &Files, 109 const JobAction *JA, 110 bool IssueErrors) const { 111 bool Success = true; 112 for (ArgStringMap::const_iterator 113 it = Files.begin(), ie = Files.end(); it != ie; ++it) { 114 115 // If specified, only delete the files associated with the JobAction. 116 // Otherwise, delete all files in the map. 117 if (JA && it->first != JA) 118 continue; 119 Success &= CleanupFile(it->second, IssueErrors); 120 } 121 return Success; 122 } 123 124 int Compilation::ExecuteCommand(const Command &C, 125 const Command *&FailingCommand) const { 126 if ((getDriver().CCPrintOptions || 127 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) { 128 raw_ostream *OS = &llvm::errs(); 129 130 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the 131 // output stream. 132 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { 133 std::error_code EC; 134 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC, 135 llvm::sys::fs::F_Append | 136 llvm::sys::fs::F_Text); 137 if (EC) { 138 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure) 139 << EC.message(); 140 FailingCommand = &C; 141 delete OS; 142 return 1; 143 } 144 } 145 146 if (getDriver().CCPrintOptions) 147 *OS << "[Logging clang options]"; 148 149 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions); 150 151 if (OS != &llvm::errs()) 152 delete OS; 153 } 154 155 std::string Error; 156 bool ExecutionFailed; 157 int Res = C.Execute(Redirects, &Error, &ExecutionFailed); 158 if (!Error.empty()) { 159 assert(Res && "Error string set with 0 result code!"); 160 getDriver().Diag(clang::diag::err_drv_command_failure) << Error; 161 } 162 163 if (Res) 164 FailingCommand = &C; 165 166 return ExecutionFailed ? 1 : Res; 167 } 168 169 void Compilation::ExecuteJobs( 170 const JobList &Jobs, 171 SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const { 172 for (const auto &Job : Jobs) { 173 const Command *FailingCommand = nullptr; 174 if (int Res = ExecuteCommand(Job, FailingCommand)) { 175 FailingCommands.push_back(std::make_pair(Res, FailingCommand)); 176 // Bail as soon as one command fails, so we don't output duplicate error 177 // messages if we die on e.g. the same file. 178 return; 179 } 180 } 181 } 182 183 void Compilation::initCompilationForDiagnostics() { 184 ForDiagnostics = true; 185 186 // Free actions and jobs. 187 Actions.clear(); 188 AllActions.clear(); 189 Jobs.clear(); 190 191 // Clear temporary/results file lists. 192 TempFiles.clear(); 193 ResultFiles.clear(); 194 FailureResultFiles.clear(); 195 196 // Remove any user specified output. Claim any unclaimed arguments, so as 197 // to avoid emitting warnings about unused args. 198 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD, 199 options::OPT_MMD }; 200 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) { 201 if (TranslatedArgs->hasArg(OutputOpts[i])) 202 TranslatedArgs->eraseArg(OutputOpts[i]); 203 } 204 TranslatedArgs->ClaimAllArgs(); 205 206 // Redirect stdout/stderr to /dev/null. 207 Redirects = new const StringRef*[3](); 208 Redirects[0] = nullptr; 209 Redirects[1] = new StringRef(); 210 Redirects[2] = new StringRef(); 211 } 212 213 StringRef Compilation::getSysRoot() const { 214 return getDriver().SysRoot; 215 } 216 217 void Compilation::Redirect(const StringRef** Redirects) { 218 this->Redirects = Redirects; 219 } 220