1 //===- SourceCoverageView.cpp - 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 14 #include "SourceCoverageView.h" 15 #include "SourceCoverageViewText.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/ADT/StringExtras.h" 18 #include "llvm/Support/FileSystem.h" 19 #include "llvm/Support/LineIterator.h" 20 #include "llvm/Support/Path.h" 21 22 using namespace llvm; 23 24 void CoveragePrinter::StreamDestructor::operator()(raw_ostream *OS) const { 25 if (OS == &outs()) 26 return; 27 delete OS; 28 } 29 30 std::string CoveragePrinter::getOutputPath(StringRef Path, StringRef Extension, 31 bool InToplevel) { 32 assert(Extension.size() && "The file extension may not be empty"); 33 34 SmallString<256> FullPath(Opts.ShowOutputDirectory); 35 if (!InToplevel) 36 sys::path::append(FullPath, getCoverageDir()); 37 38 auto PathBaseDir = sys::path::relative_path(sys::path::parent_path(Path)); 39 sys::path::append(FullPath, PathBaseDir); 40 41 auto PathFilename = (sys::path::filename(Path) + "." + Extension).str(); 42 sys::path::append(FullPath, PathFilename); 43 44 return FullPath.str(); 45 } 46 47 Expected<CoveragePrinter::OwnedStream> 48 CoveragePrinter::createOutputStream(StringRef Path, StringRef Extension, 49 bool InToplevel) { 50 if (!Opts.hasOutputDirectory()) 51 return OwnedStream(&outs()); 52 53 std::string FullPath = getOutputPath(Path, Extension, InToplevel); 54 55 auto ParentDir = sys::path::parent_path(FullPath); 56 if (auto E = sys::fs::create_directories(ParentDir)) 57 return errorCodeToError(E); 58 59 std::error_code E; 60 raw_ostream *RawStream = new raw_fd_ostream(FullPath, E, sys::fs::F_RW); 61 auto OS = CoveragePrinter::OwnedStream(RawStream); 62 if (E) 63 return errorCodeToError(E); 64 return std::move(OS); 65 } 66 67 std::unique_ptr<CoveragePrinter> 68 CoveragePrinter::create(const CoverageViewOptions &Opts) { 69 switch (Opts.Format) { 70 case CoverageViewOptions::OutputFormat::Text: 71 return llvm::make_unique<CoveragePrinterText>(Opts); 72 } 73 llvm_unreachable("Unknown coverage output format!"); 74 } 75 76 std::string SourceCoverageView::formatCount(uint64_t N) { 77 std::string Number = utostr(N); 78 int Len = Number.size(); 79 if (Len <= 3) 80 return Number; 81 int IntLen = Len % 3 == 0 ? 3 : Len % 3; 82 std::string Result(Number.data(), IntLen); 83 if (IntLen != 3) { 84 Result.push_back('.'); 85 Result += Number.substr(IntLen, 3 - IntLen); 86 } 87 Result.push_back(" kMGTPEZY"[(Len - 1) / 3]); 88 return Result; 89 } 90 91 std::unique_ptr<SourceCoverageView> 92 SourceCoverageView::create(StringRef SourceName, const MemoryBuffer &File, 93 const CoverageViewOptions &Options, 94 coverage::CoverageData &&CoverageInfo) { 95 switch (Options.Format) { 96 case CoverageViewOptions::OutputFormat::Text: 97 return llvm::make_unique<SourceCoverageViewText>(SourceName, File, Options, 98 std::move(CoverageInfo)); 99 } 100 llvm_unreachable("Unknown coverage output format!"); 101 } 102 103 void SourceCoverageView::addExpansion( 104 const coverage::CounterMappingRegion &Region, 105 std::unique_ptr<SourceCoverageView> View) { 106 ExpansionSubViews.emplace_back(Region, std::move(View)); 107 } 108 109 void SourceCoverageView::addInstantiation( 110 StringRef FunctionName, unsigned Line, 111 std::unique_ptr<SourceCoverageView> View) { 112 InstantiationSubViews.emplace_back(FunctionName, Line, std::move(View)); 113 } 114 115 void SourceCoverageView::print(raw_ostream &OS, bool WholeFile, 116 bool ShowSourceName, unsigned ViewDepth) { 117 if (ShowSourceName) 118 renderSourceName(OS); 119 120 // We need the expansions and instantiations sorted so we can go through them 121 // while we iterate lines. 122 std::sort(ExpansionSubViews.begin(), ExpansionSubViews.end()); 123 std::sort(InstantiationSubViews.begin(), InstantiationSubViews.end()); 124 auto NextESV = ExpansionSubViews.begin(); 125 auto EndESV = ExpansionSubViews.end(); 126 auto NextISV = InstantiationSubViews.begin(); 127 auto EndISV = InstantiationSubViews.end(); 128 129 // Get the coverage information for the file. 130 auto NextSegment = CoverageInfo.begin(); 131 auto EndSegment = CoverageInfo.end(); 132 133 unsigned FirstLine = NextSegment != EndSegment ? NextSegment->Line : 0; 134 const coverage::CoverageSegment *WrappedSegment = nullptr; 135 SmallVector<const coverage::CoverageSegment *, 8> LineSegments; 136 for (line_iterator LI(File, /*SkipBlanks=*/false); !LI.is_at_eof(); ++LI) { 137 // If we aren't rendering the whole file, we need to filter out the prologue 138 // and epilogue. 139 if (!WholeFile) { 140 if (NextSegment == EndSegment) 141 break; 142 else if (LI.line_number() < FirstLine) 143 continue; 144 } 145 146 // Collect the coverage information relevant to this line. 147 if (LineSegments.size()) 148 WrappedSegment = LineSegments.back(); 149 LineSegments.clear(); 150 while (NextSegment != EndSegment && NextSegment->Line == LI.line_number()) 151 LineSegments.push_back(&*NextSegment++); 152 153 // Calculate a count to be for the line as a whole. 154 LineCoverageStats LineCount; 155 if (WrappedSegment && WrappedSegment->HasCount) 156 LineCount.addRegionCount(WrappedSegment->Count); 157 for (const auto *S : LineSegments) 158 if (S->HasCount && S->IsRegionEntry) 159 LineCount.addRegionStartCount(S->Count); 160 161 renderLinePrefix(OS, ViewDepth); 162 if (getOptions().ShowLineStats) 163 renderLineCoverageColumn(OS, LineCount); 164 if (getOptions().ShowLineNumbers) 165 renderLineNumberColumn(OS, LI.line_number()); 166 167 // If there are expansion subviews, we want to highlight the first one. 168 unsigned ExpansionColumn = 0; 169 if (NextESV != EndESV && NextESV->getLine() == LI.line_number() && 170 getOptions().Colors) 171 ExpansionColumn = NextESV->getStartCol(); 172 173 // Display the source code for the current line. 174 renderLine(OS, {*LI, LI.line_number()}, WrappedSegment, LineSegments, 175 ExpansionColumn, ViewDepth); 176 177 // Show the region markers. 178 if (getOptions().ShowRegionMarkers && 179 (!getOptions().ShowLineStatsOrRegionMarkers || 180 LineCount.hasMultipleRegions()) && 181 !LineSegments.empty()) { 182 renderRegionMarkers(OS, LineSegments, ViewDepth); 183 } 184 185 // Show the expansions and instantiations for this line. 186 bool RenderedSubView = false; 187 for (; NextESV != EndESV && NextESV->getLine() == LI.line_number(); 188 ++NextESV) { 189 renderViewDivider(OS, ViewDepth + 1); 190 191 // Re-render the current line and highlight the expansion range for 192 // this subview. 193 if (RenderedSubView) { 194 ExpansionColumn = NextESV->getStartCol(); 195 renderExpansionSite( 196 OS, *NextESV, {*LI, LI.line_number()}, WrappedSegment, LineSegments, 197 ExpansionColumn, ViewDepth); 198 renderViewDivider(OS, ViewDepth + 1); 199 } 200 201 renderExpansionView(OS, *NextESV, ViewDepth + 1); 202 RenderedSubView = true; 203 } 204 for (; NextISV != EndISV && NextISV->Line == LI.line_number(); ++NextISV) { 205 renderViewDivider(OS, ViewDepth + 1); 206 renderInstantiationView(OS, *NextISV, ViewDepth + 1); 207 RenderedSubView = true; 208 } 209 if (RenderedSubView) 210 renderViewDivider(OS, ViewDepth + 1); 211 } 212 } 213