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/Support/Debug.h"
18 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/Format.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
23 #include <system_error>
27 //===----------------------------------------------------------------------===//
28 // GCOVFile implementation.
30 /// readGCNO - Read GCNO buffer.
31 bool GCOVFile::readGCNO(GCOVBuffer
&Buffer
) {
32 if (!Buffer
.readGCNOFormat())
34 if (!Buffer
.readGCOVVersion(Version
))
37 if (!Buffer
.readInt(Checksum
))
40 if (!Buffer
.readFunctionTag())
42 auto GFun
= make_unique
<GCOVFunction
>(*this);
43 if (!GFun
->readGCNO(Buffer
, Version
))
45 Functions
.push_back(std::move(GFun
));
48 GCNOInitialized
= true;
52 /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
53 /// called after readGCNO().
54 bool GCOVFile::readGCDA(GCOVBuffer
&Buffer
) {
55 assert(GCNOInitialized
&& "readGCDA() can only be called after readGCNO()");
56 if (!Buffer
.readGCDAFormat())
58 GCOV::GCOVVersion GCDAVersion
;
59 if (!Buffer
.readGCOVVersion(GCDAVersion
))
61 if (Version
!= GCDAVersion
) {
62 errs() << "GCOV versions do not match.\n";
66 uint32_t GCDAChecksum
;
67 if (!Buffer
.readInt(GCDAChecksum
))
69 if (Checksum
!= GCDAChecksum
) {
70 errs() << "File checksums do not match: " << Checksum
71 << " != " << GCDAChecksum
<< ".\n";
74 for (size_t i
= 0, e
= Functions
.size(); i
< e
; ++i
) {
75 if (!Buffer
.readFunctionTag()) {
76 errs() << "Unexpected number of functions.\n";
79 if (!Functions
[i
]->readGCDA(Buffer
, Version
))
82 if (Buffer
.readObjectTag()) {
85 if (!Buffer
.readInt(Length
))
87 if (!Buffer
.readInt(Dummy
))
88 return false; // checksum
89 if (!Buffer
.readInt(Dummy
))
91 if (!Buffer
.readInt(RunCount
))
93 Buffer
.advanceCursor(Length
- 3);
95 while (Buffer
.readProgramTag()) {
97 if (!Buffer
.readInt(Length
))
99 Buffer
.advanceCursor(Length
);
106 void GCOVFile::print(raw_ostream
&OS
) const {
107 for (const auto &FPtr
: Functions
)
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
112 /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
113 LLVM_DUMP_METHOD
void GCOVFile::dump() const {
118 /// collectLineCounts - Collect line counts. This must be used after
119 /// reading .gcno and .gcda files.
120 void GCOVFile::collectLineCounts(FileInfo
&FI
) {
121 for (const auto &FPtr
: Functions
)
122 FPtr
->collectLineCounts(FI
);
123 FI
.setRunCount(RunCount
);
124 FI
.setProgramCount(ProgramCount
);
127 //===----------------------------------------------------------------------===//
128 // GCOVFunction implementation.
130 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
132 bool GCOVFunction::readGCNO(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
134 if (!Buff
.readInt(Dummy
))
135 return false; // Function header length
136 if (!Buff
.readInt(Ident
))
138 if (!Buff
.readInt(Checksum
))
140 if (Version
!= GCOV::V402
) {
141 uint32_t CfgChecksum
;
142 if (!Buff
.readInt(CfgChecksum
))
144 if (Parent
.getChecksum() != CfgChecksum
) {
145 errs() << "File checksums do not match: " << Parent
.getChecksum()
146 << " != " << CfgChecksum
<< " in (" << Name
<< ").\n";
150 if (!Buff
.readString(Name
))
152 if (!Buff
.readString(Filename
))
154 if (!Buff
.readInt(LineNumber
))
158 if (!Buff
.readBlockTag()) {
159 errs() << "Block tag not found.\n";
163 if (!Buff
.readInt(BlockCount
))
165 for (uint32_t i
= 0, e
= BlockCount
; i
!= e
; ++i
) {
166 if (!Buff
.readInt(Dummy
))
167 return false; // Block flags;
168 Blocks
.push_back(make_unique
<GCOVBlock
>(*this, i
));
172 while (Buff
.readEdgeTag()) {
174 if (!Buff
.readInt(EdgeCount
))
176 EdgeCount
= (EdgeCount
- 1) / 2;
178 if (!Buff
.readInt(BlockNo
))
180 if (BlockNo
>= BlockCount
) {
181 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
185 for (uint32_t i
= 0, e
= EdgeCount
; i
!= e
; ++i
) {
187 if (!Buff
.readInt(Dst
))
189 Edges
.push_back(make_unique
<GCOVEdge
>(*Blocks
[BlockNo
], *Blocks
[Dst
]));
190 GCOVEdge
*Edge
= Edges
.back().get();
191 Blocks
[BlockNo
]->addDstEdge(Edge
);
192 Blocks
[Dst
]->addSrcEdge(Edge
);
193 if (!Buff
.readInt(Dummy
))
194 return false; // Edge flag
199 while (Buff
.readLineTag()) {
200 uint32_t LineTableLength
;
201 // Read the length of this line table.
202 if (!Buff
.readInt(LineTableLength
))
204 uint32_t EndPos
= Buff
.getCursor() + LineTableLength
* 4;
206 // Read the block number this table is associated with.
207 if (!Buff
.readInt(BlockNo
))
209 if (BlockNo
>= BlockCount
) {
210 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
214 GCOVBlock
&Block
= *Blocks
[BlockNo
];
215 // Read the word that pads the beginning of the line table. This may be a
216 // flag of some sort, but seems to always be zero.
217 if (!Buff
.readInt(Dummy
))
220 // Line information starts here and continues up until the last word.
221 if (Buff
.getCursor() != (EndPos
- sizeof(uint32_t))) {
223 // Read the source file name.
224 if (!Buff
.readString(F
))
227 errs() << "Multiple sources for a single basic block: " << Filename
228 << " != " << F
<< " (in " << Name
<< ").\n";
231 // Read lines up to, but not including, the null terminator.
232 while (Buff
.getCursor() < (EndPos
- 2 * sizeof(uint32_t))) {
234 if (!Buff
.readInt(Line
))
236 // Line 0 means this instruction was injected by the compiler. Skip it.
241 // Read the null terminator.
242 if (!Buff
.readInt(Dummy
))
245 // The last word is either a flag or padding, it isn't clear which. Skip
247 if (!Buff
.readInt(Dummy
))
253 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
255 bool GCOVFunction::readGCDA(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
256 uint32_t HeaderLength
;
257 if (!Buff
.readInt(HeaderLength
))
258 return false; // Function header length
260 uint64_t EndPos
= Buff
.getCursor() + HeaderLength
* sizeof(uint32_t);
263 if (!Buff
.readInt(GCDAIdent
))
265 if (Ident
!= GCDAIdent
) {
266 errs() << "Function identifiers do not match: " << Ident
267 << " != " << GCDAIdent
<< " (in " << Name
<< ").\n";
271 uint32_t GCDAChecksum
;
272 if (!Buff
.readInt(GCDAChecksum
))
274 if (Checksum
!= GCDAChecksum
) {
275 errs() << "Function checksums do not match: " << Checksum
276 << " != " << GCDAChecksum
<< " (in " << Name
<< ").\n";
280 uint32_t CfgChecksum
;
281 if (Version
!= GCOV::V402
) {
282 if (!Buff
.readInt(CfgChecksum
))
284 if (Parent
.getChecksum() != CfgChecksum
) {
285 errs() << "File checksums do not match: " << Parent
.getChecksum()
286 << " != " << CfgChecksum
<< " (in " << Name
<< ").\n";
291 if (Buff
.getCursor() < EndPos
) {
293 if (!Buff
.readString(GCDAName
))
295 if (Name
!= GCDAName
) {
296 errs() << "Function names do not match: " << Name
<< " != " << GCDAName
302 if (!Buff
.readArcTag()) {
303 errs() << "Arc tag not found (in " << Name
<< ").\n";
308 if (!Buff
.readInt(Count
))
312 // This for loop adds the counts for each block. A second nested loop is
313 // required to combine the edge counts that are contained in the GCDA file.
314 for (uint32_t BlockNo
= 0; Count
> 0; ++BlockNo
) {
315 // The last block is always reserved for exit block
316 if (BlockNo
>= Blocks
.size()) {
317 errs() << "Unexpected number of edges (in " << Name
<< ").\n";
320 if (BlockNo
== Blocks
.size() - 1)
321 errs() << "(" << Name
<< ") has arcs from exit block.\n";
322 GCOVBlock
&Block
= *Blocks
[BlockNo
];
323 for (size_t EdgeNo
= 0, End
= Block
.getNumDstEdges(); EdgeNo
< End
;
326 errs() << "Unexpected number of edges (in " << Name
<< ").\n";
330 if (!Buff
.readInt64(ArcCount
))
332 Block
.addCount(EdgeNo
, ArcCount
);
335 Block
.sortDstEdges();
340 /// getEntryCount - Get the number of times the function was called by
341 /// retrieving the entry block's count.
342 uint64_t GCOVFunction::getEntryCount() const {
343 return Blocks
.front()->getCount();
346 /// getExitCount - Get the number of times the function returned by retrieving
347 /// the exit block's count.
348 uint64_t GCOVFunction::getExitCount() const {
349 return Blocks
.back()->getCount();
352 void GCOVFunction::print(raw_ostream
&OS
) const {
353 OS
<< "===== " << Name
<< " (" << Ident
<< ") @ " << Filename
<< ":"
354 << LineNumber
<< "\n";
355 for (const auto &Block
: Blocks
)
359 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
360 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
361 LLVM_DUMP_METHOD
void GCOVFunction::dump() const {
366 /// collectLineCounts - Collect line counts. This must be used after
367 /// reading .gcno and .gcda files.
368 void GCOVFunction::collectLineCounts(FileInfo
&FI
) {
369 // If the line number is zero, this is a function that doesn't actually appear
370 // in the source file, so there isn't anything we can do with it.
374 for (const auto &Block
: Blocks
)
375 Block
->collectLineCounts(FI
);
376 FI
.addFunctionLine(Filename
, LineNumber
, this);
379 //===----------------------------------------------------------------------===//
380 // GCOVBlock implementation.
382 /// ~GCOVBlock - Delete GCOVBlock and its content.
383 GCOVBlock::~GCOVBlock() {
389 /// addCount - Add to block counter while storing the edge count. If the
390 /// destination has no outgoing edges, also update that block's count too.
391 void GCOVBlock::addCount(size_t DstEdgeNo
, uint64_t N
) {
392 assert(DstEdgeNo
< DstEdges
.size()); // up to caller to ensure EdgeNo is valid
393 DstEdges
[DstEdgeNo
]->Count
= N
;
395 if (!DstEdges
[DstEdgeNo
]->Dst
.getNumDstEdges())
396 DstEdges
[DstEdgeNo
]->Dst
.Counter
+= N
;
399 /// sortDstEdges - Sort destination edges by block number, nop if already
400 /// sorted. This is required for printing branch info in the correct order.
401 void GCOVBlock::sortDstEdges() {
402 if (!DstEdgesAreSorted
) {
403 SortDstEdgesFunctor SortEdges
;
404 std::stable_sort(DstEdges
.begin(), DstEdges
.end(), SortEdges
);
408 /// collectLineCounts - Collect line counts. This must be used after
409 /// reading .gcno and .gcda files.
410 void GCOVBlock::collectLineCounts(FileInfo
&FI
) {
411 for (uint32_t N
: Lines
)
412 FI
.addBlockLine(Parent
.getFilename(), N
, this);
415 void GCOVBlock::print(raw_ostream
&OS
) const {
416 OS
<< "Block : " << Number
<< " Counter : " << Counter
<< "\n";
417 if (!SrcEdges
.empty()) {
418 OS
<< "\tSource Edges : ";
419 for (const GCOVEdge
*Edge
: SrcEdges
)
420 OS
<< Edge
->Src
.Number
<< " (" << Edge
->Count
<< "), ";
423 if (!DstEdges
.empty()) {
424 OS
<< "\tDestination Edges : ";
425 for (const GCOVEdge
*Edge
: DstEdges
)
426 OS
<< Edge
->Dst
.Number
<< " (" << Edge
->Count
<< "), ";
429 if (!Lines
.empty()) {
431 for (uint32_t N
: Lines
)
437 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
438 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
439 LLVM_DUMP_METHOD
void GCOVBlock::dump() const {
444 //===----------------------------------------------------------------------===//
445 // FileInfo implementation.
447 // Safe integer division, returns 0 if numerator is 0.
448 static uint32_t safeDiv(uint64_t Numerator
, uint64_t Divisor
) {
451 return Numerator
/ Divisor
;
454 // This custom division function mimics gcov's branch ouputs:
455 // - Round to closest whole number
456 // - Only output 0% or 100% if it's exactly that value
457 static uint32_t branchDiv(uint64_t Numerator
, uint64_t Divisor
) {
460 if (Numerator
== Divisor
)
463 uint8_t Res
= (Numerator
* 100 + Divisor
/ 2) / Divisor
;
472 struct formatBranchInfo
{
473 formatBranchInfo(const GCOV::Options
&Options
, uint64_t Count
, uint64_t Total
)
474 : Options(Options
), Count(Count
), Total(Total
) {}
476 void print(raw_ostream
&OS
) const {
478 OS
<< "never executed";
479 else if (Options
.BranchCount
)
480 OS
<< "taken " << Count
;
482 OS
<< "taken " << branchDiv(Count
, Total
) << "%";
485 const GCOV::Options
&Options
;
490 static raw_ostream
&operator<<(raw_ostream
&OS
, const formatBranchInfo
&FBI
) {
496 std::unique_ptr
<MemoryBuffer
> Buffer
;
500 LineConsumer(StringRef Filename
) {
501 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
502 MemoryBuffer::getFileOrSTDIN(Filename
);
503 if (std::error_code EC
= BufferOrErr
.getError()) {
504 errs() << Filename
<< ": " << EC
.message() << "\n";
507 Buffer
= std::move(BufferOrErr
.get());
508 Remaining
= Buffer
->getBuffer();
511 bool empty() { return Remaining
.empty(); }
512 void printNext(raw_ostream
&OS
, uint32_t LineNum
) {
517 std::tie(Line
, Remaining
) = Remaining
.split("\n");
518 OS
<< format("%5u:", LineNum
) << Line
<< "\n";
521 } // end anonymous namespace
523 /// Convert a path to a gcov filename. If PreservePaths is true, this
524 /// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
525 static std::string
mangleCoveragePath(StringRef Filename
, bool PreservePaths
) {
527 return sys::path::filename(Filename
).str();
529 // This behaviour is defined by gcov in terms of text replacements, so it's
530 // not likely to do anything useful on filesystems with different textual
532 llvm::SmallString
<256> Result("");
533 StringRef::iterator I
, S
, E
;
534 for (I
= S
= Filename
.begin(), E
= Filename
.end(); I
!= E
; ++I
) {
538 if (I
- S
== 1 && *S
== '.') {
539 // ".", the current directory, is skipped.
540 } else if (I
- S
== 2 && *S
== '.' && *(S
+ 1) == '.') {
541 // "..", the parent directory, is replaced with "^".
545 // Leave other components intact,
547 // And separate with "#".
548 Result
.push_back('#');
558 std::string
FileInfo::getCoveragePath(StringRef Filename
,
559 StringRef MainFilename
) {
560 if (Options
.NoOutput
)
561 // This is probably a bug in gcov, but when -n is specified, paths aren't
562 // mangled at all, and the -l and -p options are ignored. Here, we do the
566 std::string CoveragePath
;
567 if (Options
.LongFileNames
&& !Filename
.equals(MainFilename
))
569 mangleCoveragePath(MainFilename
, Options
.PreservePaths
) + "##";
570 CoveragePath
+= mangleCoveragePath(Filename
, Options
.PreservePaths
) + ".gcov";
574 std::unique_ptr
<raw_ostream
>
575 FileInfo::openCoveragePath(StringRef CoveragePath
) {
576 if (Options
.NoOutput
)
577 return llvm::make_unique
<raw_null_ostream
>();
580 auto OS
= llvm::make_unique
<raw_fd_ostream
>(CoveragePath
, EC
,
583 errs() << EC
.message() << "\n";
584 return llvm::make_unique
<raw_null_ostream
>();
586 return std::move(OS
);
589 /// print - Print source files with collected line count information.
590 void FileInfo::print(raw_ostream
&InfoOS
, StringRef MainFilename
,
591 StringRef GCNOFile
, StringRef GCDAFile
) {
592 SmallVector
<StringRef
, 4> Filenames
;
593 for (const auto &LI
: LineInfo
)
594 Filenames
.push_back(LI
.first());
595 std::sort(Filenames
.begin(), Filenames
.end());
597 for (StringRef Filename
: Filenames
) {
598 auto AllLines
= LineConsumer(Filename
);
600 std::string CoveragePath
= getCoveragePath(Filename
, MainFilename
);
601 std::unique_ptr
<raw_ostream
> CovStream
= openCoveragePath(CoveragePath
);
602 raw_ostream
&CovOS
= *CovStream
;
604 CovOS
<< " -: 0:Source:" << Filename
<< "\n";
605 CovOS
<< " -: 0:Graph:" << GCNOFile
<< "\n";
606 CovOS
<< " -: 0:Data:" << GCDAFile
<< "\n";
607 CovOS
<< " -: 0:Runs:" << RunCount
<< "\n";
608 CovOS
<< " -: 0:Programs:" << ProgramCount
<< "\n";
610 const LineData
&Line
= LineInfo
[Filename
];
611 GCOVCoverage
FileCoverage(Filename
);
612 for (uint32_t LineIndex
= 0; LineIndex
< Line
.LastLine
|| !AllLines
.empty();
614 if (Options
.BranchInfo
) {
615 FunctionLines::const_iterator FuncsIt
= Line
.Functions
.find(LineIndex
);
616 if (FuncsIt
!= Line
.Functions
.end())
617 printFunctionSummary(CovOS
, FuncsIt
->second
);
620 BlockLines::const_iterator BlocksIt
= Line
.Blocks
.find(LineIndex
);
621 if (BlocksIt
== Line
.Blocks
.end()) {
622 // No basic blocks are on this line. Not an executable line of code.
624 AllLines
.printNext(CovOS
, LineIndex
+ 1);
626 const BlockVector
&Blocks
= BlocksIt
->second
;
628 // Add up the block counts to form line counts.
629 DenseMap
<const GCOVFunction
*, bool> LineExecs
;
630 uint64_t LineCount
= 0;
631 for (const GCOVBlock
*Block
: Blocks
) {
632 if (Options
.AllBlocks
) {
633 // Only take the highest block count for that line.
634 uint64_t BlockCount
= Block
->getCount();
635 LineCount
= LineCount
> BlockCount
? LineCount
: BlockCount
;
637 // Sum up all of the block counts.
638 LineCount
+= Block
->getCount();
641 if (Options
.FuncCoverage
) {
642 // This is a slightly convoluted way to most accurately gather line
643 // statistics for functions. Basically what is happening is that we
644 // don't want to count a single line with multiple blocks more than
645 // once. However, we also don't simply want to give the total line
646 // count to every function that starts on the line. Thus, what is
647 // happening here are two things:
648 // 1) Ensure that the number of logical lines is only incremented
649 // once per function.
650 // 2) If there are multiple blocks on the same line, ensure that the
651 // number of lines executed is incremented as long as at least
652 // one of the blocks are executed.
653 const GCOVFunction
*Function
= &Block
->getParent();
654 if (FuncCoverages
.find(Function
) == FuncCoverages
.end()) {
655 std::pair
<const GCOVFunction
*, GCOVCoverage
> KeyValue(
656 Function
, GCOVCoverage(Function
->getName()));
657 FuncCoverages
.insert(KeyValue
);
659 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
661 if (LineExecs
.find(Function
) == LineExecs
.end()) {
662 if (Block
->getCount()) {
663 ++FuncCoverage
.LinesExec
;
664 LineExecs
[Function
] = true;
666 LineExecs
[Function
] = false;
668 ++FuncCoverage
.LogicalLines
;
669 } else if (!LineExecs
[Function
] && Block
->getCount()) {
670 ++FuncCoverage
.LinesExec
;
671 LineExecs
[Function
] = true;
679 CovOS
<< format("%9" PRIu64
":", LineCount
);
680 ++FileCoverage
.LinesExec
;
682 ++FileCoverage
.LogicalLines
;
684 AllLines
.printNext(CovOS
, LineIndex
+ 1);
686 uint32_t BlockNo
= 0;
688 for (const GCOVBlock
*Block
: Blocks
) {
689 // Only print block and branch information at the end of the block.
690 if (Block
->getLastLine() != LineIndex
+ 1)
692 if (Options
.AllBlocks
)
693 printBlockInfo(CovOS
, *Block
, LineIndex
, BlockNo
);
694 if (Options
.BranchInfo
) {
695 size_t NumEdges
= Block
->getNumDstEdges();
697 printBranchInfo(CovOS
, *Block
, FileCoverage
, EdgeNo
);
698 else if (Options
.UncondBranch
&& NumEdges
== 1)
699 printUncondBranchInfo(CovOS
, EdgeNo
,
700 (*Block
->dst_begin())->Count
);
705 FileCoverages
.push_back(std::make_pair(CoveragePath
, FileCoverage
));
708 // FIXME: There is no way to detect calls given current instrumentation.
709 if (Options
.FuncCoverage
)
710 printFuncCoverage(InfoOS
);
711 printFileCoverage(InfoOS
);
714 /// printFunctionSummary - Print function and block summary.
715 void FileInfo::printFunctionSummary(raw_ostream
&OS
,
716 const FunctionVector
&Funcs
) const {
717 for (const GCOVFunction
*Func
: Funcs
) {
718 uint64_t EntryCount
= Func
->getEntryCount();
719 uint32_t BlocksExec
= 0;
720 for (const GCOVBlock
&Block
: Func
->blocks())
721 if (Block
.getNumDstEdges() && Block
.getCount())
724 OS
<< "function " << Func
->getName() << " called " << EntryCount
725 << " returned " << safeDiv(Func
->getExitCount() * 100, EntryCount
)
726 << "% blocks executed "
727 << safeDiv(BlocksExec
* 100, Func
->getNumBlocks() - 1) << "%\n";
731 /// printBlockInfo - Output counts for each block.
732 void FileInfo::printBlockInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
733 uint32_t LineIndex
, uint32_t &BlockNo
) const {
734 if (Block
.getCount() == 0)
737 OS
<< format("%9" PRIu64
":", Block
.getCount());
738 OS
<< format("%5u-block %2u\n", LineIndex
+ 1, BlockNo
++);
741 /// printBranchInfo - Print conditional branch probabilities.
742 void FileInfo::printBranchInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
743 GCOVCoverage
&Coverage
, uint32_t &EdgeNo
) {
744 SmallVector
<uint64_t, 16> BranchCounts
;
745 uint64_t TotalCounts
= 0;
746 for (const GCOVEdge
*Edge
: Block
.dsts()) {
747 BranchCounts
.push_back(Edge
->Count
);
748 TotalCounts
+= Edge
->Count
;
749 if (Block
.getCount())
750 ++Coverage
.BranchesExec
;
752 ++Coverage
.BranchesTaken
;
755 if (Options
.FuncCoverage
) {
756 const GCOVFunction
*Function
= &Block
.getParent();
757 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
758 if (Block
.getCount())
759 ++FuncCoverage
.BranchesExec
;
761 ++FuncCoverage
.BranchesTaken
;
762 ++FuncCoverage
.Branches
;
766 for (uint64_t N
: BranchCounts
)
767 OS
<< format("branch %2u ", EdgeNo
++)
768 << formatBranchInfo(Options
, N
, TotalCounts
) << "\n";
771 /// printUncondBranchInfo - Print unconditional branch probabilities.
772 void FileInfo::printUncondBranchInfo(raw_ostream
&OS
, uint32_t &EdgeNo
,
773 uint64_t Count
) const {
774 OS
<< format("unconditional %2u ", EdgeNo
++)
775 << formatBranchInfo(Options
, Count
, Count
) << "\n";
778 // printCoverage - Print generic coverage info used by both printFuncCoverage
779 // and printFileCoverage.
780 void FileInfo::printCoverage(raw_ostream
&OS
,
781 const GCOVCoverage
&Coverage
) const {
782 OS
<< format("Lines executed:%.2f%% of %u\n",
783 double(Coverage
.LinesExec
) * 100 / Coverage
.LogicalLines
,
784 Coverage
.LogicalLines
);
785 if (Options
.BranchInfo
) {
786 if (Coverage
.Branches
) {
787 OS
<< format("Branches executed:%.2f%% of %u\n",
788 double(Coverage
.BranchesExec
) * 100 / Coverage
.Branches
,
790 OS
<< format("Taken at least once:%.2f%% of %u\n",
791 double(Coverage
.BranchesTaken
) * 100 / Coverage
.Branches
,
794 OS
<< "No branches\n";
796 OS
<< "No calls\n"; // to be consistent with gcov
800 // printFuncCoverage - Print per-function coverage info.
801 void FileInfo::printFuncCoverage(raw_ostream
&OS
) const {
802 for (const auto &FC
: FuncCoverages
) {
803 const GCOVCoverage
&Coverage
= FC
.second
;
804 OS
<< "Function '" << Coverage
.Name
<< "'\n";
805 printCoverage(OS
, Coverage
);
810 // printFileCoverage - Print per-file coverage info.
811 void FileInfo::printFileCoverage(raw_ostream
&OS
) const {
812 for (const auto &FC
: FileCoverages
) {
813 const std::string
&Filename
= FC
.first
;
814 const GCOVCoverage
&Coverage
= FC
.second
;
815 OS
<< "File '" << Coverage
.Name
<< "'\n";
816 printCoverage(OS
, Coverage
);
817 if (!Options
.NoOutput
)
818 OS
<< Coverage
.Name
<< ":creating '" << Filename
<< "'\n";