clang API Documentation
00001 //===--- Compilation.h - Compilation Task Data Structure --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_CLANG_DRIVER_COMPILATION_H 00011 #define LLVM_CLANG_DRIVER_COMPILATION_H 00012 00013 #include "clang/Driver/Job.h" 00014 #include "clang/Driver/Util.h" 00015 #include "llvm/ADT/DenseMap.h" 00016 #include "llvm/Support/Path.h" 00017 00018 namespace llvm { 00019 namespace opt { 00020 class DerivedArgList; 00021 class InputArgList; 00022 } 00023 } 00024 00025 namespace clang { 00026 namespace driver { 00027 class Driver; 00028 class JobAction; 00029 class JobList; 00030 class ToolChain; 00031 00032 /// Compilation - A set of tasks to perform for a single driver 00033 /// invocation. 00034 class Compilation { 00035 /// The driver we were created by. 00036 const Driver &TheDriver; 00037 00038 /// The default tool chain. 00039 const ToolChain &DefaultToolChain; 00040 00041 /// The original (untranslated) input argument list. 00042 llvm::opt::InputArgList *Args; 00043 00044 /// The driver translated arguments. Note that toolchains may perform their 00045 /// own argument translation. 00046 llvm::opt::DerivedArgList *TranslatedArgs; 00047 00048 /// The list of actions. 00049 ActionList Actions; 00050 00051 /// The root list of jobs. 00052 JobList Jobs; 00053 00054 /// Cache of translated arguments for a particular tool chain and bound 00055 /// architecture. 00056 llvm::DenseMap<std::pair<const ToolChain *, const char *>, 00057 llvm::opt::DerivedArgList *> TCArgs; 00058 00059 /// Temporary files which should be removed on exit. 00060 llvm::opt::ArgStringList TempFiles; 00061 00062 /// Result files which should be removed on failure. 00063 ArgStringMap ResultFiles; 00064 00065 /// Result files which are generated correctly on failure, and which should 00066 /// only be removed if we crash. 00067 ArgStringMap FailureResultFiles; 00068 00069 /// Redirection for stdout, stderr, etc. 00070 const StringRef **Redirects; 00071 00072 /// Whether we're compiling for diagnostic purposes. 00073 bool ForDiagnostics; 00074 00075 public: 00076 Compilation(const Driver &D, const ToolChain &DefaultToolChain, 00077 llvm::opt::InputArgList *Args, 00078 llvm::opt::DerivedArgList *TranslatedArgs); 00079 ~Compilation(); 00080 00081 const Driver &getDriver() const { return TheDriver; } 00082 00083 const ToolChain &getDefaultToolChain() const { return DefaultToolChain; } 00084 00085 const llvm::opt::InputArgList &getInputArgs() const { return *Args; } 00086 00087 const llvm::opt::DerivedArgList &getArgs() const { return *TranslatedArgs; } 00088 00089 llvm::opt::DerivedArgList &getArgs() { return *TranslatedArgs; } 00090 00091 ActionList &getActions() { return Actions; } 00092 const ActionList &getActions() const { return Actions; } 00093 00094 JobList &getJobs() { return Jobs; } 00095 const JobList &getJobs() const { return Jobs; } 00096 00097 void addCommand(std::unique_ptr<Command> C) { Jobs.addJob(std::move(C)); } 00098 00099 const llvm::opt::ArgStringList &getTempFiles() const { return TempFiles; } 00100 00101 const ArgStringMap &getResultFiles() const { return ResultFiles; } 00102 00103 const ArgStringMap &getFailureResultFiles() const { 00104 return FailureResultFiles; 00105 } 00106 00107 /// Returns the sysroot path. 00108 StringRef getSysRoot() const; 00109 00110 /// getArgsForToolChain - Return the derived argument list for the 00111 /// tool chain \p TC (or the default tool chain, if TC is not specified). 00112 /// 00113 /// \param BoundArch - The bound architecture name, or 0. 00114 const llvm::opt::DerivedArgList &getArgsForToolChain(const ToolChain *TC, 00115 const char *BoundArch); 00116 00117 /// addTempFile - Add a file to remove on exit, and returns its 00118 /// argument. 00119 const char *addTempFile(const char *Name) { 00120 TempFiles.push_back(Name); 00121 return Name; 00122 } 00123 00124 /// addResultFile - Add a file to remove on failure, and returns its 00125 /// argument. 00126 const char *addResultFile(const char *Name, const JobAction *JA) { 00127 ResultFiles[JA] = Name; 00128 return Name; 00129 } 00130 00131 /// addFailureResultFile - Add a file to remove if we crash, and returns its 00132 /// argument. 00133 const char *addFailureResultFile(const char *Name, const JobAction *JA) { 00134 FailureResultFiles[JA] = Name; 00135 return Name; 00136 } 00137 00138 /// CleanupFile - Delete a given file. 00139 /// 00140 /// \param IssueErrors - Report failures as errors. 00141 /// \return Whether the file was removed successfully. 00142 bool CleanupFile(const char *File, bool IssueErrors = false) const; 00143 00144 /// CleanupFileList - Remove the files in the given list. 00145 /// 00146 /// \param IssueErrors - Report failures as errors. 00147 /// \return Whether all files were removed successfully. 00148 bool CleanupFileList(const llvm::opt::ArgStringList &Files, 00149 bool IssueErrors = false) const; 00150 00151 /// CleanupFileMap - Remove the files in the given map. 00152 /// 00153 /// \param JA - If specified, only delete the files associated with this 00154 /// JobAction. Otherwise, delete all files in the map. 00155 /// \param IssueErrors - Report failures as errors. 00156 /// \return Whether all files were removed successfully. 00157 bool CleanupFileMap(const ArgStringMap &Files, 00158 const JobAction *JA, 00159 bool IssueErrors = false) const; 00160 00161 /// ExecuteCommand - Execute an actual command. 00162 /// 00163 /// \param FailingCommand - For non-zero results, this will be set to the 00164 /// Command which failed, if any. 00165 /// \return The result code of the subprocess. 00166 int ExecuteCommand(const Command &C, const Command *&FailingCommand) const; 00167 00168 /// ExecuteJob - Execute a single job. 00169 /// 00170 /// \param FailingCommands - For non-zero results, this will be a vector of 00171 /// failing commands and their associated result code. 00172 void ExecuteJob(const Job &J, 00173 SmallVectorImpl< std::pair<int, const Command *> > &FailingCommands) const; 00174 00175 /// initCompilationForDiagnostics - Remove stale state and suppress output 00176 /// so compilation can be reexecuted to generate additional diagnostic 00177 /// information (e.g., preprocessed source(s)). 00178 void initCompilationForDiagnostics(); 00179 00180 /// Return true if we're compiling for diagnostics. 00181 bool isForDiagnostics() { return ForDiagnostics; } 00182 }; 00183 00184 } // end namespace driver 00185 } // end namespace clang 00186 00187 #endif