2023-12-28 14:07:18 +11:00
|
|
|
use rustc_middle::mir::coverage::{CodeRegion, CounterId, CovTerm, ExpressionId, MappingKind};
|
2020-07-28 23:09:16 -07: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)]
|
2020-10-05 16:36:10 -07:00
|
|
|
pub 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)]
|
|
|
|
pub struct Counter {
|
|
|
|
// Important: The layout (order and types of fields) must match its C++ counterpart.
|
2020-10-05 16:36:10 -07:00
|
|
|
pub 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`.
|
|
|
|
pub fn counter_value_reference(counter_id: CounterId) -> Self {
|
|
|
|
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)]
|
|
|
|
pub enum ExprKind {
|
|
|
|
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)]
|
|
|
|
pub struct CounterExpression {
|
2020-10-05 16:36:10 -07:00
|
|
|
pub kind: ExprKind,
|
|
|
|
pub lhs: Counter,
|
|
|
|
pub rhs: Counter,
|
2020-07-28 23:09:16 -07:00
|
|
|
}
|
|
|
|
|
2023-07-22 18:23:39 +10:00
|
|
|
/// Corresponds to enum `llvm::coverage::CounterMappingRegion::RegionKind`.
|
|
|
|
///
|
|
|
|
/// Must match the layout of `LLVMRustCounterMappingRegionKind`.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub enum RegionKind {
|
|
|
|
/// A CodeRegion associates some code with a counter
|
|
|
|
CodeRegion = 0,
|
|
|
|
|
|
|
|
/// An ExpansionRegion represents a file expansion region that associates
|
|
|
|
/// a source range with the expansion of a virtual source file, such as
|
|
|
|
/// for a macro instantiation or #include file.
|
|
|
|
ExpansionRegion = 1,
|
|
|
|
|
|
|
|
/// A SkippedRegion represents a source range with code that was skipped
|
|
|
|
/// by a preprocessor or similar means.
|
|
|
|
SkippedRegion = 2,
|
|
|
|
|
|
|
|
/// A GapRegion is like a CodeRegion, but its count is only set as the
|
|
|
|
/// line execution count when its the only region in the line.
|
|
|
|
GapRegion = 3,
|
|
|
|
|
|
|
|
/// A BranchRegion represents leaf-level boolean expressions and is
|
|
|
|
/// associated with two counters, each representing the number of times the
|
|
|
|
/// expression evaluates to true or false.
|
|
|
|
BranchRegion = 4,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
|
|
|
|
/// coverage map, in accordance with the
|
|
|
|
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
|
|
|
|
/// The struct composes fields representing the `Counter` type and value(s) (injected counter
|
|
|
|
/// ID, or expression type and operands), the source file (an indirect index into a "filenames
|
|
|
|
/// array", encoded separately), and source location (start and end positions of the represented
|
|
|
|
/// code region).
|
|
|
|
///
|
|
|
|
/// Corresponds to struct `llvm::coverage::CounterMappingRegion`.
|
|
|
|
///
|
|
|
|
/// Must match the layout of `LLVMRustCounterMappingRegion`.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct CounterMappingRegion {
|
|
|
|
/// The counter type and type-dependent counter data, if any.
|
|
|
|
counter: Counter,
|
|
|
|
|
|
|
|
/// If the `RegionKind` is a `BranchRegion`, this represents the counter
|
|
|
|
/// for the false branch of the region.
|
|
|
|
false_counter: Counter,
|
|
|
|
|
|
|
|
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
|
|
|
|
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
|
|
|
|
/// that, in turn, are used to look up the filename for this region.
|
|
|
|
file_id: u32,
|
|
|
|
|
|
|
|
/// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find
|
|
|
|
/// the mapping regions created as a result of macro expansion, by checking if their file id
|
|
|
|
/// matches the expanded file id.
|
|
|
|
expanded_file_id: u32,
|
|
|
|
|
|
|
|
/// 1-based starting line of the mapping region.
|
|
|
|
start_line: u32,
|
|
|
|
|
|
|
|
/// 1-based starting column of the mapping region.
|
|
|
|
start_col: u32,
|
|
|
|
|
|
|
|
/// 1-based ending line of the mapping region.
|
|
|
|
end_line: u32,
|
|
|
|
|
|
|
|
/// 1-based ending column of the mapping region. If the high bit is set, the current
|
|
|
|
/// mapping region is a gap area.
|
|
|
|
end_col: u32,
|
|
|
|
|
|
|
|
kind: RegionKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CounterMappingRegion {
|
2023-12-28 14:07:18 +11:00
|
|
|
pub(crate) fn from_mapping(
|
|
|
|
mapping_kind: &MappingKind,
|
|
|
|
local_file_id: u32,
|
|
|
|
code_region: &CodeRegion,
|
|
|
|
) -> Self {
|
|
|
|
let &CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = code_region;
|
|
|
|
match *mapping_kind {
|
|
|
|
MappingKind::Code(term) => Self::code_region(
|
|
|
|
Counter::from_term(term),
|
|
|
|
local_file_id,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 18:23:39 +10:00
|
|
|
pub(crate) fn code_region(
|
|
|
|
counter: Counter,
|
|
|
|
file_id: u32,
|
|
|
|
start_line: u32,
|
|
|
|
start_col: u32,
|
|
|
|
end_line: u32,
|
|
|
|
end_col: u32,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
counter,
|
2023-08-02 15:55:37 +10:00
|
|
|
false_counter: Counter::ZERO,
|
2023-07-22 18:23:39 +10:00
|
|
|
file_id,
|
|
|
|
expanded_file_id: 0,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
kind: RegionKind::CodeRegion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function might be used in the future; the LLVM API is still evolving, as is coverage
|
|
|
|
// support.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) fn branch_region(
|
|
|
|
counter: Counter,
|
|
|
|
false_counter: Counter,
|
|
|
|
file_id: u32,
|
|
|
|
start_line: u32,
|
|
|
|
start_col: u32,
|
|
|
|
end_line: u32,
|
|
|
|
end_col: u32,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
counter,
|
|
|
|
false_counter,
|
|
|
|
file_id,
|
|
|
|
expanded_file_id: 0,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
kind: RegionKind::BranchRegion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function might be used in the future; the LLVM API is still evolving, as is coverage
|
|
|
|
// support.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) fn expansion_region(
|
|
|
|
file_id: u32,
|
|
|
|
expanded_file_id: u32,
|
|
|
|
start_line: u32,
|
|
|
|
start_col: u32,
|
|
|
|
end_line: u32,
|
|
|
|
end_col: u32,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-08-02 15:55:37 +10:00
|
|
|
counter: Counter::ZERO,
|
|
|
|
false_counter: Counter::ZERO,
|
2023-07-22 18:23:39 +10:00
|
|
|
file_id,
|
|
|
|
expanded_file_id,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
kind: RegionKind::ExpansionRegion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function might be used in the future; the LLVM API is still evolving, as is coverage
|
|
|
|
// support.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) fn skipped_region(
|
|
|
|
file_id: u32,
|
|
|
|
start_line: u32,
|
|
|
|
start_col: u32,
|
|
|
|
end_line: u32,
|
|
|
|
end_col: u32,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
2023-08-02 15:55:37 +10:00
|
|
|
counter: Counter::ZERO,
|
|
|
|
false_counter: Counter::ZERO,
|
2023-07-22 18:23:39 +10:00
|
|
|
file_id,
|
|
|
|
expanded_file_id: 0,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col,
|
|
|
|
kind: RegionKind::SkippedRegion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This function might be used in the future; the LLVM API is still evolving, as is coverage
|
|
|
|
// support.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) fn gap_region(
|
|
|
|
counter: Counter,
|
|
|
|
file_id: u32,
|
|
|
|
start_line: u32,
|
|
|
|
start_col: u32,
|
|
|
|
end_line: u32,
|
|
|
|
end_col: u32,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
counter,
|
2023-08-02 15:55:37 +10:00
|
|
|
false_counter: Counter::ZERO,
|
2023-07-22 18:23:39 +10:00
|
|
|
file_id,
|
|
|
|
expanded_file_id: 0,
|
|
|
|
start_line,
|
|
|
|
start_col,
|
|
|
|
end_line,
|
|
|
|
end_col: (1_u32 << 31) | end_col,
|
|
|
|
kind: RegionKind::GapRegion,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|