| 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 | //===----------------------------------------------------------------------===// |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | |
| Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 18 | #include "Options.h" |
| Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTConsumer.h" |
| 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/Decl.h" |
| 22 | #include "clang/AST/DeclGroup.h" |
| 23 | #include "clang/Analysis/PathDiagnostic.h" |
| 24 | #include "clang/Basic/FileManager.h" |
| 25 | #include "clang/Basic/SourceManager.h" |
| 26 | #include "clang/Basic/TargetInfo.h" |
| 27 | #include "clang/Basic/Version.h" |
| 28 | #include "clang/CodeGen/ModuleBuilder.h" |
| Eli Friedman | 8ceb0d9 | 2009-05-18 23:02:01 +0000 | [diff] [blame] | 29 | #include "clang/Frontend/ASTConsumers.h" |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 30 | #include "clang/Frontend/ASTUnit.h" |
| Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 31 | #include "clang/Frontend/AnalysisConsumer.h" |
| Daniel Dunbar | dbf75fe | 2009-11-11 08:13:24 +0000 | [diff] [blame] | 32 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
| Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 33 | #include "clang/Frontend/CommandLineSourceLoc.h" |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 34 | #include "clang/Frontend/CompilerInvocation.h" |
| Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 35 | #include "clang/Frontend/DependencyOutputOptions.h" |
| Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 36 | #include "clang/Frontend/FixItRewriter.h" |
| Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 37 | #include "clang/Frontend/FrontendDiagnostic.h" |
| Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 38 | #include "clang/Frontend/PCHReader.h" |
| Daniel Dunbar | 8863b98 | 2009-11-07 04:20:15 +0000 | [diff] [blame] | 39 | #include "clang/Frontend/PathDiagnosticClients.h" |
| 40 | #include "clang/Frontend/PreprocessorOptions.h" |
| Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 41 | #include "clang/Frontend/PreprocessorOutputOptions.h" |
| Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 42 | #include "clang/Frontend/TextDiagnosticBuffer.h" |
| 43 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 44 | #include "clang/Frontend/Utils.h" |
| Daniel Dunbar | 775bee7 | 2009-11-11 10:07:22 +0000 | [diff] [blame] | 45 | #include "clang/Lex/HeaderSearch.h" |
| 46 | #include "clang/Lex/LexDiagnostic.h" |
| 47 | #include "clang/Parse/Parser.h" |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 48 | #include "clang/Sema/CodeCompleteConsumer.h" |
| Chris Lattner | e91c134 | 2008-02-06 00:23:21 +0000 | [diff] [blame] | 49 | #include "clang/Sema/ParseAST.h" |
| Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 50 | #include "clang/Sema/SemaDiagnostic.h" |
| Owen Anderson | 42253cc | 2009-07-01 17:00:06 +0000 | [diff] [blame] | 51 | #include "llvm/LLVMContext.h" |
| Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/OwningPtr.h" |
| Chris Lattner | 8f3dab8 | 2007-12-15 23:20:07 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SmallPtrSet.h" |
| Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/StringExtras.h" |
| Daniel Dunbar | 868bd0a | 2009-05-06 03:16:41 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/StringMap.h" |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/StringSwitch.h" |
| Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 57 | #include "llvm/Config/config.h" |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | #include "llvm/Support/CommandLine.h" |
| Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 59 | #include "llvm/Support/ErrorHandling.h" |
| Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 60 | #include "llvm/Support/ManagedStatic.h" |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 61 | #include "llvm/Support/MemoryBuffer.h" |
| Zhongxing Xu | 2092236 | 2008-11-26 05:23:17 +0000 | [diff] [blame] | 62 | #include "llvm/Support/PluginLoader.h" |
| Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 63 | #include "llvm/Support/PrettyStackTrace.h" |
| Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Timer.h" |
| Chris Lattner | 0fa0daa | 2009-08-24 04:11:30 +0000 | [diff] [blame] | 65 | #include "llvm/Support/raw_ostream.h" |
| Daniel Dunbar | e553a72 | 2008-10-02 01:21:33 +0000 | [diff] [blame] | 66 | #include "llvm/System/Host.h" |
| Chris Lattner | dcaa096 | 2008-03-03 03:16:03 +0000 | [diff] [blame] | 67 | #include "llvm/System/Path.h" |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 68 | #include "llvm/System/Program.h" |
| Chris Lattner | ba0f25f | 2008-09-30 20:16:56 +0000 | [diff] [blame] | 69 | #include "llvm/System/Signals.h" |
| Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 70 | #include "llvm/Target/TargetSelect.h" |
| Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 71 | #include <cstdlib> |
| Douglas Gregor | 44cf08e | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 72 | #if HAVE_SYS_TYPES_H |
| Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 73 | # include <sys/types.h> |
| 74 | #endif |
| Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 75 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | using namespace clang; |
| 77 | |
| 78 | //===----------------------------------------------------------------------===// |
| 79 | // Global options. |
| 80 | //===----------------------------------------------------------------------===// |
| 81 | |
| Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 82 | /// ClangFrontendTimer - The front-end activities should charge time to it with |
| 83 | /// TimeRegion. The -ftime-report option controls whether this will do |
| 84 | /// anything. |
| 85 | llvm::Timer *ClangFrontendTimer = 0; |
| 86 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | static llvm::cl::opt<bool> |
| 88 | Verbose("v", llvm::cl::desc("Enable verbose output")); |
| 89 | static llvm::cl::opt<bool> |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | Stats("print-stats", |
| Nate Begeman | aabbb12 | 2007-12-30 01:38:50 +0000 | [diff] [blame] | 91 | llvm::cl::desc("Print performance metrics and statistics")); |
| Daniel Dunbar | d3db401 | 2008-10-16 16:54:18 +0000 | [diff] [blame] | 92 | static llvm::cl::opt<bool> |
| 93 | DisableFree("disable-free", |
| 94 | llvm::cl::desc("Disable freeing of memory on exit"), |
| 95 | llvm::cl::init(false)); |
| Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 96 | static llvm::cl::opt<bool> |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | EmptyInputOnly("empty-input-only", |
| Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 98 | llvm::cl::desc("Force running on an empty input file")); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | |
| 100 | enum ProgActions { |
| Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 101 | RewriteObjC, // ObjC->C Rewriter. |
| Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 102 | RewriteBlocks, // ObjC->C Rewriter for Blocks. |
| Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 103 | RewriteMacros, // Expand macros but not #includes. |
| Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 104 | RewriteTest, // Rewriter playground |
| Ted Kremenek | 13e479b | 2008-03-19 07:53:42 +0000 | [diff] [blame] | 105 | HTMLTest, // HTML displayer testing stuff. |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 106 | EmitAssembly, // Emit a .s file. |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 107 | EmitLLVM, // Emit a .ll file. |
| Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 108 | EmitBC, // Emit a .bc file. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | EmitLLVMOnly, // Generate LLVM IR, but do not |
| Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 110 | EmitHTML, // Translate input source into HTML. |
| Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 111 | ASTPrint, // Parse ASTs and print them. |
| Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 112 | ASTPrintXML, // Parse ASTs and print them in XML. |
| Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 113 | ASTDump, // Parse ASTs and dump them. |
| 114 | ASTView, // Parse ASTs and view them in Graphviz. |
| Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 115 | PrintDeclContext, // Print DeclContext and their Decls. |
| Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 116 | DumpRecordLayouts, // Dump record layout information. |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 117 | ParsePrintCallbacks, // Parse and print each callback. |
| 118 | ParseSyntaxOnly, // Parse and perform semantic analysis. |
| 119 | ParseNoop, // Parse with noop callbacks. |
| 120 | RunPreprocessorOnly, // Just lex, no output. |
| 121 | PrintPreprocessedInput, // -E mode. |
| Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 122 | DumpTokens, // Dump out preprocessed tokens. |
| 123 | DumpRawTokens, // Dump out raw tokens. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 124 | RunAnalysis, // Run one or more source code analyses. |
| Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 125 | GeneratePTH, // Generate pre-tokenized header. |
| Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 126 | GeneratePCH, // Generate pre-compiled header. |
| Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 127 | InheritanceView // View C++ inheritance for a specified class. |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | static llvm::cl::opt<ProgActions> |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | ProgAction(llvm::cl::desc("Choose output type:"), llvm::cl::ZeroOrMore, |
| 132 | llvm::cl::init(ParseSyntaxOnly), |
| 133 | llvm::cl::values( |
| 134 | clEnumValN(RunPreprocessorOnly, "Eonly", |
| 135 | "Just run preprocessor, no output (for timings)"), |
| 136 | clEnumValN(PrintPreprocessedInput, "E", |
| 137 | "Run preprocessor, emit preprocessed file"), |
| Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 138 | clEnumValN(DumpRawTokens, "dump-raw-tokens", |
| 139 | "Lex file in raw mode and dump raw tokens"), |
| Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 140 | clEnumValN(RunAnalysis, "analyze", |
| 141 | "Run static analysis engine"), |
| Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 142 | clEnumValN(DumpTokens, "dump-tokens", |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | "Run preprocessor, dump internal rep of tokens"), |
| 144 | clEnumValN(ParseNoop, "parse-noop", |
| 145 | "Run parser with noop callbacks (for timings)"), |
| 146 | clEnumValN(ParseSyntaxOnly, "fsyntax-only", |
| 147 | "Run parser and perform semantic analysis"), |
| 148 | clEnumValN(ParsePrintCallbacks, "parse-print-callbacks", |
| 149 | "Run parser and print each callback invoked"), |
| Ted Kremenek | 6a34083 | 2008-03-18 21:19:49 +0000 | [diff] [blame] | 150 | clEnumValN(EmitHTML, "emit-html", |
| 151 | "Output input source as HTML"), |
| Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 152 | clEnumValN(ASTPrint, "ast-print", |
| 153 | "Build ASTs and then pretty-print them"), |
| Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 154 | clEnumValN(ASTPrintXML, "ast-print-xml", |
| 155 | "Build ASTs and then print them in XML format"), |
| Chris Lattner | 3b427b3 | 2007-10-11 00:18:28 +0000 | [diff] [blame] | 156 | clEnumValN(ASTDump, "ast-dump", |
| 157 | "Build ASTs and then debug dump them"), |
| Chris Lattner | ea254db | 2007-10-11 00:37:43 +0000 | [diff] [blame] | 158 | clEnumValN(ASTView, "ast-view", |
| Sanjiv Gupta | 56cf96b | 2008-05-08 08:28:14 +0000 | [diff] [blame] | 159 | "Build ASTs and view them with GraphViz"), |
| Zhongxing Xu | 2d75d6f | 2009-01-13 01:29:24 +0000 | [diff] [blame] | 160 | clEnumValN(PrintDeclContext, "print-decl-contexts", |
| Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 161 | "Print DeclContexts and their Decls"), |
| Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 162 | clEnumValN(DumpRecordLayouts, "dump-record-layouts", |
| 163 | "Dump record layout information"), |
| Douglas Gregor | bf1bd6e | 2009-04-02 23:43:50 +0000 | [diff] [blame] | 164 | clEnumValN(GeneratePTH, "emit-pth", |
| Ted Kremenek | 08478eb | 2009-04-01 00:23:28 +0000 | [diff] [blame] | 165 | "Generate pre-tokenized header file"), |
| Douglas Gregor | 2cf2634 | 2009-04-09 22:27:44 +0000 | [diff] [blame] | 166 | clEnumValN(GeneratePCH, "emit-pch", |
| 167 | "Generate pre-compiled header file"), |
| Daniel Dunbar | d69bacc | 2008-10-21 23:49:24 +0000 | [diff] [blame] | 168 | clEnumValN(EmitAssembly, "S", |
| 169 | "Emit native assembly code"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | clEnumValN(EmitLLVM, "emit-llvm", |
| Ted Kremenek | 27b07c5 | 2007-09-06 21:26:58 +0000 | [diff] [blame] | 171 | "Build ASTs then convert to LLVM, emit .ll file"), |
| Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 172 | clEnumValN(EmitBC, "emit-llvm-bc", |
| 173 | "Build ASTs then convert to LLVM, emit .bc file"), |
| Daniel Dunbar | e8e2600 | 2009-02-26 22:39:37 +0000 | [diff] [blame] | 174 | clEnumValN(EmitLLVMOnly, "emit-llvm-only", |
| 175 | "Build ASTs and convert to LLVM, discarding output"), |
| Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 176 | clEnumValN(RewriteTest, "rewrite-test", |
| 177 | "Rewriter playground"), |
| Steve Naroff | b29b427 | 2008-04-14 22:03:09 +0000 | [diff] [blame] | 178 | clEnumValN(RewriteObjC, "rewrite-objc", |
| Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 179 | "Rewrite ObjC into C (code rewriter example)"), |
| 180 | clEnumValN(RewriteMacros, "rewrite-macros", |
| 181 | "Expand macros without full preprocessing"), |
| Steve Naroff | 1318895 | 2008-09-18 14:10:13 +0000 | [diff] [blame] | 182 | clEnumValN(RewriteBlocks, "rewrite-blocks", |
| 183 | "Rewrite Blocks to C"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | clEnumValEnd)); |
| 185 | |
| Ted Kremenek | ccc7647 | 2007-12-19 19:47:59 +0000 | [diff] [blame] | 186 | |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 187 | enum CodeCompletionPrinter { |
| 188 | CCP_Debug, |
| 189 | CCP_CIndex |
| 190 | }; |
| 191 | |
| Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 192 | static llvm::cl::opt<ParsedSourceLocation> |
| 193 | CodeCompletionAt("code-completion-at", |
| 194 | llvm::cl::value_desc("file:line:column"), |
| 195 | llvm::cl::desc("Dump code-completion information at a location")); |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 196 | |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 197 | static llvm::cl::opt<CodeCompletionPrinter> |
| 198 | CodeCompletionPrinter("code-completion-printer", |
| 199 | llvm::cl::desc("Choose output type:"), |
| 200 | llvm::cl::init(CCP_Debug), |
| 201 | llvm::cl::values( |
| 202 | clEnumValN(CCP_Debug, "debug", |
| 203 | "Debug code-completion results"), |
| 204 | clEnumValN(CCP_CIndex, "cindex", |
| 205 | "Code-completion results for the CIndex library"), |
| 206 | clEnumValEnd)); |
| 207 | |
| 208 | static llvm::cl::opt<bool> |
| 209 | CodeCompletionWantsMacros("code-completion-macros", |
| 210 | llvm::cl::desc("Include macros in code-completion results")); |
| 211 | |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 212 | /// \brief Buld a new code-completion consumer that prints the results of |
| 213 | /// code completion to standard output. |
| 214 | static CodeCompleteConsumer *BuildPrintingCodeCompleter(Sema &S, void *) { |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 215 | switch (CodeCompletionPrinter.getValue()) { |
| 216 | case CCP_Debug: |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 217 | return new PrintingCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 218 | llvm::outs()); |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 219 | |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 220 | case CCP_CIndex: |
| 221 | return new CIndexCodeCompleteConsumer(S, CodeCompletionWantsMacros, |
| 222 | llvm::outs()); |
| 223 | }; |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 224 | |
| Douglas Gregor | 0c8296d | 2009-11-07 00:00:49 +0000 | [diff] [blame] | 225 | return 0; |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| Ted Kremenek | c2e7299 | 2008-12-02 19:57:31 +0000 | [diff] [blame] | 228 | //===----------------------------------------------------------------------===// |
| Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 229 | // C++ Visualization. |
| 230 | //===----------------------------------------------------------------------===// |
| 231 | |
| 232 | static llvm::cl::opt<std::string> |
| 233 | InheritanceViewCls("cxx-inheritance-view", |
| 234 | llvm::cl::value_desc("class name"), |
| Daniel Dunbar | d77b251 | 2009-01-14 18:56:36 +0000 | [diff] [blame] | 235 | llvm::cl::desc("View C++ inheritance for a specified class")); |
| Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 236 | |
| 237 | //===----------------------------------------------------------------------===// |
| Daniel Dunbar | 0db4b76 | 2009-11-11 08:13:40 +0000 | [diff] [blame] | 238 | // Frontend Options |
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 239 | //===----------------------------------------------------------------------===// |
| Chris Lattner | b2509e1 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 240 | |
| Daniel Dunbar | d1cdcf3 | 2009-11-11 08:13:47 +0000 | [diff] [blame] | 241 | static llvm::cl::list<std::string> |
| 242 | InputFilenames(llvm::cl::Positional, llvm::cl::desc("<input files>")); |
| 243 | |
| 244 | static llvm::cl::opt<std::string> |
| 245 | OutputFile("o", |
| 246 | llvm::cl::value_desc("path"), |
| 247 | llvm::cl::desc("Specify output file")); |
| 248 | |
| Chris Lattner | b2509e1 | 2009-02-18 01:12:43 +0000 | [diff] [blame] | 249 | static llvm::cl::opt<bool> |
| 250 | TimeReport("ftime-report", |
| 251 | llvm::cl::desc("Print the amount of time each " |
| 252 | "phase of compilation takes")); |
| 253 | |
| Daniel Dunbar | 0db4b76 | 2009-11-11 08:13:40 +0000 | [diff] [blame] | 254 | static llvm::cl::opt<bool> |
| 255 | VerifyDiagnostics("verify", |
| 256 | llvm::cl::desc("Verify emitted diagnostics and warnings")); |
| 257 | |
| Douglas Gregor | 3573c0c | 2009-02-14 20:49:29 +0000 | [diff] [blame] | 258 | //===----------------------------------------------------------------------===// |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | // Language Options |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 262 | static llvm::cl::opt<LangKind> |
| 263 | BaseLang("x", llvm::cl::desc("Base language to compile"), |
| 264 | llvm::cl::init(langkind_unspecified), |
| 265 | llvm::cl::values(clEnumValN(langkind_c, "c", "C"), |
| Nate Begeman | 4e3629e | 2009-06-25 22:43:10 +0000 | [diff] [blame] | 266 | clEnumValN(langkind_ocl, "cl", "OpenCL C"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 267 | clEnumValN(langkind_cxx, "c++", "C++"), |
| 268 | clEnumValN(langkind_objc, "objective-c", "Objective C"), |
| 269 | clEnumValN(langkind_objcxx,"objective-c++","Objective C++"), |
| Daniel Dunbar | d2ea386 | 2009-01-29 23:50:47 +0000 | [diff] [blame] | 270 | clEnumValN(langkind_c_cpp, "cpp-output", |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | "Preprocessed C"), |
| Chris Lattner | a778d7d | 2008-10-22 17:29:21 +0000 | [diff] [blame] | 272 | clEnumValN(langkind_asm_cpp, "assembler-with-cpp", |
| 273 | "Preprocessed asm"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | clEnumValN(langkind_cxx_cpp, "c++-cpp-output", |
| Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 275 | "Preprocessed C++"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | clEnumValN(langkind_objc_cpp, "objective-c-cpp-output", |
| 277 | "Preprocessed Objective C"), |
| Chris Lattner | c76d807 | 2009-02-06 06:19:20 +0000 | [diff] [blame] | 278 | clEnumValN(langkind_objcxx_cpp, "objective-c++-cpp-output", |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | "Preprocessed Objective C++"), |
| Daniel Dunbar | 0b5b0da | 2009-04-01 05:09:09 +0000 | [diff] [blame] | 280 | clEnumValN(langkind_c, "c-header", |
| 281 | "C header"), |
| 282 | clEnumValN(langkind_objc, "objective-c-header", |
| 283 | "Objective-C header"), |
| 284 | clEnumValN(langkind_cxx, "c++-header", |
| 285 | "C++ header"), |
| 286 | clEnumValN(langkind_objcxx, "objective-c++-header", |
| 287 | "Objective-C++ header"), |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 288 | clEnumValN(langkind_ast, "ast", |
| 289 | "Clang AST"), |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 290 | clEnumValEnd)); |
| 291 | |
| Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 292 | static llvm::cl::opt<std::string> |
| 293 | TargetTriple("triple", |
| 294 | llvm::cl::desc("Specify target triple (e.g. i686-apple-darwin9)")); |
| 295 | |
| Daniel Dunbar | 5674908 | 2009-11-11 07:26:12 +0000 | [diff] [blame] | 296 | static llvm::cl::opt<std::string> |
| 297 | TargetABI("target-abi", |
| 298 | llvm::cl::desc("Target a particular ABI type")); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 299 | |
| 300 | //===----------------------------------------------------------------------===// |
| Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 301 | // SourceManager initialization. |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | //===----------------------------------------------------------------------===// |
| 303 | |
| Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 304 | static bool InitializeSourceManager(Preprocessor &PP, |
| 305 | const std::string &InFile) { |
| 306 | // Figure out where to get and map in the main file. |
| 307 | SourceManager &SourceMgr = PP.getSourceManager(); |
| 308 | FileManager &FileMgr = PP.getFileManager(); |
| Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 309 | |
| 310 | if (EmptyInputOnly) { |
| 311 | const char *EmptyStr = ""; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 312 | llvm::MemoryBuffer *SB = |
| Daniel Dunbar | 57cbfc0 | 2009-04-27 21:19:07 +0000 | [diff] [blame] | 313 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<empty input>"); |
| 314 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 315 | } else if (InFile != "-") { |
| Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 316 | const FileEntry *File = FileMgr.getFile(InFile); |
| 317 | if (File) SourceMgr.createMainFileID(File, SourceLocation()); |
| 318 | if (SourceMgr.getMainFileID().isInvalid()) { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 319 | PP.getDiagnostics().Report(diag::err_fe_error_reading) << InFile.c_str(); |
| Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 320 | return true; |
| 321 | } |
| 322 | } else { |
| 323 | llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); |
| Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 324 | SourceMgr.createMainFileIDForMemBuffer(SB); |
| 325 | if (SourceMgr.getMainFileID().isInvalid()) { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 326 | PP.getDiagnostics().Report(diag::err_fe_error_reading_stdin); |
| Douglas Gregor | e1d918e | 2009-04-10 23:10:45 +0000 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return false; |
| 332 | } |
| 333 | |
| Chris Lattner | aa39197 | 2008-04-19 23:09:31 +0000 | [diff] [blame] | 334 | |
| Chris Lattner | e116ccf | 2009-04-21 05:40:52 +0000 | [diff] [blame] | 335 | //===----------------------------------------------------------------------===// |
| 336 | // Preprocessor Initialization |
| 337 | //===----------------------------------------------------------------------===// |
| Sam Bishop | 1102d6b | 2008-04-14 14:41:57 +0000 | [diff] [blame] | 338 | |
| Chris Lattner | e6113de | 2009-11-03 19:50:27 +0000 | [diff] [blame] | 339 | static llvm::cl::opt<bool> |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 340 | RelocatablePCH("relocatable-pch", |
| Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 341 | llvm::cl::desc("Whether to build a relocatable precompiled " |
| 342 | "header")); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 343 | |
| Daniel Dunbar | 11e729d | 2009-11-12 07:28:21 +0000 | [diff] [blame] | 344 | //===----------------------------------------------------------------------===// |
| 345 | // Preprocessor construction |
| 346 | //===----------------------------------------------------------------------===// |
| Chris Lattner | 5f9eae5 | 2008-03-01 08:07:28 +0000 | [diff] [blame] | 347 | |
| Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 348 | std::string GetBuiltinIncludePath(const char *Argv0) { |
| 349 | llvm::sys::Path P = |
| 350 | llvm::sys::Path::GetMainExecutable(Argv0, |
| 351 | (void*)(intptr_t) GetBuiltinIncludePath); |
| Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 352 | |
| Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 353 | if (!P.isEmpty()) { |
| 354 | P.eraseComponent(); // Remove /clang from foo/bin/clang |
| 355 | P.eraseComponent(); // Remove /bin from foo/bin |
| Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 356 | |
| Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 357 | // Get foo/lib/clang/<version>/include |
| 358 | P.appendComponent("lib"); |
| 359 | P.appendComponent("clang"); |
| 360 | P.appendComponent(CLANG_VERSION_STRING); |
| 361 | P.appendComponent("include"); |
| 362 | } |
| Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 363 | |
| Daniel Dunbar | 750156a | 2009-11-07 04:19:57 +0000 | [diff] [blame] | 364 | return P.str(); |
| Rafael Espindola | 1bb15a9 | 2009-10-05 13:12:17 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 367 | static Preprocessor * |
| Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 368 | CreatePreprocessor(Diagnostic &Diags, const LangOptions &LangInfo, |
| Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 369 | const PreprocessorOptions &PPOpts, |
| Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 370 | const HeaderSearchOptions &HSOpts, |
| Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 371 | const DependencyOutputOptions &DepOpts, |
| 372 | TargetInfo &Target, SourceManager &SourceMgr, |
| Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 373 | FileManager &FileMgr) { |
| Daniel Dunbar | b3cb98e | 2009-11-12 02:53:59 +0000 | [diff] [blame] | 374 | // Create a PTH manager if we are using some form of a token cache. |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 375 | PTHManager *PTHMgr = 0; |
| Daniel Dunbar | b3cb98e | 2009-11-12 02:53:59 +0000 | [diff] [blame] | 376 | if (!PPOpts.getTokenCache().empty()) |
| 377 | PTHMgr = PTHManager::Create(PPOpts.getTokenCache(), Diags); |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 378 | |
| Daniel Dunbar | b3cb98e | 2009-11-12 02:53:59 +0000 | [diff] [blame] | 379 | // FIXME: Don't fail like this. |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 380 | if (Diags.hasErrorOccurred()) |
| 381 | exit(1); |
| 382 | |
| 383 | // Create the Preprocessor. |
| Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 384 | HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr); |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 385 | Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, |
| Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 386 | SourceMgr, *HeaderInfo, PTHMgr, |
| 387 | /*OwnsHeaderSearch=*/true); |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 388 | |
| 389 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 390 | // That argument is used as the IdentifierInfoLookup argument to |
| 391 | // IdentifierTable's ctor. |
| 392 | if (PTHMgr) { |
| 393 | PTHMgr->setPreprocessor(PP); |
| 394 | PP->setPTHManager(PTHMgr); |
| 395 | } |
| 396 | |
| Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 397 | InitializePreprocessor(*PP, PPOpts, HSOpts); |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 398 | |
| Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 399 | // Handle generating dependencies, if requested. |
| 400 | if (!DepOpts.OutputFile.empty()) |
| 401 | AttachDependencyFileGen(*PP, DepOpts); |
| 402 | |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 403 | return PP; |
| Ted Kremenek | a42cf2e | 2008-04-17 21:38:34 +0000 | [diff] [blame] | 404 | } |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | //===----------------------------------------------------------------------===// |
| 407 | // Basic Parser driver |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | |
| Chris Lattner | 51574ea | 2008-04-19 23:25:44 +0000 | [diff] [blame] | 410 | static void ParseFile(Preprocessor &PP, MinimalAction *PA) { |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | Parser P(PP, *PA); |
| Ted Kremenek | 95041a2 | 2007-12-19 22:51:13 +0000 | [diff] [blame] | 412 | PP.EnterMainSourceFile(); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 413 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 414 | // Parsing the specified input file. |
| 415 | P.ParseTranslationUnit(); |
| 416 | delete PA; |
| 417 | } |
| 418 | |
| 419 | //===----------------------------------------------------------------------===// |
| Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 420 | // Fix-It Options |
| 421 | //===----------------------------------------------------------------------===// |
| Daniel Dunbar | bfbeadb | 2009-11-12 06:48:24 +0000 | [diff] [blame] | 422 | |
| 423 | static llvm::cl::opt<bool> |
| 424 | FixItAll("fixit", llvm::cl::desc("Apply fix-it advice to the input source")); |
| 425 | |
| Douglas Gregor | 26df2f0 | 2009-04-02 19:05:20 +0000 | [diff] [blame] | 426 | static llvm::cl::list<ParsedSourceLocation> |
| 427 | FixItAtLocations("fixit-at", llvm::cl::value_desc("source-location"), |
| 428 | llvm::cl::desc("Perform Fix-It modifications at the given source location")); |
| 429 | |
| 430 | //===----------------------------------------------------------------------===// |
| Daniel Dunbar | 11e729d | 2009-11-12 07:28:21 +0000 | [diff] [blame] | 431 | // Dump Build Information |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 432 | //===----------------------------------------------------------------------===// |
| 433 | |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 434 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 435 | unsigned argc, char **argv, |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 436 | llvm::OwningPtr<DiagnosticClient> &DiagClient) { |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 437 | std::string ErrorInfo; |
| Daniel Dunbar | 11e729d | 2009-11-12 07:28:21 +0000 | [diff] [blame] | 438 | llvm::raw_ostream *OS = |
| 439 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo); |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 440 | if (!ErrorInfo.empty()) { |
| 441 | llvm::errs() << "error opening -dump-build-information file '" |
| Daniel Dunbar | 11e729d | 2009-11-12 07:28:21 +0000 | [diff] [blame] | 442 | << DiagOpts.DumpBuildInformation << "', option ignored!\n"; |
| Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 443 | delete OS; |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
| Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 447 | (*OS) << "clang-cc command line arguments: "; |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 448 | for (unsigned i = 0; i != argc; ++i) |
| Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 449 | (*OS) << argv[i] << ' '; |
| 450 | (*OS) << '\n'; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 451 | |
| Daniel Dunbar | dbf75fe | 2009-11-11 08:13:24 +0000 | [diff] [blame] | 452 | // Chain in a diagnostic client which will log the diagnostics. |
| Daniel Dunbar | aea3641 | 2009-11-11 09:38:24 +0000 | [diff] [blame] | 453 | DiagnosticClient *Logger = |
| 454 | new TextDiagnosticPrinter(*OS, DiagOpts, /*OwnsOutputStream=*/true); |
| Daniel Dunbar | dbf75fe | 2009-11-11 08:13:24 +0000 | [diff] [blame] | 455 | DiagClient.reset(new ChainedDiagnosticClient(DiagClient.take(), Logger)); |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 458 | //===----------------------------------------------------------------------===// |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 459 | // Main driver |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 462 | static llvm::raw_ostream *ComputeOutFile(const CompilerInvocation &CompOpts, |
| 463 | const std::string &InFile, |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 464 | const char *Extension, |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 465 | bool Binary, |
| 466 | llvm::sys::Path& OutPath) { |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 467 | llvm::raw_ostream *Ret; |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 468 | std::string OutFile; |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 469 | if (!CompOpts.getOutputFile().empty()) |
| 470 | OutFile = CompOpts.getOutputFile(); |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 471 | else if (InFile == "-") { |
| 472 | OutFile = "-"; |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 473 | } else if (Extension) { |
| 474 | llvm::sys::Path Path(InFile); |
| 475 | Path.eraseSuffix(); |
| 476 | Path.appendSuffix(Extension); |
| Chris Lattner | d57a7ef | 2009-08-23 22:45:33 +0000 | [diff] [blame] | 477 | OutFile = Path.str(); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 478 | } else { |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 479 | OutFile = "-"; |
| Chris Lattner | 8a5c809 | 2009-02-18 01:20:05 +0000 | [diff] [blame] | 480 | } |
| Seo Sanghyeon | fe947ad | 2007-12-24 01:52:34 +0000 | [diff] [blame] | 481 | |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 482 | std::string Error; |
| 483 | Ret = new llvm::raw_fd_ostream(OutFile.c_str(), Error, |
| Dan Gohman | b044c47 | 2009-08-25 15:36:09 +0000 | [diff] [blame] | 484 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0)); |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 485 | if (!Error.empty()) { |
| 486 | // FIXME: Don't fail this way. |
| 487 | llvm::errs() << "ERROR: " << Error << "\n"; |
| 488 | ::exit(1); |
| Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 489 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 491 | if (OutFile != "-") |
| 492 | OutPath = OutFile; |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 493 | |
| 494 | return Ret; |
| Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 497 | /// AddFixItLocations - Add any individual user specified "fix-it" locations, |
| Daniel Dunbar | bd9f04b | 2009-11-11 09:38:35 +0000 | [diff] [blame] | 498 | /// and return true on success (if any were added). |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 499 | static bool AddFixItLocations(FixItRewriter *FixItRewrite, |
| Daniel Dunbar | bd9f04b | 2009-11-11 09:38:35 +0000 | [diff] [blame] | 500 | FileManager &FileMgr) { |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 501 | bool AddedFixItLocation = false; |
| Daniel Dunbar | bd9f04b | 2009-11-11 09:38:35 +0000 | [diff] [blame] | 502 | |
| 503 | for (unsigned i = 0, e = FixItAtLocations.size(); i != e; ++i) { |
| 504 | if (const FileEntry *File = FileMgr.getFile(FixItAtLocations[i].FileName)) { |
| 505 | RequestedSourceLocation Requested; |
| 506 | Requested.File = File; |
| 507 | Requested.Line = FixItAtLocations[i].Line; |
| 508 | Requested.Column = FixItAtLocations[i].Column; |
| 509 | FixItRewrite->addFixItLocation(Requested); |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 510 | AddedFixItLocation = true; |
| Daniel Dunbar | bd9f04b | 2009-11-11 09:38:35 +0000 | [diff] [blame] | 511 | } else { |
| 512 | llvm::errs() << "FIX-IT could not find file \"" |
| 513 | << FixItAtLocations[i].FileName << "\"\n"; |
| 514 | } |
| 515 | } |
| 516 | |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 517 | return AddedFixItLocation; |
| Daniel Dunbar | bd9f04b | 2009-11-11 09:38:35 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 520 | static ASTConsumer *CreateConsumerAction(const CompilerInvocation &CompOpts, |
| 521 | Preprocessor &PP, |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 522 | const std::string &InFile, |
| 523 | ProgActions PA, |
| 524 | llvm::OwningPtr<llvm::raw_ostream> &OS, |
| 525 | llvm::sys::Path &OutPath, |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 526 | llvm::LLVMContext& Context) { |
| Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 527 | switch (PA) { |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 528 | default: |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 529 | return 0; |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 530 | |
| 531 | case ASTPrint: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 532 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, false, OutPath)); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 533 | return CreateASTPrinter(OS.get()); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 534 | |
| Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 535 | case ASTPrintXML: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 536 | OS.reset(ComputeOutFile(CompOpts, InFile, "xml", false, OutPath)); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 537 | return CreateASTPrinterXML(OS.get()); |
| Douglas Gregor | ee75c05 | 2009-05-21 20:55:50 +0000 | [diff] [blame] | 538 | |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 539 | case ASTDump: |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 540 | return CreateASTDumper(); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 541 | |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 542 | case ASTView: |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 543 | return CreateASTViewer(); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 544 | |
| Anders Carlsson | 78762eb | 2009-09-24 18:54:49 +0000 | [diff] [blame] | 545 | case DumpRecordLayouts: |
| 546 | return CreateRecordLayoutDumper(); |
| 547 | |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 548 | case InheritanceView: |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 549 | return CreateInheritanceViewer(InheritanceViewCls); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 550 | |
| 551 | case EmitAssembly: |
| 552 | case EmitLLVM: |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | case EmitBC: |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 554 | case EmitLLVMOnly: { |
| 555 | BackendAction Act; |
| 556 | if (ProgAction == EmitAssembly) { |
| 557 | Act = Backend_EmitAssembly; |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 558 | OS.reset(ComputeOutFile(CompOpts, InFile, "s", true, OutPath)); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 559 | } else if (ProgAction == EmitLLVM) { |
| 560 | Act = Backend_EmitLL; |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 561 | OS.reset(ComputeOutFile(CompOpts, InFile, "ll", true, OutPath)); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 562 | } else if (ProgAction == EmitLLVMOnly) { |
| 563 | Act = Backend_EmitNothing; |
| 564 | } else { |
| 565 | Act = Backend_EmitBC; |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 566 | OS.reset(ComputeOutFile(CompOpts, InFile, "bc", true, OutPath)); |
| Ted Kremenek | db094a2 | 2007-12-05 18:27:04 +0000 | [diff] [blame] | 567 | } |
| Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 568 | |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 569 | return CreateBackendConsumer(Act, PP.getDiagnostics(), PP.getLangOptions(), |
| Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 570 | CompOpts.getCompileOpts(), InFile, OS.get(), |
| 571 | Context); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 574 | case RewriteObjC: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 575 | OS.reset(ComputeOutFile(CompOpts, InFile, "cpp", true, OutPath)); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 576 | return CreateObjCRewriter(InFile, OS.get(), PP.getDiagnostics(), |
| Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 577 | PP.getLangOptions(), |
| 578 | CompOpts.getDiagnosticOpts().NoRewriteMacros); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 579 | |
| 580 | case RewriteBlocks: |
| 581 | return CreateBlockRewriter(InFile, PP.getDiagnostics(), |
| 582 | PP.getLangOptions()); |
| Daniel Dunbar | 5ee0aa7 | 2009-11-11 00:54:56 +0000 | [diff] [blame] | 583 | |
| 584 | case ParseSyntaxOnly: |
| 585 | return new ASTConsumer(); |
| 586 | |
| 587 | case PrintDeclContext: |
| 588 | return CreateDeclContextPrinter(); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
| Daniel Dunbar | 975790e | 2009-11-12 02:53:20 +0000 | [diff] [blame] | 592 | /// ReadPCHFile - Load a PCH file from disk, and initialize the preprocessor for |
| 593 | /// reading from the PCH file. |
| 594 | /// |
| 595 | /// \return The AST source, or null on failure. |
| 596 | static ExternalASTSource *ReadPCHFile(llvm::StringRef Path, |
| 597 | const CompilerInvocation &CompOpts, |
| 598 | Preprocessor &PP, |
| 599 | ASTContext &Context) { |
| 600 | // If the user specified -isysroot, it will be used for relocatable PCH files. |
| 601 | const char *isysrootPCH = CompOpts.getHeaderSearchOpts().Sysroot.c_str(); |
| 602 | if (isysrootPCH[0] == '\0') |
| 603 | isysrootPCH = 0; |
| 604 | |
| 605 | llvm::OwningPtr<PCHReader> Reader; |
| 606 | Reader.reset(new PCHReader(PP, &Context, isysrootPCH)); |
| 607 | |
| 608 | switch (Reader->ReadPCH(Path)) { |
| 609 | case PCHReader::Success: |
| 610 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 611 | // predefines buffer will be empty. |
| 612 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 613 | return Reader.take(); |
| 614 | |
| 615 | case PCHReader::Failure: |
| 616 | // Unrecoverable failure: don't even try to process the input file. |
| 617 | break; |
| 618 | |
| 619 | case PCHReader::IgnorePCH: |
| 620 | // No suitable PCH file could be found. Return an error. |
| 621 | break; |
| 622 | } |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 627 | /// ProcessInputFile - Process a single input file with the specified state. |
| 628 | /// |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 629 | static void ProcessInputFile(const CompilerInvocation &CompOpts, |
| 630 | Preprocessor &PP, const std::string &InFile, |
| Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 631 | ProgActions PA, llvm::LLVMContext& Context) { |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 632 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 633 | llvm::OwningPtr<ASTConsumer> Consumer; |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 634 | FixItRewriter *FixItRewrite = 0; |
| 635 | bool CompleteTranslationUnit = true; |
| 636 | llvm::sys::Path OutPath; |
| 637 | |
| 638 | switch (PA) { |
| 639 | default: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 640 | Consumer.reset(CreateConsumerAction(CompOpts, PP, InFile, PA, OS, OutPath, |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 641 | Context)); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 642 | if (!Consumer.get()) { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 643 | PP.getDiagnostics().Report(diag::err_fe_invalid_ast_action); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 644 | return; |
| 645 | } |
| Daniel Dunbar | dc8bbac | 2009-11-11 09:38:42 +0000 | [diff] [blame] | 646 | break; |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 647 | |
| 648 | case EmitHTML: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 649 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
| Daniel Dunbar | 90b1827 | 2009-11-04 23:56:25 +0000 | [diff] [blame] | 650 | Consumer.reset(CreateHTMLPrinter(OS.get(), PP)); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 651 | break; |
| 652 | |
| Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 653 | case RunAnalysis: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 654 | Consumer.reset(CreateAnalysisConsumer(PP, CompOpts.getOutputFile(), |
| Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 655 | CompOpts.getAnalyzerOpts())); |
| Daniel Dunbar | 0794d8d | 2009-09-17 00:48:00 +0000 | [diff] [blame] | 656 | break; |
| 657 | |
| Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 658 | case GeneratePCH: { |
| 659 | const std::string &Sysroot = CompOpts.getHeaderSearchOpts().Sysroot; |
| 660 | if (RelocatablePCH.getValue() && Sysroot.empty()) { |
| Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 661 | PP.Diag(SourceLocation(), diag::err_relocatable_without_without_isysroot); |
| 662 | RelocatablePCH.setValue(false); |
| 663 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 664 | |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 665 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
| Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 666 | if (RelocatablePCH.getValue()) |
| Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 667 | Consumer.reset(CreatePCHGenerator(PP, OS.get(), Sysroot.c_str())); |
| Douglas Gregor | e650c8c | 2009-07-07 00:12:59 +0000 | [diff] [blame] | 668 | else |
| 669 | Consumer.reset(CreatePCHGenerator(PP, OS.get())); |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 670 | CompleteTranslationUnit = false; |
| 671 | break; |
| Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 672 | } |
| Chris Lattner | c106c10 | 2008-10-12 05:03:36 +0000 | [diff] [blame] | 673 | |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 674 | // Do any necessary set up for non-consumer actions. |
| 675 | case DumpRawTokens: |
| 676 | case DumpTokens: |
| Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 677 | case RunPreprocessorOnly: |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 678 | case ParseNoop: |
| 679 | break; // No setup. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 681 | case GeneratePTH: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 682 | if (CompOpts.getOutputFile().empty() || CompOpts.getOutputFile() == "-") { |
| Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 683 | // FIXME: Don't fail this way. |
| 684 | // FIXME: Verify that we can actually seek in the given file. |
| Chris Lattner | 92bcc27 | 2009-08-23 02:59:41 +0000 | [diff] [blame] | 685 | llvm::errs() << "ERROR: PTH requires an seekable file for output!\n"; |
| Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 686 | ::exit(1); |
| 687 | } |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 688 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
| Ted Kremenek | 8588896 | 2008-10-21 00:54:44 +0000 | [diff] [blame] | 689 | break; |
| Douglas Gregor | 6ab3524 | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 690 | |
| Chris Lattner | cc7dea8 | 2009-04-27 22:02:30 +0000 | [diff] [blame] | 691 | case PrintPreprocessedInput: |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 692 | case ParsePrintCallbacks: |
| Chris Lattner | b57e3d4 | 2008-05-08 06:52:13 +0000 | [diff] [blame] | 693 | case RewriteMacros: |
| Eli Friedman | f54fce8 | 2009-05-19 01:02:07 +0000 | [diff] [blame] | 694 | case RewriteTest: |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 695 | OS.reset(ComputeOutFile(CompOpts, InFile, 0, true, OutPath)); |
| Chris Lattner | b13c5ee | 2008-10-12 05:29:20 +0000 | [diff] [blame] | 696 | break; |
| Daniel Dunbar | 387ecbd | 2009-11-11 10:22:48 +0000 | [diff] [blame] | 697 | } |
| Douglas Gregor | 558cb56 | 2009-04-02 01:08:08 +0000 | [diff] [blame] | 698 | |
| Daniel Dunbar | 387ecbd | 2009-11-11 10:22:48 +0000 | [diff] [blame] | 699 | // Check if we want a fix-it rewriter. |
| Daniel Dunbar | bfbeadb | 2009-11-12 06:48:24 +0000 | [diff] [blame] | 700 | if (FixItAll || !FixItAtLocations.empty()) { |
| Douglas Gregor | de4bf6a | 2009-04-02 17:13:00 +0000 | [diff] [blame] | 701 | FixItRewrite = new FixItRewriter(PP.getDiagnostics(), |
| Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 702 | PP.getSourceManager(), |
| 703 | PP.getLangOptions()); |
| Daniel Dunbar | 387ecbd | 2009-11-11 10:22:48 +0000 | [diff] [blame] | 704 | if (!FixItAtLocations.empty() && |
| 705 | !AddFixItLocations(FixItRewrite, PP.getFileManager())) { |
| Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 706 | // All of the fix-it locations were bad. Don't fix anything. |
| 707 | delete FixItRewrite; |
| 708 | FixItRewrite = 0; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | llvm::OwningPtr<ASTContext> ContextOwner; |
| Daniel Dunbar | 6606864 | 2009-11-12 02:53:13 +0000 | [diff] [blame] | 713 | llvm::OwningPtr<ExternalASTSource> Source; |
| 714 | const std::string &ImplicitPCHInclude = |
| 715 | CompOpts.getPreprocessorOpts().getImplicitPCHInclude(); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 716 | if (Consumer) { |
| Chris Lattner | 9ecd26a | 2009-03-28 01:37:17 +0000 | [diff] [blame] | 717 | ContextOwner.reset(new ASTContext(PP.getLangOptions(), |
| 718 | PP.getSourceManager(), |
| 719 | PP.getTargetInfo(), |
| 720 | PP.getIdentifierTable(), |
| 721 | PP.getSelectorTable(), |
| Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 722 | PP.getBuiltinInfo(), |
| Douglas Gregor | 2deaea3 | 2009-04-22 18:49:13 +0000 | [diff] [blame] | 723 | /* FreeMemory = */ !DisableFree, |
| Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 724 | /* size_reserve = */0)); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 725 | |
| 726 | if (!ImplicitPCHInclude.empty()) { |
| 727 | Source.reset(ReadPCHFile(ImplicitPCHInclude, CompOpts, PP, |
| 728 | *ContextOwner)); |
| 729 | if (!Source) |
| 730 | return; |
| 731 | |
| 732 | // Attach the PCH reader to the AST context as an external AST source, so |
| 733 | // that declarations will be deserialized from the PCH file as needed. |
| 734 | ContextOwner->setExternalSource(Source); |
| 735 | } else { |
| 736 | // Initialize builtin info when not using PCH. |
| 737 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), |
| 738 | PP.getLangOptions().NoBuiltin); |
| 739 | } |
| 740 | |
| 741 | // Initialize the main file entry. This needs to be delayed until after PCH |
| 742 | // has loaded. |
| 743 | if (InitializeSourceManager(PP, InFile)) |
| Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 744 | return; |
| 745 | |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 746 | CodeCompleteConsumer *(*CreateCodeCompleter)(Sema &, void *) = 0; |
| 747 | void *CreateCodeCompleterData = 0; |
| Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 748 | |
| Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 749 | if (!CodeCompletionAt.FileName.empty()) { |
| 750 | // Tell the source manager to chop off the given file at a specific |
| 751 | // line and column. |
| Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 752 | if (const FileEntry *Entry |
| Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 753 | = PP.getFileManager().getFile(CodeCompletionAt.FileName)) { |
| 754 | // Truncate the named file at the given line/column. |
| 755 | PP.getSourceManager().truncateFileAt(Entry, CodeCompletionAt.Line, |
| 756 | CodeCompletionAt.Column); |
| Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 757 | |
| Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 758 | // Set up the creation routine for code-completion. |
| 759 | CreateCodeCompleter = BuildPrintingCodeCompleter; |
| 760 | } else { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 761 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| Douglas Gregor | b657f11 | 2009-09-22 21:11:38 +0000 | [diff] [blame] | 762 | << CodeCompletionAt.FileName; |
| 763 | } |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 766 | // Run the AST consumer action. |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 767 | ParseAST(PP, Consumer.get(), *ContextOwner.get(), Stats, |
| Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 768 | CompleteTranslationUnit, |
| 769 | CreateCodeCompleter, CreateCodeCompleterData); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 770 | } else { |
| 771 | // Initialize builtin info. |
| 772 | PP.getBuiltinInfo().InitializeBuiltins(PP.getIdentifierTable(), |
| 773 | PP.getLangOptions().NoBuiltin); |
| Chris Lattner | d1d64a0 | 2009-04-27 21:45:14 +0000 | [diff] [blame] | 774 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 775 | // Initialize the main file entry. This needs to be delayed until after PCH |
| 776 | // has loaded. |
| 777 | if (InitializeSourceManager(PP, InFile)) |
| 778 | return; |
| Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 779 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 780 | // Run the preprocessor actions. |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 781 | llvm::TimeRegion Timer(ClangFrontendTimer); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 782 | switch (PA) { |
| 783 | default: |
| 784 | assert(0 && "unexpected program action"); |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 785 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 786 | case DumpRawTokens: { |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 787 | SourceManager &SM = PP.getSourceManager(); |
| 788 | // Start lexing the specified input file. |
| 789 | Lexer RawLex(SM.getMainFileID(), SM, PP.getLangOptions()); |
| 790 | RawLex.SetKeepWhitespaceMode(true); |
| 791 | |
| 792 | Token RawTok; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 793 | RawLex.LexFromRawLexer(RawTok); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 794 | while (RawTok.isNot(tok::eof)) { |
| 795 | PP.DumpToken(RawTok, true); |
| 796 | fprintf(stderr, "\n"); |
| 797 | RawLex.LexFromRawLexer(RawTok); |
| 798 | } |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 799 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 800 | } |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 801 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 802 | case DumpTokens: { |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 803 | Token Tok; |
| 804 | // Start preprocessing the specified input file. |
| 805 | PP.EnterMainSourceFile(); |
| 806 | do { |
| 807 | PP.Lex(Tok); |
| 808 | PP.DumpToken(Tok, true); |
| 809 | fprintf(stderr, "\n"); |
| 810 | } while (Tok.isNot(tok::eof)); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 811 | break; |
| 812 | } |
| Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 813 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 814 | case GeneratePTH: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 815 | CacheTokens(PP, static_cast<llvm::raw_fd_ostream*>(OS.get())); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 816 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 817 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 818 | case ParseNoop: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 819 | ParseFile(PP, new MinimalAction(PP)); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 820 | break; |
| Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 821 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 822 | case ParsePrintCallbacks: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 823 | ParseFile(PP, CreatePrintParserActionsAction(PP, OS.get())); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 824 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 825 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 826 | case PrintPreprocessedInput: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 827 | DoPrintPreprocessedInput(PP, OS.get(), |
| 828 | CompOpts.getPreprocessorOutputOpts()); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 829 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 830 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 831 | case RewriteMacros: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 832 | RewriteMacrosInInput(PP, OS.get()); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 833 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 834 | |
| Daniel Dunbar | c67a5f9 | 2009-11-12 02:53:34 +0000 | [diff] [blame] | 835 | case RewriteTest: |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 836 | DoRewriteTest(PP, OS.get()); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 837 | break; |
| Daniel Dunbar | 8e1bcb0 | 2009-11-12 01:36:27 +0000 | [diff] [blame] | 838 | |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 839 | case RunPreprocessorOnly: { // Just lex as fast as we can, no output. |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 840 | Token Tok; |
| 841 | // Start parsing the specified input file. |
| 842 | PP.EnterMainSourceFile(); |
| 843 | do { |
| 844 | PP.Lex(Tok); |
| 845 | } while (Tok.isNot(tok::eof)); |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 846 | break; |
| 847 | } |
| Daniel Dunbar | d1e7a96 | 2009-11-12 02:53:27 +0000 | [diff] [blame] | 848 | } |
| Daniel Dunbar | 593c41f | 2009-11-04 23:41:40 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 851 | if (FixItRewrite) |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 852 | FixItRewrite->WriteFixedFile(InFile, CompOpts.getOutputFile()); |
| Daniel Dunbar | 70186ab | 2009-07-29 02:40:09 +0000 | [diff] [blame] | 853 | |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 854 | // Release the consumer and the AST, in that order since the consumer may |
| 855 | // perform actions in its destructor which require the context. |
| 856 | if (DisableFree) { |
| Daniel Dunbar | 70186ab | 2009-07-29 02:40:09 +0000 | [diff] [blame] | 857 | Consumer.take(); |
| Chris Lattner | 1aee61a | 2009-04-27 21:25:27 +0000 | [diff] [blame] | 858 | ContextOwner.take(); |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 859 | } else { |
| 860 | Consumer.reset(); |
| 861 | ContextOwner.reset(); |
| 862 | } |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 863 | |
| Daniel Dunbar | 879c3ea | 2008-10-27 22:03:52 +0000 | [diff] [blame] | 864 | if (VerifyDiagnostics) |
| Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 865 | if (CheckDiagnostics(PP)) |
| 866 | exit(1); |
| Chris Lattner | e66b65c | 2008-02-06 01:42:25 +0000 | [diff] [blame] | 867 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 868 | if (Stats) { |
| Ted Kremenek | fdfc198 | 2007-12-19 22:24:34 +0000 | [diff] [blame] | 869 | fprintf(stderr, "\nSTATISTICS FOR '%s':\n", InFile.c_str()); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | PP.PrintStats(); |
| 871 | PP.getIdentifierTable().PrintStats(); |
| Chris Lattner | dee7359 | 2007-12-15 20:48:40 +0000 | [diff] [blame] | 872 | PP.getHeaderSearchInfo().PrintStats(); |
| Ted Kremenek | 1b95a65 | 2009-01-09 18:20:21 +0000 | [diff] [blame] | 873 | PP.getSourceManager().PrintStats(); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 874 | fprintf(stderr, "\n"); |
| 875 | } |
| Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 876 | |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 877 | // Always delete the output stream because we don't want to leak file |
| 878 | // handles. Also, we don't want to try to erase an open file. |
| 879 | OS.reset(); |
| 880 | |
| Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 881 | // If we had errors, try to erase the output file. |
| 882 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
| Eli Friedman | 66d6f04 | 2009-05-18 22:20:00 +0000 | [diff] [blame] | 883 | OutPath.eraseFromDisk(); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 886 | /// ProcessInputFile - Process a single AST input file with the specified state. |
| 887 | /// |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 888 | static void ProcessASTInputFile(const CompilerInvocation &CompOpts, |
| 889 | const std::string &InFile, ProgActions PA, |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 890 | Diagnostic &Diags, FileManager &FileMgr, |
| 891 | llvm::LLVMContext& Context) { |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 892 | std::string Error; |
| Steve Naroff | 36c4464 | 2009-10-19 14:34:22 +0000 | [diff] [blame] | 893 | llvm::OwningPtr<ASTUnit> AST(ASTUnit::LoadFromPCHFile(InFile, &Error)); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 894 | if (!AST) { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 895 | Diags.Report(diag::err_fe_invalid_ast_file) << Error; |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 896 | return; |
| 897 | } |
| 898 | |
| 899 | Preprocessor &PP = AST->getPreprocessor(); |
| 900 | |
| 901 | llvm::OwningPtr<llvm::raw_ostream> OS; |
| 902 | llvm::sys::Path OutPath; |
| Daniel Dunbar | c8daaa4 | 2009-11-12 00:24:28 +0000 | [diff] [blame] | 903 | llvm::OwningPtr<ASTConsumer> Consumer(CreateConsumerAction(CompOpts, PP, |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 904 | InFile, PA, OS, |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 905 | OutPath, Context)); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 906 | if (!Consumer.get()) { |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 907 | Diags.Report(diag::err_fe_invalid_ast_action); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 908 | return; |
| 909 | } |
| 910 | |
| Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 911 | // Set the main file ID to an empty file. |
| 912 | // |
| 913 | // FIXME: We probably shouldn't need this, but for now this is the simplest |
| 914 | // way to reuse the logic in ParseAST. |
| 915 | const char *EmptyStr = ""; |
| 916 | llvm::MemoryBuffer *SB = |
| 917 | llvm::MemoryBuffer::getMemBuffer(EmptyStr, EmptyStr, "<dummy input>"); |
| 918 | AST->getSourceManager().createMainFileIDForMemBuffer(SB); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 919 | |
| Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 920 | // Stream the input AST to the consumer. |
| Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 921 | Diags.getClient()->BeginSourceFile(PP.getLangOptions()); |
| Daniel Dunbar | a674bf4 | 2009-09-21 03:03:56 +0000 | [diff] [blame] | 922 | ParseAST(PP, Consumer.get(), AST->getASTContext(), Stats); |
| Daniel Dunbar | efcbe94 | 2009-11-05 02:42:12 +0000 | [diff] [blame] | 923 | Diags.getClient()->EndSourceFile(); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 924 | |
| 925 | // Release the consumer and the AST, in that order since the consumer may |
| 926 | // perform actions in its destructor which require the context. |
| 927 | if (DisableFree) { |
| 928 | Consumer.take(); |
| 929 | AST.take(); |
| 930 | } else { |
| 931 | Consumer.reset(); |
| 932 | AST.reset(); |
| 933 | } |
| 934 | |
| 935 | // Always delete the output stream because we don't want to leak file |
| 936 | // handles. Also, we don't want to try to erase an open file. |
| 937 | OS.reset(); |
| 938 | |
| Daniel Dunbar | 8cb6562 | 2009-10-29 21:05:18 +0000 | [diff] [blame] | 939 | // If we had errors, try to erase the output file. |
| 940 | if (PP.getDiagnostics().getNumErrors() && !OutPath.isEmpty()) |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 941 | OutPath.eraseFromDisk(); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 944 | static void LLVMErrorHandler(void *UserData, const std::string &Message) { |
| 945 | Diagnostic &Diags = *static_cast<Diagnostic*>(UserData); |
| 946 | |
| Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 947 | Diags.Report(diag::err_fe_error_backend) << Message; |
| Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 948 | |
| 949 | // We cannot recover from llvm errors. |
| 950 | exit(1); |
| 951 | } |
| 952 | |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 953 | static LangKind GetLanguage() { |
| 954 | // If -x was given, that's the language. |
| 955 | if (BaseLang != langkind_unspecified) |
| 956 | return BaseLang; |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 957 | |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 958 | // Otherwise guess it from the input filenames; |
| 959 | LangKind LK = langkind_unspecified; |
| 960 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| 961 | llvm::StringRef Name(InputFilenames[i]); |
| 962 | LangKind ThisKind = llvm::StringSwitch<LangKind>(Name.rsplit('.').second) |
| 963 | .Case("ast", langkind_ast) |
| 964 | .Case("c", langkind_c) |
| 965 | .Cases("S", "s", langkind_asm_cpp) |
| 966 | .Case("i", langkind_c_cpp) |
| 967 | .Case("ii", langkind_cxx_cpp) |
| 968 | .Case("m", langkind_objc) |
| 969 | .Case("mi", langkind_objc_cpp) |
| 970 | .Cases("mm", "M", langkind_objcxx) |
| 971 | .Case("mii", langkind_objcxx_cpp) |
| 972 | .Case("C", langkind_cxx) |
| 973 | .Cases("C", "cc", "cp", langkind_cxx) |
| 974 | .Cases("cpp", "CPP", "c++", "cxx", langkind_cxx) |
| 975 | .Case("cl", langkind_ocl) |
| 976 | .Default(langkind_c); |
| 977 | |
| 978 | if (LK != langkind_unspecified && ThisKind != LK) { |
| 979 | llvm::errs() << "error: cannot have multiple input files of distinct " |
| 980 | << "language kinds without -x\n"; |
| 981 | exit(1); |
| 982 | } |
| 983 | |
| 984 | LK = ThisKind; |
| 985 | } |
| 986 | |
| 987 | return LK; |
| 988 | } |
| 989 | |
| Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 990 | static void FinalizeCompileOptions(CompileOptions &Opts, |
| 991 | const LangOptions &Lang) { |
| 992 | if (Lang.NoBuiltin) |
| 993 | Opts.SimplifyLibCalls = 0; |
| 994 | if (Lang.CPlusPlus) |
| 995 | Opts.NoCommon = 1; |
| 996 | |
| 997 | // Handle -ftime-report. |
| 998 | Opts.TimePasses = TimeReport; |
| 999 | } |
| 1000 | |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1001 | static void ConstructCompilerInvocation(CompilerInvocation &Opts, |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1002 | const char *Argv0, |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1003 | const DiagnosticOptions &DiagOpts, |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1004 | TargetInfo &Target, |
| 1005 | LangKind LK) { |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1006 | Opts.getDiagnosticOpts() = DiagOpts; |
| 1007 | |
| 1008 | Opts.getOutputFile() = OutputFile; |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1009 | |
| Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 1010 | // Initialize backend options, which may also be used to key some language |
| 1011 | // options. |
| Daniel Dunbar | 29a790b | 2009-11-11 09:38:56 +0000 | [diff] [blame] | 1012 | InitializeCompileOptions(Opts.getCompileOpts(), Target); |
| Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 1013 | |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1014 | // Initialize language options. |
| Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 1015 | // |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1016 | // FIXME: These aren't used during operations on ASTs. Split onto a separate |
| 1017 | // code path to make this obvious. |
| Daniel Dunbar | 5674908 | 2009-11-11 07:26:12 +0000 | [diff] [blame] | 1018 | if (LK != langkind_ast) |
| 1019 | InitializeLangOptions(Opts.getLangOpts(), LK, Target, |
| Daniel Dunbar | 29a790b | 2009-11-11 09:38:56 +0000 | [diff] [blame] | 1020 | Opts.getCompileOpts()); |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1021 | |
| Daniel Dunbar | 5746f1f | 2009-11-12 00:24:10 +0000 | [diff] [blame] | 1022 | // Initialize the static analyzer options. |
| 1023 | InitializeAnalyzerOptions(Opts.getAnalyzerOpts()); |
| 1024 | |
| Daniel Dunbar | 0e0bae8 | 2009-11-11 21:43:12 +0000 | [diff] [blame] | 1025 | // Initialize the dependency output options (-M...). |
| 1026 | InitializeDependencyOutputOptions(Opts.getDependencyOutputOpts()); |
| 1027 | |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1028 | // Initialize the header search options. |
| Daniel Dunbar | f797329 | 2009-11-11 08:13:32 +0000 | [diff] [blame] | 1029 | InitializeHeaderSearchOptions(Opts.getHeaderSearchOpts(), |
| 1030 | GetBuiltinIncludePath(Argv0), |
| 1031 | Verbose, |
| 1032 | Opts.getLangOpts()); |
| Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 1033 | |
| 1034 | // Initialize the other preprocessor options. |
| 1035 | InitializePreprocessorOptions(Opts.getPreprocessorOpts()); |
| Daniel Dunbar | 36f4ec3 | 2009-11-10 16:19:45 +0000 | [diff] [blame] | 1036 | |
| Daniel Dunbar | 29cf746 | 2009-11-11 10:07:44 +0000 | [diff] [blame] | 1037 | // Initialize the preprocessed output options. |
| 1038 | InitializePreprocessorOutputOptions(Opts.getPreprocessorOutputOpts()); |
| 1039 | |
| Daniel Dunbar | 0498cfc | 2009-11-10 19:51:53 +0000 | [diff] [blame] | 1040 | // Finalize some code generation options. |
| Daniel Dunbar | fcb0c3b | 2009-11-10 18:47:35 +0000 | [diff] [blame] | 1041 | FinalizeCompileOptions(Opts.getCompileOpts(), Opts.getLangOpts()); |
| Daniel Dunbar | e29709f | 2009-11-09 20:55:08 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1044 | static Diagnostic *CreateDiagnosticEngine(const DiagnosticOptions &Opts, |
| 1045 | int argc, char **argv) { |
| 1046 | // Create the diagnostic client for reporting errors or for |
| 1047 | // implementing -verify. |
| 1048 | llvm::OwningPtr<DiagnosticClient> DiagClient; |
| 1049 | if (VerifyDiagnostics) { |
| 1050 | // When checking diagnostics, just buffer them up. |
| 1051 | DiagClient.reset(new TextDiagnosticBuffer()); |
| 1052 | } else { |
| 1053 | DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
| 1054 | } |
| 1055 | |
| Daniel Dunbar | 11e729d | 2009-11-12 07:28:21 +0000 | [diff] [blame] | 1056 | if (!Opts.DumpBuildInformation.empty()) |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1057 | SetUpBuildDumpLog(Opts, argc, argv, DiagClient); |
| 1058 | |
| 1059 | // Configure our handling of diagnostics. |
| 1060 | Diagnostic *Diags = new Diagnostic(DiagClient.take()); |
| Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 1061 | if (ProcessWarningOptions(*Diags, Opts)) |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1062 | return 0; |
| 1063 | |
| 1064 | // Set an error handler, so that any LLVM backend diagnostics go through our |
| 1065 | // error handler. |
| 1066 | llvm::llvm_install_error_handler(LLVMErrorHandler, |
| 1067 | static_cast<void*>(Diags)); |
| 1068 | |
| 1069 | return Diags; |
| 1070 | } |
| 1071 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1072 | int main(int argc, char **argv) { |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | llvm::sys::PrintStackTraceOnErrorSignal(); |
| Chris Lattner | 09e94a3 | 2009-03-04 21:41:39 +0000 | [diff] [blame] | 1074 | llvm::PrettyStackTraceProgram X(argc, argv); |
| Owen Anderson | d720046 | 2009-07-16 00:14:12 +0000 | [diff] [blame] | 1075 | llvm::LLVMContext &Context = llvm::getGlobalContext(); |
| Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 1076 | |
| Daniel Dunbar | 4d86151 | 2009-09-03 04:54:12 +0000 | [diff] [blame] | 1077 | // Initialize targets first, so that --version shows registered targets. |
| Chris Lattner | 2fe1194 | 2009-06-17 17:25:50 +0000 | [diff] [blame] | 1078 | llvm::InitializeAllTargets(); |
| 1079 | llvm::InitializeAllAsmPrinters(); |
| Daniel Dunbar | d697081 | 2009-09-02 23:20:15 +0000 | [diff] [blame] | 1080 | |
| 1081 | llvm::cl::ParseCommandLineOptions(argc, argv, |
| 1082 | "LLVM 'Clang' Compiler: http://clang.llvm.org\n"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1083 | |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1084 | if (VerifyDiagnostics && InputFilenames.size() > 1) { |
| 1085 | fprintf(stderr, "-verify only works on single input files.\n"); |
| 1086 | return 1; |
| 1087 | } |
| 1088 | |
| Chris Lattner | 4709974 | 2009-02-18 01:51:21 +0000 | [diff] [blame] | 1089 | if (TimeReport) |
| 1090 | ClangFrontendTimer = new llvm::Timer("Clang front-end time"); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1091 | |
| Daniel Dunbar | 08e6dc6 | 2009-05-28 16:37:33 +0000 | [diff] [blame] | 1092 | if (Verbose) |
| Mike Stump | 3cbf5a0 | 2009-09-15 21:49:22 +0000 | [diff] [blame] | 1093 | llvm::errs() << "clang-cc version " CLANG_VERSION_STRING |
| 1094 | << " based upon " << PACKAGE_STRING |
| Daniel Dunbar | 2e30e59 | 2009-09-04 17:43:10 +0000 | [diff] [blame] | 1095 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1096 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1097 | // If no input was specified, read from stdin. |
| 1098 | if (InputFilenames.empty()) |
| 1099 | InputFilenames.push_back("-"); |
| Douglas Gregor | 68a0d78 | 2009-05-02 00:03:46 +0000 | [diff] [blame] | 1100 | |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1101 | // Construct the diagnostic engine first, so that we can build a diagnostic |
| 1102 | // client to use for any errors during option handling. |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1103 | DiagnosticOptions DiagOpts; |
| Daniel Dunbar | 0db4b76 | 2009-11-11 08:13:40 +0000 | [diff] [blame] | 1104 | InitializeDiagnosticOptions(DiagOpts); |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1105 | llvm::OwningPtr<Diagnostic> |
| 1106 | Diags(CreateDiagnosticEngine(DiagOpts, argc, argv)); |
| 1107 | if (!Diags) |
| Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 1108 | return 1; |
| Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1109 | |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1110 | // FIXME: Hack to make sure we release the diagnostic client, the engine |
| 1111 | // should (optionally?) take ownership of it. |
| 1112 | llvm::OwningPtr<DiagnosticClient> DiagClient(Diags->getClient()); |
| Daniel Dunbar | 70121eb | 2009-08-10 03:40:28 +0000 | [diff] [blame] | 1113 | |
| Daniel Dunbar | 7c15e71 | 2009-10-30 18:12:31 +0000 | [diff] [blame] | 1114 | // Initialize base triple. If a -triple option has been specified, use |
| 1115 | // that triple. Otherwise, default to the host triple. |
| 1116 | llvm::Triple Triple(TargetTriple); |
| 1117 | if (Triple.getTriple().empty()) |
| 1118 | Triple = llvm::Triple(llvm::sys::getHostTriple()); |
| 1119 | |
| Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1120 | // Get information about the target being compiled for. |
| Daniel Dunbar | bea5a84 | 2009-10-29 01:53:18 +0000 | [diff] [blame] | 1121 | llvm::OwningPtr<TargetInfo> |
| Chris Lattner | 2f60af7 | 2009-09-12 22:45:58 +0000 | [diff] [blame] | 1122 | Target(TargetInfo::CreateTargetInfo(Triple.getTriple())); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
| Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1124 | if (Target == 0) { |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1125 | Diags->Report(diag::err_fe_unknown_triple) << Triple.getTriple().c_str(); |
| Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 1126 | return 1; |
| Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1127 | } |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
| Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 1129 | // Set the target ABI if specified. |
| 1130 | if (!TargetABI.empty()) { |
| 1131 | if (!Target->setABI(TargetABI)) { |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1132 | Diags->Report(diag::err_fe_unknown_target_abi) << TargetABI; |
| Daniel Dunbar | 73b7959 | 2009-09-14 00:02:12 +0000 | [diff] [blame] | 1133 | return 1; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| Daniel Dunbar | d427023 | 2009-01-20 23:17:32 +0000 | [diff] [blame] | 1137 | if (!InheritanceViewCls.empty()) // C++ visualization? |
| Ted Kremenek | 7cae2f6 | 2008-10-23 23:36:29 +0000 | [diff] [blame] | 1138 | ProgAction = InheritanceView; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1140 | // Infer the input language. |
| 1141 | // |
| 1142 | // FIXME: We should move .ast inputs to taking a separate path, they are |
| 1143 | // really quite different. |
| 1144 | LangKind LK = GetLanguage(); |
| 1145 | |
| 1146 | // Now that we have initialized the diagnostics engine and the target, finish |
| 1147 | // setting up the compiler invocation. |
| 1148 | CompilerInvocation CompOpts; |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1149 | ConstructCompilerInvocation(CompOpts, argv[0], DiagOpts, *Target, LK); |
| Daniel Dunbar | 227b238 | 2009-11-09 22:45:57 +0000 | [diff] [blame] | 1150 | |
| Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 1151 | // Create the source manager. |
| 1152 | SourceManager SourceMgr; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1153 | |
| Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 1154 | // Create a file manager object to provide access to and cache the filesystem. |
| 1155 | FileManager FileMgr; |
| Douglas Gregor | 4fed3f4 | 2009-04-27 18:38:38 +0000 | [diff] [blame] | 1156 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1157 | for (unsigned i = 0, e = InputFilenames.size(); i != e; ++i) { |
| Ted Kremenek | 31e703b | 2007-12-11 23:28:38 +0000 | [diff] [blame] | 1158 | const std::string &InFile = InputFilenames[i]; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1159 | |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1160 | // AST inputs are handled specially. |
| 1161 | if (LK == langkind_ast) { |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1162 | ProcessASTInputFile(CompOpts, InFile, ProgAction, *Diags, FileMgr, |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1163 | Context); |
| Daniel Dunbar | aca2ebd | 2009-09-17 00:48:13 +0000 | [diff] [blame] | 1164 | continue; |
| 1165 | } |
| 1166 | |
| Daniel Dunbar | 4cc1a25 | 2009-11-05 01:53:23 +0000 | [diff] [blame] | 1167 | // Reset the ID tables if we are reusing the SourceManager. |
| 1168 | if (i) |
| 1169 | SourceMgr.clearIDTables(); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
| Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1171 | // Set up the preprocessor with these options. |
| Daniel Dunbar | 5fc7d34 | 2009-11-09 23:12:31 +0000 | [diff] [blame] | 1172 | llvm::OwningPtr<Preprocessor> |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1173 | PP(CreatePreprocessor(*Diags, CompOpts.getLangOpts(), |
| Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 1174 | CompOpts.getPreprocessorOpts(), |
| Daniel Dunbar | 961c76e | 2009-11-11 21:44:42 +0000 | [diff] [blame] | 1175 | CompOpts.getHeaderSearchOpts(), |
| Daniel Dunbar | ca11f61 | 2009-11-11 21:44:00 +0000 | [diff] [blame] | 1176 | CompOpts.getDependencyOutputOpts(), |
| Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 1177 | *Target, SourceMgr, FileMgr)); |
| 1178 | |
| Chris Lattner | f63aea3 | 2009-03-04 21:40:56 +0000 | [diff] [blame] | 1179 | // Process the source file. |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1180 | Diags->getClient()->BeginSourceFile(CompOpts.getLangOpts()); |
| Daniel Dunbar | 26a0cac | 2009-11-09 22:46:17 +0000 | [diff] [blame] | 1181 | ProcessInputFile(CompOpts, *PP, InFile, ProgAction, Context); |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1182 | Diags->getClient()->EndSourceFile(); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1183 | } |
| Chris Lattner | 1121519 | 2008-03-14 06:12:05 +0000 | [diff] [blame] | 1184 | |
| Daniel Dunbar | 9253e49 | 2009-11-10 00:46:12 +0000 | [diff] [blame] | 1185 | if (CompOpts.getDiagnosticOpts().ShowCarets) |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1186 | if (unsigned NumDiagnostics = Diags->getNumDiagnostics()) |
| Mike Stump | fc0fed3 | 2009-04-28 01:19:10 +0000 | [diff] [blame] | 1187 | fprintf(stderr, "%d diagnostic%s generated.\n", NumDiagnostics, |
| 1188 | (NumDiagnostics == 1 ? "" : "s")); |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1190 | if (Stats) { |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1191 | FileMgr.PrintStats(); |
| 1192 | fprintf(stderr, "\n"); |
| 1193 | } |
| Chris Lattner | 75a97cb | 2009-04-17 21:05:01 +0000 | [diff] [blame] | 1194 | |
| 1195 | delete ClangFrontendTimer; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1196 | |
| Daniel Dunbar | 276373d | 2008-10-27 22:10:13 +0000 | [diff] [blame] | 1197 | // If verifying diagnostics and we reached here, all is well. |
| 1198 | if (VerifyDiagnostics) |
| 1199 | return 0; |
| Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
| Daniel Dunbar | 524b86f | 2008-10-28 00:38:08 +0000 | [diff] [blame] | 1201 | // Managed static deconstruction. Useful for making things like |
| 1202 | // -time-passes usable. |
| 1203 | llvm::llvm_shutdown(); |
| 1204 | |
| Daniel Dunbar | 00e5b8d | 2009-11-12 06:48:31 +0000 | [diff] [blame] | 1205 | return (Diags->getNumErrors() != 0); |
| Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | } |