about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-12-11 15:12:23 +1100
committerZalathar <Zalathar@users.noreply.github.com>2024-12-11 21:35:44 +1100
commit512f3fdebe72532a435238435f0e16eff61fbf38 (patch)
treebd26c1faa74f3a6100c0c1afa4750b016bcf5aa9 /compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
parent9e6b7c17c8d39563074c8ed8ba8a2ece72b3d243 (diff)
downloadrust-512f3fdebe72532a435238435f0e16eff61fbf38.tar.gz
rust-512f3fdebe72532a435238435f0e16eff61fbf38.zip
coverage: Only generate a CGU's covmap record if it has covfun records
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs')
-rw-r--r--compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs18
1 files changed, 14 insertions, 4 deletions
diff --git a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
index 39fb0aeed07..f7c3193a449 100644
--- a/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
+++ b/compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs
@@ -80,10 +80,6 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
     let filenames_val = cx.const_bytes(&filenames_buffer);
     let filenames_ref = llvm_cov::hash_bytes(&filenames_buffer);
 
-    // Generate the coverage map header, which contains the filenames used by
-    // this CGU's coverage mappings, and store it in a well-known global.
-    generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
-
     let mut unused_function_names = Vec::new();
 
     let covfun_records = function_coverage_map
@@ -93,6 +89,15 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
         })
         .collect::<Vec<_>>();
 
+    // If there are no covfun records for this CGU, don't generate a covmap record.
+    // Emitting a covmap record without any covfun records causes `llvm-cov` to
+    // fail when generating coverage reports, and if there are no covfun records
+    // then the covmap record isn't useful anyway.
+    // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
+    if covfun_records.is_empty() {
+        return;
+    }
+
     for covfun in &covfun_records {
         unused_function_names.extend(covfun.mangled_function_name_if_unused());
 
@@ -117,6 +122,11 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
         llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
         llvm::set_initializer(array, initializer);
     }
+
+    // Generate the coverage map header, which contains the filenames used by
+    // this CGU's coverage mappings, and store it in a well-known global.
+    // (This is skipped if we returned early due to having no covfun records.)
+    generate_covmap_record(cx, covmap_version, filenames_size, filenames_val);
 }
 
 /// Maps "global" (per-CGU) file ID numbers to their underlying filenames.