3 use crate::builder::Builder;
4 use crate::common::CodegenCx;
7 use llvm::coverageinfo::CounterMappingRegion;
9 use rustc_codegen_ssa::coverageinfo::map::{CounterExpression, ExprKind, FunctionCoverage, Region};
10 use rustc_codegen_ssa::traits::{
11 BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, StaticMethods,
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_llvm::RustString;
15 use rustc_middle::ty::Instance;
17 use std::cell::RefCell;
18 use std::ffi::CString;
22 const COVMAP_VAR_ALIGN_BYTES: usize = 8;
24 /// A context object for maintaining all state needed by the coverageinfo module.
25 pub struct CrateCoverageContext<'tcx> {
26 // Coverage region data for each instrumented function identified by DefId.
27 pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>>,
30 impl<'tcx> CrateCoverageContext<'tcx> {
31 pub fn new() -> Self {
32 Self { function_coverage_map: Default::default() }
35 pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
36 self.function_coverage_map.replace(FxHashMap::default())
40 impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
41 fn coverageinfo_finalize(&self) {
42 mapgen::finalize(self)
46 impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
47 fn add_counter_region(
49 instance: Instance<'tcx>,
50 function_source_hash: u64,
55 "adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={}, \
57 instance, function_source_hash, id, region,
59 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
62 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
63 .add_counter(function_source_hash, id, region);
66 fn add_counter_expression_region(
68 instance: Instance<'tcx>,
69 id_descending_from_max: u32,
76 "adding counter expression to coverage_regions: instance={:?}, id={}, {} {:?} {}, \
78 instance, id_descending_from_max, lhs, op, rhs, region,
80 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
83 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
84 .add_counter_expression(id_descending_from_max, lhs, op, rhs, region);
87 fn add_unreachable_region(&mut self, instance: Instance<'tcx>, region: Region<'tcx>) {
89 "adding unreachable code to coverage_regions: instance={:?}, at {:?}",
92 let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
95 .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
96 .add_unreachable_region(region);
100 pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
101 let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
103 llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
111 pub(crate) fn write_mapping_to_buffer(
112 virtual_file_mapping: Vec<u32>,
113 expressions: Vec<CounterExpression>,
114 mut mapping_regions: Vec<CounterMappingRegion>,
118 llvm::LLVMRustCoverageWriteMappingToBuffer(
119 virtual_file_mapping.as_ptr(),
120 virtual_file_mapping.len() as c_uint,
121 expressions.as_ptr(),
122 expressions.len() as c_uint,
123 mapping_regions.as_mut_ptr(),
124 mapping_regions.len() as c_uint,
130 pub(crate) fn compute_hash(name: &str) -> u64 {
131 let name = CString::new(name).expect("null error converting hashable name to C string");
132 unsafe { llvm::LLVMRustCoverageComputeHash(name.as_ptr()) }
135 pub(crate) fn mapping_version() -> u32 {
136 unsafe { llvm::LLVMRustCoverageMappingVersion() }
139 pub(crate) fn save_map_to_mod<'ll, 'tcx>(
140 cx: &CodegenCx<'ll, 'tcx>,
141 cov_data_val: &'ll llvm::Value,
143 let covmap_var_name = llvm::build_string(|s| unsafe {
144 llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
146 .expect("Rust Coverage Mapping var name failed UTF-8 conversion");
147 debug!("covmap var name: {:?}", covmap_var_name);
149 let covmap_section_name = llvm::build_string(|s| unsafe {
150 llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
152 .expect("Rust Coverage section name failed UTF-8 conversion");
153 debug!("covmap section name: {:?}", covmap_section_name);
155 let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
156 llvm::set_initializer(llglobal, cov_data_val);
157 llvm::set_global_constant(llglobal, true);
158 llvm::set_linkage(llglobal, llvm::Linkage::InternalLinkage);
159 llvm::set_section(llglobal, &covmap_section_name);
160 llvm::set_alignment(llglobal, COVMAP_VAR_ALIGN_BYTES);
161 cx.add_used_global(llglobal);