Skip to content

Commit

Permalink
coverage. Trace Candidate and MatchPair with coverage id
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuUx committed Jul 10, 2024
1 parent 7b095a9 commit b363e65
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
81 changes: 81 additions & 0 deletions compiler/rustc_middle/src/mir/coverage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,84 @@ impl MCDCDecisionSpan {
}
}
}

/// Identify subcandidates in a candidate.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SubcandidateId(usize);

impl SubcandidateId {
// Get `SubcandidateId` of the root candidate.
pub const fn root() -> Self {
Self(0)
}

pub fn is_root(&self) -> bool {
*self == Self::root()
}

pub fn next_subcandidate_id(&self) -> Self {
Self(self.0 + 1)
}
}

/// Identify MatchPair in a candidate.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct MatchPairId(pub usize);

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct CandidateCovId {
pub decision_id: DecisionId,
pub subcandidate_id: SubcandidateId,
}

impl Default for CandidateCovId {
fn default() -> Self {
Self { decision_id: DecisionId::MAX, subcandidate_id: SubcandidateId(usize::MAX) }
}
}

impl CandidateCovId {
pub fn is_valid(&self) -> bool {
*self != Self::default()
}

pub fn new_match_info(
&mut self,
match_id: MatchPairId,
span: Span,
fully_matched: bool,
) -> MatchCoverageInfo {
let key = MatchKey {
decision_id: self.decision_id,
match_id,
subcandidate_id: self.subcandidate_id,
};
MatchCoverageInfo { key, span, fully_matched }
}
}

/// Key for matched patterns.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct MatchKey {
pub decision_id: DecisionId,
pub match_id: MatchPairId,
pub subcandidate_id: SubcandidateId,
}

impl Default for MatchKey {
fn default() -> Self {
Self {
decision_id: DecisionId::MAX,
match_id: MatchPairId(0),
subcandidate_id: SubcandidateId(0),
}
}
}

/// Information about matched patterns.
#[derive(Clone, Copy, Debug)]
pub struct MatchCoverageInfo {
pub key: MatchKey,
pub span: Span,
pub fully_matched: bool,
}
30 changes: 30 additions & 0 deletions compiler/rustc_mir_build/src/build/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,9 @@ struct Candidate<'pat, 'tcx> {
false_edge_start_block: Option<BasicBlock>,
/// The `false_edge_start_block` of the next candidate.
next_candidate_start_block: Option<BasicBlock>,

/// The id to identify the candidate in coverage instrument.
coverage_id: coverage::CandidateCovId,
}

impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
Expand All @@ -1114,6 +1117,7 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
pre_binding_block: None,
false_edge_start_block: None,
next_candidate_start_block: None,
coverage_id: coverage::CandidateCovId::default(),
}
}

Expand All @@ -1128,6 +1132,29 @@ impl<'tcx, 'pat> Candidate<'pat, 'tcx> {
|_| {},
);
}

pub(crate) fn set_coverage_id(&mut self, coverage_id: coverage::CandidateCovId) {
self.coverage_id = coverage_id;
// Assign id for match pairs only if this candidate is the root.
if !coverage_id.subcandidate_id.is_root() {
return;
};
let mut match_pairs_count = 0;
let mut next_match_id = || {
match_pairs_count += 1;
coverage::MatchPairId(match_pairs_count)
};
let mut match_pairs = self.match_pairs.iter_mut().collect::<Vec<_>>();
while let Some(match_pair) = match_pairs.pop() {
match_pair.coverage_id = next_match_id();
match_pairs.extend(match_pair.subpairs.iter_mut());
if let TestCase::Or { ref mut pats } = match_pair.test_case {
match_pairs.extend(
pats.iter_mut().map(|flat_pat| flat_pat.match_pairs.iter_mut()).flatten(),
);
}
}
}
}

/// A depth-first traversal of the `Candidate` and all of its recursive
Expand Down Expand Up @@ -1206,6 +1233,9 @@ pub(crate) struct MatchPair<'pat, 'tcx> {

/// The pattern this was created from.
pattern: &'pat Pat<'tcx>,

/// Key to identify the match pair in coverage.
coverage_id: coverage::MatchPairId,
}

/// See [`Test`] for more.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/build/matches/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<'pat, 'tcx> MatchPair<'pat, 'tcx> {
PatKind::Never => TestCase::Never,
};

MatchPair { place, test_case, subpairs, pattern }
MatchPair { place, test_case, subpairs, pattern, coverage_id: Default::default() }
}
}

Expand Down

0 comments on commit b363e65

Please sign in to comment.