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