Skip to content

Commit 07d0d15

Browse files
committed
Update error message handling to use DiagnosticDetails.
1 parent 33a9971 commit 07d0d15

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lldb/include/lldb/ValueObject/DILParser.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ enum class ErrorCode : unsigned char {
2828
kUnknown,
2929
};
3030

31-
std::string FormatDiagnostics(llvm::StringRef input_expr,
32-
const std::string &message, uint32_t loc);
31+
std::string NewFormatDiagnostics(llvm::StringRef input_expr,
32+
const std::string &message, uint32_t loc,
33+
uint16_t err_len);
34+
35+
std::string OldFormatDiagnostics(llvm::StringRef input_expr,
36+
const std::string &message, uint32_t loc);
3337

3438
/// Pure recursive descent parser for C++ like expressions.
3539
/// EBNF grammar for the parser is described in lldb/docs/dil-expr-lang.ebnf

lldb/source/ValueObject/DILEval.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,8 @@ Interpreter::Visit(const IdentifierNode *node) {
237237
llvm::formatv("use of undeclared identifier '{0}'", node->GetName());
238238
Status error = Status(
239239
(uint32_t)ErrorCode::kUndeclaredIdentifier, lldb::eErrorTypeGeneric,
240-
FormatDiagnostics(m_expr, errMsg, node->GetLocation()));
240+
NewFormatDiagnostics(m_expr, errMsg, node->GetLocation(),
241+
node->GetName().size()));
241242
return error.ToError();
242243
}
243244
lldb::ValueObjectSP val;

lldb/source/ValueObject/DILParser.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,49 @@
1313

1414
#include "lldb/ValueObject/DILParser.h"
1515
#include "lldb/Target/ExecutionContextScope.h"
16+
#include "lldb/Utility/DiagnosticsRendering.h"
1617
#include "lldb/ValueObject/DILAST.h"
1718
#include "lldb/ValueObject/DILEval.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include "llvm/Support/FormatAdapters.h"
21+
#include <cstdlib>
2022
#include <limits.h>
2123
#include <memory>
2224
#include <sstream>
23-
#include <cstdlib>
2425
#include <string>
2526

2627
namespace lldb_private::dil {
2728

28-
std::string FormatDiagnostics(llvm::StringRef text, const std::string &message,
29-
uint32_t loc) {
29+
std::string NewFormatDiagnostics(llvm::StringRef text,
30+
const std::string &message, uint32_t loc,
31+
uint16_t err_len) {
32+
DiagnosticDetail::SourceLocation sloc = {
33+
FileSpec{}, /*line=*/1, loc + 1, err_len, false, /*in_user_input=*/true};
34+
std::string arrow_str = "^";
35+
std::string rendered_msg =
36+
llvm::formatv("<user expression 0>:1:{0}: {1}\n 1 | {2}\n | ^",
37+
loc + 1, message, text);
38+
DiagnosticDetail detail;
39+
detail.source_location = sloc;
40+
detail.severity = lldb::eSeverityError;
41+
detail.message = message;
42+
detail.rendered = rendered_msg;
43+
std::vector<DiagnosticDetail> diagnostics;
44+
diagnostics.push_back(detail);
45+
StreamString diag_stream(true);
46+
RenderDiagnosticDetails(diag_stream, 7, true, diagnostics);
47+
std::string ret_str = text.str() + "\n" + diag_stream.GetString().str();
48+
return ret_str;
49+
}
50+
51+
std::string OldFormatDiagnostics(llvm::StringRef text,
52+
const std::string &message, uint32_t loc) {
3053
// Get the position, in the current line of text, of the diagnostics pointer.
3154
// ('loc' is the location of the start of the current token/error within the
3255
// overall text line).
3356
int32_t arrow = loc + 1; // Column offset starts at 1, not 0.
3457

35-
return llvm::formatv("<expr:1:{0}>: {1}\n{2}\n{3}", loc, message,
58+
return llvm::formatv("<expr:1:{0}>: {1}\n{2}\n{3}", loc + 1, message,
3659
llvm::fmt_pad(text, 0, 0),
3760
llvm::fmt_pad("^", arrow - 1, 0));
3861
}
@@ -234,7 +257,8 @@ void DILParser::BailOut(ErrorCode code, const std::string &error,
234257
}
235258

236259
m_error = Status((uint32_t)code, lldb::eErrorTypeGeneric,
237-
FormatDiagnostics(m_input_expr, error, loc));
260+
NewFormatDiagnostics(m_input_expr, error, loc,
261+
CurToken().GetSpelling().length()));
238262
// Advance the lexer token index to the end of the lexed tokens vector.
239263
m_dil_lexer.ResetTokenIdx(m_dil_lexer.NumLexedTokens() - 1);
240264
}

0 commit comments

Comments
 (0)