use super::*;
use rustc_data_structures::captures::Captures;
use rustc_middle::mir::coverage::*;
use rustc_middle::mir::{self, Body, Coverage, CoverageInfo};
use rustc_middle::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::def_id::DefId;
pub(crate) fn provide(providers: &mut Providers) {
providers.coverageinfo = |tcx, def_id| coverageinfo(tcx, def_id);
providers.covered_code_regions = |tcx, def_id| covered_code_regions(tcx, def_id);
}
struct CoverageVisitor {
max_counter_id: CounterId,
max_expression_id: ExpressionId,
}
impl CoverageVisitor {
#[inline(always)]
fn update_max_counter_id(&mut self, counter_id: CounterId) {
self.max_counter_id = self.max_counter_id.max(counter_id);
}
#[inline(always)]
fn update_max_expression_id(&mut self, expression_id: ExpressionId) {
self.max_expression_id = self.max_expression_id.max(expression_id);
}
fn update_from_expression_operand(&mut self, operand: Operand) {
match operand {
Operand::Counter(id) => self.update_max_counter_id(id),
Operand::Expression(id) => self.update_max_expression_id(id),
Operand::Zero => {}
}
}
fn visit_body(&mut self, body: &Body<'_>) {
for coverage in all_coverage_in_mir_body(body) {
self.visit_coverage(coverage);
}
}
fn visit_coverage(&mut self, coverage: &Coverage) {
match coverage.kind {
CoverageKind::Counter { id, .. } => self.update_max_counter_id(id),
CoverageKind::Expression { id, lhs, rhs, .. } => {
self.update_max_expression_id(id);
self.update_from_expression_operand(lhs);
self.update_from_expression_operand(rhs);
}
CoverageKind::Unreachable => {}
}
}
}
fn coverageinfo<'tcx>(tcx: TyCtxt<'tcx>, instance_def: ty::InstanceDef<'tcx>) -> CoverageInfo {
let mir_body = tcx.instance_mir(instance_def);
let mut coverage_visitor = CoverageVisitor {
max_counter_id: CounterId::START,
max_expression_id: ExpressionId::START,
};
coverage_visitor.visit_body(mir_body);
CoverageInfo {
num_counters: (coverage_visitor.max_counter_id + 1).as_u32(),
num_expressions: (coverage_visitor.max_expression_id + 1).as_u32(),
}
}
fn covered_code_regions(tcx: TyCtxt<'_>, def_id: DefId) -> Vec<&CodeRegion> {
let body = mir_body(tcx, def_id);
all_coverage_in_mir_body(body)
.flat_map(|coverage| coverage.code_regions.as_slice())
.collect()
}
fn all_coverage_in_mir_body<'a, 'tcx>(
body: &'a Body<'tcx>,
) -> impl Iterator<Item = &'a Coverage> + Captures<'tcx> {
body.basic_blocks.iter().flat_map(|bb_data| &bb_data.statements).filter_map(|statement| {
match statement.kind {
StatementKind::Coverage(box ref coverage) if !is_inlined(body, statement) => {
Some(coverage)
}
_ => None,
}
})
}
fn is_inlined(body: &Body<'_>, statement: &Statement<'_>) -> bool {
let scope_data = &body.source_scopes[statement.source_info.scope];
scope_data.inlined.is_some() || scope_data.inlined_parent_scope.is_some()
}
fn mir_body(tcx: TyCtxt<'_>, def_id: DefId) -> &mir::Body<'_> {
let def = ty::InstanceDef::Item(def_id);
tcx.instance_mir(def)
}