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