|
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,104 @@ 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 | +std::string RenderDiagnosticDetails(std::optional<uint16_t> offset_in_command, |
| 427 | + llvm::ArrayRef<DiagnosticDetail> details) { |
| 428 | + if (details.empty()) |
| 429 | + return {}; |
| 430 | + |
| 431 | + StreamString stream(true); |
| 432 | + if (!offset_in_command) { |
| 433 | + for (const DiagnosticDetail &detail : details) { |
| 434 | + PrintSeverity(stream, detail.severity); |
| 435 | + stream << detail.rendered << '\n'; |
| 436 | + } |
| 437 | + return stream.GetData(); |
| 438 | + } |
| 439 | + |
| 440 | + // Print a line with caret indicator(s) below the lldb prompt + command. |
| 441 | + const size_t padding = *offset_in_command; |
| 442 | + stream << std::string(padding, ' '); |
| 443 | + |
| 444 | + size_t offset = 1; |
| 445 | + std::vector<DiagnosticDetail> remaining_details, other_details; |
| 446 | + for (const DiagnosticDetail &detail : details) { |
| 447 | + if (!detail.source_location) { |
| 448 | + other_details.push_back(detail); |
| 449 | + continue; |
| 450 | + } |
| 451 | + |
| 452 | + auto &loc = *detail.source_location; |
| 453 | + remaining_details.push_back(detail); |
| 454 | + if (offset > loc.column) |
| 455 | + continue; |
| 456 | + stream << std::string(loc.column - offset, ' ') << '^'; |
| 457 | + if (loc.length > 1) |
| 458 | + stream << std::string(loc.length - 1, '~'); |
| 459 | + offset = loc.column + 1; |
| 460 | + } |
| 461 | + stream << '\n'; |
| 462 | + |
| 463 | + // Work through each detail in reverse order using the vector/stack. |
| 464 | + for (auto detail = remaining_details.rbegin(); |
| 465 | + detail != remaining_details.rend(); |
| 466 | + ++detail, remaining_details.pop_back()) { |
| 467 | + // Get the information to print this detail and remove it from the stack. |
| 468 | + // Print all the lines for all the other messages first. |
| 469 | + stream << std::string(padding, ' '); |
| 470 | + size_t offset = 1; |
| 471 | + for (auto &remaining_detail : |
| 472 | + llvm::ArrayRef(remaining_details).drop_back(1)) { |
| 473 | + uint16_t column = remaining_detail.source_location->column; |
| 474 | + stream << std::string(column - offset, ' ') << "│"; |
| 475 | + offset = column + 1; |
| 476 | + } |
| 477 | + |
| 478 | + // Print the line connecting the ^ with the error message. |
| 479 | + uint16_t column = detail->source_location->column; |
| 480 | + if (offset <= column) |
| 481 | + stream << std::string(column - offset, ' ') << "╰─ "; |
| 482 | + |
| 483 | + // Print a colorized string based on the message's severity type. |
| 484 | + PrintSeverity(stream, detail->severity); |
| 485 | + |
| 486 | + // Finally, print the message and start a new line. |
| 487 | + stream << detail->message << '\n'; |
| 488 | + } |
| 489 | + |
| 490 | + // Print the non-located details. |
| 491 | + for (const DiagnosticDetail &detail : other_details) { |
| 492 | + PrintSeverity(stream, detail.severity); |
| 493 | + stream << detail.message << '\n'; |
| 494 | + } |
| 495 | + |
| 496 | + return stream.GetData(); |
| 497 | +} |
| 498 | +} // namespace lldb_private |
| 499 | + |
401 | 500 | bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, |
402 | 501 | Stream &output_stream, |
403 | 502 | Stream &error_stream, |
@@ -486,19 +585,37 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr, |
486 | 585 |
|
487 | 586 | result.SetStatus(eReturnStatusSuccessFinishResult); |
488 | 587 | } 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(); |
498 | | - } else { |
499 | | - error_stream.PutCString("error: unknown error\n"); |
| 588 | + // Retrieve the diagnostics. |
| 589 | + std::vector<DiagnosticDetail> details; |
| 590 | + llvm::consumeError( |
| 591 | + llvm::handleErrors(result_valobj_sp->GetError().ToError(), |
| 592 | + [&](DetailedExpressionError &error) { |
| 593 | + details.push_back(error.GetDetail()); |
| 594 | + })); |
| 595 | + // Find the position of the expression in the command. |
| 596 | + std::optional<uint16_t> expr_pos; |
| 597 | + size_t nchar = m_original_command.find(expr); |
| 598 | + if (nchar != std::string::npos) |
| 599 | + expr_pos = nchar + GetDebugger().GetPrompt().size(); |
| 600 | + |
| 601 | + std::string error = RenderDiagnosticDetails(expr_pos, details); |
| 602 | + if (!error.empty()) |
| 603 | + error_stream.PutCString(error.c_str()); |
| 604 | + else { |
| 605 | + const char *error_cstr = result_valobj_sp->GetError().AsCString(); |
| 606 | + if (error_cstr && error_cstr[0]) { |
| 607 | + const size_t error_cstr_len = strlen(error_cstr); |
| 608 | + const bool ends_with_newline = |
| 609 | + error_cstr[error_cstr_len - 1] == '\n'; |
| 610 | + if (strstr(error_cstr, "error:") != error_cstr) |
| 611 | + error_stream.PutCString("error: "); |
| 612 | + error_stream.Write(error_cstr, error_cstr_len); |
| 613 | + if (!ends_with_newline) |
| 614 | + error_stream.EOL(); |
| 615 | + } else { |
| 616 | + error_stream.PutCString("error: unknown error\n"); |
| 617 | + } |
500 | 618 | } |
501 | | - |
502 | 619 | result.SetStatus(eReturnStatusFailed); |
503 | 620 | } |
504 | 621 | } |
|
0 commit comments