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