Skip to content

Commit

Permalink
coverage. Instrument mcdc for pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuUx committed Jul 11, 2024
1 parent b363e65 commit 443429a
Show file tree
Hide file tree
Showing 14 changed files with 3,552 additions and 12 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ impl Default for CandidateCovId {
}

impl CandidateCovId {
/// Return `true` is this `CandidateCovId` is assigned properly through coverage mechanism
/// and can be mapped as a decision.
pub fn is_valid(&self) -> bool {
*self != Self::default()
}
Expand Down
29 changes: 25 additions & 4 deletions compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod matching;
use matching::{LateMatchingState, MatchingDecisionCtx};
use std::cell::Cell;
use std::collections::VecDeque;
use std::rc::Rc;
Expand Down Expand Up @@ -174,14 +176,17 @@ impl BooleanDecisionCtx {
#[derive(Debug)]
enum DecisionCtx {
Boolean(BooleanDecisionCtx),
#[allow(unused)]
Matching,
Matching(MatchingDecisionCtx),
}

impl DecisionCtx {
fn new_boolean(id: DecisionId) -> Self {
Self::Boolean(BooleanDecisionCtx::new(id))
}

fn new_matching(ids: &[DecisionId]) -> Self {
Self::Matching(MatchingDecisionCtx::new(ids))
}
}

pub(crate) struct MCDCStateGuard {
Expand Down Expand Up @@ -270,6 +275,7 @@ pub(crate) struct MCDCInfoBuilder {
normal_branch_spans: Vec<MCDCBranchSpan>,
mcdc_targets: FxIndexMap<DecisionId, MCDCTargetInfo>,
state_stack: Vec<MCDCState>,
late_matching_state: LateMatchingState,
decision_id_gen: DecisionIdGen,
}

Expand All @@ -279,6 +285,7 @@ impl MCDCInfoBuilder {
normal_branch_spans: vec![],
mcdc_targets: FxIndexMap::default(),
state_stack: vec![],
late_matching_state: Default::default(),
decision_id_gen: DecisionIdGen::default(),
}
}
Expand All @@ -293,6 +300,11 @@ impl MCDCInfoBuilder {
&mut self.state_stack[current_idx]
}

fn current_processing_ctx_mut(&mut self) -> Option<&mut DecisionCtx> {
self.ensure_active_state();
self.state_stack.last_mut().and_then(|state| state.current_ctx.as_mut())
}

fn ensure_active_state(&mut self) {
let mut active_state_idx = None;
// Down to the first non-stashed state or non-empty state, which can be ensured to be
Expand Down Expand Up @@ -445,17 +457,26 @@ impl MCDCInfoBuilder {

let (id, decision, conditions) = ctx.into_done();
let info = MCDCTargetInfo { decision, conditions, nested_decisions_id };
let entry_decision_id = self.append_mcdc_info(tcx, id, info).then_some(id);
self.on_ctx_finished(tcx, entry_decision_id);
if self.late_matching_state.is_guard_decision(id) {
self.late_matching_state.add_guard_decision(id, info);
} else {
let entry_id = self.append_mcdc_info(tcx, id, info).then_some(id);
self.on_ctx_finished(tcx, entry_id)
}
}

pub(crate) fn into_done(
self,
) -> (Vec<MCDCBranchSpan>, Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>) {
assert!(
!self.has_processing_decision() && self.late_matching_state.is_empty(),
"has unfinished decisions"
);
let MCDCInfoBuilder {
normal_branch_spans,
mcdc_targets,
state_stack: _,
late_matching_state: _,
decision_id_gen: _,
} = self;

Expand Down
Loading

0 comments on commit 443429a

Please sign in to comment.