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