Skip to content

Commit ac43716

Browse files
committed
coverage: Store branch spans in the expansion tree
1 parent 61c923b commit ac43716

File tree

4 files changed

+29
-24
lines changed

4 files changed

+29
-24
lines changed

compiler/rustc_mir_transform/src/coverage/expansion.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
22
use rustc_middle::mir;
3-
use rustc_middle::mir::coverage::BasicCoverageBlock;
3+
use rustc_middle::mir::coverage::{BasicCoverageBlock, BranchSpan};
44
use rustc_span::{ExpnId, ExpnKind, Span};
55

66
use crate::coverage::from_mir;
@@ -83,6 +83,9 @@ pub(crate) struct ExpnNode {
8383
/// Expansions whose call-site is in this expansion.
8484
pub(crate) child_expn_ids: FxIndexSet<ExpnId>,
8585

86+
/// Branch spans (recorded during MIR building) belonging to this expansion.
87+
pub(crate) branch_spans: Vec<BranchSpan>,
88+
8689
/// Hole spans belonging to this expansion, to be carved out from the
8790
/// code spans during span refinement.
8891
pub(crate) hole_spans: Vec<Span>,
@@ -108,6 +111,8 @@ impl ExpnNode {
108111
spans: vec![],
109112
child_expn_ids: FxIndexSet::default(),
110113

114+
branch_spans: vec![],
115+
111116
hole_spans: vec![],
112117
}
113118
}
@@ -174,5 +179,15 @@ pub(crate) fn build_expn_tree(
174179
node.hole_spans.push(hole_span);
175180
}
176181

182+
// Associate each branch span (recorded during MIR building) with its
183+
// corresponding expansion tree node.
184+
if let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() {
185+
for branch_span in &coverage_info_hi.branch_spans {
186+
if let Some(node) = nodes.get_mut(&branch_span.span.ctxt().outer_expn()) {
187+
node.branch_spans.push(BranchSpan::clone(branch_span));
188+
}
189+
}
190+
}
191+
177192
ExpnTree { nodes }
178193
}

compiler/rustc_mir_transform/src/coverage/mappings.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use rustc_middle::mir::coverage::{
44
};
55
use rustc_middle::mir::{self, BasicBlock, StatementKind};
66
use rustc_middle::ty::TyCtxt;
7+
use rustc_span::ExpnKind;
78

8-
use crate::coverage::expansion;
9+
use crate::coverage::expansion::{self, ExpnTree};
910
use crate::coverage::graph::CoverageGraph;
1011
use crate::coverage::hir_info::ExtractedHirInfo;
1112
use crate::coverage::spans::extract_refined_covspans;
12-
use crate::coverage::unexpand::unexpand_into_body_span;
1313

1414
#[derive(Default)]
1515
pub(crate) struct ExtractedMappings {
@@ -31,7 +31,7 @@ pub(crate) fn extract_mappings_from_mir<'tcx>(
3131
// Extract ordinary code mappings from MIR statement/terminator spans.
3232
extract_refined_covspans(tcx, hir_info, graph, &expn_tree, &mut mappings);
3333

34-
extract_branch_mappings(mir_body, hir_info, graph, &mut mappings);
34+
extract_branch_mappings(mir_body, hir_info, graph, &expn_tree, &mut mappings);
3535

3636
ExtractedMappings { mappings }
3737
}
@@ -57,25 +57,25 @@ fn resolve_block_markers(
5757
block_markers
5858
}
5959

60-
pub(super) fn extract_branch_mappings(
60+
fn extract_branch_mappings(
6161
mir_body: &mir::Body<'_>,
6262
hir_info: &ExtractedHirInfo,
6363
graph: &CoverageGraph,
64+
expn_tree: &ExpnTree,
6465
mappings: &mut Vec<Mapping>,
6566
) {
6667
let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() else { return };
67-
6868
let block_markers = resolve_block_markers(coverage_info_hi, mir_body);
6969

70-
mappings.extend(coverage_info_hi.branch_spans.iter().filter_map(
71-
|&BranchSpan { span: raw_span, true_marker, false_marker }| try {
72-
// For now, ignore any branch span that was introduced by
73-
// expansion. This makes things like assert macros less noisy.
74-
if !raw_span.ctxt().outer_expn_data().is_root() {
75-
return None;
76-
}
77-
let span = unexpand_into_body_span(raw_span, hir_info.body_span)?;
70+
// For now, ignore any branch span that was introduced by
71+
// expansion. This makes things like assert macros less noisy.
72+
let Some(node) = expn_tree.get(hir_info.body_span.ctxt().outer_expn()) else { return };
73+
if node.expn_kind != ExpnKind::Root {
74+
return;
75+
}
7876

77+
mappings.extend(node.branch_spans.iter().filter_map(
78+
|&BranchSpan { span, true_marker, false_marker }| try {
7979
let bcb_from_marker = |marker: BlockMarkerId| graph.bcb_from_bb(block_markers[marker]?);
8080

8181
let true_bcb = bcb_from_marker(true_marker)?;

compiler/rustc_mir_transform/src/coverage/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ pub(super) mod query;
1717
mod spans;
1818
#[cfg(test)]
1919
mod tests;
20-
mod unexpand;
2120

2221
/// Inserts `StatementKind::Coverage` statements that either instrument the binary with injected
2322
/// counters, via intrinsic `llvm.instrprof.increment`, and/or inject metadata used during codegen

compiler/rustc_mir_transform/src/coverage/unexpand.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)