1 //===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // GCOV implements the interface to read and write coverage files that use
13 //===----------------------------------------------------------------------===//
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"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/raw_ostream.h"
24 #include <system_error>
28 //===----------------------------------------------------------------------===//
29 // GCOVFile implementation.
31 /// readGCNO - Read GCNO buffer.
32 bool GCOVFile::readGCNO(GCOVBuffer
&Buffer
) {
33 if (!Buffer
.readGCNOFormat())
35 if (!Buffer
.readGCOVVersion(Version
))
38 if (!Buffer
.readInt(Checksum
))
41 if (!Buffer
.readFunctionTag())
43 auto GFun
= make_unique
<GCOVFunction
>(*this);
44 if (!GFun
->readGCNO(Buffer
, Version
))
46 Functions
.push_back(std::move(GFun
));
49 GCNOInitialized
= true;
53 /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
54 /// called after readGCNO().
55 bool GCOVFile::readGCDA(GCOVBuffer
&Buffer
) {
56 assert(GCNOInitialized
&& "readGCDA() can only be called after readGCNO()");
57 if (!Buffer
.readGCDAFormat())
59 GCOV::GCOVVersion GCDAVersion
;
60 if (!Buffer
.readGCOVVersion(GCDAVersion
))
62 if (Version
!= GCDAVersion
) {
63 errs() << "GCOV versions do not match.\n";
67 uint32_t GCDAChecksum
;
68 if (!Buffer
.readInt(GCDAChecksum
))
70 if (Checksum
!= GCDAChecksum
) {
71 errs() << "File checksums do not match: " << Checksum
72 << " != " << GCDAChecksum
<< ".\n";
75 for (size_t i
= 0, e
= Functions
.size(); i
< e
; ++i
) {
76 if (!Buffer
.readFunctionTag()) {
77 errs() << "Unexpected number of functions.\n";
80 if (!Functions
[i
]->readGCDA(Buffer
, Version
))
83 if (Buffer
.readObjectTag()) {
86 if (!Buffer
.readInt(Length
))
88 if (!Buffer
.readInt(Dummy
))
89 return false; // checksum
90 if (!Buffer
.readInt(Dummy
))
92 if (!Buffer
.readInt(RunCount
))
94 Buffer
.advanceCursor(Length
- 3);
96 while (Buffer
.readProgramTag()) {
98 if (!Buffer
.readInt(Length
))
100 Buffer
.advanceCursor(Length
);
107 void GCOVFile::print(raw_ostream
&OS
) const {
108 for (const auto &FPtr
: Functions
)
112 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
113 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
114 LLVM_DUMP_METHOD
void GCOVFile::dump() const {
119 /// collectLineCounts - Collect line counts. This must be used after
120 /// reading .gcno and .gcda files.
121 void GCOVFile::collectLineCounts(FileInfo
&FI
) {
122 for (const auto &FPtr
: Functions
)
123 FPtr
->collectLineCounts(FI
);
124 FI
.setRunCount(RunCount
);
125 FI
.setProgramCount(ProgramCount
);
128 //===----------------------------------------------------------------------===//
129 // GCOVFunction implementation.
131 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
133 bool GCOVFunction::readGCNO(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
135 if (!Buff
.readInt(Dummy
))
136 return false; // Function header length
137 if (!Buff
.readInt(Ident
))
139 if (!Buff
.readInt(Checksum
))
141 if (Version
!= GCOV::V402
) {
142 uint32_t CfgChecksum
;
143 if (!Buff
.readInt(CfgChecksum
))
145 if (Parent
.getChecksum() != CfgChecksum
) {
146 errs() << "File checksums do not match: " << Parent
.getChecksum()
147 << " != " << CfgChecksum
<< " in (" << Name
<< ").\n";
151 if (!Buff
.readString(Name
))
153 if (!Buff
.readString(Filename
))
155 if (!Buff
.readInt(LineNumber
))
159 if (!Buff
.readBlockTag()) {
160 errs() << "Block tag not found.\n";
164 if (!Buff
.readInt(BlockCount
))
166 for (uint32_t i
= 0, e
= BlockCount
; i
!= e
; ++i
) {
167 if (!Buff
.readInt(Dummy
))
168 return false; // Block flags;
169 Blocks
.push_back(make_unique
<GCOVBlock
>(*this, i
));
173 while (Buff
.readEdgeTag()) {
175 if (!Buff
.readInt(EdgeCount
))
177 EdgeCount
= (EdgeCount
- 1) / 2;
179 if (!Buff
.readInt(BlockNo
))
181 if (BlockNo
>= BlockCount
) {
182 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
186 for (uint32_t i
= 0, e
= EdgeCount
; i
!= e
; ++i
) {
188 if (!Buff
.readInt(Dst
))
190 Edges
.push_back(make_unique
<GCOVEdge
>(*Blocks
[BlockNo
], *Blocks
[Dst
]));
191 GCOVEdge
*Edge
= Edges
.back().get();
192 Blocks
[BlockNo
]->addDstEdge(Edge
);
193 Blocks
[Dst
]->addSrcEdge(Edge
);
194 if (!Buff
.readInt(Dummy
))
195 return false; // Edge flag
200 while (Buff
.readLineTag()) {
201 uint32_t LineTableLength
;
202 // Read the length of this line table.
203 if (!Buff
.readInt(LineTableLength
))
205 uint32_t EndPos
= Buff
.getCursor() + LineTableLength
* 4;
207 // Read the block number this table is associated with.
208 if (!Buff
.readInt(BlockNo
))
210 if (BlockNo
>= BlockCount
) {
211 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
215 GCOVBlock
&Block
= *Blocks
[BlockNo
];
216 // Read the word that pads the beginning of the line table. This may be a
217 // flag of some sort, but seems to always be zero.
218 if (!Buff
.readInt(Dummy
))
221 // Line information starts here and continues up until the last word.
222 if (Buff
.getCursor() != (EndPos
- sizeof(uint32_t))) {
224 // Read the source file name.
225 if (!Buff
.readString(F
))
228 errs() << "Multiple sources for a single basic block: " << Filename
229 << " != " << F
<< " (in " << Name
<< ").\n";
232 // Read lines up to, but not including, the null terminator.
233 while (Buff
.getCursor() < (EndPos
- 2 * sizeof(uint32_t))) {
235 if (!Buff
.readInt(Line
))
237 // Line 0 means this instruction was injected by the compiler. Skip it.
242 // Read the null terminator.
243 if (!Buff
.readInt(Dummy
))
246 // The last word is either a flag or padding, it isn't clear which. Skip
248 if (!Buff
.readInt(Dummy
))
254 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
256 bool GCOVFunction::readGCDA(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
257 uint32_t HeaderLength
;
258 if (!Buff
.readInt(HeaderLength
))
259 return false; // Function header length
261 uint64_t EndPos
= Buff
.getCursor() + HeaderLength
* sizeof(uint32_t);
264 if (!Buff
.readInt(GCDAIdent
))
266 if (Ident
!= GCDAIdent
) {
267 errs() << "Function identifiers do not match: " << Ident
268 << " != " << GCDAIdent
<< " (in " << Name
<< ").\n";
272 uint32_t GCDAChecksum
;
273 if (!Buff
.readInt(GCDAChecksum
))
275 if (Checksum
!= GCDAChecksum
) {
276 errs() << "Function checksums do not match: " << Checksum
277 << " != " << GCDAChecksum
<< " (in " << Name
<< ").\n";
281 uint32_t CfgChecksum
;
282 if (Version
!= GCOV::V402
) {
283 if (!Buff
.readInt(CfgChecksum
))
285 if (Parent
.getChecksum() != CfgChecksum
) {
286 errs() << "File checksums do not match: " << Parent
.getChecksum()
287 << " != " << CfgChecksum
<< " (in " << Name
<< ").\n";
292 if (Buff
.getCursor() < EndPos
) {
294 if (!Buff
.readString(GCDAName
))
296 if (Name
!= GCDAName
) {
297 errs() << "Function names do not match: " << Name
<< " != " << GCDAName
303 if (!Buff
.readArcTag()) {
304 errs() << "Arc tag not found (in " << Name
<< ").\n";
309 if (!Buff
.readInt(Count
))
313 // This for loop adds the counts for each block. A second nested loop is
314 // required to combine the edge counts that are contained in the GCDA file.
315 for (uint32_t BlockNo
= 0; Count
> 0; ++BlockNo
) {
316 // The last block is always reserved for exit block
317 if (BlockNo
>= Blocks
.size()) {
318 errs() << "Unexpected number of edges (in " << Name
<< ").\n";
321 if (BlockNo
== Blocks
.size() - 1)
322 errs() << "(" << Name
<< ") has arcs from exit block.\n";
323 GCOVBlock
&Block
= *Blocks
[BlockNo
];
324 for (size_t EdgeNo
= 0, End
= Block
.getNumDstEdges(); EdgeNo
< End
;
327 errs() << "Unexpected number of edges (in " << Name
<< ").\n";
331 if (!Buff
.readInt64(ArcCount
))
333 Block
.addCount(EdgeNo
, ArcCount
);
336 Block
.sortDstEdges();
341 /// getEntryCount - Get the number of times the function was called by
342 /// retrieving the entry block's count.
343 uint64_t GCOVFunction::getEntryCount() const {
344 return Blocks
.front()->getCount();
347 /// getExitCount - Get the number of times the function returned by retrieving
348 /// the exit block's count.
349 uint64_t GCOVFunction::getExitCount() const {
350 return Blocks
.back()->getCount();
353 void GCOVFunction::print(raw_ostream
&OS
) const {
354 OS
<< "===== " << Name
<< " (" << Ident
<< ") @ " << Filename
<< ":"
355 << LineNumber
<< "\n";
356 for (const auto &Block
: Blocks
)
360 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
361 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
362 LLVM_DUMP_METHOD
void GCOVFunction::dump() const {
367 /// collectLineCounts - Collect line counts. This must be used after
368 /// reading .gcno and .gcda files.
369 void GCOVFunction::collectLineCounts(FileInfo
&FI
) {
370 // If the line number is zero, this is a function that doesn't actually appear
371 // in the source file, so there isn't anything we can do with it.
375 for (const auto &Block
: Blocks
)
376 Block
->collectLineCounts(FI
);
377 FI
.addFunctionLine(Filename
, LineNumber
, this);
380 //===----------------------------------------------------------------------===//
381 // GCOVBlock implementation.
383 /// ~GCOVBlock - Delete GCOVBlock and its content.
384 GCOVBlock::~GCOVBlock() {
390 /// addCount - Add to block counter while storing the edge count. If the
391 /// destination has no outgoing edges, also update that block's count too.
392 void GCOVBlock::addCount(size_t DstEdgeNo
, uint64_t N
) {
393 assert(DstEdgeNo
< DstEdges
.size()); // up to caller to ensure EdgeNo is valid
394 DstEdges
[DstEdgeNo
]->Count
= N
;
396 if (!DstEdges
[DstEdgeNo
]->Dst
.getNumDstEdges())
397 DstEdges
[DstEdgeNo
]->Dst
.Counter
+= N
;
400 /// sortDstEdges - Sort destination edges by block number, nop if already
401 /// sorted. This is required for printing branch info in the correct order.
402 void GCOVBlock::sortDstEdges() {
403 if (!DstEdgesAreSorted
) {
404 SortDstEdgesFunctor SortEdges
;
405 std::stable_sort(DstEdges
.begin(), DstEdges
.end(), SortEdges
);
409 /// collectLineCounts - Collect line counts. This must be used after
410 /// reading .gcno and .gcda files.
411 void GCOVBlock::collectLineCounts(FileInfo
&FI
) {
412 for (uint32_t N
: Lines
)
413 FI
.addBlockLine(Parent
.getFilename(), N
, this);
416 void GCOVBlock::print(raw_ostream
&OS
) const {
417 OS
<< "Block : " << Number
<< " Counter : " << Counter
<< "\n";
418 if (!SrcEdges
.empty()) {
419 OS
<< "\tSource Edges : ";
420 for (const GCOVEdge
*Edge
: SrcEdges
)
421 OS
<< Edge
->Src
.Number
<< " (" << Edge
->Count
<< "), ";
424 if (!DstEdges
.empty()) {
425 OS
<< "\tDestination Edges : ";
426 for (const GCOVEdge
*Edge
: DstEdges
)
427 OS
<< Edge
->Dst
.Number
<< " (" << Edge
->Count
<< "), ";
430 if (!Lines
.empty()) {
432 for (uint32_t N
: Lines
)
438 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
439 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
440 LLVM_DUMP_METHOD
void GCOVBlock::dump() const {
445 //===----------------------------------------------------------------------===//
446 // FileInfo implementation.
448 // Safe integer division, returns 0 if numerator is 0.
449 static uint32_t safeDiv(uint64_t Numerator
, uint64_t Divisor
) {
452 return Numerator
/ Divisor
;
455 // This custom division function mimics gcov's branch ouputs:
456 // - Round to closest whole number
457 // - Only output 0% or 100% if it's exactly that value
458 static uint32_t branchDiv(uint64_t Numerator
, uint64_t Divisor
) {
461 if (Numerator
== Divisor
)
464 uint8_t Res
= (Numerator
* 100 + Divisor
/ 2) / Divisor
;
473 struct formatBranchInfo
{
474 formatBranchInfo(const GCOV::Options
&Options
, uint64_t Count
, uint64_t Total
)
475 : Options(Options
), Count(Count
), Total(Total
) {}
477 void print(raw_ostream
&OS
) const {
479 OS
<< "never executed";
480 else if (Options
.BranchCount
)
481 OS
<< "taken " << Count
;
483 OS
<< "taken " << branchDiv(Count
, Total
) << "%";
486 const GCOV::Options
&Options
;
491 static raw_ostream
&operator<<(raw_ostream
&OS
, const formatBranchInfo
&FBI
) {
497 std::unique_ptr
<MemoryBuffer
> Buffer
;
501 LineConsumer(StringRef Filename
) {
502 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
503 MemoryBuffer::getFileOrSTDIN(Filename
);
504 if (std::error_code EC
= BufferOrErr
.getError()) {
505 errs() << Filename
<< ": " << EC
.message() << "\n";
508 Buffer
= std::move(BufferOrErr
.get());
509 Remaining
= Buffer
->getBuffer();
512 bool empty() { return Remaining
.empty(); }
513 void printNext(raw_ostream
&OS
, uint32_t LineNum
) {
518 std::tie(Line
, Remaining
) = Remaining
.split("\n");
519 OS
<< format("%5u:", LineNum
) << Line
<< "\n";
522 } // end anonymous namespace
524 /// Convert a path to a gcov filename. If PreservePaths is true, this
525 /// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
526 static std::string
mangleCoveragePath(StringRef Filename
, bool PreservePaths
) {
528 return sys::path::filename(Filename
).str();
530 // This behaviour is defined by gcov in terms of text replacements, so it's
531 // not likely to do anything useful on filesystems with different textual
533 llvm::SmallString
<256> Result("");
534 StringRef::iterator I
, S
, E
;
535 for (I
= S
= Filename
.begin(), E
= Filename
.end(); I
!= E
; ++I
) {
539 if (I
- S
== 1 && *S
== '.') {
540 // ".", the current directory, is skipped.
541 } else if (I
- S
== 2 && *S
== '.' && *(S
+ 1) == '.') {
542 // "..", the parent directory, is replaced with "^".
546 // Leave other components intact,
548 // And separate with "#".
549 Result
.push_back('#');
559 std::string
FileInfo::getCoveragePath(StringRef Filename
,
560 StringRef MainFilename
) {
561 if (Options
.NoOutput
)
562 // This is probably a bug in gcov, but when -n is specified, paths aren't
563 // mangled at all, and the -l and -p options are ignored. Here, we do the
567 std::string CoveragePath
;
568 if (Options
.LongFileNames
&& !Filename
.equals(MainFilename
))
570 mangleCoveragePath(MainFilename
, Options
.PreservePaths
) + "##";
571 CoveragePath
+= mangleCoveragePath(Filename
, Options
.PreservePaths
) + ".gcov";
575 std::unique_ptr
<raw_ostream
>
576 FileInfo::openCoveragePath(StringRef CoveragePath
) {
577 if (Options
.NoOutput
)
578 return llvm::make_unique
<raw_null_ostream
>();
581 auto OS
= llvm::make_unique
<raw_fd_ostream
>(CoveragePath
, EC
,
584 errs() << EC
.message() << "\n";
585 return llvm::make_unique
<raw_null_ostream
>();
587 return std::move(OS
);
590 /// print - Print source files with collected line count information.
591 void FileInfo::print(raw_ostream
&InfoOS
, StringRef MainFilename
,
592 StringRef GCNOFile
, StringRef GCDAFile
) {
593 SmallVector
<StringRef
, 4> Filenames
;
594 for (const auto &LI
: LineInfo
)
595 Filenames
.push_back(LI
.first());
596 llvm::sort(Filenames
.begin(), Filenames
.end());
598 for (StringRef Filename
: Filenames
) {
599 auto AllLines
= LineConsumer(Filename
);
601 std::string CoveragePath
= getCoveragePath(Filename
, MainFilename
);
602 std::unique_ptr
<raw_ostream
> CovStream
= openCoveragePath(CoveragePath
);
603 raw_ostream
&CovOS
= *CovStream
;
605 CovOS
<< " -: 0:Source:" << Filename
<< "\n";
606 CovOS
<< " -: 0:Graph:" << GCNOFile
<< "\n";
607 CovOS
<< " -: 0:Data:" << GCDAFile
<< "\n";
608 CovOS
<< " -: 0:Runs:" << RunCount
<< "\n";
609 CovOS
<< " -: 0:Programs:" << ProgramCount
<< "\n";
611 const LineData
&Line
= LineInfo
[Filename
];
612 GCOVCoverage
FileCoverage(Filename
);
613 for (uint32_t LineIndex
= 0; LineIndex
< Line
.LastLine
|| !AllLines
.empty();
615 if (Options
.BranchInfo
) {
616 FunctionLines::const_iterator FuncsIt
= Line
.Functions
.find(LineIndex
);
617 if (FuncsIt
!= Line
.Functions
.end())
618 printFunctionSummary(CovOS
, FuncsIt
->second
);
621 BlockLines::const_iterator BlocksIt
= Line
.Blocks
.find(LineIndex
);
622 if (BlocksIt
== Line
.Blocks
.end()) {
623 // No basic blocks are on this line. Not an executable line of code.
625 AllLines
.printNext(CovOS
, LineIndex
+ 1);
627 const BlockVector
&Blocks
= BlocksIt
->second
;
629 // Add up the block counts to form line counts.
630 DenseMap
<const GCOVFunction
*, bool> LineExecs
;
631 uint64_t LineCount
= 0;
632 for (const GCOVBlock
*Block
: Blocks
) {
633 if (Options
.AllBlocks
) {
634 // Only take the highest block count for that line.
635 uint64_t BlockCount
= Block
->getCount();
636 LineCount
= LineCount
> BlockCount
? LineCount
: BlockCount
;
638 // Sum up all of the block counts.
639 LineCount
+= Block
->getCount();
642 if (Options
.FuncCoverage
) {
643 // This is a slightly convoluted way to most accurately gather line
644 // statistics for functions. Basically what is happening is that we
645 // don't want to count a single line with multiple blocks more than
646 // once. However, we also don't simply want to give the total line
647 // count to every function that starts on the line. Thus, what is
648 // happening here are two things:
649 // 1) Ensure that the number of logical lines is only incremented
650 // once per function.
651 // 2) If there are multiple blocks on the same line, ensure that the
652 // number of lines executed is incremented as long as at least
653 // one of the blocks are executed.
654 const GCOVFunction
*Function
= &Block
->getParent();
655 if (FuncCoverages
.find(Function
) == FuncCoverages
.end()) {
656 std::pair
<const GCOVFunction
*, GCOVCoverage
> KeyValue(
657 Function
, GCOVCoverage(Function
->getName()));
658 FuncCoverages
.insert(KeyValue
);
660 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
662 if (LineExecs
.find(Function
) == LineExecs
.end()) {
663 if (Block
->getCount()) {
664 ++FuncCoverage
.LinesExec
;
665 LineExecs
[Function
] = true;
667 LineExecs
[Function
] = false;
669 ++FuncCoverage
.LogicalLines
;
670 } else if (!LineExecs
[Function
] && Block
->getCount()) {
671 ++FuncCoverage
.LinesExec
;
672 LineExecs
[Function
] = true;
680 CovOS
<< format("%9" PRIu64
":", LineCount
);
681 ++FileCoverage
.LinesExec
;
683 ++FileCoverage
.LogicalLines
;
685 AllLines
.printNext(CovOS
, LineIndex
+ 1);
687 uint32_t BlockNo
= 0;
689 for (const GCOVBlock
*Block
: Blocks
) {
690 // Only print block and branch information at the end of the block.
691 if (Block
->getLastLine() != LineIndex
+ 1)
693 if (Options
.AllBlocks
)
694 printBlockInfo(CovOS
, *Block
, LineIndex
, BlockNo
);
695 if (Options
.BranchInfo
) {
696 size_t NumEdges
= Block
->getNumDstEdges();
698 printBranchInfo(CovOS
, *Block
, FileCoverage
, EdgeNo
);
699 else if (Options
.UncondBranch
&& NumEdges
== 1)
700 printUncondBranchInfo(CovOS
, EdgeNo
,
701 (*Block
->dst_begin())->Count
);
706 FileCoverages
.push_back(std::make_pair(CoveragePath
, FileCoverage
));
709 // FIXME: There is no way to detect calls given current instrumentation.
710 if (Options
.FuncCoverage
)
711 printFuncCoverage(InfoOS
);
712 printFileCoverage(InfoOS
);
715 /// printFunctionSummary - Print function and block summary.
716 void FileInfo::printFunctionSummary(raw_ostream
&OS
,
717 const FunctionVector
&Funcs
) const {
718 for (const GCOVFunction
*Func
: Funcs
) {
719 uint64_t EntryCount
= Func
->getEntryCount();
720 uint32_t BlocksExec
= 0;
721 for (const GCOVBlock
&Block
: Func
->blocks())
722 if (Block
.getNumDstEdges() && Block
.getCount())
725 OS
<< "function " << Func
->getName() << " called " << EntryCount
726 << " returned " << safeDiv(Func
->getExitCount() * 100, EntryCount
)
727 << "% blocks executed "
728 << safeDiv(BlocksExec
* 100, Func
->getNumBlocks() - 1) << "%\n";
732 /// printBlockInfo - Output counts for each block.
733 void FileInfo::printBlockInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
734 uint32_t LineIndex
, uint32_t &BlockNo
) const {
735 if (Block
.getCount() == 0)
738 OS
<< format("%9" PRIu64
":", Block
.getCount());
739 OS
<< format("%5u-block %2u\n", LineIndex
+ 1, BlockNo
++);
742 /// printBranchInfo - Print conditional branch probabilities.
743 void FileInfo::printBranchInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
744 GCOVCoverage
&Coverage
, uint32_t &EdgeNo
) {
745 SmallVector
<uint64_t, 16> BranchCounts
;
746 uint64_t TotalCounts
= 0;
747 for (const GCOVEdge
*Edge
: Block
.dsts()) {
748 BranchCounts
.push_back(Edge
->Count
);
749 TotalCounts
+= Edge
->Count
;
750 if (Block
.getCount())
751 ++Coverage
.BranchesExec
;
753 ++Coverage
.BranchesTaken
;
756 if (Options
.FuncCoverage
) {
757 const GCOVFunction
*Function
= &Block
.getParent();
758 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
759 if (Block
.getCount())
760 ++FuncCoverage
.BranchesExec
;
762 ++FuncCoverage
.BranchesTaken
;
763 ++FuncCoverage
.Branches
;
767 for (uint64_t N
: BranchCounts
)
768 OS
<< format("branch %2u ", EdgeNo
++)
769 << formatBranchInfo(Options
, N
, TotalCounts
) << "\n";
772 /// printUncondBranchInfo - Print unconditional branch probabilities.
773 void FileInfo::printUncondBranchInfo(raw_ostream
&OS
, uint32_t &EdgeNo
,
774 uint64_t Count
) const {
775 OS
<< format("unconditional %2u ", EdgeNo
++)
776 << formatBranchInfo(Options
, Count
, Count
) << "\n";
779 // printCoverage - Print generic coverage info used by both printFuncCoverage
780 // and printFileCoverage.
781 void FileInfo::printCoverage(raw_ostream
&OS
,
782 const GCOVCoverage
&Coverage
) const {
783 OS
<< format("Lines executed:%.2f%% of %u\n",
784 double(Coverage
.LinesExec
) * 100 / Coverage
.LogicalLines
,
785 Coverage
.LogicalLines
);
786 if (Options
.BranchInfo
) {
787 if (Coverage
.Branches
) {
788 OS
<< format("Branches executed:%.2f%% of %u\n",
789 double(Coverage
.BranchesExec
) * 100 / Coverage
.Branches
,
791 OS
<< format("Taken at least once:%.2f%% of %u\n",
792 double(Coverage
.BranchesTaken
) * 100 / Coverage
.Branches
,
795 OS
<< "No branches\n";
797 OS
<< "No calls\n"; // to be consistent with gcov
801 // printFuncCoverage - Print per-function coverage info.
802 void FileInfo::printFuncCoverage(raw_ostream
&OS
) const {
803 for (const auto &FC
: FuncCoverages
) {
804 const GCOVCoverage
&Coverage
= FC
.second
;
805 OS
<< "Function '" << Coverage
.Name
<< "'\n";
806 printCoverage(OS
, Coverage
);
811 // printFileCoverage - Print per-file coverage info.
812 void FileInfo::printFileCoverage(raw_ostream
&OS
) const {
813 for (const auto &FC
: FileCoverages
) {
814 const std::string
&Filename
= FC
.first
;
815 const GCOVCoverage
&Coverage
= FC
.second
;
816 OS
<< "File '" << Coverage
.Name
<< "'\n";
817 printCoverage(OS
, Coverage
);
818 if (!Options
.NoOutput
)
819 OS
<< Coverage
.Name
<< ":creating '" << Filename
<< "'\n";