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