spans.rs 39.9 KB
Newer Older
1
use super::debug::term_type;
R
Rich Kadel 已提交
2
use super::graph::{BasicCoverageBlock, BasicCoverageBlockData, CoverageGraph, START_BCB};
3

4
use itertools::Itertools;
5
use rustc_data_structures::graph::WithNumNodes;
6
use rustc_middle::mir::spanview::source_range_no_file;
7
use rustc_middle::mir::{
8 9
    self, AggregateKind, BasicBlock, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
    TerminatorKind,
10
};
11
use rustc_middle::ty::TyCtxt;
12
use rustc_span::source_map::original_sp;
R
Rich Kadel 已提交
13
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol};
14

R
Rich Kadel 已提交
15
use std::cell::RefCell;
16 17 18
use std::cmp::Ordering;

#[derive(Debug, Copy, Clone)]
R
Rich Kadel 已提交
19
pub(super) enum CoverageStatement {
20 21 22 23 24
    Statement(BasicBlock, Span, usize),
    Terminator(BasicBlock, Span),
}

impl CoverageStatement {
25
    pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String {
26 27
        match *self {
            Self::Statement(bb, span, stmt_index) => {
28
                let stmt = &mir_body[bb].statements[stmt_index];
29 30
                format!(
                    "{}: @{}[{}]: {:?}",
L
lcnr 已提交
31
                    source_range_no_file(tcx, span),
32 33 34 35 36 37
                    bb.index(),
                    stmt_index,
                    stmt
                )
            }
            Self::Terminator(bb, span) => {
38
                let term = mir_body[bb].terminator();
39 40
                format!(
                    "{}: @{}.{}: {:?}",
L
lcnr 已提交
41
                    source_range_no_file(tcx, span),
42 43 44 45 46 47 48 49
                    bb.index(),
                    term_type(&term.kind),
                    term.kind
                )
            }
        }
    }

50
    pub fn span(&self) -> Span {
51
        match self {
52
            Self::Statement(_, span, _) | Self::Terminator(_, span) => *span,
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        }
    }
}

/// A BCB is deconstructed into one or more `Span`s. Each `Span` maps to a `CoverageSpan` that
/// references the originating BCB and one or more MIR `Statement`s and/or `Terminator`s.
/// Initially, the `Span`s come from the `Statement`s and `Terminator`s, but subsequent
/// transforms can combine adjacent `Span`s and `CoverageSpan` from the same BCB, merging the
/// `CoverageStatement` vectors, and the `Span`s to cover the extent of the combined `Span`s.
///
/// Note: A `CoverageStatement` merged into another CoverageSpan may come from a `BasicBlock` that
/// is not part of the `CoverageSpan` bcb if the statement was included because it's `Span` matches
/// or is subsumed by the `Span` associated with this `CoverageSpan`, and it's `BasicBlock`
/// `is_dominated_by()` the `BasicBlock`s in this `CoverageSpan`.
#[derive(Debug, Clone)]
R
Rich Kadel 已提交
68
pub(super) struct CoverageSpan {
69
    pub span: Span,
R
Rich Kadel 已提交
70
    pub expn_span: Span,
R
Rich Kadel 已提交
71
    pub current_macro_or_none: RefCell<Option<Option<Symbol>>>,
72
    pub bcb: BasicCoverageBlock,
73 74
    pub coverage_statements: Vec<CoverageStatement>,
    pub is_closure: bool,
75 76 77
}

impl CoverageSpan {
R
Rich Kadel 已提交
78
    pub fn for_fn_sig(fn_sig_span: Span) -> Self {
79 80
        Self {
            span: fn_sig_span,
R
Rich Kadel 已提交
81
            expn_span: fn_sig_span,
R
Rich Kadel 已提交
82
            current_macro_or_none: Default::default(),
83 84 85 86
            bcb: START_BCB,
            coverage_statements: vec![],
            is_closure: false,
        }
R
Rich Kadel 已提交
87 88
    }

89
    pub fn for_statement(
90
        statement: &Statement<'_>,
91
        span: Span,
R
Rich Kadel 已提交
92
        expn_span: Span,
93
        bcb: BasicCoverageBlock,
94 95 96 97
        bb: BasicBlock,
        stmt_index: usize,
    ) -> Self {
        let is_closure = match statement.kind {
98 99 100
            StatementKind::Assign(box (_, Rvalue::Aggregate(box ref kind, _))) => {
                matches!(kind, AggregateKind::Closure(_, _) | AggregateKind::Generator(_, _, _))
            }
101 102 103 104 105
            _ => false,
        };

        Self {
            span,
R
Rich Kadel 已提交
106
            expn_span,
R
Rich Kadel 已提交
107
            current_macro_or_none: Default::default(),
108
            bcb,
109 110 111 112 113
            coverage_statements: vec![CoverageStatement::Statement(bb, span, stmt_index)],
            is_closure,
        }
    }

114 115
    pub fn for_terminator(
        span: Span,
R
Rich Kadel 已提交
116
        expn_span: Span,
117 118 119
        bcb: BasicCoverageBlock,
        bb: BasicBlock,
    ) -> Self {
120 121
        Self {
            span,
R
Rich Kadel 已提交
122
            expn_span,
R
Rich Kadel 已提交
123
            current_macro_or_none: Default::default(),
124
            bcb,
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            coverage_statements: vec![CoverageStatement::Terminator(bb, span)],
            is_closure: false,
        }
    }

    pub fn merge_from(&mut self, mut other: CoverageSpan) {
        debug_assert!(self.is_mergeable(&other));
        self.span = self.span.to(other.span);
        self.coverage_statements.append(&mut other.coverage_statements);
    }

    pub fn cutoff_statements_at(&mut self, cutoff_pos: BytePos) {
        self.coverage_statements.retain(|covstmt| covstmt.span().hi() <= cutoff_pos);
        if let Some(highest_covstmt) =
            self.coverage_statements.iter().max_by_key(|covstmt| covstmt.span().hi())
        {
            self.span = self.span.with_hi(highest_covstmt.span().hi());
        }
    }

145
    #[inline]
146 147 148 149
    pub fn is_mergeable(&self, other: &Self) -> bool {
        self.is_in_same_bcb(other) && !(self.is_closure || other.is_closure)
    }

150
    #[inline]
151
    pub fn is_in_same_bcb(&self, other: &Self) -> bool {
152
        self.bcb == other.bcb
153
    }
154

155
    pub fn format<'tcx>(&self, tcx: TyCtxt<'tcx>, mir_body: &mir::Body<'tcx>) -> String {
R
Rich Kadel 已提交
156 157
        format!(
            "{}\n    {}",
L
lcnr 已提交
158
            source_range_no_file(tcx, self.span),
159
            self.format_coverage_statements(tcx, mir_body).replace('\n', "\n    "),
R
Rich Kadel 已提交
160 161 162
        )
    }

163
    pub fn format_coverage_statements<'tcx>(
164 165
        &self,
        tcx: TyCtxt<'tcx>,
166
        mir_body: &mir::Body<'tcx>,
167 168 169 170 171 172
    ) -> String {
        let mut sorted_coverage_statements = self.coverage_statements.clone();
        sorted_coverage_statements.sort_unstable_by_key(|covstmt| match *covstmt {
            CoverageStatement::Statement(bb, _, index) => (bb, index),
            CoverageStatement::Terminator(bb, _) => (bb, usize::MAX),
        });
173
        sorted_coverage_statements.iter().map(|covstmt| covstmt.format(tcx, mir_body)).join("\n")
174
    }
R
Rich Kadel 已提交
175

R
Rich Kadel 已提交
176
    /// If the span is part of a macro, returns the macro name symbol.
R
Rich Kadel 已提交
177
    pub fn current_macro(&self) -> Option<Symbol> {
R
Rich Kadel 已提交
178 179 180
        self.current_macro_or_none
            .borrow_mut()
            .get_or_insert_with(|| {
181 182
                if let ExpnKind::Macro(MacroKind::Bang, current_macro) =
                    self.expn_span.ctxt().outer_expn_data().kind
R
Rich Kadel 已提交
183 184 185 186 187 188
                {
                    return Some(current_macro);
                }
                None
            })
            .map(|symbol| symbol)
R
Rich Kadel 已提交
189 190 191 192 193
    }

    /// If the span is part of a macro, and the macro is visible (expands directly to the given
    /// body_span), returns the macro name symbol.
    pub fn visible_macro(&self, body_span: Span) -> Option<Symbol> {
C
Caio 已提交
194 195 196 197 198 199 200
        if let Some(current_macro) = self.current_macro() && self
            .expn_span
            .parent_callsite()
            .unwrap_or_else(|| bug!("macro must have a parent"))
            .ctxt() == body_span.ctxt()
        {
            return Some(current_macro);
R
Rich Kadel 已提交
201 202 203 204 205 206 207
        }
        None
    }

    pub fn is_macro_expansion(&self) -> bool {
        self.current_macro().is_some()
    }
208 209
}

210 211 212 213 214 215 216 217 218 219 220
/// Converts the initial set of `CoverageSpan`s (one per MIR `Statement` or `Terminator`) into a
/// minimal set of `CoverageSpan`s, using the BCB CFG to determine where it is safe and useful to:
///
///  * Remove duplicate source code coverage regions
///  * Merge spans that represent continuous (both in source code and control flow), non-branching
///    execution
///  * Carve out (leave uncovered) any span that will be counted by another MIR (notably, closures)
pub struct CoverageSpans<'a, 'tcx> {
    /// The MIR, used to look up `BasicBlockData`.
    mir_body: &'a mir::Body<'tcx>,

R
Rich Kadel 已提交
221 222 223
    /// A `Span` covering the signature of function for the MIR.
    fn_sig_span: Span,

224 225 226 227 228
    /// A `Span` covering the function body of the MIR (typically from left curly brace to right
    /// curly brace).
    body_span: Span,

    /// The BasicCoverageBlock Control Flow Graph (BCB CFG).
229
    basic_coverage_blocks: &'a CoverageGraph,
230 231 232 233 234 235 236 237 238 239

    /// The initial set of `CoverageSpan`s, sorted by `Span` (`lo` and `hi`) and by relative
    /// dominance between the `BasicCoverageBlock`s of equal `Span`s.
    sorted_spans_iter: Option<std::vec::IntoIter<CoverageSpan>>,

    /// The current `CoverageSpan` to compare to its `prev`, to possibly merge, discard, force the
    /// discard of the `prev` (and or `pending_dups`), or keep both (with `prev` moved to
    /// `pending_dups`). If `curr` is not discarded or merged, it becomes `prev` for the next
    /// iteration.
    some_curr: Option<CoverageSpan>,
240

241 242 243
    /// The original `span` for `curr`, in case `curr.span()` is modified. The `curr_original_span`
    /// **must not be mutated** (except when advancing to the next `curr`), even if `curr.span()`
    /// is mutated.
244
    curr_original_span: Span,
245

246 247 248 249
    /// The CoverageSpan from a prior iteration; typically assigned from that iteration's `curr`.
    /// If that `curr` was discarded, `prev` retains its value from the previous iteration.
    some_prev: Option<CoverageSpan>,

250 251 252
    /// Assigned from `curr_original_span` from the previous iteration. The `prev_original_span`
    /// **must not be mutated** (except when advancing to the next `prev`), even if `prev.span()`
    /// is mutated.
253 254
    prev_original_span: Span,

R
Rich Kadel 已提交
255 256 257
    /// A copy of the expn_span from the prior iteration.
    prev_expn_span: Option<Span>,

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    /// One or more `CoverageSpan`s with the same `Span` but different `BasicCoverageBlock`s, and
    /// no `BasicCoverageBlock` in this list dominates another `BasicCoverageBlock` in the list.
    /// If a new `curr` span also fits this criteria (compared to an existing list of
    /// `pending_dups`), that `curr` `CoverageSpan` moves to `prev` before possibly being added to
    /// the `pending_dups` list, on the next iteration. As a result, if `prev` and `pending_dups`
    /// have the same `Span`, the criteria for `pending_dups` holds for `prev` as well: a `prev`
    /// with a matching `Span` does not dominate any `pending_dup` and no `pending_dup` dominates a
    /// `prev` with a matching `Span`)
    pending_dups: Vec<CoverageSpan>,

    /// The final `CoverageSpan`s to add to the coverage map. A `Counter` or `Expression`
    /// will also be injected into the MIR for each `CoverageSpan`.
    refined_spans: Vec<CoverageSpan>,
}

impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
R
Rich Kadel 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    /// Generate a minimal set of `CoverageSpan`s, each representing a contiguous code region to be
    /// counted.
    ///
    /// The basic steps are:
    ///
    /// 1. Extract an initial set of spans from the `Statement`s and `Terminator`s of each
    ///    `BasicCoverageBlockData`.
    /// 2. Sort the spans by span.lo() (starting position). Spans that start at the same position
    ///    are sorted with longer spans before shorter spans; and equal spans are sorted
    ///    (deterministically) based on "dominator" relationship (if any).
    /// 3. Traverse the spans in sorted order to identify spans that can be dropped (for instance,
    ///    if another span or spans are already counting the same code region), or should be merged
    ///    into a broader combined span (because it represents a contiguous, non-branching, and
    ///    uninterrupted region of source code).
    ///
    ///    Closures are exposed in their enclosing functions as `Assign` `Rvalue`s, and since
    ///    closures have their own MIR, their `Span` in their enclosing function should be left
    ///    "uncovered".
    ///
    /// Note the resulting vector of `CoverageSpan`s may not be fully sorted (and does not need
    /// to be).
R
Rich Kadel 已提交
295
    pub(super) fn generate_coverage_spans(
296
        mir_body: &'a mir::Body<'tcx>,
297
        fn_sig_span: Span, // Ensured to be same SourceFile and SyntaxContext as `body_span`
298
        body_span: Span,
299
        basic_coverage_blocks: &'a CoverageGraph,
300 301 302
    ) -> Vec<CoverageSpan> {
        let mut coverage_spans = CoverageSpans {
            mir_body,
303
            fn_sig_span,
304
            body_span,
305 306 307 308 309 310 311
            basic_coverage_blocks,
            sorted_spans_iter: None,
            refined_spans: Vec::with_capacity(basic_coverage_blocks.num_nodes() * 2),
            some_curr: None,
            curr_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)),
            some_prev: None,
            prev_original_span: Span::with_root_ctxt(BytePos(0), BytePos(0)),
R
Rich Kadel 已提交
312
            prev_expn_span: None,
313 314 315 316 317 318 319 320
            pending_dups: Vec::new(),
        };

        let sorted_spans = coverage_spans.mir_to_initial_sorted_coverage_spans();

        coverage_spans.sorted_spans_iter = Some(sorted_spans.into_iter());

        coverage_spans.to_refined_spans()
321 322
    }

323 324
    fn mir_to_initial_sorted_coverage_spans(&self) -> Vec<CoverageSpan> {
        let mut initial_spans = Vec::<CoverageSpan>::with_capacity(self.mir_body.num_nodes() * 2);
325
        for (bcb, bcb_data) in self.basic_coverage_blocks.iter_enumerated() {
326
            initial_spans.extend(self.bcb_to_initial_coverage_spans(bcb, bcb_data));
327 328 329 330 331 332 333 334
        }

        if initial_spans.is_empty() {
            // This can happen if, for example, the function is unreachable (contains only a
            // `BasicBlock`(s) with an `Unreachable` terminator).
            return initial_spans;
        }

R
Rich Kadel 已提交
335 336
        initial_spans.push(CoverageSpan::for_fn_sig(self.fn_sig_span));

337 338 339 340 341 342 343 344 345 346
        initial_spans.sort_unstable_by(|a, b| {
            if a.span.lo() == b.span.lo() {
                if a.span.hi() == b.span.hi() {
                    if a.is_in_same_bcb(b) {
                        Some(Ordering::Equal)
                    } else {
                        // Sort equal spans by dominator relationship, in reverse order (so
                        // dominators always come after the dominated equal spans). When later
                        // comparing two spans in order, the first will either dominate the second,
                        // or they will have no dominator relationship.
347
                        self.basic_coverage_blocks.dominators().rank_partial_cmp(b.bcb, a.bcb)
348 349 350
                    }
                } else {
                    // Sort hi() in reverse order so shorter spans are attempted after longer spans.
351 352 353 354
                    // This guarantees that, if a `prev` span overlaps, and is not equal to, a
                    // `curr` span, the prev span either extends further left of the curr span, or
                    // they start at the same position and the prev span extends further right of
                    // the end of the curr span.
355 356 357 358
                    b.span.hi().partial_cmp(&a.span.hi())
                }
            } else {
                a.span.lo().partial_cmp(&b.span.lo())
359
            }
360 361 362
            .unwrap()
        });

363
        initial_spans
364 365 366 367 368 369
    }

    /// Iterate through the sorted `CoverageSpan`s, and return the refined list of merged and
    /// de-duplicated `CoverageSpan`s.
    fn to_refined_spans(mut self) -> Vec<CoverageSpan> {
        while self.next_coverage_span() {
R
Rich Kadel 已提交
370 371 372 373
            if self.some_prev.is_none() {
                debug!("  initial span");
                self.check_invoked_macro_name_span();
            } else if self.curr().is_mergeable(self.prev()) {
374 375 376
                debug!("  same bcb (and neither is a closure), merge with prev={:?}", self.prev());
                let prev = self.take_prev();
                self.curr_mut().merge_from(prev);
R
Rich Kadel 已提交
377
                self.check_invoked_macro_name_span();
378 379 380 381 382 383 384 385
            // Note that curr.span may now differ from curr_original_span
            } else if self.prev_ends_before_curr() {
                debug!(
                    "  different bcbs and disjoint spans, so keep curr for next iter, and add \
                    prev={:?}",
                    self.prev()
                );
                let prev = self.take_prev();
R
Rich Kadel 已提交
386 387
                self.push_refined_span(prev);
                self.check_invoked_macro_name_span();
388 389 390 391 392 393 394 395
            } else if self.prev().is_closure {
                // drop any equal or overlapping span (`curr`) and keep `prev` to test again in the
                // next iter
                debug!(
                    "  curr overlaps a closure (prev). Drop curr and keep prev for next iter. \
                    prev={:?}",
                    self.prev()
                );
R
Rich Kadel 已提交
396
                self.take_curr();
397 398 399
            } else if self.curr().is_closure {
                self.carve_out_span_for_closure();
            } else if self.prev_original_span == self.curr().span {
400 401 402 403 404 405
                // 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
                // important. What is important is knowing whether the new `curr` span was
                // **originally** the same as the original span of `prev()`. The original spans
                // reflect their original sort order, and for equal spans, conveys a partial
                // ordering based on CFG dominator priority.
R
Rich Kadel 已提交
406
                if self.prev().is_macro_expansion() && self.curr().is_macro_expansion() {
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
                    // Macros that expand to include branching (such as
                    // `assert_eq!()`, `assert_ne!()`, `info!()`, `debug!()`, or
                    // `trace!()) typically generate callee spans with identical
                    // ranges (typically the full span of the macro) for all
                    // `BasicBlocks`. This makes it impossible to distinguish
                    // the condition (`if val1 != val2`) from the optional
                    // branched statements (such as the call to `panic!()` on
                    // assert failure). In this case it is better (or less
                    // worse) to drop the optional branch bcbs and keep the
                    // non-conditional statements, to count when reached.
                    debug!(
                        "  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. \
                        prev={:?}",
                        self.prev()
                    );
                    self.take_curr();
                } else {
                    self.hold_pending_dups_unless_dominated();
                }
427 428
            } else {
                self.cutoff_prev_at_overlapping_curr();
R
Rich Kadel 已提交
429
                self.check_invoked_macro_name_span();
430 431
            }
        }
432

433
        debug!("    AT END, adding last prev={:?}", self.prev());
434
        let prev = self.take_prev();
R
Rich Kadel 已提交
435
        let pending_dups = self.pending_dups.split_off(0);
436
        for dup in pending_dups {
437
            debug!("    ...adding at least one pending dup={:?}", dup);
R
Rich Kadel 已提交
438
            self.push_refined_span(dup);
439
        }
R
Rich Kadel 已提交
440 441

        // Async functions wrap a closure that implements the body to be executed. The enclosing
R
Rich Kadel 已提交
442 443 444 445 446 447
        // function is called and returns an `impl Future` without initially executing any of the
        // body. To avoid showing the return from the enclosing function as a "covered" return from
        // the closure, the enclosing function's `TerminatorKind::Return`s `CoverageSpan` is
        // excluded. The closure's `Return` is the only one that will be counted. This provides
        // adequate coverage, and more intuitive counts. (Avoids double-counting the closing brace
        // of the function body.)
R
Rich Kadel 已提交
448
        let body_ends_with_closure = if let Some(last_covspan) = self.refined_spans.last() {
R
Rich Kadel 已提交
449 450 451 452 453 454
            last_covspan.is_closure && last_covspan.span.hi() == self.body_span.hi()
        } else {
            false
        };

        if !body_ends_with_closure {
R
Rich Kadel 已提交
455
            self.push_refined_span(prev);
456
        }
457

458 459 460
        // Remove `CoverageSpan`s derived from closures, originally added to ensure the coverage
        // regions for the current function leave room for the closure's own coverage regions
        // (injected separately, from the closure's own MIR).
R
Rich Kadel 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
        self.refined_spans.retain(|covspan| !covspan.is_closure);
        self.refined_spans
    }

    fn push_refined_span(&mut self, covspan: CoverageSpan) {
        let len = self.refined_spans.len();
        if len > 0 {
            let last = &mut self.refined_spans[len - 1];
            if last.is_mergeable(&covspan) {
                debug!(
                    "merging new refined span with last refined span, last={:?}, covspan={:?}",
                    last, covspan
                );
                last.merge_from(covspan);
                return;
            }
        }
        self.refined_spans.push(covspan)
    }

    fn check_invoked_macro_name_span(&mut self) {
        if let Some(visible_macro) = self.curr().visible_macro(self.body_span) {
            if self.prev_expn_span.map_or(true, |prev_expn_span| {
                self.curr().expn_span.ctxt() != prev_expn_span.ctxt()
            }) {
                let merged_prefix_len = self.curr_original_span.lo() - self.curr().span.lo();
R
Rich Kadel 已提交
487 488
                let after_macro_bang =
                    merged_prefix_len + BytePos(visible_macro.as_str().bytes().count() as u32 + 1);
R
Rich Kadel 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501
                let mut macro_name_cov = self.curr().clone();
                self.curr_mut().span =
                    self.curr().span.with_lo(self.curr().span.lo() + after_macro_bang);
                macro_name_cov.span =
                    macro_name_cov.span.with_hi(macro_name_cov.span.lo() + after_macro_bang);
                debug!(
                    "  and curr starts a new macro expansion, so add a new span just for \
                            the macro `{}!`, new span={:?}",
                    visible_macro, macro_name_cov
                );
                self.push_refined_span(macro_name_cov);
            }
        }
502 503
    }

504
    // Generate a set of `CoverageSpan`s from the filtered set of `Statement`s and `Terminator`s of
505
    // the `BasicBlock`(s) in the given `BasicCoverageBlockData`. One `CoverageSpan` is generated
506 507 508
    // for each `Statement` and `Terminator`. (Note that subsequent stages of coverage analysis will
    // merge some `CoverageSpan`s, at which point a `CoverageSpan` may represent multiple
    // `Statement`s and/or `Terminator`s.)
509 510 511 512 513 514 515
    fn bcb_to_initial_coverage_spans(
        &self,
        bcb: BasicCoverageBlock,
        bcb_data: &'a BasicCoverageBlockData,
    ) -> Vec<CoverageSpan> {
        bcb_data
            .basic_blocks
516
            .iter()
R
Rich Kadel 已提交
517
            .flat_map(|&bb| {
518 519 520 521 522
                let data = &self.mir_body[bb];
                data.statements
                    .iter()
                    .enumerate()
                    .filter_map(move |(index, statement)| {
523 524 525 526 527 528 529 530 531 532
                        filtered_statement_span(statement).map(|span| {
                            CoverageSpan::for_statement(
                                statement,
                                function_source_span(span, self.body_span),
                                span,
                                bcb,
                                bb,
                                index,
                            )
                        })
533
                    })
534 535 536 537 538 539 540 541
                    .chain(filtered_terminator_span(data.terminator()).map(|span| {
                        CoverageSpan::for_terminator(
                            function_source_span(span, self.body_span),
                            span,
                            bcb,
                            bb,
                        )
                    }))
542 543
            })
            .collect()
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    }

    fn curr(&self) -> &CoverageSpan {
        self.some_curr
            .as_ref()
            .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
    }

    fn curr_mut(&mut self) -> &mut CoverageSpan {
        self.some_curr
            .as_mut()
            .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
    }

    fn prev(&self) -> &CoverageSpan {
        self.some_prev
            .as_ref()
            .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
    }

    fn prev_mut(&mut self) -> &mut CoverageSpan {
        self.some_prev
            .as_mut()
            .unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
    }

    fn take_prev(&mut self) -> CoverageSpan {
        self.some_prev.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_prev"))
    }

    /// If there are `pending_dups` but `prev` is not a matching dup (`prev.span` doesn't match the
    /// `pending_dups` spans), then one of the following two things happened during the previous
    /// iteration:
577 578 579 580
    ///   * the previous `curr` span (which is now `prev`) was not a duplicate of the pending_dups
    ///     (in which case there should be at least two spans in `pending_dups`); or
    ///   * the `span` of `prev` was modified by `curr_mut().merge_from(prev)` (in which case
    ///     `pending_dups` could have as few as one span)
581 582 583
    /// In either case, no more spans will match the span of `pending_dups`, so
    /// add the `pending_dups` if they don't overlap `curr`, and clear the list.
    fn check_pending_dups(&mut self) {
C
Caio 已提交
584 585 586 587 588 589 590 591 592 593
        if let Some(dup) = self.pending_dups.last() && dup.span != self.prev().span {
            debug!(
                "    SAME spans, but pending_dups are NOT THE SAME, so BCBs matched on \
                previous iteration, or prev started a new disjoint span"
            );
            if dup.span.hi() <= self.curr().span.lo() {
                let pending_dups = self.pending_dups.split_off(0);
                for dup in pending_dups.into_iter() {
                    debug!("    ...adding at least one pending={:?}", dup);
                    self.push_refined_span(dup);
594
                }
C
Caio 已提交
595 596
            } else {
                self.pending_dups.clear();
597 598 599 600 601 602 603
            }
        }
    }

    /// Advance `prev` to `curr` (if any), and `curr` to the next `CoverageSpan` in sorted order.
    fn next_coverage_span(&mut self) -> bool {
        if let Some(curr) = self.some_curr.take() {
R
Rich Kadel 已提交
604
            self.prev_expn_span = Some(curr.expn_span);
605 606 607
            self.some_prev = Some(curr);
            self.prev_original_span = self.curr_original_span;
        }
608
        while let Some(curr) = self.sorted_spans_iter.as_mut().unwrap().next() {
609
            debug!("FOR curr={:?}", curr);
R
Rich Kadel 已提交
610
            if self.some_prev.is_some() && self.prev_starts_after_next(&curr) {
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
                debug!(
                    "  prev.span starts after curr.span, so curr will be dropped (skipping past \
                    closure?); prev={:?}",
                    self.prev()
                );
            } else {
                // Save a copy of the original span for `curr` in case the `CoverageSpan` is changed
                // by `self.curr_mut().merge_from(prev)`.
                self.curr_original_span = curr.span;
                self.some_curr.replace(curr);
                self.check_pending_dups();
                return true;
            }
        }
        false
    }

    /// If called, then the next call to `next_coverage_span()` will *not* update `prev` with the
    /// `curr` coverage span.
R
Rich Kadel 已提交
630 631
    fn take_curr(&mut self) -> CoverageSpan {
        self.some_curr.take().unwrap_or_else(|| bug!("invalid attempt to unwrap a None some_curr"))
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
    }

    /// Returns true if the curr span should be skipped because prev has already advanced beyond the
    /// end of curr. This can only happen if a prior iteration updated `prev` to skip past a region
    /// of code, such as skipping past a closure.
    fn prev_starts_after_next(&self, next_curr: &CoverageSpan) -> bool {
        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()
    }

R
Rich Kadel 已提交
647 648 649 650 651
    /// 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
    /// closure's own MIR.) Add the portion of the span to the left of the closure; and if the span
    /// extends to the right of the closure, update `prev` to that portion of the span. For any
    /// `pending_dups`, repeat the same process.
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
    fn carve_out_span_for_closure(&mut self) {
        let curr_span = self.curr().span;
        let left_cutoff = curr_span.lo();
        let right_cutoff = curr_span.hi();
        let has_pre_closure_span = self.prev().span.lo() < right_cutoff;
        let has_post_closure_span = self.prev().span.hi() > right_cutoff;
        let mut pending_dups = self.pending_dups.split_off(0);
        if has_pre_closure_span {
            let mut pre_closure = self.prev().clone();
            pre_closure.span = pre_closure.span.with_hi(left_cutoff);
            debug!("  prev overlaps a closure. Adding span for pre_closure={:?}", pre_closure);
            if !pending_dups.is_empty() {
                for mut dup in pending_dups.iter().cloned() {
                    dup.span = dup.span.with_hi(left_cutoff);
                    debug!("    ...and at least one pre_closure dup={:?}", dup);
R
Rich Kadel 已提交
667
                    self.push_refined_span(dup);
668 669
                }
            }
R
Rich Kadel 已提交
670
            self.push_refined_span(pre_closure);
671 672
        }
        if has_post_closure_span {
673 674 675
            // Mutate `prev.span()` to start after the closure (and discard curr).
            // (**NEVER** update `prev_original_span` because it affects the assumptions
            // about how the `CoverageSpan`s are ordered.)
676
            self.prev_mut().span = self.prev().span.with_lo(right_cutoff);
677
            debug!("  Mutated prev.span to start after the closure. prev={:?}", self.prev());
678
            for dup in pending_dups.iter_mut() {
679
                debug!("    ...and at least one overlapping dup={:?}", dup);
680 681 682
                dup.span = dup.span.with_lo(right_cutoff);
            }
            self.pending_dups.append(&mut pending_dups);
R
Rich Kadel 已提交
683
            let closure_covspan = self.take_curr();
R
Rich Kadel 已提交
684
            self.push_refined_span(closure_covspan); // since self.prev() was already updated
685 686 687 688 689
        } else {
            pending_dups.clear();
        }
    }

690
    /// Called if `curr.span` equals `prev_original_span` (and potentially equal to all
691 692 693 694 695 696 697 698
    /// `pending_dups` spans, if any). Keep in mind, `prev.span()` may have been changed.
    /// If prev.span() was merged into other spans (with matching BCB, for instance),
    /// `prev.span.hi()` will be greater than (further right of) `prev_original_span.hi()`.
    /// If prev.span() was split off to the right of a closure, prev.span().lo() will be
    /// greater than prev_original_span.lo(). The actual span of `prev_original_span` is
    /// not as important as knowing that `prev()` **used to have the same span** as `curr(),
    /// which means their sort order is still meaningful for determinating the dominator
    /// relationship.
699
    ///
700 701 702 703 704
    /// When two `CoverageSpan`s have the same `Span`, dominated spans can be discarded; but if
    /// neither `CoverageSpan` dominates the other, both (or possibly more than two) are held,
    /// until their disposition is determined. In this latter case, the `prev` dup is moved into
    /// `pending_dups` so the new `curr` dup can be moved to `prev` for the next iteration.
    fn hold_pending_dups_unless_dominated(&mut self) {
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
        // Equal coverage spans are ordered by dominators before dominated (if any), so it should be
        // impossible for `curr` to dominate any previous `CoverageSpan`.
        debug_assert!(!self.span_bcb_is_dominated_by(self.prev(), self.curr()));

        let initial_pending_count = self.pending_dups.len();
        if initial_pending_count > 0 {
            let mut pending_dups = self.pending_dups.split_off(0);
            pending_dups.retain(|dup| !self.span_bcb_is_dominated_by(self.curr(), dup));
            self.pending_dups.append(&mut pending_dups);
            if self.pending_dups.len() < initial_pending_count {
                debug!(
                    "  discarded {} of {} pending_dups that dominated curr",
                    initial_pending_count - self.pending_dups.len(),
                    initial_pending_count
                );
            }
        }

        if self.span_bcb_is_dominated_by(self.curr(), self.prev()) {
724
            debug!(
725
                "  different bcbs but SAME spans, and prev dominates curr. Discard prev={:?}",
726 727
                self.prev()
            );
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
            self.cutoff_prev_at_overlapping_curr();
        // If one span dominates the other, assocate the span with the code from the dominated
        // block only (`curr`), and discard the overlapping portion of the `prev` span. (Note
        // that if `prev.span` is wider than `prev_original_span`, a `CoverageSpan` will still
        // be created for `prev`s block, for the non-overlapping portion, left of `curr.span`.)
        //
        // For example:
        //     match somenum {
        //         x if x < 1 => { ... }
        //     }...
        //
        // The span for the first `x` is referenced by both the pattern block (every time it is
        // evaluated) and the arm code (only when matched). The counter will be applied only to
        // the dominated block. This allows coverage to track and highlight things like the
        // assignment of `x` above, if the branch is matched, making `x` available to the arm
        // code; and to track and highlight the question mark `?` "try" operator at the end of
        // a function call returning a `Result`, so the `?` is covered when the function returns
        // an `Err`, and not counted as covered if the function always returns `Ok`.
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
        } else {
            // Save `prev` in `pending_dups`. (`curr` will become `prev` in the next iteration.)
            // If the `curr` CoverageSpan is later discarded, `pending_dups` can be discarded as
            // well; but if `curr` is added to refined_spans, the `pending_dups` will also be added.
            debug!(
                "  different bcbs but SAME spans, and neither dominates, so keep curr for \
                next iter, and, pending upcoming spans (unless overlapping) add prev={:?}",
                self.prev()
            );
            let prev = self.take_prev();
            self.pending_dups.push(prev);
        }
    }

    /// `curr` overlaps `prev`. If `prev`s span extends left of `curr`s span, keep _only_
    /// statements that end before `curr.lo()` (if any), and add the portion of the
    /// combined span for those statements. Any other statements have overlapping spans
    /// that can be ignored because `curr` and/or other upcoming statements/spans inside
    /// the overlap area will produce their own counters. This disambiguation process
    /// avoids injecting multiple counters for overlapping spans, and the potential for
    /// double-counting.
    fn cutoff_prev_at_overlapping_curr(&mut self) {
        debug!(
            "  different bcbs, overlapping spans, so ignore/drop pending and only add prev \
770
            if it has statements that end before curr; prev={:?}",
771 772 773 774 775 776 777 778 779 780
            self.prev()
        );
        if self.pending_dups.is_empty() {
            let curr_span = self.curr().span;
            self.prev_mut().cutoff_statements_at(curr_span.lo());
            if self.prev().coverage_statements.is_empty() {
                debug!("  ... no non-overlapping statements to add");
            } else {
                debug!("  ... adding modified prev={:?}", self.prev());
                let prev = self.take_prev();
R
Rich Kadel 已提交
781
                self.push_refined_span(prev);
782 783 784 785
            }
        } else {
            // with `pending_dups`, `prev` cannot have any statements that don't overlap
            self.pending_dups.clear();
786 787
        }
    }
788 789

    fn span_bcb_is_dominated_by(&self, covspan: &CoverageSpan, dom_covspan: &CoverageSpan) -> bool {
790
        self.basic_coverage_blocks.is_dominated_by(covspan.bcb, dom_covspan.bcb)
791
    }
792 793
}

794 795
/// If the MIR `Statement` has a span contributive to computing coverage spans,
/// return it; otherwise return `None`.
796
pub(super) fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {
797 798 799 800 801 802 803 804 805 806
    match statement.kind {
        // These statements have spans that are often outside the scope of the executed source code
        // for their parent `BasicBlock`.
        StatementKind::StorageLive(_)
        | StatementKind::StorageDead(_)
        // Coverage should not be encountered, but don't inject coverage coverage
        | StatementKind::Coverage(_)
        // Ignore `Nop`s
        | StatementKind::Nop => None,

807 808 809
        // FIXME(#78546): MIR InstrumentCoverage - Can the source_info.span for `FakeRead`
        // statements be more consistent?
        //
810 811 812 813 814 815 816 817 818 819 820 821
        // FakeReadCause::ForGuardBinding, in this example:
        //     match somenum {
        //         x if x < 1 => { ... }
        //     }...
        // The BasicBlock within the match arm code included one of these statements, but the span
        // for it covered the `1` in this source. The actual statements have nothing to do with that
        // source span:
        //     FakeRead(ForGuardBinding, _4);
        // where `_4` is:
        //     _4 = &_1; (at the span for the first `x`)
        // and `_1` is the `Place` for `somenum`.
        //
822
        // If and when the Issue is resolved, remove this special case match pattern:
R
Roxane 已提交
823
        StatementKind::FakeRead(box (cause, _)) if cause == FakeReadCause::ForGuardBinding => None,
824 825

        // Retain spans from all other statements
R
Roxane 已提交
826
        StatementKind::FakeRead(box (_, _)) // Not including `ForGuardBinding`
K
kadmin 已提交
827
        | StatementKind::CopyNonOverlapping(..)
828 829 830 831
        | StatementKind::Assign(_)
        | StatementKind::SetDiscriminant { .. }
        | StatementKind::Retag(_, _)
        | StatementKind::AscribeUserType(_, _) => {
832
            Some(statement.source_info.span)
833 834 835 836
        }
    }
}

837 838
/// If the MIR `Terminator` has a span contributive to computing coverage spans,
/// return it; otherwise return `None`.
839
pub(super) fn filtered_terminator_span(terminator: &Terminator<'_>) -> Option<Span> {
840 841 842 843 844 845
    match terminator.kind {
        // These terminators have spans that don't positively contribute to computing a reasonable
        // span of actually executed source code. (For example, SwitchInt terminators extracted from
        // an `if condition { block }` has a span that includes the executed block, if true,
        // but for coverage, the code region executed, up to *and* through the SwitchInt,
        // actually stops before the if's block.)
846
        TerminatorKind::Unreachable // Unreachable blocks are not connected to the MIR CFG
847 848 849 850 851
        | TerminatorKind::Assert { .. }
        | TerminatorKind::Drop { .. }
        | TerminatorKind::DropAndReplace { .. }
        | TerminatorKind::SwitchInt { .. }
        // For `FalseEdge`, only the `real` branch is taken, so it is similar to a `Goto`.
R
Rich Kadel 已提交
852 853
        | TerminatorKind::FalseEdge { .. }
        | TerminatorKind::Goto { .. } => None,
854

855 856 857 858 859 860 861 862
        // Call `func` operand can have a more specific span when part of a chain of calls
        | TerminatorKind::Call { ref func, .. } => {
            let mut span = terminator.source_info.span;
            if let mir::Operand::Constant(box constant) = func {
                if constant.span.lo() > span.lo() {
                    span = span.with_lo(constant.span.lo());
                }
            }
863
            Some(span)
864 865
        }

866 867 868 869 870 871 872 873
        // Retain spans from all other terminators
        TerminatorKind::Resume
        | TerminatorKind::Abort
        | TerminatorKind::Return
        | TerminatorKind::Yield { .. }
        | TerminatorKind::GeneratorDrop
        | TerminatorKind::FalseUnwind { .. }
        | TerminatorKind::InlineAsm { .. } => {
874
            Some(terminator.source_info.span)
875 876 877 878
        }
    }
}

879 880 881 882
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
/// within the function's body source. This span is guaranteed to be contained
/// within, or equal to, the `body_span`. If the extrapolated span is not
/// contained within the `body_span`, the `body_span` is returned.
R
Rich Kadel 已提交
883
///
884 885
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
/// etc.).
886
#[inline]
887
pub(super) fn function_source_span(span: Span, body_span: Span) -> Span {
R
Rich Kadel 已提交
888
    let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
889
    if body_span.contains(original_span) { original_span } else { body_span }
890
}