[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@gmail.com>
This commit is contained in:
Zile Xiong 2026-04-07 04:58:31 +08:00 committed by GitHub
parent 9033e872fd
commit d917027334
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 50 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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%