2024-12-18 21:00:51 +11:00
|
|
|
use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId};
|
2024-11-10 11:53:04 +11:00
|
|
|
|
2023-05-08 13:57:20 +10:00
|
|
|
/// Must match the layout of `LLVMRustCounterKind`.
|
2020-07-28 23:09:16 -07:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) enum CounterKind {
|
2020-07-28 23:09:16 -07:00
|
|
|
Zero = 0,
|
|
|
|
|
CounterValueReference = 1,
|
|
|
|
|
Expression = 2,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A reference to an instance of an abstract "counter" that will yield a value in a coverage
|
|
|
|
|
/// report. Note that `id` has different interpretations, depending on the `kind`:
|
|
|
|
|
/// * For `CounterKind::Zero`, `id` is assumed to be `0`
|
|
|
|
|
/// * For `CounterKind::CounterValueReference`, `id` matches the `counter_id` of the injected
|
|
|
|
|
/// instrumentation counter (the `index` argument to the LLVM intrinsic
|
|
|
|
|
/// `instrprof.increment()`)
|
|
|
|
|
/// * For `CounterKind::Expression`, `id` is the index into the coverage map's array of
|
|
|
|
|
/// counter expressions.
|
2023-05-08 13:57:20 +10:00
|
|
|
///
|
|
|
|
|
/// Corresponds to struct `llvm::coverage::Counter`.
|
|
|
|
|
///
|
|
|
|
|
/// Must match the layout of `LLVMRustCounter`.
|
2020-07-28 23:09:16 -07:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) struct Counter {
|
2020-07-28 23:09:16 -07:00
|
|
|
// Important: The layout (order and types of fields) must match its C++ counterpart.
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) kind: CounterKind,
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 00:08:48 -07:00
|
|
|
id: u32,
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Counter {
|
2023-08-02 15:55:37 +10:00
|
|
|
/// A `Counter` of kind `Zero`. For this counter kind, the `id` is not used.
|
|
|
|
|
pub(crate) const ZERO: Self = Self { kind: CounterKind::Zero, id: 0 };
|
2020-07-28 23:09:16 -07:00
|
|
|
|
2023-06-29 12:36:19 +10:00
|
|
|
/// Constructs a new `Counter` of kind `CounterValueReference`.
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) fn counter_value_reference(counter_id: CounterId) -> Self {
|
2023-06-29 12:36:19 +10:00
|
|
|
Self { kind: CounterKind::CounterValueReference, id: counter_id.as_u32() }
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
|
|
|
|
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 00:08:48 -07:00
|
|
|
/// Constructs a new `Counter` of kind `Expression`.
|
2023-07-23 11:48:31 +10:00
|
|
|
pub(crate) fn expression(expression_id: ExpressionId) -> Self {
|
|
|
|
|
Self { kind: CounterKind::Expression, id: expression_id.as_u32() }
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 00:08:48 -07:00
|
|
|
|
2023-08-31 16:03:12 +10:00
|
|
|
pub(crate) fn from_term(term: CovTerm) -> Self {
|
|
|
|
|
match term {
|
|
|
|
|
CovTerm::Zero => Self::ZERO,
|
|
|
|
|
CovTerm::Counter(id) => Self::counter_value_reference(id),
|
|
|
|
|
CovTerm::Expression(id) => Self::expression(id),
|
2023-07-23 11:48:31 +10:00
|
|
|
}
|
Translate counters from Rust 1-based to LLVM 0-based counter ids
A colleague contacted me and asked why Rust's counters start at 1, when
Clangs appear to start at 0. There is a reason why Rust's internal
counters start at 1 (see the docs), and I tried to keep them consistent
when codegenned to LLVM's coverage mapping format. LLVM should be
tolerant of missing counters, but as my colleague pointed out,
`llvm-cov` will silently fail to generate a coverage report for a
function based on LLVM's assumption that the counters are 0-based.
See:
https://github.com/llvm/llvm-project/blob/main/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp#L170
Apparently, if, for example, a function has no branches, it would have
exactly 1 counter. `CounterValues.size()` would be 1, and (with the
1-based index), the counter ID would be 1. This would fail the check
and abort reporting coverage for the function.
It turns out that by correcting for this during coverage map generation,
by subtracting 1 from the Rust Counter ID (both when generating the
counter increment intrinsic call, and when adding counters to the map),
some uncovered functions (including in tests) now appear covered! This
corrects the coverage for a few tests!
2021-04-02 00:08:48 -07:00
|
|
|
}
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-08 13:57:20 +10:00
|
|
|
/// Corresponds to enum `llvm::coverage::CounterExpression::ExprKind`.
|
|
|
|
|
///
|
|
|
|
|
/// Must match the layout of `LLVMRustCounterExprKind`.
|
2020-07-28 23:09:16 -07:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) enum ExprKind {
|
2020-07-28 23:09:16 -07:00
|
|
|
Subtract = 0,
|
|
|
|
|
Add = 1,
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 13:57:20 +10:00
|
|
|
/// Corresponds to struct `llvm::coverage::CounterExpression`.
|
|
|
|
|
///
|
|
|
|
|
/// Must match the layout of `LLVMRustCounterExpression`.
|
2020-07-28 23:09:16 -07:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
2024-10-19 21:47:42 +11:00
|
|
|
pub(crate) struct CounterExpression {
|
|
|
|
|
pub(crate) kind: ExprKind,
|
|
|
|
|
pub(crate) lhs: Counter,
|
|
|
|
|
pub(crate) rhs: Counter,
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
|
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) mod mcdc {
|
2024-07-25 14:26:36 +08:00
|
|
|
use rustc_middle::mir::coverage::{ConditionId, ConditionInfo, DecisionInfo};
|
2024-04-19 10:53:04 +08:00
|
|
|
|
|
|
|
|
/// Must match the layout of `LLVMRustMCDCDecisionParameters`.
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) struct DecisionParameters {
|
2024-04-19 10:53:04 +08:00
|
|
|
bitmap_idx: u32,
|
2024-05-30 13:16:07 +10:00
|
|
|
num_conditions: u16,
|
2024-04-19 10:53:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type LLVMConditionId = i16;
|
|
|
|
|
|
|
|
|
|
/// Must match the layout of `LLVMRustMCDCBranchParameters`.
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
#[derive(Clone, Copy, Debug, Default)]
|
2024-07-06 22:26:42 +10:00
|
|
|
pub(crate) struct BranchParameters {
|
2024-04-19 10:53:04 +08:00
|
|
|
condition_id: LLVMConditionId,
|
|
|
|
|
condition_ids: [LLVMConditionId; 2],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<ConditionInfo> for BranchParameters {
|
|
|
|
|
fn from(value: ConditionInfo) -> Self {
|
2024-07-25 14:26:36 +08:00
|
|
|
let to_llvm_cond_id = |cond_id: Option<ConditionId>| {
|
|
|
|
|
cond_id.and_then(|id| LLVMConditionId::try_from(id.as_usize()).ok()).unwrap_or(-1)
|
|
|
|
|
};
|
|
|
|
|
let ConditionInfo { condition_id, true_next_id, false_next_id } = value;
|
2024-04-19 10:53:04 +08:00
|
|
|
Self {
|
2024-07-25 14:26:36 +08:00
|
|
|
condition_id: to_llvm_cond_id(Some(condition_id)),
|
|
|
|
|
condition_ids: [to_llvm_cond_id(false_next_id), to_llvm_cond_id(true_next_id)],
|
2024-04-19 10:53:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<DecisionInfo> for DecisionParameters {
|
2024-05-30 13:16:07 +10:00
|
|
|
fn from(info: DecisionInfo) -> Self {
|
|
|
|
|
let DecisionInfo { bitmap_idx, num_conditions } = info;
|
|
|
|
|
Self { bitmap_idx, num_conditions }
|
2024-04-19 10:53:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-22 18:23:39 +10:00
|
|
|
}
|
|
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// A span of source code coordinates to be embedded in coverage metadata.
|
2023-07-22 18:23:39 +10:00
|
|
|
///
|
2024-10-19 22:22:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageSpan`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
2023-07-22 18:23:39 +10:00
|
|
|
#[repr(C)]
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) struct CoverageSpan {
|
|
|
|
|
/// Local index into the function's local-to-global file ID table.
|
|
|
|
|
/// The value at that index is itself an index into the coverage filename
|
|
|
|
|
/// table in the CGU's `__llvm_covmap` section.
|
2024-12-18 21:00:51 +11:00
|
|
|
pub(crate) file_id: u32,
|
2023-07-22 18:23:39 +10:00
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// 1-based starting line of the source code span.
|
2024-12-18 21:00:51 +11:00
|
|
|
pub(crate) start_line: u32,
|
2024-10-19 22:22:43 +11:00
|
|
|
/// 1-based starting column of the source code span.
|
2024-12-18 21:00:51 +11:00
|
|
|
pub(crate) start_col: u32,
|
2024-10-19 22:22:43 +11:00
|
|
|
/// 1-based ending line of the source code span.
|
2024-12-18 21:00:51 +11:00
|
|
|
pub(crate) end_line: u32,
|
2024-10-19 22:22:43 +11:00
|
|
|
/// 1-based ending column of the source code span. High bit must be unset.
|
2024-12-18 21:00:51 +11:00
|
|
|
pub(crate) end_col: u32,
|
2024-10-19 22:22:43 +11:00
|
|
|
}
|
2023-07-22 18:23:39 +10:00
|
|
|
|
2024-12-11 15:41:02 +11:00
|
|
|
/// Holds tables of the various region types in one struct.
|
|
|
|
|
///
|
|
|
|
|
/// Don't pass this struct across FFI; pass the individual region tables as
|
|
|
|
|
/// pointer/length pairs instead.
|
|
|
|
|
///
|
|
|
|
|
/// Each field name has a `_regions` suffix for improved readability after
|
|
|
|
|
/// exhaustive destructing, which ensures that all region types are handled.
|
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
|
pub(crate) struct Regions {
|
|
|
|
|
pub(crate) code_regions: Vec<CodeRegion>,
|
2025-03-04 21:55:43 +11:00
|
|
|
pub(crate) expansion_regions: Vec<ExpansionRegion>,
|
2024-12-11 15:41:02 +11:00
|
|
|
pub(crate) branch_regions: Vec<BranchRegion>,
|
|
|
|
|
pub(crate) mcdc_branch_regions: Vec<MCDCBranchRegion>,
|
|
|
|
|
pub(crate) mcdc_decision_regions: Vec<MCDCDecisionRegion>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Regions {
|
|
|
|
|
/// Returns true if none of this structure's tables contain any regions.
|
|
|
|
|
pub(crate) fn has_no_regions(&self) -> bool {
|
2025-03-04 21:55:43 +11:00
|
|
|
let Self {
|
|
|
|
|
code_regions,
|
|
|
|
|
expansion_regions,
|
|
|
|
|
branch_regions,
|
|
|
|
|
mcdc_branch_regions,
|
|
|
|
|
mcdc_decision_regions,
|
|
|
|
|
} = self;
|
2024-12-11 15:41:02 +11:00
|
|
|
|
|
|
|
|
code_regions.is_empty()
|
2025-03-04 21:55:43 +11:00
|
|
|
&& expansion_regions.is_empty()
|
2024-12-11 15:41:02 +11:00
|
|
|
&& branch_regions.is_empty()
|
|
|
|
|
&& mcdc_branch_regions.is_empty()
|
|
|
|
|
&& mcdc_decision_regions.is_empty()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageCodeRegion`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub(crate) struct CodeRegion {
|
2024-12-18 18:59:27 +11:00
|
|
|
pub(crate) cov_span: CoverageSpan,
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) counter: Counter,
|
|
|
|
|
}
|
2024-04-19 10:53:04 +08:00
|
|
|
|
2025-03-04 21:55:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageExpansionRegion`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub(crate) struct ExpansionRegion {
|
|
|
|
|
pub(crate) cov_span: CoverageSpan,
|
|
|
|
|
pub(crate) expanded_file_id: u32,
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageBranchRegion`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub(crate) struct BranchRegion {
|
2024-12-18 18:59:27 +11:00
|
|
|
pub(crate) cov_span: CoverageSpan,
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) true_counter: Counter,
|
|
|
|
|
pub(crate) false_counter: Counter,
|
|
|
|
|
}
|
2023-07-22 18:23:39 +10:00
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageMCDCBranchRegion`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub(crate) struct MCDCBranchRegion {
|
2024-12-18 18:59:27 +11:00
|
|
|
pub(crate) cov_span: CoverageSpan,
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) true_counter: Counter,
|
|
|
|
|
pub(crate) false_counter: Counter,
|
|
|
|
|
pub(crate) mcdc_branch_params: mcdc::BranchParameters,
|
|
|
|
|
}
|
2023-07-22 18:23:39 +10:00
|
|
|
|
2024-10-19 22:22:43 +11:00
|
|
|
/// Must match the layout of `LLVMRustCoverageMCDCDecisionRegion`.
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub(crate) struct MCDCDecisionRegion {
|
2024-12-18 18:59:27 +11:00
|
|
|
pub(crate) cov_span: CoverageSpan,
|
2024-10-19 22:22:43 +11:00
|
|
|
pub(crate) mcdc_decision_params: mcdc::DecisionParameters,
|
2023-07-22 18:23:39 +10:00
|
|
|
}
|