blob: e0a5c492068c5737231b94d48e924b5fb491f819 [file] [log] [blame]
Sean Callanan579e70c2016-03-19 00:03:59 +00001//===-- 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#include "lldb/Core/Log.h"
12#include "lldb/Core/StreamString.h"
13
14using namespace lldb_private;
15
16void
17DiagnosticManager::Dump(Log *log)
18{
19 if (!log)
20 return;
21
22 std::string str = GetString();
23
24 // GetString() puts a separator after each diagnostic.
25 // We want to remove the last '\n' because log->PutCString will add one for us.
26
27 if (str.size() && str.back() == '\n')
28 {
29 str.pop_back();
30 }
31
32 log->PutCString(str.c_str());
33}
34
35static const char *
36StringForSeverity(DiagnosticSeverity severity)
37{
38 switch (severity)
39 {
40 // this should be exhaustive
41 case lldb_private::eDiagnosticSeverityError:
42 return "error: ";
43 case lldb_private::eDiagnosticSeverityWarning:
44 return "warning: ";
45 case lldb_private::eDiagnosticSeverityRemark:
46 return "";
47 }
48}
49
50std::string
51DiagnosticManager::GetString(char separator)
52{
53 std::string ret;
54
Jim Inghama1e541b2016-03-25 01:57:14 +000055 for (const Diagnostic *diagnostic : Diagnostics())
Sean Callanan579e70c2016-03-19 00:03:59 +000056 {
Jim Inghama1e541b2016-03-25 01:57:14 +000057 ret.append(StringForSeverity(diagnostic->GetSeverity()));
58 ret.append(diagnostic->GetMessage());
Sean Callanan579e70c2016-03-19 00:03:59 +000059 ret.push_back(separator);
60 }
61
62 return ret;
63}
64
65size_t
66DiagnosticManager::Printf(DiagnosticSeverity severity, const char *format, ...)
67{
68 StreamString ss;
69
70 va_list args;
71 va_start(args, format);
72 size_t result = ss.PrintfVarArg(format, args);
73 va_end(args);
74
75 AddDiagnostic(ss.GetData(), severity, eDiagnosticOriginLLDB);
76
77 return result;
78}
79
80size_t
81DiagnosticManager::PutCString(DiagnosticSeverity severity, const char *cstr)
82{
83 if (!cstr)
84 return 0;
85 AddDiagnostic(cstr, severity, eDiagnosticOriginLLDB);
86 return strlen(cstr);
87}