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