Skip to content

Commit 80df5e0

Browse files
author
zhuyunxing
committed
coverage. Refactor MCDCInfoBuilder for pattern matching implementation and change the way to calculate decision depth
1 parent 628c3d0 commit 80df5e0

File tree

12 files changed

+896
-247
lines changed

12 files changed

+896
-247
lines changed

compiler/rustc_middle/src/mir/coverage.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ rustc_index::newtype_index! {
5151
pub struct ExpressionId {}
5252
}
5353

54+
rustc_index::newtype_index! {
55+
/// ID of a mcdc decision. Used to identify decision in a function.
56+
#[derive(HashStable)]
57+
#[encodable]
58+
#[orderable]
59+
#[debug_format = "DecisionId({})"]
60+
pub struct DecisionId {}
61+
}
62+
5463
rustc_index::newtype_index! {
5564
/// ID of a mcdc condition. Used by llvm to check mcdc coverage.
5665
///
@@ -271,7 +280,7 @@ pub struct CoverageInfoHi {
271280
pub branch_spans: Vec<BranchSpan>,
272281
/// Branch spans generated by mcdc. Because of some limits mcdc builder give up generating
273282
/// decisions including them so that they are handled as normal branch spans.
274-
pub mcdc_degraded_branch_spans: Vec<MCDCBranchSpan>,
283+
pub mcdc_degraded_spans: Vec<MCDCBranchSpan>,
275284
pub mcdc_spans: Vec<(MCDCDecisionSpan, Vec<MCDCBranchSpan>)>,
276285
}
277286

@@ -296,8 +305,11 @@ pub struct ConditionInfo {
296305
pub struct MCDCBranchSpan {
297306
pub span: Span,
298307
pub condition_info: ConditionInfo,
299-
pub true_marker: BlockMarkerId,
300-
pub false_marker: BlockMarkerId,
308+
// For boolean decisions and most pattern matching decisions there is only
309+
// one true marker and one false marker in each branch. But for matching decisions
310+
// with `|` there can be several.
311+
pub true_markers: Vec<BlockMarkerId>,
312+
pub false_markers: Vec<BlockMarkerId>,
301313
}
302314

303315
#[derive(Copy, Clone, Debug)]
@@ -313,5 +325,10 @@ pub struct MCDCDecisionSpan {
313325
pub span: Span,
314326
pub end_markers: Vec<BlockMarkerId>,
315327
pub decision_depth: u16,
316-
pub num_conditions: usize,
328+
}
329+
330+
impl MCDCDecisionSpan {
331+
pub fn new(span: Span) -> Self {
332+
Self { span, end_markers: Vec::new(), decision_depth: 0 }
333+
}
317334
}

compiler/rustc_middle/src/mir/pretty.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ fn write_coverage_info_hi(
490490
let coverage::CoverageInfoHi {
491491
num_block_markers: _,
492492
branch_spans,
493-
mcdc_degraded_branch_spans,
493+
mcdc_degraded_spans,
494494
mcdc_spans,
495495
} = coverage_info_hi;
496496

@@ -505,32 +505,27 @@ fn write_coverage_info_hi(
505505
did_print = true;
506506
}
507507

508-
for coverage::MCDCBranchSpan { span, true_marker, false_marker, .. } in
509-
mcdc_degraded_branch_spans
510-
{
508+
for coverage::MCDCBranchSpan { span, true_markers, false_markers, .. } in mcdc_degraded_spans {
511509
writeln!(
512510
w,
513-
"{INDENT}coverage branch {{ true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
511+
"{INDENT}coverage branch {{ true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
514512
)?;
515513
did_print = true;
516514
}
517515

518-
for (
519-
coverage::MCDCDecisionSpan { span, end_markers, decision_depth, num_conditions: _ },
520-
conditions,
521-
) in mcdc_spans
516+
for (coverage::MCDCDecisionSpan { span, end_markers, decision_depth }, conditions) in mcdc_spans
522517
{
523518
let num_conditions = conditions.len();
524519
writeln!(
525520
w,
526521
"{INDENT}coverage mcdc decision {{ num_conditions: {num_conditions:?}, end: {end_markers:?}, depth: {decision_depth:?} }} => {span:?}"
527522
)?;
528-
for coverage::MCDCBranchSpan { span, condition_info, true_marker, false_marker } in
523+
for coverage::MCDCBranchSpan { span, condition_info, true_markers, false_markers } in
529524
conditions
530525
{
531526
writeln!(
532527
w,
533-
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_marker:?}, false: {false_marker:?} }} => {span:?}",
528+
"{INDENT}coverage mcdc branch {{ condition_id: {:?}, true: {true_markers:?}, false: {false_markers:?} }} => {span:?}",
534529
condition_info.condition_id
535530
)?;
536531
}

compiler/rustc_mir_build/src/build/coverageinfo.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ impl CoverageInfoBuilder {
144144
// Separate path for handling branches when MC/DC is enabled.
145145
if let Some(mcdc_info) = self.mcdc_info.as_mut() {
146146
let inject_block_marker =
147-
|source_info, block| self.markers.inject_block_marker(cfg, source_info, block);
147+
|block| self.markers.inject_block_marker(cfg, source_info, block);
148148
mcdc_info.visit_evaluated_condition(
149149
tcx,
150-
source_info,
150+
source_info.span,
151151
true_block,
152152
false_block,
153153
inject_block_marker,
@@ -175,18 +175,18 @@ impl CoverageInfoBuilder {
175175
let branch_spans =
176176
branch_info.map(|branch_info| branch_info.branch_spans).unwrap_or_default();
177177

178-
let (mcdc_spans, mcdc_degraded_branch_spans) =
178+
let (mcdc_degraded_spans, mcdc_spans) =
179179
mcdc_info.map(MCDCInfoBuilder::into_done).unwrap_or_default();
180180

181181
// For simplicity, always return an info struct (without Option), even
182182
// if there's nothing interesting in it.
183183
Box::new(CoverageInfoHi {
184184
num_block_markers,
185185
branch_spans,
186-
mcdc_degraded_branch_spans,
187-
mcdc_spans,
188-
})
189-
}
186+
mcdc_degraded_spans,
187+
mcdc_spans
188+
})
189+
}
190190
}
191191

192192
impl<'tcx> Builder<'_, 'tcx> {

0 commit comments

Comments
 (0)