1
1
use std:: cmp:: Ordering ;
2
- use std:: fmt:: { self , Debug } ;
3
2
4
3
use either:: Either ;
5
4
use itertools:: Itertools ;
@@ -110,39 +109,6 @@ fn transcribe_counters(
110
109
new
111
110
}
112
111
113
- /// The coverage counter or counter expression associated with a particular
114
- /// BCB node or BCB edge.
115
- #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
116
- enum BcbCounter {
117
- Counter { id : CounterId } ,
118
- Expression { id : ExpressionId } ,
119
- }
120
-
121
- impl BcbCounter {
122
- fn as_term ( & self ) -> CovTerm {
123
- match * self {
124
- BcbCounter :: Counter { id, .. } => CovTerm :: Counter ( id) ,
125
- BcbCounter :: Expression { id, .. } => CovTerm :: Expression ( id) ,
126
- }
127
- }
128
- }
129
-
130
- impl Debug for BcbCounter {
131
- fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
132
- match self {
133
- Self :: Counter { id, .. } => write ! ( fmt, "Counter({:?})" , id. index( ) ) ,
134
- Self :: Expression { id } => write ! ( fmt, "Expression({:?})" , id. index( ) ) ,
135
- }
136
- }
137
- }
138
-
139
- #[ derive( Clone , Copy , Debug , PartialEq , Eq , Hash ) ]
140
- struct BcbExpression {
141
- lhs : BcbCounter ,
142
- op : Op ,
143
- rhs : BcbCounter ,
144
- }
145
-
146
112
/// Generates and stores coverage counter and coverage expression information
147
113
/// associated with nodes in the coverage graph.
148
114
pub ( super ) struct CoverageCounters {
@@ -152,14 +118,14 @@ pub(super) struct CoverageCounters {
152
118
next_counter_id : CounterId ,
153
119
154
120
/// Coverage counters/expressions that are associated with individual BCBs.
155
- node_counters : IndexVec < BasicCoverageBlock , Option < BcbCounter > > ,
121
+ node_counters : IndexVec < BasicCoverageBlock , Option < CovTerm > > ,
156
122
157
123
/// Table of expression data, associating each expression ID with its
158
124
/// corresponding operator (+ or -) and its LHS/RHS operands.
159
- expressions : IndexVec < ExpressionId , BcbExpression > ,
125
+ expressions : IndexVec < ExpressionId , Expression > ,
160
126
/// Remember expressions that have already been created (or simplified),
161
127
/// so that we don't create unnecessary duplicates.
162
- expressions_memo : FxHashMap < BcbExpression , BcbCounter > ,
128
+ expressions_memo : FxHashMap < Expression , CovTerm > ,
163
129
}
164
130
165
131
impl CoverageCounters {
@@ -174,35 +140,35 @@ impl CoverageCounters {
174
140
}
175
141
176
142
/// Returns the physical counter for the given node, creating it if necessary.
177
- fn ensure_phys_counter ( & mut self , bcb : BasicCoverageBlock ) -> BcbCounter {
143
+ fn ensure_phys_counter ( & mut self , bcb : BasicCoverageBlock ) -> CovTerm {
178
144
let id = * self . phys_counter_for_node . entry ( bcb) . or_insert_with ( || {
179
145
let id = self . next_counter_id ;
180
146
self . next_counter_id = id + 1 ;
181
147
id
182
148
} ) ;
183
- BcbCounter :: Counter { id }
149
+ CovTerm :: Counter ( id )
184
150
}
185
151
186
- fn make_expression ( & mut self , lhs : BcbCounter , op : Op , rhs : BcbCounter ) -> BcbCounter {
187
- let new_expr = BcbExpression { lhs, op, rhs } ;
188
- * self . expressions_memo . entry ( new_expr) . or_insert_with ( || {
152
+ fn make_expression ( & mut self , lhs : CovTerm , op : Op , rhs : CovTerm ) -> CovTerm {
153
+ let new_expr = Expression { lhs, op, rhs } ;
154
+ * self . expressions_memo . entry ( new_expr. clone ( ) ) . or_insert_with ( || {
189
155
let id = self . expressions . push ( new_expr) ;
190
- BcbCounter :: Expression { id }
156
+ CovTerm :: Expression ( id )
191
157
} )
192
158
}
193
159
194
160
/// Creates a counter that is the sum of the given counters.
195
161
///
196
162
/// Returns `None` if the given list of counters was empty.
197
- fn make_sum ( & mut self , counters : & [ BcbCounter ] ) -> Option < BcbCounter > {
163
+ fn make_sum ( & mut self , counters : & [ CovTerm ] ) -> Option < CovTerm > {
198
164
counters
199
165
. iter ( )
200
166
. copied ( )
201
167
. reduce ( |accum, counter| self . make_expression ( accum, Op :: Add , counter) )
202
168
}
203
169
204
170
/// Creates a counter whose value is `lhs - SUM(rhs)`.
205
- fn make_subtracted_sum ( & mut self , lhs : BcbCounter , rhs : & [ BcbCounter ] ) -> BcbCounter {
171
+ fn make_subtracted_sum ( & mut self , lhs : CovTerm , rhs : & [ CovTerm ] ) -> CovTerm {
206
172
let Some ( rhs_sum) = self . make_sum ( rhs) else { return lhs } ;
207
173
self . make_expression ( lhs, Op :: Subtract , rhs_sum)
208
174
}
@@ -213,7 +179,7 @@ impl CoverageCounters {
213
179
num_counters
214
180
}
215
181
216
- fn set_node_counter ( & mut self , bcb : BasicCoverageBlock , counter : BcbCounter ) -> BcbCounter {
182
+ fn set_node_counter ( & mut self , bcb : BasicCoverageBlock , counter : CovTerm ) -> CovTerm {
217
183
let existing = self . node_counters [ bcb] . replace ( counter) ;
218
184
assert ! (
219
185
existing. is_none( ) ,
@@ -223,7 +189,7 @@ impl CoverageCounters {
223
189
}
224
190
225
191
pub ( super ) fn term_for_bcb ( & self , bcb : BasicCoverageBlock ) -> Option < CovTerm > {
226
- self . node_counters [ bcb] . map ( |counter| counter . as_term ( ) )
192
+ self . node_counters [ bcb]
227
193
}
228
194
229
195
/// Returns an iterator over all the nodes in the coverage graph that
@@ -242,27 +208,13 @@ impl CoverageCounters {
242
208
) -> impl Iterator < Item = ( BasicCoverageBlock , ExpressionId ) > + Captures < ' _ > {
243
209
self . node_counters . iter_enumerated ( ) . filter_map ( |( bcb, & counter) | match counter {
244
210
// Yield the BCB along with its associated expression ID.
245
- Some ( BcbCounter :: Expression { id } ) => Some ( ( bcb, id) ) ,
211
+ Some ( CovTerm :: Expression ( id ) ) => Some ( ( bcb, id) ) ,
246
212
// This BCB is associated with a counter or nothing, so skip it.
247
- Some ( BcbCounter :: Counter { .. } ) | None => None ,
213
+ Some ( CovTerm :: Counter { .. } | CovTerm :: Zero ) | None => None ,
248
214
} )
249
215
}
250
216
251
217
pub ( super ) fn into_expressions ( self ) -> IndexVec < ExpressionId , Expression > {
252
- let old_len = self . expressions . len ( ) ;
253
- let expressions = self
254
- . expressions
255
- . into_iter ( )
256
- . map ( |BcbExpression { lhs, op, rhs } | Expression {
257
- lhs : lhs. as_term ( ) ,
258
- op,
259
- rhs : rhs. as_term ( ) ,
260
- } )
261
- . collect :: < IndexVec < ExpressionId , _ > > ( ) ;
262
-
263
- // Expression IDs are indexes into this vector, so make sure we didn't
264
- // accidentally invalidate them by changing its length.
265
- assert_eq ! ( old_len, expressions. len( ) ) ;
266
- expressions
218
+ self . expressions
267
219
}
268
220
}
0 commit comments