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