• 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 CLANG_DRIVER_COMPILATION_H_
11 #define 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 public:
73   Compilation(const Driver &D, const ToolChain &DefaultToolChain,
74               llvm::opt::InputArgList *Args,
75               llvm::opt::DerivedArgList *TranslatedArgs);
76   ~Compilation();
77 
getDriver()78   const Driver &getDriver() const { return TheDriver; }
79 
getDefaultToolChain()80   const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
81 
getInputArgs()82   const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
83 
getArgs()84   const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
85 
getArgs()86   llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
87 
getActions()88   ActionList &getActions() { return Actions; }
getActions()89   const ActionList &getActions() const { return Actions; }
90 
getJobs()91   JobList &getJobs() { return Jobs; }
getJobs()92   const JobList &getJobs() const { return Jobs; }
93 
addCommand(Command * C)94   void addCommand(Command *C) { Jobs.addJob(C); }
95 
getTempFiles()96   const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
97 
getResultFiles()98   const ArgStringMap &getResultFiles() const { return ResultFiles; }
99 
getFailureResultFiles()100   const ArgStringMap &getFailureResultFiles() const {
101     return FailureResultFiles;
102   }
103 
104   /// Returns the sysroot path.
105   StringRef getSysRoot() const;
106 
107   /// getArgsForToolChain - Return the derived argument list for the
108   /// tool chain \p TC (or the default tool chain, if TC is not specified).
109   ///
110   /// \param BoundArch - The bound architecture name, or 0.
111   const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC,
112                                                        const char *BoundArch);
113 
114   /// addTempFile - Add a file to remove on exit, and returns its
115   /// argument.
addTempFile(const char * Name)116   const char *addTempFile(const char *Name) {
117     TempFiles.push_back(Name);
118     return Name;
119   }
120 
121   /// addResultFile - Add a file to remove on failure, and returns its
122   /// argument.
addResultFile(const char * Name,const JobAction * JA)123   const char *addResultFile(const char *Name, const JobAction *JA) {
124     ResultFiles[JA] = Name;
125     return Name;
126   }
127 
128   /// addFailureResultFile - Add a file to remove if we crash, and returns its
129   /// argument.
addFailureResultFile(const char * Name,const JobAction * JA)130   const char *addFailureResultFile(const char *Name, const JobAction *JA) {
131     FailureResultFiles[JA] = Name;
132     return Name;
133   }
134 
135   /// CleanupFile - Delete a given file.
136   ///
137   /// \param IssueErrors - Report failures as errors.
138   /// \return Whether the file was removed successfully.
139   bool CleanupFile(const char *File, bool IssueErrors = false) const;
140 
141   /// CleanupFileList - Remove the files in the given list.
142   ///
143   /// \param IssueErrors - Report failures as errors.
144   /// \return Whether all files were removed successfully.
145   bool CleanupFileList(const llvm::opt::ArgStringList &Files,
146                        bool IssueErrors = false) const;
147 
148   /// CleanupFileMap - Remove the files in the given map.
149   ///
150   /// \param JA - If specified, only delete the files associated with this
151   /// JobAction.  Otherwise, delete all files in the map.
152   /// \param IssueErrors - Report failures as errors.
153   /// \return Whether all files were removed successfully.
154   bool CleanupFileMap(const ArgStringMap &Files,
155                       const JobAction *JA,
156                       bool IssueErrors = false) const;
157 
158   /// PrintJob - Print one job in -### format.
159   ///
160   /// \param OS - The stream to print on.
161   /// \param J - The job to print.
162   /// \param Terminator - A string to print at the end of the line.
163   /// \param Quote - Should separate arguments be quoted.
164   void PrintJob(raw_ostream &OS, const Job &J,
165                 const char *Terminator, bool Quote) const;
166 
167   /// PrintDiagnosticJob - Print one job in -### format, but with the
168   /// superfluous options removed, which are not necessary for
169   /// reproducing the crash.
170   ///
171   /// \param OS - The stream to print on.
172   /// \param J - The job to print.
173   void PrintDiagnosticJob(raw_ostream &OS, const Job &J) 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 ExecuteJob(const Job &J,
187      SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) const;
188 
189   /// initCompilationForDiagnostics - Remove stale state and suppress output
190   /// so compilation can be reexecuted to generate additional diagnostic
191   /// information (e.g., preprocessed source(s)).
192   void initCompilationForDiagnostics();
193 };
194 
195 } // end namespace driver
196 } // end namespace clang
197 
198 #endif
199