rustc_codegen_llvm/coverageinfo/mapgen/spans.rs
1use rustc_span::source_map::SourceMap;
2use rustc_span::{BytePos, Pos, SourceFile, Span};
3use tracing::debug;
4
5use crate::coverageinfo::ffi;
6use crate::coverageinfo::mapgen::LocalFileId;
7
8/// Line and byte-column coordinates of a source code span within some file.
9/// The file itself must be tracked separately.
10#[derive(Clone, Copy, Debug)]
11pub(crate) struct Coords {
12 /// 1-based starting line of the source code span.
13 pub(crate) start_line: u32,
14 /// 1-based starting column (in bytes) of the source code span.
15 pub(crate) start_col: u32,
16 /// 1-based ending line of the source code span.
17 pub(crate) end_line: u32,
18 /// 1-based ending column (in bytes) of the source code span. High bit must be unset.
19 pub(crate) end_col: u32,
20}
21
22impl Coords {
23 /// Attaches a local file ID to these coordinates to produce an `ffi::CoverageSpan`.
24 pub(crate) fn make_coverage_span(&self, local_file_id: LocalFileId) -> ffi::CoverageSpan {
25 let &Self { start_line, start_col, end_line, end_col } = self;
26 let file_id = local_file_id.as_u32();
27 ffi::CoverageSpan { file_id, start_line, start_col, end_line, end_col }
28 }
29}
30
31/// Converts the span into its start line and column, and end line and column.
32///
33/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by
34/// the compiler, these column numbers are denoted in **bytes**, because that's what
35/// LLVM's `llvm-cov` tool expects to see in coverage maps.
36///
37/// Returns `None` if the conversion failed for some reason. This should be uncommon,
38/// but it's hard to rule out entirely (especially in the presence of complex macros
39/// or other expansions), and if it does happen then skipping a span or function is
40/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid.
41pub(crate) fn make_coords(source_map: &SourceMap, file: &SourceFile, span: Span) -> Option<Coords> {
42 let span = ensure_non_empty_span(source_map, span)?;
43
44 let lo = span.lo();
45 let hi = span.hi();
46
47 // Column numbers need to be in bytes, so we can't use the more convenient
48 // `SourceMap` methods for looking up file coordinates.
49 let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> {
50 let rpos = file.relative_position(pos);
51 let line_index = file.lookup_line(rpos)?;
52 let line_start = file.lines()[line_index];
53 // Line numbers and column numbers are 1-based, so add 1 to each.
54 Some((line_index + 1, (rpos - line_start).to_usize() + 1))
55 };
56
57 let (mut start_line, start_col) = line_and_byte_column(lo)?;
58 let (mut end_line, end_col) = line_and_byte_column(hi)?;
59
60 // Apply an offset so that code in doctests has correct line numbers.
61 // FIXME(#79417): Currently we have no way to offset doctest _columns_.
62 start_line = source_map.doctest_offset_line(&file.name, start_line);
63 end_line = source_map.doctest_offset_line(&file.name, end_line);
64
65 check_coords(Coords {
66 start_line: start_line as u32,
67 start_col: start_col as u32,
68 end_line: end_line as u32,
69 end_col: end_col as u32,
70 })
71}
72
73fn ensure_non_empty_span(source_map: &SourceMap, span: Span) -> Option<Span> {
74 if !span.is_empty() {
75 return Some(span);
76 }
77
78 // The span is empty, so try to enlarge it to cover an adjacent '{' or '}'.
79 source_map
80 .span_to_source(span, |src, start, end| try {
81 // Adjusting span endpoints by `BytePos(1)` is normally a bug,
82 // but in this case we have specifically checked that the character
83 // we're skipping over is one of two specific ASCII characters, so
84 // adjusting by exactly 1 byte is correct.
85 if src.as_bytes().get(end).copied() == Some(b'{') {
86 Some(span.with_hi(span.hi() + BytePos(1)))
87 } else if start > 0 && src.as_bytes()[start - 1] == b'}' {
88 Some(span.with_lo(span.lo() - BytePos(1)))
89 } else {
90 None
91 }
92 })
93 .ok()?
94}
95
96/// If `llvm-cov` sees a source region that is improperly ordered (end < start),
97/// it will immediately exit with a fatal error. To prevent that from happening,
98/// discard regions that are improperly ordered, or might be interpreted in a
99/// way that makes them improperly ordered.
100fn check_coords(coords: Coords) -> Option<Coords> {
101 let Coords { start_line, start_col, end_line, end_col } = coords;
102
103 // Line/column coordinates are supposed to be 1-based. If we ever emit
104 // coordinates of 0, `llvm-cov` might misinterpret them.
105 let all_nonzero = [start_line, start_col, end_line, end_col].into_iter().all(|x| x != 0);
106 // Coverage mappings use the high bit of `end_col` to indicate that a
107 // region is actually a "gap" region, so make sure it's unset.
108 let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0;
109 // If a region is improperly ordered (end < start), `llvm-cov` will exit
110 // with a fatal error, which is inconvenient for users and hard to debug.
111 let is_ordered = (start_line, start_col) <= (end_line, end_col);
112
113 if all_nonzero && end_col_has_high_bit_unset && is_ordered {
114 Some(coords)
115 } else {
116 debug!(
117 ?coords,
118 ?all_nonzero,
119 ?end_col_has_high_bit_unset,
120 ?is_ordered,
121 "Skipping source region that would be misinterpreted or rejected by LLVM"
122 );
123 // If this happens in a debug build, ICE to make it easier to notice.
124 debug_assert!(false, "Improper source region: {coords:?}");
125 None
126 }
127}