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 customizable "detail" for an error. For example, expression
27+ // / evaluation failures often have more than one diagnostic that the
28+ // / UI layer might want to render differently.
29+ // /
30+ // / Running example:
31+ // / (lldb) expr 1+foo
32+ // / error: <user expression 0>:1:3: use of undeclared identifier 'foo'
33+ // / 1+foo
34+ // / ^
35+ struct DiagnosticDetail {
36+ struct SourceLocation {
37+ FileSpec file;
38+ unsigned line = 0 ;
39+ uint16_t column = 0 ;
40+ uint16_t length = 0 ;
41+ bool in_user_input = false ;
42+ };
43+ // / Contains {{}, 1, 3, 3, true} in the example above.
44+ std::optional<SourceLocation> source_location;
45+ // / Contains eSeverityError in the example above.
46+ lldb::Severity severity = lldb::eSeverityInfo;
47+ // / Contains "use of undeclared identifier 'x'" in the example above.
48+ std::string message;
49+ // / Contains the fully rendered error message.
50+ std::string rendered;
51+ };
52+
53+ // / An llvm::Error used to communicate diagnostics in Status. Multiple
54+ // / diagnostics may be chained in an llvm::ErrorList.
55+ class DetailedExpressionError
56+ : public llvm::ErrorInfo<DetailedExpressionError, llvm::ECError> {
57+ DiagnosticDetail m_detail;
58+
59+ public:
60+ using llvm::ErrorInfo<DetailedExpressionError, llvm::ECError>::ErrorInfo;
61+ DetailedExpressionError (DiagnosticDetail detail) {}
62+ std::string message () const override ;
63+ static char ID;
64+ };
65+
2366enum DiagnosticOrigin {
2467 eDiagnosticOriginUnknown = 0 ,
2568 eDiagnosticOriginLLDB,
@@ -49,37 +92,28 @@ class Diagnostic {
4992 }
5093 }
5194
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) {}
95+ Diagnostic (DiagnosticOrigin origin, uint32_t compiler_id,
96+ DiagnosticDetail detail)
97+ : m_origin(origin), m_compiler_id(compiler_id), m_detail(detail) {}
6098
6199 virtual ~Diagnostic () = default ;
62100
63101 virtual bool HasFixIts () const { return false ; }
64102
65- lldb::Severity GetSeverity () const { return m_severity ; }
103+ lldb::Severity GetSeverity () const { return m_detail. severity ; }
66104
67105 uint32_t GetCompilerID () const { return m_compiler_id; }
68106
69- llvm::StringRef GetMessage () const { return m_message; }
107+ llvm::StringRef GetMessage () const { return m_detail.message ; }
108+ llvm::Error GetAsError () const ;
70109
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- }
110+ void AppendMessage (llvm::StringRef message, bool precede_with_newline = true );
77111
78112protected:
79- std::string m_message;
80- lldb::Severity m_severity;
81113 DiagnosticOrigin m_origin;
82- uint32_t m_compiler_id; // Compiler-specific diagnostic ID
114+ // / Compiler-specific diagnostic ID.
115+ uint32_t m_compiler_id;
116+ DiagnosticDetail m_detail;
83117};
84118
85119typedef std::vector<std::unique_ptr<Diagnostic>> DiagnosticList;
@@ -102,10 +136,7 @@ class DiagnosticManager {
102136
103137 void AddDiagnostic (llvm::StringRef message, lldb::Severity severity,
104138 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- }
139+ uint32_t compiler_id = LLDB_INVALID_COMPILER_ID);
109140
110141 void AddDiagnostic (std::unique_ptr<Diagnostic> diagnostic) {
111142 if (diagnostic)
@@ -130,13 +161,17 @@ class DiagnosticManager {
130161 m_diagnostics.back ()->AppendMessage (str);
131162 }
132163
133- // Returns a string containing errors in this format:
134- //
135- // "error: error text\n
136- // warning: warning text\n
137- // remark text\n"
164+ // / Returns a string containing errors in this format:
165+ // /
166+ // / "error: error text\n
167+ // / warning: warning text\n
168+ // / remark text\n"
169+ LLVM_DEPRECATED (" Use GetAsStatus instead" , " GetAsStatus()" )
138170 std::string GetString (char separator = ' \n ' );
139171
172+ // / Copies the diagnostics into an llvm::Error.
173+ llvm::Error GetAsError (lldb::ExpressionResults result) const ;
174+
140175 void Dump (Log *log);
141176
142177 const std::string &GetFixedExpression () { return m_fixed_expression; }
0 commit comments