about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-12-11 15:03:31 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-12-11 18:25:10 +1100
commit6a8c016266a6b7514ff8284dc6d8b056e34b9399 (patch)
tree7cd593c7218d33a8bd9f3fdbc2dee77a14c87aff /compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
parent7c4ac71ad1bef9b1e4cb5a3fd10d0b2ef7b418c4 (diff)
downloadrust-6a8c016266a6b7514ff8284dc6d8b056e34b9399.tar.gz
rust-6a8c016266a6b7514ff8284dc6d8b056e34b9399.zip
coverage: Reify `CovfunRecord` as an intermediate step
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs')
-rw-r--r--compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs59
1 files changed, 33 insertions, 26 deletions
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
index aaa6bbcdfc9..a68b026a4c0 100644
--- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
+++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen/covfun.rs
@@ -22,16 +22,30 @@ use crate::coverageinfo::mapgen::{GlobalFileTable, VirtualFileMapping, span_file
 use crate::coverageinfo::{ffi, llvm_cov};
 use crate::llvm;
 
-pub(crate) fn prepare_and_generate_covfun_record<'ll, 'tcx>(
-    cx: &CodegenCx<'ll, 'tcx>,
+/// Intermediate coverage metadata for a single function, used to help build
+/// the final record that will be embedded in the `__llvm_covfun` section.
+#[derive(Debug)]
+pub(crate) struct CovfunRecord<'tcx> {
+    mangled_function_name: &'tcx str,
+    source_hash: u64,
+    is_used: bool,
+    coverage_mapping_buffer: Vec<u8>,
+}
+
+impl<'tcx> CovfunRecord<'tcx> {
+    /// FIXME(Zalathar): Make this the responsibility of the code that determines
+    /// which functions are unused.
+    pub(crate) fn mangled_function_name_if_unused(&self) -> Option<&'tcx str> {
+        (!self.is_used).then_some(self.mangled_function_name)
+    }
+}
+
+pub(crate) fn prepare_covfun_record<'tcx>(
+    tcx: TyCtxt<'tcx>,
     global_file_table: &GlobalFileTable,
-    filenames_ref: u64,
-    unused_function_names: &mut Vec<&'tcx str>,
     instance: Instance<'tcx>,
     function_coverage: &FunctionCoverage<'tcx>,
-) {
-    let tcx = cx.tcx;
-
+) -> Option<CovfunRecord<'tcx>> {
     let mangled_function_name = tcx.symbol_name(instance).name;
     let source_hash = function_coverage.source_hash();
     let is_used = function_coverage.is_used();
@@ -47,22 +61,11 @@ pub(crate) fn prepare_and_generate_covfun_record<'ll, 'tcx>(
             );
         } else {
             debug!("unused function had no coverage mapping data: {}", mangled_function_name);
-            return;
+            return None;
         }
     }
 
-    if !is_used {
-        unused_function_names.push(mangled_function_name);
-    }
-
-    generate_covfun_record(
-        cx,
-        mangled_function_name,
-        source_hash,
-        filenames_ref,
-        coverage_mapping_buffer,
-        is_used,
-    );
+    Some(CovfunRecord { mangled_function_name, source_hash, is_used, coverage_mapping_buffer })
 }
 
 /// Using the expressions and counter regions collected for a single function,
@@ -145,14 +148,18 @@ fn encode_mappings_for_function(
 /// Generates the contents of the covfun record for this function, which
 /// contains the function's coverage mapping data. The record is then stored
 /// as a global variable in the `__llvm_covfun` section.
-fn generate_covfun_record(
-    cx: &CodegenCx<'_, '_>,
-    mangled_function_name: &str,
-    source_hash: u64,
+pub(crate) fn generate_covfun_record<'tcx>(
+    cx: &CodegenCx<'_, 'tcx>,
     filenames_ref: u64,
-    coverage_mapping_buffer: Vec<u8>,
-    is_used: bool,
+    covfun: &CovfunRecord<'tcx>,
 ) {
+    let &CovfunRecord {
+        mangled_function_name,
+        source_hash,
+        is_used,
+        ref coverage_mapping_buffer, // Previously-encoded coverage mappings
+    } = covfun;
+
     // Concatenate the encoded coverage mappings
     let coverage_mapping_size = coverage_mapping_buffer.len();
     let coverage_mapping_val = cx.const_bytes(&coverage_mapping_buffer);