[clang][Diagnostics] Fix wrong line number display (#65238)
authorTakuya Shimizu <shimizu2486@gmail.com>
Tue, 5 Sep 2023 03:12:42 +0000 (12:12 +0900)
committerTobias Hieta <tobias@hieta.se>
Wed, 27 Sep 2023 15:44:34 +0000 (17:44 +0200)
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 ef5217b3c0dcbb58927fe43400b6d1faa677bf98)

clang/lib/Frontend/TextDiagnostic.cpp
clang/test/Misc/diag-style.cpp

index 3a3cc24..1b58261 100644 (file)
@@ -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);
index 3b24df9..626edef 100644 (file)
@@ -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: {{^}}      |
+