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