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" |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 27 | #include "clang/Driver/CompileOptions.h" |
Ted Kremenek | ad99dbf | 2008-11-03 22:31:48 +0000 | [diff] [blame] | 28 | #include "clang/Driver/PathDiagnosticClients.h" |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 29 | #include "clang/Driver/InitHeaderSearch.h" |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 30 | #include "clang/Driver/TextDiagnosticBuffer.h" |
| 31 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 32 | #include "clang/Analysis/PathDiagnostic.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 33 | #include "clang/AST/Decl.h" |
Ted Kremenek | 77cda50 | 2007-12-18 21:34:28 +0000 | [diff] [blame] | 34 | #include "clang/AST/TranslationUnit.h" |
Chris Lattner | 8ee3c03 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 35 | #include "clang/CodeGen/ModuleBuilder.h" |
Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 36 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 37 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | 556beb7 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 38 | #include "clang/AST/ASTConsumer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | #include "clang/Parse/Parser.h" |
| 40 | #include "clang/Lex/HeaderSearch.h" |
| 41 | #include "clang/Basic/FileManager.h" |
| 42 | #include "clang/Basic/SourceManager.h" |
| 43 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/StringExtras.h" |
| 47 | #include "llvm/Config/config.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 49 | #include "llvm/Support/ManagedStatic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 51 | #include "llvm/Support/PluginLoader.h" |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 52 | #include "llvm/System/Host.h" |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 53 | #include "llvm/System/Path.h" |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 54 | #include "llvm/System/Signals.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | using namespace clang; |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // Global options. |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 61 | static bool HadErrors = false; |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 62 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | static llvm::cl::opt<bool> |
| 64 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 65 | static llvm::cl::opt<bool> |
Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 66 | Stats("print-stats", |
| 67 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 68 | static llvm::cl::opt<bool> |
| 69 | DisableFree("disable-free", |
| 70 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 71 | llvm::cl::init(false)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | |
| 73 | enum ProgActions { |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 74 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 75 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 76 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 77 | RewriteTest, // Rewriter playground |
Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 78 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 79 | EmitAssembly, // Emit a .s file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 81 | EmitBC, // Emit a .bc file. |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 82 | SerializeAST, // Emit a .ast file. |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 83 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 84 | ASTPrint, // Parse ASTs and print them. |
| 85 | ASTDump, // Parse ASTs and dump them. |
| 86 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 87 | PrintDeclContext, // Print DeclContext and their Decls. |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 88 | TestSerialization, // Run experimental serialization code. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | ParsePrintCallbacks, // Parse and print each callback. |
| 90 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 91 | ParseNoop, // Parse with noop callbacks. |
| 92 | RunPreprocessorOnly, // Just lex, no output. |
| 93 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 94 | DumpTokens, // Dump out preprocessed tokens. |
| 95 | DumpRawTokens, // Dump out raw tokens. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 96 | RunAnalysis, // Run one or more source code analyses. |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 97 | GeneratePCH, // Generate precompiled header. |
| 98 | InheritanceView // View C++ inheritance for a specified class. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | static llvm::cl::opt<ProgActions> |
| 102 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 103 | llvm::cl::init(ParseSyntaxOnly), |
| 104 | llvm::cl::values( |
| 105 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 106 | "Just run preprocessor, no output (for timings)"), |
| 107 | clEnumValN(PrintPreprocessedInput, "E", |
| 108 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 109 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 110 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 111 | clEnumValN(RunAnalysis, "analyze", |
| 112 | "Run static analysis engine"), |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 113 | clEnumValN(DumpTokens, "dump-tokens", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | "Run preprocessor, dump internal rep of tokens"), |
| 115 | clEnumValN(ParseNoop, "parse-noop", |
| 116 | "Run parser with noop callbacks (for timings)"), |
| 117 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 118 | "Run parser and perform semantic analysis"), |
| 119 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 120 | "Run parser and print each callback invoked"), |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 121 | clEnumValN(EmitHTML, "emit-html", |
| 122 | "Output input source as HTML"), |
Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 123 | clEnumValN(ASTPrint, "ast-print", |
| 124 | "Build ASTs and then pretty-print them"), |
| 125 | clEnumValN(ASTDump, "ast-dump", |
| 126 | "Build ASTs and then debug dump them"), |
Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 127 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 128 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 129 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
| 130 | "Print DeclContexts and their Decls."), |
Ted Kremenek | bfa82c4 | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 131 | clEnumValN(TestSerialization, "test-pickling", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 132 | "Run prototype serialization code"), |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 133 | clEnumValN(EmitAssembly, "S", |
| 134 | "Emit native assembly code"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 136 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 137 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 138 | "Build ASTs then convert to LLVM, emit .bc file"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 139 | clEnumValN(SerializeAST, "serialize", |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 140 | "Build ASTs and emit .ast file"), |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 141 | clEnumValN(RewriteTest, "rewrite-test", |
| 142 | "Rewriter playground"), |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 143 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 144 | "Rewrite ObjC into C (code rewriter example)"), |
| 145 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 146 | "Expand macros without full preprocessing"), |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 147 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 148 | "Rewrite Blocks to C"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | clEnumValEnd)); |
| 150 | |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 151 | |
| 152 | static llvm::cl::opt<std::string> |
| 153 | OutputFile("o", |
Ted Kremenek | 50b5641 | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 154 | llvm::cl::value_desc("path"), |
Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 155 | llvm::cl::desc("Specify output file (for --serialize, this is a directory)")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 156 | |
| 157 | //===----------------------------------------------------------------------===// |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 158 | // Code Generator Options |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | static llvm::cl::opt<bool> |
| 161 | GenerateDebugInfo("g", |
| 162 | llvm::cl::desc("Generate source level debug information")); |
| 163 | |
Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 164 | |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | // PTH. |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | |
| 169 | static llvm::cl::opt<std::string> |
| 170 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 171 | llvm::cl::desc("Use specified token cache file")); |
| 172 | |
Sanjiv Gupta | e8b9f5b | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 173 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 174 | // Diagnostic Options |
| 175 | //===----------------------------------------------------------------------===// |
| 176 | |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 177 | static llvm::cl::opt<bool> |
| 178 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 179 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 41193e4 | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 181 | static llvm::cl::opt<std::string> |
| 182 | HTMLDiag("html-diags", |
| 183 | llvm::cl::desc("Generate HTML to report diagnostics"), |
| 184 | llvm::cl::value_desc("HTML directory")); |
| 185 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 186 | static llvm::cl::opt<bool> |
| 187 | NoShowColumn("fno-show-column", |
| 188 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 189 | |
| 190 | static llvm::cl::opt<bool> |
Chris Lattner | 65f5e64 | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 191 | NoShowLocation("fno-show-source-location", |
| 192 | llvm::cl::desc("Do not include source location information with" |
| 193 | " diagnostics")); |
| 194 | |
| 195 | static llvm::cl::opt<bool> |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 196 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 197 | llvm::cl::desc("Do not include source line and caret with" |
| 198 | " diagnostics")); |
| 199 | |
| 200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 202 | // C++ Visualization. |
| 203 | //===----------------------------------------------------------------------===// |
| 204 | |
| 205 | static llvm::cl::opt<std::string> |
| 206 | InheritanceViewCls("cxx-inheritance-view", |
| 207 | llvm::cl::value_desc("class name"), |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 208 | llvm::cl::desc("View C++ inheritance for a specified class")); |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 209 | |
| 210 | //===----------------------------------------------------------------------===// |
| 211 | // Analyzer Options. |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 212 | //===----------------------------------------------------------------------===// |
| 213 | |
| 214 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 215 | VisualizeEGDot("analyzer-viz-egraph-graphviz", |
| 216 | llvm::cl::desc("Display exploded graph using GraphViz")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 217 | |
| 218 | static llvm::cl::opt<bool> |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 219 | VisualizeEGUbi("analyzer-viz-egraph-ubigraph", |
| 220 | llvm::cl::desc("Display exploded graph using Ubigraph")); |
| 221 | |
| 222 | static llvm::cl::opt<bool> |
| 223 | AnalyzeAll("analyzer-opt-analyze-headers", |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 224 | llvm::cl::desc("Force the static analyzer to analyze " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 225 | "functions defined in header files")); |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 226 | |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 227 | static llvm::cl::opt<bool> |
| 228 | AnalyzerDisplayProgress("analyzer-display-progress", |
| 229 | llvm::cl::desc("Emit verbose output about the analyzer's progress.")); |
| 230 | |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 231 | static llvm::cl::list<Analyses> |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 232 | AnalysisList(llvm::cl::desc("SCA Checks/Analyses:"), |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 233 | llvm::cl::values( |
Ted Kremenek | f7f3c20 | 2008-07-15 00:46:02 +0000 | [diff] [blame] | 234 | #define ANALYSIS(NAME, CMDFLAG, DESC, SCOPE)\ |
Ted Kremenek | fb9a48c | 2008-07-14 23:41:13 +0000 | [diff] [blame] | 235 | clEnumValN(NAME, CMDFLAG, DESC), |
| 236 | #include "Analyses.def" |
| 237 | clEnumValEnd)); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 95c7b00 | 2008-10-24 01:04:59 +0000 | [diff] [blame] | 239 | static llvm::cl::opt<AnalysisStores> |
| 240 | AnalysisStoreOpt(llvm::cl::desc("SCA Low-Level Options (Store):"), |
| 241 | llvm::cl::init(BasicStoreModel), |
| 242 | llvm::cl::values( |
| 243 | #define ANALYSIS_STORE(NAME, CMDFLAG, DESC)\ |
| 244 | clEnumValN(NAME##Model, "analyzer-store-" CMDFLAG, DESC), |
| 245 | #include "Analyses.def" |
| 246 | clEnumValEnd)); |
| 247 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 248 | static llvm::cl::opt<AnalysisDiagClients> |
| 249 | AnalysisDiagOpt(llvm::cl::desc("SCA Output Options:"), |
| 250 | llvm::cl::init(PD_HTML), |
| 251 | llvm::cl::values( |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 252 | #define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATFN, AUTOCREATE)\ |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 253 | clEnumValN(PD_##NAME, "analyzer-output-" CMDFLAG, DESC), |
| 254 | #include "Analyses.def" |
| 255 | clEnumValEnd)); |
| 256 | |
Ted Kremenek | 55af98c | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 257 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | // Language Options |
| 259 | //===----------------------------------------------------------------------===// |
| 260 | |
| 261 | enum LangKind { |
| 262 | langkind_unspecified, |
| 263 | langkind_c, |
| 264 | langkind_c_cpp, |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 265 | langkind_asm_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 266 | langkind_c_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | langkind_cxx, |
| 268 | langkind_cxx_cpp, |
| 269 | langkind_objc, |
| 270 | langkind_objc_cpp, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 271 | langkind_objc_pch, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 272 | langkind_objcxx, |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 273 | langkind_objcxx_cpp, |
| 274 | langkind_objcxx_pch |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | /* TODO: GCC also accepts: |
| 278 | c-header c++-header objective-c-header objective-c++-header |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 279 | assembler |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | ada, f77*, ratfor (!), f95, java, treelang |
| 281 | */ |
| 282 | static llvm::cl::opt<LangKind> |
| 283 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 284 | llvm::cl::init(langkind_unspecified), |
| 285 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 286 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 287 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 288 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
Daniel Dunbar | d2ea386 | 2009-01-29 23:50:47 +0000 | [diff] [blame] | 289 | clEnumValN(langkind_c_cpp, "cpp-output", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | "Preprocessed C"), |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 291 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 292 | "Preprocessed asm"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 294 | "Preprocessed C++"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 295 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 296 | "Preprocessed Objective C"), |
| 297 | clEnumValN(langkind_objcxx_cpp,"objective-c++-cpp-output", |
| 298 | "Preprocessed Objective C++"), |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 299 | clEnumValN(langkind_c_pch,"c-header", |
| 300 | "Precompiled C header"), |
| 301 | clEnumValN(langkind_objc_pch, "objective-c-header", |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 302 | "Precompiled Objective-C header"), |
| 303 | clEnumValN(langkind_objcxx_pch, "objective-c++-header", |
| 304 | "Precompiled Objective-C++ header"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | clEnumValEnd)); |
| 306 | |
| 307 | static llvm::cl::opt<bool> |
| 308 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 309 | llvm::cl::Hidden); |
| 310 | static llvm::cl::opt<bool> |
| 311 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 312 | llvm::cl::Hidden); |
| 313 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 314 | /// InitializeBaseLanguage - Handle the -x foo options. |
| 315 | static void InitializeBaseLanguage() { |
| 316 | if (LangObjC) |
| 317 | BaseLang = langkind_objc; |
| 318 | else if (LangObjCXX) |
| 319 | BaseLang = langkind_objcxx; |
| 320 | } |
| 321 | |
| 322 | static LangKind GetLanguage(const std::string &Filename) { |
| 323 | if (BaseLang != langkind_unspecified) |
| 324 | return BaseLang; |
| 325 | |
| 326 | std::string::size_type DotPos = Filename.rfind('.'); |
| 327 | |
| 328 | if (DotPos == std::string::npos) { |
| 329 | BaseLang = langkind_c; // Default to C if no extension. |
Chris Lattner | 9b2f6c4 | 2008-01-04 19:12:28 +0000 | [diff] [blame] | 330 | return langkind_c; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 333 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 334 | // C header: .h |
| 335 | // C++ header: .hh or .H; |
| 336 | // assembler no preprocessing: .s |
| 337 | // assembler: .S |
| 338 | if (Ext == "c") |
| 339 | return langkind_c; |
Chris Lattner | 1b450b0 | 2008-10-28 20:33:42 +0000 | [diff] [blame] | 340 | else if (Ext == "S") |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 341 | return langkind_asm_cpp; |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 342 | else if (Ext == "i") |
| 343 | return langkind_c_cpp; |
| 344 | else if (Ext == "ii") |
| 345 | return langkind_cxx_cpp; |
| 346 | else if (Ext == "m") |
| 347 | return langkind_objc; |
| 348 | else if (Ext == "mi") |
| 349 | return langkind_objc_cpp; |
| 350 | else if (Ext == "mm" || Ext == "M") |
| 351 | return langkind_objcxx; |
| 352 | else if (Ext == "mii") |
| 353 | return langkind_objcxx_cpp; |
| 354 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 355 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 356 | return langkind_cxx; |
| 357 | else |
| 358 | return langkind_c; |
| 359 | } |
| 360 | |
| 361 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 362 | static void InitializeCOptions(LangOptions &Options) { |
| 363 | // Do nothing. |
| 364 | } |
| 365 | |
| 366 | static void InitializeObjCOptions(LangOptions &Options) { |
| 367 | Options.ObjC1 = Options.ObjC2 = 1; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | static bool InitializeLangOptions(LangOptions &Options, LangKind LK){ |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 372 | // FIXME: implement -fpreprocessed mode. |
| 373 | bool NoPreprocess = false; |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 374 | bool PCH = false; |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 375 | |
| 376 | // Test for 'PCH'. |
| 377 | switch (LK) { |
| 378 | default: |
| 379 | break; |
| 380 | case langkind_c_pch: |
| 381 | LK = langkind_c; |
| 382 | PCH = true; |
| 383 | break; |
| 384 | case langkind_objc_pch: |
| 385 | LK = langkind_objc; |
| 386 | PCH = true; |
| 387 | break; |
| 388 | case langkind_objcxx_pch: |
| 389 | LK = langkind_objcxx; |
| 390 | PCH = true; |
| 391 | break; |
| 392 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 394 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | default: assert(0 && "Unknown language kind!"); |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 396 | case langkind_asm_cpp: |
Daniel Dunbar | c157145 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 397 | Options.AsmPreprocessor = 1; |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 398 | // FALLTHROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 399 | case langkind_c_cpp: |
| 400 | NoPreprocess = true; |
| 401 | // FALLTHROUGH |
| 402 | case langkind_c: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 403 | InitializeCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 404 | break; |
| 405 | case langkind_cxx_cpp: |
| 406 | NoPreprocess = true; |
| 407 | // FALLTHROUGH |
| 408 | case langkind_cxx: |
| 409 | Options.CPlusPlus = 1; |
| 410 | break; |
| 411 | case langkind_objc_cpp: |
| 412 | NoPreprocess = true; |
| 413 | // FALLTHROUGH |
| 414 | case langkind_objc: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 415 | InitializeObjCOptions(Options); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | break; |
| 417 | case langkind_objcxx_cpp: |
| 418 | NoPreprocess = true; |
| 419 | // FALLTHROUGH |
| 420 | case langkind_objcxx: |
| 421 | Options.ObjC1 = Options.ObjC2 = 1; |
| 422 | Options.CPlusPlus = 1; |
| 423 | break; |
| 424 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 425 | |
| 426 | return PCH; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /// LangStds - Language standards we support. |
| 430 | enum LangStds { |
| 431 | lang_unspecified, |
| 432 | lang_c89, lang_c94, lang_c99, |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 433 | lang_gnu_START, |
| 434 | lang_gnu89 = lang_gnu_START, lang_gnu99, |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 435 | lang_cxx98, lang_gnucxx98, |
| 436 | lang_cxx0x, lang_gnucxx0x |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 437 | }; |
| 438 | |
| 439 | static llvm::cl::opt<LangStds> |
| 440 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 441 | llvm::cl::init(lang_unspecified), |
| 442 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 443 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 444 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 445 | clEnumValN(lang_c94, "iso9899:199409", |
| 446 | "ISO C 1990 with amendment 1"), |
| 447 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
| 448 | // clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
| 449 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
| 450 | // clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
| 451 | clEnumValN(lang_gnu89, "gnu89", |
| 452 | "ISO C 1990 with GNU extensions (default for C)"), |
| 453 | clEnumValN(lang_gnu99, "gnu99", |
| 454 | "ISO C 1999 with GNU extensions"), |
| 455 | clEnumValN(lang_gnu99, "gnu9x", |
| 456 | "ISO C 1999 with GNU extensions"), |
| 457 | clEnumValN(lang_cxx98, "c++98", |
| 458 | "ISO C++ 1998 with amendments"), |
| 459 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 460 | "ISO C++ 1998 with amendments and GNU " |
| 461 | "extensions (default for C++)"), |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 462 | clEnumValN(lang_cxx0x, "c++0x", |
| 463 | "Upcoming ISO C++ 200x with amendments"), |
| 464 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 465 | "Upcoming ISO C++ 200x with amendments and GNU " |
| 466 | "extensions (default for C++)"), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 467 | clEnumValEnd)); |
| 468 | |
| 469 | static llvm::cl::opt<bool> |
| 470 | NoOperatorNames("fno-operator-names", |
| 471 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 472 | "synonyms for operators")); |
| 473 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 474 | static llvm::cl::opt<bool> |
| 475 | PascalStrings("fpascal-strings", |
| 476 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 477 | "string literals")); |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 478 | |
| 479 | static llvm::cl::opt<bool> |
| 480 | MSExtensions("fms-extensions", |
| 481 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 482 | "Microsoft header files ")); |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 483 | |
| 484 | static llvm::cl::opt<bool> |
| 485 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 486 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | 695dbb6 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 487 | |
| 488 | static llvm::cl::opt<bool> |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame^] | 489 | NoLaxVectorConversions("fnolax-vector-conversions", |
| 490 | llvm::cl::desc("Disallow implicit conversions between " |
| 491 | "vectors with a different number of " |
| 492 | "elements or different element types")); |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 493 | static llvm::cl::opt<bool> |
Mike Stump | b55cc63 | 2009-01-30 08:22:07 +0000 | [diff] [blame] | 494 | EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature"), llvm::cl::ValueDisallowed); |
| 495 | |
| 496 | static llvm::cl::inverse_opt |
| 497 | DisableBlocks("fno-blocks", llvm::cl::opposite_of(EnableBlocks), llvm::cl::ValueDisallowed); |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 498 | |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 499 | static llvm::cl::opt<bool> |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 500 | ObjCNonFragileABI("fobjc-nonfragile-abi", llvm::cl::desc("enable objective-c's nonfragile abi")); |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 501 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 502 | |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 503 | // FIXME: This (and all GCC -f options) really come in -f... and |
| 504 | // -fno-... forms, and additionally support automagic behavior when |
| 505 | // they are not defined. For example, -fexceptions defaults to on or |
| 506 | // off depending on the language. We should support this behavior in |
| 507 | // some form (perhaps just add a facility for distinguishing when an |
| 508 | // has its default value from when it has been set to its default |
| 509 | // value). |
| 510 | static llvm::cl::opt<bool> |
| 511 | Exceptions("fexceptions", |
| 512 | llvm::cl::desc("Enable support for exception handling.")); |
| 513 | |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 514 | static llvm::cl::opt<bool> |
| 515 | GNURuntime("fgnu-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 516 | llvm::cl::desc("Generate output compatible with the standard GNU " |
| 517 | "Objective-C runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 518 | |
| 519 | static llvm::cl::opt<bool> |
| 520 | NeXTRuntime("fnext-runtime", |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 521 | llvm::cl::desc("Generate output compatible with the NeXT " |
| 522 | "runtime.")); |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 523 | |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 524 | |
| 525 | |
| 526 | static llvm::cl::opt<bool> |
| 527 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences.")); |
| 528 | |
| 529 | static llvm::cl::opt<bool> |
| 530 | Ansi("ansi", llvm::cl::desc("Equivalent to specifying -std=c89.")); |
| 531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | // FIXME: add: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | // -fdollars-in-identifiers |
Daniel Dunbar | dcb4a1a | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 534 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
| 535 | TargetInfo *Target) { |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 536 | // Allow the target to set the default the langauge options as it sees fit. |
| 537 | Target->getDefaultLangOptions(Options); |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 538 | |
| 539 | if (Ansi) // "The -ansi option is equivalent to -std=c89." |
| 540 | LangStd = lang_c89; |
| 541 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 542 | if (LangStd == lang_unspecified) { |
| 543 | // Based on the base language, pick one. |
Ted Kremenek | 8904f15 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 544 | switch (LK) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 545 | default: assert(0 && "Unknown base language"); |
| 546 | case langkind_c: |
Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 547 | case langkind_asm_cpp: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 548 | case langkind_c_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 549 | case langkind_c_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | case langkind_objc: |
| 551 | case langkind_objc_cpp: |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 552 | case langkind_objc_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | LangStd = lang_gnu99; |
| 554 | break; |
| 555 | case langkind_cxx: |
| 556 | case langkind_cxx_cpp: |
| 557 | case langkind_objcxx: |
| 558 | case langkind_objcxx_cpp: |
Ted Kremenek | f890191 | 2009-01-09 00:38:08 +0000 | [diff] [blame] | 559 | case langkind_objcxx_pch: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | LangStd = lang_gnucxx98; |
| 561 | break; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | switch (LangStd) { |
| 566 | default: assert(0 && "Unknown language standard!"); |
| 567 | |
| 568 | // Fall through from newer standards to older ones. This isn't really right. |
| 569 | // FIXME: Enable specifically the right features based on the language stds. |
Chris Lattner | d4b80f1 | 2007-07-16 04:18:29 +0000 | [diff] [blame] | 570 | case lang_gnucxx0x: |
| 571 | case lang_cxx0x: |
| 572 | Options.CPlusPlus0x = 1; |
| 573 | // FALL THROUGH |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 574 | case lang_gnucxx98: |
| 575 | case lang_cxx98: |
| 576 | Options.CPlusPlus = 1; |
| 577 | Options.CXXOperatorNames = !NoOperatorNames; |
Nate Begeman | 8aebcb7 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 578 | Options.Boolean = 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 579 | // FALL THROUGH. |
| 580 | case lang_gnu99: |
| 581 | case lang_c99: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 582 | Options.C99 = 1; |
| 583 | Options.HexFloats = 1; |
| 584 | // FALL THROUGH. |
| 585 | case lang_gnu89: |
| 586 | Options.BCPLComment = 1; // Only for C99/C++. |
| 587 | // FALL THROUGH. |
| 588 | case lang_c94: |
Chris Lattner | 3426b9b | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 589 | Options.Digraphs = 1; // C94, C99, C++. |
| 590 | // FALL THROUGH. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 591 | case lang_c89: |
| 592 | break; |
| 593 | } |
Argyrios Kyrtzidis | d146552 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 594 | |
| 595 | if (Options.CPlusPlus) { |
| 596 | Options.C99 = 0; |
| 597 | Options.HexFloats = (LangStd == lang_gnucxx98 || LangStd==lang_gnucxx0x); |
| 598 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 599 | |
Chris Lattner | d658b56 | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 600 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 601 | Options.ImplicitInt = 1; |
| 602 | else |
| 603 | Options.ImplicitInt = 0; |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 604 | |
| 605 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs or -ansi |
| 606 | // is specified, or -std is set to a conforming mode. |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 607 | Options.Trigraphs = LangStd < lang_gnu_START; |
| 608 | if (Trigraphs.getPosition()) |
| 609 | Options.Trigraphs = Trigraphs; // Command line option wins. |
Ted Kremenek | ea644d8 | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 610 | |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 611 | // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off |
| 612 | // even if they are normally on for the target. In GNU modes (e.g. |
| 613 | // -std=gnu99) the default for blocks depends on the target settings. |
Anders Carlsson | e56f6ff | 2009-01-21 18:47:36 +0000 | [diff] [blame] | 614 | // However, blocks are not turned off when compiling Obj-C or Obj-C++ code. |
| 615 | if (!Options.ObjC1 && LangStd < lang_gnu_START) |
Chris Lattner | 802db9b | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 616 | Options.Blocks = 0; |
| 617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 618 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 619 | if (PascalStrings.getPosition()) |
| 620 | Options.PascalStrings = PascalStrings; |
Steve Naroff | d62701b | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 621 | Options.Microsoft = MSExtensions; |
Chris Lattner | 45e8cbd | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 622 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame^] | 623 | if (NoLaxVectorConversions.getPosition()) |
| 624 | Options.LaxVectorConversions = 0; |
Daniel Dunbar | 6379a7a | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 625 | Options.Exceptions = Exceptions; |
Mike Stump | b55cc63 | 2009-01-30 08:22:07 +0000 | [diff] [blame] | 626 | if (EnableBlocks.getPosition() || DisableBlocks.getPosition()) |
Chris Lattner | ae0ee03 | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 627 | Options.Blocks = EnableBlocks; |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 628 | |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 629 | // Override the default runtime if the user requested it. |
| 630 | if (NeXTRuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 631 | Options.NeXTRuntime = 1; |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 632 | else if (GNURuntime) |
Daniel Dunbar | f77ac86 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 633 | Options.NeXTRuntime = 0; |
Fariborz Jahanian | ee0af74 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 634 | |
Fariborz Jahanian | 30bc571 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 635 | if (ObjCNonFragileABI) |
| 636 | Options.ObjCNonFragileABI = 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 639 | static llvm::cl::opt<bool> |
| 640 | ObjCExclusiveGC("fobjc-gc-only", |
| 641 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 642 | "memory management")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 643 | |
| 644 | static llvm::cl::opt<bool> |
| 645 | ObjCEnableGC("fobjc-gc", |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 646 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 647 | |
| 648 | void InitializeGCMode(LangOptions &Options) { |
| 649 | if (ObjCExclusiveGC) |
| 650 | Options.setGCMode(LangOptions::GCOnly); |
| 651 | else if (ObjCEnableGC) |
| 652 | Options.setGCMode(LangOptions::HybridGC); |
| 653 | } |
| 654 | |
| 655 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 656 | //===----------------------------------------------------------------------===// |
| 657 | // Our DiagnosticClient implementation |
| 658 | //===----------------------------------------------------------------------===// |
| 659 | |
| 660 | // FIXME: Werror should take a list of things, -Werror=foo,bar |
| 661 | static llvm::cl::opt<bool> |
| 662 | WarningsAsErrors("Werror", llvm::cl::desc("Treat all warnings as errors")); |
| 663 | |
| 664 | static llvm::cl::opt<bool> |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 665 | SilenceWarnings("w", llvm::cl::desc("Do not emit any warnings")); |
| 666 | |
| 667 | static llvm::cl::opt<bool> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | WarnOnExtensions("pedantic", llvm::cl::init(false), |
| 669 | llvm::cl::desc("Issue a warning on uses of GCC extensions")); |
| 670 | |
| 671 | static llvm::cl::opt<bool> |
| 672 | ErrorOnExtensions("pedantic-errors", |
| 673 | llvm::cl::desc("Issue an error on uses of GCC extensions")); |
| 674 | |
| 675 | static llvm::cl::opt<bool> |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 676 | SuppressSystemWarnings("suppress-system-warnings", |
Daniel Dunbar | 320a054 | 2008-09-30 20:49:53 +0000 | [diff] [blame] | 677 | llvm::cl::desc("Suppress warnings issued in system headers"), |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 678 | llvm::cl::init(true)); |
| 679 | |
| 680 | static llvm::cl::opt<bool> |
Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 681 | WarnUnusedMacros("Wunused-macros", |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | llvm::cl::desc("Warn for unused macros in the main translation unit")); |
| 683 | |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 684 | static llvm::cl::opt<bool> |
| 685 | WarnFloatEqual("Wfloat-equal", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 686 | llvm::cl::desc("Warn about equality comparisons of floating point values")); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 687 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 688 | static llvm::cl::opt<bool> |
Fariborz Jahanian | 17d0d0d | 2009-01-08 23:23:10 +0000 | [diff] [blame] | 689 | WarnPropertyReadonlyAttrs("Wreadonly-setter-attrs", |
| 690 | llvm::cl::desc("Warn about readonly properties with writable attributes")); |
| 691 | |
| 692 | static llvm::cl::opt<bool> |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 693 | WarnNoFormatNonLiteral("Wno-format-nonliteral", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 694 | llvm::cl::desc("Do not warn about non-literal format strings")); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 696 | static llvm::cl::opt<bool> |
| 697 | WarnUndefMacros("Wundef", |
| 698 | llvm::cl::desc("Warn on use of undefined macros in #if's")); |
| 699 | |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 700 | static llvm::cl::opt<bool> |
Ted Kremenek | 358256d | 2008-05-30 16:42:02 +0000 | [diff] [blame] | 701 | WarnImplicitFunctionDeclaration("Wimplicit-function-declaration", |
| 702 | llvm::cl::desc("Warn about uses of implicitly defined functions")); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 703 | |
Steve Naroff | fe6b0dc | 2008-10-21 10:37:50 +0000 | [diff] [blame] | 704 | static llvm::cl::opt<bool> |
| 705 | WarnNoStrictSelectorMatch("Wno-strict-selector-match", |
| 706 | llvm::cl::desc("Do not warn about duplicate methods that have the same size and alignment"), |
| 707 | llvm::cl::init(true)); |
| 708 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | /// InitializeDiagnostics - Initialize the diagnostic object, based on the |
| 710 | /// current command line option settings. |
| 711 | static void InitializeDiagnostics(Diagnostic &Diags) { |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 712 | Diags.setIgnoreAllWarnings(SilenceWarnings); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 713 | Diags.setWarningsAsErrors(WarningsAsErrors); |
| 714 | Diags.setWarnOnExtensions(WarnOnExtensions); |
| 715 | Diags.setErrorOnExtensions(ErrorOnExtensions); |
| 716 | |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 717 | // Suppress warnings in system headers unless requested not to. |
| 718 | Diags.setSuppressSystemWarnings(SuppressSystemWarnings); |
| 719 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 720 | // Silence the "macro is not used" warning unless requested. |
| 721 | if (!WarnUnusedMacros) |
| 722 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
Ted Kremenek | db87bca | 2007-11-13 18:37:02 +0000 | [diff] [blame] | 723 | |
| 724 | // Silence "floating point comparison" warnings unless requested. |
| 725 | if (!WarnFloatEqual) |
| 726 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 727 | |
Fariborz Jahanian | 17d0d0d | 2009-01-08 23:23:10 +0000 | [diff] [blame] | 728 | if (!WarnPropertyReadonlyAttrs) |
| 729 | Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive, |
| 730 | diag::MAP_IGNORE); |
| 731 | |
Ted Kremenek | 73da590 | 2007-12-17 17:50:07 +0000 | [diff] [blame] | 732 | // Silence "format string is not a string literal" warnings if requested |
| 733 | if (WarnNoFormatNonLiteral) |
Ted Kremenek | 7c1d3df | 2007-12-17 17:50:39 +0000 | [diff] [blame] | 734 | Diags.setDiagnosticMapping(diag::warn_printf_not_string_constant, |
| 735 | diag::MAP_IGNORE); |
Chris Lattner | 116a4b1 | 2008-01-23 17:19:46 +0000 | [diff] [blame] | 736 | if (!WarnUndefMacros) |
| 737 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier,diag::MAP_IGNORE); |
Steve Naroff | e7a3730 | 2008-02-11 22:40:08 +0000 | [diff] [blame] | 738 | |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 739 | if (WarnImplicitFunctionDeclaration) |
| 740 | Diags.setDiagnosticMapping(diag::ext_implicit_function_decl, |
| 741 | diag::MAP_WARNING); |
| 742 | else |
Chris Lattner | 37d1084 | 2008-05-05 21:18:06 +0000 | [diff] [blame] | 743 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 744 | diag::MAP_IGNORE); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | //===----------------------------------------------------------------------===// |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 748 | // Analysis-specific options. |
| 749 | //===----------------------------------------------------------------------===// |
| 750 | |
| 751 | static llvm::cl::opt<std::string> |
| 752 | AnalyzeSpecificFunction("analyze-function", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 753 | llvm::cl::desc("Run analysis on specific function")); |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 754 | |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 755 | static llvm::cl::opt<bool> |
Ted Kremenek | d71ed26 | 2008-04-10 22:16:52 +0000 | [diff] [blame] | 756 | TrimGraph("trim-egraph", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 757 | llvm::cl::desc("Only show error-related paths in the analysis graph")); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 758 | |
Ted Kremenek | cb33093 | 2008-02-18 21:21:23 +0000 | [diff] [blame] | 759 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 760 | // Target Triple Processing. |
| 761 | //===----------------------------------------------------------------------===// |
| 762 | |
| 763 | static llvm::cl::opt<std::string> |
| 764 | TargetTriple("triple", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 765 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 766 | |
Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 767 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 768 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 769 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 770 | static llvm::cl::opt<std::string> |
| 771 | MacOSVersionMin("mmacosx-version-min", |
| 772 | llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)")); |
| 773 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 774 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 775 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
| 776 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 777 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 778 | if (DarwinDashIdx == std::string::npos) { |
| 779 | fprintf(stderr, |
| 780 | "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n"); |
| 781 | exit(1); |
| 782 | } |
| 783 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 784 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 785 | // Remove the number. |
| 786 | Triple.resize(DarwinNumIdx); |
| 787 | |
| 788 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 789 | bool MacOSVersionMinIsInvalid = false; |
| 790 | int VersionNum = 0; |
| 791 | if (MacOSVersionMin.size() < 4 || |
| 792 | MacOSVersionMin.substr(0, 3) != "10." || |
| 793 | !isdigit(MacOSVersionMin[3])) { |
| 794 | MacOSVersionMinIsInvalid = true; |
| 795 | } else { |
| 796 | const char *Start = MacOSVersionMin.c_str()+3; |
| 797 | char *End = 0; |
| 798 | VersionNum = (int)strtol(Start, &End, 10); |
| 799 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 800 | // The version number must be in the range 0-9. |
| 801 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 802 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 803 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 804 | Triple += llvm::itostr(VersionNum+4); |
| 805 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 806 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 807 | // Add the period piece (.7) to the end of the triple. This gives us |
| 808 | // something like ...-darwin8.7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 809 | Triple += End; |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 810 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 811 | MacOSVersionMinIsInvalid = true; |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | if (MacOSVersionMinIsInvalid) { |
| 816 | fprintf(stderr, |
| 817 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
| 818 | MacOSVersionMin.c_str()); |
| 819 | exit(1); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /// CreateTargetTriple - Process the various options that affect the target |
| 824 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 825 | static std::string CreateTargetTriple() { |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 826 | // Initialize base triple. If a -triple option has been specified, use |
| 827 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 6590d21 | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 828 | std::string Triple = TargetTriple; |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 829 | if (Triple.empty()) { |
| 830 | Triple = LLVM_HOSTTRIPLE; |
| 831 | |
Daniel Dunbar | a629190 | 2008-12-12 18:34:35 +0000 | [diff] [blame] | 832 | // Force i<N>86 to i386 when using LLVM_HOSTTRIPLE. |
| 833 | if (Triple[0] == 'i' && isdigit(Triple[1]) && |
| 834 | Triple[2] == '8' && Triple[3] == '6') |
| 835 | Triple[1] = '3'; |
| 836 | |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 837 | // On darwin, we want to update the version to match that of the |
| 838 | // host. |
| 839 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 840 | if (DarwinDashIdx != std::string::npos) { |
| 841 | Triple.resize(DarwinDashIdx + strlen("-darwin")); |
| 842 | |
Daniel Dunbar | a629190 | 2008-12-12 18:34:35 +0000 | [diff] [blame] | 843 | // Only add the major part of the os version. |
Chris Lattner | a05ff45 | 2009-01-22 20:57:52 +0000 | [diff] [blame] | 844 | std::string Version = llvm::sys::getOSVersion(); |
Daniel Dunbar | a629190 | 2008-12-12 18:34:35 +0000 | [diff] [blame] | 845 | Triple += Version.substr(0, Version.find('.')); |
Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 846 | } |
| 847 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 848 | |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 849 | // If -arch foo was specified, remove the architecture from the triple we have |
| 850 | // so far and replace it with the specified one. |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 851 | if (!Arch.empty()) { |
| 852 | // Decompose the base triple into "arch" and suffix. |
| 853 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 854 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 855 | if (FirstDashIdx == std::string::npos) { |
| 856 | fprintf(stderr, |
| 857 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 858 | Triple.c_str()); |
| 859 | exit(1); |
| 860 | } |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 861 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 862 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 863 | } |
| 864 | |
| 865 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 866 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 867 | if (!MacOSVersionMin.empty()) |
| 868 | HandleMacOSVersionMin(Triple); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 869 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 870 | return Triple; |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | // Preprocessor Initialization |
| 875 | //===----------------------------------------------------------------------===// |
| 876 | |
| 877 | // FIXME: Preprocessor builtins to support. |
| 878 | // -A... - Play with #assertions |
| 879 | // -undef - Undefine all predefined macros |
| 880 | |
| 881 | static llvm::cl::list<std::string> |
| 882 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 883 | llvm::cl::desc("Predefine the specified macro")); |
| 884 | static llvm::cl::list<std::string> |
| 885 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 886 | llvm::cl::desc("Undefine the specified macro")); |
| 887 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 888 | static llvm::cl::list<std::string> |
| 889 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 890 | llvm::cl::desc("Include file before parsing")); |
| 891 | |
| 892 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 894 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 895 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 896 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 897 | const char *Command = "#define ") { |
| 898 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 899 | if (const char *Equal = strchr(Macro, '=')) { |
| 900 | // Turn the = into ' '. |
| 901 | Buf.insert(Buf.end(), Macro, Equal); |
| 902 | Buf.push_back(' '); |
| 903 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 904 | } else { |
| 905 | // Push "macroname 1". |
| 906 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 907 | Buf.push_back(' '); |
| 908 | Buf.push_back('1'); |
| 909 | } |
| 910 | Buf.push_back('\n'); |
| 911 | } |
| 912 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 913 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 914 | /// predefines buffer. |
| 915 | static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){ |
| 916 | const char *Inc = "#include \""; |
| 917 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 918 | Buf.insert(Buf.end(), File.begin(), File.end()); |
| 919 | Buf.push_back('"'); |
| 920 | Buf.push_back('\n'); |
| 921 | } |
| 922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 923 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 924 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 925 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 926 | /// |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 927 | static bool InitializePreprocessor(Preprocessor &PP, |
| 928 | bool InitializeSourceMgr, |
| 929 | const std::string &InFile) { |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 930 | FileManager &FileMgr = PP.getFileManager(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 932 | // Figure out where to get and map in the main file. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 933 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 934 | |
| 935 | if (InitializeSourceMgr) { |
| 936 | if (InFile != "-") { |
| 937 | const FileEntry *File = FileMgr.getFile(InFile); |
| 938 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 939 | if (SourceMgr.getMainFileID().isInvalid()) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 940 | fprintf(stderr, "Error reading '%s'!\n",InFile.c_str()); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 941 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 942 | } |
| 943 | } else { |
| 944 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| 945 | if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 946 | if (SourceMgr.getMainFileID().isInvalid()) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 947 | fprintf(stderr, "Error reading standard input! Empty?\n"); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 948 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 950 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 951 | } |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 952 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 953 | std::vector<char> PredefineBuffer; |
| 954 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 955 | // Add macros from the command line. |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 956 | unsigned d = 0, D = D_macros.size(); |
| 957 | unsigned u = 0, U = U_macros.size(); |
| 958 | while (d < D || u < U) { |
| 959 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 960 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 961 | else |
| 962 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 963 | } |
| 964 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 965 | // FIXME: Read any files specified by -imacros. |
| 966 | |
| 967 | // Add implicit #includes from -include. |
| 968 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 969 | AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 970 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 971 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 972 | PredefineBuffer.push_back(0); |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 973 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 974 | |
| 975 | // Once we've read this, we're done. |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 976 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | //===----------------------------------------------------------------------===// |
| 980 | // Preprocessor include path information. |
| 981 | //===----------------------------------------------------------------------===// |
| 982 | |
| 983 | // This tool exports a large number of command line options to control how the |
| 984 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 985 | // object takes a very simple interface: a list of directories to search for |
| 986 | // |
| 987 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 988 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 989 | // |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 990 | // FIXME: -imacros |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 991 | |
| 992 | static llvm::cl::opt<bool> |
| 993 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 994 | |
| 995 | // Various command line options. These four add directories to each chain. |
| 996 | static llvm::cl::list<std::string> |
| 997 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 998 | llvm::cl::desc("Add directory to framework include search path")); |
| 999 | static llvm::cl::list<std::string> |
| 1000 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1001 | llvm::cl::desc("Add directory to include search path")); |
| 1002 | static llvm::cl::list<std::string> |
| 1003 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1004 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 1005 | static llvm::cl::list<std::string> |
| 1006 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1007 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 1008 | static llvm::cl::list<std::string> |
| 1009 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1010 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 1011 | |
| 1012 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 1013 | static llvm::cl::list<std::string> |
| 1014 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 1015 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 1016 | static llvm::cl::list<std::string> |
| 1017 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 1018 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 1019 | static llvm::cl::list<std::string> |
| 1020 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 1021 | llvm::cl::Prefix, |
| 1022 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 1023 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 1024 | static llvm::cl::opt<std::string> |
| 1025 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 1026 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 1027 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1029 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1030 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 1031 | /// HeaderSearch object. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1032 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 1033 | FileManager &FM, const LangOptions &Lang) { |
| 1034 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 1035 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1036 | // Handle -I... and -F... options, walking the lists in parallel. |
| 1037 | unsigned Iidx = 0, Fidx = 0; |
| 1038 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 1039 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1040 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1041 | ++Iidx; |
| 1042 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1043 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1044 | ++Fidx; |
| 1045 | } |
| 1046 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1048 | // Consume what's left from whatever list was longer. |
| 1049 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1050 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1051 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1052 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Handle -idirafter... options. |
| 1055 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1056 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 1057 | false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Handle -iquote... options. |
| 1060 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1061 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Handle -isystem... options. |
| 1064 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1065 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | |
| 1067 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1068 | // parallel, processing the values in order of occurance to get the right |
| 1069 | // prefixes. |
| 1070 | { |
| 1071 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1072 | unsigned iprefix_idx = 0; |
| 1073 | unsigned iwithprefix_idx = 0; |
| 1074 | unsigned iwithprefixbefore_idx = 0; |
| 1075 | bool iprefix_done = iprefix_vals.empty(); |
| 1076 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1077 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1078 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1079 | if (!iprefix_done && |
| 1080 | (iwithprefix_done || |
| 1081 | iprefix_vals.getPosition(iprefix_idx) < |
| 1082 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 1083 | (iwithprefixbefore_done || |
| 1084 | iprefix_vals.getPosition(iprefix_idx) < |
| 1085 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1086 | Prefix = iprefix_vals[iprefix_idx]; |
| 1087 | ++iprefix_idx; |
| 1088 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1089 | } else if (!iwithprefix_done && |
| 1090 | (iwithprefixbefore_done || |
| 1091 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 1092 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1093 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 1094 | InitHeaderSearch::System, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1095 | ++iwithprefix_idx; |
| 1096 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1097 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1098 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 1099 | InitHeaderSearch::Angled, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1100 | ++iwithprefixbefore_idx; |
| 1101 | iwithprefixbefore_done = |
| 1102 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1103 | } |
| 1104 | } |
| 1105 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1106 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1107 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1109 | // Add the clang headers, which are relative to the clang driver. |
| 1110 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 985e182 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 1111 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1112 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1113 | if (!MainExecutablePath.isEmpty()) { |
| 1114 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1115 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
| 1116 | MainExecutablePath.appendComponent("Headers"); // Get foo/Headers |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1117 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 1118 | false, false, false); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1121 | if (!nostdinc) |
| 1122 | Init.AddDefaultSystemIncludePaths(Lang); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | |
| 1124 | // Now that we have collected all of the include paths, merge them all |
| 1125 | // together and tell the preprocessor about them. |
| 1126 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1127 | Init.Realize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
| 1131 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 1132 | //===----------------------------------------------------------------------===// |
| 1133 | |
| 1134 | namespace { |
| 1135 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1136 | const std::string &InFile; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1137 | Diagnostic &Diags; |
| 1138 | const LangOptions &LangInfo; |
| 1139 | TargetInfo &Target; |
| 1140 | SourceManager &SourceMgr; |
| 1141 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1142 | bool InitializeSourceMgr; |
| 1143 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1144 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1145 | DriverPreprocessorFactory(const std::string &infile, |
| 1146 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1147 | TargetInfo &target, SourceManager &SM, |
| 1148 | HeaderSearch &Headers) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1149 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 1150 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 1151 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1152 | |
| 1153 | virtual ~DriverPreprocessorFactory() {} |
| 1154 | |
| 1155 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1156 | llvm::OwningPtr<PTHManager> PTHMgr; |
| 1157 | |
| 1158 | // Use PTH? |
| 1159 | if (!TokenCache.empty()) |
Ted Kremenek | 8a6aec6 | 2009-01-28 20:49:33 +0000 | [diff] [blame] | 1160 | PTHMgr.reset(PTHManager::Create(TokenCache, &Diags)); |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1161 | |
| 1162 | // Create the Preprocessor. |
| 1163 | llvm::OwningPtr<Preprocessor> PP(new Preprocessor(Diags, LangInfo, Target, |
| 1164 | SourceMgr, HeaderInfo, |
| 1165 | PTHMgr.get())); |
| 1166 | |
| 1167 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 1168 | // That argument is used as the IdentifierInfoLookup argument to |
| 1169 | // IdentifierTable's ctor. |
| 1170 | if (PTHMgr) { |
| 1171 | PTHMgr->setPreprocessor(PP.get()); |
| 1172 | PP->setPTHManager(PTHMgr.take()); |
| 1173 | } |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1174 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1175 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1176 | return NULL; |
| 1177 | } |
| 1178 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1179 | /// FIXME: PP can only handle one callback |
| 1180 | if (ProgAction != PrintPreprocessedInput) { |
| 1181 | const char* ErrStr; |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1182 | bool DFG = CreateDependencyFileGen(PP.get(), OutputFile, InFile, ErrStr); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1183 | if (!DFG && ErrStr) { |
Ted Kremenek | 9a30c24 | 2008-10-25 20:19:34 +0000 | [diff] [blame] | 1184 | fprintf(stderr, "%s", ErrStr); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1185 | return NULL; |
| 1186 | } |
| 1187 | } |
| 1188 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1189 | InitializeSourceMgr = false; |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1190 | return PP.take(); |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1191 | } |
| 1192 | }; |
| 1193 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1195 | //===----------------------------------------------------------------------===// |
| 1196 | // Basic Parser driver |
| 1197 | //===----------------------------------------------------------------------===// |
| 1198 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1199 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1200 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1201 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | |
| 1203 | // Parsing the specified input file. |
| 1204 | P.ParseTranslationUnit(); |
| 1205 | delete PA; |
| 1206 | } |
| 1207 | |
| 1208 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1209 | // Code generation options |
| 1210 | //===----------------------------------------------------------------------===// |
| 1211 | |
| 1212 | static llvm::cl::opt<bool> |
| 1213 | OptSize("Os", |
| 1214 | llvm::cl::desc("Optimize for size")); |
| 1215 | |
| 1216 | // It might be nice to add bounds to the CommandLine library directly. |
| 1217 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 1218 | bool parse(llvm::cl::Option &O, const char *ArgName, |
| 1219 | const std::string &Arg, unsigned &Val) { |
| 1220 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 1221 | return true; |
| 1222 | // FIXME: Support -O4. |
| 1223 | if (Val > 3) |
| 1224 | return O.error(": '" + Arg + "' invalid optimization level!"); |
| 1225 | return false; |
| 1226 | } |
| 1227 | }; |
| 1228 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 1229 | OptLevel("O", llvm::cl::Prefix, |
| 1230 | llvm::cl::desc("Optimization level"), |
| 1231 | llvm::cl::init(0)); |
| 1232 | |
| 1233 | static void InitializeCompileOptions(CompileOptions &Opts) { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1234 | Opts.OptimizeSize = OptSize; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1235 | if (OptSize) { |
| 1236 | // -Os implies -O2 |
| 1237 | // FIXME: Diagnose conflicting options. |
| 1238 | Opts.OptimizationLevel = 2; |
| 1239 | } else { |
| 1240 | Opts.OptimizationLevel = OptLevel; |
| 1241 | } |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1242 | |
| 1243 | // FIXME: There are llvm-gcc options to control these selectively. |
| 1244 | Opts.InlineFunctions = (Opts.OptimizationLevel > 1); |
| 1245 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
| 1246 | Opts.SimplifyLibCalls = 1; |
Daniel Dunbar | dd913e5 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1247 | |
| 1248 | #ifdef NDEBUG |
| 1249 | Opts.VerifyModule = 0; |
| 1250 | #endif |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | // Main driver |
| 1255 | //===----------------------------------------------------------------------===// |
| 1256 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1257 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
| 1258 | /// action. These consumers can operate on both ASTs that are freshly |
| 1259 | /// parsed from source files as well as those deserialized from Bitcode. |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1260 | static ASTConsumer* CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1261 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1262 | const LangOptions& LangOpts, |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1263 | Preprocessor *PP, |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1264 | PreprocessorFactory *PPF) { |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1265 | switch (ProgAction) { |
| 1266 | default: |
| 1267 | return NULL; |
| 1268 | |
| 1269 | case ASTPrint: |
| 1270 | return CreateASTPrinter(); |
| 1271 | |
| 1272 | case ASTDump: |
| 1273 | return CreateASTDumper(); |
| 1274 | |
| 1275 | case ASTView: |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1276 | return CreateASTViewer(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1277 | |
| 1278 | case PrintDeclContext: |
| 1279 | return CreateDeclContextPrinter(); |
Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 1280 | |
| 1281 | case EmitHTML: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1282 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1283 | |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1284 | case InheritanceView: |
| 1285 | return CreateInheritanceViewer(InheritanceViewCls); |
| 1286 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1287 | case TestSerialization: |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1288 | return CreateSerializationTest(Diag, FileMgr); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1289 | |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1290 | case EmitAssembly: |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1291 | case EmitLLVM: |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1292 | case EmitBC: { |
| 1293 | BackendAction Act; |
| 1294 | if (ProgAction == EmitAssembly) { |
| 1295 | Act = Backend_EmitAssembly; |
| 1296 | } else if (ProgAction == EmitLLVM) { |
| 1297 | Act = Backend_EmitLL; |
| 1298 | } else { |
| 1299 | Act = Backend_EmitBC; |
| 1300 | } |
| 1301 | CompileOptions Opts; |
| 1302 | InitializeCompileOptions(Opts); |
| 1303 | return CreateBackendConsumer(Act, Diag, LangOpts, Opts, |
Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 1304 | InFile, OutputFile, GenerateDebugInfo); |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1305 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1306 | |
Ted Kremenek | 3910c7c | 2007-12-19 17:25:59 +0000 | [diff] [blame] | 1307 | case SerializeAST: |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1308 | // FIXME: Allow user to tailor where the file is written. |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1309 | return CreateASTSerializer(InFile, OutputFile, Diag); |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1310 | |
Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 1311 | case RewriteObjC: |
Chris Lattner | c68ab77 | 2008-03-22 00:08:40 +0000 | [diff] [blame] | 1312 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1313 | |
| 1314 | case RewriteBlocks: |
| 1315 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1316 | |
| 1317 | case RunAnalysis: |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1318 | return CreateAnalysisConsumer(&AnalysisList[0], |
| 1319 | &AnalysisList[0]+AnalysisList.size(), |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 1320 | AnalysisStoreOpt, AnalysisDiagOpt, |
Ted Kremenek | f4381fd | 2008-07-02 00:03:09 +0000 | [diff] [blame] | 1321 | Diag, PP, PPF, LangOpts, |
| 1322 | AnalyzeSpecificFunction, |
Ted Kremenek | f8ce699 | 2008-08-27 22:31:43 +0000 | [diff] [blame] | 1323 | OutputFile, VisualizeEGDot, VisualizeEGUbi, |
Ted Kremenek | 491918e | 2009-01-23 20:52:26 +0000 | [diff] [blame] | 1324 | TrimGraph, AnalyzeAll, |
| 1325 | AnalyzerDisplayProgress); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1326 | } |
| 1327 | } |
| 1328 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1330 | /// |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1331 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1332 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1333 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1334 | bool ClearSourceMgr = false; |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1335 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1336 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | default: |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1338 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1339 | PP.getFileManager(), PP.getLangOptions(), |
| 1340 | &PP, &PPF)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1341 | |
| 1342 | if (!Consumer) { |
| 1343 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1344 | HadErrors = true; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1345 | return; |
| 1346 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1347 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1348 | break; |
| 1349 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1350 | case DumpRawTokens: { |
| 1351 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1352 | // Start lexing the specified input file. |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 1353 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1354 | RawLex.SetKeepWhitespaceMode(true); |
| 1355 | |
| 1356 | Token RawTok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1357 | RawLex.LexFromRawLexer(RawTok); |
| 1358 | while (RawTok.isNot(tok::eof)) { |
| 1359 | PP.DumpToken(RawTok, true); |
| 1360 | fprintf(stderr, "\n"); |
| 1361 | RawLex.LexFromRawLexer(RawTok); |
| 1362 | } |
| 1363 | ClearSourceMgr = true; |
| 1364 | break; |
| 1365 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | case DumpTokens: { // Token dump mode. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1367 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1368 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1369 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1370 | do { |
| 1371 | PP.Lex(Tok); |
| 1372 | PP.DumpToken(Tok, true); |
| 1373 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1374 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1375 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1376 | break; |
| 1377 | } |
| 1378 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1379 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1380 | // Start parsing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1381 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | do { |
| 1383 | PP.Lex(Tok); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1384 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1385 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1388 | |
| 1389 | case GeneratePCH: { |
| 1390 | CacheTokens(PP, OutputFile); |
| 1391 | ClearSourceMgr = true; |
| 1392 | break; |
| 1393 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1394 | |
| 1395 | case PrintPreprocessedInput: // -E mode. |
Chris Lattner | e988bc2 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1396 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1397 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1399 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1400 | case ParseNoop: // -parse-noop |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1401 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1402 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1403 | break; |
| 1404 | |
| 1405 | case ParsePrintCallbacks: |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1406 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1407 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1408 | break; |
Ted Kremenek | 4457978 | 2007-09-25 18:37:20 +0000 | [diff] [blame] | 1409 | |
Ted Kremenek | d39bcd8 | 2007-09-26 18:39:29 +0000 | [diff] [blame] | 1410 | case ParseSyntaxOnly: // -fsyntax-only |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1411 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1412 | break; |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1413 | |
| 1414 | case RewriteMacros: |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1415 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1416 | ClearSourceMgr = true; |
| 1417 | break; |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1418 | |
| 1419 | case RewriteTest: |
| 1420 | DoRewriteTest(PP, InFile, OutputFile); |
| 1421 | ClearSourceMgr = true; |
| 1422 | break; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1423 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1424 | |
| 1425 | if (Consumer) { |
| 1426 | TranslationUnit *TU = 0; |
| 1427 | if (DisableFree) { |
| 1428 | ASTContext *Context = new ASTContext(PP.getLangOptions(), |
| 1429 | PP.getSourceManager(), |
| 1430 | PP.getTargetInfo(), |
| 1431 | PP.getIdentifierTable(), |
| 1432 | PP.getSelectorTable(), |
| 1433 | /* FreeMemory = */ false); |
| 1434 | TU = new TranslationUnit(*Context); |
| 1435 | } |
| 1436 | ParseAST(PP, Consumer.get(), TU, Stats); |
| 1437 | } |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1438 | |
| 1439 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1440 | if (CheckDiagnostics(PP)) |
| 1441 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1443 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1444 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1445 | PP.PrintStats(); |
| 1446 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1447 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 1448 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | fprintf(stderr, "\n"); |
| 1450 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1451 | |
| 1452 | // For a multi-file compilation, some things are ok with nuking the source |
| 1453 | // manager tables, other require stable fileid/macroid's across multiple |
| 1454 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1455 | if (ClearSourceMgr) |
| 1456 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (DisableFree) |
| 1459 | Consumer.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1462 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1463 | FileManager& FileMgr) { |
| 1464 | |
| 1465 | if (VerifyDiagnostics) { |
| 1466 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1467 | exit (1); |
| 1468 | } |
| 1469 | |
| 1470 | llvm::sys::Path Filename(InFile); |
| 1471 | |
| 1472 | if (!Filename.isValid()) { |
| 1473 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1474 | exit (1); |
| 1475 | } |
| 1476 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1477 | llvm::OwningPtr<TranslationUnit> TU(ReadASTBitcodeFile(Filename, FileMgr)); |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (!TU) { |
| 1480 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1481 | InFile.c_str()); |
| 1482 | exit (1); |
| 1483 | } |
| 1484 | |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1485 | // Observe that we use the source file name stored in the deserialized |
| 1486 | // translation unit, rather than InFile. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1487 | llvm::OwningPtr<ASTConsumer> |
Ted Kremenek | e7d07d1 | 2008-06-04 15:55:15 +0000 | [diff] [blame] | 1488 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, TU->getLangOptions(), |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1489 | 0, 0)); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1490 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1491 | if (!Consumer) { |
| 1492 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1493 | exit (1); |
| 1494 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1495 | |
Ted Kremenek | c1e9dea | 2008-04-23 16:25:39 +0000 | [diff] [blame] | 1496 | Consumer->Initialize(TU->getContext()); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1497 | |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1498 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1499 | for (TranslationUnit::iterator I=TU->begin(), E=TU->end(); I!=E; ++I) |
| 1500 | Consumer->HandleTopLevelDecl(*I); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | static llvm::cl::list<std::string> |
| 1505 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1506 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1507 | static bool isSerializedFile(const std::string& InFile) { |
| 1508 | if (InFile.size() < 4) |
| 1509 | return false; |
| 1510 | |
| 1511 | const char* s = InFile.c_str()+InFile.size()-4; |
| 1512 | |
| 1513 | return s[0] == '.' && |
| 1514 | s[1] == 'a' && |
| 1515 | s[2] == 's' && |
| 1516 | s[3] == 't'; |
| 1517 | } |
| 1518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1519 | |
| 1520 | int main(int argc, char **argv) { |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1521 | llvm::cl::ParseCommandLineOptions(argc, argv, " llvm clang cfe\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1522 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| 1523 | |
| 1524 | // If no input was specified, read from stdin. |
| 1525 | if (InputFilenames.empty()) |
| 1526 | InputFilenames.push_back("-"); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1527 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1528 | // Create a file manager object to provide access to and cache the filesystem. |
| 1529 | FileManager FileMgr; |
| 1530 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1531 | // Create the diagnostic client for reporting errors or for |
| 1532 | // implementing -verify. |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1533 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1534 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1535 | if (!VerifyDiagnostics) { |
| 1536 | // Print diagnostics to stderr by default. |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 1537 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 1538 | !NoShowColumn, |
Chris Lattner | 65f5e64 | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 1539 | !NoCaretDiagnostics, |
| 1540 | !NoShowLocation); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1541 | } else { |
| 1542 | // When checking diagnostics, just buffer them up. |
| 1543 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1544 | |
| 1545 | if (InputFilenames.size() != 1) { |
| 1546 | fprintf(stderr, |
| 1547 | "-verify only works on single input files for now.\n"); |
| 1548 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1551 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1552 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1553 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1554 | Diagnostic Diags(DiagClient.get()); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1555 | InitializeDiagnostics(Diags); |
| 1556 | |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1557 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1558 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1559 | if (I_dirs[i] == "-") { |
Chris Lattner | 5917fe1 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 1560 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1561 | I_dirs.erase(I_dirs.begin()+i); |
| 1562 | --i; |
| 1563 | } |
| 1564 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1565 | |
| 1566 | // Get information about the target being compiled for. |
| 1567 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1568 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1569 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1570 | if (Target == 0) { |
| 1571 | fprintf(stderr, "Sorry, I don't know what target this is: %s\n", |
| 1572 | Triple.c_str()); |
| 1573 | fprintf(stderr, "Please use -triple or -arch.\n"); |
| 1574 | exit(1); |
| 1575 | } |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1576 | |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1577 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1578 | ProgAction = InheritanceView; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1579 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1580 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1583 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1584 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1585 | if (isSerializedFile(InFile)) { |
| 1586 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1587 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1588 | } |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1589 | else { |
| 1590 | /// Create a SourceManager object. This tracks and owns all the file |
| 1591 | /// buffers allocated to a translation unit. |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1592 | if (!SourceMgr) |
| 1593 | SourceMgr.reset(new SourceManager()); |
| 1594 | else |
| 1595 | SourceMgr->clearIDTables(); |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1596 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1597 | // Initialize language options, inferring file types from input filenames. |
| 1598 | LangOptions LangInfo; |
| 1599 | InitializeBaseLanguage(); |
| 1600 | LangKind LK = GetLanguage(InFile); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1601 | bool PCH = InitializeLangOptions(LangInfo, LK); |
Ted Kremenek | 01d9dbf | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 1602 | InitializeGCMode(LangInfo); |
Chris Lattner | 8fc4dfb | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 1603 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1604 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1605 | // Process the -I options and set them in the HeaderInfo. |
| 1606 | HeaderSearch HeaderInfo(FileMgr); |
Ted Kremenek | c68ecb5 | 2008-06-06 01:47:30 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1608 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1609 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1610 | // Set up the preprocessor with these options. |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1611 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1612 | *SourceMgr.get(), HeaderInfo); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1613 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1614 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1615 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1616 | if (!PP) |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1617 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1618 | |
| 1619 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1620 | // always reset to using TextDiagClient. |
| 1621 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 1622 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1623 | if (!HTMLDiag.empty()) { |
| 1624 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1625 | &PPFactory)); |
| 1626 | Diags.setClient(TmpClient.get()); |
| 1627 | } |
| 1628 | else |
| 1629 | Diags.setClient(TextDiagClient); |
| 1630 | |
| 1631 | // Process the source file. |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1632 | ProcessInputFile(*PP, PPFactory, InFile, PCH ? GeneratePCH : ProgAction); |
| 1633 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1634 | HeaderInfo.ClearFileInfo(); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1635 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1637 | |
Mike Stump | 007f2a9 | 2009-01-28 02:43:35 +0000 | [diff] [blame] | 1638 | if (Verbose) |
| 1639 | fprintf(stderr, "clang version 1.0 based upon " PACKAGE_STRING |
| 1640 | " hosted on " LLVM_HOSTTRIPLE "\n"); |
| 1641 | |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1642 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1643 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1644 | (NumDiagnostics == 1 ? "" : "s")); |
| 1645 | |
| 1646 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1647 | FileMgr.PrintStats(); |
| 1648 | fprintf(stderr, "\n"); |
| 1649 | } |
| 1650 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1651 | // If verifying diagnostics and we reached here, all is well. |
| 1652 | if (VerifyDiagnostics) |
| 1653 | return 0; |
| 1654 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1655 | // Managed static deconstruction. Useful for making things like |
| 1656 | // -time-passes usable. |
| 1657 | llvm::llvm_shutdown(); |
| 1658 | |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1659 | return HadErrors || (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1660 | } |