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