Skip to content

Commit 1ddb464

Browse files
committed
coverage: Extract helper for getting HIR info for coverage
1 parent 049f76f commit 1ddb464

File tree

1 file changed

+38
-42
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+38
-42
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,64 +68,31 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
6868

6969
struct Instrumentor<'a, 'tcx> {
7070
tcx: TyCtxt<'tcx>,
71+
hir_info: ExtractedHirInfo,
7172
mir_body: &'a mut mir::Body<'tcx>,
72-
fn_sig_span: Span,
73-
body_span: Span,
74-
function_source_hash: u64,
7573
basic_coverage_blocks: CoverageGraph,
7674
coverage_counters: CoverageCounters,
7775
}
7876

7977
impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
8078
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);
8480

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());
10382

104-
let function_source_hash = hash_mir_source(tcx, hir_body);
10583
let basic_coverage_blocks = CoverageGraph::from_mir(mir_body);
10684
let coverage_counters = CoverageCounters::new(&basic_coverage_blocks);
10785

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 }
11787
}
11888

11989
fn inject_counters(&'a mut self) {
120-
let fn_sig_span = self.fn_sig_span;
121-
let body_span = self.body_span;
122-
12390
////////////////////////////////////////////////////
12491
// Compute coverage spans from the `CoverageGraph`.
12592
let coverage_spans = CoverageSpans::generate_coverage_spans(
12693
self.mir_body,
127-
fn_sig_span,
128-
body_span,
94+
self.hir_info.fn_sig_span,
95+
self.hir_info.body_span,
12996
&self.basic_coverage_blocks,
13097
);
13198

@@ -141,7 +108,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
141108
let mappings = self.create_mappings_and_inject_coverage_statements(&coverage_spans);
142109

143110
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,
145112
num_counters: self.coverage_counters.num_counters(),
146113
expressions: self.coverage_counters.take_expressions(),
147114
mappings,
@@ -156,7 +123,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
156123
coverage_spans: &CoverageSpans,
157124
) -> Vec<Mapping> {
158125
let source_map = self.tcx.sess.source_map();
159-
let body_span = self.body_span;
126+
let body_span = self.hir_info.body_span;
160127

161128
let source_file = source_map.lookup_source_file(body_span.lo());
162129
use rustc_session::RemapFileNameExt;
@@ -327,6 +294,35 @@ fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
327294
true
328295
}
329296

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+
330326
fn fn_sig_and_body(
331327
tcx: TyCtxt<'_>,
332328
def_id: DefId,
@@ -342,7 +338,7 @@ fn fn_sig_and_body(
342338
fn get_body_span<'tcx>(
343339
tcx: TyCtxt<'tcx>,
344340
hir_body: &rustc_hir::Body<'tcx>,
345-
mir_body: &mut mir::Body<'tcx>,
341+
mir_body: &mir::Body<'tcx>,
346342
) -> Span {
347343
let mut body_span = hir_body.value.span;
348344
let def_id = mir_body.source.def_id();

0 commit comments

Comments
 (0)