coverage: Convert CoverageMapGenerator
to GlobalFileTable
This struct was only being used to hold the global file table, and one of its methods didn't even use the table. Changing its methods to ordinary functions makes it easier to see where the table is mutated.
This commit is contained in:
parent
9b72cc9abf
commit
525ac15b66
1 changed files with 94 additions and 74 deletions
|
@ -55,7 +55,7 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||
return;
|
||||
}
|
||||
|
||||
let mut mapgen = CoverageMapGenerator::new(tcx);
|
||||
let mut global_file_table = GlobalFileTable::new(tcx);
|
||||
|
||||
// Encode coverage mappings and generate function records
|
||||
let mut function_data = Vec::new();
|
||||
|
@ -68,7 +68,12 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||
function_coverage.get_expressions_and_counter_regions();
|
||||
|
||||
let coverage_mapping_buffer = llvm::build_byte_buffer(|coverage_mapping_buffer| {
|
||||
mapgen.write_coverage_mapping(expressions, counter_regions, coverage_mapping_buffer);
|
||||
write_coverage_mapping(
|
||||
&mut global_file_table,
|
||||
expressions,
|
||||
counter_regions,
|
||||
coverage_mapping_buffer,
|
||||
);
|
||||
});
|
||||
|
||||
if coverage_mapping_buffer.is_empty() {
|
||||
|
@ -87,19 +92,14 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||
}
|
||||
|
||||
// Encode all filenames referenced by counters/expressions in this module
|
||||
let filenames_buffer = llvm::build_byte_buffer(|filenames_buffer| {
|
||||
coverageinfo::write_filenames_section_to_buffer(
|
||||
mapgen.filenames.iter().map(Symbol::as_str),
|
||||
filenames_buffer,
|
||||
);
|
||||
});
|
||||
let filenames_buffer = global_file_table.into_filenames_buffer();
|
||||
|
||||
let filenames_size = filenames_buffer.len();
|
||||
let filenames_val = cx.const_bytes(&filenames_buffer);
|
||||
let filenames_ref = coverageinfo::hash_bytes(&filenames_buffer);
|
||||
|
||||
// Generate the LLVM IR representation of the coverage map and store it in a well-known global
|
||||
let cov_data_val = mapgen.generate_coverage_map(cx, version, filenames_size, filenames_val);
|
||||
let cov_data_val = generate_coverage_map(cx, version, filenames_size, filenames_val);
|
||||
|
||||
let covfun_section_name = coverageinfo::covfun_section_name(cx);
|
||||
for (mangled_function_name, source_hash, is_used, coverage_mapping_buffer) in function_data {
|
||||
|
@ -118,13 +118,13 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
|
|||
coverageinfo::save_cov_data_to_mod(cx, cov_data_val);
|
||||
}
|
||||
|
||||
struct CoverageMapGenerator {
|
||||
filenames: FxIndexSet<Symbol>,
|
||||
struct GlobalFileTable {
|
||||
global_file_table: FxIndexSet<Symbol>,
|
||||
}
|
||||
|
||||
impl CoverageMapGenerator {
|
||||
impl GlobalFileTable {
|
||||
fn new(tcx: TyCtxt<'_>) -> Self {
|
||||
let mut filenames = FxIndexSet::default();
|
||||
let mut global_file_table = FxIndexSet::default();
|
||||
// LLVM Coverage Mapping Format version 6 (zero-based encoded as 5)
|
||||
// requires setting the first filename to the compilation directory.
|
||||
// Since rustc generates coverage maps with relative paths, the
|
||||
|
@ -133,8 +133,26 @@ impl CoverageMapGenerator {
|
|||
let working_dir = Symbol::intern(
|
||||
&tcx.sess.opts.working_dir.remapped_path_if_available().to_string_lossy(),
|
||||
);
|
||||
filenames.insert(working_dir);
|
||||
Self { filenames }
|
||||
global_file_table.insert(working_dir);
|
||||
Self { global_file_table }
|
||||
}
|
||||
|
||||
fn global_file_id_for_file_name(&mut self, file_name: Symbol) -> u32 {
|
||||
let (global_file_id, _) = self.global_file_table.insert_full(file_name);
|
||||
global_file_id as u32
|
||||
}
|
||||
|
||||
fn into_filenames_buffer(self) -> Vec<u8> {
|
||||
// This method takes `self` so that the caller can't accidentally
|
||||
// modify the original file table after encoding it into a buffer.
|
||||
|
||||
llvm::build_byte_buffer(|buffer| {
|
||||
coverageinfo::write_filenames_section_to_buffer(
|
||||
self.global_file_table.iter().map(Symbol::as_str),
|
||||
buffer,
|
||||
);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Using the `expressions` and `counter_regions` collected for the current function, generate
|
||||
|
@ -142,7 +160,7 @@ impl CoverageMapGenerator {
|
|||
/// LLVM APIs to encode the `virtual_file_mapping`, `expressions`, and `mapping_regions` into
|
||||
/// the given `coverage_mapping` byte buffer, compliant with the LLVM Coverage Mapping format.
|
||||
fn write_coverage_mapping<'a>(
|
||||
&mut self,
|
||||
global_file_table: &mut GlobalFileTable,
|
||||
expressions: Vec<CounterExpression>,
|
||||
counter_regions: impl Iterator<Item = (Counter, &'a CodeRegion)>,
|
||||
coverage_mapping_buffer: &RustString,
|
||||
|
@ -172,9 +190,10 @@ impl CoverageMapGenerator {
|
|||
}
|
||||
current_file_name = Some(file_name);
|
||||
debug!(" file_id: {} = '{:?}'", current_file_id, file_name);
|
||||
let (filenames_index, _) = self.filenames.insert_full(file_name);
|
||||
virtual_file_mapping.push(filenames_index as u32);
|
||||
let global_file_id = global_file_table.global_file_id_for_file_name(file_name);
|
||||
virtual_file_mapping.push(global_file_id);
|
||||
}
|
||||
{
|
||||
debug!("Adding counter {:?} to map for {:?}", counter, region);
|
||||
mapping_regions.push(CounterMappingRegion::code_region(
|
||||
counter,
|
||||
|
@ -185,7 +204,9 @@ impl CoverageMapGenerator {
|
|||
end_col,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Encode and append the current function's coverage mapping data
|
||||
coverageinfo::write_mapping_to_buffer(
|
||||
virtual_file_mapping,
|
||||
|
@ -194,12 +215,12 @@ impl CoverageMapGenerator {
|
|||
coverage_mapping_buffer,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct coverage map header and the array of function records, and combine them into the
|
||||
/// coverage map. Save the coverage map data into the LLVM IR as a static global using a
|
||||
/// specific, well-known section and name.
|
||||
fn generate_coverage_map<'ll>(
|
||||
self,
|
||||
cx: &CodegenCx<'ll, '_>,
|
||||
version: u32,
|
||||
filenames_size: usize,
|
||||
|
@ -221,7 +242,6 @@ impl CoverageMapGenerator {
|
|||
// Create the complete LLVM coverage data value to add to the LLVM IR
|
||||
cx.const_struct(&[cov_data_header_val, filenames_val], /*packed=*/ false)
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct a function record and combine it with the function's coverage mapping data.
|
||||
/// Save the function record into the LLVM IR as a static global using a
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue