1
- use std:: cell:: RefCell ;
2
- use std:: ffi:: CString ;
1
+ use std:: cell:: { OnceCell , RefCell } ;
2
+ use std:: ffi:: { CStr , CString } ;
3
3
4
4
use libc:: c_uint;
5
5
use rustc_codegen_ssa:: traits:: {
@@ -29,6 +29,8 @@ pub(crate) struct CrateCoverageContext<'ll, 'tcx> {
29
29
RefCell < FxIndexMap < Instance < ' tcx > , FunctionCoverageCollector < ' tcx > > > ,
30
30
pub ( crate ) pgo_func_name_var_map : RefCell < FxHashMap < Instance < ' tcx > , & ' ll llvm:: Value > > ,
31
31
pub ( crate ) mcdc_condition_bitmap_map : RefCell < FxHashMap < Instance < ' tcx > , Vec < & ' ll llvm:: Value > > > ,
32
+
33
+ covfun_section_name : OnceCell < CString > ,
32
34
}
33
35
34
36
impl < ' ll , ' tcx > CrateCoverageContext < ' ll , ' tcx > {
@@ -37,6 +39,7 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
37
39
function_coverage_map : Default :: default ( ) ,
38
40
pgo_func_name_var_map : Default :: default ( ) ,
39
41
mcdc_condition_bitmap_map : Default :: default ( ) ,
42
+ covfun_section_name : Default :: default ( ) ,
40
43
}
41
44
}
42
45
@@ -63,13 +66,28 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
63
66
}
64
67
}
65
68
66
- // These methods used to be part of trait `CoverageInfoMethods`, which no longer
67
- // exists after most coverage code was moved out of SSA.
68
69
impl < ' ll , ' tcx > CodegenCx < ' ll , ' tcx > {
69
70
pub ( crate ) fn coverageinfo_finalize ( & self ) {
70
71
mapgen:: finalize ( self )
71
72
}
72
73
74
+ /// Returns the section name to use when embedding per-function coverage information
75
+ /// in the object file, according to the target's object file format. LLVM's coverage
76
+ /// tools use information from this section when producing coverage reports.
77
+ ///
78
+ /// Typical values are:
79
+ /// - `__llvm_covfun` on Linux
80
+ /// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
81
+ /// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
82
+ fn covfun_section_name ( & self ) -> & CStr {
83
+ self . coverage_cx ( ) . covfun_section_name . get_or_init ( || {
84
+ CString :: new ( llvm:: build_byte_buffer ( |s| unsafe {
85
+ llvm:: LLVMRustCoverageWriteFuncSectionNameToString ( self . llmod , s) ;
86
+ } ) )
87
+ . expect ( "covfun section name should not contain NUL" )
88
+ } )
89
+ }
90
+
73
91
/// For LLVM codegen, returns a function-specific `Value` for a global
74
92
/// string, to hold the function name passed to LLVM intrinsic
75
93
/// `instrprof.increment()`. The `Value` is only created once per instance.
@@ -278,21 +296,3 @@ pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
278
296
pub ( crate ) fn mapping_version ( ) -> u32 {
279
297
unsafe { llvm:: LLVMRustCoverageMappingVersion ( ) }
280
298
}
281
-
282
- /// Returns the section name string to pass through to the linker when embedding
283
- /// per-function coverage information in the object file, according to the target
284
- /// platform's object file format.
285
- ///
286
- /// LLVM's coverage tools read coverage mapping details from this section when
287
- /// producing coverage reports.
288
- ///
289
- /// Typical values are:
290
- /// - `__llvm_covfun` on Linux
291
- /// - `__LLVM_COV,__llvm_covfun` on macOS (includes `__LLVM_COV,` segment prefix)
292
- /// - `.lcovfun$M` on Windows (includes `$M` sorting suffix)
293
- pub ( crate ) fn covfun_section_name ( cx : & CodegenCx < ' _ , ' _ > ) -> CString {
294
- CString :: new ( llvm:: build_byte_buffer ( |s| unsafe {
295
- llvm:: LLVMRustCoverageWriteFuncSectionNameToString ( cx. llmod , s) ;
296
- } ) )
297
- . expect ( "covfun section name should not contain NUL" )
298
- }
0 commit comments