SourceCoverageView.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. // This class implements rendering for code coverage of source code.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "SourceCoverageView.h"
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Support/LineIterator.h"
  18. using namespace llvm;
  19. void SourceCoverageView::renderLine(
  20. raw_ostream &OS, StringRef Line, int64_t LineNumber,
  21. const coverage::CoverageSegment *WrappedSegment,
  22. ArrayRef<const coverage::CoverageSegment *> Segments,
  23. unsigned ExpansionCol) {
  24. Optional<raw_ostream::Colors> Highlight;
  25. SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
  26. // The first segment overlaps from a previous line, so we treat it specially.
  27. if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
  28. Highlight = raw_ostream::RED;
  29. // Output each segment of the line, possibly highlighted.
  30. unsigned Col = 1;
  31. for (const auto *S : Segments) {
  32. unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
  33. colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
  34. Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
  35. << Line.substr(Col - 1, End - Col);
  36. if (Options.Debug && Highlight)
  37. HighlightedRanges.push_back(std::make_pair(Col, End));
  38. Col = End;
  39. if (Col == ExpansionCol)
  40. Highlight = raw_ostream::CYAN;
  41. else if (S->HasCount && S->Count == 0)
  42. Highlight = raw_ostream::RED;
  43. else
  44. Highlight = None;
  45. }
  46. // Show the rest of the line
  47. colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
  48. Options.Colors && Highlight, /*Bold=*/false, /*BG=*/true)
  49. << Line.substr(Col - 1, Line.size() - Col + 1);
  50. OS << "\n";
  51. if (Options.Debug) {
  52. for (const auto &Range : HighlightedRanges)
  53. errs() << "Highlighted line " << LineNumber << ", " << Range.first
  54. << " -> " << Range.second << "\n";
  55. if (Highlight)
  56. errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
  57. }
  58. }
  59. void SourceCoverageView::renderIndent(raw_ostream &OS, unsigned Level) {
  60. for (unsigned I = 0; I < Level; ++I)
  61. OS << " |";
  62. }
  63. void SourceCoverageView::renderViewDivider(unsigned Level, unsigned Length,
  64. raw_ostream &OS) {
  65. assert(Level != 0 && "Cannot render divider at top level");
  66. renderIndent(OS, Level - 1);
  67. OS.indent(2);
  68. for (unsigned I = 0; I < Length; ++I)
  69. OS << "-";
  70. }
  71. /// Format a count using engineering notation with 3 significant digits.
  72. static std::string formatCount(uint64_t N) {
  73. std::string Number = utostr(N);
  74. int Len = Number.size();
  75. if (Len <= 3)
  76. return Number;
  77. int IntLen = Len % 3 == 0 ? 3 : Len % 3;
  78. std::string Result(Number.data(), IntLen);
  79. if (IntLen != 3) {
  80. Result.push_back('.');
  81. Result += Number.substr(IntLen, 3 - IntLen);
  82. }
  83. Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
  84. return Result;
  85. }
  86. void
  87. SourceCoverageView::renderLineCoverageColumn(raw_ostream &OS,
  88. const LineCoverageInfo &Line) {
  89. if (!Line.isMapped()) {
  90. OS.indent(LineCoverageColumnWidth) << '|';
  91. return;
  92. }
  93. std::string C = formatCount(Line.ExecutionCount);
  94. OS.indent(LineCoverageColumnWidth - C.size());
  95. colored_ostream(OS, raw_ostream::MAGENTA,
  96. Line.hasMultipleRegions() && Options.Colors)
  97. << C;
  98. OS << '|';
  99. }
  100. void SourceCoverageView::renderLineNumberColumn(raw_ostream &OS,
  101. unsigned LineNo) {
  102. SmallString<32> Buffer;
  103. raw_svector_ostream BufferOS(Buffer);
  104. BufferOS << LineNo;
  105. auto Str = BufferOS.str();
  106. // Trim and align to the right
  107. Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
  108. OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
  109. }
  110. void SourceCoverageView::renderRegionMarkers(
  111. raw_ostream &OS, ArrayRef<const coverage::CoverageSegment *> Segments) {
  112. unsigned PrevColumn = 1;
  113. for (const auto *S : Segments) {
  114. if (!S->IsRegionEntry)
  115. continue;
  116. // Skip to the new region
  117. if (S->Col > PrevColumn)
  118. OS.indent(S->Col - PrevColumn);
  119. PrevColumn = S->Col + 1;
  120. std::string C = formatCount(S->Count);
  121. PrevColumn += C.size();
  122. OS << '^' << C;
  123. }
  124. OS << "\n";
  125. if (Options.Debug)
  126. for (const auto *S : Segments)
  127. errs() << "Marker at " << S->Line << ":" << S->Col << " = "
  128. << formatCount(S->Count) << (S->IsRegionEntry ? "\n" : " (pop)\n");
  129. }
  130. void SourceCoverageView::render(raw_ostream &OS, bool WholeFile,
  131. unsigned IndentLevel) {
  132. // The width of the leading columns
  133. unsigned CombinedColumnWidth =
  134. (Options.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
  135. (Options.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
  136. // The width of the line that is used to divide between the view and the
  137. // subviews.
  138. unsigned DividerWidth = CombinedColumnWidth + 4;
  139. // We need the expansions and instantiations sorted so we can go through them
  140. // while we iterate lines.
  141. std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
  142. std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
  143. auto NextESV = ExpansionSubViews.begin();
  144. auto EndESV = ExpansionSubViews.end();
  145. auto NextISV = InstantiationSubViews.begin();
  146. auto EndISV = InstantiationSubViews.end();
  147. // Get the coverage information for the file.
  148. auto NextSegment = CoverageInfo.begin();
  149. auto EndSegment = CoverageInfo.end();
  150. unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
  151. const coverage::CoverageSegment *WrappedSegment = nullptr;
  152. SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
  153. for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
  154. // If we aren't rendering the whole file, we need to filter out the prologue
  155. // and epilogue.
  156. if (!WholeFile) {
  157. if (NextSegment == EndSegment)
  158. break;
  159. else if (LI.line_number() < FirstLine)
  160. continue;
  161. }
  162. // Collect the coverage information relevant to this line.
  163. if (LineSegments.size())
  164. WrappedSegment = LineSegments.back();
  165. LineSegments.clear();
  166. while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
  167. LineSegments.push_back(&*NextSegment++);
  168. // Calculate a count to be for the line as a whole.
  169. LineCoverageInfo LineCount;
  170. if (WrappedSegment && WrappedSegment->HasCount)
  171. LineCount.addRegionCount(WrappedSegment->Count);
  172. for (const auto *S : LineSegments)
  173. if (S->HasCount && S->IsRegionEntry)
  174. LineCount.addRegionStartCount(S->Count);
  175. // Render the line prefix.
  176. renderIndent(OS, IndentLevel);
  177. if (Options.ShowLineStats)
  178. renderLineCoverageColumn(OS, LineCount);
  179. if (Options.ShowLineNumbers)
  180. renderLineNumberColumn(OS, LI.line_number());
  181. // If there are expansion subviews, we want to highlight the first one.
  182. unsigned ExpansionColumn = 0;
  183. if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
  184. Options.Colors)
  185. ExpansionColumn = NextESV->getStartCol();
  186. // Display the source code for the current line.
  187. renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
  188. ExpansionColumn);
  189. // Show the region markers.
  190. if (Options.ShowRegionMarkers && (!Options.ShowLineStatsOrRegionMarkers ||
  191. LineCount.hasMultipleRegions()) &&
  192. !LineSegments.empty()) {
  193. renderIndent(OS, IndentLevel);
  194. OS.indent(CombinedColumnWidth);
  195. renderRegionMarkers(OS, LineSegments);
  196. }
  197. // Show the expansions and instantiations for this line.
  198. unsigned NestedIndent = IndentLevel + 1;
  199. bool RenderedSubView = false;
  200. for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
  201. ++NextESV) {
  202. renderViewDivider(NestedIndent, DividerWidth, OS);
  203. OS << "\n";
  204. if (RenderedSubView) {
  205. // Re-render the current line and highlight the expansion range for
  206. // this subview.
  207. ExpansionColumn = NextESV->getStartCol();
  208. renderIndent(OS, IndentLevel);
  209. OS.indent(CombinedColumnWidth + (IndentLevel == 0 ? 0 : 1));
  210. renderLine(OS, *LI, LI.line_number(), WrappedSegment, LineSegments,
  211. ExpansionColumn);
  212. renderViewDivider(NestedIndent, DividerWidth, OS);
  213. OS << "\n";
  214. }
  215. // Render the child subview
  216. if (Options.Debug)
  217. errs() << "Expansion at line " << NextESV->getLine() << ", "
  218. << NextESV->getStartCol() << " -> " << NextESV->getEndCol()
  219. << "\n";
  220. NextESV->View->render(OS, false, NestedIndent);
  221. RenderedSubView = true;
  222. }
  223. for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
  224. renderViewDivider(NestedIndent, DividerWidth, OS);
  225. OS << "\n";
  226. renderIndent(OS, NestedIndent);
  227. OS << ' ';
  228. Options.colored_ostream(OS, raw_ostream::CYAN) << NextISV->FunctionName
  229. << ":";
  230. OS << "\n";
  231. NextISV->View->render(OS, false, NestedIndent);
  232. RenderedSubView = true;
  233. }
  234. if (RenderedSubView) {
  235. renderViewDivider(NestedIndent, DividerWidth, OS);
  236. OS << "\n";
  237. }
  238. }
  239. }