From 8554a55d041f2c7de329adda538cadf7eeb6e8a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Tue, 27 Jun 2023 11:33:22 +0200 Subject: [PATCH] [clang][Diagnostics] Fix diagnostic line numbers The first line of the code snippet we print is potentially lower than the caret line, so handle that case. Fixes #63524 Differential Revision: https://reviews.llvm.org/D153849 --- clang/lib/Frontend/TextDiagnostic.cpp | 6 +++++- clang/test/Misc/diag-style.cpp | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/clang/lib/Frontend/TextDiagnostic.cpp b/clang/lib/Frontend/TextDiagnostic.cpp index 48082d7..3a3cc24 100644 --- a/clang/lib/Frontend/TextDiagnostic.cpp +++ b/clang/lib/Frontend/TextDiagnostic.cpp @@ -1160,16 +1160,20 @@ void TextDiagnostic::emitSnippetAndCaret( // Find the set of lines to include. const unsigned MaxLines = DiagOpts->SnippetLineLimit; std::pair Lines = {CaretLineNo, CaretLineNo}; + unsigned DisplayLineNo = + Ranges.empty() ? Loc.getPresumedLoc().getLine() : ~0u; for (const auto &I : Ranges) { if (auto OptionalRange = findLinesForRange(I, FID, SM)) Lines = maybeAddRange(Lines, *OptionalRange, MaxLines); + + DisplayLineNo = + std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin())); } // Our line numbers look like: // " [number] | " // Where [number] is MaxLineNoDisplayWidth columns // and the full thing is therefore MaxLineNoDisplayWidth + 4 columns. - unsigned DisplayLineNo = Loc.getPresumedLoc().getLine(); unsigned MaxLineNoDisplayWidth = DiagOpts->ShowLineNumbers ? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines)) diff --git a/clang/test/Misc/diag-style.cpp b/clang/test/Misc/diag-style.cpp index b12afb2c..3b24df9 100644 --- a/clang/test/Misc/diag-style.cpp +++ b/clang/test/Misc/diag-style.cpp @@ -10,3 +10,17 @@ static_assert(false && // CHECK-NEXT: {{^}} 5 | {{$}} // CHECK-NEXT: {{^}} 6 | true, "");{{$}} // CHECK-NEXT: {{^}} | ~~~~{{$}} + + +/// #line pragmas are respected +void printf(const char *a, ...) __attribute__((__format__(__printf__, 1, 2))); +#line 10 +void f(int x) { + printf("%f", + x); +} +// CHECK: 12:10: warning: format specifies type +// CHECK-NEXT: {{^}} 11 | +// CHECK-NEXT: {{^}} | +// CHECK-NEXT: {{^}} | +// CHECK-NEXT: {{^}} 12 | -- 2.7.4