DiagnosticManager.cpp revision 321369
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/Utility/Log.h"
15#include "lldb/Utility/StreamString.h"
16
17using namespace lldb_private;
18
19void 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
36static 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
49std::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
61size_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.GetString(), severity, eDiagnosticOriginLLDB);
71
72  return result;
73}
74
75size_t DiagnosticManager::PutString(DiagnosticSeverity severity,
76                                    llvm::StringRef str) {
77  if (str.empty())
78    return 0;
79  AddDiagnostic(str, severity, eDiagnosticOriginLLDB);
80  return str.size();
81}
82
83void DiagnosticManager::CopyDiagnostics(DiagnosticManager &otherDiagnostics) {
84  for (const DiagnosticList::value_type &other_diagnostic:
85       otherDiagnostics.Diagnostics()) {
86    AddDiagnostic(
87        other_diagnostic->GetMessage(), other_diagnostic->GetSeverity(),
88        other_diagnostic->getKind(), other_diagnostic->GetCompilerID());
89  }
90}
91