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