driver.cpp revision c88aa79a6bdfd445df800006f723f83f39934e98
1//===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===// 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// This is the entry point to the clang driver; it is a thin wrapper 11// for functionality in the Driver clang library. 12// 13//===----------------------------------------------------------------------===// 14 15#include "clang/Driver/Compilation.h" 16#include "clang/Driver/Driver.h" 17#include "clang/Driver/Option.h" 18 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" 28using namespace clang; 29using namespace clang::driver; 30 31class DriverDiagnosticPrinter : public DiagnosticClient { 32 std::string ProgName; 33 llvm::raw_ostream &OS; 34 35public: 36 DriverDiagnosticPrinter(const std::string _ProgName, 37 llvm::raw_ostream &_OS) 38 : ProgName(_ProgName), 39 OS(_OS) {} 40 41 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel, 42 const DiagnosticInfo &Info); 43}; 44 45void DriverDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, 46 const DiagnosticInfo &Info) { 47 OS << ProgName << ": "; 48 49 switch (Level) { 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; 55 } 56 57 llvm::SmallString<100> OutStr; 58 Info.FormatDiagnostic(OutStr); 59 OS.write(OutStr.begin(), OutStr.size()); 60 OS << '\n'; 61} 62 63llvm::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); 68} 69 70static const char *SaveStringInSet(std::set<std::string> &SavedStrings, 71 const std::string &S) { 72 return SavedStrings.insert(S).first->c_str(); 73} 74 75/// ApplyQAOverride - Apply a list of edits to the input argument lists. 76/// 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: 80/// 81/// '#': Silence information about the changes to the command line arguments. 82/// 83/// '^': Add FOO as a new argument at the beginning of the command line. 84/// 85/// '+': Add FOO as a new argument at the end of the command line. 86/// 87/// 's/XXX/YYY/': Replace the literal argument XXX by YYY in the 88/// command line. 89/// 90/// 'xOPTION': Removes all instances of the literal argument OPTION. 91/// 92/// 'XOPTION': Removes all instances of the literal argument OPTION, 93/// and the following argument. 94/// 95/// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox' 96/// at the end of the command line. 97/// 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. 102void 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. 107 108 if (Edit[0] == '^') { 109 const char *Str = 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] == '+') { 114 const char *Str = 115 SaveStringInSet(SavedStrings, Edit.substr(1, std::string::npos)); 116 OS << "### Adding argument " << Str << " at end\n"; 117 Args.push_back(Str); 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); 128 } else 129 OS << "### Invalid X edit, end of command line!\n"; 130 } 131 } else 132 ++i; 133 } 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' && 138 (A[2] == '\0' || 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); 143 } else 144 ++i; 145 } 146 OS << "### Adding argument " << Edit << " at end\n"; 147 Args.push_back(SaveStringInSet(SavedStrings, '-' + Edit)); 148 } else { 149 OS << "### Unrecognized edit: " << Edit << "\n"; 150 } 151} 152 153/// ApplyQAOverride - Apply a comma separate list of edits to the 154/// input argument lists. See ApplyOneQAOverride. 155void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr, 156 std::set<std::string> &SavedStrings) { 157 llvm::raw_ostream *OS = &llvm::errs(); 158 159 if (OverrideStr[0] == '#') { 160 ++OverrideStr; 161 OS = &llvm::nulls(); 162 } 163 164 *OS << "### QA_OVERRIDE_GCC3_OPTIONS: " << OverrideStr << "\n"; 165 166 // This does not need to be efficient. 167 168 const char *S = OverrideStr; 169 while (*S) { 170 const char *End = ::strchr(S, ' '); 171 if (!End) 172 End = S + strlen(S); 173 if (End != S) 174 ApplyOneQAOverride(*OS, Args, std::string(S, End), SavedStrings); 175 S = End; 176 if (*S != '\0') 177 ++S; 178 } 179} 180 181extern int cc1_main(const char **ArgBegin, const char **ArgEnd, 182 const char *Argv0, void *MainAddr); 183 184int main(int argc, const char **argv) { 185 llvm::sys::PrintStackTraceOnErrorSignal(); 186 llvm::PrettyStackTraceProgram X(argc, argv); 187 188 // Dispatch to cc1_main if appropriate. 189 if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1") 190 return cc1_main(argv+2, argv+argc, argv[0], 191 (void*) (intptr_t) GetExecutablePath); 192 193 llvm::sys::Path Path = GetExecutablePath(argv[0]); 194 DriverDiagnosticPrinter DiagClient(Path.getBasename(), llvm::errs()); 195 196 Diagnostic Diags(&DiagClient); 197 198#ifdef CLANG_IS_PRODUCTION 199 bool IsProduction = true; 200#else 201 bool IsProduction = false; 202#endif 203 Driver TheDriver(Path.getBasename().c_str(), Path.getDirname().c_str(), 204 llvm::sys::getHostTriple().c_str(), 205 "a.out", IsProduction, Diags); 206 207 // Check for ".*++" or ".*++-[^-]*" to determine if we are a C++ 208 // compiler. This matches things like "c++", "clang++", and "clang++-1.1". 209 // 210 // Note that we intentionally want to use argv[0] here, to support "clang++" 211 // being a symlink. 212 // 213 // We use *argv instead of argv[0] to work around a bogus g++ warning. 214 std::string ProgName(llvm::sys::Path(*argv).getBasename()); 215 if (llvm::StringRef(ProgName).endswith("++") || 216 llvm::StringRef(ProgName).rsplit('-').first.endswith("++")) 217 TheDriver.CCCIsCXX = true; 218 219 llvm::OwningPtr<Compilation> C; 220 221 // Handle QA_OVERRIDE_GCC3_OPTIONS and CCC_ADD_ARGS, used for editing a 222 // command line behind the scenes. 223 std::set<std::string> SavedStrings; 224 if (const char *OverrideStr = ::getenv("QA_OVERRIDE_GCC3_OPTIONS")) { 225 // FIXME: Driver shouldn't take extra initial argument. 226 std::vector<const char*> StringPointers(argv, argv + argc); 227 228 ApplyQAOverride(StringPointers, OverrideStr, SavedStrings); 229 230 C.reset(TheDriver.BuildCompilation(StringPointers.size(), 231 &StringPointers[0])); 232 } else if (const char *Cur = ::getenv("CCC_ADD_ARGS")) { 233 std::vector<const char*> StringPointers; 234 235 // FIXME: Driver shouldn't take extra initial argument. 236 StringPointers.push_back(argv[0]); 237 238 for (;;) { 239 const char *Next = strchr(Cur, ','); 240 241 if (Next) { 242 StringPointers.push_back(SaveStringInSet(SavedStrings, 243 std::string(Cur, Next))); 244 Cur = Next + 1; 245 } else { 246 if (*Cur != '\0') 247 StringPointers.push_back(SaveStringInSet(SavedStrings, Cur)); 248 break; 249 } 250 } 251 252 StringPointers.insert(StringPointers.end(), argv + 1, argv + argc); 253 254 C.reset(TheDriver.BuildCompilation(StringPointers.size(), 255 &StringPointers[0])); 256 } else 257 C.reset(TheDriver.BuildCompilation(argc, argv)); 258 259 int Res = 0; 260 if (C.get()) 261 Res = TheDriver.ExecuteCompilation(*C); 262 263 llvm::llvm_shutdown(); 264 265 return Res; 266} 267 268