Home | History | Annotate | Download | only in Driver
      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 CLANG_DRIVER_JOB_H_
     11 #define CLANG_DRIVER_JOB_H_
     12 
     13 #include "clang/Basic/LLVM.h"
     14 #include "llvm/ADT/SmallVector.h"
     15 #include "llvm/Option/Option.h"
     16 #include <memory>
     17 
     18 namespace llvm {
     19   class raw_ostream;
     20 }
     21 
     22 namespace clang {
     23 namespace driver {
     24 class Action;
     25 class Command;
     26 class Tool;
     27 
     28 // Re-export this as clang::driver::ArgStringList.
     29 using llvm::opt::ArgStringList;
     30 
     31 class Job {
     32 public:
     33   enum JobClass {
     34     CommandClass,
     35     FallbackCommandClass,
     36     JobListClass
     37   };
     38 
     39 private:
     40   JobClass Kind;
     41 
     42 protected:
     43   Job(JobClass _Kind) : Kind(_Kind) {}
     44 public:
     45   virtual ~Job();
     46 
     47   JobClass getKind() const { return Kind; }
     48 
     49   /// Print - Print this Job in -### format.
     50   ///
     51   /// \param OS - The stream to print on.
     52   /// \param Terminator - A string to print at the end of the line.
     53   /// \param Quote - Should separate arguments be quoted.
     54   /// \param CrashReport - Whether to print for inclusion in a crash report.
     55   virtual void Print(llvm::raw_ostream &OS, const char *Terminator,
     56                      bool Quote, bool CrashReport = false) const = 0;
     57 };
     58 
     59 /// Command - An executable path/name and argument vector to
     60 /// execute.
     61 class Command : public Job {
     62   /// Source - The action which caused the creation of this job.
     63   const Action &Source;
     64 
     65   /// Tool - The tool which caused the creation of this job.
     66   const Tool &Creator;
     67 
     68   /// The executable to run.
     69   const char *Executable;
     70 
     71   /// The list of program arguments (not including the implicit first
     72   /// argument, which will be the executable).
     73   llvm::opt::ArgStringList Arguments;
     74 
     75 public:
     76   Command(const Action &_Source, const Tool &_Creator, const char *_Executable,
     77           const llvm::opt::ArgStringList &_Arguments);
     78 
     79   void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
     80              bool CrashReport = false) const override;
     81 
     82   virtual int Execute(const StringRef **Redirects, std::string *ErrMsg,
     83                       bool *ExecutionFailed) const;
     84 
     85   /// getSource - Return the Action which caused the creation of this job.
     86   const Action &getSource() const { return Source; }
     87 
     88   /// getCreator - Return the Tool which caused the creation of this job.
     89   const Tool &getCreator() const { return Creator; }
     90 
     91   const char *getExecutable() const { return Executable; }
     92 
     93   const llvm::opt::ArgStringList &getArguments() const { return Arguments; }
     94 
     95   static bool classof(const Job *J) {
     96     return J->getKind() == CommandClass ||
     97            J->getKind() == FallbackCommandClass;
     98   }
     99 };
    100 
    101 /// Like Command, but with a fallback which is executed in case
    102 /// the primary command crashes.
    103 class FallbackCommand : public Command {
    104 public:
    105   FallbackCommand(const Action &Source_, const Tool &Creator_,
    106                   const char *Executable_, const ArgStringList &Arguments_,
    107                   Command *Fallback_);
    108 
    109   void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote,
    110              bool CrashReport = false) const override;
    111 
    112   int Execute(const StringRef **Redirects, std::string *ErrMsg,
    113               bool *ExecutionFailed) const override;
    114 
    115   static bool classof(const Job *J) {
    116     return J->getKind() == FallbackCommandClass;
    117   }
    118 
    119 private:
    120   std::unique_ptr<Command> Fallback;
    121 };
    122 
    123 /// JobList - A sequence of jobs to perform.
    124 class JobList : public Job {
    125 public:
    126   typedef SmallVector<Job*, 4> list_type;
    127   typedef list_type::size_type size_type;
    128   typedef list_type::iterator iterator;
    129   typedef list_type::const_iterator const_iterator;
    130 
    131 private:
    132   list_type Jobs;
    133 
    134 public:
    135   JobList();
    136   virtual ~JobList();
    137 
    138   void Print(llvm::raw_ostream &OS, const char *Terminator,
    139              bool Quote, bool CrashReport = false) const override;
    140 
    141   /// Add a job to the list (taking ownership).
    142   void addJob(Job *J) { Jobs.push_back(J); }
    143 
    144   /// Clear the job list.
    145   void clear();
    146 
    147   const list_type &getJobs() const { return Jobs; }
    148 
    149   size_type size() const { return Jobs.size(); }
    150   iterator begin() { return Jobs.begin(); }
    151   const_iterator begin() const { return Jobs.begin(); }
    152   iterator end() { return Jobs.end(); }
    153   const_iterator end() const { return Jobs.end(); }
    154 
    155   static bool classof(const Job *J) {
    156     return J->getKind() == JobListClass;
    157   }
    158 };
    159 
    160 } // end namespace driver
    161 } // end namespace clang
    162 
    163 #endif
    164