2023-08-30 13:29:37 +00:00
|
|
|
use rustc_data_structures::captures::Captures;
|
2024-02-08 01:21:22 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
|
|
|
use rustc_middle::mir::coverage::{CounterId, CoverageKind};
|
2024-03-23 08:13:52 +00:00
|
|
|
use rustc_middle::mir::{Body, CoverageIdsInfo, Statement, StatementKind};
|
2024-02-08 01:21:22 +00:00
|
|
|
use rustc_middle::query::TyCtxtAt;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2024-02-08 00:46:26 +00:00
|
|
|
use rustc_middle::util::Providers;
|
2024-02-08 01:21:22 +00:00
|
|
|
use rustc_span::def_id::LocalDefId;
|
2024-06-20 00:49:40 +00:00
|
|
|
use rustc_span::sym;
|
2024-08-28 05:03:14 +00:00
|
|
|
use tracing::trace;
|
2020-10-23 07:45:07 +00:00
|
|
|
|
2024-02-08 00:46:26 +00:00
|
|
|
/// Registers query/hook implementations related to coverage.
|
2020-10-23 07:45:07 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2024-02-08 01:21:22 +00:00
|
|
|
providers.hooks.is_eligible_for_coverage =
|
|
|
|
|TyCtxtAt { tcx, .. }, def_id| is_eligible_for_coverage(tcx, def_id);
|
2024-06-20 00:49:40 +00:00
|
|
|
providers.queries.coverage_attr_on = coverage_attr_on;
|
2024-02-08 00:46:26 +00:00
|
|
|
providers.queries.coverage_ids_info = coverage_ids_info;
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
|
|
|
|
2024-02-08 01:21:22 +00:00
|
|
|
/// Hook implementation for [`TyCtxt::is_eligible_for_coverage`].
|
|
|
|
fn is_eligible_for_coverage(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
|
|
|
// Only instrument functions, methods, and closures (not constants since they are evaluated
|
|
|
|
// at compile time by Miri).
|
|
|
|
// FIXME(#73156): Handle source code coverage in const eval, but note, if and when const
|
|
|
|
// expressions get coverage spans, we will probably have to "carve out" space for const
|
|
|
|
// expressions from coverage spans in enclosing MIR's, like we do for closures. (That might
|
|
|
|
// be tricky if const expressions have no corresponding statements in the enclosing MIR.
|
|
|
|
// Closures are carved out by their initial `Assign` statement.)
|
|
|
|
if !tcx.def_kind(def_id).is_fn_like() {
|
|
|
|
trace!("InstrumentCoverage skipped for {def_id:?} (not an fn-like)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't instrument functions with `#[automatically_derived]` on their
|
|
|
|
// enclosing impl block, on the assumption that most users won't care about
|
|
|
|
// coverage for derived impls.
|
|
|
|
if let Some(impl_of) = tcx.impl_of_method(def_id.to_def_id())
|
|
|
|
&& tcx.is_automatically_derived(impl_of)
|
|
|
|
{
|
|
|
|
trace!("InstrumentCoverage skipped for {def_id:?} (automatically derived)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:49:40 +00:00
|
|
|
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::NAKED) {
|
|
|
|
trace!("InstrumentCoverage skipped for {def_id:?} (`#[naked]`)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !tcx.coverage_attr_on(def_id) {
|
2024-02-08 01:21:22 +00:00
|
|
|
trace!("InstrumentCoverage skipped for {def_id:?} (`#[coverage(off)]`)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-06-20 00:49:40 +00:00
|
|
|
/// Query implementation for `coverage_attr_on`.
|
|
|
|
fn coverage_attr_on(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
2024-06-22 06:34:24 +00:00
|
|
|
// Check for annotations directly on this def.
|
2024-06-20 00:49:40 +00:00
|
|
|
if let Some(attr) = tcx.get_attr(def_id, sym::coverage) {
|
|
|
|
match attr.meta_item_list().as_deref() {
|
|
|
|
Some([item]) if item.has_name(sym::off) => return false,
|
|
|
|
Some([item]) if item.has_name(sym::on) => return true,
|
|
|
|
Some(_) | None => {
|
|
|
|
// Other possibilities should have been rejected by `rustc_parse::validate_attr`.
|
2024-10-03 06:53:51 +00:00
|
|
|
// Use `span_delayed_bug` to avoid an ICE in failing builds (#127880).
|
|
|
|
tcx.dcx().span_delayed_bug(attr.span, "unexpected value of coverage attribute");
|
2024-06-20 00:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-22 06:34:24 +00:00
|
|
|
match tcx.opt_local_parent(def_id) {
|
|
|
|
// Check the parent def (and so on recursively) until we find an
|
|
|
|
// enclosing attribute or reach the crate root.
|
|
|
|
Some(parent) => tcx.coverage_attr_on(parent),
|
|
|
|
// We reached the crate root without seeing a coverage attribute, so
|
|
|
|
// allow coverage instrumentation by default.
|
|
|
|
None => true,
|
|
|
|
}
|
2024-06-20 00:49:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 02:15:40 +00:00
|
|
|
/// Query implementation for `coverage_ids_info`.
|
|
|
|
fn coverage_ids_info<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-06-17 01:35:16 +00:00
|
|
|
instance_def: ty::InstanceKind<'tcx>,
|
2023-09-13 02:15:40 +00:00
|
|
|
) -> CoverageIdsInfo {
|
2021-05-12 03:56:23 +00:00
|
|
|
let mir_body = tcx.instance_mir(instance_def);
|
2020-10-23 07:45:07 +00:00
|
|
|
|
2023-09-13 02:15:40 +00:00
|
|
|
let max_counter_id = all_coverage_in_mir_body(mir_body)
|
2024-03-23 08:13:52 +00:00
|
|
|
.filter_map(|kind| match *kind {
|
2023-09-13 02:51:43 +00:00
|
|
|
CoverageKind::CounterIncrement { id } => Some(id),
|
2023-09-13 02:15:40 +00:00
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.max()
|
2024-04-04 00:04:32 +00:00
|
|
|
.unwrap_or(CounterId::ZERO);
|
2020-10-23 07:45:07 +00:00
|
|
|
|
2024-04-30 13:36:08 +00:00
|
|
|
CoverageIdsInfo { max_counter_id }
|
2020-10-23 07:45:07 +00:00
|
|
|
}
|
2020-12-01 07:58:08 +00:00
|
|
|
|
2023-08-30 13:29:37 +00:00
|
|
|
fn all_coverage_in_mir_body<'a, 'tcx>(
|
|
|
|
body: &'a Body<'tcx>,
|
2024-03-23 08:13:52 +00:00
|
|
|
) -> impl Iterator<Item = &'a CoverageKind> + Captures<'tcx> {
|
2023-08-30 13:29:37 +00:00
|
|
|
body.basic_blocks.iter().flat_map(|bb_data| &bb_data.statements).filter_map(|statement| {
|
|
|
|
match statement.kind {
|
2024-03-23 08:13:52 +00:00
|
|
|
StatementKind::Coverage(ref kind) if !is_inlined(body, statement) => Some(kind),
|
2023-08-30 13:29:37 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-13 00:00:00 +00:00
|
|
|
fn is_inlined(body: &Body<'_>, statement: &Statement<'_>) -> bool {
|
|
|
|
let scope_data = &body.source_scopes[statement.source_info.scope];
|
|
|
|
scope_data.inlined.is_some() || scope_data.inlined_parent_scope.is_some()
|
|
|
|
}
|