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