1
Fork 0

coverage: More consistent variable names for span processing

This commit is contained in:
Zalathar 2024-06-16 01:04:40 +10:00
parent d5b34a28cf
commit e102d2dbd6
2 changed files with 25 additions and 25 deletions

View file

@ -19,11 +19,11 @@ pub(super) fn extract_refined_covspans(
basic_coverage_blocks: &CoverageGraph, basic_coverage_blocks: &CoverageGraph,
code_mappings: &mut impl Extend<mappings::CodeMapping>, code_mappings: &mut impl Extend<mappings::CodeMapping>,
) { ) {
let sorted_span_buckets = let buckets =
from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks); from_mir::mir_to_initial_sorted_coverage_spans(mir_body, hir_info, basic_coverage_blocks);
for bucket in sorted_span_buckets { for covspans in buckets {
let refined_spans = refine_sorted_spans(bucket); let covspans = refine_sorted_spans(covspans);
code_mappings.extend(refined_spans.into_iter().map(|RefinedCovspan { span, bcb }| { code_mappings.extend(covspans.into_iter().map(|RefinedCovspan { span, bcb }| {
// Each span produced by the refiner represents an ordinary code region. // Each span produced by the refiner represents an ordinary code region.
mappings::CodeMapping { span, bcb } mappings::CodeMapping { span, bcb }
})); }));

View file

@ -31,7 +31,7 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
) -> Vec<Vec<SpanFromMir>> { ) -> Vec<Vec<SpanFromMir>> {
let &ExtractedHirInfo { body_span, .. } = hir_info; let &ExtractedHirInfo { body_span, .. } = hir_info;
let mut initial_spans = vec![]; let mut covspans = vec![];
let mut holes = vec![]; let mut holes = vec![];
for (bcb, bcb_data) in basic_coverage_blocks.iter_enumerated() { for (bcb, bcb_data) in basic_coverage_blocks.iter_enumerated() {
@ -40,36 +40,36 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
body_span, body_span,
bcb, bcb,
bcb_data, bcb_data,
&mut initial_spans, &mut covspans,
&mut holes, &mut holes,
); );
} }
// Only add the signature span if we found at least one span in the body. // Only add the signature span if we found at least one span in the body.
if !initial_spans.is_empty() || !holes.is_empty() { if !covspans.is_empty() || !holes.is_empty() {
// If there is no usable signature span, add a fake one (before refinement) // If there is no usable signature span, add a fake one (before refinement)
// to avoid an ugly gap between the body start and the first real span. // to avoid an ugly gap between the body start and the first real span.
// FIXME: Find a more principled way to solve this problem. // FIXME: Find a more principled way to solve this problem.
let fn_sig_span = hir_info.fn_sig_span_extended.unwrap_or_else(|| body_span.shrink_to_lo()); let fn_sig_span = hir_info.fn_sig_span_extended.unwrap_or_else(|| body_span.shrink_to_lo());
initial_spans.push(SpanFromMir::for_fn_sig(fn_sig_span)); covspans.push(SpanFromMir::for_fn_sig(fn_sig_span));
} }
initial_spans.sort_by(|a, b| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb)); covspans.sort_by(|a, b| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb));
remove_unwanted_macro_spans(&mut initial_spans); remove_unwanted_macro_spans(&mut covspans);
split_visible_macro_spans(&mut initial_spans); split_visible_macro_spans(&mut covspans);
let compare_covspans = |a: &SpanFromMir, b: &SpanFromMir| { let compare_covspans = |a: &SpanFromMir, b: &SpanFromMir| {
compare_spans(a.span, b.span) compare_spans(a.span, b.span)
// After deduplication, we want to keep only the most-dominated BCB. // After deduplication, we want to keep only the most-dominated BCB.
.then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb).reverse()) .then_with(|| basic_coverage_blocks.cmp_in_dominator_order(a.bcb, b.bcb).reverse())
}; };
initial_spans.sort_by(compare_covspans); covspans.sort_by(compare_covspans);
// Among covspans with the same span, keep only one, // Among covspans with the same span, keep only one,
// preferring the one with the most-dominated BCB. // preferring the one with the most-dominated BCB.
// (Ideally we should try to preserve _all_ non-dominating BCBs, but that // (Ideally we should try to preserve _all_ non-dominating BCBs, but that
// requires a lot more complexity in the span refiner, for little benefit.) // requires a lot more complexity in the span refiner, for little benefit.)
initial_spans.dedup_by(|b, a| a.span.source_equal(b.span)); covspans.dedup_by(|b, a| a.span.source_equal(b.span));
// Sort the holes, and merge overlapping/adjacent holes. // Sort the holes, and merge overlapping/adjacent holes.
holes.sort_by(|a, b| compare_spans(a.span, b.span)); holes.sort_by(|a, b| compare_spans(a.span, b.span));
@ -78,7 +78,7 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
// Now we're ready to start carving holes out of the initial coverage spans, // Now we're ready to start carving holes out of the initial coverage spans,
// and grouping them in buckets separated by the holes. // and grouping them in buckets separated by the holes.
let mut initial_spans = VecDeque::from(initial_spans); let mut input_covspans = VecDeque::from(covspans);
let mut fragments: Vec<SpanFromMir> = vec![]; let mut fragments: Vec<SpanFromMir> = vec![];
// For each hole: // For each hole:
@ -93,10 +93,10 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
// Only inspect spans that precede or overlap this hole, // Only inspect spans that precede or overlap this hole,
// leaving the rest to be inspected by later holes. // leaving the rest to be inspected by later holes.
// (This relies on the spans and holes both being sorted.) // (This relies on the spans and holes both being sorted.)
let relevant_initial_spans = let relevant_input_covspans =
drain_front_while(&mut initial_spans, |c| c.span.lo() < hole.span.hi()); drain_front_while(&mut input_covspans, |c| c.span.lo() < hole.span.hi());
for covspan in fragments_from_prev.into_iter().chain(relevant_initial_spans) { for covspan in fragments_from_prev.into_iter().chain(relevant_input_covspans) {
let (before, after) = covspan.split_around_hole_span(hole.span); let (before, after) = covspan.split_around_hole_span(hole.span);
bucket.extend(before); bucket.extend(before);
fragments.extend(after); fragments.extend(after);
@ -106,12 +106,12 @@ pub(super) fn mir_to_initial_sorted_coverage_spans(
// After finding the spans before each hole, any remaining fragments/spans // After finding the spans before each hole, any remaining fragments/spans
// form their own final bucket, after the final hole. // form their own final bucket, after the final hole.
// (If there were no holes, this will just be all of the initial spans.) // (If there were no holes, this will just be all of the initial spans.)
fragments.extend(initial_spans); fragments.extend(input_covspans);
buckets.push(fragments); buckets.push(fragments);
// Make sure each individual bucket is still internally sorted. // Make sure each individual bucket is still internally sorted.
for bucket in &mut buckets { for covspans in &mut buckets {
bucket.sort_by(compare_covspans); covspans.sort_by(compare_covspans);
} }
buckets buckets
} }
@ -143,9 +143,9 @@ fn drain_front_while<'a, T>(
/// ///
/// (The input spans should be sorted in BCB dominator order, so that the /// (The input spans should be sorted in BCB dominator order, so that the
/// retained "first" span is likely to dominate the others.) /// retained "first" span is likely to dominate the others.)
fn remove_unwanted_macro_spans(initial_spans: &mut Vec<SpanFromMir>) { fn remove_unwanted_macro_spans(covspans: &mut Vec<SpanFromMir>) {
let mut seen_macro_spans = FxHashSet::default(); let mut seen_macro_spans = FxHashSet::default();
initial_spans.retain(|covspan| { covspans.retain(|covspan| {
// Ignore (retain) non-macro-expansion spans. // Ignore (retain) non-macro-expansion spans.
if covspan.visible_macro.is_none() { if covspan.visible_macro.is_none() {
return true; return true;
@ -160,10 +160,10 @@ fn remove_unwanted_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
/// function body, split it into two parts. The first part covers just the /// function body, split it into two parts. The first part covers just the
/// macro name plus `!`, and the second part covers the rest of the macro /// macro name plus `!`, and the second part covers the rest of the macro
/// invocation. This seems to give better results for code that uses macros. /// invocation. This seems to give better results for code that uses macros.
fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) { fn split_visible_macro_spans(covspans: &mut Vec<SpanFromMir>) {
let mut extra_spans = vec![]; let mut extra_spans = vec![];
initial_spans.retain(|covspan| { covspans.retain(|covspan| {
let Some(visible_macro) = covspan.visible_macro else { return true }; let Some(visible_macro) = covspan.visible_macro else { return true };
let split_len = visible_macro.as_str().len() as u32 + 1; let split_len = visible_macro.as_str().len() as u32 + 1;
@ -183,7 +183,7 @@ fn split_visible_macro_spans(initial_spans: &mut Vec<SpanFromMir>) {
// The newly-split spans are added at the end, so any previous sorting // The newly-split spans are added at the end, so any previous sorting
// is not preserved. // is not preserved.
initial_spans.extend(extra_spans); covspans.extend(extra_spans);
} }
// Generate a set of coverage spans from the filtered set of `Statement`s and `Terminator`s of // Generate a set of coverage spans from the filtered set of `Statement`s and `Terminator`s of