blob: 53d85f8a6d54d068cb0dc12687c69896668a1114 [file] [log] [blame]
Sean Callanan579e70c2016-03-19 00:03:59 +00001//===-- DiagnosticManager.cpp -----------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sean Callanan579e70c2016-03-19 00:03:59 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Expression/DiagnosticManager.h"
Adrian McCarthy6cd53642016-04-28 20:14:44 +000010
11#include "llvm/Support/ErrorHandling.h"
12
Zachary Turner6f9e6902017-03-03 20:56:28 +000013#include "lldb/Utility/Log.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000014#include "lldb/Utility/StreamString.h"
Sean Callanan579e70c2016-03-19 00:03:59 +000015
16using namespace lldb_private;
17
Kate Stoneb9c1b512016-09-06 20:57:50 +000018void DiagnosticManager::Dump(Log *log) {
19 if (!log)
20 return;
Sean Callanan579e70c2016-03-19 00:03:59 +000021
Kate Stoneb9c1b512016-09-06 20:57:50 +000022 std::string str = GetString();
Sean Callanan579e70c2016-03-19 00:03:59 +000023
Adrian Prantl05097242018-04-30 16:49:04 +000024 // GetString() puts a separator after each diagnostic. We want to remove the
25 // last '\n' because log->PutCString will add one for us.
Sean Callanan579e70c2016-03-19 00:03:59 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 if (str.size() && str.back() == '\n') {
28 str.pop_back();
29 }
Sean Callanan579e70c2016-03-19 00:03:59 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 log->PutCString(str.c_str());
Sean Callanan579e70c2016-03-19 00:03:59 +000032}
33
Kate Stoneb9c1b512016-09-06 20:57:50 +000034static 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");
Sean Callanan579e70c2016-03-19 00:03:59 +000045}
46
Kate Stoneb9c1b512016-09-06 20:57:50 +000047std::string DiagnosticManager::GetString(char separator) {
48 std::string ret;
Sean Callanan579e70c2016-03-19 00:03:59 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 for (const Diagnostic *diagnostic : Diagnostics()) {
51 ret.append(StringForSeverity(diagnostic->GetSeverity()));
52 ret.append(diagnostic->GetMessage());
53 ret.push_back(separator);
54 }
Sean Callanan579e70c2016-03-19 00:03:59 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 return ret;
Sean Callanan579e70c2016-03-19 00:03:59 +000057}
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059size_t DiagnosticManager::Printf(DiagnosticSeverity severity,
60 const char *format, ...) {
61 StreamString ss;
Sean Callanan579e70c2016-03-19 00:03:59 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 va_list args;
64 va_start(args, format);
65 size_t result = ss.PrintfVarArg(format, args);
66 va_end(args);
Sean Callanan579e70c2016-03-19 00:03:59 +000067
Zachary Turnere2411fa2016-11-12 19:12:56 +000068 AddDiagnostic(ss.GetString(), severity, eDiagnosticOriginLLDB);
Sean Callanan579e70c2016-03-19 00:03:59 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 return result;
Sean Callanan579e70c2016-03-19 00:03:59 +000071}
72
Zachary Turnere2411fa2016-11-12 19:12:56 +000073size_t DiagnosticManager::PutString(DiagnosticSeverity severity,
74 llvm::StringRef str) {
75 if (str.empty())
Kate Stoneb9c1b512016-09-06 20:57:50 +000076 return 0;
Zachary Turnere2411fa2016-11-12 19:12:56 +000077 AddDiagnostic(str, severity, eDiagnosticOriginLLDB);
78 return str.size();
Sean Callanan579e70c2016-03-19 00:03:59 +000079}
Jim Ingham56454b52017-04-19 18:56:44 +000080
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}