Skip to content

Commit 5b6c0b0

Browse files
committed
LLVMCoverage: Unify getCoverageForFile and getCoverageForFunction
1 parent 4e3c0bb commit 5b6c0b0

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ class InstantiationGroup {
889889
class CoverageData {
890890
friend class CoverageMapping;
891891

892+
protected:
892893
std::string Filename;
893894
std::vector<CoverageSegment> Segments;
894895
std::vector<ExpansionRecord> Expansions;
@@ -900,6 +901,8 @@ class CoverageData {
900901

901902
CoverageData(StringRef Filename) : Filename(Filename) {}
902903

904+
CoverageData(CoverageData &&RHS) = default;
905+
903906
/// Get the name of the file this data covers.
904907
StringRef getFilename() const { return Filename; }
905908

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1343,6 +1343,36 @@ class SegmentBuilder {
13431343
}
13441344
};
13451345

1346+
struct MergeableCoverageData : public CoverageData {
1347+
std::vector<CountedRegion> CodeRegions;
1348+
1349+
MergeableCoverageData(StringRef Filename) : CoverageData(Filename) {}
1350+
1351+
void addFunctionRegions(
1352+
const FunctionRecord &Function,
1353+
std::function<bool(const CounterMappingRegion &CR)> shouldProcess,
1354+
std::function<bool(const CountedRegion &CR)> shouldExpand) {
1355+
for (const auto &CR : Function.CountedRegions)
1356+
if (shouldProcess(CR)) {
1357+
CodeRegions.push_back(CR);
1358+
if (shouldExpand(CR))
1359+
Expansions.emplace_back(CR, Function);
1360+
}
1361+
// Capture branch regions specific to the function (excluding expansions).
1362+
for (const auto &CR : Function.CountedBranchRegions)
1363+
if (shouldProcess(CR))
1364+
BranchRegions.push_back(CR);
1365+
// Capture MCDC records specific to the function.
1366+
for (const auto &MR : Function.MCDCRecords)
1367+
if (shouldProcess(MR.getDecisionRegion()))
1368+
MCDCRecords.push_back(MR);
1369+
}
1370+
1371+
CoverageData buildSegments() {
1372+
Segments = SegmentBuilder::buildSegments(CodeRegions);
1373+
return CoverageData(std::move(*this));
1374+
}
1375+
};
13461376
} // end anonymous namespace
13471377

13481378
std::vector<StringRef> CoverageMapping::getUniqueSourceFiles() const {
@@ -1393,8 +1423,7 @@ static bool isExpansion(const CountedRegion &R, unsigned FileID) {
13931423
}
13941424

13951425
CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
1396-
CoverageData FileCoverage(Filename);
1397-
std::vector<CountedRegion> Regions;
1426+
MergeableCoverageData FileCoverage(Filename);
13981427

13991428
// Look up the function records in the given file. Due to hash collisions on
14001429
// the filename, we may get back some records that are not in the file.
@@ -1404,26 +1433,14 @@ CoverageData CoverageMapping::getCoverageForFile(StringRef Filename) const {
14041433
const FunctionRecord &Function = Functions[RecordIndex];
14051434
auto MainFileID = findMainViewFileID(Filename, Function);
14061435
auto FileIDs = gatherFileIDs(Filename, Function);
1407-
for (const auto &CR : Function.CountedRegions)
1408-
if (FileIDs.test(CR.FileID)) {
1409-
Regions.push_back(CR);
1410-
if (MainFileID && isExpansion(CR, *MainFileID))
1411-
FileCoverage.Expansions.emplace_back(CR, Function);
1412-
}
1413-
// Capture branch regions specific to the function (excluding expansions).
1414-
for (const auto &CR : Function.CountedBranchRegions)
1415-
if (FileIDs.test(CR.FileID))
1416-
FileCoverage.BranchRegions.push_back(CR);
1417-
// Capture MCDC records specific to the function.
1418-
for (const auto &MR : Function.MCDCRecords)
1419-
if (FileIDs.test(MR.getDecisionRegion().FileID))
1420-
FileCoverage.MCDCRecords.push_back(MR);
1436+
FileCoverage.addFunctionRegions(
1437+
Function, [&](auto &CR) { return FileIDs.test(CR.FileID); },
1438+
[&](auto &CR) { return (MainFileID && isExpansion(CR, *MainFileID)); });
14211439
}
14221440

14231441
LLVM_DEBUG(dbgs() << "Emitting segments for file: " << Filename << "\n");
1424-
FileCoverage.Segments = SegmentBuilder::buildSegments(Regions);
14251442

1426-
return FileCoverage;
1443+
return FileCoverage.buildSegments();
14271444
}
14281445

14291446
std::vector<InstantiationGroup>
@@ -1457,29 +1474,15 @@ CoverageMapping::getCoverageForFunction(const FunctionRecord &Function) const {
14571474
if (!MainFileID)
14581475
return CoverageData();
14591476

1460-
CoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
1461-
std::vector<CountedRegion> Regions;
1462-
for (const auto &CR : Function.CountedRegions)
1463-
if (CR.FileID == *MainFileID) {
1464-
Regions.push_back(CR);
1465-
if (isExpansion(CR, *MainFileID))
1466-
FunctionCoverage.Expansions.emplace_back(CR, Function);
1467-
}
1468-
// Capture branch regions specific to the function (excluding expansions).
1469-
for (const auto &CR : Function.CountedBranchRegions)
1470-
if (CR.FileID == *MainFileID)
1471-
FunctionCoverage.BranchRegions.push_back(CR);
1472-
1473-
// Capture MCDC records specific to the function.
1474-
for (const auto &MR : Function.MCDCRecords)
1475-
if (MR.getDecisionRegion().FileID == *MainFileID)
1476-
FunctionCoverage.MCDCRecords.push_back(MR);
1477+
MergeableCoverageData FunctionCoverage(Function.Filenames[*MainFileID]);
1478+
FunctionCoverage.addFunctionRegions(
1479+
Function, [&](auto &CR) { return (CR.FileID == *MainFileID); },
1480+
[&](auto &CR) { return isExpansion(CR, *MainFileID); });
14771481

14781482
LLVM_DEBUG(dbgs() << "Emitting segments for function: " << Function.Name
14791483
<< "\n");
1480-
FunctionCoverage.Segments = SegmentBuilder::buildSegments(Regions);
14811484

1482-
return FunctionCoverage;
1485+
return FunctionCoverage.buildSegments();
14831486
}
14841487

14851488
CoverageData CoverageMapping::getCoverageForExpansion(

0 commit comments

Comments
 (0)