blob: 54a11d8c51caae850ebae6a8e8e89568be58c0b2 [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"
18#include "SourceCoverageDataManager.h"
19#include "llvm/ProfileData/CoverageMapping.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <vector>
22
23namespace llvm {
24
Justin Bogner5e1400a2014-09-17 05:33:20 +000025class SourceCoverageView;
26
27/// \brief A view that represents a macro or include expansion
28struct ExpansionView {
29 coverage::CounterMappingRegion Region;
30 std::unique_ptr<SourceCoverageView> View;
31
32 ExpansionView(const coverage::CounterMappingRegion &Region,
33 std::unique_ptr<SourceCoverageView> View)
34 : Region(Region), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000035 ExpansionView(ExpansionView &&RHS)
36 : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
37 ExpansionView &operator=(ExpansionView &&RHS) {
38 Region = std::move(RHS.Region);
39 View = std::move(RHS.View);
40 return *this;
41 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000042
43 unsigned getLine() const { return Region.LineStart; }
44 unsigned getStartCol() const { return Region.ColumnStart; }
45 unsigned getEndCol() const { return Region.ColumnEnd; }
46
47 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
48 return LHS.Region.startLoc() < RHS.Region.startLoc();
49 }
50};
51
52/// \brief A view that represents a function instantiation
53struct InstantiationView {
54 StringRef FunctionName;
55 unsigned Line;
56 std::unique_ptr<SourceCoverageView> View;
57
58 InstantiationView(StringRef FunctionName, unsigned Line,
59 std::unique_ptr<SourceCoverageView> View)
60 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
Justin Bogner99e95182014-09-17 06:32:48 +000061 InstantiationView(InstantiationView &&RHS)
62 : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)),
63 View(std::move(RHS.View)) {}
64 InstantiationView &operator=(InstantiationView &&RHS) {
65 FunctionName = std::move(RHS.FunctionName);
66 Line = std::move(RHS.Line);
67 View = std::move(RHS.View);
68 return *this;
69 }
Justin Bogner5e1400a2014-09-17 05:33:20 +000070
71 friend bool operator<(const InstantiationView &LHS,
72 const InstantiationView &RHS) {
73 return LHS.Line < RHS.Line;
74 }
75};
76
Alex Lorenze82d89c2014-08-22 22:56:03 +000077/// \brief A code coverage view of a specific source file.
78/// It can have embedded coverage views.
79class SourceCoverageView {
Justin Bognerfe357c02014-09-17 18:23:47 +000080private:
Alex Lorenze82d89c2014-08-22 22:56:03 +000081 /// \brief Coverage information for a single line.
82 struct LineCoverageInfo {
83 uint64_t ExecutionCount;
84 unsigned RegionCount;
85 bool Mapped;
86
87 LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
88
89 bool isMapped() const { return Mapped; }
90
91 bool hasMultipleRegions() const { return RegionCount > 1; }
92
93 void addRegionStartCount(uint64_t Count) {
94 Mapped = true;
95 ExecutionCount = Count;
96 ++RegionCount;
97 }
98
99 void addRegionCount(uint64_t Count) {
100 Mapped = true;
Justin Bognerfe357c02014-09-17 18:23:47 +0000101 if (!RegionCount)
102 ExecutionCount = Count;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000103 }
104 };
105
Alex Lorenze82d89c2014-08-22 22:56:03 +0000106 const MemoryBuffer &File;
107 const CoverageViewOptions &Options;
Justin Bognerfe357c02014-09-17 18:23:47 +0000108 std::unique_ptr<SourceCoverageDataManager> RegionManager;
Justin Bogner5e1400a2014-09-17 05:33:20 +0000109 std::vector<ExpansionView> ExpansionSubViews;
110 std::vector<InstantiationView> InstantiationSubViews;
Alex Lorenze82d89c2014-08-22 22:56:03 +0000111
Alex Lorenze82d89c2014-08-22 22:56:03 +0000112 /// \brief Render a source line with highlighting.
Justin Bognerfe357c02014-09-17 18:23:47 +0000113 void renderLine(raw_ostream &OS, StringRef Line, int64_t LineNumber,
114 const CoverageSegment *WrappedSegment,
115 ArrayRef<const CoverageSegment *> Segments,
116 unsigned ExpansionCol);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000117
Justin Bogner76e251c2014-09-16 06:21:57 +0000118 void renderIndent(raw_ostream &OS, unsigned Level);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000119
120 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
121
122 /// \brief Render the line's execution count column.
123 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
124
125 /// \brief Render the line number column.
126 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
127
128 /// \brief Render all the region's execution counts on a line.
Justin Bognerfe357c02014-09-17 18:23:47 +0000129 void renderRegionMarkers(raw_ostream &OS,
130 ArrayRef<const CoverageSegment *> Segments);
Alex Lorenze82d89c2014-08-22 22:56:03 +0000131
132 static const unsigned LineCoverageColumnWidth = 7;
133 static const unsigned LineNumberColumnWidth = 5;
134
135public:
136 SourceCoverageView(const MemoryBuffer &File,
137 const CoverageViewOptions &Options)
Justin Bognerfe357c02014-09-17 18:23:47 +0000138 : File(File), Options(Options) {}
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 /// \brief Load the coverage information required for rendering
159 /// from the mapping regions in the data manager.
Justin Bognerfe357c02014-09-17 18:23:47 +0000160 void load(std::unique_ptr<SourceCoverageDataManager> Data) {
161 RegionManager = std::move(Data);
162 }
Alex Lorenze82d89c2014-08-22 22:56:03 +0000163};
164
165} // namespace llvm
166
167#endif // LLVM_COV_SOURCECOVERAGEVIEW_H