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 if span.is_empty() {
43 debug_assert!(false, "can't make coords from empty span: {span:?}");
44 return None;
45 }
46
47 let lo = span.lo();
48 let hi = span.hi();
49
50 // Column numbers need to be in bytes, so we can't use the more convenient
51 // `SourceMap` methods for looking up file coordinates.
52 let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> {
53 let rpos = file.relative_position(pos);
54 let line_index = file.lookup_line(rpos)?;
55 let line_start = file.lines()[line_index];
56 // Line numbers and column numbers are 1-based, so add 1 to each.
57 Some((line_index + 1, (rpos - line_start).to_usize() + 1))
58 };
59
60 let (mut start_line, start_col) = line_and_byte_column(lo)?;
61 let (mut end_line, end_col) = line_and_byte_column(hi)?;
62
63 // Apply an offset so that code in doctests has correct line numbers.
64 // FIXME(#79417): Currently we have no way to offset doctest _columns_.
65 start_line = source_map.doctest_offset_line(&file.name, start_line);
66 end_line = source_map.doctest_offset_line(&file.name, end_line);
67
68 check_coords(Coords {
69 start_line: start_line as u32,
70 start_col: start_col as u32,
71 end_line: end_line as u32,
72 end_col: end_col as u32,
73 })
74}
75
76/// If `llvm-cov` sees a source region that is improperly ordered (end < start),
77/// it will immediately exit with a fatal error. To prevent that from happening,
78/// discard regions that are improperly ordered, or might be interpreted in a
79/// way that makes them improperly ordered.
80fn check_coords(coords: Coords) -> Option<Coords> {
81 let Coords { start_line, start_col, end_line, end_col } = coords;
82
83 // Line/column coordinates are supposed to be 1-based. If we ever emit
84 // coordinates of 0, `llvm-cov` might misinterpret them.
85 let all_nonzero = [start_line, start_col, end_line, end_col].into_iter().all(|x| x != 0);
86 // Coverage mappings use the high bit of `end_col` to indicate that a
87 // region is actually a "gap" region, so make sure it's unset.
88 let end_col_has_high_bit_unset = (end_col & (1 << 31)) == 0;
89 // If a region is improperly ordered (end < start), `llvm-cov` will exit
90 // with a fatal error, which is inconvenient for users and hard to debug.
91 let is_ordered = (start_line, start_col) <= (end_line, end_col);
92
93 if all_nonzero && end_col_has_high_bit_unset && is_ordered {
94 Some(coords)
95 } else {
96 debug!(
97 ?coords,
98 ?all_nonzero,
99 ?end_col_has_high_bit_unset,
100 ?is_ordered,
101 "Skipping source region that would be misinterpreted or rejected by LLVM"
102 );
103 // If this happens in a debug build, ICE to make it easier to notice.
104 debug_assert!(false, "Improper source region: {coords:?}");
105 None
106 }
107}