Skip to content

Commit 0661a3d

Browse files
committed
Remove inherent methods from coverageinfo::ffi::Counter
This patch also moves `Regions` to a different module, since that type can stay put when the FFI bindings move to another crate.
1 parent f955c76 commit 0661a3d

File tree

3 files changed

+45
-54
lines changed

3 files changed

+45
-54
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use rustc_middle::mir::coverage::{CounterId, CovTerm, ExpressionId};
2-
31
/// Must match the layout of `LLVMRustCounterKind`.
42
#[derive(Copy, Clone, Debug)]
53
#[repr(C)]
@@ -26,30 +24,12 @@ pub(crate) enum CounterKind {
2624
pub(crate) struct Counter {
2725
// Important: The layout (order and types of fields) must match its C++ counterpart.
2826
pub(crate) kind: CounterKind,
29-
id: u32,
27+
pub(crate) id: u32,
3028
}
3129

3230
impl Counter {
3331
/// A `Counter` of kind `Zero`. For this counter kind, the `id` is not used.
3432
pub(crate) const ZERO: Self = Self { kind: CounterKind::Zero, id: 0 };
35-
36-
/// Constructs a new `Counter` of kind `CounterValueReference`.
37-
pub(crate) fn counter_value_reference(counter_id: CounterId) -> Self {
38-
Self { kind: CounterKind::CounterValueReference, id: counter_id.as_u32() }
39-
}
40-
41-
/// Constructs a new `Counter` of kind `Expression`.
42-
pub(crate) fn expression(expression_id: ExpressionId) -> Self {
43-
Self { kind: CounterKind::Expression, id: expression_id.as_u32() }
44-
}
45-
46-
pub(crate) fn from_term(term: CovTerm) -> Self {
47-
match term {
48-
CovTerm::Zero => Self::ZERO,
49-
CovTerm::Counter(id) => Self::counter_value_reference(id),
50-
CovTerm::Expression(id) => Self::expression(id),
51-
}
52-
}
5333
}
5434

5535
/// Corresponds to enum `llvm::coverage::CounterExpression::ExprKind`.
@@ -94,29 +74,6 @@ pub(crate) struct CoverageSpan {
9474
pub(crate) end_col: u32,
9575
}
9676

97-
/// Holds tables of the various region types in one struct.
98-
///
99-
/// Don't pass this struct across FFI; pass the individual region tables as
100-
/// pointer/length pairs instead.
101-
///
102-
/// Each field name has a `_regions` suffix for improved readability after
103-
/// exhaustive destructing, which ensures that all region types are handled.
104-
#[derive(Clone, Debug, Default)]
105-
pub(crate) struct Regions {
106-
pub(crate) code_regions: Vec<CodeRegion>,
107-
pub(crate) expansion_regions: Vec<ExpansionRegion>,
108-
pub(crate) branch_regions: Vec<BranchRegion>,
109-
}
110-
111-
impl Regions {
112-
/// Returns true if none of this structure's tables contain any regions.
113-
pub(crate) fn has_no_regions(&self) -> bool {
114-
let Self { code_regions, expansion_regions, branch_regions } = self;
115-
116-
code_regions.is_empty() && expansion_regions.is_empty() && branch_regions.is_empty()
117-
}
118-
}
119-
12077
/// Must match the layout of `LLVMRustCoverageCodeRegion`.
12178
#[derive(Clone, Debug)]
12279
#[repr(C)]

compiler/rustc_codegen_llvm/src/coverageinfo/llvm_cov.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,35 @@ pub(crate) fn write_filenames_to_buffer(filenames: &[impl AsRef<str>]) -> Vec<u8
5757
})
5858
}
5959

60+
/// Holds tables of the various region types in one struct.
61+
///
62+
/// Don't pass this struct across FFI; pass the individual region tables as
63+
/// pointer/length pairs instead.
64+
///
65+
/// Each field name has a `_regions` suffix for improved readability after
66+
/// exhaustive destructing, which ensures that all region types are handled.
67+
#[derive(Clone, Debug, Default)]
68+
pub(crate) struct Regions {
69+
pub(crate) code_regions: Vec<ffi::CodeRegion>,
70+
pub(crate) expansion_regions: Vec<ffi::ExpansionRegion>,
71+
pub(crate) branch_regions: Vec<ffi::BranchRegion>,
72+
}
73+
74+
impl Regions {
75+
/// Returns true if none of this structure's tables contain any regions.
76+
pub(crate) fn has_no_regions(&self) -> bool {
77+
let Self { code_regions, expansion_regions, branch_regions } = self;
78+
79+
code_regions.is_empty() && expansion_regions.is_empty() && branch_regions.is_empty()
80+
}
81+
}
82+
6083
pub(crate) fn write_function_mappings_to_buffer(
6184
virtual_file_mapping: &[u32],
6285
expressions: &[ffi::CounterExpression],
63-
regions: &ffi::Regions,
86+
regions: &Regions,
6487
) -> Vec<u8> {
65-
let ffi::Regions { code_regions, expansion_regions, branch_regions } = regions;
88+
let Regions { code_regions, expansion_regions, branch_regions } = regions;
6689

6790
// SAFETY:
6891
// - All types are FFI-compatible and have matching representations in Rust/C++.

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::sync::Arc;
1010
use rustc_abi::Align;
1111
use rustc_codegen_ssa::traits::{BaseTypeCodegenMethods as _, ConstCodegenMethods};
1212
use rustc_middle::mir::coverage::{
13-
BasicCoverageBlock, CovTerm, CoverageIdsInfo, Expression, FunctionCoverageInfo, Mapping,
14-
MappingKind, Op,
13+
BasicCoverageBlock, CounterId, CovTerm, CoverageIdsInfo, Expression, ExpressionId,
14+
FunctionCoverageInfo, Mapping, MappingKind, Op,
1515
};
1616
use rustc_middle::ty::{Instance, TyCtxt};
1717
use rustc_span::{SourceFile, Span};
@@ -36,7 +36,7 @@ pub(crate) struct CovfunRecord<'tcx> {
3636

3737
virtual_file_mapping: VirtualFileMapping,
3838
expressions: Vec<ffi::CounterExpression>,
39-
regions: ffi::Regions,
39+
regions: llvm_cov::Regions,
4040
}
4141

4242
impl<'tcx> CovfunRecord<'tcx> {
@@ -64,7 +64,7 @@ pub(crate) fn prepare_covfun_record<'tcx>(
6464
is_used,
6565
virtual_file_mapping: VirtualFileMapping::default(),
6666
expressions,
67-
regions: ffi::Regions::default(),
67+
regions: llvm_cov::Regions::default(),
6868
};
6969

7070
fill_region_tables(tcx, fn_cov_info, ids_info, &mut covfun);
@@ -77,10 +77,21 @@ pub(crate) fn prepare_covfun_record<'tcx>(
7777
Some(covfun)
7878
}
7979

80+
pub(crate) fn counter_for_term(term: CovTerm) -> ffi::Counter {
81+
use ffi::Counter;
82+
match term {
83+
CovTerm::Zero => Counter::ZERO,
84+
CovTerm::Counter(id) => {
85+
Counter { kind: ffi::CounterKind::CounterValueReference, id: CounterId::as_u32(id) }
86+
}
87+
CovTerm::Expression(id) => {
88+
Counter { kind: ffi::CounterKind::Expression, id: ExpressionId::as_u32(id) }
89+
}
90+
}
91+
}
92+
8093
/// Convert the function's coverage-counter expressions into a form suitable for FFI.
8194
fn prepare_expressions(ids_info: &CoverageIdsInfo) -> Vec<ffi::CounterExpression> {
82-
let counter_for_term = ffi::Counter::from_term;
83-
8495
// We know that LLVM will optimize out any unused expressions before
8596
// producing the final coverage map, so there's no need to do the same
8697
// thing on the Rust side unless we're confident we can do much better.
@@ -113,7 +124,7 @@ fn fill_region_tables<'tcx>(
113124
} else {
114125
CovTerm::Zero
115126
};
116-
ffi::Counter::from_term(term)
127+
counter_for_term(term)
117128
};
118129

119130
// Currently a function's mappings must all be in the same file, so use the
@@ -136,7 +147,7 @@ fn fill_region_tables<'tcx>(
136147
if discard_all { None } else { spans::make_coords(source_map, &source_file, span) }
137148
};
138149

139-
let ffi::Regions {
150+
let llvm_cov::Regions {
140151
code_regions,
141152
expansion_regions: _, // FIXME(Zalathar): Fill out support for expansion regions
142153
branch_regions,

0 commit comments

Comments
 (0)