blob: 22c87254e7c7adb8d7aba30e92d2278258abde7d [file] [log] [blame]
Alex Lorenze82d89c2014-08-22 22:56:03 +00001//===- SourceCoverageView.h - 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
14#ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15#define LLVM_COV_SOURCECOVERAGEVIEW_H
16
17#include "CoverageViewOptions.h"
Easwaran Ramandc707122016-04-29 18:53:05 +000018#include "llvm/ProfileData/Coverage/CoverageMapping.h"
Alex Lorenze82d89c2014-08-22 22:56:03 +000019#include "llvm/Support/MemoryBuffer.h"
20#include <vector>
21
22namespace llvm {
23
Justin Bogner5e1400a2014-09-17 05:33:20 +000024class SourceCoverageView;
25
26/// \brief A view that represents a macro or include expansion
27struct ExpansionView {
28 coverage::CounterMappingRegion Region;
29 std::unique_ptr<SourceCoverageView> View;
30
31 ExpansionView(const coverage::CounterMappingRegion &Region,
32 std::unique_ptr<SourceCoverageView> View)
33 : Region(Region), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000034 ExpansionView(ExpansionView &&RHS)
35 : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
36 ExpansionView &operator=(ExpansionView &&RHS) {
37 Region = std::move(RHS.Region);
38 View = std::move(RHS.View);
39 return *this;
40 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000041
42 unsigned getLine() const { return Region.LineStart; }
43 unsigned getStartCol() const { return Region.ColumnStart; }
44 unsigned getEndCol() const { return Region.ColumnEnd; }
45
46 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
47 return LHS.Region.startLoc() < RHS.Region.startLoc();
48 }
49};
50
51/// \brief A view that represents a function instantiation
52struct InstantiationView {
53 StringRef FunctionName;
54 unsigned Line;
55 std::unique_ptr<SourceCoverageView> View;
56
57 InstantiationView(StringRef FunctionName, unsigned Line,
58 std::unique_ptr<SourceCoverageView> View)
59 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000060 InstantiationView(InstantiationView &&RHS)
61 : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)),
62 View(std::move(RHS.View)) {}
63 InstantiationView &operator=(InstantiationView &&RHS) {
64 FunctionName = std::move(RHS.FunctionName);
65 Line = std::move(RHS.Line);
66 View = std::move(RHS.View);
67 return *this;
68 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000069
70 friend bool operator<(const InstantiationView &LHS,
71 const InstantiationView &RHS) {
72 return LHS.Line < RHS.Line;
73 }
74};
75
Alex Lorenze82d89c2014-08-22 22:56:03 +000076/// \brief A code coverage view of a specific source file.
77/// It can have embedded coverage views.
78class SourceCoverageView {
Justin Bognerfe357c02014-09-17 18:23:47 +000079private:
Alex Lorenze82d89c2014-08-22 22:56:03 +000080 /// \brief Coverage information for a single line.
81 struct LineCoverageInfo {
82 uint64_t ExecutionCount;
83 unsigned RegionCount;
84 bool Mapped;
85
86 LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
87
88 bool isMapped() const { return Mapped; }
89
90 bool hasMultipleRegions() const { return RegionCount > 1; }
91
92 void addRegionStartCount(uint64_t Count) {
Justin Bogner4d7aae92015-02-23 21:21:34 +000093 // The max of all region starts is the most interesting value.
94 addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count);
Alex Lorenze82d89c2014-08-22 22:56:03 +000095 ++RegionCount;
96 }
97
98 void addRegionCount(uint64_t Count) {
99 Mapped = true;
Justin Bogner4d7aae92015-02-23 21:21:34 +0000100 ExecutionCount = Count;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000101 }
102 };
103
Alex Lorenze82d89c2014-08-22 22:56:03 +0000104 const MemoryBuffer &File;
105 const CoverageViewOptions &Options;
Justin Bogner953e2402014-09-20 15:31:56 +0000106 coverage::CoverageData CoverageInfo;
Justin Bogner5e1400a2014-09-17 05:33:20 +0000107 std::vector<ExpansionView> ExpansionSubViews;
108 std::vector<InstantiationView> InstantiationSubViews;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000109
Alex Lorenze82d89c2014-08-22 22:56:03 +0000110 /// \brief Render a source line with highlighting.
Justin Bognerfe357c02014-09-17 18:23:47 +0000111 void renderLine(raw_ostream &OS, StringRef Line, int64_t LineNumber,
Justin Bogner953e2402014-09-20 15:31:56 +0000112 const coverage::CoverageSegment *WrappedSegment,
113 ArrayRef<const coverage::CoverageSegment *> Segments,
Justin Bognerfe357c02014-09-17 18:23:47 +0000114 unsigned ExpansionCol);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000115
Justin Bogner76e251c2014-09-16 06:21:57 +0000116 void renderIndent(raw_ostream &OS, unsigned Level);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000117
118 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
119
120 /// \brief Render the line's execution count column.
121 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
122
123 /// \brief Render the line number column.
124 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
125
126 /// \brief Render all the region's execution counts on a line.
Justin Bogner953e2402014-09-20 15:31:56 +0000127 void
128 renderRegionMarkers(raw_ostream &OS,
129 ArrayRef<const coverage::CoverageSegment *> Segments);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000130
131 static const unsigned LineCoverageColumnWidth = 7;
132 static const unsigned LineNumberColumnWidth = 5;
133
134public:
135 SourceCoverageView(const MemoryBuffer &File,
Justin Bogner953e2402014-09-20 15:31:56 +0000136 const CoverageViewOptions &Options,
137 coverage::CoverageData &&CoverageInfo)
138 : File(File), Options(Options), CoverageInfo(std::move(CoverageInfo)) {}
Alex Lorenze82d89c2014-08-22 22:56:03 +0000139
140 const CoverageViewOptions &getOptions() const { return Options; }
141
Justin Bogner5e1400a2014-09-17 05:33:20 +0000142 /// \brief Add an expansion subview to this view.
143 void addExpansion(const coverage::CounterMappingRegion &Region,
144 std::unique_ptr<SourceCoverageView> View) {
145 ExpansionSubViews.emplace_back(Region, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000146 }
147
Justin Bogner5e1400a2014-09-17 05:33:20 +0000148 /// \brief Add a function instantiation subview to this view.
149 void addInstantiation(StringRef FunctionName, unsigned Line,
150 std::unique_ptr<SourceCoverageView> View) {
151 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View));
Alex Lorenze82d89c2014-08-22 22:56:03 +0000152 }
153
154 /// \brief Print the code coverage information for a specific
155 /// portion of a source file to the output stream.
Justin Bognerfe357c02014-09-17 18:23:47 +0000156 void render(raw_ostream &OS, bool WholeFile, unsigned IndentLevel = 0);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000157};
158
159} // namespace llvm
160
161#endif // LLVM_COV_SOURCECOVERAGEVIEW_H