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;
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 Initialize the visible source range for this view.
123 void setUpVisibleRange(SourceCoverageDataManager &Data);
125 /// \brief Create the line coverage information using the coverage data.
126 void createLineCoverageInfo(SourceCoverageDataManager &Data);
128 /// \brief Create the line highlighting ranges using the coverage data.
129 void createHighlightRanges(SourceCoverageDataManager &Data);
131 /// \brief Create the region markers using the coverage data.
132 void createRegionMarkers(SourceCoverageDataManager &Data);
134 /// \brief Sort children by the starting location.
137 /// \brief Return a highlight range for the expansion region of this view.
138 HighlightRange getExpansionHighlightRange() const;
140 /// \brief Render a source line with highlighting.
141 void renderLine(raw_ostream &OS, StringRef Line,
142 ArrayRef<HighlightRange> Ranges);
144 void renderIndent(raw_ostream &OS, unsigned Level);
146 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
148 /// \brief Render the line's execution count column.
149 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
151 /// \brief Render the line number column.
152 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
154 /// \brief Render all the region's execution counts on a line.
155 void renderRegionMarkers(raw_ostream &OS, ArrayRef<RegionMarker> Regions);
157 static const unsigned LineCoverageColumnWidth = 7;
158 static const unsigned LineNumberColumnWidth = 5;
161 SourceCoverageView(const MemoryBuffer &File,
162 const CoverageViewOptions &Options)
163 : File(File), Options(Options), LineOffset(0), Kind(View),
164 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0) {}
166 SourceCoverageView(SourceCoverageView &Parent, StringRef FunctionName)
167 : File(Parent.File), Options(Parent.Options), LineOffset(0),
168 Kind(InstantiationView),
169 ExpansionRegion(coverage::Counter(), 0, 0, 0, 0, 0),
170 FunctionName(FunctionName) {}
172 SourceCoverageView(const MemoryBuffer &File,
173 const CoverageViewOptions &Options,
174 const coverage::CounterMappingRegion &ExpansionRegion)
175 : File(File), Options(Options), LineOffset(0), Kind(ExpansionView),
176 ExpansionRegion(ExpansionRegion) {}
178 const CoverageViewOptions &getOptions() const { return Options; }
180 bool isExpansionSubView() const { return Kind == ExpansionView; }
182 bool isInstantiationSubView() const { return Kind == InstantiationView; }
184 /// \brief Return the line number after which the subview expansion is shown.
185 unsigned getSubViewsExpansionLine() const {
186 return ExpansionRegion.LineStart;
189 void addChild(std::unique_ptr<SourceCoverageView> View) {
190 Children.push_back(std::move(View));
193 /// \brief Print the code coverage information for a specific
194 /// portion of a source file to the output stream.
195 void render(raw_ostream &OS, unsigned IndentLevel = 0);
197 /// \brief Load the coverage information required for rendering
198 /// from the mapping regions in the data manager.
199 void load(SourceCoverageDataManager &Data);
204 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H