@@ -10,7 +10,7 @@ use rustc_middle::ty::Instance;
10
10
/// Holds all of the coverage mapping data associated with a function instance,
11
11
/// collected during traversal of `Coverage` statements in the function's MIR.
12
12
#[ derive( Debug ) ]
13
- pub struct FunctionCoverage < ' tcx > {
13
+ pub struct FunctionCoverageCollector < ' tcx > {
14
14
/// Coverage info that was attached to this function by the instrumentor.
15
15
function_coverage_info : & ' tcx FunctionCoverageInfo ,
16
16
is_used : bool ,
@@ -26,7 +26,7 @@ pub struct FunctionCoverage<'tcx> {
26
26
expressions_seen : BitSet < ExpressionId > ,
27
27
}
28
28
29
- impl < ' tcx > FunctionCoverage < ' tcx > {
29
+ impl < ' tcx > FunctionCoverageCollector < ' tcx > {
30
30
/// Creates a new set of coverage data for a used (called) function.
31
31
pub fn new (
32
32
instance : Instance < ' tcx > ,
@@ -76,11 +76,6 @@ impl<'tcx> FunctionCoverage<'tcx> {
76
76
}
77
77
}
78
78
79
- /// Returns true for a used (called) function, and false for an unused function.
80
- pub fn is_used ( & self ) -> bool {
81
- self . is_used
82
- }
83
-
84
79
/// Marks a counter ID as having been seen in a counter-increment statement.
85
80
#[ instrument( level = "debug" , skip( self ) ) ]
86
81
pub ( crate ) fn mark_counter_id_seen ( & mut self , id : CounterId ) {
@@ -165,6 +160,28 @@ impl<'tcx> FunctionCoverage<'tcx> {
165
160
ZeroExpressions ( zero_expressions)
166
161
}
167
162
163
+ pub ( crate ) fn into_finished ( self ) -> FunctionCoverage < ' tcx > {
164
+ let zero_expressions = self . identify_zero_expressions ( ) ;
165
+ let FunctionCoverageCollector { function_coverage_info, is_used, counters_seen, .. } = self ;
166
+
167
+ FunctionCoverage { function_coverage_info, is_used, counters_seen, zero_expressions }
168
+ }
169
+ }
170
+
171
+ pub ( crate ) struct FunctionCoverage < ' tcx > {
172
+ function_coverage_info : & ' tcx FunctionCoverageInfo ,
173
+ is_used : bool ,
174
+
175
+ counters_seen : BitSet < CounterId > ,
176
+ zero_expressions : ZeroExpressions ,
177
+ }
178
+
179
+ impl < ' tcx > FunctionCoverage < ' tcx > {
180
+ /// Returns true for a used (called) function, and false for an unused function.
181
+ pub ( crate ) fn is_used ( & self ) -> bool {
182
+ self . is_used
183
+ }
184
+
168
185
/// Return the source hash, generated from the HIR node structure, and used to indicate whether
169
186
/// or not the source code structure changed between different compilations.
170
187
pub fn source_hash ( & self ) -> u64 {
@@ -177,29 +194,27 @@ impl<'tcx> FunctionCoverage<'tcx> {
177
194
pub fn get_expressions_and_counter_regions (
178
195
& self ,
179
196
) -> ( Vec < CounterExpression > , impl Iterator < Item = ( Counter , & CodeRegion ) > ) {
180
- let zero_expressions = self . identify_zero_expressions ( ) ;
181
-
182
- let counter_expressions = self . counter_expressions ( & zero_expressions) ;
197
+ let counter_expressions = self . counter_expressions ( ) ;
183
198
// Expression IDs are indices into `self.expressions`, and on the LLVM
184
199
// side they will be treated as indices into `counter_expressions`, so
185
200
// the two vectors should correspond 1:1.
186
201
assert_eq ! ( self . function_coverage_info. expressions. len( ) , counter_expressions. len( ) ) ;
187
202
188
- let counter_regions = self . counter_regions ( zero_expressions ) ;
203
+ let counter_regions = self . counter_regions ( ) ;
189
204
190
205
( counter_expressions, counter_regions)
191
206
}
192
207
193
208
/// Convert this function's coverage expression data into a form that can be
194
209
/// passed through FFI to LLVM.
195
- fn counter_expressions ( & self , zero_expressions : & ZeroExpressions ) -> Vec < CounterExpression > {
210
+ fn counter_expressions ( & self ) -> Vec < CounterExpression > {
196
211
// We know that LLVM will optimize out any unused expressions before
197
212
// producing the final coverage map, so there's no need to do the same
198
213
// thing on the Rust side unless we're confident we can do much better.
199
214
// (See `CounterExpressionsMinimizer` in `CoverageMappingWriter.cpp`.)
200
215
201
216
let counter_from_operand = |operand : CovTerm | match operand {
202
- CovTerm :: Expression ( id) if zero_expressions. contains ( id) => Counter :: ZERO ,
217
+ CovTerm :: Expression ( id) if self . zero_expressions . contains ( id) => Counter :: ZERO ,
203
218
_ => Counter :: from_term ( operand) ,
204
219
} ;
205
220
@@ -219,18 +234,15 @@ impl<'tcx> FunctionCoverage<'tcx> {
219
234
220
235
/// Converts this function's coverage mappings into an intermediate form
221
236
/// that will be used by `mapgen` when preparing for FFI.
222
- fn counter_regions (
223
- & self ,
224
- zero_expressions : ZeroExpressions ,
225
- ) -> impl Iterator < Item = ( Counter , & CodeRegion ) > {
237
+ fn counter_regions ( & self ) -> impl Iterator < Item = ( Counter , & CodeRegion ) > {
226
238
// Historically, mappings were stored directly in counter/expression
227
239
// statements in MIR, and MIR optimizations would sometimes remove them.
228
240
// That's mostly no longer true, so now we detect cases where that would
229
241
// have happened, and zero out the corresponding mappings here instead.
230
242
let counter_for_term = move |term : CovTerm | {
231
243
let force_to_zero = match term {
232
244
CovTerm :: Counter ( id) => !self . counters_seen . contains ( id) ,
233
- CovTerm :: Expression ( id) => zero_expressions. contains ( id) ,
245
+ CovTerm :: Expression ( id) => self . zero_expressions . contains ( id) ,
234
246
CovTerm :: Zero => false ,
235
247
} ;
236
248
if force_to_zero { Counter :: ZERO } else { Counter :: from_term ( term) }
0 commit comments