@@ -14,6 +14,48 @@ 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 { function_source_hash : u64 , 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 {
@@ -22,18 +64,18 @@ pub(super) struct CoverageCounters {
22
64
next_expression_id : ExpressionId ,
23
65
24
66
/// Coverage counters/expressions that are associated with individual BCBs.
25
- bcb_counters : IndexVec < BasicCoverageBlock , Option < CoverageKind > > ,
67
+ bcb_counters : IndexVec < BasicCoverageBlock , Option < BcbCounter > > ,
26
68
/// Coverage counters/expressions that are associated with the control-flow
27
69
/// edge between two BCBs.
28
- bcb_edge_counters : FxHashMap < ( BasicCoverageBlock , BasicCoverageBlock ) , CoverageKind > ,
70
+ bcb_edge_counters : FxHashMap < ( BasicCoverageBlock , BasicCoverageBlock ) , BcbCounter > ,
29
71
/// Tracks which BCBs have a counter associated with some incoming edge.
30
72
/// Only used by debug assertions, to verify that BCBs with incoming edge
31
73
/// counters do not have their own physical counters (expressions are allowed).
32
74
bcb_has_incoming_edge_counters : BitSet < BasicCoverageBlock > ,
33
75
/// Expression nodes that are not directly associated with any particular
34
76
/// BCB/edge, but are needed as operands to more complex expressions.
35
- /// These are always `CoverageKind ::Expression`.
36
- pub ( super ) intermediate_expressions : Vec < CoverageKind > ,
77
+ /// These are always [`BcbCounter ::Expression`] .
78
+ pub ( super ) intermediate_expressions : Vec < BcbCounter > ,
37
79
38
80
pub debug_counters : DebugCounters ,
39
81
}
@@ -57,12 +99,12 @@ impl CoverageCounters {
57
99
}
58
100
59
101
/// Activate the `DebugCounters` data structures, to provide additional debug formatting
60
- /// features when formatting `CoverageKind` (counter) values.
102
+ /// features when formatting [`BcbCounter`] (counter) values.
61
103
pub fn enable_debug ( & mut self ) {
62
104
self . debug_counters . enable ( ) ;
63
105
}
64
106
65
- /// Makes `CoverageKind` `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
107
+ /// Makes [`BcbCounter`] `Counter`s and `Expressions` for the `BasicCoverageBlock`s directly or
66
108
/// indirectly associated with `CoverageSpans`, and accumulates additional `Expression`s
67
109
/// representing intermediate values.
68
110
pub fn make_bcb_counters (
@@ -73,11 +115,11 @@ impl CoverageCounters {
73
115
MakeBcbCounters :: new ( self , basic_coverage_blocks) . make_bcb_counters ( coverage_spans)
74
116
}
75
117
76
- fn make_counter < F > ( & mut self , debug_block_label_fn : F ) -> CoverageKind
118
+ fn make_counter < F > ( & mut self , debug_block_label_fn : F ) -> BcbCounter
77
119
where
78
120
F : Fn ( ) -> Option < String > ,
79
121
{
80
- let counter = CoverageKind :: Counter {
122
+ let counter = BcbCounter :: Counter {
81
123
function_source_hash : self . function_source_hash ,
82
124
id : self . next_counter ( ) ,
83
125
} ;
@@ -93,19 +135,19 @@ impl CoverageCounters {
93
135
op : Op ,
94
136
rhs : Operand ,
95
137
debug_block_label_fn : F ,
96
- ) -> CoverageKind
138
+ ) -> BcbCounter
97
139
where
98
140
F : Fn ( ) -> Option < String > ,
99
141
{
100
142
let id = self . next_expression ( ) ;
101
- let expression = CoverageKind :: Expression { id, lhs, op, rhs } ;
143
+ let expression = BcbCounter :: Expression { id, lhs, op, rhs } ;
102
144
if self . debug_counters . is_enabled ( ) {
103
145
self . debug_counters . add_counter ( & expression, ( debug_block_label_fn) ( ) ) ;
104
146
}
105
147
expression
106
148
}
107
149
108
- pub fn make_identity_counter ( & mut self , counter_operand : Operand ) -> CoverageKind {
150
+ pub fn make_identity_counter ( & mut self , counter_operand : Operand ) -> BcbCounter {
109
151
let some_debug_block_label = if self . debug_counters . is_enabled ( ) {
110
152
self . debug_counters . some_block_label ( counter_operand) . cloned ( )
111
153
} else {
@@ -134,7 +176,7 @@ impl CoverageCounters {
134
176
fn set_bcb_counter (
135
177
& mut self ,
136
178
bcb : BasicCoverageBlock ,
137
- counter_kind : CoverageKind ,
179
+ counter_kind : BcbCounter ,
138
180
) -> Result < Operand , Error > {
139
181
debug_assert ! (
140
182
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
@@ -158,7 +200,7 @@ impl CoverageCounters {
158
200
& mut self ,
159
201
from_bcb : BasicCoverageBlock ,
160
202
to_bcb : BasicCoverageBlock ,
161
- counter_kind : CoverageKind ,
203
+ counter_kind : BcbCounter ,
162
204
) -> Result < Operand , Error > {
163
205
if level_enabled ! ( tracing:: Level :: DEBUG ) {
164
206
// If the BCB has an edge counter (to be injected into a new `BasicBlock`), it can also
@@ -183,25 +225,25 @@ impl CoverageCounters {
183
225
}
184
226
}
185
227
186
- pub ( super ) fn bcb_counter ( & self , bcb : BasicCoverageBlock ) -> Option < & CoverageKind > {
228
+ pub ( super ) fn bcb_counter ( & self , bcb : BasicCoverageBlock ) -> Option < & BcbCounter > {
187
229
self . bcb_counters [ bcb] . as_ref ( )
188
230
}
189
231
190
- pub ( super ) fn take_bcb_counter ( & mut self , bcb : BasicCoverageBlock ) -> Option < CoverageKind > {
232
+ pub ( super ) fn take_bcb_counter ( & mut self , bcb : BasicCoverageBlock ) -> Option < BcbCounter > {
191
233
self . bcb_counters [ bcb] . take ( )
192
234
}
193
235
194
236
pub ( super ) fn drain_bcb_counters (
195
237
& mut self ,
196
- ) -> impl Iterator < Item = ( BasicCoverageBlock , CoverageKind ) > + ' _ {
238
+ ) -> impl Iterator < Item = ( BasicCoverageBlock , BcbCounter ) > + ' _ {
197
239
self . bcb_counters
198
240
. iter_enumerated_mut ( )
199
241
. filter_map ( |( bcb, counter) | Some ( ( bcb, counter. take ( ) ?) ) )
200
242
}
201
243
202
244
pub ( super ) fn drain_bcb_edge_counters (
203
245
& mut self ,
204
- ) -> impl Iterator < Item = ( ( BasicCoverageBlock , BasicCoverageBlock ) , CoverageKind ) > + ' _ {
246
+ ) -> impl Iterator < Item = ( ( BasicCoverageBlock , BasicCoverageBlock ) , BcbCounter ) > + ' _ {
205
247
self . bcb_edge_counters . drain ( )
206
248
}
207
249
}
@@ -653,7 +695,7 @@ impl<'a> MakeBcbCounters<'a> {
653
695
self . branch_counter ( branch) . is_none ( )
654
696
}
655
697
656
- fn branch_counter ( & self , branch : & BcbBranch ) -> Option < & CoverageKind > {
698
+ fn branch_counter ( & self , branch : & BcbBranch ) -> Option < & BcbCounter > {
657
699
let to_bcb = branch. target_bcb ;
658
700
if let Some ( from_bcb) = branch. edge_from_bcb {
659
701
self . coverage_counters . bcb_edge_counters . get ( & ( from_bcb, to_bcb) )
@@ -675,7 +717,7 @@ impl<'a> MakeBcbCounters<'a> {
675
717
}
676
718
677
719
#[ inline]
678
- fn format_counter ( & self , counter_kind : & CoverageKind ) -> String {
720
+ fn format_counter ( & self , counter_kind : & BcbCounter ) -> String {
679
721
self . coverage_counters . debug_counters . format_counter ( counter_kind)
680
722
}
681
723
}
0 commit comments