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