Skip to content

Commit d8f3dde

Browse files
author
zhuyunxing
committed
coverage. Do not check raw logical expressions temporarily
1 parent 75608af commit d8f3dde

File tree

3 files changed

+27
-43
lines changed

3 files changed

+27
-43
lines changed

compiler/rustc_mir_build/src/build/coverageinfo.rs

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) struct BranchInfoBuilder {
2020

2121
num_block_markers: usize,
2222
branch_spans: Vec<BranchSpan>,
23-
23+
decision_spans: Vec<DecisionSpan>,
2424
mcdc_state: Option<MCDCState>,
2525
}
2626

@@ -43,6 +43,7 @@ impl BranchInfoBuilder {
4343
nots: FxHashMap::default(),
4444
num_block_markers: 0,
4545
branch_spans: vec![],
46+
decision_spans: vec![],
4647
mcdc_state: MCDCState::new_if_enabled(tcx),
4748
})
4849
} else {
@@ -101,13 +102,12 @@ impl BranchInfoBuilder {
101102
}
102103

103104
pub(crate) fn into_done(self) -> Option<Box<mir::coverage::BranchInfo>> {
104-
let Self { nots: _, num_block_markers, branch_spans, mcdc_state } = self;
105+
let Self { nots: _, num_block_markers, branch_spans, decision_spans, .. } = self;
105106

106107
if num_block_markers == 0 {
107108
assert!(branch_spans.is_empty());
108109
return None;
109110
}
110-
let decision_spans = mcdc_state.map(|state| state.decisions).unwrap_or_default();
111111

112112
Some(Box::new(mir::coverage::BranchInfo {
113113
num_block_markers,
@@ -120,22 +120,19 @@ impl BranchInfoBuilder {
120120
/// The MCDC bitmap scales exponentially (2^n) based on the number of conditions seen,
121121
/// So llvm sets a maximum value prevents the bitmap footprint from growing too large without the user's knowledge.
122122
/// This limit may be relaxed if the [upstream change](https://github.com/llvm/llvm-project/pull/82448) is merged.
123-
const MAX_CONDITIONS_NUM_IN_DECISION: u16 = 6;
123+
const MAX_CONDITIONS_NUM_IN_DECISION: usize = 6;
124124

125125
struct MCDCState {
126126
/// To construct condition evaluation tree.
127127
decision_stack: VecDeque<ConditionInfo>,
128128
next_condition_id: usize,
129-
decisions: Vec<DecisionSpan>,
130129
}
131130

132131
impl MCDCState {
133132
fn new_if_enabled(tcx: TyCtxt<'_>) -> Option<Self> {
134-
tcx.sess.instrument_coverage_mcdc().then(|| Self {
135-
decision_stack: VecDeque::new(),
136-
next_condition_id: 0,
137-
decisions: vec![],
138-
})
133+
tcx.sess
134+
.instrument_coverage_mcdc()
135+
.then(|| Self { decision_stack: VecDeque::new(), next_condition_id: 0 })
139136
}
140137

141138
/// At first we assign ConditionIds for each sub expression.
@@ -278,24 +275,7 @@ impl Builder<'_, '_> {
278275
});
279276
}
280277

281-
pub(crate) fn visit_coverage_decision(&mut self, expr_id: ExprId) {
282-
let Some(branch_info) = self.coverage_branch_info.as_mut() else { return };
283-
if branch_info.mcdc_state.is_some() {
284-
let join_marker = branch_info.next_block_marker_id();
285-
let mcdc_state = branch_info.mcdc_state.as_mut().unwrap();
286-
assert!(
287-
mcdc_state.decision_stack.is_empty() && mcdc_state.next_condition_id == 0,
288-
"There is a unfinished decision"
289-
);
290-
mcdc_state.decisions.push(DecisionSpan {
291-
span: self.thir[expr_id].span,
292-
conditions_num: 0,
293-
join_marker,
294-
});
295-
}
296-
}
297-
298-
pub(crate) fn visit_coverage_decision_end(&mut self, join_block: BasicBlock) {
278+
pub(crate) fn visit_coverage_decision(&mut self, expr_id: ExprId, join_block: BasicBlock) {
299279
if let Some((mcdc_state, branches)) = self
300280
.coverage_branch_info
301281
.as_mut()
@@ -305,18 +285,26 @@ impl Builder<'_, '_> {
305285
mcdc_state.decision_stack.is_empty(),
306286
"All condition should have been checked before the decision ends"
307287
);
308-
let Some(decision) = mcdc_state.decisions.last_mut() else { return };
309288

310-
decision.conditions_num = mcdc_state.next_condition_id as u16;
289+
let conditions_num = mcdc_state.next_condition_id;
290+
311291
mcdc_state.next_condition_id = 0;
312292

313-
match decision.conditions_num {
293+
match conditions_num {
314294
0 => {
315295
unreachable!("Decision with no conditions is not allowed");
316296
}
317297
1..=MAX_CONDITIONS_NUM_IN_DECISION => {
318-
let span = decision.span;
319-
let id = decision.join_marker;
298+
let span = self.thir[expr_id].span;
299+
let branch_info =
300+
self.coverage_branch_info.as_mut().expect("updating to existed");
301+
let id = branch_info.next_block_marker_id();
302+
303+
branch_info.decision_spans.push(DecisionSpan {
304+
span,
305+
conditions_num: conditions_num as u16,
306+
join_marker: id,
307+
});
320308

321309
let statement = mir::Statement {
322310
source_info: self.source_info(span),
@@ -326,16 +314,15 @@ impl Builder<'_, '_> {
326314
}
327315
_ => {
328316
// Do not generate mcdc mappings and statements for decisions with too many conditions.
329-
for branch in branches.iter_mut().rev().take(decision.conditions_num as usize) {
317+
for branch in branches.iter_mut().rev().take(conditions_num) {
330318
branch.condition_info = Default::default();
331319
}
332320

333321
self.tcx.dcx().emit_warn(MCDCExceedsConditionNumLimit {
334-
span: decision.span,
335-
conditions_num: decision.conditions_num,
322+
span: self.thir[expr_id].span,
323+
conditions_num,
336324
max_conditions_num: MAX_CONDITIONS_NUM_IN_DECISION,
337325
});
338-
mcdc_state.decisions.pop();
339326
}
340327
}
341328
}

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6161
let then_span = this.thir[then].span;
6262
let then_source_info = this.source_info(then_span);
6363
let condition_scope = this.local_scope();
64-
this.visit_coverage_decision(cond);
6564

6665
let then_and_else_blocks = this.in_scope(
6766
(if_then_scope, then_source_info),
@@ -114,7 +113,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
114113
// The `then` and `else` arms have been lowered into their respective
115114
// blocks, so make both of them meet up in a new block.
116115
let join_block = this.cfg.start_new_block();
117-
this.visit_coverage_decision_end(join_block);
116+
this.visit_coverage_decision(cond, join_block);
118117
this.cfg.goto(then_blk, source_info, join_block);
119118
this.cfg.goto(else_blk, source_info, join_block);
120119
join_block.unit()
@@ -151,7 +150,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
151150
let condition_scope = this.local_scope();
152151
let source_info = this.source_info(expr.span);
153152

154-
this.visit_coverage_decision(expr_id);
155153
// We first evaluate the left-hand side of the predicate ...
156154
let (then_block, else_block) =
157155
this.in_if_then_scope(condition_scope, expr.span, |this| {
@@ -187,7 +185,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
187185
);
188186
let rhs = unpack!(this.expr_into_dest(destination, continuation, rhs));
189187
let target = this.cfg.start_new_block();
190-
this.visit_coverage_decision_end(target);
191188
this.cfg.goto(rhs, source_info, target);
192189
this.cfg.goto(short_circuit, source_info, target);
193190
target.unit()

compiler/rustc_mir_build/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ pub struct NontrivialStructuralMatch<'tcx> {
823823
pub(crate) struct MCDCExceedsConditionNumLimit {
824824
#[primary_span]
825825
pub span: Span,
826-
pub conditions_num: u16,
827-
pub max_conditions_num: u16,
826+
pub conditions_num: usize,
827+
pub max_conditions_num: usize,
828828
}
829829

830830
#[derive(Diagnostic)]

0 commit comments

Comments
 (0)