]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/Driver/Compilation.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / Driver / Compilation.cpp
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/Options.h"
17 #include "clang/Driver/ToolChain.h"
18
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/Program.h"
22 #include <sys/stat.h>
23 #include <errno.h>
24
25 using namespace clang::driver;
26 using namespace clang;
27
28 Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
29                          InputArgList *_Args, DerivedArgList *_TranslatedArgs)
30   : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
31     TranslatedArgs(_TranslatedArgs), Redirects(0) {
32 }
33
34 Compilation::~Compilation() {
35   delete TranslatedArgs;
36   delete Args;
37
38   // Free any derived arg lists.
39   for (llvm::DenseMap<std::pair<const ToolChain*, const char*>,
40                       DerivedArgList*>::iterator it = TCArgs.begin(),
41          ie = TCArgs.end(); it != ie; ++it)
42     if (it->second != TranslatedArgs)
43       delete it->second;
44
45   // Free the actions, if built.
46   for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
47        it != ie; ++it)
48     delete *it;
49
50   // Free redirections of stdout/stderr.
51   if (Redirects) {
52     delete Redirects[1];
53     delete Redirects[2];
54     delete [] Redirects;
55   }
56 }
57
58 const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC,
59                                                        const char *BoundArch) {
60   if (!TC)
61     TC = &DefaultToolChain;
62
63   DerivedArgList *&Entry = TCArgs[std::make_pair(TC, BoundArch)];
64   if (!Entry) {
65     Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch);
66     if (!Entry)
67       Entry = TranslatedArgs;
68   }
69
70   return *Entry;
71 }
72
73 void Compilation::PrintJob(raw_ostream &OS, const Job &J,
74                            const char *Terminator, bool Quote) const {
75   if (const Command *C = dyn_cast<Command>(&J)) {
76     OS << " \"" << C->getExecutable() << '"';
77     for (ArgStringList::const_iterator it = C->getArguments().begin(),
78            ie = C->getArguments().end(); it != ie; ++it) {
79       OS << ' ';
80       if (!Quote && !std::strpbrk(*it, " \"\\$")) {
81         OS << *it;
82         continue;
83       }
84
85       // Quote the argument and escape shell special characters; this isn't
86       // really complete but is good enough.
87       OS << '"';
88       for (const char *s = *it; *s; ++s) {
89         if (*s == '"' || *s == '\\' || *s == '$')
90           OS << '\\';
91         OS << *s;
92       }
93       OS << '"';
94     }
95     OS << Terminator;
96   } else {
97     const JobList *Jobs = cast<JobList>(&J);
98     for (JobList::const_iterator
99            it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
100       PrintJob(OS, **it, Terminator, Quote);
101   }
102 }
103
104 bool Compilation::CleanupFileList(const ArgStringList &Files,
105                                   bool IssueErrors) const {
106   bool Success = true;
107
108   for (ArgStringList::const_iterator
109          it = Files.begin(), ie = Files.end(); it != ie; ++it) {
110
111     llvm::sys::Path P(*it);
112     std::string Error;
113
114     // Don't try to remove files which we don't have write access to (but may be
115     // able to remove). Underlying tools may have intentionally not overwritten
116     // them.
117     if (!P.canWrite())
118       continue;
119
120     if (P.eraseFromDisk(false, &Error)) {
121       // Failure is only failure if the file exists and is "regular". There is
122       // a race condition here due to the limited interface of
123       // llvm::sys::Path, we want to know if the removal gave ENOENT.
124
125       // FIXME: Grumble, P.exists() is broken. PR3837.
126       struct stat buf;
127       if (::stat(P.c_str(), &buf) == 0 ? (buf.st_mode & S_IFMT) == S_IFREG :
128                                          (errno != ENOENT)) {
129         if (IssueErrors)
130           getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
131             << Error;
132         Success = false;
133       }
134     }
135   }
136
137   return Success;
138 }
139
140 int Compilation::ExecuteCommand(const Command &C,
141                                 const Command *&FailingCommand) const {
142   llvm::sys::Path Prog(C.getExecutable());
143   const char **Argv = new const char*[C.getArguments().size() + 2];
144   Argv[0] = C.getExecutable();
145   std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
146   Argv[C.getArguments().size() + 1] = 0;
147
148   if ((getDriver().CCCEcho || getDriver().CCPrintOptions ||
149        getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
150     raw_ostream *OS = &llvm::errs();
151
152     // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
153     // output stream.
154     if (getDriver().CCPrintOptions && getDriver().CCPrintOptionsFilename) {
155       std::string Error;
156       OS = new llvm::raw_fd_ostream(getDriver().CCPrintOptionsFilename,
157                                     Error,
158                                     llvm::raw_fd_ostream::F_Append);
159       if (!Error.empty()) {
160         getDriver().Diag(clang::diag::err_drv_cc_print_options_failure)
161           << Error;
162         FailingCommand = &C;
163         delete OS;
164         return 1;
165       }
166     }
167
168     if (getDriver().CCPrintOptions)
169       *OS << "[Logging clang options]";
170
171     PrintJob(*OS, C, "\n", /*Quote=*/getDriver().CCPrintOptions);
172
173     if (OS != &llvm::errs())
174       delete OS;
175   }
176
177   std::string Error;
178   int Res =
179     llvm::sys::Program::ExecuteAndWait(Prog, Argv,
180                                        /*env*/0, Redirects,
181                                        /*secondsToWait*/0, /*memoryLimit*/0,
182                                        &Error);
183   if (!Error.empty()) {
184     assert(Res && "Error string set with 0 result code!");
185     getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
186   }
187
188   if (Res)
189     FailingCommand = &C;
190
191   delete[] Argv;
192   return Res;
193 }
194
195 int Compilation::ExecuteJob(const Job &J,
196                             const Command *&FailingCommand) const {
197   if (const Command *C = dyn_cast<Command>(&J)) {
198     return ExecuteCommand(*C, FailingCommand);
199   } else {
200     const JobList *Jobs = cast<JobList>(&J);
201     for (JobList::const_iterator
202            it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
203       if (int Res = ExecuteJob(**it, FailingCommand))
204         return Res;
205     return 0;
206   }
207 }
208
209 void Compilation::initCompilationForDiagnostics(void) {
210   // Free actions and jobs.
211   DeleteContainerPointers(Actions);
212   Jobs.clear();
213
214   // Clear temporary/results file lists.
215   TempFiles.clear();
216   ResultFiles.clear();
217
218   // Remove any user specified output.  Claim any unclaimed arguments, so as
219   // to avoid emitting warnings about unused args.
220   if (TranslatedArgs->hasArg(options::OPT_o))
221     TranslatedArgs->eraseArg(options::OPT_o);
222   TranslatedArgs->ClaimAllArgs();
223
224   // Redirect stdout/stderr to /dev/null.
225   Redirects = new const llvm::sys::Path*[3]();
226   Redirects[1] = new const llvm::sys::Path();
227   Redirects[2] = new const llvm::sys::Path();
228 }