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