Compilation.cpp revision e530ad407af4a8904377592bfdb236acd320c6c2
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 10#include "clang/Driver/Compilation.h" 11 12#include "clang/Driver/Action.h" 13#include "clang/Driver/ArgList.h" 14#include "clang/Driver/Driver.h" 15#include "clang/Driver/DriverDiagnostic.h" 16#include "clang/Driver/ToolChain.h" 17 18#include "llvm/Support/raw_ostream.h" 19#include <sys/stat.h> 20#include <errno.h> 21using namespace clang::driver; 22 23Compilation::Compilation(Driver &D, 24 ToolChain &_DefaultToolChain, 25 ArgList *_Args) 26 : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) { 27} 28 29Compilation::~Compilation() { 30 delete Args; 31 32 // Free any derived arg lists. 33 for (llvm::DenseMap<const ToolChain*, ArgList*>::iterator 34 it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it) { 35 ArgList *A = it->second; 36 if (A != Args) 37 delete Args; 38 } 39 40 // Free the actions, if built. 41 for (ActionList::iterator it = Actions.begin(), ie = Actions.end(); 42 it != ie; ++it) 43 delete *it; 44} 45 46const ArgList &Compilation::getArgsForToolChain(const ToolChain *TC) { 47 if (!TC) 48 TC = &DefaultToolChain; 49 50 ArgList *&Entry = TCArgs[TC]; 51 if (!Entry) 52 Entry = TC->TranslateArgs(*Args); 53 54 return *Entry; 55} 56 57void Compilation::PrintJob(llvm::raw_ostream &OS, const Job *J, 58 const char *Terminator) const { 59 if (const Command *C = dyn_cast<Command>(J)) { 60 OS << " \"" << C->getExecutable() << '"'; 61 for (ArgStringList::const_iterator it = C->getArguments().begin(), 62 ie = C->getArguments().end(); it != ie; ++it) 63 OS << " \"" << *it << '"'; 64 OS << Terminator; 65 } else if (const PipedJob *PJ = dyn_cast<PipedJob>(J)) { 66 for (PipedJob::const_iterator 67 it = PJ->begin(), ie = PJ->end(); it != ie; ++it) 68 PrintJob(OS, *it, (it + 1 != PJ->end()) ? " |\n" : "\n"); 69 } else { 70 const JobList *Jobs = cast<JobList>(J); 71 for (JobList::const_iterator 72 it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it) 73 PrintJob(OS, *it, Terminator); 74 } 75} 76 77bool Compilation::CleanupFileList(const ArgStringList &Files, 78 bool IssueErrors) const { 79 bool Success = true; 80 81 for (ArgStringList::const_iterator 82 it = Files.begin(), ie = Files.end(); it != ie; ++it) { 83 llvm::sys::Path P(*it); 84 std::string Error; 85 86 if (P.eraseFromDisk(false, &Error)) { 87 // Failure is only failure if the file doesn't exist. There is a 88 // race condition here due to the limited interface of 89 // llvm::sys::Path, we want to know if the removal gave E_NOENT. 90 91 // FIXME: Grumble, P.exists() is broken. PR3837. 92 struct stat buf; 93 if (::stat(P.c_str(), &buf) || errno != ENOENT) { 94 if (IssueErrors) 95 getDriver().Diag(clang::diag::err_drv_unable_to_remove_file) 96 << Error; 97 Success = false; 98 } 99 } 100 } 101 102 return Success; 103} 104 105int Compilation::Execute() const { 106 // Just print if -### was present. 107 if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { 108 PrintJob(llvm::errs(), &Jobs, "\n"); 109 return 0; 110 } 111 112 // FIXME: Execute. 113 114 int Res = 0; 115 116 // Remove temp files. 117 CleanupFileList(TempFiles); 118 119 // If the compilation failed, remove result files as well. 120 if (Res != 0) 121 CleanupFileList(ResultFiles, true); 122 123 return 0; 124} 125