Skip to content

Commit

Permalink
[clang][Diagnostics] Fix wrong line number display (#65238)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hazohelet committed Sep 5, 2023
1 parent 880f39a commit ef5217b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 1 addition & 2 deletions clang/lib/Frontend/TextDiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1160,8 +1160,7 @@ void TextDiagnostic::emitSnippetAndCaret(
// Find the set of lines to include.
const unsigned MaxLines = DiagOpts->SnippetLineLimit;
std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
unsigned DisplayLineNo =
Ranges.empty() ? Loc.getPresumedLoc().getLine() : ~0u;
unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
for (const auto &I : Ranges) {
if (auto OptionalRange = findLinesForRange(I, FID, SM))
Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
Expand Down
19 changes: 19 additions & 0 deletions clang/test/Misc/diag-style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,22 @@ void f(int x) {
// CHECK-NEXT: {{^}} |
// CHECK-NEXT: {{^}} |
// CHECK-NEXT: {{^}} 12 |

#line 10
int func(
int a, int b,
int&
r);

void test() {
func(3, 4, 5);
}
// CHECK: 10:5: note: candidate function not viable
// CHECK-NEXT: {{^}} 10 |
// CHECK-NEXT: {{^}} |
// CHECK-NEXT: {{^}} 11 |
// CHECK-NEXT: {{^}} 12 |
// CHECK-NEXT: {{^}} |
// CHECK-NEXT: {{^}} 13 |
// CHECK-NEXT: {{^}} |

0 comments on commit ef5217b

Please sign in to comment.