SourceCoverageViewText.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //===- SourceCoverageViewText.cpp - A text-based code coverage view -------===//
  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. /// \file This file implements the text-based coverage renderer.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "SourceCoverageViewText.h"
  13. #include "CoverageReport.h"
  14. #include "llvm/ADT/Optional.h"
  15. #include "llvm/ADT/SmallString.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/Support/Format.h"
  18. using namespace llvm;
  19. Expected<CoveragePrinter::OwnedStream>
  20. CoveragePrinterText::createViewFile(StringRef Path, bool InToplevel) {
  21. return createOutputStream(Path, "txt", InToplevel);
  22. }
  23. void CoveragePrinterText::closeViewFile(OwnedStream OS) {
  24. OS->operator<<('\n');
  25. }
  26. Error CoveragePrinterText::createIndexFile(
  27. ArrayRef<std::string> SourceFiles, const CoverageMapping &Coverage,
  28. const CoverageFiltersMatchAll &Filters) {
  29. auto OSOrErr = createOutputStream("index", "txt", /*InToplevel=*/true);
  30. if (Error E = OSOrErr.takeError())
  31. return E;
  32. auto OS = std::move(OSOrErr.get());
  33. raw_ostream &OSRef = *OS.get();
  34. CoverageReport Report(Opts, Coverage);
  35. Report.renderFileReports(OSRef, SourceFiles, Filters);
  36. Opts.colored_ostream(OSRef, raw_ostream::CYAN) << "\n"
  37. << Opts.getLLVMVersionString();
  38. return Error::success();
  39. }
  40. namespace {
  41. static const unsigned LineCoverageColumnWidth = 7;
  42. static const unsigned LineNumberColumnWidth = 5;
  43. /// Get the width of the leading columns.
  44. unsigned getCombinedColumnWidth(const CoverageViewOptions &Opts) {
  45. return (Opts.ShowLineStats ? LineCoverageColumnWidth + 1 : 0) +
  46. (Opts.ShowLineNumbers ? LineNumberColumnWidth + 1 : 0);
  47. }
  48. /// The width of the line that is used to divide between the view and
  49. /// the subviews.
  50. unsigned getDividerWidth(const CoverageViewOptions &Opts) {
  51. return getCombinedColumnWidth(Opts) + 4;
  52. }
  53. } // anonymous namespace
  54. void SourceCoverageViewText::renderViewHeader(raw_ostream &) {}
  55. void SourceCoverageViewText::renderViewFooter(raw_ostream &) {}
  56. void SourceCoverageViewText::renderSourceName(raw_ostream &OS, bool WholeFile) {
  57. getOptions().colored_ostream(OS, raw_ostream::CYAN) << getSourceName()
  58. << ":\n";
  59. }
  60. void SourceCoverageViewText::renderLinePrefix(raw_ostream &OS,
  61. unsigned ViewDepth) {
  62. for (unsigned I = 0; I < ViewDepth; ++I)
  63. OS << " |";
  64. }
  65. void SourceCoverageViewText::renderLineSuffix(raw_ostream &, unsigned) {}
  66. void SourceCoverageViewText::renderViewDivider(raw_ostream &OS,
  67. unsigned ViewDepth) {
  68. assert(ViewDepth != 0 && "Cannot render divider at top level");
  69. renderLinePrefix(OS, ViewDepth - 1);
  70. OS.indent(2);
  71. unsigned Length = getDividerWidth(getOptions());
  72. for (unsigned I = 0; I < Length; ++I)
  73. OS << '-';
  74. OS << '\n';
  75. }
  76. void SourceCoverageViewText::renderLine(raw_ostream &OS, LineRef L,
  77. const LineCoverageStats &LCS,
  78. unsigned ExpansionCol,
  79. unsigned ViewDepth) {
  80. StringRef Line = L.Line;
  81. unsigned LineNumber = L.LineNo;
  82. auto *WrappedSegment = LCS.getWrappedSegment();
  83. CoverageSegmentArray Segments = LCS.getLineSegments();
  84. Optional<raw_ostream::Colors> Highlight;
  85. SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
  86. // The first segment overlaps from a previous line, so we treat it specially.
  87. if (WrappedSegment && !WrappedSegment->IsGapRegion &&
  88. WrappedSegment->HasCount && WrappedSegment->Count == 0)
  89. Highlight = raw_ostream::RED;
  90. // Output each segment of the line, possibly highlighted.
  91. unsigned Col = 1;
  92. for (const auto *S : Segments) {
  93. unsigned End = std::min(S->Col, static_cast<unsigned>(Line.size()) + 1);
  94. colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
  95. getOptions().Colors && Highlight, /*Bold=*/false,
  96. /*BG=*/true)
  97. << Line.substr(Col - 1, End - Col);
  98. if (getOptions().Debug && Highlight)
  99. HighlightedRanges.push_back(std::make_pair(Col, End));
  100. Col = End;
  101. if ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) &&
  102. S->HasCount && S->Count == 0)
  103. Highlight = raw_ostream::RED;
  104. else if (Col == ExpansionCol)
  105. Highlight = raw_ostream::CYAN;
  106. else
  107. Highlight = None;
  108. }
  109. // Show the rest of the line.
  110. colored_ostream(OS, Highlight ? *Highlight : raw_ostream::SAVEDCOLOR,
  111. getOptions().Colors && Highlight, /*Bold=*/false, /*BG=*/true)
  112. << Line.substr(Col - 1, Line.size() - Col + 1);
  113. OS << '\n';
  114. if (getOptions().Debug) {
  115. for (const auto &Range : HighlightedRanges)
  116. errs() << "Highlighted line " << LineNumber << ", " << Range.first
  117. << " -> " << Range.second << '\n';
  118. if (Highlight)
  119. errs() << "Highlighted line " << LineNumber << ", " << Col << " -> ?\n";
  120. }
  121. }
  122. void SourceCoverageViewText::renderLineCoverageColumn(
  123. raw_ostream &OS, const LineCoverageStats &Line) {
  124. if (!Line.isMapped()) {
  125. OS.indent(LineCoverageColumnWidth) << '|';
  126. return;
  127. }
  128. std::string C = formatCount(Line.getExecutionCount());
  129. OS.indent(LineCoverageColumnWidth - C.size());
  130. colored_ostream(OS, raw_ostream::MAGENTA,
  131. Line.hasMultipleRegions() && getOptions().Colors)
  132. << C;
  133. OS << '|';
  134. }
  135. void SourceCoverageViewText::renderLineNumberColumn(raw_ostream &OS,
  136. unsigned LineNo) {
  137. SmallString<32> Buffer;
  138. raw_svector_ostream BufferOS(Buffer);
  139. BufferOS << LineNo;
  140. auto Str = BufferOS.str();
  141. // Trim and align to the right.
  142. Str = Str.substr(0, std::min(Str.size(), (size_t)LineNumberColumnWidth));
  143. OS.indent(LineNumberColumnWidth - Str.size()) << Str << '|';
  144. }
  145. void SourceCoverageViewText::renderRegionMarkers(raw_ostream &OS,
  146. const LineCoverageStats &Line,
  147. unsigned ViewDepth) {
  148. renderLinePrefix(OS, ViewDepth);
  149. OS.indent(getCombinedColumnWidth(getOptions()));
  150. CoverageSegmentArray Segments = Line.getLineSegments();
  151. // Just consider the segments which start *and* end on this line.
  152. if (Segments.size() > 1)
  153. Segments = Segments.drop_back();
  154. unsigned PrevColumn = 1;
  155. for (const auto *S : Segments) {
  156. if (!S->IsRegionEntry)
  157. continue;
  158. if (S->Count == Line.getExecutionCount())
  159. continue;
  160. // Skip to the new region.
  161. if (S->Col > PrevColumn)
  162. OS.indent(S->Col - PrevColumn);
  163. PrevColumn = S->Col + 1;
  164. std::string C = formatCount(S->Count);
  165. PrevColumn += C.size();
  166. OS << '^' << C;
  167. if (getOptions().Debug)
  168. errs() << "Marker at " << S->Line << ":" << S->Col << " = "
  169. << formatCount(S->Count) << "\n";
  170. }
  171. OS << '\n';
  172. }
  173. void SourceCoverageViewText::renderExpansionSite(raw_ostream &OS, LineRef L,
  174. const LineCoverageStats &LCS,
  175. unsigned ExpansionCol,
  176. unsigned ViewDepth) {
  177. renderLinePrefix(OS, ViewDepth);
  178. OS.indent(getCombinedColumnWidth(getOptions()) + (ViewDepth == 0 ? 0 : 1));
  179. renderLine(OS, L, LCS, ExpansionCol, ViewDepth);
  180. }
  181. void SourceCoverageViewText::renderExpansionView(raw_ostream &OS,
  182. ExpansionView &ESV,
  183. unsigned ViewDepth) {
  184. // Render the child subview.
  185. if (getOptions().Debug)
  186. errs() << "Expansion at line " << ESV.getLine() << ", " << ESV.getStartCol()
  187. << " -> " << ESV.getEndCol() << '\n';
  188. ESV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/false,
  189. /*ShowTitle=*/false, ViewDepth + 1);
  190. }
  191. void SourceCoverageViewText::renderBranchView(raw_ostream &OS, BranchView &BRV,
  192. unsigned ViewDepth) {
  193. // Render the child subview.
  194. if (getOptions().Debug)
  195. errs() << "Branch at line " << BRV.getLine() << '\n';
  196. for (const auto &R : BRV.Regions) {
  197. double TruePercent = 0.0;
  198. double FalsePercent = 0.0;
  199. unsigned Total = R.ExecutionCount + R.FalseExecutionCount;
  200. if (!getOptions().ShowBranchCounts && Total != 0) {
  201. TruePercent = ((double)(R.ExecutionCount) / (double)Total) * 100.0;
  202. FalsePercent = ((double)(R.FalseExecutionCount) / (double)Total) * 100.0;
  203. }
  204. renderLinePrefix(OS, ViewDepth);
  205. OS << " Branch (" << R.LineStart << ":" << R.ColumnStart << "): [";
  206. if (R.Folded) {
  207. OS << "Folded - Ignored]\n";
  208. continue;
  209. }
  210. colored_ostream(OS, raw_ostream::RED,
  211. getOptions().Colors && !R.ExecutionCount,
  212. /*Bold=*/false, /*BG=*/true)
  213. << "True";
  214. if (getOptions().ShowBranchCounts)
  215. OS << ": " << formatCount(R.ExecutionCount) << ", ";
  216. else
  217. OS << ": " << format("%0.2f", TruePercent) << "%, ";
  218. colored_ostream(OS, raw_ostream::RED,
  219. getOptions().Colors && !R.FalseExecutionCount,
  220. /*Bold=*/false, /*BG=*/true)
  221. << "False";
  222. if (getOptions().ShowBranchCounts)
  223. OS << ": " << formatCount(R.FalseExecutionCount);
  224. else
  225. OS << ": " << format("%0.2f", FalsePercent) << "%";
  226. OS << "]\n";
  227. }
  228. }
  229. void SourceCoverageViewText::renderInstantiationView(raw_ostream &OS,
  230. InstantiationView &ISV,
  231. unsigned ViewDepth) {
  232. renderLinePrefix(OS, ViewDepth);
  233. OS << ' ';
  234. if (!ISV.View)
  235. getOptions().colored_ostream(OS, raw_ostream::RED)
  236. << "Unexecuted instantiation: " << ISV.FunctionName << "\n";
  237. else
  238. ISV.View->print(OS, /*WholeFile=*/false, /*ShowSourceName=*/true,
  239. /*ShowTitle=*/false, ViewDepth);
  240. }
  241. void SourceCoverageViewText::renderTitle(raw_ostream &OS, StringRef Title) {
  242. if (getOptions().hasProjectTitle())
  243. getOptions().colored_ostream(OS, raw_ostream::CYAN)
  244. << getOptions().ProjectTitle << "\n";
  245. getOptions().colored_ostream(OS, raw_ostream::CYAN) << Title << "\n";
  246. if (getOptions().hasCreatedTime())
  247. getOptions().colored_ostream(OS, raw_ostream::CYAN)
  248. << getOptions().CreatedTimeStr << "\n";
  249. }
  250. void SourceCoverageViewText::renderTableHeader(raw_ostream &, unsigned,
  251. unsigned) {}