Compilation.h revision b1e25a1bc03292dc538d336573e0be1490223171
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#include "llvm/Support/PathV1.h"
18
19namespace llvm {
20namespace opt {
21  class DerivedArgList;
22  class InputArgList;
23}
24}
25
26namespace clang {
27namespace driver {
28  // FIXME: Remove this using directive and qualify class usage below.
29  using namespace llvm::opt;
30
31  class Driver;
32  class JobAction;
33  class JobList;
34  class ToolChain;
35
36/// Compilation - A set of tasks to perform for a single driver
37/// invocation.
38class Compilation {
39  /// The driver we were created by.
40  const Driver &TheDriver;
41
42  /// The default tool chain.
43  const ToolChain &DefaultToolChain;
44
45  /// The original (untranslated) input argument list.
46  InputArgList *Args;
47
48  /// The driver translated arguments. Note that toolchains may perform their
49  /// own argument translation.
50  DerivedArgList *TranslatedArgs;
51
52  /// The list of actions.
53  ActionList Actions;
54
55  /// The root list of jobs.
56  JobList Jobs;
57
58  /// Cache of translated arguments for a particular tool chain and bound
59  /// architecture.
60  llvm::DenseMap<std::pair<const ToolChain*, const char*>,
61                 DerivedArgList*> TCArgs;
62
63  /// Temporary files which should be removed on exit.
64  ArgStringList TempFiles;
65
66  /// Result files which should be removed on failure.
67  ArgStringMap ResultFiles;
68
69  /// Result files which are generated correctly on failure, and which should
70  /// only be removed if we crash.
71  ArgStringMap FailureResultFiles;
72
73  /// Redirection for stdout, stderr, etc.
74  const StringRef **Redirects;
75
76public:
77  Compilation(const Driver &D, const ToolChain &DefaultToolChain,
78              InputArgList *Args, DerivedArgList *TranslatedArgs);
79  ~Compilation();
80
81  const Driver &getDriver() const { return TheDriver; }
82
83  const ToolChain &getDefaultToolChain() const { return DefaultToolChain; }
84
85  const InputArgList &getInputArgs() const { return *Args; }
86
87  const DerivedArgList &getArgs() const { return *TranslatedArgs; }
88
89  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(Command *C) { Jobs.addJob(C); }
98
99  const ArgStringList &getTempFiles() const { return TempFiles; }
100
101  const ArgStringMap &getResultFiles() const { return ResultFiles; }
102
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 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 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  /// PrintJob - Print one job in -### format.
162  ///
163  /// \param OS - The stream to print on.
164  /// \param J - The job to print.
165  /// \param Terminator - A string to print at the end of the line.
166  /// \param Quote - Should separate arguments be quoted.
167  void PrintJob(raw_ostream &OS, const Job &J,
168                const char *Terminator, bool Quote) const;
169
170  /// PrintDiagnosticJob - Print one job in -### format, but with the
171  /// superfluous options removed, which are not necessary for
172  /// reproducing the crash.
173  ///
174  /// \param OS - The stream to print on.
175  /// \param J - The job to print.
176  void PrintDiagnosticJob(raw_ostream &OS, const Job &J) const;
177
178  /// ExecuteCommand - Execute an actual command.
179  ///
180  /// \param FailingCommand - For non-zero results, this will be set to the
181  /// Command which failed, if any.
182  /// \return The result code of the subprocess.
183  int ExecuteCommand(const Command &C, const Command *&FailingCommand) const;
184
185  /// ExecuteJob - Execute a single job.
186  ///
187  /// \param FailingCommands - For non-zero results, this will be a vector of
188  /// failing commands and their associated result code.
189  void ExecuteJob(const Job &J,
190     SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) const;
191
192  /// initCompilationForDiagnostics - Remove stale state and suppress output
193  /// so compilation can be reexecuted to generate additional diagnostic
194  /// information (e.g., preprocessed source(s)).
195  void initCompilationForDiagnostics();
196};
197
198} // end namespace driver
199} // end namespace clang
200
201#endif
202