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