Skip to content

Commit 4f73cd1

Browse files
committed
coverage: Move a buffer creation into the function that needs it
Instead of writing coverage mappings into a supplied `&RustString`, this function can just create the buffer itself and return `Vec<u8>` directly.
1 parent 4a5c14e commit 4f73cd1

File tree

1 file changed

+19
-24
lines changed
  • compiler/rustc_codegen_llvm/src/coverageinfo

1 file changed

+19
-24
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_codegen_ssa::traits::ConstMethods;
77
use rustc_data_structures::fx::FxIndexSet;
88
use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::DefId;
10-
use rustc_llvm::RustString;
1110
use rustc_middle::bug;
1211
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1312
use rustc_middle::mir::coverage::CodeRegion;
@@ -69,14 +68,8 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
6968
let (expressions, counter_regions) =
7069
function_coverage.get_expressions_and_counter_regions();
7170

72-
let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
73-
write_coverage_mapping(
74-
&mut global_file_table,
75-
expressions,
76-
counter_regions,
77-
coverage_mapping_buffer,
78-
);
79-
});
71+
let coverage_mapping_buffer =
72+
encode_mappings_for_function(&mut global_file_table, expressions, counter_regions);
8073

8174
if coverage_mapping_buffer.is_empty() {
8275
if function_coverage.is_used() {
@@ -157,19 +150,19 @@ impl GlobalFileTable {
157150
}
158151
}
159152

160-
/// Using the `expressions` and `counter_regions` collected for the current function, generate
161-
/// the `mapping_regions` and `virtual_file_mapping`, and capture any new filenames. Then use
162-
/// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
163-
/// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
164-
fn write_coverage_mapping<'a>(
153+
/// Using the expressions and counter regions collected for a single function,
154+
/// generate the variable-sized payload of its corresponding `__llvm_covfun`
155+
/// entry as a `Vec<u8>`.
156+
///
157+
/// Newly-encountered filenames will be added to the global file table.
158+
fn encode_mappings_for_function<'a>(
165159
global_file_table: &mut GlobalFileTable,
166160
expressions: Vec<CounterExpression>,
167161
counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
168-
coverage_mapping_buffer: &RustString,
169-
) {
162+
) -> Vec<u8> {
170163
let mut counter_regions = counter_regions.collect::<Vec<_>>();
171164
if counter_regions.is_empty() {
172-
return;
165+
return Vec::new();
173166
}
174167

175168
let mut virtual_file_mapping = Vec::new();
@@ -210,13 +203,15 @@ fn write_coverage_mapping<'a>(
210203
}
211204
}
212205

213-
// Encode and append the current function's coverage mapping data
214-
coverageinfo::write_mapping_to_buffer(
215-
virtual_file_mapping,
216-
expressions,
217-
mapping_regions,
218-
coverage_mapping_buffer,
219-
);
206+
// Encode the function's coverage mappings into a buffer.
207+
llvm::build_byte_buffer(|buffer| {
208+
coverageinfo::write_mapping_to_buffer(
209+
virtual_file_mapping,
210+
expressions,
211+
mapping_regions,
212+
buffer,
213+
);
214+
})
220215
}
221216

222217
/// Construct coverage map header and the array of function records, and combine them into the

0 commit comments

Comments
 (0)