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