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