Skip to content

Commit 5bdabd0

Browse files
committed
mcdc-coverage: Add Data in BranchInfo for MCDC Tracability
1 parent 7d1dc67 commit 5bdabd0

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

compiler/rustc_middle/src/mir/coverage.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,28 @@ pub struct BranchInfo {
257257
/// data structures without having to scan the entire body first.
258258
pub num_block_markers: usize,
259259
pub branch_spans: Vec<BranchSpan>,
260-
pub decisions: IndexVec<DecisionMarkerId, Span>,
260+
261+
/// Associate a span for every decision in the function body.
262+
/// Empty if MCDC coverage is disabled.
263+
pub decision_spans: IndexVec<DecisionMarkerId, DecisionSpan>,
264+
}
265+
266+
#[derive(Clone, Debug)]
267+
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
268+
pub struct DecisionSpan {
269+
/// Source code region associated to the decision.
270+
pub span: Span,
271+
/// Number of conditions in the decision.
272+
pub num_conditions: u32,
261273
}
262274

263275
#[derive(Clone, Debug)]
264276
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
265277
pub struct BranchSpan {
266278
/// Source code region associated to the branch.
267279
pub span: Span,
268-
/// Decision structure the branch is part of. Only used in the MCDC coverage.
280+
/// ID of Decision structure the branch is part of. Only used in
281+
/// the MCDC coverage.
269282
pub decision_id: DecisionMarkerId,
270283
pub true_marker: BlockMarkerId,
271284
pub false_marker: BlockMarkerId,

compiler/rustc_mir_build/src/build/coverageinfo.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use std::collections::hash_map::Entry;
33

44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_index::IndexVec;
6-
use rustc_middle::mir::coverage::{BlockMarkerId, BranchSpan, CoverageKind, DecisionMarkerId};
6+
use rustc_middle::mir::coverage::{
7+
BlockMarkerId, BranchSpan, CoverageKind, DecisionMarkerId, DecisionSpan,
8+
};
79
use rustc_middle::mir::{self, BasicBlock, UnOp};
810
use rustc_middle::thir::{ExprId, ExprKind, Thir};
911
use rustc_middle::ty::TyCtxt;
@@ -103,7 +105,24 @@ impl BranchInfoBuilder {
103105
return None;
104106
}
105107

106-
Some(Box::new(mir::coverage::BranchInfo { num_block_markers, branch_spans, decisions }))
108+
let mut decision_spans = IndexVec::from_iter(
109+
decisions
110+
.into_iter()
111+
.map(|span| DecisionSpan { span, num_conditions: 0 }),
112+
);
113+
114+
// Count the number of conditions linked to each decision.
115+
if !decision_spans.is_empty() {
116+
for branch_span in branch_spans.iter() {
117+
decision_spans[branch_span.decision_id].num_conditions += 1;
118+
}
119+
}
120+
121+
Some(Box::new(mir::coverage::BranchInfo {
122+
num_block_markers,
123+
branch_spans,
124+
decision_spans,
125+
}))
107126
}
108127
}
109128

0 commit comments

Comments
 (0)