DiagnosticManager.cpp revision 360660
1//===-- DiagnosticManager.cpp -----------------------------------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8 9#include "lldb/Expression/DiagnosticManager.h" 10 11#include "llvm/Support/ErrorHandling.h" 12 13#include "lldb/Utility/Log.h" 14#include "lldb/Utility/StreamString.h" 15 16using namespace lldb_private; 17 18void DiagnosticManager::Dump(Log *log) { 19 if (!log) 20 return; 21 22 std::string str = GetString(); 23 24 // GetString() puts a separator after each diagnostic. We want to remove the 25 // last '\n' because log->PutCString will add one for us. 26 27 if (str.size() && str.back() == '\n') { 28 str.pop_back(); 29 } 30 31 log->PutCString(str.c_str()); 32} 33 34static const char *StringForSeverity(DiagnosticSeverity severity) { 35 switch (severity) { 36 // this should be exhaustive 37 case lldb_private::eDiagnosticSeverityError: 38 return "error: "; 39 case lldb_private::eDiagnosticSeverityWarning: 40 return "warning: "; 41 case lldb_private::eDiagnosticSeverityRemark: 42 return ""; 43 } 44 llvm_unreachable("switch needs another case for DiagnosticSeverity enum"); 45} 46 47std::string DiagnosticManager::GetString(char separator) { 48 std::string ret; 49 50 for (const Diagnostic *diagnostic : Diagnostics()) { 51 ret.append(StringForSeverity(diagnostic->GetSeverity())); 52 ret.append(diagnostic->GetMessage()); 53 ret.push_back(separator); 54 } 55 56 return ret; 57} 58 59size_t DiagnosticManager::Printf(DiagnosticSeverity severity, 60 const char *format, ...) { 61 StreamString ss; 62 63 va_list args; 64 va_start(args, format); 65 size_t result = ss.PrintfVarArg(format, args); 66 va_end(args); 67 68 AddDiagnostic(ss.GetString(), severity, eDiagnosticOriginLLDB); 69 70 return result; 71} 72 73size_t DiagnosticManager::PutString(DiagnosticSeverity severity, 74 llvm::StringRef str) { 75 if (str.empty()) 76 return 0; 77 AddDiagnostic(str, severity, eDiagnosticOriginLLDB); 78 return str.size(); 79} 80 81void DiagnosticManager::CopyDiagnostics(DiagnosticManager &otherDiagnostics) { 82 for (const DiagnosticList::value_type &other_diagnostic: 83 otherDiagnostics.Diagnostics()) { 84 AddDiagnostic( 85 other_diagnostic->GetMessage(), other_diagnostic->GetSeverity(), 86 other_diagnostic->getKind(), other_diagnostic->GetCompilerID()); 87 } 88} 89