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> |
Mike Stump | 5d8b2cf | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 683 | UnsignedOverflowChecking("ftrapu", |
| 684 | llvm::cl::desc("Trap on unsigned and signed integer overflow"), |
| 685 | llvm::cl::init(false)); |
| 686 | |
| 687 | static llvm::cl::opt<bool> |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 688 | OverflowChecking("ftrapv", |
Mike Stump | 5d8b2cf | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 689 | llvm::cl::desc("Trap on signed integer overflow"), |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 690 | llvm::cl::init(false)); |
| 691 | |
| 692 | void InitializeOverflowChecking(LangOptions &Options) { |
Mike Stump | 5d8b2cf | 2009-04-02 01:03:55 +0000 | [diff] [blame] | 693 | Options.OverflowChecking = OverflowChecking | UnsignedOverflowChecking; |
| 694 | Options.UnsignedOverflowChecking = UnsignedOverflowChecking; |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 695 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 696 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 697 | // Target Triple Processing. |
| 698 | //===----------------------------------------------------------------------===// |
| 699 | |
| 700 | static llvm::cl::opt<std::string> |
| 701 | TargetTriple("triple", |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 702 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 703 | |
Chris Lattner | 42e6737 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 704 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 705 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 706 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 707 | static llvm::cl::opt<std::string> |
| 708 | MacOSVersionMin("mmacosx-version-min", |
| 709 | llvm::cl::desc("Specify target Mac OS/X version (e.g. 10.5)")); |
| 710 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 711 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 712 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Daniel Dunbar | 64ffc14 | 2009-03-31 20:10:05 +0000 | [diff] [blame] | 713 | |
| 714 | // FIXME: We should have the driver do this instead. |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 715 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 716 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 717 | if (DarwinDashIdx == std::string::npos) { |
| 718 | fprintf(stderr, |
| 719 | "-mmacosx-version-min only valid for darwin (Mac OS/X) targets\n"); |
| 720 | exit(1); |
| 721 | } |
| 722 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 723 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 724 | // Remove the number. |
| 725 | Triple.resize(DarwinNumIdx); |
| 726 | |
| 727 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 728 | bool MacOSVersionMinIsInvalid = false; |
| 729 | int VersionNum = 0; |
| 730 | if (MacOSVersionMin.size() < 4 || |
| 731 | MacOSVersionMin.substr(0, 3) != "10." || |
| 732 | !isdigit(MacOSVersionMin[3])) { |
| 733 | MacOSVersionMinIsInvalid = true; |
| 734 | } else { |
| 735 | const char *Start = MacOSVersionMin.c_str()+3; |
| 736 | char *End = 0; |
| 737 | VersionNum = (int)strtol(Start, &End, 10); |
| 738 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 739 | // The version number must be in the range 0-9. |
| 740 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 741 | |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 742 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 743 | Triple += llvm::itostr(VersionNum+4); |
| 744 | |
Chris Lattner | 079f2c46 | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 745 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 746 | // Add the period piece (.7) to the end of the triple. This gives us |
| 747 | // something like ...-darwin8.7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 748 | Triple += End; |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 749 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 750 | MacOSVersionMinIsInvalid = true; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (MacOSVersionMinIsInvalid) { |
| 755 | fprintf(stderr, |
Daniel Dunbar | af07f93 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 756 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 757 | MacOSVersionMin.c_str()); |
| 758 | exit(1); |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | /// CreateTargetTriple - Process the various options that affect the target |
| 763 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 764 | static std::string CreateTargetTriple() { |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 765 | // Initialize base triple. If a -triple option has been specified, use |
| 766 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 6590d21 | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 767 | std::string Triple = TargetTriple; |
Daniel Dunbar | af07f93 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 768 | if (Triple.empty()) |
| 769 | Triple = llvm::sys::getHostTriple(); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 770 | |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 771 | // If -arch foo was specified, remove the architecture from the triple we have |
| 772 | // so far and replace it with the specified one. |
Daniel Dunbar | 64ffc14 | 2009-03-31 20:10:05 +0000 | [diff] [blame] | 773 | |
| 774 | // FIXME: -arch should be removed, the driver should handle this. |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 775 | if (!Arch.empty()) { |
| 776 | // Decompose the base triple into "arch" and suffix. |
| 777 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | 6fd9fa1 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 778 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 779 | if (FirstDashIdx == std::string::npos) { |
| 780 | fprintf(stderr, |
| 781 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 782 | Triple.c_str()); |
| 783 | exit(1); |
| 784 | } |
Chris Lattner | 37e217c | 2009-03-24 16:18:41 +0000 | [diff] [blame] | 785 | |
| 786 | // Canonicalize -arch ppc to add "powerpc" to the triple, not ppc. |
| 787 | if (Arch == "ppc") |
| 788 | Arch = "powerpc"; |
| 789 | else if (Arch == "ppc64") |
| 790 | Arch = "powerpc64"; |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 792 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 793 | } |
| 794 | |
| 795 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 796 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 797 | if (!MacOSVersionMin.empty()) |
| 798 | HandleMacOSVersionMin(Triple); |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 6a30c1f | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 800 | return Triple; |
Ted Kremenek | ae36076 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 804 | // Preprocessor Initialization |
| 805 | //===----------------------------------------------------------------------===// |
| 806 | |
| 807 | // FIXME: Preprocessor builtins to support. |
| 808 | // -A... - Play with #assertions |
| 809 | // -undef - Undefine all predefined macros |
| 810 | |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 811 | // FIXME: -imacros |
| 812 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 813 | static llvm::cl::list<std::string> |
| 814 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 815 | llvm::cl::desc("Predefine the specified macro")); |
| 816 | static llvm::cl::list<std::string> |
| 817 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 818 | llvm::cl::desc("Undefine the specified macro")); |
| 819 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 820 | static llvm::cl::list<std::string> |
| 821 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 822 | llvm::cl::desc("Include file before parsing")); |
| 823 | |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 824 | static llvm::cl::opt<std::string> |
| 825 | ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"), |
| 826 | llvm::cl::desc("Include file before parsing")); |
| 827 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 828 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 829 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 830 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 831 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 832 | const char *Command = "#define ") { |
| 833 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 834 | if (const char *Equal = strchr(Macro, '=')) { |
| 835 | // Turn the = into ' '. |
| 836 | Buf.insert(Buf.end(), Macro, Equal); |
| 837 | Buf.push_back(' '); |
| 838 | Buf.insert(Buf.end(), Equal+1, Equal+strlen(Equal)); |
| 839 | } else { |
| 840 | // Push "macroname 1". |
| 841 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 842 | Buf.push_back(' '); |
| 843 | Buf.push_back('1'); |
| 844 | } |
| 845 | Buf.push_back('\n'); |
| 846 | } |
| 847 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 848 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 849 | /// predefines buffer. |
| 850 | static void AddImplicitInclude(std::vector<char> &Buf, const std::string &File){ |
| 851 | const char *Inc = "#include \""; |
| 852 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 853 | Buf.insert(Buf.end(), File.begin(), File.end()); |
| 854 | Buf.push_back('"'); |
| 855 | Buf.push_back('\n'); |
| 856 | } |
| 857 | |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 858 | /// AddImplicitIncludePTH - Add an implicit #include using the original file |
| 859 | /// used to generate a PTH cache. |
| 860 | static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor & PP) { |
| 861 | PTHManager *P = PP.getPTHManager(); |
| 862 | assert(P && "No PTHManager."); |
| 863 | const char *OriginalFile = P->getOriginalSourceFile(); |
| 864 | |
| 865 | if (!OriginalFile) { |
| 866 | assert(!ImplicitIncludePTH.empty()); |
| 867 | fprintf(stderr, "error: PTH file '%s' does not designate an original " |
| 868 | "source header file for -include-pth\n", |
| 869 | ImplicitIncludePTH.c_str()); |
| 870 | exit (1); |
| 871 | } |
| 872 | |
| 873 | AddImplicitInclude(Buf, OriginalFile); |
| 874 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 876 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 877 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 878 | /// |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 879 | static bool InitializePreprocessor(Preprocessor &PP, |
| 880 | bool InitializeSourceMgr, |
| 881 | const std::string &InFile) { |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 882 | FileManager &FileMgr = PP.getFileManager(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 884 | // Figure out where to get and map in the main file. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 885 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 886 | |
| 887 | if (InitializeSourceMgr) { |
| 888 | if (InFile != "-") { |
| 889 | const FileEntry *File = FileMgr.getFile(InFile); |
| 890 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 891 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 892 | PP.getDiagnostics().Report(FullSourceLoc(), diag::err_fe_error_reading) |
| 893 | << InFile.c_str(); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 894 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 895 | } |
| 896 | } else { |
| 897 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
Daniel Dunbar | 5674388 | 2009-03-21 17:56:30 +0000 | [diff] [blame] | 898 | |
| 899 | // If stdin was empty, SB is null. Cons up an empty memory |
| 900 | // buffer now. |
| 901 | if (!SB) { |
| 902 | const char *EmptyStr = ""; |
| 903 | SB = llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>"); |
| 904 | } |
| 905 | |
| 906 | SourceMgr.createMainFileIDForMemBuffer(SB); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 907 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 908 | PP.getDiagnostics().Report(FullSourceLoc(), |
| 909 | diag::err_fe_error_reading_stdin); |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 910 | return true; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 911 | } |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 912 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 913 | } |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 914 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 915 | std::vector<char> PredefineBuffer; |
| 916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 917 | // Add macros from the command line. |
Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 918 | unsigned d = 0, D = D_macros.size(); |
| 919 | unsigned u = 0, U = U_macros.size(); |
| 920 | while (d < D || u < U) { |
| 921 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 922 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 923 | else |
| 924 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 925 | } |
| 926 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 927 | // FIXME: Read any files specified by -imacros. |
| 928 | |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 929 | // Add implicit #includes from -include and -include-pth. |
| 930 | bool handledPTH = ImplicitIncludePTH.empty(); |
| 931 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) { |
Ted Kremenek | c53b60a | 2009-03-20 00:40:03 +0000 | [diff] [blame] | 932 | if (!handledPTH && |
| 933 | ImplicitIncludePTH.getPosition() < ImplicitIncludes.getPosition(i)) { |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 934 | AddImplicitIncludePTH(PredefineBuffer, PP); |
| 935 | handledPTH = true; |
| 936 | } |
| 937 | |
Chris Lattner | 64299f8 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 938 | AddImplicitInclude(PredefineBuffer, ImplicitIncludes[i]); |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 939 | } |
| 940 | if (!handledPTH && !ImplicitIncludePTH.empty()) |
| 941 | AddImplicitIncludePTH(PredefineBuffer, PP); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 942 | |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 943 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 944 | PredefineBuffer.push_back(0); |
Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 945 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 946 | |
| 947 | // Once we've read this, we're done. |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 948 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | //===----------------------------------------------------------------------===// |
| 952 | // Preprocessor include path information. |
| 953 | //===----------------------------------------------------------------------===// |
| 954 | |
| 955 | // This tool exports a large number of command line options to control how the |
| 956 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 957 | // object takes a very simple interface: a list of directories to search for |
| 958 | // |
| 959 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 960 | // FIXME: -imultilib |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 961 | // |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 962 | |
| 963 | static llvm::cl::opt<bool> |
| 964 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 965 | |
| 966 | // Various command line options. These four add directories to each chain. |
| 967 | static llvm::cl::list<std::string> |
| 968 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 969 | llvm::cl::desc("Add directory to framework include search path")); |
| 970 | static llvm::cl::list<std::string> |
| 971 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 972 | llvm::cl::desc("Add directory to include search path")); |
| 973 | static llvm::cl::list<std::string> |
| 974 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 975 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 976 | static llvm::cl::list<std::string> |
| 977 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 978 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 979 | static llvm::cl::list<std::string> |
| 980 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 981 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 982 | |
| 983 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 984 | static llvm::cl::list<std::string> |
| 985 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 986 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 987 | static llvm::cl::list<std::string> |
| 988 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 989 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 990 | static llvm::cl::list<std::string> |
| 991 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 992 | llvm::cl::Prefix, |
| 993 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 994 | |
Chris Lattner | 0c94641 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 995 | static llvm::cl::opt<std::string> |
| 996 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 997 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 999 | // Finally, implement the code that groks the options above. |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1000 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1001 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 1002 | /// HeaderSearch object. |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1003 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 1004 | FileManager &FM, const LangOptions &Lang) { |
| 1005 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 1006 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1007 | // Handle -I... and -F... options, walking the lists in parallel. |
| 1008 | unsigned Iidx = 0, Fidx = 0; |
| 1009 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 1010 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1011 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1012 | ++Iidx; |
| 1013 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1014 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1015 | ++Fidx; |
| 1016 | } |
| 1017 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1019 | // Consume what's left from whatever list was longer. |
| 1020 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1021 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | f372111 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1022 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1023 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1024 | |
| 1025 | // Handle -idirafter... options. |
| 1026 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1027 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 1028 | false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1029 | |
| 1030 | // Handle -iquote... options. |
| 1031 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1032 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Handle -isystem... options. |
| 1035 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1036 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1037 | |
| 1038 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1039 | // parallel, processing the values in order of occurance to get the right |
| 1040 | // prefixes. |
| 1041 | { |
| 1042 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1043 | unsigned iprefix_idx = 0; |
| 1044 | unsigned iwithprefix_idx = 0; |
| 1045 | unsigned iwithprefixbefore_idx = 0; |
| 1046 | bool iprefix_done = iprefix_vals.empty(); |
| 1047 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1048 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1049 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1050 | if (!iprefix_done && |
| 1051 | (iwithprefix_done || |
| 1052 | iprefix_vals.getPosition(iprefix_idx) < |
| 1053 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 1054 | (iwithprefixbefore_done || |
| 1055 | iprefix_vals.getPosition(iprefix_idx) < |
| 1056 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1057 | Prefix = iprefix_vals[iprefix_idx]; |
| 1058 | ++iprefix_idx; |
| 1059 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1060 | } else if (!iwithprefix_done && |
| 1061 | (iwithprefixbefore_done || |
| 1062 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 1063 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1064 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 1065 | InitHeaderSearch::System, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | ++iwithprefix_idx; |
| 1067 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1068 | } else { |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1069 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 1070 | InitHeaderSearch::Angled, false, false, false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1071 | ++iwithprefixbefore_idx; |
| 1072 | iwithprefixbefore_done = |
| 1073 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1074 | } |
| 1075 | } |
| 1076 | } |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1077 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1078 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1079 | |
Daniel Dunbar | adcf5b3 | 2009-02-21 20:52:41 +0000 | [diff] [blame] | 1080 | // Add the clang headers, which are relative to the clang binary. |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1081 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 985e182 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 1082 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1083 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1084 | if (!MainExecutablePath.isEmpty()) { |
| 1085 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1086 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
Daniel Dunbar | adcf5b3 | 2009-02-21 20:52:41 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Get foo/lib/clang/1.0/include |
| 1089 | // |
| 1090 | // FIXME: Don't embed version here. |
| 1091 | MainExecutablePath.appendComponent("lib"); |
| 1092 | MainExecutablePath.appendComponent("clang"); |
| 1093 | MainExecutablePath.appendComponent("1.0"); |
| 1094 | MainExecutablePath.appendComponent("include"); |
Chris Lattner | 6858dd3 | 2009-02-19 06:48:28 +0000 | [diff] [blame] | 1095 | |
| 1096 | // We pass true to ignore sysroot so that we *always* look for clang headers |
| 1097 | // relative to our executable, never relative to -isysroot. |
| 1098 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 1099 | false, false, false, true /*ignore sysroot*/); |
Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1102 | if (!nostdinc) |
| 1103 | Init.AddDefaultSystemIncludePaths(Lang); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Now that we have collected all of the include paths, merge them all |
| 1106 | // together and tell the preprocessor about them. |
| 1107 | |
Nico Weber | 0fca022 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1108 | Init.Realize(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1111 | //===----------------------------------------------------------------------===// |
| 1112 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 1113 | //===----------------------------------------------------------------------===// |
| 1114 | |
| 1115 | namespace { |
| 1116 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1117 | const std::string &InFile; |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1118 | Diagnostic &Diags; |
| 1119 | const LangOptions &LangInfo; |
| 1120 | TargetInfo &Target; |
| 1121 | SourceManager &SourceMgr; |
| 1122 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1123 | bool InitializeSourceMgr; |
| 1124 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1125 | public: |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1126 | DriverPreprocessorFactory(const std::string &infile, |
| 1127 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1128 | TargetInfo &target, SourceManager &SM, |
| 1129 | HeaderSearch &Headers) |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1130 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 1131 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 1132 | |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1133 | |
| 1134 | virtual ~DriverPreprocessorFactory() {} |
| 1135 | |
| 1136 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1137 | llvm::OwningPtr<PTHManager> PTHMgr; |
| 1138 | |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1139 | if (!TokenCache.empty() && !ImplicitIncludePTH.empty()) { |
| 1140 | fprintf(stderr, "error: cannot use both -token-cache and -include-pth " |
| 1141 | "options\n"); |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1142 | exit(1); |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1145 | // Use PTH? |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1146 | if (!TokenCache.empty() || !ImplicitIncludePTH.empty()) { |
| 1147 | const std::string& x = TokenCache.empty() ? ImplicitIncludePTH:TokenCache; |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1148 | PTHMgr.reset(PTHManager::Create(x, &Diags, |
| 1149 | TokenCache.empty() ? Diagnostic::Error |
| 1150 | : Diagnostic::Warning)); |
Ted Kremenek | 748d5d6 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1151 | } |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1152 | |
Ted Kremenek | 22f0d09 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1153 | if (Diags.hasErrorOccurred()) |
| 1154 | exit(1); |
| 1155 | |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1156 | // Create the Preprocessor. |
| 1157 | llvm::OwningPtr<Preprocessor> PP(new Preprocessor(Diags, LangInfo, Target, |
| 1158 | SourceMgr, HeaderInfo, |
| 1159 | PTHMgr.get())); |
| 1160 | |
| 1161 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 1162 | // That argument is used as the IdentifierInfoLookup argument to |
| 1163 | // IdentifierTable's ctor. |
| 1164 | if (PTHMgr) { |
| 1165 | PTHMgr->setPreprocessor(PP.get()); |
| 1166 | PP->setPTHManager(PTHMgr.take()); |
| 1167 | } |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1168 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1169 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1170 | return NULL; |
| 1171 | } |
| 1172 | |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1173 | /// FIXME: PP can only handle one callback |
Daniel Dunbar | a5a7bd0 | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 1174 | if (ProgAction != PrintPreprocessedInput) { |
| 1175 | std::string ErrStr; |
| 1176 | bool DFG = CreateDependencyFileGen(PP.get(), ErrStr); |
| 1177 | if (!DFG && !ErrStr.empty()) { |
| 1178 | fprintf(stderr, "%s", ErrStr.c_str()); |
Daniel Dunbar | 750c358 | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1179 | return NULL; |
| 1180 | } |
| 1181 | } |
| 1182 | |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1183 | InitializeSourceMgr = false; |
Ted Kremenek | 72b1b15 | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1184 | return PP.take(); |
Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1185 | } |
| 1186 | }; |
| 1187 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1189 | //===----------------------------------------------------------------------===// |
| 1190 | // Basic Parser driver |
| 1191 | //===----------------------------------------------------------------------===// |
| 1192 | |
Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1193 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | Parser P(PP, *PA); |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1195 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Parsing the specified input file. |
| 1198 | P.ParseTranslationUnit(); |
| 1199 | delete PA; |
| 1200 | } |
| 1201 | |
| 1202 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1203 | // Code generation options |
| 1204 | //===----------------------------------------------------------------------===// |
| 1205 | |
| 1206 | static llvm::cl::opt<bool> |
Chris Lattner | 1510488 | 2009-03-09 22:05:03 +0000 | [diff] [blame] | 1207 | GenerateDebugInfo("g", |
| 1208 | llvm::cl::desc("Generate source level debug information")); |
| 1209 | |
| 1210 | static llvm::cl::opt<bool> |
Daniel Dunbar | ef2abfe | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 1211 | OptSize("Os", llvm::cl::desc("Optimize for size")); |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1212 | |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 1213 | static llvm::cl::opt<bool> |
| 1214 | NoCommon("fno-common", |
Mike Stump | 1c9b742 | 2009-03-27 20:15:22 +0000 | [diff] [blame] | 1215 | llvm::cl::desc("Compile common globals like normal definitions"), |
Chris Lattner | 925a704 | 2009-03-28 02:12:08 +0000 | [diff] [blame] | 1216 | llvm::cl::ValueDisallowed); |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 1217 | |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1218 | // It might be nice to add bounds to the CommandLine library directly. |
| 1219 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 1220 | bool parse(llvm::cl::Option &O, const char *ArgName, |
| 1221 | const std::string &Arg, unsigned &Val) { |
| 1222 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 1223 | return true; |
| 1224 | // FIXME: Support -O4. |
| 1225 | if (Val > 3) |
| 1226 | return O.error(": '" + Arg + "' invalid optimization level!"); |
| 1227 | return false; |
| 1228 | } |
| 1229 | }; |
| 1230 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 1231 | OptLevel("O", llvm::cl::Prefix, |
| 1232 | llvm::cl::desc("Optimization level"), |
| 1233 | llvm::cl::init(0)); |
| 1234 | |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1235 | static llvm::cl::opt<std::string> |
| 1236 | TargetCPU("mcpu", |
| 1237 | llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)")); |
| 1238 | |
Chris Lattner | 7afae71 | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1239 | static void InitializeCompileOptions(CompileOptions &Opts, |
| 1240 | const LangOptions &LangOpts) { |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1241 | Opts.OptimizeSize = OptSize; |
Chris Lattner | 2012604 | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 1242 | Opts.DebugInfo = GenerateDebugInfo; |
Daniel Dunbar | ac7ffe0 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1243 | if (OptSize) { |
| 1244 | // -Os implies -O2 |
| 1245 | // FIXME: Diagnose conflicting options. |
| 1246 | Opts.OptimizationLevel = 2; |
| 1247 | } else { |
| 1248 | Opts.OptimizationLevel = OptLevel; |
| 1249 | } |
Daniel Dunbar | 8e8f3b7 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1250 | |
| 1251 | // FIXME: There are llvm-gcc options to control these selectively. |
| 1252 | Opts.InlineFunctions = (Opts.OptimizationLevel > 1); |
| 1253 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
Chris Lattner | 7afae71 | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1254 | Opts.SimplifyLibCalls = !LangOpts.NoBuiltin; |
Daniel Dunbar | dd913e5 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1255 | |
| 1256 | #ifdef NDEBUG |
| 1257 | Opts.VerifyModule = 0; |
| 1258 | #endif |
Daniel Dunbar | a034ba8 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1259 | |
| 1260 | Opts.CPU = TargetCPU; |
| 1261 | Opts.Features.insert(Opts.Features.end(), |
| 1262 | TargetFeatures.begin(), TargetFeatures.end()); |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 1263 | |
Chris Lattner | bd36064 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 1264 | Opts.NoCommon = NoCommon | LangOpts.CPlusPlus; |
| 1265 | |
Chris Lattner | 4450266 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 1266 | // Handle -ftime-report. |
| 1267 | Opts.TimePasses = TimeReport; |
Daniel Dunbar | 70f9243 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | //===----------------------------------------------------------------------===// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | // Main driver |
| 1272 | //===----------------------------------------------------------------------===// |
| 1273 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1274 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
Chris Lattner | 1510488 | 2009-03-09 22:05:03 +0000 | [diff] [blame] | 1275 | /// action. These consumers can operate on both ASTs that are freshly |
| 1276 | /// parsed from source files as well as those deserialized from Bitcode. |
| 1277 | /// Note that PP and PPF may be null here. |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1278 | static ASTConsumer *CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | a1fa3a1 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1279 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1280 | const LangOptions& LangOpts, |
Chris Lattner | 3245a0a | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1281 | Preprocessor *PP, |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1282 | PreprocessorFactory *PPF) { |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1283 | switch (ProgAction) { |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1284 | default: |
| 1285 | return NULL; |
| 1286 | |
| 1287 | case ASTPrint: |
| 1288 | return CreateASTPrinter(); |
| 1289 | |
| 1290 | case ASTDump: |
| 1291 | return CreateASTDumper(); |
| 1292 | |
| 1293 | case ASTView: |
| 1294 | return CreateASTViewer(); |
Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1296 | case PrintDeclContext: |
| 1297 | return CreateDeclContextPrinter(); |
| 1298 | |
| 1299 | case EmitHTML: |
| 1300 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | 902141f | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1301 | |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1302 | case InheritanceView: |
| 1303 | return CreateInheritanceViewer(InheritanceViewCls); |
| 1304 | |
| 1305 | case TestSerialization: |
| 1306 | return CreateSerializationTest(Diag, FileMgr); |
| 1307 | |
| 1308 | case EmitAssembly: |
| 1309 | case EmitLLVM: |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 1310 | case EmitBC: |
| 1311 | case EmitLLVMOnly: { |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1312 | BackendAction Act; |
| 1313 | if (ProgAction == EmitAssembly) |
| 1314 | Act = Backend_EmitAssembly; |
| 1315 | else if (ProgAction == EmitLLVM) |
| 1316 | Act = Backend_EmitLL; |
Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 1317 | else if (ProgAction == EmitLLVMOnly) |
| 1318 | Act = Backend_EmitNothing; |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1319 | else |
| 1320 | Act = Backend_EmitBC; |
| 1321 | |
| 1322 | CompileOptions Opts; |
Chris Lattner | 7afae71 | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1323 | InitializeCompileOptions(Opts, LangOpts); |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1324 | return CreateBackendConsumer(Act, Diag, LangOpts, Opts, |
Chris Lattner | 2012604 | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 1325 | InFile, OutputFile); |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1326 | } |
Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1328 | case SerializeAST: |
| 1329 | // FIXME: Allow user to tailor where the file is written. |
| 1330 | return CreateASTSerializer(InFile, OutputFile, Diag); |
| 1331 | |
| 1332 | case RewriteObjC: |
| 1333 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1335 | case RewriteBlocks: |
| 1336 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
| 1337 | |
| 1338 | case RunAnalysis: |
| 1339 | return CreateAnalysisConsumer(Diag, PP, PPF, LangOpts, OutputFile); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1343 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1344 | /// |
Ted Kremenek | 339b9c2 | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1345 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1346 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1347 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1348 | bool ClearSourceMgr = false; |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame^] | 1349 | FixItRewriter *FixItRewrite = 0; |
| 1350 | |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1351 | switch (PA) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1352 | default: |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1353 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1354 | PP.getFileManager(), PP.getLangOptions(), |
| 1355 | &PP, &PPF)); |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1356 | |
| 1357 | if (!Consumer) { |
| 1358 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1359 | HadErrors = true; |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1360 | return; |
| 1361 | } |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1362 | |
Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1363 | break; |
| 1364 | |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1365 | case DumpRawTokens: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1366 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1367 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1368 | // Start lexing the specified input file. |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 1369 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1370 | RawLex.SetKeepWhitespaceMode(true); |
| 1371 | |
| 1372 | Token RawTok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1373 | RawLex.LexFromRawLexer(RawTok); |
| 1374 | while (RawTok.isNot(tok::eof)) { |
| 1375 | PP.DumpToken(RawTok, true); |
| 1376 | fprintf(stderr, "\n"); |
| 1377 | RawLex.LexFromRawLexer(RawTok); |
| 1378 | } |
| 1379 | ClearSourceMgr = true; |
| 1380 | break; |
| 1381 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1382 | case DumpTokens: { // Token dump mode. |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1383 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1384 | Token Tok; |
Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1385 | // Start preprocessing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1386 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1387 | do { |
| 1388 | PP.Lex(Tok); |
| 1389 | PP.DumpToken(Tok, true); |
| 1390 | fprintf(stderr, "\n"); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1391 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1392 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1393 | break; |
| 1394 | } |
| 1395 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1396 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1397 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1398 | // Start parsing the specified input file. |
Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1399 | PP.EnterMainSourceFile(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1400 | do { |
| 1401 | PP.Lex(Tok); |
Chris Lattner | 057aaf6 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1402 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1403 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1404 | break; |
| 1405 | } |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1406 | |
| 1407 | case GeneratePCH: { |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1408 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1409 | CacheTokens(PP, OutputFile); |
| 1410 | ClearSourceMgr = true; |
| 1411 | break; |
| 1412 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1414 | case PrintPreprocessedInput: { // -E mode. |
| 1415 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | e988bc2 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 1416 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1417 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1419 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1420 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1421 | case ParseNoop: { // -parse-noop |
| 1422 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1423 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1424 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1425 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1426 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1427 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1428 | case ParsePrintCallbacks: { |
| 1429 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | e10b0f2 | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 1430 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1431 | ClearSourceMgr = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | case ParseSyntaxOnly: { // -fsyntax-only |
| 1436 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Ted Kremenek | 7e7e625 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1437 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 2bf5514 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 1438 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1439 | } |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1440 | |
| 1441 | case RewriteMacros: |
Chris Lattner | 0951052 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 1442 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 1443 | ClearSourceMgr = true; |
| 1444 | break; |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1445 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1446 | case RewriteTest: { |
Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 1447 | DoRewriteTest(PP, InFile, OutputFile); |
| 1448 | ClearSourceMgr = true; |
| 1449 | break; |
Chris Lattner | 580980b | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 1450 | } |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame^] | 1451 | |
| 1452 | case FixIt: |
| 1453 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 1454 | Consumer.reset(new ASTConsumer()); |
| 1455 | FixItRewrite = new FixItRewriter(PP.getDiagnostics().getClient(), |
| 1456 | PP.getSourceManager()); |
| 1457 | PP.getDiagnostics().setClient(FixItRewrite); |
| 1458 | break; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1459 | } |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1460 | |
| 1461 | if (Consumer) { |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1462 | llvm::OwningPtr<ASTContext> ContextOwner; |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1463 | |
| 1464 | ContextOwner.reset(new ASTContext(PP.getLangOptions(), |
| 1465 | PP.getSourceManager(), |
| 1466 | PP.getTargetInfo(), |
| 1467 | PP.getIdentifierTable(), |
| 1468 | PP.getSelectorTable(), |
| 1469 | /* FreeMemory = */ !DisableFree)); |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1470 | |
| 1471 | |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 1472 | ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats); |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1473 | |
Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame^] | 1474 | if (FixItRewrite) |
| 1475 | FixItRewrite->WriteFixedFile(InFile, OutputFile); |
| 1476 | |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1477 | // If in -disable-free mode, don't deallocate these when they go out of |
| 1478 | // scope. |
Chris Lattner | 3599dbe | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 1479 | if (DisableFree) |
Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 1480 | ContextOwner.take(); |
Ted Kremenek | 46157b5 | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 1481 | } |
Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (VerifyDiagnostics) |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1484 | if (CheckDiagnostics(PP)) |
| 1485 | exit(1); |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1486 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1487 | if (Stats) { |
Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1488 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1489 | PP.PrintStats(); |
| 1490 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1491 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 1492 | PP.getSourceManager().PrintStats(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1493 | fprintf(stderr, "\n"); |
| 1494 | } |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 1495 | |
| 1496 | // For a multi-file compilation, some things are ok with nuking the source |
| 1497 | // manager tables, other require stable fileid/macroid's across multiple |
| 1498 | // files. |
Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1499 | if (ClearSourceMgr) |
| 1500 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | d68ba0e | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 1501 | |
| 1502 | if (DisableFree) |
| 1503 | Consumer.take(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1506 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 1507 | FileManager& FileMgr) { |
| 1508 | |
| 1509 | if (VerifyDiagnostics) { |
| 1510 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 1511 | exit (1); |
| 1512 | } |
| 1513 | |
| 1514 | llvm::sys::Path Filename(InFile); |
| 1515 | |
| 1516 | if (!Filename.isValid()) { |
| 1517 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 1518 | exit (1); |
| 1519 | } |
| 1520 | |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1521 | llvm::OwningPtr<ASTContext> Ctx; |
Chris Lattner | 5f737cc | 2009-03-28 03:49:26 +0000 | [diff] [blame] | 1522 | |
| 1523 | // Create the memory buffer that contains the contents of the file. |
| 1524 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 1525 | MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); |
| 1526 | |
| 1527 | if (MBuffer) |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1528 | Ctx.reset(ASTContext::ReadASTBitcodeBuffer(*MBuffer, FileMgr)); |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1530 | if (!Ctx) { |
Ted Kremenek | fe4e015 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 1531 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 1532 | InFile.c_str()); |
| 1533 | exit (1); |
| 1534 | } |
| 1535 | |
Ted Kremenek | 63ea863 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 1536 | // Observe that we use the source file name stored in the deserialized |
| 1537 | // translation unit, rather than InFile. |
Ted Kremenek | ee53364 | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 1538 | llvm::OwningPtr<ASTConsumer> |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1539 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, Ctx->getLangOptions(), |
Ted Kremenek | 815c78f | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1540 | 0, 0)); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1541 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1542 | if (!Consumer) { |
| 1543 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 1544 | exit (1); |
| 1545 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1546 | |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1547 | Consumer->Initialize(*Ctx); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1549 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Chris Lattner | 557c5b1 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 1550 | TranslationUnitDecl *TUD = Ctx->getTranslationUnitDecl(); |
| 1551 | for (DeclContext::decl_iterator I = TUD->decls_begin(), E = TUD->decls_end(); |
| 1552 | I != E; ++I) |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1553 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1557 | static llvm::cl::list<std::string> |
| 1558 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 1559 | |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1560 | static bool isSerializedFile(const std::string& InFile) { |
| 1561 | if (InFile.size() < 4) |
| 1562 | return false; |
| 1563 | |
| 1564 | const char* s = InFile.c_str()+InFile.size()-4; |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1565 | return s[0] == '.' && s[1] == 'a' && s[2] == 's' && s[3] == 't'; |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | |
| 1569 | int main(int argc, char **argv) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1570 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 1571 | llvm::PrettyStackTraceProgram X(argc, argv); |
Chris Lattner | dc76310 | 2009-03-06 05:38:04 +0000 | [diff] [blame] | 1572 | llvm::cl::ParseCommandLineOptions(argc, argv, |
Chris Lattner | 110e478 | 2009-03-06 05:38:25 +0000 | [diff] [blame] | 1573 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1574 | |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1575 | if (TimeReport) |
| 1576 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
| 1577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1578 | // If no input was specified, read from stdin. |
| 1579 | if (InputFilenames.empty()) |
| 1580 | InputFilenames.push_back("-"); |
Chris Lattner | b2509e1 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 1581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | // Create a file manager object to provide access to and cache the filesystem. |
| 1583 | FileManager FileMgr; |
| 1584 | |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1585 | // Create the diagnostic client for reporting errors or for |
| 1586 | // implementing -verify. |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1587 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | 88f5cde | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 1588 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1589 | if (!VerifyDiagnostics) { |
| 1590 | // Print diagnostics to stderr by default. |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 1591 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 1592 | !NoShowColumn, |
Chris Lattner | 65f5e64 | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 1593 | !NoCaretDiagnostics, |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 1594 | !NoShowLocation, |
| 1595 | PrintSourceRangeInfo); |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1596 | } else { |
| 1597 | // When checking diagnostics, just buffer them up. |
| 1598 | TextDiagClient = new TextDiagnosticBuffer(); |
| 1599 | |
| 1600 | if (InputFilenames.size() != 1) { |
| 1601 | fprintf(stderr, |
| 1602 | "-verify only works on single input files for now.\n"); |
| 1603 | return 1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1604 | } |
| 1605 | } |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1606 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1607 | // Configure our handling of diagnostics. |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1608 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 1609 | Diagnostic Diags(DiagClient.get()); |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 1610 | if (ProcessWarningOptions(Diags)) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 1611 | return 1; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1613 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 1614 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 1615 | if (I_dirs[i] == "-") { |
Chris Lattner | 5917fe1 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 1616 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1617 | I_dirs.erase(I_dirs.begin()+i); |
| 1618 | --i; |
| 1619 | } |
| 1620 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Get information about the target being compiled for. |
| 1623 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1624 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 1625 | |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1626 | if (Target == 0) { |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 1627 | Diags.Report(FullSourceLoc(), diag::err_fe_unknown_triple) |
| 1628 | << Triple.c_str(); |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 1629 | return 1; |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1630 | } |
Chris Lattner | 4f03783 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 1631 | |
Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1632 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1633 | ProgAction = InheritanceView; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1634 | |
Ted Kremenek | c0c03bc | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 1635 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 1636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1638 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1639 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1640 | if (isSerializedFile(InFile)) { |
| 1641 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1642 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1643 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1644 | } |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1645 | |
| 1646 | /// Create a SourceManager object. This tracks and owns all the file |
| 1647 | /// buffers allocated to a translation unit. |
| 1648 | if (!SourceMgr) |
| 1649 | SourceMgr.reset(new SourceManager()); |
| 1650 | else |
| 1651 | SourceMgr->clearIDTables(); |
| 1652 | |
| 1653 | // Initialize language options, inferring file types from input filenames. |
| 1654 | LangOptions LangInfo; |
| 1655 | InitializeBaseLanguage(); |
| 1656 | LangKind LK = GetLanguage(InFile); |
Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 1657 | InitializeLangOptions(LangInfo, LK); |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1658 | InitializeGCMode(LangInfo); |
Mike Stump | 2add473 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 1659 | InitializeOverflowChecking(LangInfo); |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1660 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
| 1661 | |
| 1662 | // Process the -I options and set them in the HeaderInfo. |
| 1663 | HeaderSearch HeaderInfo(FileMgr); |
| 1664 | |
| 1665 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
| 1666 | |
| 1667 | // Set up the preprocessor with these options. |
| 1668 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
| 1669 | *SourceMgr.get(), HeaderInfo); |
| 1670 | |
| 1671 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 1672 | |
| 1673 | if (!PP) |
| 1674 | continue; |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 1675 | |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1676 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 1677 | // always reset to using TextDiagClient. |
| 1678 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
| 1679 | |
| 1680 | if (!HTMLDiag.empty()) { |
| 1681 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 1682 | &PPFactory)); |
| 1683 | Diags.setClient(TmpClient.get()); |
Ted Kremenek | 20e9748 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 1684 | } |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1685 | else |
| 1686 | Diags.setClient(TextDiagClient); |
| 1687 | |
| 1688 | // Process the source file. |
Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 1689 | ProcessInputFile(*PP, PPFactory, InFile, ProgAction); |
Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1690 | |
| 1691 | HeaderInfo.ClearFileInfo(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1692 | } |
Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1693 | |
Mike Stump | 007f2a9 | 2009-01-28 02:43:35 +0000 | [diff] [blame] | 1694 | if (Verbose) |
| 1695 | fprintf(stderr, "clang version 1.0 based upon " PACKAGE_STRING |
| 1696 | " hosted on " LLVM_HOSTTRIPLE "\n"); |
| 1697 | |
Ted Kremenek | 7a08e28 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 1698 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1699 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1700 | (NumDiagnostics == 1 ? "" : "s")); |
| 1701 | |
| 1702 | if (Stats) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1703 | FileMgr.PrintStats(); |
| 1704 | fprintf(stderr, "\n"); |
| 1705 | } |
| 1706 | |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1707 | // If verifying diagnostics and we reached here, all is well. |
| 1708 | if (VerifyDiagnostics) |
| 1709 | return 0; |
Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1710 | |
| 1711 | delete ClangFrontendTimer; |
Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1712 | |
Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1713 | // Managed static deconstruction. Useful for making things like |
| 1714 | // -time-passes usable. |
| 1715 | llvm::llvm_shutdown(); |
| 1716 | |
Daniel Dunbar | b0adbba | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1717 | return HadErrors || (Diags.getNumErrors() != 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1718 | } |