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)) { |
| 49 | while (true) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 50 | if (!Buffer.readFunctionTag()) break; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 51 | GCOVFunction *GFun = new GCOVFunction(); |
| 52 | if (!GFun->read(Buffer, Format)) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 53 | return false; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 54 | Functions.push_back(GFun); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 55 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 56 | } |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 57 | else if (isGCDAFile(Format)) { |
| 58 | for (size_t i = 0, e = Functions.size(); i < e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 59 | if (!Buffer.readFunctionTag()) { |
| 60 | errs() << "Unexpected number of functions.\n"; |
| 61 | return false; |
| 62 | } |
| 63 | if (!Functions[i]->read(Buffer, Format)) |
| 64 | return false; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 65 | } |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 66 | if (Buffer.readObjectTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 67 | uint32_t Length; |
| 68 | uint32_t Dummy; |
| 69 | if (!Buffer.readInt(Length)) return false; |
| 70 | if (!Buffer.readInt(Dummy)) return false; // checksum |
| 71 | if (!Buffer.readInt(Dummy)) return false; // num |
| 72 | if (!Buffer.readInt(RunCount)) return false;; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 73 | Buffer.advanceCursor(Length-3); |
| 74 | } |
| 75 | while (Buffer.readProgramTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 76 | uint32_t Length; |
| 77 | if (!Buffer.readInt(Length)) return false; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 78 | Buffer.advanceCursor(Length); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 79 | ++ProgramCount; |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 80 | } |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 83 | return true; |
| 84 | } |
| 85 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 86 | /// dump - Dump GCOVFile content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 87 | void GCOVFile::dump() const { |
| 88 | for (SmallVectorImpl<GCOVFunction *>::const_iterator I = Functions.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 89 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 90 | (*I)->dump(); |
| 91 | } |
| 92 | |
| 93 | /// collectLineCounts - Collect line counts. This must be used after |
| 94 | /// reading .gcno and .gcda files. |
| 95 | void GCOVFile::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 96 | for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(), |
NAKAMURA Takumi | 3b55196 | 2013-11-14 11:44:58 +0000 | [diff] [blame] | 97 | E = Functions.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 98 | (*I)->collectLineCounts(FI); |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 99 | FI.setRunCount(RunCount); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 100 | FI.setProgramCount(ProgramCount); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | //===----------------------------------------------------------------------===// |
| 104 | // GCOVFunction implementation. |
| 105 | |
| 106 | /// ~GCOVFunction - Delete GCOVFunction and its content. |
| 107 | GCOVFunction::~GCOVFunction() { |
| 108 | DeleteContainerPointers(Blocks); |
| 109 | } |
| 110 | |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 111 | /// read - Read a function from the buffer. Return false if buffer cursor |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 112 | /// does not point to a function tag. |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 113 | bool GCOVFunction::read(GCOVBuffer &Buff, GCOV::GCOVFormat Format) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 114 | uint32_t Dummy; |
| 115 | if (!Buff.readInt(Dummy)) return false; // Function header length |
| 116 | if (!Buff.readInt(Ident)) return false; |
| 117 | if (!Buff.readInt(Dummy)) return false; // Checksum #1 |
Bill Wendling | c432503 | 2013-06-25 18:13:52 +0000 | [diff] [blame] | 118 | if (Format != GCOV::GCNO_402 && Format != GCOV::GCDA_402) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 119 | if (!Buff.readInt(Dummy)) return false; // Checksum #2 |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 120 | |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 121 | if (!Buff.readString(Name)) return false; |
| 122 | |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 123 | if (Format == GCOV::GCNO_402 || Format == GCOV::GCNO_404) |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 124 | if (!Buff.readString(Filename)) return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 125 | |
Bill Wendling | 6bbe489 | 2012-08-31 17:31:28 +0000 | [diff] [blame] | 126 | if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 127 | if (!Buff.readArcTag()) { |
| 128 | errs() << "Arc tag not found.\n"; |
| 129 | return false; |
| 130 | } |
| 131 | uint32_t Count; |
| 132 | if (!Buff.readInt(Count)) return false; |
| 133 | Count /= 2; |
Yuchen Wu | 887c20f | 2013-10-24 01:51:04 +0000 | [diff] [blame] | 134 | |
| 135 | // This for loop adds the counts for each block. A second nested loop is |
| 136 | // required to combine the edge counts that are contained in the GCDA file. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 137 | for (uint32_t Line = 0; Count > 0; ++Line) { |
Yuchen Wu | 7981f5b | 2013-11-14 00:38:41 +0000 | [diff] [blame] | 138 | if (Line >= Blocks.size()) { |
| 139 | errs() << "Unexpected number of edges.\n"; |
| 140 | return false; |
| 141 | } |
Yuchen Wu | 887c20f | 2013-10-24 01:51:04 +0000 | [diff] [blame] | 142 | GCOVBlock &Block = *Blocks[Line]; |
| 143 | for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 144 | if (Count == 0) { |
| 145 | errs() << "Unexpected number of edges.\n"; |
| 146 | return false; |
| 147 | } |
| 148 | uint64_t ArcCount; |
| 149 | if (!Buff.readInt64(ArcCount)) return false; |
| 150 | Block.addCount(ArcCount); |
| 151 | --Count; |
Yuchen Wu | 887c20f | 2013-10-24 01:51:04 +0000 | [diff] [blame] | 152 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 153 | } |
Chad Rosier | 5dfe6da | 2012-02-22 17:25:00 +0000 | [diff] [blame] | 154 | return true; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 157 | if (!Buff.readInt(LineNumber)) return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 158 | |
| 159 | // read blocks. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 160 | if (!Buff.readBlockTag()) { |
| 161 | errs() << "Block tag not found.\n"; |
| 162 | return false; |
| 163 | } |
| 164 | uint32_t BlockCount; |
| 165 | if (!Buff.readInt(BlockCount)) return false; |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 166 | for (uint32_t i = 0, e = BlockCount; i != e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 167 | if (!Buff.readInt(Dummy)) return false; // Block flags; |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 168 | Blocks.push_back(new GCOVBlock(*this, i)); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | // read edges. |
| 172 | while (Buff.readEdgeTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 173 | uint32_t EdgeCount; |
| 174 | if (!Buff.readInt(EdgeCount)) return false; |
| 175 | EdgeCount = (EdgeCount - 1) / 2; |
| 176 | uint32_t BlockNo; |
| 177 | if (!Buff.readInt(BlockNo)) return false; |
| 178 | if (BlockNo >= BlockCount) { |
| 179 | errs() << "Unexpected block number.\n"; |
| 180 | return false; |
| 181 | } |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 182 | for (uint32_t i = 0, e = EdgeCount; i != e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 183 | uint32_t Dst; |
| 184 | if (!Buff.readInt(Dst)) return false; |
| 185 | Blocks[BlockNo]->addEdge(Dst); |
| 186 | if (!Buff.readInt(Dummy)) return false; // Edge flag |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | // read line table. |
| 191 | while (Buff.readLineTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 192 | uint32_t LineTableLength; |
| 193 | if (!Buff.readInt(LineTableLength)) return false; |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 194 | uint32_t EndPos = Buff.getCursor() + LineTableLength*4; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 195 | uint32_t BlockNo; |
| 196 | if (!Buff.readInt(BlockNo)) return false; |
| 197 | if (BlockNo >= BlockCount) { |
| 198 | errs() << "Unexpected block number.\n"; |
| 199 | return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 200 | } |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 201 | GCOVBlock *Block = Blocks[BlockNo]; |
| 202 | if (!Buff.readInt(Dummy)) return false; // flag |
| 203 | while (Buff.getCursor() != (EndPos - 4)) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 204 | StringRef F; |
| 205 | if (!Buff.readString(F)) return false; |
| 206 | if (F != Filename) { |
| 207 | errs() << "Multiple sources for a single basic block.\n"; |
| 208 | return false; |
| 209 | } |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 210 | if (Buff.getCursor() == (EndPos - 4)) break; |
| 211 | while (true) { |
| 212 | uint32_t Line; |
| 213 | if (!Buff.readInt(Line)) return false; |
| 214 | if (!Line) break; |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 215 | Block->addLine(Line); |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | if (!Buff.readInt(Dummy)) return false; // flag |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 219 | } |
| 220 | return true; |
| 221 | } |
| 222 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 223 | /// dump - Dump GCOVFunction content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 224 | void GCOVFunction::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 225 | dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 226 | for (SmallVectorImpl<GCOVBlock *>::const_iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 227 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 228 | (*I)->dump(); |
| 229 | } |
| 230 | |
| 231 | /// collectLineCounts - Collect line counts. This must be used after |
| 232 | /// reading .gcno and .gcda files. |
| 233 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
Craig Topper | af0dea1 | 2013-07-04 01:31:24 +0000 | [diff] [blame] | 234 | for (SmallVectorImpl<GCOVBlock *>::iterator I = Blocks.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 235 | E = Blocks.end(); I != E; ++I) |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 236 | (*I)->collectLineCounts(FI); |
| 237 | } |
| 238 | |
| 239 | //===----------------------------------------------------------------------===// |
| 240 | // GCOVBlock implementation. |
| 241 | |
| 242 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 243 | GCOVBlock::~GCOVBlock() { |
| 244 | Edges.clear(); |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 245 | Lines.clear(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /// collectLineCounts - Collect line counts. This must be used after |
| 249 | /// reading .gcno and .gcda files. |
| 250 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 251 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 252 | E = Lines.end(); I != E; ++I) |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 253 | FI.addLineCount(Parent.getFilename(), *I, Counter); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 256 | /// dump - Dump GCOVBlock content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 257 | void GCOVBlock::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 258 | dbgs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 259 | if (!Edges.empty()) { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 260 | dbgs() << "\tEdges : "; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 261 | for (SmallVectorImpl<uint32_t>::const_iterator I = Edges.begin(), E = Edges.end(); |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 262 | I != E; ++I) |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 263 | dbgs() << (*I) << ","; |
| 264 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 265 | } |
| 266 | if (!Lines.empty()) { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 267 | dbgs() << "\tLines : "; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 268 | for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(), |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 269 | E = Lines.end(); I != E; ++I) |
| 270 | dbgs() << (*I) << ","; |
| 271 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 276 | // FileInfo implementation. |
| 277 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 278 | /// print - Print source files with collected line count information. |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 279 | void FileInfo::print(raw_fd_ostream &OS, StringRef gcnoFile, |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 280 | StringRef gcdaFile) const { |
| 281 | for (StringMap<LineCounts>::const_iterator I = LineInfo.begin(), |
| 282 | E = LineInfo.end(); I != E; ++I) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 283 | StringRef Filename = I->first(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 284 | OwningPtr<MemoryBuffer> Buff; |
| 285 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 286 | errs() << Filename << ": " << ec.message() << "\n"; |
| 287 | return; |
| 288 | } |
Benjamin Kramer | 67421c1 | 2013-11-15 09:44:17 +0000 | [diff] [blame] | 289 | StringRef AllLines = Buff->getBuffer(); |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame^] | 290 | |
| 291 | OS << " -: 0:Source:" << Filename << "\n"; |
| 292 | OS << " -: 0:Graph:" << gcnoFile << "\n"; |
| 293 | OS << " -: 0:Data:" << gcdaFile << "\n"; |
| 294 | OS << " -: 0:Runs:" << RunCount << "\n"; |
| 295 | OS << " -: 0:Programs:" << ProgramCount << "\n"; |
| 296 | |
| 297 | const LineCounts &L = I->second; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 298 | uint32_t i = 0; |
| 299 | while (!AllLines.empty()) { |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 300 | LineCounts::const_iterator CountIt = L.find(i); |
| 301 | if (CountIt != L.end()) { |
| 302 | if (CountIt->second == 0) |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 303 | OS << " #####:"; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 304 | else |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 305 | OS << format("%9" PRIu64 ":", CountIt->second); |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 306 | } else { |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 307 | OS << " -:"; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 308 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 309 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
| 310 | if (AllLines != P.first) |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 311 | OS << format("%5u:", i+1) << P.first; |
| 312 | OS << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 313 | AllLines = P.second; |
Yuchen Wu | 48342ee | 2013-10-23 19:45:03 +0000 | [diff] [blame] | 314 | ++i; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | } |