LLDB mainline
DiagnosticManager.h
Go to the documentation of this file.
1//===-- DiagnosticManager.h -------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_EXPRESSION_DIAGNOSTICMANAGER_H
10#define LLDB_EXPRESSION_DIAGNOSTICMANAGER_H
11
12#include "lldb/lldb-defines.h"
13#include "lldb/lldb-types.h"
14
17#include "lldb/Utility/Status.h"
18
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/StringRef.h"
21
22#include <string>
23#include <vector>
24
25namespace lldb_private {
26
27/// An llvm::Error used to communicate diagnostics in Status. Multiple
28/// diagnostics may be chained in an llvm::ErrorList.
30 : public llvm::ErrorInfo<ExpressionError, DiagnosticError> {
31 std::string m_message;
32 std::vector<DiagnosticDetail> m_details;
33
34public:
35 static char ID;
36 using llvm::ErrorInfo<ExpressionError, DiagnosticError>::ErrorInfo;
37 ExpressionError(lldb::ExpressionResults result, std::string msg,
38 std::vector<DiagnosticDetail> details = {});
39 std::string message() const override;
40 llvm::ArrayRef<DiagnosticDetail> GetDetails() const override {
41 return m_details;
42 }
43 std::error_code convertToErrorCode() const override;
44 void log(llvm::raw_ostream &OS) const override;
45 std::unique_ptr<CloneableError> Clone() const override;
46};
47
54};
55
57
59 friend class DiagnosticManager;
60
61public:
62 DiagnosticOrigin getKind() const { return m_origin; }
63
64 static bool classof(const Diagnostic *diag) {
65 DiagnosticOrigin kind = diag->getKind();
66 switch (kind) {
70 return true;
73 return false;
74 }
75 }
76
77 Diagnostic(DiagnosticOrigin origin, uint32_t compiler_id,
78 DiagnosticDetail detail)
79 : m_origin(origin), m_compiler_id(compiler_id), m_detail(detail) {}
80
81 virtual ~Diagnostic() = default;
82
83 virtual bool HasFixIts() const { return false; }
84
86
87 uint32_t GetCompilerID() const { return m_compiler_id; }
88
89 llvm::StringRef GetMessage() const { return m_detail.message; }
90 const DiagnosticDetail &GetDetail() const { return m_detail; }
91
92 void AppendMessage(llvm::StringRef message, bool precede_with_newline = true);
93
94protected:
96 /// Compiler-specific diagnostic ID.
97 uint32_t m_compiler_id;
99};
100
101typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
102
104public:
105 void Clear() {
106 m_diagnostics.clear();
107 m_fixed_expression.clear();
108 }
109
111
112 bool HasFixIts() const {
113 return llvm::any_of(m_diagnostics,
114 [](const std::unique_ptr<Diagnostic> &diag) {
115 return diag->HasFixIts();
116 });
117 }
118
119 void AddDiagnostic(llvm::StringRef message, lldb::Severity severity,
120 DiagnosticOrigin origin,
121 uint32_t compiler_id = LLDB_INVALID_COMPILER_ID);
122
123 void AddDiagnostic(std::unique_ptr<Diagnostic> diagnostic) {
124 if (diagnostic)
125 m_diagnostics.push_back(std::move(diagnostic));
126 }
127
128 /// Moves over the contents of a second diagnostic manager over. Leaves other
129 /// diagnostic manager in an empty state.
131 std::move(other.m_diagnostics.begin(), other.m_diagnostics.end(),
132 std::back_inserter(m_diagnostics));
133 m_fixed_expression = std::move(other.m_fixed_expression);
134 other.Clear();
135 }
136
137 size_t Printf(lldb::Severity severity, const char *format, ...)
138 __attribute__((format(printf, 3, 4)));
139 void PutString(lldb::Severity severity, llvm::StringRef str);
140
141 void AppendMessageToDiagnostic(llvm::StringRef str) {
142 if (!m_diagnostics.empty())
143 m_diagnostics.back()->AppendMessage(str);
144 }
145
146 /// Returns an \ref ExpressionError with \c arg as error code.
147 llvm::Error GetAsError(lldb::ExpressionResults result,
148 llvm::Twine message = {}) const;
149
150 // Returns a string containing errors in this format:
151 //
152 // "error: error text\n
153 // warning: warning text\n
154 // remark text\n"
155 std::string GetString(char separator = '\n');
156
157 void Dump(Log *log);
158
159 const std::string &GetFixedExpression() { return m_fixed_expression; }
160
161 // Moves fixed_expression to the internal storage.
162 void SetFixedExpression(std::string fixed_expression) {
163 m_fixed_expression = std::move(fixed_expression);
164 }
165
166protected:
169};
170}
171
172#endif // LLDB_EXPRESSION_DIAGNOSTICMANAGER_H
size_t void PutString(lldb::Severity severity, llvm::StringRef str)
void Consume(DiagnosticManager &&other)
Moves over the contents of a second diagnostic manager over.
llvm::Error GetAsError(lldb::ExpressionResults result, llvm::Twine message={}) const
Returns an ExpressionError with arg as error code.
void SetFixedExpression(std::string fixed_expression)
void AppendMessageToDiagnostic(llvm::StringRef str)
void AddDiagnostic(std::unique_ptr< Diagnostic > diagnostic)
std::string GetString(char separator='\n')
const DiagnosticList & Diagnostics()
void AddDiagnostic(llvm::StringRef message, lldb::Severity severity, DiagnosticOrigin origin, uint32_t compiler_id=LLDB_INVALID_COMPILER_ID)
size_t Printf(lldb::Severity severity, const char *format,...) __attribute__((format(printf
const std::string & GetFixedExpression()
const DiagnosticDetail & GetDetail() const
Diagnostic(DiagnosticOrigin origin, uint32_t compiler_id, DiagnosticDetail detail)
virtual ~Diagnostic()=default
uint32_t m_compiler_id
Compiler-specific diagnostic ID.
uint32_t GetCompilerID() const
void AppendMessage(llvm::StringRef message, bool precede_with_newline=true)
virtual bool HasFixIts() const
lldb::Severity GetSeverity() const
DiagnosticOrigin getKind() const
llvm::StringRef GetMessage() const
static bool classof(const Diagnostic *diag)
An llvm::Error used to communicate diagnostics in Status.
std::vector< DiagnosticDetail > m_details
void log(llvm::raw_ostream &OS) const override
std::unique_ptr< CloneableError > Clone() const override
llvm::ArrayRef< DiagnosticDetail > GetDetails() const override
std::error_code convertToErrorCode() const override
std::string message() const override
#define UINT32_MAX
Definition: lldb-defines.h:19
A class that represents a running process on the host machine.
const uint32_t LLDB_INVALID_COMPILER_ID
std::vector< std::unique_ptr< Diagnostic > > DiagnosticList
Definition: SBAddress.h:15
Severity
Used for expressing severity in logs and diagnostics.
ExpressionResults
The results of expression evaluation.
Definition: Debugger.h:54
A compiler-independent representation of an lldb_private::Diagnostic.
lldb::Severity severity
Contains eSeverityError in the example above.
std::string message
Contains "use of undeclared identifier 'foo'" in the example above.