]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/coverageinfo/mod.rs
Fixed coverage map issues; better aligned with LLVM APIs
[rust.git] / src / librustc_codegen_llvm / coverageinfo / mod.rs
1 use crate::llvm;
2
3 use crate::builder::Builder;
4 use crate::common::CodegenCx;
5
6 use libc::c_uint;
7 use log::debug;
8 use rustc_codegen_ssa::coverageinfo::map::*;
9 use rustc_codegen_ssa::traits::{
10     BaseTypeMethods, CoverageInfoBuilderMethods, CoverageInfoMethods, StaticMethods,
11 };
12 use rustc_data_structures::fx::FxHashMap;
13 use rustc_llvm::RustString;
14 use rustc_middle::ty::Instance;
15
16 use std::cell::RefCell;
17 use std::ffi::CString;
18
19 pub mod mapgen;
20
21 const COVMAP_VAR_ALIGN_BYTES: usize = 8;
22
23 /// A context object for maintaining all state needed by the coverageinfo module.
24 pub struct CrateCoverageContext<'tcx> {
25     // Coverage region data for each instrumented function identified by DefId.
26     pub(crate) function_coverage_map: RefCell<FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>>>,
27 }
28
29 impl<'tcx> CrateCoverageContext<'tcx> {
30     pub fn new() -> Self {
31         Self { function_coverage_map: Default::default() }
32     }
33
34     pub fn take_function_coverage_map(&self) -> FxHashMap<Instance<'tcx>, FunctionCoverage<'tcx>> {
35         self.function_coverage_map.replace(FxHashMap::default())
36     }
37 }
38
39 impl CoverageInfoMethods for CodegenCx<'ll, 'tcx> {
40     fn coverageinfo_finalize(&self) {
41         mapgen::finalize(self)
42     }
43 }
44
45 impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
46     fn add_counter_region(
47         &mut self,
48         instance: Instance<'tcx>,
49         function_source_hash: u64,
50         id: u32,
51         start_byte_pos: u32,
52         end_byte_pos: u32,
53     ) {
54         debug!(
55             "adding counter to coverage_regions: instance={:?}, function_source_hash={}, id={}, \
56              byte range {}..{}",
57             instance, function_source_hash, id, start_byte_pos, end_byte_pos,
58         );
59         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
60         coverage_regions
61             .entry(instance)
62             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
63             .add_counter(function_source_hash, id, start_byte_pos, end_byte_pos);
64     }
65
66     fn add_counter_expression_region(
67         &mut self,
68         instance: Instance<'tcx>,
69         id_descending_from_max: u32,
70         lhs: u32,
71         op: ExprKind,
72         rhs: u32,
73         start_byte_pos: u32,
74         end_byte_pos: u32,
75     ) {
76         debug!(
77             "adding counter expression to coverage_regions: instance={:?}, id={}, {} {:?} {}, \
78              byte range {}..{}",
79             instance, id_descending_from_max, lhs, op, rhs, start_byte_pos, end_byte_pos,
80         );
81         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
82         coverage_regions
83             .entry(instance)
84             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
85             .add_counter_expression(
86                 id_descending_from_max,
87                 lhs,
88                 op,
89                 rhs,
90                 start_byte_pos,
91                 end_byte_pos,
92             );
93     }
94
95     fn add_unreachable_region(
96         &mut self,
97         instance: Instance<'tcx>,
98         start_byte_pos: u32,
99         end_byte_pos: u32,
100     ) {
101         debug!(
102             "adding unreachable code to coverage_regions: instance={:?}, byte range {}..{}",
103             instance, start_byte_pos, end_byte_pos,
104         );
105         let mut coverage_regions = self.coverage_context().function_coverage_map.borrow_mut();
106         coverage_regions
107             .entry(instance)
108             .or_insert_with(|| FunctionCoverage::new(self.tcx, instance))
109             .add_unreachable_region(start_byte_pos, end_byte_pos);
110     }
111 }
112
113 /// Aligns to C++ struct llvm::coverage::Counter::CounterKind.
114 /// The order of discrimiators is important.
115 #[derive(Copy, Clone, Debug)]
116 #[repr(C)]
117 enum RegionKind {
118     /// A CodeRegion associates some code with a counter
119     CodeRegion,
120
121     /// An ExpansionRegion represents a file expansion region that associates
122     /// a source range with the expansion of a virtual source file, such as
123     /// for a macro instantiation or #include file.
124     ExpansionRegion,
125
126     /// A SkippedRegion represents a source range with code that was skipped
127     /// by a preprocessor or similar means.
128     SkippedRegion,
129
130     /// A GapRegion is like a CodeRegion, but its count is only set as the
131     /// line execution count when its the only region in the line.
132     GapRegion,
133 }
134
135 /// This struct provides LLVM's representation of a "CoverageMappingRegion", encoded into the
136 /// coverage map in accordance with LLVM's "Coverage Mapping Format". The struct composes fields
137 /// representing the `Counter` type and value(s) (injected counter ID, or expression type and
138 /// operands), the source file (an indirect index into a "filenames array", encoded separately),
139 /// and source location (start and end positions of the represented code region).
140 ///
141 /// Aligns to C++ struct llvm::coverage::CounterMappingRegion.
142 /// The order of fields is important.
143 #[derive(Copy, Clone, Debug)]
144 #[repr(C)]
145 pub struct CounterMappingRegion {
146     /// The counter type and type-dependent counter data, if any.
147     counter: Counter,
148
149     /// An indirect reference to the source filename. In the LLVM Coverage Mapping Format, the
150     /// file_id is an index into a function-specific `virtual_file_mapping` array of indexes that,
151     /// in turn, are used to look up the filename for this region.
152     file_id: u32,
153
154     /// If the `RegionKind` is an `ExpansionRegion`, the `expanded_file_id` can be used to find the
155     /// mapping regions created as a result of macro expansion, by checking if their file id matches
156     /// the expanded file id.
157     expanded_file_id: u32,
158
159     /// 1-based starting line of the mapping region.
160     start_line: u32,
161
162     /// 1-based starting column of the mapping region.
163     start_col: u32,
164
165     /// 1-based ending line of the mapping region.
166     end_line: u32,
167
168     /// 1-based ending column of the mapping region. If the high bit is set, the current mapping
169     /// region is a gap area.
170     end_col: u32,
171
172     kind: RegionKind,
173 }
174
175 impl CounterMappingRegion {
176     pub fn code_region(
177         counter: Counter,
178         file_id: u32,
179         start_line: u32,
180         start_col: u32,
181         end_line: u32,
182         end_col: u32,
183     ) -> Self {
184         Self {
185             counter,
186             file_id,
187             expanded_file_id: 0,
188             start_line,
189             start_col,
190             end_line,
191             end_col,
192             kind: RegionKind::CodeRegion,
193         }
194     }
195
196     pub fn expansion_region(
197         file_id: u32,
198         expanded_file_id: u32,
199         start_line: u32,
200         start_col: u32,
201         end_line: u32,
202         end_col: u32,
203     ) -> Self {
204         Self {
205             counter: Counter::zero(),
206             file_id,
207             expanded_file_id,
208             start_line,
209             start_col,
210             end_line,
211             end_col,
212             kind: RegionKind::ExpansionRegion,
213         }
214     }
215
216     pub fn skipped_region(
217         file_id: u32,
218         start_line: u32,
219         start_col: u32,
220         end_line: u32,
221         end_col: u32,
222     ) -> Self {
223         Self {
224             counter: Counter::zero(),
225             file_id,
226             expanded_file_id: 0,
227             start_line,
228             start_col,
229             end_line,
230             end_col,
231             kind: RegionKind::SkippedRegion,
232         }
233     }
234
235     pub fn gap_region(
236         counter: Counter,
237         file_id: u32,
238         start_line: u32,
239         start_col: u32,
240         end_line: u32,
241         end_col: u32,
242     ) -> Self {
243         Self {
244             counter,
245             file_id,
246             expanded_file_id: 0,
247             start_line,
248             start_col,
249             end_line,
250             end_col: ((1 as u32) << 31) | end_col,
251             kind: RegionKind::GapRegion,
252         }
253     }
254 }
255
256 pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
257     let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
258     unsafe {
259         llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
260             c_str_vec.as_ptr(),
261             c_str_vec.len(),
262             buffer,
263         );
264     }
265 }
266
267 pub(crate) fn write_mapping_to_buffer(
268     virtual_file_mapping: Vec<u32>,
269     expressions: Vec<CounterExpression>,
270     mut mapping_regions: Vec<CounterMappingRegion>,
271     buffer: &RustString,
272 ) {
273     unsafe {
274         llvm::LLVMRustCoverageWriteMappingToBuffer(
275             virtual_file_mapping.as_ptr(),
276             virtual_file_mapping.len() as c_uint,
277             expressions.as_ptr(),
278             expressions.len() as c_uint,
279             mapping_regions.as_mut_ptr(),
280             mapping_regions.len() as c_uint,
281             buffer,
282         );
283     }
284 }
285
286 pub(crate) fn compute_hash(name: &str) -> u64 {
287     let name = CString::new(name).expect("null error converting hashable name to C string");
288     unsafe { llvm::LLVMRustCoverageComputeHash(name.as_ptr()) }
289 }
290
291 pub(crate) fn mapping_version() -> u32 {
292     unsafe { llvm::LLVMRustCoverageMappingVersion() }
293 }
294
295 pub(crate) fn save_map_to_mod<'ll, 'tcx>(
296     cx: &CodegenCx<'ll, 'tcx>,
297     cov_data_val: &'ll llvm::Value,
298 ) {
299     let covmap_var_name = llvm::build_string(|s| unsafe {
300         llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
301     })
302     .expect("Rust Coverage Mapping var name failed UTF-8 conversion");
303     debug!("covmap var name: {:?}", covmap_var_name);
304
305     let covmap_section_name = llvm::build_string(|s| unsafe {
306         llvm::LLVMRustCoverageWriteSectionNameToString(cx.llmod, s);
307     })
308     .expect("Rust Coverage section name failed UTF-8 conversion");
309     debug!("covmap section name: {:?}", covmap_section_name);
310
311     let llglobal = llvm::add_global(cx.llmod, cx.val_ty(cov_data_val), &covmap_var_name);
312     llvm::set_initializer(llglobal, cov_data_val);
313     llvm::set_global_constant(llglobal, true);
314     llvm::set_linkage(llglobal, llvm::Linkage::InternalLinkage);
315     llvm::set_section(llglobal, &covmap_section_name);
316     llvm::set_alignment(llglobal, COVMAP_VAR_ALIGN_BYTES);
317     cx.add_used_global(llglobal);
318 }