1 //===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // GCOV implements the interface to read and write coverage files that use
10 // 'gcov' format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ProfileData/GCOV.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Demangle/Demangle.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/MD5.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_ostream.h"
24 #include <algorithm>
25 #include <system_error>
26 #include <unordered_map>
27
28 using namespace llvm;
29
30 enum : uint32_t {
31 GCOV_ARC_ON_TREE = 1 << 0,
32 GCOV_ARC_FALLTHROUGH = 1 << 2,
33
34 GCOV_TAG_FUNCTION = 0x01000000,
35 GCOV_TAG_BLOCKS = 0x01410000,
36 GCOV_TAG_ARCS = 0x01430000,
37 GCOV_TAG_LINES = 0x01450000,
38 GCOV_TAG_COUNTER_ARCS = 0x01a10000,
39 // GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.
40 GCOV_TAG_OBJECT_SUMMARY = 0xa1000000,
41 GCOV_TAG_PROGRAM_SUMMARY = 0xa3000000,
42 };
43
44 namespace {
45 struct Summary {
Summary__anonf833bdb00211::Summary46 Summary(StringRef Name) : Name(Name) {}
47
48 StringRef Name;
49 uint64_t lines = 0;
50 uint64_t linesExec = 0;
51 uint64_t branches = 0;
52 uint64_t branchesExec = 0;
53 uint64_t branchesTaken = 0;
54 };
55
56 struct LineInfo {
57 SmallVector<const GCOVBlock *, 1> blocks;
58 uint64_t count = 0;
59 bool exists = false;
60 };
61
62 struct SourceInfo {
63 StringRef filename;
64 SmallString<0> displayName;
65 std::vector<std::vector<const GCOVFunction *>> startLineToFunctions;
66 std::vector<LineInfo> lines;
67 bool ignored = false;
SourceInfo__anonf833bdb00211::SourceInfo68 SourceInfo(StringRef filename) : filename(filename) {}
69 };
70
71 class Context {
72 public:
Context(const GCOV::Options & Options)73 Context(const GCOV::Options &Options) : options(Options) {}
74 void print(StringRef filename, StringRef gcno, StringRef gcda,
75 GCOVFile &file);
76
77 private:
78 std::string getCoveragePath(StringRef filename, StringRef mainFilename) const;
79 void printFunctionDetails(const GCOVFunction &f, raw_ostream &os) const;
80 void printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,
81 raw_ostream &OS) const;
82 void printSummary(const Summary &summary, raw_ostream &os) const;
83
84 void collectFunction(GCOVFunction &f, Summary &summary);
85 void collectSourceLine(SourceInfo &si, Summary *summary, LineInfo &line,
86 size_t lineNum) const;
87 void collectSource(SourceInfo &si, Summary &summary) const;
88 void annotateSource(SourceInfo &si, const GCOVFile &file, StringRef gcno,
89 StringRef gcda, raw_ostream &os) const;
90 void printSourceToIntermediate(const SourceInfo &si, raw_ostream &os) const;
91
92 const GCOV::Options &options;
93 std::vector<SourceInfo> sources;
94 };
95 } // namespace
96
97 //===----------------------------------------------------------------------===//
98 // GCOVFile implementation.
99
100 /// readGCNO - Read GCNO buffer.
readGCNO(GCOVBuffer & buf)101 bool GCOVFile::readGCNO(GCOVBuffer &buf) {
102 if (!buf.readGCNOFormat())
103 return false;
104 if (!buf.readGCOVVersion(Version))
105 return false;
106
107 Checksum = buf.getWord();
108 if (Version >= GCOV::V900)
109 cwd = buf.getString();
110 if (Version >= GCOV::V800)
111 buf.getWord(); // hasUnexecutedBlocks
112
113 uint32_t tag, length;
114 GCOVFunction *fn;
115 while ((tag = buf.getWord())) {
116 if (!buf.readInt(length))
117 return false;
118 if (tag == GCOV_TAG_FUNCTION) {
119 functions.push_back(std::make_unique<GCOVFunction>(*this));
120 fn = functions.back().get();
121 fn->ident = buf.getWord();
122 fn->linenoChecksum = buf.getWord();
123 if (Version >= GCOV::V407)
124 fn->cfgChecksum = buf.getWord();
125 buf.readString(fn->Name);
126 StringRef filename;
127 if (Version < GCOV::V800) {
128 filename = buf.getString();
129 fn->startLine = buf.getWord();
130 } else {
131 fn->artificial = buf.getWord();
132 filename = buf.getString();
133 fn->startLine = buf.getWord();
134 fn->startColumn = buf.getWord();
135 fn->endLine = buf.getWord();
136 if (Version >= GCOV::V900)
137 fn->endColumn = buf.getWord();
138 }
139 auto r = filenameToIdx.try_emplace(filename, filenameToIdx.size());
140 if (r.second)
141 filenames.emplace_back(filename);
142 fn->srcIdx = r.first->second;
143 IdentToFunction[fn->ident] = fn;
144 } else if (tag == GCOV_TAG_BLOCKS && fn) {
145 if (Version < GCOV::V800) {
146 for (uint32_t i = 0; i != length; ++i) {
147 buf.getWord(); // Ignored block flags
148 fn->blocks.push_back(std::make_unique<GCOVBlock>(i));
149 }
150 } else {
151 uint32_t num = buf.getWord();
152 for (uint32_t i = 0; i != num; ++i)
153 fn->blocks.push_back(std::make_unique<GCOVBlock>(i));
154 }
155 } else if (tag == GCOV_TAG_ARCS && fn) {
156 uint32_t srcNo = buf.getWord();
157 if (srcNo >= fn->blocks.size()) {
158 errs() << "unexpected block number: " << srcNo << " (in "
159 << fn->blocks.size() << ")\n";
160 return false;
161 }
162 GCOVBlock *src = fn->blocks[srcNo].get();
163 for (uint32_t i = 0, e = (length - 1) / 2; i != e; ++i) {
164 uint32_t dstNo = buf.getWord(), flags = buf.getWord();
165 GCOVBlock *dst = fn->blocks[dstNo].get();
166 auto arc = std::make_unique<GCOVArc>(*src, *dst, flags);
167 src->addDstEdge(arc.get());
168 dst->addSrcEdge(arc.get());
169 if (arc->onTree())
170 fn->treeArcs.push_back(std::move(arc));
171 else
172 fn->arcs.push_back(std::move(arc));
173 }
174 } else if (tag == GCOV_TAG_LINES && fn) {
175 uint32_t srcNo = buf.getWord();
176 if (srcNo >= fn->blocks.size()) {
177 errs() << "unexpected block number: " << srcNo << " (in "
178 << fn->blocks.size() << ")\n";
179 return false;
180 }
181 GCOVBlock &Block = *fn->blocks[srcNo];
182 for (;;) {
183 uint32_t line = buf.getWord();
184 if (line)
185 Block.addLine(line);
186 else {
187 StringRef filename = buf.getString();
188 if (filename.empty())
189 break;
190 // TODO Unhandled
191 }
192 }
193 }
194 }
195
196 GCNOInitialized = true;
197 return true;
198 }
199
200 /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
201 /// called after readGCNO().
readGCDA(GCOVBuffer & buf)202 bool GCOVFile::readGCDA(GCOVBuffer &buf) {
203 assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
204 if (!buf.readGCDAFormat())
205 return false;
206 GCOV::GCOVVersion GCDAVersion;
207 if (!buf.readGCOVVersion(GCDAVersion))
208 return false;
209 if (Version != GCDAVersion) {
210 errs() << "GCOV versions do not match.\n";
211 return false;
212 }
213
214 uint32_t GCDAChecksum;
215 if (!buf.readInt(GCDAChecksum))
216 return false;
217 if (Checksum != GCDAChecksum) {
218 errs() << "File checksums do not match: " << Checksum
219 << " != " << GCDAChecksum << ".\n";
220 return false;
221 }
222 uint32_t dummy, tag, length;
223 uint32_t ident;
224 GCOVFunction *fn = nullptr;
225 while ((tag = buf.getWord())) {
226 if (!buf.readInt(length))
227 return false;
228 uint32_t pos = buf.cursor.tell();
229 if (tag == GCOV_TAG_OBJECT_SUMMARY) {
230 buf.readInt(RunCount);
231 buf.readInt(dummy);
232 // clang<11 uses a fake 4.2 format which sets length to 9.
233 if (length == 9)
234 buf.readInt(RunCount);
235 } else if (tag == GCOV_TAG_PROGRAM_SUMMARY) {
236 // clang<11 uses a fake 4.2 format which sets length to 0.
237 if (length > 0) {
238 buf.readInt(dummy);
239 buf.readInt(dummy);
240 buf.readInt(RunCount);
241 }
242 ++ProgramCount;
243 } else if (tag == GCOV_TAG_FUNCTION) {
244 if (length == 0) // Placeholder
245 continue;
246 // As of GCC 10, GCOV_TAG_FUNCTION_LENGTH has never been larger than 3.
247 // However, clang<11 uses a fake 4.2 format which may set length larger
248 // than 3.
249 if (length < 2 || !buf.readInt(ident))
250 return false;
251 auto It = IdentToFunction.find(ident);
252 uint32_t linenoChecksum, cfgChecksum = 0;
253 buf.readInt(linenoChecksum);
254 if (Version >= GCOV::V407)
255 buf.readInt(cfgChecksum);
256 if (It != IdentToFunction.end()) {
257 fn = It->second;
258 if (linenoChecksum != fn->linenoChecksum ||
259 cfgChecksum != fn->cfgChecksum) {
260 errs() << fn->Name
261 << format(": checksum mismatch, (%u, %u) != (%u, %u)\n",
262 linenoChecksum, cfgChecksum, fn->linenoChecksum,
263 fn->cfgChecksum);
264 return false;
265 }
266 }
267 } else if (tag == GCOV_TAG_COUNTER_ARCS && fn) {
268 if (length != 2 * fn->arcs.size()) {
269 errs() << fn->Name
270 << format(
271 ": GCOV_TAG_COUNTER_ARCS mismatch, got %u, expected %u\n",
272 length, unsigned(2 * fn->arcs.size()));
273 return false;
274 }
275 for (std::unique_ptr<GCOVArc> &arc : fn->arcs) {
276 if (!buf.readInt64(arc->count))
277 return false;
278 arc->src.count += arc->count;
279 }
280
281 if (fn->blocks.size() >= 2) {
282 GCOVBlock &src = *fn->blocks[0];
283 GCOVBlock &sink =
284 Version < GCOV::V408 ? *fn->blocks.back() : *fn->blocks[1];
285 auto arc = std::make_unique<GCOVArc>(sink, src, GCOV_ARC_ON_TREE);
286 sink.addDstEdge(arc.get());
287 src.addSrcEdge(arc.get());
288 fn->treeArcs.push_back(std::move(arc));
289
290 for (GCOVBlock &block : fn->blocksRange())
291 fn->propagateCounts(block, nullptr);
292 for (size_t i = fn->treeArcs.size() - 1; i; --i)
293 fn->treeArcs[i - 1]->src.count += fn->treeArcs[i - 1]->count;
294 }
295 }
296 pos += 4 * length;
297 if (pos < buf.cursor.tell())
298 return false;
299 buf.de.skip(buf.cursor, pos - buf.cursor.tell());
300 }
301
302 return true;
303 }
304
print(raw_ostream & OS) const305 void GCOVFile::print(raw_ostream &OS) const {
306 for (const GCOVFunction &f : *this)
307 f.print(OS);
308 }
309
310 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
311 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
dump() const312 LLVM_DUMP_METHOD void GCOVFile::dump() const { print(dbgs()); }
313 #endif
314
onTree() const315 bool GCOVArc::onTree() const { return flags & GCOV_ARC_ON_TREE; }
316
317 //===----------------------------------------------------------------------===//
318 // GCOVFunction implementation.
319
getName(bool demangle) const320 StringRef GCOVFunction::getName(bool demangle) const {
321 if (!demangle)
322 return Name;
323 if (demangled.empty()) {
324 do {
325 if (Name.startswith("_Z")) {
326 int status = 0;
327 // Name is guaranteed to be NUL-terminated.
328 char *res = itaniumDemangle(Name.data(), nullptr, nullptr, &status);
329 if (status == 0) {
330 demangled = res;
331 free(res);
332 break;
333 }
334 }
335 demangled = Name;
336 } while (0);
337 }
338 return demangled;
339 }
getFilename() const340 StringRef GCOVFunction::getFilename() const { return file.filenames[srcIdx]; }
341
342 /// getEntryCount - Get the number of times the function was called by
343 /// retrieving the entry block's count.
getEntryCount() const344 uint64_t GCOVFunction::getEntryCount() const {
345 return blocks.front()->getCount();
346 }
347
getExitBlock() const348 GCOVBlock &GCOVFunction::getExitBlock() const {
349 return file.getVersion() < GCOV::V408 ? *blocks.back() : *blocks[1];
350 }
351
352 // For each basic block, the sum of incoming edge counts equals the sum of
353 // outgoing edge counts by Kirchoff's circuit law. If the unmeasured arcs form a
354 // spanning tree, the count for each unmeasured arc (GCOV_ARC_ON_TREE) can be
355 // uniquely identified.
propagateCounts(const GCOVBlock & v,GCOVArc * pred)356 uint64_t GCOVFunction::propagateCounts(const GCOVBlock &v, GCOVArc *pred) {
357 // If GCOV_ARC_ON_TREE edges do form a tree, visited is not needed; otherwise
358 // this prevents infinite recursion.
359 if (!visited.insert(&v).second)
360 return 0;
361
362 uint64_t excess = 0;
363 for (GCOVArc *e : v.srcs())
364 if (e != pred)
365 excess += e->onTree() ? propagateCounts(e->src, e) : e->count;
366 for (GCOVArc *e : v.dsts())
367 if (e != pred)
368 excess -= e->onTree() ? propagateCounts(e->dst, e) : e->count;
369 if (int64_t(excess) < 0)
370 excess = -excess;
371 if (pred)
372 pred->count = excess;
373 return excess;
374 }
375
print(raw_ostream & OS) const376 void GCOVFunction::print(raw_ostream &OS) const {
377 OS << "===== " << Name << " (" << ident << ") @ " << getFilename() << ":"
378 << startLine << "\n";
379 for (const auto &Block : blocks)
380 Block->print(OS);
381 }
382
383 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
384 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
dump() const385 LLVM_DUMP_METHOD void GCOVFunction::dump() const { print(dbgs()); }
386 #endif
387
388 /// collectLineCounts - Collect line counts. This must be used after
389 /// reading .gcno and .gcda files.
390
391 //===----------------------------------------------------------------------===//
392 // GCOVBlock implementation.
393
print(raw_ostream & OS) const394 void GCOVBlock::print(raw_ostream &OS) const {
395 OS << "Block : " << number << " Counter : " << count << "\n";
396 if (!pred.empty()) {
397 OS << "\tSource Edges : ";
398 for (const GCOVArc *Edge : pred)
399 OS << Edge->src.number << " (" << Edge->count << "), ";
400 OS << "\n";
401 }
402 if (!succ.empty()) {
403 OS << "\tDestination Edges : ";
404 for (const GCOVArc *Edge : succ) {
405 if (Edge->flags & GCOV_ARC_ON_TREE)
406 OS << '*';
407 OS << Edge->dst.number << " (" << Edge->count << "), ";
408 }
409 OS << "\n";
410 }
411 if (!lines.empty()) {
412 OS << "\tLines : ";
413 for (uint32_t N : lines)
414 OS << (N) << ",";
415 OS << "\n";
416 }
417 }
418
419 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
420 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
dump() const421 LLVM_DUMP_METHOD void GCOVBlock::dump() const { print(dbgs()); }
422 #endif
423
424 //===----------------------------------------------------------------------===//
425 // Cycles detection
426 //
427 // The algorithm in GCC is based on the algorithm by Hawick & James:
428 // "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
429 // http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf.
430
431 /// Get the count for the detected cycle.
getCycleCount(const Edges & Path)432 uint64_t GCOVBlock::getCycleCount(const Edges &Path) {
433 uint64_t CycleCount = std::numeric_limits<uint64_t>::max();
434 for (auto E : Path) {
435 CycleCount = std::min(E->cycleCount, CycleCount);
436 }
437 for (auto E : Path) {
438 E->cycleCount -= CycleCount;
439 }
440 return CycleCount;
441 }
442
443 /// Unblock a vertex previously marked as blocked.
unblock(const GCOVBlock * U,BlockVector & Blocked,BlockVectorLists & BlockLists)444 void GCOVBlock::unblock(const GCOVBlock *U, BlockVector &Blocked,
445 BlockVectorLists &BlockLists) {
446 auto it = find(Blocked, U);
447 if (it == Blocked.end()) {
448 return;
449 }
450
451 const size_t index = it - Blocked.begin();
452 Blocked.erase(it);
453
454 const BlockVector ToUnblock(BlockLists[index]);
455 BlockLists.erase(BlockLists.begin() + index);
456 for (auto GB : ToUnblock) {
457 GCOVBlock::unblock(GB, Blocked, BlockLists);
458 }
459 }
460
lookForCircuit(const GCOVBlock * V,const GCOVBlock * Start,Edges & Path,BlockVector & Blocked,BlockVectorLists & BlockLists,const BlockVector & Blocks,uint64_t & Count)461 bool GCOVBlock::lookForCircuit(const GCOVBlock *V, const GCOVBlock *Start,
462 Edges &Path, BlockVector &Blocked,
463 BlockVectorLists &BlockLists,
464 const BlockVector &Blocks, uint64_t &Count) {
465 Blocked.push_back(V);
466 BlockLists.emplace_back(BlockVector());
467 bool FoundCircuit = false;
468
469 for (auto E : V->dsts()) {
470 const GCOVBlock *W = &E->dst;
471 if (W < Start || find(Blocks, W) == Blocks.end()) {
472 continue;
473 }
474
475 Path.push_back(E);
476
477 if (W == Start) {
478 // We've a cycle.
479 Count += GCOVBlock::getCycleCount(Path);
480 FoundCircuit = true;
481 } else if (find(Blocked, W) == Blocked.end() && // W is not blocked.
482 GCOVBlock::lookForCircuit(W, Start, Path, Blocked, BlockLists,
483 Blocks, Count)) {
484 FoundCircuit = true;
485 }
486
487 Path.pop_back();
488 }
489
490 if (FoundCircuit) {
491 GCOVBlock::unblock(V, Blocked, BlockLists);
492 } else {
493 for (auto E : V->dsts()) {
494 const GCOVBlock *W = &E->dst;
495 if (W < Start || find(Blocks, W) == Blocks.end()) {
496 continue;
497 }
498 const size_t index = find(Blocked, W) - Blocked.begin();
499 BlockVector &List = BlockLists[index];
500 if (find(List, V) == List.end()) {
501 List.push_back(V);
502 }
503 }
504 }
505
506 return FoundCircuit;
507 }
508
509 /// Get the count for the list of blocks which lie on the same line.
getCyclesCount(const BlockVector & Blocks,uint64_t & Count)510 void GCOVBlock::getCyclesCount(const BlockVector &Blocks, uint64_t &Count) {
511 for (auto Block : Blocks) {
512 Edges Path;
513 BlockVector Blocked;
514 BlockVectorLists BlockLists;
515
516 GCOVBlock::lookForCircuit(Block, Block, Path, Blocked, BlockLists, Blocks,
517 Count);
518 }
519 }
520
521 //===----------------------------------------------------------------------===//
522 // FileInfo implementation.
523
524 // Format dividend/divisor as a percentage. Return 1 if the result is greater
525 // than 0% and less than 1%.
formatPercentage(uint64_t dividend,uint64_t divisor)526 static uint32_t formatPercentage(uint64_t dividend, uint64_t divisor) {
527 if (!dividend || !divisor)
528 return 0;
529 dividend *= 100;
530 return dividend < divisor ? 1 : dividend / divisor;
531 }
532
533 // This custom division function mimics gcov's branch ouputs:
534 // - Round to closest whole number
535 // - Only output 0% or 100% if it's exactly that value
branchDiv(uint64_t Numerator,uint64_t Divisor)536 static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
537 if (!Numerator)
538 return 0;
539 if (Numerator == Divisor)
540 return 100;
541
542 uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
543 if (Res == 0)
544 return 1;
545 if (Res == 100)
546 return 99;
547 return Res;
548 }
549
550 namespace {
551 struct formatBranchInfo {
formatBranchInfo__anonf833bdb00311::formatBranchInfo552 formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
553 : Options(Options), Count(Count), Total(Total) {}
554
print__anonf833bdb00311::formatBranchInfo555 void print(raw_ostream &OS) const {
556 if (!Total)
557 OS << "never executed";
558 else if (Options.BranchCount)
559 OS << "taken " << Count;
560 else
561 OS << "taken " << branchDiv(Count, Total) << "%";
562 }
563
564 const GCOV::Options &Options;
565 uint64_t Count;
566 uint64_t Total;
567 };
568
operator <<(raw_ostream & OS,const formatBranchInfo & FBI)569 static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
570 FBI.print(OS);
571 return OS;
572 }
573
574 class LineConsumer {
575 std::unique_ptr<MemoryBuffer> Buffer;
576 StringRef Remaining;
577
578 public:
579 LineConsumer() = default;
LineConsumer(StringRef Filename)580 LineConsumer(StringRef Filename) {
581 // Open source files without requiring a NUL terminator. The concurrent
582 // modification may nullify the NUL terminator condition.
583 ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
584 MemoryBuffer::getFileOrSTDIN(Filename, -1,
585 /*RequiresNullTerminator=*/false);
586 if (std::error_code EC = BufferOrErr.getError()) {
587 errs() << Filename << ": " << EC.message() << "\n";
588 Remaining = "";
589 } else {
590 Buffer = std::move(BufferOrErr.get());
591 Remaining = Buffer->getBuffer();
592 }
593 }
empty()594 bool empty() { return Remaining.empty(); }
printNext(raw_ostream & OS,uint32_t LineNum)595 void printNext(raw_ostream &OS, uint32_t LineNum) {
596 StringRef Line;
597 if (empty())
598 Line = "/*EOF*/";
599 else
600 std::tie(Line, Remaining) = Remaining.split("\n");
601 OS << format("%5u:", LineNum) << Line << "\n";
602 }
603 };
604 } // end anonymous namespace
605
606 /// Convert a path to a gcov filename. If PreservePaths is true, this
607 /// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
mangleCoveragePath(StringRef Filename,bool PreservePaths)608 static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
609 if (!PreservePaths)
610 return sys::path::filename(Filename).str();
611
612 // This behaviour is defined by gcov in terms of text replacements, so it's
613 // not likely to do anything useful on filesystems with different textual
614 // conventions.
615 llvm::SmallString<256> Result("");
616 StringRef::iterator I, S, E;
617 for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
618 if (*I != '/')
619 continue;
620
621 if (I - S == 1 && *S == '.') {
622 // ".", the current directory, is skipped.
623 } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
624 // "..", the parent directory, is replaced with "^".
625 Result.append("^#");
626 } else {
627 if (S < I)
628 // Leave other components intact,
629 Result.append(S, I);
630 // And separate with "#".
631 Result.push_back('#');
632 }
633 S = I + 1;
634 }
635
636 if (S < I)
637 Result.append(S, I);
638 return std::string(Result.str());
639 }
640
getCoveragePath(StringRef filename,StringRef mainFilename) const641 std::string Context::getCoveragePath(StringRef filename,
642 StringRef mainFilename) const {
643 if (options.NoOutput)
644 // This is probably a bug in gcov, but when -n is specified, paths aren't
645 // mangled at all, and the -l and -p options are ignored. Here, we do the
646 // same.
647 return std::string(filename);
648
649 std::string CoveragePath;
650 if (options.LongFileNames && !filename.equals(mainFilename))
651 CoveragePath =
652 mangleCoveragePath(mainFilename, options.PreservePaths) + "##";
653 CoveragePath += mangleCoveragePath(filename, options.PreservePaths);
654 if (options.HashFilenames) {
655 MD5 Hasher;
656 MD5::MD5Result Result;
657 Hasher.update(filename.str());
658 Hasher.final(Result);
659 CoveragePath += "##" + std::string(Result.digest());
660 }
661 CoveragePath += ".gcov";
662 return CoveragePath;
663 }
664
collectFunction(GCOVFunction & f,Summary & summary)665 void Context::collectFunction(GCOVFunction &f, Summary &summary) {
666 SourceInfo &si = sources[f.srcIdx];
667 if (f.startLine >= si.startLineToFunctions.size())
668 si.startLineToFunctions.resize(f.startLine + 1);
669 si.startLineToFunctions[f.startLine].push_back(&f);
670 for (const GCOVBlock &b : f.blocksRange()) {
671 if (b.lines.empty())
672 continue;
673 uint32_t maxLineNum = *std::max_element(b.lines.begin(), b.lines.end());
674 if (maxLineNum >= si.lines.size())
675 si.lines.resize(maxLineNum + 1);
676 for (uint32_t lineNum : b.lines) {
677 LineInfo &line = si.lines[lineNum];
678 if (!line.exists)
679 ++summary.lines;
680 if (line.count == 0 && b.count)
681 ++summary.linesExec;
682 line.exists = true;
683 line.count += b.count;
684 line.blocks.push_back(&b);
685 }
686 }
687 }
688
collectSourceLine(SourceInfo & si,Summary * summary,LineInfo & line,size_t lineNum) const689 void Context::collectSourceLine(SourceInfo &si, Summary *summary,
690 LineInfo &line, size_t lineNum) const {
691 uint64_t count = 0;
692 for (const GCOVBlock *b : line.blocks) {
693 if (b->number == 0) {
694 // For nonstandard control flows, arcs into the exit block may be
695 // duplicately counted (fork) or not be counted (abnormal exit), and thus
696 // the (exit,entry) counter may be inaccurate. Count the entry block with
697 // the outgoing arcs.
698 for (const GCOVArc *arc : b->succ)
699 count += arc->count;
700 } else {
701 // Add counts from predecessors that are not on the same line.
702 for (const GCOVArc *arc : b->pred)
703 if (!llvm::is_contained(line.blocks, &arc->src))
704 count += arc->count;
705 }
706 for (GCOVArc *arc : b->succ)
707 arc->cycleCount = arc->count;
708 }
709
710 GCOVBlock::getCyclesCount(line.blocks, count);
711 line.count = count;
712 if (line.exists) {
713 ++summary->lines;
714 if (line.count != 0)
715 ++summary->linesExec;
716 }
717
718 if (options.BranchInfo)
719 for (const GCOVBlock *b : line.blocks) {
720 if (b->getLastLine() != lineNum)
721 continue;
722 int branches = 0, execBranches = 0, takenBranches = 0;
723 for (const GCOVArc *arc : b->succ) {
724 ++branches;
725 if (count != 0)
726 ++execBranches;
727 if (arc->count != 0)
728 ++takenBranches;
729 }
730 if (branches > 1) {
731 summary->branches += branches;
732 summary->branchesExec += execBranches;
733 summary->branchesTaken += takenBranches;
734 }
735 }
736 }
737
collectSource(SourceInfo & si,Summary & summary) const738 void Context::collectSource(SourceInfo &si, Summary &summary) const {
739 size_t lineNum = 0;
740 for (LineInfo &line : si.lines) {
741 collectSourceLine(si, &summary, line, lineNum);
742 ++lineNum;
743 }
744 }
745
annotateSource(SourceInfo & si,const GCOVFile & file,StringRef gcno,StringRef gcda,raw_ostream & os) const746 void Context::annotateSource(SourceInfo &si, const GCOVFile &file,
747 StringRef gcno, StringRef gcda,
748 raw_ostream &os) const {
749 auto source =
750 options.Intermediate ? LineConsumer() : LineConsumer(si.filename);
751
752 os << " -: 0:Source:" << si.displayName << '\n';
753 os << " -: 0:Graph:" << gcno << '\n';
754 os << " -: 0:Data:" << gcda << '\n';
755 os << " -: 0:Runs:" << file.RunCount << '\n';
756 if (file.Version < GCOV::V900)
757 os << " -: 0:Programs:" << file.ProgramCount << '\n';
758
759 for (size_t lineNum = 1; !source.empty(); ++lineNum) {
760 if (lineNum >= si.lines.size()) {
761 os << " -:";
762 source.printNext(os, lineNum);
763 continue;
764 }
765
766 const LineInfo &line = si.lines[lineNum];
767 if (options.BranchInfo && lineNum < si.startLineToFunctions.size())
768 for (const auto *f : si.startLineToFunctions[lineNum])
769 printFunctionDetails(*f, os);
770 if (!line.exists)
771 os << " -:";
772 else if (line.count == 0)
773 os << " #####:";
774 else
775 os << format("%9" PRIu64 ":", line.count);
776 source.printNext(os, lineNum);
777
778 uint32_t blockIdx = 0, edgeIdx = 0;
779 for (const GCOVBlock *b : line.blocks) {
780 if (b->getLastLine() != lineNum)
781 continue;
782 if (options.AllBlocks) {
783 if (b->getCount() == 0)
784 os << " $$$$$:";
785 else
786 os << format("%9" PRIu64 ":", b->count);
787 os << format("%5u-block %2u\n", lineNum, blockIdx++);
788 }
789 if (options.BranchInfo) {
790 size_t NumEdges = b->succ.size();
791 if (NumEdges > 1)
792 printBranchInfo(*b, edgeIdx, os);
793 else if (options.UncondBranch && NumEdges == 1) {
794 uint64_t count = b->succ[0]->count;
795 os << format("unconditional %2u ", edgeIdx++)
796 << formatBranchInfo(options, count, count) << '\n';
797 }
798 }
799 }
800 }
801 }
802
printSourceToIntermediate(const SourceInfo & si,raw_ostream & os) const803 void Context::printSourceToIntermediate(const SourceInfo &si,
804 raw_ostream &os) const {
805 os << "file:" << si.filename << '\n';
806 for (const auto &fs : si.startLineToFunctions)
807 for (const GCOVFunction *f : fs)
808 os << "function:" << f->startLine << ',' << f->getEntryCount() << ','
809 << f->getName(options.Demangle) << '\n';
810 for (size_t lineNum = 1, size = si.lines.size(); lineNum < size; ++lineNum) {
811 const LineInfo &line = si.lines[lineNum];
812 if (line.blocks.empty())
813 continue;
814 // GCC 8 (r254259) added third third field for Ada:
815 // lcount:<line>,<count>,<has_unexecuted_blocks>
816 // We don't need the third field.
817 os << "lcount:" << lineNum << ',' << line.count << '\n';
818
819 if (!options.BranchInfo)
820 continue;
821 for (const GCOVBlock *b : line.blocks) {
822 if (b->succ.size() < 2 || b->getLastLine() != lineNum)
823 continue;
824 for (const GCOVArc *arc : b->succ) {
825 const char *type =
826 b->getCount() ? arc->count ? "taken" : "nottaken" : "notexec";
827 os << "branch:" << lineNum << ',' << type << '\n';
828 }
829 }
830 }
831 }
832
print(StringRef filename,StringRef gcno,StringRef gcda,GCOVFile & file)833 void Context::print(StringRef filename, StringRef gcno, StringRef gcda,
834 GCOVFile &file) {
835 for (StringRef filename : file.filenames) {
836 sources.emplace_back(filename);
837 SourceInfo &si = sources.back();
838 si.displayName = si.filename;
839 if (!options.SourcePrefix.empty() &&
840 sys::path::replace_path_prefix(si.displayName, options.SourcePrefix,
841 "") &&
842 !si.displayName.empty()) {
843 // TODO replace_path_prefix may strip the prefix even if the remaining
844 // part does not start with a separator.
845 if (sys::path::is_separator(si.displayName[0]))
846 si.displayName.erase(si.displayName.begin());
847 else
848 si.displayName = si.filename;
849 }
850 if (options.RelativeOnly && sys::path::is_absolute(si.displayName))
851 si.ignored = true;
852 }
853
854 raw_ostream &os = llvm::outs();
855 for (GCOVFunction &f : make_pointee_range(file.functions)) {
856 Summary summary(f.getName(options.Demangle));
857 collectFunction(f, summary);
858 if (options.FuncCoverage && !options.UseStdout) {
859 os << "Function '" << summary.Name << "'\n";
860 printSummary(summary, os);
861 os << '\n';
862 }
863 }
864
865 for (SourceInfo &si : sources) {
866 if (si.ignored)
867 continue;
868 Summary summary(si.displayName);
869 collectSource(si, summary);
870
871 // Print file summary unless -t is specified.
872 std::string gcovName = getCoveragePath(si.filename, filename);
873 if (!options.UseStdout) {
874 os << "File '" << summary.Name << "'\n";
875 printSummary(summary, os);
876 if (!options.NoOutput && !options.Intermediate)
877 os << "Creating '" << gcovName << "'\n";
878 os << '\n';
879 }
880
881 if (options.NoOutput || options.Intermediate)
882 continue;
883 Optional<raw_fd_ostream> os;
884 if (!options.UseStdout) {
885 std::error_code ec;
886 os.emplace(gcovName, ec, sys::fs::OF_Text);
887 if (ec) {
888 errs() << ec.message() << '\n';
889 continue;
890 }
891 }
892 annotateSource(si, file, gcno, gcda,
893 options.UseStdout ? llvm::outs() : *os);
894 }
895
896 if (options.Intermediate && !options.NoOutput) {
897 // gcov 7.* unexpectedly create multiple .gcov files, which was fixed in 8.0
898 // (PR GCC/82702). We create just one file.
899 std::string outputPath(sys::path::filename(filename));
900 std::error_code ec;
901 raw_fd_ostream os(outputPath + ".gcov", ec, sys::fs::OF_Text);
902 if (ec) {
903 errs() << ec.message() << '\n';
904 return;
905 }
906
907 for (const SourceInfo &si : sources)
908 printSourceToIntermediate(si, os);
909 }
910 }
911
printFunctionDetails(const GCOVFunction & f,raw_ostream & os) const912 void Context::printFunctionDetails(const GCOVFunction &f,
913 raw_ostream &os) const {
914 const uint64_t entryCount = f.getEntryCount();
915 uint32_t blocksExec = 0;
916 const GCOVBlock &exitBlock = f.getExitBlock();
917 uint64_t exitCount = 0;
918 for (const GCOVArc *arc : exitBlock.pred)
919 exitCount += arc->count;
920 for (const GCOVBlock &b : f.blocksRange())
921 if (b.number != 0 && &b != &exitBlock && b.getCount())
922 ++blocksExec;
923
924 os << "function " << f.getName(options.Demangle) << " called " << entryCount
925 << " returned " << formatPercentage(exitCount, entryCount)
926 << "% blocks executed "
927 << formatPercentage(blocksExec, f.blocks.size() - 2) << "%\n";
928 }
929
930 /// printBranchInfo - Print conditional branch probabilities.
printBranchInfo(const GCOVBlock & Block,uint32_t & edgeIdx,raw_ostream & os) const931 void Context::printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,
932 raw_ostream &os) const {
933 uint64_t total = 0;
934 for (const GCOVArc *arc : Block.dsts())
935 total += arc->count;
936 for (const GCOVArc *arc : Block.dsts())
937 os << format("branch %2u ", edgeIdx++)
938 << formatBranchInfo(options, arc->count, total) << '\n';
939 }
940
printSummary(const Summary & summary,raw_ostream & os) const941 void Context::printSummary(const Summary &summary, raw_ostream &os) const {
942 os << format("Lines executed:%.2f%% of %" PRIu64 "\n",
943 double(summary.linesExec) * 100 / summary.lines, summary.lines);
944 if (options.BranchInfo) {
945 if (summary.branches == 0) {
946 os << "No branches\n";
947 } else {
948 os << format("Branches executed:%.2f%% of %" PRIu64 "\n",
949 double(summary.branchesExec) * 100 / summary.branches,
950 summary.branches);
951 os << format("Taken at least once:%.2f%% of %" PRIu64 "\n",
952 double(summary.branchesTaken) * 100 / summary.branches,
953 summary.branches);
954 }
955 os << "No calls\n";
956 }
957 }
958
gcovOneInput(const GCOV::Options & options,StringRef filename,StringRef gcno,StringRef gcda,GCOVFile & file)959 void llvm::gcovOneInput(const GCOV::Options &options, StringRef filename,
960 StringRef gcno, StringRef gcda, GCOVFile &file) {
961 Context fi(options);
962 fi.print(filename, gcno, gcda, file);
963 }
964