1 //===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // GCOV implements the interface to read and write coverage files that use
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ProfileData/GCOV.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/Config/llvm-config.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/MD5.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
= std::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 { print(dbgs()); }
117 /// collectLineCounts - Collect line counts. This must be used after
118 /// reading .gcno and .gcda files.
119 void GCOVFile::collectLineCounts(FileInfo
&FI
) {
120 for (const auto &FPtr
: Functions
)
121 FPtr
->collectLineCounts(FI
);
122 FI
.setRunCount(RunCount
);
123 FI
.setProgramCount(ProgramCount
);
126 //===----------------------------------------------------------------------===//
127 // GCOVFunction implementation.
129 /// readGCNO - Read a function from the GCNO buffer. Return false if an error
131 bool GCOVFunction::readGCNO(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
133 if (!Buff
.readInt(Dummy
))
134 return false; // Function header length
135 if (!Buff
.readInt(Ident
))
137 if (!Buff
.readInt(Checksum
))
139 if (Version
!= GCOV::V402
) {
140 uint32_t CfgChecksum
;
141 if (!Buff
.readInt(CfgChecksum
))
143 if (Parent
.getChecksum() != CfgChecksum
) {
144 errs() << "File checksums do not match: " << Parent
.getChecksum()
145 << " != " << CfgChecksum
<< " in (" << Name
<< ").\n";
149 if (!Buff
.readString(Name
))
151 if (!Buff
.readString(Filename
))
153 if (!Buff
.readInt(LineNumber
))
157 if (!Buff
.readBlockTag()) {
158 errs() << "Block tag not found.\n";
162 if (!Buff
.readInt(BlockCount
))
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(std::make_unique
<GCOVBlock
>(*this, i
));
171 while (Buff
.readEdgeTag()) {
173 if (!Buff
.readInt(EdgeCount
))
175 EdgeCount
= (EdgeCount
- 1) / 2;
177 if (!Buff
.readInt(BlockNo
))
179 if (BlockNo
>= BlockCount
) {
180 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
184 for (uint32_t i
= 0, e
= EdgeCount
; i
!= e
; ++i
) {
186 if (!Buff
.readInt(Dst
))
188 Edges
.push_back(std::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
198 while (Buff
.readLineTag()) {
199 uint32_t LineTableLength
;
200 // Read the length of this line table.
201 if (!Buff
.readInt(LineTableLength
))
203 uint32_t EndPos
= Buff
.getCursor() + LineTableLength
* 4;
205 // Read the block number this table is associated with.
206 if (!Buff
.readInt(BlockNo
))
208 if (BlockNo
>= BlockCount
) {
209 errs() << "Unexpected block number: " << BlockNo
<< " (in " << Name
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
))
219 // Line information starts here and continues up until the last word.
220 if (Buff
.getCursor() != (EndPos
- sizeof(uint32_t))) {
222 // Read the source file name.
223 if (!Buff
.readString(F
))
226 errs() << "Multiple sources for a single basic block: " << Filename
227 << " != " << F
<< " (in " << Name
<< ").\n";
230 // Read lines up to, but not including, the null terminator.
231 while (Buff
.getCursor() < (EndPos
- 2 * sizeof(uint32_t))) {
233 if (!Buff
.readInt(Line
))
235 // Line 0 means this instruction was injected by the compiler. Skip it.
240 // Read the null terminator.
241 if (!Buff
.readInt(Dummy
))
244 // The last word is either a flag or padding, it isn't clear which. Skip
246 if (!Buff
.readInt(Dummy
))
252 /// readGCDA - Read a function from the GCDA buffer. Return false if an error
254 bool GCOVFunction::readGCDA(GCOVBuffer
&Buff
, GCOV::GCOVVersion Version
) {
255 uint32_t HeaderLength
;
256 if (!Buff
.readInt(HeaderLength
))
257 return false; // Function header length
259 uint64_t EndPos
= Buff
.getCursor() + HeaderLength
* sizeof(uint32_t);
262 if (!Buff
.readInt(GCDAIdent
))
264 if (Ident
!= GCDAIdent
) {
265 errs() << "Function identifiers do not match: " << Ident
266 << " != " << GCDAIdent
<< " (in " << Name
<< ").\n";
270 uint32_t GCDAChecksum
;
271 if (!Buff
.readInt(GCDAChecksum
))
273 if (Checksum
!= GCDAChecksum
) {
274 errs() << "Function checksums do not match: " << Checksum
275 << " != " << GCDAChecksum
<< " (in " << Name
<< ").\n";
279 uint32_t CfgChecksum
;
280 if (Version
!= GCOV::V402
) {
281 if (!Buff
.readInt(CfgChecksum
))
283 if (Parent
.getChecksum() != CfgChecksum
) {
284 errs() << "File checksums do not match: " << Parent
.getChecksum()
285 << " != " << CfgChecksum
<< " (in " << Name
<< ").\n";
290 if (Buff
.getCursor() < EndPos
) {
292 if (!Buff
.readString(GCDAName
))
294 if (Name
!= GCDAName
) {
295 errs() << "Function names do not match: " << Name
<< " != " << GCDAName
301 if (!Buff
.readArcTag()) {
302 errs() << "Arc tag not found (in " << Name
<< ").\n";
307 if (!Buff
.readInt(Count
))
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";
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
;
325 errs() << "Unexpected number of edges (in " << Name
<< ").\n";
329 if (!Buff
.readInt64(ArcCount
))
331 Block
.addCount(EdgeNo
, ArcCount
);
334 Block
.sortDstEdges();
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();
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();
351 void GCOVFunction::print(raw_ostream
&OS
) const {
352 OS
<< "===== " << Name
<< " (" << Ident
<< ") @ " << Filename
<< ":"
353 << LineNumber
<< "\n";
354 for (const auto &Block
: Blocks
)
358 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
359 /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
360 LLVM_DUMP_METHOD
void GCOVFunction::dump() const { print(dbgs()); }
363 /// collectLineCounts - Collect line counts. This must be used after
364 /// reading .gcno and .gcda files.
365 void GCOVFunction::collectLineCounts(FileInfo
&FI
) {
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.
371 for (const auto &Block
: Blocks
)
372 Block
->collectLineCounts(FI
);
373 FI
.addFunctionLine(Filename
, LineNumber
, this);
376 //===----------------------------------------------------------------------===//
377 // GCOVBlock implementation.
379 /// ~GCOVBlock - Delete GCOVBlock and its content.
380 GCOVBlock::~GCOVBlock() {
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
;
392 if (!DstEdges
[DstEdgeNo
]->Dst
.getNumDstEdges())
393 DstEdges
[DstEdgeNo
]->Dst
.Counter
+= N
;
396 /// sortDstEdges - Sort destination edges by block number, nop if already
397 /// sorted. This is required for printing branch info in the correct order.
398 void GCOVBlock::sortDstEdges() {
399 if (!DstEdgesAreSorted
)
400 llvm::stable_sort(DstEdges
, [](const GCOVEdge
*E1
, const GCOVEdge
*E2
) {
401 return E1
->Dst
.Number
< E2
->Dst
.Number
;
405 /// collectLineCounts - Collect line counts. This must be used after
406 /// reading .gcno and .gcda files.
407 void GCOVBlock::collectLineCounts(FileInfo
&FI
) {
408 for (uint32_t N
: Lines
)
409 FI
.addBlockLine(Parent
.getFilename(), N
, this);
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
<< "), ";
420 if (!DstEdges
.empty()) {
421 OS
<< "\tDestination Edges : ";
422 for (const GCOVEdge
*Edge
: DstEdges
)
423 OS
<< Edge
->Dst
.Number
<< " (" << Edge
->Count
<< "), ";
426 if (!Lines
.empty()) {
428 for (uint32_t N
: Lines
)
434 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
435 /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
436 LLVM_DUMP_METHOD
void GCOVBlock::dump() const { print(dbgs()); }
439 //===----------------------------------------------------------------------===//
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.
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
);
452 for (auto E
: Path
) {
453 E
->CyclesCount
-= CycleCount
;
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()) {
466 const size_t index
= it
- Blocked
.begin();
469 const BlockVector
ToUnblock(BlockLists
[index
]);
470 BlockLists
.erase(BlockLists
.begin() + index
);
471 for (auto GB
: ToUnblock
) {
472 GCOVBlock::unblock(GB
, Blocked
, BlockLists
);
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;
484 for (auto E
: V
->dsts()) {
485 const GCOVBlock
*W
= &E
->Dst
;
486 if (W
< Start
|| find(Blocks
, W
) == Blocks
.end()) {
494 Count
+= GCOVBlock::getCycleCount(Path
);
496 } else if (find(Blocked
, W
) == Blocked
.end() && // W is not blocked.
497 GCOVBlock::lookForCircuit(W
, Start
, Path
, Blocked
, BlockLists
,
506 GCOVBlock::unblock(V
, Blocked
, BlockLists
);
508 for (auto E
: V
->dsts()) {
509 const GCOVBlock
*W
= &E
->Dst
;
510 if (W
< Start
|| find(Blocks
, W
) == Blocks
.end()) {
513 const size_t index
= find(Blocked
, W
) - Blocked
.begin();
514 BlockVector
&List
= BlockLists
[index
];
515 if (find(List
, V
) == List
.end()) {
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
) {
529 BlockVectorLists BlockLists
;
531 GCOVBlock::lookForCircuit(Block
, Block
, Path
, Blocked
, BlockLists
, Blocks
,
536 /// Get the count for the list of blocks which lie on the same line.
537 uint64_t GCOVBlock::getLineCount(const BlockVector
&Blocks
) {
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();
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()) {
554 for (auto E
: Block
->dsts()) {
555 E
->CyclesCount
= E
->Count
;
559 GCOVBlock::getCyclesCount(Blocks
, Count
);
564 //===----------------------------------------------------------------------===//
565 // FileInfo implementation.
567 // Safe integer division, returns 0 if numerator is 0.
568 static uint32_t safeDiv(uint64_t Numerator
, uint64_t Divisor
) {
571 return Numerator
/ Divisor
;
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
) {
580 if (Numerator
== Divisor
)
583 uint8_t Res
= (Numerator
* 100 + Divisor
/ 2) / Divisor
;
592 struct formatBranchInfo
{
593 formatBranchInfo(const GCOV::Options
&Options
, uint64_t Count
, uint64_t Total
)
594 : Options(Options
), Count(Count
), Total(Total
) {}
596 void print(raw_ostream
&OS
) const {
598 OS
<< "never executed";
599 else if (Options
.BranchCount
)
600 OS
<< "taken " << Count
;
602 OS
<< "taken " << branchDiv(Count
, Total
) << "%";
605 const GCOV::Options
&Options
;
610 static raw_ostream
&operator<<(raw_ostream
&OS
, const formatBranchInfo
&FBI
) {
616 std::unique_ptr
<MemoryBuffer
> Buffer
;
620 LineConsumer(StringRef Filename
) {
621 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> BufferOrErr
=
622 MemoryBuffer::getFileOrSTDIN(Filename
);
623 if (std::error_code EC
= BufferOrErr
.getError()) {
624 errs() << Filename
<< ": " << EC
.message() << "\n";
627 Buffer
= std::move(BufferOrErr
.get());
628 Remaining
= Buffer
->getBuffer();
631 bool empty() { return Remaining
.empty(); }
632 void printNext(raw_ostream
&OS
, uint32_t LineNum
) {
637 std::tie(Line
, Remaining
) = Remaining
.split("\n");
638 OS
<< format("%5u:", LineNum
) << Line
<< "\n";
641 } // end anonymous namespace
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
) {
647 return sys::path::filename(Filename
).str();
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
652 llvm::SmallString
<256> Result("");
653 StringRef::iterator I
, S
, E
;
654 for (I
= S
= Filename
.begin(), E
= Filename
.end(); I
!= E
; ++I
) {
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 "^".
665 // Leave other components intact,
667 // And separate with "#".
668 Result
.push_back('#');
678 std::string
FileInfo::getCoveragePath(StringRef Filename
,
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
686 std::string CoveragePath
;
687 if (Options
.LongFileNames
&& !Filename
.equals(MainFilename
))
689 mangleCoveragePath(MainFilename
, Options
.PreservePaths
) + "##";
690 CoveragePath
+= mangleCoveragePath(Filename
, Options
.PreservePaths
);
691 if (Options
.HashFilenames
) {
693 MD5::MD5Result Result
;
694 Hasher
.update(Filename
.str());
695 Hasher
.final(Result
);
696 CoveragePath
+= "##" + Result
.digest().str().str();
698 CoveragePath
+= ".gcov";
702 std::unique_ptr
<raw_ostream
>
703 FileInfo::openCoveragePath(StringRef CoveragePath
) {
704 if (Options
.NoOutput
)
705 return std::make_unique
<raw_null_ostream
>();
709 std::make_unique
<raw_fd_ostream
>(CoveragePath
, EC
, sys::fs::OF_Text
);
711 errs() << EC
.message() << "\n";
712 return std::make_unique
<raw_null_ostream
>();
714 return std::move(OS
);
717 /// print - Print source files with collected line count information.
718 void FileInfo::print(raw_ostream
&InfoOS
, StringRef MainFilename
,
719 StringRef GCNOFile
, StringRef GCDAFile
) {
720 SmallVector
<StringRef
, 4> Filenames
;
721 for (const auto &LI
: LineInfo
)
722 Filenames
.push_back(LI
.first());
723 llvm::sort(Filenames
);
725 for (StringRef Filename
: Filenames
) {
726 auto AllLines
= LineConsumer(Filename
);
728 std::string CoveragePath
= getCoveragePath(Filename
, MainFilename
);
729 std::unique_ptr
<raw_ostream
> CovStream
= openCoveragePath(CoveragePath
);
730 raw_ostream
&CovOS
= *CovStream
;
732 CovOS
<< " -: 0:Source:" << Filename
<< "\n";
733 CovOS
<< " -: 0:Graph:" << GCNOFile
<< "\n";
734 CovOS
<< " -: 0:Data:" << GCDAFile
<< "\n";
735 CovOS
<< " -: 0:Runs:" << RunCount
<< "\n";
736 CovOS
<< " -: 0:Programs:" << ProgramCount
<< "\n";
738 const LineData
&Line
= LineInfo
[Filename
];
739 GCOVCoverage
FileCoverage(Filename
);
740 for (uint32_t LineIndex
= 0; LineIndex
< Line
.LastLine
|| !AllLines
.empty();
742 if (Options
.BranchInfo
) {
743 FunctionLines::const_iterator FuncsIt
= Line
.Functions
.find(LineIndex
);
744 if (FuncsIt
!= Line
.Functions
.end())
745 printFunctionSummary(CovOS
, FuncsIt
->second
);
748 BlockLines::const_iterator BlocksIt
= Line
.Blocks
.find(LineIndex
);
749 if (BlocksIt
== Line
.Blocks
.end()) {
750 // No basic blocks are on this line. Not an executable line of code.
752 AllLines
.printNext(CovOS
, LineIndex
+ 1);
754 const BlockVector
&Blocks
= BlocksIt
->second
;
756 // Add up the block counts to form line counts.
757 DenseMap
<const GCOVFunction
*, bool> LineExecs
;
758 for (const GCOVBlock
*Block
: Blocks
) {
759 if (Options
.FuncCoverage
) {
760 // This is a slightly convoluted way to most accurately gather line
761 // statistics for functions. Basically what is happening is that we
762 // don't want to count a single line with multiple blocks more than
763 // once. However, we also don't simply want to give the total line
764 // count to every function that starts on the line. Thus, what is
765 // happening here are two things:
766 // 1) Ensure that the number of logical lines is only incremented
767 // once per function.
768 // 2) If there are multiple blocks on the same line, ensure that the
769 // number of lines executed is incremented as long as at least
770 // one of the blocks are executed.
771 const GCOVFunction
*Function
= &Block
->getParent();
772 if (FuncCoverages
.find(Function
) == FuncCoverages
.end()) {
773 std::pair
<const GCOVFunction
*, GCOVCoverage
> KeyValue(
774 Function
, GCOVCoverage(Function
->getName()));
775 FuncCoverages
.insert(KeyValue
);
777 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
779 if (LineExecs
.find(Function
) == LineExecs
.end()) {
780 if (Block
->getCount()) {
781 ++FuncCoverage
.LinesExec
;
782 LineExecs
[Function
] = true;
784 LineExecs
[Function
] = false;
786 ++FuncCoverage
.LogicalLines
;
787 } else if (!LineExecs
[Function
] && Block
->getCount()) {
788 ++FuncCoverage
.LinesExec
;
789 LineExecs
[Function
] = true;
794 const uint64_t LineCount
= GCOVBlock::getLineCount(Blocks
);
798 CovOS
<< format("%9" PRIu64
":", LineCount
);
799 ++FileCoverage
.LinesExec
;
801 ++FileCoverage
.LogicalLines
;
803 AllLines
.printNext(CovOS
, LineIndex
+ 1);
805 uint32_t BlockNo
= 0;
807 for (const GCOVBlock
*Block
: Blocks
) {
808 // Only print block and branch information at the end of the block.
809 if (Block
->getLastLine() != LineIndex
+ 1)
811 if (Options
.AllBlocks
)
812 printBlockInfo(CovOS
, *Block
, LineIndex
, BlockNo
);
813 if (Options
.BranchInfo
) {
814 size_t NumEdges
= Block
->getNumDstEdges();
816 printBranchInfo(CovOS
, *Block
, FileCoverage
, EdgeNo
);
817 else if (Options
.UncondBranch
&& NumEdges
== 1)
818 printUncondBranchInfo(CovOS
, EdgeNo
,
819 (*Block
->dst_begin())->Count
);
824 FileCoverages
.push_back(std::make_pair(CoveragePath
, FileCoverage
));
827 // FIXME: There is no way to detect calls given current instrumentation.
828 if (Options
.FuncCoverage
)
829 printFuncCoverage(InfoOS
);
830 printFileCoverage(InfoOS
);
833 /// printFunctionSummary - Print function and block summary.
834 void FileInfo::printFunctionSummary(raw_ostream
&OS
,
835 const FunctionVector
&Funcs
) const {
836 for (const GCOVFunction
*Func
: Funcs
) {
837 uint64_t EntryCount
= Func
->getEntryCount();
838 uint32_t BlocksExec
= 0;
839 for (const GCOVBlock
&Block
: Func
->blocks())
840 if (Block
.getNumDstEdges() && Block
.getCount())
843 OS
<< "function " << Func
->getName() << " called " << EntryCount
844 << " returned " << safeDiv(Func
->getExitCount() * 100, EntryCount
)
845 << "% blocks executed "
846 << safeDiv(BlocksExec
* 100, Func
->getNumBlocks() - 1) << "%\n";
850 /// printBlockInfo - Output counts for each block.
851 void FileInfo::printBlockInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
852 uint32_t LineIndex
, uint32_t &BlockNo
) const {
853 if (Block
.getCount() == 0)
856 OS
<< format("%9" PRIu64
":", Block
.getCount());
857 OS
<< format("%5u-block %2u\n", LineIndex
+ 1, BlockNo
++);
860 /// printBranchInfo - Print conditional branch probabilities.
861 void FileInfo::printBranchInfo(raw_ostream
&OS
, const GCOVBlock
&Block
,
862 GCOVCoverage
&Coverage
, uint32_t &EdgeNo
) {
863 SmallVector
<uint64_t, 16> BranchCounts
;
864 uint64_t TotalCounts
= 0;
865 for (const GCOVEdge
*Edge
: Block
.dsts()) {
866 BranchCounts
.push_back(Edge
->Count
);
867 TotalCounts
+= Edge
->Count
;
868 if (Block
.getCount())
869 ++Coverage
.BranchesExec
;
871 ++Coverage
.BranchesTaken
;
874 if (Options
.FuncCoverage
) {
875 const GCOVFunction
*Function
= &Block
.getParent();
876 GCOVCoverage
&FuncCoverage
= FuncCoverages
.find(Function
)->second
;
877 if (Block
.getCount())
878 ++FuncCoverage
.BranchesExec
;
880 ++FuncCoverage
.BranchesTaken
;
881 ++FuncCoverage
.Branches
;
885 for (uint64_t N
: BranchCounts
)
886 OS
<< format("branch %2u ", EdgeNo
++)
887 << formatBranchInfo(Options
, N
, TotalCounts
) << "\n";
890 /// printUncondBranchInfo - Print unconditional branch probabilities.
891 void FileInfo::printUncondBranchInfo(raw_ostream
&OS
, uint32_t &EdgeNo
,
892 uint64_t Count
) const {
893 OS
<< format("unconditional %2u ", EdgeNo
++)
894 << formatBranchInfo(Options
, Count
, Count
) << "\n";
897 // printCoverage - Print generic coverage info used by both printFuncCoverage
898 // and printFileCoverage.
899 void FileInfo::printCoverage(raw_ostream
&OS
,
900 const GCOVCoverage
&Coverage
) const {
901 OS
<< format("Lines executed:%.2f%% of %u\n",
902 double(Coverage
.LinesExec
) * 100 / Coverage
.LogicalLines
,
903 Coverage
.LogicalLines
);
904 if (Options
.BranchInfo
) {
905 if (Coverage
.Branches
) {
906 OS
<< format("Branches executed:%.2f%% of %u\n",
907 double(Coverage
.BranchesExec
) * 100 / Coverage
.Branches
,
909 OS
<< format("Taken at least once:%.2f%% of %u\n",
910 double(Coverage
.BranchesTaken
) * 100 / Coverage
.Branches
,
913 OS
<< "No branches\n";
915 OS
<< "No calls\n"; // to be consistent with gcov
919 // printFuncCoverage - Print per-function coverage info.
920 void FileInfo::printFuncCoverage(raw_ostream
&OS
) const {
921 for (const auto &FC
: FuncCoverages
) {
922 const GCOVCoverage
&Coverage
= FC
.second
;
923 OS
<< "Function '" << Coverage
.Name
<< "'\n";
924 printCoverage(OS
, Coverage
);
929 // printFileCoverage - Print per-file coverage info.
930 void FileInfo::printFileCoverage(raw_ostream
&OS
) const {
931 for (const auto &FC
: FileCoverages
) {
932 const std::string
&Filename
= FC
.first
;
933 const GCOVCoverage
&Coverage
= FC
.second
;
934 OS
<< "File '" << Coverage
.Name
<< "'\n";
935 printCoverage(OS
, Coverage
);
936 if (!Options
.NoOutput
)
937 OS
<< Coverage
.Name
<< ":creating '" << Filename
<< "'\n";