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" |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 23 | #include <system_error> |
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 { |
Justin Bogner | 58e4134 | 2014-11-06 06:55:02 +0000 | [diff] [blame^] | 301 | dbgs() << "===== " << Name << " (" << Ident << ") @ " << Filename << ":" |
| 302 | << LineNumber << "\n"; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 303 | for (const auto &Block : Blocks) |
| 304 | Block->dump(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /// collectLineCounts - Collect line counts. This must be used after |
| 308 | /// reading .gcno and .gcda files. |
| 309 | void GCOVFunction::collectLineCounts(FileInfo &FI) { |
Justin Bogner | 95e0a70 | 2014-03-26 22:03:06 +0000 | [diff] [blame] | 310 | // If the line number is zero, this is a function that doesn't actually appear |
| 311 | // in the source file, so there isn't anything we can do with it. |
| 312 | if (LineNumber == 0) |
| 313 | return; |
| 314 | |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 315 | for (const auto &Block : Blocks) |
| 316 | Block->collectLineCounts(FI); |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 317 | FI.addFunctionLine(Filename, LineNumber, this); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | //===----------------------------------------------------------------------===// |
| 321 | // GCOVBlock implementation. |
| 322 | |
| 323 | /// ~GCOVBlock - Delete GCOVBlock and its content. |
| 324 | GCOVBlock::~GCOVBlock() { |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 325 | SrcEdges.clear(); |
| 326 | DstEdges.clear(); |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 327 | Lines.clear(); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 330 | /// addCount - Add to block counter while storing the edge count. If the |
| 331 | /// destination has no outgoing edges, also update that block's count too. |
| 332 | void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) { |
| 333 | assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid |
| 334 | DstEdges[DstEdgeNo]->Count = N; |
| 335 | Counter += N; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 336 | if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges()) |
| 337 | DstEdges[DstEdgeNo]->Dst.Counter += N; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 340 | /// sortDstEdges - Sort destination edges by block number, nop if already |
| 341 | /// sorted. This is required for printing branch info in the correct order. |
| 342 | void GCOVBlock::sortDstEdges() { |
| 343 | if (!DstEdgesAreSorted) { |
| 344 | SortDstEdgesFunctor SortEdges; |
| 345 | std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges); |
| 346 | } |
| 347 | } |
| 348 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 349 | /// collectLineCounts - Collect line counts. This must be used after |
| 350 | /// reading .gcno and .gcda files. |
| 351 | void GCOVBlock::collectLineCounts(FileInfo &FI) { |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 352 | for (SmallVectorImpl<uint32_t>::iterator I = Lines.begin(), |
Bill Wendling | ea6397f | 2012-07-19 00:11:40 +0000 | [diff] [blame] | 353 | E = Lines.end(); I != E; ++I) |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 354 | FI.addBlockLine(Parent.getFilename(), *I, this); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 357 | /// dump - Dump GCOVBlock content to dbgs() for debugging purposes. |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 358 | void GCOVBlock::dump() const { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 359 | dbgs() << "Block : " << Number << " Counter : " << Counter << "\n"; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 360 | if (!SrcEdges.empty()) { |
| 361 | dbgs() << "\tSource Edges : "; |
| 362 | for (EdgeIterator I = SrcEdges.begin(), E = SrcEdges.end(); I != E; ++I) { |
| 363 | const GCOVEdge *Edge = *I; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 364 | dbgs() << Edge->Src.Number << " (" << Edge->Count << "), "; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 365 | } |
| 366 | dbgs() << "\n"; |
| 367 | } |
| 368 | if (!DstEdges.empty()) { |
| 369 | dbgs() << "\tDestination Edges : "; |
| 370 | for (EdgeIterator I = DstEdges.begin(), E = DstEdges.end(); I != E; ++I) { |
| 371 | const GCOVEdge *Edge = *I; |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 372 | dbgs() << Edge->Dst.Number << " (" << Edge->Count << "), "; |
Yuchen Wu | 8ad9b04 | 2013-12-03 00:24:44 +0000 | [diff] [blame] | 373 | } |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 374 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 375 | } |
| 376 | if (!Lines.empty()) { |
Yuchen Wu | 0367815 | 2013-10-25 02:22:24 +0000 | [diff] [blame] | 377 | dbgs() << "\tLines : "; |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 378 | for (SmallVectorImpl<uint32_t>::const_iterator I = Lines.begin(), |
Yuchen Wu | d738bee | 2013-11-14 00:32:00 +0000 | [diff] [blame] | 379 | E = Lines.end(); I != E; ++I) |
| 380 | dbgs() << (*I) << ","; |
| 381 | dbgs() << "\n"; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | //===----------------------------------------------------------------------===// |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 386 | // FileInfo implementation. |
| 387 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 388 | // Safe integer division, returns 0 if numerator is 0. |
| 389 | static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) { |
| 390 | if (!Numerator) |
| 391 | return 0; |
| 392 | return Numerator/Divisor; |
| 393 | } |
| 394 | |
| 395 | // This custom division function mimics gcov's branch ouputs: |
| 396 | // - Round to closest whole number |
| 397 | // - Only output 0% or 100% if it's exactly that value |
| 398 | static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) { |
| 399 | if (!Numerator) |
| 400 | return 0; |
| 401 | if (Numerator == Divisor) |
| 402 | return 100; |
| 403 | |
| 404 | uint8_t Res = (Numerator*100+Divisor/2) / Divisor; |
| 405 | if (Res == 0) |
| 406 | return 1; |
| 407 | if (Res == 100) |
| 408 | return 99; |
| 409 | return Res; |
| 410 | } |
| 411 | |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 412 | struct formatBranchInfo { |
| 413 | formatBranchInfo(const GCOVOptions &Options, uint64_t Count, |
| 414 | uint64_t Total) : |
| 415 | Options(Options), Count(Count), Total(Total) {} |
| 416 | |
| 417 | void print(raw_ostream &OS) const { |
| 418 | if (!Total) |
| 419 | OS << "never executed"; |
| 420 | else if (Options.BranchCount) |
| 421 | OS << "taken " << Count; |
| 422 | else |
| 423 | OS << "taken " << branchDiv(Count, Total) << "%"; |
| 424 | } |
| 425 | |
| 426 | const GCOVOptions &Options; |
| 427 | uint64_t Count; |
| 428 | uint64_t Total; |
| 429 | }; |
| 430 | |
| 431 | static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) { |
| 432 | FBI.print(OS); |
| 433 | return OS; |
| 434 | } |
| 435 | |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 436 | namespace { |
| 437 | class LineConsumer { |
| 438 | std::unique_ptr<MemoryBuffer> Buffer; |
| 439 | StringRef Remaining; |
| 440 | public: |
| 441 | LineConsumer(StringRef Filename) { |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 442 | ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = |
| 443 | MemoryBuffer::getFileOrSTDIN(Filename); |
| 444 | if (std::error_code EC = BufferOrErr.getError()) { |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 445 | errs() << Filename << ": " << EC.message() << "\n"; |
| 446 | Remaining = ""; |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 447 | } else { |
| 448 | Buffer = std::move(BufferOrErr.get()); |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 449 | Remaining = Buffer->getBuffer(); |
Rafael Espindola | adf21f2 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 450 | } |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 451 | } |
| 452 | bool empty() { return Remaining.empty(); } |
| 453 | void printNext(raw_ostream &OS, uint32_t LineNum) { |
| 454 | StringRef Line; |
| 455 | if (empty()) |
| 456 | Line = "/*EOF*/"; |
| 457 | else |
| 458 | std::tie(Line, Remaining) = Remaining.split("\n"); |
| 459 | OS << format("%5u:", LineNum) << Line << "\n"; |
| 460 | } |
| 461 | }; |
| 462 | } |
| 463 | |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 464 | /// Convert a path to a gcov filename. If PreservePaths is true, this |
| 465 | /// translates "/" to "#", ".." to "^", and drops ".", to match gcov. |
| 466 | static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) { |
| 467 | if (!PreservePaths) |
Justin Bogner | c67f025 | 2014-04-23 21:44:55 +0000 | [diff] [blame] | 468 | return sys::path::filename(Filename).str(); |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 469 | |
| 470 | // This behaviour is defined by gcov in terms of text replacements, so it's |
| 471 | // not likely to do anything useful on filesystems with different textual |
| 472 | // conventions. |
| 473 | llvm::SmallString<256> Result(""); |
| 474 | StringRef::iterator I, S, E; |
| 475 | for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) { |
| 476 | if (*I != '/') |
| 477 | continue; |
| 478 | |
| 479 | if (I - S == 1 && *S == '.') { |
| 480 | // ".", the current directory, is skipped. |
| 481 | } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') { |
| 482 | // "..", the parent directory, is replaced with "^". |
| 483 | Result.append("^#"); |
| 484 | } else { |
| 485 | if (S < I) |
| 486 | // Leave other components intact, |
| 487 | Result.append(S, I); |
| 488 | // And separate with "#". |
| 489 | Result.push_back('#'); |
| 490 | } |
| 491 | S = I + 1; |
| 492 | } |
| 493 | |
| 494 | if (S < I) |
| 495 | Result.append(S, I); |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 496 | return Result.str(); |
| 497 | } |
| 498 | |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 499 | std::string FileInfo::getCoveragePath(StringRef Filename, |
| 500 | StringRef MainFilename) { |
| 501 | if (Options.NoOutput) |
| 502 | // This is probably a bug in gcov, but when -n is specified, paths aren't |
| 503 | // mangled at all, and the -l and -p options are ignored. Here, we do the |
| 504 | // same. |
| 505 | return Filename; |
| 506 | |
| 507 | std::string CoveragePath; |
| 508 | if (Options.LongFileNames && !Filename.equals(MainFilename)) |
| 509 | CoveragePath = |
| 510 | mangleCoveragePath(MainFilename, Options.PreservePaths) + "##"; |
| 511 | CoveragePath += |
| 512 | mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov"; |
| 513 | return CoveragePath; |
| 514 | } |
| 515 | |
| 516 | std::unique_ptr<raw_ostream> |
| 517 | FileInfo::openCoveragePath(StringRef CoveragePath) { |
| 518 | if (Options.NoOutput) |
Justin Bogner | 7c09373 | 2014-05-07 16:01:27 +0000 | [diff] [blame] | 519 | return llvm::make_unique<raw_null_ostream>(); |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 520 | |
Rafael Espindola | 3fd1e99 | 2014-08-25 18:16:47 +0000 | [diff] [blame] | 521 | std::error_code EC; |
| 522 | auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC, |
| 523 | sys::fs::F_Text); |
| 524 | if (EC) { |
| 525 | errs() << EC.message() << "\n"; |
Justin Bogner | 7c09373 | 2014-05-07 16:01:27 +0000 | [diff] [blame] | 526 | return llvm::make_unique<raw_null_ostream>(); |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 527 | } |
| 528 | return std::move(OS); |
| 529 | } |
| 530 | |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 531 | /// print - Print source files with collected line count information. |
Justin Bogner | c67f025 | 2014-04-23 21:44:55 +0000 | [diff] [blame] | 532 | void FileInfo::print(StringRef MainFilename, StringRef GCNOFile, |
| 533 | StringRef GCDAFile) { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 534 | for (StringMap<LineData>::const_iterator I = LineInfo.begin(), |
Yuchen Wu | ef6909d | 2013-11-19 20:33:32 +0000 | [diff] [blame] | 535 | E = LineInfo.end(); I != E; ++I) { |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 536 | StringRef Filename = I->first(); |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 537 | auto AllLines = LineConsumer(Filename); |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 538 | |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 539 | std::string CoveragePath = getCoveragePath(Filename, MainFilename); |
| 540 | std::unique_ptr<raw_ostream> S = openCoveragePath(CoveragePath); |
| 541 | raw_ostream &OS = *S; |
Yuchen Wu | 26326ad | 2013-12-03 00:57:11 +0000 | [diff] [blame] | 542 | |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 543 | OS << " -: 0:Source:" << Filename << "\n"; |
Yuchen Wu | 21517e4 | 2013-12-04 05:07:36 +0000 | [diff] [blame] | 544 | OS << " -: 0:Graph:" << GCNOFile << "\n"; |
| 545 | OS << " -: 0:Data:" << GCDAFile << "\n"; |
Yuchen Wu | 8aac4f6 | 2013-11-19 20:57:20 +0000 | [diff] [blame] | 546 | OS << " -: 0:Runs:" << RunCount << "\n"; |
| 547 | OS << " -: 0:Programs:" << ProgramCount << "\n"; |
| 548 | |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame] | 549 | const LineData &Line = I->second; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 550 | GCOVCoverage FileCoverage(Filename); |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 551 | for (uint32_t LineIndex = 0; |
| 552 | LineIndex < Line.LastLine || !AllLines.empty(); ++LineIndex) { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 553 | if (Options.BranchInfo) { |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 554 | FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex); |
| 555 | if (FuncsIt != Line.Functions.end()) |
| 556 | printFunctionSummary(OS, FuncsIt->second); |
| 557 | } |
Yuchen Wu | 1c06816 | 2013-12-03 01:35:31 +0000 | [diff] [blame] | 558 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 559 | BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex); |
| 560 | if (BlocksIt == Line.Blocks.end()) { |
| 561 | // No basic blocks are on this line. Not an executable line of code. |
| 562 | OS << " -:"; |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 563 | AllLines.printNext(OS, LineIndex + 1); |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 564 | } else { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 565 | const BlockVector &Blocks = BlocksIt->second; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 566 | |
| 567 | // Add up the block counts to form line counts. |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 568 | DenseMap<const GCOVFunction *, bool> LineExecs; |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 569 | uint64_t LineCount = 0; |
| 570 | for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); |
| 571 | I != E; ++I) { |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 572 | const GCOVBlock *Block = *I; |
| 573 | if (Options.AllBlocks) { |
| 574 | // Only take the highest block count for that line. |
| 575 | uint64_t BlockCount = Block->getCount(); |
| 576 | LineCount = LineCount > BlockCount ? LineCount : BlockCount; |
| 577 | } else { |
| 578 | // Sum up all of the block counts. |
| 579 | LineCount += Block->getCount(); |
| 580 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 581 | |
| 582 | if (Options.FuncCoverage) { |
| 583 | // This is a slightly convoluted way to most accurately gather line |
| 584 | // statistics for functions. Basically what is happening is that we |
| 585 | // don't want to count a single line with multiple blocks more than |
| 586 | // once. However, we also don't simply want to give the total line |
| 587 | // count to every function that starts on the line. Thus, what is |
| 588 | // happening here are two things: |
| 589 | // 1) Ensure that the number of logical lines is only incremented |
| 590 | // once per function. |
| 591 | // 2) If there are multiple blocks on the same line, ensure that the |
| 592 | // number of lines executed is incremented as long as at least |
| 593 | // one of the blocks are executed. |
| 594 | const GCOVFunction *Function = &Block->getParent(); |
| 595 | if (FuncCoverages.find(Function) == FuncCoverages.end()) { |
| 596 | std::pair<const GCOVFunction *, GCOVCoverage> |
| 597 | KeyValue(Function, GCOVCoverage(Function->getName())); |
| 598 | FuncCoverages.insert(KeyValue); |
| 599 | } |
| 600 | GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; |
| 601 | |
| 602 | if (LineExecs.find(Function) == LineExecs.end()) { |
| 603 | if (Block->getCount()) { |
| 604 | ++FuncCoverage.LinesExec; |
| 605 | LineExecs[Function] = true; |
| 606 | } else { |
| 607 | LineExecs[Function] = false; |
| 608 | } |
| 609 | ++FuncCoverage.LogicalLines; |
| 610 | } else if (!LineExecs[Function] && Block->getCount()) { |
| 611 | ++FuncCoverage.LinesExec; |
| 612 | LineExecs[Function] = true; |
| 613 | } |
| 614 | } |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 615 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 616 | |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 617 | if (LineCount == 0) |
Yuchen Wu | dbcf197 | 2013-11-02 00:09:17 +0000 | [diff] [blame] | 618 | OS << " #####:"; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 619 | else { |
Yuchen Wu | 8f1c881 | 2013-12-03 00:38:21 +0000 | [diff] [blame] | 620 | OS << format("%9" PRIu64 ":", LineCount); |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 621 | ++FileCoverage.LinesExec; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 622 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 623 | ++FileCoverage.LogicalLines; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 624 | |
Justin Bogner | cf27e1b | 2014-05-07 02:11:23 +0000 | [diff] [blame] | 625 | AllLines.printNext(OS, LineIndex + 1); |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 626 | |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 627 | uint32_t BlockNo = 0; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 628 | uint32_t EdgeNo = 0; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 629 | for (BlockVector::const_iterator I = Blocks.begin(), E = Blocks.end(); |
| 630 | I != E; ++I) { |
| 631 | const GCOVBlock *Block = *I; |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 632 | |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 633 | // Only print block and branch information at the end of the block. |
| 634 | if (Block->getLastLine() != LineIndex+1) |
| 635 | continue; |
| 636 | if (Options.AllBlocks) |
| 637 | printBlockInfo(OS, *Block, LineIndex, BlockNo); |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 638 | if (Options.BranchInfo) { |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 639 | size_t NumEdges = Block->getNumDstEdges(); |
| 640 | if (NumEdges > 1) |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 641 | printBranchInfo(OS, *Block, FileCoverage, EdgeNo); |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 642 | else if (Options.UncondBranch && NumEdges == 1) |
| 643 | printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count); |
| 644 | } |
Yuchen Wu | 8c6bb5f | 2013-12-10 01:02:07 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 647 | } |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 648 | FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage)); |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 649 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 650 | |
| 651 | // FIXME: There is no way to detect calls given current instrumentation. |
| 652 | if (Options.FuncCoverage) |
| 653 | printFuncCoverage(); |
| 654 | printFileCoverage(); |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 655 | return; |
Devang Patel | 3714065 | 2011-09-28 18:50:00 +0000 | [diff] [blame] | 656 | } |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 657 | |
| 658 | /// printFunctionSummary - Print function and block summary. |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 659 | void FileInfo::printFunctionSummary(raw_ostream &OS, |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 660 | const FunctionVector &Funcs) const { |
| 661 | for (FunctionVector::const_iterator I = Funcs.begin(), E = Funcs.end(); |
| 662 | I != E; ++I) { |
| 663 | const GCOVFunction *Func = *I; |
| 664 | uint64_t EntryCount = Func->getEntryCount(); |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 665 | uint32_t BlocksExec = 0; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 666 | for (GCOVFunction::BlockIterator I = Func->block_begin(), |
| 667 | E = Func->block_end(); I != E; ++I) { |
David Blaikie | 0975749 | 2014-04-21 21:40:16 +0000 | [diff] [blame] | 668 | const GCOVBlock &Block = **I; |
| 669 | if (Block.getNumDstEdges() && Block.getCount()) |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 670 | ++BlocksExec; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | OS << "function " << Func->getName() << " called " << EntryCount |
| 674 | << " returned " << safeDiv(Func->getExitCount()*100, EntryCount) |
| 675 | << "% blocks executed " |
Yuchen Wu | c9b2dcd | 2013-12-18 18:46:25 +0000 | [diff] [blame] | 676 | << safeDiv(BlocksExec*100, Func->getNumBlocks()-1) << "%\n"; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | |
| 680 | /// printBlockInfo - Output counts for each block. |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 681 | void FileInfo::printBlockInfo(raw_ostream &OS, const GCOVBlock &Block, |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 682 | uint32_t LineIndex, uint32_t &BlockNo) const { |
| 683 | if (Block.getCount() == 0) |
| 684 | OS << " $$$$$:"; |
| 685 | else |
| 686 | OS << format("%9" PRIu64 ":", Block.getCount()); |
| 687 | OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++); |
| 688 | } |
| 689 | |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 690 | /// printBranchInfo - Print conditional branch probabilities. |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 691 | void FileInfo::printBranchInfo(raw_ostream &OS, const GCOVBlock &Block, |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 692 | GCOVCoverage &Coverage, uint32_t &EdgeNo) { |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 693 | SmallVector<uint64_t, 16> BranchCounts; |
| 694 | uint64_t TotalCounts = 0; |
| 695 | for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end(); |
| 696 | I != E; ++I) { |
| 697 | const GCOVEdge *Edge = *I; |
| 698 | BranchCounts.push_back(Edge->Count); |
| 699 | TotalCounts += Edge->Count; |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 700 | if (Block.getCount()) ++Coverage.BranchesExec; |
| 701 | if (Edge->Count) ++Coverage.BranchesTaken; |
| 702 | ++Coverage.Branches; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 703 | |
| 704 | if (Options.FuncCoverage) { |
| 705 | const GCOVFunction *Function = &Block.getParent(); |
| 706 | GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second; |
| 707 | if (Block.getCount()) ++FuncCoverage.BranchesExec; |
| 708 | if (Edge->Count) ++FuncCoverage.BranchesTaken; |
| 709 | ++FuncCoverage.Branches; |
| 710 | } |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(), |
| 714 | E = BranchCounts.end(); I != E; ++I) { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 715 | OS << format("branch %2u ", EdgeNo++) |
| 716 | << formatBranchInfo(Options, *I, TotalCounts) << "\n"; |
Yuchen Wu | 342714c | 2013-12-13 01:15:07 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 719 | |
| 720 | /// printUncondBranchInfo - Print unconditional branch probabilities. |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 721 | void FileInfo::printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo, |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 722 | uint64_t Count) const { |
Yuchen Wu | 73dc381 | 2013-12-18 18:40:15 +0000 | [diff] [blame] | 723 | OS << format("unconditional %2u ", EdgeNo++) |
| 724 | << formatBranchInfo(Options, Count, Count) << "\n"; |
Yuchen Wu | 66d93b8 | 2013-12-16 22:14:02 +0000 | [diff] [blame] | 725 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 726 | |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 727 | // printCoverage - Print generic coverage info used by both printFuncCoverage |
| 728 | // and printFileCoverage. |
| 729 | void FileInfo::printCoverage(const GCOVCoverage &Coverage) const { |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 730 | outs() << format("Lines executed:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 731 | double(Coverage.LinesExec)*100/Coverage.LogicalLines, |
| 732 | Coverage.LogicalLines); |
| 733 | if (Options.BranchInfo) { |
| 734 | if (Coverage.Branches) { |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 735 | outs() << format("Branches executed:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 736 | double(Coverage.BranchesExec)*100/Coverage.Branches, |
| 737 | Coverage.Branches); |
NAKAMURA Takumi | 6e3c423 | 2013-12-19 08:46:28 +0000 | [diff] [blame] | 738 | outs() << format("Taken at least once:%.2f%% of %u\n", |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 739 | double(Coverage.BranchesTaken)*100/Coverage.Branches, |
| 740 | Coverage.Branches); |
| 741 | } else { |
| 742 | outs() << "No branches\n"; |
| 743 | } |
| 744 | outs() << "No calls\n"; // to be consistent with gcov |
| 745 | } |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | // printFuncCoverage - Print per-function coverage info. |
| 749 | void FileInfo::printFuncCoverage() const { |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 750 | for (FuncCoverageMap::const_iterator I = FuncCoverages.begin(), |
| 751 | E = FuncCoverages.end(); I != E; ++I) { |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 752 | const GCOVCoverage &Coverage = I->second; |
| 753 | outs() << "Function '" << Coverage.Name << "'\n"; |
| 754 | printCoverage(Coverage); |
| 755 | outs() << "\n"; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | // printFileCoverage - Print per-file coverage info. |
| 760 | void FileInfo::printFileCoverage() const { |
Justin Bogner | c6af350 | 2014-02-04 10:45:02 +0000 | [diff] [blame] | 761 | for (FileCoverageList::const_iterator I = FileCoverages.begin(), |
| 762 | E = FileCoverages.end(); I != E; ++I) { |
| 763 | const std::string &Filename = I->first; |
| 764 | const GCOVCoverage &Coverage = I->second; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 765 | outs() << "File '" << Coverage.Name << "'\n"; |
| 766 | printCoverage(Coverage); |
Justin Bogner | 1a18d7c | 2014-05-07 02:11:18 +0000 | [diff] [blame] | 767 | if (!Options.NoOutput) |
| 768 | outs() << Coverage.Name << ":creating '" << Filename << "'\n"; |
| 769 | outs() << "\n"; |
Yuchen Wu | bb6a477 | 2013-12-19 00:29:25 +0000 | [diff] [blame] | 770 | } |
Yuchen Wu | 8256ee6 | 2013-12-18 21:12:51 +0000 | [diff] [blame] | 771 | } |