12
12
#include " lldb/lldb-defines.h"
13
13
#include " lldb/lldb-types.h"
14
14
15
+ #include " lldb/Utility/FileSpec.h"
16
+ #include " lldb/Utility/Status.h"
17
+
15
18
#include " llvm/ADT/STLExtras.h"
16
19
#include " llvm/ADT/StringRef.h"
17
20
20
23
21
24
namespace lldb_private {
22
25
26
+ // / A compiler-independent representation of a Diagnostic. Expression
27
+ // / evaluation failures often have more than one diagnostic that a UI
28
+ // / layer might want to render differently, for example to colorize
29
+ // / it.
30
+ // /
31
+ // / Running example:
32
+ // / (lldb) expr 1+foo
33
+ // / error: <user expression 0>:1:3: use of undeclared identifier 'foo'
34
+ // / 1+foo
35
+ // / ^
36
+ struct DiagnosticDetail {
37
+ struct SourceLocation {
38
+ FileSpec file;
39
+ unsigned line = 0 ;
40
+ uint16_t column = 0 ;
41
+ uint16_t length = 0 ;
42
+ bool in_user_input = false ;
43
+ };
44
+ // / Contains {{}, 1, 3, 3, true} in the example above.
45
+ std::optional<SourceLocation> source_location;
46
+ // / Contains eSeverityError in the example above.
47
+ lldb::Severity severity = lldb::eSeverityInfo;
48
+ // / Contains "use of undeclared identifier 'x'" in the example above.
49
+ std::string message;
50
+ // / Contains the fully rendered error message.
51
+ std::string rendered;
52
+ };
53
+
54
+ // / An llvm::Error used to communicate diagnostics in Status. Multiple
55
+ // / diagnostics may be chained in an llvm::ErrorList.
56
+ class DetailedExpressionError
57
+ : public llvm::ErrorInfo<DetailedExpressionError, llvm::ECError> {
58
+ DiagnosticDetail m_detail;
59
+
60
+ public:
61
+ using llvm::ErrorInfo<DetailedExpressionError, llvm::ECError>::ErrorInfo;
62
+ DetailedExpressionError (DiagnosticDetail detail) : m_detail(detail) {}
63
+ std::string message () const override ;
64
+ static char ID;
65
+ };
66
+
23
67
enum DiagnosticOrigin {
24
68
eDiagnosticOriginUnknown = 0 ,
25
69
eDiagnosticOriginLLDB,
@@ -49,37 +93,28 @@ class Diagnostic {
49
93
}
50
94
}
51
95
52
- Diagnostic (llvm::StringRef message, lldb::Severity severity,
53
- DiagnosticOrigin origin, uint32_t compiler_id)
54
- : m_message(message), m_severity(severity), m_origin(origin),
55
- m_compiler_id (compiler_id) {}
56
-
57
- Diagnostic (const Diagnostic &rhs)
58
- : m_message(rhs.m_message), m_severity(rhs.m_severity),
59
- m_origin(rhs.m_origin), m_compiler_id(rhs.m_compiler_id) {}
96
+ Diagnostic (DiagnosticOrigin origin, uint32_t compiler_id,
97
+ DiagnosticDetail detail)
98
+ : m_origin(origin), m_compiler_id(compiler_id), m_detail(detail) {}
60
99
61
100
virtual ~Diagnostic () = default ;
62
101
63
102
virtual bool HasFixIts () const { return false ; }
64
103
65
- lldb::Severity GetSeverity () const { return m_severity ; }
104
+ lldb::Severity GetSeverity () const { return m_detail. severity ; }
66
105
67
106
uint32_t GetCompilerID () const { return m_compiler_id; }
68
107
69
- llvm::StringRef GetMessage () const { return m_message; }
108
+ llvm::StringRef GetMessage () const { return m_detail.message ; }
109
+ llvm::Error GetAsError () const ;
70
110
71
- void AppendMessage (llvm::StringRef message,
72
- bool precede_with_newline = true ) {
73
- if (precede_with_newline)
74
- m_message.push_back (' \n ' );
75
- m_message += message;
76
- }
111
+ void AppendMessage (llvm::StringRef message, bool precede_with_newline = true );
77
112
78
113
protected:
79
- std::string m_message;
80
- lldb::Severity m_severity;
81
114
DiagnosticOrigin m_origin;
82
- uint32_t m_compiler_id; // Compiler-specific diagnostic ID
115
+ // / Compiler-specific diagnostic ID.
116
+ uint32_t m_compiler_id;
117
+ DiagnosticDetail m_detail;
83
118
};
84
119
85
120
typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
@@ -102,10 +137,7 @@ class DiagnosticManager {
102
137
103
138
void AddDiagnostic (llvm::StringRef message, lldb::Severity severity,
104
139
DiagnosticOrigin origin,
105
- uint32_t compiler_id = LLDB_INVALID_COMPILER_ID) {
106
- m_diagnostics.emplace_back (
107
- std::make_unique<Diagnostic>(message, severity, origin, compiler_id));
108
- }
140
+ uint32_t compiler_id = LLDB_INVALID_COMPILER_ID);
109
141
110
142
void AddDiagnostic (std::unique_ptr<Diagnostic> diagnostic) {
111
143
if (diagnostic)
@@ -130,13 +162,21 @@ class DiagnosticManager {
130
162
m_diagnostics.back ()->AppendMessage (str);
131
163
}
132
164
165
+ // / Copies the diagnostics into an llvm::Error{List}.
166
+ // / The first diagnostic wraps \c result.
167
+ llvm::Error GetAsError (lldb::ExpressionResults result) const ;
168
+
169
+ // / Copies the diagnostics into an llvm::Error, the first diagnostic
170
+ // / being an llvm::StringError.
171
+ llvm::Error GetAsError (llvm::Twine msg) const ;
172
+
133
173
// Returns a string containing errors in this format:
134
174
//
135
175
// "error: error text\n
136
176
// warning: warning text\n
137
177
// remark text\n"
138
178
std::string GetString (char separator = ' \n ' );
139
-
179
+
140
180
void Dump (Log *log);
141
181
142
182
const std::string &GetFixedExpression () { return m_fixed_expression; }
0 commit comments