Skip to content

[lldb] Improve rendering of inline diagnostics on the same column #116727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions lldb/source/Utility/DiagnosticsRendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "lldb/Utility/DiagnosticsRendering.h"
#include <cstdint>

using namespace lldb_private;
using namespace lldb;
Expand Down Expand Up @@ -98,7 +99,7 @@ void RenderDiagnosticDetails(Stream &stream,
}

// Sort the diagnostics.
auto sort = [](auto &ds) {
auto sort = [](std::vector<DiagnosticDetail> &ds) {
std::stable_sort(ds.begin(), ds.end(), [](auto &d1, auto &d2) {
auto l1 = d1.source_location.value_or(DiagnosticDetail::SourceLocation{});
auto l2 = d2.source_location.value_or(DiagnosticDetail::SourceLocation{});
Expand Down Expand Up @@ -130,6 +131,25 @@ void RenderDiagnosticDetails(Stream &stream,
}
stream << '\n';

// Reverse the order within groups of diagnostics that are on the same column.
auto group = [](const std::vector<DiagnosticDetail> &details) {
uint16_t column = 0;
std::vector<DiagnosticDetail> result, group;
for (auto &d : details) {
if (d.source_location->column == column) {
group.push_back(d);
continue;
}
result.insert(result.end(), group.rbegin(), group.rend());
group.clear();
column = d.source_location->column;
group.push_back(d);
}
result.insert(result.end(), group.rbegin(), group.rend());
return result;
};
remaining_details = group(remaining_details);

Copy link
Contributor

@felipepiovezan felipepiovezan Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a bit convoluted, and it is one of those rare cases where iterators are very helpful:

  auto group = [](llvm::ArrayRef<DiagnosticDetail> details) {
    std::vector<DiagnosticDetail> result;
    for (auto it = details.begin(), end = details.end(); it != end;) {
      auto eq_end = std::find_if(it, end, [&](const DiagnosticDetail &d) {
        return d.source_location->column != it->source_location->column;
      });
      std::reverse_copy(it, eq_end, std::back_inserter(result));
      it = eq_end;
    }
    return result;
  };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it's already been merged, nvm

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can still make the change.

// Work through each detail in reverse order using the vector/stack.
bool did_print = false;
for (auto detail = remaining_details.rbegin();
Expand All @@ -142,14 +162,19 @@ void RenderDiagnosticDetails(Stream &stream,
for (auto &remaining_detail :
llvm::ArrayRef(remaining_details).drop_back(1)) {
uint16_t column = remaining_detail.source_location->column;
if (x_pos <= column)
// Is this a note with the same column as another diagnostic?
if (column == detail->source_location->column)
continue;

if (column >= x_pos) {
stream << std::string(column - x_pos, ' ') << vbar;
x_pos = column + 1;
x_pos = column + 1;
}
}

// Print the line connecting the ^ with the error message.
uint16_t column = detail->source_location->column;
if (x_pos <= column)
// Print the line connecting the ^ with the error message.
if (column >= x_pos)
stream << std::string(column - x_pos, ' ') << joint << hbar << spacer;

// Print a colorized string based on the message's severity type.
Expand Down
49 changes: 24 additions & 25 deletions lldb/unittests/Utility/DiagnosticsRenderingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,22 @@ TEST_F(ErrorDisplayTest, RenderStatus) {
{
// Test that diagnostics on the same column can be handled and all
// three errors are diagnosed.
SourceLocation loc1 = {FileSpec{"a.c"}, 13, 11, 0, false, true};
SourceLocation loc2 = {FileSpec{"a.c"}, 13, 13, 0, false, true};
SourceLocation loc1 = {FileSpec{"a.c"}, 13, 5, 0, false, true};
SourceLocation loc2 = {FileSpec{"a.c"}, 13, 7, 0, false, true};
SourceLocation loc3 = {FileSpec{"a.c"}, 13, 9, 0, false, true};
std::string result =
Render({DiagnosticDetail{loc1, eSeverityError, "1", "1"},
DiagnosticDetail{loc1, eSeverityError, "2", "2"},
DiagnosticDetail{loc2, eSeverityError, "3", "3"}});
ASSERT_TRUE(StringRef(result).contains("error: 1"));
ASSERT_TRUE(StringRef(result).contains("error: 2"));
ASSERT_TRUE(StringRef(result).contains("error: 3"));
DiagnosticDetail{loc2, eSeverityError, "2a", "2a"},
DiagnosticDetail{loc2, eSeverityInfo, "2b", "2b"},
DiagnosticDetail{loc3, eSeverityError, "3", "3"}});
llvm::SmallVector<StringRef> lines;
StringRef(result).split(lines, '\n');
// 1234567890123
ASSERT_EQ(lines[0], " ^ ^ ^");
ASSERT_EQ(lines[1], " | | error: 3");
ASSERT_EQ(lines[2], " | error: 2a");
ASSERT_EQ(lines[3], " | note: 2b");
ASSERT_EQ(lines[4], " error: 1");
}
{
// Test that diagnostics in reverse order are emitted correctly.
Expand Down Expand Up @@ -68,16 +75,12 @@ TEST_F(ErrorDisplayTest, RenderStatus) {
std::string result =
Render({DiagnosticDetail{loc1, eSeverityError, "X", "X"},
DiagnosticDetail{loc2, eSeverityError, "Y", "Y"}});
auto lines = StringRef(result).split('\n');
auto line1 = lines.first;
lines = lines.second.split('\n');
auto line2 = lines.first;
lines = lines.second.split('\n');
auto line3 = lines.first;
llvm::SmallVector<StringRef> lines;
StringRef(result).split(lines, '\n');
// 1234567
ASSERT_EQ(line1, "^~~ ^~~");
ASSERT_EQ(line2, "| error: Y");
ASSERT_EQ(line3, "error: X");
ASSERT_EQ(lines[0], "^~~ ^~~");
ASSERT_EQ(lines[1], "| error: Y");
ASSERT_EQ(lines[2], "error: X");
}
{
// Test diagnostics on the same line are emitted correctly.
Expand All @@ -86,15 +89,11 @@ TEST_F(ErrorDisplayTest, RenderStatus) {
std::string result =
Render({DiagnosticDetail{loc1, eSeverityError, "X", "X"},
DiagnosticDetail{loc2, eSeverityError, "Y", "Y"}});
auto lines = StringRef(result).split('\n');
auto line1 = lines.first;
lines = lines.second.split('\n');
auto line2 = lines.first;
lines = lines.second.split('\n');
auto line3 = lines.first;
llvm::SmallVector<StringRef> lines;
StringRef(result).split(lines, '\n');
// 1234567
ASSERT_EQ(line1, " ^ ^");
ASSERT_EQ(line2, " | error: Y");
ASSERT_EQ(line3, " error: X");
ASSERT_EQ(lines[0], " ^ ^");
ASSERT_EQ(lines[1], " | error: Y");
ASSERT_EQ(lines[2], " error: X");
}
}
Loading