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