DiagnosticManager.cpp revision 303241
139224Sabial//===-- DiagnosticManager.cpp -----------------------------------*- C++ -*-===//
239224Sabial//
339224Sabial//                     The LLVM Compiler Infrastructure
439224Sabial//
539224Sabial// This file is distributed under the University of Illinois Open Source
639224Sabial// License. See LICENSE.TXT for details.
739224Sabial//
839224Sabial//===----------------------------------------------------------------------===//
939224Sabial
10#include "lldb/Expression/DiagnosticManager.h"
11
12#include "llvm/Support/ErrorHandling.h"
13
14#include "lldb/Core/Log.h"
15#include "lldb/Core/StreamString.h"
16
17using namespace lldb_private;
18
19void
20DiagnosticManager::Dump(Log *log)
21{
22    if (!log)
23        return;
24
25    std::string str = GetString();
26
27    // GetString() puts a separator after each diagnostic.
28    // We want to remove the last '\n' because log->PutCString will add one for us.
29
30    if (str.size() && str.back() == '\n')
31    {
32        str.pop_back();
33    }
34
35    log->PutCString(str.c_str());
36}
37
38static const char *
39StringForSeverity(DiagnosticSeverity severity)
40{
41    switch (severity)
42    {
43        // this should be exhaustive
44        case lldb_private::eDiagnosticSeverityError:
45            return "error: ";
46        case lldb_private::eDiagnosticSeverityWarning:
47            return "warning: ";
48        case lldb_private::eDiagnosticSeverityRemark:
49            return "";
50    }
51    llvm_unreachable("switch needs another case for DiagnosticSeverity enum");
52}
53
54std::string
55DiagnosticManager::GetString(char separator)
56{
57    std::string ret;
58
59    for (const Diagnostic *diagnostic : Diagnostics())
60    {
61        ret.append(StringForSeverity(diagnostic->GetSeverity()));
62        ret.append(diagnostic->GetMessage());
63        ret.push_back(separator);
64    }
65
66    return ret;
67}
68
69size_t
70DiagnosticManager::Printf(DiagnosticSeverity severity, const char *format, ...)
71{
72    StreamString ss;
73
74    va_list args;
75    va_start(args, format);
76    size_t result = ss.PrintfVarArg(format, args);
77    va_end(args);
78
79    AddDiagnostic(ss.GetData(), severity, eDiagnosticOriginLLDB);
80
81    return result;
82}
83
84size_t
85DiagnosticManager::PutCString(DiagnosticSeverity severity, const char *cstr)
86{
87    if (!cstr)
88        return 0;
89    AddDiagnostic(cstr, severity, eDiagnosticOriginLLDB);
90    return strlen(cstr);
91}
92