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