From d91702733445ea8bc6d6a8f2f50dd6b65e52d106 Mon Sep 17 00:00:00 2001 From: Zile Xiong Date: Tue, 7 Apr 2026 04:58:31 +0800 Subject: [PATCH] [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 --- .../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 4ce78fe6e318..8b54374aa0b1 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 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@LfVb?4B?i-@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}d7f8pix?Rhm>I>m8AKSFI2n`}8Cei=@{g7ajbdqqh0F){L}FsL#zfvf_9`E7e;^Gzd#xUs8eRA6M_/dev/null | FileCheck %s + +CHECK: Filename +CHECK: lib.rs +CHECK: TOTAL +CHECK: 100.00%