1212#include " lldb/lldb-defines.h"
1313#include " lldb/lldb-types.h"
1414
15+ #include " lldb/Utility/FileSpec.h"
16+ #include " lldb/Utility/Status.h"
17+
1518#include " llvm/ADT/STLExtras.h"
1619#include " llvm/ADT/StringRef.h"
1720
2023
2124namespace lldb_private {
2225
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+
2367enum DiagnosticOrigin {
2468 eDiagnosticOriginUnknown = 0 ,
2569 eDiagnosticOriginLLDB,
@@ -49,37 +93,28 @@ class Diagnostic {
4993 }
5094 }
5195
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) {}
6099
61100 virtual ~Diagnostic () = default ;
62101
63102 virtual bool HasFixIts () const { return false ; }
64103
65- lldb::Severity GetSeverity () const { return m_severity ; }
104+ lldb::Severity GetSeverity () const { return m_detail. severity ; }
66105
67106 uint32_t GetCompilerID () const { return m_compiler_id; }
68107
69- llvm::StringRef GetMessage () const { return m_message; }
108+ llvm::StringRef GetMessage () const { return m_detail.message ; }
109+ llvm::Error GetAsError () const ;
70110
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 );
77112
78113protected:
79- std::string m_message;
80- lldb::Severity m_severity;
81114 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;
83118};
84119
85120typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
@@ -102,10 +137,7 @@ class DiagnosticManager {
102137
103138 void AddDiagnostic (llvm::StringRef message, lldb::Severity severity,
104139 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);
109141
110142 void AddDiagnostic (std::unique_ptr<Diagnostic> diagnostic) {
111143 if (diagnostic)
@@ -130,13 +162,21 @@ class DiagnosticManager {
130162 m_diagnostics.back ()->AppendMessage (str);
131163 }
132164
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+
133173 // Returns a string containing errors in this format:
134174 //
135175 // "error: error text\n
136176 // warning: warning text\n
137177 // remark text\n"
138178 std::string GetString (char separator = ' \n ' );
139-
179+
140180 void Dump (Log *log);
141181
142182 const std::string &GetFixedExpression () { return m_fixed_expression; }
0 commit comments