Skip to content

Commit d6ed6e3

Browse files
committed
coverage: Consolidate FFI types into one module
Coverage FFI types were historically split across two modules, because some of them were needed by code in `rustc_codegen_ssa`. Now that all of the coverage codegen code has been moved into `rustc_codegen_llvm` (#113355), it's possible to move all of the FFI types into a single module, making it easier to see all of them at once.
1 parent 90bb418 commit d6ed6e3

File tree

5 files changed

+201
-208
lines changed

5 files changed

+201
-208
lines changed

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

+194
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,197 @@ impl CounterExpression {
8585
Self { kind, lhs, rhs }
8686
}
8787
}
88+
89+
/// Corresponds to enum `llvm::coverage::CounterMappingRegion::RegionKind`.
90+
///
91+
/// Must match the layout of `LLVMRustCounterMappingRegionKind`.
92+
#[derive(Copy, Clone, Debug)]
93+
#[repr(C)]
94+
pub enum RegionKind {
95+
/// A CodeRegion associates some code with a counter
96+
CodeRegion = 0,
97+
98+
/// An ExpansionRegion represents a file expansion region that associates
99+
/// a source range with the expansion of a virtual source file, such as
100+
/// for a macro instantiation or #include file.
101+
ExpansionRegion = 1,
102+
103+
/// A SkippedRegion represents a source range with code that was skipped
104+
/// by a preprocessor or similar means.
105+
SkippedRegion = 2,
106+
107+
/// A GapRegion is like a CodeRegion, but its count is only set as the
108+
/// line execution count when its the only region in the line.
109+
GapRegion = 3,
110+
111+
/// A BranchRegion represents leaf-level boolean expressions and is
112+
/// associated with two counters, each representing the number of times the
113+
/// expression evaluates to true or false.
114+
BranchRegion = 4,
115+
}
116+
117+
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
118+
/// coverage map, in accordance with the
119+
/// [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).
120+
/// The struct composes fields representing the `Counter` type and value(s) (injected counter
121+
/// ID, or expression type and operands), the source file (an indirect index into a "filenames
122+
/// array", encoded separately), and source location (start and end positions of the represented
123+
/// code region).
124+
///
125+
/// Corresponds to struct `llvm::coverage::CounterMappingRegion`.
126+
///
127+
/// Must match the layout of `LLVMRustCounterMappingRegion`.
128+
#[derive(Copy, Clone, Debug)]
129+
#[repr(C)]
130+
pub struct CounterMappingRegion {
131+
/// The counter type and type-dependent counter data, if any.
132+
counter: Counter,
133+
134+
/// If the `RegionKind` is a `BranchRegion`, this represents the counter
135+
/// for the false branch of the region.
136+
false_counter: Counter,
137+
138+
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
139+
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
140+
/// that, in turn, are used to look up the filename for this region.
141+
file_id: u32,
142+
143+
/// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find
144+
/// the mapping regions created as a result of macro expansion, by checking if their file id
145+
/// matches the expanded file id.
146+
expanded_file_id: u32,
147+
148+
/// 1-based starting line of the mapping region.
149+
start_line: u32,
150+
151+
/// 1-based starting column of the mapping region.
152+
start_col: u32,
153+
154+
/// 1-based ending line of the mapping region.
155+
end_line: u32,
156+
157+
/// 1-based ending column of the mapping region. If the high bit is set, the current
158+
/// mapping region is a gap area.
159+
end_col: u32,
160+
161+
kind: RegionKind,
162+
}
163+
164+
impl CounterMappingRegion {
165+
pub(crate) fn code_region(
166+
counter: Counter,
167+
file_id: u32,
168+
start_line: u32,
169+
start_col: u32,
170+
end_line: u32,
171+
end_col: u32,
172+
) -> Self {
173+
Self {
174+
counter,
175+
false_counter: Counter::zero(),
176+
file_id,
177+
expanded_file_id: 0,
178+
start_line,
179+
start_col,
180+
end_line,
181+
end_col,
182+
kind: RegionKind::CodeRegion,
183+
}
184+
}
185+
186+
// This function might be used in the future; the LLVM API is still evolving, as is coverage
187+
// support.
188+
#[allow(dead_code)]
189+
pub(crate) fn branch_region(
190+
counter: Counter,
191+
false_counter: Counter,
192+
file_id: u32,
193+
start_line: u32,
194+
start_col: u32,
195+
end_line: u32,
196+
end_col: u32,
197+
) -> Self {
198+
Self {
199+
counter,
200+
false_counter,
201+
file_id,
202+
expanded_file_id: 0,
203+
start_line,
204+
start_col,
205+
end_line,
206+
end_col,
207+
kind: RegionKind::BranchRegion,
208+
}
209+
}
210+
211+
// This function might be used in the future; the LLVM API is still evolving, as is coverage
212+
// support.
213+
#[allow(dead_code)]
214+
pub(crate) fn expansion_region(
215+
file_id: u32,
216+
expanded_file_id: u32,
217+
start_line: u32,
218+
start_col: u32,
219+
end_line: u32,
220+
end_col: u32,
221+
) -> Self {
222+
Self {
223+
counter: Counter::zero(),
224+
false_counter: Counter::zero(),
225+
file_id,
226+
expanded_file_id,
227+
start_line,
228+
start_col,
229+
end_line,
230+
end_col,
231+
kind: RegionKind::ExpansionRegion,
232+
}
233+
}
234+
235+
// This function might be used in the future; the LLVM API is still evolving, as is coverage
236+
// support.
237+
#[allow(dead_code)]
238+
pub(crate) fn skipped_region(
239+
file_id: u32,
240+
start_line: u32,
241+
start_col: u32,
242+
end_line: u32,
243+
end_col: u32,
244+
) -> Self {
245+
Self {
246+
counter: Counter::zero(),
247+
false_counter: Counter::zero(),
248+
file_id,
249+
expanded_file_id: 0,
250+
start_line,
251+
start_col,
252+
end_line,
253+
end_col,
254+
kind: RegionKind::SkippedRegion,
255+
}
256+
}
257+
258+
// This function might be used in the future; the LLVM API is still evolving, as is coverage
259+
// support.
260+
#[allow(dead_code)]
261+
pub(crate) fn gap_region(
262+
counter: Counter,
263+
file_id: u32,
264+
start_line: u32,
265+
start_col: u32,
266+
end_line: u32,
267+
end_col: u32,
268+
) -> Self {
269+
Self {
270+
counter,
271+
false_counter: Counter::zero(),
272+
file_id,
273+
expanded_file_id: 0,
274+
start_line,
275+
start_col,
276+
end_line,
277+
end_col: (1_u32 << 31) | end_col,
278+
kind: RegionKind::GapRegion,
279+
}
280+
}
281+
}

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub use super::ffi::*;
1+
use crate::coverageinfo::ffi::{Counter, CounterExpression, ExprKind};
22

33
use rustc_index::{IndexSlice, IndexVec};
44
use rustc_middle::bug;

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::common::CodegenCx;
22
use crate::coverageinfo;
3-
use crate::coverageinfo::map_data::{Counter, CounterExpression};
3+
use crate::coverageinfo::ffi::{Counter, CounterExpression, CounterMappingRegion};
44
use crate::llvm;
55

6-
use llvm::coverageinfo::CounterMappingRegion;
76
use rustc_codegen_ssa::traits::ConstMethods;
87
use rustc_data_structures::fx::FxIndexSet;
98
use rustc_hir::def::DefKind;

Diff for: compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::llvm;
33
use crate::abi::Abi;
44
use crate::builder::Builder;
55
use crate::common::CodegenCx;
6-
use crate::coverageinfo::map_data::{CounterExpression, FunctionCoverage};
6+
use crate::coverageinfo::ffi::{CounterExpression, CounterMappingRegion};
7+
use crate::coverageinfo::map_data::FunctionCoverage;
78

89
use libc::c_uint;
9-
use llvm::coverageinfo::CounterMappingRegion;
1010
use rustc_codegen_ssa::traits::{
1111
BaseTypeMethods, BuilderMethods, ConstMethods, CoverageInfoBuilderMethods, MiscMethods,
1212
StaticMethods,
@@ -27,7 +27,7 @@ use rustc_middle::ty::Ty;
2727
use std::cell::RefCell;
2828
use std::ffi::CString;
2929

30-
mod ffi;
30+
pub(crate) mod ffi;
3131
pub(crate) mod map_data;
3232
pub mod mapgen;
3333

0 commit comments

Comments
 (0)