Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- clang.cpp - C-Language Front-end ---------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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 | 39fb14e | 2009-02-18 01:17:01 +0000 | [diff] [blame] | 20 | // -Wfatal-errors |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | // -ftabstop=width |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
Ted Kremenek | 377a319 | 2009-03-31 18:58:14 +0000 | [diff] [blame] | 25 | #include "clang-cc.h" |
Chris Lattner | eb8c963 | 2007-10-07 06:04:32 +0000 | [diff] [blame] | 26 | #include "ASTConsumers.h" |
Daniel Dunbar | 68952de | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/CompileOptions.h" |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FixItRewriter.h" |
Daniel Dunbar | f45afe6 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 68952de | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/InitHeaderSearch.h" |
Daniel Dunbar | f45afe6 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/PathDiagnosticClients.h" |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/PCHReader.h" |
Daniel Dunbar | 68952de | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 34 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 35 | #include "clang/Analysis/PathDiagnostic.h" |
Chris Lattner | f5e9db0 | 2008-02-06 02:01:47 +0000 | [diff] [blame] | 36 | #include "clang/CodeGen/ModuleBuilder.h" |
Chris Lattner | 0bed6ec | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ParseAST.h" |
Chris Lattner | 86a2484 | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 38 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | 1cc0171 | 2007-09-15 22:56:56 +0000 | [diff] [blame] | 39 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | fc31819 | 2009-03-28 04:31:31 +0000 | [diff] [blame] | 40 | #include "clang/AST/ASTContext.h" |
| 41 | #include "clang/AST/Decl.h" |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 42 | #include "clang/AST/DeclGroup.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | #include "clang/Parse/Parser.h" |
| 44 | #include "clang/Lex/HeaderSearch.h" |
Chris Lattner | 71af6d6 | 2009-02-06 04:16:41 +0000 | [diff] [blame] | 45 | #include "clang/Lex/LexDiagnostic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | #include "clang/Basic/FileManager.h" |
| 47 | #include "clang/Basic/SourceManager.h" |
| 48 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/OwningPtr.h" |
Chris Lattner | ac139d2 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 50 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 51 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 53 | #include "llvm/Config/config.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CommandLine.h" |
Daniel Dunbar | bb298c0 | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 55 | #include "llvm/Support/ManagedStatic.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | #include "llvm/Support/MemoryBuffer.h" |
Zhongxing Xu | f4ec4d9 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 57 | #include "llvm/Support/PluginLoader.h" |
Chris Lattner | 2b2d0c4 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 58 | #include "llvm/Support/PrettyStackTrace.h" |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Timer.h" |
Daniel Dunbar | abe2e54 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 60 | #include "llvm/System/Host.h" |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 61 | #include "llvm/System/Path.h" |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 62 | #include "llvm/System/Signals.h" |
Douglas Gregor | 24b48b0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 63 | #include <cstdlib> |
| 64 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 65 | using namespace clang; |
| 66 | |
| 67 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 24b48b0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 68 | // Source Location Parser |
| 69 | //===----------------------------------------------------------------------===// |
| 70 | |
| 71 | /// \brief A source location that has been parsed on the command line. |
| 72 | struct ParsedSourceLocation { |
| 73 | std::string FileName; |
| 74 | unsigned Line; |
| 75 | unsigned Column; |
| 76 | |
| 77 | /// \brief Try to resolve the file name of a parsed source location. |
| 78 | /// |
| 79 | /// \returns true if there was an error, false otherwise. |
| 80 | bool ResolveLocation(FileManager &FileMgr, RequestedSourceLocation &Result); |
| 81 | }; |
| 82 | |
| 83 | bool |
| 84 | ParsedSourceLocation::ResolveLocation(FileManager &FileMgr, |
| 85 | RequestedSourceLocation &Result) { |
| 86 | const FileEntry *File = FileMgr.getFile(FileName); |
| 87 | if (!File) |
| 88 | return true; |
| 89 | |
| 90 | Result.File = File; |
| 91 | Result.Line = Line; |
| 92 | Result.Column = Column; |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | namespace llvm { |
| 97 | namespace cl { |
| 98 | /// \brief Command-line option parser that parses source locations. |
| 99 | /// |
| 100 | /// Source locations are of the form filename:line:column. |
| 101 | template<> |
| 102 | class parser<ParsedSourceLocation> |
| 103 | : public basic_parser<ParsedSourceLocation> { |
| 104 | public: |
| 105 | bool parse(Option &O, const char *ArgName, |
| 106 | const std::string &ArgValue, |
| 107 | ParsedSourceLocation &Val); |
| 108 | }; |
| 109 | |
| 110 | bool |
| 111 | parser<ParsedSourceLocation>:: |
| 112 | parse(Option &O, const char *ArgName, const std::string &ArgValue, |
| 113 | ParsedSourceLocation &Val) { |
| 114 | using namespace clang; |
| 115 | |
| 116 | const char *ExpectedFormat |
| 117 | = "source location must be of the form filename:line:column"; |
| 118 | std::string::size_type SecondColon = ArgValue.rfind(':'); |
| 119 | if (SecondColon == std::string::npos) { |
| 120 | std::fprintf(stderr, "%s\n", ExpectedFormat); |
| 121 | return true; |
| 122 | } |
| 123 | char *EndPtr; |
| 124 | long Column |
| 125 | = std::strtol(ArgValue.c_str() + SecondColon + 1, &EndPtr, 10); |
| 126 | if (EndPtr != ArgValue.c_str() + ArgValue.size()) { |
| 127 | std::fprintf(stderr, "%s\n", ExpectedFormat); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | std::string::size_type FirstColon = ArgValue.rfind(':', SecondColon-1); |
| 132 | if (SecondColon == std::string::npos) { |
| 133 | std::fprintf(stderr, "%s\n", ExpectedFormat); |
| 134 | return true; |
| 135 | } |
| 136 | long Line = std::strtol(ArgValue.c_str() + FirstColon + 1, &EndPtr, 10); |
| 137 | if (EndPtr != ArgValue.c_str() + SecondColon) { |
| 138 | std::fprintf(stderr, "%s\n", ExpectedFormat); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | Val.FileName = ArgValue.substr(0, FirstColon); |
| 143 | Val.Line = Line; |
| 144 | Val.Column = Column; |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 151 | // Global options. |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 154 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 155 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 156 | /// anything. |
| 157 | llvm::Timer *ClangFrontendTimer = 0; |
| 158 | |
Daniel Dunbar | 4efedde | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 159 | static bool HadErrors = false; |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 160 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 161 | static llvm::cl::opt<bool> |
| 162 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 163 | static llvm::cl::opt<bool> |
Nate Begeman | 6acbedd | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 164 | Stats("print-stats", |
| 165 | llvm::cl::desc("Print performance metrics and statistics")); |
Daniel Dunbar | 4efedde | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 166 | static llvm::cl::opt<bool> |
| 167 | DisableFree("disable-free", |
| 168 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 169 | llvm::cl::init(false)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 170 | |
| 171 | enum ProgActions { |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 172 | RewriteObjC, // ObjC->C Rewriter. |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 173 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 174 | RewriteMacros, // Expand macros but not #includes. |
Chris Lattner | c3fbf39 | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 175 | RewriteTest, // Rewriter playground |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 176 | FixIt, // Fix-It Rewriter |
Ted Kremenek | e1a79d8 | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 177 | HTMLTest, // HTML displayer testing stuff. |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 178 | EmitAssembly, // Emit a .s file. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 179 | EmitLLVM, // Emit a .ll file. |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 180 | EmitBC, // Emit a .bc file. |
Daniel Dunbar | d999a8e | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 181 | EmitLLVMOnly, // Generate LLVM IR, but do not |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 182 | SerializeAST, // Emit a .ast file. |
Ted Kremenek | 24612ae | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 183 | EmitHTML, // Translate input source into HTML. |
Chris Lattner | 4045a8a | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 184 | ASTPrint, // Parse ASTs and print them. |
| 185 | ASTDump, // Parse ASTs and dump them. |
| 186 | ASTView, // Parse ASTs and view them in Graphviz. |
Zhongxing Xu | 6036bbe | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 187 | PrintDeclContext, // Print DeclContext and their Decls. |
Ted Kremenek | 221bb8d | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 188 | TestSerialization, // Run experimental serialization code. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 189 | ParsePrintCallbacks, // Parse and print each callback. |
| 190 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 191 | ParseNoop, // Parse with noop callbacks. |
| 192 | RunPreprocessorOnly, // Just lex, no output. |
| 193 | PrintPreprocessedInput, // -E mode. |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 194 | DumpTokens, // Dump out preprocessed tokens. |
| 195 | DumpRawTokens, // Dump out raw tokens. |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 196 | RunAnalysis, // Run one or more source code analyses. |
Douglas Gregor | 2a0e874 | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 197 | GeneratePTH, // Generate pre-tokenized header. |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 198 | GeneratePCH, // Generate pre-compiled header. |
Ted Kremenek | d9ceb3d | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 199 | InheritanceView // View C++ inheritance for a specified class. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | static llvm::cl::opt<ProgActions> |
| 203 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 204 | llvm::cl::init(ParseSyntaxOnly), |
| 205 | llvm::cl::values( |
| 206 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 207 | "Just run preprocessor, no output (for timings)"), |
| 208 | clEnumValN(PrintPreprocessedInput, "E", |
| 209 | "Run preprocessor, emit preprocessed file"), |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 210 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 211 | "Lex file in raw mode and dump raw tokens"), |
Daniel Dunbar | 9c32110 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 212 | clEnumValN(RunAnalysis, "analyze", |
| 213 | "Run static analysis engine"), |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 214 | clEnumValN(DumpTokens, "dump-tokens", |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 215 | "Run preprocessor, dump internal rep of tokens"), |
| 216 | clEnumValN(ParseNoop, "parse-noop", |
| 217 | "Run parser with noop callbacks (for timings)"), |
| 218 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 219 | "Run parser and perform semantic analysis"), |
| 220 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 221 | "Run parser and print each callback invoked"), |
Ted Kremenek | 24612ae | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 222 | clEnumValN(EmitHTML, "emit-html", |
| 223 | "Output input source as HTML"), |
Chris Lattner | 4045a8a | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 224 | clEnumValN(ASTPrint, "ast-print", |
| 225 | "Build ASTs and then pretty-print them"), |
| 226 | clEnumValN(ASTDump, "ast-dump", |
| 227 | "Build ASTs and then debug dump them"), |
Chris Lattner | 664dd08 | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 228 | clEnumValN(ASTView, "ast-view", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 229 | "Build ASTs and view them with GraphViz"), |
Zhongxing Xu | 6036bbe | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 230 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
Ted Kremenek | 4787dcf | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 231 | "Print DeclContexts and their Decls"), |
Douglas Gregor | 2a0e874 | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 232 | clEnumValN(GeneratePTH, "emit-pth", |
Ted Kremenek | 4787dcf | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 233 | "Generate pre-tokenized header file"), |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 234 | clEnumValN(GeneratePCH, "emit-pch", |
| 235 | "Generate pre-compiled header file"), |
Ted Kremenek | 221bb8d | 2007-10-16 23:37:27 +0000 | [diff] [blame] | 236 | clEnumValN(TestSerialization, "test-pickling", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 237 | "Run prototype serialization code"), |
Daniel Dunbar | 85e44e2 | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 238 | clEnumValN(EmitAssembly, "S", |
| 239 | "Emit native assembly code"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 240 | clEnumValN(EmitLLVM, "emit-llvm", |
Ted Kremenek | 0533468 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 241 | "Build ASTs then convert to LLVM, emit .ll file"), |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 242 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 243 | "Build ASTs then convert to LLVM, emit .bc file"), |
Daniel Dunbar | d999a8e | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 244 | clEnumValN(EmitLLVMOnly, "emit-llvm-only", |
| 245 | "Build ASTs and convert to LLVM, discarding output"), |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 246 | clEnumValN(SerializeAST, "serialize", |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 247 | "Build ASTs and emit .ast file"), |
Chris Lattner | c3fbf39 | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 248 | clEnumValN(RewriteTest, "rewrite-test", |
| 249 | "Rewriter playground"), |
Steve Naroff | 44e8122 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 250 | clEnumValN(RewriteObjC, "rewrite-objc", |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 251 | "Rewrite ObjC into C (code rewriter example)"), |
| 252 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 253 | "Expand macros without full preprocessing"), |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 254 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 255 | "Rewrite Blocks to C"), |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 256 | clEnumValN(FixIt, "fixit", |
| 257 | "Apply fix-it advice to the input source"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 258 | clEnumValEnd)); |
| 259 | |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 260 | |
| 261 | static llvm::cl::opt<std::string> |
| 262 | OutputFile("o", |
Ted Kremenek | 09b3f0d | 2007-12-19 19:50:41 +0000 | [diff] [blame] | 263 | llvm::cl::value_desc("path"), |
Ted Kremenek | d01eae6 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 264 | llvm::cl::desc("Specify output file (for --serialize, this is a directory)")); |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 57f25b2 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 266 | |
| 267 | //===----------------------------------------------------------------------===// |
| 268 | // PTH. |
| 269 | //===----------------------------------------------------------------------===// |
| 270 | |
| 271 | static llvm::cl::opt<std::string> |
| 272 | TokenCache("token-cache", llvm::cl::value_desc("path"), |
| 273 | llvm::cl::desc("Use specified token cache file")); |
| 274 | |
Sanjiv Gupta | 40e56a1 | 2008-05-08 08:54:20 +0000 | [diff] [blame] | 275 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 517cb51 | 2008-04-14 18:40:58 +0000 | [diff] [blame] | 276 | // Diagnostic Options |
| 277 | //===----------------------------------------------------------------------===// |
| 278 | |
Ted Kremenek | 10389cf | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 279 | static llvm::cl::opt<bool> |
| 280 | VerifyDiagnostics("verify", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 281 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
Ted Kremenek | 10389cf | 2007-09-26 19:42:19 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 283 | static llvm::cl::opt<std::string> |
| 284 | HTMLDiag("html-diags", |
| 285 | llvm::cl::desc("Generate HTML to report diagnostics"), |
| 286 | llvm::cl::value_desc("HTML directory")); |
| 287 | |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 288 | static llvm::cl::opt<bool> |
| 289 | NoShowColumn("fno-show-column", |
| 290 | llvm::cl::desc("Do not include column number on diagnostics")); |
| 291 | |
| 292 | static llvm::cl::opt<bool> |
Chris Lattner | b96a04f | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 293 | NoShowLocation("fno-show-source-location", |
| 294 | llvm::cl::desc("Do not include source location information with" |
| 295 | " diagnostics")); |
| 296 | |
| 297 | static llvm::cl::opt<bool> |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 298 | NoCaretDiagnostics("fno-caret-diagnostics", |
| 299 | llvm::cl::desc("Do not include source line and caret with" |
| 300 | " diagnostics")); |
| 301 | |
Chris Lattner | 695a4f5 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 302 | static llvm::cl::opt<bool> |
| 303 | PrintSourceRangeInfo("fprint-source-range-info", |
| 304 | llvm::cl::desc("Print source range spans in numeric form")); |
| 305 | |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 306 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 307 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d9ceb3d | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 308 | // C++ Visualization. |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | |
| 311 | static llvm::cl::opt<std::string> |
| 312 | InheritanceViewCls("cxx-inheritance-view", |
| 313 | llvm::cl::value_desc("class name"), |
Daniel Dunbar | 47f0b0f | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 314 | llvm::cl::desc("View C++ inheritance for a specified class")); |
Ted Kremenek | d9ceb3d | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 315 | |
| 316 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 317 | // Builtin Options |
| 318 | //===----------------------------------------------------------------------===// |
Chris Lattner | 93d4d98 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 319 | |
| 320 | static llvm::cl::opt<bool> |
| 321 | TimeReport("ftime-report", |
| 322 | llvm::cl::desc("Print the amount of time each " |
| 323 | "phase of compilation takes")); |
| 324 | |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 325 | static llvm::cl::opt<bool> |
| 326 | Freestanding("ffreestanding", |
Daniel Dunbar | fd46ea2 | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 327 | llvm::cl::desc("Assert that the compilation takes place in a " |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 328 | "freestanding environment")); |
| 329 | |
Daniel Dunbar | fd46ea2 | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 330 | static llvm::cl::opt<bool> |
Daniel Dunbar | 9d805ce | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 331 | AllowBuiltins("fbuiltin", llvm::cl::init(true), |
| 332 | llvm::cl::desc("Disable implicit builtin knowledge of functions")); |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 333 | |
| 334 | |
| 335 | static llvm::cl::opt<bool> |
Daniel Dunbar | 9d805ce | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 336 | MathErrno("fmath-errno", llvm::cl::init(true), |
| 337 | llvm::cl::desc("Require math functions to respect errno")); |
Daniel Dunbar | fd46ea2 | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 338 | |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 339 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 340 | // Language Options |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
| 343 | enum LangKind { |
| 344 | langkind_unspecified, |
| 345 | langkind_c, |
| 346 | langkind_c_cpp, |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 347 | langkind_asm_cpp, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 348 | langkind_cxx, |
| 349 | langkind_cxx_cpp, |
| 350 | langkind_objc, |
| 351 | langkind_objc_cpp, |
| 352 | langkind_objcxx, |
Daniel Dunbar | db6126e | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 353 | langkind_objcxx_cpp |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 356 | static llvm::cl::opt<LangKind> |
| 357 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 358 | llvm::cl::init(langkind_unspecified), |
| 359 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| 360 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 361 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 362 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
Daniel Dunbar | b148859 | 2009-01-29 23:50:47 +0000 | [diff] [blame] | 363 | clEnumValN(langkind_c_cpp, "cpp-output", |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 364 | "Preprocessed C"), |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 365 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 366 | "Preprocessed asm"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 367 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
Chris Lattner | b5555ea | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 368 | "Preprocessed C++"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 369 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 370 | "Preprocessed Objective C"), |
Chris Lattner | b5555ea | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 371 | clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output", |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 372 | "Preprocessed Objective C++"), |
Daniel Dunbar | db6126e | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 373 | clEnumValN(langkind_c, "c-header", |
| 374 | "C header"), |
| 375 | clEnumValN(langkind_objc, "objective-c-header", |
| 376 | "Objective-C header"), |
| 377 | clEnumValN(langkind_cxx, "c++-header", |
| 378 | "C++ header"), |
| 379 | clEnumValN(langkind_objcxx, "objective-c++-header", |
| 380 | "Objective-C++ header"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 381 | clEnumValEnd)); |
| 382 | |
| 383 | static llvm::cl::opt<bool> |
| 384 | LangObjC("ObjC", llvm::cl::desc("Set base language to Objective-C"), |
| 385 | llvm::cl::Hidden); |
| 386 | static llvm::cl::opt<bool> |
| 387 | LangObjCXX("ObjC++", llvm::cl::desc("Set base language to Objective-C++"), |
| 388 | llvm::cl::Hidden); |
| 389 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 390 | /// InitializeBaseLanguage - Handle the -x foo options. |
| 391 | static void InitializeBaseLanguage() { |
| 392 | if (LangObjC) |
| 393 | BaseLang = langkind_objc; |
| 394 | else if (LangObjCXX) |
| 395 | BaseLang = langkind_objcxx; |
| 396 | } |
| 397 | |
| 398 | static LangKind GetLanguage(const std::string &Filename) { |
| 399 | if (BaseLang != langkind_unspecified) |
| 400 | return BaseLang; |
| 401 | |
| 402 | std::string::size_type DotPos = Filename.rfind('.'); |
| 403 | |
| 404 | if (DotPos == std::string::npos) { |
| 405 | BaseLang = langkind_c; // Default to C if no extension. |
Chris Lattner | 4eac050 | 2008-01-04 19:12:28 +0000 | [diff] [blame] | 406 | return langkind_c; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 409 | std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); |
| 410 | // C header: .h |
| 411 | // C++ header: .hh or .H; |
| 412 | // assembler no preprocessing: .s |
| 413 | // assembler: .S |
| 414 | if (Ext == "c") |
| 415 | return langkind_c; |
Chris Lattner | 5dc0c1b | 2009-03-23 16:24:37 +0000 | [diff] [blame] | 416 | else if (Ext == "S" || |
| 417 | // If the compiler is run on a .s file, preprocess it as .S |
| 418 | Ext == "s") |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 419 | return langkind_asm_cpp; |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 420 | else if (Ext == "i") |
| 421 | return langkind_c_cpp; |
| 422 | else if (Ext == "ii") |
| 423 | return langkind_cxx_cpp; |
| 424 | else if (Ext == "m") |
| 425 | return langkind_objc; |
| 426 | else if (Ext == "mi") |
| 427 | return langkind_objc_cpp; |
| 428 | else if (Ext == "mm" || Ext == "M") |
| 429 | return langkind_objcxx; |
| 430 | else if (Ext == "mii") |
| 431 | return langkind_objcxx_cpp; |
| 432 | else if (Ext == "C" || Ext == "cc" || Ext == "cpp" || Ext == "CPP" || |
| 433 | Ext == "c++" || Ext == "cp" || Ext == "cxx") |
| 434 | return langkind_cxx; |
| 435 | else |
| 436 | return langkind_c; |
| 437 | } |
| 438 | |
| 439 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 440 | static void InitializeCOptions(LangOptions &Options) { |
| 441 | // Do nothing. |
| 442 | } |
| 443 | |
| 444 | static void InitializeObjCOptions(LangOptions &Options) { |
| 445 | Options.ObjC1 = Options.ObjC2 = 1; |
| 446 | } |
| 447 | |
| 448 | |
Daniel Dunbar | db6126e | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 449 | static void InitializeLangOptions(LangOptions &Options, LangKind LK){ |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 450 | // FIXME: implement -fpreprocessed mode. |
| 451 | bool NoPreprocess = false; |
| 452 | |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 453 | switch (LK) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 454 | default: assert(0 && "Unknown language kind!"); |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 455 | case langkind_asm_cpp: |
Daniel Dunbar | 20b8802 | 2008-12-01 18:55:22 +0000 | [diff] [blame] | 456 | Options.AsmPreprocessor = 1; |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 457 | // FALLTHROUGH |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 458 | case langkind_c_cpp: |
| 459 | NoPreprocess = true; |
| 460 | // FALLTHROUGH |
| 461 | case langkind_c: |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 462 | InitializeCOptions(Options); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 463 | break; |
| 464 | case langkind_cxx_cpp: |
| 465 | NoPreprocess = true; |
| 466 | // FALLTHROUGH |
| 467 | case langkind_cxx: |
| 468 | Options.CPlusPlus = 1; |
| 469 | break; |
| 470 | case langkind_objc_cpp: |
| 471 | NoPreprocess = true; |
| 472 | // FALLTHROUGH |
| 473 | case langkind_objc: |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 474 | InitializeObjCOptions(Options); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 475 | break; |
| 476 | case langkind_objcxx_cpp: |
| 477 | NoPreprocess = true; |
| 478 | // FALLTHROUGH |
| 479 | case langkind_objcxx: |
| 480 | Options.ObjC1 = Options.ObjC2 = 1; |
| 481 | Options.CPlusPlus = 1; |
| 482 | break; |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | /// LangStds - Language standards we support. |
| 487 | enum LangStds { |
| 488 | lang_unspecified, |
| 489 | lang_c89, lang_c94, lang_c99, |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 490 | lang_gnu_START, |
| 491 | lang_gnu89 = lang_gnu_START, lang_gnu99, |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 492 | lang_cxx98, lang_gnucxx98, |
| 493 | lang_cxx0x, lang_gnucxx0x |
| 494 | }; |
| 495 | |
| 496 | static llvm::cl::opt<LangStds> |
| 497 | LangStd("std", llvm::cl::desc("Language standard to compile for"), |
| 498 | llvm::cl::init(lang_unspecified), |
| 499 | llvm::cl::values(clEnumValN(lang_c89, "c89", "ISO C 1990"), |
| 500 | clEnumValN(lang_c89, "c90", "ISO C 1990"), |
| 501 | clEnumValN(lang_c89, "iso9899:1990", "ISO C 1990"), |
| 502 | clEnumValN(lang_c94, "iso9899:199409", |
| 503 | "ISO C 1990 with amendment 1"), |
| 504 | clEnumValN(lang_c99, "c99", "ISO C 1999"), |
Chris Lattner | ddeb740 | 2009-04-06 17:17:55 +0000 | [diff] [blame] | 505 | clEnumValN(lang_c99, "c9x", "ISO C 1999"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 506 | clEnumValN(lang_c99, "iso9899:1999", "ISO C 1999"), |
Chris Lattner | ddeb740 | 2009-04-06 17:17:55 +0000 | [diff] [blame] | 507 | clEnumValN(lang_c99, "iso9899:199x", "ISO C 1999"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 508 | clEnumValN(lang_gnu89, "gnu89", |
Gabor Greif | be1618a | 2009-02-28 09:22:15 +0000 | [diff] [blame] | 509 | "ISO C 1990 with GNU extensions"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 510 | clEnumValN(lang_gnu99, "gnu99", |
Gabor Greif | be1618a | 2009-02-28 09:22:15 +0000 | [diff] [blame] | 511 | "ISO C 1999 with GNU extensions (default for C)"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 512 | clEnumValN(lang_gnu99, "gnu9x", |
| 513 | "ISO C 1999 with GNU extensions"), |
| 514 | clEnumValN(lang_cxx98, "c++98", |
| 515 | "ISO C++ 1998 with amendments"), |
| 516 | clEnumValN(lang_gnucxx98, "gnu++98", |
| 517 | "ISO C++ 1998 with amendments and GNU " |
| 518 | "extensions (default for C++)"), |
| 519 | clEnumValN(lang_cxx0x, "c++0x", |
| 520 | "Upcoming ISO C++ 200x with amendments"), |
| 521 | clEnumValN(lang_gnucxx0x, "gnu++0x", |
| 522 | "Upcoming ISO C++ 200x with amendments and GNU " |
Gabor Greif | 4661228 | 2009-03-11 23:07:18 +0000 | [diff] [blame] | 523 | "extensions"), |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 524 | clEnumValEnd)); |
| 525 | |
| 526 | static llvm::cl::opt<bool> |
| 527 | NoOperatorNames("fno-operator-names", |
| 528 | llvm::cl::desc("Do not treat C++ operator name keywords as " |
| 529 | "synonyms for operators")); |
| 530 | |
Anders Carlsson | 55bfe0d | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 531 | static llvm::cl::opt<bool> |
| 532 | PascalStrings("fpascal-strings", |
| 533 | llvm::cl::desc("Recognize and construct Pascal-style " |
| 534 | "string literals")); |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 535 | |
| 536 | static llvm::cl::opt<bool> |
| 537 | MSExtensions("fms-extensions", |
| 538 | llvm::cl::desc("Accept some non-standard constructs used in " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 539 | "Microsoft header files ")); |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 540 | |
| 541 | static llvm::cl::opt<bool> |
| 542 | WritableStrings("fwritable-strings", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 543 | llvm::cl::desc("Store string literals as writable data")); |
Anders Carlsson | e87cd98 | 2007-11-30 04:21:22 +0000 | [diff] [blame] | 544 | |
| 545 | static llvm::cl::opt<bool> |
Anders Carlsson | 6cf61c9 | 2009-01-30 23:26:40 +0000 | [diff] [blame] | 546 | NoLaxVectorConversions("fno-lax-vector-conversions", |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 547 | llvm::cl::desc("Disallow implicit conversions between " |
| 548 | "vectors with a different number of " |
| 549 | "elements or different element types")); |
Chris Lattner | d7bc88b | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 550 | |
Fariborz Jahanian | 48543f5 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 551 | static llvm::cl::opt<bool> |
Daniel Dunbar | 9d805ce | 2009-04-07 21:16:11 +0000 | [diff] [blame] | 552 | EnableBlocks("fblocks", llvm::cl::desc("enable the 'blocks' language feature")); |
Mike Stump | 9093c74 | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 553 | |
| 554 | static llvm::cl::opt<bool> |
Chris Lattner | 1e3eedb | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 555 | EnableHeinousExtensions("fheinous-gnu-extensions", |
| 556 | llvm::cl::desc("enable GNU extensions that you really really shouldn't use"), |
| 557 | llvm::cl::ValueDisallowed, llvm::cl::Hidden); |
| 558 | |
| 559 | static llvm::cl::opt<bool> |
Mike Stump | 9093c74 | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 560 | ObjCNonFragileABI("fobjc-nonfragile-abi", |
| 561 | llvm::cl::desc("enable objective-c's nonfragile abi")); |
Fariborz Jahanian | 48543f5 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 562 | |
Daniel Dunbar | 9bae865 | 2009-02-04 21:19:06 +0000 | [diff] [blame] | 563 | static llvm::cl::opt<bool> |
| 564 | EmitAllDecls("femit-all-decls", |
| 565 | llvm::cl::desc("Emit all declarations, even if unused")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 566 | |
Daniel Dunbar | 91692d9 | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 567 | // FIXME: This (and all GCC -f options) really come in -f... and |
| 568 | // -fno-... forms, and additionally support automagic behavior when |
| 569 | // they are not defined. For example, -fexceptions defaults to on or |
| 570 | // off depending on the language. We should support this behavior in |
| 571 | // some form (perhaps just add a facility for distinguishing when an |
| 572 | // has its default value from when it has been set to its default |
| 573 | // value). |
| 574 | static llvm::cl::opt<bool> |
| 575 | Exceptions("fexceptions", |
| 576 | llvm::cl::desc("Enable support for exception handling.")); |
| 577 | |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 578 | static llvm::cl::opt<bool> |
| 579 | GNURuntime("fgnu-runtime", |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 580 | llvm::cl::desc("Generate output compatible with the standard GNU " |
| 581 | "Objective-C runtime.")); |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 582 | |
| 583 | static llvm::cl::opt<bool> |
| 584 | NeXTRuntime("fnext-runtime", |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 585 | llvm::cl::desc("Generate output compatible with the NeXT " |
| 586 | "runtime.")); |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 587 | |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 588 | |
| 589 | |
| 590 | static llvm::cl::opt<bool> |
| 591 | Trigraphs("trigraphs", llvm::cl::desc("Process trigraph sequences.")); |
| 592 | |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 593 | static llvm::cl::list<std::string> |
Chris Lattner | f6790a8 | 2009-03-03 19:56:18 +0000 | [diff] [blame] | 594 | TargetFeatures("mattr", llvm::cl::CommaSeparated, |
| 595 | llvm::cl::desc("Target specific attributes (-mattr=help for details)")); |
| 596 | |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 597 | static llvm::cl::opt<unsigned> |
| 598 | TemplateDepth("ftemplate-depth", llvm::cl::init(99), |
| 599 | llvm::cl::desc("Maximum depth of recursive template " |
| 600 | "instantiation")); |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 601 | |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 602 | |
| 603 | static llvm::cl::opt<bool> |
| 604 | OptSize("Os", llvm::cl::desc("Optimize for size")); |
| 605 | |
| 606 | static llvm::cl::opt<bool> |
| 607 | NoCommon("fno-common", |
| 608 | llvm::cl::desc("Compile common globals like normal definitions"), |
| 609 | llvm::cl::ValueDisallowed); |
| 610 | |
Daniel Dunbar | dedc94c | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 611 | static llvm::cl::opt<std::string> |
| 612 | MainFileName("main-file-name", |
| 613 | llvm::cl::desc("Main file name to use for debug info")); |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 614 | |
| 615 | // It might be nice to add bounds to the CommandLine library directly. |
| 616 | struct OptLevelParser : public llvm::cl::parser<unsigned> { |
| 617 | bool parse(llvm::cl::Option &O, const char *ArgName, |
| 618 | const std::string &Arg, unsigned &Val) { |
| 619 | if (llvm::cl::parser<unsigned>::parse(O, ArgName, Arg, Val)) |
| 620 | return true; |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 621 | if (Val > 3) |
| 622 | return O.error(": '" + Arg + "' invalid optimization level!"); |
| 623 | return false; |
| 624 | } |
| 625 | }; |
| 626 | static llvm::cl::opt<unsigned, false, OptLevelParser> |
| 627 | OptLevel("O", llvm::cl::Prefix, |
| 628 | llvm::cl::desc("Optimization level"), |
| 629 | llvm::cl::init(0)); |
| 630 | |
Daniel Dunbar | e079c71 | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 631 | static llvm::cl::opt<unsigned> |
Daniel Dunbar | cdd3c76 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 632 | PICLevel("pic-level", llvm::cl::desc("Value for __PIC__")); |
| 633 | |
| 634 | static llvm::cl::opt<bool> |
| 635 | StaticDefine("static-define", llvm::cl::desc("Should __STATIC__ be defined")); |
Daniel Dunbar | e079c71 | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 636 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 637 | // FIXME: add: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 638 | // -fdollars-in-identifiers |
Daniel Dunbar | 3454295 | 2008-08-23 08:43:39 +0000 | [diff] [blame] | 639 | static void InitializeLanguageStandard(LangOptions &Options, LangKind LK, |
| 640 | TargetInfo *Target) { |
Chris Lattner | ddae710 | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 641 | // Allow the target to set the default the langauge options as it sees fit. |
| 642 | Target->getDefaultLangOptions(Options); |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 643 | |
Chris Lattner | f6790a8 | 2009-03-03 19:56:18 +0000 | [diff] [blame] | 644 | // If there are any -mattr options, pass them to the target for validation and |
| 645 | // processing. The driver should have already consolidated all the |
| 646 | // target-feature settings and passed them to us in the -mattr list. The |
| 647 | // -mattr list is treated by the code generator as a diff against the -mcpu |
| 648 | // setting, but the driver should pass all enabled options as "+" settings. |
| 649 | // This means that the target should only look at + settings. |
| 650 | if (!TargetFeatures.empty() |
| 651 | // FIXME: The driver is not quite yet ready for this. |
| 652 | && 0) { |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 653 | std::string ErrorStr; |
Chris Lattner | f6790a8 | 2009-03-03 19:56:18 +0000 | [diff] [blame] | 654 | int Opt = Target->HandleTargetFeatures(&TargetFeatures[0], |
| 655 | TargetFeatures.size(), ErrorStr); |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 656 | if (Opt != -1) { |
| 657 | if (ErrorStr.empty()) |
Chris Lattner | f6790a8 | 2009-03-03 19:56:18 +0000 | [diff] [blame] | 658 | fprintf(stderr, "invalid feature '%s'\n", |
| 659 | TargetFeatures[Opt].c_str()); |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 660 | else |
Chris Lattner | f6790a8 | 2009-03-03 19:56:18 +0000 | [diff] [blame] | 661 | fprintf(stderr, "feature '%s': %s\n", |
| 662 | TargetFeatures[Opt].c_str(), ErrorStr.c_str()); |
Chris Lattner | 738d3f6 | 2009-03-02 22:11:07 +0000 | [diff] [blame] | 663 | exit(1); |
| 664 | } |
| 665 | } |
| 666 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 667 | if (LangStd == lang_unspecified) { |
| 668 | // Based on the base language, pick one. |
Ted Kremenek | 11ad895 | 2007-12-05 23:49:08 +0000 | [diff] [blame] | 669 | switch (LK) { |
Ted Kremenek | 9bd3431 | 2009-03-19 19:02:20 +0000 | [diff] [blame] | 670 | case lang_unspecified: assert(0 && "Unknown base language"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 671 | case langkind_c: |
Chris Lattner | a19689a | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 672 | case langkind_asm_cpp: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 673 | case langkind_c_cpp: |
| 674 | case langkind_objc: |
| 675 | case langkind_objc_cpp: |
| 676 | LangStd = lang_gnu99; |
| 677 | break; |
| 678 | case langkind_cxx: |
| 679 | case langkind_cxx_cpp: |
| 680 | case langkind_objcxx: |
| 681 | case langkind_objcxx_cpp: |
| 682 | LangStd = lang_gnucxx98; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | switch (LangStd) { |
| 688 | default: assert(0 && "Unknown language standard!"); |
| 689 | |
| 690 | // Fall through from newer standards to older ones. This isn't really right. |
| 691 | // FIXME: Enable specifically the right features based on the language stds. |
| 692 | case lang_gnucxx0x: |
| 693 | case lang_cxx0x: |
| 694 | Options.CPlusPlus0x = 1; |
| 695 | // FALL THROUGH |
| 696 | case lang_gnucxx98: |
| 697 | case lang_cxx98: |
| 698 | Options.CPlusPlus = 1; |
| 699 | Options.CXXOperatorNames = !NoOperatorNames; |
Nate Begeman | ca89334 | 2007-11-15 07:30:50 +0000 | [diff] [blame] | 700 | Options.Boolean = 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 701 | // FALL THROUGH. |
| 702 | case lang_gnu99: |
| 703 | case lang_c99: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 704 | Options.C99 = 1; |
| 705 | Options.HexFloats = 1; |
| 706 | // FALL THROUGH. |
| 707 | case lang_gnu89: |
| 708 | Options.BCPLComment = 1; // Only for C99/C++. |
| 709 | // FALL THROUGH. |
| 710 | case lang_c94: |
Chris Lattner | 0297c76 | 2008-02-25 04:01:39 +0000 | [diff] [blame] | 711 | Options.Digraphs = 1; // C94, C99, C++. |
| 712 | // FALL THROUGH. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 713 | case lang_c89: |
| 714 | break; |
| 715 | } |
Argiris Kirtzidis | b151955 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 716 | |
Chris Lattner | 31e7f6f | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 717 | // GNUMode - Set if we're in gnu99, gnu89, gnucxx98, etc. |
| 718 | Options.GNUMode = LangStd >= lang_gnu_START; |
| 719 | |
Argiris Kirtzidis | b151955 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 720 | if (Options.CPlusPlus) { |
| 721 | Options.C99 = 0; |
Chris Lattner | 31e7f6f | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 722 | Options.HexFloats = Options.GNUMode; |
Argiris Kirtzidis | b151955 | 2008-09-11 04:21:06 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 724 | |
Chris Lattner | 6ab935b | 2008-04-05 06:32:51 +0000 | [diff] [blame] | 725 | if (LangStd == lang_c89 || LangStd == lang_c94 || LangStd == lang_gnu89) |
| 726 | Options.ImplicitInt = 1; |
| 727 | else |
| 728 | Options.ImplicitInt = 0; |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 729 | |
Daniel Dunbar | facf351 | 2009-04-07 22:13:21 +0000 | [diff] [blame] | 730 | // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs |
| 731 | // is specified, or -std is set to a conforming mode. |
Chris Lattner | 31e7f6f | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 732 | Options.Trigraphs = !Options.GNUMode; |
Chris Lattner | 58d5ba5 | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 733 | if (Trigraphs.getPosition()) |
Chris Lattner | 31e7f6f | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 734 | Options.Trigraphs = Trigraphs; // Command line option wins if specified. |
Ted Kremenek | 88bec0f | 2008-09-03 21:22:16 +0000 | [diff] [blame] | 735 | |
Chris Lattner | 58d5ba5 | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 736 | // If in a conformant language mode (e.g. -std=c99) Blocks defaults to off |
| 737 | // even if they are normally on for the target. In GNU modes (e.g. |
| 738 | // -std=gnu99) the default for blocks depends on the target settings. |
Anders Carlsson | 8ffcf73 | 2009-01-21 18:47:36 +0000 | [diff] [blame] | 739 | // However, blocks are not turned off when compiling Obj-C or Obj-C++ code. |
Chris Lattner | 31e7f6f | 2009-03-20 15:44:26 +0000 | [diff] [blame] | 740 | if (!Options.ObjC1 && !Options.GNUMode) |
Chris Lattner | 58d5ba5 | 2008-12-05 00:10:44 +0000 | [diff] [blame] | 741 | Options.Blocks = 0; |
| 742 | |
Daniel Dunbar | 551236b | 2009-03-15 00:11:28 +0000 | [diff] [blame] | 743 | // Never accept '$' in identifiers when preprocessing assembler. |
| 744 | if (LK != langkind_asm_cpp) |
| 745 | Options.DollarIdents = 1; // FIXME: Really a target property. |
Chris Lattner | d7bc88b | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 746 | if (PascalStrings.getPosition()) |
| 747 | Options.PascalStrings = PascalStrings; |
Steve Naroff | 73a0703 | 2008-02-07 03:50:06 +0000 | [diff] [blame] | 748 | Options.Microsoft = MSExtensions; |
Chris Lattner | db6be56 | 2007-11-28 05:34:05 +0000 | [diff] [blame] | 749 | Options.WritableStrings = WritableStrings; |
Anders Carlsson | 355ed05 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 750 | if (NoLaxVectorConversions.getPosition()) |
| 751 | Options.LaxVectorConversions = 0; |
Daniel Dunbar | 91692d9 | 2008-08-11 17:36:14 +0000 | [diff] [blame] | 752 | Options.Exceptions = Exceptions; |
Mike Stump | 9093c74 | 2009-02-02 22:57:57 +0000 | [diff] [blame] | 753 | if (EnableBlocks.getPosition()) |
Chris Lattner | d7bc88b | 2008-12-04 23:20:07 +0000 | [diff] [blame] | 754 | Options.Blocks = EnableBlocks; |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 755 | |
Daniel Dunbar | 73e8e03 | 2009-03-20 23:49:28 +0000 | [diff] [blame] | 756 | if (!AllowBuiltins) |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 757 | Options.NoBuiltin = 1; |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 758 | if (Freestanding) |
Chris Lattner | 911b867 | 2009-03-13 22:38:49 +0000 | [diff] [blame] | 759 | Options.Freestanding = Options.NoBuiltin = 1; |
| 760 | |
Chris Lattner | 1e3eedb | 2009-03-13 17:38:01 +0000 | [diff] [blame] | 761 | if (EnableHeinousExtensions) |
| 762 | Options.HeinousExtensions = 1; |
Douglas Gregor | 23d2326 | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 763 | |
Daniel Dunbar | fd46ea2 | 2009-02-16 22:43:43 +0000 | [diff] [blame] | 764 | Options.MathErrno = MathErrno; |
| 765 | |
Douglas Gregor | 375733c | 2009-03-10 00:06:19 +0000 | [diff] [blame] | 766 | Options.InstantiationDepth = TemplateDepth; |
| 767 | |
Chris Lattner | ddae710 | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 768 | // Override the default runtime if the user requested it. |
| 769 | if (NeXTRuntime) |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 770 | Options.NeXTRuntime = 1; |
Chris Lattner | ddae710 | 2008-12-04 22:54:33 +0000 | [diff] [blame] | 771 | else if (GNURuntime) |
Daniel Dunbar | 1be1df3 | 2008-08-11 21:35:06 +0000 | [diff] [blame] | 772 | Options.NeXTRuntime = 0; |
Fariborz Jahanian | 48543f5 | 2009-01-21 22:04:16 +0000 | [diff] [blame] | 773 | |
Fariborz Jahanian | d037481 | 2009-01-22 23:02:58 +0000 | [diff] [blame] | 774 | if (ObjCNonFragileABI) |
| 775 | Options.ObjCNonFragileABI = 1; |
Daniel Dunbar | 9bae865 | 2009-02-04 21:19:06 +0000 | [diff] [blame] | 776 | |
| 777 | if (EmitAllDecls) |
| 778 | Options.EmitAllDecls = 1; |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 779 | |
Daniel Dunbar | cdd3c76 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 780 | // The __OPTIMIZE_SIZE__ define is tied to -Oz, which we don't |
| 781 | // support. |
| 782 | Options.OptimizeSize = 0; |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 783 | |
| 784 | // -Os implies -O2 |
Daniel Dunbar | cdd3c76 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 785 | if (OptSize || OptLevel) |
Anders Carlsson | dfd7764 | 2009-04-06 17:37:10 +0000 | [diff] [blame] | 786 | Options.Optimize = 1; |
Daniel Dunbar | e079c71 | 2009-04-08 03:03:23 +0000 | [diff] [blame] | 787 | |
| 788 | assert(PICLevel <= 2 && "Invalid value for -pic-level"); |
| 789 | Options.PICLevel = PICLevel; |
Daniel Dunbar | dedc94c | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 790 | |
Daniel Dunbar | cdd3c76 | 2009-04-08 18:03:55 +0000 | [diff] [blame] | 791 | Options.GNUInline = !Options.C99; |
| 792 | // FIXME: This is affected by other options (-fno-inline). |
| 793 | Options.NoInline = !OptSize && !OptLevel; |
| 794 | |
| 795 | Options.Static = StaticDefine; |
| 796 | |
Daniel Dunbar | dedc94c | 2009-04-08 05:11:16 +0000 | [diff] [blame] | 797 | if (MainFileName.getPosition()) |
| 798 | Options.setMainFileName(MainFileName.c_str()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 801 | static llvm::cl::opt<bool> |
| 802 | ObjCExclusiveGC("fobjc-gc-only", |
| 803 | llvm::cl::desc("Use GC exclusively for Objective-C related " |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 804 | "memory management")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 805 | |
| 806 | static llvm::cl::opt<bool> |
| 807 | ObjCEnableGC("fobjc-gc", |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 808 | llvm::cl::desc("Enable Objective-C garbage collection")); |
Ted Kremenek | 2658c4a | 2008-04-29 04:37:03 +0000 | [diff] [blame] | 809 | |
| 810 | void InitializeGCMode(LangOptions &Options) { |
| 811 | if (ObjCExclusiveGC) |
| 812 | Options.setGCMode(LangOptions::GCOnly); |
| 813 | else if (ObjCEnableGC) |
| 814 | Options.setGCMode(LangOptions::HybridGC); |
| 815 | } |
| 816 | |
Fariborz Jahanian | 4934333 | 2009-04-03 03:28:57 +0000 | [diff] [blame] | 817 | static llvm::cl::opt<std::string> |
| 818 | SymbolVisibility("fvisibility", |
| 819 | llvm::cl::desc("Set the default visibility to the specific option")); |
| 820 | |
| 821 | void InitializeSymbolVisibility(LangOptions &Options) { |
| 822 | if (SymbolVisibility.empty()) |
| 823 | return; |
| 824 | std::string Visibility = SymbolVisibility; |
| 825 | const char *vkind = Visibility.c_str(); |
| 826 | if (!strcmp(vkind, "default")) |
| 827 | Options.setVisibilityMode(LangOptions::DefaultVisibility); |
| 828 | else if (!strcmp(vkind, "protected")) |
| 829 | Options.setVisibilityMode(LangOptions::ProtectedVisibility); |
| 830 | else if (!strcmp(vkind, "hidden")) |
| 831 | Options.setVisibilityMode(LangOptions::HiddenVisibility); |
| 832 | else if (!strcmp(vkind, "internal")) |
| 833 | Options.setVisibilityMode(LangOptions::InternalVisibility); |
| 834 | else |
| 835 | fprintf(stderr, |
| 836 | "-fvisibility only valid for default|protected|hidden|internal\n"); |
| 837 | } |
| 838 | |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 839 | static llvm::cl::opt<bool> |
| 840 | OverflowChecking("ftrapv", |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 841 | llvm::cl::desc("Trap on integer overflow"), |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 842 | llvm::cl::init(false)); |
| 843 | |
| 844 | void InitializeOverflowChecking(LangOptions &Options) { |
Mike Stump | f71b774 | 2009-04-02 18:15:54 +0000 | [diff] [blame] | 845 | Options.OverflowChecking = OverflowChecking; |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 846 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 847 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 848 | // Target Triple Processing. |
| 849 | //===----------------------------------------------------------------------===// |
| 850 | |
| 851 | static llvm::cl::opt<std::string> |
| 852 | TargetTriple("triple", |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 853 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 854 | |
Chris Lattner | fc45700 | 2008-03-05 01:18:20 +0000 | [diff] [blame] | 855 | static llvm::cl::opt<std::string> |
Sanjiv Gupta | 6968353 | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 856 | Arch("arch", llvm::cl::desc("Specify target architecture (e.g. i686)")); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 857 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 858 | static llvm::cl::opt<std::string> |
| 859 | MacOSVersionMin("mmacosx-version-min", |
Daniel Dunbar | 93f6453 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 860 | llvm::cl::desc("Specify target Mac OS X version (e.g. 10.5)")); |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 861 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 862 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 863 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Daniel Dunbar | 4d36d98 | 2009-03-31 20:10:05 +0000 | [diff] [blame] | 864 | |
| 865 | // FIXME: We should have the driver do this instead. |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 866 | static void HandleMacOSVersionMin(std::string &Triple) { |
| 867 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 868 | if (DarwinDashIdx == std::string::npos) { |
| 869 | fprintf(stderr, |
Daniel Dunbar | 93f6453 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 870 | "-mmacosx-version-min only valid for darwin (Mac OS X) targets\n"); |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 871 | exit(1); |
| 872 | } |
| 873 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 874 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 875 | // Remove the number. |
| 876 | Triple.resize(DarwinNumIdx); |
| 877 | |
| 878 | // Validate that MacOSVersionMin is a 'version number', starting with 10.[3-9] |
| 879 | bool MacOSVersionMinIsInvalid = false; |
| 880 | int VersionNum = 0; |
| 881 | if (MacOSVersionMin.size() < 4 || |
| 882 | MacOSVersionMin.substr(0, 3) != "10." || |
| 883 | !isdigit(MacOSVersionMin[3])) { |
| 884 | MacOSVersionMinIsInvalid = true; |
| 885 | } else { |
| 886 | const char *Start = MacOSVersionMin.c_str()+3; |
| 887 | char *End = 0; |
| 888 | VersionNum = (int)strtol(Start, &End, 10); |
| 889 | |
Chris Lattner | d376f6d | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 890 | // The version number must be in the range 0-9. |
| 891 | MacOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 892 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 893 | // Turn MacOSVersionMin into a darwin number: e.g. 10.3.9 is 3 -> 7. |
| 894 | Triple += llvm::itostr(VersionNum+4); |
| 895 | |
Chris Lattner | d376f6d | 2008-09-30 20:30:12 +0000 | [diff] [blame] | 896 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 10.4.7 is ok. |
| 897 | // Add the period piece (.7) to the end of the triple. This gives us |
| 898 | // something like ...-darwin8.7 |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 899 | Triple += End; |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 900 | } else if (End[0] != '\0') { // "10.4" is ok. 10.4x is not. |
| 901 | MacOSVersionMinIsInvalid = true; |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | if (MacOSVersionMinIsInvalid) { |
| 906 | fprintf(stderr, |
Daniel Dunbar | 7fe2af6 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 907 | "-mmacosx-version-min=%s is invalid, expected something like '10.4'.\n", |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 908 | MacOSVersionMin.c_str()); |
| 909 | exit(1); |
| 910 | } |
Fariborz Jahanian | 27c08a5 | 2009-04-10 20:33:45 +0000 | [diff] [blame] | 911 | else if (VersionNum <= 4 && |
| 912 | !strncmp(Triple.c_str(), "x86_64", strlen("x86_64"))) { |
| 913 | fprintf(stderr, |
| 914 | "-mmacosx-version-min=%s is invalid with -arch x86_64.\n", |
| 915 | MacOSVersionMin.c_str()); |
| 916 | exit(1); |
| 917 | } |
| 918 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Daniel Dunbar | 93f6453 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 921 | static llvm::cl::opt<std::string> |
| 922 | IPhoneOSVersionMin("miphoneos-version-min", |
| 923 | llvm::cl::desc("Specify target iPhone OS version (e.g. 2.0)")); |
| 924 | |
| 925 | // If -miphoneos-version-min=2.2 is specified, change the triple from being |
| 926 | // something like armv6-apple-darwin10 to armv6-apple-darwin9.2.2. We use |
| 927 | // 9 as the default major Darwin number, and encode the iPhone OS version |
| 928 | // number in the minor version and revision. |
| 929 | |
| 930 | // FIXME: We should have the driver do this instead. |
| 931 | static void HandleIPhoneOSVersionMin(std::string &Triple) { |
| 932 | std::string::size_type DarwinDashIdx = Triple.find("-darwin"); |
| 933 | if (DarwinDashIdx == std::string::npos) { |
| 934 | fprintf(stderr, |
| 935 | "-miphoneos-version-min only valid for darwin (Mac OS X) targets\n"); |
| 936 | exit(1); |
| 937 | } |
| 938 | unsigned DarwinNumIdx = DarwinDashIdx + strlen("-darwin"); |
| 939 | |
| 940 | // Remove the number. |
| 941 | Triple.resize(DarwinNumIdx); |
| 942 | |
| 943 | // Validate that IPhoneOSVersionMin is a 'version number', starting with [2-9].[0-9] |
| 944 | bool IPhoneOSVersionMinIsInvalid = false; |
| 945 | int VersionNum = 0; |
| 946 | if (IPhoneOSVersionMin.size() < 3 || |
| 947 | !isdigit(IPhoneOSVersionMin[0])) { |
| 948 | IPhoneOSVersionMinIsInvalid = true; |
| 949 | } else { |
| 950 | const char *Start = IPhoneOSVersionMin.c_str(); |
| 951 | char *End = 0; |
| 952 | VersionNum = (int)strtol(Start, &End, 10); |
| 953 | |
| 954 | // The version number must be in the range 0-9. |
| 955 | IPhoneOSVersionMinIsInvalid = (unsigned)VersionNum > 9; |
| 956 | |
| 957 | // Turn IPhoneOSVersionMin into a darwin number: e.g. 2.0 is 2 -> 9.2. |
| 958 | Triple += "9." + llvm::itostr(VersionNum); |
| 959 | |
| 960 | if (End[0] == '.' && isdigit(End[1]) && End[2] == '\0') { // 2.2 is ok. |
| 961 | // Add the period piece (.2) to the end of the triple. This gives us |
| 962 | // something like ...-darwin9.2.2 |
| 963 | Triple += End; |
| 964 | } else if (End[0] != '\0') { // "2.2" is ok. 2x is not. |
| 965 | IPhoneOSVersionMinIsInvalid = true; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | if (IPhoneOSVersionMinIsInvalid) { |
| 970 | fprintf(stderr, |
| 971 | "-miphoneos-version-min=%s is invalid, expected something like '2.0'.\n", |
| 972 | IPhoneOSVersionMin.c_str()); |
| 973 | exit(1); |
| 974 | } |
| 975 | } |
| 976 | |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 977 | /// CreateTargetTriple - Process the various options that affect the target |
| 978 | /// triple and build a final aggregate triple that we are compiling for. |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 979 | static std::string CreateTargetTriple() { |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 980 | // Initialize base triple. If a -triple option has been specified, use |
| 981 | // that triple. Otherwise, default to the host triple. |
Chris Lattner | 210c0cc | 2007-12-12 05:01:48 +0000 | [diff] [blame] | 982 | std::string Triple = TargetTriple; |
Daniel Dunbar | 7fe2af6 | 2009-03-31 17:35:15 +0000 | [diff] [blame] | 983 | if (Triple.empty()) |
| 984 | Triple = llvm::sys::getHostTriple(); |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 985 | |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 986 | // If -arch foo was specified, remove the architecture from the triple we have |
| 987 | // so far and replace it with the specified one. |
Daniel Dunbar | 4d36d98 | 2009-03-31 20:10:05 +0000 | [diff] [blame] | 988 | |
| 989 | // FIXME: -arch should be removed, the driver should handle this. |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 990 | if (!Arch.empty()) { |
| 991 | // Decompose the base triple into "arch" and suffix. |
| 992 | std::string::size_type FirstDashIdx = Triple.find('-'); |
Chris Lattner | f3d79c3 | 2008-03-09 01:35:13 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 994 | if (FirstDashIdx == std::string::npos) { |
| 995 | fprintf(stderr, |
| 996 | "Malformed target triple: \"%s\" ('-' could not be found).\n", |
| 997 | Triple.c_str()); |
| 998 | exit(1); |
| 999 | } |
Chris Lattner | f6cde9f | 2009-03-24 16:18:41 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Canonicalize -arch ppc to add "powerpc" to the triple, not ppc. |
| 1002 | if (Arch == "ppc") |
| 1003 | Arch = "powerpc"; |
| 1004 | else if (Arch == "ppc64") |
| 1005 | Arch = "powerpc64"; |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 1006 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 1007 | Triple = Arch + std::string(Triple.begin()+FirstDashIdx, Triple.end()); |
| 1008 | } |
| 1009 | |
| 1010 | // If -mmacosx-version-min=10.3.9 is specified, change the triple from being |
| 1011 | // something like powerpc-apple-darwin9 to powerpc-apple-darwin7 |
Chris Lattner | 01de9c8 | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 1012 | if (!MacOSVersionMin.empty()) |
| 1013 | HandleMacOSVersionMin(Triple); |
Daniel Dunbar | 93f6453 | 2009-04-10 19:52:24 +0000 | [diff] [blame] | 1014 | else if (!IPhoneOSVersionMin.empty()) |
| 1015 | HandleIPhoneOSVersionMin(Triple);; |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 2b168e0 | 2008-09-30 01:13:12 +0000 | [diff] [blame] | 1017 | return Triple; |
Ted Kremenek | 4049948 | 2007-12-03 22:06:55 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1021 | // Preprocessor Initialization |
| 1022 | //===----------------------------------------------------------------------===// |
| 1023 | |
| 1024 | // FIXME: Preprocessor builtins to support. |
| 1025 | // -A... - Play with #assertions |
| 1026 | // -undef - Undefine all predefined macros |
| 1027 | |
Chris Lattner | 695a4f5 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 1028 | // FIXME: -imacros |
| 1029 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1030 | static llvm::cl::list<std::string> |
| 1031 | D_macros("D", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 1032 | llvm::cl::desc("Predefine the specified macro")); |
| 1033 | static llvm::cl::list<std::string> |
| 1034 | U_macros("U", llvm::cl::value_desc("macro"), llvm::cl::Prefix, |
| 1035 | llvm::cl::desc("Undefine the specified macro")); |
| 1036 | |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 1037 | static llvm::cl::list<std::string> |
| 1038 | ImplicitIncludes("include", llvm::cl::value_desc("file"), |
| 1039 | llvm::cl::desc("Include file before parsing")); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1040 | static llvm::cl::list<std::string> |
| 1041 | ImplicitMacroIncludes("imacros", llvm::cl::value_desc("file"), |
| 1042 | llvm::cl::desc("Include macros from file before parsing")); |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 1043 | |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1044 | static llvm::cl::opt<std::string> |
| 1045 | ImplicitIncludePTH("include-pth", llvm::cl::value_desc("file"), |
| 1046 | llvm::cl::desc("Include file before parsing")); |
| 1047 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1048 | static llvm::cl::opt<std::string> |
| 1049 | ImplicitIncludePCH("include-pch", llvm::cl::value_desc("file"), |
| 1050 | llvm::cl::desc("Include precompiled header file")); |
| 1051 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1052 | // Append a #define line to Buf for Macro. Macro should be of the form XXX, |
| 1053 | // in which case we emit "#define XXX 1" or "XXX=Y z W" in which case we emit |
| 1054 | // "#define XXX Y z W". To get a #define with no value, use "XXX=". |
| 1055 | static void DefineBuiltinMacro(std::vector<char> &Buf, const char *Macro, |
| 1056 | const char *Command = "#define ") { |
| 1057 | Buf.insert(Buf.end(), Command, Command+strlen(Command)); |
| 1058 | if (const char *Equal = strchr(Macro, '=')) { |
| 1059 | // Turn the = into ' '. |
| 1060 | Buf.insert(Buf.end(), Macro, Equal); |
| 1061 | Buf.push_back(' '); |
Chris Lattner | 39baafb | 2009-04-07 06:02:44 +0000 | [diff] [blame] | 1062 | |
| 1063 | // Per GCC -D semantics, the macro ends at \n if it exists. |
| 1064 | const char *End = strpbrk(Equal, "\n\r"); |
Chris Lattner | 39c610d | 2009-04-07 18:18:09 +0000 | [diff] [blame] | 1065 | if (End) { |
Chris Lattner | d93f790 | 2009-04-08 03:36:03 +0000 | [diff] [blame] | 1066 | fprintf(stderr, "warning: macro '%s' contains embedded newline, text " |
Chris Lattner | 39c610d | 2009-04-07 18:18:09 +0000 | [diff] [blame] | 1067 | "after the newline is ignored.\n", |
| 1068 | std::string(Macro, Equal).c_str()); |
| 1069 | } else { |
| 1070 | End = Equal+strlen(Equal); |
| 1071 | } |
Chris Lattner | 39baafb | 2009-04-07 06:02:44 +0000 | [diff] [blame] | 1072 | |
| 1073 | Buf.insert(Buf.end(), Equal+1, End); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1074 | } else { |
| 1075 | // Push "macroname 1". |
| 1076 | Buf.insert(Buf.end(), Macro, Macro+strlen(Macro)); |
| 1077 | Buf.push_back(' '); |
| 1078 | Buf.push_back('1'); |
| 1079 | } |
| 1080 | Buf.push_back('\n'); |
| 1081 | } |
| 1082 | |
Daniel Dunbar | 43b9169 | 2009-04-09 00:51:16 +0000 | [diff] [blame] | 1083 | /// Add the quoted name of an implicit include file. |
| 1084 | static void AddQuotedIncludePath(std::vector<char> &Buf, |
| 1085 | const std::string &File) { |
| 1086 | // Implicit include paths are relative to the current working |
| 1087 | // directory; resolve them now instead of using the normal machinery |
| 1088 | // (which would look relative to the input file). |
| 1089 | llvm::sys::Path Path(File); |
| 1090 | Path.makeAbsolute(); |
| 1091 | |
Chris Lattner | 6ac92a3 | 2009-04-08 20:10:57 +0000 | [diff] [blame] | 1092 | // Escape double quotes etc. |
Daniel Dunbar | 43b9169 | 2009-04-09 00:51:16 +0000 | [diff] [blame] | 1093 | Buf.push_back('"'); |
| 1094 | std::string EscapedFile = Lexer::Stringify(Path.toString()); |
Chris Lattner | 6ac92a3 | 2009-04-08 20:10:57 +0000 | [diff] [blame] | 1095 | Buf.insert(Buf.end(), EscapedFile.begin(), EscapedFile.end()); |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 1096 | Buf.push_back('"'); |
Daniel Dunbar | 43b9169 | 2009-04-09 00:51:16 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
| 1099 | /// AddImplicitInclude - Add an implicit #include of the specified file to the |
| 1100 | /// predefines buffer. |
| 1101 | static void AddImplicitInclude(std::vector<char> &Buf, |
| 1102 | const std::string &File) { |
| 1103 | const char *Inc = "#include "; |
| 1104 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
| 1105 | AddQuotedIncludePath(Buf, File); |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 1106 | Buf.push_back('\n'); |
| 1107 | } |
| 1108 | |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1109 | static void AddImplicitIncludeMacros(std::vector<char> &Buf, |
| 1110 | const std::string &File) { |
Daniel Dunbar | 43b9169 | 2009-04-09 00:51:16 +0000 | [diff] [blame] | 1111 | const char *Inc = "#__include_macros "; |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1112 | Buf.insert(Buf.end(), Inc, Inc+strlen(Inc)); |
Daniel Dunbar | 43b9169 | 2009-04-09 00:51:16 +0000 | [diff] [blame] | 1113 | AddQuotedIncludePath(Buf, File); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1114 | Buf.push_back('\n'); |
Chris Lattner | a6e4411 | 2009-04-08 20:53:24 +0000 | [diff] [blame] | 1115 | // Marker token to stop the __include_macros fetch loop. |
| 1116 | const char *Marker = "##\n"; // ##? |
| 1117 | Buf.insert(Buf.end(), Marker, Marker+strlen(Marker)); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1120 | /// AddImplicitIncludePTH - Add an implicit #include using the original file |
| 1121 | /// used to generate a PTH cache. |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1122 | static void AddImplicitIncludePTH(std::vector<char> &Buf, Preprocessor &PP) { |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1123 | PTHManager *P = PP.getPTHManager(); |
| 1124 | assert(P && "No PTHManager."); |
| 1125 | const char *OriginalFile = P->getOriginalSourceFile(); |
| 1126 | |
| 1127 | if (!OriginalFile) { |
| 1128 | assert(!ImplicitIncludePTH.empty()); |
| 1129 | fprintf(stderr, "error: PTH file '%s' does not designate an original " |
| 1130 | "source header file for -include-pth\n", |
| 1131 | ImplicitIncludePTH.c_str()); |
| 1132 | exit (1); |
| 1133 | } |
| 1134 | |
| 1135 | AddImplicitInclude(Buf, OriginalFile); |
| 1136 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1137 | |
Chris Lattner | a9a0dd5 | 2009-04-10 21:58:23 +0000 | [diff] [blame] | 1138 | /// PickFP - This is used to pick a value based on the FP semantics of the |
| 1139 | /// specified FP model. |
| 1140 | template <typename T> |
| 1141 | static T PickFP(const llvm::fltSemantics *Sem, T IEEESingleVal, |
| 1142 | T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal) { |
| 1143 | if (Sem == &llvm::APFloat::IEEEsingle) |
| 1144 | return IEEESingleVal; |
| 1145 | if (Sem == &llvm::APFloat::IEEEdouble) |
| 1146 | return IEEEDoubleVal; |
| 1147 | if (Sem == &llvm::APFloat::x87DoubleExtended) |
| 1148 | return X87DoubleExtendedVal; |
| 1149 | assert(Sem == &llvm::APFloat::PPCDoubleDouble); |
| 1150 | return PPCDoubleDoubleVal; |
| 1151 | } |
| 1152 | |
| 1153 | static void DefineFloatMacros(std::vector<char> &Buf, const char *Prefix, |
| 1154 | const llvm::fltSemantics *Sem) { |
| 1155 | const char *DenormMin, *Epsilon, *Max, *Min; |
| 1156 | DenormMin = PickFP(Sem, "1.40129846e-45F", "4.9406564584124654e-324", |
| 1157 | "3.64519953188247460253e-4951L", |
| 1158 | "4.94065645841246544176568792868221e-324L"); |
| 1159 | int Digits = PickFP(Sem, 6, 15, 18, 31); |
| 1160 | Epsilon = PickFP(Sem, "1.19209290e-7F", "2.2204460492503131e-16", |
| 1161 | "1.08420217248550443401e-19L", |
| 1162 | "4.94065645841246544176568792868221e-324L"); |
| 1163 | int HasInifinity = 1, HasQuietNaN = 1; |
| 1164 | int MantissaDigits = PickFP(Sem, 24, 53, 64, 106); |
| 1165 | int Min10Exp = PickFP(Sem, -37, -307, -4931, -291); |
| 1166 | int Max10Exp = PickFP(Sem, 38, 308, 4932, 308); |
| 1167 | int MinExp = PickFP(Sem, -125, -1021, -16381, -968); |
| 1168 | int MaxExp = PickFP(Sem, 128, 1024, 16384, 1024); |
| 1169 | Min = PickFP(Sem, "1.17549435e-38F", "2.2250738585072014e-308", |
| 1170 | "3.36210314311209350626e-4932L", |
| 1171 | "2.00416836000897277799610805135016e-292L"); |
| 1172 | Max = PickFP(Sem, "3.40282347e+38F", "1.7976931348623157e+308", |
| 1173 | "1.18973149535723176502e+4932L", |
| 1174 | "1.79769313486231580793728971405301e+308L"); |
| 1175 | |
| 1176 | char MacroBuf[60]; |
| 1177 | sprintf(MacroBuf, "__%s_DENORM_MIN__=%s", Prefix, DenormMin); |
| 1178 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1179 | sprintf(MacroBuf, "__%s_DIG__=%d", Prefix, Digits); |
| 1180 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1181 | sprintf(MacroBuf, "__%s_EPSILON__=%s", Prefix, Epsilon); |
| 1182 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1183 | sprintf(MacroBuf, "__%s_HAS_INFINITY__=%d", Prefix, HasInifinity); |
| 1184 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1185 | sprintf(MacroBuf, "__%s_HAS_QUIET_NAN__=%d", Prefix, HasQuietNaN); |
| 1186 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1187 | sprintf(MacroBuf, "__%s_MANT_DIG__=%d", Prefix, MantissaDigits); |
| 1188 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1189 | sprintf(MacroBuf, "__%s_MAX_10_EXP__=%d", Prefix, Max10Exp); |
| 1190 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1191 | sprintf(MacroBuf, "__%s_MAX_EXP__=%d", Prefix, MaxExp); |
| 1192 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1193 | sprintf(MacroBuf, "__%s_MAX__=%s", Prefix, Max); |
| 1194 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1195 | sprintf(MacroBuf, "__%s_MIN_10_EXP__=(%d)", Prefix, Min10Exp); |
| 1196 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1197 | sprintf(MacroBuf, "__%s_MIN_EXP__=(%d)", Prefix, MinExp); |
| 1198 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1199 | sprintf(MacroBuf, "__%s_MIN__=%s", Prefix, Min); |
| 1200 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1201 | sprintf(MacroBuf, "__%s_HAS_DENORM__=1", Prefix); |
| 1202 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro |
| 1207 | /// named MacroName with the max value for a type with width 'TypeWidth' a |
| 1208 | /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). |
| 1209 | static void DefineTypeSize(const char *MacroName, unsigned TypeWidth, |
| 1210 | const char *ValSuffix, bool isSigned, |
| 1211 | std::vector<char> &Buf) { |
| 1212 | char MacroBuf[60]; |
| 1213 | long long MaxVal; |
| 1214 | if (isSigned) |
| 1215 | MaxVal = (1LL << (TypeWidth - 1)) - 1; |
| 1216 | else |
| 1217 | MaxVal = ~0LL >> (64-TypeWidth); |
| 1218 | |
| 1219 | sprintf(MacroBuf, "%s=%llu%s", MacroName, MaxVal, ValSuffix); |
| 1220 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1221 | } |
| 1222 | |
| 1223 | static void DefineType(const char *MacroName, TargetInfo::IntType Ty, |
| 1224 | std::vector<char> &Buf) { |
| 1225 | char MacroBuf[60]; |
| 1226 | sprintf(MacroBuf, "%s=%s", MacroName, TargetInfo::getTypeName(Ty)); |
| 1227 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1228 | } |
| 1229 | |
| 1230 | |
| 1231 | static void InitializePredefinedMacros(const TargetInfo &TI, |
| 1232 | const LangOptions &LangOpts, |
| 1233 | std::vector<char> &Buf) { |
| 1234 | char MacroBuf[60]; |
| 1235 | // Compiler version introspection macros. |
| 1236 | DefineBuiltinMacro(Buf, "__llvm__=1"); // LLVM Backend |
| 1237 | DefineBuiltinMacro(Buf, "__clang__=1"); // Clang Frontend |
| 1238 | |
| 1239 | // Currently claim to be compatible with GCC 4.2.1-5621. |
| 1240 | DefineBuiltinMacro(Buf, "__APPLE_CC__=5621"); |
| 1241 | DefineBuiltinMacro(Buf, "__GNUC_MINOR__=2"); |
| 1242 | DefineBuiltinMacro(Buf, "__GNUC_PATCHLEVEL__=1"); |
| 1243 | DefineBuiltinMacro(Buf, "__GNUC__=4"); |
| 1244 | DefineBuiltinMacro(Buf, "__GXX_ABI_VERSION=1002"); |
| 1245 | DefineBuiltinMacro(Buf, "__VERSION__=\"4.2.1 Compatible Clang Compiler\""); |
| 1246 | |
| 1247 | |
| 1248 | // Initialize language-specific preprocessor defines. |
| 1249 | |
| 1250 | // These should all be defined in the preprocessor according to the |
| 1251 | // current language configuration. |
| 1252 | if (!LangOpts.Microsoft) |
| 1253 | DefineBuiltinMacro(Buf, "__STDC__=1"); |
| 1254 | if (LangOpts.AsmPreprocessor) |
| 1255 | DefineBuiltinMacro(Buf, "__ASSEMBLER__=1"); |
| 1256 | if (LangOpts.C99 && !LangOpts.CPlusPlus) |
| 1257 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199901L"); |
| 1258 | else if (0) // STDC94 ? |
| 1259 | DefineBuiltinMacro(Buf, "__STDC_VERSION__=199409L"); |
| 1260 | |
| 1261 | // Standard conforming mode? |
| 1262 | if (!LangOpts.GNUMode) |
| 1263 | DefineBuiltinMacro(Buf, "__STRICT_ANSI__=1"); |
| 1264 | |
| 1265 | if (LangOpts.CPlusPlus0x) |
| 1266 | DefineBuiltinMacro(Buf, "__GXX_EXPERIMENTAL_CXX0X__"); |
| 1267 | |
| 1268 | if (LangOpts.Freestanding) |
| 1269 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=0"); |
| 1270 | else |
| 1271 | DefineBuiltinMacro(Buf, "__STDC_HOSTED__=1"); |
| 1272 | |
| 1273 | if (LangOpts.ObjC1) { |
| 1274 | DefineBuiltinMacro(Buf, "__OBJC__=1"); |
| 1275 | if (LangOpts.ObjCNonFragileABI) { |
| 1276 | DefineBuiltinMacro(Buf, "__OBJC2__=1"); |
| 1277 | DefineBuiltinMacro(Buf, "OBJC_ZEROCOST_EXCEPTIONS=1"); |
| 1278 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 1279 | } |
| 1280 | |
| 1281 | if (LangOpts.getGCMode() != LangOptions::NonGC) |
| 1282 | DefineBuiltinMacro(Buf, "__OBJC_GC__=1"); |
| 1283 | |
| 1284 | if (LangOpts.NeXTRuntime) |
| 1285 | DefineBuiltinMacro(Buf, "__NEXT_RUNTIME__=1"); |
| 1286 | } |
| 1287 | |
| 1288 | // darwin_constant_cfstrings controls this. This is also dependent |
| 1289 | // on other things like the runtime I believe. This is set even for C code. |
| 1290 | DefineBuiltinMacro(Buf, "__CONSTANT_CFSTRINGS__=1"); |
| 1291 | |
| 1292 | if (LangOpts.ObjC2) |
| 1293 | DefineBuiltinMacro(Buf, "OBJC_NEW_PROPERTIES"); |
| 1294 | |
| 1295 | if (LangOpts.PascalStrings) |
| 1296 | DefineBuiltinMacro(Buf, "__PASCAL_STRINGS__"); |
| 1297 | |
| 1298 | if (LangOpts.Blocks) { |
| 1299 | DefineBuiltinMacro(Buf, "__block=__attribute__((__blocks__(byref)))"); |
| 1300 | DefineBuiltinMacro(Buf, "__BLOCKS__=1"); |
| 1301 | } |
| 1302 | |
| 1303 | if (LangOpts.CPlusPlus) { |
| 1304 | DefineBuiltinMacro(Buf, "__DEPRECATED=1"); |
| 1305 | DefineBuiltinMacro(Buf, "__EXCEPTIONS=1"); |
| 1306 | DefineBuiltinMacro(Buf, "__GNUG__=4"); |
| 1307 | DefineBuiltinMacro(Buf, "__GXX_WEAK__=1"); |
| 1308 | DefineBuiltinMacro(Buf, "__cplusplus=1"); |
| 1309 | DefineBuiltinMacro(Buf, "__private_extern__=extern"); |
| 1310 | } |
| 1311 | |
| 1312 | // Filter out some microsoft extensions when trying to parse in ms-compat |
| 1313 | // mode. |
| 1314 | if (LangOpts.Microsoft) { |
| 1315 | DefineBuiltinMacro(Buf, "_cdecl=__cdecl"); |
| 1316 | DefineBuiltinMacro(Buf, "__int8=__INT8_TYPE__"); |
| 1317 | DefineBuiltinMacro(Buf, "__int16=__INT16_TYPE__"); |
| 1318 | DefineBuiltinMacro(Buf, "__int32=__INT32_TYPE__"); |
| 1319 | DefineBuiltinMacro(Buf, "__int64=__INT64_TYPE__"); |
| 1320 | } |
| 1321 | |
| 1322 | if (LangOpts.Optimize) |
| 1323 | DefineBuiltinMacro(Buf, "__OPTIMIZE__=1"); |
| 1324 | if (LangOpts.OptimizeSize) |
| 1325 | DefineBuiltinMacro(Buf, "__OPTIMIZE_SIZE__=1"); |
| 1326 | |
| 1327 | // Initialize target-specific preprocessor defines. |
| 1328 | |
| 1329 | // Define type sizing macros based on the target properties. |
| 1330 | assert(TI.getCharWidth() == 8 && "Only support 8-bit char so far"); |
| 1331 | DefineBuiltinMacro(Buf, "__CHAR_BIT__=8"); |
| 1332 | |
| 1333 | unsigned IntMaxWidth; |
| 1334 | const char *IntMaxSuffix; |
| 1335 | if (TI.getIntMaxType() == TargetInfo::SignedLongLong) { |
| 1336 | IntMaxWidth = TI.getLongLongWidth(); |
| 1337 | IntMaxSuffix = "LL"; |
| 1338 | } else if (TI.getIntMaxType() == TargetInfo::SignedLong) { |
| 1339 | IntMaxWidth = TI.getLongWidth(); |
| 1340 | IntMaxSuffix = "L"; |
| 1341 | } else { |
| 1342 | assert(TI.getIntMaxType() == TargetInfo::SignedInt); |
| 1343 | IntMaxWidth = TI.getIntWidth(); |
| 1344 | IntMaxSuffix = ""; |
| 1345 | } |
| 1346 | |
| 1347 | DefineTypeSize("__SCHAR_MAX__", TI.getCharWidth(), "", true, Buf); |
| 1348 | DefineTypeSize("__SHRT_MAX__", TI.getShortWidth(), "", true, Buf); |
| 1349 | DefineTypeSize("__INT_MAX__", TI.getIntWidth(), "", true, Buf); |
| 1350 | DefineTypeSize("__LONG_MAX__", TI.getLongWidth(), "L", true, Buf); |
| 1351 | DefineTypeSize("__LONG_LONG_MAX__", TI.getLongLongWidth(), "LL", true, Buf); |
| 1352 | DefineTypeSize("__WCHAR_MAX__", TI.getWCharWidth(), "", true, Buf); |
| 1353 | DefineTypeSize("__INTMAX_MAX__", IntMaxWidth, IntMaxSuffix, true, Buf); |
| 1354 | |
| 1355 | DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Buf); |
| 1356 | DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Buf); |
| 1357 | DefineType("__PTRDIFF_TYPE__", TI.getPtrDiffType(0), Buf); |
| 1358 | DefineType("__INTPTR_TYPE__", TI.getIntPtrType(), Buf); |
| 1359 | DefineType("__SIZE_TYPE__", TI.getSizeType(), Buf); |
| 1360 | DefineType("__WCHAR_TYPE__", TI.getWCharType(), Buf); |
| 1361 | // FIXME: TargetInfo hookize __WINT_TYPE__. |
| 1362 | DefineBuiltinMacro(Buf, "__WINT_TYPE__=int"); |
| 1363 | |
| 1364 | DefineFloatMacros(Buf, "FLT", &TI.getFloatFormat()); |
| 1365 | DefineFloatMacros(Buf, "DBL", &TI.getDoubleFormat()); |
| 1366 | DefineFloatMacros(Buf, "LDBL", &TI.getLongDoubleFormat()); |
| 1367 | |
| 1368 | // Define a __POINTER_WIDTH__ macro for stdint.h. |
| 1369 | sprintf(MacroBuf, "__POINTER_WIDTH__=%d", (int)TI.getPointerWidth(0)); |
| 1370 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1371 | |
| 1372 | if (!TI.isCharSigned()) |
| 1373 | DefineBuiltinMacro(Buf, "__CHAR_UNSIGNED__"); |
| 1374 | |
| 1375 | // Define fixed-sized integer types for stdint.h |
| 1376 | assert(TI.getCharWidth() == 8 && "unsupported target types"); |
| 1377 | assert(TI.getShortWidth() == 16 && "unsupported target types"); |
| 1378 | DefineBuiltinMacro(Buf, "__INT8_TYPE__=char"); |
| 1379 | DefineBuiltinMacro(Buf, "__INT16_TYPE__=short"); |
| 1380 | |
| 1381 | if (TI.getIntWidth() == 32) |
| 1382 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=int"); |
| 1383 | else { |
| 1384 | assert(TI.getLongLongWidth() == 32 && "unsupported target types"); |
| 1385 | DefineBuiltinMacro(Buf, "__INT32_TYPE__=long long"); |
| 1386 | } |
| 1387 | |
| 1388 | // 16-bit targets doesn't necessarily have a 64-bit type. |
| 1389 | if (TI.getLongLongWidth() == 64) |
| 1390 | DefineBuiltinMacro(Buf, "__INT64_TYPE__=long long"); |
| 1391 | |
| 1392 | // Add __builtin_va_list typedef. |
| 1393 | { |
| 1394 | const char *VAList = TI.getVAListDeclaration(); |
| 1395 | Buf.insert(Buf.end(), VAList, VAList+strlen(VAList)); |
| 1396 | Buf.push_back('\n'); |
| 1397 | } |
| 1398 | |
| 1399 | if (const char *Prefix = TI.getUserLabelPrefix()) { |
| 1400 | sprintf(MacroBuf, "__USER_LABEL_PREFIX__=%s", Prefix); |
| 1401 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1402 | } |
| 1403 | |
| 1404 | // Build configuration options. FIXME: these should be controlled by |
| 1405 | // command line options or something. |
| 1406 | DefineBuiltinMacro(Buf, "__FINITE_MATH_ONLY__=0"); |
| 1407 | |
| 1408 | if (LangOpts.Static) |
| 1409 | DefineBuiltinMacro(Buf, "__STATIC__=1"); |
| 1410 | else |
| 1411 | DefineBuiltinMacro(Buf, "__DYNAMIC__=1"); |
| 1412 | |
| 1413 | if (LangOpts.GNUInline) |
| 1414 | DefineBuiltinMacro(Buf, "__GNUC_GNU_INLINE__=1"); |
| 1415 | else |
| 1416 | DefineBuiltinMacro(Buf, "__GNUC_STDC_INLINE__=1"); |
| 1417 | |
| 1418 | if (LangOpts.NoInline) |
| 1419 | DefineBuiltinMacro(Buf, "__NO_INLINE__=1"); |
| 1420 | |
| 1421 | if (unsigned PICLevel = LangOpts.PICLevel) { |
| 1422 | sprintf(MacroBuf, "__PIC__=%d", PICLevel); |
| 1423 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1424 | |
| 1425 | sprintf(MacroBuf, "__pic__=%d", PICLevel); |
| 1426 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1427 | } |
| 1428 | |
| 1429 | // Macros to control C99 numerics and <float.h> |
| 1430 | DefineBuiltinMacro(Buf, "__FLT_EVAL_METHOD__=0"); |
| 1431 | DefineBuiltinMacro(Buf, "__FLT_RADIX__=2"); |
| 1432 | sprintf(MacroBuf, "__DECIMAL_DIG__=%d", |
| 1433 | PickFP(&TI.getLongDoubleFormat(), -1/*FIXME*/, 17, 21, 33)); |
| 1434 | DefineBuiltinMacro(Buf, MacroBuf); |
| 1435 | |
| 1436 | // Get other target #defines. |
| 1437 | TI.getTargetDefines(LangOpts, Buf); |
| 1438 | } |
| 1439 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1440 | /// InitializePreprocessor - Initialize the preprocessor getting it and the |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1441 | /// environment ready to process a single file. This returns true on error. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1442 | /// |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1443 | static bool InitializePreprocessor(Preprocessor &PP, |
| 1444 | bool InitializeSourceMgr, |
| 1445 | const std::string &InFile) { |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1446 | FileManager &FileMgr = PP.getFileManager(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1447 | |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1448 | // Figure out where to get and map in the main file. |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 1449 | SourceManager &SourceMgr = PP.getSourceManager(); |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1450 | |
| 1451 | if (InitializeSourceMgr) { |
| 1452 | if (InFile != "-") { |
| 1453 | const FileEntry *File = FileMgr.getFile(InFile); |
| 1454 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1455 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | f45afe6 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 1456 | PP.getDiagnostics().Report(FullSourceLoc(), diag::err_fe_error_reading) |
| 1457 | << InFile.c_str(); |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1458 | return true; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1459 | } |
| 1460 | } else { |
| 1461 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
Daniel Dunbar | 9961716 | 2009-03-21 17:56:30 +0000 | [diff] [blame] | 1462 | |
| 1463 | // If stdin was empty, SB is null. Cons up an empty memory |
| 1464 | // buffer now. |
| 1465 | if (!SB) { |
| 1466 | const char *EmptyStr = ""; |
| 1467 | SB = llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<stdin>"); |
| 1468 | } |
| 1469 | |
| 1470 | SourceMgr.createMainFileIDForMemBuffer(SB); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1471 | if (SourceMgr.getMainFileID().isInvalid()) { |
Daniel Dunbar | f45afe6 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 1472 | PP.getDiagnostics().Report(FullSourceLoc(), |
| 1473 | diag::err_fe_error_reading_stdin); |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1474 | return true; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1475 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1476 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1477 | } |
Sam Bishop | 61a2078 | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 1478 | |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 1479 | std::vector<char> PredefineBuffer; |
Chris Lattner | a9a0dd5 | 2009-04-10 21:58:23 +0000 | [diff] [blame] | 1480 | |
| 1481 | // Install things like __POWERPC__, __GNUC__, etc into the macro table. |
| 1482 | InitializePredefinedMacros(PP.getTargetInfo(), PP.getLangOptions(), |
| 1483 | PredefineBuffer); |
| 1484 | |
| 1485 | // Add on the predefines from the driver. Wrap in a #line directive to report |
| 1486 | // that they come from the command line. |
| 1487 | const char *LineDirective = "# 1 \"<command line>\" 1\n"; |
| 1488 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 1489 | LineDirective, LineDirective+strlen(LineDirective)); |
| 1490 | |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1492 | // Add macros from the command line. |
Sam Bishop | 61a2078 | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 1493 | unsigned d = 0, D = D_macros.size(); |
| 1494 | unsigned u = 0, U = U_macros.size(); |
| 1495 | while (d < D || u < U) { |
| 1496 | if (u == U || (d < D && D_macros.getPosition(d) < U_macros.getPosition(u))) |
| 1497 | DefineBuiltinMacro(PredefineBuffer, D_macros[d++].c_str()); |
| 1498 | else |
| 1499 | DefineBuiltinMacro(PredefineBuffer, U_macros[u++].c_str(), "#undef "); |
| 1500 | } |
| 1501 | |
Chris Lattner | b62396d | 2009-04-08 20:15:42 +0000 | [diff] [blame] | 1502 | // If -imacros are specified, include them now. These are processed before |
| 1503 | // any -include directives. |
| 1504 | |
| 1505 | for (unsigned i = 0, e = ImplicitMacroIncludes.size(); i != e; ++i) |
| 1506 | AddImplicitIncludeMacros(PredefineBuffer, ImplicitMacroIncludes[i]); |
Chris Lattner | 008da78 | 2008-01-10 01:53:41 +0000 | [diff] [blame] | 1507 | |
Chris Lattner | b62396d | 2009-04-08 20:15:42 +0000 | [diff] [blame] | 1508 | if (!ImplicitIncludePTH.empty() || !ImplicitIncludes.empty()) { |
| 1509 | // We want to add these paths to the predefines buffer in order, make a |
| 1510 | // temporary vector to sort by their occurrence. |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1511 | llvm::SmallVector<std::pair<unsigned, std::string*>, 8> OrderedPaths; |
| 1512 | |
| 1513 | if (!ImplicitIncludePTH.empty()) |
| 1514 | OrderedPaths.push_back(std::make_pair(ImplicitIncludePTH.getPosition(), |
| 1515 | &ImplicitIncludePTH)); |
| 1516 | for (unsigned i = 0, e = ImplicitIncludes.size(); i != e; ++i) |
| 1517 | OrderedPaths.push_back(std::make_pair(ImplicitIncludes.getPosition(i), |
| 1518 | &ImplicitIncludes[i])); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1519 | llvm::array_pod_sort(OrderedPaths.begin(), OrderedPaths.end()); |
| 1520 | |
| 1521 | // Now that they are ordered by position, add to the predefines buffer. |
| 1522 | for (unsigned i = 0, e = OrderedPaths.size(); i != e; ++i) { |
| 1523 | std::string *Ptr = OrderedPaths[i].second; |
| 1524 | if (!ImplicitIncludes.empty() && |
| 1525 | Ptr >= &ImplicitIncludes[0] && |
| 1526 | Ptr <= &ImplicitIncludes[ImplicitIncludes.size()-1]) { |
| 1527 | AddImplicitInclude(PredefineBuffer, *Ptr); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1528 | } else { |
Chris Lattner | b62396d | 2009-04-08 20:15:42 +0000 | [diff] [blame] | 1529 | assert(Ptr == &ImplicitIncludePTH); |
| 1530 | AddImplicitIncludePTH(PredefineBuffer, PP); |
Chris Lattner | 0e004a1 | 2009-04-08 18:24:34 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1533 | } |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1534 | |
Chris Lattner | a9a0dd5 | 2009-04-10 21:58:23 +0000 | [diff] [blame] | 1535 | LineDirective = "# 2 \"<built-in>\" 2\n"; |
| 1536 | PredefineBuffer.insert(PredefineBuffer.end(), |
| 1537 | LineDirective, LineDirective+strlen(LineDirective)); |
| 1538 | |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 1539 | // Null terminate PredefinedBuffer and add it. |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1540 | PredefineBuffer.push_back(0); |
Chris Lattner | 47b6a16 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 1541 | PP.setPredefines(&PredefineBuffer[0]); |
Chris Lattner | d1f21e1 | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Once we've read this, we're done. |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1544 | return false; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | //===----------------------------------------------------------------------===// |
| 1548 | // Preprocessor include path information. |
| 1549 | //===----------------------------------------------------------------------===// |
| 1550 | |
| 1551 | // This tool exports a large number of command line options to control how the |
| 1552 | // preprocessor searches for header files. At root, however, the Preprocessor |
| 1553 | // object takes a very simple interface: a list of directories to search for |
| 1554 | // |
| 1555 | // FIXME: -nostdinc,-nostdinc++ |
Chris Lattner | ae3dcc0 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 1556 | // FIXME: -imultilib |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1557 | // |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1558 | |
| 1559 | static llvm::cl::opt<bool> |
| 1560 | nostdinc("nostdinc", llvm::cl::desc("Disable standard #include directories")); |
| 1561 | |
| 1562 | // Various command line options. These four add directories to each chain. |
| 1563 | static llvm::cl::list<std::string> |
| 1564 | F_dirs("F", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1565 | llvm::cl::desc("Add directory to framework include search path")); |
| 1566 | static llvm::cl::list<std::string> |
| 1567 | I_dirs("I", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1568 | llvm::cl::desc("Add directory to include search path")); |
| 1569 | static llvm::cl::list<std::string> |
| 1570 | idirafter_dirs("idirafter", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1571 | llvm::cl::desc("Add directory to AFTER include search path")); |
| 1572 | static llvm::cl::list<std::string> |
| 1573 | iquote_dirs("iquote", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1574 | llvm::cl::desc("Add directory to QUOTE include search path")); |
| 1575 | static llvm::cl::list<std::string> |
| 1576 | isystem_dirs("isystem", llvm::cl::value_desc("directory"), llvm::cl::Prefix, |
| 1577 | llvm::cl::desc("Add directory to SYSTEM include search path")); |
| 1578 | |
| 1579 | // These handle -iprefix/-iwithprefix/-iwithprefixbefore. |
| 1580 | static llvm::cl::list<std::string> |
| 1581 | iprefix_vals("iprefix", llvm::cl::value_desc("prefix"), llvm::cl::Prefix, |
| 1582 | llvm::cl::desc("Set the -iwithprefix/-iwithprefixbefore prefix")); |
| 1583 | static llvm::cl::list<std::string> |
| 1584 | iwithprefix_vals("iwithprefix", llvm::cl::value_desc("dir"), llvm::cl::Prefix, |
| 1585 | llvm::cl::desc("Set directory to SYSTEM include search path with prefix")); |
| 1586 | static llvm::cl::list<std::string> |
| 1587 | iwithprefixbefore_vals("iwithprefixbefore", llvm::cl::value_desc("dir"), |
| 1588 | llvm::cl::Prefix, |
| 1589 | llvm::cl::desc("Set directory to include search path with prefix")); |
| 1590 | |
Chris Lattner | ae3dcc0 | 2007-08-26 17:47:35 +0000 | [diff] [blame] | 1591 | static llvm::cl::opt<std::string> |
| 1592 | isysroot("isysroot", llvm::cl::value_desc("dir"), llvm::cl::init("/"), |
| 1593 | llvm::cl::desc("Set the system root directory (usually /)")); |
| 1594 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1595 | // Finally, implement the code that groks the options above. |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1596 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1597 | /// InitializeIncludePaths - Process the -I options and set them in the |
| 1598 | /// HeaderSearch object. |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1599 | void InitializeIncludePaths(const char *Argv0, HeaderSearch &Headers, |
| 1600 | FileManager &FM, const LangOptions &Lang) { |
| 1601 | InitHeaderSearch Init(Headers, Verbose, isysroot); |
| 1602 | |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1603 | // Handle -I... and -F... options, walking the lists in parallel. |
| 1604 | unsigned Iidx = 0, Fidx = 0; |
| 1605 | while (Iidx < I_dirs.size() && Fidx < F_dirs.size()) { |
| 1606 | if (I_dirs.getPosition(Iidx) < F_dirs.getPosition(Fidx)) { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1607 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1608 | ++Iidx; |
| 1609 | } else { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1610 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1611 | ++Fidx; |
| 1612 | } |
| 1613 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1614 | |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1615 | // Consume what's left from whatever list was longer. |
| 1616 | for (; Iidx != I_dirs.size(); ++Iidx) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1617 | Init.AddPath(I_dirs[Iidx], InitHeaderSearch::Angled, false, true, false); |
Ted Kremenek | b4d41e1 | 2008-05-31 00:27:00 +0000 | [diff] [blame] | 1618 | for (; Fidx != F_dirs.size(); ++Fidx) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1619 | Init.AddPath(F_dirs[Fidx], InitHeaderSearch::Angled, false, true, true); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1620 | |
| 1621 | // Handle -idirafter... options. |
| 1622 | for (unsigned i = 0, e = idirafter_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1623 | Init.AddPath(idirafter_dirs[i], InitHeaderSearch::After, |
| 1624 | false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1625 | |
| 1626 | // Handle -iquote... options. |
| 1627 | for (unsigned i = 0, e = iquote_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1628 | Init.AddPath(iquote_dirs[i], InitHeaderSearch::Quoted, false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Handle -isystem... options. |
| 1631 | for (unsigned i = 0, e = isystem_dirs.size(); i != e; ++i) |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1632 | Init.AddPath(isystem_dirs[i], InitHeaderSearch::System, false, true, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1633 | |
| 1634 | // Walk the -iprefix/-iwithprefix/-iwithprefixbefore argument lists in |
| 1635 | // parallel, processing the values in order of occurance to get the right |
| 1636 | // prefixes. |
| 1637 | { |
| 1638 | std::string Prefix = ""; // FIXME: this isn't the correct default prefix. |
| 1639 | unsigned iprefix_idx = 0; |
| 1640 | unsigned iwithprefix_idx = 0; |
| 1641 | unsigned iwithprefixbefore_idx = 0; |
| 1642 | bool iprefix_done = iprefix_vals.empty(); |
| 1643 | bool iwithprefix_done = iwithprefix_vals.empty(); |
| 1644 | bool iwithprefixbefore_done = iwithprefixbefore_vals.empty(); |
| 1645 | while (!iprefix_done || !iwithprefix_done || !iwithprefixbefore_done) { |
| 1646 | if (!iprefix_done && |
| 1647 | (iwithprefix_done || |
| 1648 | iprefix_vals.getPosition(iprefix_idx) < |
| 1649 | iwithprefix_vals.getPosition(iwithprefix_idx)) && |
| 1650 | (iwithprefixbefore_done || |
| 1651 | iprefix_vals.getPosition(iprefix_idx) < |
| 1652 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
| 1653 | Prefix = iprefix_vals[iprefix_idx]; |
| 1654 | ++iprefix_idx; |
| 1655 | iprefix_done = iprefix_idx == iprefix_vals.size(); |
| 1656 | } else if (!iwithprefix_done && |
| 1657 | (iwithprefixbefore_done || |
| 1658 | iwithprefix_vals.getPosition(iwithprefix_idx) < |
| 1659 | iwithprefixbefore_vals.getPosition(iwithprefixbefore_idx))) { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1660 | Init.AddPath(Prefix+iwithprefix_vals[iwithprefix_idx], |
| 1661 | InitHeaderSearch::System, false, false, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1662 | ++iwithprefix_idx; |
| 1663 | iwithprefix_done = iwithprefix_idx == iwithprefix_vals.size(); |
| 1664 | } else { |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1665 | Init.AddPath(Prefix+iwithprefixbefore_vals[iwithprefixbefore_idx], |
| 1666 | InitHeaderSearch::Angled, false, false, false); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1667 | ++iwithprefixbefore_idx; |
| 1668 | iwithprefixbefore_done = |
| 1669 | iwithprefixbefore_idx == iwithprefixbefore_vals.size(); |
| 1670 | } |
| 1671 | } |
| 1672 | } |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1673 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1674 | Init.AddDefaultEnvVarPaths(Lang); |
Chris Lattner | 4f022a7 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 1675 | |
Daniel Dunbar | 4e60429 | 2009-02-21 20:52:41 +0000 | [diff] [blame] | 1676 | // Add the clang headers, which are relative to the clang binary. |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1677 | llvm::sys::Path MainExecutablePath = |
Chris Lattner | 716a054 | 2008-03-03 05:57:43 +0000 | [diff] [blame] | 1678 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 1679 | (void*)(intptr_t)InitializeIncludePaths); |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1680 | if (!MainExecutablePath.isEmpty()) { |
| 1681 | MainExecutablePath.eraseComponent(); // Remove /clang from foo/bin/clang |
| 1682 | MainExecutablePath.eraseComponent(); // Remove /bin from foo/bin |
Daniel Dunbar | 4e60429 | 2009-02-21 20:52:41 +0000 | [diff] [blame] | 1683 | |
| 1684 | // Get foo/lib/clang/1.0/include |
| 1685 | // |
| 1686 | // FIXME: Don't embed version here. |
| 1687 | MainExecutablePath.appendComponent("lib"); |
| 1688 | MainExecutablePath.appendComponent("clang"); |
| 1689 | MainExecutablePath.appendComponent("1.0"); |
| 1690 | MainExecutablePath.appendComponent("include"); |
Chris Lattner | 99a7265 | 2009-02-19 06:48:28 +0000 | [diff] [blame] | 1691 | |
| 1692 | // We pass true to ignore sysroot so that we *always* look for clang headers |
| 1693 | // relative to our executable, never relative to -isysroot. |
| 1694 | Init.AddPath(MainExecutablePath.c_str(), InitHeaderSearch::System, |
| 1695 | false, false, false, true /*ignore sysroot*/); |
Chris Lattner | 3ee4a2f | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1698 | if (!nostdinc) |
| 1699 | Init.AddDefaultSystemIncludePaths(Lang); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Now that we have collected all of the include paths, merge them all |
| 1702 | // together and tell the preprocessor about them. |
| 1703 | |
Nico Weber | 770e388 | 2008-08-22 09:25:22 +0000 | [diff] [blame] | 1704 | Init.Realize(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1707 | //===----------------------------------------------------------------------===// |
| 1708 | // Driver PreprocessorFactory - For lazily generating preprocessors ... |
| 1709 | //===----------------------------------------------------------------------===// |
| 1710 | |
| 1711 | namespace { |
| 1712 | class VISIBILITY_HIDDEN DriverPreprocessorFactory : public PreprocessorFactory { |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1713 | const std::string &InFile; |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1714 | Diagnostic &Diags; |
| 1715 | const LangOptions &LangInfo; |
| 1716 | TargetInfo &Target; |
| 1717 | SourceManager &SourceMgr; |
| 1718 | HeaderSearch &HeaderInfo; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1719 | bool InitializeSourceMgr; |
| 1720 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1721 | public: |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1722 | DriverPreprocessorFactory(const std::string &infile, |
| 1723 | Diagnostic &diags, const LangOptions &opts, |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1724 | TargetInfo &target, SourceManager &SM, |
| 1725 | HeaderSearch &Headers) |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1726 | : InFile(infile), Diags(diags), LangInfo(opts), Target(target), |
| 1727 | SourceMgr(SM), HeaderInfo(Headers), InitializeSourceMgr(true) {} |
| 1728 | |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1729 | |
| 1730 | virtual ~DriverPreprocessorFactory() {} |
| 1731 | |
| 1732 | virtual Preprocessor* CreatePreprocessor() { |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1733 | llvm::OwningPtr<PTHManager> PTHMgr; |
| 1734 | |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1735 | if (!TokenCache.empty() && !ImplicitIncludePTH.empty()) { |
| 1736 | fprintf(stderr, "error: cannot use both -token-cache and -include-pth " |
| 1737 | "options\n"); |
Ted Kremenek | 6348cc6 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1738 | exit(1); |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1741 | // Use PTH? |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1742 | if (!TokenCache.empty() || !ImplicitIncludePTH.empty()) { |
| 1743 | const std::string& x = TokenCache.empty() ? ImplicitIncludePTH:TokenCache; |
Ted Kremenek | 6348cc6 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1744 | PTHMgr.reset(PTHManager::Create(x, &Diags, |
| 1745 | TokenCache.empty() ? Diagnostic::Error |
| 1746 | : Diagnostic::Warning)); |
Ted Kremenek | 2ee90d5 | 2009-03-20 00:26:38 +0000 | [diff] [blame] | 1747 | } |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1748 | |
Ted Kremenek | 6348cc6 | 2009-03-22 06:42:39 +0000 | [diff] [blame] | 1749 | if (Diags.hasErrorOccurred()) |
| 1750 | exit(1); |
| 1751 | |
Ted Kremenek | d976c3d | 2009-01-15 18:47:46 +0000 | [diff] [blame] | 1752 | // Create the Preprocessor. |
| 1753 | llvm::OwningPtr<Preprocessor> PP(new Preprocessor(Diags, LangInfo, Target, |
| 1754 | SourceMgr, HeaderInfo, |
| 1755 | PTHMgr.get())); |
| 1756 | |
| 1757 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 1758 | // That argument is used as the IdentifierInfoLookup argument to |
| 1759 | // IdentifierTable's ctor. |
| 1760 | if (PTHMgr) { |
| 1761 | PTHMgr->setPreprocessor(PP.get()); |
| 1762 | PP->setPTHManager(PTHMgr.take()); |
| 1763 | } |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1764 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1765 | return PP.take(); |
| 1766 | } |
| 1767 | |
| 1768 | virtual bool FinishInitialization(Preprocessor *PP) { |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1769 | if (InitializePreprocessor(*PP, InitializeSourceMgr, InFile)) { |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1770 | return true; |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1773 | /// FIXME: PP can only handle one callback |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 1774 | if (ProgAction != PrintPreprocessedInput) { |
| 1775 | std::string ErrStr; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1776 | bool DFG = CreateDependencyFileGen(PP, ErrStr); |
Daniel Dunbar | 0bf13eb | 2009-03-30 00:34:04 +0000 | [diff] [blame] | 1777 | if (!DFG && !ErrStr.empty()) { |
| 1778 | fprintf(stderr, "%s", ErrStr.c_str()); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1779 | return true; |
Daniel Dunbar | 35fe5de | 2008-10-24 22:12:41 +0000 | [diff] [blame] | 1780 | } |
| 1781 | } |
| 1782 | |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1783 | InitializeSourceMgr = false; |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 1784 | return false; |
Ted Kremenek | 01d3bf7 | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 1785 | } |
| 1786 | }; |
| 1787 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1788 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1789 | //===----------------------------------------------------------------------===// |
| 1790 | // Basic Parser driver |
| 1791 | //===----------------------------------------------------------------------===// |
| 1792 | |
Chris Lattner | 9d818a2 | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 1793 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1794 | Parser P(PP, *PA); |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1795 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1796 | |
| 1797 | // Parsing the specified input file. |
| 1798 | P.ParseTranslationUnit(); |
| 1799 | delete PA; |
| 1800 | } |
| 1801 | |
| 1802 | //===----------------------------------------------------------------------===// |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1803 | // Code generation options |
| 1804 | //===----------------------------------------------------------------------===// |
| 1805 | |
| 1806 | static llvm::cl::opt<bool> |
Chris Lattner | 9a09eda | 2009-03-09 22:05:03 +0000 | [diff] [blame] | 1807 | GenerateDebugInfo("g", |
| 1808 | llvm::cl::desc("Generate source level debug information")); |
| 1809 | |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1810 | static llvm::cl::opt<std::string> |
| 1811 | TargetCPU("mcpu", |
| 1812 | llvm::cl::desc("Target a specific cpu type (-mcpu=help for details)")); |
| 1813 | |
Chris Lattner | 8a9615c | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1814 | static void InitializeCompileOptions(CompileOptions &Opts, |
| 1815 | const LangOptions &LangOpts) { |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1816 | Opts.OptimizeSize = OptSize; |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 1817 | Opts.DebugInfo = GenerateDebugInfo; |
Daniel Dunbar | d725b16 | 2008-10-29 07:56:11 +0000 | [diff] [blame] | 1818 | if (OptSize) { |
| 1819 | // -Os implies -O2 |
| 1820 | // FIXME: Diagnose conflicting options. |
| 1821 | Opts.OptimizationLevel = 2; |
| 1822 | } else { |
| 1823 | Opts.OptimizationLevel = OptLevel; |
| 1824 | } |
Daniel Dunbar | 721cbf1 | 2008-10-29 03:42:18 +0000 | [diff] [blame] | 1825 | |
| 1826 | // FIXME: There are llvm-gcc options to control these selectively. |
| 1827 | Opts.InlineFunctions = (Opts.OptimizationLevel > 1); |
| 1828 | Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !OptSize); |
Chris Lattner | 8a9615c | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1829 | Opts.SimplifyLibCalls = !LangOpts.NoBuiltin; |
Daniel Dunbar | e8d0ba7 | 2008-10-31 09:34:21 +0000 | [diff] [blame] | 1830 | |
| 1831 | #ifdef NDEBUG |
| 1832 | Opts.VerifyModule = 0; |
| 1833 | #endif |
Daniel Dunbar | 9101a63 | 2009-02-17 19:47:34 +0000 | [diff] [blame] | 1834 | |
| 1835 | Opts.CPU = TargetCPU; |
| 1836 | Opts.Features.insert(Opts.Features.end(), |
| 1837 | TargetFeatures.begin(), TargetFeatures.end()); |
Chris Lattner | e8f7071 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | f04a756 | 2009-03-26 05:00:52 +0000 | [diff] [blame] | 1839 | Opts.NoCommon = NoCommon | LangOpts.CPlusPlus; |
| 1840 | |
Chris Lattner | e8f7071 | 2009-02-18 01:23:44 +0000 | [diff] [blame] | 1841 | // Handle -ftime-report. |
| 1842 | Opts.TimePasses = TimeReport; |
Daniel Dunbar | aa7a066 | 2008-10-23 05:50:47 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 24b48b0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 1846 | // Fix-It Options |
| 1847 | //===----------------------------------------------------------------------===// |
| 1848 | static llvm::cl::list<ParsedSourceLocation> |
| 1849 | FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"), |
| 1850 | llvm::cl::desc("Perform Fix-It modifications at the given source location")); |
| 1851 | |
| 1852 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1853 | // Main driver |
| 1854 | //===----------------------------------------------------------------------===// |
| 1855 | |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1856 | /// CreateASTConsumer - Create the ASTConsumer for the corresponding program |
Chris Lattner | 9a09eda | 2009-03-09 22:05:03 +0000 | [diff] [blame] | 1857 | /// action. These consumers can operate on both ASTs that are freshly |
| 1858 | /// parsed from source files as well as those deserialized from Bitcode. |
| 1859 | /// Note that PP and PPF may be null here. |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1860 | static ASTConsumer *CreateASTConsumer(const std::string& InFile, |
Ted Kremenek | 397de01 | 2007-12-13 00:37:31 +0000 | [diff] [blame] | 1861 | Diagnostic& Diag, FileManager& FileMgr, |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 1862 | const LangOptions& LangOpts, |
Chris Lattner | 21f72d6 | 2008-04-16 06:11:58 +0000 | [diff] [blame] | 1863 | Preprocessor *PP, |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 1864 | PreprocessorFactory *PPF) { |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1865 | switch (ProgAction) { |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1866 | default: |
| 1867 | return NULL; |
| 1868 | |
| 1869 | case ASTPrint: |
| 1870 | return CreateASTPrinter(); |
| 1871 | |
| 1872 | case ASTDump: |
| 1873 | return CreateASTDumper(); |
| 1874 | |
| 1875 | case ASTView: |
| 1876 | return CreateASTViewer(); |
Zhongxing Xu | 6036bbe | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1878 | case PrintDeclContext: |
| 1879 | return CreateDeclContextPrinter(); |
| 1880 | |
| 1881 | case EmitHTML: |
| 1882 | return CreateHTMLPrinter(OutputFile, Diag, PP, PPF); |
Ted Kremenek | e972d85 | 2008-07-02 18:23:21 +0000 | [diff] [blame] | 1883 | |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1884 | case InheritanceView: |
| 1885 | return CreateInheritanceViewer(InheritanceViewCls); |
| 1886 | |
| 1887 | case TestSerialization: |
| 1888 | return CreateSerializationTest(Diag, FileMgr); |
| 1889 | |
| 1890 | case EmitAssembly: |
| 1891 | case EmitLLVM: |
Daniel Dunbar | d999a8e | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 1892 | case EmitBC: |
| 1893 | case EmitLLVMOnly: { |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1894 | BackendAction Act; |
| 1895 | if (ProgAction == EmitAssembly) |
| 1896 | Act = Backend_EmitAssembly; |
| 1897 | else if (ProgAction == EmitLLVM) |
| 1898 | Act = Backend_EmitLL; |
Daniel Dunbar | d999a8e | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 1899 | else if (ProgAction == EmitLLVMOnly) |
| 1900 | Act = Backend_EmitNothing; |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1901 | else |
| 1902 | Act = Backend_EmitBC; |
| 1903 | |
| 1904 | CompileOptions Opts; |
Chris Lattner | 8a9615c | 2009-03-16 18:41:18 +0000 | [diff] [blame] | 1905 | InitializeCompileOptions(Opts, LangOpts); |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1906 | return CreateBackendConsumer(Act, Diag, LangOpts, Opts, |
Chris Lattner | 88cbb9b | 2009-03-09 22:00:34 +0000 | [diff] [blame] | 1907 | InFile, OutputFile); |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1908 | } |
Seo Sanghyeon | 550a1eb | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 1909 | |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1910 | case SerializeAST: |
| 1911 | // FIXME: Allow user to tailor where the file is written. |
| 1912 | return CreateASTSerializer(InFile, OutputFile, Diag); |
| 1913 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1914 | case GeneratePCH: |
Chris Lattner | ffc05ed | 2009-04-10 17:15:23 +0000 | [diff] [blame] | 1915 | assert(PP && "Generate PCH doesn't work from serialized file yet"); |
| 1916 | return CreatePCHGenerator(*PP, OutputFile); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 1917 | |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1918 | case RewriteObjC: |
| 1919 | return CreateCodeRewriterTest(InFile, OutputFile, Diag, LangOpts); |
Steve Naroff | 93c1835 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 1920 | |
Chris Lattner | 7f90292 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 1921 | case RewriteBlocks: |
| 1922 | return CreateBlockRewriter(InFile, OutputFile, Diag, LangOpts); |
| 1923 | |
| 1924 | case RunAnalysis: |
| 1925 | return CreateAnalysisConsumer(Diag, PP, PPF, LangOpts, OutputFile); |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1926 | } |
| 1927 | } |
| 1928 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1929 | /// ProcessInputFile - Process a single input file with the specified state. |
| 1930 | /// |
Ted Kremenek | 4e9899f | 2008-04-17 22:31:54 +0000 | [diff] [blame] | 1931 | static void ProcessInputFile(Preprocessor &PP, PreprocessorFactory &PPF, |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1932 | const std::string &InFile, ProgActions PA) { |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1933 | llvm::OwningPtr<ASTConsumer> Consumer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1934 | bool ClearSourceMgr = false; |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 1935 | FixItRewriter *FixItRewrite = 0; |
| 1936 | |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1937 | switch (PA) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1938 | default: |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 1939 | Consumer.reset(CreateASTConsumer(InFile, PP.getDiagnostics(), |
| 1940 | PP.getFileManager(), PP.getLangOptions(), |
| 1941 | &PP, &PPF)); |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1942 | |
| 1943 | if (!Consumer) { |
| 1944 | fprintf(stderr, "Unexpected program action!\n"); |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 1945 | HadErrors = true; |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 1948 | |
Ted Kremenek | a36aaef | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 1949 | break; |
| 1950 | |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1951 | case DumpRawTokens: { |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1952 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1953 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1954 | // Start lexing the specified input file. |
Chris Lattner | c7b2359 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 1955 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1956 | RawLex.SetKeepWhitespaceMode(true); |
| 1957 | |
| 1958 | Token RawTok; |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1959 | RawLex.LexFromRawLexer(RawTok); |
| 1960 | while (RawTok.isNot(tok::eof)) { |
| 1961 | PP.DumpToken(RawTok, true); |
| 1962 | fprintf(stderr, "\n"); |
| 1963 | RawLex.LexFromRawLexer(RawTok); |
| 1964 | } |
| 1965 | ClearSourceMgr = true; |
| 1966 | break; |
| 1967 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1968 | case DumpTokens: { // Token dump mode. |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1969 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1970 | Token Tok; |
Chris Lattner | af669fb | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 1971 | // Start preprocessing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1972 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1973 | do { |
| 1974 | PP.Lex(Tok); |
| 1975 | PP.DumpToken(Tok, true); |
| 1976 | fprintf(stderr, "\n"); |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1977 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1978 | ClearSourceMgr = true; |
| 1979 | break; |
| 1980 | } |
| 1981 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1982 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1983 | Token Tok; |
| 1984 | // Start parsing the specified input file. |
Ted Kremenek | 17861c5 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 1985 | PP.EnterMainSourceFile(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1986 | do { |
| 1987 | PP.Lex(Tok); |
Chris Lattner | 3b49415 | 2007-10-09 18:03:42 +0000 | [diff] [blame] | 1988 | } while (Tok.isNot(tok::eof)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1989 | ClearSourceMgr = true; |
| 1990 | break; |
| 1991 | } |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1992 | |
Douglas Gregor | 2a0e874 | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 1993 | case GeneratePTH: { |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1994 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Ted Kremenek | 71c6cc6 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 1995 | CacheTokens(PP, OutputFile); |
| 1996 | ClearSourceMgr = true; |
| 1997 | break; |
| 1998 | } |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2000 | case PrintPreprocessedInput: { // -E mode. |
| 2001 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Chris Lattner | efd02a3 | 2008-01-27 23:55:11 +0000 | [diff] [blame] | 2002 | DoPrintPreprocessedInput(PP, OutputFile); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2003 | ClearSourceMgr = true; |
| 2004 | break; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2005 | } |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 2006 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2007 | case ParseNoop: { // -parse-noop |
| 2008 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | 747a95e | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 2009 | ParseFile(PP, new MinimalAction(PP)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2010 | ClearSourceMgr = true; |
| 2011 | break; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2012 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2013 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2014 | case ParsePrintCallbacks: { |
| 2015 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Daniel Dunbar | 747a95e | 2008-10-31 08:56:51 +0000 | [diff] [blame] | 2016 | ParseFile(PP, CreatePrintParserActionsAction(PP)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2017 | ClearSourceMgr = true; |
| 2018 | break; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | case ParseSyntaxOnly: { // -fsyntax-only |
| 2022 | llvm::TimeRegion Timer(ClangFrontendTimer); |
Ted Kremenek | 50aab98 | 2008-08-08 02:46:37 +0000 | [diff] [blame] | 2023 | Consumer.reset(new ASTConsumer()); |
Ted Kremenek | 0a03ce6 | 2007-09-17 20:49:30 +0000 | [diff] [blame] | 2024 | break; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2025 | } |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 2026 | |
| 2027 | case RewriteMacros: |
Chris Lattner | 302b062 | 2008-05-09 22:43:24 +0000 | [diff] [blame] | 2028 | RewriteMacrosInInput(PP, InFile, OutputFile); |
Chris Lattner | 1665a9f | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 2029 | ClearSourceMgr = true; |
| 2030 | break; |
Chris Lattner | c3fbf39 | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2032 | case RewriteTest: { |
Chris Lattner | c3fbf39 | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 2033 | DoRewriteTest(PP, InFile, OutputFile); |
| 2034 | ClearSourceMgr = true; |
| 2035 | break; |
Chris Lattner | 129758d | 2007-09-16 19:46:59 +0000 | [diff] [blame] | 2036 | } |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2037 | |
| 2038 | case FixIt: |
| 2039 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| 2040 | Consumer.reset(new ASTConsumer()); |
Douglas Gregor | 563a251 | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 2041 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2042 | PP.getSourceManager()); |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2043 | break; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2044 | } |
Ted Kremenek | 88eebed | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 2045 | |
| 2046 | if (Consumer) { |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2047 | llvm::OwningPtr<ASTContext> ContextOwner; |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2048 | |
Douglas Gregor | 24b48b0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 2049 | if (FixItAtLocations.size() > 0) { |
| 2050 | // Even without the "-fixit" flag, with may have some specific |
| 2051 | // locations where the user has requested fixes. Process those |
| 2052 | // locations now. |
| 2053 | if (!FixItRewrite) |
| 2054 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
| 2055 | PP.getSourceManager()); |
| 2056 | |
| 2057 | bool AddedFixitLocation = false; |
| 2058 | for (unsigned Idx = 0, Last = FixItAtLocations.size(); |
| 2059 | Idx != Last; ++Idx) { |
| 2060 | RequestedSourceLocation Requested; |
| 2061 | if (FixItAtLocations[Idx].ResolveLocation(PP.getFileManager(), |
| 2062 | Requested)) { |
| 2063 | fprintf(stderr, "FIX-IT could not find file \"%s\"\n", |
| 2064 | FixItAtLocations[Idx].FileName.c_str()); |
| 2065 | } else { |
| 2066 | FixItRewrite->addFixItLocation(Requested); |
| 2067 | AddedFixitLocation = true; |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | if (!AddedFixitLocation) { |
| 2072 | // All of the fix-it locations were bad. Don't fix anything. |
| 2073 | delete FixItRewrite; |
| 2074 | FixItRewrite = 0; |
| 2075 | } |
| 2076 | } |
| 2077 | |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2078 | ContextOwner.reset(new ASTContext(PP.getLangOptions(), |
| 2079 | PP.getSourceManager(), |
| 2080 | PP.getTargetInfo(), |
| 2081 | PP.getIdentifierTable(), |
| 2082 | PP.getSelectorTable(), |
| 2083 | /* FreeMemory = */ !DisableFree)); |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2084 | |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2085 | if (!ImplicitIncludePCH.empty()) { |
| 2086 | // The user has asked us to include a precompiled header. Load |
| 2087 | // the precompiled header into the AST context. |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 2088 | llvm::OwningPtr<PCHReader> |
| 2089 | Reader(new clang::PCHReader(PP, *ContextOwner.get())); |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2090 | if (Reader->ReadPCH(ImplicitIncludePCH)) |
| 2091 | return; |
| 2092 | |
| 2093 | llvm::OwningPtr<ExternalASTSource> Source(Reader.take()); |
| 2094 | ContextOwner->setExternalSource(Source); |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 2095 | |
| 2096 | // Finish preprocessor initialization. We do this now (rather |
| 2097 | // than earlier) because this initialization creates new source |
| 2098 | // location entries in the source manager, which must come after |
| 2099 | // the source location entries for the PCH file. |
| 2100 | if (PPF.FinishInitialization(&PP)) |
| 2101 | return; |
Douglas Gregor | c34897d | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
Chris Lattner | 07b08c0 | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 2104 | ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats); |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2105 | |
Douglas Gregor | 133d255 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 2106 | if (FixItRewrite) |
| 2107 | FixItRewrite->WriteFixedFile(InFile, OutputFile); |
| 2108 | |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2109 | // If in -disable-free mode, don't deallocate these when they go out of |
| 2110 | // scope. |
Chris Lattner | 07b08c0 | 2009-03-28 04:13:34 +0000 | [diff] [blame] | 2111 | if (DisableFree) |
Chris Lattner | 143fd6d | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 2112 | ContextOwner.take(); |
Ted Kremenek | 88eebed | 2009-01-28 04:29:29 +0000 | [diff] [blame] | 2113 | } |
Daniel Dunbar | 849bfc6 | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 2114 | |
| 2115 | if (VerifyDiagnostics) |
Daniel Dunbar | 8d4dff5 | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 2116 | if (CheckDiagnostics(PP)) |
| 2117 | exit(1); |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 2118 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2119 | if (Stats) { |
Ted Kremenek | d890f6a | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 2120 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2121 | PP.PrintStats(); |
| 2122 | PP.getIdentifierTable().PrintStats(); |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 2123 | PP.getHeaderSearchInfo().PrintStats(); |
Ted Kremenek | 40997b6 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 2124 | PP.getSourceManager().PrintStats(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2125 | fprintf(stderr, "\n"); |
| 2126 | } |
| 2127 | |
| 2128 | // For a multi-file compilation, some things are ok with nuking the source |
| 2129 | // manager tables, other require stable fileid/macroid's across multiple |
| 2130 | // files. |
Chris Lattner | 968982d | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 2131 | if (ClearSourceMgr) |
| 2132 | PP.getSourceManager().clearIDTables(); |
Daniel Dunbar | 622d6d0 | 2008-11-11 06:35:39 +0000 | [diff] [blame] | 2133 | |
| 2134 | if (DisableFree) |
| 2135 | Consumer.take(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2138 | static void ProcessSerializedFile(const std::string& InFile, Diagnostic& Diag, |
| 2139 | FileManager& FileMgr) { |
| 2140 | |
| 2141 | if (VerifyDiagnostics) { |
| 2142 | fprintf(stderr, "-verify does not yet work with serialized ASTs.\n"); |
| 2143 | exit (1); |
| 2144 | } |
| 2145 | |
| 2146 | llvm::sys::Path Filename(InFile); |
| 2147 | |
| 2148 | if (!Filename.isValid()) { |
| 2149 | fprintf(stderr, "serialized file '%s' not available.\n",InFile.c_str()); |
| 2150 | exit (1); |
| 2151 | } |
| 2152 | |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2153 | llvm::OwningPtr<ASTContext> Ctx; |
Chris Lattner | 06459ae | 2009-03-28 03:49:26 +0000 | [diff] [blame] | 2154 | |
| 2155 | // Create the memory buffer that contains the contents of the file. |
| 2156 | llvm::OwningPtr<llvm::MemoryBuffer> |
| 2157 | MBuffer(llvm::MemoryBuffer::getFile(Filename.c_str())); |
| 2158 | |
| 2159 | if (MBuffer) |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2160 | Ctx.reset(ASTContext::ReadASTBitcodeBuffer(*MBuffer, FileMgr)); |
Ted Kremenek | 2bd4241 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 2161 | |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2162 | if (!Ctx) { |
Ted Kremenek | 2bd4241 | 2007-12-13 18:11:11 +0000 | [diff] [blame] | 2163 | fprintf(stderr, "error: file '%s' could not be deserialized\n", |
| 2164 | InFile.c_str()); |
| 2165 | exit (1); |
| 2166 | } |
| 2167 | |
Ted Kremenek | ab74937 | 2007-12-19 19:27:38 +0000 | [diff] [blame] | 2168 | // Observe that we use the source file name stored in the deserialized |
| 2169 | // translation unit, rather than InFile. |
Ted Kremenek | 0c7cd7a | 2007-12-20 19:47:16 +0000 | [diff] [blame] | 2170 | llvm::OwningPtr<ASTConsumer> |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2171 | Consumer(CreateASTConsumer(InFile, Diag, FileMgr, Ctx->getLangOptions(), |
Ted Kremenek | 7c65b6c | 2008-08-05 18:50:11 +0000 | [diff] [blame] | 2172 | 0, 0)); |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 2173 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2174 | if (!Consumer) { |
| 2175 | fprintf(stderr, "Unsupported program action with serialized ASTs!\n"); |
| 2176 | exit (1); |
| 2177 | } |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 2178 | |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2179 | Consumer->Initialize(*Ctx); |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 2180 | |
Chris Lattner | 8d72ee0 | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 2181 | // FIXME: We need to inform Consumer about completed TagDecls as well. |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2182 | TranslationUnitDecl *TUD = Ctx->getTranslationUnitDecl(); |
Douglas Gregor | c55b0b0 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 2183 | for (DeclContext::decl_iterator I = TUD->decls_begin(*Ctx), |
| 2184 | E = TUD->decls_end(*Ctx); |
Chris Lattner | f4fbc44 | 2009-03-28 04:27:18 +0000 | [diff] [blame] | 2185 | I != E; ++I) |
Chris Lattner | a17991f | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 2186 | Consumer->HandleTopLevelDecl(DeclGroupRef(*I)); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2190 | static llvm::cl::list<std::string> |
| 2191 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 2192 | |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2193 | static bool isSerializedFile(const std::string& InFile) { |
| 2194 | if (InFile.size() < 4) |
| 2195 | return false; |
| 2196 | |
| 2197 | const char* s = InFile.c_str()+InFile.size()-4; |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2198 | return s[0] == '.' && s[1] == 'a' && s[2] == 's' && s[3] == 't'; |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2201 | |
| 2202 | int main(int argc, char **argv) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2203 | llvm::sys::PrintStackTraceOnErrorSignal(); |
Chris Lattner | 2b2d0c4 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 2204 | llvm::PrettyStackTraceProgram X(argc, argv); |
Chris Lattner | cb7dddb | 2009-03-06 05:38:04 +0000 | [diff] [blame] | 2205 | llvm::cl::ParseCommandLineOptions(argc, argv, |
Chris Lattner | 559a747 | 2009-03-06 05:38:25 +0000 | [diff] [blame] | 2206 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2207 | |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2208 | if (TimeReport) |
| 2209 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
| 2210 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2211 | // If no input was specified, read from stdin. |
| 2212 | if (InputFilenames.empty()) |
| 2213 | InputFilenames.push_back("-"); |
Chris Lattner | 93d4d98 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 2214 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2215 | // Create a file manager object to provide access to and cache the filesystem. |
| 2216 | FileManager FileMgr; |
| 2217 | |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2218 | // Create the diagnostic client for reporting errors or for |
| 2219 | // implementing -verify. |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 2220 | DiagnosticClient* TextDiagClient = 0; |
Ted Kremenek | fd75e31 | 2008-03-27 06:17:42 +0000 | [diff] [blame] | 2221 | |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2222 | if (!VerifyDiagnostics) { |
| 2223 | // Print diagnostics to stderr by default. |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 2224 | TextDiagClient = new TextDiagnosticPrinter(llvm::errs(), |
| 2225 | !NoShowColumn, |
Chris Lattner | b96a04f | 2009-01-30 19:01:41 +0000 | [diff] [blame] | 2226 | !NoCaretDiagnostics, |
Chris Lattner | 695a4f5 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 2227 | !NoShowLocation, |
| 2228 | PrintSourceRangeInfo); |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2229 | } else { |
| 2230 | // When checking diagnostics, just buffer them up. |
| 2231 | TextDiagClient = new TextDiagnosticBuffer(); |
| 2232 | |
| 2233 | if (InputFilenames.size() != 1) { |
| 2234 | fprintf(stderr, |
| 2235 | "-verify only works on single input files for now.\n"); |
| 2236 | return 1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2237 | } |
| 2238 | } |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2239 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2240 | // Configure our handling of diagnostics. |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2241 | llvm::OwningPtr<DiagnosticClient> DiagClient(TextDiagClient); |
| 2242 | Diagnostic Diags(DiagClient.get()); |
Sebastian Redl | f10cbca | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 2243 | if (ProcessWarningOptions(Diags)) |
Sebastian Redl | 44ff86c | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 2244 | return 1; |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2245 | |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 2246 | // -I- is a deprecated GCC feature, scan for it and reject it. |
| 2247 | for (unsigned i = 0, e = I_dirs.size(); i != e; ++i) { |
| 2248 | if (I_dirs[i] == "-") { |
Chris Lattner | a143347 | 2008-11-18 05:05:28 +0000 | [diff] [blame] | 2249 | Diags.Report(FullSourceLoc(), diag::err_pp_I_dash_not_supported); |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 2250 | I_dirs.erase(I_dirs.begin()+i); |
| 2251 | --i; |
| 2252 | } |
| 2253 | } |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2254 | |
| 2255 | // Get information about the target being compiled for. |
| 2256 | std::string Triple = CreateTargetTriple(); |
Ted Kremenek | ec6c525 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 2257 | llvm::OwningPtr<TargetInfo> Target(TargetInfo::CreateTargetInfo(Triple)); |
| 2258 | |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2259 | if (Target == 0) { |
Daniel Dunbar | f45afe6 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 2260 | Diags.Report(FullSourceLoc(), diag::err_fe_unknown_triple) |
| 2261 | << Triple.c_str(); |
Sebastian Redl | f10cbca | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 2262 | return 1; |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2263 | } |
Chris Lattner | 45a56e0 | 2007-12-05 23:24:17 +0000 | [diff] [blame] | 2264 | |
Daniel Dunbar | 9c32110 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 2265 | if (!InheritanceViewCls.empty()) // C++ visualization? |
Ted Kremenek | d9ceb3d | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 2266 | ProgAction = InheritanceView; |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2267 | |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 2268 | llvm::OwningPtr<SourceManager> SourceMgr; |
| 2269 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2270 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2271 | const std::string &InFile = InputFilenames[i]; |
Ted Kremenek | b240e82 | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 2272 | |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2273 | if (isSerializedFile(InFile)) { |
| 2274 | Diags.setClient(TextDiagClient); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2275 | ProcessSerializedFile(InFile,Diags,FileMgr); |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2276 | continue; |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2277 | } |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2278 | |
| 2279 | /// Create a SourceManager object. This tracks and owns all the file |
| 2280 | /// buffers allocated to a translation unit. |
| 2281 | if (!SourceMgr) |
| 2282 | SourceMgr.reset(new SourceManager()); |
| 2283 | else |
| 2284 | SourceMgr->clearIDTables(); |
| 2285 | |
| 2286 | // Initialize language options, inferring file types from input filenames. |
| 2287 | LangOptions LangInfo; |
| 2288 | InitializeBaseLanguage(); |
| 2289 | LangKind LK = GetLanguage(InFile); |
Daniel Dunbar | db6126e | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 2290 | InitializeLangOptions(LangInfo, LK); |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2291 | InitializeGCMode(LangInfo); |
Fariborz Jahanian | 4934333 | 2009-04-03 03:28:57 +0000 | [diff] [blame] | 2292 | InitializeSymbolVisibility(LangInfo); |
Mike Stump | db78991 | 2009-04-01 20:28:16 +0000 | [diff] [blame] | 2293 | InitializeOverflowChecking(LangInfo); |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2294 | InitializeLanguageStandard(LangInfo, LK, Target.get()); |
| 2295 | |
| 2296 | // Process the -I options and set them in the HeaderInfo. |
| 2297 | HeaderSearch HeaderInfo(FileMgr); |
| 2298 | |
| 2299 | InitializeIncludePaths(argv[0], HeaderInfo, FileMgr, LangInfo); |
| 2300 | |
| 2301 | // Set up the preprocessor with these options. |
| 2302 | DriverPreprocessorFactory PPFactory(InFile, Diags, LangInfo, *Target, |
| 2303 | *SourceMgr.get(), HeaderInfo); |
| 2304 | |
| 2305 | llvm::OwningPtr<Preprocessor> PP(PPFactory.CreatePreprocessor()); |
| 2306 | |
| 2307 | if (!PP) |
| 2308 | continue; |
Ted Kremenek | 5c34173 | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 2309 | |
Douglas Gregor | ab1cef7 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 2310 | if (ImplicitIncludePCH.empty() |
| 2311 | && PPFactory.FinishInitialization(PP.get())) |
| 2312 | continue; |
| 2313 | |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2314 | // Create the HTMLDiagnosticsClient if we are using one. Otherwise, |
| 2315 | // always reset to using TextDiagClient. |
| 2316 | llvm::OwningPtr<DiagnosticClient> TmpClient; |
| 2317 | |
| 2318 | if (!HTMLDiag.empty()) { |
| 2319 | TmpClient.reset(CreateHTMLDiagnosticClient(HTMLDiag, PP.get(), |
| 2320 | &PPFactory)); |
| 2321 | Diags.setClient(TmpClient.get()); |
Ted Kremenek | 80d5337 | 2007-12-12 23:41:08 +0000 | [diff] [blame] | 2322 | } |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2323 | else |
| 2324 | Diags.setClient(TextDiagClient); |
| 2325 | |
| 2326 | // Process the source file. |
Daniel Dunbar | db6126e | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 2327 | ProcessInputFile(*PP, PPFactory, InFile, ProgAction); |
Chris Lattner | 2b98956 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 2328 | |
| 2329 | HeaderInfo.ClearFileInfo(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2330 | } |
Chris Lattner | 2c77d85 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 2331 | |
Mike Stump | 91d0135 | 2009-01-28 02:43:35 +0000 | [diff] [blame] | 2332 | if (Verbose) |
| 2333 | fprintf(stderr, "clang version 1.0 based upon " PACKAGE_STRING |
| 2334 | " hosted on " LLVM_HOSTTRIPLE "\n"); |
| 2335 | |
Ted Kremenek | ec6c525 | 2008-08-07 18:13:12 +0000 | [diff] [blame] | 2336 | if (unsigned NumDiagnostics = Diags.getNumDiagnostics()) |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2337 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 2338 | (NumDiagnostics == 1 ? "" : "s")); |
| 2339 | |
| 2340 | if (Stats) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2341 | FileMgr.PrintStats(); |
| 2342 | fprintf(stderr, "\n"); |
| 2343 | } |
| 2344 | |
Daniel Dunbar | 8d4dff5 | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 2345 | // If verifying diagnostics and we reached here, all is well. |
| 2346 | if (VerifyDiagnostics) |
| 2347 | return 0; |
Chris Lattner | efe3338 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 2348 | |
| 2349 | delete ClangFrontendTimer; |
Daniel Dunbar | 8d4dff5 | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 2350 | |
Daniel Dunbar | bb298c0 | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 2351 | // Managed static deconstruction. Useful for making things like |
| 2352 | // -time-passes usable. |
| 2353 | llvm::llvm_shutdown(); |
| 2354 | |
Daniel Dunbar | 70a66b1 | 2008-10-04 23:42:49 +0000 | [diff] [blame] | 2355 | return HadErrors || (Diags.getNumErrors() != 0); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 2356 | } |