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