clang  3.8.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),
28  CudaHostToolChain(&DefaultToolChain), CudaDeviceToolChain(nullptr),
29  Args(_Args), TranslatedArgs(_TranslatedArgs), Redirects(nullptr),
30  ForDiagnostics(false) {}
31 
33  delete TranslatedArgs;
34  delete Args;
35 
36  // Free any derived arg lists.
37  for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
38  DerivedArgList*>::iterator it = TCArgs.begin(),
39  ie = TCArgs.end(); it != ie; ++it)
40  if (it->second != TranslatedArgs)
41  delete it->second;
42 
43  // Free redirections of stdout/stderr.
44  if (Redirects) {
45  delete Redirects[1];
46  delete Redirects[2];
47  delete [] Redirects;
48  }
49 }
50 
51 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
52  const char *BoundArch) {
53  if (!TC)
54  TC = &DefaultToolChain;
55 
56  DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
57  if (!Entry) {
58  Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
59  if (!Entry)
60  Entry = TranslatedArgs;
61  }
62 
63  return *Entry;
64 }
65 
66 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
67  // FIXME: Why are we trying to remove files that we have not created? For
68  // example we should only try to remove a temporary assembly file if
69  // "clang -cc1" succeed in writing it. Was this a workaround for when
70  // clang was writing directly to a .s file and sometimes leaving it behind
71  // during a failure?
72 
73  // FIXME: If this is necessary, we can still try to split
74  // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
75  // duplicated stat from is_regular_file.
76 
77  // Don't try to remove files which we don't have write access to (but may be
78  // able to remove), or non-regular files. Underlying tools may have
79  // intentionally not overwritten them.
80  if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
81  return true;
82 
83  if (std::error_code EC = llvm::sys::fs::remove(File)) {
84  // Failure is only failure if the file exists and is "regular". We checked
85  // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
86  // so we don't need to check again.
87 
88  if (IssueErrors)
89  getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
90  << EC.message();
91  return false;
92  }
93  return true;
94 }
95 
96 bool Compilation::CleanupFileList(const ArgStringList &Files,
97  bool IssueErrors) const {
98  bool Success = true;
99  for (ArgStringList::const_iterator
100  it = Files.begin(), ie = Files.end(); it != ie; ++it)
101  Success &= CleanupFile(*it, IssueErrors);
102  return Success;
103 }
104 
106  const JobAction *JA,
107  bool IssueErrors) const {
108  bool Success = true;
109  for (ArgStringMap::const_iterator
110  it = Files.begin(), ie = Files.end(); it != ie; ++it) {
111 
112  // If specified, only delete the files associated with the JobAction.
113  // Otherwise, delete all files in the map.
114  if (JA && it->first != JA)
115  continue;
116  Success &= CleanupFile(it->second, IssueErrors);
117  }
118  return Success;
119 }
120 
122  const Command *&FailingCommand) const {
123  if ((getDriver().CCPrintOptions ||
124  getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
125  raw_ostream *OS = &llvm::errs();
126 
127  // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
128  // output stream.
129  if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
130  std::error_code EC;
131  OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC,
132  llvm::sys::fs::F_Append |
133  llvm::sys::fs::F_Text);
134  if (EC) {
135  getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
136  << EC.message();
137  FailingCommand = &C;
138  delete OS;
139  return 1;
140  }
141  }
142 
143  if (getDriver().CCPrintOptions)
144  *OS << "[Logging clang options]";
145 
146  C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
147 
148  if (OS != &llvm::errs())
149  delete OS;
150  }
151 
152  std::string Error;
153  bool ExecutionFailed;
154  int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
155  if (!Error.empty()) {
156  assert(Res && "Error string set with 0 result code!");
157  getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
158  }
159 
160  if (Res)
161  FailingCommand = &C;
162 
163  return ExecutionFailed ? 1 : Res;
164 }
165 
166 typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
167 
168 static bool ActionFailed(const Action *A,
169  const FailingCommandList &FailingCommands) {
170 
171  if (FailingCommands.empty())
172  return false;
173 
174  for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
175  CE = FailingCommands.end(); CI != CE; ++CI)
176  if (A == &(CI->second->getSource()))
177  return true;
178 
179  for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
180  if (ActionFailed(*AI, FailingCommands))
181  return true;
182 
183  return false;
184 }
185 
186 static bool InputsOk(const Command &C,
187  const FailingCommandList &FailingCommands) {
188  return !ActionFailed(&C.getSource(), FailingCommands);
189 }
190 
192  FailingCommandList &FailingCommands) const {
193  for (const auto &Job : Jobs) {
194  if (!InputsOk(Job, FailingCommands))
195  continue;
196  const Command *FailingCommand = nullptr;
197  if (int Res = ExecuteCommand(Job, FailingCommand))
198  FailingCommands.push_back(std::make_pair(Res, FailingCommand));
199  }
200 }
201 
203  ForDiagnostics = true;
204 
205  // Free actions and jobs.
206  Actions.clear();
207  AllActions.clear();
208  Jobs.clear();
209 
210  // Clear temporary/results file lists.
211  TempFiles.clear();
212  ResultFiles.clear();
213  FailureResultFiles.clear();
214 
215  // Remove any user specified output. Claim any unclaimed arguments, so as
216  // to avoid emitting warnings about unused args.
217  OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
218  options::OPT_MMD };
219  for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
220  if (TranslatedArgs->hasArg(OutputOpts[i]))
221  TranslatedArgs->eraseArg(OutputOpts[i]);
222  }
223  TranslatedArgs->ClaimAllArgs();
224 
225  // Redirect stdout/stderr to /dev/null.
226  Redirects = new const StringRef*[3]();
227  Redirects[0] = nullptr;
228  Redirects[1] = new StringRef();
229  Redirects[2] = new StringRef();
230 }
231 
232 StringRef Compilation::getSysRoot() const {
233  return getDriver().SysRoot;
234 }
const Driver & getDriver() const
Definition: Compilation.h:89
virtual llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, const char *BoundArch) const
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: ToolChain.h:182
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:90
iterator begin()
Definition: Action.h:101
static bool InputsOk(const Command &C, const FailingCommandList &FailingCommands)
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Definition: Compilation.cpp:66
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:154
Action - Represent an abstract compilation step to perform.
Definition: Action.h:41
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:106
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
Definition: Job.h:100
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
CleanupFileList - Remove the files in the given list.
Definition: Compilation.cpp:96
virtual int Execute(const StringRef **Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition: Job.cpp:218
JobList - A sequence of jobs to perform.
Definition: Job.h:142
iterator end()
Definition: Action.h:102
ActionList::const_iterator const_iterator
Definition: Action.h:45
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:306
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
Command - An executable path/name and argument vector to execute.
Definition: Job.h:43
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, const char *BoundArch)
getArgsForToolChain - Return the derived argument list for the tool chain TC (or the default tool cha...
Definition: Compilation.cpp:51
Represents a template argument.
Definition: TemplateBase.h:40
static bool ActionFailed(const Action *A, const FailingCommandList &FailingCommands)
std::string SysRoot
sysroot, if present
Definition: Driver.h:120
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * >> &FailingCommands) const
ExecuteJob - Execute a single job.
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
ExecuteCommand - Execute an actual command.
StringRef getSysRoot() const
Returns the sysroot path.
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:47