@@ -80,10 +80,6 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
80
80
let filenames_val = cx. const_bytes ( & filenames_buffer) ;
81
81
let filenames_ref = llvm_cov:: hash_bytes ( & filenames_buffer) ;
82
82
83
- // Generate the coverage map header, which contains the filenames used by
84
- // this CGU's coverage mappings, and store it in a well-known global.
85
- generate_covmap_record ( cx, covmap_version, filenames_size, filenames_val) ;
86
-
87
83
let mut unused_function_names = Vec :: new ( ) ;
88
84
89
85
let covfun_records = function_coverage_map
@@ -93,6 +89,15 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
93
89
} )
94
90
. collect :: < Vec < _ > > ( ) ;
95
91
92
+ // If there are no covfun records for this CGU, don't generate a covmap record.
93
+ // Emitting a covmap record without any covfun records causes `llvm-cov` to
94
+ // fail when generating coverage reports, and if there are no covfun records
95
+ // then the covmap record isn't useful anyway.
96
+ // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
97
+ if covfun_records. is_empty ( ) {
98
+ return ;
99
+ }
100
+
96
101
for covfun in & covfun_records {
97
102
unused_function_names. extend ( covfun. mangled_function_name_if_unused ( ) ) ;
98
103
@@ -117,6 +122,11 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
117
122
llvm:: set_linkage ( array, llvm:: Linkage :: InternalLinkage ) ;
118
123
llvm:: set_initializer ( array, initializer) ;
119
124
}
125
+
126
+ // Generate the coverage map header, which contains the filenames used by
127
+ // this CGU's coverage mappings, and store it in a well-known global.
128
+ // (This is skipped if we returned early due to having no covfun records.)
129
+ generate_covmap_record ( cx, covmap_version, filenames_size, filenames_val) ;
120
130
}
121
131
122
132
/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
0 commit comments