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