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" |
Devang Patel | 3714065 | 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 | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 30 | /// isGCDAFile - Return true if Format identifies a .gcda file. |
Bill Wendling | 6bbe489 | 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 | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /// isGCNOFile - Return true if Format identifies a .gcno file. |
Bill Wendling | 6bbe489 | 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 | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 40 | /// read - Read GCOV buffer. |
| 41 | bool GCOVFile::read(GCOVBuffer &Buffer) { |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame^] | 42 | GCOV::GCOVFormat Format = Buffer.readGCOVFormat(); |
| 43 | if (Format == GCOV::InvalidGCOV) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 44 | return false; |
| 45 | |
| 46 | unsigned i = 0; |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 47 | while (1) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 48 | GCOVFunction *GFun = NULL; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 49 | if (isGCDAFile(Format)) { |
| 50 | // Use existing function while reading .gcda file. |
| 51 | assert (i < Functions.size() && ".gcda data does not match .gcno data"); |
| 52 | GFun = Functions[i]; |
| 53 | } else if (isGCNOFile(Format)){ |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 54 | GFun = new GCOVFunction(); |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 55 | Functions.push_back(GFun); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 56 | } |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 57 | if (!GFun || !GFun->read(Buffer, Format)) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 58 | break; |
Devang Patel | 3714065 | 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 | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 67 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 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 | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 75 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 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 | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame^] | 90 | bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { |
Devang Patel | 3714065 | 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 | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame^] | 97 | if (Format != GCOV::GCNO_402) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 98 | Buff.readInt(); // Checksum #2 |
| 99 | |
| 100 | Name = Buff.readString(); |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame^] | 101 | if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 102 | Filename = Buff.readString(); |
| 103 | |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame^] | 104 | if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) { |
Devang Patel | 3714065 | 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 | 5dfe6da | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 110 | return true; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | LineNumber = Buff.readInt(); |
| 114 | |
| 115 | // read blocks. |
| 116 | assert (Buff.readBlockTag() && "Block Tag not found!"); |
| 117 | uint32_t BlockCount = Buff.readInt(); |
| 118 | for (int i = 0, e = BlockCount; i != e; ++i) { |
| 119 | Buff.readInt(); // Block flags; |
| 120 | Blocks.push_back(new GCOVBlock(i)); |
| 121 | } |
| 122 | |
| 123 | // read edges. |
| 124 | while (Buff.readEdgeTag()) { |
| 125 | uint32_t EdgeCount = (Buff.readInt() - 1) / 2; |
| 126 | uint32_t BlockNo = Buff.readInt(); |
| 127 | assert (BlockNo < BlockCount && "Unexpected Block number!"); |
| 128 | for (int i = 0, e = EdgeCount; i != e; ++i) { |
| 129 | Blocks[BlockNo]->addEdge(Buff.readInt()); |
| 130 | Buff.readInt(); // Edge flag |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // read line table. |
| 135 | while (Buff.readLineTag()) { |
| 136 | uint32_t LineTableLength = Buff.readInt(); |
| 137 | uint32_t Size = Buff.getCursor() + LineTableLength*4; |
| 138 | uint32_t BlockNo = Buff.readInt(); |
| 139 | assert (BlockNo < BlockCount && "Unexpected Block number!"); |
| 140 | GCOVBlock *Block = Blocks[BlockNo]; |
| 141 | Buff.readInt(); // flag |
| 142 | while (Buff.getCursor() != (Size - 4)) { |
| 143 | StringRef Filename = Buff.readString(); |
| 144 | if (Buff.getCursor() == (Size - 4)) break; |
| 145 | while (uint32_t L = Buff.readInt()) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 146 | Block->addLine(Filename, L); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 147 | } |
| 148 | Buff.readInt(); // flag |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | /// dump - Dump GCOVFunction content on standard out for debugging purposes. |
| 154 | void GCOVFunction::dump() { |
| 155 | outs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; |
| 156 | for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 157 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 158 | (*I)->dump(); |
| 159 | } |
| 160 | |
| 161 | /// collectLineCounts - Collect line counts. This must be used after |
| 162 | /// reading .gcno and .gcda files. |
| 163 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
| 164 | for (SmallVector<GCOVBlock *, 16>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 165 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 166 | (*I)->collectLineCounts(FI); |
| 167 | } |
| 168 | |
| 169 | //===----------------------------------------------------------------------===// |
| 170 | // GCOVBlock implementation. |
| 171 | |
| 172 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 173 | GCOVBlock::~GCOVBlock() { |
| 174 | Edges.clear(); |
| 175 | DeleteContainerSeconds(Lines); |
| 176 | } |
| 177 | |
| 178 | void GCOVBlock::addLine(StringRef Filename, uint32_t LineNo) { |
| 179 | GCOVLines *&LinesForFile = Lines[Filename]; |
| 180 | if (!LinesForFile) |
| 181 | LinesForFile = new GCOVLines(); |
| 182 | LinesForFile->add(LineNo); |
| 183 | } |
| 184 | |
| 185 | /// collectLineCounts - Collect line counts. This must be used after |
| 186 | /// reading .gcno and .gcda files. |
| 187 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
| 188 | for (StringMap<GCOVLines *>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 189 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 190 | I->second->collectLineCounts(FI, I->first(), Counter); |
| 191 | } |
| 192 | |
| 193 | /// dump - Dump GCOVBlock content on standard out for debugging purposes. |
| 194 | void GCOVBlock::dump() { |
| 195 | outs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
| 196 | if (!Edges.empty()) { |
| 197 | outs() << "\tEdges : "; |
| 198 | for (SmallVector<uint32_t, 16>::iterator I = Edges.begin(), E = Edges.end(); |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 199 | I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 200 | outs() << (*I) << ","; |
| 201 | outs() << "\n"; |
| 202 | } |
| 203 | if (!Lines.empty()) { |
| 204 | outs() << "\tLines : "; |
| 205 | for (StringMap<GCOVLines *>::iterator LI = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 206 | LE = Lines.end(); LI != LE; ++LI) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 207 | outs() << LI->first() << " -> "; |
| 208 | LI->second->dump(); |
| 209 | outs() << "\n"; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // GCOVLines implementation. |
| 216 | |
| 217 | /// collectLineCounts - Collect line counts. This must be used after |
| 218 | /// reading .gcno and .gcda files. |
| 219 | void GCOVLines::collectLineCounts(FileInfo &FI, StringRef Filename, |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 220 | uint32_t Count) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 221 | for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 222 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 223 | FI.addLineCount(Filename, *I, Count); |
| 224 | } |
| 225 | |
| 226 | /// dump - Dump GCOVLines content on standard out for debugging purposes. |
| 227 | void GCOVLines::dump() { |
| 228 | for (SmallVector<uint32_t, 16>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 229 | E = Lines.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 230 | outs() << (*I) << ","; |
| 231 | } |
| 232 | |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | // FileInfo implementation. |
| 235 | |
| 236 | /// addLineCount - Add line count for the given line number in a file. |
| 237 | void FileInfo::addLineCount(StringRef Filename, uint32_t Line, uint32_t Count) { |
| 238 | if (LineInfo.find(Filename) == LineInfo.end()) { |
| 239 | OwningPtr<MemoryBuffer> Buff; |
| 240 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 241 | errs() << Filename << ": " << ec.message() << "\n"; |
| 242 | return; |
| 243 | } |
| 244 | StringRef AllLines = Buff.take()->getBuffer(); |
| 245 | LineCounts L(AllLines.count('\n')+2); |
| 246 | L[Line-1] = Count; |
| 247 | LineInfo[Filename] = L; |
| 248 | return; |
| 249 | } |
| 250 | LineCounts &L = LineInfo[Filename]; |
| 251 | L[Line-1] = Count; |
| 252 | } |
| 253 | |
| 254 | /// print - Print source files with collected line count information. |
| 255 | void FileInfo::print() { |
| 256 | for (StringMap<LineCounts>::iterator I = LineInfo.begin(), E = LineInfo.end(); |
| 257 | I != E; ++I) { |
| 258 | StringRef Filename = I->first(); |
| 259 | outs() << Filename << "\n"; |
| 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(); |
| 267 | for (unsigned i = 0, e = L.size(); i != e; ++i) { |
| 268 | if (L[i]) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 269 | outs() << L[i] << ":\t"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 270 | else |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 271 | outs() << " :\t"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 272 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
| 273 | if (AllLines != P.first) |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 274 | outs() << P.first; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 275 | outs() << "\n"; |
| 276 | AllLines = P.second; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |