[llvm] [Coverage] Guard against empty CountedRegions in findMainViewFileID (PR #189270)

Zile Xiong via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 20:26:27 PDT 2026


https://github.com/xiongzile updated https://github.com/llvm/llvm-project/pull/189270

>From 7ff40d4f66217b1182a90c11072d0a0b362f8349 Mon Sep 17 00:00:00 2001
From: Zile Xiong <xiongzile99 at gmail.com>
Date: Mon, 30 Mar 2026 02:03:54 +0800
Subject: [PATCH] [llvm-cov] Guard against empty CountedRegions in
 findMainViewFileID

findMainViewFileID assumes that a function has at least one counted
region to infer its main view file. In practice, some inputs can reach
this path with an empty CountedRegions list, which leaves the function
without enough information to select a FileID.

Return std::nullopt early in that case instead of continuing with the
normal selection logic.

Add a regression test to cover this case.

Fixes: https://github.com/llvm/llvm-project/issues/189169
---
 .../ProfileData/Coverage/CoverageMapping.cpp  |   2 ++
 .../main-view-fileid-regression.covmapping    | Bin 0 -> 903 bytes
 .../main-view-fileid-regression.proftext      |  32 ++++++++++++++++++
 .../llvm-cov/main-view-fileid-regression.test |  16 +++++++++
 4 files changed, 50 insertions(+)
 create mode 100644 llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.covmapping
 create mode 100644 llvm/test/tools/llvm-cov/Inputs/main-view-fileid-regression.proftext
 create mode 100644 llvm/test/tools/llvm-cov/main-view-fileid-regression.test

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 0000000000000000000000000000000000000000..c30b5eab6decf9fd759bd9062d0d945177373085
GIT binary patch
literal 903
zcmd1FDa%dHFUw_QfPhw}b+5S%%_?qn^MBI*tX-(*cal at LfVb<z*ApN6%uP<7)I8nm
ze@?&KU(ZYDoPn;VuAZh|pE=`2h65EsP3AUWeZ}8M(aGkw>?4B?i- at GXGsM>J0=$-f
z&@MFUGXHo+_XOi+ALlM1WA8IX!QMunf*CGrik))xfH{E(V>+=J!_&aT5TPiMvVa*H
zBn)9t1{;uOn#dSback+BgS-t2JPsE-x14!!SJ5lCA!1FYVDDw=#~gD1t^aU}<w!84
ztmO&6sC(B{!f)%<auv3TaqZ at _4V`T!)w|qcH~M1pKfXb_af-z(`|3kxb08Ls+CzG{
zwawm|tRI!aFANOA3oYlD*L|M%%53U>d7f8pix?Rhm>I>m8AKSFI2n`}8Ce<F82BUj
zW=7A~UZR9uJsYE#D1#UyBQFCVBZDA=C?lgFgAgNwFoPr`gA9Wr1H<=a)=SSge6QgH
w>i=@{g7ajbdqqh0F){L}FsL#zfvf_9`E7e;^Gzd#xUs8eRA6M_<zWy202Jgvi2wiq

literal 0
HcmV?d00001

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