coverage: Simplify initial creation of coverage spans
This commit is contained in:
parent
e16494469e
commit
319693a927
2 changed files with 32 additions and 45 deletions
|
@ -2,7 +2,7 @@ use std::cell::OnceCell;
|
||||||
|
|
||||||
use rustc_data_structures::graph::WithNumNodes;
|
use rustc_data_structures::graph::WithNumNodes;
|
||||||
use rustc_index::IndexVec;
|
use rustc_index::IndexVec;
|
||||||
use rustc_middle::mir::{self, AggregateKind, Rvalue, Statement, StatementKind};
|
use rustc_middle::mir;
|
||||||
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol, DUMMY_SP};
|
use rustc_span::{BytePos, ExpnKind, MacroKind, Span, Symbol, DUMMY_SP};
|
||||||
|
|
||||||
use super::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
|
use super::graph::{BasicCoverageBlock, CoverageGraph, START_BCB};
|
||||||
|
@ -75,29 +75,15 @@ struct CoverageSpan {
|
||||||
|
|
||||||
impl CoverageSpan {
|
impl CoverageSpan {
|
||||||
pub fn for_fn_sig(fn_sig_span: Span) -> Self {
|
pub fn for_fn_sig(fn_sig_span: Span) -> Self {
|
||||||
Self {
|
Self::new(fn_sig_span, fn_sig_span, START_BCB, false)
|
||||||
span: fn_sig_span,
|
|
||||||
expn_span: fn_sig_span,
|
|
||||||
current_macro_or_none: Default::default(),
|
|
||||||
bcb: START_BCB,
|
|
||||||
merged_spans: vec![fn_sig_span],
|
|
||||||
is_closure: false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_statement(
|
pub(super) fn new(
|
||||||
statement: &Statement<'_>,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
expn_span: Span,
|
expn_span: Span,
|
||||||
bcb: BasicCoverageBlock,
|
bcb: BasicCoverageBlock,
|
||||||
|
is_closure: bool,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let is_closure = match statement.kind {
|
|
||||||
StatementKind::Assign(box (_, Rvalue::Aggregate(box ref kind, _))) => {
|
|
||||||
matches!(kind, AggregateKind::Closure(_, _) | AggregateKind::Coroutine(_, _, _))
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
span,
|
span,
|
||||||
expn_span,
|
expn_span,
|
||||||
|
@ -108,17 +94,6 @@ impl CoverageSpan {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn for_terminator(span: Span, expn_span: Span, bcb: BasicCoverageBlock) -> Self {
|
|
||||||
Self {
|
|
||||||
span,
|
|
||||||
expn_span,
|
|
||||||
current_macro_or_none: Default::default(),
|
|
||||||
bcb,
|
|
||||||
merged_spans: vec![span],
|
|
||||||
is_closure: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn merge_from(&mut self, mut other: CoverageSpan) {
|
pub fn merge_from(&mut self, mut other: CoverageSpan) {
|
||||||
debug_assert!(self.is_mergeable(&other));
|
debug_assert!(self.is_mergeable(&other));
|
||||||
self.span = self.span.to(other.span);
|
self.span = self.span.to(other.span);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use rustc_data_structures::captures::Captures;
|
use rustc_data_structures::captures::Captures;
|
||||||
use rustc_middle::mir::{
|
use rustc_middle::mir::{
|
||||||
self, FakeReadCause, Statement, StatementKind, Terminator, TerminatorKind,
|
self, AggregateKind, FakeReadCause, Rvalue, Statement, StatementKind, Terminator,
|
||||||
|
TerminatorKind,
|
||||||
};
|
};
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
|
@ -59,24 +60,35 @@ fn bcb_to_initial_coverage_spans<'a, 'tcx>(
|
||||||
) -> impl Iterator<Item = CoverageSpan> + Captures<'a> + Captures<'tcx> {
|
) -> impl Iterator<Item = CoverageSpan> + Captures<'a> + Captures<'tcx> {
|
||||||
bcb_data.basic_blocks.iter().flat_map(move |&bb| {
|
bcb_data.basic_blocks.iter().flat_map(move |&bb| {
|
||||||
let data = &mir_body[bb];
|
let data = &mir_body[bb];
|
||||||
data.statements
|
|
||||||
.iter()
|
let statement_spans = data.statements.iter().filter_map(move |statement| {
|
||||||
.filter_map(move |statement| {
|
let expn_span = filtered_statement_span(statement)?;
|
||||||
filtered_statement_span(statement).map(|span| {
|
let span = function_source_span(expn_span, body_span);
|
||||||
CoverageSpan::for_statement(
|
|
||||||
statement,
|
Some(CoverageSpan::new(span, expn_span, bcb, is_closure(statement)))
|
||||||
function_source_span(span, body_span),
|
});
|
||||||
span,
|
|
||||||
bcb,
|
let terminator_span = Some(data.terminator()).into_iter().filter_map(move |terminator| {
|
||||||
)
|
let expn_span = filtered_terminator_span(terminator)?;
|
||||||
})
|
let span = function_source_span(expn_span, body_span);
|
||||||
})
|
|
||||||
.chain(filtered_terminator_span(data.terminator()).map(|span| {
|
Some(CoverageSpan::new(span, expn_span, bcb, false))
|
||||||
CoverageSpan::for_terminator(function_source_span(span, body_span), span, bcb)
|
});
|
||||||
}))
|
|
||||||
|
statement_spans.chain(terminator_span)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_closure(statement: &Statement<'_>) -> bool {
|
||||||
|
match statement.kind {
|
||||||
|
StatementKind::Assign(box (_, Rvalue::Aggregate(box ref agg_kind, _))) => match agg_kind {
|
||||||
|
AggregateKind::Closure(_, _) | AggregateKind::Coroutine(_, _, _) => true,
|
||||||
|
_ => false,
|
||||||
|
},
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// If the MIR `Statement` has a span contributive to computing coverage spans,
|
/// If the MIR `Statement` has a span contributive to computing coverage spans,
|
||||||
/// return it; otherwise return `None`.
|
/// return it; otherwise return `None`.
|
||||||
fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {
|
fn filtered_statement_span(statement: &Statement<'_>) -> Option<Span> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue