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