23 #include <system_error>
42 auto GFun = make_unique<GCOVFunction>(*this);
43 if (!GFun->readGCNO(Buffer, Version))
45 Functions.push_back(std::move(GFun));
48 GCNOInitialized =
true;
55 assert(GCNOInitialized &&
"readGCDA() can only be called after readGCNO()");
61 if (Version != GCDAVersion) {
62 errs() <<
"GCOV versions do not match.\n";
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) {
76 errs() <<
"Unexpected number of functions.\n";
79 if (!Functions[i]->
readGCDA(Buffer, Version))
107 for (
const auto &FPtr : Functions)
111 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
121 for (
const auto &FPtr : Functions)
122 FPtr->collectLineCounts(FI);
142 if (!Buff.
readInt(CfgChecksum))
146 <<
" != " << CfgChecksum <<
" in (" << Name <<
").\n";
159 errs() <<
"Block tag not found.\n";
165 for (
uint32_t i = 0, e = BlockCount; i != e; ++i) {
168 Blocks.push_back(make_unique<GCOVBlock>(*
this, i));
176 EdgeCount = (EdgeCount - 1) / 2;
180 if (BlockNo >= BlockCount) {
181 errs() <<
"Unexpected block number: " << BlockNo <<
" (in " << Name
185 for (
uint32_t i = 0, e = EdgeCount; i != e; ++i) {
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);
202 if (!Buff.
readInt(LineTableLength))
209 if (BlockNo >= BlockCount) {
210 errs() <<
"Unexpected block number: " << BlockNo <<
" (in " << Name
227 errs() <<
"Multiple sources for a single basic block: " << Filename
228 <<
" != " << F <<
" (in " << Name <<
").\n";
257 if (!Buff.
readInt(HeaderLength))
265 if (Ident != GCDAIdent) {
266 errs() <<
"Function identifiers do not match: " << Ident
267 <<
" != " << GCDAIdent <<
" (in " << Name <<
").\n";
272 if (!Buff.
readInt(GCDAChecksum))
274 if (Checksum != GCDAChecksum) {
275 errs() <<
"Function checksums do not match: " << Checksum
276 <<
" != " << GCDAChecksum <<
" (in " << Name <<
").\n";
282 if (!Buff.
readInt(CfgChecksum))
286 <<
" != " << CfgChecksum <<
" (in " << Name <<
").\n";
295 if (Name != GCDAName) {
296 errs() <<
"Function names do not match: " << Name <<
" != " << GCDAName
303 errs() <<
"Arc tag not found (in " << Name <<
").\n";
314 for (
uint32_t BlockNo = 0; Count > 0; ++BlockNo) {
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";
326 errs() <<
"Unexpected number of edges (in " << Name <<
").\n";
343 return Blocks.front()->getCount();
349 return Blocks.back()->getCount();
353 OS <<
"===== " << Name <<
" (" << Ident <<
") @ " << Filename <<
":"
354 << LineNumber <<
"\n";
355 for (
const auto &Block : Blocks)
359 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
374 for (
const auto &Block : Blocks)
375 Block->collectLineCounts(FI);
392 assert(DstEdgeNo < DstEdges.size());
393 DstEdges[DstEdgeNo]->Count =
N;
395 if (!DstEdges[DstEdgeNo]->Dst.getNumDstEdges())
396 DstEdges[DstEdgeNo]->Dst.Counter += N;
402 if (!DstEdgesAreSorted) {
403 SortDstEdgesFunctor SortEdges;
404 std::stable_sort(DstEdges.begin(), DstEdges.end(), SortEdges);
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()) {
437 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
451 return Numerator / 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) {}
478 OS <<
"never executed";
479 else if (Options.BranchCount)
480 OS <<
"taken " << Count;
482 OS <<
"taken " <<
branchDiv(Count, Total) <<
"%";
496 std::unique_ptr<MemoryBuffer> Buffer;
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(); }
517 std::tie(Line, Remaining) = Remaining.
split(
"\n");
518 OS <<
format(
"%5u:", LineNum) << Line <<
"\n";
534 for (I = S = Filename.
begin(), E = Filename.
end(); I !=
E; ++
I) {
538 if (I - S == 1 && *S ==
'.') {
540 }
else if (I - S == 2 && *S ==
'.' && *(S + 1) ==
'.') {
558 std::string FileInfo::getCoveragePath(
StringRef Filename,
566 std::string CoveragePath;
574 std::unique_ptr<raw_ostream>
575 FileInfo::openCoveragePath(
StringRef CoveragePath) {
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);
593 for (
const auto &LI : LineInfo)
598 auto AllLines = LineConsumer(Filename);
600 std::string CoveragePath = getCoveragePath(Filename, MainFilename);
601 std::unique_ptr<raw_ostream> CovStream = openCoveragePath(CoveragePath);
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();
616 if (FuncsIt != Line.Functions.end())
617 printFunctionSummary(CovOS, FuncsIt->second);
621 if (BlocksIt == Line.Blocks.end()) {
624 AllLines.printNext(CovOS, LineIndex + 1);
630 uint64_t LineCount = 0;
634 uint64_t BlockCount = Block->getCount();
635 LineCount = LineCount > BlockCount ? LineCount : BlockCount;
638 LineCount += Block->getCount();
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);
690 if (Block->getLastLine() != LineIndex + 1)
693 printBlockInfo(CovOS, *Block, LineIndex, BlockNo);
695 size_t NumEdges = Block->getNumDstEdges();
697 printBranchInfo(CovOS, *Block, FileCoverage, EdgeNo);
699 printUncondBranchInfo(CovOS, EdgeNo,
700 (*Block->dst_begin())->Count);
705 FileCoverages.
push_back(std::make_pair(CoveragePath, FileCoverage));
710 printFuncCoverage(InfoOS);
711 printFileCoverage(InfoOS);
715 void FileInfo::printFunctionSummary(
raw_ostream &OS,
716 const FunctionVector &Funcs)
const {
718 uint64_t EntryCount = Func->getEntryCount();
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";
738 OS <<
format(
"%5u-block %2u\n", LineIndex + 1, BlockNo++);
743 GCOVCoverage &Coverage,
uint32_t &EdgeNo) {
745 uint64_t TotalCounts = 0;
748 TotalCounts += Edge->Count;
750 ++Coverage.BranchesExec;
752 ++Coverage.BranchesTaken;
757 GCOVCoverage &FuncCoverage = FuncCoverages.
find(Function)->second;
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";
773 uint64_t Count)
const {
774 OS <<
format(
"unconditional %2u ", EdgeNo++)
775 << formatBranchInfo(Options, Count, Count) <<
"\n";
781 const GCOVCoverage &Coverage)
const {
782 OS <<
format(
"Lines executed:%.2f%% of %u\n",
783 double(Coverage.LinesExec) * 100 / Coverage.LogicalLines,
784 Coverage.LogicalLines);
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";
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);
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);
818 OS << Coverage.Name <<
":creating '" << Filename <<
"'\n";
void push_back(const T &Elt)
std::error_code getError() const
Represents either an error or a value T.
iterator_range< EdgeIterator > dsts() const
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)
bool readGCDAFormat()
readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
GCOVEdge - Collects edge information.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
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.
void print(raw_ostream &OS) const
uint64_t getCount() const
bool readLineTag()
readLineTag - If cursor points to a line tag then increment the cursor and return true otherwise retu...
void setProgramCount(uint32_t Programs)
bool readString(StringRef &Str)
uint32_t getChecksum() const
A struct for passing gcov options between functions.
void dump() const
dump - Dump GCOVFile content to dbgs() for debugging purposes.
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.
GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific read operations.
void print(raw_ostream &OS) const
StringRef getName() const
static uint32_t safeDiv(uint64_t Numerator, uint64_t Divisor)
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 ...
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
LLVM_NODISCARD bool empty() const
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.
GCOVBlock - Collects block information.
uint64_t getExitCount() const
getExitCount - Get the number of times the function returned by retrieving the exit block's count...
void print(raw_ostream &OS) const
iterator find(const KeyT &Key)
void sortDstEdges()
sortDstEdges - Sort destination edges by block number, nop if already sorted.
bool readInt64(uint64_t &Val)
void collectLineCounts(FileInfo &FI)
collectLineCounts - Collect line counts.
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
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.
bool readBlockTag()
readBlockTag - If cursor points to a block tag then increment the cursor and return true otherwise re...
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
static const unsigned End
void dump() const
dump - Dump GCOVFunction content to dbgs() for debugging purposes.
bool readGCNOFormat()
readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
void advanceCursor(uint32_t n)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
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...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
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.
StringRef str() const
Explicit conversion to StringRef.
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
The file should be opened in text mode on platforms that make this distinction.
StringRef getFilename() const
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()
GCOVFunction - Collects function information.
size_t getNumDstEdges() const
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)
uint64_t getEntryCount() const
getEntryCount - Get the number of times the function was called by retrieving the entry block's count...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
uint64_t getCursor() const
~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 sort(Policy policy, RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
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.