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