Skip to content

Commit 66a0cff

Browse files
committed
coverage: Add a coverage field to the MIR builder
1 parent 2004a73 commit 66a0cff

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

compiler/rustc_mir_build/src/build/coverageinfo.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ use rustc_middle::ty::TyCtxt;
55
use rustc_span::def_id::LocalDefId;
66
use rustc_span::{ExpnKind, Span};
77

8-
/// If the given item is eligible for coverage instrumentation, collect relevant
9-
/// HIR information that will be needed by the instrumentor pass.
10-
pub(crate) fn make_coverage_hir_info_if_eligible(
11-
tcx: TyCtxt<'_>,
12-
def_id: LocalDefId,
13-
) -> Option<Box<mir::coverage::HirInfo>> {
14-
assert!(tcx.sess.instrument_coverage());
8+
pub(crate) struct HirInfoBuilder {
9+
// Currently there is no extra state tracked here.
10+
}
1511

16-
is_eligible_for_coverage(tcx, def_id).then(|| Box::new(make_coverage_hir_info(tcx, def_id)))
12+
impl HirInfoBuilder {
13+
pub(crate) fn new_if_enabled_and_eligible(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<Self> {
14+
if tcx.sess.instrument_coverage() && is_eligible_for_coverage(tcx, def_id) {
15+
Some(Self {})
16+
} else {
17+
None
18+
}
19+
}
20+
21+
pub(crate) fn finish(self, tcx: TyCtxt<'_>, def_id: LocalDefId) -> Box<mir::coverage::HirInfo> {
22+
let Self {} = self;
23+
Box::new(make_coverage_hir_info(tcx, def_id))
24+
}
1725
}
1826

1927
fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {

compiler/rustc_mir_build/src/build/mod.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ struct Builder<'a, 'tcx> {
227227
// the root (most of them do) and saves us from retracing many sub-paths
228228
// many times, and rechecking many nodes.
229229
lint_level_roots_cache: GrowableBitSet<hir::ItemLocalId>,
230+
231+
coverage: Option<coverageinfo::HirInfoBuilder>,
230232
}
231233

232234
type CaptureMap<'tcx> = SortedIndexMultiMap<usize, hir::HirId, Capture<'tcx>>;
@@ -757,6 +759,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
757759
unit_temp: None,
758760
var_debug_info: vec![],
759761
lint_level_roots_cache: GrowableBitSet::new_empty(),
762+
coverage: coverageinfo::HirInfoBuilder::new_if_enabled_and_eligible(tcx, def),
760763
};
761764

762765
assert_eq!(builder.cfg.start_new_block(), START_BLOCK);
@@ -776,12 +779,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
776779
}
777780
}
778781

779-
let coverage_hir_info = if self.tcx.sess.instrument_coverage() {
780-
coverageinfo::make_coverage_hir_info_if_eligible(self.tcx, self.def_id)
781-
} else {
782-
None
783-
};
784-
785782
Body::new(
786783
MirSource::item(self.def_id.to_def_id()),
787784
self.cfg.basic_blocks,
@@ -793,7 +790,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
793790
self.fn_span,
794791
self.coroutine_kind,
795792
None,
796-
coverage_hir_info,
793+
self.coverage.map(|coverage| coverage.finish(self.tcx, self.def_id)),
797794
)
798795
}
799796

0 commit comments

Comments
 (0)