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