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