CoverageSummaryInfo.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //===- CoverageSummaryInfo.h - Coverage summary for function/file ---------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // These structures are used to represent code coverage metrics
  11. // for functions/files.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_COV_COVERAGESUMMARYINFO_H
  15. #define LLVM_COV_COVERAGESUMMARYINFO_H
  16. #include "llvm/ProfileData/CoverageMapping.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. namespace llvm {
  19. /// \brief Provides information about region coverage for a function/file.
  20. struct RegionCoverageInfo {
  21. /// \brief The number of regions that were executed at least once.
  22. size_t Covered;
  23. /// \brief The number of regions that weren't executed.
  24. size_t NotCovered;
  25. /// \brief The total number of regions in a function/file.
  26. size_t NumRegions;
  27. RegionCoverageInfo() : Covered(0), NotCovered(0), NumRegions(0) {}
  28. RegionCoverageInfo(size_t Covered, size_t NumRegions)
  29. : Covered(Covered), NotCovered(NumRegions - Covered),
  30. NumRegions(NumRegions) {}
  31. RegionCoverageInfo &operator+=(const RegionCoverageInfo &RHS) {
  32. Covered += RHS.Covered;
  33. NotCovered += RHS.NotCovered;
  34. NumRegions += RHS.NumRegions;
  35. return *this;
  36. }
  37. bool isFullyCovered() const { return Covered == NumRegions; }
  38. double getPercentCovered() const {
  39. return double(Covered) / double(NumRegions) * 100.0;
  40. }
  41. };
  42. /// \brief Provides information about line coverage for a function/file.
  43. struct LineCoverageInfo {
  44. /// \brief The number of lines that were executed at least once.
  45. size_t Covered;
  46. /// \brief The number of lines that weren't executed.
  47. size_t NotCovered;
  48. /// \brief The number of lines that aren't code.
  49. size_t NonCodeLines;
  50. /// \brief The total number of lines in a function/file.
  51. size_t NumLines;
  52. LineCoverageInfo()
  53. : Covered(0), NotCovered(0), NonCodeLines(0), NumLines(0) {}
  54. LineCoverageInfo(size_t Covered, size_t NumNonCodeLines, size_t NumLines)
  55. : Covered(Covered), NotCovered(NumLines - NumNonCodeLines - Covered),
  56. NonCodeLines(NumNonCodeLines), NumLines(NumLines) {}
  57. LineCoverageInfo &operator+=(const LineCoverageInfo &RHS) {
  58. Covered += RHS.Covered;
  59. NotCovered += RHS.NotCovered;
  60. NonCodeLines += RHS.NonCodeLines;
  61. NumLines += RHS.NumLines;
  62. return *this;
  63. }
  64. bool isFullyCovered() const { return Covered == (NumLines - NonCodeLines); }
  65. double getPercentCovered() const {
  66. return double(Covered) / double(NumLines - NonCodeLines) * 100.0;
  67. }
  68. };
  69. /// \brief Provides information about function coverage for a file.
  70. struct FunctionCoverageInfo {
  71. /// \brief The number of functions that were executed.
  72. size_t Executed;
  73. /// \brief The total number of functions in this file.
  74. size_t NumFunctions;
  75. FunctionCoverageInfo() : Executed(0), NumFunctions(0) {}
  76. FunctionCoverageInfo(size_t Executed, size_t NumFunctions)
  77. : Executed(Executed), NumFunctions(NumFunctions) {}
  78. void addFunction(bool Covered) {
  79. if (Covered)
  80. ++Executed;
  81. ++NumFunctions;
  82. }
  83. bool isFullyCovered() const { return Executed == NumFunctions; }
  84. double getPercentCovered() const {
  85. return double(Executed) / double(NumFunctions) * 100.0;
  86. }
  87. };
  88. /// \brief A summary of function's code coverage.
  89. struct FunctionCoverageSummary {
  90. StringRef Name;
  91. uint64_t ExecutionCount;
  92. RegionCoverageInfo RegionCoverage;
  93. LineCoverageInfo LineCoverage;
  94. FunctionCoverageSummary(StringRef Name) : Name(Name), ExecutionCount(0) {}
  95. FunctionCoverageSummary(StringRef Name, uint64_t ExecutionCount,
  96. const RegionCoverageInfo &RegionCoverage,
  97. const LineCoverageInfo &LineCoverage)
  98. : Name(Name), ExecutionCount(ExecutionCount),
  99. RegionCoverage(RegionCoverage), LineCoverage(LineCoverage) {
  100. }
  101. /// \brief Compute the code coverage summary for the given function coverage
  102. /// mapping record.
  103. static FunctionCoverageSummary
  104. get(const coverage::FunctionRecord &Function);
  105. };
  106. /// \brief A summary of file's code coverage.
  107. struct FileCoverageSummary {
  108. StringRef Name;
  109. RegionCoverageInfo RegionCoverage;
  110. LineCoverageInfo LineCoverage;
  111. FunctionCoverageInfo FunctionCoverage;
  112. FileCoverageSummary(StringRef Name) : Name(Name) {}
  113. void addFunction(const FunctionCoverageSummary &Function) {
  114. RegionCoverage += Function.RegionCoverage;
  115. LineCoverage += Function.LineCoverage;
  116. FunctionCoverage.addFunction(/*Covered=*/Function.ExecutionCount > 0);
  117. }
  118. };
  119. } // namespace llvm
  120. #endif // LLVM_COV_COVERAGESUMMARYINFO_H