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