rustc_codegen_llvm/coverageinfo/
ffi.rs

1/// Must match the layout of `LLVMRustCounterKind`.
2#[derive(Copy, Clone, Debug)]
3#[repr(C)]
4pub(crate) enum CounterKind {
5    Zero = 0,
6    CounterValueReference = 1,
7    Expression = 2,
8}
9
10/// A reference to an instance of an abstract "counter" that will yield a value in a coverage
11/// report. Note that `id` has different interpretations, depending on the `kind`:
12///   * For `CounterKind::Zero`, `id` is assumed to be `0`
13///   * For `CounterKind::CounterValueReference`,  `id` matches the `counter_id` of the injected
14///     instrumentation counter (the `index` argument to the LLVM intrinsic
15///     `instrprof.increment()`)
16///   * For `CounterKind::Expression`, `id` is the index into the coverage map's array of
17///     counter expressions.
18///
19/// Corresponds to struct `llvm::coverage::Counter`.
20///
21/// Must match the layout of `LLVMRustCounter`.
22#[derive(Copy, Clone, Debug)]
23#[repr(C)]
24pub(crate) struct Counter {
25    // Important: The layout (order and types of fields) must match its C++ counterpart.
26    pub(crate) kind: CounterKind,
27    pub(crate) id: u32,
28}
29
30impl Counter {
31    /// A `Counter` of kind `Zero`. For this counter kind, the `id` is not used.
32    pub(crate) const ZERO: Self = Self { kind: CounterKind::Zero, id: 0 };
33}
34
35/// Corresponds to enum `llvm::coverage::CounterExpression::ExprKind`.
36///
37/// Must match the layout of `LLVMRustCounterExprKind`.
38#[derive(Copy, Clone, Debug)]
39#[repr(C)]
40pub(crate) enum ExprKind {
41    Subtract = 0,
42    Add = 1,
43}
44
45/// Corresponds to struct `llvm::coverage::CounterExpression`.
46///
47/// Must match the layout of `LLVMRustCounterExpression`.
48#[derive(Copy, Clone, Debug)]
49#[repr(C)]
50pub(crate) struct CounterExpression {
51    pub(crate) kind: ExprKind,
52    pub(crate) lhs: Counter,
53    pub(crate) rhs: Counter,
54}
55
56/// A span of source code coordinates to be embedded in coverage metadata.
57///
58/// Must match the layout of `LLVMRustCoverageSpan`.
59#[derive(Clone, Debug)]
60#[repr(C)]
61pub(crate) struct CoverageSpan {
62    /// Local index into the function's local-to-global file ID table.
63    /// The value at that index is itself an index into the coverage filename
64    /// table in the CGU's `__llvm_covmap` section.
65    pub(crate) file_id: u32,
66
67    /// 1-based starting line of the source code span.
68    pub(crate) start_line: u32,
69    /// 1-based starting column of the source code span.
70    pub(crate) start_col: u32,
71    /// 1-based ending line of the source code span.
72    pub(crate) end_line: u32,
73    /// 1-based ending column of the source code span. High bit must be unset.
74    pub(crate) end_col: u32,
75}
76
77/// Must match the layout of `LLVMRustCoverageCodeRegion`.
78#[derive(Clone, Debug)]
79#[repr(C)]
80pub(crate) struct CodeRegion {
81    pub(crate) cov_span: CoverageSpan,
82    pub(crate) counter: Counter,
83}
84
85/// Must match the layout of `LLVMRustCoverageExpansionRegion`.
86#[derive(Clone, Debug)]
87#[repr(C)]
88pub(crate) struct ExpansionRegion {
89    pub(crate) cov_span: CoverageSpan,
90    pub(crate) expanded_file_id: u32,
91}
92
93/// Must match the layout of `LLVMRustCoverageBranchRegion`.
94#[derive(Clone, Debug)]
95#[repr(C)]
96pub(crate) struct BranchRegion {
97    pub(crate) cov_span: CoverageSpan,
98    pub(crate) true_counter: Counter,
99    pub(crate) false_counter: Counter,
100}