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