@@ -4,6 +4,7 @@ use rustc_data_structures::captures::Captures;
4
4
use rustc_data_structures:: fx:: FxHashMap ;
5
5
use rustc_data_structures:: graph:: DirectedGraph ;
6
6
use rustc_index:: IndexVec ;
7
+ use rustc_index:: bit_set:: BitSet ;
7
8
use rustc_middle:: bug;
8
9
use rustc_middle:: mir:: coverage:: { CounterId , CovTerm , Expression , ExpressionId , Op } ;
9
10
use tracing:: { debug, debug_span, instrument} ;
@@ -13,13 +14,13 @@ use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph, TraverseCoverage
13
14
/// The coverage counter or counter expression associated with a particular
14
15
/// BCB node or BCB edge.
15
16
#[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
16
- pub ( super ) enum BcbCounter {
17
+ enum BcbCounter {
17
18
Counter { id : CounterId } ,
18
19
Expression { id : ExpressionId } ,
19
20
}
20
21
21
22
impl BcbCounter {
22
- pub ( super ) fn as_term ( & self ) -> CovTerm {
23
+ fn as_term ( & self ) -> CovTerm {
23
24
match * self {
24
25
BcbCounter :: Counter { id, .. } => CovTerm :: Counter ( id) ,
25
26
BcbCounter :: Expression { id, .. } => CovTerm :: Expression ( id) ,
@@ -78,21 +79,22 @@ impl CoverageCounters {
78
79
/// counters or counter expressions for nodes and edges as required.
79
80
pub ( super ) fn make_bcb_counters (
80
81
basic_coverage_blocks : & CoverageGraph ,
81
- bcb_needs_counter : impl Fn ( BasicCoverageBlock ) -> bool ,
82
+ bcb_needs_counter : & BitSet < BasicCoverageBlock > ,
82
83
) -> Self {
83
- let num_bcbs = basic_coverage_blocks. num_nodes ( ) ;
84
+ let mut counters = MakeBcbCounters :: new ( basic_coverage_blocks, bcb_needs_counter) ;
85
+ counters. make_bcb_counters ( ) ;
84
86
85
- let mut this = Self {
87
+ counters. coverage_counters
88
+ }
89
+
90
+ fn with_num_bcbs ( num_bcbs : usize ) -> Self {
91
+ Self {
86
92
counter_increment_sites : IndexVec :: new ( ) ,
87
93
bcb_counters : IndexVec :: from_elem_n ( None , num_bcbs) ,
88
94
bcb_edge_counters : FxHashMap :: default ( ) ,
89
95
expressions : IndexVec :: new ( ) ,
90
96
expressions_memo : FxHashMap :: default ( ) ,
91
- } ;
92
-
93
- MakeBcbCounters :: new ( & mut this, basic_coverage_blocks) . make_bcb_counters ( bcb_needs_counter) ;
94
-
95
- this
97
+ }
96
98
}
97
99
98
100
/// Shared helper used by [`Self::make_phys_node_counter`] and
@@ -218,8 +220,8 @@ impl CoverageCounters {
218
220
}
219
221
}
220
222
221
- pub ( super ) fn bcb_counter ( & self , bcb : BasicCoverageBlock ) -> Option < BcbCounter > {
222
- self . bcb_counters [ bcb]
223
+ pub ( super ) fn term_for_bcb ( & self , bcb : BasicCoverageBlock ) -> Option < CovTerm > {
224
+ self . bcb_counters [ bcb] . map ( |counter| counter . as_term ( ) )
223
225
}
224
226
225
227
/// Returns an iterator over all the nodes/edges in the coverage graph that
@@ -265,19 +267,25 @@ impl CoverageCounters {
265
267
266
268
/// Helper struct that allows counter creation to inspect the BCB graph.
267
269
struct MakeBcbCounters < ' a > {
268
- coverage_counters : & ' a mut CoverageCounters ,
270
+ coverage_counters : CoverageCounters ,
269
271
basic_coverage_blocks : & ' a CoverageGraph ,
272
+ bcb_needs_counter : & ' a BitSet < BasicCoverageBlock > ,
270
273
}
271
274
272
275
impl < ' a > MakeBcbCounters < ' a > {
273
276
fn new (
274
- coverage_counters : & ' a mut CoverageCounters ,
275
277
basic_coverage_blocks : & ' a CoverageGraph ,
278
+ bcb_needs_counter : & ' a BitSet < BasicCoverageBlock > ,
276
279
) -> Self {
277
- Self { coverage_counters, basic_coverage_blocks }
280
+ assert_eq ! ( basic_coverage_blocks. num_nodes( ) , bcb_needs_counter. domain_size( ) ) ;
281
+ Self {
282
+ coverage_counters : CoverageCounters :: with_num_bcbs ( basic_coverage_blocks. num_nodes ( ) ) ,
283
+ basic_coverage_blocks,
284
+ bcb_needs_counter,
285
+ }
278
286
}
279
287
280
- fn make_bcb_counters ( & mut self , bcb_needs_counter : impl Fn ( BasicCoverageBlock ) -> bool ) {
288
+ fn make_bcb_counters ( & mut self ) {
281
289
debug ! ( "make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock" ) ;
282
290
283
291
// Traverse the coverage graph, ensuring that every node that needs a
@@ -290,7 +298,7 @@ impl<'a> MakeBcbCounters<'a> {
290
298
let mut traversal = TraverseCoverageGraphWithLoops :: new ( self . basic_coverage_blocks ) ;
291
299
while let Some ( bcb) = traversal. next ( ) {
292
300
let _span = debug_span ! ( "traversal" , ?bcb) . entered ( ) ;
293
- if bcb_needs_counter ( bcb) {
301
+ if self . bcb_needs_counter . contains ( bcb) {
294
302
self . make_node_counter_and_out_edge_counters ( & traversal, bcb) ;
295
303
}
296
304
}
0 commit comments