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"
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"
21 using namespace clang::driver;
22 using namespace clang;
23 using namespace llvm::opt;
25 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
26 InputArgList *_Args, DerivedArgList *_TranslatedArgs)
27 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
28 TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
29 ForDiagnostics(false) {}
31 Compilation::~Compilation() {
32 delete TranslatedArgs;
35 // Free any derived arg lists.
36 for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
37 DerivedArgList*>::iterator it = TCArgs.begin(),
38 ie = TCArgs.end(); it != ie; ++it)
39 if (it->second != TranslatedArgs)
42 // Free the actions, if built.
43 for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
47 // Free redirections of stdout/stderr.
55 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
56 const char *BoundArch) {
58 TC = &DefaultToolChain;
60 DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
62 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
64 Entry = TranslatedArgs;
70 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
71 // FIXME: Why are we trying to remove files that we have not created? For
72 // example we should only try to remove a temporary assembly file if
73 // "clang -cc1" succeed in writing it. Was this a workaround for when
74 // clang was writing directly to a .s file and sometimes leaving it behind
77 // FIXME: If this is necessary, we can still try to split
78 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
79 // duplicated stat from is_regular_file.
81 // Don't try to remove files which we don't have write access to (but may be
82 // able to remove), or non-regular files. Underlying tools may have
83 // intentionally not overwritten them.
84 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
87 if (std::error_code EC = llvm::sys::fs::remove(File)) {
88 // Failure is only failure if the file exists and is "regular". We checked
89 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
90 // so we don't need to check again.
93 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
100 bool Compilation::CleanupFileList(const ArgStringList &Files,
101 bool IssueErrors) const {
103 for (ArgStringList::const_iterator
104 it = Files.begin(), ie = Files.end(); it != ie; ++it)
105 Success &= CleanupFile(*it, IssueErrors);
109 bool Compilation::CleanupFileMap(const ArgStringMap &Files,
111 bool IssueErrors) const {
113 for (ArgStringMap::const_iterator
114 it = Files.begin(), ie = Files.end(); it != ie; ++it) {
116 // If specified, only delete the files associated with the JobAction.
117 // Otherwise, delete all files in the map.
118 if (JA && it->first != JA)
120 Success &= CleanupFile(it->second, IssueErrors);
125 int Compilation::ExecuteCommand(const Command &C,
126 const Command *&FailingCommand) const {
127 if ((getDriver().CCPrintOptions ||
128 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
129 raw_ostream *OS = &llvm::errs();
131 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
133 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
135 OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
136 llvm::sys::fs::F_Append |
137 llvm::sys::fs::F_Text);
138 if (!Error.empty()) {
139 getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
147 if (getDriver().CCPrintOptions)
148 *OS << "[Logging clang options]";
150 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
152 if (OS != &llvm::errs())
157 bool ExecutionFailed;
158 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
159 if (!Error.empty()) {
160 assert(Res && "Error string set with 0 result code!");
161 getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
167 return ExecutionFailed ? 1 : Res;
170 typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
172 static bool ActionFailed(const Action *A,
173 const FailingCommandList &FailingCommands) {
175 if (FailingCommands.empty())
178 for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
179 CE = FailingCommands.end(); CI != CE; ++CI)
180 if (A == &(CI->second->getSource()))
183 for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
184 if (ActionFailed(*AI, FailingCommands))
190 static bool InputsOk(const Command &C,
191 const FailingCommandList &FailingCommands) {
192 return !ActionFailed(&C.getSource(), FailingCommands);
195 void Compilation::ExecuteJob(const Job &J,
196 FailingCommandList &FailingCommands) const {
197 if (const Command *C = dyn_cast<Command>(&J)) {
198 if (!InputsOk(*C, FailingCommands))
200 const Command *FailingCommand = nullptr;
201 if (int Res = ExecuteCommand(*C, FailingCommand))
202 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
204 const JobList *Jobs = cast<JobList>(&J);
205 for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
207 ExecuteJob(**it, FailingCommands);
211 void Compilation::initCompilationForDiagnostics() {
212 ForDiagnostics = true;
214 // Free actions and jobs.
215 DeleteContainerPointers(Actions);
218 // Clear temporary/results file lists.
221 FailureResultFiles.clear();
223 // Remove any user specified output. Claim any unclaimed arguments, so as
224 // to avoid emitting warnings about unused args.
225 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
227 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
228 if (TranslatedArgs->hasArg(OutputOpts[i]))
229 TranslatedArgs->eraseArg(OutputOpts[i]);
231 TranslatedArgs->ClaimAllArgs();
233 // Redirect stdout/stderr to /dev/null.
234 Redirects = new const StringRef*[3]();
235 Redirects[0] = nullptr;
236 Redirects[1] = new const StringRef();
237 Redirects[2] = new const StringRef();
240 StringRef Compilation::getSysRoot() const {
241 return getDriver().SysRoot;