@@ -14,36 +14,76 @@ use rustc_index::bit_set::BitSet;
14
14
use rustc_index:: IndexVec ;
15
15
use rustc_middle:: mir:: coverage:: * ;
16
16
17
+ use std:: fmt:: { self , Debug } ;
18
+
19
+ /// The coverage counter or counter expression associated with a particular
20
+ /// BCB node or BCB edge.
21
+ #[ derive( Clone ) ]
22
+ pub ( super ) enum BcbCounter {
23
+ Counter { id : CounterId } ,
24
+ Expression { id : ExpressionId , lhs : Operand , op : Op , rhs : Operand } ,
25
+ }
26
+
27
+ impl BcbCounter {
28
+ fn is_expression ( & self ) -> bool {
29
+ matches ! ( self , Self :: Expression { .. } )
30
+ }
31
+
32
+ pub ( super ) fn as_operand ( & self ) -> Operand {
33
+ match * self {
34
+ BcbCounter :: Counter { id, .. } => Operand :: Counter ( id) ,
35
+ BcbCounter :: Expression { id, .. } => Operand :: Expression ( id) ,
36
+ }
37
+ }
38
+ }
39
+
40
+ impl Debug for BcbCounter {
41
+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
42
+ match self {
43
+ Self :: Counter { id, .. } => write ! ( fmt, "Counter({:?})" , id. index( ) ) ,
44
+ Self :: Expression { id, lhs, op, rhs } => write ! (
45
+ fmt,
46
+ "Expression({:?}) = {:?} {} {:?}" ,
47
+ id. index( ) ,
48
+ lhs,
49
+ match op {
50
+ Op :: Add => "+" ,
51
+ Op :: Subtract => "-" ,
52
+ } ,
53
+ rhs,
54
+ ) ,
55
+ }
56
+ }
57
+ }
58
+
17
59
/// Generates and stores coverage counter and coverage expression information
18
60
/// associated with nodes/edges in the BCB graph.
19
61
pub ( super ) struct CoverageCounters {
20
- function_source_hash : u64 ,
21
62
next_counter_id : CounterId ,
22
63
next_expression_id : ExpressionId ,
23
64
24
65
/// Coverage counters/expressions that are associated with individual BCBs.
25
- bcb_counters : IndexVec < BasicCoverageBlock , Option < CoverageKind > > ,
66
+ bcb_counters : IndexVec < BasicCoverageBlock , Option < BcbCounter > > ,
26
67
/// Coverage counters/expressions that are associated with the control-flow
27
68
/// edge between two BCBs.
28
- bcb_edge_counters : FxHashMap < ( BasicCoverageBlock , BasicCoverageBlock ) , CoverageKind > ,
69
+ bcb_edge_counters : FxHashMap < ( BasicCoverageBlock , BasicCoverageBlock ) , BcbCounter > ,
29
70
/// Tracks which BCBs have a counter associated with some incoming edge.
30
71
/// Only used by debug assertions, to verify that BCBs with incoming edge
31
72
/// counters do not have their own physical counters (expressions are allowed).
32
73
bcb_has_incoming_edge_counters : BitSet < BasicCoverageBlock > ,
33
74
/// Expression nodes that are not directly associated with any particular
34
75
/// BCB/edge, but are needed as operands to more complex expressions.
35
- /// These are always `CoverageKind ::Expression`.
36
- pub ( super ) intermediate_expressions : Vec < CoverageKind > ,
76
+ /// These are always [`BcbCounter ::Expression`] .
77
+ pub ( super ) intermediate_expressions : Vec < BcbCounter > ,
37
78
38
79
pub debug_counters : DebugCounters ,
39
80
}
40
81
41
82
impl CoverageCounters {
42
- pub ( super ) fn new ( function_source_hash : u64 , basic_coverage_blocks : & CoverageGraph ) -> Self {
83
+ pub ( super ) fn new ( basic_coverage_blocks : & CoverageGraph ) -> Self {
43
84
let num_bcbs = basic_coverage_blocks. num_nodes ( ) ;
44
85
45
86
Self {
46
- function_source_hash,
47
87
next_counter_id : CounterId :: START ,
48
88
next_expression_id : ExpressionId :: START ,
49
89
@@ -57,12 +97,12 @@ impl CoverageCounters {
57
97
}
58
98
59
99
/// Activate the `DebugCounters` data structures, to provide additional debug formatting
60
- /// features when formatting `CoverageKind` (counter) values.
100
+ /// features when formatting [`BcbCounter`] (counter) values.
61
101
pub fn enable_debug ( & mut self ) {
62
102
self . debug_counters . enable ( ) ;
63
103
}
64
104
65
- /// Makes `CoverageKind` `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
105
+ /// Makes [`BcbCounter`] `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
66
106
/// indirectly associated with `CoverageSpans`, and accumulates additional `Expression`s
67
107
/// representing intermediate values.
68
108
pub fn make_bcb_counters (
@@ -73,14 +113,11 @@ impl CoverageCounters {
73
113
MakeBcbCounters :: new ( self , basic_coverage_blocks) . make_bcb_counters ( coverage_spans)
74
114
}
75
115
76
- fn make_counter < F > ( & mut self , debug_block_label_fn : F ) -> CoverageKind
116
+ fn make_counter < F > ( & mut self , debug_block_label_fn : F ) -> BcbCounter
77
117
where
78
118
F : Fn ( ) -> Option < String > ,
79
119
{
80
- let counter = CoverageKind :: Counter {
81
- function_source_hash : self . function_source_hash ,
82
- id : self . next_counter ( ) ,
83
- } ;
120
+ let counter = BcbCounter :: Counter { id : self . next_counter ( ) } ;
84
121
if self . debug_counters . is_enabled ( ) {
85
122
self . debug_counters . add_counter ( & counter, ( debug_block_label_fn) ( ) ) ;
86
123
}
@@ -93,19 +130,19 @@ impl CoverageCounters {
93
130
op : Op ,
94
131
rhs : Operand ,
95
132
debug_block_label_fn : F ,
96
- ) -> CoverageKind
133
+ ) -> BcbCounter
97
134
where
98
135
F : Fn ( ) -> Option < String > ,
99
136
{
100
137
let id = self . next_expression ( ) ;
101
- let expression = CoverageKind :: Expression { id, lhs, op, rhs } ;
138
+ let expression = BcbCounter :: Expression { id, lhs, op, rhs } ;
102
139
if self . debug_counters . is_enabled ( ) {
103
140
self . debug_counters . add_counter ( & expression, ( debug_block_label_fn) ( ) ) ;
104
141
}
105
142
expression
106
143
}
107
144
108
- pub fn make_identity_counter ( & mut self , counter_operand : Operand ) -> CoverageKind {
145
+ pub fn make_identity_counter ( & mut self , counter_operand : Operand ) -> BcbCounter {
109
146
let some_debug_block_label = if self . debug_counters . is_enabled ( ) {
110
147
self . debug_counters . some_block_label ( counter_operand) . cloned ( )
111
148
} else {
@@ -134,7 +171,7 @@ impl CoverageCounters {
134
171
fn set_bcb_counter (
135
172
& mut self ,
136
173
bcb : BasicCoverageBlock ,
137
- counter_kind : CoverageKind ,
174
+ counter_kind : BcbCounter ,
138
175
) -> Result < Operand , Error > {
139
176
debug_assert ! (
140
177
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
@@ -158,7 +195,7 @@ impl CoverageCounters {
158
195
& mut self ,
159
196
from_bcb : BasicCoverageBlock ,
160
197
to_bcb : BasicCoverageBlock ,
161
- counter_kind : CoverageKind ,
198
+ counter_kind : BcbCounter ,
162
199
) -> Result < Operand , Error > {
163
200
if level_enabled ! ( tracing:: Level :: DEBUG ) {
164
201
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
@@ -183,25 +220,25 @@ impl CoverageCounters {
183
220
}
184
221
}
185
222
186
- pub ( super ) fn bcb_counter ( & self , bcb : BasicCoverageBlock ) -> Option < & CoverageKind > {
223
+ pub ( super ) fn bcb_counter ( & self , bcb : BasicCoverageBlock ) -> Option < & BcbCounter > {
187
224
self . bcb_counters [ bcb] . as_ref ( )
188
225
}
189
226
190
- pub ( super ) fn take_bcb_counter ( & mut self , bcb : BasicCoverageBlock ) -> Option < CoverageKind > {
227
+ pub ( super ) fn take_bcb_counter ( & mut self , bcb : BasicCoverageBlock ) -> Option < BcbCounter > {
191
228
self . bcb_counters [ bcb] . take ( )
192
229
}
193
230
194
231
pub ( super ) fn drain_bcb_counters (
195
232
& mut self ,
196
- ) -> impl Iterator < Item = ( BasicCoverageBlock , CoverageKind ) > + ' _ {
233
+ ) -> impl Iterator < Item = ( BasicCoverageBlock , BcbCounter ) > + ' _ {
197
234
self . bcb_counters
198
235
. iter_enumerated_mut ( )
199
236
. filter_map ( |( bcb, counter) | Some ( ( bcb, counter. take ( ) ?) ) )
200
237
}
201
238
202
239
pub ( super ) fn drain_bcb_edge_counters (
203
240
& mut self ,
204
- ) -> impl Iterator < Item = ( ( BasicCoverageBlock , BasicCoverageBlock ) , CoverageKind ) > + ' _ {
241
+ ) -> impl Iterator < Item = ( ( BasicCoverageBlock , BasicCoverageBlock ) , BcbCounter ) > + ' _ {
205
242
self . bcb_edge_counters . drain ( )
206
243
}
207
244
}
@@ -653,7 +690,7 @@ impl<'a> MakeBcbCounters<'a> {
653
690
self . branch_counter ( branch) . is_none ( )
654
691
}
655
692
656
- fn branch_counter ( & self , branch : & BcbBranch ) -> Option < & CoverageKind > {
693
+ fn branch_counter ( & self , branch : & BcbBranch ) -> Option < & BcbCounter > {
657
694
let to_bcb = branch. target_bcb ;
658
695
if let Some ( from_bcb) = branch. edge_from_bcb {
659
696
self . coverage_counters . bcb_edge_counters . get ( & ( from_bcb, to_bcb) )
@@ -675,7 +712,7 @@ impl<'a> MakeBcbCounters<'a> {
675
712
}
676
713
677
714
#[ inline]
678
- fn format_counter ( & self , counter_kind : & CoverageKind ) -> String {
715
+ fn format_counter ( & self , counter_kind : & BcbCounter ) -> String {
679
716
self . coverage_counters . debug_counters . format_counter ( counter_kind)
680
717
}
681
718
}
0 commit comments