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