• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Job.h"
14 #include "clang/Driver/Util.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/Support/Path.h"
17 
18 namespace llvm {
19 namespace opt {
20   class DerivedArgList;
21   class InputArgList;
22 }
23 }
24 
25 namespace clang {
26 namespace driver {
27   class Driver;
28   class JobAction;
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.
52   ActionList Actions;
53 
54   /// The root list of jobs.
55   JobList Jobs;
56 
57   /// Cache of translated arguments for a particular tool chain and bound
58   /// architecture.
59   llvm::DenseMap<std::pair<const ToolChain *, const char *>,
60                  llvm::opt::DerivedArgList *> TCArgs;
61 
62   /// Temporary files which should be removed on exit.
63   llvm::opt::ArgStringList TempFiles;
64 
65   /// Result files which should be removed on failure.
66   ArgStringMap ResultFiles;
67 
68   /// Result files which are generated correctly on failure, and which should
69   /// only be removed if we crash.
70   ArgStringMap FailureResultFiles;
71 
72   /// Redirection for stdout, stderr, etc.
73   const StringRef **Redirects;
74 
75   /// Whether we're compiling for diagnostic purposes.
76   bool ForDiagnostics;
77 
78 public:
79   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
80               llvm::opt::InputArgList *Args,
81               llvm::opt::DerivedArgList *TranslatedArgs);
82   ~Compilation();
83 
getDriver()84   const Driver &getDriver() const { return TheDriver; }
85 
getDefaultToolChain()86   const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
getCudaHostToolChain()87   const ToolChain *getCudaHostToolChain() const { return CudaHostToolChain; }
getCudaDeviceToolChain()88   const ToolChain *getCudaDeviceToolChain() const {
89     return CudaDeviceToolChain;
90   }
91 
setCudaHostToolChain(const ToolChain * HostToolChain)92   void setCudaHostToolChain(const ToolChain *HostToolChain) {
93     CudaHostToolChain = HostToolChain;
94   }
setCudaDeviceToolChain(const ToolChain * DeviceToolChain)95   void setCudaDeviceToolChain(const ToolChain *DeviceToolChain) {
96     CudaDeviceToolChain = DeviceToolChain;
97   }
98 
getInputArgs()99   const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
100 
getArgs()101   const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
102 
getArgs()103   llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
104 
getActions()105   ActionList &getActions() { return Actions; }
getActions()106   const ActionList &getActions() const { return Actions; }
107 
getJobs()108   JobList &getJobs() { return Jobs; }
getJobs()109   const JobList &getJobs() const { return Jobs; }
110 
addCommand(std::unique_ptr<Command> C)111   void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
112 
getTempFiles()113   const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
114 
getResultFiles()115   const ArgStringMap &getResultFiles() const { return ResultFiles; }
116 
getFailureResultFiles()117   const ArgStringMap &getFailureResultFiles() const {
118     return FailureResultFiles;
119   }
120 
121   /// Returns the sysroot path.
122   StringRef getSysRoot() const;
123 
124   /// getArgsForToolChain - Return the derived argument list for the
125   /// tool chain \p TC (or the default tool chain, if TC is not specified).
126   ///
127   /// \param BoundArch - The bound architecture name, or 0.
128   const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
129                                                        const char *BoundArch);
130 
131   /// addTempFile - Add a file to remove on exit, and returns its
132   /// argument.
addTempFile(const char * Name)133   const char *addTempFile(const char *Name) {
134     TempFiles.push_back(Name);
135     return Name;
136   }
137 
138   /// addResultFile - Add a file to remove on failure, and returns its
139   /// argument.
addResultFile(const char * Name,const JobAction * JA)140   const char *addResultFile(const char *Name, const JobAction *JA) {
141     ResultFiles[JA] = Name;
142     return Name;
143   }
144 
145   /// addFailureResultFile - Add a file to remove if we crash, and returns its
146   /// argument.
addFailureResultFile(const char * Name,const JobAction * JA)147   const char *addFailureResultFile(const char *Name, const JobAction *JA) {
148     FailureResultFiles[JA] = Name;
149     return Name;
150   }
151 
152   /// CleanupFile - Delete a given file.
153   ///
154   /// \param IssueErrors - Report failures as errors.
155   /// \return Whether the file was removed successfully.
156   bool CleanupFile(const char *File, bool IssueErrors = false) const;
157 
158   /// CleanupFileList - Remove the files in the given list.
159   ///
160   /// \param IssueErrors - Report failures as errors.
161   /// \return Whether all files were removed successfully.
162   bool CleanupFileList(const llvm::opt::ArgStringList &Files,
163                        bool IssueErrors = false) const;
164 
165   /// CleanupFileMap - Remove the files in the given map.
166   ///
167   /// \param JA - If specified, only delete the files associated with this
168   /// JobAction.  Otherwise, delete all files in the map.
169   /// \param IssueErrors - Report failures as errors.
170   /// \return Whether all files were removed successfully.
171   bool CleanupFileMap(const ArgStringMap &Files,
172                       const JobAction *JA,
173                       bool IssueErrors = false) const;
174 
175   /// ExecuteCommand - Execute an actual command.
176   ///
177   /// \param FailingCommand - For non-zero results, this will be set to the
178   /// Command which failed, if any.
179   /// \return The result code of the subprocess.
180   int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
181 
182   /// ExecuteJob - Execute a single job.
183   ///
184   /// \param FailingCommands - For non-zero results, this will be a vector of
185   /// failing commands and their associated result code.
186   void ExecuteJobs(
187       const JobList &Jobs,
188       SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
189 
190   /// initCompilationForDiagnostics - Remove stale state and suppress output
191   /// so compilation can be reexecuted to generate additional diagnostic
192   /// information (e.g., preprocessed source(s)).
193   void initCompilationForDiagnostics();
194 
195   /// Return true if we're compiling for diagnostics.
isForDiagnostics()196   bool isForDiagnostics() const { return ForDiagnostics; }
197 };
198 
199 } // end namespace driver
200 } // end namespace clang
201 
202 #endif
203