SourceCoverageView.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //===- 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. /// \file This class implements rendering for code coverage of source code.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_COV_SOURCECOVERAGEVIEW_H
  14. #define LLVM_COV_SOURCECOVERAGEVIEW_H
  15. #include "CoverageViewOptions.h"
  16. #include "llvm/ProfileData/Coverage/CoverageMapping.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include <vector>
  20. namespace llvm {
  21. class SourceCoverageView;
  22. /// \brief A view that represents a macro or include expansion.
  23. struct ExpansionView {
  24. coverage::CounterMappingRegion Region;
  25. std::unique_ptr<SourceCoverageView> View;
  26. ExpansionView(const coverage::CounterMappingRegion &Region,
  27. std::unique_ptr<SourceCoverageView> View)
  28. : Region(Region), View(std::move(View)) {}
  29. ExpansionView(ExpansionView &&RHS)
  30. : Region(std::move(RHS.Region)), View(std::move(RHS.View)) {}
  31. ExpansionView &operator=(ExpansionView &&RHS) {
  32. Region = std::move(RHS.Region);
  33. View = std::move(RHS.View);
  34. return *this;
  35. }
  36. unsigned getLine() const { return Region.LineStart; }
  37. unsigned getStartCol() const { return Region.ColumnStart; }
  38. unsigned getEndCol() const { return Region.ColumnEnd; }
  39. friend bool operator<(const ExpansionView &LHS, const ExpansionView &RHS) {
  40. return LHS.Region.startLoc() < RHS.Region.startLoc();
  41. }
  42. };
  43. /// \brief A view that represents a function instantiation.
  44. struct InstantiationView {
  45. StringRef FunctionName;
  46. unsigned Line;
  47. std::unique_ptr<SourceCoverageView> View;
  48. InstantiationView(StringRef FunctionName, unsigned Line,
  49. std::unique_ptr<SourceCoverageView> View)
  50. : FunctionName(FunctionName), Line(Line), View(std::move(View)) {}
  51. InstantiationView(InstantiationView &&RHS)
  52. : FunctionName(std::move(RHS.FunctionName)), Line(std::move(RHS.Line)),
  53. View(std::move(RHS.View)) {}
  54. InstantiationView &operator=(InstantiationView &&RHS) {
  55. FunctionName = std::move(RHS.FunctionName);
  56. Line = std::move(RHS.Line);
  57. View = std::move(RHS.View);
  58. return *this;
  59. }
  60. friend bool operator<(const InstantiationView &LHS,
  61. const InstantiationView &RHS) {
  62. return LHS.Line < RHS.Line;
  63. }
  64. };
  65. /// \brief Coverage statistics for a single line.
  66. struct LineCoverageStats {
  67. uint64_t ExecutionCount;
  68. unsigned RegionCount;
  69. bool Mapped;
  70. LineCoverageStats() : ExecutionCount(0), RegionCount(0), Mapped(false) {}
  71. bool isMapped() const { return Mapped; }
  72. bool hasMultipleRegions() const { return RegionCount > 1; }
  73. void addRegionStartCount(uint64_t Count) {
  74. // The max of all region starts is the most interesting value.
  75. addRegionCount(RegionCount ? std::max(ExecutionCount, Count) : Count);
  76. ++RegionCount;
  77. }
  78. void addRegionCount(uint64_t Count) {
  79. Mapped = true;
  80. ExecutionCount = Count;
  81. }
  82. };
  83. /// \brief A code coverage view of a source file or function.
  84. ///
  85. /// A source coverage view and its nested sub-views form a file-oriented
  86. /// representation of code coverage data. This view can be printed out by a
  87. /// renderer which implements the Rendering Interface.
  88. class SourceCoverageView {
  89. /// A function or file name.
  90. StringRef SourceName;
  91. /// A memory buffer backing the source on display.
  92. const MemoryBuffer &File;
  93. /// Various options to guide the coverage renderer.
  94. const CoverageViewOptions &Options;
  95. /// Complete coverage information about the source on display.
  96. coverage::CoverageData CoverageInfo;
  97. /// A container for all expansions (e.g macros) in the source on display.
  98. std::vector<ExpansionView> ExpansionSubViews;
  99. /// A container for all instantiations (e.g template functions) in the source
  100. /// on display.
  101. std::vector<InstantiationView> InstantiationSubViews;
  102. protected:
  103. struct LineRef {
  104. StringRef Line;
  105. int64_t LineNo;
  106. LineRef(StringRef Line, int64_t LineNo) : Line(Line), LineNo(LineNo) {}
  107. };
  108. using CoverageSegmentArray = ArrayRef<const coverage::CoverageSegment *>;
  109. /// @name Rendering Interface
  110. /// @{
  111. /// \brief Render the source name for the view.
  112. virtual void renderSourceName(raw_ostream &OS) = 0;
  113. /// \brief Render the line prefix at the given \p ViewDepth.
  114. virtual void renderLinePrefix(raw_ostream &OS, unsigned ViewDepth) = 0;
  115. /// \brief Render a view divider at the given \p ViewDepth.
  116. virtual void renderViewDivider(raw_ostream &OS, unsigned ViewDepth) = 0;
  117. /// \brief Render a source line with highlighting.
  118. virtual void renderLine(raw_ostream &OS, LineRef L,
  119. const coverage::CoverageSegment *WrappedSegment,
  120. CoverageSegmentArray Segments, unsigned ExpansionCol,
  121. unsigned ViewDepth) = 0;
  122. /// \brief Render the line's execution count column.
  123. virtual void renderLineCoverageColumn(raw_ostream &OS,
  124. const LineCoverageStats &Line) = 0;
  125. /// \brief Render the line number column.
  126. virtual void renderLineNumberColumn(raw_ostream &OS, unsigned LineNo) = 0;
  127. /// \brief Render all the region's execution counts on a line.
  128. virtual void renderRegionMarkers(raw_ostream &OS,
  129. CoverageSegmentArray Segments,
  130. unsigned ViewDepth) = 0;
  131. /// \brief Render an expansion view. If \p FirstLine is provided, it points
  132. /// to the expansion site, which must be re-rendered for clarity.
  133. virtual unsigned renderExpansionView(
  134. raw_ostream &OS, ExpansionView &ESV, Optional<LineRef> FirstLine,
  135. unsigned ExpansionCol, const coverage::CoverageSegment *WrappedSegment,
  136. CoverageSegmentArray LineSegments, unsigned ViewDepth) = 0;
  137. /// \brief Render an instantiation view.
  138. virtual void renderInstantiationView(raw_ostream &OS, InstantiationView &ISV,
  139. unsigned ViewDepth) = 0;
  140. /// @}
  141. /// \brief Format a count using engineering notation with 3 significant
  142. /// digits.
  143. static std::string formatCount(uint64_t N);
  144. SourceCoverageView(StringRef SourceName, const MemoryBuffer &File,
  145. const CoverageViewOptions &Options,
  146. coverage::CoverageData &&CoverageInfo)
  147. : SourceName(SourceName), File(File), Options(Options),
  148. CoverageInfo(std::move(CoverageInfo)) {}
  149. public:
  150. static std::unique_ptr<SourceCoverageView>
  151. create(StringRef SourceName, const MemoryBuffer &File,
  152. const CoverageViewOptions &Options,
  153. coverage::CoverageData &&CoverageInfo);
  154. virtual ~SourceCoverageView() {}
  155. StringRef getSourceName() const { return SourceName; }
  156. const CoverageViewOptions &getOptions() const { return Options; }
  157. /// \brief Add an expansion subview to this view.
  158. void addExpansion(const coverage::CounterMappingRegion &Region,
  159. std::unique_ptr<SourceCoverageView> View);
  160. /// \brief Add a function instantiation subview to this view.
  161. void addInstantiation(StringRef FunctionName, unsigned Line,
  162. std::unique_ptr<SourceCoverageView> View);
  163. /// \brief Print the code coverage information for a specific portion of a
  164. /// source file to the output stream.
  165. void print(raw_ostream &OS, bool WholeFile, bool ShowSourceName,
  166. unsigned ViewDepth = 0);
  167. };
  168. } // namespace llvm
  169. #endif // LLVM_COV_SOURCECOVERAGEVIEW_H