Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61b608c

Browse files
author
zhuyunxing
committedJul 3, 2024
coverage. Trace multiple branch blocks of mcdc conditions
1 parent 105addc commit 61b608c

File tree

7 files changed

+237
-183
lines changed

7 files changed

+237
-183
lines changed
 

‎compiler/rustc_middle/src/mir/coverage.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ pub struct BranchInfo {
291291
/// data structures without having to scan the entire body first.
292292
pub num_block_markers: usize,
293293
pub branch_spans: Vec<BranchSpan>,
294-
pub mcdc_branch_spans: Vec<MCDCBranchSpan>,
295-
pub mcdc_decision_spans: Vec<MCDCDecisionSpan>,
294+
pub mcdc_degraded_spans: Vec<MCDCBranchSpan>,
295+
pub mcdc_spans: Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>,
296296
}
297297

298298
#[derive(Clone, Debug)]
@@ -325,12 +325,9 @@ impl Default for ConditionInfo {
325325
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
326326
pub struct MCDCBranchSpan {
327327
pub span: Span,
328-
/// If `None`, this actually represents a normal branch span inserted for
329-
/// code that was too complex for MC/DC.
330-
pub condition_info: Option<ConditionInfo>,
331-
pub true_marker: BlockMarkerId,
332-
pub false_marker: BlockMarkerId,
333-
pub decision_depth: u16,
328+
pub condition_info: ConditionInfo,
329+
pub true_markers: Vec<BlockMarkerId>,
330+
pub false_markers: Vec<BlockMarkerId>,
334331
}
335332

336333
#[derive(Copy, Clone, Debug)]
@@ -344,7 +341,6 @@ pub struct DecisionInfo {
344341
#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)]
345342
pub struct MCDCDecisionSpan {
346343
pub span: Span,
347-
pub num_conditions: usize,
348344
pub end_markers: Vec<BlockMarkerId>,
349345
pub decision_depth: u16,
350346
}

‎compiler/rustc_middle/src/mir/pretty.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,7 @@ fn write_coverage_branch_info(
487487
branch_info: &coverage::BranchInfo,
488488
w: &mut dyn io::Write,
489489
) -> io::Result<()> {
490-
let coverage::BranchInfo { branch_spans, mcdc_branch_spans, mcdc_decision_spans, .. } =
491-
branch_info;
490+
let coverage::BranchInfo { branch_spans, mcdc_degraded_spans, mcdc_spans, .. } = branch_info;
492491

493492
for coverage::BranchSpan { span, true_marker, false_marker } in branch_spans {
494493
writeln!(
@@ -497,32 +496,32 @@ fn write_coverage_branch_info(
497496
)?;
498497
}
499498

500-
for coverage::MCDCBranchSpan {
501-
span,
502-
condition_info,
503-
true_marker,
504-
false_marker,
505-
decision_depth,
506-
} in mcdc_branch_spans
507-
{
499+
for coverage::MCDCBranchSpan { span, true_markers, false_markers, .. } in mcdc_degraded_spans {
508500
writeln!(
509501
w,
510-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?}, depth: {decision_depth:?} }} => {span:?}",
511-
condition_info.map(|info| info.condition_id)
502+
"{INDENT}coverage branch {{ true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
512503
)?;
513504
}
514505

515-
for coverage::MCDCDecisionSpan { span, num_conditions, end_markers, decision_depth } in
516-
mcdc_decision_spans
506+
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
517507
{
508+
let num_conditions = conditions.len();
518509
writeln!(
519510
w,
520511
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
521512
)?;
513+
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
514+
conditions
515+
{
516+
writeln!(
517+
w,
518+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
519+
condition_info.condition_id
520+
)?;
521+
}
522522
}
523523

524-
if !branch_spans.is_empty() || !mcdc_branch_spans.is_empty() || !mcdc_decision_spans.is_empty()
525-
{
524+
if !branch_spans.is_empty() || !mcdc_degraded_spans.is_empty() || !mcdc_spans.is_empty() {
526525
writeln!(w)?;
527526
}
528527

‎compiler/rustc_mir_build/src/build/coverageinfo.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,13 @@ impl BranchInfoBuilder {
162162
return None;
163163
}
164164

165-
let (mut mcdc_branch_spans, mcdc_spans) =
165+
let (mcdc_degraded_spans, mcdc_spans) =
166166
mcdc_info.map(MCDCInfoBuilder::into_done).unwrap_or_default();
167-
let mut mcdc_decision_spans = Vec::with_capacity(mcdc_spans.len());
168-
for (decision, conditions) in mcdc_spans {
169-
mcdc_branch_spans.extend(conditions);
170-
mcdc_decision_spans.push(decision);
171-
}
172167
Some(Box::new(mir::coverage::BranchInfo {
173168
num_block_markers,
174169
branch_spans,
175-
mcdc_branch_spans,
176-
mcdc_decision_spans,
170+
mcdc_degraded_spans,
171+
mcdc_spans,
177172
}))
178173
}
179174
}

‎compiler/rustc_mir_build/src/build/coverageinfo/mcdc.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ struct BooleanDecisionCtx {
3131
/// To construct condition evaluation tree.
3232
decision_stack: VecDeque<ConditionInfo>,
3333
conditions: Vec<MCDCBranchSpan>,
34+
condition_id_counter: usize,
3435
}
3536

3637
impl BooleanDecisionCtx {
@@ -39,15 +40,20 @@ impl BooleanDecisionCtx {
3940
id,
4041
decision_info: MCDCDecisionSpan {
4142
span: Span::default(),
42-
num_conditions: 0,
4343
end_markers: vec![],
4444
decision_depth: 0,
4545
},
4646
decision_stack: VecDeque::new(),
4747
conditions: vec![],
48+
condition_id_counter: 0,
4849
}
4950
}
5051

52+
fn next_condition_id(&mut self) -> ConditionId {
53+
self.condition_id_counter += 1;
54+
ConditionId::from_usize(self.condition_id_counter)
55+
}
56+
5157
// At first we assign ConditionIds for each sub expression.
5258
// If the sub expression is composite, re-assign its ConditionId to its LHS and generate a new ConditionId for its RHS.
5359
//
@@ -91,14 +97,12 @@ impl BooleanDecisionCtx {
9197
fn record_conditions(&mut self, op: LogicalOp) {
9298
let parent_condition = self.decision_stack.pop_back().unwrap_or_default();
9399
let lhs_id = if parent_condition.condition_id == ConditionId::NONE {
94-
self.decision_info.num_conditions += 1;
95-
ConditionId::from(self.decision_info.num_conditions)
100+
ConditionId::from(self.next_condition_id())
96101
} else {
97102
parent_condition.condition_id
98103
};
99104

100-
self.decision_info.num_conditions += 1;
101-
let rhs_condition_id = ConditionId::from(self.decision_info.num_conditions);
105+
let rhs_condition_id = self.next_condition_id();
102106

103107
let (lhs, rhs) = match op {
104108
LogicalOp::And => {
@@ -149,13 +153,10 @@ impl BooleanDecisionCtx {
149153

150154
self.conditions.push(MCDCBranchSpan {
151155
span,
152-
condition_info: Some(condition_info),
153-
true_marker,
154-
false_marker,
155-
decision_depth: 0,
156+
condition_info,
157+
true_markers: vec![true_marker],
158+
false_markers: vec![false_marker],
156159
});
157-
// In case this decision had only one condition
158-
self.decision_info.num_conditions = self.decision_info.num_conditions.max(1);
159160
}
160161

161162
fn is_finished(&self) -> bool {
@@ -247,7 +248,6 @@ struct MCDCTargetInfo {
247248
impl MCDCTargetInfo {
248249
fn set_depth(&mut self, depth: u16) {
249250
self.decision.decision_depth = depth;
250-
self.conditions.iter_mut().for_each(|branch| branch.decision_depth = depth);
251251
}
252252
}
253253

@@ -340,7 +340,9 @@ impl MCDCInfoBuilder {
340340
}
341341

342342
fn append_normal_branches(&mut self, mut branches: Vec<MCDCBranchSpan>) {
343-
branches.iter_mut().for_each(|branch| branch.condition_info = None);
343+
branches
344+
.iter_mut()
345+
.for_each(|branch| branch.condition_info.condition_id = ConditionId::NONE);
344346
self.normal_branch_spans.extend(branches);
345347
}
346348

‎compiler/rustc_mir_transform/src/coverage/counters.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,12 @@ impl CoverageCounters {
101101
BcbCounter::Counter { id }
102102
}
103103

104-
fn make_expression(&mut self, lhs: BcbCounter, op: Op, rhs: BcbCounter) -> BcbCounter {
104+
pub(super) fn make_expression(
105+
&mut self,
106+
lhs: BcbCounter,
107+
op: Op,
108+
rhs: BcbCounter,
109+
) -> BcbCounter {
105110
let new_expr = BcbExpression { lhs, op, rhs };
106111
*self
107112
.expressions_memo
@@ -159,7 +164,11 @@ impl CoverageCounters {
159164
/// Variant of `make_expression` that makes `lhs` optional and assumes [`Op::Add`].
160165
///
161166
/// This is useful when using [`Iterator::fold`] to build an arbitrary-length sum.
162-
fn make_sum_expression(&mut self, lhs: Option<BcbCounter>, rhs: BcbCounter) -> BcbCounter {
167+
pub(super) fn make_sum_expression(
168+
&mut self,
169+
lhs: Option<BcbCounter>,
170+
rhs: BcbCounter,
171+
) -> BcbCounter {
163172
let Some(lhs) = lhs else { return rhs };
164173
self.make_expression(lhs, Op::Add, rhs)
165174
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.