Skip to content

Commit 5b2e2b2

Browse files
committed
Moved structs/enums with repr(C) to LLVM types into ffi.rs crates
Some were in librustc_codegen_llvm, but others are not tied to LLVM, so I put them in a new crate: librustc_codegen_ssa/coverageinfo/ffi.rs
1 parent b58afc0 commit 5b2e2b2

File tree

6 files changed

+227
-214
lines changed

6 files changed

+227
-214
lines changed

src/librustc_codegen_llvm/coverageinfo/mapgen.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ use crate::common::CodegenCx;
22
use crate::coverageinfo;
33
use crate::llvm;
44

5+
use llvm::coverageinfo::CounterMappingRegion;
56
use log::debug;
6-
use rustc_codegen_ssa::coverageinfo::map::*;
7+
use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression, Region};
78
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
89
use rustc_data_structures::fx::FxHashMap;
910
use rustc_llvm::RustString;
@@ -132,7 +133,7 @@ impl CoverageMapGenerator {
132133
};
133134
virtual_file_mapping.push(filenames_index);
134135
}
135-
mapping_regions.push(coverageinfo::CounterMappingRegion::code_region(
136+
mapping_regions.push(CounterMappingRegion::code_region(
136137
counter,
137138
current_file_id,
138139
start_line,

src/librustc_codegen_llvm/coverageinfo/mod.rs

+2-145
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ use crate::builder::Builder;
44
use crate::common::CodegenCx;
55

66
use libc::c_uint;
7+
use llvm::coverageinfo::CounterMappingRegion;
78
use log::debug;
8-
use rustc_codegen_ssa::coverageinfo::map::*;
9+
use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, ExprKind, FunctionCoverage};
910
use rustc_codegen_ssa::traits::{
1011
BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, StaticMethods,
1112
};
@@ -110,150 +111,6 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
110111
}
111112
}
112113

113-
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L205-L221)
114-
#[derive(Copy, Clone, Debug)]
115-
#[repr(C)]
116-
enum RegionKind {
117-
/// A CodeRegion associates some code with a counter
118-
CodeRegion = 0,
119-
120-
/// An ExpansionRegion represents a file expansion region that associates
121-
/// a source range with the expansion of a virtual source file, such as
122-
/// for a macro instantiation or #include file.
123-
ExpansionRegion = 1,
124-
125-
/// A SkippedRegion represents a source range with code that was skipped
126-
/// by a preprocessor or similar means.
127-
SkippedRegion = 2,
128-
129-
/// A GapRegion is like a CodeRegion, but its count is only set as the
130-
/// line execution count when its the only region in the line.
131-
GapRegion = 3,
132-
}
133-
134-
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
135-
/// coverage map, in accordance with the
136-
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
137-
/// The struct composes fields representing the `Counter` type and value(s) (injected counter ID,
138-
/// or expression type and operands), the source file (an indirect index into a "filenames array",
139-
/// encoded separately), and source location (start and end positions of the represented code
140-
/// region).
141-
///
142-
/// Aligns with [llvm::coverage::CounterMappingRegion](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L223-L226)
143-
/// Important: The Rust struct layout (order and types of fields) must match its C++ counterpart.
144-
#[derive(Copy, Clone, Debug)]
145-
#[repr(C)]
146-
pub struct CounterMappingRegion {
147-
/// The counter type and type-dependent counter data, if any.
148-
counter: Counter,
149-
150-
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
151-
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes that,
152-
/// in turn, are used to look up the filename for this region.
153-
file_id: u32,
154-
155-
/// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find the
156-
/// mapping regions created as a result of macro expansion, by checking if their file id matches
157-
/// the expanded file id.
158-
expanded_file_id: u32,
159-
160-
/// 1-based starting line of the mapping region.
161-
start_line: u32,
162-
163-
/// 1-based starting column of the mapping region.
164-
start_col: u32,
165-
166-
/// 1-based ending line of the mapping region.
167-
end_line: u32,
168-
169-
/// 1-based ending column of the mapping region. If the high bit is set, the current mapping
170-
/// region is a gap area.
171-
end_col: u32,
172-
173-
kind: RegionKind,
174-
}
175-
176-
impl CounterMappingRegion {
177-
pub fn code_region(
178-
counter: Counter,
179-
file_id: u32,
180-
start_line: u32,
181-
start_col: u32,
182-
end_line: u32,
183-
end_col: u32,
184-
) -> Self {
185-
Self {
186-
counter,
187-
file_id,
188-
expanded_file_id: 0,
189-
start_line,
190-
start_col,
191-
end_line,
192-
end_col,
193-
kind: RegionKind::CodeRegion,
194-
}
195-
}
196-
197-
pub fn expansion_region(
198-
file_id: u32,
199-
expanded_file_id: u32,
200-
start_line: u32,
201-
start_col: u32,
202-
end_line: u32,
203-
end_col: u32,
204-
) -> Self {
205-
Self {
206-
counter: Counter::zero(),
207-
file_id,
208-
expanded_file_id,
209-
start_line,
210-
start_col,
211-
end_line,
212-
end_col,
213-
kind: RegionKind::ExpansionRegion,
214-
}
215-
}
216-
217-
pub fn skipped_region(
218-
file_id: u32,
219-
start_line: u32,
220-
start_col: u32,
221-
end_line: u32,
222-
end_col: u32,
223-
) -> Self {
224-
Self {
225-
counter: Counter::zero(),
226-
file_id,
227-
expanded_file_id: 0,
228-
start_line,
229-
start_col,
230-
end_line,
231-
end_col,
232-
kind: RegionKind::SkippedRegion,
233-
}
234-
}
235-
236-
pub fn gap_region(
237-
counter: Counter,
238-
file_id: u32,
239-
start_line: u32,
240-
start_col: u32,
241-
end_line: u32,
242-
end_col: u32,
243-
) -> Self {
244-
Self {
245-
counter,
246-
file_id,
247-
expanded_file_id: 0,
248-
start_line,
249-
start_col,
250-
end_line,
251-
end_col: ((1 as u32) << 31) | end_col,
252-
kind: RegionKind::GapRegion,
253-
}
254-
}
255-
}
256-
257114
pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
258115
let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
259116
unsafe {

src/librustc_codegen_llvm/llvm/ffi.rs

+152-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(non_camel_case_types)]
22
#![allow(non_upper_case_globals)]
33

4-
use crate::coverageinfo::CounterMappingRegion;
4+
use rustc_codegen_ssa::coverageinfo::map as coverage_map;
55

66
use super::debuginfo::{
77
DIArray, DIBasicType, DIBuilder, DICompositeType, DIDerivedType, DIDescriptor, DIEnumerator,
@@ -652,6 +652,155 @@ pub struct Linker<'a>(InvariantOpaque<'a>);
652652
pub type DiagnosticHandler = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
653653
pub type InlineAsmDiagHandler = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
654654

655+
pub mod coverageinfo {
656+
use super::coverage_map;
657+
658+
/// Aligns with [llvm::coverage::CounterMappingRegion::RegionKind](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L205-L221)
659+
#[derive(Copy, Clone, Debug)]
660+
#[repr(C)]
661+
pub enum RegionKind {
662+
/// A CodeRegion associates some code with a counter
663+
CodeRegion = 0,
664+
665+
/// An ExpansionRegion represents a file expansion region that associates
666+
/// a source range with the expansion of a virtual source file, such as
667+
/// for a macro instantiation or #include file.
668+
ExpansionRegion = 1,
669+
670+
/// A SkippedRegion represents a source range with code that was skipped
671+
/// by a preprocessor or similar means.
672+
SkippedRegion = 2,
673+
674+
/// A GapRegion is like a CodeRegion, but its count is only set as the
675+
/// line execution count when its the only region in the line.
676+
GapRegion = 3,
677+
}
678+
679+
/// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
680+
/// coverage map, in accordance with the
681+
/// [LLVM Code Coverage Mapping Format](https://github.com/rust-lang/llvm-project/blob/llvmorg-8.0.0/llvm/docs/CoverageMappingFormat.rst#llvm-code-coverage-mapping-format).
682+
/// The struct composes fields representing the `Counter` type and value(s) (injected counter
683+
/// ID, or expression type and operands), the source file (an indirect index into a "filenames
684+
/// array", encoded separately), and source location (start and end positions of the represented
685+
/// code region).
686+
///
687+
/// Aligns with [llvm::coverage::CounterMappingRegion](https://github.com/rust-lang/llvm-project/blob/rustc/10.0-2020-05-05/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L223-L226)
688+
/// Important: The Rust struct layout (order and types of fields) must match its C++
689+
/// counterpart.
690+
#[derive(Copy, Clone, Debug)]
691+
#[repr(C)]
692+
pub struct CounterMappingRegion {
693+
/// The counter type and type-dependent counter data, if any.
694+
counter: coverage_map::Counter,
695+
696+
/// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
697+
/// file_id is an index into a function-specific `virtual_file_mapping` array of indexes
698+
/// that, in turn, are used to look up the filename for this region.
699+
file_id: u32,
700+
701+
/// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find
702+
/// the mapping regions created as a result of macro expansion, by checking if their file id
703+
/// matches the expanded file id.
704+
expanded_file_id: u32,
705+
706+
/// 1-based starting line of the mapping region.
707+
start_line: u32,
708+
709+
/// 1-based starting column of the mapping region.
710+
start_col: u32,
711+
712+
/// 1-based ending line of the mapping region.
713+
end_line: u32,
714+
715+
/// 1-based ending column of the mapping region. If the high bit is set, the current
716+
/// mapping region is a gap area.
717+
end_col: u32,
718+
719+
kind: RegionKind,
720+
}
721+
722+
impl CounterMappingRegion {
723+
pub fn code_region(
724+
counter: coverage_map::Counter,
725+
file_id: u32,
726+
start_line: u32,
727+
start_col: u32,
728+
end_line: u32,
729+
end_col: u32,
730+
) -> Self {
731+
Self {
732+
counter,
733+
file_id,
734+
expanded_file_id: 0,
735+
start_line,
736+
start_col,
737+
end_line,
738+
end_col,
739+
kind: RegionKind::CodeRegion,
740+
}
741+
}
742+
743+
pub fn expansion_region(
744+
file_id: u32,
745+
expanded_file_id: u32,
746+
start_line: u32,
747+
start_col: u32,
748+
end_line: u32,
749+
end_col: u32,
750+
) -> Self {
751+
Self {
752+
counter: coverage_map::Counter::zero(),
753+
file_id,
754+
expanded_file_id,
755+
start_line,
756+
start_col,
757+
end_line,
758+
end_col,
759+
kind: RegionKind::ExpansionRegion,
760+
}
761+
}
762+
763+
pub fn skipped_region(
764+
file_id: u32,
765+
start_line: u32,
766+
start_col: u32,
767+
end_line: u32,
768+
end_col: u32,
769+
) -> Self {
770+
Self {
771+
counter: coverage_map::Counter::zero(),
772+
file_id,
773+
expanded_file_id: 0,
774+
start_line,
775+
start_col,
776+
end_line,
777+
end_col,
778+
kind: RegionKind::SkippedRegion,
779+
}
780+
}
781+
782+
pub fn gap_region(
783+
counter: coverage_map::Counter,
784+
file_id: u32,
785+
start_line: u32,
786+
start_col: u32,
787+
end_line: u32,
788+
end_col: u32,
789+
) -> Self {
790+
Self {
791+
counter,
792+
file_id,
793+
expanded_file_id: 0,
794+
start_line,
795+
start_col,
796+
end_line,
797+
end_col: ((1 as u32) << 31) | end_col,
798+
kind: RegionKind::GapRegion,
799+
}
800+
}
801+
}
802+
}
803+
655804
pub mod debuginfo {
656805
use super::{InvariantOpaque, Metadata};
657806
use bitflags::bitflags;
@@ -1646,9 +1795,9 @@ extern "C" {
16461795
pub fn LLVMRustCoverageWriteMappingToBuffer(
16471796
VirtualFileMappingIDs: *const c_uint,
16481797
NumVirtualFileMappingIDs: c_uint,
1649-
Expressions: *const rustc_codegen_ssa::coverageinfo::map::CounterExpression,
1798+
Expressions: *const coverage_map::CounterExpression,
16501799
NumExpressions: c_uint,
1651-
MappingRegions: *mut CounterMappingRegion,
1800+
MappingRegions: *mut coverageinfo::CounterMappingRegion,
16521801
NumMappingRegions: c_uint,
16531802
BufferOut: &RustString,
16541803
);

0 commit comments

Comments
 (0)