]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Driver/Compilation.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Driver / Compilation.cpp
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 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 }