Rollup merge of #132675 - Zalathar:empty-spans, r=jieyouxu
coverage: Restrict empty-span expansion to only cover `{` and `}` Coverage instrumentation has some tricky code for converting a coverage-relevant `Span` into a set of start/end line/byte-column coordinates that will be embedded in the CGU's coverage metadata. A big part of this complexity is special code for handling empty spans, which are expanded into non-empty spans (if possible) because LLVM's coverage reporter does not handle empty spans well. This PR simplifies that code by restricting it to only apply in two specific situations: when the character after the empty span is `{`, or the character before the empty span is `}`. (As an added benefit, this means that the expanded spans no longer extend awkwardly beyond the end of a physical line, which was common under the previous implementation.) Along the way, this PR also removes some unhelpful code for dealing with function source code spread across multiple files. Functions currently can't have coverage spans in multiple files, and if that ever changes (e.g. to properly support expansion regions) then this code will need to be completely overhauled anyway.
This commit is contained in:
commit
b95232dabb
57 changed files with 318 additions and 303 deletions
|
@ -22,8 +22,8 @@ use rustc_middle::mir::{
|
|||
use rustc_middle::ty::TyCtxt;
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::source_map::SourceMap;
|
||||
use rustc_span::{BytePos, Pos, RelativeBytePos, Span, Symbol};
|
||||
use tracing::{debug, debug_span, instrument, trace};
|
||||
use rustc_span::{BytePos, Pos, SourceFile, Span};
|
||||
use tracing::{debug, debug_span, trace};
|
||||
|
||||
use crate::coverage::counters::{CounterIncrementSite, CoverageCounters};
|
||||
use crate::coverage::graph::CoverageGraph;
|
||||
|
@ -122,6 +122,7 @@ fn instrument_function_for_coverage<'tcx>(tcx: TyCtxt<'tcx>, mir_body: &mut mir:
|
|||
|
||||
mir_body.function_coverage_info = Some(Box::new(FunctionCoverageInfo {
|
||||
function_source_hash: hir_info.function_source_hash,
|
||||
body_span: hir_info.body_span,
|
||||
num_counters: coverage_counters.num_counters(),
|
||||
mcdc_bitmap_bits: extracted_mappings.mcdc_bitmap_bits,
|
||||
expressions: coverage_counters.into_expressions(),
|
||||
|
@ -142,19 +143,11 @@ fn create_mappings<'tcx>(
|
|||
coverage_counters: &CoverageCounters,
|
||||
) -> Vec<Mapping> {
|
||||
let source_map = tcx.sess.source_map();
|
||||
let body_span = hir_info.body_span;
|
||||
|
||||
let source_file = source_map.lookup_source_file(body_span.lo());
|
||||
|
||||
use rustc_session::RemapFileNameExt;
|
||||
use rustc_session::config::RemapPathScopeComponents;
|
||||
let file_name = Symbol::intern(
|
||||
&source_file.name.for_scope(tcx.sess, RemapPathScopeComponents::MACRO).to_string_lossy(),
|
||||
);
|
||||
let file = source_map.lookup_source_file(hir_info.body_span.lo());
|
||||
|
||||
let term_for_bcb =
|
||||
|bcb| coverage_counters.term_for_bcb(bcb).expect("all BCBs with spans were given counters");
|
||||
let region_for_span = |span: Span| make_source_region(source_map, file_name, span, body_span);
|
||||
let region_for_span = |span: Span| make_source_region(source_map, hir_info, &file, span);
|
||||
|
||||
// Fully destructure the mappings struct to make sure we don't miss any kinds.
|
||||
let ExtractedMappings {
|
||||
|
@ -398,7 +391,42 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
|
|||
data.statements.insert(0, statement);
|
||||
}
|
||||
|
||||
/// Convert the Span into its file name, start line and column, and end line and column.
|
||||
fn ensure_non_empty_span(
|
||||
source_map: &SourceMap,
|
||||
hir_info: &ExtractedHirInfo,
|
||||
span: Span,
|
||||
) -> Option<Span> {
|
||||
if !span.is_empty() {
|
||||
return Some(span);
|
||||
}
|
||||
|
||||
let lo = span.lo();
|
||||
let hi = span.hi();
|
||||
|
||||
// The span is empty, so try to expand it to cover an adjacent '{' or '}',
|
||||
// but only within the bounds of the body span.
|
||||
let try_next = hi < hir_info.body_span.hi();
|
||||
let try_prev = hir_info.body_span.lo() < lo;
|
||||
if !(try_next || try_prev) {
|
||||
return None;
|
||||
}
|
||||
|
||||
source_map
|
||||
.span_to_source(span, |src, start, end| try {
|
||||
// We're only checking for specific ASCII characters, so we don't
|
||||
// have to worry about multi-byte code points.
|
||||
if try_next && src.as_bytes()[end] == b'{' {
|
||||
Some(span.with_hi(hi + BytePos(1)))
|
||||
} else if try_prev && src.as_bytes()[start - 1] == b'}' {
|
||||
Some(span.with_lo(lo - BytePos(1)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.ok()?
|
||||
}
|
||||
|
||||
/// Converts the span into its start line and column, and end line and column.
|
||||
///
|
||||
/// Line numbers and column numbers are 1-based. Unlike most column numbers emitted by
|
||||
/// the compiler, these column numbers are denoted in **bytes**, because that's what
|
||||
|
@ -408,56 +436,29 @@ fn inject_statement(mir_body: &mut mir::Body<'_>, counter_kind: CoverageKind, bb
|
|||
/// but it's hard to rule out entirely (especially in the presence of complex macros
|
||||
/// or other expansions), and if it does happen then skipping a span or function is
|
||||
/// better than an ICE or `llvm-cov` failure that the user might have no way to avoid.
|
||||
#[instrument(level = "debug", skip(source_map))]
|
||||
fn make_source_region(
|
||||
source_map: &SourceMap,
|
||||
file_name: Symbol,
|
||||
hir_info: &ExtractedHirInfo,
|
||||
file: &SourceFile,
|
||||
span: Span,
|
||||
body_span: Span,
|
||||
) -> Option<SourceRegion> {
|
||||
let span = ensure_non_empty_span(source_map, hir_info, span)?;
|
||||
|
||||
let lo = span.lo();
|
||||
let hi = span.hi();
|
||||
|
||||
let file = source_map.lookup_source_file(lo);
|
||||
if !file.contains(hi) {
|
||||
debug!(?span, ?file, ?lo, ?hi, "span crosses multiple files; skipping");
|
||||
return None;
|
||||
}
|
||||
|
||||
// Column numbers need to be in bytes, so we can't use the more convenient
|
||||
// `SourceMap` methods for looking up file coordinates.
|
||||
let rpos_and_line_and_byte_column = |pos: BytePos| -> Option<(RelativeBytePos, usize, usize)> {
|
||||
let line_and_byte_column = |pos: BytePos| -> Option<(usize, usize)> {
|
||||
let rpos = file.relative_position(pos);
|
||||
let line_index = file.lookup_line(rpos)?;
|
||||
let line_start = file.lines()[line_index];
|
||||
// Line numbers and column numbers are 1-based, so add 1 to each.
|
||||
Some((rpos, line_index + 1, (rpos - line_start).to_usize() + 1))
|
||||
Some((line_index + 1, (rpos - line_start).to_usize() + 1))
|
||||
};
|
||||
|
||||
let (lo_rpos, mut start_line, mut start_col) = rpos_and_line_and_byte_column(lo)?;
|
||||
let (hi_rpos, mut end_line, mut end_col) = rpos_and_line_and_byte_column(hi)?;
|
||||
|
||||
// If the span is empty, try to expand it horizontally by one character's
|
||||
// worth of bytes, so that it is more visible in `llvm-cov` reports.
|
||||
// We do this after resolving line/column numbers, so that empty spans at the
|
||||
// end of a line get an extra column instead of wrapping to the next line.
|
||||
if span.is_empty()
|
||||
&& body_span.contains(span)
|
||||
&& let Some(src) = &file.src
|
||||
{
|
||||
// Prefer to expand the end position, if it won't go outside the body span.
|
||||
if hi < body_span.hi() {
|
||||
let hi_rpos = hi_rpos.to_usize();
|
||||
let nudge_bytes = src.ceil_char_boundary(hi_rpos + 1) - hi_rpos;
|
||||
end_col += nudge_bytes;
|
||||
} else if lo > body_span.lo() {
|
||||
let lo_rpos = lo_rpos.to_usize();
|
||||
let nudge_bytes = lo_rpos - src.floor_char_boundary(lo_rpos - 1);
|
||||
// Subtract the nudge, but don't go below column 1.
|
||||
start_col = start_col.saturating_sub(nudge_bytes).max(1);
|
||||
}
|
||||
// If neither nudge could be applied, stick with the empty span coordinates.
|
||||
}
|
||||
let (mut start_line, start_col) = line_and_byte_column(lo)?;
|
||||
let (mut end_line, end_col) = line_and_byte_column(hi)?;
|
||||
|
||||
// Apply an offset so that code in doctests has correct line numbers.
|
||||
// FIXME(#79417): Currently we have no way to offset doctest _columns_.
|
||||
|
@ -465,7 +466,6 @@ fn make_source_region(
|
|||
end_line = source_map.doctest_offset_line(&file.name, end_line);
|
||||
|
||||
check_source_region(SourceRegion {
|
||||
file_name,
|
||||
start_line: start_line as u32,
|
||||
start_col: start_col as u32,
|
||||
end_line: end_line as u32,
|
||||
|
@ -478,7 +478,7 @@ fn make_source_region(
|
|||
/// discard regions that are improperly ordered, or might be interpreted in a
|
||||
/// way that makes them improperly ordered.
|
||||
fn check_source_region(source_region: SourceRegion) -> Option<SourceRegion> {
|
||||
let SourceRegion { file_name: _, start_line, start_col, end_line, end_col } = source_region;
|
||||
let SourceRegion { start_line, start_col, end_line, end_col } = source_region;
|
||||
|
||||
// Line/column coordinates are supposed to be 1-based. If we ever emit
|
||||
// coordinates of 0, `llvm-cov` might misinterpret them.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue