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