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