@@ -20,7 +20,7 @@ pub(crate) struct BranchInfoBuilder {
20
20
/// Maps condition expressions to their enclosing `!`, for better instrumentation.
21
21
nots : FxHashMap < ExprId , NotInfo > ,
22
22
23
- num_block_markers : usize ,
23
+ block_marker_gen : BlockMarkerGen ,
24
24
branch_spans : Vec < BranchSpan > ,
25
25
26
26
mcdc_branch_spans : Vec < MCDCBranchSpan > ,
@@ -38,14 +38,43 @@ struct NotInfo {
38
38
is_flipped : bool ,
39
39
}
40
40
41
+ #[ derive( Default ) ]
42
+ struct BlockMarkerGen {
43
+ num_block_markers : usize ,
44
+ }
45
+
46
+ impl BlockMarkerGen {
47
+ fn next_block_marker_id ( & mut self ) -> BlockMarkerId {
48
+ let id = BlockMarkerId :: from_usize ( self . num_block_markers ) ;
49
+ self . num_block_markers += 1 ;
50
+ id
51
+ }
52
+
53
+ fn inject_block_marker (
54
+ & mut self ,
55
+ cfg : & mut CFG < ' _ > ,
56
+ source_info : SourceInfo ,
57
+ block : BasicBlock ,
58
+ ) -> BlockMarkerId {
59
+ let id = self . next_block_marker_id ( ) ;
60
+ let marker_statement = mir:: Statement {
61
+ source_info,
62
+ kind : mir:: StatementKind :: Coverage ( CoverageKind :: BlockMarker { id } ) ,
63
+ } ;
64
+ cfg. push ( block, marker_statement) ;
65
+
66
+ id
67
+ }
68
+ }
69
+
41
70
impl BranchInfoBuilder {
42
71
/// Creates a new branch info builder, but only if branch coverage instrumentation
43
72
/// is enabled and `def_id` represents a function that is eligible for coverage.
44
73
pub ( crate ) fn new_if_enabled ( tcx : TyCtxt < ' _ > , def_id : LocalDefId ) -> Option < Self > {
45
74
if tcx. sess . instrument_coverage_branch ( ) && tcx. is_eligible_for_coverage ( def_id) {
46
75
Some ( Self {
47
76
nots : FxHashMap :: default ( ) ,
48
- num_block_markers : 0 ,
77
+ block_marker_gen : BlockMarkerGen :: default ( ) ,
49
78
branch_spans : vec ! [ ] ,
50
79
mcdc_branch_spans : vec ! [ ] ,
51
80
mcdc_decision_spans : vec ! [ ] ,
@@ -145,39 +174,16 @@ impl BranchInfoBuilder {
145
174
true_block : BasicBlock ,
146
175
false_block : BasicBlock ,
147
176
) {
148
- let true_marker = self . inject_block_marker ( cfg, source_info, true_block) ;
149
- let false_marker = self . inject_block_marker ( cfg, source_info, false_block) ;
177
+ let true_marker = self . block_marker_gen . inject_block_marker ( cfg, source_info, true_block) ;
178
+ let false_marker = self . block_marker_gen . inject_block_marker ( cfg, source_info, false_block) ;
150
179
151
180
self . branch_spans . push ( BranchSpan { span : source_info. span , true_marker, false_marker } ) ;
152
181
}
153
182
154
- fn next_block_marker_id ( & mut self ) -> BlockMarkerId {
155
- let id = BlockMarkerId :: from_usize ( self . num_block_markers ) ;
156
- self . num_block_markers += 1 ;
157
- id
158
- }
159
-
160
- fn inject_block_marker (
161
- & mut self ,
162
- cfg : & mut CFG < ' _ > ,
163
- source_info : SourceInfo ,
164
- block : BasicBlock ,
165
- ) -> BlockMarkerId {
166
- let id = self . next_block_marker_id ( ) ;
167
-
168
- let marker_statement = mir:: Statement {
169
- source_info,
170
- kind : mir:: StatementKind :: Coverage ( CoverageKind :: BlockMarker { id } ) ,
171
- } ;
172
- cfg. push ( block, marker_statement) ;
173
-
174
- id
175
- }
176
-
177
183
pub ( crate ) fn into_done ( self ) -> Option < Box < mir:: coverage:: BranchInfo > > {
178
184
let Self {
179
185
nots : _,
180
- num_block_markers,
186
+ block_marker_gen : BlockMarkerGen { num_block_markers } ,
181
187
branch_spans,
182
188
mcdc_branch_spans,
183
189
mcdc_decision_spans,
@@ -385,8 +391,9 @@ impl Builder<'_, '_> {
385
391
386
392
// Separate path for handling branches when MC/DC is enabled.
387
393
if branch_info. mcdc_state . is_some ( ) {
388
- let mut inject_block_marker =
389
- |block| branch_info. inject_block_marker ( & mut self . cfg , source_info, block) ;
394
+ let mut inject_block_marker = |block| {
395
+ branch_info. block_marker_gen . inject_block_marker ( & mut self . cfg , source_info, block)
396
+ } ;
390
397
let true_marker = inject_block_marker ( then_block) ;
391
398
let false_marker = inject_block_marker ( else_block) ;
392
399
let ( decision_depth, condition_info) = branch_info
0 commit comments