Skip to content

Commit c783871

Browse files
committed
coverage: Hoist expansion tree creation out of span refinement
This is an incremental step towards making the expansion tree central to coverage mapping creation, which will be needed for proper expansion region support.
1 parent 29a6971 commit c783871

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

compiler/rustc_mir_transform/src/coverage/expansion.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
2+
use rustc_middle::mir;
23
use rustc_middle::mir::coverage::BasicCoverageBlock;
34
use rustc_span::{ExpnId, ExpnKind, Span};
45

6+
use crate::coverage::from_mir;
7+
use crate::coverage::graph::CoverageGraph;
8+
59
#[derive(Clone, Copy, Debug)]
610
pub(crate) struct SpanWithBcb {
711
pub(crate) span: Span,
@@ -92,13 +96,17 @@ impl ExpnNode {
9296
}
9397
}
9498

95-
/// Given a collection of span/BCB pairs from potentially-different syntax contexts,
99+
/// Extracts raw span/BCB pairs from potentially-different syntax contexts, and
96100
/// arranges them into an "expansion tree" based on their expansion call-sites.
97-
pub(crate) fn build_expn_tree(spans: impl IntoIterator<Item = SpanWithBcb>) -> ExpnTree {
101+
pub(crate) fn build_expn_tree(mir_body: &mir::Body<'_>, graph: &CoverageGraph) -> ExpnTree {
102+
let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, graph);
103+
98104
let mut nodes = FxIndexMap::default();
99105
let new_node = |&expn_id: &ExpnId| ExpnNode::new(expn_id);
100106

101-
for span_with_bcb in spans {
107+
for from_mir::RawSpanFromMir { raw_span, bcb } in raw_spans {
108+
let span_with_bcb = SpanWithBcb { span: raw_span, bcb };
109+
102110
// Create a node for this span's enclosing expansion, and add the span to it.
103111
let expn_id = span_with_bcb.span.ctxt().outer_expn();
104112
let node = nodes.entry(expn_id).or_insert_with_key(new_node);

compiler/rustc_mir_transform/src/coverage/mappings.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::mir::coverage::{
55
use rustc_middle::mir::{self, BasicBlock, StatementKind};
66
use rustc_middle::ty::TyCtxt;
77

8+
use crate::coverage::expansion;
89
use crate::coverage::graph::CoverageGraph;
910
use crate::coverage::hir_info::ExtractedHirInfo;
1011
use crate::coverage::spans::extract_refined_covspans;
@@ -23,10 +24,12 @@ pub(crate) fn extract_mappings_from_mir<'tcx>(
2324
hir_info: &ExtractedHirInfo,
2425
graph: &CoverageGraph,
2526
) -> ExtractedMappings {
27+
let expn_tree = expansion::build_expn_tree(mir_body, graph);
28+
2629
let mut mappings = vec![];
2730

2831
// Extract ordinary code mappings from MIR statement/terminator spans.
29-
extract_refined_covspans(tcx, mir_body, hir_info, graph, &mut mappings);
32+
extract_refined_covspans(tcx, hir_info, graph, &expn_tree, &mut mappings);
3033

3134
extract_branch_mappings(mir_body, hir_info, graph, &mut mappings);
3235

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::coverage::mappings::ExtractedMappings;
99

1010
mod counters;
1111
mod expansion;
12+
mod from_mir;
1213
mod graph;
1314
mod hir_info;
1415
mod mappings;

compiler/rustc_mir_transform/src/coverage/spans.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
use rustc_middle::mir;
21
use rustc_middle::mir::coverage::{Mapping, MappingKind, START_BCB};
32
use rustc_middle::ty::TyCtxt;
43
use rustc_span::source_map::SourceMap;
54
use rustc_span::{BytePos, DesugaringKind, ExpnId, ExpnKind, MacroKind, Span};
65
use tracing::instrument;
76

8-
use crate::coverage::expansion::{self, ExpnTree, SpanWithBcb};
7+
use crate::coverage::expansion::{ExpnTree, SpanWithBcb};
8+
use crate::coverage::from_mir::Hole;
99
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};
1010
use crate::coverage::hir_info::ExtractedHirInfo;
11-
use crate::coverage::spans::from_mir::{Hole, RawSpanFromMir};
12-
13-
mod from_mir;
1411

1512
pub(super) fn extract_refined_covspans<'tcx>(
1613
tcx: TyCtxt<'tcx>,
17-
mir_body: &mir::Body<'tcx>,
1814
hir_info: &ExtractedHirInfo,
1915
graph: &CoverageGraph,
16+
expn_tree: &ExpnTree,
2017
mappings: &mut Vec<Mapping>,
2118
) {
2219
if hir_info.is_async_fn {
@@ -32,14 +29,6 @@ pub(super) fn extract_refined_covspans<'tcx>(
3229

3330
let &ExtractedHirInfo { body_span, .. } = hir_info;
3431

35-
let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, graph);
36-
// Use the raw spans to build a tree of expansions for this function.
37-
let expn_tree = expansion::build_expn_tree(
38-
raw_spans
39-
.into_iter()
40-
.map(|RawSpanFromMir { raw_span, bcb }| SpanWithBcb { span: raw_span, bcb }),
41-
);
42-
4332
let mut covspans = vec![];
4433
let mut push_covspan = |covspan: Covspan| {
4534
let covspan_span = covspan.span;

0 commit comments

Comments
 (0)