Clang Project

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