Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This utility may be invoked in the following manner: |
| 11 | // clang --help - Output help info. |
| 12 | // clang [options] - Read from stdin. |
| 13 | // clang [options] file - Read from "file". |
| 14 | // clang [options] file1 file2 - Read these files. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // |
| 18 | // TODO: Options to support: |
| 19 | // |
| 20 | // -ffatal-errors |
| 21 | // -ftabstop=width |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "clang.h" |
| 26 | #include "ASTStreamers.h" |
| 27 | #include "TextDiagnosticBuffer.h" |
| 28 | #include "TextDiagnosticPrinter.h" |
| 29 | #include "clang/Parse/Parser.h" |
| 30 | #include "clang/Lex/HeaderSearch.h" |
| 31 | #include "clang/Basic/FileManager.h" |
| 32 | #include "clang/Basic/SourceManager.h" |
| 33 | #include "clang/Basic/TargetInfo.h" |
| 34 | #include "llvm/Support/CommandLine.h" |
| 35 | #include "llvm/Support/MemoryBuffer.h" |
| 36 | #include "llvm/System/Signals.h" |
| 37 | #include <memory> |
| 38 | using namespace clang; |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Global options. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | static llvm::cl::opt<bool> |
| 45 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 46 | static llvm::cl::opt<bool> |
| 47 | Stats("stats", llvm::cl::desc("Print performance metrics and statistics")); |
| 48 | |
| 49 | enum ProgActions { |
| 50 | EmitLLVM, // Emit a .ll file. |
| 51 | ParseASTPrint, // Parse ASTs and print them. |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 52 | ParseASTDump, // Parse ASTs and dump them. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | ParseASTCheck, // Parse ASTs and check diagnostics. |
| 54 | ParseAST, // Parse ASTs. |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 55 | ParseCFGDump, // Parse ASTS. Build CFGs. Print CFGs. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | ParsePrintCallbacks, // Parse and print each callback. |
| 57 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 58 | ParseNoop, // Parse with noop callbacks. |
| 59 | RunPreprocessorOnly, // Just lex, no output. |
| 60 | PrintPreprocessedInput, // -E mode. |
| 61 | DumpTokens // Token dump mode. |
| 62 | }; |
| 63 | |
| 64 | static llvm::cl::opt<ProgActions> |
| 65 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 66 | llvm::cl::init(ParseSyntaxOnly), |
| 67 | llvm::cl::values( |
| 68 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 69 | "Just run preprocessor, no output (for timings)"), |
| 70 | clEnumValN(PrintPreprocessedInput, "E", |
| 71 | "Run preprocessor, emit preprocessed file"), |
| 72 | clEnumValN(DumpTokens, "dumptokens", |
| 73 | "Run preprocessor, dump internal rep of tokens"), |
| 74 | clEnumValN(ParseNoop, "parse-noop", |
| 75 | "Run parser with noop callbacks (for timings)"), |
| 76 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 77 | "Run parser and perform semantic analysis"), |
| 78 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 79 | "Run parser and print each callback invoked"), |
| 80 | clEnumValN(ParseAST, "parse-ast", |
| 81 | "Run parser and build ASTs"), |
| 82 | clEnumValN(ParseASTPrint, "parse-ast-print", |
| 83 | "Run parser, build ASTs, then print ASTs"), |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 84 | clEnumValN(ParseASTDump, "parse-ast-dump", |
| 85 | "Run parser, build ASTs, then dump them"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | clEnumValN(ParseASTCheck, "parse-ast-check", |
| 87 | "Run parser, build ASTs, then check diagnostics"), |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 88 | clEnumValN(ParseCFGDump, "dump-cfg", |
| 89 | "Run parser, build ASTs, then build and print CFGs."), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 90 | clEnumValN(EmitLLVM, "emit-llvm", |
| 91 | "Build ASTs then convert to LLVM, emit .ll file"), |
| 92 | clEnumValEnd)); |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Language Options |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | enum LangKind { |
| 99 | langkind_unspecified, |
| 100 | langkind_c, |
| 101 | langkind_c_cpp, |
| 102 | langkind_cxx, |
| 103 | langkind_cxx_cpp, |
| 104 | langkind_objc, |
| 105 | langkind_objc_cpp, |
| 106 | langkind_objcxx, |
| 107 | langkind_objcxx_cpp |
| 108 | }; |
| 109 | |
| 110 | /* TODO: GCC also accepts: |
| 111 | c-header c++-header objective-c-header objective-c++-header |
| 112 | assembler assembler-with-cpp |
| 113 | ada, f77*, ratfor (!), f95, java, treelang |
| 114 | */ |
| 115 | static llvm::cl::opt<LangKind> |
| 116 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 117 | llvm::cl::init(langkind_unspecified), |
| 118 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 119 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 120 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 121 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
| 122 | clEnumValN(langkind_c_cpp, "c-cpp-output", |
| 123 | "Preprocessed C"), |
| 124 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
| 125 | "Preprocessed C++"), |
| 126 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 127 | "Preprocessed Objective C"), |
| 128 | clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output", |
| 129 | "Preprocessed Objective C++"), |
| 130 | clEnumValEnd)); |
| 131 | |
| 132 | static llvm::cl::opt<bool> |
| 133 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 134 | llvm::cl::Hidden); |
| 135 | static llvm::cl::opt<bool> |
| 136 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 137 | llvm::cl::Hidden); |
| 138 | |
| 139 | /// InitializeBaseLanguage - Handle the -x foo options or infer a base language |
| 140 | /// from the input filename. |
| 141 | static void InitializeBaseLanguage(LangOptions &Options, |
| 142 | const std::string &Filename) { |
| 143 | if (BaseLang == langkind_unspecified) { |
| 144 | std::string::size_type DotPos = Filename.rfind('.'); |
| 145 | if (LangObjC) { |
| 146 | BaseLang = langkind_objc; |
| 147 | } else if (LangObjCXX) { |
| 148 | BaseLang = langkind_objcxx; |
| 149 | } else if (DotPos == std::string::npos) { |
| 150 | BaseLang = langkind_c; // Default to C if no extension. |
| 151 | } else { |
| 152 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 153 | // C header: .h |
| 154 | // C++ header: .hh or .H; |
| 155 | // assembler no preprocessing: .s |
| 156 | // assembler: .S |
| 157 | if (Ext == "c") |
| 158 | BaseLang = langkind_c; |
| 159 | else if (Ext == "i") |
| 160 | BaseLang = langkind_c_cpp; |
| 161 | else if (Ext == "ii") |
| 162 | BaseLang = langkind_cxx_cpp; |
| 163 | else if (Ext == "m") |
| 164 | BaseLang = langkind_objc; |
| 165 | else if (Ext == "mi") |
| 166 | BaseLang = langkind_objc_cpp; |
| 167 | else if (Ext == "mm" || Ext == "M") |
| 168 | BaseLang = langkind_objcxx; |
| 169 | else if (Ext == "mii") |
| 170 | BaseLang = langkind_objcxx_cpp; |
| 171 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 172 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 173 | BaseLang = langkind_cxx; |
| 174 | else |
| 175 | BaseLang = langkind_c; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // FIXME: implement -fpreprocessed mode. |
| 180 | bool NoPreprocess = false; |
| 181 | |
| 182 | switch (BaseLang) { |
| 183 | default: assert(0 && "Unknown language kind!"); |
| 184 | case langkind_c_cpp: |
| 185 | NoPreprocess = true; |
| 186 | // FALLTHROUGH |
| 187 | case langkind_c: |
| 188 | break; |
| 189 | case langkind_cxx_cpp: |
| 190 | NoPreprocess = true; |
| 191 | // FALLTHROUGH |
| 192 | case langkind_cxx: |
| 193 | Options.CPlusPlus = 1; |
| 194 | break; |
| 195 | case langkind_objc_cpp: |
| 196 | NoPreprocess = true; |
| 197 | // FALLTHROUGH |
| 198 | case langkind_objc: |
| 199 | Options.ObjC1 = Options.ObjC2 = 1; |
| 200 | break; |
| 201 | case langkind_objcxx_cpp: |
| 202 | NoPreprocess = true; |
| 203 | // FALLTHROUGH |
| 204 | case langkind_objcxx: |
| 205 | Options.ObjC1 = Options.ObjC2 = 1; |
| 206 | Options.CPlusPlus = 1; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// LangStds - Language standards we support. |
| 212 | enum LangStds { |
| 213 | lang_unspecified, |
| 214 | lang_c89, lang_c94, lang_c99, |
| 215 | lang_gnu89, lang_gnu99, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 216 | lang_cxx98, lang_gnucxx98, |
| 217 | lang_cxx0x, lang_gnucxx0x |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | static llvm::cl::opt<LangStds> |
| 221 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 222 | llvm::cl::init(lang_unspecified), |
| 223 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 224 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 225 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 226 | clEnumValN(lang_c94, "iso9899:199409", |
| 227 | "ISO C 1990 with amendment 1"), |
| 228 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
| 229 | // clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
| 230 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
| 231 | // clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
| 232 | clEnumValN(lang_gnu89, "gnu89", |
| 233 | "ISO C 1990 with GNU extensions (default for C)"), |
| 234 | clEnumValN(lang_gnu99, "gnu99", |
| 235 | "ISO C 1999 with GNU extensions"), |
| 236 | clEnumValN(lang_gnu99, "gnu9x", |
| 237 | "ISO C 1999 with GNU extensions"), |
| 238 | clEnumValN(lang_cxx98, "c++98", |
| 239 | "ISO C++ 1998 with amendments"), |
| 240 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 241 | "ISO C++ 1998 with amendments and GNU " |
| 242 | "extensions (default for C++)"), |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 243 | clEnumValN(lang_cxx0x, "c++0x", |
| 244 | "Upcoming ISO C++ 200x with amendments"), |
| 245 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 246 | "Upcoming ISO C++ 200x with amendments and GNU " |
| 247 | "extensions (default for C++)"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 248 | clEnumValEnd)); |
| 249 | |
| 250 | static llvm::cl::opt<bool> |
| 251 | NoOperatorNames("fno-operator-names", |
| 252 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 253 | "synonyms for operators")); |
| 254 | |
| 255 | // FIXME: add: |
| 256 | // -ansi |
| 257 | // -trigraphs |
| 258 | // -fdollars-in-identifiers |
| 259 | static void InitializeLanguageStandard(LangOptions &Options) { |
| 260 | if (LangStd == lang_unspecified) { |
| 261 | // Based on the base language, pick one. |
| 262 | switch (BaseLang) { |
| 263 | default: assert(0 && "Unknown base language"); |
| 264 | case langkind_c: |
| 265 | case langkind_c_cpp: |
| 266 | case langkind_objc: |
| 267 | case langkind_objc_cpp: |
| 268 | LangStd = lang_gnu99; |
| 269 | break; |
| 270 | case langkind_cxx: |
| 271 | case langkind_cxx_cpp: |
| 272 | case langkind_objcxx: |
| 273 | case langkind_objcxx_cpp: |
| 274 | LangStd = lang_gnucxx98; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | switch (LangStd) { |
| 280 | default: assert(0 && "Unknown language standard!"); |
| 281 | |
| 282 | // Fall through from newer standards to older ones. This isn't really right. |
| 283 | // FIXME: Enable specifically the right features based on the language stds. |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 284 | case lang_gnucxx0x: |
| 285 | case lang_cxx0x: |
| 286 | Options.CPlusPlus0x = 1; |
| 287 | // FALL THROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | case lang_gnucxx98: |
| 289 | case lang_cxx98: |
| 290 | Options.CPlusPlus = 1; |
| 291 | Options.CXXOperatorNames = !NoOperatorNames; |
| 292 | // FALL THROUGH. |
| 293 | case lang_gnu99: |
| 294 | case lang_c99: |
| 295 | Options.Digraphs = 1; |
| 296 | Options.C99 = 1; |
| 297 | Options.HexFloats = 1; |
| 298 | // FALL THROUGH. |
| 299 | case lang_gnu89: |
| 300 | Options.BCPLComment = 1; // Only for C99/C++. |
| 301 | // FALL THROUGH. |
| 302 | case lang_c94: |
| 303 | case lang_c89: |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | Options.Trigraphs = 1; // -trigraphs or -ansi |
| 308 | Options.DollarIdents = 1; // FIXME: Really a target property. |
| 309 | } |
| 310 | |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | // Our DiagnosticClient implementation |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | |
| 315 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 316 | static llvm::cl::opt<bool> |
| 317 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 318 | |
| 319 | static llvm::cl::opt<bool> |
| 320 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 321 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 322 | |
| 323 | static llvm::cl::opt<bool> |
| 324 | ErrorOnExtensions("pedantic-errors", |
| 325 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 326 | |
| 327 | static llvm::cl::opt<bool> |
| 328 | WarnUnusedMacros("Wunused_macros", |
| 329 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 330 | |
| 331 | |
| 332 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 333 | /// current command line option settings. |
| 334 | static void InitializeDiagnostics(Diagnostic &Diags) { |
| 335 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 336 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 337 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 338 | |
| 339 | // Silence the "macro is not used" warning unless requested. |
| 340 | if (!WarnUnusedMacros) |
| 341 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
| 342 | } |
| 343 | |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | // Preprocessor Initialization |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | |
| 348 | // FIXME: Preprocessor builtins to support. |
| 349 | // -A... - Play with #assertions |
| 350 | // -undef - Undefine all predefined macros |
| 351 | |
| 352 | static llvm::cl::list<std::string> |
| 353 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 354 | llvm::cl::desc("Predefine the specified macro")); |
| 355 | static llvm::cl::list<std::string> |
| 356 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 357 | llvm::cl::desc("Undefine the specified macro")); |
| 358 | |
| 359 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 360 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 361 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 362 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 363 | const char *Command = "#define ") { |
| 364 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 365 | if (const char *Equal = strchr(Macro, '=')) { |
| 366 | // Turn the = into ' '. |
| 367 | Buf.insert(Buf.end(), Macro, Equal); |
| 368 | Buf.push_back(' '); |
| 369 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 370 | } else { |
| 371 | // Push "macroname 1". |
| 372 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 373 | Buf.push_back(' '); |
| 374 | Buf.push_back('1'); |
| 375 | } |
| 376 | Buf.push_back('\n'); |
| 377 | } |
| 378 | |
| 379 | static void InitializePredefinedMacros(Preprocessor &PP, |
| 380 | std::vector<char> &Buf) { |
| 381 | // FIXME: Implement magic like cpp_init_builtins for things like __STDC__ |
| 382 | // and __DATE__ etc. |
| 383 | #if 0 |
| 384 | /* __STDC__ has the value 1 under normal circumstances. |
| 385 | However, if (a) we are in a system header, (b) the option |
| 386 | stdc_0_in_system_headers is true (set by target config), and |
| 387 | (c) we are not in strictly conforming mode, then it has the |
| 388 | value 0. (b) and (c) are already checked in cpp_init_builtins. */ |
| 389 | //case BT_STDC: |
| 390 | if (cpp_in_system_header (pfile)) |
| 391 | number = 0; |
| 392 | else |
| 393 | number = 1; |
| 394 | break; |
| 395 | #endif |
| 396 | // These should all be defined in the preprocessor according to the |
| 397 | // current language configuration. |
| 398 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 399 | //DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
Chris Lattner | 94fea5a | 2007-07-22 22:11:35 +0000 | [diff] [blame] | 400 | if (PP.getLangOptions().C99 && !PP.getLangOptions().CPlusPlus) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 401 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
Chris Lattner | 94fea5a | 2007-07-22 22:11:35 +0000 | [diff] [blame] | 402 | else if (0) // STDC94 ? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 404 | |
| 405 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 406 | if (PP.getLangOptions().ObjC1) |
| 407 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 408 | if (PP.getLangOptions().ObjC2) |
| 409 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
| 410 | |
| 411 | // Get the target #defines. |
| 412 | PP.getTargetInfo().getTargetDefines(Buf); |
| 413 | |
| 414 | // Compiler set macros. |
| 415 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5250"); |
| 416 | DefineBuiltinMacro(Buf, "__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__=1030"); |
| 417 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=0"); |
| 418 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 419 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 420 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 421 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.0.1 (Apple Computer, Inc. " |
| 422 | "build 5250)\""); |
| 423 | |
| 424 | // Build configuration options. |
| 425 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 426 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 427 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 428 | DefineBuiltinMacro(Buf, "__PIC__=1"); |
| 429 | |
| 430 | |
| 431 | if (PP.getLangOptions().CPlusPlus) { |
| 432 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 433 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 434 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 435 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 436 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 437 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 438 | } |
| 439 | |
| 440 | // FIXME: Should emit a #line directive here. |
| 441 | |
| 442 | // Add macros from the command line. |
| 443 | // FIXME: Should traverse the #define/#undef lists in parallel. |
| 444 | for (unsigned i = 0, e = D_macros.size(); i != e; ++i) |
| 445 | DefineBuiltinMacro(Buf, D_macros[i].c_str()); |
| 446 | for (unsigned i = 0, e = U_macros.size(); i != e; ++i) |
| 447 | DefineBuiltinMacro(Buf, U_macros[i].c_str(), "#undef "); |
| 448 | } |
| 449 | |
| 450 | //===----------------------------------------------------------------------===// |
| 451 | // Preprocessor include path information. |
| 452 | //===----------------------------------------------------------------------===// |
| 453 | |
| 454 | // This tool exports a large number of command line options to control how the |
| 455 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 456 | // object takes a very simple interface: a list of directories to search for |
| 457 | // |
| 458 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame^] | 459 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 460 | // |
| 461 | // FIXME: -include,-imacros |
| 462 | |
| 463 | static llvm::cl::opt<bool> |
| 464 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 465 | |
| 466 | // Various command line options. These four add directories to each chain. |
| 467 | static llvm::cl::list<std::string> |
| 468 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 469 | llvm::cl::desc("Add directory to framework include search path")); |
| 470 | static llvm::cl::list<std::string> |
| 471 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 472 | llvm::cl::desc("Add directory to include search path")); |
| 473 | static llvm::cl::list<std::string> |
| 474 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 475 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 476 | static llvm::cl::list<std::string> |
| 477 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 478 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 479 | static llvm::cl::list<std::string> |
| 480 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 481 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 482 | |
| 483 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 484 | static llvm::cl::list<std::string> |
| 485 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 486 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 487 | static llvm::cl::list<std::string> |
| 488 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 489 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 490 | static llvm::cl::list<std::string> |
| 491 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 492 | llvm::cl::Prefix, |
| 493 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 494 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame^] | 495 | static llvm::cl::opt<std::string> |
| 496 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 497 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 498 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 499 | // Finally, implement the code that groks the options above. |
| 500 | enum IncludeDirGroup { |
| 501 | Quoted = 0, |
| 502 | Angled, |
| 503 | System, |
| 504 | After |
| 505 | }; |
| 506 | |
| 507 | static std::vector<DirectoryLookup> IncludeGroup[4]; |
| 508 | |
| 509 | /// AddPath - Add the specified path to the specified group list. |
| 510 | /// |
| 511 | static void AddPath(const std::string &Path, IncludeDirGroup Group, |
| 512 | bool isCXXAware, bool isUserSupplied, |
| 513 | bool isFramework, FileManager &FM) { |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame^] | 514 | const DirectoryEntry *DE; |
| 515 | if (Group == System) |
| 516 | DE = FM.getDirectory(isysroot + "/" + Path); |
| 517 | else |
| 518 | DE = FM.getDirectory(Path); |
| 519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 520 | if (DE == 0) { |
| 521 | if (Verbose) |
| 522 | fprintf(stderr, "ignoring nonexistent directory \"%s\"\n", |
| 523 | Path.c_str()); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | DirectoryLookup::DirType Type; |
| 528 | if (Group == Quoted || Group == Angled) |
| 529 | Type = DirectoryLookup::NormalHeaderDir; |
| 530 | else if (isCXXAware) |
| 531 | Type = DirectoryLookup::SystemHeaderDir; |
| 532 | else |
| 533 | Type = DirectoryLookup::ExternCSystemHeaderDir; |
| 534 | |
| 535 | IncludeGroup[Group].push_back(DirectoryLookup(DE, Type, isUserSupplied, |
| 536 | isFramework)); |
| 537 | } |
| 538 | |
| 539 | /// RemoveDuplicates - If there are duplicate directory entries in the specified |
| 540 | /// search list, remove the later (dead) ones. |
| 541 | static void RemoveDuplicates(std::vector<DirectoryLookup> &SearchList) { |
| 542 | std::set<const DirectoryEntry *> SeenDirs; |
| 543 | for (unsigned i = 0; i != SearchList.size(); ++i) { |
| 544 | // If this isn't the first time we've seen this dir, remove it. |
| 545 | if (!SeenDirs.insert(SearchList[i].getDir()).second) { |
| 546 | if (Verbose) |
| 547 | fprintf(stderr, "ignoring duplicate directory \"%s\"\n", |
| 548 | SearchList[i].getDir()->getName()); |
| 549 | SearchList.erase(SearchList.begin()+i); |
| 550 | --i; |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 556 | /// HeaderSearch object. |
| 557 | static void InitializeIncludePaths(HeaderSearch &Headers, FileManager &FM, |
| 558 | Diagnostic &Diags, const LangOptions &Lang) { |
| 559 | // Handle -F... options. |
| 560 | for (unsigned i = 0, e = F_dirs.size(); i != e; ++i) |
| 561 | AddPath(F_dirs[i], Angled, false, true, true, FM); |
| 562 | |
| 563 | // Handle -I... options. |
| 564 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 565 | if (I_dirs[i] == "-") { |
| 566 | // -I- is a deprecated GCC feature. |
| 567 | Diags.Report(SourceLocation(), diag::err_pp_I_dash_not_supported); |
| 568 | } else { |
| 569 | AddPath(I_dirs[i], Angled, false, true, false, FM); |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | // Handle -idirafter... options. |
| 574 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
| 575 | AddPath(idirafter_dirs[i], After, false, true, false, FM); |
| 576 | |
| 577 | // Handle -iquote... options. |
| 578 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
| 579 | AddPath(iquote_dirs[i], Quoted, false, true, false, FM); |
| 580 | |
| 581 | // Handle -isystem... options. |
| 582 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
| 583 | AddPath(isystem_dirs[i], System, false, true, false, FM); |
| 584 | |
| 585 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 586 | // parallel, processing the values in order of occurance to get the right |
| 587 | // prefixes. |
| 588 | { |
| 589 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 590 | unsigned iprefix_idx = 0; |
| 591 | unsigned iwithprefix_idx = 0; |
| 592 | unsigned iwithprefixbefore_idx = 0; |
| 593 | bool iprefix_done = iprefix_vals.empty(); |
| 594 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 595 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 596 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 597 | if (!iprefix_done && |
| 598 | (iwithprefix_done || |
| 599 | iprefix_vals.getPosition(iprefix_idx) < |
| 600 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 601 | (iwithprefixbefore_done || |
| 602 | iprefix_vals.getPosition(iprefix_idx) < |
| 603 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 604 | Prefix = iprefix_vals[iprefix_idx]; |
| 605 | ++iprefix_idx; |
| 606 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 607 | } else if (!iwithprefix_done && |
| 608 | (iwithprefixbefore_done || |
| 609 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 610 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 611 | AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 612 | System, false, false, false, FM); |
| 613 | ++iwithprefix_idx; |
| 614 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 615 | } else { |
| 616 | AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 617 | Angled, false, false, false, FM); |
| 618 | ++iwithprefixbefore_idx; |
| 619 | iwithprefixbefore_done = |
| 620 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | // FIXME: Add contents of the CPATH, C_INCLUDE_PATH, CPLUS_INCLUDE_PATH, |
| 626 | // OBJC_INCLUDE_PATH, OBJCPLUS_INCLUDE_PATH environment variables. |
| 627 | |
| 628 | // FIXME: temporary hack: hard-coded paths. |
| 629 | // FIXME: get these from the target? |
| 630 | if (!nostdinc) { |
| 631 | if (Lang.CPlusPlus) { |
| 632 | AddPath("/usr/include/c++/4.0.0", System, true, false, false, FM); |
| 633 | AddPath("/usr/include/c++/4.0.0/i686-apple-darwin8", System, true, false, |
| 634 | false, FM); |
| 635 | AddPath("/usr/include/c++/4.0.0/backward", System, true, false, false,FM); |
| 636 | } |
| 637 | |
| 638 | AddPath("/usr/local/include", System, false, false, false, FM); |
| 639 | // leopard |
| 640 | AddPath("/usr/lib/gcc/i686-apple-darwin9/4.0.1/include", System, |
| 641 | false, false, false, FM); |
| 642 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/4.0.1/include", |
| 643 | System, false, false, false, FM); |
| 644 | AddPath("/usr/lib/gcc/powerpc-apple-darwin9/" |
| 645 | "4.0.1/../../../../powerpc-apple-darwin0/include", |
| 646 | System, false, false, false, FM); |
| 647 | |
| 648 | // tiger |
| 649 | AddPath("/usr/lib/gcc/i686-apple-darwin8/4.0.1/include", System, |
| 650 | false, false, false, FM); |
| 651 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/4.0.1/include", |
| 652 | System, false, false, false, FM); |
| 653 | AddPath("/usr/lib/gcc/powerpc-apple-darwin8/" |
| 654 | "4.0.1/../../../../powerpc-apple-darwin8/include", |
| 655 | System, false, false, false, FM); |
| 656 | |
| 657 | AddPath("/usr/include", System, false, false, false, FM); |
| 658 | AddPath("/System/Library/Frameworks", System, true, false, true, FM); |
| 659 | AddPath("/Library/Frameworks", System, true, false, true, FM); |
| 660 | } |
| 661 | |
| 662 | // Now that we have collected all of the include paths, merge them all |
| 663 | // together and tell the preprocessor about them. |
| 664 | |
| 665 | // Concatenate ANGLE+SYSTEM+AFTER chains together into SearchList. |
| 666 | std::vector<DirectoryLookup> SearchList; |
| 667 | SearchList = IncludeGroup[Angled]; |
| 668 | SearchList.insert(SearchList.end(), IncludeGroup[System].begin(), |
| 669 | IncludeGroup[System].end()); |
| 670 | SearchList.insert(SearchList.end(), IncludeGroup[After].begin(), |
| 671 | IncludeGroup[After].end()); |
| 672 | RemoveDuplicates(SearchList); |
| 673 | RemoveDuplicates(IncludeGroup[Quoted]); |
| 674 | |
| 675 | // Prepend QUOTED list on the search list. |
| 676 | SearchList.insert(SearchList.begin(), IncludeGroup[Quoted].begin(), |
| 677 | IncludeGroup[Quoted].end()); |
| 678 | |
| 679 | |
| 680 | bool DontSearchCurDir = false; // TODO: set to true if -I- is set? |
| 681 | Headers.SetSearchPaths(SearchList, IncludeGroup[Quoted].size(), |
| 682 | DontSearchCurDir); |
| 683 | |
| 684 | // If verbose, print the list of directories that will be searched. |
| 685 | if (Verbose) { |
| 686 | fprintf(stderr, "#include \"...\" search starts here:\n"); |
| 687 | unsigned QuotedIdx = IncludeGroup[Quoted].size(); |
| 688 | for (unsigned i = 0, e = SearchList.size(); i != e; ++i) { |
| 689 | if (i == QuotedIdx) |
| 690 | fprintf(stderr, "#include <...> search starts here:\n"); |
| 691 | fprintf(stderr, " %s\n", SearchList[i].getDir()->getName()); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | |
| 697 | // Read any files specified by -imacros or -include. |
| 698 | static void ReadPrologFiles(Preprocessor &PP, std::vector<char> &Buf) { |
| 699 | // FIXME: IMPLEMENT |
| 700 | } |
| 701 | |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | // Basic Parser driver |
| 704 | //===----------------------------------------------------------------------===// |
| 705 | |
| 706 | static void ParseFile(Preprocessor &PP, MinimalAction *PA, unsigned MainFileID){ |
| 707 | Parser P(PP, *PA); |
| 708 | PP.EnterSourceFile(MainFileID, 0, true); |
| 709 | |
| 710 | // Parsing the specified input file. |
| 711 | P.ParseTranslationUnit(); |
| 712 | delete PA; |
| 713 | } |
| 714 | |
| 715 | //===----------------------------------------------------------------------===// |
| 716 | // Main driver |
| 717 | //===----------------------------------------------------------------------===// |
| 718 | |
| 719 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
| 720 | /// environment ready to process a single file. This returns the file ID for the |
| 721 | /// input file. If a failure happens, it returns 0. |
| 722 | /// |
| 723 | static unsigned InitializePreprocessor(Preprocessor &PP, |
| 724 | const std::string &InFile, |
| 725 | SourceManager &SourceMgr, |
| 726 | HeaderSearch &HeaderInfo, |
| 727 | const LangOptions &LangInfo, |
| 728 | std::vector<char> &PrologMacros) { |
| 729 | FileManager &FileMgr = HeaderInfo.getFileMgr(); |
| 730 | |
| 731 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 732 | InitializePredefinedMacros(PP, PrologMacros); |
| 733 | |
| 734 | // Read any files specified by -imacros or -include. |
| 735 | ReadPrologFiles(PP, PrologMacros); |
| 736 | |
| 737 | // Figure out where to get and map in the main file. |
| 738 | unsigned MainFileID = 0; |
| 739 | if (InFile != "-") { |
| 740 | const FileEntry *File = FileMgr.getFile(InFile); |
| 741 | if (File) MainFileID = SourceMgr.createFileID(File, SourceLocation()); |
| 742 | if (MainFileID == 0) { |
| 743 | fprintf(stderr, "Error reading '%s'!\n",InFile.c_str()); |
| 744 | return 0; |
| 745 | } |
| 746 | } else { |
| 747 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 748 | if (SB) MainFileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 749 | if (MainFileID == 0) { |
| 750 | fprintf(stderr, "Error reading standard input! Empty?\n"); |
| 751 | return 0; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | // Now that we have emitted the predefined macros, #includes, etc into |
| 756 | // PrologMacros, preprocess it to populate the initial preprocessor state. |
| 757 | |
| 758 | // Memory buffer must end with a null byte! |
| 759 | PrologMacros.push_back(0); |
| 760 | |
| 761 | llvm::MemoryBuffer *SB = |
| 762 | llvm::MemoryBuffer::getMemBuffer(&PrologMacros.front(),&PrologMacros.back(), |
| 763 | "<predefines>"); |
| 764 | assert(SB && "Cannot fail to create predefined source buffer"); |
| 765 | unsigned FileID = SourceMgr.createFileIDForMemBuffer(SB); |
| 766 | assert(FileID && "Could not create FileID for predefines?"); |
| 767 | |
| 768 | // Start parsing the predefines. |
| 769 | PP.EnterSourceFile(FileID, 0); |
| 770 | |
| 771 | // Lex the file, which will read all the macros. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 772 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 773 | PP.Lex(Tok); |
| 774 | assert(Tok.getKind() == tok::eof && "Didn't read entire file!"); |
| 775 | |
| 776 | // Once we've read this, we're done. |
| 777 | return MainFileID; |
| 778 | } |
| 779 | |
| 780 | /// ProcessInputFile - Process a single input file with the specified state. |
| 781 | /// |
| 782 | static void ProcessInputFile(Preprocessor &PP, unsigned MainFileID, |
| 783 | const std::string &InFile, |
| 784 | SourceManager &SourceMgr, |
| 785 | TextDiagnostics &OurDiagnosticClient, |
| 786 | HeaderSearch &HeaderInfo, |
| 787 | const LangOptions &LangInfo) { |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 788 | bool ClearSourceMgr = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 789 | switch (ProgAction) { |
| 790 | default: |
| 791 | fprintf(stderr, "Unexpected program action!\n"); |
| 792 | return; |
| 793 | case DumpTokens: { // Token dump mode. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 794 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 795 | // Start parsing the specified input file. |
| 796 | PP.EnterSourceFile(MainFileID, 0, true); |
| 797 | do { |
| 798 | PP.Lex(Tok); |
| 799 | PP.DumpToken(Tok, true); |
| 800 | fprintf(stderr, "\n"); |
| 801 | } while (Tok.getKind() != tok::eof); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 802 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | break; |
| 804 | } |
| 805 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 806 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 807 | // Start parsing the specified input file. |
| 808 | PP.EnterSourceFile(MainFileID, 0, true); |
| 809 | do { |
| 810 | PP.Lex(Tok); |
| 811 | } while (Tok.getKind() != tok::eof); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 812 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | |
| 816 | case PrintPreprocessedInput: // -E mode. |
| 817 | DoPrintPreprocessedInput(MainFileID, PP, LangInfo); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 818 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 819 | break; |
| 820 | |
| 821 | case ParseNoop: // -parse-noop |
| 822 | ParseFile(PP, new MinimalAction(), MainFileID); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 823 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 824 | break; |
| 825 | |
| 826 | case ParsePrintCallbacks: |
| 827 | ParseFile(PP, CreatePrintParserActionsAction(), MainFileID); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 828 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 829 | break; |
| 830 | case ParseSyntaxOnly: // -fsyntax-only |
| 831 | case ParseAST: |
| 832 | BuildASTs(PP, MainFileID, Stats); |
| 833 | break; |
| 834 | case ParseASTPrint: |
| 835 | PrintASTs(PP, MainFileID, Stats); |
| 836 | break; |
Chris Lattner | 6000dac | 2007-08-08 22:51:59 +0000 | [diff] [blame] | 837 | case ParseASTDump: |
| 838 | DumpASTs(PP, MainFileID, Stats); |
| 839 | break; |
Ted Kremenek | fddd518 | 2007-08-21 21:42:03 +0000 | [diff] [blame] | 840 | case ParseCFGDump: |
| 841 | DumpCFGs(PP, MainFileID, Stats); |
| 842 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 843 | case EmitLLVM: |
| 844 | EmitLLVMFromASTs(PP, MainFileID, Stats); |
| 845 | break; |
| 846 | case ParseASTCheck: |
| 847 | exit(CheckDiagnostics(PP, MainFileID)); |
| 848 | break; |
| 849 | } |
| 850 | |
| 851 | if (Stats) { |
| 852 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
| 853 | PP.PrintStats(); |
| 854 | PP.getIdentifierTable().PrintStats(); |
| 855 | HeaderInfo.PrintStats(); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 856 | if (ClearSourceMgr) |
| 857 | SourceMgr.PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 858 | fprintf(stderr, "\n"); |
| 859 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 860 | |
| 861 | // For a multi-file compilation, some things are ok with nuking the source |
| 862 | // manager tables, other require stable fileid/macroid's across multiple |
| 863 | // files. |
| 864 | if (ClearSourceMgr) { |
| 865 | SourceMgr.clearIDTables(); |
| 866 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static llvm::cl::list<std::string> |
| 870 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 871 | |
| 872 | |
| 873 | int main(int argc, char **argv) { |
| 874 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm cfe\n"); |
| 875 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 876 | |
| 877 | // If no input was specified, read from stdin. |
| 878 | if (InputFilenames.empty()) |
| 879 | InputFilenames.push_back("-"); |
| 880 | |
| 881 | /// Create a SourceManager object. This tracks and owns all the file buffers |
| 882 | /// allocated to the program. |
| 883 | SourceManager SourceMgr; |
| 884 | |
| 885 | // Create a file manager object to provide access to and cache the filesystem. |
| 886 | FileManager FileMgr; |
| 887 | |
| 888 | // Initialize language options, inferring file types from input filenames. |
| 889 | // FIXME: This infers info from the first file, we should clump by language |
| 890 | // to handle 'x.c y.c a.cpp b.cpp'. |
| 891 | LangOptions LangInfo; |
| 892 | InitializeBaseLanguage(LangInfo, InputFilenames[0]); |
| 893 | InitializeLanguageStandard(LangInfo); |
| 894 | |
| 895 | std::auto_ptr<TextDiagnostics> DiagClient; |
| 896 | if (ProgAction != ParseASTCheck) { |
| 897 | // Print diagnostics to stderr by default. |
| 898 | DiagClient.reset(new TextDiagnosticPrinter(SourceMgr)); |
| 899 | } else { |
| 900 | // When checking diagnostics, just buffer them up. |
| 901 | DiagClient.reset(new TextDiagnosticBuffer(SourceMgr)); |
| 902 | |
| 903 | if (InputFilenames.size() != 1) { |
| 904 | fprintf(stderr, |
| 905 | "parse-ast-check only works on single input files for now.\n"); |
| 906 | return 1; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | // Configure our handling of diagnostics. |
| 911 | Diagnostic Diags(*DiagClient); |
| 912 | InitializeDiagnostics(Diags); |
| 913 | |
| 914 | // Get information about the targets being compiled for. Note that this |
| 915 | // pointer and the TargetInfoImpl objects are never deleted by this toy |
| 916 | // driver. |
| 917 | TargetInfo *Target = CreateTargetInfo(Diags); |
| 918 | if (Target == 0) { |
| 919 | fprintf(stderr, |
| 920 | "Sorry, don't know what target this is, please use -arch.\n"); |
| 921 | exit(1); |
| 922 | } |
| 923 | |
| 924 | // Process the -I options and set them in the HeaderInfo. |
| 925 | HeaderSearch HeaderInfo(FileMgr); |
| 926 | DiagClient->setHeaderSearch(HeaderInfo); |
| 927 | InitializeIncludePaths(HeaderInfo, FileMgr, Diags, LangInfo); |
| 928 | |
| 929 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 930 | // Set up the preprocessor with these options. |
| 931 | Preprocessor PP(Diags, LangInfo, *Target, SourceMgr, HeaderInfo); |
| 932 | DiagClient->setPreprocessor(PP); |
| 933 | const std::string &InFile = InputFilenames[i]; |
| 934 | std::vector<char> PrologMacros; |
| 935 | unsigned MainFileID = InitializePreprocessor(PP, InFile, SourceMgr, |
| 936 | HeaderInfo, LangInfo, |
| 937 | PrologMacros); |
| 938 | |
| 939 | if (!MainFileID) continue; |
| 940 | |
| 941 | ProcessInputFile(PP, MainFileID, InFile, SourceMgr, |
| 942 | *DiagClient, HeaderInfo, LangInfo); |
| 943 | HeaderInfo.ClearFileInfo(); |
| 944 | } |
| 945 | |
| 946 | unsigned NumDiagnostics = Diags.getNumDiagnostics(); |
| 947 | |
| 948 | if (NumDiagnostics) |
| 949 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 950 | (NumDiagnostics == 1 ? "" : "s")); |
| 951 | |
| 952 | if (Stats) { |
| 953 | // Printed from high-to-low level. |
| 954 | SourceMgr.PrintStats(); |
| 955 | FileMgr.PrintStats(); |
| 956 | fprintf(stderr, "\n"); |
| 957 | } |
| 958 | |
Chris Lattner | 96f1a64 | 2007-07-21 05:40:53 +0000 | [diff] [blame] | 959 | return Diags.getNumErrors() != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 960 | } |