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/Core/Log.h"
15 #include "lldb/Core/StreamString.h"
16 
17 using namespace lldb_private;
18 
19 void
20 DiagnosticManager::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 
38 static const char *
39 StringForSeverity(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 
54 std::string
55 DiagnosticManager::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 
69 size_t
70 DiagnosticManager::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 
84 size_t
85 DiagnosticManager::PutCString(DiagnosticSeverity severity, const char *cstr)
86 {
87     if (!cstr)
88         return 0;
89     AddDiagnostic(cstr, severity, eDiagnosticOriginLLDB);
90     return strlen(cstr);
91 }
92