|
1 | 1 | use crate::common::CodegenCx; |
2 | 2 | use crate::coverageinfo; |
3 | | -use crate::coverageinfo::ffi::CounterMappingRegion; |
| 3 | +use crate::coverageinfo::ffi::{Counter, CounterMappingRegion}; |
4 | 4 | use crate::coverageinfo::map_data::FunctionCoverage; |
5 | 5 | use crate::llvm; |
6 | 6 |
|
@@ -159,49 +159,45 @@ fn encode_mappings_for_function( |
159 | 159 | global_file_table: &mut GlobalFileTable, |
160 | 160 | function_coverage: &FunctionCoverage<'_>, |
161 | 161 | ) -> Vec<u8> { |
162 | | - let (expressions, counter_regions) = function_coverage.get_expressions_and_counter_regions(); |
163 | | - |
164 | | - let mut counter_regions = counter_regions.collect::<Vec<_>>(); |
165 | | - if counter_regions.is_empty() { |
| 162 | + if !function_coverage.has_mappings() { |
166 | 163 | return Vec::new(); |
167 | 164 | } |
168 | 165 |
|
| 166 | + let expressions = function_coverage.counter_expressions(); |
| 167 | + |
169 | 168 | let mut virtual_file_mapping = Vec::new(); |
170 | | - let mut mapping_regions = Vec::with_capacity(counter_regions.len()); |
171 | | - |
172 | | - // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted |
173 | | - // by filename and position. Capture any new files to compute the `CounterMappingRegion`s |
174 | | - // `file_id` (indexing files referenced by the current function), and construct the |
175 | | - // function-specific `virtual_file_mapping` from `file_id` to its index in the module's |
176 | | - // `filenames` array. |
177 | | - counter_regions.sort_by_key(|(_counter, region)| region.file_name); |
178 | | - for (local_file_id, counter_regions_for_file) in |
179 | | - (0u32..).zip(counter_regions.group_by(|(_, a), (_, b)| a.file_name == b.file_name)) |
| 169 | + let mut mapping_regions = Vec::new(); |
| 170 | + |
| 171 | + for (local_file_id, (file_name, mappings_for_file)) in |
| 172 | + (0u32..).zip(function_coverage.all_mappings_by_filename()) |
180 | 173 | { |
| 174 | + assert!(!mappings_for_file.is_empty(), "{mappings_for_file:?}"); |
181 | 175 | // Look up (or allocate) the global file ID for this file name. |
182 | | - let file_name = counter_regions_for_file[0].1.file_name; |
183 | 176 | let global_file_id = global_file_table.global_file_id_for_file_name(file_name); |
184 | 177 |
|
185 | 178 | // Associate that global file ID with a local file ID for this function. |
186 | 179 | assert_eq!(local_file_id as usize, virtual_file_mapping.len()); |
187 | 180 | virtual_file_mapping.push(global_file_id); |
188 | 181 | debug!(" file_id: local {local_file_id} => global {global_file_id} = '{file_name:?}'"); |
189 | 182 |
|
190 | | - // For each counter/region pair in this function+file, convert it |
191 | | - // to a form suitable for FFI. |
192 | | - for &(counter, region) in counter_regions_for_file { |
| 183 | + mapping_regions.extend(mappings_for_file.iter().map(|(mapping_kind, region)| { |
| 184 | + let counter = Counter::from_term(*mapping_kind); |
193 | 185 | let CodeRegion { file_name: _, start_line, start_col, end_line, end_col } = *region; |
194 | 186 |
|
195 | 187 | debug!("Adding counter {counter:?} to map for {region:?}"); |
196 | | - mapping_regions.push(CounterMappingRegion::code_region( |
| 188 | + CounterMappingRegion::code_region( |
197 | 189 | counter, |
198 | 190 | local_file_id, |
199 | 191 | start_line, |
200 | 192 | start_col, |
201 | 193 | end_line, |
202 | 194 | end_col, |
203 | | - )); |
204 | | - } |
| 195 | + ) |
| 196 | + })); |
| 197 | + } |
| 198 | + |
| 199 | + if mapping_regions.is_empty() { |
| 200 | + bug!("no mappings generated for function: {function_coverage:?}"); |
205 | 201 | } |
206 | 202 |
|
207 | 203 | // Encode the function's coverage mappings into a buffer. |
|
0 commit comments