clang  3.8.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 
19 namespace llvm {
20 namespace opt {
21  class DerivedArgList;
22  class InputArgList;
23 }
24 }
25 
26 namespace clang {
27 namespace driver {
28  class Driver;
29  class JobList;
30  class ToolChain;
31 
32 /// Compilation - A set of tasks to perform for a single driver
33 /// invocation.
34 class Compilation {
35  /// The driver we were created by.
36  const Driver &TheDriver;
37 
38  /// The default tool chain.
39  const ToolChain &DefaultToolChain;
40 
41  const ToolChain *CudaHostToolChain;
42  const ToolChain *CudaDeviceToolChain;
43 
44  /// The original (untranslated) input argument list.
45  llvm::opt::InputArgList *Args;
46 
47  /// The driver translated arguments. Note that toolchains may perform their
48  /// own argument translation.
49  llvm::opt::DerivedArgList *TranslatedArgs;
50 
51  /// The list of actions we've created via MakeAction. This is not accessible
52  /// to consumers; it's here just to manage ownership.
53  std::vector<std::unique_ptr<Action>> AllActions;
54 
55  /// The list of actions. This is maintained and modified by consumers, via
56  /// getActions().
57  ActionList Actions;
58 
59  /// The root list of jobs.
60  JobList Jobs;
61 
62  /// Cache of translated arguments for a particular tool chain and bound
63  /// architecture.
64  llvm::DenseMap<std::pair<const ToolChain *, const char *>,
65  llvm::opt::DerivedArgList *> TCArgs;
66 
67  /// Temporary files which should be removed on exit.
68  llvm::opt::ArgStringList TempFiles;
69 
70  /// Result files which should be removed on failure.
71  ArgStringMap ResultFiles;
72 
73  /// Result files which are generated correctly on failure, and which should
74  /// only be removed if we crash.
75  ArgStringMap FailureResultFiles;
76 
77  /// Redirection for stdout, stderr, etc.
78  const StringRef **Redirects;
79 
80  /// Whether we're compiling for diagnostic purposes.
81  bool ForDiagnostics;
82 
83 public:
84  Compilation(const Driver &D, const ToolChain &DefaultToolChain,
85  llvm::opt::InputArgList *Args,
86  llvm::opt::DerivedArgList *TranslatedArgs);
87  ~Compilation();
88 
89  const Driver &getDriver() const { return TheDriver; }
90 
91  const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
92  const ToolChain *getCudaHostToolChain() const { return CudaHostToolChain; }
94  return CudaDeviceToolChain;
95  }
96 
97  void setCudaHostToolChain(const ToolChain *HostToolChain) {
98  CudaHostToolChain = HostToolChain;
99  }
100  void setCudaDeviceToolChain(const ToolChain *DeviceToolChain) {
101  CudaDeviceToolChain = DeviceToolChain;
102  }
103 
104  const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
105 
106  const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
107 
108  llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
109 
110  ActionList &getActions() { return Actions; }
111  const ActionList &getActions() const { return Actions; }
112 
113  /// Creates a new Action owned by this Compilation.
114  ///
115  /// The new Action is *not* added to the list returned by getActions().
116  template <typename T, typename... Args> T *MakeAction(Args &&... Arg) {
117  T *RawPtr = new T(std::forward<Args>(Arg)...);
118  AllActions.push_back(std::unique_ptr<Action>(RawPtr));
119  return RawPtr;
120  }
121 
122  JobList &getJobs() { return Jobs; }
123  const JobList &getJobs() const { return Jobs; }
124 
125  void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
126 
127  const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
128 
129  const ArgStringMap &getResultFiles() const { return ResultFiles; }
130 
132  return FailureResultFiles;
133  }
134 
135  /// Returns the sysroot path.
136  StringRef getSysRoot() const;
137 
138  /// getArgsForToolChain - Return the derived argument list for the
139  /// tool chain \p TC (or the default tool chain, if TC is not specified).
140  ///
141  /// \param BoundArch - The bound architecture name, or 0.
142  const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
143  const char *BoundArch);
144 
145  /// addTempFile - Add a file to remove on exit, and returns its
146  /// argument.
147  const char *addTempFile(const char *Name) {
148  TempFiles.push_back(Name);
149  return Name;
150  }
151 
152  /// addResultFile - Add a file to remove on failure, and returns its
153  /// argument.
154  const char *addResultFile(const char *Name, const JobAction *JA) {
155  ResultFiles[JA] = Name;
156  return Name;
157  }
158 
159  /// addFailureResultFile - Add a file to remove if we crash, and returns its
160  /// argument.
161  const char *addFailureResultFile(const char *Name, const JobAction *JA) {
162  FailureResultFiles[JA] = Name;
163  return Name;
164  }
165 
166  /// CleanupFile - Delete a given file.
167  ///
168  /// \param IssueErrors - Report failures as errors.
169  /// \return Whether the file was removed successfully.
170  bool CleanupFile(const char *File, bool IssueErrors = false) const;
171 
172  /// CleanupFileList - Remove the files in the given list.
173  ///
174  /// \param IssueErrors - Report failures as errors.
175  /// \return Whether all files were removed successfully.
176  bool CleanupFileList(const llvm::opt::ArgStringList &Files,
177  bool IssueErrors = false) const;
178 
179  /// CleanupFileMap - Remove the files in the given map.
180  ///
181  /// \param JA - If specified, only delete the files associated with this
182  /// JobAction. Otherwise, delete all files in the map.
183  /// \param IssueErrors - Report failures as errors.
184  /// \return Whether all files were removed successfully.
185  bool CleanupFileMap(const ArgStringMap &Files,
186  const JobAction *JA,
187  bool IssueErrors = false) const;
188 
189  /// ExecuteCommand - Execute an actual command.
190  ///
191  /// \param FailingCommand - For non-zero results, this will be set to the
192  /// Command which failed, if any.
193  /// \return The result code of the subprocess.
194  int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
195 
196  /// ExecuteJob - Execute a single job.
197  ///
198  /// \param FailingCommands - For non-zero results, this will be a vector of
199  /// failing commands and their associated result code.
200  void ExecuteJobs(
201  const JobList &Jobs,
202  SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
203 
204  /// initCompilationForDiagnostics - Remove stale state and suppress output
205  /// so compilation can be reexecuted to generate additional diagnostic
206  /// information (e.g., preprocessed source(s)).
208 
209  /// Return true if we're compiling for diagnostics.
210  bool isForDiagnostics() const { return ForDiagnostics; }
211 };
212 
213 } // end namespace driver
214 } // end namespace clang
215 
216 #endif
const Driver & getDriver() const
Definition: Compilation.h:89
const ArgStringMap & getResultFiles() const
Definition: Compilation.h:129
void setCudaDeviceToolChain(const ToolChain *DeviceToolChain)
Definition: Compilation.h:100
T * MakeAction(Args &&...Arg)
Creates a new Action owned by this Compilation.
Definition: Compilation.h:116
bool CleanupFile(const char *File, bool IssueErrors=false) const
CleanupFile - Delete a given file.
Definition: Compilation.cpp:66
class LLVM_ALIGNAS(8) DependentTemplateSpecializationType const IdentifierInfo * Name
Represents a template specialization type whose template cannot be resolved, e.g. ...
Definition: Type.h:4381
void setCudaHostToolChain(const ToolChain *HostToolChain)
Definition: Compilation.h:97
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:106
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
const ToolChain * getCudaHostToolChain() const
Definition: Compilation.h:92
Compilation(const Driver &D, const ToolChain &DefaultToolChain, llvm::opt::InputArgList *Args, llvm::opt::DerivedArgList *TranslatedArgs)
Definition: Compilation.cpp:25
llvm::opt::DerivedArgList & getArgs()
Definition: Compilation.h:108
bool isForDiagnostics() const
Return true if we're compiling for diagnostics.
Definition: Compilation.h:210
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
CleanupFileList - Remove the files in the given list.
Definition: Compilation.cpp:96
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:125
JobList - A sequence of jobs to perform.
Definition: Job.h:142
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition: Job.h:157
llvm::DenseMap< const JobAction *, const char * > ArgStringMap
ArgStringMap - Type used to map a JobAction to its result file.
Definition: Util.h:21
bool CleanupFileMap(const ArgStringMap &Files, const JobAction *JA, bool IssueErrors=false) const
CleanupFileMap - Remove the files in the given map.
const ToolChain * getCudaDeviceToolChain() const
Definition: Compilation.h:93
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:154
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:51
const ToolChain & getDefaultToolChain() const
Definition: Compilation.h:91
const ArgStringMap & getFailureResultFiles() const
Definition: Compilation.h:131
const ActionList & getActions() const
Definition: Compilation.h:111
ActionList & getActions()
Definition: Compilation.h:110
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:34
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
ExecuteCommand - Execute an actual command.
const llvm::opt::InputArgList & getInputArgs() const
Definition: Compilation.h:104
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:161
const llvm::opt::ArgStringList & getTempFiles() const
Definition: Compilation.h:127
const char * addTempFile(const char *Name)
addTempFile - Add a file to remove on exit, and returns its argument.
Definition: Compilation.h:147
const JobList & getJobs() const
Definition: Compilation.h:123
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:47