clang  3.9.0
Compilation.h
Go to the documentation of this file.
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 
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
110  typedef std::pair<const_offload_toolchains_iterator,
113 
114  template <Action::OffloadKind Kind>
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>
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 
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)).
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
const Driver & getDriver() const
Definition: Compilation.h:98
const ArgStringMap & getResultFiles() const
Definition: Compilation.h:168
T * MakeAction(Args &&...Arg)
Creates a new Action owned by this Compilation.
Definition: Compilation.h:155
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Definition: Compilation.cpp:70
void addOffloadDeviceToolChain(const ToolChain *DeviceToolChain, Action::OffloadKind OffloadKind)
Definition: Compilation.h:132
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4549
const std::multimap< Action::OffloadKind, const ToolChain * >::const_iterator const_offload_toolchains_iterator
Iterator that visits device toolchains of a given kind.
Definition: Compilation.h:109
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:145
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:66
Compilation(const Driver &D, const ToolChain &DefaultToolChain, llvm::opt::InputArgList *Args, llvm::opt::DerivedArgList *TranslatedArgs)
Definition: Compilation.cpp:25
unsigned isOffloadingHostKind(Action::OffloadKind Kind) const
Definition: Compilation.h:102
llvm::opt::DerivedArgList & getArgs()
Definition: Compilation.h:147
bool isForDiagnostics() const
Return true if we're compiling for diagnostics.
Definition: Compilation.h:249
const ToolChain * getSingleOffloadToolChain() const
Return an offload toolchain of the provided kind.
Definition: Compilation.h:122
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
CleanupFileList - Remove the files in the given list.
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:164
JobList - A sequence of jobs to perform.
Definition: Job.h:156
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition: Job.h:171
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:21
Kind
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
void Redirect(const StringRef **Redirects)
Redirect - Redirect output of this compilation.
const_offload_toolchains_range getOffloadToolChains() const
Definition: Compilation.h:115
Command - An executable path/name and argument vector to execute.
Definition: Job.h:43
const char * addResultFile(const char *Name, const JobAction *JA)
addResultFile - Add a file to remove on failure, and returns its argument.
Definition: Compilation.h:193
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, const char *BoundArch)
getArgsForToolChain - Return the derived argument list for the tool chain TC (or the default tool cha...
Definition: Compilation.cpp:55
const ToolChain & getDefaultToolChain() const
Definition: Compilation.h:100
const ArgStringMap & getFailureResultFiles() const
Definition: Compilation.h:170
const ActionList & getActions() const
Definition: Compilation.h:150
ActionList & getActions()
Definition: Compilation.h:149
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * >> &FailingCommands) const
ExecuteJob - Execute a single job.
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:35
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
ExecuteCommand - Execute an actual command.
const llvm::opt::InputArgList & getInputArgs() const
Definition: Compilation.h:143
StringRef getSysRoot() const
Returns the sysroot path.
const char * addFailureResultFile(const char *Name, const JobAction *JA)
addFailureResultFile - Add a file to remove if we crash, and returns its argument.
Definition: Compilation.h:200
const llvm::opt::ArgStringList & getTempFiles() const
Definition: Compilation.h:166
std::pair< const_offload_toolchains_iterator, const_offload_toolchains_iterator > const_offload_toolchains_range
Definition: Compilation.h:112
const char * addTempFile(const char *Name)
addTempFile - Add a file to remove on exit, and returns its argument.
Definition: Compilation.h:186
const JobList & getJobs() const
Definition: Compilation.h:162
void initCompilationForDiagnostics()
initCompilationForDiagnostics - Remove stale state and suppress output so compilation can be reexecut...
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:48