@@ -6,7 +6,7 @@ use crate::llvm;
6
6
7
7
use itertools:: Itertools as _;
8
8
use rustc_codegen_ssa:: traits:: { BaseTypeMethods , ConstMethods } ;
9
- use rustc_data_structures:: fx:: FxIndexSet ;
9
+ use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
10
10
use rustc_hir:: def:: DefKind ;
11
11
use rustc_hir:: def_id:: DefId ;
12
12
use rustc_index:: IndexVec ;
@@ -146,6 +146,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
146
146
coverageinfo:: save_cov_data_to_mod ( cx, cov_data_val) ;
147
147
}
148
148
149
+ /// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
149
150
struct GlobalFileTable {
150
151
/// This "raw" table doesn't include the working dir, so a filename's
151
152
/// global ID is its index in this set **plus one**.
@@ -206,6 +207,30 @@ impl GlobalFileTable {
206
207
}
207
208
}
208
209
210
+ rustc_index:: newtype_index! {
211
+ // Tell the newtype macro to not generate `Encode`/`Decode` impls.
212
+ #[ custom_encodable]
213
+ #[ max = 0xFFFF_FFFF ]
214
+ struct LocalFileId { }
215
+ }
216
+
217
+ /// Holds a mapping from "local" (per-function) file IDs to "global" (per-CGU)
218
+ /// file IDs.
219
+ #[ derive( Default ) ]
220
+ struct VirtualFileMapping {
221
+ local_to_global : IndexVec < LocalFileId , u32 > ,
222
+ }
223
+
224
+ impl VirtualFileMapping {
225
+ fn push_global_id ( & mut self , global_file_id : u32 ) -> LocalFileId {
226
+ self . local_to_global . push ( global_file_id)
227
+ }
228
+
229
+ fn into_vec ( self ) -> Vec < u32 > {
230
+ self . local_to_global . raw
231
+ }
232
+ }
233
+
209
234
/// Using the expressions and counter regions collected for a single function,
210
235
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
211
236
/// entry. The payload is returned as a vector of bytes.
@@ -222,7 +247,7 @@ fn encode_mappings_for_function(
222
247
223
248
let expressions = function_coverage. counter_expressions ( ) . collect :: < Vec < _ > > ( ) ;
224
249
225
- let mut virtual_file_mapping = IndexVec :: < u32 , u32 > :: new ( ) ;
250
+ let mut virtual_file_mapping = VirtualFileMapping :: default ( ) ;
226
251
let mut mapping_regions = Vec :: with_capacity ( counter_regions. len ( ) ) ;
227
252
228
253
// Sort and group the list of (counter, region) mapping pairs by filename.
@@ -238,8 +263,8 @@ fn encode_mappings_for_function(
238
263
let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
239
264
240
265
// Associate that global file ID with a local file ID for this function.
241
- let local_file_id: u32 = virtual_file_mapping. push ( global_file_id) ;
242
- debug ! ( " file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'" ) ;
266
+ let local_file_id = virtual_file_mapping. push_global_id ( global_file_id) ;
267
+ debug ! ( " file id: {local_file_id:? } => global {global_file_id} = '{file_name:?}'" ) ;
243
268
244
269
// For each counter/region pair in this function+file, convert it to a
245
270
// form suitable for FFI.
@@ -249,7 +274,7 @@ fn encode_mappings_for_function(
249
274
debug ! ( "Adding counter {counter:?} to map for {region:?}" ) ;
250
275
mapping_regions. push ( CounterMappingRegion :: code_region (
251
276
counter,
252
- local_file_id,
277
+ local_file_id. as_u32 ( ) ,
253
278
start_line,
254
279
start_col,
255
280
end_line,
@@ -261,7 +286,7 @@ fn encode_mappings_for_function(
261
286
// Encode the function's coverage mappings into a buffer.
262
287
llvm:: build_byte_buffer ( |buffer| {
263
288
coverageinfo:: write_mapping_to_buffer (
264
- virtual_file_mapping. raw ,
289
+ virtual_file_mapping. into_vec ( ) ,
265
290
expressions,
266
291
mapping_regions,
267
292
buffer,
0 commit comments