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 "llvm/ProfileData/CoverageMapping.h"
19 #include "llvm/Support/MemoryBuffer.h"
24 class SourceCoverageView;
26 /// \brief A view that represents a macro or include expansion
27 struct ExpansionView {
28 coverage::CounterMappingRegion Region;
29 std::unique_ptr<SourceCoverageView> View;
31 ExpansionView(const coverage::CounterMappingRegion &Region,
32 std::unique_ptr<SourceCoverageView> View)
33 : Region(Region), View(std::move(View)) {}
34 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);
42 unsigned getLine() const { return Region.LineStart; }
43 unsigned getStartCol() const { return Region.ColumnStart; }
44 unsigned getEndCol() const { return Region.ColumnEnd; }
46 friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
47 return LHS.Region.startLoc() < RHS.Region.startLoc();
51 /// \brief A view that represents a function instantiation
52 struct InstantiationView {
53 StringRef FunctionName;
55 std::unique_ptr<SourceCoverageView> View;
57 InstantiationView(StringRef FunctionName, unsigned Line,
58 std::unique_ptr<SourceCoverageView> View)
59 : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
60 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);
70 friend bool operator<(const InstantiationView &LHS,
71 const InstantiationView &RHS) {
72 return LHS.Line < RHS.Line;
76 /// \brief A code coverage view of a specific source file.
77 /// It can have embedded coverage views.
78 class SourceCoverageView {
80 /// \brief Coverage information for a single line.
81 struct LineCoverageInfo {
82 uint64_t ExecutionCount;
86 LineCoverageInfo() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
88 bool isMapped() const { return Mapped; }
90 bool hasMultipleRegions() const { return RegionCount > 1; }
92 void addRegionStartCount(uint64_t Count) {
93 // The max of all region starts is the most interesting value.
94 addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count);
98 void addRegionCount(uint64_t Count) {
100 ExecutionCount = Count;
104 const MemoryBuffer &File;
105 const CoverageViewOptions &Options;
106 coverage::CoverageData CoverageInfo;
107 std::vector<ExpansionView> ExpansionSubViews;
108 std::vector<InstantiationView> InstantiationSubViews;
110 /// \brief Render a source line with highlighting.
111 void renderLine(raw_ostream &OS, StringRef Line, int64_t LineNumber,
112 const coverage::CoverageSegment *WrappedSegment,
113 ArrayRef<const coverage::CoverageSegment *> Segments,
114 unsigned ExpansionCol);
116 void renderIndent(raw_ostream &OS, unsigned Level);
118 void renderViewDivider(unsigned Offset, unsigned Length, raw_ostream &OS);
120 /// \brief Render the line's execution count column.
121 void renderLineCoverageColumn(raw_ostream &OS, const LineCoverageInfo &Line);
123 /// \brief Render the line number column.
124 void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo);
126 /// \brief Render all the region's execution counts on a line.
128 renderRegionMarkers(raw_ostream &OS,
129 ArrayRef<const coverage::CoverageSegment *> Segments);
131 static const unsigned LineCoverageColumnWidth = 7;
132 static const unsigned LineNumberColumnWidth = 5;
135 SourceCoverageView(const MemoryBuffer &File,
136 const CoverageViewOptions &Options,
137 coverage::CoverageData &&CoverageInfo)
138 : File(File), Options(Options), CoverageInfo(std::move(CoverageInfo)) {}
140 const CoverageViewOptions &getOptions() const { return Options; }
142 /// \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));
148 /// \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));
154 /// \brief Print the code coverage information for a specific
155 /// portion of a source file to the output stream.
156 void render(raw_ostream &OS, bool WholeFile, unsigned IndentLevel = 0);
161 #endif // LLVM_COV_SOURCECOVERAGEVIEW_H