1- use std:: cell:: RefCell ;
2- use std:: ffi:: CString ;
1+ use std:: cell:: { OnceCell , RefCell } ;
2+ use std:: ffi:: { CStr , CString } ;
33
44use libc:: c_uint;
55use rustc_codegen_ssa:: traits:: {
@@ -29,6 +29,8 @@ pub(crate) struct CrateCoverageContext<'ll, 'tcx> {
2929 RefCell < FxIndexMap < Instance < ' tcx > , FunctionCoverageCollector < ' tcx > > > ,
3030 pub ( crate ) pgo_func_name_var_map : RefCell < FxHashMap < Instance < ' tcx > , & ' ll llvm:: Value > > ,
3131 pub ( crate ) mcdc_condition_bitmap_map : RefCell < FxHashMap < Instance < ' tcx > , Vec < & ' ll llvm:: Value > > > ,
32+
33+ covfun_section_name : OnceCell < CString > ,
3234}
3335
3436impl < ' ll , ' tcx > CrateCoverageContext < ' ll , ' tcx > {
@@ -37,6 +39,7 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
3739 function_coverage_map : Default :: default ( ) ,
3840 pgo_func_name_var_map : Default :: default ( ) ,
3941 mcdc_condition_bitmap_map : Default :: default ( ) ,
42+ covfun_section_name : Default :: default ( ) ,
4043 }
4144 }
4245
@@ -63,13 +66,28 @@ impl<'ll, 'tcx> CrateCoverageContext<'ll, 'tcx> {
6366 }
6467}
6568
66- // These methods used to be part of trait `CoverageInfoMethods`, which no longer
67- // exists after most coverage code was moved out of SSA.
6869impl < ' ll , ' tcx > CodegenCx < ' ll , ' tcx > {
6970 pub ( crate ) fn coverageinfo_finalize ( & self ) {
7071 mapgen:: finalize ( self )
7172 }
7273
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+
7391 /// For LLVM codegen, returns a function-specific `Value` for a global
7492 /// string, to hold the function name passed to LLVM intrinsic
7593 /// `instrprof.increment()`. The `Value` is only created once per instance.
@@ -278,21 +296,3 @@ pub(crate) fn hash_bytes(bytes: &[u8]) -> u64 {
278296pub ( crate ) fn mapping_version ( ) -> u32 {
279297 unsafe { llvm:: LLVMRustCoverageMappingVersion ( ) }
280298}
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