LLVM  8.0.1
GCOV.cpp
Go to the documentation of this file.
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/ProfileData/GCOV.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/Path.h"
23 #include <algorithm>
24 #include <system_error>
25 
26 using namespace llvm;
27 
28 //===----------------------------------------------------------------------===//
29 // GCOVFile implementation.
30 
31 /// readGCNO - Read GCNO 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().
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;
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 void GCOVFile::print(raw_ostream &OS) const {
108  for (const auto &FPtr : Functions)
109  FPtr->print(OS);
110 }
111 
112 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
113 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
115 #endif
116 
117 /// collectLineCounts - Collect line counts. This must be used after
118 /// reading .gcno and .gcda files.
120  for (const auto &FPtr : Functions)
121  FPtr->collectLineCounts(FI);
122  FI.setRunCount(RunCount);
123  FI.setProgramCount(ProgramCount);
124 }
125 
126 //===----------------------------------------------------------------------===//
127 // GCOVFunction implementation.
128 
129 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
130 /// occurs.
132  uint32_t Dummy;
133  if (!Buff.readInt(Dummy))
134  return false; // Function header length
135  if (!Buff.readInt(Ident))
136  return false;
137  if (!Buff.readInt(Checksum))
138  return false;
139  if (Version != GCOV::V402) {
140  uint32_t CfgChecksum;
141  if (!Buff.readInt(CfgChecksum))
142  return false;
143  if (Parent.getChecksum() != CfgChecksum) {
144  errs() << "File checksums do not match: " << Parent.getChecksum()
145  << " != " << CfgChecksum << " in (" << Name << ").\n";
146  return false;
147  }
148  }
149  if (!Buff.readString(Name))
150  return false;
151  if (!Buff.readString(Filename))
152  return false;
153  if (!Buff.readInt(LineNumber))
154  return false;
155 
156  // read blocks.
157  if (!Buff.readBlockTag()) {
158  errs() << "Block tag not found.\n";
159  return false;
160  }
161  uint32_t BlockCount;
162  if (!Buff.readInt(BlockCount))
163  return false;
164  for (uint32_t i = 0, e = BlockCount; i != e; ++i) {
165  if (!Buff.readInt(Dummy))
166  return false; // Block flags;
167  Blocks.push_back(make_unique<GCOVBlock>(*this, i));
168  }
169 
170  // read edges.
171  while (Buff.readEdgeTag()) {
172  uint32_t EdgeCount;
173  if (!Buff.readInt(EdgeCount))
174  return false;
175  EdgeCount = (EdgeCount - 1) / 2;
176  uint32_t BlockNo;
177  if (!Buff.readInt(BlockNo))
178  return false;
179  if (BlockNo >= BlockCount) {
180  errs() << "Unexpected block number: " << BlockNo << " (in " << Name
181  << ").\n";
182  return false;
183  }
184  for (uint32_t i = 0, e = EdgeCount; i != e; ++i) {
185  uint32_t Dst;
186  if (!Buff.readInt(Dst))
187  return false;
188  Edges.push_back(make_unique<GCOVEdge>(*Blocks[BlockNo], *Blocks[Dst]));
189  GCOVEdge *Edge = Edges.back().get();
190  Blocks[BlockNo]->addDstEdge(Edge);
191  Blocks[Dst]->addSrcEdge(Edge);
192  if (!Buff.readInt(Dummy))
193  return false; // Edge flag
194  }
195  }
196 
197  // read line table.
198  while (Buff.readLineTag()) {
199  uint32_t LineTableLength;
200  // Read the length of this line table.
201  if (!Buff.readInt(LineTableLength))
202  return false;
203  uint32_t EndPos = Buff.getCursor() + LineTableLength * 4;
204  uint32_t BlockNo;
205  // Read the block number this table is associated with.
206  if (!Buff.readInt(BlockNo))
207  return false;
208  if (BlockNo >= BlockCount) {
209  errs() << "Unexpected block number: " << BlockNo << " (in " << Name
210  << ").\n";
211  return false;
212  }
213  GCOVBlock &Block = *Blocks[BlockNo];
214  // Read the word that pads the beginning of the line table. This may be a
215  // flag of some sort, but seems to always be zero.
216  if (!Buff.readInt(Dummy))
217  return false;
218 
219  // Line information starts here and continues up until the last word.
220  if (Buff.getCursor() != (EndPos - sizeof(uint32_t))) {
221  StringRef F;
222  // Read the source file name.
223  if (!Buff.readString(F))
224  return false;
225  if (Filename != F) {
226  errs() << "Multiple sources for a single basic block: " << Filename
227  << " != " << F << " (in " << Name << ").\n";
228  return false;
229  }
230  // Read lines up to, but not including, the null terminator.
231  while (Buff.getCursor() < (EndPos - 2 * sizeof(uint32_t))) {
232  uint32_t Line;
233  if (!Buff.readInt(Line))
234  return false;
235  // Line 0 means this instruction was injected by the compiler. Skip it.
236  if (!Line)
237  continue;
238  Block.addLine(Line);
239  }
240  // Read the null terminator.
241  if (!Buff.readInt(Dummy))
242  return false;
243  }
244  // The last word is either a flag or padding, it isn't clear which. Skip
245  // over it.
246  if (!Buff.readInt(Dummy))
247  return false;
248  }
249  return true;
250 }
251 
252 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
253 /// occurs.
255  uint32_t HeaderLength;
256  if (!Buff.readInt(HeaderLength))
257  return false; // Function header length
258 
259  uint64_t EndPos = Buff.getCursor() + HeaderLength * sizeof(uint32_t);
260 
261  uint32_t GCDAIdent;
262  if (!Buff.readInt(GCDAIdent))
263  return false;
264  if (Ident != GCDAIdent) {
265  errs() << "Function identifiers do not match: " << Ident
266  << " != " << GCDAIdent << " (in " << Name << ").\n";
267  return false;
268  }
269 
270  uint32_t GCDAChecksum;
271  if (!Buff.readInt(GCDAChecksum))
272  return false;
273  if (Checksum != GCDAChecksum) {
274  errs() << "Function checksums do not match: " << Checksum
275  << " != " << GCDAChecksum << " (in " << Name << ").\n";
276  return false;
277  }
278 
279  uint32_t CfgChecksum;
280  if (Version != GCOV::V402) {
281  if (!Buff.readInt(CfgChecksum))
282  return false;
283  if (Parent.getChecksum() != CfgChecksum) {
284  errs() << "File checksums do not match: " << Parent.getChecksum()
285  << " != " << CfgChecksum << " (in " << Name << ").\n";
286  return false;
287  }
288  }
289 
290  if (Buff.getCursor() < EndPos) {
291  StringRef GCDAName;
292  if (!Buff.readString(GCDAName))
293  return false;
294  if (Name != GCDAName) {
295  errs() << "Function names do not match: " << Name << " != " << GCDAName
296  << ".\n";
297  return false;
298  }
299  }
300 
301  if (!Buff.readArcTag()) {
302  errs() << "Arc tag not found (in " << Name << ").\n";
303  return false;
304  }
305 
306  uint32_t Count;
307  if (!Buff.readInt(Count))
308  return false;
309  Count /= 2;
310 
311  // This for loop adds the counts for each block. A second nested loop is
312  // required to combine the edge counts that are contained in the GCDA file.
313  for (uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
314  // The last block is always reserved for exit block
315  if (BlockNo >= Blocks.size()) {
316  errs() << "Unexpected number of edges (in " << Name << ").\n";
317  return false;
318  }
319  if (BlockNo == Blocks.size() - 1)
320  errs() << "(" << Name << ") has arcs from exit block.\n";
321  GCOVBlock &Block = *Blocks[BlockNo];
322  for (size_t EdgeNo = 0, End = Block.getNumDstEdges(); EdgeNo < End;
323  ++EdgeNo) {
324  if (Count == 0) {
325  errs() << "Unexpected number of edges (in " << Name << ").\n";
326  return false;
327  }
328  uint64_t ArcCount;
329  if (!Buff.readInt64(ArcCount))
330  return false;
331  Block.addCount(EdgeNo, ArcCount);
332  --Count;
333  }
334  Block.sortDstEdges();
335  }
336  return true;
337 }
338 
339 /// getEntryCount - Get the number of times the function was called by
340 /// retrieving the entry block's count.
341 uint64_t GCOVFunction::getEntryCount() const {
342  return Blocks.front()->getCount();
343 }
344 
345 /// getExitCount - Get the number of times the function returned by retrieving
346 /// the exit block's count.
347 uint64_t GCOVFunction::getExitCount() const {
348  return Blocks.back()->getCount();
349 }
350 
352  OS << "===== " << Name << " (" << Ident << ") @ " << Filename << ":"
353  << LineNumber << "\n";
354  for (const auto &Block : Blocks)
355  Block->print(OS);
356 }
357 
358 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
359 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
361 #endif
362 
363 /// collectLineCounts - Collect line counts. This must be used after
364 /// reading .gcno and .gcda files.
366  // If the line number is zero, this is a function that doesn't actually appear
367  // in the source file, so there isn't anything we can do with it.
368  if (LineNumber == 0)
369  return;
370 
371  for (const auto &Block : Blocks)
372  Block->collectLineCounts(FI);
373  FI.addFunctionLine(Filename, LineNumber, this);
374 }
375 
376 //===----------------------------------------------------------------------===//
377 // GCOVBlock implementation.
378 
379 /// ~GCOVBlock - Delete GCOVBlock and its content.
381  SrcEdges.clear();
382  DstEdges.clear();
383  Lines.clear();
384 }
385 
386 /// addCount - Add to block counter while storing the edge count. If the
387 /// destination has no outgoing edges, also update that block's count too.
388 void GCOVBlock::addCount(size_t DstEdgeNo, uint64_t N) {
389  assert(DstEdgeNo < DstEdges.size()); // up to caller to ensure EdgeNo is valid
390  DstEdges[DstEdgeNo]->Count = N;
391  Counter += N;
392  if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
393  DstEdges[DstEdgeNo]->Dst.Counter += N;
394 }
395 
396 /// sortDstEdges - Sort destination edges by block number, nop if already
397 /// sorted. This is required for printing branch info in the correct order.
399  if (!DstEdgesAreSorted) {
400  SortDstEdgesFunctor SortEdges;
401  std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
402  }
403 }
404 
405 /// collectLineCounts - Collect line counts. This must be used after
406 /// reading .gcno and .gcda files.
408  for (uint32_t N : Lines)
409  FI.addBlockLine(Parent.getFilename(), N, this);
410 }
411 
412 void GCOVBlock::print(raw_ostream &OS) const {
413  OS << "Block : " << Number << " Counter : " << Counter << "\n";
414  if (!SrcEdges.empty()) {
415  OS << "\tSource Edges : ";
416  for (const GCOVEdge *Edge : SrcEdges)
417  OS << Edge->Src.Number << " (" << Edge->Count << "), ";
418  OS << "\n";
419  }
420  if (!DstEdges.empty()) {
421  OS << "\tDestination Edges : ";
422  for (const GCOVEdge *Edge : DstEdges)
423  OS << Edge->Dst.Number << " (" << Edge->Count << "), ";
424  OS << "\n";
425  }
426  if (!Lines.empty()) {
427  OS << "\tLines : ";
428  for (uint32_t N : Lines)
429  OS << (N) << ",";
430  OS << "\n";
431  }
432 }
433 
434 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
435 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
437 #endif
438 
439 //===----------------------------------------------------------------------===//
440 // Cycles detection
441 //
442 // The algorithm in GCC is based on the algorihtm by Hawick & James:
443 // "Enumerating Circuits and Loops in Graphs with Self-Arcs and Multiple-Arcs"
444 // http://complexity.massey.ac.nz/cstn/013/cstn-013.pdf.
445 
446 /// Get the count for the detected cycle.
447 uint64_t GCOVBlock::getCycleCount(const Edges &Path) {
448  uint64_t CycleCount = std::numeric_limits<uint64_t>::max();
449  for (auto E : Path) {
450  CycleCount = std::min(E->CyclesCount, CycleCount);
451  }
452  for (auto E : Path) {
453  E->CyclesCount -= CycleCount;
454  }
455  return CycleCount;
456 }
457 
458 /// Unblock a vertex previously marked as blocked.
459 void GCOVBlock::unblock(const GCOVBlock *U, BlockVector &Blocked,
460  BlockVectorLists &BlockLists) {
461  auto it = find(Blocked, U);
462  if (it == Blocked.end()) {
463  return;
464  }
465 
466  const size_t index = it - Blocked.begin();
467  Blocked.erase(it);
468 
469  const BlockVector ToUnblock(BlockLists[index]);
470  BlockLists.erase(BlockLists.begin() + index);
471  for (auto GB : ToUnblock) {
472  GCOVBlock::unblock(GB, Blocked, BlockLists);
473  }
474 }
475 
476 bool GCOVBlock::lookForCircuit(const GCOVBlock *V, const GCOVBlock *Start,
477  Edges &Path, BlockVector &Blocked,
478  BlockVectorLists &BlockLists,
479  const BlockVector &Blocks, uint64_t &Count) {
480  Blocked.push_back(V);
481  BlockLists.emplace_back(BlockVector());
482  bool FoundCircuit = false;
483 
484  for (auto E : V->dsts()) {
485  const GCOVBlock *W = &E->Dst;
486  if (W < Start || find(Blocks, W) == Blocks.end()) {
487  continue;
488  }
489 
490  Path.push_back(E);
491 
492  if (W == Start) {
493  // We've a cycle.
494  Count += GCOVBlock::getCycleCount(Path);
495  FoundCircuit = true;
496  } else if (find(Blocked, W) == Blocked.end() && // W is not blocked.
497  GCOVBlock::lookForCircuit(W, Start, Path, Blocked, BlockLists,
498  Blocks, Count)) {
499  FoundCircuit = true;
500  }
501 
502  Path.pop_back();
503  }
504 
505  if (FoundCircuit) {
506  GCOVBlock::unblock(V, Blocked, BlockLists);
507  } else {
508  for (auto E : V->dsts()) {
509  const GCOVBlock *W = &E->Dst;
510  if (W < Start || find(Blocks, W) == Blocks.end()) {
511  continue;
512  }
513  const size_t index = find(Blocked, W) - Blocked.begin();
514  BlockVector &List = BlockLists[index];
515  if (find(List, V) == List.end()) {
516  List.push_back(V);
517  }
518  }
519  }
520 
521  return FoundCircuit;
522 }
523 
524 /// Get the count for the list of blocks which lie on the same line.
525 void GCOVBlock::getCyclesCount(const BlockVector &Blocks, uint64_t &Count) {
526  for (auto Block : Blocks) {
527  Edges Path;
528  BlockVector Blocked;
529  BlockVectorLists BlockLists;
530 
531  GCOVBlock::lookForCircuit(Block, Block, Path, Blocked, BlockLists, Blocks,
532  Count);
533  }
534 }
535 
536 /// Get the count for the list of blocks which lie on the same line.
537 uint64_t GCOVBlock::getLineCount(const BlockVector &Blocks) {
538  uint64_t Count = 0;
539 
540  for (auto Block : Blocks) {
541  if (Block->getNumSrcEdges() == 0) {
542  // The block has no predecessors and a non-null counter
543  // (can be the case with entry block in functions).
544  Count += Block->getCount();
545  } else {
546  // Add counts from predecessors that are not on the same line.
547  for (auto E : Block->srcs()) {
548  const GCOVBlock *W = &E->Src;
549  if (find(Blocks, W) == Blocks.end()) {
550  Count += E->Count;
551  }
552  }
553  }
554  for (auto E : Block->dsts()) {
555  E->CyclesCount = E->Count;
556  }
557  }
558 
559  GCOVBlock::getCyclesCount(Blocks, Count);
560 
561  return Count;
562 }
563 
564 //===----------------------------------------------------------------------===//
565 // FileInfo implementation.
566 
567 // Safe integer division, returns 0 if numerator is 0.
568 static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor) {
569  if (!Numerator)
570  return 0;
571  return Numerator / Divisor;
572 }
573 
574 // This custom division function mimics gcov's branch ouputs:
575 // - Round to closest whole number
576 // - Only output 0% or 100% if it's exactly that value
577 static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
578  if (!Numerator)
579  return 0;
580  if (Numerator == Divisor)
581  return 100;
582 
583  uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
584  if (Res == 0)
585  return 1;
586  if (Res == 100)
587  return 99;
588  return Res;
589 }
590 
591 namespace {
592 struct formatBranchInfo {
593  formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
594  : Options(Options), Count(Count), Total(Total) {}
595 
596  void print(raw_ostream &OS) const {
597  if (!Total)
598  OS << "never executed";
599  else if (Options.BranchCount)
600  OS << "taken " << Count;
601  else
602  OS << "taken " << branchDiv(Count, Total) << "%";
603  }
604 
605  const GCOV::Options &Options;
606  uint64_t Count;
607  uint64_t Total;
608 };
609 
610 static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
611  FBI.print(OS);
612  return OS;
613 }
614 
615 class LineConsumer {
616  std::unique_ptr<MemoryBuffer> Buffer;
617  StringRef Remaining;
618 
619 public:
620  LineConsumer(StringRef Filename) {
623  if (std::error_code EC = BufferOrErr.getError()) {
624  errs() << Filename << ": " << EC.message() << "\n";
625  Remaining = "";
626  } else {
627  Buffer = std::move(BufferOrErr.get());
628  Remaining = Buffer->getBuffer();
629  }
630  }
631  bool empty() { return Remaining.empty(); }
632  void printNext(raw_ostream &OS, uint32_t LineNum) {
633  StringRef Line;
634  if (empty())
635  Line = "/*EOF*/";
636  else
637  std::tie(Line, Remaining) = Remaining.split("\n");
638  OS << format("%5u:", LineNum) << Line << "\n";
639  }
640 };
641 } // end anonymous namespace
642 
643 /// Convert a path to a gcov filename. If PreservePaths is true, this
644 /// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
645 static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
646  if (!PreservePaths)
647  return sys::path::filename(Filename).str();
648 
649  // This behaviour is defined by gcov in terms of text replacements, so it's
650  // not likely to do anything useful on filesystems with different textual
651  // conventions.
652  llvm::SmallString<256> Result("");
653  StringRef::iterator I, S, E;
654  for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
655  if (*I != '/')
656  continue;
657 
658  if (I - S == 1 && *S == '.') {
659  // ".", the current directory, is skipped.
660  } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
661  // "..", the parent directory, is replaced with "^".
662  Result.append("^#");
663  } else {
664  if (S < I)
665  // Leave other components intact,
666  Result.append(S, I);
667  // And separate with "#".
668  Result.push_back('#');
669  }
670  S = I + 1;
671  }
672 
673  if (S < I)
674  Result.append(S, I);
675  return Result.str();
676 }
677 
679  StringRef MainFilename) {
680  if (Options.NoOutput)
681  // This is probably a bug in gcov, but when -n is specified, paths aren't
682  // mangled at all, and the -l and -p options are ignored. Here, we do the
683  // same.
684  return Filename;
685 
686  std::string CoveragePath;
687  if (Options.LongFileNames && !Filename.equals(MainFilename))
688  CoveragePath =
689  mangleCoveragePath(MainFilename, Options.PreservePaths) + "##";
690  CoveragePath += mangleCoveragePath(Filename, Options.PreservePaths) + ".gcov";
691  return CoveragePath;
692 }
693 
694 std::unique_ptr<raw_ostream>
696  if (Options.NoOutput)
697  return llvm::make_unique<raw_null_ostream>();
698 
699  std::error_code EC;
700  auto OS =
701  llvm::make_unique<raw_fd_ostream>(CoveragePath, EC, sys::fs::F_Text);
702  if (EC) {
703  errs() << EC.message() << "\n";
704  return llvm::make_unique<raw_null_ostream>();
705  }
706  return std::move(OS);
707 }
708 
709 /// print - Print source files with collected line count information.
710 void FileInfo::print(raw_ostream &InfoOS, StringRef MainFilename,
711  StringRef GCNOFile, StringRef GCDAFile) {
712  SmallVector<StringRef, 4> Filenames;
713  for (const auto &LI : LineInfo)
714  Filenames.push_back(LI.first());
715  llvm::sort(Filenames);
716 
717  for (StringRef Filename : Filenames) {
718  auto AllLines = LineConsumer(Filename);
719 
720  std::string CoveragePath = getCoveragePath(Filename, MainFilename);
721  std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
722  raw_ostream &CovOS = *CovStream;
723 
724  CovOS << " -: 0:Source:" << Filename << "\n";
725  CovOS << " -: 0:Graph:" << GCNOFile << "\n";
726  CovOS << " -: 0:Data:" << GCDAFile << "\n";
727  CovOS << " -: 0:Runs:" << RunCount << "\n";
728  CovOS << " -: 0:Programs:" << ProgramCount << "\n";
729 
730  const LineData &Line = LineInfo[Filename];
731  GCOVCoverage FileCoverage(Filename);
732  for (uint32_t LineIndex = 0; LineIndex < Line.LastLine || !AllLines.empty();
733  ++LineIndex) {
734  if (Options.BranchInfo) {
735  FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
736  if (FuncsIt != Line.Functions.end())
737  printFunctionSummary(CovOS, FuncsIt->second);
738  }
739 
740  BlockLines::const_iterator BlocksIt = Line.Blocks.find(LineIndex);
741  if (BlocksIt == Line.Blocks.end()) {
742  // No basic blocks are on this line. Not an executable line of code.
743  CovOS << " -:";
744  AllLines.printNext(CovOS, LineIndex + 1);
745  } else {
746  const BlockVector &Blocks = BlocksIt->second;
747 
748  // Add up the block counts to form line counts.
750  for (const GCOVBlock *Block : Blocks) {
751  if (Options.FuncCoverage) {
752  // This is a slightly convoluted way to most accurately gather line
753  // statistics for functions. Basically what is happening is that we
754  // don't want to count a single line with multiple blocks more than
755  // once. However, we also don't simply want to give the total line
756  // count to every function that starts on the line. Thus, what is
757  // happening here are two things:
758  // 1) Ensure that the number of logical lines is only incremented
759  // once per function.
760  // 2) If there are multiple blocks on the same line, ensure that the
761  // number of lines executed is incremented as long as at least
762  // one of the blocks are executed.
763  const GCOVFunction *Function = &Block->getParent();
764  if (FuncCoverages.find(Function) == FuncCoverages.end()) {
765  std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
766  Function, GCOVCoverage(Function->getName()));
767  FuncCoverages.insert(KeyValue);
768  }
769  GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
770 
771  if (LineExecs.find(Function) == LineExecs.end()) {
772  if (Block->getCount()) {
773  ++FuncCoverage.LinesExec;
774  LineExecs[Function] = true;
775  } else {
776  LineExecs[Function] = false;
777  }
778  ++FuncCoverage.LogicalLines;
779  } else if (!LineExecs[Function] && Block->getCount()) {
780  ++FuncCoverage.LinesExec;
781  LineExecs[Function] = true;
782  }
783  }
784  }
785 
786  const uint64_t LineCount = GCOVBlock::getLineCount(Blocks);
787  if (LineCount == 0)
788  CovOS << " #####:";
789  else {
790  CovOS << format("%9" PRIu64 ":", LineCount);
791  ++FileCoverage.LinesExec;
792  }
793  ++FileCoverage.LogicalLines;
794 
795  AllLines.printNext(CovOS, LineIndex + 1);
796 
797  uint32_t BlockNo = 0;
798  uint32_t EdgeNo = 0;
799  for (const GCOVBlock *Block : Blocks) {
800  // Only print block and branch information at the end of the block.
801  if (Block->getLastLine() != LineIndex + 1)
802  continue;
803  if (Options.AllBlocks)
804  printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
805  if (Options.BranchInfo) {
806  size_t NumEdges = Block->getNumDstEdges();
807  if (NumEdges > 1)
808  printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
809  else if (Options.UncondBranch && NumEdges == 1)
810  printUncondBranchInfo(CovOS, EdgeNo,
811  (*Block->dst_begin())->Count);
812  }
813  }
814  }
815  }
816  FileCoverages.push_back(std::make_pair(CoveragePath, FileCoverage));
817  }
818 
819  // FIXME: There is no way to detect calls given current instrumentation.
820  if (Options.FuncCoverage)
821  printFuncCoverage(InfoOS);
822  printFileCoverage(InfoOS);
823 }
824 
825 /// printFunctionSummary - Print function and block summary.
827  const FunctionVector &Funcs) const {
828  for (const GCOVFunction *Func : Funcs) {
829  uint64_t EntryCount = Func->getEntryCount();
830  uint32_t BlocksExec = 0;
831  for (const GCOVBlock &Block : Func->blocks())
832  if (Block.getNumDstEdges() && Block.getCount())
833  ++BlocksExec;
834 
835  OS << "function " << Func->getName() << " called " << EntryCount
836  << " returned " << safeDiv(Func->getExitCount() * 100, EntryCount)
837  << "% blocks executed "
838  << safeDiv(BlocksExec * 100, Func->getNumBlocks() - 1) << "%\n";
839  }
840 }
841 
842 /// printBlockInfo - Output counts for each block.
844  uint32_t LineIndex, uint32_t &BlockNo) const {
845  if (Block.getCount() == 0)
846  OS << " $$$$$:";
847  else
848  OS << format("%9" PRIu64 ":", Block.getCount());
849  OS << format("%5u-block %2u\n", LineIndex + 1, BlockNo++);
850 }
851 
852 /// printBranchInfo - Print conditional branch probabilities.
854  GCOVCoverage &Coverage, uint32_t &EdgeNo) {
855  SmallVector<uint64_t, 16> BranchCounts;
856  uint64_t TotalCounts = 0;
857  for (const GCOVEdge *Edge : Block.dsts()) {
858  BranchCounts.push_back(Edge->Count);
859  TotalCounts += Edge->Count;
860  if (Block.getCount())
861  ++Coverage.BranchesExec;
862  if (Edge->Count)
863  ++Coverage.BranchesTaken;
864  ++Coverage.Branches;
865 
866  if (Options.FuncCoverage) {
867  const GCOVFunction *Function = &Block.getParent();
868  GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
869  if (Block.getCount())
870  ++FuncCoverage.BranchesExec;
871  if (Edge->Count)
872  ++FuncCoverage.BranchesTaken;
873  ++FuncCoverage.Branches;
874  }
875  }
876 
877  for (uint64_t N : BranchCounts)
878  OS << format("branch %2u ", EdgeNo++)
879  << formatBranchInfo(Options, N, TotalCounts) << "\n";
880 }
881 
882 /// printUncondBranchInfo - Print unconditional branch probabilities.
884  uint64_t Count) const {
885  OS << format("unconditional %2u ", EdgeNo++)
886  << formatBranchInfo(Options, Count, Count) << "\n";
887 }
888 
889 // printCoverage - Print generic coverage info used by both printFuncCoverage
890 // and printFileCoverage.
892  const GCOVCoverage &Coverage) const {
893  OS << format("Lines executed:%.2f%% of %u\n",
894  double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
895  Coverage.LogicalLines);
896  if (Options.BranchInfo) {
897  if (Coverage.Branches) {
898  OS << format("Branches executed:%.2f%% of %u\n",
899  double(Coverage.BranchesExec) * 100 / Coverage.Branches,
900  Coverage.Branches);
901  OS << format("Taken at least once:%.2f%% of %u\n",
902  double(Coverage.BranchesTaken) * 100 / Coverage.Branches,
903  Coverage.Branches);
904  } else {
905  OS << "No branches\n";
906  }
907  OS << "No calls\n"; // to be consistent with gcov
908  }
909 }
910 
911 // printFuncCoverage - Print per-function coverage info.
913  for (const auto &FC : FuncCoverages) {
914  const GCOVCoverage &Coverage = FC.second;
915  OS << "Function '" << Coverage.Name << "'\n";
916  printCoverage(OS, Coverage);
917  OS << "\n";
918  }
919 }
920 
921 // printFileCoverage - Print per-file coverage info.
923  for (const auto &FC : FileCoverages) {
924  const std::string &Filename = FC.first;
925  const GCOVCoverage &Coverage = FC.second;
926  OS << "File '" << Coverage.Name << "'\n";
927  printCoverage(OS, Coverage);
928  if (!Options.NoOutput)
929  OS << Coverage.Name << ":creating '" << Filename << "'\n";
930  OS << "\n";
931  }
932 }
Represents either an error or a value T.
Definition: ErrorOr.h:57
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool readGCNO(GCOVBuffer &Buffer, GCOV::GCOVVersion Version)
readGCNO - Read a function from the GCNO buffer.
Definition: GCOV.cpp:131
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
bool readInt(uint32_t &Val)
Definition: GCOV.h:198
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
Definition: GCOV.h:82
static void getCyclesCount(const BlockVector &Blocks, uint64_t &Count)
Get the count for the list of blocks which lie on the same line.
Definition: GCOV.cpp:525
GCOVEdge - Collects edge information.
Definition: GCOV.h:265
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void addFunctionLine(StringRef Filename, uint32_t Line, const GCOVFunction *Function)
Definition: GCOV.h:436
void print(raw_ostream &OS, StringRef MainFilename, StringRef GCNOFile, StringRef GCDAFile)
print - Print source files with collected line count information.
Definition: GCOV.cpp:710
bool readLineTag()
readLineTag - If cursor points to a line tag then increment the cursor and return true otherwise retu...
Definition: GCOV.h:152
void setProgramCount(uint32_t Programs)
Definition: GCOV.h:444
void push_back(const T &Elt)
Definition: SmallVector.h:218
void printFuncCoverage(raw_ostream &OS) const
Definition: GCOV.cpp:912
bool readString(StringRef &Str)
Definition: GCOV.h:217
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
void printBlockInfo(raw_ostream &OS, const GCOVBlock &Block, uint32_t LineIndex, uint32_t &BlockNo) const
printBlockInfo - Output counts for each block.
Definition: GCOV.cpp:843
void addLine(uint32_t N)
Definition: GCOV.h:336
A struct for passing gcov options between functions.
Definition: GCOV.h:47
uint64_t getCursor() const
Definition: GCOV.h:234
F(f)
void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block)
Definition: GCOV.h:430
static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths)
Convert a path to a gcov filename.
Definition: GCOV.cpp:645
GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific read operations.
Definition: GCOV.h:66
void print(raw_ostream &OS) const
Definition: GCOV.cpp:107
amdgpu Simplify well known AMD library false Value Value const Twine & Name
uint64_t getCount() const
Definition: GCOV.h:339
static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor)
Definition: GCOV.cpp:568
void printFunctionSummary(raw_ostream &OS, const FunctionVector &Funcs) const
printFunctionSummary - Print function and block summary.
Definition: GCOV.cpp:826
void printFileCoverage(raw_ostream &OS) const
Definition: GCOV.cpp:922
std::string getCoveragePath(StringRef Filename, StringRef MainFilename)
Definition: GCOV.cpp:678
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
Definition: GCOV.cpp:365
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
bool readGCDA(GCOVBuffer &Buffer)
readGCDA - Read GCDA buffer.
Definition: GCOV.cpp:55
bool readArcTag()
readArcTag - If cursor points to an gcda arc tag then increment the cursor and return true otherwise ...
Definition: GCOV.h:164
void dump() const
dump - Dump GCOVBlock content to dbgs() for debugging purposes.
Definition: GCOV.cpp:436
StringRef getName() const
Definition: GCOV.h:284
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
GCOVBlock - Collects block information.
Definition: GCOV.h:312
bool BranchCount
Definition: GCOV.h:54
void sortDstEdges()
sortDstEdges - Sort destination edges by block number, nop if already sorted.
Definition: GCOV.cpp:398
bool readInt64(uint64_t &Val)
Definition: GCOV.h:209
void printCoverage(raw_ostream &OS, const GCOVCoverage &Coverage) const
Definition: GCOV.cpp:891
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
Definition: GCOV.cpp:119
void printBranchInfo(raw_ostream &OS, const GCOVBlock &Block, GCOVCoverage &Coverage, uint32_t &EdgeNo)
printBranchInfo - Print conditional branch probabilities.
Definition: GCOV.cpp:853
std::unique_ptr< raw_ostream > openCoveragePath(StringRef CoveragePath)
Definition: GCOV.cpp:695
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:176
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
iterator_range< EdgeIterator > dsts() const
Definition: GCOV.h:366
bool readBlockTag()
readBlockTag - If cursor points to a block tag then increment the cursor and return true otherwise re...
Definition: GCOV.h:128
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
std::error_code getError() const
Definition: ErrorOr.h:160
uint64_t getExitCount() const
getExitCount - Get the number of times the function returned by retrieving the exit block&#39;s count...
Definition: GCOV.cpp:347
bool readGCNOFormat()
readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
Definition: GCOV.h:71
void advanceCursor(uint32_t n)
Definition: GCOV.h:235
bool readGCNO(GCOVBuffer &Buffer)
readGCNO - Read GCNO buffer.
Definition: GCOV.cpp:32
const GCOVFunction & getParent() const
Definition: GCOV.h:335
iterator erase(const_iterator CI)
Definition: SmallVector.h:445
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1207
GCOVVersion
Definition: GCOV.h:44
bool readEdgeTag()
readEdgeTag - If cursor points to an edge tag then increment the cursor and return true otherwise ret...
Definition: GCOV.h:140
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1116
constexpr bool empty(const T &RangeOrContainer)
Test whether RangeOrContainer is empty. Similar to C++17 std::empty.
Definition: STLExtras.h:210
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version)
readGCDA - Read a function from the GCDA buffer.
Definition: GCOV.cpp:254
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
void printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo, uint64_t Count) const
printUncondBranchInfo - Print unconditional branch probabilities.
Definition: GCOV.cpp:883
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
iterator begin() const
Definition: StringRef.h:106
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
Definition: GCOV.cpp:407
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
void dump() const
dump - Dump GCOVFile content to dbgs() for debugging purposes.
Definition: GCOV.cpp:114
size_t getNumDstEdges() const
Definition: GCOV.h:355
static uint64_t getCycleCount(const Edges &Path)
Get the count for the detected cycle.
Definition: GCOV.cpp:447
GCOVFunction - Collects function information.
Definition: GCOV.h:275
BlockLines Blocks
Definition: GCOV.h:409
void emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:652
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
Definition: Path.cpp:590
FunctionLines Functions
Definition: GCOV.h:410
const NodeList & List
Definition: RDFGraph.cpp:210
static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor)
Definition: GCOV.cpp:577
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
static uint64_t getLineCount(const BlockVector &Blocks)
Get the count for the list of blocks which lie on the same line.
Definition: GCOV.cpp:537
iterator end()
Definition: DenseMap.h:109
static bool lookForCircuit(const GCOVBlock *V, const GCOVBlock *Start, Edges &Path, BlockVector &Blocked, BlockVectorLists &BlockLists, const BlockVector &Blocks, uint64_t &Count)
Definition: GCOV.cpp:476
bool readObjectTag()
readObjectTag - If cursor points to an object summary tag then increment the cursor and return true o...
Definition: GCOV.h:176
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
~GCOVBlock()
~GCOVBlock - Delete GCOVBlock and its content.
Definition: GCOV.cpp:380
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
static void unblock(const GCOVBlock *U, BlockVector &Blocked, BlockVectorLists &BlockLists)
Unblock a vertex previously marked as blocked.
Definition: GCOV.cpp:459
bool readProgramTag()
readProgramTag - If cursor points to a program summary tag then increment the cursor and return true ...
Definition: GCOV.h:188
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool readFunctionTag()
readFunctionTag - If cursor points to a function tag then increment the cursor and return true otherw...
Definition: GCOV.h:116
void addCount(size_t DstEdgeNo, uint64_t N)
addCount - Add to block counter while storing the edge count.
Definition: GCOV.cpp:388
void setRunCount(uint32_t Runs)
Definition: GCOV.h:443
bool readGCOVVersion(GCOV::GCOVVersion &Version)
readGCOVVersion - Read GCOV version.
Definition: GCOV.h:93
iterator end() const
Definition: StringRef.h:108
void print(raw_ostream &OS) const
Definition: GCOV.cpp:412
void print(raw_ostream &OS) const
Definition: GCOV.cpp:351
void dump() const
dump - Dump GCOVFunction content to dbgs() for debugging purposes.
Definition: GCOV.cpp:360
reference get()
Definition: ErrorOr.h:157
uint64_t getEntryCount() const
getEntryCount - Get the number of times the function was called by retrieving the entry block&#39;s count...
Definition: GCOV.cpp:341