Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 26 | #include "ASTConsumers.h" |
Zhongxing Xu | e707944 | 2008-08-24 02:33:36 +0000 | [diff] [blame] | 27 | #include "clang/Driver/HTMLDiagnostics.h" |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 28 | #include "clang/Driver/InitHeaderSearch.h" |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 29 | #include "clang/Driver/TextDiagnosticBuffer.h" |
| 30 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 31 | #include "clang/Analysis/PathDiagnostic.h" |
Daniel Dunbar | de30073 | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 32 | #include "clang/AST/Decl.h" |
Ted Kremenek | ac88193 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 33 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 34 | #include "clang/CodeGen/ModuleBuilder.h" |
Chris Lattner | 0bed6ec | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 35 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 36 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | #include "clang/Parse/Parser.h" |
| 38 | #include "clang/Lex/HeaderSearch.h" |
| 39 | #include "clang/Basic/FileManager.h" |
| 40 | #include "clang/Basic/SourceManager.h" |
| 41 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | ac139d2 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/StringExtras.h" |
| 45 | #include "llvm/Config/config.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | #include "llvm/Support/CommandLine.h" |
| 47 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | abe2e54 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 48 | #include "llvm/System/Host.h" |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 49 | #include "llvm/System/Path.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 50 | #include "llvm/System/Signals.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | using namespace clang; |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // Global options. |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 57 | bool HadErrors = false; |
| 58 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 59 | static llvm::cl::opt<bool> |
| 60 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 61 | static llvm::cl::opt<bool> |
Nate Begeman | 6acbedd | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 62 | Stats("print-stats", |
| 63 | llvm::cl::desc("Print performance metrics and statistics")); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 64 | |
| 65 | enum ProgActions { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 66 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 67 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 68 | RewriteMacros, // Expand macros but not #includes. |
Ted Kremenek | e1a79d8 | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 69 | HTMLTest, // HTML displayer testing stuff. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 71 | EmitBC, // Emit a .bc file. |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 72 | SerializeAST, // Emit a .ast file. |
Ted Kremenek | 24612ae | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 73 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 4045a8a | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 74 | ASTPrint, // Parse ASTs and print them. |
| 75 | ASTDump, // Parse ASTs and dump them. |
| 76 | ASTView, // Parse ASTs and view them in Graphviz. |
Ted Kremenek | 221bb8d | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 77 | TestSerialization, // Run experimental serialization code. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 78 | ParsePrintCallbacks, // Parse and print each callback. |
| 79 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 80 | ParseNoop, // Parse with noop callbacks. |
| 81 | RunPreprocessorOnly, // Just lex, no output. |
| 82 | PrintPreprocessedInput, // -E mode. |
Ted Kremenek | 81ea799 | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 83 | DumpTokens, // Token dump mode. |
| 84 | RunAnalysis // Run one or more source code analyses. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | static llvm::cl::opt<ProgActions> |
| 88 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 89 | llvm::cl::init(ParseSyntaxOnly), |
| 90 | llvm::cl::values( |
| 91 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 92 | "Just run preprocessor, no output (for timings)"), |
| 93 | clEnumValN(PrintPreprocessedInput, "E", |
| 94 | "Run preprocessor, emit preprocessed file"), |
| 95 | clEnumValN(DumpTokens, "dumptokens", |
| 96 | "Run preprocessor, dump internal rep of tokens"), |
| 97 | clEnumValN(ParseNoop, "parse-noop", |
| 98 | "Run parser with noop callbacks (for timings)"), |
| 99 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 100 | "Run parser and perform semantic analysis"), |
| 101 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 102 | "Run parser and print each callback invoked"), |
Ted Kremenek | 24612ae | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 103 | clEnumValN(EmitHTML, "emit-html", |
| 104 | "Output input source as HTML"), |
Chris Lattner | 4045a8a | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 105 | clEnumValN(ASTPrint, "ast-print", |
| 106 | "Build ASTs and then pretty-print them"), |
| 107 | clEnumValN(ASTDump, "ast-dump", |
| 108 | "Build ASTs and then debug dump them"), |
Chris Lattner | 664dd08 | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 109 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 110 | "Build ASTs and view them with GraphViz"), |
Ted Kremenek | 221bb8d | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 111 | clEnumValN(TestSerialization, "test-pickling", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 112 | "Run prototype serialization code"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 113 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 114 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 115 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 116 | "Build ASTs then convert to LLVM, emit .bc file"), |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 117 | clEnumValN(SerializeAST, "serialize", |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 118 | "Build ASTs and emit .ast file"), |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 119 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 120 | "Rewrite ObjC into C (code rewriter example)"), |
| 121 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 122 | "Expand macros without full preprocessing"), |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 123 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 124 | "Rewrite Blocks to C"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 125 | clEnumValEnd)); |
| 126 | |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 127 | |
| 128 | static llvm::cl::opt<std::string> |
| 129 | OutputFile("o", |
Ted Kremenek | 09b3f0d | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 130 | llvm::cl::value_desc("path"), |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 131 | llvm::cl::desc("Specify output file (for --serialize, this is a directory)")); |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 132 | |
| 133 | //===----------------------------------------------------------------------===// |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 134 | // Code Generator Options |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | static llvm::cl::opt<bool> |
| 137 | GenerateDebugInfo("g", |
| 138 | llvm::cl::desc("Generate source level debug information")); |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 141 | // Diagnostic Options |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
Ted Kremenek | 10389cf | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 144 | static llvm::cl::opt<bool> |
| 145 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 146 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 10389cf | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 148 | static llvm::cl::opt<std::string> |
| 149 | HTMLDiag("html-diags", |
| 150 | llvm::cl::desc("Generate HTML to report diagnostics"), |
| 151 | llvm::cl::value_desc("HTML directory")); |
| 152 | |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 153 | static llvm::cl::opt<bool> |
| 154 | NoShowColumn("fno-show-column", |
| 155 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 156 | |
| 157 | static llvm::cl::opt<bool> |
| 158 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 159 | llvm::cl::desc("Do not include source line and caret with" |
| 160 | " diagnostics")); |
| 161 | |
| 162 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 164 | // Analyzer Options |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | |
| 167 | static llvm::cl::opt<bool> |
Ted Kremenek | cf26225 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 168 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 169 | llvm::cl::desc("Display exploded graph using GraphViz")); |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 170 | |
| 171 | static llvm::cl::opt<bool> |
Ted Kremenek | cf26225 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 172 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 173 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 174 | |
| 175 | static llvm::cl::opt<bool> |
| 176 | AnalyzeAll("analyzer-opt-analyze-headers", |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 177 | llvm::cl::desc("Force the static analyzer to analyze " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 178 | "functions defined in header files")); |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 179 | |
Ted Kremenek | 81ea799 | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 180 | static llvm::cl::list<Analyses> |
| 181 | AnalysisList(llvm::cl::desc("Available Source Code Analyses:"), |
| 182 | llvm::cl::values( |
Ted Kremenek | fbda0ef | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 183 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
Ted Kremenek | 8ce61b3 | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 184 | clEnumValN(NAME, CMDFLAG, DESC), |
| 185 | #include "Analyses.def" |
| 186 | clEnumValEnd)); |
Ted Kremenek | 81ea799 | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 188 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 189 | // Language Options |
| 190 | //===----------------------------------------------------------------------===// |
| 191 | |
| 192 | enum LangKind { |
| 193 | langkind_unspecified, |
| 194 | langkind_c, |
| 195 | langkind_c_cpp, |
| 196 | langkind_cxx, |
| 197 | langkind_cxx_cpp, |
| 198 | langkind_objc, |
| 199 | langkind_objc_cpp, |
| 200 | langkind_objcxx, |
| 201 | langkind_objcxx_cpp |
| 202 | }; |
| 203 | |
| 204 | /* TODO: GCC also accepts: |
| 205 | c-header c++-header objective-c-header objective-c++-header |
| 206 | assembler assembler-with-cpp |
| 207 | ada, f77*, ratfor (!), f95, java, treelang |
| 208 | */ |
| 209 | static llvm::cl::opt<LangKind> |
| 210 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 211 | llvm::cl::init(langkind_unspecified), |
| 212 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 213 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 214 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 215 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
| 216 | clEnumValN(langkind_c_cpp, "c-cpp-output", |
| 217 | "Preprocessed C"), |
| 218 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
| 219 | "Preprocessed C++"), |
| 220 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 221 | "Preprocessed Objective C"), |
| 222 | clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output", |
| 223 | "Preprocessed Objective C++"), |
| 224 | clEnumValEnd)); |
| 225 | |
| 226 | static llvm::cl::opt<bool> |
| 227 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 228 | llvm::cl::Hidden); |
| 229 | static llvm::cl::opt<bool> |
| 230 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 231 | llvm::cl::Hidden); |
| 232 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 233 | /// InitializeBaseLanguage - Handle the -x foo options. |
| 234 | static void InitializeBaseLanguage() { |
| 235 | if (LangObjC) |
| 236 | BaseLang = langkind_objc; |
| 237 | else if (LangObjCXX) |
| 238 | BaseLang = langkind_objcxx; |
| 239 | } |
| 240 | |
| 241 | static LangKind GetLanguage(const std::string &Filename) { |
| 242 | if (BaseLang != langkind_unspecified) |
| 243 | return BaseLang; |
| 244 | |
| 245 | std::string::size_type DotPos = Filename.rfind('.'); |
| 246 | |
| 247 | if (DotPos == std::string::npos) { |
| 248 | BaseLang = langkind_c; // Default to C if no extension. |
Chris Lattner | 4eac050 | 2008-01-04 19:12:28 +0000 | [diff] [blame] | 249 | return langkind_c; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 252 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 253 | // C header: .h |
| 254 | // C++ header: .hh or .H; |
| 255 | // assembler no preprocessing: .s |
| 256 | // assembler: .S |
| 257 | if (Ext == "c") |
| 258 | return langkind_c; |
| 259 | else if (Ext == "i") |
| 260 | return langkind_c_cpp; |
| 261 | else if (Ext == "ii") |
| 262 | return langkind_cxx_cpp; |
| 263 | else if (Ext == "m") |
| 264 | return langkind_objc; |
| 265 | else if (Ext == "mi") |
| 266 | return langkind_objc_cpp; |
| 267 | else if (Ext == "mm" || Ext == "M") |
| 268 | return langkind_objcxx; |
| 269 | else if (Ext == "mii") |
| 270 | return langkind_objcxx_cpp; |
| 271 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 272 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 273 | return langkind_cxx; |
| 274 | else |
| 275 | return langkind_c; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | static void InitializeLangOptions(LangOptions &Options, LangKind LK) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 280 | // FIXME: implement -fpreprocessed mode. |
| 281 | bool NoPreprocess = false; |
| 282 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 283 | switch (LK) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 284 | default: assert(0 && "Unknown language kind!"); |
| 285 | case langkind_c_cpp: |
| 286 | NoPreprocess = true; |
| 287 | // FALLTHROUGH |
| 288 | case langkind_c: |
| 289 | break; |
| 290 | case langkind_cxx_cpp: |
| 291 | NoPreprocess = true; |
| 292 | // FALLTHROUGH |
| 293 | case langkind_cxx: |
| 294 | Options.CPlusPlus = 1; |
| 295 | break; |
| 296 | case langkind_objc_cpp: |
| 297 | NoPreprocess = true; |
| 298 | // FALLTHROUGH |
| 299 | case langkind_objc: |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 300 | Options.ObjC1 = Options.ObjC2 = 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 301 | break; |
| 302 | case langkind_objcxx_cpp: |
| 303 | NoPreprocess = true; |
| 304 | // FALLTHROUGH |
| 305 | case langkind_objcxx: |
| 306 | Options.ObjC1 = Options.ObjC2 = 1; |
| 307 | Options.CPlusPlus = 1; |
| 308 | break; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /// LangStds - Language standards we support. |
| 313 | enum LangStds { |
| 314 | lang_unspecified, |
| 315 | lang_c89, lang_c94, lang_c99, |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 316 | lang_gnu_START, |
| 317 | lang_gnu89 = lang_gnu_START, lang_gnu99, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 318 | lang_cxx98, lang_gnucxx98, |
| 319 | lang_cxx0x, lang_gnucxx0x |
| 320 | }; |
| 321 | |
| 322 | static llvm::cl::opt<LangStds> |
| 323 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 324 | llvm::cl::init(lang_unspecified), |
| 325 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 326 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 327 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 328 | clEnumValN(lang_c94, "iso9899:199409", |
| 329 | "ISO C 1990 with amendment 1"), |
| 330 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
| 331 | // clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
| 332 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
| 333 | // clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
| 334 | clEnumValN(lang_gnu89, "gnu89", |
| 335 | "ISO C 1990 with GNU extensions (default for C)"), |
| 336 | clEnumValN(lang_gnu99, "gnu99", |
| 337 | "ISO C 1999 with GNU extensions"), |
| 338 | clEnumValN(lang_gnu99, "gnu9x", |
| 339 | "ISO C 1999 with GNU extensions"), |
| 340 | clEnumValN(lang_cxx98, "c++98", |
| 341 | "ISO C++ 1998 with amendments"), |
| 342 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 343 | "ISO C++ 1998 with amendments and GNU " |
| 344 | "extensions (default for C++)"), |
| 345 | clEnumValN(lang_cxx0x, "c++0x", |
| 346 | "Upcoming ISO C++ 200x with amendments"), |
| 347 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 348 | "Upcoming ISO C++ 200x with amendments and GNU " |
| 349 | "extensions (default for C++)"), |
| 350 | clEnumValEnd)); |
| 351 | |
| 352 | static llvm::cl::opt<bool> |
| 353 | NoOperatorNames("fno-operator-names", |
| 354 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 355 | "synonyms for operators")); |
| 356 | |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 357 | static llvm::cl::opt<bool> |
| 358 | PascalStrings("fpascal-strings", |
| 359 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 360 | "string literals")); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 361 | |
| 362 | static llvm::cl::opt<bool> |
| 363 | MSExtensions("fms-extensions", |
| 364 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 365 | "Microsoft header files ")); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 366 | |
| 367 | static llvm::cl::opt<bool> |
| 368 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 369 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | e87cd98 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 370 | |
| 371 | static llvm::cl::opt<bool> |
| 372 | LaxVectorConversions("flax-vector-conversions", |
| 373 | llvm::cl::desc("Allow implicit conversions between vectors" |
| 374 | " with a different number of elements or " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 375 | "different element types")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 376 | |
Daniel Dunbar | 91692d9 | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 377 | // FIXME: This (and all GCC -f options) really come in -f... and |
| 378 | // -fno-... forms, and additionally support automagic behavior when |
| 379 | // they are not defined. For example, -fexceptions defaults to on or |
| 380 | // off depending on the language. We should support this behavior in |
| 381 | // some form (perhaps just add a facility for distinguishing when an |
| 382 | // has its default value from when it has been set to its default |
| 383 | // value). |
| 384 | static llvm::cl::opt<bool> |
| 385 | Exceptions("fexceptions", |
| 386 | llvm::cl::desc("Enable support for exception handling.")); |
| 387 | |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 388 | static llvm::cl::opt<bool> |
| 389 | GNURuntime("fgnu-runtime", |
| 390 | llvm::cl::desc("Generate output compatible with the standard GNU Objective-C runtime.")); |
| 391 | |
| 392 | static llvm::cl::opt<bool> |
| 393 | NeXTRuntime("fnext-runtime", |
| 394 | llvm::cl::desc("Generate output compatible with the NeXT runtime.")); |
| 395 | |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 396 | |
| 397 | |
| 398 | static llvm::cl::opt<bool> |
| 399 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences.")); |
| 400 | |
| 401 | static llvm::cl::opt<bool> |
| 402 | Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89.")); |
| 403 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 404 | // FIXME: add: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 405 | // -fdollars-in-identifiers |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 406 | // -fpascal-strings |
Daniel Dunbar | 3454295 | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 407 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
| 408 | TargetInfo *Target) { |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 409 | |
| 410 | if (Ansi) // "The -ansi option is equivalent to -std=c89." |
| 411 | LangStd = lang_c89; |
| 412 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 413 | if (LangStd == lang_unspecified) { |
| 414 | // Based on the base language, pick one. |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 415 | switch (LK) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 416 | default: assert(0 && "Unknown base language"); |
| 417 | case langkind_c: |
| 418 | case langkind_c_cpp: |
| 419 | case langkind_objc: |
| 420 | case langkind_objc_cpp: |
| 421 | LangStd = lang_gnu99; |
| 422 | break; |
| 423 | case langkind_cxx: |
| 424 | case langkind_cxx_cpp: |
| 425 | case langkind_objcxx: |
| 426 | case langkind_objcxx_cpp: |
| 427 | LangStd = lang_gnucxx98; |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | switch (LangStd) { |
| 433 | default: assert(0 && "Unknown language standard!"); |
| 434 | |
| 435 | // Fall through from newer standards to older ones. This isn't really right. |
| 436 | // FIXME: Enable specifically the right features based on the language stds. |
| 437 | case lang_gnucxx0x: |
| 438 | case lang_cxx0x: |
| 439 | Options.CPlusPlus0x = 1; |
| 440 | // FALL THROUGH |
| 441 | case lang_gnucxx98: |
| 442 | case lang_cxx98: |
| 443 | Options.CPlusPlus = 1; |
| 444 | Options.CXXOperatorNames = !NoOperatorNames; |
Nate Begeman | ca89334 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 445 | Options.Boolean = 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 446 | // FALL THROUGH. |
| 447 | case lang_gnu99: |
| 448 | case lang_c99: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 449 | Options.C99 = 1; |
| 450 | Options.HexFloats = 1; |
| 451 | // FALL THROUGH. |
| 452 | case lang_gnu89: |
| 453 | Options.BCPLComment = 1; // Only for C99/C++. |
| 454 | // FALL THROUGH. |
| 455 | case lang_c94: |
Chris Lattner | 0297c76 | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 456 | Options.Digraphs = 1; // C94, C99, C++. |
| 457 | // FALL THROUGH. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 458 | case lang_c89: |
| 459 | break; |
| 460 | } |
Argiris Kirtzidis | b151955 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 461 | |
| 462 | if (Options.CPlusPlus) { |
| 463 | Options.C99 = 0; |
| 464 | Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x); |
| 465 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 466 | |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 467 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 468 | Options.ImplicitInt = 1; |
| 469 | else |
| 470 | Options.ImplicitInt = 0; |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 471 | |
| 472 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi |
| 473 | // is specified, or -std is set to a conforming mode. |
| 474 | Options.Trigraphs = LangStd < lang_gnu_START || Trigraphs ? 1 : 0; |
| 475 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 476 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 477 | Options.PascalStrings = PascalStrings; |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 478 | Options.Microsoft = MSExtensions; |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 479 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | e87cd98 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 480 | Options.LaxVectorConversions = LaxVectorConversions; |
Daniel Dunbar | 91692d9 | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 481 | Options.Exceptions = Exceptions; |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 482 | |
| 483 | if (NeXTRuntime) { |
| 484 | Options.NeXTRuntime = 1; |
| 485 | } else if (GNURuntime) { |
| 486 | Options.NeXTRuntime = 0; |
| 487 | } else { |
Daniel Dunbar | 3454295 | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 488 | Options.NeXTRuntime = Target->useNeXTRuntimeAsDefault(); |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 489 | } |
Daniel Dunbar | 7744d4a | 2008-10-10 00:20:52 +0000 | [diff] [blame] | 490 | |
| 491 | if (Options.CPlusPlus) |
| 492 | Options.Blocks = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 495 | static llvm::cl::opt<bool> |
| 496 | ObjCExclusiveGC("fobjc-gc-only", |
| 497 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 498 | "memory management")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 499 | |
| 500 | static llvm::cl::opt<bool> |
| 501 | ObjCEnableGC("fobjc-gc", |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 502 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 503 | |
| 504 | void InitializeGCMode(LangOptions &Options) { |
| 505 | if (ObjCExclusiveGC) |
| 506 | Options.setGCMode(LangOptions::GCOnly); |
| 507 | else if (ObjCEnableGC) |
| 508 | Options.setGCMode(LangOptions::HybridGC); |
| 509 | } |
| 510 | |
| 511 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 512 | //===----------------------------------------------------------------------===// |
| 513 | // Our DiagnosticClient implementation |
| 514 | //===----------------------------------------------------------------------===// |
| 515 | |
| 516 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 517 | static llvm::cl::opt<bool> |
| 518 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 519 | |
| 520 | static llvm::cl::opt<bool> |
Chris Lattner | 3a22b7c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 521 | SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings")); |
| 522 | |
| 523 | static llvm::cl::opt<bool> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 524 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 525 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 526 | |
| 527 | static llvm::cl::opt<bool> |
| 528 | ErrorOnExtensions("pedantic-errors", |
| 529 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 530 | |
| 531 | static llvm::cl::opt<bool> |
Daniel Dunbar | 4dbd857 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 532 | SuppressSystemWarnings("suppress-system-warnings", |
Daniel Dunbar | 35e5094 | 2008-09-30 20:49:53 +0000 | [diff] [blame] | 533 | llvm::cl::desc("Suppress warnings issued in system headers"), |
Daniel Dunbar | 4dbd857 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 534 | llvm::cl::init(true)); |
| 535 | |
| 536 | static llvm::cl::opt<bool> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 537 | WarnUnusedMacros("Wunused_macros", |
| 538 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 539 | |
Ted Kremenek | 24f59fb | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 540 | static llvm::cl::opt<bool> |
| 541 | WarnFloatEqual("Wfloat-equal", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 542 | llvm::cl::desc("Warn about equality comparisons of floating point values")); |
Ted Kremenek | 24f59fb | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 543 | |
Ted Kremenek | 4b57bc7 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 544 | static llvm::cl::opt<bool> |
| 545 | WarnNoFormatNonLiteral("Wno-format-nonliteral", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 546 | llvm::cl::desc("Do not warn about non-literal format strings")); |
Ted Kremenek | 4b57bc7 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 547 | |
Chris Lattner | a616ee3 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 548 | static llvm::cl::opt<bool> |
| 549 | WarnUndefMacros("Wundef", |
| 550 | llvm::cl::desc("Warn on use of undefined macros in #if's")); |
| 551 | |
Chris Lattner | dea31bf | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 552 | static llvm::cl::opt<bool> |
Ted Kremenek | c6e1669 | 2008-05-30 16:42:02 +0000 | [diff] [blame] | 553 | WarnImplicitFunctionDeclaration("Wimplicit-function-declaration", |
| 554 | llvm::cl::desc("Warn about uses of implicitly defined functions")); |
Chris Lattner | a616ee3 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 555 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 556 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 557 | /// current command line option settings. |
| 558 | static void InitializeDiagnostics(Diagnostic &Diags) { |
Chris Lattner | 3a22b7c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 559 | Diags.setIgnoreAllWarnings(SilenceWarnings); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 560 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 561 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 562 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 563 | |
Daniel Dunbar | 4dbd857 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 564 | // Suppress warnings in system headers unless requested not to. |
| 565 | Diags.setSuppressSystemWarnings(SuppressSystemWarnings); |
| 566 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 567 | // Silence the "macro is not used" warning unless requested. |
| 568 | if (!WarnUnusedMacros) |
| 569 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
Ted Kremenek | 24f59fb | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 570 | |
| 571 | // Silence "floating point comparison" warnings unless requested. |
| 572 | if (!WarnFloatEqual) |
| 573 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
Ted Kremenek | 4b57bc7 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 574 | |
| 575 | // Silence "format string is not a string literal" warnings if requested |
| 576 | if (WarnNoFormatNonLiteral) |
Ted Kremenek | f0a6ae0 | 2007-12-17 17:50:39 +0000 | [diff] [blame] | 577 | Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant, |
| 578 | diag::MAP_IGNORE); |
Chris Lattner | a616ee3 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 579 | if (!WarnUndefMacros) |
| 580 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE); |
Steve Naroff | 606f707 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 581 | |
Chris Lattner | dea31bf | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 582 | if (!WarnImplicitFunctionDeclaration) |
| 583 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 584 | diag::MAP_IGNORE); |
| 585 | |
Steve Naroff | 606f707 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 586 | if (MSExtensions) // MS allows unnamed struct/union fields. |
| 587 | Diags.setDiagnosticMapping(diag::w_no_declarators, diag::MAP_IGNORE); |
Chris Lattner | 057a2f5 | 2008-05-04 23:52:02 +0000 | [diff] [blame] | 588 | |
| 589 | // If -pedantic-errors is set, turn extensions that warn by default into |
| 590 | // errors. |
| 591 | if (ErrorOnExtensions) { |
| 592 | Diags.setDiagnosticMapping(diag::warn_hex_escape_too_large, |
| 593 | diag::MAP_ERROR); |
| 594 | Diags.setDiagnosticMapping(diag::warn_octal_escape_too_large, |
| 595 | diag::MAP_ERROR); |
| 596 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0118bb5 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 600 | // Analysis-specific options. |
| 601 | //===----------------------------------------------------------------------===// |
| 602 | |
| 603 | static llvm::cl::opt<std::string> |
| 604 | AnalyzeSpecificFunction("analyze-function", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 605 | llvm::cl::desc("Run analysis on specific function")); |
Ted Kremenek | 0118bb5 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 606 | |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 607 | static llvm::cl::opt<bool> |
Ted Kremenek | b1983ba | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 608 | TrimGraph("trim-egraph", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 609 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
Ted Kremenek | 5e1e05c | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 610 | |
Ted Kremenek | 0118bb5 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 611 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 612 | // Target Triple Processing. |
| 613 | //===----------------------------------------------------------------------===// |
| 614 | |
| 615 | static llvm::cl::opt<std::string> |
| 616 | TargetTriple("triple", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 617 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 618 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 619 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 620 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 621 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 622 | static llvm::cl::opt<std::string> |
| 623 | MacOSVersionMin("mmacosx-version-min", |
| 624 | llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)")); |
| 625 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 626 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 627 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
| 628 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 629 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 630 | if (DarwinDashIdx == std::string::npos) { |
| 631 | fprintf(stderr, |
| 632 | "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n"); |
| 633 | exit(1); |
| 634 | } |
| 635 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 636 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 637 | // Remove the number. |
| 638 | Triple.resize(DarwinNumIdx); |
| 639 | |
| 640 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 641 | bool MacOSVersionMinIsInvalid = false; |
| 642 | int VersionNum = 0; |
| 643 | if (MacOSVersionMin.size() < 4 || |
| 644 | MacOSVersionMin.substr(0, 3) != "10." || |
| 645 | !isdigit(MacOSVersionMin[3])) { |
| 646 | MacOSVersionMinIsInvalid = true; |
| 647 | } else { |
| 648 | const char *Start = MacOSVersionMin.c_str()+3; |
| 649 | char *End = 0; |
| 650 | VersionNum = (int)strtol(Start, &End, 10); |
| 651 | |
Chris Lattner | d376f6d | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 652 | // The version number must be in the range 0-9. |
| 653 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 654 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 655 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 656 | Triple += llvm::itostr(VersionNum+4); |
| 657 | |
Chris Lattner | d376f6d | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 658 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 659 | // Add the period piece (.7) to the end of the triple. This gives us |
| 660 | // something like ...-darwin8.7 |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 661 | Triple += End; |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 662 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 663 | MacOSVersionMinIsInvalid = true; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | if (MacOSVersionMinIsInvalid) { |
| 668 | fprintf(stderr, |
| 669 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
| 670 | MacOSVersionMin.c_str()); |
| 671 | exit(1); |
| 672 | } |
| 673 | } |
| 674 | |
| 675 | /// CreateTargetTriple - Process the various options that affect the target |
| 676 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 677 | static std::string CreateTargetTriple() { |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 678 | // Initialize base triple. If a -triple option has been specified, use |
| 679 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 210c0cc | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 680 | std::string Triple = TargetTriple; |
Daniel Dunbar | abe2e54 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 681 | if (Triple.empty()) { |
| 682 | Triple = LLVM_HOSTTRIPLE; |
| 683 | |
| 684 | // On darwin, we want to update the version to match that of the |
| 685 | // host. |
| 686 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 687 | if (DarwinDashIdx != std::string::npos) { |
| 688 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
| 689 | |
| 690 | Triple += llvm::sys::osVersion(); |
| 691 | } |
| 692 | } |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 693 | |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 694 | // If -arch foo was specified, remove the architecture from the triple we have |
| 695 | // so far and replace it with the specified one. |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 696 | if (!Arch.empty()) { |
| 697 | // Decompose the base triple into "arch" and suffix. |
| 698 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 700 | if (FirstDashIdx == std::string::npos) { |
| 701 | fprintf(stderr, |
| 702 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 703 | Triple.c_str()); |
| 704 | exit(1); |
| 705 | } |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 707 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 708 | } |
| 709 | |
| 710 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 711 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 712 | if (!MacOSVersionMin.empty()) |
| 713 | HandleMacOSVersionMin(Triple); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 715 | return Triple; |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 719 | // Preprocessor Initialization |
| 720 | //===----------------------------------------------------------------------===// |
| 721 | |
| 722 | // FIXME: Preprocessor builtins to support. |
| 723 | // -A... - Play with #assertions |
| 724 | // -undef - Undefine all predefined macros |
| 725 | |
| 726 | static llvm::cl::list<std::string> |
| 727 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 728 | llvm::cl::desc("Predefine the specified macro")); |
| 729 | static llvm::cl::list<std::string> |
| 730 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 731 | llvm::cl::desc("Undefine the specified macro")); |
| 732 | |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 733 | static llvm::cl::list<std::string> |
| 734 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 735 | llvm::cl::desc("Include file before parsing")); |
| 736 | |
| 737 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 738 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 739 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 740 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 741 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 742 | const char *Command = "#define ") { |
| 743 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 744 | if (const char *Equal = strchr(Macro, '=')) { |
| 745 | // Turn the = into ' '. |
| 746 | Buf.insert(Buf.end(), Macro, Equal); |
| 747 | Buf.push_back(' '); |
| 748 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 749 | } else { |
| 750 | // Push "macroname 1". |
| 751 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 752 | Buf.push_back(' '); |
| 753 | Buf.push_back('1'); |
| 754 | } |
| 755 | Buf.push_back('\n'); |
| 756 | } |
| 757 | |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 758 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 759 | /// predefines buffer. |
| 760 | static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){ |
| 761 | const char *Inc = "#include \""; |
| 762 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 763 | Buf.insert(Buf.end(), File.begin(), File.end()); |
| 764 | Buf.push_back('"'); |
| 765 | Buf.push_back('\n'); |
| 766 | } |
| 767 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 768 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 769 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 770 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 771 | /// |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 772 | static bool InitializePreprocessor(Preprocessor &PP, |
| 773 | bool InitializeSourceMgr, |
| 774 | const std::string &InFile) { |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 775 | FileManager &FileMgr = PP.getFileManager(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 776 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 777 | // Figure out where to get and map in the main file. |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 778 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 779 | |
| 780 | if (InitializeSourceMgr) { |
| 781 | if (InFile != "-") { |
| 782 | const FileEntry *File = FileMgr.getFile(InFile); |
| 783 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 784 | if (SourceMgr.getMainFileID() == 0) { |
| 785 | fprintf(stderr, "Error reading '%s'!\n",InFile.c_str()); |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 786 | return true; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 787 | } |
| 788 | } else { |
| 789 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 790 | if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); |
| 791 | if (SourceMgr.getMainFileID() == 0) { |
| 792 | fprintf(stderr, "Error reading standard input! Empty?\n"); |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 793 | return true; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 794 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 795 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 796 | } |
Sam Bishop | 61a2078 | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 797 | |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 798 | std::vector<char> PredefineBuffer; |
| 799 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 800 | // Add macros from the command line. |
Sam Bishop | 61a2078 | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 801 | unsigned d = 0, D = D_macros.size(); |
| 802 | unsigned u = 0, U = U_macros.size(); |
| 803 | while (d < D || u < U) { |
| 804 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 805 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 806 | else |
| 807 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 808 | } |
| 809 | |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 810 | // FIXME: Read any files specified by -imacros. |
| 811 | |
| 812 | // Add implicit #includes from -include. |
| 813 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 814 | AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 815 | |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 816 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 817 | PredefineBuffer.push_back(0); |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 818 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 819 | |
| 820 | // Once we've read this, we're done. |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 821 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | //===----------------------------------------------------------------------===// |
| 825 | // Preprocessor include path information. |
| 826 | //===----------------------------------------------------------------------===// |
| 827 | |
| 828 | // This tool exports a large number of command line options to control how the |
| 829 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 830 | // object takes a very simple interface: a list of directories to search for |
| 831 | // |
| 832 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | ae3dcc0 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 833 | // FIXME: -imultilib |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 834 | // |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 835 | // FIXME: -imacros |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 836 | |
| 837 | static llvm::cl::opt<bool> |
| 838 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 839 | |
| 840 | // Various command line options. These four add directories to each chain. |
| 841 | static llvm::cl::list<std::string> |
| 842 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 843 | llvm::cl::desc("Add directory to framework include search path")); |
| 844 | static llvm::cl::list<std::string> |
| 845 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 846 | llvm::cl::desc("Add directory to include search path")); |
| 847 | static llvm::cl::list<std::string> |
| 848 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 849 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 850 | static llvm::cl::list<std::string> |
| 851 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 852 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 853 | static llvm::cl::list<std::string> |
| 854 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 855 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 856 | |
| 857 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 858 | static llvm::cl::list<std::string> |
| 859 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 860 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 861 | static llvm::cl::list<std::string> |
| 862 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 863 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 864 | static llvm::cl::list<std::string> |
| 865 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 866 | llvm::cl::Prefix, |
| 867 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 868 | |
Chris Lattner | ae3dcc0 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 869 | static llvm::cl::opt<std::string> |
| 870 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 871 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 872 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 873 | // Finally, implement the code that groks the options above. |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 874 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 875 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 876 | /// HeaderSearch object. |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 877 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 878 | FileManager &FM, const LangOptions &Lang) { |
| 879 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 880 | |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 881 | // Handle -I... and -F... options, walking the lists in parallel. |
| 882 | unsigned Iidx = 0, Fidx = 0; |
| 883 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 884 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 885 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 886 | ++Iidx; |
| 887 | } else { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 888 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 889 | ++Fidx; |
| 890 | } |
| 891 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 892 | |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 893 | // Consume what's left from whatever list was longer. |
| 894 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 895 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 896 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 897 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 898 | |
| 899 | // Handle -idirafter... options. |
| 900 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 901 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 902 | false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 903 | |
| 904 | // Handle -iquote... options. |
| 905 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 906 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 907 | |
| 908 | // Handle -isystem... options. |
| 909 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 910 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 911 | |
| 912 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 913 | // parallel, processing the values in order of occurance to get the right |
| 914 | // prefixes. |
| 915 | { |
| 916 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 917 | unsigned iprefix_idx = 0; |
| 918 | unsigned iwithprefix_idx = 0; |
| 919 | unsigned iwithprefixbefore_idx = 0; |
| 920 | bool iprefix_done = iprefix_vals.empty(); |
| 921 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 922 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 923 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 924 | if (!iprefix_done && |
| 925 | (iwithprefix_done || |
| 926 | iprefix_vals.getPosition(iprefix_idx) < |
| 927 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 928 | (iwithprefixbefore_done || |
| 929 | iprefix_vals.getPosition(iprefix_idx) < |
| 930 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 931 | Prefix = iprefix_vals[iprefix_idx]; |
| 932 | ++iprefix_idx; |
| 933 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 934 | } else if (!iwithprefix_done && |
| 935 | (iwithprefixbefore_done || |
| 936 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 937 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 938 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 939 | InitHeaderSearch::System, false, false, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 940 | ++iwithprefix_idx; |
| 941 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 942 | } else { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 943 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 944 | InitHeaderSearch::Angled, false, false, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 945 | ++iwithprefixbefore_idx; |
| 946 | iwithprefixbefore_done = |
| 947 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 948 | } |
| 949 | } |
| 950 | } |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 951 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 952 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 953 | |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 954 | // Add the clang headers, which are relative to the clang driver. |
| 955 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 716a054 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 956 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 957 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 958 | if (!MainExecutablePath.isEmpty()) { |
| 959 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 960 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
| 961 | MainExecutablePath.appendComponent("Headers"); // Get foo/Headers |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 962 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 963 | false, false, false); |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 966 | if (!nostdinc) |
| 967 | Init.AddDefaultSystemIncludePaths(Lang); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 968 | |
| 969 | // Now that we have collected all of the include paths, merge them all |
| 970 | // together and tell the preprocessor about them. |
| 971 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 972 | Init.Realize(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 973 | } |
| 974 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 975 | //===----------------------------------------------------------------------===// |
| 976 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 977 | //===----------------------------------------------------------------------===// |
| 978 | |
| 979 | namespace { |
| 980 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 981 | const std::string &InFile; |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 982 | Diagnostic &Diags; |
| 983 | const LangOptions &LangInfo; |
| 984 | TargetInfo &Target; |
| 985 | SourceManager &SourceMgr; |
| 986 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 987 | bool InitializeSourceMgr; |
| 988 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 989 | public: |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 990 | DriverPreprocessorFactory(const std::string &infile, |
| 991 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 992 | TargetInfo &target, SourceManager &SM, |
| 993 | HeaderSearch &Headers) |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 994 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 995 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 996 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 997 | |
| 998 | virtual ~DriverPreprocessorFactory() {} |
| 999 | |
| 1000 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1001 | Preprocessor* PP = new Preprocessor(Diags, LangInfo, Target, |
| 1002 | SourceMgr, HeaderInfo); |
| 1003 | |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1004 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1005 | delete PP; |
| 1006 | return NULL; |
| 1007 | } |
| 1008 | |
| 1009 | InitializeSourceMgr = false; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1010 | return PP; |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1011 | } |
| 1012 | }; |
| 1013 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1015 | //===----------------------------------------------------------------------===// |
| 1016 | // Basic Parser driver |
| 1017 | //===----------------------------------------------------------------------===// |
| 1018 | |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1019 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1020 | Parser P(PP, *PA); |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1021 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1022 | |
| 1023 | // Parsing the specified input file. |
| 1024 | P.ParseTranslationUnit(); |
| 1025 | delete PA; |
| 1026 | } |
| 1027 | |
| 1028 | //===----------------------------------------------------------------------===// |
| 1029 | // Main driver |
| 1030 | //===----------------------------------------------------------------------===// |
| 1031 | |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1032 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
| 1033 | /// action. These consumers can operate on both ASTs that are freshly |
| 1034 | /// parsed from source files as well as those deserialized from Bitcode. |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1035 | static ASTConsumer* CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1036 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1037 | const LangOptions& LangOpts, |
Chris Lattner | 21f72d6 | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1038 | Preprocessor *PP, |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1039 | PreprocessorFactory *PPF) { |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1040 | switch (ProgAction) { |
| 1041 | default: |
| 1042 | return NULL; |
| 1043 | |
| 1044 | case ASTPrint: |
| 1045 | return CreateASTPrinter(); |
| 1046 | |
| 1047 | case ASTDump: |
| 1048 | return CreateASTDumper(); |
| 1049 | |
| 1050 | case ASTView: |
Ted Kremenek | 24612ae | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1051 | return CreateASTViewer(); |
| 1052 | |
| 1053 | case EmitHTML: |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1054 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | e972d85 | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1055 | |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1056 | case TestSerialization: |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1057 | return CreateSerializationTest(Diag, FileMgr); |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1058 | |
| 1059 | case EmitLLVM: |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1060 | case EmitBC: |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1061 | return CreateLLVMCodeGenWriter(ProgAction == EmitBC, Diag, LangOpts, |
| 1062 | InFile, OutputFile, GenerateDebugInfo); |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1063 | |
Ted Kremenek | bde3033 | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 1064 | case SerializeAST: |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1065 | // FIXME: Allow user to tailor where the file is written. |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1066 | return CreateASTSerializer(InFile, OutputFile, Diag); |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1067 | |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1068 | case RewriteObjC: |
Chris Lattner | 673f2bd | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 1069 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1070 | |
| 1071 | case RewriteBlocks: |
| 1072 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
Ted Kremenek | 81ea799 | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1073 | |
| 1074 | case RunAnalysis: |
| 1075 | assert (!AnalysisList.empty()); |
| 1076 | return CreateAnalysisConsumer(&AnalysisList[0], |
| 1077 | &AnalysisList[0]+AnalysisList.size(), |
| 1078 | Diag, PP, PPF, LangOpts, |
| 1079 | AnalyzeSpecificFunction, |
Ted Kremenek | cf26225 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 1080 | OutputFile, VisualizeEGDot, VisualizeEGUbi, |
| 1081 | TrimGraph, AnalyzeAll); |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1085 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1086 | /// |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1087 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
| 1088 | const std::string &InFile) { |
Ted Kremenek | 6856c63 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1089 | |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1090 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1091 | bool ClearSourceMgr = false; |
Ted Kremenek | 6856c63 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1092 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1093 | switch (ProgAction) { |
| 1094 | default: |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1095 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1096 | PP.getFileManager(), PP.getLangOptions(), |
| 1097 | &PP, &PPF)); |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (!Consumer) { |
| 1100 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1101 | HadErrors = true; |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1102 | return; |
| 1103 | } |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1104 | |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1105 | break; |
| 1106 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1107 | case DumpTokens: { // Token dump mode. |
| 1108 | Token Tok; |
| 1109 | // Start parsing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1110 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1111 | do { |
| 1112 | PP.Lex(Tok); |
| 1113 | PP.DumpToken(Tok, true); |
| 1114 | fprintf(stderr, "\n"); |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1115 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1116 | ClearSourceMgr = true; |
| 1117 | break; |
| 1118 | } |
| 1119 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
| 1120 | Token Tok; |
| 1121 | // Start parsing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1122 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1123 | do { |
| 1124 | PP.Lex(Tok); |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1125 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1126 | ClearSourceMgr = true; |
| 1127 | break; |
| 1128 | } |
| 1129 | |
| 1130 | case PrintPreprocessedInput: // -E mode. |
Chris Lattner | efd02a3 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1131 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1132 | ClearSourceMgr = true; |
| 1133 | break; |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1134 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1135 | case ParseNoop: // -parse-noop |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1136 | ParseFile(PP, new MinimalAction(PP.getIdentifierTable())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1137 | ClearSourceMgr = true; |
| 1138 | break; |
| 1139 | |
| 1140 | case ParsePrintCallbacks: |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1141 | ParseFile(PP, CreatePrintParserActionsAction(PP.getIdentifierTable())); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1142 | ClearSourceMgr = true; |
| 1143 | break; |
Ted Kremenek | 0841c70 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 1144 | |
Ted Kremenek | 6856c63 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1145 | case ParseSyntaxOnly: // -fsyntax-only |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1146 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1147 | break; |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1148 | |
| 1149 | case RewriteMacros: |
Chris Lattner | 302b062 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1150 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1151 | ClearSourceMgr = true; |
| 1152 | break; |
Chris Lattner | 129758d | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1153 | } |
Ted Kremenek | 6856c63 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1154 | |
| 1155 | if (Consumer) { |
Ted Kremenek | 56b7086 | 2007-09-26 20:14:22 +0000 | [diff] [blame] | 1156 | if (VerifyDiagnostics) |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1157 | exit(CheckASTConsumer(PP, Consumer.get())); |
Chris Lattner | 8593cbf | 2007-11-03 06:24:16 +0000 | [diff] [blame] | 1158 | |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1159 | ParseAST(PP, Consumer.get(), Stats); |
Daniel Dunbar | ab3288e | 2008-10-05 01:38:39 +0000 | [diff] [blame] | 1160 | } else { |
| 1161 | if (VerifyDiagnostics) |
| 1162 | exit(CheckDiagnostics(PP)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1163 | } |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1164 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1165 | if (Stats) { |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1166 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1167 | PP.PrintStats(); |
| 1168 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1169 | PP.getHeaderSearchInfo().PrintStats(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1170 | if (ClearSourceMgr) |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1171 | PP.getSourceManager().PrintStats(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1172 | fprintf(stderr, "\n"); |
| 1173 | } |
| 1174 | |
| 1175 | // For a multi-file compilation, some things are ok with nuking the source |
| 1176 | // manager tables, other require stable fileid/macroid's across multiple |
| 1177 | // files. |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1178 | if (ClearSourceMgr) |
| 1179 | PP.getSourceManager().clearIDTables(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1182 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1183 | FileManager& FileMgr) { |
| 1184 | |
| 1185 | if (VerifyDiagnostics) { |
| 1186 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1187 | exit (1); |
| 1188 | } |
| 1189 | |
| 1190 | llvm::sys::Path Filename(InFile); |
| 1191 | |
| 1192 | if (!Filename.isValid()) { |
| 1193 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1194 | exit (1); |
| 1195 | } |
| 1196 | |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1197 | llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr)); |
Ted Kremenek | 2bd4241 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1198 | |
| 1199 | if (!TU) { |
| 1200 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1201 | InFile.c_str()); |
| 1202 | exit (1); |
| 1203 | } |
| 1204 | |
Ted Kremenek | ab74937 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1205 | // Observe that we use the source file name stored in the deserialized |
| 1206 | // translation unit, rather than InFile. |
Ted Kremenek | 0c7cd7a | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1207 | llvm::OwningPtr<ASTConsumer> |
Ted Kremenek | 842126e | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1208 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(), |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1209 | 0, 0)); |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1210 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1211 | if (!Consumer) { |
| 1212 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1213 | exit (1); |
| 1214 | } |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1215 | |
Ted Kremenek | 863b01f | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1216 | Consumer->Initialize(TU->getContext()); |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1218 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1219 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
| 1220 | Consumer->HandleTopLevelDecl(*I); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1224 | static llvm::cl::list<std::string> |
| 1225 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1226 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1227 | static bool isSerializedFile(const std::string& InFile) { |
| 1228 | if (InFile.size() < 4) |
| 1229 | return false; |
| 1230 | |
| 1231 | const char* s = InFile.c_str()+InFile.size()-4; |
| 1232 | |
| 1233 | return s[0] == '.' && |
| 1234 | s[1] == 'a' && |
| 1235 | s[2] == 's' && |
| 1236 | s[3] == 't'; |
| 1237 | } |
| 1238 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1239 | |
| 1240 | int main(int argc, char **argv) { |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1241 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1242 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 1243 | |
| 1244 | // If no input was specified, read from stdin. |
| 1245 | if (InputFilenames.empty()) |
| 1246 | InputFilenames.push_back("-"); |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1247 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1248 | // Create a file manager object to provide access to and cache the filesystem. |
| 1249 | FileManager FileMgr; |
| 1250 | |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1251 | // Create the diagnostic client for reporting errors or for |
| 1252 | // implementing -verify. |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1253 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1254 | |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1255 | if (!VerifyDiagnostics) { |
| 1256 | // Print diagnostics to stderr by default. |
| 1257 | TextDiagClient = new TextDiagnosticPrinter(!NoShowColumn, |
| 1258 | !NoCaretDiagnostics); |
| 1259 | } else { |
| 1260 | // When checking diagnostics, just buffer them up. |
| 1261 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1262 | |
| 1263 | if (InputFilenames.size() != 1) { |
| 1264 | fprintf(stderr, |
| 1265 | "-verify only works on single input files for now.\n"); |
| 1266 | return 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1267 | } |
| 1268 | } |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1269 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1270 | // Configure our handling of diagnostics. |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1271 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1272 | Diagnostic Diags(DiagClient.get()); |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1273 | InitializeDiagnostics(Diags); |
| 1274 | |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1275 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1276 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1277 | if (I_dirs[i] == "-") { |
Ted Kremenek | de79f79 | 2007-12-11 22:57:35 +0000 | [diff] [blame] | 1278 | Diags.Report(diag::err_pp_I_dash_not_supported); |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1279 | I_dirs.erase(I_dirs.begin()+i); |
| 1280 | --i; |
| 1281 | } |
| 1282 | } |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1283 | |
| 1284 | // Get information about the target being compiled for. |
| 1285 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | ec6c525 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1286 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1287 | |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1288 | if (Target == 0) { |
| 1289 | fprintf(stderr, "Sorry, I don't know what target this is: %s\n", |
| 1290 | Triple.c_str()); |
| 1291 | fprintf(stderr, "Please use -triple or -arch.\n"); |
| 1292 | exit(1); |
| 1293 | } |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1294 | |
Ted Kremenek | 81ea799 | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1295 | // Are we invoking one or more source analyses? |
| 1296 | if (!AnalysisList.empty() && ProgAction == ParseSyntaxOnly) |
| 1297 | ProgAction = RunAnalysis; |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1298 | |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1299 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1300 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1301 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1302 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1303 | |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1304 | if (isSerializedFile(InFile)) { |
| 1305 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1306 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1307 | } |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1308 | else { |
| 1309 | /// Create a SourceManager object. This tracks and owns all the file |
| 1310 | /// buffers allocated to a translation unit. |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1311 | if (!SourceMgr) |
| 1312 | SourceMgr.reset(new SourceManager()); |
| 1313 | else |
| 1314 | SourceMgr->clearIDTables(); |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1315 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1316 | // Initialize language options, inferring file types from input filenames. |
| 1317 | LangOptions LangInfo; |
| 1318 | InitializeBaseLanguage(); |
| 1319 | LangKind LK = GetLanguage(InFile); |
| 1320 | InitializeLangOptions(LangInfo, LK); |
Daniel Dunbar | 3454295 | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 1321 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 1322 | InitializeGCMode(LangInfo); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1323 | |
| 1324 | // Process the -I options and set them in the HeaderInfo. |
| 1325 | HeaderSearch HeaderInfo(FileMgr); |
Ted Kremenek | 649465c | 2008-06-06 01:47:30 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1327 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1328 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1329 | // Set up the preprocessor with these options. |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1330 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1331 | *SourceMgr.get(), HeaderInfo); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1332 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1333 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1334 | |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1335 | if (!PP) |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1336 | continue; |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1337 | |
| 1338 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1339 | // always reset to using TextDiagClient. |
| 1340 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1341 | |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1342 | if (!HTMLDiag.empty()) { |
| 1343 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1344 | &PPFactory)); |
| 1345 | Diags.setClient(TmpClient.get()); |
| 1346 | } |
| 1347 | else |
| 1348 | Diags.setClient(TextDiagClient); |
| 1349 | |
| 1350 | // Process the source file. |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1351 | ProcessInputFile(*PP, PPFactory, InFile); |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1352 | HeaderInfo.ClearFileInfo(); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1353 | |
| 1354 | if (Stats) |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1355 | SourceMgr->PrintStats(); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1356 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1357 | } |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1358 | |
Ted Kremenek | ec6c525 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1359 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1360 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1361 | (NumDiagnostics == 1 ? "" : "s")); |
| 1362 | |
| 1363 | if (Stats) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1364 | FileMgr.PrintStats(); |
| 1365 | fprintf(stderr, "\n"); |
| 1366 | } |
| 1367 | |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1368 | return HadErrors || (Diags.getNumErrors() != 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1369 | } |