@@ -74,12 +74,11 @@ pub(super) struct CoverageCounters {
74
74
}
75
75
76
76
impl CoverageCounters {
77
- /// Makes [`BcbCounter`] `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
78
- /// indirectly associated with coverage spans, and accumulates additional `Expression`s
79
- /// representing intermediate values.
77
+ /// Ensures that each BCB node needing a counter has one, by creating physical
78
+ /// counters or counter expressions for nodes and edges as required.
80
79
pub ( super ) fn make_bcb_counters (
81
80
basic_coverage_blocks : & CoverageGraph ,
82
- bcb_has_coverage_spans : impl Fn ( BasicCoverageBlock ) -> bool ,
81
+ bcb_needs_counter : impl Fn ( BasicCoverageBlock ) -> bool ,
83
82
) -> Self {
84
83
let num_bcbs = basic_coverage_blocks. num_nodes ( ) ;
85
84
@@ -91,8 +90,7 @@ impl CoverageCounters {
91
90
expressions_memo : FxHashMap :: default ( ) ,
92
91
} ;
93
92
94
- MakeBcbCounters :: new ( & mut this, basic_coverage_blocks)
95
- . make_bcb_counters ( bcb_has_coverage_spans) ;
93
+ MakeBcbCounters :: new ( & mut this, basic_coverage_blocks) . make_bcb_counters ( bcb_needs_counter) ;
96
94
97
95
this
98
96
}
@@ -241,10 +239,7 @@ impl CoverageCounters {
241
239
}
242
240
}
243
241
244
- /// Traverse the `CoverageGraph` and add either a `Counter` or `Expression` to every BCB, to be
245
- /// injected with coverage spans. `Expressions` have no runtime overhead, so if a viable expression
246
- /// (adding or subtracting two other counters or expressions) can compute the same result as an
247
- /// embedded counter, an `Expression` should be used.
242
+ /// Helper struct that allows counter creation to inspect the BCB graph.
248
243
struct MakeBcbCounters < ' a > {
249
244
coverage_counters : & ' a mut CoverageCounters ,
250
245
basic_coverage_blocks : & ' a CoverageGraph ,
@@ -264,30 +259,21 @@ impl<'a> MakeBcbCounters<'a> {
264
259
/// One way to predict which branch executes the least is by considering loops. A loop is exited
265
260
/// at a branch, so the branch that jumps to a `BasicCoverageBlock` outside the loop is almost
266
261
/// always executed less than the branch that does not exit the loop.
267
- fn make_bcb_counters ( & mut self , bcb_has_coverage_spans : impl Fn ( BasicCoverageBlock ) -> bool ) {
262
+ fn make_bcb_counters ( & mut self , bcb_needs_counter : impl Fn ( BasicCoverageBlock ) -> bool ) {
268
263
debug ! ( "make_bcb_counters(): adding a counter or expression to each BasicCoverageBlock" ) ;
269
264
270
- // Walk the `CoverageGraph`. For each `BasicCoverageBlock` node with an associated
271
- // coverage span, add a counter. If the `BasicCoverageBlock` branches, add a counter or
272
- // expression to each branch `BasicCoverageBlock` (if the branch BCB has only one incoming
273
- // edge) or edge from the branching BCB to the branch BCB (if the branch BCB has multiple
274
- // incoming edges).
265
+ // Traverse the coverage graph, ensuring that every node that needs a
266
+ // coverage counter has one.
275
267
//
276
- // The `TraverseCoverageGraphWithLoops` traversal ensures that, when a loop is encountered,
277
- // all `BasicCoverageBlock` nodes in the loop are visited before visiting any node outside
278
- // the loop. The `traversal` state includes a `context_stack`, providing a way to know if
279
- // the current BCB is in one or more nested loops or not .
268
+ // The traversal tries to ensure that, when a loop is encountered, all
269
+ // nodes within the loop are visited before visiting any nodes outside
270
+ // the loop. It also keeps track of which loop(s) the traversal is
271
+ // currently inside .
280
272
let mut traversal = TraverseCoverageGraphWithLoops :: new ( self . basic_coverage_blocks ) ;
281
273
while let Some ( bcb) = traversal. next ( ) {
282
- if bcb_has_coverage_spans ( bcb) {
283
- debug ! ( "{:?} has at least one coverage span. Get or make its counter" , bcb) ;
274
+ let _span = debug_span ! ( "traversal" , ? bcb) . entered ( ) ;
275
+ if bcb_needs_counter ( bcb) {
284
276
self . make_node_and_branch_counters ( & traversal, bcb) ;
285
- } else {
286
- debug ! (
287
- "{:?} does not have any coverage spans. A counter will only be added if \
288
- and when a covered BCB has an expression dependency.",
289
- bcb,
290
- ) ;
291
277
}
292
278
}
293
279
@@ -298,6 +284,7 @@ impl<'a> MakeBcbCounters<'a> {
298
284
) ;
299
285
}
300
286
287
+ #[ instrument( level = "debug" , skip( self , traversal) ) ]
301
288
fn make_node_and_branch_counters (
302
289
& mut self ,
303
290
traversal : & TraverseCoverageGraphWithLoops < ' _ > ,
0 commit comments