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