@@ -78,30 +78,11 @@ struct Instrumentor<'a, 'tcx> {
7878
7979impl < ' a , ' tcx > Instrumentor < ' a , ' tcx > {
8080 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) ;
81+ let hir_info @ ExtractedHirInfo { function_source_hash, fn_sig_span, body_span } =
82+ extract_hir_info ( tcx, mir_body) ;
8483
85- let body_span = get_body_span ( tcx , hir_body , mir_body) ;
84+ debug ! ( ?hir_info , "instrumenting {:?}" , mir_body. source . def_id ( ) ) ;
8685
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- ) ;
103-
104- let function_source_hash = hash_mir_source ( tcx, hir_body) ;
10586 let basic_coverage_blocks = CoverageGraph :: from_mir ( mir_body) ;
10687 let coverage_counters = CoverageCounters :: new ( & basic_coverage_blocks) ;
10788
@@ -117,15 +98,12 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
11798 }
11899
119100 fn inject_counters ( & ' a mut self ) {
120- let fn_sig_span = self . fn_sig_span ;
121- let body_span = self . body_span ;
122-
123101 ////////////////////////////////////////////////////
124102 // Compute coverage spans from the `CoverageGraph`.
125103 let coverage_spans = CoverageSpans :: generate_coverage_spans (
126104 self . mir_body ,
127- fn_sig_span,
128- body_span,
105+ self . fn_sig_span ,
106+ self . body_span ,
129107 & self . basic_coverage_blocks ,
130108 ) ;
131109
@@ -327,6 +305,35 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
327305 true
328306}
329307
308+ /// Function information extracted from HIR by the coverage instrumentor.
309+ #[ derive( Debug ) ]
310+ struct ExtractedHirInfo {
311+ function_source_hash : u64 ,
312+ fn_sig_span : Span ,
313+ body_span : Span ,
314+ }
315+
316+ fn extract_hir_info < ' tcx > ( tcx : TyCtxt < ' tcx > , mir_body : & mir:: Body < ' tcx > ) -> ExtractedHirInfo {
317+ let source_map = tcx. sess . source_map ( ) ;
318+ let def_id = mir_body. source . def_id ( ) ;
319+ let ( some_fn_sig, hir_body) = fn_sig_and_body ( tcx, def_id) ;
320+
321+ let body_span = get_body_span ( tcx, hir_body, mir_body) ;
322+
323+ let source_file = source_map. lookup_source_file ( body_span. lo ( ) ) ;
324+ let fn_sig_span = match some_fn_sig. filter ( |fn_sig| {
325+ fn_sig. span . eq_ctxt ( body_span)
326+ && Lrc :: ptr_eq ( & source_file, & source_map. lookup_source_file ( fn_sig. span . lo ( ) ) )
327+ } ) {
328+ Some ( fn_sig) => fn_sig. span . with_hi ( body_span. lo ( ) ) ,
329+ None => body_span. shrink_to_lo ( ) ,
330+ } ;
331+
332+ let function_source_hash = hash_mir_source ( tcx, hir_body) ;
333+
334+ ExtractedHirInfo { function_source_hash, fn_sig_span, body_span }
335+ }
336+
330337fn fn_sig_and_body (
331338 tcx : TyCtxt < ' _ > ,
332339 def_id : DefId ,
@@ -342,7 +349,7 @@ fn fn_sig_and_body(
342349fn get_body_span < ' tcx > (
343350 tcx : TyCtxt < ' tcx > ,
344351 hir_body : & rustc_hir:: Body < ' tcx > ,
345- mir_body : & mut mir:: Body < ' tcx > ,
352+ mir_body : & mir:: Body < ' tcx > ,
346353) -> Span {
347354 let mut body_span = hir_body. value . span ;
348355 let def_id = mir_body. source . def_id ( ) ;
0 commit comments