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

via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 4 20:12:46 PDT 2023


Author: Takuya Shimizu
Date: 2023-09-05T12:12:42+09:00
New Revision: ef5217b3c0dcbb58927fe43400b6d1faa677bf98

URL: https://github.com/llvm/llvm-project/commit/ef5217b3c0dcbb58927fe43400b6d1faa677bf98
DIFF: https://github.com/llvm/llvm-project/commit/ef5217b3c0dcbb58927fe43400b6d1faa677bf98.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

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 cfe-commits mailing list