Skip to content

Commit 178cf5b

Browse files
hazohelettru
authored andcommitted
[clang][Diagnostics] Fix wrong line number display (#65238)
When the caret location is lower than the lowest source range, clang is printing wrong line numbers. The first line number should consider caret location line even when there are source ranges provided. Current wrong line example: https://godbolt.org/z/aj4qEjzs4 (cherry picked from commit ef5217b)
1 parent 25a150b commit 178cf5b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,8 +1160,7 @@ void TextDiagnostic::emitSnippetAndCaret(
11601160
// Find the set of lines to include.
11611161
const unsigned MaxLines = DiagOpts->SnippetLineLimit;
11621162
std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
1163-
unsigned DisplayLineNo =
1164-
Ranges.empty() ? Loc.getPresumedLoc().getLine() : ~0u;
1163+
unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
11651164
for (const auto &I : Ranges) {
11661165
if (auto OptionalRange = findLinesForRange(I, FID, SM))
11671166
Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);

clang/test/Misc/diag-style.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,22 @@ void f(int x) {
2424
// CHECK-NEXT: {{^}} |
2525
// CHECK-NEXT: {{^}} |
2626
// CHECK-NEXT: {{^}} 12 |
27+
28+
#line 10
29+
int func(
30+
int a, int b,
31+
int&
32+
r);
33+
34+
void test() {
35+
func(3, 4, 5);
36+
}
37+
// CHECK: 10:5: note: candidate function not viable
38+
// CHECK-NEXT: {{^}} 10 |
39+
// CHECK-NEXT: {{^}} |
40+
// CHECK-NEXT: {{^}} 11 |
41+
// CHECK-NEXT: {{^}} 12 |
42+
// CHECK-NEXT: {{^}} |
43+
// CHECK-NEXT: {{^}} 13 |
44+
// CHECK-NEXT: {{^}} |
45+

0 commit comments

Comments
 (0)