Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 1 | //===- GCOVr.cpp - LLVM coverage tool -------------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 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 | // |
Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 10 | // GCOV implements the interface to read and write coverage files that use |
| 11 | // 'gcov' format. |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 12 | // |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 15 | #include "llvm/Support/GCOV.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/OwningPtr.h" |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Format.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryObject.h" |
| 20 | #include "llvm/Support/system_error.h" |
| 21 | using namespace llvm; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // GCOVFile implementation. |
| 25 | |
| 26 | /// ~GCOVFile - Delete GCOVFile and its content. |
| 27 | GCOVFile::~GCOVFile() { |
| 28 | DeleteContainerPointers(Functions); |
| 29 | } |
| 30 | |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 31 | /// isGCDAFile - Return true if Format identifies a .gcda file. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 32 | static bool isGCDAFile(GCOV::GCOVFormat Format) { |
| 33 | return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /// isGCNOFile - Return true if Format identifies a .gcno file. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 37 | static bool isGCNOFile(GCOV::GCOVFormat Format) { |
| 38 | return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 41 | /// read - Read GCOV buffer. |
| 42 | bool GCOVFile::read(GCOVBuffer &Buffer) { |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 43 | GCOV::GCOVFormat Format = Buffer.readGCOVFormat(); |
| 44 | if (Format == GCOV::InvalidGCOV) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 45 | return false; |
| 46 | |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame^] | 47 | if (isGCNOFile(Format)) { |
| 48 | while (true) { |
| 49 | GCOVFunction *GFun = new GCOVFunction(); |
| 50 | if (!GFun->read(Buffer, Format)) |
| 51 | break; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 52 | Functions.push_back(GFun); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 53 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 54 | } |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame^] | 55 | else if (isGCDAFile(Format)) { |
| 56 | for (size_t i = 0, e = Functions.size(); i < e; ++i) { |
| 57 | bool ReadGCDA = Functions[i]->read(Buffer, Format); |
| 58 | (void)ReadGCDA; |
| 59 | assert(ReadGCDA && ".gcda data does not match .gcno data"); |
| 60 | } |
| 61 | while (Buffer.readProgramTag()) |
| 62 | ++ProgramCount; |
| 63 | } |
| 64 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | /// dump - Dump GCOVFile content on standard out for debugging purposes. |
| 69 | void GCOVFile::dump() { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 70 | for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 71 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 72 | (*I)->dump(); |
| 73 | } |
| 74 | |
| 75 | /// collectLineCounts - Collect line counts. This must be used after |
| 76 | /// reading .gcno and .gcda files. |
| 77 | void GCOVFile::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 78 | for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 79 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 80 | (*I)->collectLineCounts(FI); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame^] | 81 | FI.setProgramCount(ProgramCount); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //===----------------------------------------------------------------------===// |
| 85 | // GCOVFunction implementation. |
| 86 | |
| 87 | /// ~GCOVFunction - Delete GCOVFunction and its content. |
| 88 | GCOVFunction::~GCOVFunction() { |
| 89 | DeleteContainerPointers(Blocks); |
| 90 | } |
| 91 | |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 92 | /// read - Read a function from the buffer. Return false if buffer cursor |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 93 | /// does not point to a function tag. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 94 | bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 95 | if (!Buff.readFunctionTag()) |
| 96 | return false; |
| 97 | |
| 98 | Buff.readInt(); // Function header length |
| 99 | Ident = Buff.readInt(); |
| 100 | Buff.readInt(); // Checksum #1 |
Bill Wendling | c432503 | 2013-06-25 18:13:52 +0000 | [diff] [blame] | 101 | if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 102 | Buff.readInt(); // Checksum #2 |
| 103 | |
| 104 | Name = Buff.readString(); |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 105 | if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 106 | Filename = Buff.readString(); |
| 107 | |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 108 | if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 109 | Buff.readArcTag(); |
Yuchen Wu | 887c20f | 2013-10-24 01:51:04 +0000 | [diff] [blame] | 110 | uint32_t i = 0; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 111 | uint32_t Count = Buff.readInt() / 2; |
Yuchen Wu | 887c20f | 2013-10-24 01:51:04 +0000 | [diff] [blame] | 112 | |
| 113 | // This for loop adds the counts for each block. A second nested loop is |
| 114 | // required to combine the edge counts that are contained in the GCDA file. |
| 115 | for (uint32_t Line = 0; i < Count; ++Line) { |
| 116 | GCOVBlock &Block = *Blocks[Line]; |
| 117 | for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) { |
| 118 | assert(i < Count && "Unexpected number of Edges!"); |
| 119 | Block.addCount(Buff.readInt64()); |
| 120 | ++i; |
| 121 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 122 | } |
Chad Rosier | 5dfe6da | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 123 | return true; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | LineNumber = Buff.readInt(); |
| 127 | |
| 128 | // read blocks. |
Nick Lewycky | c6b4f03 | 2012-09-23 03:58:21 +0000 | [diff] [blame] | 129 | bool BlockTagFound = Buff.readBlockTag(); |
| 130 | (void)BlockTagFound; |
| 131 | assert(BlockTagFound && "Block Tag not found!"); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 132 | uint32_t BlockCount = Buff.readInt(); |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 133 | for (uint32_t i = 0, e = BlockCount; i != e; ++i) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 134 | Buff.readInt(); // Block flags; |
| 135 | Blocks.push_back(new GCOVBlock(i)); |
| 136 | } |
| 137 | |
| 138 | // read edges. |
| 139 | while (Buff.readEdgeTag()) { |
| 140 | uint32_t EdgeCount = (Buff.readInt() - 1) / 2; |
| 141 | uint32_t BlockNo = Buff.readInt(); |
Nick Lewycky | c6b4f03 | 2012-09-23 03:58:21 +0000 | [diff] [blame] | 142 | assert(BlockNo < BlockCount && "Unexpected Block number!"); |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 143 | for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 144 | Blocks[BlockNo]->addEdge(Buff.readInt()); |
| 145 | Buff.readInt(); // Edge flag |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // read line table. |
| 150 | while (Buff.readLineTag()) { |
| 151 | uint32_t LineTableLength = Buff.readInt(); |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 152 | uint32_t EndPos = Buff.getCursor() + LineTableLength*4; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 153 | uint32_t BlockNo = Buff.readInt(); |
Nick Lewycky | c6b4f03 | 2012-09-23 03:58:21 +0000 | [diff] [blame] | 154 | assert(BlockNo < BlockCount && "Unexpected Block number!"); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 155 | GCOVBlock *Block = Blocks[BlockNo]; |
| 156 | Buff.readInt(); // flag |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 157 | while (Buff.getCursor() != (EndPos - 4)) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 158 | StringRef Filename = Buff.readString(); |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 159 | if (Buff.getCursor() == (EndPos - 4)) break; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 160 | while (uint32_t L = Buff.readInt()) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 161 | Block->addLine(Filename, L); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 162 | } |
| 163 | Buff.readInt(); // flag |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | /// dump - Dump GCOVFunction content on standard out for debugging purposes. |
| 169 | void GCOVFunction::dump() { |
| 170 | outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 171 | for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 172 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 173 | (*I)->dump(); |
| 174 | } |
| 175 | |
| 176 | /// collectLineCounts - Collect line counts. This must be used after |
| 177 | /// reading .gcno and .gcda files. |
| 178 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 179 | for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 180 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 181 | (*I)->collectLineCounts(FI); |
| 182 | } |
| 183 | |
| 184 | //===----------------------------------------------------------------------===// |
| 185 | // GCOVBlock implementation. |
| 186 | |
| 187 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 188 | GCOVBlock::~GCOVBlock() { |
| 189 | Edges.clear(); |
| 190 | DeleteContainerSeconds(Lines); |
| 191 | } |
| 192 | |
| 193 | void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) { |
| 194 | GCOVLines *&LinesForFile = Lines[Filename]; |
| 195 | if (!LinesForFile) |
| 196 | LinesForFile = new GCOVLines(); |
| 197 | LinesForFile->add(LineNo); |
| 198 | } |
| 199 | |
| 200 | /// collectLineCounts - Collect line counts. This must be used after |
| 201 | /// reading .gcno and .gcda files. |
| 202 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
| 203 | for (StringMap<GCOVLines *>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 204 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 205 | I->second->collectLineCounts(FI, I->first(), Counter); |
| 206 | } |
| 207 | |
| 208 | /// dump - Dump GCOVBlock content on standard out for debugging purposes. |
| 209 | void GCOVBlock::dump() { |
| 210 | outs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
| 211 | if (!Edges.empty()) { |
| 212 | outs() << "\tEdges : "; |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 213 | for (SmallVectorImpl<uint32_t>::iterator I = Edges.begin(), E = Edges.end(); |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 214 | I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 215 | outs() << (*I) << ","; |
| 216 | outs() << "\n"; |
| 217 | } |
| 218 | if (!Lines.empty()) { |
| 219 | outs() << "\tLines : "; |
| 220 | for (StringMap<GCOVLines *>::iterator LI = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 221 | LE = Lines.end(); LI != LE; ++LI) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 222 | outs() << LI->first() << " -> "; |
| 223 | LI->second->dump(); |
| 224 | outs() << "\n"; |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | //===----------------------------------------------------------------------===// |
| 230 | // GCOVLines implementation. |
| 231 | |
| 232 | /// collectLineCounts - Collect line counts. This must be used after |
| 233 | /// reading .gcno and .gcda files. |
| 234 | void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename, |
Bob Wilson | 68bf30a | 2013-10-22 17:43:47 +0000 | [diff] [blame] | 235 | uint64_t Count) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 236 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 237 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 238 | FI.addLineCount(Filename, *I, Count); |
| 239 | } |
| 240 | |
| 241 | /// dump - Dump GCOVLines content on standard out for debugging purposes. |
| 242 | void GCOVLines::dump() { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 243 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 244 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 245 | outs() << (*I) << ","; |
| 246 | } |
| 247 | |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | // FileInfo implementation. |
| 250 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 251 | /// print - Print source files with collected line count information. |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 252 | void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 253 | for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end(); |
| 254 | I != E; ++I) { |
| 255 | StringRef Filename = I->first(); |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 256 | outs() << " -: 0:Source:" << Filename << "\n"; |
| 257 | outs() << " -: 0:Graph:" << gcnoFile << "\n"; |
| 258 | outs() << " -: 0:Data:" << gcdaFile << "\n"; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame^] | 259 | outs() << " -: 0:Programs:" << ProgramCount << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 260 | LineCounts &L = LineInfo[Filename]; |
| 261 | OwningPtr<MemoryBuffer> Buff; |
| 262 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 263 | errs() << Filename << ": " << ec.message() << "\n"; |
| 264 | return; |
| 265 | } |
| 266 | StringRef AllLines = Buff.take()->getBuffer(); |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 267 | uint32_t i = 0; |
| 268 | while (!AllLines.empty()) { |
| 269 | if (L.find(i) != L.end()) { |
| 270 | if (L[i] == 0) |
| 271 | outs() << " #####:"; |
| 272 | else |
| 273 | outs() << format("%9lu:", L[i]); |
| 274 | } else { |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 275 | outs() << " -:"; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 276 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 277 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
| 278 | if (AllLines != P.first) |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 279 | outs() << format("%5u:", i+1) << P.first; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 280 | outs() << "\n"; |
| 281 | AllLines = P.second; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 282 | ++i; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
| 286 | |