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