clang 10.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
9 #include "clang/Driver/Compilation.h"
10 #include "clang/Basic/LLVM.h"
11 #include "clang/Driver/Action.h"
12 #include "clang/Driver/Driver.h"
13 #include "clang/Driver/DriverDiagnostic.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
46 Compilation::~Compilation() {
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 if (!OpenMPArgs) {
80 Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
81 if (!Entry)
82 Entry = TranslatedArgs;
83 } else {
84 Entry = TC->TranslateArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind);
85 if (!Entry)
86 Entry = OpenMPArgs;
87 else
88 delete OpenMPArgs;
89 }
90
91 // Add allocated arguments to the final DAL.
92 for (auto ArgPtr : AllocatedArgs)
93 Entry->AddSynthesizedArg(ArgPtr);
94 }
95
96 return *Entry;
97 }
98
99 bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
100 // FIXME: Why are we trying to remove files that we have not created? For
101 // example we should only try to remove a temporary assembly file if
102 // "clang -cc1" succeed in writing it. Was this a workaround for when
103 // clang was writing directly to a .s file and sometimes leaving it behind
104 // during a failure?
105
106 // FIXME: If this is necessary, we can still try to split
107 // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
108 // duplicated stat from is_regular_file.
109
110 // Don't try to remove files which we don't have write access to (but may be
111 // able to remove), or non-regular files. Underlying tools may have
112 // intentionally not overwritten them.
113 if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
114 return true;
115
116 if (std::error_code EC = llvm::sys::fs::remove(File)) {
117 // Failure is only failure if the file exists and is "regular". We checked
118 // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
119 // so we don't need to check again.
120
121 if (IssueErrors)
122 getDriver().Diag(diag::err_drv_unable_to_remove_file)
123 << EC.message();
124 return false;
125 }
126 return true;
127 }
128
129 bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files,
130 bool IssueErrors) const {
131 bool Success = true;
132 for (const auto &File: Files)
133 Success &= CleanupFile(File, IssueErrors);
134 return Success;
135 }
136
137 bool Compilation::CleanupFileMap(const ArgStringMap &Files,
138 const JobAction *JA,
139 bool IssueErrors) const {
140 bool Success = true;
141 for (const auto &File : Files) {
142 // If specified, only delete the files associated with the JobAction.
143 // Otherwise, delete all files in the map.
144 if (JA && File.first != JA)
145 continue;
146 Success &= CleanupFile(File.second, IssueErrors);
147 }
148 return Success;
149 }
150
151 int Compilation::ExecuteCommand(const Command &C,
152 const Command *&FailingCommand) const {
153 if ((getDriver().CCPrintOptions ||
154 getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
155 raw_ostream *OS = &llvm::errs();
156 std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
157
158 // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
159 // output stream.
160 if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
161 std::error_code EC;
162 OwnedStream.reset(new llvm::raw_fd_ostream(
163 getDriver().CCPrintOptionsFilename, EC,
164 llvm::sys::fs::OF_Append | llvm::sys::fs::OF_Text));
165 if (EC) {
166 getDriver().Diag(diag::err_drv_cc_print_options_failure)
167 << EC.message();
168 FailingCommand = &C;
169 return 1;
170 }
171 OS = OwnedStream.get();
172 }
173
174 if (getDriver().CCPrintOptions)
175 *OS << "[Logging clang options]\n";
176
177 C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
178 }
179
180 std::string Error;
181 bool ExecutionFailed;
182 int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
183 if (!Error.empty()) {
184 assert(Res && "Error string set with 0 result code!");
185 getDriver().Diag(diag::err_drv_command_failure) << Error;
186 }
187
188 if (Res)
189 FailingCommand = &C;
190
191 return ExecutionFailed ? 1 : Res;
192 }
193
194 using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>;
195
196 static bool ActionFailed(const Action *A,
197 const FailingCommandList &FailingCommands) {
198 if (FailingCommands.empty())
199 return false;
200
201 // CUDA/HIP can have the same input source code compiled multiple times so do
202 // not compiled again if there are already failures. It is OK to abort the
203 // CUDA pipeline on errors.
204 if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP))
205 return true;
206
207 for (const auto &CI : FailingCommands)
208 if (A == &(CI.second->getSource()))
209 return true;
210
211 for (const auto *AI : A->inputs())
212 if (ActionFailed(AI, FailingCommands))
213 return true;
214
215 return false;
216 }
217
218 static bool InputsOk(const Command &C,
219 const FailingCommandList &FailingCommands) {
220 return !ActionFailed(&C.getSource(), FailingCommands);
221 }
222
223 void Compilation::ExecuteJobs(const JobList &Jobs,
224 FailingCommandList &FailingCommands) const {
225 // According to UNIX standard, driver need to continue compiling all the
226 // inputs on the command line even one of them failed.
227 // In all but CLMode, execute all the jobs unless the necessary inputs for the
228 // job is missing due to previous failures.
229 for (const auto &Job : Jobs) {
230 if (!InputsOk(Job, FailingCommands))
231 continue;
232 const Command *FailingCommand = nullptr;
233 if (int Res = ExecuteCommand(Job, FailingCommand)) {
234 FailingCommands.push_back(std::make_pair(Res, FailingCommand));
235 // Bail as soon as one command fails in cl driver mode.
236 if (TheDriver.IsCLMode())
237 return;
238 }
239 }
240 }
241
242 void Compilation::initCompilationForDiagnostics() {
243 ForDiagnostics = true;
244
245 // Free actions and jobs.
246 Actions.clear();
247 AllActions.clear();
248 Jobs.clear();
249
250 // Remove temporary files.
251 if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
252 CleanupFileList(TempFiles);
253
254 // Clear temporary/results file lists.
255 TempFiles.clear();
256 ResultFiles.clear();
257 FailureResultFiles.clear();
258
259 // Remove any user specified output. Claim any unclaimed arguments, so as
260 // to avoid emitting warnings about unused args.
261 OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
262 options::OPT_MMD };
263 for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
264 if (TranslatedArgs->hasArg(OutputOpts[i]))
265 TranslatedArgs->eraseArg(OutputOpts[i]);
266 }
267 TranslatedArgs->ClaimAllArgs();
268
269 // Redirect stdout/stderr to /dev/null.
270 Redirects = {None, {""}, {""}};
271
272 // Temporary files added by diagnostics should be kept.
273 ForceKeepTempFiles = true;
274 }
275
276 StringRef Compilation::getSysRoot() const {
277 return getDriver().SysRoot;
278 }
279
280 void Compilation::Redirect(ArrayRef<Optional<StringRef>> Redirects) {
281 this->Redirects = Redirects;
282 }
clang::driver::Compilation::getSysRoot
StringRef getSysRoot() const
Returns the sysroot path.
Definition: Compilation.cpp:276
clang::driver::Action::OFK_OpenMP
Definition: Action.h:92
Job.h
clang::driver::Action::inputs
input_range inputs()
Definition: Action.h:151
clang::driver::Driver::Diag
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
InputsOk
static bool InputsOk(const Command &C, const FailingCommandList &FailingCommands)
Definition: Compilation.cpp:218
Action.h
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
llvm::Optional
Definition: LLVM.h:37
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:129
clang::driver::JobAction
Definition: Action.h:375
clang::driver::Action::isOffloading
bool isOffloading(OffloadKind OKind) const
Definition: Action.h:209
clang::driver::Action
Action - Represent an abstract compilation step to perform.
Definition: Action.h:47
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Error
llvm::Error Error
Definition: ByteCodeEmitter.cpp:19
clang::driver::Driver
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:59
llvm::ArrayRef
Definition: LLVM.h:31
Driver.h
Compilation.h
clang::driver::Action::OFK_HIP
Definition: Action.h:93
clang::driver::Action::OFK_Host
Definition: Action.h:88
llvm::opt
Definition: Action.h:23
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:1030
clang::driver::Driver::IsCLMode
bool IsCLMode() const
Whether the driver should follow cl.exe like behavior.
Definition: Driver.h:182
clang::driver::JobList
JobList - A sequence of jobs to perform.
Definition: Job.h:189
clang::transformer::remove
ASTEdit remove(RangeSelector S)
Removes the source selected by S.
Definition: RewriteRule.cpp:82
clang::driver::ArgStringMap
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:20
clang::driver::Compilation::CleanupFile
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Definition: Compilation.cpp:99
clang::driver::JobList::clear
void clear()
Clear the job list.
Definition: Job.cpp:483
clang::driver::Compilation::getArgs
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:186
clang::driver::Compilation::Redirect
void Redirect(ArrayRef< Optional< StringRef >> Redirects)
Redirect - Redirect output of this compilation.
Definition: Compilation.cpp:280
clang::driver::Command
Command - An executable path/name and argument vector to execute.
Definition: Job.h:41
ToolChain.h
clang::driver::Driver::isSaveTempsEnabled
bool isSaveTempsEnabled() const
Definition: Driver.h:347
Util.h
llvm::SmallVector
Definition: LLVM.h:35
clang::driver::Compilation::ExecuteCommand
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
ExecuteCommand - Execute an actual command.
Definition: Compilation.cpp:151
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
Options.h
clang::driver::Compilation::ExecuteJobs
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command *>> &FailingCommands) const
ExecuteJob - Execute a single job.
Definition: Compilation.cpp:223
clang::driver::Command::getSource
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
Definition: Job.h:104
clang::driver::Action::OffloadKind
OffloadKind
Definition: Action.h:84
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
ActionFailed
static bool ActionFailed(const Action *A, const FailingCommandList &FailingCommands)
Definition: Compilation.cpp:196
clang::driver::Driver::SysRoot
std::string SysRoot
sysroot, if present
Definition: Driver.h:148
clang::driver::Compilation::getDriver
const Driver & getDriver() const
Definition: Compilation.h:133
clang::driver::ToolChain::getTriple
const llvm::Triple & getTriple() const
Definition: ToolChain.h:201
DriverDiagnostic.h
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:137
FailingCommandList
SmallVectorImpl< std::pair< int, const Command * > > FailingCommandList
Definition: Compilation.cpp:194
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:284
clang::driver::Action::OFK_Cuda
Definition: Action.h:91
clang::driver::Compilation::initCompilationForDiagnostics
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
Definition: Compilation.cpp:242
clang::driver::Compilation::~Compilation
~Compilation()
Definition: Compilation.cpp:46
clang::driver::Command::Print
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:216
clang::driver::Command::Execute
virtual int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition: Job.cpp:326
clang::driver::ToolChain
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:88