Path: blob/main/contrib/llvm-project/lldb/source/Expression/DiagnosticManager.cpp
39587 views
//===-- DiagnosticManager.cpp ---------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Expression/DiagnosticManager.h"910#include "llvm/Support/ErrorHandling.h"1112#include "lldb/Utility/Log.h"13#include "lldb/Utility/StreamString.h"1415using namespace lldb_private;1617void DiagnosticManager::Dump(Log *log) {18if (!log)19return;2021std::string str = GetString();2223// GetString() puts a separator after each diagnostic. We want to remove the24// last '\n' because log->PutCString will add one for us.2526if (str.size() && str.back() == '\n') {27str.pop_back();28}2930log->PutCString(str.c_str());31}3233static const char *StringForSeverity(lldb::Severity severity) {34switch (severity) {35// this should be exhaustive36case lldb::eSeverityError:37return "error: ";38case lldb::eSeverityWarning:39return "warning: ";40case lldb::eSeverityInfo:41return "";42}43llvm_unreachable("switch needs another case for lldb::Severity enum");44}4546std::string DiagnosticManager::GetString(char separator) {47std::string ret;48llvm::raw_string_ostream stream(ret);4950for (const auto &diagnostic : Diagnostics()) {51llvm::StringRef severity = StringForSeverity(diagnostic->GetSeverity());52stream << severity;5354llvm::StringRef message = diagnostic->GetMessage();55std::string searchable_message = message.lower();56auto severity_pos = message.find(severity);57stream << message.take_front(severity_pos);5859if (severity_pos != llvm::StringRef::npos)60stream << message.drop_front(severity_pos + severity.size());61stream << separator;62}6364return ret;65}6667size_t DiagnosticManager::Printf(lldb::Severity severity, const char *format,68...) {69StreamString ss;7071va_list args;72va_start(args, format);73size_t result = ss.PrintfVarArg(format, args);74va_end(args);7576AddDiagnostic(ss.GetString(), severity, eDiagnosticOriginLLDB);7778return result;79}8081void DiagnosticManager::PutString(lldb::Severity severity,82llvm::StringRef str) {83if (str.empty())84return;85AddDiagnostic(str, severity, eDiagnosticOriginLLDB);86}878889