1 //===- SourceCoverageView.h - Code coverage view for source code ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements rendering for code coverage of source code.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
15 #define LLVM_COV_SOURCECOVERAGEVIEW_H
17 #include "CoverageViewOptions.h"
18 #include "SourceCoverageDataManager.h"
19 #include "llvm/ProfileData/CoverageMapping.h"
20 #include "llvm/Support/MemoryBuffer.h"
25 /// \brief A code coverage view of a specific source file.
26 /// It can have embedded coverage views.
27 class SourceCoverageView {
29 enum SubViewKind { View, ExpansionView, InstantiationView };
31 /// \brief Coverage information for a single line.
32 struct LineCoverageInfo {
33 uint64_t ExecutionCount;
37 LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
39 bool isMapped() const { return Mapped; }
41 bool hasMultipleRegions() const { return RegionCount > 1; }
43 void addRegionStartCount(uint64_t Count) {
45 ExecutionCount = Count;
49 void addRegionCount(uint64_t Count) {
51 ExecutionCount = Count;
55 /// \brief A marker that points at the start
56 /// of a specific mapping region.
58 unsigned Line, Column;
59 uint64_t ExecutionCount;
61 RegionMarker(unsigned Line, unsigned Column, uint64_t Value)
62 : Line(Line), Column(Column), ExecutionCount(Value) {}
65 /// \brief A single line source range used to
66 /// render highlighted text.
67 struct HighlightRange {
69 /// The code that wasn't executed.
72 /// The region of code that was expanded.
80 HighlightRange(unsigned Line, unsigned ColumnStart, unsigned ColumnEnd,
81 HighlightKind Kind = NotCovered)
82 : Kind(Kind), Line(Line), ColumnStart(ColumnStart),
83 ColumnEnd(ColumnEnd) {}
85 bool operator<(const HighlightRange &Other) const {
86 if (Line == Other.Line)
87 return ColumnStart < Other.ColumnStart;
88 return Line < Other.Line;
91 bool columnStartOverlaps(const HighlightRange &Other) const {
92 return ColumnStart <= Other.ColumnStart && ColumnEnd > Other.ColumnStart;
94 bool columnEndOverlaps(const HighlightRange &Other) const {
95 return ColumnEnd >= Other.ColumnEnd && ColumnStart < Other.ColumnEnd;
97 bool contains(const HighlightRange &Other) const {
98 if (Line != Other.Line)
100 return ColumnStart <= Other.ColumnStart && ColumnEnd >= Other.ColumnEnd;
103 bool overlaps(const HighlightRange &Other) const {
104 if (Line != Other.Line)
106 return columnStartOverlaps(Other) || columnEndOverlaps(Other);
111 const MemoryBuffer &File;
112 const CoverageViewOptions &Options;
113 unsigned LineStart, LineCount;
115 coverage::CounterMappingRegion ExpansionRegion;
116 std::vector<std::unique_ptr<SourceCoverageView>> Children;
117 std::vector<LineCoverageInfo> LineStats;
118 std::vector<HighlightRange> HighlightRanges;
119 std::vector<RegionMarker> Markers;
120 StringRef FunctionName;
122 /// \brief Create the line coverage information using the coverage data.
123 void createLineCoverageInfo(SourceCoverageDataManager &Data);
125 /// \brief Create the line highlighting ranges using the coverage data.
126 void createHighlightRanges(SourceCoverageDataManager &Data);
128 /// \brief Create the region markers using the coverage data.
129 void createRegionMarkers(SourceCoverageDataManager &Data);
131 /// \brief Sort children by the starting location.
134 /// \brief Return a highlight range for the expansion region of this view.
135 HighlightRange getExpansionHighlightRange() const;
137 /// \brief Render a source line with highlighting.
138 void renderLine(raw_ostream &OS, StringRef Line,
139 ArrayRef<HighlightRange> Ranges);
141 void renderOffset(raw_ostream &OS, unsigned I);
143 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
145 /// \brief Render the line's execution count column.
146 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
148 /// \brief Render the line number column.
149 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
151 /// \brief Render all the region's execution counts on a line.
152 void renderRegionMarkers(raw_ostream &OS, ArrayRef<RegionMarker> Regions);
154 static const unsigned LineCoverageColumnWidth = 7;
155 static const unsigned LineNumberColumnWidth = 5;
158 SourceCoverageView(const MemoryBuffer &File,
159 const CoverageViewOptions &Options)
160 : File(File), Options(Options), LineStart(1), Kind(View),
161 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {
162 LineCount = File.getBuffer().count('\n') + 1;
165 SourceCoverageView(const MemoryBuffer &File,
166 const CoverageViewOptions &Options, unsigned LineStart,
168 : File(File), Options(Options), LineStart(LineStart),
169 LineCount(LineEnd - LineStart + 1), Kind(View),
170 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {}
172 SourceCoverageView(SourceCoverageView &Parent, unsigned LineStart,
173 unsigned LineEnd, StringRef FunctionName)
174 : File(Parent.File), Options(Parent.Options), LineStart(LineStart),
175 LineCount(LineEnd - LineStart + 1), Kind(InstantiationView),
176 ExpansionRegion(coverage::Counter(), 0, LineEnd, 0, LineEnd, 0),
177 FunctionName(FunctionName) {}
179 SourceCoverageView(const MemoryBuffer &File,
180 const CoverageViewOptions &Options, unsigned LineStart,
182 const coverage::CounterMappingRegion &ExpansionRegion)
183 : File(File), Options(Options), LineStart(LineStart),
184 LineCount(LineEnd - LineStart + 1), Kind(ExpansionView),
185 ExpansionRegion(ExpansionRegion) {}
187 const CoverageViewOptions &getOptions() const { return Options; }
189 bool isExpansionSubView() const { return Kind == ExpansionView; }
191 bool isInstantiationSubView() const { return Kind == InstantiationView; }
193 /// \brief Return the line number after which the subview expansion is shown.
194 unsigned getSubViewsExpansionLine() const {
195 return ExpansionRegion.LineStart;
198 void addChild(std::unique_ptr<SourceCoverageView> View) {
199 Children.push_back(std::move(View));
202 /// \brief Print the code coverage information for a specific
203 /// portion of a source file to the output stream.
204 void render(raw_ostream &OS, unsigned Offset = 0);
206 /// \brief Load the coverage information required for rendering
207 /// from the mapping regions in the data manager.
208 void load(SourceCoverageDataManager &Data);
213 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H