Skip to content

Commit

Permalink
rustc_codegen_llvm: use IndexSet in CoverageMapGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Aug 9, 2020
1 parent 997a766 commit 1f71f0f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
19 changes: 5 additions & 14 deletions src/librustc_codegen_llvm/coverageinfo/mapgen.rs
Expand Up @@ -6,7 +6,7 @@ use llvm::coverageinfo::CounterMappingRegion;
use log::debug;
use rustc_codegen_ssa::coverageinfo::map::{Counter, CounterExpression, Region};
use rustc_codegen_ssa::traits::{BaseTypeMethods, ConstMethods};
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::FxIndexSet;
use rustc_llvm::RustString;

use std::ffi::CString;
Expand Down Expand Up @@ -76,13 +76,12 @@ pub fn finalize<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) {
}

struct CoverageMapGenerator {
filenames: Vec<CString>,
filename_to_index: FxHashMap<CString, u32>,
filenames: FxIndexSet<CString>,
}

impl CoverageMapGenerator {
fn new() -> Self {
Self { filenames: Vec::new(), filename_to_index: FxHashMap::default() }
Self { filenames: FxIndexSet::default() }
}

/// Using the `expressions` and `counter_regions` collected for the current function, generate
Expand Down Expand Up @@ -122,16 +121,8 @@ impl CoverageMapGenerator {
let c_filename =
CString::new(file_name).expect("null error converting filename to C string");
debug!(" file_id: {} = '{:?}'", current_file_id, c_filename);
let filenames_index = match self.filename_to_index.get(&c_filename) {
Some(index) => *index,
None => {
let index = self.filenames.len() as u32;
self.filenames.push(c_filename.clone());
self.filename_to_index.insert(c_filename.clone(), index);
index
}
};
virtual_file_mapping.push(filenames_index);
let (filenames_index, _) = self.filenames.insert_full(c_filename);
virtual_file_mapping.push(filenames_index as u32);
}
mapping_regions.push(CounterMappingRegion::code_region(
counter,
Expand Down
7 changes: 5 additions & 2 deletions src/librustc_codegen_llvm/coverageinfo/mod.rs
Expand Up @@ -97,8 +97,11 @@ impl CoverageInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
}

pub(crate) fn write_filenames_section_to_buffer(filenames: &Vec<CString>, buffer: &RustString) {
let c_str_vec = filenames.iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
pub(crate) fn write_filenames_section_to_buffer<'a>(
filenames: impl IntoIterator<Item = &'a CString>,
buffer: &RustString,
) {
let c_str_vec = filenames.into_iter().map(|cstring| cstring.as_ptr()).collect::<Vec<_>>();
unsafe {
llvm::LLVMRustCoverageWriteFilenamesSectionToBuffer(
c_str_vec.as_ptr(),
Expand Down

0 comments on commit 1f71f0f

Please sign in to comment.