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