blob: 285767e322cbefcfb1ec164541411b15aea7633d [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- 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//===----------------------------------------------------------------------===//
Vedant Kumaree5a5e92016-06-25 05:48:54 +00009///
10/// \file This class implements rendering for code coverage of source code.
11///
Alex Lorenze82d89c2014-08-22 22:56:03 +000012//===----------------------------------------------------------------------===//
13
14#include "SourceCoverageView.h"
Vedant Kumarf9151b92016-06-25 02:58:30 +000015#include "SourceCoverageViewText.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000016#include "llvm/ADT/SmallString.h"
Justin Bognerd0ceebf2015-05-13 22:41:48 +000017#include "llvm/ADT/StringExtras.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000018#include "llvm/Support/LineIterator.h"
19
20using namespace llvm;
21
Vedant Kumarf9151b92016-06-25 02:58:30 +000022std::string SourceCoverageView::formatCount(uint64_t N) {
Justin Bognerd0ceebf2015-05-13 22:41:48 +000023 std::string Number = utostr(N);
24 int Len = Number.size();
25 if (Len <= 3)
26 return Number;
27 int IntLen = Len % 3 == 0 ? 3 : Len % 3;
28 std::string Result(Number.data(), IntLen);
29 if (IntLen != 3) {
30 Result.push_back('.');
31 Result += Number.substr(IntLen, 3 - IntLen);
32 }
33 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]);
34 return Result;
35}
36
Vedant Kumarf9151b92016-06-25 02:58:30 +000037void SourceCoverageView::addExpansion(
38 const coverage::CounterMappingRegion &Region,
39 std::unique_ptr<SourceCoverageView> View) {
40 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +000041}
42
Vedant Kumarf9151b92016-06-25 02:58:30 +000043void SourceCoverageView::addInstantiation(
44 StringRef FunctionName, unsigned Line,
45 std::unique_ptr<SourceCoverageView> View) {
46 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +000047}
48
Vedant Kumarf9151b92016-06-25 02:58:30 +000049std::unique_ptr<SourceCoverageView>
50SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File,
51 const CoverageViewOptions &Options,
52 coverage::CoverageData &&CoverageInfo) {
53 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options,
54 std::move(CoverageInfo));
Alex Lorenze82d89c2014-08-22 22:56:03 +000055}
56
Vedant Kumarf9151b92016-06-25 02:58:30 +000057void SourceCoverageView::print(raw_ostream &OS, bool WholeFile,
58 bool ShowSourceName, unsigned ViewDepth) {
59 if (ShowSourceName)
60 renderSourceName(OS);
Alex Lorenze82d89c2014-08-22 22:56:03 +000061
Justin Bogner5e1400a2014-09-17 05:33:20 +000062 // We need the expansions and instantiations sorted so we can go through them
63 // while we iterate lines.
64 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end());
65 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end());
66 auto NextESV = ExpansionSubViews.begin();
67 auto EndESV = ExpansionSubViews.end();
68 auto NextISV = InstantiationSubViews.begin();
69 auto EndISV = InstantiationSubViews.end();
70
Justin Bognerfe357c02014-09-17 18:23:47 +000071 // Get the coverage information for the file.
Justin Bogner953e2402014-09-20 15:31:56 +000072 auto NextSegment = CoverageInfo.begin();
73 auto EndSegment = CoverageInfo.end();
Alex Lorenze82d89c2014-08-22 22:56:03 +000074
Justin Bogner13ba23b2014-09-19 08:13:16 +000075 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0;
Justin Bogner953e2402014-09-20 15:31:56 +000076 const coverage::CoverageSegment *WrappedSegment = nullptr;
77 SmallVector<const coverage::CoverageSegment *, 8> LineSegments;
Justin Bognerfe357c02014-09-17 18:23:47 +000078 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) {
79 // If we aren't rendering the whole file, we need to filter out the prologue
80 // and epilogue.
81 if (!WholeFile) {
82 if (NextSegment == EndSegment)
83 break;
Justin Bogner13ba23b2014-09-19 08:13:16 +000084 else if (LI.line_number() < FirstLine)
Justin Bognerfe357c02014-09-17 18:23:47 +000085 continue;
Alex Lorenze82d89c2014-08-22 22:56:03 +000086 }
87
Justin Bognerfe357c02014-09-17 18:23:47 +000088 // Collect the coverage information relevant to this line.
89 if (LineSegments.size())
90 WrappedSegment = LineSegments.back();
91 LineSegments.clear();
92 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number())
93 LineSegments.push_back(&*NextSegment++);
94
95 // Calculate a count to be for the line as a whole.
Vedant Kumar60dcb482016-06-24 00:34:48 +000096 LineCoverageStats LineCount;
Justin Bognerfe357c02014-09-17 18:23:47 +000097 if (WrappedSegment && WrappedSegment->HasCount)
98 LineCount.addRegionCount(WrappedSegment->Count);
99 for (const auto *S : LineSegments)
100 if (S->HasCount && S->IsRegionEntry)
Vedant Kumarf9151b92016-06-25 02:58:30 +0000101 LineCount.addRegionStartCount(S->Count);
Justin Bognerfe357c02014-09-17 18:23:47 +0000102
Vedant Kumarf9151b92016-06-25 02:58:30 +0000103 renderLinePrefix(OS, ViewDepth);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000104 if (getOptions().ShowLineStats)
Justin Bognerfe357c02014-09-17 18:23:47 +0000105 renderLineCoverageColumn(OS, LineCount);
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000106 if (getOptions().ShowLineNumbers)
Justin Bognerfe357c02014-09-17 18:23:47 +0000107 renderLineNumberColumn(OS, LI.line_number());
108
109 // If there are expansion subviews, we want to highlight the first one.
110 unsigned ExpansionColumn = 0;
111 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() &&
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000112 getOptions().Colors)
Justin Bognerfe357c02014-09-17 18:23:47 +0000113 ExpansionColumn = NextESV->getStartCol();
114
Alex Lorenze82d89c2014-08-22 22:56:03 +0000115 // Display the source code for the current line.
Vedant Kumarf9151b92016-06-25 02:58:30 +0000116 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments,
117 ExpansionColumn, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000118
119 // Show the region markers.
Vedant Kumar1c4f5882016-06-24 00:41:26 +0000120 if (getOptions().ShowRegionMarkers &&
121 (!getOptions().ShowLineStatsOrRegionMarkers ||
122 LineCount.hasMultipleRegions()) &&
Justin Bognerfe357c02014-09-17 18:23:47 +0000123 !LineSegments.empty()) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000124 renderRegionMarkers(OS, LineSegments, ViewDepth);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000125 }
126
Justin Bogner5e1400a2014-09-17 05:33:20 +0000127 // Show the expansions and instantiations for this line.
Justin Bogner5e1400a2014-09-17 05:33:20 +0000128 bool RenderedSubView = false;
Justin Bognerfe357c02014-09-17 18:23:47 +0000129 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number();
130 ++NextESV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000131 renderViewDivider(OS, ViewDepth + 1);
132 ExpansionColumn = renderExpansionView(
133 OS, *NextESV,
134 RenderedSubView ? Optional<LineRef>({*LI, LI.line_number()})
135 : Optional<LineRef>(),
136 ExpansionColumn, WrappedSegment, LineSegments, ViewDepth);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000137 RenderedSubView = true;
138 }
Justin Bognerfe357c02014-09-17 18:23:47 +0000139 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) {
Vedant Kumarf9151b92016-06-25 02:58:30 +0000140 renderInstantiationView(OS, *NextISV, ViewDepth + 1);
Justin Bogner5e1400a2014-09-17 05:33:20 +0000141 RenderedSubView = true;
142 }
Vedant Kumarf9151b92016-06-25 02:58:30 +0000143 if (RenderedSubView)
144 renderViewDivider(OS, ViewDepth + 1);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000145 }
146}