1use rustc_span::source_map::SourceMap;
2use rustc_span::{BytePos, Pos, SourceFile, Span};
3use tracing::debug;
45use crate::coverageinfo::ffi;
6use crate::coverageinfo::mapgen::LocalFileId;
78/// Line and byte-column coordinates of a source code span within some file.
9/// The file itself must be tracked separately.
10#[derive(#[automatically_derived]
impl ::core::clone::Clone for Coords {
#[inline]
fn clone(&self) -> Coords {
let _: ::core::clone::AssertParamIsClone<u32>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for Coords { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for Coords {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f, "Coords",
"start_line", &self.start_line, "start_col", &self.start_col,
"end_line", &self.end_line, "end_col", &&self.end_col)
}
}Debug)]
11pub(crate) struct Coords {
12/// 1-based starting line of the source code span.
13pub(crate) start_line: u32,
14/// 1-based starting column (in bytes) of the source code span.
15pub(crate) start_col: u32,
16/// 1-based ending line of the source code span.
17pub(crate) end_line: u32,
18/// 1-based ending column (in bytes) of the source code span. High bit must be unset.
19pub(crate) end_col: u32,
20}
2122impl Coords {
23/// Attaches a local file ID to these coordinates to produce an `ffi::CoverageSpan`.
24pub(crate) fn make_coverage_span(&self, local_file_id: LocalFileId) -> ffi::CoverageSpan {
25let &Self { start_line, start_col, end_line, end_col } = self;
26let file_id = local_file_id.as_u32();
27 ffi::CoverageSpan { file_id, start_line, start_col, end_line, end_col }
28 }
29}
3031/// 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> {
42if span.is_empty() {
43if true {
if !false {
{
::core::panicking::panic_fmt(format_args!("can\'t make coords from empty span: {0:?}",
span));
}
};
};debug_assert!(false, "can't make coords from empty span: {span:?}");
44return None;
45 }
4647let lo = span.lo();
48let hi = span.hi();
4950// Column numbers need to be in bytes, so we can't use the more convenient
51 // `SourceMap` methods for looking up file coordinates.
52let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> {
53let rpos = file.relative_position(pos);
54let line_index = file.lookup_line(rpos)?;
55let line_start = file.lines()[line_index];
56// Line numbers and column numbers are 1-based, so add 1 to each.
57Some((line_index + 1, (rpos - line_start).to_usize() + 1))
58 };
5960let (mut start_line, start_col) = line_and_byte_column(lo)?;
61let (mut end_line, end_col) = line_and_byte_column(hi)?;
6263// Apply an offset so that code in doctests has correct line numbers.
64 // FIXME(#79417): Currently we have no way to offset doctest _columns_.
65start_line = source_map.doctest_offset_line(&file.name, start_line);
66end_line = source_map.doctest_offset_line(&file.name, end_line);
6768check_coords(Coords {
69 start_line: start_lineas u32,
70 start_col: start_colas u32,
71 end_line: end_lineas u32,
72 end_col: end_colas u32,
73 })
74}
7576/// 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> {
81let Coords { start_line, start_col, end_line, end_col } = coords;
8283// Line/column coordinates are supposed to be 1-based. If we ever emit
84 // coordinates of 0, `llvm-cov` might misinterpret them.
85let 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.
88let 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.
91let is_ordered = (start_line, start_col) <= (end_line, end_col);
9293if all_nonzero && end_col_has_high_bit_unset && is_ordered {
94Some(coords)
95 } else {
96{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs:96",
"rustc_codegen_llvm::coverageinfo::mapgen::spans",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/spans.rs"),
::tracing_core::__macro_support::Option::Some(96u32),
::tracing_core::__macro_support::Option::Some("rustc_codegen_llvm::coverageinfo::mapgen::spans"),
::tracing_core::field::FieldSet::new(&["message", "coords",
"all_nonzero", "end_col_has_high_bit_unset", "is_ordered"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("Skipping source region that would be misinterpreted or rejected by LLVM")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&coords) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&all_nonzero)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&end_col_has_high_bit_unset)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&is_ordered)
as &dyn Value))])
});
} else { ; }
};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.
104if true {
if !false {
{
::core::panicking::panic_fmt(format_args!("Improper source region: {0:?}",
coords));
}
};
};debug_assert!(false, "Improper source region: {coords:?}");
105None106 }
107}