clang  3.7.0
Compilation.cpp
Go to the documentation of this file.
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 
11 #include "clang/Driver/Action.h"
12 #include "clang/Driver/Driver.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), Args(_Args),
28  TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
29  ForDiagnostics(false) {}
30 
32  delete TranslatedArgs;
33  delete Args;
34 
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)
40  delete it->second;
41 
42  // Free the actions, if built.
43  for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
44  it != ie; ++it)
45  delete *it;
46 
47  // Free redirections of stdout/stderr.
48  if (Redirects) {
49  delete Redirects[1];
50  delete Redirects[2];
51  delete [] Redirects;
52  }
53 }
54 
55 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
56  const char *BoundArch) {
57  if (!TC)
58  TC = &DefaultToolChain;
59 
60  DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
61  if (!Entry) {
62  Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
63  if (!Entry)
64  Entry = TranslatedArgs;
65  }
66 
67  return *Entry;
68 }
69 
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
75  // during a failure?
76 
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.
80 
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))
85  return true;
86 
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.
91 
92  if (IssueErrors)
93  getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
94  << EC.message();
95  return false;
96  }
97  return true;
98 }
99 
100 bool Compilation::CleanupFileList(const ArgStringList &Files,
101  bool IssueErrors) const {
102  bool Success = true;
103  for (ArgStringList::const_iterator
104  it = Files.begin(), ie = Files.end(); it != ie; ++it)
105  Success &= CleanupFile(*it, IssueErrors);
106  return Success;
107 }
108 
110  const JobAction *JA,
111  bool IssueErrors) const {
112  bool Success = true;
113  for (ArgStringMap::const_iterator
114  it = Files.begin(), ie = Files.end(); it != ie; ++it) {
115 
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)
119  continue;
120  Success &= CleanupFile(it->second, IssueErrors);
121  }
122  return Success;
123 }
124 
126  const Command *&FailingCommand) const {
127  if ((getDriver().CCPrintOptions ||
128  getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
129  raw_ostream *OS = &llvm::errs();
130 
131  // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
132  // output stream.
133  if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
134  std::error_code EC;
135  OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
136  llvm::sys::fs::F_Append |
137  llvm::sys::fs::F_Text);
138  if (EC) {
139  getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
140  << EC.message();
141  FailingCommand = &C;
142  delete OS;
143  return 1;
144  }
145  }
146 
147  if (getDriver().CCPrintOptions)
148  *OS << "[Logging clang options]";
149 
150  C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
151 
152  if (OS != &llvm::errs())
153  delete OS;
154  }
155 
156  std::string Error;
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;
162  }
163 
164  if (Res)
165  FailingCommand = &C;
166 
167  return ExecutionFailed ? 1 : Res;
168 }
169 
170 typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
171 
172 static bool ActionFailed(const Action *A,
173  const FailingCommandList &FailingCommands) {
174 
175  if (FailingCommands.empty())
176  return false;
177 
178  for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
179  CE = FailingCommands.end(); CI != CE; ++CI)
180  if (A == &(CI->second->getSource()))
181  return true;
182 
183  for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
184  if (ActionFailed(*AI, FailingCommands))
185  return true;
186 
187  return false;
188 }
189 
190 static bool InputsOk(const Command &C,
191  const FailingCommandList &FailingCommands) {
192  return !ActionFailed(&C.getSource(), FailingCommands);
193 }
194 
196  FailingCommandList &FailingCommands) const {
197  for (const auto &Job : Jobs) {
198  if (!InputsOk(Job, FailingCommands))
199  continue;
200  const Command *FailingCommand = nullptr;
201  if (int Res = ExecuteCommand(Job, FailingCommand))
202  FailingCommands.push_back(std::make_pair(Res, FailingCommand));
203  }
204 }
205 
207  ForDiagnostics = true;
208 
209  // Free actions and jobs.
210  DeleteContainerPointers(Actions);
211  Jobs.clear();
212 
213  // Clear temporary/results file lists.
214  TempFiles.clear();
215  ResultFiles.clear();
216  FailureResultFiles.clear();
217 
218  // Remove any user specified output. Claim any unclaimed arguments, so as
219  // to avoid emitting warnings about unused args.
220  OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
221  options::OPT_MMD };
222  for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
223  if (TranslatedArgs->hasArg(OutputOpts[i]))
224  TranslatedArgs->eraseArg(OutputOpts[i]);
225  }
226  TranslatedArgs->ClaimAllArgs();
227 
228  // Redirect stdout/stderr to /dev/null.
229  Redirects = new const StringRef*[3]();
230  Redirects[0] = nullptr;
231  Redirects[1] = new StringRef();
232  Redirects[2] = new StringRef();
233 }
234 
235 StringRef Compilation::getSysRoot() const {
236  return getDriver().SysRoot;
237 }
const Driver & getDriver() const
Definition: Compilation.h:81
virtual llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, const char *BoundArch) const
Definition: ToolChain.h:162
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:72
iterator begin()
Definition: Action.h:102
static bool InputsOk(const Command &C, const FailingCommandList &FailingCommands)
bool CleanupFile(const char *File, bool IssueErrors=false) const
Definition: Compilation.cpp:70
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:145
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:87
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
Definition: Job.h:92
Compilation(const Driver &D, const ToolChain &DefaultToolChain, llvm::opt::InputArgList *Args, llvm::opt::DerivedArgList *TranslatedArgs)
Definition: Compilation.cpp:25
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
virtual int Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition: Job.cpp:213
JobList - A sequence of jobs to perform.
Definition: Job.h:133
iterator end()
Definition: Action.h:103
ActionList::const_iterator const_iterator
Definition: Action.h:39
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:21
SmallVectorImpl< std::pair< int, const Command * > > FailingCommandList
#define false
Definition: stdbool.h:33
void clear()
Clear the job list.
Definition: Job.cpp:300
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, const char *BoundArch)
Definition: Compilation.cpp:55
static bool ActionFailed(const Action *A, const FailingCommandList &FailingCommands)
std::string SysRoot
sysroot, if present
Definition: Driver.h:102
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * >> &FailingCommands) const
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
StringRef getSysRoot() const
Returns the sysroot path.
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:43