@@ -68,64 +68,31 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
68
68
69
69
struct Instrumentor < ' a , ' tcx > {
70
70
tcx : TyCtxt < ' tcx > ,
71
+ hir_info : ExtractedHirInfo ,
71
72
mir_body : & ' a mut mir:: Body < ' tcx > ,
72
- fn_sig_span : Span ,
73
- body_span : Span ,
74
- function_source_hash : u64 ,
75
73
basic_coverage_blocks : CoverageGraph ,
76
74
coverage_counters : CoverageCounters ,
77
75
}
78
76
79
77
impl < ' a , ' tcx > Instrumentor < ' a , ' tcx > {
80
78
fn new ( tcx : TyCtxt < ' tcx > , mir_body : & ' a mut mir:: Body < ' tcx > ) -> Self {
81
- let source_map = tcx. sess . source_map ( ) ;
82
- let def_id = mir_body. source . def_id ( ) ;
83
- let ( some_fn_sig, hir_body) = fn_sig_and_body ( tcx, def_id) ;
79
+ let hir_info = extract_hir_info ( tcx, mir_body) ;
84
80
85
- let body_span = get_body_span ( tcx, hir_body, mir_body) ;
86
-
87
- let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
88
- let fn_sig_span = match some_fn_sig. filter ( |fn_sig| {
89
- fn_sig. span . eq_ctxt ( body_span)
90
- && Lrc :: ptr_eq ( & source_file, & source_map. lookup_source_file ( fn_sig. span . lo ( ) ) )
91
- } ) {
92
- Some ( fn_sig) => fn_sig. span . with_hi ( body_span. lo ( ) ) ,
93
- None => body_span. shrink_to_lo ( ) ,
94
- } ;
95
-
96
- debug ! (
97
- "instrumenting {}: {:?}, fn sig span: {:?}, body span: {:?}" ,
98
- if tcx. is_closure( def_id) { "closure" } else { "function" } ,
99
- def_id,
100
- fn_sig_span,
101
- body_span
102
- ) ;
81
+ debug ! ( ?hir_info, "instrumenting {:?}" , mir_body. source. def_id( ) ) ;
103
82
104
- let function_source_hash = hash_mir_source ( tcx, hir_body) ;
105
83
let basic_coverage_blocks = CoverageGraph :: from_mir ( mir_body) ;
106
84
let coverage_counters = CoverageCounters :: new ( & basic_coverage_blocks) ;
107
85
108
- Self {
109
- tcx,
110
- mir_body,
111
- fn_sig_span,
112
- body_span,
113
- function_source_hash,
114
- basic_coverage_blocks,
115
- coverage_counters,
116
- }
86
+ Self { tcx, hir_info, mir_body, basic_coverage_blocks, coverage_counters }
117
87
}
118
88
119
89
fn inject_counters ( & ' a mut self ) {
120
- let fn_sig_span = self . fn_sig_span ;
121
- let body_span = self . body_span ;
122
-
123
90
////////////////////////////////////////////////////
124
91
// Compute coverage spans from the `CoverageGraph`.
125
92
let coverage_spans = CoverageSpans :: generate_coverage_spans (
126
93
self . mir_body ,
127
- fn_sig_span,
128
- body_span,
94
+ self . hir_info . fn_sig_span ,
95
+ self . hir_info . body_span ,
129
96
& self . basic_coverage_blocks ,
130
97
) ;
131
98
@@ -141,7 +108,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
141
108
let mappings = self . create_mappings_and_inject_coverage_statements ( & coverage_spans) ;
142
109
143
110
self . mir_body . function_coverage_info = Some ( Box :: new ( FunctionCoverageInfo {
144
- function_source_hash : self . function_source_hash ,
111
+ function_source_hash : self . hir_info . function_source_hash ,
145
112
num_counters : self . coverage_counters . num_counters ( ) ,
146
113
expressions : self . coverage_counters . take_expressions ( ) ,
147
114
mappings,
@@ -156,7 +123,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
156
123
coverage_spans : & CoverageSpans ,
157
124
) -> Vec < Mapping > {
158
125
let source_map = self . tcx . sess . source_map ( ) ;
159
- let body_span = self . body_span ;
126
+ let body_span = self . hir_info . body_span ;
160
127
161
128
let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
162
129
use rustc_session:: RemapFileNameExt ;
@@ -327,6 +294,35 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
327
294
true
328
295
}
329
296
297
+ /// Function information extracted from HIR by the coverage instrumentor.
298
+ #[ derive( Debug ) ]
299
+ struct ExtractedHirInfo {
300
+ function_source_hash : u64 ,
301
+ fn_sig_span : Span ,
302
+ body_span : Span ,
303
+ }
304
+
305
+ fn extract_hir_info < ' tcx > ( tcx : TyCtxt < ' tcx > , mir_body : & mir:: Body < ' tcx > ) -> ExtractedHirInfo {
306
+ let source_map = tcx. sess . source_map ( ) ;
307
+ let def_id = mir_body. source . def_id ( ) ;
308
+ let ( some_fn_sig, hir_body) = fn_sig_and_body ( tcx, def_id) ;
309
+
310
+ let body_span = get_body_span ( tcx, hir_body, mir_body) ;
311
+
312
+ let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
313
+ let fn_sig_span = match some_fn_sig. filter ( |fn_sig| {
314
+ fn_sig. span . eq_ctxt ( body_span)
315
+ && Lrc :: ptr_eq ( & source_file, & source_map. lookup_source_file ( fn_sig. span . lo ( ) ) )
316
+ } ) {
317
+ Some ( fn_sig) => fn_sig. span . with_hi ( body_span. lo ( ) ) ,
318
+ None => body_span. shrink_to_lo ( ) ,
319
+ } ;
320
+
321
+ let function_source_hash = hash_mir_source ( tcx, hir_body) ;
322
+
323
+ ExtractedHirInfo { function_source_hash, fn_sig_span, body_span }
324
+ }
325
+
330
326
fn fn_sig_and_body (
331
327
tcx : TyCtxt < ' _ > ,
332
328
def_id : DefId ,
@@ -342,7 +338,7 @@ fn fn_sig_and_body(
342
338
fn get_body_span < ' tcx > (
343
339
tcx : TyCtxt < ' tcx > ,
344
340
hir_body : & rustc_hir:: Body < ' tcx > ,
345
- mir_body : & mut mir:: Body < ' tcx > ,
341
+ mir_body : & mir:: Body < ' tcx > ,
346
342
) -> Span {
347
343
let mut body_span = hir_body. value . span ;
348
344
let def_id = mir_body. source . def_id ( ) ;
0 commit comments