1
Fork 0

coverage: Call prev/curr less in to_refined_spans

This makes it easier to see that the non-initial cases assume that `prev` and
`curr` are set, and all operate on the same prev/curr references.
This commit is contained in:
Zalathar 2023-10-15 13:17:47 +11:00
parent 9bb27f3adf
commit b1c44f4a25

View file

@ -284,43 +284,48 @@ impl<'a> CoverageSpansGenerator<'a> {
/// de-duplicated `CoverageSpan`s. /// de-duplicated `CoverageSpan`s.
fn to_refined_spans(mut self) -> Vec<CoverageSpan> { fn to_refined_spans(mut self) -> Vec<CoverageSpan> {
while self.next_coverage_span() { while self.next_coverage_span() {
// For the first span we don't have `prev` set, so most of the
// span-processing steps don't make sense yet.
if self.some_prev.is_none() { if self.some_prev.is_none() {
debug!(" initial span"); debug!(" initial span");
self.maybe_push_macro_name_span(); self.maybe_push_macro_name_span();
} else if self.curr().is_mergeable(self.prev()) { continue;
debug!(" same bcb (and neither is a closure), merge with prev={:?}", self.prev()); }
// The remaining cases assume that `prev` and `curr` are set.
let prev = self.prev();
let curr = self.curr();
if curr.is_mergeable(prev) {
debug!(" same bcb (and neither is a closure), merge with prev={prev:?}");
let prev = self.take_prev(); let prev = self.take_prev();
self.curr_mut().merge_from(prev); self.curr_mut().merge_from(prev);
self.maybe_push_macro_name_span(); self.maybe_push_macro_name_span();
// Note that curr.span may now differ from curr_original_span // Note that curr.span may now differ from curr_original_span
} else if self.prev_ends_before_curr() { } else if prev.span.hi() <= curr.span.lo() {
debug!( debug!(
" different bcbs and disjoint spans, so keep curr for next iter, and add \ " different bcbs and disjoint spans, so keep curr for next iter, and add prev={prev:?}",
prev={:?}",
self.prev()
); );
let prev = self.take_prev(); let prev = self.take_prev();
self.push_refined_span(prev); self.push_refined_span(prev);
self.maybe_push_macro_name_span(); self.maybe_push_macro_name_span();
} else if self.prev().is_closure { } else if prev.is_closure {
// drop any equal or overlapping span (`curr`) and keep `prev` to test again in the // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
// next iter // next iter
debug!( debug!(
" curr overlaps a closure (prev). Drop curr and keep prev for next iter. \ " curr overlaps a closure (prev). Drop curr and keep prev for next iter. prev={prev:?}",
prev={:?}",
self.prev()
); );
self.take_curr(); self.take_curr();
} else if self.curr().is_closure { } else if curr.is_closure {
self.carve_out_span_for_closure(); self.carve_out_span_for_closure();
} else if self.prev_original_span == self.curr().span { } else if self.prev_original_span == curr.span {
// Note that this compares the new (`curr`) span to `prev_original_span`. // Note that this compares the new (`curr`) span to `prev_original_span`.
// In this branch, the actual span byte range of `prev_original_span` is not // In this branch, the actual span byte range of `prev_original_span` is not
// important. What is important is knowing whether the new `curr` span was // important. What is important is knowing whether the new `curr` span was
// **originally** the same as the original span of `prev()`. The original spans // **originally** the same as the original span of `prev()`. The original spans
// reflect their original sort order, and for equal spans, conveys a partial // reflect their original sort order, and for equal spans, conveys a partial
// ordering based on CFG dominator priority. // ordering based on CFG dominator priority.
if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() { if prev.is_macro_expansion() && curr.is_macro_expansion() {
// Macros that expand to include branching (such as // Macros that expand to include branching (such as
// `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or // `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
// `trace!()`) typically generate callee spans with identical // `trace!()`) typically generate callee spans with identical
@ -334,8 +339,7 @@ impl<'a> CoverageSpansGenerator<'a> {
debug!( debug!(
" curr and prev are part of a macro expansion, and curr has the same span \ " curr and prev are part of a macro expansion, and curr has the same span \
as prev, but is in a different bcb. Drop curr and keep prev for next iter. \ as prev, but is in a different bcb. Drop curr and keep prev for next iter. \
prev={:?}", prev={prev:?}",
self.prev()
); );
self.take_curr(); self.take_curr();
} else { } else {
@ -347,8 +351,8 @@ impl<'a> CoverageSpansGenerator<'a> {
} }
} }
debug!(" AT END, adding last prev={:?}", self.prev());
let prev = self.take_prev(); let prev = self.take_prev();
debug!(" AT END, adding last prev={prev:?}");
let pending_dups = self.pending_dups.split_off(0); let pending_dups = self.pending_dups.split_off(0);
for dup in pending_dups { for dup in pending_dups {
debug!(" ...adding at least one pending dup={:?}", dup); debug!(" ...adding at least one pending dup={:?}", dup);
@ -511,12 +515,6 @@ impl<'a> CoverageSpansGenerator<'a> {
self.prev().span.lo() > next_curr.span.lo() self.prev().span.lo() > next_curr.span.lo()
} }
/// Returns true if the curr span starts past the end of the prev span, which means they don't
/// overlap, so we now know the prev can be added to the refined coverage spans.
fn prev_ends_before_curr(&self) -> bool {
self.prev().span.hi() <= self.curr().span.lo()
}
/// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from /// If `prev`s span extends left of the closure (`curr`), carve out the closure's span from
/// `prev`'s span. (The closure's coverage counters will be injected when processing the /// `prev`'s span. (The closure's coverage counters will be injected when processing the
/// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span /// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span