]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Driver/Compilation.h
Merge ^/head r305361 through r305389.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Driver / Compilation.h
1 //===--- Compilation.h - Compilation Task Data Structure --------*- C++ -*-===//
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 #ifndef LLVM_CLANG_DRIVER_COMPILATION_H
11 #define LLVM_CLANG_DRIVER_COMPILATION_H
12
13 #include "clang/Driver/Action.h"
14 #include "clang/Driver/Job.h"
15 #include "clang/Driver/Util.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/Support/Path.h"
18 #include <map>
19
20 namespace llvm {
21 namespace opt {
22   class DerivedArgList;
23   class InputArgList;
24 }
25 }
26
27 namespace clang {
28 namespace driver {
29   class Driver;
30   class JobList;
31   class ToolChain;
32
33 /// Compilation - A set of tasks to perform for a single driver
34 /// invocation.
35 class Compilation {
36   /// The driver we were created by.
37   const Driver &TheDriver;
38
39   /// The default tool chain.
40   const ToolChain &DefaultToolChain;
41
42   /// A mask of all the programming models the host has to support in the
43   /// current compilation.
44   unsigned ActiveOffloadMask;
45
46   /// Array with the toolchains of offloading host and devices in the order they
47   /// were requested by the user. We are preserving that order in case the code
48   /// generation needs to derive a programming-model-specific semantic out of
49   /// it.
50   std::multimap<Action::OffloadKind, const ToolChain *>
51       OrderedOffloadingToolchains;
52
53   /// The original (untranslated) input argument list.
54   llvm::opt::InputArgList *Args;
55
56   /// The driver translated arguments. Note that toolchains may perform their
57   /// own argument translation.
58   llvm::opt::DerivedArgList *TranslatedArgs;
59
60   /// The list of actions we've created via MakeAction.  This is not accessible
61   /// to consumers; it's here just to manage ownership.
62   std::vector<std::unique_ptr<Action>> AllActions;
63
64   /// The list of actions.  This is maintained and modified by consumers, via
65   /// getActions().
66   ActionList Actions;
67
68   /// The root list of jobs.
69   JobList Jobs;
70
71   /// Cache of translated arguments for a particular tool chain and bound
72   /// architecture.
73   llvm::DenseMap<std::pair<const ToolChain *, const char *>,
74                  llvm::opt::DerivedArgList *> TCArgs;
75
76   /// Temporary files which should be removed on exit.
77   llvm::opt::ArgStringList TempFiles;
78
79   /// Result files which should be removed on failure.
80   ArgStringMap ResultFiles;
81
82   /// Result files which are generated correctly on failure, and which should
83   /// only be removed if we crash.
84   ArgStringMap FailureResultFiles;
85
86   /// Redirection for stdout, stderr, etc.
87   const StringRef **Redirects;
88
89   /// Whether we're compiling for diagnostic purposes.
90   bool ForDiagnostics;
91
92 public:
93   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
94               llvm::opt::InputArgList *Args,
95               llvm::opt::DerivedArgList *TranslatedArgs);
96   ~Compilation();
97
98   const Driver &getDriver() const { return TheDriver; }
99
100   const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
101
102   unsigned isOffloadingHostKind(Action::OffloadKind Kind) const {
103     return ActiveOffloadMask & Kind;
104   }
105
106   /// Iterator that visits device toolchains of a given kind.
107   typedef const std::multimap<Action::OffloadKind,
108                               const ToolChain *>::const_iterator
109       const_offload_toolchains_iterator;
110   typedef std::pair<const_offload_toolchains_iterator,
111                     const_offload_toolchains_iterator>
112       const_offload_toolchains_range;
113
114   template <Action::OffloadKind Kind>
115   const_offload_toolchains_range getOffloadToolChains() const {
116     return OrderedOffloadingToolchains.equal_range(Kind);
117   }
118
119   /// Return an offload toolchain of the provided kind. Only one is expected to
120   /// exist.
121   template <Action::OffloadKind Kind>
122   const ToolChain *getSingleOffloadToolChain() const {
123     auto TCs = getOffloadToolChains<Kind>();
124
125     assert(TCs.first != TCs.second &&
126            "No tool chains of the selected kind exist!");
127     assert(std::next(TCs.first) == TCs.second &&
128            "More than one tool chain of the this kind exist.");
129     return TCs.first->second;
130   }
131
132   void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain,
133                                  Action::OffloadKind OffloadKind) {
134     assert(OffloadKind != Action::OFK_Host && OffloadKind != Action::OFK_None &&
135            "This is not a device tool chain!");
136
137     // Update the host offload kind to also contain this kind.
138     ActiveOffloadMask |= OffloadKind;
139     OrderedOffloadingToolchains.insert(
140         std::make_pair(OffloadKind, DeviceToolChain));
141   }
142
143   const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
144
145   const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
146
147   llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
148
149   ActionList &getActions() { return Actions; }
150   const ActionList &getActions() const { return Actions; }
151
152   /// Creates a new Action owned by this Compilation.
153   ///
154   /// The new Action is *not* added to the list returned by getActions().
155   template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
156     T *RawPtr = new T(std::forward<Args>(Arg)...);
157     AllActions.push_back(std::unique_ptr<Action>(RawPtr));
158     return RawPtr;
159   }
160
161   JobList &getJobs() { return Jobs; }
162   const JobList &getJobs() const { return Jobs; }
163
164   void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
165
166   const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
167
168   const ArgStringMap &getResultFiles() const { return ResultFiles; }
169
170   const ArgStringMap &getFailureResultFiles() const {
171     return FailureResultFiles;
172   }
173
174   /// Returns the sysroot path.
175   StringRef getSysRoot() const;
176
177   /// getArgsForToolChain - Return the derived argument list for the
178   /// tool chain \p TC (or the default tool chain, if TC is not specified).
179   ///
180   /// \param BoundArch - The bound architecture name, or 0.
181   const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
182                                                        const char *BoundArch);
183
184   /// addTempFile - Add a file to remove on exit, and returns its
185   /// argument.
186   const char *addTempFile(const char *Name) {
187     TempFiles.push_back(Name);
188     return Name;
189   }
190
191   /// addResultFile - Add a file to remove on failure, and returns its
192   /// argument.
193   const char *addResultFile(const char *Name, const JobAction *JA) {
194     ResultFiles[JA] = Name;
195     return Name;
196   }
197
198   /// addFailureResultFile - Add a file to remove if we crash, and returns its
199   /// argument.
200   const char *addFailureResultFile(const char *Name, const JobAction *JA) {
201     FailureResultFiles[JA] = Name;
202     return Name;
203   }
204
205   /// CleanupFile - Delete a given file.
206   ///
207   /// \param IssueErrors - Report failures as errors.
208   /// \return Whether the file was removed successfully.
209   bool CleanupFile(const char *File, bool IssueErrors = false) const;
210
211   /// CleanupFileList - Remove the files in the given list.
212   ///
213   /// \param IssueErrors - Report failures as errors.
214   /// \return Whether all files were removed successfully.
215   bool CleanupFileList(const llvm::opt::ArgStringList &Files,
216                        bool IssueErrors = false) const;
217
218   /// CleanupFileMap - Remove the files in the given map.
219   ///
220   /// \param JA - If specified, only delete the files associated with this
221   /// JobAction.  Otherwise, delete all files in the map.
222   /// \param IssueErrors - Report failures as errors.
223   /// \return Whether all files were removed successfully.
224   bool CleanupFileMap(const ArgStringMap &Files,
225                       const JobAction *JA,
226                       bool IssueErrors = false) const;
227
228   /// ExecuteCommand - Execute an actual command.
229   ///
230   /// \param FailingCommand - For non-zero results, this will be set to the
231   /// Command which failed, if any.
232   /// \return The result code of the subprocess.
233   int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
234
235   /// ExecuteJob - Execute a single job.
236   ///
237   /// \param FailingCommands - For non-zero results, this will be a vector of
238   /// failing commands and their associated result code.
239   void ExecuteJobs(
240       const JobList &Jobs,
241       SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
242
243   /// initCompilationForDiagnostics - Remove stale state and suppress output
244   /// so compilation can be reexecuted to generate additional diagnostic
245   /// information (e.g., preprocessed source(s)).
246   void initCompilationForDiagnostics();
247
248   /// Return true if we're compiling for diagnostics.
249   bool isForDiagnostics() const { return ForDiagnostics; }
250
251   /// Redirect - Redirect output of this compilation. Can only be done once.
252   ///
253   /// \param Redirects - array of pointers to paths. The array
254   /// should have a size of three. The inferior process's
255   /// stdin(0), stdout(1), and stderr(2) will be redirected to the
256   /// corresponding paths. This compilation instance becomes
257   /// the owner of Redirects and will delete the array and StringRef's.
258   void Redirect(const StringRef** Redirects);
259 };
260
261 } // end namespace driver
262 } // end namespace clang
263
264 #endif