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