1 //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the entry point to the clang driver; it is a thin wrapper
11 // for functionality in the Driver clang library.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Driver/Compilation.h"
16 #include "clang/Driver/Driver.h"
17 #include "clang/Driver/Option.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/Config/config.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/PrettyStackTrace.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/System/Host.h"
26 #include "llvm/System/Path.h"
27 #include "llvm/System/Signals.h"
28 using namespace clang;
29 using namespace clang::driver;
31 class DriverDiagnosticPrinter : public DiagnosticClient {
33 llvm::raw_ostream &OS;
36 DriverDiagnosticPrinter(const std::string _ProgName,
37 llvm::raw_ostream &_OS)
38 : ProgName(_ProgName),
41 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
42 const DiagnosticInfo &Info);
45 void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
46 const DiagnosticInfo &Info) {
47 OS << ProgName << ": ";
50 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type");
51 case Diagnostic::Note: OS << "note: "; break;
52 case Diagnostic::Warning: OS << "warning: "; break;
53 case Diagnostic::Error: OS << "error: "; break;
54 case Diagnostic::Fatal: OS << "fatal error: "; break;
57 llvm::SmallString<100> OutStr;
58 Info.FormatDiagnostic(OutStr);
59 OS.write(OutStr.begin(), OutStr.size());
63 llvm::sys::Path GetExecutablePath(const char *Argv0) {
64 // This just needs to be some symbol in the binary; C++ doesn't
65 // allow taking the address of ::main however.
66 void *P = (void*) (intptr_t) GetExecutablePath;
67 return llvm::sys::Path::GetMainExecutable(Argv0, P);
70 static const char *SaveStringInSet(std::set<std::string> &SavedStrings,
71 const std::string &S) {
72 return SavedStrings.insert(S).first->c_str();
75 /// ApplyQAOverride - Apply a list of edits to the input argument lists.
77 /// The input string is a space separate list of edits to perform,
78 /// they are applied in order to the input argument lists. Edits
79 /// should be one of the following forms:
81 /// '#': Silence information about the changes to the command line arguments.
83 /// '^': Add FOO as a new argument at the beginning of the command line.
85 /// '+': Add FOO as a new argument at the end of the command line.
87 /// 's/XXX/YYY/': Replace the literal argument XXX by YYY in the
90 /// 'xOPTION': Removes all instances of the literal argument OPTION.
92 /// 'XOPTION': Removes all instances of the literal argument OPTION,
93 /// and the following argument.
95 /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox'
96 /// at the end of the command line.
98 /// \param OS - The stream to write edit information to.
99 /// \param Args - The vector of command line arguments.
100 /// \param Edit - The override command to perform.
101 /// \param SavedStrings - Set to use for storing string representations.
102 void ApplyOneQAOverride(llvm::raw_ostream &OS,
103 std::vector<const char*> &Args,
104 const std::string &Edit,
105 std::set<std::string> &SavedStrings) {
106 // This does not need to be efficient.
108 if (Edit[0] == '^') {
110 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
111 OS << "### Adding argument " << Str << " at beginning\n";
112 Args.insert(Args.begin() + 1, Str);
113 } else if (Edit[0] == '+') {
115 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos));
116 OS << "### Adding argument " << Str << " at end\n";
118 } else if (Edit[0] == 'x' || Edit[0] == 'X') {
119 std::string Option = Edit.substr(1, std::string::npos);
120 for (unsigned i = 1; i < Args.size();) {
121 if (Option == Args[i]) {
122 OS << "### Deleting argument " << Args[i] << '\n';
123 Args.erase(Args.begin() + i);
124 if (Edit[0] == 'X') {
125 if (i < Args.size()) {
126 OS << "### Deleting argument " << Args[i] << '\n';
127 Args.erase(Args.begin() + i);
129 OS << "### Invalid X edit, end of command line!\n";
134 } else if (Edit[0] == 'O') {
135 for (unsigned i = 1; i < Args.size();) {
136 const char *A = Args[i];
137 if (A[0] == '-' && A[1] == 'O' &&
139 (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' ||
140 ('0' <= A[2] && A[2] <= '9'))))) {
141 OS << "### Deleting argument " << Args[i] << '\n';
142 Args.erase(Args.begin() + i);
146 OS << "### Adding argument " << Edit << " at end\n";
147 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit));
149 OS << "### Unrecognized edit: " << Edit << "\n";
153 /// ApplyQAOverride - Apply a comma separate list of edits to the
154 /// input argument lists. See ApplyOneQAOverride.
155 void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
156 std::set<std::string> &SavedStrings) {
157 llvm::raw_ostream *OS = &llvm::errs();
159 if (OverrideStr[0] == '#') {
164 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n";
166 // This does not need to be efficient.
168 const char *S = OverrideStr;
170 const char *End = ::strchr(S, ' ');
174 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings);
181 extern int cc1_main(Diagnostic &Diags,
182 const char **ArgBegin, const char **ArgEnd);
184 int main(int argc, const char **argv) {
185 llvm::sys::PrintStackTraceOnErrorSignal();
186 llvm::PrettyStackTraceProgram X(argc, argv);
188 llvm::sys::Path Path = GetExecutablePath(argv[0]);
189 DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs());
191 Diagnostic Diags(&DiagClient);
193 // Dispatch to cc1_main if appropriate.
194 if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1")
195 return cc1_main(Diags, argv+2, argv+argc);
197 #ifdef CLANG_IS_PRODUCTION
198 bool IsProduction = true;
200 bool IsProduction = false;
202 Driver TheDriver(Path.getBasename().c_str(), Path.getDirname().c_str(),
203 llvm::sys::getHostTriple().c_str(),
204 "a.out", IsProduction, Diags);
206 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++
207 // compiler. This matches things like "c++", "clang++", and "clang++-1.1".
209 // Note that we intentionally want to use argv[0] here, to support "clang++"
211 std::string ProgName(llvm::sys::Path(argv[0]).getBasename());
212 if (llvm::StringRef(ProgName).endswith("++") ||
213 llvm::StringRef(ProgName).rsplit('-').first.endswith("++"))
214 TheDriver.CCCIsCXX = true;
216 llvm::OwningPtr<Compilation> C;
218 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a
219 // command line behind the scenes.
220 std::set<std::string> SavedStrings;
221 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) {
222 // FIXME: Driver shouldn't take extra initial argument.
223 std::vector<const char*> StringPointers(argv, argv + argc);
225 ApplyQAOverride(StringPointers, OverrideStr, SavedStrings);
227 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
228 &StringPointers[0]));
229 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) {
230 std::vector<const char*> StringPointers;
232 // FIXME: Driver shouldn't take extra initial argument.
233 StringPointers.push_back(argv[0]);
236 const char *Next = strchr(Cur, ',');
239 StringPointers.push_back(SaveStringInSet(SavedStrings,
240 std::string(Cur, Next)));
244 StringPointers.push_back(SaveStringInSet(SavedStrings, Cur));
249 StringPointers.insert(StringPointers.end(), argv + 1, argv + argc);
251 C.reset(TheDriver.BuildCompilation(StringPointers.size(),
252 &StringPointers[0]));
254 C.reset(TheDriver.BuildCompilation(argc, argv));
258 Res = TheDriver.ExecuteCompilation(*C);
260 llvm::llvm_shutdown();