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