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 DiagnosticManager::Dump(Log *log) {
20   if (!log)
21     return;
22 
23   std::string str = GetString();
24 
25   // GetString() puts a separator after each diagnostic.
26   // We want to remove the last '\n' because log->PutCString will add one for
27   // us.
28 
29   if (str.size() && str.back() == '\n') {
30     str.pop_back();
31   }
32 
33   log->PutCString(str.c_str());
34 }
35 
36 static const char *StringForSeverity(DiagnosticSeverity severity) {
37   switch (severity) {
38   // this should be exhaustive
39   case lldb_private::eDiagnosticSeverityError:
40     return "error: ";
41   case lldb_private::eDiagnosticSeverityWarning:
42     return "warning: ";
43   case lldb_private::eDiagnosticSeverityRemark:
44     return "";
45   }
46   llvm_unreachable("switch needs another case for DiagnosticSeverity enum");
47 }
48 
49 std::string DiagnosticManager::GetString(char separator) {
50   std::string ret;
51 
52   for (const Diagnostic *diagnostic : Diagnostics()) {
53     ret.append(StringForSeverity(diagnostic->GetSeverity()));
54     ret.append(diagnostic->GetMessage());
55     ret.push_back(separator);
56   }
57 
58   return ret;
59 }
60 
61 size_t DiagnosticManager::Printf(DiagnosticSeverity severity,
62                                  const char *format, ...) {
63   StreamString ss;
64 
65   va_list args;
66   va_start(args, format);
67   size_t result = ss.PrintfVarArg(format, args);
68   va_end(args);
69 
70   AddDiagnostic(ss.GetData(), severity, eDiagnosticOriginLLDB);
71 
72   return result;
73 }
74 
75 size_t DiagnosticManager::PutCString(DiagnosticSeverity severity,
76                                      const char *cstr) {
77   if (!cstr)
78     return 0;
79   AddDiagnostic(cstr, severity, eDiagnosticOriginLLDB);
80   return strlen(cstr);
81 }
82