clang  16.0.0git
Compilation.cpp
Go to the documentation of this file.
1 //===- Compilation.cpp - Compilation Task Implementation ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/Basic/LLVM.h"
11 #include "clang/Driver/Action.h"
12 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Job.h"
15 #include "clang/Driver/Options.h"
16 #include "clang/Driver/ToolChain.h"
17 #include "clang/Driver/Util.h"
18 #include "llvm/ADT/None.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Option/OptSpecifier.h"
24 #include "llvm/Option/Option.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cassert>
28 #include <string>
29 #include <system_error>
30 #include <utility>
31 
32 using namespace clang;
33 using namespace driver;
34 using namespace llvm::opt;
35 
36 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
37  InputArgList *_Args, DerivedArgList *_TranslatedArgs,
38  bool ContainsError)
39  : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
40  TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
41  // The offloading host toolchain is the default toolchain.
42  OrderedOffloadingToolchains.insert(
43  std::make_pair(Action::OFK_Host, &DefaultToolChain));
44 }
45 
47  // Remove temporary files. This must be done before arguments are freed, as
48  // the file names might be derived from the input arguments.
49  if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
50  CleanupFileList(TempFiles);
51 
52  delete TranslatedArgs;
53  delete Args;
54 
55  // Free any derived arg lists.
56  for (auto Arg : TCArgs)
57  if (Arg.second != TranslatedArgs)
58  delete Arg.second;
59 }
60 
61 const DerivedArgList &
62 Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
63  Action::OffloadKind DeviceOffloadKind) {
64  if (!TC)
65  TC = &DefaultToolChain;
66 
67  DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];
68  if (!Entry) {
69  SmallVector<Arg *, 4> AllocatedArgs;
70  DerivedArgList *OpenMPArgs = nullptr;
71  // Translate OpenMP toolchain arguments provided via the -Xopenmp-target flags.
72  if (DeviceOffloadKind == Action::OFK_OpenMP) {
73  const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>();
74  bool SameTripleAsHost = (TC->getTriple() == HostTC->getTriple());
75  OpenMPArgs = TC->TranslateOpenMPTargetArgs(
76  *TranslatedArgs, SameTripleAsHost, AllocatedArgs);
77  }
78 
79  DerivedArgList *NewDAL = nullptr;
80  if (!OpenMPArgs) {
81  NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
82  DeviceOffloadKind, &AllocatedArgs);
83  } else {
84  NewDAL = TC->TranslateXarchArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind,
85  &AllocatedArgs);
86  if (!NewDAL)
87  NewDAL = OpenMPArgs;
88  else
89  delete OpenMPArgs;
90  }
91 
92  if (!NewDAL) {
93  Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
94  if (!Entry)
95  Entry = TranslatedArgs;
96  } else {
97  Entry = TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind);
98  if (!Entry)
99  Entry = NewDAL;
100  else
101  delete NewDAL;
102  }
103 
104  // Add allocated arguments to the final DAL.
105  for (auto *ArgPtr : AllocatedArgs)
106  Entry->AddSynthesizedArg(ArgPtr);
107  }
108 
109  return *Entry;
110 }
111 
112 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
113  // FIXME: Why are we trying to remove files that we have not created? For
114  // example we should only try to remove a temporary assembly file if
115  // "clang -cc1" succeed in writing it. Was this a workaround for when
116  // clang was writing directly to a .s file and sometimes leaving it behind
117  // during a failure?
118 
119  // FIXME: If this is necessary, we can still try to split
120  // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
121  // duplicated stat from is_regular_file.
122 
123  // Don't try to remove files which we don't have write access to (but may be
124  // able to remove), or non-regular files. Underlying tools may have
125  // intentionally not overwritten them.
126  if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
127  return true;
128 
129  if (std::error_code EC = llvm::sys::fs::remove(File)) {
130  // Failure is only failure if the file exists and is "regular". We checked
131  // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
132  // so we don't need to check again.
133 
134  if (IssueErrors)
135  getDriver().Diag(diag::err_drv_unable_to_remove_file)
136  << EC.message();
137  return false;
138  }
139  return true;
140 }
141 
142 bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files,
143  bool IssueErrors) const {
144  bool Success = true;
145  for (const auto &File: Files)
146  Success &= CleanupFile(File, IssueErrors);
147  return Success;
148 }
149 
151  const JobAction *JA,
152  bool IssueErrors) const {
153  bool Success = true;
154  for (const auto &File : Files) {
155  // If specified, only delete the files associated with the JobAction.
156  // Otherwise, delete all files in the map.
157  if (JA && File.first != JA)
158  continue;
159  Success &= CleanupFile(File.second, IssueErrors);
160  }
161  return Success;
162 }
163 
165  const Command *&FailingCommand,
166  bool LogOnly) const {
167  if ((getDriver().CCPrintOptions ||
168  getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
169  raw_ostream *OS = &llvm::errs();
170  std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
171 
172  // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
173  // output stream.
174  if (getDriver().CCPrintOptions &&
175  !getDriver().CCPrintOptionsFilename.empty()) {
176  std::error_code EC;
177  OwnedStream.reset(new llvm::raw_fd_ostream(
178  getDriver().CCPrintOptionsFilename, EC,
179  llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF));
180  if (EC) {
181  getDriver().Diag(diag::err_drv_cc_print_options_failure)
182  << EC.message();
183  FailingCommand = &C;
184  return 1;
185  }
186  OS = OwnedStream.get();
187  }
188 
189  if (getDriver().CCPrintOptions)
190  *OS << "[Logging clang options]\n";
191 
192  C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
193  }
194 
195  if (LogOnly)
196  return 0;
197 
199  bool ExecutionFailed;
200  int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
201  if (PostCallback)
202  PostCallback(C, Res);
203  if (!Error.empty()) {
204  assert(Res && "Error string set with 0 result code!");
205  getDriver().Diag(diag::err_drv_command_failure) << Error;
206  }
207 
208  if (Res)
209  FailingCommand = &C;
210 
211  return ExecutionFailed ? 1 : Res;
212 }
213 
214 using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>;
215 
216 static bool ActionFailed(const Action *A,
217  const FailingCommandList &FailingCommands) {
218  if (FailingCommands.empty())
219  return false;
220 
221  // CUDA/HIP can have the same input source code compiled multiple times so do
222  // not compiled again if there are already failures. It is OK to abort the
223  // CUDA pipeline on errors.
225  return true;
226 
227  for (const auto &CI : FailingCommands)
228  if (A == &(CI.second->getSource()))
229  return true;
230 
231  for (const auto *AI : A->inputs())
232  if (ActionFailed(AI, FailingCommands))
233  return true;
234 
235  return false;
236 }
237 
238 static bool InputsOk(const Command &C,
239  const FailingCommandList &FailingCommands) {
240  return !ActionFailed(&C.getSource(), FailingCommands);
241 }
242 
244  FailingCommandList &FailingCommands,
245  bool LogOnly) const {
246  // According to UNIX standard, driver need to continue compiling all the
247  // inputs on the command line even one of them failed.
248  // In all but CLMode, execute all the jobs unless the necessary inputs for the
249  // job is missing due to previous failures.
250  for (const auto &Job : Jobs) {
251  if (!InputsOk(Job, FailingCommands))
252  continue;
253  const Command *FailingCommand = nullptr;
254  if (int Res = ExecuteCommand(Job, FailingCommand, LogOnly)) {
255  FailingCommands.push_back(std::make_pair(Res, FailingCommand));
256  // Bail as soon as one command fails in cl driver mode.
257  if (TheDriver.IsCLMode())
258  return;
259  }
260  }
261 }
262 
264  ForDiagnostics = true;
265 
266  // Free actions and jobs.
267  Actions.clear();
268  AllActions.clear();
269  Jobs.clear();
270 
271  // Remove temporary files.
272  if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
273  CleanupFileList(TempFiles);
274 
275  // Clear temporary/results file lists.
276  TempFiles.clear();
277  ResultFiles.clear();
278  FailureResultFiles.clear();
279 
280  // Remove any user specified output. Claim any unclaimed arguments, so as
281  // to avoid emitting warnings about unused args.
282  OptSpecifier OutputOpts[] = {
283  options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M,
284  options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ,
285  options::OPT_MQ, options::OPT_MT, options::OPT_MV};
286  for (const auto &Opt : OutputOpts) {
287  if (TranslatedArgs->hasArg(Opt))
288  TranslatedArgs->eraseArg(Opt);
289  }
290  TranslatedArgs->ClaimAllArgs();
291 
292  // Force re-creation of the toolchain Args, otherwise our modifications just
293  // above will have no effect.
294  for (auto Arg : TCArgs)
295  if (Arg.second != TranslatedArgs)
296  delete Arg.second;
297  TCArgs.clear();
298 
299  // Redirect stdout/stderr to /dev/null.
300  Redirects = {None, {""}, {""}};
301 
302  // Temporary files added by diagnostics should be kept.
303  ForceKeepTempFiles = true;
304 }
305 
306 StringRef Compilation::getSysRoot() const {
307  return getDriver().SysRoot;
308 }
309 
311  this->Redirects = Redirects;
312 }
clang::driver::Compilation::initCompilationForDiagnostics
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
Definition: Compilation.cpp:263
clang::driver::Compilation::getArgsForToolChain
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind)
getArgsForToolChain - Return the derived argument list for the tool chain TC (or the default tool cha...
Definition: Compilation.cpp:62
Util.h
clang::DeclaratorContext::File
@ File
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:20
Driver.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::driver::Action::OFK_Cuda
@ OFK_Cuda
Definition: Action.h:93
clang::driver::Driver::IsCLMode
bool IsCLMode() const
Whether the driver should follow cl.exe like behavior.
Definition: Driver.h:220
clang::driver::ToolChain::TranslateXarchArgs
virtual void TranslateXarchArgs(const llvm::opt::DerivedArgList &Args, llvm::opt::Arg *&A, llvm::opt::DerivedArgList *DAL, SmallVectorImpl< llvm::opt::Arg * > *AllocatedArgs=nullptr) const
Append the argument following A to DAL assuming A is an Xarch argument.
Definition: ToolChain.cpp:1247
llvm::SmallVector
Definition: LLVM.h:38
clang::driver::Action
Action - Represent an abstract compilation step to perform.
Definition: Action.h:47
clang::driver::Action::inputs
input_range inputs()
Definition: Action.h:156
Job.h
clang::driver::Action::OFK_HIP
@ OFK_HIP
Definition: Action.h:95
llvm::Optional
Definition: LLVM.h:40
clang::driver::JobList::clear
void clear()
Clear the job list.
Definition: Job.cpp:471
FailingCommandList
SmallVectorImpl< std::pair< int, const Command * > > FailingCommandList
Definition: Compilation.cpp:214
clang::driver::Action::OFK_OpenMP
@ OFK_OpenMP
Definition: Action.h:94
clang::driver::Compilation::ExecuteCommand
int ExecuteCommand(const Command &C, const Command *&FailingCommand, bool LogOnly=false) const
ExecuteCommand - Execute an actual command.
Definition: Compilation.cpp:164
clang::driver::Compilation::getDriver
const Driver & getDriver() const
Definition: Compilation.h:138
Action.h
clang::driver::Driver::isSaveTempsEnabled
bool isSaveTempsEnabled() const
Definition: Driver.h:398
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:142
Options.h
llvm::opt
Definition: DiagnosticOptions.h:19
DriverDiagnostic.h
clang::driver::Compilation::Compilation
Compilation(const Driver &D, const ToolChain &DefaultToolChain, llvm::opt::InputArgList *Args, llvm::opt::DerivedArgList *TranslatedArgs, bool ContainsError)
Definition: Compilation.cpp:36
clang::driver::Compilation::~Compilation
~Compilation()
Definition: Compilation.cpp:46
clang::driver::JobList
JobList - A sequence of jobs to perform.
Definition: Job.h:272
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:234
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:181
clang::driver::Action::OFK_Host
@ OFK_Host
Definition: Action.h:90
clang::driver::ToolChain::TranslateOpenMPTargetArgs
virtual llvm::opt::DerivedArgList * TranslateOpenMPTargetArgs(const llvm::opt::DerivedArgList &Args, bool SameTripleAsHost, SmallVectorImpl< llvm::opt::Arg * > &AllocatedArgs) const
TranslateOpenMPTargetArgs - Create a new derived argument list for that contains the OpenMP target sp...
Definition: ToolChain.cpp:1175
clang::driver::Compilation::Redirect
void Redirect(ArrayRef< Optional< StringRef >> Redirects)
Redirect - Redirect output of this compilation.
Definition: Compilation.cpp:310
clang::driver::Compilation::getArgs
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:198
clang::driver::Compilation::ExecuteJobs
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * >> &FailingCommands, bool LogOnly=false) const
ExecuteJob - Execute a single job.
Definition: Compilation.cpp:243
ToolChain.h
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:163
clang::driver::Command
Command - An executable path/name and argument vector to execute.
Definition: Job.h:106
Compilation.h
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:91
llvm::ArrayRef
Definition: LLVM.h:34
clang::driver::ArgStringMap
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:19
clang::driver::Compilation::CleanupFileMap
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
Definition: Compilation.cpp:150
LLVM.h
clang::driver::Compilation::CleanupFile
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Definition: Compilation.cpp:112
clang::driver::Compilation::CleanupFileList
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
CleanupFileList - Remove the files in the given list.
Definition: Compilation.cpp:142
clang
Definition: CalledOnceCheck.h:17
InputsOk
static bool InputsOk(const Command &C, const FailingCommandList &FailingCommands)
Definition: Compilation.cpp:238
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:75
clang::driver::ToolChain::TranslateArgs
virtual llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: ToolChain.h:321
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:86
clang::driver::Action::isOffloading
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:223
clang::driver::JobAction
Definition: Action.h:398
clang::driver::Compilation::getSysRoot
StringRef getSysRoot() const
Returns the sysroot path.
Definition: Compilation.cpp:306
ActionFailed
static bool ActionFailed(const Action *A, const FailingCommandList &FailingCommands)
Definition: Compilation.cpp:216