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