SourceCoverageView.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===- SourceCoverageView.cpp - Code coverage view for source code --------===//
  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. /// \file This class implements rendering for code coverage of source code.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "SourceCoverageView.h"
  14. #include "SourceCoverageViewText.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Support/LineIterator.h"
  18. using namespace llvm;
  19. std::string SourceCoverageView::formatCount(uint64_t N) {
  20. std::string Number = utostr(N);
  21. int Len = Number.size();
  22. if (Len <= 3)
  23. return Number;
  24. int IntLen = Len % 3 == 0 ? 3 : Len % 3;
  25. std::string Result(Number.data(), IntLen);
  26. if (IntLen != 3) {
  27. Result.push_back('.');
  28. Result += Number.substr(IntLen, 3 - IntLen);
  29. }
  30. Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
  31. return Result;
  32. }
  33. void SourceCoverageView::addExpansion(
  34. const coverage::CounterMappingRegion &Region,
  35. std::unique_ptr<SourceCoverageView> View) {
  36. ExpansionSubViews.emplace_back(Region, std::move(View));
  37. }
  38. void SourceCoverageView::addInstantiation(
  39. StringRef FunctionName, unsigned Line,
  40. std::unique_ptr<SourceCoverageView> View) {
  41. InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
  42. }
  43. std::unique_ptr<SourceCoverageView>
  44. SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
  45. const CoverageViewOptions &Options,
  46. coverage::CoverageData &&CoverageInfo) {
  47. return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
  48. std::move(CoverageInfo));
  49. }
  50. void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
  51. bool ShowSourceName, unsigned ViewDepth) {
  52. if (ShowSourceName)
  53. renderSourceName(OS);
  54. // We need the expansions and instantiations sorted so we can go through them
  55. // while we iterate lines.
  56. std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
  57. std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
  58. auto NextESV = ExpansionSubViews.begin();
  59. auto EndESV = ExpansionSubViews.end();
  60. auto NextISV = InstantiationSubViews.begin();
  61. auto EndISV = InstantiationSubViews.end();
  62. // Get the coverage information for the file.
  63. auto NextSegment = CoverageInfo.begin();
  64. auto EndSegment = CoverageInfo.end();
  65. unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
  66. const coverage::CoverageSegment *WrappedSegment = nullptr;
  67. SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
  68. for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
  69. // If we aren't rendering the whole file, we need to filter out the prologue
  70. // and epilogue.
  71. if (!WholeFile) {
  72. if (NextSegment == EndSegment)
  73. break;
  74. else if (LI.line_number() < FirstLine)
  75. continue;
  76. }
  77. // Collect the coverage information relevant to this line.
  78. if (LineSegments.size())
  79. WrappedSegment = LineSegments.back();
  80. LineSegments.clear();
  81. while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
  82. LineSegments.push_back(&*NextSegment++);
  83. // Calculate a count to be for the line as a whole.
  84. LineCoverageStats LineCount;
  85. if (WrappedSegment && WrappedSegment->HasCount)
  86. LineCount.addRegionCount(WrappedSegment->Count);
  87. for (const auto *S : LineSegments)
  88. if (S->HasCount && S->IsRegionEntry)
  89. LineCount.addRegionStartCount(S->Count);
  90. renderLinePrefix(OS, ViewDepth);
  91. if (getOptions().ShowLineStats)
  92. renderLineCoverageColumn(OS, LineCount);
  93. if (getOptions().ShowLineNumbers)
  94. renderLineNumberColumn(OS, LI.line_number());
  95. // If there are expansion subviews, we want to highlight the first one.
  96. unsigned ExpansionColumn = 0;
  97. if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
  98. getOptions().Colors)
  99. ExpansionColumn = NextESV->getStartCol();
  100. // Display the source code for the current line.
  101. renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
  102. ExpansionColumn, ViewDepth);
  103. // Show the region markers.
  104. if (getOptions().ShowRegionMarkers &&
  105. (!getOptions().ShowLineStatsOrRegionMarkers ||
  106. LineCount.hasMultipleRegions()) &&
  107. !LineSegments.empty()) {
  108. renderRegionMarkers(OS, LineSegments, ViewDepth);
  109. }
  110. // Show the expansions and instantiations for this line.
  111. bool RenderedSubView = false;
  112. for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
  113. ++NextESV) {
  114. renderViewDivider(OS, ViewDepth + 1);
  115. ExpansionColumn = renderExpansionView(
  116. OS, *NextESV,
  117. RenderedSubView ? Optional<LineRef>({*LI, LI.line_number()})
  118. : Optional<LineRef>(),
  119. ExpansionColumn, WrappedSegment, LineSegments, ViewDepth);
  120. RenderedSubView = true;
  121. }
  122. for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
  123. renderInstantiationView(OS, *NextISV, ViewDepth + 1);
  124. RenderedSubView = true;
  125. }
  126. if (RenderedSubView)
  127. renderViewDivider(OS, ViewDepth + 1);
  128. }
  129. }