Skip to content

Commit d50499a

Browse files
committed
coverage: Extract is_eligible_for_coverage
1 parent f571e76 commit d50499a

File tree

1 file changed

+28
-20
lines changed
  • compiler/rustc_mir_transform/src/coverage

1 file changed

+28
-20
lines changed

compiler/rustc_mir_transform/src/coverage/mod.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_middle::mir::{
2222
TerminatorKind,
2323
};
2424
use rustc_middle::ty::TyCtxt;
25-
use rustc_span::def_id::DefId;
25+
use rustc_span::def_id::{DefId, LocalDefId};
2626
use rustc_span::source_map::SourceMap;
2727
use rustc_span::{ExpnKind, SourceFile, Span, Symbol};
2828

@@ -43,18 +43,9 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
4343
// be transformed, so it should never see promoted MIR.
4444
assert!(mir_source.promoted.is_none());
4545

46-
let is_fn_like =
47-
tcx.hir().get_by_def_id(mir_source.def_id().expect_local()).fn_kind().is_some();
48-
49-
// Only instrument functions, methods, and closures (not constants since they are evaluated
50-
// at compile time by Miri).
51-
// FIXME(#73156): Handle source code coverage in const eval, but note, if and when const
52-
// expressions get coverage spans, we will probably have to "carve out" space for const
53-
// expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
54-
// be tricky if const expressions have no corresponding statements in the enclosing MIR.
55-
// Closures are carved out by their initial `Assign` statement.)
56-
if !is_fn_like {
57-
trace!("InstrumentCoverage skipped for {:?} (not an fn-like)", mir_source.def_id());
46+
let def_id = mir_source.def_id().expect_local();
47+
if !is_eligible_for_coverage(tcx, def_id) {
48+
trace!("InstrumentCoverage skipped for {def_id:?} (not eligible)");
5849
return;
5950
}
6051

@@ -66,14 +57,9 @@ impl<'tcx> MirPass<'tcx> for InstrumentCoverage {
6657
_ => {}
6758
}
6859

69-
let codegen_fn_attrs = tcx.codegen_fn_attrs(mir_source.def_id());
70-
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
71-
return;
72-
}
73-
74-
trace!("InstrumentCoverage starting for {:?}", mir_source.def_id());
60+
trace!("InstrumentCoverage starting for {def_id:?}");
7561
Instrumentor::new(tcx, mir_body).inject_counters();
76-
trace!("InstrumentCoverage done for {:?}", mir_source.def_id());
62+
trace!("InstrumentCoverage done for {def_id:?}");
7763
}
7864
}
7965

@@ -319,6 +305,28 @@ fn make_code_region(
319305
}
320306
}
321307

308+
fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
309+
let is_fn_like = tcx.hir().get_by_def_id(def_id).fn_kind().is_some();
310+
311+
// Only instrument functions, methods, and closures (not constants since they are evaluated
312+
// at compile time by Miri).
313+
// FIXME(#73156): Handle source code coverage in const eval, but note, if and when const
314+
// expressions get coverage spans, we will probably have to "carve out" space for const
315+
// expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
316+
// be tricky if const expressions have no corresponding statements in the enclosing MIR.
317+
// Closures are carved out by their initial `Assign` statement.)
318+
if !is_fn_like {
319+
return false;
320+
}
321+
322+
let codegen_fn_attrs = tcx.codegen_fn_attrs(def_id);
323+
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_COVERAGE) {
324+
return false;
325+
}
326+
327+
true
328+
}
329+
322330
fn fn_sig_and_body(
323331
tcx: TyCtxt<'_>,
324332
def_id: DefId,

0 commit comments

Comments
 (0)