[llvm] d917027 - [llvm-cov] Guard against empty CountedRegions in findMainViewFileID (#189270)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 13:58:36 PDT 2026
Author: Zile Xiong
Date: 2026-04-06T15:58:31-05:00
New Revision: d91702733445ea8bc6d6a8f2f50dd6b65e52d106
URL: https://github.com/llvm/llvm-project/commit/d91702733445ea8bc6d6a8f2f50dd6b65e52d106
DIFF: https://github.com/llvm/llvm-project/commit/d91702733445ea8bc6d6a8f2f50dd6b65e52d106.diff
LOG: [llvm-cov] Guard against empty CountedRegions in findMainViewFileID (#189270)
When processing coverage generated from branch coverage mode, some
functions can reach findMainViewFileID with an empty CountedRegions
list. In that case the current logic still proceeds to infer the main
view file, even though there is no regular counted region available to
do so.
Return std::nullopt early when CountedRegions is empty.
This was observed when reproducing issue #189169 with:
cargo llvm-cov --lib --branch
The issue appears related to branch-only coverage information being
recorded separately in CountedBranchRegions, while
findMainViewFileID currently only consults CountedRegions.
This patch is a defensive fix for the empty-region case; further
investigation may still be needed to determine whether branch regions
should participate in main view file selection.
Co-authored-by: Zile Xiong <xiongzile99 at gmail.com>
Added:
llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.covmapping
llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.proftext
llvm/test/tools/llvm-cov/main-view-fileid-regression.test
Modified:
llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Removed:
################################################################################
diff --git a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
index 4ce78fe6e3182..8b54374aa0b1c 100644
--- a/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -1436,6 +1436,8 @@ static SmallBitVector gatherFileIDs(StringRef SourceFile,
/// Return the ID of the file where the definition of the function is located.
static std::optional<unsigned>
findMainViewFileID(const FunctionRecord &Function) {
+ if (Function.CountedRegions.empty())
+ return std::nullopt;
SmallBitVector IsNotExpandedFile(Function.Filenames.size(), true);
for (const auto &CR : Function.CountedRegions)
if (CR.Kind == CounterMappingRegion::ExpansionRegion)
diff --git a/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.covmapping b/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.covmapping
new file mode 100644
index 0000000000000..c30b5eab6decf
Binary files /dev/null and b/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.covmapping
diff er
diff --git a/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.proftext b/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.proftext
new file mode 100644
index 0000000000000..7e355d634b399
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.proftext
@@ -0,0 +1,32 @@
+_RNCNvNtCsdlUpERXMHlK_14llvm_cov_crash5testss_4test0B5_
+# Func Hash:
+16906217776502244560
+# Num Counters:
+1
+# Counter Values:
+1
+
+_RNCNvXCsdlUpERXMHlK_14llvm_cov_crashNtB4_4ImplNtB4_5Trait6method0B4_
+# Func Hash:
+8277137360535214580
+# Num Counters:
+1
+# Counter Values:
+1
+
+_RNvNtCsdlUpERXMHlK_14llvm_cov_crash5testss_4test
+# Func Hash:
+16906217776502244560
+# Num Counters:
+1
+# Counter Values:
+1
+
+_RNvXCsdlUpERXMHlK_14llvm_cov_crashNtB2_4ImplNtB2_5Trait6method
+# Func Hash:
+8277137360535214580
+# Num Counters:
+1
+# Counter Values:
+1
+
diff --git a/llvm/test/tools/llvm-cov/main-view-fileid-regression.test b/llvm/test/tools/llvm-cov/main-view-fileid-regression.test
new file mode 100644
index 0000000000000..ea09e7d4791a9
--- /dev/null
+++ b/llvm/test/tools/llvm-cov/main-view-fileid-regression.test
@@ -0,0 +1,16 @@
+# These inputs were derived from a Rust reproducer (see llvm#189169)
+# using cargo llvm-cov --lib --branch.
+#
+# The generated coverage data was converted into .proftext and
+# .covmapping files and checked in for this regression test.
+
+REQUIRES: zlib
+RUN: llvm-profdata merge %S/Inputs/main-view-fileid-regression.proftext -o %t.profdata
+RUN: llvm-cov report %S/Inputs/main-view-fileid-regression.covmapping \
+RUN: -instr-profile=%t.profdata \
+RUN: 2>/dev/null | FileCheck %s
+
+CHECK: Filename
+CHECK: lib.rs
+CHECK: TOTAL
+CHECK: 100.00%
More information about the llvm-commits
mailing list