Compilation.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //===--- Compilation.cpp - Compilation Task Implementation ----------------===//
  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. #include "clang/Driver/Compilation.h"
  10. #include "clang/Driver/Action.h"
  11. #include "clang/Driver/Driver.h"
  12. #include "clang/Driver/DriverDiagnostic.h"
  13. #include "clang/Driver/Options.h"
  14. #include "clang/Driver/ToolChain.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/Option/ArgList.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. #include <errno.h>
  20. #include <sys/stat.h>
  21. using namespace clang::driver;
  22. using namespace clang;
  23. using namespace llvm::opt;
  24. Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
  25. InputArgList *_Args, DerivedArgList *_TranslatedArgs)
  26. : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
  27. TranslatedArgs(_TranslatedArgs), Redirects(nullptr) {
  28. }
  29. Compilation::~Compilation() {
  30. delete TranslatedArgs;
  31. delete Args;
  32. // Free any derived arg lists.
  33. for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
  34. DerivedArgList*>::iterator it = TCArgs.begin(),
  35. ie = TCArgs.end(); it != ie; ++it)
  36. if (it->second != TranslatedArgs)
  37. delete it->second;
  38. // Free the actions, if built.
  39. for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
  40. it != ie; ++it)
  41. delete *it;
  42. // Free redirections of stdout/stderr.
  43. if (Redirects) {
  44. delete Redirects[1];
  45. delete Redirects[2];
  46. delete [] Redirects;
  47. }
  48. }
  49. const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
  50. const char *BoundArch) {
  51. if (!TC)
  52. TC = &DefaultToolChain;
  53. DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
  54. if (!Entry) {
  55. Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
  56. if (!Entry)
  57. Entry = TranslatedArgs;
  58. }
  59. return *Entry;
  60. }
  61. bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
  62. // FIXME: Why are we trying to remove files that we have not created? For
  63. // example we should only try to remove a temporary assembly file if
  64. // "clang -cc1" succeed in writing it. Was this a workaround for when
  65. // clang was writing directly to a .s file and sometimes leaving it behind
  66. // during a failure?
  67. // FIXME: If this is necessary, we can still try to split
  68. // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
  69. // duplicated stat from is_regular_file.
  70. // Don't try to remove files which we don't have write access to (but may be
  71. // able to remove), or non-regular files. Underlying tools may have
  72. // intentionally not overwritten them.
  73. if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
  74. return true;
  75. if (llvm::error_code EC = llvm::sys::fs::remove(File)) {
  76. // Failure is only failure if the file exists and is "regular". We checked
  77. // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
  78. // so we don't need to check again.
  79. if (IssueErrors)
  80. getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
  81. << EC.message();
  82. return false;
  83. }
  84. return true;
  85. }
  86. bool Compilation::CleanupFileList(const ArgStringList &Files,
  87. bool IssueErrors) const {
  88. bool Success = true;
  89. for (ArgStringList::const_iterator
  90. it = Files.begin(), ie = Files.end(); it != ie; ++it)
  91. Success &= CleanupFile(*it, IssueErrors);
  92. return Success;
  93. }
  94. bool Compilation::CleanupFileMap(const ArgStringMap &Files,
  95. const JobAction *JA,
  96. bool IssueErrors) const {
  97. bool Success = true;
  98. for (ArgStringMap::const_iterator
  99. it = Files.begin(), ie = Files.end(); it != ie; ++it) {
  100. // If specified, only delete the files associated with the JobAction.
  101. // Otherwise, delete all files in the map.
  102. if (JA && it->first != JA)
  103. continue;
  104. Success &= CleanupFile(it->second, IssueErrors);
  105. }
  106. return Success;
  107. }
  108. int Compilation::ExecuteCommand(const Command &C,
  109. const Command *&FailingCommand) const {
  110. if ((getDriver().CCPrintOptions ||
  111. getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
  112. raw_ostream *OS = &llvm::errs();
  113. // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
  114. // output stream.
  115. if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
  116. std::string Error;
  117. OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename, Error,
  118. llvm::sys::fs::F_Append |
  119. llvm::sys::fs::F_Text);
  120. if (!Error.empty()) {
  121. getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
  122. << Error;
  123. FailingCommand = &C;
  124. delete OS;
  125. return 1;
  126. }
  127. }
  128. if (getDriver().CCPrintOptions)
  129. *OS << "[Logging clang options]";
  130. C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
  131. if (OS != &llvm::errs())
  132. delete OS;
  133. }
  134. std::string Error;
  135. bool ExecutionFailed;
  136. int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
  137. if (!Error.empty()) {
  138. assert(Res && "Error string set with 0 result code!");
  139. getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
  140. }
  141. if (Res)
  142. FailingCommand = &C;
  143. return ExecutionFailed ? 1 : Res;
  144. }
  145. typedef SmallVectorImpl< std::pair<int, const Command *> > FailingCommandList;
  146. static bool ActionFailed(const Action *A,
  147. const FailingCommandList &FailingCommands) {
  148. if (FailingCommands.empty())
  149. return false;
  150. for (FailingCommandList::const_iterator CI = FailingCommands.begin(),
  151. CE = FailingCommands.end(); CI != CE; ++CI)
  152. if (A == &(CI->second->getSource()))
  153. return true;
  154. for (Action::const_iterator AI = A->begin(), AE = A->end(); AI != AE; ++AI)
  155. if (ActionFailed(*AI, FailingCommands))
  156. return true;
  157. return false;
  158. }
  159. static bool InputsOk(const Command &C,
  160. const FailingCommandList &FailingCommands) {
  161. return !ActionFailed(&C.getSource(), FailingCommands);
  162. }
  163. void Compilation::ExecuteJob(const Job &J,
  164. FailingCommandList &FailingCommands) const {
  165. if (const Command *C = dyn_cast<Command>(&J)) {
  166. if (!InputsOk(*C, FailingCommands))
  167. return;
  168. const Command *FailingCommand = nullptr;
  169. if (int Res = ExecuteCommand(*C, FailingCommand))
  170. FailingCommands.push_back(std::make_pair(Res, FailingCommand));
  171. } else {
  172. const JobList *Jobs = cast<JobList>(&J);
  173. for (JobList::const_iterator it = Jobs->begin(), ie = Jobs->end();
  174. it != ie; ++it)
  175. ExecuteJob(**it, FailingCommands);
  176. }
  177. }
  178. void Compilation::initCompilationForDiagnostics() {
  179. // Free actions and jobs.
  180. DeleteContainerPointers(Actions);
  181. Jobs.clear();
  182. // Clear temporary/results file lists.
  183. TempFiles.clear();
  184. ResultFiles.clear();
  185. FailureResultFiles.clear();
  186. // Remove any user specified output. Claim any unclaimed arguments, so as
  187. // to avoid emitting warnings about unused args.
  188. OptSpecifier OutputOpts[] = { options::OPT_o, options::OPT_MD,
  189. options::OPT_MMD };
  190. for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
  191. if (TranslatedArgs->hasArg(OutputOpts[i]))
  192. TranslatedArgs->eraseArg(OutputOpts[i]);
  193. }
  194. TranslatedArgs->ClaimAllArgs();
  195. // Redirect stdout/stderr to /dev/null.
  196. Redirects = new const StringRef*[3]();
  197. Redirects[0] = nullptr;
  198. Redirects[1] = new const StringRef();
  199. Redirects[2] = new const StringRef();
  200. }
  201. StringRef Compilation::getSysRoot() const {
  202. return getDriver().SysRoot;
  203. }