1 | |
2 | |
3 | |
4 | |
5 | |
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 | |
42 | OrderedOffloadingToolchains.insert( |
43 | std::make_pair(Action::OFK_Host, &DefaultToolChain)); |
44 | } |
45 | |
46 | Compilation::~Compilation() { |
47 | |
48 | |
49 | if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles) |
50 | CleanupFileList(TempFiles); |
51 | |
52 | delete TranslatedArgs; |
53 | delete Args; |
54 | |
55 | |
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 | |
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 | |
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 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
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 | |
118 | |
119 | |
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 | |
143 | |
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 | |
157 | |
158 | |
159 | if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) { |
160 | std::error_code EC; |
161 | OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, EC, |
162 | llvm::sys::fs::F_Append | |
163 | llvm::sys::fs::F_Text); |
164 | if (EC) { |
165 | getDriver().Diag(diag::err_drv_cc_print_options_failure) |
166 | << EC.message(); |
167 | FailingCommand = &C; |
168 | delete OS; |
169 | return 1; |
170 | } |
171 | } |
172 | |
173 | if (getDriver().CCPrintOptions) |
174 | *OS << "[Logging clang options]"; |
175 | |
176 | C.Print(*OS, "\n", ().CCPrintOptions); |
177 | |
178 | if (OS != &llvm::errs()) |
179 | delete OS; |
180 | } |
181 | |
182 | std::string Error; |
183 | bool ExecutionFailed; |
184 | int Res = C.Execute(Redirects, &Error, &ExecutionFailed); |
185 | if (!Error.empty()) { |
186 | (0) . __assert_fail ("Res && \"Error string set with 0 result code!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Compilation.cpp", 186, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Res && "Error string set with 0 result code!"); |
187 | getDriver().Diag(diag::err_drv_command_failure) << Error; |
188 | } |
189 | |
190 | if (Res) |
191 | FailingCommand = &C; |
192 | |
193 | return ExecutionFailed ? 1 : Res; |
194 | } |
195 | |
196 | using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>; |
197 | |
198 | static bool ActionFailed(const Action *A, |
199 | const FailingCommandList &FailingCommands) { |
200 | if (FailingCommands.empty()) |
201 | return false; |
202 | |
203 | |
204 | |
205 | |
206 | if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP)) |
207 | return true; |
208 | |
209 | for (const auto &CI : FailingCommands) |
210 | if (A == &(CI.second->getSource())) |
211 | return true; |
212 | |
213 | for (const auto *AI : A->inputs()) |
214 | if (ActionFailed(AI, FailingCommands)) |
215 | return true; |
216 | |
217 | return false; |
218 | } |
219 | |
220 | static bool InputsOk(const Command &C, |
221 | const FailingCommandList &FailingCommands) { |
222 | return !ActionFailed(&C.getSource(), FailingCommands); |
223 | } |
224 | |
225 | void Compilation::ExecuteJobs(const JobList &Jobs, |
226 | FailingCommandList &FailingCommands) const { |
227 | |
228 | |
229 | |
230 | |
231 | for (const auto &Job : Jobs) { |
232 | if (!InputsOk(Job, FailingCommands)) |
233 | continue; |
234 | const Command *FailingCommand = nullptr; |
235 | if (int Res = ExecuteCommand(Job, FailingCommand)) { |
236 | FailingCommands.push_back(std::make_pair(Res, FailingCommand)); |
237 | |
238 | if (TheDriver.IsCLMode()) |
239 | return; |
240 | } |
241 | } |
242 | } |
243 | |
244 | void Compilation::initCompilationForDiagnostics() { |
245 | ForDiagnostics = true; |
246 | |
247 | |
248 | Actions.clear(); |
249 | AllActions.clear(); |
250 | Jobs.clear(); |
251 | |
252 | |
253 | if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles) |
254 | CleanupFileList(TempFiles); |
255 | |
256 | |
257 | TempFiles.clear(); |
258 | ResultFiles.clear(); |
259 | FailureResultFiles.clear(); |
260 | |
261 | |
262 | |
263 | OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD, |
264 | options::OPT_MMD }; |
265 | for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) { |
266 | if (TranslatedArgs->hasArg(OutputOpts[i])) |
267 | TranslatedArgs->eraseArg(OutputOpts[i]); |
268 | } |
269 | TranslatedArgs->ClaimAllArgs(); |
270 | |
271 | |
272 | Redirects = {None, {""}, {""}}; |
273 | |
274 | |
275 | ForceKeepTempFiles = true; |
276 | } |
277 | |
278 | StringRef Compilation::getSysRoot() const { |
279 | return getDriver().SysRoot; |
280 | } |
281 | |
282 | void Compilation::Redirect(ArrayRef<Optional<StringRef>> Redirects) { |
283 | this->Redirects = Redirects; |
284 | } |
285 | |