Skip to content

Commit 8554a55

Browse files
committed
[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
1 parent 550fa4e commit 8554a55

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

clang/lib/Frontend/TextDiagnostic.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,16 +1160,20 @@ void TextDiagnostic::emitSnippetAndCaret(
11601160
// Find the set of lines to include.
11611161
const unsigned MaxLines = DiagOpts->SnippetLineLimit;
11621162
std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo};
1163+
unsigned DisplayLineNo =
1164+
Ranges.empty() ? Loc.getPresumedLoc().getLine() : ~0u;
11631165
for (const auto &I : Ranges) {
11641166
if (auto OptionalRange = findLinesForRange(I, FID, SM))
11651167
Lines = maybeAddRange(Lines, *OptionalRange, MaxLines);
1168+
1169+
DisplayLineNo =
1170+
std::min(DisplayLineNo, SM.getPresumedLineNumber(I.getBegin()));
11661171
}
11671172

11681173
// Our line numbers look like:
11691174
// " [number] | "
11701175
// Where [number] is MaxLineNoDisplayWidth columns
11711176
// and the full thing is therefore MaxLineNoDisplayWidth + 4 columns.
1172-
unsigned DisplayLineNo = Loc.getPresumedLoc().getLine();
11731177
unsigned MaxLineNoDisplayWidth =
11741178
DiagOpts->ShowLineNumbers
11751179
? std::max(4u, getNumDisplayWidth(DisplayLineNo + MaxLines))

clang/test/Misc/diag-style.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,17 @@ static_assert(false &&
1010
// CHECK-NEXT: {{^}} 5 | {{$}}
1111
// CHECK-NEXT: {{^}} 6 | true, "");{{$}}
1212
// CHECK-NEXT: {{^}} | ~~~~{{$}}
13+
14+
15+
/// #line pragmas are respected
16+
void printf(const char *a, ...) __attribute__((__format__(__printf__, 1, 2)));
17+
#line 10
18+
void f(int x) {
19+
printf("%f",
20+
x);
21+
}
22+
// CHECK: 12:10: warning: format specifies type
23+
// CHECK-NEXT: {{^}} 11 |
24+
// CHECK-NEXT: {{^}} |
25+
// CHECK-NEXT: {{^}} |
26+
// CHECK-NEXT: {{^}} 12 |

0 commit comments

Comments
 (0)