|
10 | 10 |
|
11 | 11 | #include "CommandObjectExpression.h"
|
12 | 12 | #include "lldb/Core/Debugger.h"
|
| 13 | +#include "lldb/Expression/DiagnosticManager.h" |
13 | 14 | #include "lldb/Expression/ExpressionVariable.h"
|
14 | 15 | #include "lldb/Expression/REPL.h"
|
15 | 16 | #include "lldb/Expression/UserExpression.h"
|
@@ -398,6 +399,122 @@ CanBeUsedForElementCountPrinting(ValueObject &valobj) {
|
398 | 399 | return Status();
|
399 | 400 | }
|
400 | 401 |
|
| 402 | +static llvm::raw_ostream &PrintSeverity(Stream &stream, |
| 403 | + lldb::Severity severity) { |
| 404 | + llvm::HighlightColor color; |
| 405 | + llvm::StringRef text; |
| 406 | + switch (severity) { |
| 407 | + case eSeverityError: |
| 408 | + color = llvm::HighlightColor::Error; |
| 409 | + text = "error: "; |
| 410 | + break; |
| 411 | + case eSeverityWarning: |
| 412 | + color = llvm::HighlightColor::Warning; |
| 413 | + text = "warning: "; |
| 414 | + break; |
| 415 | + case eSeverityInfo: |
| 416 | + color = llvm::HighlightColor::Remark; |
| 417 | + text = "note: "; |
| 418 | + break; |
| 419 | + } |
| 420 | + return llvm::WithColor(stream.AsRawOstream(), color, llvm::ColorMode::Enable) |
| 421 | + << text; |
| 422 | +} |
| 423 | + |
| 424 | +namespace lldb_private { |
| 425 | +// Public for unittesting. |
| 426 | +void RenderDiagnosticDetails(Stream &stream, |
| 427 | + std::optional<uint16_t> offset_in_command, |
| 428 | + bool show_inline, |
| 429 | + llvm::ArrayRef<DiagnosticDetail> details) { |
| 430 | + if (details.empty()) |
| 431 | + return; |
| 432 | + |
| 433 | + if (!offset_in_command) { |
| 434 | + for (const DiagnosticDetail &detail : details) { |
| 435 | + PrintSeverity(stream, detail.severity); |
| 436 | + stream << detail.rendered << '\n'; |
| 437 | + } |
| 438 | + return; |
| 439 | + } |
| 440 | + |
| 441 | + // Print a line with caret indicator(s) below the lldb prompt + command. |
| 442 | + const size_t padding = *offset_in_command; |
| 443 | + stream << std::string(padding, ' '); |
| 444 | + |
| 445 | + size_t offset = 1; |
| 446 | + std::vector<DiagnosticDetail> remaining_details, other_details, |
| 447 | + hidden_details; |
| 448 | + for (const DiagnosticDetail &detail : details) { |
| 449 | + if (!show_inline || !detail.source_location) { |
| 450 | + other_details.push_back(detail); |
| 451 | + continue; |
| 452 | + } |
| 453 | + if (detail.source_location->hidden) { |
| 454 | + hidden_details.push_back(detail); |
| 455 | + continue; |
| 456 | + } |
| 457 | + if (!detail.source_location->in_user_input) { |
| 458 | + other_details.push_back(detail); |
| 459 | + continue; |
| 460 | + } |
| 461 | + |
| 462 | + auto &loc = *detail.source_location; |
| 463 | + remaining_details.push_back(detail); |
| 464 | + if (offset > loc.column) |
| 465 | + continue; |
| 466 | + stream << std::string(loc.column - offset, ' ') << '^'; |
| 467 | + if (loc.length > 1) |
| 468 | + stream << std::string(loc.length - 1, '~'); |
| 469 | + offset = loc.column + 1; |
| 470 | + } |
| 471 | + stream << '\n'; |
| 472 | + |
| 473 | + // Work through each detail in reverse order using the vector/stack. |
| 474 | + bool did_print = false; |
| 475 | + for (auto detail = remaining_details.rbegin(); |
| 476 | + detail != remaining_details.rend(); |
| 477 | + ++detail, remaining_details.pop_back()) { |
| 478 | + // Get the information to print this detail and remove it from the stack. |
| 479 | + // Print all the lines for all the other messages first. |
| 480 | + stream << std::string(padding, ' '); |
| 481 | + size_t offset = 1; |
| 482 | + for (auto &remaining_detail : |
| 483 | + llvm::ArrayRef(remaining_details).drop_back(1)) { |
| 484 | + uint16_t column = remaining_detail.source_location->column; |
| 485 | + stream << std::string(column - offset, ' ') << "│"; |
| 486 | + offset = column + 1; |
| 487 | + } |
| 488 | + |
| 489 | + // Print the line connecting the ^ with the error message. |
| 490 | + uint16_t column = detail->source_location->column; |
| 491 | + if (offset <= column) |
| 492 | + stream << std::string(column - offset, ' ') << "╰─ "; |
| 493 | + |
| 494 | + // Print a colorized string based on the message's severity type. |
| 495 | + PrintSeverity(stream, detail->severity); |
| 496 | + |
| 497 | + // Finally, print the message and start a new line. |
| 498 | + stream << detail->message << '\n'; |
| 499 | + did_print = true; |
| 500 | + } |
| 501 | + |
| 502 | + // Print the non-located details. |
| 503 | + for (const DiagnosticDetail &detail : other_details) { |
| 504 | + PrintSeverity(stream, detail.severity); |
| 505 | + stream << detail.rendered << '\n'; |
| 506 | + did_print = true; |
| 507 | + } |
| 508 | + |
| 509 | + // Print the hidden details as a last resort. |
| 510 | + if (!did_print) |
| 511 | + for (const DiagnosticDetail &detail : hidden_details) { |
| 512 | + PrintSeverity(stream, detail.severity); |
| 513 | + stream << detail.rendered << '\n'; |
| 514 | + } |
| 515 | +} |
| 516 | +} // namespace lldb_private |
| 517 | + |
401 | 518 | bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
|
402 | 519 | Stream &output_stream,
|
403 | 520 | Stream &error_stream,
|
@@ -486,19 +603,34 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
|
486 | 603 |
|
487 | 604 | result.SetStatus(eReturnStatusSuccessFinishResult);
|
488 | 605 | } else {
|
489 |
| - const char *error_cstr = result_valobj_sp->GetError().AsCString(); |
490 |
| - if (error_cstr && error_cstr[0]) { |
491 |
| - const size_t error_cstr_len = strlen(error_cstr); |
492 |
| - const bool ends_with_newline = error_cstr[error_cstr_len - 1] == '\n'; |
493 |
| - if (strstr(error_cstr, "error:") != error_cstr) |
494 |
| - error_stream.PutCString("error: "); |
495 |
| - error_stream.Write(error_cstr, error_cstr_len); |
496 |
| - if (!ends_with_newline) |
497 |
| - error_stream.EOL(); |
| 606 | + // Retrieve the diagnostics. |
| 607 | + std::vector<DiagnosticDetail> details; |
| 608 | + llvm::consumeError(llvm::handleErrors( |
| 609 | + result_valobj_sp->GetError().ToError(), |
| 610 | + [&](ExpressionError &error) { details = error.GetDetails(); })); |
| 611 | + // Find the position of the expression in the command. |
| 612 | + std::optional<uint16_t> expr_pos; |
| 613 | + size_t nchar = m_original_command.find(expr); |
| 614 | + if (nchar != std::string::npos) |
| 615 | + expr_pos = nchar + GetDebugger().GetPrompt().size(); |
| 616 | + |
| 617 | + if (!details.empty()) { |
| 618 | + bool show_inline = |
| 619 | + GetDebugger().GetShowInlineDiagnostics() && !expr.contains('\n'); |
| 620 | + RenderDiagnosticDetails(error_stream, expr_pos, show_inline, details); |
498 | 621 | } else {
|
499 |
| - error_stream.PutCString("error: unknown error\n"); |
| 622 | + const char *error_cstr = result_valobj_sp->GetError().AsCString(); |
| 623 | + llvm::StringRef error(error_cstr); |
| 624 | + if (!error.empty()) { |
| 625 | + if (!error.starts_with("error:")) |
| 626 | + error_stream << "error: "; |
| 627 | + error_stream << error; |
| 628 | + if (!error.ends_with('\n')) |
| 629 | + error_stream.EOL(); |
| 630 | + } else { |
| 631 | + error_stream << "error: unknown error\n"; |
| 632 | + } |
500 | 633 | }
|
501 |
| - |
502 | 634 | result.SetStatus(eReturnStatusFailed);
|
503 | 635 | }
|
504 | 636 | }
|
|
0 commit comments