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