Skip to content

Commit 88159ca

Browse files
committed
coverage: Encapsulate local-to-global file mappings
1 parent e985ae5 commit 88159ca

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+28-5
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ impl GlobalFileTable {
194194
}
195195
}
196196

197+
rustc_index::newtype_index! {
198+
// Tell the newtype macro to not generate `Encode`/`Decode` impls.
199+
#[custom_encodable]
200+
struct LocalFileId {}
201+
}
202+
203+
/// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU)
204+
/// file IDs.
205+
#[derive(Default)]
206+
struct VirtualFileMapping {
207+
local_to_global: IndexVec<LocalFileId, u32>,
208+
}
209+
210+
impl VirtualFileMapping {
211+
fn push_global_id(&mut self, global_file_id: u32) -> LocalFileId {
212+
self.local_to_global.push(global_file_id)
213+
}
214+
215+
fn into_vec(self) -> Vec<u32> {
216+
self.local_to_global.raw
217+
}
218+
}
219+
197220
/// Using the expressions and counter regions collected for a single function,
198221
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
199222
/// entry. The payload is returned as a vector of bytes.
@@ -210,7 +233,7 @@ fn encode_mappings_for_function(
210233

211234
let expressions = function_coverage.counter_expressions().collect::<Vec<_>>();
212235

213-
let mut virtual_file_mapping = IndexVec::<u32, u32>::new();
236+
let mut virtual_file_mapping = VirtualFileMapping::default();
214237
let mut mapping_regions = Vec::with_capacity(counter_regions.len());
215238

216239
// Sort and group the list of (counter, region) mapping pairs by filename.
@@ -226,8 +249,8 @@ fn encode_mappings_for_function(
226249
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
227250

228251
// Associate that global file ID with a local file ID for this function.
229-
let local_file_id: u32 = virtual_file_mapping.push(global_file_id);
230-
debug!(" file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'");
252+
let local_file_id = virtual_file_mapping.push_global_id(global_file_id);
253+
debug!(" file id: {local_file_id:?} => global {global_file_id} = '{file_name:?}'");
231254

232255
// For each counter/region pair in this function+file, convert it to a
233256
// form suitable for FFI.
@@ -237,7 +260,7 @@ fn encode_mappings_for_function(
237260
debug!("Adding counter {counter:?} to map for {region:?}");
238261
mapping_regions.push(CounterMappingRegion::code_region(
239262
counter,
240-
local_file_id,
263+
local_file_id.as_u32(),
241264
start_line,
242265
start_col,
243266
end_line,
@@ -249,7 +272,7 @@ fn encode_mappings_for_function(
249272
// Encode the function's coverage mappings into a buffer.
250273
llvm::build_byte_buffer(|buffer| {
251274
coverageinfo::write_mapping_to_buffer(
252-
virtual_file_mapping.raw,
275+
virtual_file_mapping.into_vec(),
253276
expressions,
254277
mapping_regions,
255278
buffer,

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(hash_raw_entry)]
1313
#![feature(iter_intersperse)]
1414
#![feature(let_chains)]
15+
#![feature(min_specialization)]
1516
#![feature(never_type)]
1617
#![feature(slice_group_by)]
1718
#![feature(impl_trait_in_assoc_type)]

0 commit comments

Comments
 (0)