@@ -165,32 +165,34 @@ fn encode_mappings_for_function(
165
165
166
166
let mut virtual_file_mapping = Vec :: new ( ) ;
167
167
let mut mapping_regions = Vec :: with_capacity ( counter_regions. len ( ) ) ;
168
- let mut current_file_name = None ;
169
- let mut current_file_id = 0 ;
170
-
171
- // Convert the list of (Counter, CodeRegion) pairs to an array of `CounterMappingRegion`, sorted
172
- // by filename and position. Capture any new files to compute the `CounterMappingRegion`s
173
- // `file_id` (indexing files referenced by the current function), and construct the
174
- // function-specific `virtual_file_mapping` from `file_id` to its index in the module's
175
- // `filenames` array.
168
+
169
+ // Sort the list of (counter, region) mapping pairs by region, so that they
170
+ // can be grouped by filename. Prepare file IDs for each filename, and
171
+ // prepare the mapping data so that we can pass it through FFI to LLVM.
176
172
counter_regions. sort_by_key ( |( _counter, region) | * region) ;
177
- for ( counter, region) in counter_regions {
178
- let CodeRegion { file_name, start_line, start_col, end_line, end_col } = * region;
179
- let same_file = current_file_name. is_some_and ( |p| p == file_name) ;
180
- if !same_file {
181
- if current_file_name. is_some ( ) {
182
- current_file_id += 1 ;
183
- }
184
- current_file_name = Some ( file_name) ;
185
- debug ! ( " file_id: {} = '{:?}'" , current_file_id, file_name) ;
186
- let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
187
- virtual_file_mapping. push ( global_file_id) ;
188
- }
189
- {
190
- debug ! ( "Adding counter {:?} to map for {:?}" , counter, region) ;
173
+ for counter_regions_for_file in
174
+ counter_regions. group_by ( |( _, a) , ( _, b) | a. file_name == b. file_name )
175
+ {
176
+ // Look up (or allocate) the global file ID for this filename.
177
+ let file_name = counter_regions_for_file[ 0 ] . 1 . file_name ;
178
+ let global_file_id = global_file_table. global_file_id_for_file_name ( file_name) ;
179
+
180
+ // Associate that global file ID with a local file ID for this function.
181
+ let Ok ( local_file_id) = virtual_file_mapping. len ( ) . try_into ( ) else {
182
+ bug ! ( "overflow in local file ID" ) ;
183
+ } ;
184
+ virtual_file_mapping. push ( global_file_id) ;
185
+ debug ! ( " file id: local {local_file_id} => global {global_file_id} = '{file_name:?}'" ) ;
186
+
187
+ // For each counter/region pair in this function+file, convert it to a
188
+ // form suitable for FFI.
189
+ for & ( counter, region) in counter_regions_for_file {
190
+ let CodeRegion { file_name : _, start_line, start_col, end_line, end_col } = * region;
191
+
192
+ debug ! ( "Adding counter {counter:?} to map for {region:?}" ) ;
191
193
mapping_regions. push ( CounterMappingRegion :: code_region (
192
194
counter,
193
- current_file_id ,
195
+ local_file_id ,
194
196
start_line,
195
197
start_col,
196
198
end_line,
0 commit comments