17 #include "llvm/Config/llvm-config.h" 24 #include <system_error> 43 auto GFun = make_unique<GCOVFunction>(*this);
44 if (!GFun->readGCNO(Buffer, Version))
46 Functions.push_back(std::move(GFun));
49 GCNOInitialized =
true;
56 assert(GCNOInitialized &&
"readGCDA() can only be called after readGCNO()");
62 if (Version != GCDAVersion) {
63 errs() <<
"GCOV versions do not match.\n";
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) {
77 errs() <<
"Unexpected number of functions.\n";
80 if (!Functions[i]->
readGCDA(Buffer, Version))
108 for (
const auto &FPtr : Functions)
112 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 122 for (
const auto &FPtr : Functions)
123 FPtr->collectLineCounts(FI);
143 if (!Buff.
readInt(CfgChecksum))
145 if (Parent.getChecksum() != CfgChecksum) {
146 errs() <<
"File checksums do not match: " << Parent.getChecksum()
147 <<
" != " << CfgChecksum <<
" in (" <<
Name <<
").\n";
160 errs() <<
"Block tag not found.\n";
166 for (
uint32_t i = 0, e = BlockCount; i != e; ++i) {
169 Blocks.push_back(make_unique<GCOVBlock>(*
this, i));
177 EdgeCount = (EdgeCount - 1) / 2;
181 if (BlockNo >= BlockCount) {
182 errs() <<
"Unexpected block number: " << BlockNo <<
" (in " <<
Name 186 for (
uint32_t i = 0, e = EdgeCount; i != e; ++i) {
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);
203 if (!Buff.
readInt(LineTableLength))
210 if (BlockNo >= BlockCount) {
211 errs() <<
"Unexpected block number: " << BlockNo <<
" (in " <<
Name 228 errs() <<
"Multiple sources for a single basic block: " << Filename
229 <<
" != " << F <<
" (in " <<
Name <<
").\n";
258 if (!Buff.
readInt(HeaderLength))
266 if (Ident != GCDAIdent) {
267 errs() <<
"Function identifiers do not match: " << Ident
268 <<
" != " << GCDAIdent <<
" (in " <<
Name <<
").\n";
273 if (!Buff.
readInt(GCDAChecksum))
275 if (Checksum != GCDAChecksum) {
276 errs() <<
"Function checksums do not match: " << Checksum
277 <<
" != " << GCDAChecksum <<
" (in " <<
Name <<
").\n";
283 if (!Buff.
readInt(CfgChecksum))
285 if (Parent.getChecksum() != CfgChecksum) {
286 errs() <<
"File checksums do not match: " << Parent.getChecksum()
287 <<
" != " << CfgChecksum <<
" (in " <<
Name <<
").\n";
296 if (
Name != GCDAName) {
297 errs() <<
"Function names do not match: " <<
Name <<
" != " << GCDAName
304 errs() <<
"Arc tag not found (in " <<
Name <<
").\n";
315 for (
uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
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";
324 for (
size_t EdgeNo = 0, End = Block.
getNumDstEdges(); EdgeNo < End;
327 errs() <<
"Unexpected number of edges (in " <<
Name <<
").\n";
344 return Blocks.front()->getCount();
350 return Blocks.back()->getCount();
354 OS <<
"===== " <<
Name <<
" (" << Ident <<
") @ " << Filename <<
":" 355 << LineNumber <<
"\n";
356 for (
const auto &Block : Blocks)
360 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 375 for (
const auto &Block : Blocks)
376 Block->collectLineCounts(FI);
393 assert(DstEdgeNo < DstEdges.size());
394 DstEdges[DstEdgeNo]->Count =
N;
396 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
397 DstEdges[DstEdgeNo]->Dst.Counter += N;
403 if (!DstEdgesAreSorted) {
404 SortDstEdgesFunctor SortEdges;
405 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
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()) {
438 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 452 return Numerator / 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) {}
479 OS <<
"never executed";
481 OS <<
"taken " << Count;
483 OS <<
"taken " <<
branchDiv(Count, Total) <<
"%";
497 std::unique_ptr<MemoryBuffer> Buffer;
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(); }
518 std::tie(Line, Remaining) = Remaining.
split(
"\n");
519 OS <<
format(
"%5u:", LineNum) << Line <<
"\n";
535 for (I = S = Filename.
begin(), E = Filename.
end(); I !=
E; ++
I) {
539 if (I - S == 1 && *S ==
'.') {
541 }
else if (I - S == 2 && *S ==
'.' && *(S + 1) ==
'.') {
561 if (Options.NoOutput)
567 std::string CoveragePath;
568 if (Options.LongFileNames && !Filename.
equals(MainFilename))
575 std::unique_ptr<raw_ostream>
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);
594 for (
const auto &LI : LineInfo)
599 auto AllLines = LineConsumer(Filename);
601 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
602 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
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];
613 for (
uint32_t LineIndex = 0; LineIndex < Line.
LastLine || !AllLines.empty();
615 if (Options.BranchInfo) {
618 printFunctionSummary(CovOS, FuncsIt->second);
625 AllLines.printNext(CovOS, LineIndex + 1);
631 uint64_t LineCount = 0;
633 if (Options.AllBlocks) {
635 uint64_t BlockCount = Block->getCount();
636 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
639 LineCount += Block->getCount();
642 if (Options.FuncCoverage) {
655 if (FuncCoverages.find(Function) == FuncCoverages.end()) {
656 std::pair<const GCOVFunction *, GCOVCoverage> KeyValue(
658 FuncCoverages.insert(KeyValue);
660 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
662 if (LineExecs.
find(Function) == LineExecs.
end()) {
663 if (Block->getCount()) {
665 LineExecs[Function] =
true;
667 LineExecs[Function] =
false;
670 }
else if (!LineExecs[Function] && Block->getCount()) {
672 LineExecs[Function] =
true;
680 CovOS <<
format(
"%9" PRIu64
":", LineCount);
685 AllLines.printNext(CovOS, LineIndex + 1);
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));
710 if (Options.FuncCoverage)
711 printFuncCoverage(InfoOS);
712 printFileCoverage(InfoOS);
719 uint64_t EntryCount = Func->getEntryCount();
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";
739 OS <<
format(
"%5u-block %2u\n", LineIndex + 1, BlockNo++);
746 uint64_t TotalCounts = 0;
749 TotalCounts += Edge->Count;
756 if (Options.FuncCoverage) {
758 GCOVCoverage &FuncCoverage = FuncCoverages.find(Function)->second;
767 for (uint64_t
N : BranchCounts)
768 OS <<
format(
"branch %2u ", EdgeNo++)
769 << formatBranchInfo(Options,
N, TotalCounts) <<
"\n";
774 uint64_t Count)
const {
775 OS <<
format(
"unconditional %2u ", EdgeNo++)
776 << formatBranchInfo(Options, Count, Count) <<
"\n";
783 OS <<
format(
"Lines executed:%.2f%% of %u\n",
786 if (Options.BranchInfo) {
788 OS <<
format(
"Branches executed:%.2f%% of %u\n",
791 OS <<
format(
"Taken at least once:%.2f%% of %u\n",
795 OS <<
"No branches\n";
803 for (
const auto &
FC : FuncCoverages) {
805 OS <<
"Function '" << Coverage.
Name <<
"'\n";
806 printCoverage(OS, Coverage);
813 for (
const auto &
FC : FileCoverages) {
814 const std::string &Filename =
FC.first;
816 OS <<
"File '" << Coverage.
Name <<
"'\n";
817 printCoverage(OS, Coverage);
818 if (!Options.NoOutput)
819 OS << Coverage.
Name <<
":creating '" << Filename <<
"'\n";
void push_back(const T &Elt)
Represents either an error or a value T.
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.
bool readInt(uint32_t &Val)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
GCOVEdge - Collects edge information.
Compute iterated dominance frontiers using a linear time algorithm.
void addFunctionLine(StringRef Filename, uint32_t Line, const GCOVFunction *Function)
void print(raw_ostream &OS, StringRef MainFilename, StringRef GCNOFile, StringRef GCDAFile)
print - Print source files with collected line count information.
bool readLineTag()
readLineTag - If cursor points to a line tag then increment the cursor and return true otherwise retu...
void setProgramCount(uint32_t Programs)
void printFuncCoverage(raw_ostream &OS) const
bool readString(StringRef &Str)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
void printBlockInfo(raw_ostream &OS, const GCOVBlock &Block, uint32_t LineIndex, uint32_t &BlockNo) const
printBlockInfo - Output counts for each block.
A struct for passing gcov options between functions.
uint64_t getCursor() const
void addBlockLine(StringRef Filename, uint32_t Line, const GCOVBlock *Block)
static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths)
Convert a path to a gcov filename.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific read operations.
void print(raw_ostream &OS) const
uint64_t getCount() const
static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor)
void printFunctionSummary(raw_ostream &OS, const FunctionVector &Funcs) const
printFunctionSummary - Print function and block summary.
void printFileCoverage(raw_ostream &OS) const
std::string getCoveragePath(StringRef Filename, StringRef MainFilename)
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
StringRef str() const
Explicit conversion to StringRef.
bool readGCDA(GCOVBuffer &Buffer)
readGCDA - Read GCDA buffer.
bool readArcTag()
readArcTag - If cursor points to an gcda arc tag then increment the cursor and return true otherwise ...
void dump() const
dump - Dump GCOVBlock content to dbgs() for debugging purposes.
StringRef getName() const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
GCOVBlock - Collects block information.
void sortDstEdges()
sortDstEdges - Sort destination edges by block number, nop if already sorted.
bool readInt64(uint64_t &Val)
void printCoverage(raw_ostream &OS, const GCOVCoverage &Coverage) const
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
void printBranchInfo(raw_ostream &OS, const GCOVBlock &Block, GCOVCoverage &Coverage, uint32_t &EdgeNo)
printBranchInfo - Print conditional branch probabilities.
std::unique_ptr< raw_ostream > openCoveragePath(StringRef CoveragePath)
iterator find(const_arg_type_t< KeyT > Val)
void append(in_iter S, in_iter E)
Append from an iterator pair.
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
iterator_range< EdgeIterator > dsts() const
bool readBlockTag()
readBlockTag - If cursor points to a block tag then increment the cursor and return true otherwise re...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
std::error_code getError() const
uint64_t getExitCount() const
getExitCount - Get the number of times the function returned by retrieving the exit block's count...
bool readGCNOFormat()
readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
void advanceCursor(uint32_t n)
bool readGCNO(GCOVBuffer &Buffer)
readGCNO - Read GCNO buffer.
const GCOVFunction & getParent() const
bool readEdgeTag()
readEdgeTag - If cursor points to an edge tag then increment the cursor and return true otherwise ret...
void sort(IteratorTy Start, IteratorTy End)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool readGCDA(GCOVBuffer &Buffer, GCOV::GCOVVersion Version)
readGCDA - Read a function from the GCDA buffer.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void printUncondBranchInfo(raw_ostream &OS, uint32_t &EdgeNo, uint64_t Count) const
printUncondBranchInfo - Print unconditional branch probabilities.
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 ...
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.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
void dump() const
dump - Dump GCOVFile content to dbgs() for debugging purposes.
size_t getNumDstEdges() const
GCOVFunction - Collects function information.
StringRef filename(StringRef path, Style style=Style::native)
Get filename.
static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor)
bool readObjectTag()
readObjectTag - If cursor points to an object summary tag then increment the cursor and return true o...
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
~GCOVBlock()
~GCOVBlock - Delete GCOVBlock and its content.
This class implements an extremely fast bulk output stream that can only output to a stream...
bool readProgramTag()
readProgramTag - If cursor points to a program summary tag then increment the cursor and return true ...
StringRef - Represent a constant reference to a string, i.e.
bool readFunctionTag()
readFunctionTag - If cursor points to a function tag then increment the cursor and return true otherw...
void addCount(size_t DstEdgeNo, uint64_t N)
addCount - Add to block counter while storing the edge count.
void setRunCount(uint32_t Runs)
bool readGCOVVersion(GCOV::GCOVVersion &Version)
readGCOVVersion - Read GCOV version.
void print(raw_ostream &OS) const
void print(raw_ostream &OS) const
void dump() const
dump - Dump GCOVFunction content to dbgs() for debugging purposes.
uint64_t getEntryCount() const
getEntryCount - Get the number of times the function was called by retrieving the entry block's count...