Skip to content

Commit 6971e93

Browse files
committed
coverage: llvm-cov expects column numbers to be bytes, not code points
1 parent 88f5759 commit 6971e93

File tree

5 files changed

+111
-26
lines changed

5 files changed

+111
-26
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

+58-14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_middle::mir::{
2323
use rustc_middle::ty::TyCtxt;
2424
use rustc_span::def_id::LocalDefId;
2525
use rustc_span::source_map::SourceMap;
26-
use rustc_span::{Span, Symbol};
26+
use rustc_span::{BytePos, Pos, RelativeBytePos, Span, Symbol};
2727

2828
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
2929
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen
@@ -258,7 +258,16 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
258258
data.statements.insert(0, statement);
259259
}
260260

261-
/// Convert the Span into its file name, start line and column, and end line and column
261+
/// Convert the Span into its file name, start line and column, and end line and column.
262+
///
263+
/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by
264+
/// the compiler, these column numbers are denoted in **bytes**, because that's what
265+
/// LLVM's `llvm-cov` tool expects to see in coverage maps.
266+
///
267+
/// Returns `None` if the conversion failed for some reason. This shouldn't happen,
268+
/// but it's hard to rule out entirely (especially in the presence of complex macros
269+
/// or other expansions), and if it does happen then skipping a span or function is
270+
/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid.
262271
fn make_code_region(
263272
source_map: &SourceMap,
264273
file_name: Symbol,
@@ -272,20 +281,55 @@ fn make_code_region(
272281
source_map.span_to_diagnostic_string(body_span)
273282
);
274283

275-
let (file, mut start_line, mut start_col, mut end_line, mut end_col) =
276-
source_map.span_to_location_info(span);
277-
if span.hi() == span.lo() {
278-
// Extend an empty span by one character so the region will be counted.
279-
if span.hi() == body_span.hi() {
280-
start_col = start_col.saturating_sub(1);
281-
} else {
282-
end_col = start_col + 1;
283-
}
284+
let lo = span.lo();
285+
let hi = span.hi();
286+
287+
let file = source_map.lookup_source_file(lo);
288+
if !file.contains(hi) {
289+
debug!(?span, ?file, ?lo, ?hi, "span crosses multiple files; skipping");
290+
return None;
291+
}
292+
293+
// Column numbers need to be in bytes, so we can't use the more convenient
294+
// `SourceMap` methods for looking up file coordinates.
295+
let rpos_and_line_and_byte_column = |pos: BytePos| -> Option<(RelativeBytePos, usize, usize)> {
296+
let rpos = file.relative_position(pos);
297+
let line_index = file.lookup_line(rpos)?;
298+
let line_start = file.lines()[line_index];
299+
// Line numbers and column numbers are 1-based, so add 1 to each.
300+
Some((rpos, line_index + 1, (rpos - line_start).to_usize() + 1))
284301
};
285-
if let Some(file) = file {
286-
start_line = source_map.doctest_offset_line(&file.name, start_line);
287-
end_line = source_map.doctest_offset_line(&file.name, end_line);
302+
303+
let (lo_rpos, mut start_line, mut start_col) = rpos_and_line_and_byte_column(lo)?;
304+
let (hi_rpos, mut end_line, mut end_col) = rpos_and_line_and_byte_column(hi)?;
305+
306+
// If the span is empty, try to expand it horizontally by one character's
307+
// worth of bytes, so that it is more visible in `llvm-cov` reports.
308+
// We do this after resolving line/column numbers, so that empty spans at the
309+
// end of a line get an extra column instead of wrapping to the next line.
310+
if span.is_empty()
311+
&& body_span.contains(span)
312+
&& let Some(src) = &file.src
313+
{
314+
// Prefer to expand the end position, if it won't go outside the body span.
315+
if hi < body_span.hi() {
316+
let hi_rpos = hi_rpos.to_usize();
317+
let nudge_bytes = src.ceil_char_boundary(hi_rpos + 1) - hi_rpos;
318+
end_col += nudge_bytes;
319+
} else if lo > body_span.lo() {
320+
let lo_rpos = lo_rpos.to_usize();
321+
let nudge_bytes = lo_rpos - src.floor_char_boundary(lo_rpos - 1);
322+
// Subtract the nudge, but don't go below column 1.
323+
start_col = start_col.saturating_sub(nudge_bytes).max(1);
324+
}
325+
// If neither nudge could be applied, stick with the empty span coordinates.
288326
}
327+
328+
// Apply an offset so that code in doctests has correct line numbers.
329+
// FIXME(#79417): Currently we have no way to offset doctest _columns_.
330+
start_line = source_map.doctest_offset_line(&file.name, start_line);
331+
end_line = source_map.doctest_offset_line(&file.name, end_line);
332+
289333
Some(CodeRegion {
290334
file_name,
291335
start_line: start_line as u32,

compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(min_specialization)]
1010
#![feature(never_type)]
1111
#![feature(option_get_or_insert_default)]
12+
#![feature(round_char_boundary)]
1213
#![feature(trusted_step)]
1314
#![feature(try_blocks)]
1415
#![feature(yeet_expr)]

tests/coverage/unicode.cov-map

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Function name: unicode::main
2-
Raw bytes (67): 0x[01, 01, 09, 01, 05, 03, 05, 1e, 0d, 22, 09, 03, 05, 11, 1b, 1e, 0d, 22, 09, 03, 05, 09, 01, 0e, 01, 00, 0b, 05, 01, 09, 00, 0b, 03, 00, 0f, 00, 18, 05, 00, 19, 00, 24, 22, 02, 08, 00, 13, 09, 00, 17, 00, 22, 11, 00, 23, 02, 06, 1b, 02, 06, 00, 07, 17, 02, 05, 01, 02]
2+
Raw bytes (67): 0x[01, 01, 09, 01, 05, 03, 05, 1e, 0d, 22, 09, 03, 05, 11, 1b, 1e, 0d, 22, 09, 03, 05, 09, 01, 0e, 01, 00, 0b, 05, 01, 09, 00, 0c, 03, 00, 10, 00, 1b, 05, 00, 1c, 00, 28, 22, 02, 08, 00, 25, 09, 00, 29, 00, 46, 11, 00, 47, 02, 06, 1b, 02, 06, 00, 07, 17, 02, 05, 01, 02]
33
Number of files: 1
44
- file 0 => global file 1
55
Number of expressions: 9
@@ -14,34 +14,34 @@ Number of expressions: 9
1414
- expression 8 operands: lhs = Expression(0, Add), rhs = Counter(1)
1515
Number of file 0 mappings: 9
1616
- Code(Counter(0)) at (prev + 14, 1) to (start + 0, 11)
17-
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 11)
18-
- Code(Expression(0, Add)) at (prev + 0, 15) to (start + 0, 24)
17+
- Code(Counter(1)) at (prev + 1, 9) to (start + 0, 12)
18+
- Code(Expression(0, Add)) at (prev + 0, 16) to (start + 0, 27)
1919
= (c0 + c1)
20-
- Code(Counter(1)) at (prev + 0, 25) to (start + 0, 36)
21-
- Code(Expression(8, Sub)) at (prev + 2, 8) to (start + 0, 19)
20+
- Code(Counter(1)) at (prev + 0, 28) to (start + 0, 40)
21+
- Code(Expression(8, Sub)) at (prev + 2, 8) to (start + 0, 37)
2222
= ((c0 + c1) - c1)
23-
- Code(Counter(2)) at (prev + 0, 23) to (start + 0, 34)
24-
- Code(Counter(4)) at (prev + 0, 35) to (start + 2, 6)
23+
- Code(Counter(2)) at (prev + 0, 41) to (start + 0, 70)
24+
- Code(Counter(4)) at (prev + 0, 71) to (start + 2, 6)
2525
- Code(Expression(6, Add)) at (prev + 2, 6) to (start + 0, 7)
2626
= ((((c0 + c1) - c1) - c2) + c3)
2727
- Code(Expression(5, Add)) at (prev + 2, 5) to (start + 1, 2)
2828
= (c4 + ((((c0 + c1) - c1) - c2) + c3))
2929

3030
Function name: unicode::サビ
31-
Raw bytes (9): 0x[01, 01, 00, 01, 01, 1e, 12, 00, 14]
31+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 1e, 14, 00, 18]
3232
Number of files: 1
3333
- file 0 => global file 1
3434
Number of expressions: 0
3535
Number of file 0 mappings: 1
36-
- Code(Counter(0)) at (prev + 30, 18) to (start + 0, 20)
36+
- Code(Counter(0)) at (prev + 30, 20) to (start + 0, 24)
3737

3838
Function name: unicode::他 (unused)
39-
Raw bytes (9): 0x[01, 01, 00, 01, 00, 1e, 15, 00, 1f]
39+
Raw bytes (9): 0x[01, 01, 00, 01, 00, 1e, 19, 00, 25]
4040
Number of files: 1
4141
- file 0 => global file 1
4242
Number of expressions: 0
4343
Number of file 0 mappings: 1
44-
- Code(Zero) at (prev + 30, 21) to (start + 0, 31)
44+
- Code(Zero) at (prev + 30, 25) to (start + 0, 37)
4545

4646
Function name: unicode::申し訳ございません
4747
Raw bytes (9): 0x[01, 01, 00, 01, 01, 18, 01, 02, 02]

tests/coverage/unicode.coverage

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
LL| |// edition: 2021
2+
LL| |// ignore-windows - we can't force `llvm-cov` to use ANSI escapes on Windows
3+
LL| |// llvm-cov-flags: --use-color
4+
LL| |
5+
LL| |// Check that column numbers are denoted in bytes, so that they don't cause
6+
LL| |// `llvm-cov` to fail or emit malformed output.
7+
LL| |//
8+
LL| |// Note that when `llvm-cov` prints ^ arrows on a subsequent line, it simply
9+
LL| |// inserts one space character for each "column", with no understanding of
10+
LL| |// Unicode or character widths. So those arrows will tend to be misaligned
11+
LL| |// for non-ASCII source code, regardless of whether column numbers are code
12+
LL| |// points or bytes.
13+
LL| |
14+
LL| 1|fn main() {
15+
LL| 33| for _İ in 'А'..='Я' { /* Я */ }
16+
^32 ^32
17+
LL| |
18+
LL| 1| if 申し訳ございません() && 申し訳ございません() {
19+
^0
20+
LL| 0| println!("true");
21+
LL| 1| }
22+
LL| |
23+
LL| 1| サビ();
24+
LL| 1|}
25+
LL| |
26+
LL| 1|fn 申し訳ございません() -> bool {
27+
LL| 1| std::hint::black_box(false)
28+
LL| 1|}
29+
LL| |
30+
LL| |macro_rules! macro_that_defines_a_function {
31+
LL| | (fn $名:ident () $体:tt) => {
32+
LL| 1| fn $名 () $体 fn 他 () {}
33+
^0
34+
LL| | }
35+
LL| |}
36+
LL| |
37+
LL| |macro_that_defines_a_function! {
38+
LL| | fn サビ() {}
39+
LL| |}
40+

tests/coverage/unicode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// edition: 2021
2-
// ignore-mode-coverage-run - `llvm-cov` fails due to incorrectly-split UTF-8
2+
// ignore-windows - we can't force `llvm-cov` to use ANSI escapes on Windows
33
// llvm-cov-flags: --use-color
44

55
// Check that column numbers are denoted in bytes, so that they don't cause

0 commit comments

Comments
 (0)