Path: blob/main/contrib/llvm-project/llvm/lib/ProfileData/GCOV.cpp
35233 views
//===- GCOV.cpp - LLVM coverage tool --------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// GCOV implements the interface to read and write coverage files that use9// 'gcov' format.10//11//===----------------------------------------------------------------------===//1213#include "llvm/ProfileData/GCOV.h"14#include "llvm/ADT/STLExtras.h"15#include "llvm/ADT/SmallSet.h"16#include "llvm/Config/llvm-config.h"17#include "llvm/Demangle/Demangle.h"18#include "llvm/Support/Debug.h"19#include "llvm/Support/FileSystem.h"20#include "llvm/Support/Format.h"21#include "llvm/Support/MD5.h"22#include "llvm/Support/Path.h"23#include "llvm/Support/raw_ostream.h"24#include <algorithm>25#include <optional>26#include <system_error>2728using namespace llvm;2930enum : uint32_t {31GCOV_ARC_ON_TREE = 1 << 0,32GCOV_ARC_FALLTHROUGH = 1 << 2,3334GCOV_TAG_FUNCTION = 0x01000000,35GCOV_TAG_BLOCKS = 0x01410000,36GCOV_TAG_ARCS = 0x01430000,37GCOV_TAG_LINES = 0x01450000,38GCOV_TAG_COUNTER_ARCS = 0x01a10000,39// GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.40GCOV_TAG_OBJECT_SUMMARY = 0xa1000000,41GCOV_TAG_PROGRAM_SUMMARY = 0xa3000000,42};4344namespace {45struct Summary {46Summary(StringRef Name) : Name(Name) {}4748StringRef Name;49uint64_t lines = 0;50uint64_t linesExec = 0;51uint64_t branches = 0;52uint64_t branchesExec = 0;53uint64_t branchesTaken = 0;54};5556struct LineInfo {57SmallVector<const GCOVBlock *, 1> blocks;58uint64_t count = 0;59bool exists = false;60};6162struct SourceInfo {63StringRef filename;64SmallString<0> displayName;65std::vector<std::vector<const GCOVFunction *>> startLineToFunctions;66std::vector<LineInfo> lines;67bool ignored = false;68SourceInfo(StringRef filename) : filename(filename) {}69};7071class Context {72public:73Context(const GCOV::Options &Options) : options(Options) {}74void print(StringRef filename, StringRef gcno, StringRef gcda,75GCOVFile &file);7677private:78std::string getCoveragePath(StringRef filename, StringRef mainFilename) const;79void printFunctionDetails(const GCOVFunction &f, raw_ostream &os) const;80void printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,81raw_ostream &OS) const;82void printSummary(const Summary &summary, raw_ostream &os) const;8384void collectFunction(GCOVFunction &f, Summary &summary);85void collectSourceLine(SourceInfo &si, Summary *summary, LineInfo &line,86size_t lineNum) const;87void collectSource(SourceInfo &si, Summary &summary) const;88void annotateSource(SourceInfo &si, const GCOVFile &file, StringRef gcno,89StringRef gcda, raw_ostream &os) const;90void printSourceToIntermediate(const SourceInfo &si, raw_ostream &os) const;9192const GCOV::Options &options;93std::vector<SourceInfo> sources;94};95} // namespace9697//===----------------------------------------------------------------------===//98// GCOVFile implementation.99100/// readGCNO - Read GCNO buffer.101bool GCOVFile::readGCNO(GCOVBuffer &buf) {102if (!buf.readGCNOFormat())103return false;104if (!buf.readGCOVVersion(version))105return false;106107checksum = buf.getWord();108if (version >= GCOV::V900 && !buf.readString(cwd))109return false;110if (version >= GCOV::V800)111buf.getWord(); // hasUnexecutedBlocks112113uint32_t tag, length;114GCOVFunction *fn = nullptr;115while ((tag = buf.getWord())) {116if (!buf.readInt(length))117return false;118uint32_t pos = buf.cursor.tell();119if (tag == GCOV_TAG_FUNCTION) {120functions.push_back(std::make_unique<GCOVFunction>(*this));121fn = functions.back().get();122fn->ident = buf.getWord();123fn->linenoChecksum = buf.getWord();124if (version >= GCOV::V407)125fn->cfgChecksum = buf.getWord();126buf.readString(fn->Name);127StringRef filename;128if (version < GCOV::V800) {129if (!buf.readString(filename))130return false;131fn->startLine = buf.getWord();132} else {133fn->artificial = buf.getWord();134if (!buf.readString(filename))135return false;136fn->startLine = buf.getWord();137fn->startColumn = buf.getWord();138fn->endLine = buf.getWord();139if (version >= GCOV::V900)140fn->endColumn = buf.getWord();141}142fn->srcIdx = addNormalizedPathToMap(filename);143identToFunction[fn->ident] = fn;144} else if (tag == GCOV_TAG_BLOCKS && fn) {145if (version < GCOV::V800) {146for (uint32_t i = 0; i != length; ++i) {147buf.getWord(); // Ignored block flags148fn->blocks.push_back(std::make_unique<GCOVBlock>(i));149}150} else {151uint32_t num = buf.getWord();152for (uint32_t i = 0; i != num; ++i)153fn->blocks.push_back(std::make_unique<GCOVBlock>(i));154}155} else if (tag == GCOV_TAG_ARCS && fn) {156uint32_t srcNo = buf.getWord();157if (srcNo >= fn->blocks.size()) {158errs() << "unexpected block number: " << srcNo << " (in "159<< fn->blocks.size() << ")\n";160return false;161}162GCOVBlock *src = fn->blocks[srcNo].get();163const uint32_t e =164version >= GCOV::V1200 ? (length / 4 - 1) / 2 : (length - 1) / 2;165for (uint32_t i = 0; i != e; ++i) {166uint32_t dstNo = buf.getWord(), flags = buf.getWord();167GCOVBlock *dst = fn->blocks[dstNo].get();168auto arc = std::make_unique<GCOVArc>(*src, *dst, flags);169src->addDstEdge(arc.get());170dst->addSrcEdge(arc.get());171if (arc->onTree())172fn->treeArcs.push_back(std::move(arc));173else174fn->arcs.push_back(std::move(arc));175}176} else if (tag == GCOV_TAG_LINES && fn) {177uint32_t srcNo = buf.getWord();178if (srcNo >= fn->blocks.size()) {179errs() << "unexpected block number: " << srcNo << " (in "180<< fn->blocks.size() << ")\n";181return false;182}183GCOVBlock &Block = *fn->blocks[srcNo];184for (;;) {185uint32_t line = buf.getWord();186if (line)187Block.addLine(line);188else {189StringRef filename;190buf.readString(filename);191if (filename.empty())192break;193// TODO Unhandled194}195}196}197pos += version >= GCOV::V1200 ? length : 4 * length;198if (pos < buf.cursor.tell())199return false;200buf.de.skip(buf.cursor, pos - buf.cursor.tell());201}202203GCNOInitialized = true;204return true;205}206207/// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be208/// called after readGCNO().209bool GCOVFile::readGCDA(GCOVBuffer &buf) {210assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");211if (!buf.readGCDAFormat())212return false;213GCOV::GCOVVersion GCDAVersion;214if (!buf.readGCOVVersion(GCDAVersion))215return false;216if (version != GCDAVersion) {217errs() << "GCOV versions do not match.\n";218return false;219}220221uint32_t GCDAChecksum;222if (!buf.readInt(GCDAChecksum))223return false;224if (checksum != GCDAChecksum) {225errs() << "file checksums do not match: " << checksum226<< " != " << GCDAChecksum << "\n";227return false;228}229uint32_t dummy, tag, length;230uint32_t ident;231GCOVFunction *fn = nullptr;232while ((tag = buf.getWord())) {233if (!buf.readInt(length))234return false;235uint32_t pos = buf.cursor.tell();236if (tag == GCOV_TAG_OBJECT_SUMMARY) {237buf.readInt(runCount);238buf.readInt(dummy);239} else if (tag == GCOV_TAG_PROGRAM_SUMMARY) {240buf.readInt(dummy);241buf.readInt(dummy);242buf.readInt(runCount);243++programCount;244} else if (tag == GCOV_TAG_FUNCTION) {245if (length == 0) // Placeholder246continue;247if (length < 2 || !buf.readInt(ident))248return false;249auto It = identToFunction.find(ident);250uint32_t linenoChecksum, cfgChecksum = 0;251buf.readInt(linenoChecksum);252if (version >= GCOV::V407)253buf.readInt(cfgChecksum);254if (It != identToFunction.end()) {255fn = It->second;256if (linenoChecksum != fn->linenoChecksum ||257cfgChecksum != fn->cfgChecksum) {258errs() << fn->Name259<< format(": checksum mismatch, (%u, %u) != (%u, %u)\n",260linenoChecksum, cfgChecksum, fn->linenoChecksum,261fn->cfgChecksum);262return false;263}264}265} else if (tag == GCOV_TAG_COUNTER_ARCS && fn) {266uint32_t expected = 2 * fn->arcs.size();267if (version >= GCOV::V1200)268expected *= 4;269if (length != expected) {270errs() << fn->Name271<< format(272": GCOV_TAG_COUNTER_ARCS mismatch, got %u, expected %u\n",273length, expected);274return false;275}276for (std::unique_ptr<GCOVArc> &arc : fn->arcs) {277if (!buf.readInt64(arc->count))278return false;279arc->src.count += arc->count;280}281282if (fn->blocks.size() >= 2) {283GCOVBlock &src = *fn->blocks[0];284GCOVBlock &sink =285version < GCOV::V408 ? *fn->blocks.back() : *fn->blocks[1];286auto arc = std::make_unique<GCOVArc>(sink, src, GCOV_ARC_ON_TREE);287sink.addDstEdge(arc.get());288src.addSrcEdge(arc.get());289fn->treeArcs.push_back(std::move(arc));290291for (GCOVBlock &block : fn->blocksRange())292fn->propagateCounts(block, nullptr);293for (size_t i = fn->treeArcs.size() - 1; i; --i)294fn->treeArcs[i - 1]->src.count += fn->treeArcs[i - 1]->count;295}296}297pos += version >= GCOV::V1200 ? length : 4 * length;298if (pos < buf.cursor.tell())299return false;300buf.de.skip(buf.cursor, pos - buf.cursor.tell());301}302303return true;304}305306void GCOVFile::print(raw_ostream &OS) const {307for (const GCOVFunction &f : *this)308f.print(OS);309}310311#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)312/// dump - Dump GCOVFile content to dbgs() for debugging purposes.313LLVM_DUMP_METHOD void GCOVFile::dump() const { print(dbgs()); }314#endif315316unsigned GCOVFile::addNormalizedPathToMap(StringRef filename) {317// unify filename, as the same path can have different form318SmallString<256> P(filename);319sys::path::remove_dots(P, true);320filename = P.str();321322auto r = filenameToIdx.try_emplace(filename, filenameToIdx.size());323if (r.second)324filenames.emplace_back(filename);325326return r.first->second;327}328329bool GCOVArc::onTree() const { return flags & GCOV_ARC_ON_TREE; }330331//===----------------------------------------------------------------------===//332// GCOVFunction implementation.333334StringRef GCOVFunction::getName(bool demangle) const {335if (!demangle)336return Name;337if (demangled.empty()) {338do {339if (Name.starts_with("_Z")) {340// Name is guaranteed to be NUL-terminated.341if (char *res = itaniumDemangle(Name.data())) {342demangled = res;343free(res);344break;345}346}347demangled = Name;348} while (false);349}350return demangled;351}352StringRef GCOVFunction::getFilename() const { return file.filenames[srcIdx]; }353354/// getEntryCount - Get the number of times the function was called by355/// retrieving the entry block's count.356uint64_t GCOVFunction::getEntryCount() const {357return blocks.front()->getCount();358}359360GCOVBlock &GCOVFunction::getExitBlock() const {361return file.getVersion() < GCOV::V408 ? *blocks.back() : *blocks[1];362}363364// For each basic block, the sum of incoming edge counts equals the sum of365// outgoing edge counts by Kirchoff's circuit law. If the unmeasured arcs form a366// spanning tree, the count for each unmeasured arc (GCOV_ARC_ON_TREE) can be367// uniquely identified. Use an iterative algorithm to decrease stack usage for368// library users in threads. See the edge propagation algorithm in Optimally369// Profiling and Tracing Programs, ACM Transactions on Programming Languages and370// Systems, 1994.371void GCOVFunction::propagateCounts(const GCOVBlock &v, GCOVArc *pred) {372struct Elem {373const GCOVBlock &v;374GCOVArc *pred;375bool inDst;376size_t i = 0;377uint64_t excess = 0;378};379380SmallVector<Elem, 0> stack;381stack.push_back({v, pred, false});382for (;;) {383Elem &u = stack.back();384// If GCOV_ARC_ON_TREE edges do form a tree, visited is not needed;385// otherwise, this prevents infinite recursion for bad input.386if (u.i == 0 && !visited.insert(&u.v).second) {387stack.pop_back();388if (stack.empty())389break;390continue;391}392if (u.i < u.v.pred.size()) {393GCOVArc *e = u.v.pred[u.i++];394if (e != u.pred) {395if (e->onTree())396stack.push_back({e->src, e, /*inDst=*/false});397else398u.excess += e->count;399}400} else if (u.i < u.v.pred.size() + u.v.succ.size()) {401GCOVArc *e = u.v.succ[u.i++ - u.v.pred.size()];402if (e != u.pred) {403if (e->onTree())404stack.push_back({e->dst, e, /*inDst=*/true});405else406u.excess -= e->count;407}408} else {409uint64_t excess = u.excess;410if (static_cast<int64_t>(excess) < 0)411excess = -excess;412if (u.pred)413u.pred->count = excess;414bool inDst = u.inDst;415stack.pop_back();416if (stack.empty())417break;418stack.back().excess += inDst ? -excess : excess;419}420}421}422423void GCOVFunction::print(raw_ostream &OS) const {424OS << "===== " << Name << " (" << ident << ") @ " << getFilename() << ":"425<< startLine << "\n";426for (const auto &Block : blocks)427Block->print(OS);428}429430#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)431/// dump - Dump GCOVFunction content to dbgs() for debugging purposes.432LLVM_DUMP_METHOD void GCOVFunction::dump() const { print(dbgs()); }433#endif434435/// collectLineCounts - Collect line counts. This must be used after436/// reading .gcno and .gcda files.437438//===----------------------------------------------------------------------===//439// GCOVBlock implementation.440441void GCOVBlock::print(raw_ostream &OS) const {442OS << "Block : " << number << " Counter : " << count << "\n";443if (!pred.empty()) {444OS << "\tSource Edges : ";445for (const GCOVArc *Edge : pred)446OS << Edge->src.number << " (" << Edge->count << "), ";447OS << "\n";448}449if (!succ.empty()) {450OS << "\tDestination Edges : ";451for (const GCOVArc *Edge : succ) {452if (Edge->flags & GCOV_ARC_ON_TREE)453OS << '*';454OS << Edge->dst.number << " (" << Edge->count << "), ";455}456OS << "\n";457}458if (!lines.empty()) {459OS << "\tLines : ";460for (uint32_t N : lines)461OS << (N) << ",";462OS << "\n";463}464}465466#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)467/// dump - Dump GCOVBlock content to dbgs() for debugging purposes.468LLVM_DUMP_METHOD void GCOVBlock::dump() const { print(dbgs()); }469#endif470471uint64_t472GCOVBlock::augmentOneCycle(GCOVBlock *src,473std::vector<std::pair<GCOVBlock *, size_t>> &stack) {474GCOVBlock *u;475size_t i;476stack.clear();477stack.emplace_back(src, 0);478src->incoming = (GCOVArc *)1; // Mark u available for cycle detection479for (;;) {480std::tie(u, i) = stack.back();481if (i == u->succ.size()) {482u->traversable = false;483stack.pop_back();484if (stack.empty())485break;486continue;487}488++stack.back().second;489GCOVArc *succ = u->succ[i];490// Ignore saturated arcs (cycleCount has been reduced to 0) and visited491// blocks. Ignore self arcs to guard against bad input (.gcno has no492// self arcs).493if (succ->cycleCount == 0 || !succ->dst.traversable || &succ->dst == u)494continue;495if (succ->dst.incoming == nullptr) {496succ->dst.incoming = succ;497stack.emplace_back(&succ->dst, 0);498continue;499}500uint64_t minCount = succ->cycleCount;501for (GCOVBlock *v = u;;) {502minCount = std::min(minCount, v->incoming->cycleCount);503v = &v->incoming->src;504if (v == &succ->dst)505break;506}507succ->cycleCount -= minCount;508for (GCOVBlock *v = u;;) {509v->incoming->cycleCount -= minCount;510v = &v->incoming->src;511if (v == &succ->dst)512break;513}514return minCount;515}516return 0;517}518519// Get the total execution count of loops among blocks on the same line.520// Assuming a reducible flow graph, the count is the sum of back edge counts.521// Identifying loops is complex, so we simply find cycles and perform cycle522// cancelling iteratively.523uint64_t GCOVBlock::getCyclesCount(const BlockVector &blocks) {524std::vector<std::pair<GCOVBlock *, size_t>> stack;525uint64_t count = 0, d;526for (;;) {527// Make blocks on the line traversable and try finding a cycle.528for (const auto *b : blocks) {529const_cast<GCOVBlock *>(b)->traversable = true;530const_cast<GCOVBlock *>(b)->incoming = nullptr;531}532d = 0;533for (const auto *block : blocks) {534auto *b = const_cast<GCOVBlock *>(block);535if (b->traversable && (d = augmentOneCycle(b, stack)) > 0)536break;537}538if (d == 0)539break;540count += d;541}542// If there is no more loop, all traversable bits should have been cleared.543// This property is needed by subsequent calls.544for (const auto *b : blocks) {545assert(!b->traversable);546(void)b;547}548return count;549}550551//===----------------------------------------------------------------------===//552// FileInfo implementation.553554// Format dividend/divisor as a percentage. Return 1 if the result is greater555// than 0% and less than 1%.556static uint32_t formatPercentage(uint64_t dividend, uint64_t divisor) {557if (!dividend || !divisor)558return 0;559dividend *= 100;560return dividend < divisor ? 1 : dividend / divisor;561}562563// This custom division function mimics gcov's branch ouputs:564// - Round to closest whole number565// - Only output 0% or 100% if it's exactly that value566static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {567if (!Numerator)568return 0;569if (Numerator == Divisor)570return 100;571572uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;573if (Res == 0)574return 1;575if (Res == 100)576return 99;577return Res;578}579580namespace {581struct formatBranchInfo {582formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)583: Options(Options), Count(Count), Total(Total) {}584585void print(raw_ostream &OS) const {586if (!Total)587OS << "never executed";588else if (Options.BranchCount)589OS << "taken " << Count;590else591OS << "taken " << branchDiv(Count, Total) << "%";592}593594const GCOV::Options &Options;595uint64_t Count;596uint64_t Total;597};598599static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {600FBI.print(OS);601return OS;602}603604class LineConsumer {605std::unique_ptr<MemoryBuffer> Buffer;606StringRef Remaining;607608public:609LineConsumer() = default;610LineConsumer(StringRef Filename) {611// Open source files without requiring a NUL terminator. The concurrent612// modification may nullify the NUL terminator condition.613ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =614MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/false,615/*RequiresNullTerminator=*/false);616if (std::error_code EC = BufferOrErr.getError()) {617errs() << Filename << ": " << EC.message() << "\n";618Remaining = "";619} else {620Buffer = std::move(BufferOrErr.get());621Remaining = Buffer->getBuffer();622}623}624bool empty() { return Remaining.empty(); }625void printNext(raw_ostream &OS, uint32_t LineNum) {626StringRef Line;627if (empty())628Line = "/*EOF*/";629else630std::tie(Line, Remaining) = Remaining.split("\n");631OS << format("%5u:", LineNum) << Line << "\n";632}633};634} // end anonymous namespace635636/// Convert a path to a gcov filename. If PreservePaths is true, this637/// translates "/" to "#", ".." to "^", and drops ".", to match gcov.638static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {639if (!PreservePaths)640return sys::path::filename(Filename).str();641642// This behaviour is defined by gcov in terms of text replacements, so it's643// not likely to do anything useful on filesystems with different textual644// conventions.645llvm::SmallString<256> Result("");646StringRef::iterator I, S, E;647for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {648if (*I != '/')649continue;650651if (I - S == 1 && *S == '.') {652// ".", the current directory, is skipped.653} else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {654// "..", the parent directory, is replaced with "^".655Result.append("^#");656} else {657if (S < I)658// Leave other components intact,659Result.append(S, I);660// And separate with "#".661Result.push_back('#');662}663S = I + 1;664}665666if (S < I)667Result.append(S, I);668return std::string(Result);669}670671std::string Context::getCoveragePath(StringRef filename,672StringRef mainFilename) const {673if (options.NoOutput)674// This is probably a bug in gcov, but when -n is specified, paths aren't675// mangled at all, and the -l and -p options are ignored. Here, we do the676// same.677return std::string(filename);678679std::string CoveragePath;680if (options.LongFileNames && filename != mainFilename)681CoveragePath =682mangleCoveragePath(mainFilename, options.PreservePaths) + "##";683CoveragePath += mangleCoveragePath(filename, options.PreservePaths);684if (options.HashFilenames) {685MD5 Hasher;686MD5::MD5Result Result;687Hasher.update(filename.str());688Hasher.final(Result);689CoveragePath += "##" + std::string(Result.digest());690}691CoveragePath += ".gcov";692return CoveragePath;693}694695void Context::collectFunction(GCOVFunction &f, Summary &summary) {696SourceInfo &si = sources[f.srcIdx];697if (f.startLine >= si.startLineToFunctions.size())698si.startLineToFunctions.resize(f.startLine + 1);699si.startLineToFunctions[f.startLine].push_back(&f);700SmallSet<uint32_t, 16> lines;701SmallSet<uint32_t, 16> linesExec;702for (const GCOVBlock &b : f.blocksRange()) {703if (b.lines.empty())704continue;705uint32_t maxLineNum = *llvm::max_element(b.lines);706if (maxLineNum >= si.lines.size())707si.lines.resize(maxLineNum + 1);708for (uint32_t lineNum : b.lines) {709LineInfo &line = si.lines[lineNum];710if (lines.insert(lineNum).second)711++summary.lines;712if (b.count && linesExec.insert(lineNum).second)713++summary.linesExec;714line.exists = true;715line.count += b.count;716line.blocks.push_back(&b);717}718}719}720721void Context::collectSourceLine(SourceInfo &si, Summary *summary,722LineInfo &line, size_t lineNum) const {723uint64_t count = 0;724for (const GCOVBlock *b : line.blocks) {725if (b->number == 0) {726// For nonstandard control flows, arcs into the exit block may be727// duplicately counted (fork) or not be counted (abnormal exit), and thus728// the (exit,entry) counter may be inaccurate. Count the entry block with729// the outgoing arcs.730for (const GCOVArc *arc : b->succ)731count += arc->count;732} else {733// Add counts from predecessors that are not on the same line.734for (const GCOVArc *arc : b->pred)735if (!llvm::is_contained(line.blocks, &arc->src))736count += arc->count;737}738for (GCOVArc *arc : b->succ)739arc->cycleCount = arc->count;740}741742count += GCOVBlock::getCyclesCount(line.blocks);743line.count = count;744if (line.exists) {745++summary->lines;746if (line.count != 0)747++summary->linesExec;748}749750if (options.BranchInfo)751for (const GCOVBlock *b : line.blocks) {752if (b->getLastLine() != lineNum)753continue;754int branches = 0, execBranches = 0, takenBranches = 0;755for (const GCOVArc *arc : b->succ) {756++branches;757if (count != 0)758++execBranches;759if (arc->count != 0)760++takenBranches;761}762if (branches > 1) {763summary->branches += branches;764summary->branchesExec += execBranches;765summary->branchesTaken += takenBranches;766}767}768}769770void Context::collectSource(SourceInfo &si, Summary &summary) const {771size_t lineNum = 0;772for (LineInfo &line : si.lines) {773collectSourceLine(si, &summary, line, lineNum);774++lineNum;775}776}777778void Context::annotateSource(SourceInfo &si, const GCOVFile &file,779StringRef gcno, StringRef gcda,780raw_ostream &os) const {781auto source =782options.Intermediate ? LineConsumer() : LineConsumer(si.filename);783784os << " -: 0:Source:" << si.displayName << '\n';785os << " -: 0:Graph:" << gcno << '\n';786os << " -: 0:Data:" << gcda << '\n';787os << " -: 0:Runs:" << file.runCount << '\n';788if (file.version < GCOV::V900)789os << " -: 0:Programs:" << file.programCount << '\n';790791for (size_t lineNum = 1; !source.empty(); ++lineNum) {792if (lineNum >= si.lines.size()) {793os << " -:";794source.printNext(os, lineNum);795continue;796}797798const LineInfo &line = si.lines[lineNum];799if (options.BranchInfo && lineNum < si.startLineToFunctions.size())800for (const auto *f : si.startLineToFunctions[lineNum])801printFunctionDetails(*f, os);802if (!line.exists)803os << " -:";804else if (line.count == 0)805os << " #####:";806else807os << format("%9" PRIu64 ":", line.count);808source.printNext(os, lineNum);809810uint32_t blockIdx = 0, edgeIdx = 0;811for (const GCOVBlock *b : line.blocks) {812if (b->getLastLine() != lineNum)813continue;814if (options.AllBlocks) {815if (b->getCount() == 0)816os << " $$$$$:";817else818os << format("%9" PRIu64 ":", b->count);819os << format("%5u-block %2u\n", lineNum, blockIdx++);820}821if (options.BranchInfo) {822size_t NumEdges = b->succ.size();823if (NumEdges > 1)824printBranchInfo(*b, edgeIdx, os);825else if (options.UncondBranch && NumEdges == 1) {826uint64_t count = b->succ[0]->count;827os << format("unconditional %2u ", edgeIdx++)828<< formatBranchInfo(options, count, count) << '\n';829}830}831}832}833}834835void Context::printSourceToIntermediate(const SourceInfo &si,836raw_ostream &os) const {837os << "file:" << si.filename << '\n';838for (const auto &fs : si.startLineToFunctions)839for (const GCOVFunction *f : fs)840os << "function:" << f->startLine << ',' << f->getEntryCount() << ','841<< f->getName(options.Demangle) << '\n';842for (size_t lineNum = 1, size = si.lines.size(); lineNum < size; ++lineNum) {843const LineInfo &line = si.lines[lineNum];844if (line.blocks.empty())845continue;846// GCC 8 (r254259) added third third field for Ada:847// lcount:<line>,<count>,<has_unexecuted_blocks>848// We don't need the third field.849os << "lcount:" << lineNum << ',' << line.count << '\n';850851if (!options.BranchInfo)852continue;853for (const GCOVBlock *b : line.blocks) {854if (b->succ.size() < 2 || b->getLastLine() != lineNum)855continue;856for (const GCOVArc *arc : b->succ) {857const char *type =858b->getCount() ? arc->count ? "taken" : "nottaken" : "notexec";859os << "branch:" << lineNum << ',' << type << '\n';860}861}862}863}864865void Context::print(StringRef filename, StringRef gcno, StringRef gcda,866GCOVFile &file) {867for (StringRef filename : file.filenames) {868sources.emplace_back(filename);869SourceInfo &si = sources.back();870si.displayName = si.filename;871if (!options.SourcePrefix.empty() &&872sys::path::replace_path_prefix(si.displayName, options.SourcePrefix,873"") &&874!si.displayName.empty()) {875// TODO replace_path_prefix may strip the prefix even if the remaining876// part does not start with a separator.877if (sys::path::is_separator(si.displayName[0]))878si.displayName.erase(si.displayName.begin());879else880si.displayName = si.filename;881}882if (options.RelativeOnly && sys::path::is_absolute(si.displayName))883si.ignored = true;884}885886raw_ostream &os = llvm::outs();887for (GCOVFunction &f : make_pointee_range(file.functions)) {888Summary summary(f.getName(options.Demangle));889collectFunction(f, summary);890if (options.FuncCoverage && !options.UseStdout) {891os << "Function '" << summary.Name << "'\n";892printSummary(summary, os);893os << '\n';894}895}896897for (SourceInfo &si : sources) {898if (si.ignored)899continue;900Summary summary(si.displayName);901collectSource(si, summary);902903// Print file summary unless -t is specified.904std::string gcovName = getCoveragePath(si.filename, filename);905if (!options.UseStdout) {906os << "File '" << summary.Name << "'\n";907printSummary(summary, os);908if (!options.NoOutput && !options.Intermediate)909os << "Creating '" << gcovName << "'\n";910os << '\n';911}912913if (options.NoOutput || options.Intermediate)914continue;915std::optional<raw_fd_ostream> os;916if (!options.UseStdout) {917std::error_code ec;918os.emplace(gcovName, ec, sys::fs::OF_TextWithCRLF);919if (ec) {920errs() << ec.message() << '\n';921continue;922}923}924annotateSource(si, file, gcno, gcda,925options.UseStdout ? llvm::outs() : *os);926}927928if (options.Intermediate && !options.NoOutput) {929// gcov 7.* unexpectedly create multiple .gcov files, which was fixed in 8.0930// (PR GCC/82702). We create just one file.931std::string outputPath(sys::path::filename(filename));932std::error_code ec;933raw_fd_ostream os(outputPath + ".gcov", ec, sys::fs::OF_TextWithCRLF);934if (ec) {935errs() << ec.message() << '\n';936return;937}938939for (const SourceInfo &si : sources)940printSourceToIntermediate(si, os);941}942}943944void Context::printFunctionDetails(const GCOVFunction &f,945raw_ostream &os) const {946const uint64_t entryCount = f.getEntryCount();947uint32_t blocksExec = 0;948const GCOVBlock &exitBlock = f.getExitBlock();949uint64_t exitCount = 0;950for (const GCOVArc *arc : exitBlock.pred)951exitCount += arc->count;952for (const GCOVBlock &b : f.blocksRange())953if (b.number != 0 && &b != &exitBlock && b.getCount())954++blocksExec;955956os << "function " << f.getName(options.Demangle) << " called " << entryCount957<< " returned " << formatPercentage(exitCount, entryCount)958<< "% blocks executed "959<< formatPercentage(blocksExec, f.blocks.size() - 2) << "%\n";960}961962/// printBranchInfo - Print conditional branch probabilities.963void Context::printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,964raw_ostream &os) const {965uint64_t total = 0;966for (const GCOVArc *arc : Block.dsts())967total += arc->count;968for (const GCOVArc *arc : Block.dsts())969os << format("branch %2u ", edgeIdx++)970<< formatBranchInfo(options, arc->count, total) << '\n';971}972973void Context::printSummary(const Summary &summary, raw_ostream &os) const {974os << format("Lines executed:%.2f%% of %" PRIu64 "\n",975double(summary.linesExec) * 100 / summary.lines, summary.lines);976if (options.BranchInfo) {977if (summary.branches == 0) {978os << "No branches\n";979} else {980os << format("Branches executed:%.2f%% of %" PRIu64 "\n",981double(summary.branchesExec) * 100 / summary.branches,982summary.branches);983os << format("Taken at least once:%.2f%% of %" PRIu64 "\n",984double(summary.branchesTaken) * 100 / summary.branches,985summary.branches);986}987os << "No calls\n";988}989}990991void llvm::gcovOneInput(const GCOV::Options &options, StringRef filename,992StringRef gcno, StringRef gcda, GCOVFile &file) {993Context fi(options);994fi.print(filename, gcno, gcda, file);995}996997998