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