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