Yuchen Wu | c3e6424 | 2013-12-05 22:02:29 +0000 | [diff] [blame] | 1 | //===- GCOV.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 | |
Justin Bogner | df82c62 | 2014-02-04 21:03:17 +0000 | [diff] [blame] | 15 | #include "llvm/Support/GCOV.h" |
Devang Patel | a9e8a25 | 2011-09-29 16:46:47 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
Justin Bogner | df82c62 | 2014-02-04 21:03:17 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
Justin Bogner | df82c62 | 2014-02-04 21:03:17 +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" |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/system_error.h" |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // GCOVFile implementation. |
| 28 | |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 29 | /// readGCNO - Read GCNO buffer. |
| 30 | bool GCOVFile::readGCNO(GCOVBuffer &Buffer) { |
| 31 | if (!Buffer.readGCNOFormat()) return false; |
| 32 | if (!Buffer.readGCOVVersion(Version)) return false; |
| 33 | |
| 34 | if (!Buffer.readInt(Checksum)) return false; |
| 35 | while (true) { |
| 36 | if (!Buffer.readFunctionTag()) break; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 37 | auto GFun = make_unique<GCOVFunction>(*this); |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 38 | if (!GFun->readGCNO(Buffer, Version)) |
| 39 | return false; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 40 | Functions.push_back(std::move(GFun)); |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Yuchen Wu | 21517e4 | 2013-12-04 05:07:36 +0000 | [diff] [blame] | 43 | GCNOInitialized = true; |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 44 | return true; |
Devang Patel | e5a8f2f9 | 2011-09-29 17:06:40 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 47 | /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be |
| 48 | /// called after readGCNO(). |
| 49 | bool GCOVFile::readGCDA(GCOVBuffer &Buffer) { |
Yuchen Wu | 21517e4 | 2013-12-04 05:07:36 +0000 | [diff] [blame] | 50 | assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()"); |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 51 | if (!Buffer.readGCDAFormat()) return false; |
| 52 | GCOV::GCOVVersion GCDAVersion; |
| 53 | if (!Buffer.readGCOVVersion(GCDAVersion)) return false; |
| 54 | if (Version != GCDAVersion) { |
| 55 | errs() << "GCOV versions do not match.\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 56 | return false; |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 57 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 58 | |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 59 | uint32_t GCDAChecksum; |
| 60 | if (!Buffer.readInt(GCDAChecksum)) return false; |
| 61 | if (Checksum != GCDAChecksum) { |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 62 | errs() << "File checksums do not match: " << Checksum << " != " |
| 63 | << GCDAChecksum << ".\n"; |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | for (size_t i = 0, e = Functions.size(); i < e; ++i) { |
| 67 | if (!Buffer.readFunctionTag()) { |
| 68 | errs() << "Unexpected number of functions.\n"; |
Yuchen Wu | babe749 | 2013-11-20 04:15:05 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 71 | if (!Functions[i]->readGCDA(Buffer, Version)) |
| 72 | return false; |
| 73 | } |
| 74 | if (Buffer.readObjectTag()) { |
| 75 | uint32_t Length; |
| 76 | uint32_t Dummy; |
| 77 | if (!Buffer.readInt(Length)) return false; |
| 78 | if (!Buffer.readInt(Dummy)) return false; // checksum |
| 79 | if (!Buffer.readInt(Dummy)) return false; // num |
Yuchen Wu | 8742a28 | 2013-12-16 20:03:11 +0000 | [diff] [blame] | 80 | if (!Buffer.readInt(RunCount)) return false; |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 81 | Buffer.advanceCursor(Length-3); |
| 82 | } |
| 83 | while (Buffer.readProgramTag()) { |
| 84 | uint32_t Length; |
| 85 | if (!Buffer.readInt(Length)) return false; |
| 86 | Buffer.advanceCursor(Length); |
| 87 | ++ProgramCount; |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 90 | return true; |
| 91 | } |
| 92 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 93 | /// dump - Dump GCOVFile content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 94 | void GCOVFile::dump() const { |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 95 | for (const auto &FPtr : Functions) |
| 96 | FPtr->dump(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /// collectLineCounts - Collect line counts. This must be used after |
| 100 | /// reading .gcno and .gcda files. |
| 101 | void GCOVFile::collectLineCounts(FileInfo &FI) { |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 102 | for (const auto &FPtr : Functions) |
| 103 | FPtr->collectLineCounts(FI); |
Yuchen Wu | 30672d9 | 2013-11-05 01:11:58 +0000 | [diff] [blame] | 104 | FI.setRunCount(RunCount); |
Yuchen Wu | 14ae8e6 | 2013-10-25 02:22:21 +0000 | [diff] [blame] | 105 | FI.setProgramCount(ProgramCount); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // GCOVFunction implementation. |
| 110 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 111 | /// readGCNO - Read a function from the GCNO buffer. Return false if an error |
| 112 | /// occurs. |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 113 | bool GCOVFunction::readGCNO(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { |
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; |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 117 | if (!Buff.readInt(Checksum)) return false; |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 118 | if (Version != GCOV::V402) { |
| 119 | uint32_t CfgChecksum; |
| 120 | if (!Buff.readInt(CfgChecksum)) return false; |
| 121 | if (Parent.getChecksum() != CfgChecksum) { |
| 122 | errs() << "File checksums do not match: " << Parent.getChecksum() |
| 123 | << " != " << CfgChecksum << " in (" << Name << ").\n"; |
| 124 | return false; |
| 125 | } |
| 126 | } |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 127 | if (!Buff.readString(Name)) return false; |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 128 | if (!Buff.readString(Filename)) return false; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 129 | if (!Buff.readInt(LineNumber)) return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 130 | |
| 131 | // read blocks. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 132 | if (!Buff.readBlockTag()) { |
| 133 | errs() << "Block tag not found.\n"; |
| 134 | return false; |
| 135 | } |
| 136 | uint32_t BlockCount; |
| 137 | if (!Buff.readInt(BlockCount)) return false; |
Bob Wilson | 868e6e3 | 2013-10-22 20:02:36 +0000 | [diff] [blame] | 138 | for (uint32_t i = 0, e = BlockCount; i != e; ++i) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 139 | if (!Buff.readInt(Dummy)) return false; // Block flags; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 140 | Blocks.push_back(make_unique<GCOVBlock>(*this, i)); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // read edges. |
| 144 | while (Buff.readEdgeTag()) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 145 | uint32_t EdgeCount; |
| 146 | if (!Buff.readInt(EdgeCount)) return false; |
| 147 | EdgeCount = (EdgeCount - 1) / 2; |
| 148 | uint32_t BlockNo; |
| 149 | if (!Buff.readInt(BlockNo)) return false; |
| 150 | if (BlockNo >= BlockCount) { |
Yuchen Wu | 4c9f19d | 2013-12-05 22:02:33 +0000 | [diff] [blame] | 151 | errs() << "Unexpected block number: " << BlockNo << " (in " << Name |
| 152 | << ").\n"; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 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; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 158 | Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst])); |
| 159 | GCOVEdge *Edge = Edges.back().get(); |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 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; |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 169 | // Read the length of this line table. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 170 | if (!Buff.readInt(LineTableLength)) return false; |
Bob Wilson | 00928bc | 2013-10-22 19:54:32 +0000 | [diff] [blame] | 171 | uint32_t EndPos = Buff.getCursor() + LineTableLength*4; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 172 | uint32_t BlockNo; |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 173 | // Read the block number this table is associated with. |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 174 | if (!Buff.readInt(BlockNo)) return false; |
| 175 | if (BlockNo >= BlockCount) { |
Yuchen Wu | 4c9f19d | 2013-12-05 22:02:33 +0000 | [diff] [blame] | 176 | errs() << "Unexpected block number: " << BlockNo << " (in " << Name |
| 177 | << ").\n"; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 178 | return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 179 | } |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 180 | GCOVBlock &Block = *Blocks[BlockNo]; |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 181 | // Read the word that pads the beginning of the line table. This may be a |
| 182 | // flag of some sort, but seems to always be zero. |
| 183 | if (!Buff.readInt(Dummy)) return false; |
| 184 | |
| 185 | // Line information starts here and continues up until the last word. |
| 186 | if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 187 | StringRef F; |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 188 | // Read the source file name. |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 189 | if (!Buff.readString(F)) return false; |
Yuchen Wu | 4c9f19d | 2013-12-05 22:02:33 +0000 | [diff] [blame] | 190 | if (Filename != F) { |
| 191 | errs() << "Multiple sources for a single basic block: " << Filename |
| 192 | << " != " << F << " (in " << Name << ").\n"; |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 195 | // Read lines up to, but not including, the null terminator. |
| 196 | while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) { |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 197 | uint32_t Line; |
| 198 | if (!Buff.readInt(Line)) return false; |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 199 | // Line 0 means this instruction was injected by the compiler. Skip it. |
| 200 | if (!Line) continue; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 201 | Block.addLine(Line); |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 202 | } |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 203 | // Read the null terminator. |
| 204 | if (!Buff.readInt(Dummy)) return false; |
Yuchen Wu | e28da84 | 2013-11-14 00:07:15 +0000 | [diff] [blame] | 205 | } |
Justin Bogner | c475e1b | 2014-05-02 20:01:24 +0000 | [diff] [blame^] | 206 | // The last word is either a flag or padding, it isn't clear which. Skip |
| 207 | // over it. |
| 208 | if (!Buff.readInt(Dummy)) return false; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 209 | } |
| 210 | return true; |
| 211 | } |
| 212 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 213 | /// readGCDA - Read a function from the GCDA buffer. Return false if an error |
| 214 | /// occurs. |
Yuchen Wu | bec4e90 | 2013-12-04 04:49:23 +0000 | [diff] [blame] | 215 | bool GCOVFunction::readGCDA(GCOVBuffer &Buff, GCOV::GCOVVersion Version) { |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 216 | uint32_t Dummy; |
| 217 | if (!Buff.readInt(Dummy)) return false; // Function header length |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 218 | |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 219 | uint32_t GCDAIdent; |
| 220 | if (!Buff.readInt(GCDAIdent)) return false; |
| 221 | if (Ident != GCDAIdent) { |
| 222 | errs() << "Function identifiers do not match: " << Ident << " != " |
| 223 | << GCDAIdent << " (in " << Name << ").\n"; |
| 224 | return false; |
| 225 | } |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 226 | |
Daniel Jasper | 87a24d5 | 2013-12-04 08:57:17 +0000 | [diff] [blame] | 227 | uint32_t GCDAChecksum; |
| 228 | if (!Buff.readInt(GCDAChecksum)) return false; |
| 229 | if (Checksum != GCDAChecksum) { |
| 230 | errs() << "Function checksums do not match: " << Checksum << " != " |
| 231 | << GCDAChecksum << " (in " << Name << ").\n"; |
| 232 | return false; |
| 233 | } |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 234 | |
| 235 | uint32_t CfgChecksum; |
| 236 | if (Version != GCOV::V402) { |
| 237 | if (!Buff.readInt(CfgChecksum)) return false; |
| 238 | if (Parent.getChecksum() != CfgChecksum) { |
| 239 | errs() << "File checksums do not match: " << Parent.getChecksum() |
| 240 | << " != " << CfgChecksum << " (in " << Name << ").\n"; |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | StringRef GCDAName; |
| 246 | if (!Buff.readString(GCDAName)) return false; |
| 247 | if (Name != GCDAName) { |
| 248 | errs() << "Function names do not match: " << Name << " != " << GCDAName |
| 249 | << ".\n"; |
| 250 | return false; |
| 251 | } |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 252 | |
| 253 | if (!Buff.readArcTag()) { |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 254 | errs() << "Arc tag not found (in " << Name << ").\n"; |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 255 | return false; |
| 256 | } |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 257 | |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 258 | uint32_t Count; |
| 259 | if (!Buff.readInt(Count)) return false; |
| 260 | Count /= 2; |
| 261 | |
| 262 | // This for loop adds the counts for each block. A second nested loop is |
| 263 | // 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] | 264 | for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) { |
| 265 | // The last block is always reserved for exit block |
| 266 | if (BlockNo >= Blocks.size()-1) { |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 267 | errs() << "Unexpected number of edges (in " << Name << ").\n"; |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 268 | return false; |
| 269 | } |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 270 | GCOVBlock &Block = *Blocks[BlockNo]; |
| 271 | for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End; |
| 272 | ++EdgeNo) { |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 273 | if (Count == 0) { |
Yuchen Wu | 5752997 | 2013-12-04 05:42:28 +0000 | [diff] [blame] | 274 | errs() << "Unexpected number of edges (in " << Name << ").\n"; |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 275 | return false; |
| 276 | } |
| 277 | uint64_t ArcCount; |
| 278 | if (!Buff.readInt64(ArcCount)) return false; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 279 | Block.addCount(EdgeNo, ArcCount); |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 280 | --Count; |
| 281 | } |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 282 | Block.sortDstEdges(); |
Yuchen Wu | ba71833 | 2013-12-03 00:15:49 +0000 | [diff] [blame] | 283 | } |
| 284 | return true; |
| 285 | } |
| 286 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 287 | /// getEntryCount - Get the number of times the function was called by |
| 288 | /// retrieving the entry block's count. |
| 289 | uint64_t GCOVFunction::getEntryCount() const { |
| 290 | return Blocks.front()->getCount(); |
| 291 | } |
| 292 | |
| 293 | /// getExitCount - Get the number of times the function returned by retrieving |
| 294 | /// the exit block's count. |
| 295 | uint64_t GCOVFunction::getExitCount() const { |
| 296 | return Blocks.back()->getCount(); |
| 297 | } |
| 298 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 299 | /// dump - Dump GCOVFunction content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 300 | void GCOVFunction::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 301 | dbgs() << "===== " << Name << " @ " << Filename << ":" << LineNumber << "\n"; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 302 | for (const auto &Block : Blocks) |
| 303 | Block->dump(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /// collectLineCounts - Collect line counts. This must be used after |
| 307 | /// reading .gcno and .gcda files. |
| 308 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
Justin Bogner | 95e0a70 | 2014-03-26 22:03:06 +0000 | [diff] [blame] | 309 | // If the line number is zero, this is a function that doesn't actually appear |
| 310 | // in the source file, so there isn't anything we can do with it. |
| 311 | if (LineNumber == 0) |
| 312 | return; |
| 313 | |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 314 | for (const auto &Block : Blocks) |
| 315 | Block->collectLineCounts(FI); |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 316 | FI.addFunctionLine(Filename, LineNumber, this); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | //===----------------------------------------------------------------------===// |
| 320 | // GCOVBlock implementation. |
| 321 | |
| 322 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 323 | GCOVBlock::~GCOVBlock() { |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 324 | SrcEdges.clear(); |
| 325 | DstEdges.clear(); |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 326 | Lines.clear(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 329 | /// addCount - Add to block counter while storing the edge count. If the |
| 330 | /// destination has no outgoing edges, also update that block's count too. |
| 331 | void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) { |
| 332 | assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid |
| 333 | DstEdges[DstEdgeNo]->Count = N; |
| 334 | Counter += N; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 335 | if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges()) |
| 336 | DstEdges[DstEdgeNo]->Dst.Counter += N; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 339 | /// sortDstEdges - Sort destination edges by block number, nop if already |
| 340 | /// sorted. This is required for printing branch info in the correct order. |
| 341 | void GCOVBlock::sortDstEdges() { |
| 342 | if (!DstEdgesAreSorted) { |
| 343 | SortDstEdgesFunctor SortEdges; |
| 344 | std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges); |
| 345 | } |
| 346 | } |
| 347 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 348 | /// collectLineCounts - Collect line counts. This must be used after |
| 349 | /// reading .gcno and .gcda files. |
| 350 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 351 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 352 | E = Lines.end(); I != E; ++I) |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 353 | FI.addBlockLine(Parent.getFilename(), *I, this); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 356 | /// dump - Dump GCOVBlock content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 357 | void GCOVBlock::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 358 | dbgs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 359 | if (!SrcEdges.empty()) { |
| 360 | dbgs() << "\tSource Edges : "; |
| 361 | for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) { |
| 362 | const GCOVEdge *Edge = *I; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 363 | dbgs() << Edge->Src.Number << " (" << Edge->Count << "), "; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 364 | } |
| 365 | dbgs() << "\n"; |
| 366 | } |
| 367 | if (!DstEdges.empty()) { |
| 368 | dbgs() << "\tDestination Edges : "; |
| 369 | for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) { |
| 370 | const GCOVEdge *Edge = *I; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 371 | dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), "; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 372 | } |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 373 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 374 | } |
| 375 | if (!Lines.empty()) { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 376 | dbgs() << "\tLines : "; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 377 | for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(), |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 378 | E = Lines.end(); I != E; ++I) |
| 379 | dbgs() << (*I) << ","; |
| 380 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
| 384 | //===----------------------------------------------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 385 | // FileInfo implementation. |
| 386 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 387 | // Safe integer division, returns 0 if numerator is 0. |
| 388 | static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) { |
| 389 | if (!Numerator) |
| 390 | return 0; |
| 391 | return Numerator/Divisor; |
| 392 | } |
| 393 | |
| 394 | // This custom division function mimics gcov's branch ouputs: |
| 395 | // - Round to closest whole number |
| 396 | // - Only output 0% or 100% if it's exactly that value |
| 397 | static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) { |
| 398 | if (!Numerator) |
| 399 | return 0; |
| 400 | if (Numerator == Divisor) |
| 401 | return 100; |
| 402 | |
| 403 | uint8_t Res = (Numerator*100+Divisor/2) / Divisor; |
| 404 | if (Res == 0) |
| 405 | return 1; |
| 406 | if (Res == 100) |
| 407 | return 99; |
| 408 | return Res; |
| 409 | } |
| 410 | |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 411 | struct formatBranchInfo { |
| 412 | formatBranchInfo(const GCOVOptions &Options, uint64_t Count, |
| 413 | uint64_t Total) : |
| 414 | Options(Options), Count(Count), Total(Total) {} |
| 415 | |
| 416 | void print(raw_ostream &OS) const { |
| 417 | if (!Total) |
| 418 | OS << "never executed"; |
| 419 | else if (Options.BranchCount) |
| 420 | OS << "taken " << Count; |
| 421 | else |
| 422 | OS << "taken " << branchDiv(Count, Total) << "%"; |
| 423 | } |
| 424 | |
| 425 | const GCOVOptions &Options; |
| 426 | uint64_t Count; |
| 427 | uint64_t Total; |
| 428 | }; |
| 429 | |
| 430 | static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) { |
| 431 | FBI.print(OS); |
| 432 | return OS; |
| 433 | } |
| 434 | |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 435 | /// Convert a path to a gcov filename. If PreservePaths is true, this |
| 436 | /// translates "/" to "#", ".." to "^", and drops ".", to match gcov. |
| 437 | static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) { |
| 438 | if (!PreservePaths) |
Justin Bogner | c67f025 | 2014-04-23 21:44:55 +0000 | [diff] [blame] | 439 | return sys::path::filename(Filename).str(); |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 440 | |
| 441 | // This behaviour is defined by gcov in terms of text replacements, so it's |
| 442 | // not likely to do anything useful on filesystems with different textual |
| 443 | // conventions. |
| 444 | llvm::SmallString<256> Result(""); |
| 445 | StringRef::iterator I, S, E; |
| 446 | for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) { |
| 447 | if (*I != '/') |
| 448 | continue; |
| 449 | |
| 450 | if (I - S == 1 && *S == '.') { |
| 451 | // ".", the current directory, is skipped. |
| 452 | } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') { |
| 453 | // "..", the parent directory, is replaced with "^". |
| 454 | Result.append("^#"); |
| 455 | } else { |
| 456 | if (S < I) |
| 457 | // Leave other components intact, |
| 458 | Result.append(S, I); |
| 459 | // And separate with "#". |
| 460 | Result.push_back('#'); |
| 461 | } |
| 462 | S = I + 1; |
| 463 | } |
| 464 | |
| 465 | if (S < I) |
| 466 | Result.append(S, I); |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 467 | return Result.str(); |
| 468 | } |
| 469 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 470 | /// print - Print source files with collected line count information. |
Justin Bogner | c67f025 | 2014-04-23 21:44:55 +0000 | [diff] [blame] | 471 | void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, |
| 472 | StringRef GCDAFile) { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 473 | for (StringMap<LineData>::const_iterator I = LineInfo.begin(), |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 474 | E = LineInfo.end(); I != E; ++I) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 475 | StringRef Filename = I->first(); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 476 | std::unique_ptr<MemoryBuffer> Buff; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 477 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) { |
| 478 | errs() << Filename << ": " << ec.message() << "\n"; |
| 479 | return; |
| 480 | } |
Benjamin Kramer | 67421c1 | 2013-11-15 09:44:17 +0000 | [diff] [blame] | 481 | StringRef AllLines = Buff->getBuffer(); |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 482 | |
Justin Bogner | c67f025 | 2014-04-23 21:44:55 +0000 | [diff] [blame] | 483 | std::string CoveragePath; |
| 484 | if (Options.LongFileNames && !Filename.equals(MainFilename)) |
| 485 | CoveragePath = |
| 486 | mangleCoveragePath(MainFilename, Options.PreservePaths) + "##"; |
| 487 | CoveragePath += |
| 488 | mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov"; |
Yuchen Wu | 26326ad | 2013-12-03 00:57:11 +0000 | [diff] [blame] | 489 | std::string ErrorInfo; |
Rafael Espindola | 90c7f1c | 2014-02-24 18:20:12 +0000 | [diff] [blame] | 490 | raw_fd_ostream OS(CoveragePath.c_str(), ErrorInfo, sys::fs::F_Text); |
Yuchen Wu | 26326ad | 2013-12-03 00:57:11 +0000 | [diff] [blame] | 491 | if (!ErrorInfo.empty()) |
| 492 | errs() << ErrorInfo << "\n"; |
| 493 | |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 494 | OS << " -: 0:Source:" << Filename << "\n"; |
Yuchen Wu | 21517e4 | 2013-12-04 05:07:36 +0000 | [diff] [blame] | 495 | OS << " -: 0:Graph:" << GCNOFile << "\n"; |
| 496 | OS << " -: 0:Data:" << GCDAFile << "\n"; |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 497 | OS << " -: 0:Runs:" << RunCount << "\n"; |
| 498 | OS << " -: 0:Programs:" << ProgramCount << "\n"; |
| 499 | |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame] | 500 | const LineData &Line = I->second; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 501 | GCOVCoverage FileCoverage(Filename); |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 502 | for (uint32_t LineIndex = 0; !AllLines.empty(); ++LineIndex) { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 503 | if (Options.BranchInfo) { |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 504 | FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex); |
| 505 | if (FuncsIt != Line.Functions.end()) |
| 506 | printFunctionSummary(OS, FuncsIt->second); |
| 507 | } |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame] | 508 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 509 | BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex); |
| 510 | if (BlocksIt == Line.Blocks.end()) { |
| 511 | // No basic blocks are on this line. Not an executable line of code. |
| 512 | OS << " -:"; |
| 513 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
| 514 | OS << format("%5u:", LineIndex+1) << P.first << "\n"; |
| 515 | AllLines = P.second; |
| 516 | } else { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 517 | const BlockVector &Blocks = BlocksIt->second; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 518 | |
| 519 | // Add up the block counts to form line counts. |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 520 | DenseMap<const GCOVFunction *, bool> LineExecs; |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 521 | uint64_t LineCount = 0; |
| 522 | for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); |
| 523 | I != E; ++I) { |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 524 | const GCOVBlock *Block = *I; |
| 525 | if (Options.AllBlocks) { |
| 526 | // Only take the highest block count for that line. |
| 527 | uint64_t BlockCount = Block->getCount(); |
| 528 | LineCount = LineCount > BlockCount ? LineCount : BlockCount; |
| 529 | } else { |
| 530 | // Sum up all of the block counts. |
| 531 | LineCount += Block->getCount(); |
| 532 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 533 | |
| 534 | if (Options.FuncCoverage) { |
| 535 | // This is a slightly convoluted way to most accurately gather line |
| 536 | // statistics for functions. Basically what is happening is that we |
| 537 | // don't want to count a single line with multiple blocks more than |
| 538 | // once. However, we also don't simply want to give the total line |
| 539 | // count to every function that starts on the line. Thus, what is |
| 540 | // happening here are two things: |
| 541 | // 1) Ensure that the number of logical lines is only incremented |
| 542 | // once per function. |
| 543 | // 2) If there are multiple blocks on the same line, ensure that the |
| 544 | // number of lines executed is incremented as long as at least |
| 545 | // one of the blocks are executed. |
| 546 | const GCOVFunction *Function = &Block->getParent(); |
| 547 | if (FuncCoverages.find(Function) == FuncCoverages.end()) { |
| 548 | std::pair<const GCOVFunction *, GCOVCoverage> |
| 549 | KeyValue(Function, GCOVCoverage(Function->getName())); |
| 550 | FuncCoverages.insert(KeyValue); |
| 551 | } |
| 552 | GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; |
| 553 | |
| 554 | if (LineExecs.find(Function) == LineExecs.end()) { |
| 555 | if (Block->getCount()) { |
| 556 | ++FuncCoverage.LinesExec; |
| 557 | LineExecs[Function] = true; |
| 558 | } else { |
| 559 | LineExecs[Function] = false; |
| 560 | } |
| 561 | ++FuncCoverage.LogicalLines; |
| 562 | } else if (!LineExecs[Function] && Block->getCount()) { |
| 563 | ++FuncCoverage.LinesExec; |
| 564 | LineExecs[Function] = true; |
| 565 | } |
| 566 | } |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 567 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 568 | |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 569 | if (LineCount == 0) |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 570 | OS << " #####:"; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 571 | else { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 572 | OS << format("%9" PRIu64 ":", LineCount); |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 573 | ++FileCoverage.LinesExec; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 574 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 575 | ++FileCoverage.LogicalLines; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 576 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 577 | std::pair<StringRef, StringRef> P = AllLines.split('\n'); |
| 578 | OS << format("%5u:", LineIndex+1) << P.first << "\n"; |
| 579 | AllLines = P.second; |
| 580 | |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 581 | uint32_t BlockNo = 0; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 582 | uint32_t EdgeNo = 0; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 583 | for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); |
| 584 | I != E; ++I) { |
| 585 | const GCOVBlock *Block = *I; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 586 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 587 | // Only print block and branch information at the end of the block. |
| 588 | if (Block->getLastLine() != LineIndex+1) |
| 589 | continue; |
| 590 | if (Options.AllBlocks) |
| 591 | printBlockInfo(OS, *Block, LineIndex, BlockNo); |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 592 | if (Options.BranchInfo) { |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 593 | size_t NumEdges = Block->getNumDstEdges(); |
| 594 | if (NumEdges > 1) |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 595 | printBranchInfo(OS, *Block, FileCoverage, EdgeNo); |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 596 | else if (Options.UncondBranch && NumEdges == 1) |
| 597 | printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count); |
| 598 | } |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 601 | } |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 602 | FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage)); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 603 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 604 | |
| 605 | // FIXME: There is no way to detect calls given current instrumentation. |
| 606 | if (Options.FuncCoverage) |
| 607 | printFuncCoverage(); |
| 608 | printFileCoverage(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 609 | } |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 610 | |
| 611 | /// printFunctionSummary - Print function and block summary. |
| 612 | void FileInfo::printFunctionSummary(raw_fd_ostream &OS, |
| 613 | const FunctionVector &Funcs) const { |
| 614 | for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end(); |
| 615 | I != E; ++I) { |
| 616 | const GCOVFunction *Func = *I; |
| 617 | uint64_t EntryCount = Func->getEntryCount(); |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 618 | uint32_t BlocksExec = 0; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 619 | for (GCOVFunction::BlockIterator I = Func->block_begin(), |
| 620 | E = Func->block_end(); I != E; ++I) { |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 621 | const GCOVBlock &Block = **I; |
| 622 | if (Block.getNumDstEdges() && Block.getCount()) |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 623 | ++BlocksExec; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | OS << "function " << Func->getName() << " called " << EntryCount |
| 627 | << " returned " << safeDiv(Func->getExitCount()*100, EntryCount) |
| 628 | << "% blocks executed " |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 629 | << safeDiv(BlocksExec*100, Func->getNumBlocks()-1) << "%\n"; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
| 633 | /// printBlockInfo - Output counts for each block. |
| 634 | void FileInfo::printBlockInfo(raw_fd_ostream &OS, const GCOVBlock &Block, |
| 635 | uint32_t LineIndex, uint32_t &BlockNo) const { |
| 636 | if (Block.getCount() == 0) |
| 637 | OS << " $$$$$:"; |
| 638 | else |
| 639 | OS << format("%9" PRIu64 ":", Block.getCount()); |
| 640 | OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++); |
| 641 | } |
| 642 | |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 643 | /// printBranchInfo - Print conditional branch probabilities. |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 644 | void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block, |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 645 | GCOVCoverage &Coverage, uint32_t &EdgeNo) { |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 646 | SmallVector<uint64_t, 16> BranchCounts; |
| 647 | uint64_t TotalCounts = 0; |
| 648 | for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end(); |
| 649 | I != E; ++I) { |
| 650 | const GCOVEdge *Edge = *I; |
| 651 | BranchCounts.push_back(Edge->Count); |
| 652 | TotalCounts += Edge->Count; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 653 | if (Block.getCount()) ++Coverage.BranchesExec; |
| 654 | if (Edge->Count) ++Coverage.BranchesTaken; |
| 655 | ++Coverage.Branches; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 656 | |
| 657 | if (Options.FuncCoverage) { |
| 658 | const GCOVFunction *Function = &Block.getParent(); |
| 659 | GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; |
| 660 | if (Block.getCount()) ++FuncCoverage.BranchesExec; |
| 661 | if (Edge->Count) ++FuncCoverage.BranchesTaken; |
| 662 | ++FuncCoverage.Branches; |
| 663 | } |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(), |
| 667 | E = BranchCounts.end(); I != E; ++I) { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 668 | OS << format("branch %2u ", EdgeNo++) |
| 669 | << formatBranchInfo(Options, *I, TotalCounts) << "\n"; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 672 | |
| 673 | /// printUncondBranchInfo - Print unconditional branch probabilities. |
| 674 | void FileInfo::printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo, |
| 675 | uint64_t Count) const { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 676 | OS << format("unconditional %2u ", EdgeNo++) |
| 677 | << formatBranchInfo(Options, Count, Count) << "\n"; |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 678 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 679 | |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 680 | // printCoverage - Print generic coverage info used by both printFuncCoverage |
| 681 | // and printFileCoverage. |
| 682 | void FileInfo::printCoverage(const GCOVCoverage &Coverage) const { |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 683 | outs() << format("Lines executed:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 684 | double(Coverage.LinesExec)*100/Coverage.LogicalLines, |
| 685 | Coverage.LogicalLines); |
| 686 | if (Options.BranchInfo) { |
| 687 | if (Coverage.Branches) { |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 688 | outs() << format("Branches executed:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 689 | double(Coverage.BranchesExec)*100/Coverage.Branches, |
| 690 | Coverage.Branches); |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 691 | outs() << format("Taken at least once:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 692 | double(Coverage.BranchesTaken)*100/Coverage.Branches, |
| 693 | Coverage.Branches); |
| 694 | } else { |
| 695 | outs() << "No branches\n"; |
| 696 | } |
| 697 | outs() << "No calls\n"; // to be consistent with gcov |
| 698 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | // printFuncCoverage - Print per-function coverage info. |
| 702 | void FileInfo::printFuncCoverage() const { |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 703 | for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(), |
| 704 | E = FuncCoverages.end(); I != E; ++I) { |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 705 | const GCOVCoverage &Coverage = I->second; |
| 706 | outs() << "Function '" << Coverage.Name << "'\n"; |
| 707 | printCoverage(Coverage); |
| 708 | outs() << "\n"; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | // printFileCoverage - Print per-file coverage info. |
| 713 | void FileInfo::printFileCoverage() const { |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 714 | for (FileCoverageList::const_iterator I = FileCoverages.begin(), |
| 715 | E = FileCoverages.end(); I != E; ++I) { |
| 716 | const std::string &Filename = I->first; |
| 717 | const GCOVCoverage &Coverage = I->second; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 718 | outs() << "File '" << Coverage.Name << "'\n"; |
| 719 | printCoverage(Coverage); |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 720 | outs() << Coverage.Name << ":creating '" << Filename << "'\n\n"; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 721 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 722 | } |