clang  7.0.0svn
Job.h
Go to the documentation of this file.
1 //===- Job.h - Commands to Execute ------------------------------*- 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_JOB_H
11 #define LLVM_CLANG_DRIVER_JOB_H
12 
13 #include "clang/Basic/LLVM.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/iterator.h"
19 #include "llvm/Option/Option.h"
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 namespace clang {
26 namespace driver {
27 
28 class Action;
29 class InputInfo;
30 class Tool;
31 
32 // Re-export this as clang::driver::ArgStringList.
33 using llvm::opt::ArgStringList;
34 
36  StringRef Filename;
37  StringRef VFSPath;
38 
39  CrashReportInfo(StringRef Filename, StringRef VFSPath)
40  : Filename(Filename), VFSPath(VFSPath) {}
41 };
42 
43 /// Command - An executable path/name and argument vector to
44 /// execute.
45 class Command {
46  /// Source - The action which caused the creation of this job.
47  const Action &Source;
48 
49  /// Tool - The tool which caused the creation of this job.
50  const Tool &Creator;
51 
52  /// The executable to run.
53  const char *Executable;
54 
55  /// The list of program arguments (not including the implicit first
56  /// argument, which will be the executable).
57  llvm::opt::ArgStringList Arguments;
58 
59  /// The list of program arguments which are inputs.
60  llvm::opt::ArgStringList InputFilenames;
61 
62  /// Response file name, if this command is set to use one, or nullptr
63  /// otherwise
64  const char *ResponseFile = nullptr;
65 
66  /// The input file list in case we need to emit a file list instead of a
67  /// proper response file
68  llvm::opt::ArgStringList InputFileList;
69 
70  /// String storage if we need to create a new argument to specify a response
71  /// file
72  std::string ResponseFileFlag;
73 
74  /// See Command::setEnvironment
75  std::vector<const char *> Environment;
76 
77  /// When a response file is needed, we try to put most arguments in an
78  /// exclusive file, while others remains as regular command line arguments.
79  /// This functions fills a vector with the regular command line arguments,
80  /// argv, excluding the ones passed in a response file.
81  void buildArgvForResponseFile(llvm::SmallVectorImpl<const char *> &Out) const;
82 
83  /// Encodes an array of C strings into a single string separated by whitespace.
84  /// This function will also put in quotes arguments that have whitespaces and
85  /// will escape the regular backslashes (used in Windows paths) and quotes.
86  /// The results are the contents of a response file, written into a raw_ostream.
87  void writeResponseFile(raw_ostream &OS) const;
88 
89 public:
90  Command(const Action &Source, const Tool &Creator, const char *Executable,
91  const llvm::opt::ArgStringList &Arguments,
92  ArrayRef<InputInfo> Inputs);
93  // FIXME: This really shouldn't be copyable, but is currently copied in some
94  // error handling in Driver::generateCompilationDiagnostics.
95  Command(const Command &) = default;
96  virtual ~Command() = default;
97 
98  virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
99  CrashReportInfo *CrashInfo = nullptr) const;
100 
101  virtual int Execute(ArrayRef<Optional<StringRef>> Redirects,
102  std::string *ErrMsg, bool *ExecutionFailed) const;
103 
104  /// getSource - Return the Action which caused the creation of this job.
105  const Action &getSource() const { return Source; }
106 
107  /// getCreator - Return the Tool which caused the creation of this job.
108  const Tool &getCreator() const { return Creator; }
109 
110  /// Set to pass arguments via a response file when launching the command
111  void setResponseFile(const char *FileName);
112 
113  /// Set an input file list, necessary if we need to use a response file but
114  /// the tool being called only supports input files lists.
115  void setInputFileList(llvm::opt::ArgStringList List) {
116  InputFileList = std::move(List);
117  }
118 
119  /// Sets the environment to be used by the new process.
120  /// \param NewEnvironment An array of environment variables.
121  /// \remark If the environment remains unset, then the environment
122  /// from the parent process will be used.
123  void setEnvironment(llvm::ArrayRef<const char *> NewEnvironment);
124 
125  const char *getExecutable() const { return Executable; }
126 
127  const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
128 
129  /// Print a command argument, and optionally quote it.
130  static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote);
131 };
132 
133 /// Like Command, but with a fallback which is executed in case
134 /// the primary command crashes.
135 class FallbackCommand : public Command {
136 public:
137  FallbackCommand(const Action &Source_, const Tool &Creator_,
138  const char *Executable_, const ArgStringList &Arguments_,
139  ArrayRef<InputInfo> Inputs,
140  std::unique_ptr<Command> Fallback_);
141 
142  void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
143  CrashReportInfo *CrashInfo = nullptr) const override;
144 
145  int Execute(ArrayRef<Optional<StringRef>> Redirects, std::string *ErrMsg,
146  bool *ExecutionFailed) const override;
147 
148 private:
149  std::unique_ptr<Command> Fallback;
150 };
151 
152 /// Like Command, but always pretends that the wrapped command succeeded.
153 class ForceSuccessCommand : public Command {
154 public:
155  ForceSuccessCommand(const Action &Source_, const Tool &Creator_,
156  const char *Executable_, const ArgStringList &Arguments_,
157  ArrayRef<InputInfo> Inputs);
158 
159  void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
160  CrashReportInfo *CrashInfo = nullptr) const override;
161 
162  int Execute(ArrayRef<Optional<StringRef>> Redirects, std::string *ErrMsg,
163  bool *ExecutionFailed) const override;
164 };
165 
166 /// JobList - A sequence of jobs to perform.
167 class JobList {
168 public:
170  using size_type = list_type::size_type;
171  using iterator = llvm::pointee_iterator<list_type::iterator>;
172  using const_iterator = llvm::pointee_iterator<list_type::const_iterator>;
173 
174 private:
175  list_type Jobs;
176 
177 public:
178  void Print(llvm::raw_ostream &OS, const char *Terminator,
179  bool Quote, CrashReportInfo *CrashInfo = nullptr) const;
180 
181  /// Add a job to the list (taking ownership).
182  void addJob(std::unique_ptr<Command> J) { Jobs.push_back(std::move(J)); }
183 
184  /// Clear the job list.
185  void clear();
186 
187  const list_type &getJobs() const { return Jobs; }
188 
189  bool empty() const { return Jobs.empty(); }
190  size_type size() const { return Jobs.size(); }
191  iterator begin() { return Jobs.begin(); }
192  const_iterator begin() const { return Jobs.begin(); }
193  iterator end() { return Jobs.end(); }
194  const_iterator end() const { return Jobs.end(); }
195 };
196 
197 } // namespace driver
198 } // namespace clang
199 
200 #endif // LLVM_CLANG_DRIVER_JOB_H
iterator end()
Definition: Job.h:193
Like Command, but with a fallback which is executed in case the primary command crashes.
Definition: Job.h:135
virtual void clear()
llvm::pointee_iterator< list_type::const_iterator > const_iterator
Definition: Job.h:172
const_iterator begin() const
Definition: Job.h:192
Action - Represent an abstract compilation step to perform.
Definition: Action.h:48
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
list_type::size_type size_type
Definition: Job.h:170
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:108
JobList - A sequence of jobs to perform.
Definition: Job.h:167
void addJob(std::unique_ptr< Command > J)
Add a job to the list (taking ownership).
Definition: Job.h:182
const_iterator end() const
Definition: Job.h:194
Command - An executable path/name and argument vector to execute.
Definition: Job.h:45
iterator begin()
Definition: Job.h:191
const Action & getSource() const
getSource - Return the Action which caused the creation of this job.
Definition: Job.h:105
Dataflow Directional Tag Classes.
const char * getExecutable() const
Definition: Job.h:125
const llvm::opt::ArgStringList & getArguments() const
Definition: Job.h:127
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
size_type size() const
Definition: Job.h:190
Like Command, but always pretends that the wrapped command succeeded.
Definition: Job.h:153
void setInputFileList(llvm::opt::ArgStringList List)
Set an input file list, necessary if we need to use a response file but the tool being called only su...
Definition: Job.h:115
llvm::pointee_iterator< list_type::iterator > iterator
Definition: Job.h:171
CrashReportInfo(StringRef Filename, StringRef VFSPath)
Definition: Job.h:39
const list_type & getJobs() const
Definition: Job.h:187
bool empty() const
Definition: Job.h:189