CoverageSummaryInfo.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //===- CoverageSummaryInfo.cpp - 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. #include "CoverageSummaryInfo.h"
  15. using namespace llvm;
  16. using namespace coverage;
  17. FunctionCoverageSummary
  18. FunctionCoverageSummary::get(const coverage::FunctionRecord &Function) {
  19. // Compute the region coverage
  20. size_t NumCodeRegions = 0, CoveredRegions = 0;
  21. for (auto &CR : Function.CountedRegions) {
  22. if (CR.Kind != CounterMappingRegion::CodeRegion)
  23. continue;
  24. ++NumCodeRegions;
  25. if (CR.ExecutionCount != 0)
  26. ++CoveredRegions;
  27. }
  28. // Compute the line coverage
  29. size_t NumLines = 0, CoveredLines = 0;
  30. for (unsigned FileID = 0, E = Function.Filenames.size(); FileID < E;
  31. ++FileID) {
  32. // Find the line start and end of the function's source code
  33. // in that particular file
  34. unsigned LineStart = std::numeric_limits<unsigned>::max();
  35. unsigned LineEnd = 0;
  36. for (auto &CR : Function.CountedRegions) {
  37. if (CR.FileID != FileID)
  38. continue;
  39. LineStart = std::min(LineStart, CR.LineStart);
  40. LineEnd = std::max(LineEnd, CR.LineEnd);
  41. }
  42. unsigned LineCount = LineEnd - LineStart + 1;
  43. // Get counters
  44. llvm::SmallVector<uint64_t, 16> ExecutionCounts;
  45. ExecutionCounts.resize(LineCount, 0);
  46. for (auto &CR : Function.CountedRegions) {
  47. if (CR.FileID != FileID)
  48. continue;
  49. // Ignore the lines that were skipped by the preprocessor.
  50. auto ExecutionCount = CR.ExecutionCount;
  51. if (CR.Kind == CounterMappingRegion::SkippedRegion) {
  52. LineCount -= CR.LineEnd - CR.LineStart + 1;
  53. ExecutionCount = 1;
  54. }
  55. for (unsigned I = CR.LineStart; I <= CR.LineEnd; ++I)
  56. ExecutionCounts[I - LineStart] = ExecutionCount;
  57. }
  58. CoveredLines += LineCount - std::count(ExecutionCounts.begin(),
  59. ExecutionCounts.end(), 0);
  60. NumLines += LineCount;
  61. }
  62. return FunctionCoverageSummary(
  63. Function.Name, Function.ExecutionCount,
  64. RegionCoverageInfo(CoveredRegions, NumCodeRegions),
  65. LineCoverageInfo(CoveredLines, 0, NumLines));
  66. }