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