clang  3.7.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/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 
81  const Driver &getDriver() const { return TheDriver; }
82 
83  const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
84 
85  const llvm::opt::InputArgList &getInputArgs() const { return *Args; }
86 
87  const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; }
88 
89  llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; }
90 
91  ActionList &getActions() { return Actions; }
92  const ActionList &getActions() const { return Actions; }
93 
94  JobList &getJobs() { return Jobs; }
95  const JobList &getJobs() const { return Jobs; }
96 
97  void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); }
98 
99  const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; }
100 
101  const ArgStringMap &getResultFiles() const { return ResultFiles; }
102 
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.
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.
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.
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 ExecuteJobs(
173  const JobList &Jobs,
174  SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) const;
175 
176  /// initCompilationForDiagnostics - Remove stale state and suppress output
177  /// so compilation can be reexecuted to generate additional diagnostic
178  /// information (e.g., preprocessed source(s)).
180 
181  /// Return true if we're compiling for diagnostics.
182  bool isForDiagnostics() { return ForDiagnostics; }
183 };
184 
185 } // end namespace driver
186 } // end namespace clang
187 
188 #endif
const Driver & getDriver() const
Definition: Compilation.h:81
const ArgStringMap & getResultFiles() const
Definition: Compilation.h:101
bool CleanupFile(const char *File, bool IssueErrors=false) const
Definition: Compilation.cpp:70
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:87
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:89
bool CleanupFileList(const llvm::opt::ArgStringList &Files, bool IssueErrors=false) const
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:97
JobList - A sequence of jobs to perform.
Definition: Job.h:133
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition: Job.h:148
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
const char * addResultFile(const char *Name, const JobAction *JA)
Definition: Compilation.h:126
const llvm::opt::DerivedArgList & getArgsForToolChain(const ToolChain *TC, const char *BoundArch)
Definition: Compilation.cpp:55
const ToolChain & getDefaultToolChain() const
Definition: Compilation.h:83
const ArgStringMap & getFailureResultFiles() const
Definition: Compilation.h:103
const ActionList & getActions() const
Definition: Compilation.h:92
ActionList & getActions()
Definition: Compilation.h:91
void ExecuteJobs(const JobList &Jobs, SmallVectorImpl< std::pair< int, const Command * >> &FailingCommands) const
int ExecuteCommand(const Command &C, const Command *&FailingCommand) const
const llvm::opt::InputArgList & getInputArgs() const
Definition: Compilation.h:85
StringRef getSysRoot() const
Returns the sysroot path.
const char * addFailureResultFile(const char *Name, const JobAction *JA)
Definition: Compilation.h:133
const llvm::opt::ArgStringList & getTempFiles() const
Definition: Compilation.h:99
bool isForDiagnostics()
Return true if we're compiling for diagnostics.
Definition: Compilation.h:182
const char * addTempFile(const char *Name)
Definition: Compilation.h:119
const JobList & getJobs() const
Definition: Compilation.h:95
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:43