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