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 #include "lldb/Core/Log.h"
12 #include "lldb/Core/StreamString.h"
13 
14 using namespace lldb_private;
15 
16 void
17 DiagnosticManager::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 
35 static const char *
36 StringForSeverity(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 
50 std::string
51 DiagnosticManager::GetString(char separator)
52 {
53     std::string ret;
54 
55     for (const Diagnostic *diagnostic : Diagnostics())
56     {
57         ret.append(StringForSeverity(diagnostic->GetSeverity()));
58         ret.append(diagnostic->GetMessage());
59         ret.push_back(separator);
60     }
61 
62     return ret;
63 }
64 
65 size_t
66 DiagnosticManager::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 
80 size_t
81 DiagnosticManager::PutCString(DiagnosticSeverity severity, const char *cstr)
82 {
83     if (!cstr)
84         return 0;
85     AddDiagnostic(cstr, severity, eDiagnosticOriginLLDB);
86     return strlen(cstr);
87 }
88