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 | // |
NAKAMURA Takumi | 3b55196 | 2013-11-14 11:44:58 +0000 | [diff] [blame] | 10 | // GCOV implements the interface to read and write coverage files that use |
Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 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 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Debug.h" |
Devang Patel | 8dfb655 | 2011-10-04 17:24:48 +0000 | [diff] [blame] | 16 | #include "llvm/Support/GCOV.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/OwningPtr.h" |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Bob Wilson | 3461bed | 2013-10-22 05:09:41 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Format.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryObject.h" |
| 21 | #include "llvm/Support/system_error.h" |
| 22 | using namespace llvm; |
| 23 | |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | // GCOVFile implementation. |
| 26 | |
| 27 | /// ~GCOVFile - Delete GCOVFile and its content. |
| 28 | GCOVFile::~GCOVFile() { |
| 29 | DeleteContainerPointers(Functions); |
| 30 | } |
| 31 | |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 32 | /// isGCDAFile - Return true if Format identifies a .gcda file. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 33 | static bool isGCDAFile(GCOV::GCOVFormat Format) { |
| 34 | return Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /// isGCNOFile - Return true if Format identifies a .gcno file. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 38 | static bool isGCNOFile(GCOV::GCOVFormat Format) { |
| 39 | return Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 42 | /// read - Read GCOV buffer. |
| 43 | bool GCOVFile::read(GCOVBuffer &Buffer) { |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 44 | GCOV::GCOVFormat Format = Buffer.readGCOVFormat(); |
| 45 | if (Format == GCOV::InvalidGCOV) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 46 | return false; |
| 47 | |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 48 | if (isGCNOFile(Format)) { |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 49 | if (!Buffer.readInt(Checksum)) return false; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 50 | while (true) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 51 | if (!Buffer.readFunctionTag()) break; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 52 | GCOVFunction *GFun = new GCOVFunction(); |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 53 | if (!GFun->readGCNO(Buffer, Format)) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 54 | return false; |
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 | } |
Yuchen Wu | 9a74b8c | 2013-11-21 04:12:10 +0000 | [diff] [blame] | 57 | } else if (isGCDAFile(Format)) { |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 58 | uint32_t Checksum2; |
| 59 | if (!Buffer.readInt(Checksum2)) return false; |
| 60 | if (Checksum != Checksum2) { |
| 61 | errs() << "File checksum does not match.\n"; |
| 62 | return false; |
| 63 | } |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 64 | for (size_t i = 0, e = Functions.size(); i < e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 65 | if (!Buffer.readFunctionTag()) { |
| 66 | errs() << "Unexpected number of functions.\n"; |
| 67 | return false; |
| 68 | } |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 69 | if (!Functions[i]->readGCDA(Buffer, Format)) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 70 | return false; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 71 | } |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 72 | if (Buffer.readObjectTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 73 | uint32_t Length; |
| 74 | uint32_t Dummy; |
| 75 | if (!Buffer.readInt(Length)) return false; |
| 76 | if (!Buffer.readInt(Dummy)) return false; // checksum |
| 77 | if (!Buffer.readInt(Dummy)) return false; // num |
| 78 | if (!Buffer.readInt(RunCount)) return false;; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 79 | Buffer.advanceCursor(Length-3); |
| 80 | } |
| 81 | while (Buffer.readProgramTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 82 | uint32_t Length; |
| 83 | if (!Buffer.readInt(Length)) return false; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 84 | Buffer.advanceCursor(Length); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 85 | ++ProgramCount; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 86 | } |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 89 | return true; |
| 90 | } |
| 91 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 92 | /// dump - Dump GCOVFile content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 93 | void GCOVFile::dump() const { |
| 94 | for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 95 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 96 | (*I)->dump(); |
| 97 | } |
| 98 | |
| 99 | /// collectLineCounts - Collect line counts. This must be used after |
| 100 | /// reading .gcno and .gcda files. |
| 101 | void GCOVFile::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 102 | for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(), |
NAKAMURA Takumi | 3b55196 | 2013-11-14 11:44:58 +0000 | [diff] [blame] | 103 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 104 | (*I)->collectLineCounts(FI); |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 105 | FI.setRunCount(RunCount); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 106 | FI.setProgramCount(ProgramCount); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | //===----------------------------------------------------------------------===// |
| 110 | // GCOVFunction implementation. |
| 111 | |
| 112 | /// ~GCOVFunction - Delete GCOVFunction and its content. |
| 113 | GCOVFunction::~GCOVFunction() { |
| 114 | DeleteContainerPointers(Blocks); |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 115 | DeleteContainerPointers(Edges); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 118 | /// readGCNO - Read a function from the GCNO buffer. Return false if an error |
| 119 | /// occurs. |
| 120 | bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 121 | uint32_t Dummy; |
| 122 | if (!Buff.readInt(Dummy)) return false; // Function header length |
| 123 | if (!Buff.readInt(Ident)) return false; |
| 124 | if (!Buff.readInt(Dummy)) return false; // Checksum #1 |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 125 | if (Format != GCOV::GCNO_402) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 126 | if (!Buff.readInt(Dummy)) return false; // Checksum #2 |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 127 | |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 128 | if (!Buff.readString(Name)) return false; |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 129 | if (!Buff.readString(Filename)) return false; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 130 | if (!Buff.readInt(LineNumber)) return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 131 | |
| 132 | // read blocks. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 133 | if (!Buff.readBlockTag()) { |
| 134 | errs() << "Block tag not found.\n"; |
| 135 | return false; |
| 136 | } |
| 137 | uint32_t BlockCount; |
| 138 | if (!Buff.readInt(BlockCount)) return false; |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 139 | for (uint32_t i = 0, e = BlockCount; i != e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 140 | if (!Buff.readInt(Dummy)) return false; // Block flags; |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 141 | Blocks.push_back(new GCOVBlock(*this, i)); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // read edges. |
| 145 | while (Buff.readEdgeTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 146 | uint32_t EdgeCount; |
| 147 | if (!Buff.readInt(EdgeCount)) return false; |
| 148 | EdgeCount = (EdgeCount - 1) / 2; |
| 149 | uint32_t BlockNo; |
| 150 | if (!Buff.readInt(BlockNo)) return false; |
| 151 | if (BlockNo >= BlockCount) { |
| 152 | errs() << "Unexpected block number.\n"; |
| 153 | return false; |
| 154 | } |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 155 | for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 156 | uint32_t Dst; |
| 157 | if (!Buff.readInt(Dst)) return false; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 158 | GCOVEdge *Edge = new GCOVEdge(Blocks[BlockNo], Blocks[Dst]); |
| 159 | Edges.push_back(Edge); |
| 160 | Blocks[BlockNo]->addDstEdge(Edge); |
| 161 | Blocks[Dst]->addSrcEdge(Edge); |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 162 | if (!Buff.readInt(Dummy)) return false; // Edge flag |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
| 166 | // read line table. |
| 167 | while (Buff.readLineTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 168 | uint32_t LineTableLength; |
| 169 | if (!Buff.readInt(LineTableLength)) return false; |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 170 | uint32_t EndPos = Buff.getCursor() + LineTableLength*4; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 171 | uint32_t BlockNo; |
| 172 | if (!Buff.readInt(BlockNo)) return false; |
| 173 | if (BlockNo >= BlockCount) { |
| 174 | errs() << "Unexpected block number.\n"; |
| 175 | return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 176 | } |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 177 | GCOVBlock *Block = Blocks[BlockNo]; |
| 178 | if (!Buff.readInt(Dummy)) return false; // flag |
| 179 | while (Buff.getCursor() != (EndPos - 4)) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 180 | StringRef F; |
| 181 | if (!Buff.readString(F)) return false; |
| 182 | if (F != Filename) { |
| 183 | errs() << "Multiple sources for a single basic block.\n"; |
| 184 | return false; |
| 185 | } |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 186 | if (Buff.getCursor() == (EndPos - 4)) break; |
| 187 | while (true) { |
| 188 | uint32_t Line; |
| 189 | if (!Buff.readInt(Line)) return false; |
| 190 | if (!Line) break; |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 191 | Block->addLine(Line); |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | if (!Buff.readInt(Dummy)) return false; // flag |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 195 | } |
| 196 | return true; |
| 197 | } |
| 198 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 199 | /// readGCDA - Read a function from the GCDA buffer. Return false if an error |
| 200 | /// occurs. |
| 201 | bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { |
| 202 | uint32_t Dummy; |
| 203 | if (!Buff.readInt(Dummy)) return false; // Function header length |
| 204 | if (!Buff.readInt(Ident)) return false; |
| 205 | if (!Buff.readInt(Dummy)) return false; // Checksum #1 |
| 206 | if (Format != GCOV::GCDA_402) |
| 207 | if (!Buff.readInt(Dummy)) return false; // Checksum #2 |
| 208 | |
| 209 | if (!Buff.readString(Name)) return false; |
| 210 | |
| 211 | if (!Buff.readArcTag()) { |
| 212 | errs() << "Arc tag not found.\n"; |
| 213 | return false; |
| 214 | } |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 215 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 216 | uint32_t Count; |
| 217 | if (!Buff.readInt(Count)) return false; |
| 218 | Count /= 2; |
| 219 | |
| 220 | // This for loop adds the counts for each block. A second nested loop is |
| 221 | // required to combine the edge counts that are contained in the GCDA file. |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 222 | for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) { |
| 223 | // The last block is always reserved for exit block |
| 224 | if (BlockNo >= Blocks.size()-1) { |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 225 | errs() << "Unexpected number of edges.\n"; |
| 226 | return false; |
| 227 | } |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 228 | GCOVBlock &Block = *Blocks[BlockNo]; |
| 229 | for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End; |
| 230 | ++EdgeNo) { |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 231 | if (Count == 0) { |
| 232 | errs() << "Unexpected number of edges.\n"; |
| 233 | return false; |
| 234 | } |
| 235 | uint64_t ArcCount; |
| 236 | if (!Buff.readInt64(ArcCount)) return false; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 237 | Block.addCount(EdgeNo, ArcCount); |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 238 | --Count; |
| 239 | } |
| 240 | } |
| 241 | return true; |
| 242 | } |
| 243 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 244 | /// dump - Dump GCOVFunction content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 245 | void GCOVFunction::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 246 | dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 247 | for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 248 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 249 | (*I)->dump(); |
| 250 | } |
| 251 | |
| 252 | /// collectLineCounts - Collect line counts. This must be used after |
| 253 | /// reading .gcno and .gcda files. |
| 254 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 255 | for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 256 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 257 | (*I)->collectLineCounts(FI); |
| 258 | } |
| 259 | |
| 260 | //===----------------------------------------------------------------------===// |
| 261 | // GCOVBlock implementation. |
| 262 | |
| 263 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 264 | GCOVBlock::~GCOVBlock() { |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 265 | SrcEdges.clear(); |
| 266 | DstEdges.clear(); |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 267 | Lines.clear(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 270 | /// addCount - Add to block counter while storing the edge count. If the |
| 271 | /// destination has no outgoing edges, also update that block's count too. |
| 272 | void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) { |
| 273 | assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid |
| 274 | DstEdges[DstEdgeNo]->Count = N; |
| 275 | Counter += N; |
| 276 | if (!DstEdges[DstEdgeNo]->Dst->getNumDstEdges()) |
| 277 | DstEdges[DstEdgeNo]->Dst->Counter += N; |
| 278 | } |
| 279 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 280 | /// collectLineCounts - Collect line counts. This must be used after |
| 281 | /// reading .gcno and .gcda files. |
| 282 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 283 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 284 | E = Lines.end(); I != E; ++I) |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 285 | FI.addBlockLine(Parent.getFilename(), *I, this); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 288 | /// dump - Dump GCOVBlock content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 289 | void GCOVBlock::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 290 | dbgs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 291 | if (!SrcEdges.empty()) { |
| 292 | dbgs() << "\tSource Edges : "; |
| 293 | for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) { |
| 294 | const GCOVEdge *Edge = *I; |
| 295 | dbgs() << Edge->Src->Number << " (" << Edge->Count << "), "; |
| 296 | } |
| 297 | dbgs() << "\n"; |
| 298 | } |
| 299 | if (!DstEdges.empty()) { |
| 300 | dbgs() << "\tDestination Edges : "; |
| 301 | for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) { |
| 302 | const GCOVEdge *Edge = *I; |
| 303 | dbgs() << Edge->Dst->Number << " (" << Edge->Count << "), "; |
| 304 | } |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 305 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 306 | } |
| 307 | if (!Lines.empty()) { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 308 | dbgs() << "\tLines : "; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 309 | for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(), |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 310 | E = Lines.end(); I != E; ++I) |
| 311 | dbgs() << (*I) << ","; |
| 312 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | //===----------------------------------------------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 317 | // FileInfo implementation. |
| 318 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 319 | /// print - Print source files with collected line count information. |
Yuchen Wu | 26326ad | 2013-12-03 00:57:11 +0000 | [diff] [blame] | 320 | void FileInfo::print(StringRef gcnoFile, StringRef gcdaFile) const { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 321 | for (StringMap<LineData>::const_iterator I = LineInfo.begin(), |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 322 | E = LineInfo.end(); I != E; ++I) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 323 | StringRef Filename = I->first(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 324 | OwningPtr<MemoryBuffer> Buff; |
| 325 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 326 | errs() << Filename << ": " << ec.message() << "\n"; |
| 327 | return; |
| 328 | } |
Benjamin Kramer | 67421c1 | 2013-11-15 09:44:17 +0000 | [diff] [blame] | 329 | StringRef AllLines = Buff->getBuffer(); |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 330 | |
Yuchen Wu | 26326ad | 2013-12-03 00:57:11 +0000 | [diff] [blame] | 331 | std::string CovFilename = Filename.str() + ".llcov"; |
| 332 | std::string ErrorInfo; |
| 333 | raw_fd_ostream OS(CovFilename.c_str(), ErrorInfo); |
| 334 | if (!ErrorInfo.empty()) |
| 335 | errs() << ErrorInfo << "\n"; |
| 336 | |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 337 | OS << " -: 0:Source:" << Filename << "\n"; |
| 338 | OS << " -: 0:Graph:" << gcnoFile << "\n"; |
| 339 | OS << " -: 0:Data:" << gcdaFile << "\n"; |
| 340 | OS << " -: 0:Runs:" << RunCount << "\n"; |
| 341 | OS << " -: 0:Programs:" << ProgramCount << "\n"; |
| 342 | |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame^] | 343 | const LineData &Line = I->second; |
| 344 | for (uint32_t i = 0; !AllLines.empty(); ++i) { |
| 345 | LineData::const_iterator BlocksIt = Line.find(i); |
| 346 | |
| 347 | // Add up the block counts to form line counts. |
| 348 | if (BlocksIt != Line.end()) { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 349 | const BlockVector &Blocks = BlocksIt->second; |
| 350 | uint64_t LineCount = 0; |
| 351 | for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); |
| 352 | I != E; ++I) { |
| 353 | LineCount += (*I)->getCount(); |
| 354 | } |
| 355 | if (LineCount == 0) |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 356 | OS << " #####:"; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 357 | else |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 358 | OS << format("%9" PRIu64 ":", LineCount); |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 359 | } else { |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 360 | OS << " -:"; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 361 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 362 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame^] | 363 | OS << format("%5u:", i+1) << P.first << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 364 | AllLines = P.second; |
| 365 | } |
| 366 | } |
| 367 | } |