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