Skip to content

Commit 8046f15

Browse files
[lldb] Fix offset calculation when printing diagnostics in multiple ranges (#112466)
depends on #112451
1 parent 889e6ad commit 8046f15

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

lldb/source/Utility/DiagnosticsRendering.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,21 @@ void RenderDiagnosticDetails(Stream &stream,
112112
// Print a line with caret indicator(s) below the lldb prompt + command.
113113
const size_t padding = *offset_in_command;
114114
stream << std::string(padding, ' ');
115-
size_t offset = 1;
116-
for (const DiagnosticDetail &detail : remaining_details) {
117-
auto &loc = *detail.source_location;
118-
119-
if (offset > loc.column)
120-
continue;
121-
122-
stream << std::string(loc.column - offset, ' ') << cursor;
123-
for (unsigned i = 0; i + 1 < loc.length; ++i)
124-
stream << underline;
125-
offset = loc.column + 1;
115+
{
116+
size_t x_pos = 1;
117+
for (const DiagnosticDetail &detail : remaining_details) {
118+
auto &loc = *detail.source_location;
119+
120+
if (x_pos > loc.column)
121+
continue;
122+
123+
stream << std::string(loc.column - x_pos, ' ') << cursor;
124+
++x_pos;
125+
for (unsigned i = 0; i + 1 < loc.length; ++i) {
126+
stream << underline;
127+
++x_pos;
128+
}
129+
}
126130
}
127131
stream << '\n';
128132

@@ -134,19 +138,19 @@ void RenderDiagnosticDetails(Stream &stream,
134138
// Get the information to print this detail and remove it from the stack.
135139
// Print all the lines for all the other messages first.
136140
stream << std::string(padding, ' ');
137-
size_t offset = 1;
141+
size_t x_pos = 1;
138142
for (auto &remaining_detail :
139143
llvm::ArrayRef(remaining_details).drop_back(1)) {
140144
uint16_t column = remaining_detail.source_location->column;
141-
if (offset <= column)
142-
stream << std::string(column - offset, ' ') << vbar;
143-
offset = column + 1;
145+
if (x_pos <= column)
146+
stream << std::string(column - x_pos, ' ') << vbar;
147+
x_pos = column + 1;
144148
}
145149

146150
// Print the line connecting the ^ with the error message.
147151
uint16_t column = detail->source_location->column;
148-
if (offset <= column)
149-
stream << std::string(column - offset, ' ') << joint << hbar << spacer;
152+
if (x_pos <= column)
153+
stream << std::string(column - x_pos, ' ') << joint << hbar << spacer;
150154

151155
// Print a colorized string based on the message's severity type.
152156
PrintSeverity(stream, detail->severity);

lldb/unittests/Utility/DiagnosticsRenderingTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,22 @@ TEST_F(ErrorDisplayTest, RenderStatus) {
5757
DiagnosticDetail{loc1, eSeverityError, "Y", "Y"}});
5858
ASSERT_LT(StringRef(result).find("Y"), StringRef(result).find("X"));
5959
}
60+
{
61+
// Test that range diagnostics are emitted correctly.
62+
SourceLocation loc1 = {FileSpec{"a.c"}, 1, 1, 3, false, true};
63+
SourceLocation loc2 = {FileSpec{"a.c"}, 1, 5, 3, false, true};
64+
std::string result =
65+
Render({DiagnosticDetail{loc1, eSeverityError, "X", "X"},
66+
DiagnosticDetail{loc2, eSeverityError, "Y", "Y"}});
67+
auto lines = StringRef(result).split('\n');
68+
auto line1 = lines.first;
69+
lines = lines.second.split('\n');
70+
auto line2 = lines.first;
71+
lines = lines.second.split('\n');
72+
auto line3 = lines.first;
73+
// 1234567
74+
ASSERT_EQ(line1, "^~~ ^~~");
75+
ASSERT_EQ(line2, "| error: Y");
76+
ASSERT_EQ(line3, "error: X");
77+
}
6078
}

0 commit comments

Comments
 (0)