[llvm-branch-commits] [clang] 178cf5b - [clang][Diagnostics] Fix wrong line number display (#65238)

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Sep 27 08:54:38 PDT 2023


Author: Takuya Shimizu
Date: 2023-09-27T17:44:34+02:00
New Revision: 178cf5bc8732a010f02e9a262f08afc549a1bc1d

URL: https://github.com/llvm/llvm-project/commit/178cf5bc8732a010f02e9a262f08afc549a1bc1d
DIFF: https://github.com/llvm/llvm-project/commit/178cf5bc8732a010f02e9a262f08afc549a1bc1d.diff

LOG: [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 ef5217b3c0dcbb58927fe43400b6d1faa677bf98)

Added: 
    

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

Removed: 
    


################################################################################
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..626edef9b7e6149 100644
--- a/clang/test/Misc/diag-style.cpp
+++ b/clang/test/Misc/diag-style.cpp
@@ -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: {{^}}      |
+


        


More information about the llvm-branch-commits mailing list