1 //===--- TextDiagnosticBuffer.cpp - Buffer Text Diagnostics ---------------===// 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 // This is a concrete diagnostic client, which buffers the diagnostic messages. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Frontend/TextDiagnosticBuffer.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/Support/ErrorHandling.h" 17 using namespace clang; 18 19 /// HandleDiagnostic - Store the errors, warnings, and notes that are 20 /// reported. 21 /// 22 void TextDiagnosticBuffer::HandleDiagnostic(DiagnosticsEngine::Level Level, 23 const Diagnostic &Info) { 24 // Default implementation (Warnings/errors count). 25 DiagnosticConsumer::HandleDiagnostic(Level, Info); 26 27 SmallString<100> Buf; 28 Info.FormatDiagnostic(Buf); 29 switch (Level) { 30 default: llvm_unreachable( 31 "Diagnostic not handled during diagnostic buffering!"); 32 case DiagnosticsEngine::Note: 33 All.emplace_back(Level, Notes.size()); 34 Notes.emplace_back(Info.getLocation(), Buf.str()); 35 break; 36 case DiagnosticsEngine::Warning: 37 All.emplace_back(Level, Warnings.size()); 38 Warnings.emplace_back(Info.getLocation(), Buf.str()); 39 break; 40 case DiagnosticsEngine::Remark: 41 All.emplace_back(Level, Remarks.size()); 42 Remarks.emplace_back(Info.getLocation(), Buf.str()); 43 break; 44 case DiagnosticsEngine::Error: 45 case DiagnosticsEngine::Fatal: 46 All.emplace_back(Level, Errors.size()); 47 Errors.emplace_back(Info.getLocation(), Buf.str()); 48 break; 49 } 50 } 51 52 void TextDiagnosticBuffer::FlushDiagnostics(DiagnosticsEngine &Diags) const { 53 for (auto it = All.begin(), ie = All.end(); it != ie; ++it) { 54 auto Diag = Diags.Report(Diags.getCustomDiagID(it->first, "%0")); 55 switch (it->first) { 56 default: llvm_unreachable( 57 "Diagnostic not handled during diagnostic flushing!"); 58 case DiagnosticsEngine::Note: 59 Diag << Notes[it->second].second; 60 break; 61 case DiagnosticsEngine::Warning: 62 Diag << Warnings[it->second].second; 63 break; 64 case DiagnosticsEngine::Remark: 65 Diag << Remarks[it->second].second; 66 break; 67 case DiagnosticsEngine::Error: 68 case DiagnosticsEngine::Fatal: 69 Diag << Errors[it->second].second; 70 break; 71 } 72 } 73 } 74 75