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