]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Driver/Compilation.h
Merge libxo 0.4.6
[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
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; }
93   const ToolChain *getCudaDeviceToolChain() const {
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
131   const ArgStringMap &getFailureResultFiles() const {
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)).
207   void initCompilationForDiagnostics();
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