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