CoverageSummaryInfo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //===- CoverageSummaryInfo.cpp - Coverage summary for function/file -------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // These structures are used to represent code coverage metrics
  10. // for functions/files.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "CoverageSummaryInfo.h"
  14. using namespace llvm;
  15. using namespace coverage;
  16. static void sumBranches(size_t &NumBranches, size_t &CoveredBranches,
  17. const ArrayRef<CountedRegion> &Branches) {
  18. for (const auto &BR : Branches) {
  19. // Skip folded branches.
  20. if (BR.Folded)
  21. continue;
  22. // "True" Condition Branches.
  23. ++NumBranches;
  24. if (BR.ExecutionCount > 0)
  25. ++CoveredBranches;
  26. // "False" Condition Branches.
  27. ++NumBranches;
  28. if (BR.FalseExecutionCount > 0)
  29. ++CoveredBranches;
  30. }
  31. }
  32. static void sumBranchExpansions(size_t &NumBranches, size_t &CoveredBranches,
  33. const CoverageMapping &CM,
  34. ArrayRef<ExpansionRecord> Expansions) {
  35. for (const auto &Expansion : Expansions) {
  36. auto CE = CM.getCoverageForExpansion(Expansion);
  37. sumBranches(NumBranches, CoveredBranches, CE.getBranches());
  38. sumBranchExpansions(NumBranches, CoveredBranches, CM, CE.getExpansions());
  39. }
  40. }
  41. FunctionCoverageSummary
  42. FunctionCoverageSummary::get(const CoverageMapping &CM,
  43. const coverage::FunctionRecord &Function) {
  44. // Compute the region coverage.
  45. size_t NumCodeRegions = 0, CoveredRegions = 0;
  46. for (auto &CR : Function.CountedRegions) {
  47. if (CR.Kind != CounterMappingRegion::CodeRegion)
  48. continue;
  49. ++NumCodeRegions;
  50. if (CR.ExecutionCount != 0)
  51. ++CoveredRegions;
  52. }
  53. // Compute the line coverage
  54. size_t NumLines = 0, CoveredLines = 0;
  55. CoverageData CD = CM.getCoverageForFunction(Function);
  56. for (const auto &LCS : getLineCoverageStats(CD)) {
  57. if (!LCS.isMapped())
  58. continue;
  59. ++NumLines;
  60. if (LCS.getExecutionCount())
  61. ++CoveredLines;
  62. }
  63. // Compute the branch coverage, including branches from expansions.
  64. size_t NumBranches = 0, CoveredBranches = 0;
  65. sumBranches(NumBranches, CoveredBranches, CD.getBranches());
  66. sumBranchExpansions(NumBranches, CoveredBranches, CM, CD.getExpansions());
  67. return FunctionCoverageSummary(
  68. Function.Name, Function.ExecutionCount,
  69. RegionCoverageInfo(CoveredRegions, NumCodeRegions),
  70. LineCoverageInfo(CoveredLines, NumLines),
  71. BranchCoverageInfo(CoveredBranches, NumBranches));
  72. }
  73. FunctionCoverageSummary
  74. FunctionCoverageSummary::get(const InstantiationGroup &Group,
  75. ArrayRef<FunctionCoverageSummary> Summaries) {
  76. std::string Name;
  77. if (Group.hasName()) {
  78. Name = std::string(Group.getName());
  79. } else {
  80. llvm::raw_string_ostream OS(Name);
  81. OS << "Definition at line " << Group.getLine() << ", column "
  82. << Group.getColumn();
  83. }
  84. FunctionCoverageSummary Summary(Name);
  85. Summary.ExecutionCount = Group.getTotalExecutionCount();
  86. Summary.RegionCoverage = Summaries[0].RegionCoverage;
  87. Summary.LineCoverage = Summaries[0].LineCoverage;
  88. Summary.BranchCoverage = Summaries[0].BranchCoverage;
  89. for (const auto &FCS : Summaries.drop_front()) {
  90. Summary.RegionCoverage.merge(FCS.RegionCoverage);
  91. Summary.LineCoverage.merge(FCS.LineCoverage);
  92. Summary.BranchCoverage.merge(FCS.BranchCoverage);
  93. }
  94. return Summary;
  95. }