Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5cbfee5

Browse files
committedAug 2, 2023
Auto merge of rust-lang#114360 - Zalathar:ffi-types, r=oli-obk
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` (rust-lang#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. --- This PR only moves code and adjusts imports; there should be no functional changes.
2 parents 7a5d2d0 + d6ed6e3 commit 5cbfee5

File tree

5 files changed

+201
-208
lines changed

5 files changed

+201
-208
lines changed
 

‎compiler/rustc_codegen_llvm/src/coverageinfo/ffi.rs

Lines changed: 194 additions & 0 deletions
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+
}

‎compiler/rustc_codegen_llvm/src/coverageinfo/map_data.rs

Lines changed: 1 addition & 1 deletion
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;

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

Lines changed: 1 addition & 2 deletions
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;

‎compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

Lines changed: 3 additions & 3 deletions
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

‎compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 2 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![allow(non_camel_case_types)]
22
#![allow(non_upper_case_globals)]
33

4-
use crate::coverageinfo::map_data as coverage_map;
5-
64
use super::debuginfo::{
75
DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
86
DIFile, DIFlags, DIGlobalVariableExpression, DILexicalBlock, DILocation, DINameSpace,
@@ -688,204 +686,6 @@ extern "C" {
688686
pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
689687
pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
690688

691-
pub mod coverageinfo {
692-
use super::coverage_map;
693-
694-
/// Corresponds to enum `llvm::coverage::CounterMappingRegion::RegionKind`.
695-
///
696-
/// Must match the layout of `LLVMRustCounterMappingRegionKind`.
697-
#[derive(Copy, Clone, Debug)]
698-
#[repr(C)]
699-
pub enum RegionKind {
700-
/// A CodeRegion associates some code with a counter
701-
CodeRegion = 0,
702-
703-
/// An ExpansionRegion represents a file expansion region that associates
704-
/// a source range with the expansion of a virtual source file, such as
705-
/// for a macro instantiation or #include file.
706-
ExpansionRegion = 1,
707-
708-
/// A SkippedRegion represents a source range with code that was skipped
709-
/// by a preprocessor or similar means.
710-
SkippedRegion = 2,
711-
712-
/// A GapRegion is like a CodeRegion, but its count is only set as the
713-
/// line execution count when its the only region in the line.
714-
GapRegion = 3,
715-
716-
/// A BranchRegion represents leaf-level boolean expressions and is
717-
/// associated with two counters, each representing the number of times the
718-
/// expression evaluates to true or false.
719-
BranchRegion = 4,
720-
}
721-
722-
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
723-
/// coverage map, in accordance with the
724-
/// [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).
725-
/// The struct composes fields representing the `Counter` type and value(s) (injected counter
726-
/// ID, or expression type and operands), the source file (an indirect index into a "filenames
727-
/// array", encoded separately), and source location (start and end positions of the represented
728-
/// code region).
729-
///
730-
/// Corresponds to struct `llvm::coverage::CounterMappingRegion`.
731-
///
732-
/// Must match the layout of `LLVMRustCounterMappingRegion`.
733-
#[derive(Copy, Clone, Debug)]
734-
#[repr(C)]
735-
pub struct CounterMappingRegion {
736-
/// The counter type and type-dependent counter data, if any.
737-
counter: coverage_map::Counter,
738-
739-
/// If the `RegionKind` is a `BranchRegion`, this represents the counter
740-
/// for the false branch of the region.
741-
false_counter: coverage_map::Counter,
742-
743-
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
744-
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
745-
/// that, in turn, are used to look up the filename for this region.
746-
file_id: u32,
747-
748-
/// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find
749-
/// the mapping regions created as a result of macro expansion, by checking if their file id
750-
/// matches the expanded file id.
751-
expanded_file_id: u32,
752-
753-
/// 1-based starting line of the mapping region.
754-
start_line: u32,
755-
756-
/// 1-based starting column of the mapping region.
757-
start_col: u32,
758-
759-
/// 1-based ending line of the mapping region.
760-
end_line: u32,
761-
762-
/// 1-based ending column of the mapping region. If the high bit is set, the current
763-
/// mapping region is a gap area.
764-
end_col: u32,
765-
766-
kind: RegionKind,
767-
}
768-
769-
impl CounterMappingRegion {
770-
pub(crate) fn code_region(
771-
counter: coverage_map::Counter,
772-
file_id: u32,
773-
start_line: u32,
774-
start_col: u32,
775-
end_line: u32,
776-
end_col: u32,
777-
) -> Self {
778-
Self {
779-
counter,
780-
false_counter: coverage_map::Counter::zero(),
781-
file_id,
782-
expanded_file_id: 0,
783-
start_line,
784-
start_col,
785-
end_line,
786-
end_col,
787-
kind: RegionKind::CodeRegion,
788-
}
789-
}
790-
791-
// This function might be used in the future; the LLVM API is still evolving, as is coverage
792-
// support.
793-
#[allow(dead_code)]
794-
pub(crate) fn branch_region(
795-
counter: coverage_map::Counter,
796-
false_counter: coverage_map::Counter,
797-
file_id: u32,
798-
start_line: u32,
799-
start_col: u32,
800-
end_line: u32,
801-
end_col: u32,
802-
) -> Self {
803-
Self {
804-
counter,
805-
false_counter,
806-
file_id,
807-
expanded_file_id: 0,
808-
start_line,
809-
start_col,
810-
end_line,
811-
end_col,
812-
kind: RegionKind::BranchRegion,
813-
}
814-
}
815-
816-
// This function might be used in the future; the LLVM API is still evolving, as is coverage
817-
// support.
818-
#[allow(dead_code)]
819-
pub(crate) fn expansion_region(
820-
file_id: u32,
821-
expanded_file_id: u32,
822-
start_line: u32,
823-
start_col: u32,
824-
end_line: u32,
825-
end_col: u32,
826-
) -> Self {
827-
Self {
828-
counter: coverage_map::Counter::zero(),
829-
false_counter: coverage_map::Counter::zero(),
830-
file_id,
831-
expanded_file_id,
832-
start_line,
833-
start_col,
834-
end_line,
835-
end_col,
836-
kind: RegionKind::ExpansionRegion,
837-
}
838-
}
839-
840-
// This function might be used in the future; the LLVM API is still evolving, as is coverage
841-
// support.
842-
#[allow(dead_code)]
843-
pub(crate) fn skipped_region(
844-
file_id: u32,
845-
start_line: u32,
846-
start_col: u32,
847-
end_line: u32,
848-
end_col: u32,
849-
) -> Self {
850-
Self {
851-
counter: coverage_map::Counter::zero(),
852-
false_counter: coverage_map::Counter::zero(),
853-
file_id,
854-
expanded_file_id: 0,
855-
start_line,
856-
start_col,
857-
end_line,
858-
end_col,
859-
kind: RegionKind::SkippedRegion,
860-
}
861-
}
862-
863-
// This function might be used in the future; the LLVM API is still evolving, as is coverage
864-
// support.
865-
#[allow(dead_code)]
866-
pub(crate) fn gap_region(
867-
counter: coverage_map::Counter,
868-
file_id: u32,
869-
start_line: u32,
870-
start_col: u32,
871-
end_line: u32,
872-
end_col: u32,
873-
) -> Self {
874-
Self {
875-
counter,
876-
false_counter: coverage_map::Counter::zero(),
877-
file_id,
878-
expanded_file_id: 0,
879-
start_line,
880-
start_col,
881-
end_line,
882-
end_col: (1_u32 << 31) | end_col,
883-
kind: RegionKind::GapRegion,
884-
}
885-
}
886-
}
887-
}
888-
889689
pub mod debuginfo {
890690
use super::{InvariantOpaque, Metadata};
891691
use bitflags::bitflags;
@@ -1911,9 +1711,9 @@ extern "C" {
19111711
pub fn LLVMRustCoverageWriteMappingToBuffer(
19121712
VirtualFileMappingIDs: *const c_uint,
19131713
NumVirtualFileMappingIDs: c_uint,
1914-
Expressions: *const coverage_map::CounterExpression,
1714+
Expressions: *const crate::coverageinfo::ffi::CounterExpression,
19151715
NumExpressions: c_uint,
1916-
MappingRegions: *const coverageinfo::CounterMappingRegion,
1716+
MappingRegions: *const crate::coverageinfo::ffi::CounterMappingRegion,
19171717
NumMappingRegions: c_uint,
19181718
BufferOut: &RustString,
19191719
);

0 commit comments

Comments
 (0)
Please sign in to comment.