Skip to content

Commit 31f523f

Browse files
committed
Simplified body_span and filtered span code
Some code cleanup extracted from future (but unfinished) commit to fix coverage in attr macro functions.
1 parent e354cca commit 31f523f

File tree

3 files changed

+80
-68
lines changed

3 files changed

+80
-68
lines changed

compiler/rustc_mir/src/transform/coverage/mod.rs

+43-21
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
9595

9696
trace!("InstrumentCoverage starting for {:?}", mir_source.def_id());
9797
Instrumentor::new(&self.name(), tcx, mir_body).inject_counters();
98-
trace!("InstrumentCoverage starting for {:?}", mir_source.def_id());
98+
trace!("InstrumentCoverage done for {:?}", mir_source.def_id());
9999
}
100100
}
101101

@@ -116,25 +116,7 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
116116
let def_id = mir_body.source.def_id();
117117
let (some_fn_sig, hir_body) = fn_sig_and_body(tcx, def_id);
118118

119-
let mut body_span = hir_body.value.span;
120-
121-
if tcx.is_closure(def_id) {
122-
// If the MIR function is a closure, and if the closure body span
123-
// starts from a macro, but it's content is not in that macro, try
124-
// to find a non-macro callsite, and instrument the spans there
125-
// instead.
126-
loop {
127-
let expn_data = body_span.ctxt().outer_expn_data();
128-
if expn_data.is_root() {
129-
break;
130-
}
131-
if let ExpnKind::Macro { .. } = expn_data.kind {
132-
body_span = expn_data.call_site;
133-
} else {
134-
break;
135-
}
136-
}
137-
}
119+
let body_span = get_body_span(tcx, hir_body, mir_body);
138120

139121
let source_file = source_map.lookup_source_file(body_span.lo());
140122
let fn_sig_span = match some_fn_sig.filter(|fn_sig| {
@@ -144,6 +126,15 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
144126
Some(fn_sig) => fn_sig.span.with_hi(body_span.lo()),
145127
None => body_span.shrink_to_lo(),
146128
};
129+
130+
debug!(
131+
"instrumenting {}: {:?}, fn sig span: {:?}, body span: {:?}",
132+
if tcx.is_closure(def_id) { "closure" } else { "function" },
133+
def_id,
134+
fn_sig_span,
135+
body_span
136+
);
137+
147138
let function_source_hash = hash_mir_source(tcx, hir_body);
148139
let basic_coverage_blocks = CoverageGraph::from_mir(mir_body);
149140
Self {
@@ -160,19 +151,21 @@ impl<'a, 'tcx> Instrumentor<'a, 'tcx> {
160151

161152
fn inject_counters(&'a mut self) {
162153
let tcx = self.tcx;
163-
let source_map = tcx.sess.source_map();
164154
let mir_source = self.mir_body.source;
165155
let def_id = mir_source.def_id();
166156
let fn_sig_span = self.fn_sig_span;
167157
let body_span = self.body_span;
168158

159+
<<<<<<< HEAD
169160
debug!(
170161
"instrumenting {:?}, fn sig span: {}, body span: {}",
171162
def_id,
172163
source_map.span_to_diagnostic_string(fn_sig_span),
173164
source_map.span_to_diagnostic_string(body_span)
174165
);
175166

167+
=======
168+
>>>>>>> 476104d0f54 (Simplified body_span and filtered span code)
176169
let mut graphviz_data = debug::GraphvizData::new();
177170
let mut debug_used_expressions = debug::UsedExpressions::new();
178171

@@ -561,6 +554,35 @@ fn fn_sig_and_body<'tcx>(
561554
(hir::map::fn_sig(hir_node), tcx.hir().body(fn_body_id))
562555
}
563556

557+
fn get_body_span<'tcx>(
558+
tcx: TyCtxt<'tcx>,
559+
hir_body: &rustc_hir::Body<'tcx>,
560+
mir_body: &mut mir::Body<'tcx>,
561+
) -> Span {
562+
let mut body_span = hir_body.value.span;
563+
let def_id = mir_body.source.def_id();
564+
565+
if tcx.is_closure(def_id) {
566+
// If the MIR function is a closure, and if the closure body span
567+
// starts from a macro, but it's content is not in that macro, try
568+
// to find a non-macro callsite, and instrument the spans there
569+
// instead.
570+
loop {
571+
let expn_data = body_span.ctxt().outer_expn_data();
572+
if expn_data.is_root() {
573+
break;
574+
}
575+
if let ExpnKind::Macro{..} = expn_data.kind {
576+
body_span = expn_data.call_site;
577+
} else {
578+
break;
579+
}
580+
}
581+
}
582+
583+
body_span
584+
}
585+
564586
fn hash_mir_source<'tcx>(tcx: TyCtxt<'tcx>, hir_body: &'tcx rustc_hir::Body<'tcx>) -> u64 {
565587
let mut hcx = tcx.create_no_span_stable_hashing_context();
566588
hash(&mut hcx, &hir_body.value).to_smaller_hash()

compiler/rustc_mir/src/transform/coverage/spans.rs

+35-43
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,25 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
530530
.iter()
531531
.enumerate()
532532
.filter_map(move |(index, statement)| {
533-
filtered_statement_span(statement, self.body_span).map(
534-
|(span, expn_span)| {
535-
CoverageSpan::for_statement(
536-
statement, span, expn_span, bcb, bb, index,
537-
)
538-
},
539-
)
533+
filtered_statement_span(statement).map(|span| {
534+
CoverageSpan::for_statement(
535+
statement,
536+
function_source_span(span, self.body_span),
537+
span,
538+
bcb,
539+
bb,
540+
index,
541+
)
542+
})
540543
})
541-
.chain(filtered_terminator_span(data.terminator(), self.body_span).map(
542-
|(span, expn_span)| CoverageSpan::for_terminator(span, expn_span, bcb, bb),
543-
))
544+
.chain(filtered_terminator_span(data.terminator()).map(|span| {
545+
CoverageSpan::for_terminator(
546+
function_source_span(span, self.body_span),
547+
span,
548+
bcb,
549+
bb,
550+
)
551+
}))
544552
})
545553
.collect()
546554
}
@@ -795,13 +803,9 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> {
795803
}
796804
}
797805

798-
/// See `function_source_span()` for a description of the two returned spans.
799-
/// If the MIR `Statement` is not contributive to computing coverage spans,
800-
/// returns `None`.
801-
pub(super) fn filtered_statement_span(
802-
statement: &'a Statement<'tcx>,
803-
body_span: Span,
804-
) -> Option<(Span, Span)> {
806+
/// If the MIR `Statement` has a span contributive to computing coverage spans,
807+
/// return it; otherwise return `None`.
808+
pub(super) fn filtered_statement_span(statement: &'a Statement<'tcx>) -> Option<Span> {
805809
match statement.kind {
806810
// These statements have spans that are often outside the scope of the executed source code
807811
// for their parent `BasicBlock`.
@@ -838,18 +842,14 @@ pub(super) fn filtered_statement_span(
838842
| StatementKind::LlvmInlineAsm(_)
839843
| StatementKind::Retag(_, _)
840844
| StatementKind::AscribeUserType(_, _) => {
841-
Some(function_source_span(statement.source_info.span, body_span))
845+
Some(statement.source_info.span)
842846
}
843847
}
844848
}
845849

846-
/// See `function_source_span()` for a description of the two returned spans.
847-
/// If the MIR `Terminator` is not contributive to computing coverage spans,
848-
/// returns `None`.
849-
pub(super) fn filtered_terminator_span(
850-
terminator: &'a Terminator<'tcx>,
851-
body_span: Span,
852-
) -> Option<(Span, Span)> {
850+
/// If the MIR `Terminator` has a span contributive to computing coverage spans,
851+
/// return it; otherwise return `None`.
852+
pub(super) fn filtered_terminator_span(terminator: &'a Terminator<'tcx>) -> Option<Span> {
853853
match terminator.kind {
854854
// These terminators have spans that don't positively contribute to computing a reasonable
855855
// span of actually executed source code. (For example, SwitchInt terminators extracted from
@@ -873,7 +873,7 @@ pub(super) fn filtered_terminator_span(
873873
span = span.with_lo(constant.span.lo());
874874
}
875875
}
876-
Some(function_source_span(span, body_span))
876+
Some(span)
877877
}
878878

879879
// Retain spans from all other terminators
@@ -884,28 +884,20 @@ pub(super) fn filtered_terminator_span(
884884
| TerminatorKind::GeneratorDrop
885885
| TerminatorKind::FalseUnwind { .. }
886886
| TerminatorKind::InlineAsm { .. } => {
887-
Some(function_source_span(terminator.source_info.span, body_span))
887+
Some(terminator.source_info.span)
888888
}
889889
}
890890
}
891891

892-
/// Returns two spans from the given span (the span associated with a
893-
/// `Statement` or `Terminator`):
894-
///
895-
/// 1. An extrapolated span (pre-expansion[^1]) corresponding to a range within
896-
/// the function's body source. This span is guaranteed to be contained
897-
/// within, or equal to, the `body_span`. If the extrapolated span is not
898-
/// contained within the `body_span`, the `body_span` is returned.
899-
/// 2. The actual `span` value from the `Statement`, before expansion.
900-
///
901-
/// Only the first span is used when computing coverage code regions. The second
902-
/// span is useful if additional expansion data is needed (such as to look up
903-
/// the macro name for a composed span within that macro).)
892+
/// Returns an extrapolated span (pre-expansion[^1]) corresponding to a range
893+
/// within the function's body source. This span is guaranteed to be contained
894+
/// within, or equal to, the `body_span`. If the extrapolated span is not
895+
/// contained within the `body_span`, the `body_span` is returned.
904896
///
905-
/// [^1]Expansions result from Rust syntax including macros, syntactic
906-
/// sugar, etc.).
897+
/// [^1]Expansions result from Rust syntax including macros, syntactic sugar,
898+
/// etc.).
907899
#[inline]
908-
fn function_source_span(span: Span, body_span: Span) -> (Span, Span) {
900+
pub(super) fn function_source_span(span: Span, body_span: Span) -> Span {
909901
let original_span = original_sp(span, body_span).with_ctxt(body_span.ctxt());
910-
(if body_span.contains(original_span) { original_span } else { body_span }, span)
902+
if body_span.contains(original_span) { original_span } else { body_span }
911903
}

compiler/rustc_mir/src/transform/coverage/tests.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -683,12 +683,10 @@ fn test_make_bcb_counters() {
683683
let mut basic_coverage_blocks = graph::CoverageGraph::from_mir(&mir_body);
684684
let mut coverage_spans = Vec::new();
685685
for (bcb, data) in basic_coverage_blocks.iter_enumerated() {
686-
if let Some((span, expn_span)) =
687-
spans::filtered_terminator_span(data.terminator(&mir_body), body_span)
688-
{
686+
if let Some(span) = spans::filtered_terminator_span(data.terminator(&mir_body)) {
689687
coverage_spans.push(spans::CoverageSpan::for_terminator(
688+
spans::function_source_span(span, body_span),
690689
span,
691-
expn_span,
692690
bcb,
693691
data.last_bb(),
694692
));

0 commit comments

Comments
 (0)