[clang] [clang][Diagnostics] Fix wrong line number display (PR #65238)

Takuya Shimizu via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 4 09:00:48 PDT 2023


https://github.com/hazohelet updated https://github.com/llvm/llvm-project/pull/65238:

>From 2bc13f3b013a4bda4364e9c29bc792ca6a0c7cf1 Mon Sep 17 00:00:00 2001
From: Takuya Shimizu <shimizu2486 at gmail.com>
Date: Mon, 4 Sep 2023 10:46:17 +0900
Subject: [PATCH 1/2] [clang][Diagnostics] Fix wrong line number display

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
---
 clang/lib/Frontend/TextDiagnostic.cpp |  3 +--
 clang/test/Misc/diag-style.cpp        | 17 +++++++++++++++++
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp
index 3a3cc246d3afc2a..1b58261b22a2653 100644
--- a/clang/lib/Frontend/TextDiagnostic.cpp
+++ b/clang/lib/Frontend/TextDiagnostic.cpp
@@ -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);
diff --git a/clang/test/Misc/diag-style.cpp b/clang/test/Misc/diag-style.cpp
index 3b24df974730a80..967b51a07b6a65d 100644
--- a/clang/test/Misc/diag-style.cpp
+++ b/clang/test/Misc/diag-style.cpp
@@ -24,3 +24,20 @@ 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 |



More information about the cfe-commits mailing list