Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 1 | //===--- CompilerInstance.cpp ---------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/CompilerInstance.h" |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 11 | #include "clang/Sema/Sema.h" |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTConsumer.h" |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 14 | #include "clang/Basic/Diagnostic.h" |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 17 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 18 | #include "clang/Basic/Version.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 19 | #include "clang/Lex/HeaderSearch.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PTHManager.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/ChainedDiagnosticClient.h" |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/FrontendAction.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 24 | #include "clang/Frontend/FrontendDiagnostic.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 25 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 26 | #include "clang/Frontend/VerifyDiagnosticsClient.h" |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/Utils.h" |
Sebastian Redl | 6ab7cd8 | 2010-08-18 23:57:17 +0000 | [diff] [blame] | 28 | #include "clang/Serialization/ASTReader.h" |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 29 | #include "clang/Sema/CodeCompleteConsumer.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 30 | #include "llvm/LLVMContext.h" |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 31 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/Statistic.h" |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Timer.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Host.h" |
| 36 | #include "llvm/Support/Path.h" |
| 37 | #include "llvm/Support/Program.h" |
| 38 | #include "llvm/Support/Signals.h" |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/system_error.h" |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 40 | using namespace clang; |
| 41 | |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 42 | CompilerInstance::CompilerInstance() |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 43 | : Invocation(new CompilerInvocation()) { |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 44 | } |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 45 | |
| 46 | CompilerInstance::~CompilerInstance() { |
Daniel Dunbar | 42e9f8e4 | 2010-02-16 01:54:47 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void CompilerInstance::setLLVMContext(llvm::LLVMContext *Value) { |
| 50 | LLVMContext.reset(Value); |
Daniel Dunbar | 2a79e16 | 2009-11-13 03:51:44 +0000 | [diff] [blame] | 51 | } |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 52 | |
Daniel Dunbar | 6228ca0 | 2010-01-30 21:47:07 +0000 | [diff] [blame] | 53 | void CompilerInstance::setInvocation(CompilerInvocation *Value) { |
| 54 | Invocation.reset(Value); |
| 55 | } |
| 56 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 57 | void CompilerInstance::setDiagnostics(Diagnostic *Value) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 58 | Diagnostics = Value; |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 61 | void CompilerInstance::setTarget(TargetInfo *Value) { |
| 62 | Target.reset(Value); |
| 63 | } |
| 64 | |
| 65 | void CompilerInstance::setFileManager(FileManager *Value) { |
| 66 | FileMgr.reset(Value); |
| 67 | } |
| 68 | |
| 69 | void CompilerInstance::setSourceManager(SourceManager *Value) { |
| 70 | SourceMgr.reset(Value); |
| 71 | } |
| 72 | |
| 73 | void CompilerInstance::setPreprocessor(Preprocessor *Value) { |
| 74 | PP.reset(Value); |
| 75 | } |
| 76 | |
| 77 | void CompilerInstance::setASTContext(ASTContext *Value) { |
| 78 | Context.reset(Value); |
| 79 | } |
| 80 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 81 | void CompilerInstance::setSema(Sema *S) { |
| 82 | TheSema.reset(S); |
| 83 | } |
| 84 | |
Daniel Dunbar | 12ce694 | 2009-11-14 02:47:17 +0000 | [diff] [blame] | 85 | void CompilerInstance::setASTConsumer(ASTConsumer *Value) { |
| 86 | Consumer.reset(Value); |
| 87 | } |
| 88 | |
Daniel Dunbar | 8a9f569 | 2009-11-14 01:20:40 +0000 | [diff] [blame] | 89 | void CompilerInstance::setCodeCompletionConsumer(CodeCompleteConsumer *Value) { |
| 90 | CompletionConsumer.reset(Value); |
| 91 | } |
| 92 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 93 | // Diagnostics |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 94 | static void SetUpBuildDumpLog(const DiagnosticOptions &DiagOpts, |
Axel Naumann | 7d0c4cc | 2010-10-11 09:13:46 +0000 | [diff] [blame] | 95 | unsigned argc, const char* const *argv, |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 96 | Diagnostic &Diags) { |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 97 | std::string ErrorInfo; |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 98 | llvm::OwningPtr<llvm::raw_ostream> OS( |
| 99 | new llvm::raw_fd_ostream(DiagOpts.DumpBuildInformation.c_str(), ErrorInfo)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 100 | if (!ErrorInfo.empty()) { |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 101 | Diags.Report(diag::err_fe_unable_to_open_logfile) |
| 102 | << DiagOpts.DumpBuildInformation << ErrorInfo; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 103 | return; |
| 104 | } |
| 105 | |
Daniel Dunbar | dd63b28 | 2009-12-11 23:04:35 +0000 | [diff] [blame] | 106 | (*OS) << "clang -cc1 command line arguments: "; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 107 | for (unsigned i = 0; i != argc; ++i) |
| 108 | (*OS) << argv[i] << ' '; |
| 109 | (*OS) << '\n'; |
| 110 | |
| 111 | // Chain in a diagnostic client which will log the diagnostics. |
| 112 | DiagnosticClient *Logger = |
Kovarththanan Rajaratnam | 6924713 | 2010-03-17 09:47:30 +0000 | [diff] [blame] | 113 | new TextDiagnosticPrinter(*OS.take(), DiagOpts, /*OwnsOutputStream=*/true); |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 114 | Diags.setClient(new ChainedDiagnosticClient(Diags.takeClient(), Logger)); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 117 | void CompilerInstance::createDiagnostics(int Argc, const char* const *Argv, |
| 118 | DiagnosticClient *Client) { |
| 119 | Diagnostics = createDiagnostics(getDiagnosticOpts(), Argc, Argv, Client); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 122 | llvm::IntrusiveRefCntPtr<Diagnostic> |
| 123 | CompilerInstance::createDiagnostics(const DiagnosticOptions &Opts, |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 124 | int Argc, const char* const *Argv, |
| 125 | DiagnosticClient *Client) { |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 126 | llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs()); |
| 127 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(new Diagnostic(DiagID)); |
Daniel Dunbar | 221c721 | 2009-11-14 07:53:24 +0000 | [diff] [blame] | 128 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 129 | // Create the diagnostic client for reporting errors or for |
| 130 | // implementing -verify. |
Douglas Gregor | e47be3e | 2010-11-11 00:39:14 +0000 | [diff] [blame] | 131 | if (Client) |
| 132 | Diags->setClient(Client); |
| 133 | else |
| 134 | Diags->setClient(new TextDiagnosticPrinter(llvm::errs(), Opts)); |
Daniel Dunbar | f79dced | 2009-11-14 03:24:39 +0000 | [diff] [blame] | 135 | |
| 136 | // Chain in -verify checker, if requested. |
| 137 | if (Opts.VerifyDiagnostics) |
Douglas Gregor | bdbb004 | 2010-08-18 22:29:43 +0000 | [diff] [blame] | 138 | Diags->setClient(new VerifyDiagnosticsClient(*Diags, Diags->takeClient())); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 139 | |
| 140 | if (!Opts.DumpBuildInformation.empty()) |
Kovarththanan Rajaratnam | 3d67b1e | 2010-03-17 09:24:48 +0000 | [diff] [blame] | 141 | SetUpBuildDumpLog(Opts, Argc, Argv, *Diags); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 142 | |
| 143 | // Configure our handling of diagnostics. |
Kovarththanan Rajaratnam | 5bf932b | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 144 | ProcessWarningOptions(*Diags, Opts); |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 146 | return Diags; |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // File Manager |
| 150 | |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 151 | void CompilerInstance::createFileManager() { |
Chris Lattner | 7ad97ff | 2010-11-23 07:51:02 +0000 | [diff] [blame] | 152 | FileMgr.reset(new FileManager(getFileSystemOpts())); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 155 | // Source Manager |
| 156 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 157 | void CompilerInstance::createSourceManager(FileManager &FileMgr) { |
| 158 | SourceMgr.reset(new SourceManager(getDiagnostics(), FileMgr)); |
Daniel Dunbar | 16b7449 | 2009-11-13 04:12:06 +0000 | [diff] [blame] | 159 | } |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 160 | |
Daniel Dunbar | 0fbb3d9 | 2009-11-13 05:52:34 +0000 | [diff] [blame] | 161 | // Preprocessor |
| 162 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 163 | void CompilerInstance::createPreprocessor() { |
| 164 | PP.reset(createPreprocessor(getDiagnostics(), getLangOpts(), |
| 165 | getPreprocessorOpts(), getHeaderSearchOpts(), |
| 166 | getDependencyOutputOpts(), getTarget(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 167 | getFrontendOpts(), getSourceManager(), |
| 168 | getFileManager())); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | Preprocessor * |
| 172 | CompilerInstance::createPreprocessor(Diagnostic &Diags, |
| 173 | const LangOptions &LangInfo, |
| 174 | const PreprocessorOptions &PPOpts, |
| 175 | const HeaderSearchOptions &HSOpts, |
| 176 | const DependencyOutputOptions &DepOpts, |
| 177 | const TargetInfo &Target, |
Fariborz Jahanian | 7d95747 | 2010-01-13 18:51:17 +0000 | [diff] [blame] | 178 | const FrontendOptions &FEOpts, |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 179 | SourceManager &SourceMgr, |
| 180 | FileManager &FileMgr) { |
| 181 | // Create a PTH manager if we are using some form of a token cache. |
| 182 | PTHManager *PTHMgr = 0; |
Daniel Dunbar | 049d3a0 | 2009-11-17 05:52:41 +0000 | [diff] [blame] | 183 | if (!PPOpts.TokenCache.empty()) |
Chris Lattner | 681c74a | 2010-11-23 09:01:31 +0000 | [diff] [blame] | 184 | PTHMgr = PTHManager::Create(PPOpts.TokenCache, Diags); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 185 | |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 186 | // Create the Preprocessor. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 187 | HeaderSearch *HeaderInfo = new HeaderSearch(FileMgr); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 188 | Preprocessor *PP = new Preprocessor(Diags, LangInfo, Target, |
| 189 | SourceMgr, *HeaderInfo, PTHMgr, |
| 190 | /*OwnsHeaderSearch=*/true); |
| 191 | |
| 192 | // Note that this is different then passing PTHMgr to Preprocessor's ctor. |
| 193 | // That argument is used as the IdentifierInfoLookup argument to |
| 194 | // IdentifierTable's ctor. |
| 195 | if (PTHMgr) { |
| 196 | PTHMgr->setPreprocessor(PP); |
| 197 | PP->setPTHManager(PTHMgr); |
| 198 | } |
| 199 | |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 200 | if (PPOpts.DetailedRecord) |
| 201 | PP->createPreprocessingRecord(); |
| 202 | |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 203 | InitializePreprocessor(*PP, PPOpts, HSOpts, FEOpts); |
Daniel Dunbar | 22dacfa | 2009-11-13 05:52:11 +0000 | [diff] [blame] | 204 | |
| 205 | // Handle generating dependencies, if requested. |
| 206 | if (!DepOpts.OutputFile.empty()) |
| 207 | AttachDependencyFileGen(*PP, DepOpts); |
| 208 | |
| 209 | return PP; |
| 210 | } |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 211 | |
| 212 | // ASTContext |
| 213 | |
| 214 | void CompilerInstance::createASTContext() { |
| 215 | Preprocessor &PP = getPreprocessor(); |
| 216 | Context.reset(new ASTContext(getLangOpts(), PP.getSourceManager(), |
| 217 | getTarget(), PP.getIdentifierTable(), |
| 218 | PP.getSelectorTable(), PP.getBuiltinInfo(), |
Daniel Dunbar | 5eb8100 | 2009-11-13 08:20:47 +0000 | [diff] [blame] | 219 | /*size_reserve=*/ 0)); |
| 220 | } |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 221 | |
| 222 | // ExternalASTSource |
| 223 | |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 224 | void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 225 | bool DisablePCHValidation, |
| 226 | void *DeserializationListener){ |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 227 | llvm::OwningPtr<ExternalASTSource> Source; |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 228 | bool Preamble = getPreprocessorOpts().PrecompiledPreambleBytes.first != 0; |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 229 | Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 230 | DisablePCHValidation, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 231 | getPreprocessor(), getASTContext(), |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 232 | DeserializationListener, |
| 233 | Preamble)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 234 | getASTContext().setExternalSource(Source); |
| 235 | } |
| 236 | |
| 237 | ExternalASTSource * |
| 238 | CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path, |
| 239 | const std::string &Sysroot, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 240 | bool DisablePCHValidation, |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 241 | Preprocessor &PP, |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 242 | ASTContext &Context, |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 243 | void *DeserializationListener, |
| 244 | bool Preamble) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 245 | llvm::OwningPtr<ASTReader> Reader; |
| 246 | Reader.reset(new ASTReader(PP, &Context, |
Douglas Gregor | fae3b2f | 2010-07-27 00:27:13 +0000 | [diff] [blame] | 247 | Sysroot.empty() ? 0 : Sysroot.c_str(), |
| 248 | DisablePCHValidation)); |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 249 | |
Sebastian Redl | ffaab3e | 2010-07-30 00:29:29 +0000 | [diff] [blame] | 250 | Reader->setDeserializationListener( |
Sebastian Redl | 571db7f | 2010-08-18 23:56:56 +0000 | [diff] [blame] | 251 | static_cast<ASTDeserializationListener *>(DeserializationListener)); |
Sebastian Redl | 1d9f1fe | 2010-10-05 16:15:19 +0000 | [diff] [blame] | 252 | switch (Reader->ReadAST(Path, |
| 253 | Preamble ? ASTReader::Preamble : ASTReader::PCH)) { |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 254 | case ASTReader::Success: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 255 | // Set the predefines buffer as suggested by the PCH reader. Typically, the |
| 256 | // predefines buffer will be empty. |
| 257 | PP.setPredefines(Reader->getSuggestedPredefines()); |
| 258 | return Reader.take(); |
| 259 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 260 | case ASTReader::Failure: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 261 | // Unrecoverable failure: don't even try to process the input file. |
| 262 | break; |
| 263 | |
Sebastian Redl | c43b54c | 2010-08-18 23:56:43 +0000 | [diff] [blame] | 264 | case ASTReader::IgnorePCH: |
Daniel Dunbar | 0f80039 | 2009-11-13 08:21:10 +0000 | [diff] [blame] | 265 | // No suitable PCH file could be found. Return an error. |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 271 | |
| 272 | // Code Completion |
| 273 | |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 274 | static bool EnableCodeCompletion(Preprocessor &PP, |
| 275 | const std::string &Filename, |
| 276 | unsigned Line, |
| 277 | unsigned Column) { |
| 278 | // Tell the source manager to chop off the given file at a specific |
| 279 | // line and column. |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 280 | const FileEntry *Entry = PP.getFileManager().getFile(Filename); |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 281 | if (!Entry) { |
| 282 | PP.getDiagnostics().Report(diag::err_fe_invalid_code_complete_file) |
| 283 | << Filename; |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | // Truncate the named file at the given line/column. |
| 288 | PP.SetCodeCompletionPoint(Entry, Line, Column); |
| 289 | return false; |
| 290 | } |
| 291 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 292 | void CompilerInstance::createCodeCompletionConsumer() { |
| 293 | const ParsedSourceLocation &Loc = getFrontendOpts().CodeCompletionAt; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 294 | if (!CompletionConsumer) { |
| 295 | CompletionConsumer.reset( |
| 296 | createCodeCompletionConsumer(getPreprocessor(), |
| 297 | Loc.FileName, Loc.Line, Loc.Column, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 298 | getFrontendOpts().ShowMacrosInCodeCompletion, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 299 | getFrontendOpts().ShowCodePatternsInCodeCompletion, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 300 | getFrontendOpts().ShowGlobalSymbolsInCodeCompletion, |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 301 | llvm::outs())); |
| 302 | if (!CompletionConsumer) |
| 303 | return; |
| 304 | } else if (EnableCodeCompletion(getPreprocessor(), Loc.FileName, |
| 305 | Loc.Line, Loc.Column)) { |
| 306 | CompletionConsumer.reset(); |
Douglas Gregor | c3d43b7 | 2010-03-16 06:04:47 +0000 | [diff] [blame] | 307 | return; |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 308 | } |
Douglas Gregor | 2b4074f | 2009-12-01 05:55:20 +0000 | [diff] [blame] | 309 | |
| 310 | if (CompletionConsumer->isOutputBinary() && |
| 311 | llvm::sys::Program::ChangeStdoutToBinary()) { |
| 312 | getPreprocessor().getDiagnostics().Report(diag::err_fe_stdout_binary); |
| 313 | CompletionConsumer.reset(); |
| 314 | } |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Kovarththanan Rajaratnam | f79bafa | 2009-11-29 09:57:35 +0000 | [diff] [blame] | 317 | void CompilerInstance::createFrontendTimer() { |
| 318 | FrontendTimer.reset(new llvm::Timer("Clang front-end timer")); |
| 319 | } |
| 320 | |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 321 | CodeCompleteConsumer * |
| 322 | CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP, |
| 323 | const std::string &Filename, |
| 324 | unsigned Line, |
| 325 | unsigned Column, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 326 | bool ShowMacros, |
Douglas Gregor | d8e8a58 | 2010-05-25 21:41:55 +0000 | [diff] [blame] | 327 | bool ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 328 | bool ShowGlobals, |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 329 | llvm::raw_ostream &OS) { |
Douglas Gregor | 1abc6bc | 2010-08-04 16:47:14 +0000 | [diff] [blame] | 330 | if (EnableCodeCompletion(PP, Filename, Line, Column)) |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 331 | return 0; |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 332 | |
| 333 | // Set up the creation routine for code-completion. |
Douglas Gregor | a9f4f62 | 2010-10-11 22:12:15 +0000 | [diff] [blame] | 334 | return new PrintingCodeCompleteConsumer(ShowMacros, ShowCodePatterns, |
Douglas Gregor | 8071e42 | 2010-08-15 06:18:01 +0000 | [diff] [blame] | 335 | ShowGlobals, OS); |
Daniel Dunbar | c2f484f | 2009-11-13 09:36:05 +0000 | [diff] [blame] | 336 | } |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 337 | |
Douglas Gregor | f18d0d8 | 2010-08-12 23:31:19 +0000 | [diff] [blame] | 338 | void CompilerInstance::createSema(bool CompleteTranslationUnit, |
| 339 | CodeCompleteConsumer *CompletionConsumer) { |
| 340 | TheSema.reset(new Sema(getPreprocessor(), getASTContext(), getASTConsumer(), |
| 341 | CompleteTranslationUnit, CompletionConsumer)); |
| 342 | } |
| 343 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 344 | // Output Files |
| 345 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 346 | void CompilerInstance::addOutputFile(const OutputFile &OutFile) { |
| 347 | assert(OutFile.OS && "Attempt to add empty stream to output list!"); |
| 348 | OutputFiles.push_back(OutFile); |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Kovarththanan Rajaratnam | e51dd7b | 2010-03-06 12:07:48 +0000 | [diff] [blame] | 351 | void CompilerInstance::clearOutputFiles(bool EraseFiles) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 352 | for (std::list<OutputFile>::iterator |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 353 | it = OutputFiles.begin(), ie = OutputFiles.end(); it != ie; ++it) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 354 | delete it->OS; |
| 355 | if (!it->TempFilename.empty()) { |
| 356 | llvm::sys::Path TempPath(it->TempFilename); |
| 357 | if (EraseFiles) |
| 358 | TempPath.eraseFromDisk(); |
| 359 | else { |
| 360 | std::string Error; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 361 | llvm::sys::Path NewOutFile(it->Filename); |
| 362 | // If '-working-directory' was passed, the output filename should be |
| 363 | // relative to that. |
| 364 | FileManager::FixupRelativePath(NewOutFile, getFileSystemOpts()); |
| 365 | if (TempPath.renamePathOnDisk(NewOutFile, &Error)) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 366 | getDiagnostics().Report(diag::err_fe_unable_to_rename_temp) |
| 367 | << it->TempFilename << it->Filename << Error; |
| 368 | TempPath.eraseFromDisk(); |
| 369 | } |
| 370 | } |
| 371 | } else if (!it->Filename.empty() && EraseFiles) |
| 372 | llvm::sys::Path(it->Filename).eraseFromDisk(); |
| 373 | |
Daniel Dunbar | a920483 | 2009-11-13 10:37:48 +0000 | [diff] [blame] | 374 | } |
| 375 | OutputFiles.clear(); |
| 376 | } |
| 377 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 378 | llvm::raw_fd_ostream * |
| 379 | CompilerInstance::createDefaultOutputFile(bool Binary, |
| 380 | llvm::StringRef InFile, |
| 381 | llvm::StringRef Extension) { |
| 382 | return createOutputFile(getFrontendOpts().OutputFile, Binary, |
| 383 | InFile, Extension); |
| 384 | } |
| 385 | |
| 386 | llvm::raw_fd_ostream * |
| 387 | CompilerInstance::createOutputFile(llvm::StringRef OutputPath, |
| 388 | bool Binary, |
| 389 | llvm::StringRef InFile, |
| 390 | llvm::StringRef Extension) { |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 391 | std::string Error, OutputPathName, TempPathName; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 392 | llvm::raw_fd_ostream *OS = createOutputFile(OutputPath, Error, Binary, |
| 393 | InFile, Extension, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 394 | &OutputPathName, |
| 395 | &TempPathName); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 396 | if (!OS) { |
Daniel Dunbar | 3604359 | 2009-12-03 09:13:30 +0000 | [diff] [blame] | 397 | getDiagnostics().Report(diag::err_fe_unable_to_open_output) |
| 398 | << OutputPath << Error; |
| 399 | return 0; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | // Add the output file -- but don't try to remove "-", since this means we are |
| 403 | // using stdin. |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 404 | addOutputFile(OutputFile((OutputPathName != "-") ? OutputPathName : "", |
| 405 | TempPathName, OS)); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 406 | |
| 407 | return OS; |
| 408 | } |
| 409 | |
| 410 | llvm::raw_fd_ostream * |
| 411 | CompilerInstance::createOutputFile(llvm::StringRef OutputPath, |
| 412 | std::string &Error, |
| 413 | bool Binary, |
| 414 | llvm::StringRef InFile, |
| 415 | llvm::StringRef Extension, |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 416 | std::string *ResultPathName, |
| 417 | std::string *TempPathName) { |
| 418 | std::string OutFile, TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 419 | if (!OutputPath.empty()) { |
| 420 | OutFile = OutputPath; |
| 421 | } else if (InFile == "-") { |
| 422 | OutFile = "-"; |
| 423 | } else if (!Extension.empty()) { |
| 424 | llvm::sys::Path Path(InFile); |
| 425 | Path.eraseSuffix(); |
| 426 | Path.appendSuffix(Extension); |
| 427 | OutFile = Path.str(); |
| 428 | } else { |
| 429 | OutFile = "-"; |
| 430 | } |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 431 | |
| 432 | if (OutFile != "-") { |
| 433 | llvm::sys::Path OutPath(OutFile); |
| 434 | // Only create the temporary if we can actually write to OutPath, otherwise |
| 435 | // we want to fail early. |
| 436 | if (!OutPath.exists() || |
| 437 | (OutPath.isRegularFile() && OutPath.canWrite())) { |
| 438 | // Create a temporary file. |
| 439 | llvm::sys::Path TempPath(OutFile); |
| 440 | if (!TempPath.createTemporaryFileOnDisk()) |
| 441 | TempFile = TempPath.str(); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | std::string OSFile = OutFile; |
| 446 | if (!TempFile.empty()) |
| 447 | OSFile = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 448 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 449 | llvm::OwningPtr<llvm::raw_fd_ostream> OS( |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 450 | new llvm::raw_fd_ostream(OSFile.c_str(), Error, |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 451 | (Binary ? llvm::raw_fd_ostream::F_Binary : 0))); |
| 452 | if (!Error.empty()) |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 453 | return 0; |
| 454 | |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 455 | // Make sure the out stream file gets removed if we crash. |
| 456 | llvm::sys::RemoveFileOnSignal(llvm::sys::Path(OSFile)); |
| 457 | |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 458 | if (ResultPathName) |
| 459 | *ResultPathName = OutFile; |
Argyrios Kyrtzidis | dc24572 | 2010-09-17 17:38:48 +0000 | [diff] [blame] | 460 | if (TempPathName) |
| 461 | *TempPathName = TempFile; |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 462 | |
Daniel Dunbar | fc97102 | 2009-11-20 22:32:38 +0000 | [diff] [blame] | 463 | return OS.take(); |
Daniel Dunbar | f482d59 | 2009-11-13 18:32:08 +0000 | [diff] [blame] | 464 | } |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 465 | |
| 466 | // Initialization Utilities |
| 467 | |
| 468 | bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile) { |
| 469 | return InitializeSourceManager(InputFile, getDiagnostics(), getFileManager(), |
| 470 | getSourceManager(), getFrontendOpts()); |
| 471 | } |
| 472 | |
| 473 | bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile, |
| 474 | Diagnostic &Diags, |
| 475 | FileManager &FileMgr, |
| 476 | SourceManager &SourceMgr, |
| 477 | const FrontendOptions &Opts) { |
Douglas Gregor | 414cb64 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 478 | // Figure out where to get and map in the main file, unless it's already |
| 479 | // been created (e.g., by a precompiled preamble). |
| 480 | if (!SourceMgr.getMainFileID().isInvalid()) { |
| 481 | // Do nothing: the main file has already been set. |
| 482 | } else if (InputFile != "-") { |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 483 | const FileEntry *File = FileMgr.getFile(InputFile); |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 484 | if (!File) { |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 485 | Diags.Report(diag::err_fe_error_reading) << InputFile; |
| 486 | return false; |
| 487 | } |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 488 | SourceMgr.createMainFileID(File); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 489 | } else { |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame^] | 490 | llvm::OwningPtr<llvm::MemoryBuffer> SB; |
| 491 | if (llvm::MemoryBuffer::getSTDIN(SB)) { |
Michael J. Spencer | 3a321e2 | 2010-12-09 17:36:38 +0000 | [diff] [blame] | 492 | // FIXME: Give ec.message() in this diag. |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 493 | Diags.Report(diag::err_fe_error_reading_stdin); |
| 494 | return false; |
| 495 | } |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 496 | const FileEntry *File = FileMgr.getVirtualFile(SB->getBufferIdentifier(), |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 497 | SB->getBufferSize(), 0); |
Dan Gohman | 90d9081 | 2010-10-26 23:21:25 +0000 | [diff] [blame] | 498 | SourceMgr.createMainFileID(File); |
Michael J. Spencer | 4eeebc4 | 2010-12-16 03:28:14 +0000 | [diff] [blame^] | 499 | SourceMgr.overrideFileContents(File, SB.take()); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Dan Gohman | 694137c | 2010-10-26 21:13:51 +0000 | [diff] [blame] | 502 | assert(!SourceMgr.getMainFileID().isInvalid() && |
| 503 | "Couldn't establish MainFileID!"); |
Daniel Dunbar | ccb6cb6 | 2009-11-14 07:53:04 +0000 | [diff] [blame] | 504 | return true; |
| 505 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 506 | |
| 507 | // High-Level Operations |
| 508 | |
| 509 | bool CompilerInstance::ExecuteAction(FrontendAction &Act) { |
| 510 | assert(hasDiagnostics() && "Diagnostics engine is not initialized!"); |
| 511 | assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!"); |
| 512 | assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!"); |
| 513 | |
| 514 | // FIXME: Take this as an argument, once all the APIs we used have moved to |
| 515 | // taking it as an input instead of hard-coding llvm::errs. |
| 516 | llvm::raw_ostream &OS = llvm::errs(); |
| 517 | |
| 518 | // Create the target instance. |
| 519 | setTarget(TargetInfo::CreateTargetInfo(getDiagnostics(), getTargetOpts())); |
| 520 | if (!hasTarget()) |
| 521 | return false; |
| 522 | |
| 523 | // Inform the target of the language options. |
| 524 | // |
| 525 | // FIXME: We shouldn't need to do this, the target should be immutable once |
| 526 | // created. This complexity should be lifted elsewhere. |
| 527 | getTarget().setForcedLangOptions(getLangOpts()); |
| 528 | |
| 529 | // Validate/process some options. |
| 530 | if (getHeaderSearchOpts().Verbose) |
| 531 | OS << "clang -cc1 version " CLANG_VERSION_STRING |
| 532 | << " based upon " << PACKAGE_STRING |
| 533 | << " hosted on " << llvm::sys::getHostTriple() << "\n"; |
| 534 | |
| 535 | if (getFrontendOpts().ShowTimers) |
| 536 | createFrontendTimer(); |
| 537 | |
Douglas Gregor | 95dd558 | 2010-03-30 17:33:59 +0000 | [diff] [blame] | 538 | if (getFrontendOpts().ShowStats) |
| 539 | llvm::EnableStatistics(); |
| 540 | |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 541 | for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { |
| 542 | const std::string &InFile = getFrontendOpts().Inputs[i].second; |
| 543 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 544 | // Reset the ID tables if we are reusing the SourceManager. |
| 545 | if (hasSourceManager()) |
| 546 | getSourceManager().clearIDTables(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 547 | |
Daniel Dunbar | d3598a6 | 2010-06-07 23:23:06 +0000 | [diff] [blame] | 548 | if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 549 | Act.Execute(); |
| 550 | Act.EndSourceFile(); |
| 551 | } |
| 552 | } |
| 553 | |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 554 | if (getDiagnosticOpts().ShowCarets) { |
Argyrios Kyrtzidis | f2224d8 | 2010-11-18 20:06:46 +0000 | [diff] [blame] | 555 | // We can have multiple diagnostics sharing one diagnostic client. |
| 556 | // Get the total number of warnings/errors from the client. |
| 557 | unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings(); |
| 558 | unsigned NumErrors = getDiagnostics().getClient()->getNumErrors(); |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 559 | |
| 560 | if (NumWarnings) |
| 561 | OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s"); |
| 562 | if (NumWarnings && NumErrors) |
| 563 | OS << " and "; |
| 564 | if (NumErrors) |
| 565 | OS << NumErrors << " error" << (NumErrors == 1 ? "" : "s"); |
| 566 | if (NumWarnings || NumErrors) |
| 567 | OS << " generated.\n"; |
| 568 | } |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 569 | |
Daniel Dunbar | 2056048 | 2010-06-07 23:23:50 +0000 | [diff] [blame] | 570 | if (getFrontendOpts().ShowStats && hasFileManager()) { |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 571 | getFileManager().PrintStats(); |
| 572 | OS << "\n"; |
| 573 | } |
| 574 | |
Argyrios Kyrtzidis | ab41b97 | 2010-11-18 21:13:57 +0000 | [diff] [blame] | 575 | return !getDiagnostics().getClient()->getNumErrors(); |
Daniel Dunbar | 0397af2 | 2010-01-13 00:48:06 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | |