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