GCOV.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. //===- GCOV.cpp - LLVM coverage tool --------------------------------------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // GCOV implements the interface to read and write coverage files that use
  10. // 'gcov' format.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #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>
  27. using namespace llvm;
  28. enum : uint32_t {
  29. GCOV_ARC_ON_TREE = 1 << 0,
  30. GCOV_ARC_FALLTHROUGH = 1 << 2,
  31. GCOV_TAG_FUNCTION = 0x01000000,
  32. GCOV_TAG_BLOCKS = 0x01410000,
  33. GCOV_TAG_ARCS = 0x01430000,
  34. GCOV_TAG_LINES = 0x01450000,
  35. GCOV_TAG_COUNTER_ARCS = 0x01a10000,
  36. // GCOV_TAG_OBJECT_SUMMARY superseded GCOV_TAG_PROGRAM_SUMMARY in GCC 9.
  37. GCOV_TAG_OBJECT_SUMMARY = 0xa1000000,
  38. GCOV_TAG_PROGRAM_SUMMARY = 0xa3000000,
  39. };
  40. namespace {
  41. struct Summary {
  42. Summary(StringRef Name) : Name(Name) {}
  43. StringRef Name;
  44. uint64_t lines = 0;
  45. uint64_t linesExec = 0;
  46. uint64_t branches = 0;
  47. uint64_t branchesExec = 0;
  48. uint64_t branchesTaken = 0;
  49. };
  50. struct LineInfo {
  51. SmallVector<const GCOVBlock *, 1> blocks;
  52. uint64_t count = 0;
  53. bool exists = false;
  54. };
  55. struct SourceInfo {
  56. StringRef filename;
  57. SmallString<0> displayName;
  58. std::vector<std::vector<const GCOVFunction *>> startLineToFunctions;
  59. std::vector<LineInfo> lines;
  60. bool ignored = false;
  61. SourceInfo(StringRef filename) : filename(filename) {}
  62. };
  63. class Context {
  64. public:
  65. Context(const GCOV::Options &Options) : options(Options) {}
  66. void print(StringRef filename, StringRef gcno, StringRef gcda,
  67. GCOVFile &file);
  68. private:
  69. std::string getCoveragePath(StringRef filename, StringRef mainFilename) const;
  70. void printFunctionDetails(const GCOVFunction &f, raw_ostream &os) const;
  71. void printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,
  72. raw_ostream &OS) const;
  73. void printSummary(const Summary &summary, raw_ostream &os) const;
  74. void collectFunction(GCOVFunction &f, Summary &summary);
  75. void collectSourceLine(SourceInfo &si, Summary *summary, LineInfo &line,
  76. size_t lineNum) const;
  77. void collectSource(SourceInfo &si, Summary &summary) const;
  78. void annotateSource(SourceInfo &si, const GCOVFile &file, StringRef gcno,
  79. StringRef gcda, raw_ostream &os) const;
  80. void printSourceToIntermediate(const SourceInfo &si, raw_ostream &os) const;
  81. const GCOV::Options &options;
  82. std::vector<SourceInfo> sources;
  83. };
  84. } // namespace
  85. //===----------------------------------------------------------------------===//
  86. // GCOVFile implementation.
  87. /// readGCNO - Read GCNO buffer.
  88. bool GCOVFile::readGCNO(GCOVBuffer &buf) {
  89. if (!buf.readGCNOFormat())
  90. return false;
  91. if (!buf.readGCOVVersion(version))
  92. return false;
  93. checksum = buf.getWord();
  94. if (version >= GCOV::V900 && !buf.readString(cwd))
  95. return false;
  96. if (version >= GCOV::V800)
  97. buf.getWord(); // hasUnexecutedBlocks
  98. uint32_t tag, length;
  99. GCOVFunction *fn = nullptr;
  100. while ((tag = buf.getWord())) {
  101. if (!buf.readInt(length))
  102. return false;
  103. uint32_t pos = buf.cursor.tell();
  104. if (tag == GCOV_TAG_FUNCTION) {
  105. functions.push_back(std::make_unique<GCOVFunction>(*this));
  106. fn = functions.back().get();
  107. fn->ident = buf.getWord();
  108. fn->linenoChecksum = buf.getWord();
  109. if (version >= GCOV::V407)
  110. fn->cfgChecksum = buf.getWord();
  111. buf.readString(fn->Name);
  112. StringRef filename;
  113. if (version < GCOV::V800) {
  114. if (!buf.readString(filename))
  115. return false;
  116. fn->startLine = buf.getWord();
  117. } else {
  118. fn->artificial = buf.getWord();
  119. if (!buf.readString(filename))
  120. return false;
  121. fn->startLine = buf.getWord();
  122. fn->startColumn = buf.getWord();
  123. fn->endLine = buf.getWord();
  124. if (version >= GCOV::V900)
  125. fn->endColumn = buf.getWord();
  126. }
  127. auto r = filenameToIdx.try_emplace(filename, filenameToIdx.size());
  128. if (r.second)
  129. filenames.emplace_back(filename);
  130. fn->srcIdx = r.first->second;
  131. identToFunction[fn->ident] = fn;
  132. } else if (tag == GCOV_TAG_BLOCKS && fn) {
  133. if (version < GCOV::V800) {
  134. for (uint32_t i = 0; i != length; ++i) {
  135. buf.getWord(); // Ignored block flags
  136. fn->blocks.push_back(std::make_unique<GCOVBlock>(i));
  137. }
  138. } else {
  139. uint32_t num = buf.getWord();
  140. for (uint32_t i = 0; i != num; ++i)
  141. fn->blocks.push_back(std::make_unique<GCOVBlock>(i));
  142. }
  143. } else if (tag == GCOV_TAG_ARCS && fn) {
  144. uint32_t srcNo = buf.getWord();
  145. if (srcNo >= fn->blocks.size()) {
  146. errs() << "unexpected block number: " << srcNo << " (in "
  147. << fn->blocks.size() << ")\n";
  148. return false;
  149. }
  150. GCOVBlock *src = fn->blocks[srcNo].get();
  151. const uint32_t e =
  152. version >= GCOV::V1200 ? (length / 4 - 1) / 2 : (length - 1) / 2;
  153. for (uint32_t i = 0; i != e; ++i) {
  154. uint32_t dstNo = buf.getWord(), flags = buf.getWord();
  155. GCOVBlock *dst = fn->blocks[dstNo].get();
  156. auto arc = std::make_unique<GCOVArc>(*src, *dst, flags);
  157. src->addDstEdge(arc.get());
  158. dst->addSrcEdge(arc.get());
  159. if (arc->onTree())
  160. fn->treeArcs.push_back(std::move(arc));
  161. else
  162. fn->arcs.push_back(std::move(arc));
  163. }
  164. } else if (tag == GCOV_TAG_LINES && fn) {
  165. uint32_t srcNo = buf.getWord();
  166. if (srcNo >= fn->blocks.size()) {
  167. errs() << "unexpected block number: " << srcNo << " (in "
  168. << fn->blocks.size() << ")\n";
  169. return false;
  170. }
  171. GCOVBlock &Block = *fn->blocks[srcNo];
  172. for (;;) {
  173. uint32_t line = buf.getWord();
  174. if (line)
  175. Block.addLine(line);
  176. else {
  177. StringRef filename;
  178. buf.readString(filename);
  179. if (filename.empty())
  180. break;
  181. // TODO Unhandled
  182. }
  183. }
  184. }
  185. pos += version >= GCOV::V1200 ? length : 4 * length;
  186. if (pos < buf.cursor.tell())
  187. return false;
  188. buf.de.skip(buf.cursor, pos - buf.cursor.tell());
  189. }
  190. GCNOInitialized = true;
  191. return true;
  192. }
  193. /// readGCDA - Read GCDA buffer. It is required that readGCDA() can only be
  194. /// called after readGCNO().
  195. bool GCOVFile::readGCDA(GCOVBuffer &buf) {
  196. assert(GCNOInitialized && "readGCDA() can only be called after readGCNO()");
  197. if (!buf.readGCDAFormat())
  198. return false;
  199. GCOV::GCOVVersion GCDAVersion;
  200. if (!buf.readGCOVVersion(GCDAVersion))
  201. return false;
  202. if (version != GCDAVersion) {
  203. errs() << "GCOV versions do not match.\n";
  204. return false;
  205. }
  206. uint32_t GCDAChecksum;
  207. if (!buf.readInt(GCDAChecksum))
  208. return false;
  209. if (checksum != GCDAChecksum) {
  210. errs() << "file checksums do not match: " << checksum
  211. << " != " << GCDAChecksum << "\n";
  212. return false;
  213. }
  214. uint32_t dummy, tag, length;
  215. uint32_t ident;
  216. GCOVFunction *fn = nullptr;
  217. while ((tag = buf.getWord())) {
  218. if (!buf.readInt(length))
  219. return false;
  220. uint32_t pos = buf.cursor.tell();
  221. if (tag == GCOV_TAG_OBJECT_SUMMARY) {
  222. buf.readInt(runCount);
  223. buf.readInt(dummy);
  224. // clang<11 uses a fake 4.2 format which sets length to 9.
  225. if (length == 9)
  226. buf.readInt(runCount);
  227. } else if (tag == GCOV_TAG_PROGRAM_SUMMARY) {
  228. // clang<11 uses a fake 4.2 format which sets length to 0.
  229. if (length > 0) {
  230. buf.readInt(dummy);
  231. buf.readInt(dummy);
  232. buf.readInt(runCount);
  233. }
  234. ++programCount;
  235. } else if (tag == GCOV_TAG_FUNCTION) {
  236. if (length == 0) // Placeholder
  237. continue;
  238. // As of GCC 10, GCOV_TAG_FUNCTION_LENGTH has never been larger than 3.
  239. // However, clang<11 uses a fake 4.2 format which may set length larger
  240. // than 3.
  241. if (length < 2 || !buf.readInt(ident))
  242. return false;
  243. auto It = identToFunction.find(ident);
  244. uint32_t linenoChecksum, cfgChecksum = 0;
  245. buf.readInt(linenoChecksum);
  246. if (version >= GCOV::V407)
  247. buf.readInt(cfgChecksum);
  248. if (It != identToFunction.end()) {
  249. fn = It->second;
  250. if (linenoChecksum != fn->linenoChecksum ||
  251. cfgChecksum != fn->cfgChecksum) {
  252. errs() << fn->Name
  253. << format(": checksum mismatch, (%u, %u) != (%u, %u)\n",
  254. linenoChecksum, cfgChecksum, fn->linenoChecksum,
  255. fn->cfgChecksum);
  256. return false;
  257. }
  258. }
  259. } else if (tag == GCOV_TAG_COUNTER_ARCS && fn) {
  260. uint32_t expected = 2 * fn->arcs.size();
  261. if (version >= GCOV::V1200)
  262. expected *= 4;
  263. if (length != expected) {
  264. errs() << fn->Name
  265. << format(
  266. ": GCOV_TAG_COUNTER_ARCS mismatch, got %u, expected %u\n",
  267. length, expected);
  268. return false;
  269. }
  270. for (std::unique_ptr<GCOVArc> &arc : fn->arcs) {
  271. if (!buf.readInt64(arc->count))
  272. return false;
  273. arc->src.count += arc->count;
  274. }
  275. if (fn->blocks.size() >= 2) {
  276. GCOVBlock &src = *fn->blocks[0];
  277. GCOVBlock &sink =
  278. version < GCOV::V408 ? *fn->blocks.back() : *fn->blocks[1];
  279. auto arc = std::make_unique<GCOVArc>(sink, src, GCOV_ARC_ON_TREE);
  280. sink.addDstEdge(arc.get());
  281. src.addSrcEdge(arc.get());
  282. fn->treeArcs.push_back(std::move(arc));
  283. for (GCOVBlock &block : fn->blocksRange())
  284. fn->propagateCounts(block, nullptr);
  285. for (size_t i = fn->treeArcs.size() - 1; i; --i)
  286. fn->treeArcs[i - 1]->src.count += fn->treeArcs[i - 1]->count;
  287. }
  288. }
  289. pos += version >= GCOV::V1200 ? length : 4 * length;
  290. if (pos < buf.cursor.tell())
  291. return false;
  292. buf.de.skip(buf.cursor, pos - buf.cursor.tell());
  293. }
  294. return true;
  295. }
  296. void GCOVFile::print(raw_ostream &OS) const {
  297. for (const GCOVFunction &f : *this)
  298. f.print(OS);
  299. }
  300. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  301. /// dump - Dump GCOVFile content to dbgs() for debugging purposes.
  302. LLVM_DUMP_METHOD void GCOVFile::dump() const { print(dbgs()); }
  303. #endif
  304. bool GCOVArc::onTree() const { return flags & GCOV_ARC_ON_TREE; }
  305. //===----------------------------------------------------------------------===//
  306. // GCOVFunction implementation.
  307. StringRef GCOVFunction::getName(bool demangle) const {
  308. if (!demangle)
  309. return Name;
  310. if (demangled.empty()) {
  311. do {
  312. if (Name.startswith("_Z")) {
  313. int status = 0;
  314. // Name is guaranteed to be NUL-terminated.
  315. char *res = itaniumDemangle(Name.data(), nullptr, nullptr, &status);
  316. if (status == 0) {
  317. demangled = res;
  318. free(res);
  319. break;
  320. }
  321. }
  322. demangled = Name;
  323. } while (false);
  324. }
  325. return demangled;
  326. }
  327. StringRef GCOVFunction::getFilename() const { return file.filenames[srcIdx]; }
  328. /// getEntryCount - Get the number of times the function was called by
  329. /// retrieving the entry block's count.
  330. uint64_t GCOVFunction::getEntryCount() const {
  331. return blocks.front()->getCount();
  332. }
  333. GCOVBlock &GCOVFunction::getExitBlock() const {
  334. return file.getVersion() < GCOV::V408 ? *blocks.back() : *blocks[1];
  335. }
  336. // For each basic block, the sum of incoming edge counts equals the sum of
  337. // outgoing edge counts by Kirchoff's circuit law. If the unmeasured arcs form a
  338. // spanning tree, the count for each unmeasured arc (GCOV_ARC_ON_TREE) can be
  339. // uniquely identified.
  340. uint64_t GCOVFunction::propagateCounts(const GCOVBlock &v, GCOVArc *pred) {
  341. // If GCOV_ARC_ON_TREE edges do form a tree, visited is not needed; otherwise
  342. // this prevents infinite recursion.
  343. if (!visited.insert(&v).second)
  344. return 0;
  345. uint64_t excess = 0;
  346. for (GCOVArc *e : v.srcs())
  347. if (e != pred)
  348. excess += e->onTree() ? propagateCounts(e->src, e) : e->count;
  349. for (GCOVArc *e : v.dsts())
  350. if (e != pred)
  351. excess -= e->onTree() ? propagateCounts(e->dst, e) : e->count;
  352. if (int64_t(excess) < 0)
  353. excess = -excess;
  354. if (pred)
  355. pred->count = excess;
  356. return excess;
  357. }
  358. void GCOVFunction::print(raw_ostream &OS) const {
  359. OS << "===== " << Name << " (" << ident << ") @ " << getFilename() << ":"
  360. << startLine << "\n";
  361. for (const auto &Block : blocks)
  362. Block->print(OS);
  363. }
  364. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  365. /// dump - Dump GCOVFunction content to dbgs() for debugging purposes.
  366. LLVM_DUMP_METHOD void GCOVFunction::dump() const { print(dbgs()); }
  367. #endif
  368. /// collectLineCounts - Collect line counts. This must be used after
  369. /// reading .gcno and .gcda files.
  370. //===----------------------------------------------------------------------===//
  371. // GCOVBlock implementation.
  372. void GCOVBlock::print(raw_ostream &OS) const {
  373. OS << "Block : " << number << " Counter : " << count << "\n";
  374. if (!pred.empty()) {
  375. OS << "\tSource Edges : ";
  376. for (const GCOVArc *Edge : pred)
  377. OS << Edge->src.number << " (" << Edge->count << "), ";
  378. OS << "\n";
  379. }
  380. if (!succ.empty()) {
  381. OS << "\tDestination Edges : ";
  382. for (const GCOVArc *Edge : succ) {
  383. if (Edge->flags & GCOV_ARC_ON_TREE)
  384. OS << '*';
  385. OS << Edge->dst.number << " (" << Edge->count << "), ";
  386. }
  387. OS << "\n";
  388. }
  389. if (!lines.empty()) {
  390. OS << "\tLines : ";
  391. for (uint32_t N : lines)
  392. OS << (N) << ",";
  393. OS << "\n";
  394. }
  395. }
  396. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  397. /// dump - Dump GCOVBlock content to dbgs() for debugging purposes.
  398. LLVM_DUMP_METHOD void GCOVBlock::dump() const { print(dbgs()); }
  399. #endif
  400. uint64_t
  401. GCOVBlock::augmentOneCycle(GCOVBlock *src,
  402. std::vector<std::pair<GCOVBlock *, size_t>> &stack) {
  403. GCOVBlock *u;
  404. size_t i;
  405. stack.clear();
  406. stack.emplace_back(src, 0);
  407. src->incoming = (GCOVArc *)1; // Mark u available for cycle detection
  408. for (;;) {
  409. std::tie(u, i) = stack.back();
  410. if (i == u->succ.size()) {
  411. u->traversable = false;
  412. stack.pop_back();
  413. if (stack.empty())
  414. break;
  415. continue;
  416. }
  417. ++stack.back().second;
  418. GCOVArc *succ = u->succ[i];
  419. // Ignore saturated arcs (cycleCount has been reduced to 0) and visited
  420. // blocks. Ignore self arcs to guard against bad input (.gcno has no
  421. // self arcs).
  422. if (succ->cycleCount == 0 || !succ->dst.traversable || &succ->dst == u)
  423. continue;
  424. if (succ->dst.incoming == nullptr) {
  425. succ->dst.incoming = succ;
  426. stack.emplace_back(&succ->dst, 0);
  427. continue;
  428. }
  429. uint64_t minCount = succ->cycleCount;
  430. for (GCOVBlock *v = u;;) {
  431. minCount = std::min(minCount, v->incoming->cycleCount);
  432. v = &v->incoming->src;
  433. if (v == &succ->dst)
  434. break;
  435. }
  436. succ->cycleCount -= minCount;
  437. for (GCOVBlock *v = u;;) {
  438. v->incoming->cycleCount -= minCount;
  439. v = &v->incoming->src;
  440. if (v == &succ->dst)
  441. break;
  442. }
  443. return minCount;
  444. }
  445. return 0;
  446. }
  447. // Get the total execution count of loops among blocks on the same line.
  448. // Assuming a reducible flow graph, the count is the sum of back edge counts.
  449. // Identifying loops is complex, so we simply find cycles and perform cycle
  450. // cancelling iteratively.
  451. uint64_t GCOVBlock::getCyclesCount(const BlockVector &blocks) {
  452. std::vector<std::pair<GCOVBlock *, size_t>> stack;
  453. uint64_t count = 0, d;
  454. for (;;) {
  455. // Make blocks on the line traversable and try finding a cycle.
  456. for (const auto *b : blocks) {
  457. const_cast<GCOVBlock *>(b)->traversable = true;
  458. const_cast<GCOVBlock *>(b)->incoming = nullptr;
  459. }
  460. d = 0;
  461. for (const auto *block : blocks) {
  462. auto *b = const_cast<GCOVBlock *>(block);
  463. if (b->traversable && (d = augmentOneCycle(b, stack)) > 0)
  464. break;
  465. }
  466. if (d == 0)
  467. break;
  468. count += d;
  469. }
  470. // If there is no more loop, all traversable bits should have been cleared.
  471. // This property is needed by subsequent calls.
  472. for (const auto *b : blocks) {
  473. assert(!b->traversable);
  474. (void)b;
  475. }
  476. return count;
  477. }
  478. //===----------------------------------------------------------------------===//
  479. // FileInfo implementation.
  480. // Format dividend/divisor as a percentage. Return 1 if the result is greater
  481. // than 0% and less than 1%.
  482. static uint32_t formatPercentage(uint64_t dividend, uint64_t divisor) {
  483. if (!dividend || !divisor)
  484. return 0;
  485. dividend *= 100;
  486. return dividend < divisor ? 1 : dividend / divisor;
  487. }
  488. // This custom division function mimics gcov's branch ouputs:
  489. // - Round to closest whole number
  490. // - Only output 0% or 100% if it's exactly that value
  491. static uint32_t branchDiv(uint64_t Numerator, uint64_t Divisor) {
  492. if (!Numerator)
  493. return 0;
  494. if (Numerator == Divisor)
  495. return 100;
  496. uint8_t Res = (Numerator * 100 + Divisor / 2) / Divisor;
  497. if (Res == 0)
  498. return 1;
  499. if (Res == 100)
  500. return 99;
  501. return Res;
  502. }
  503. namespace {
  504. struct formatBranchInfo {
  505. formatBranchInfo(const GCOV::Options &Options, uint64_t Count, uint64_t Total)
  506. : Options(Options), Count(Count), Total(Total) {}
  507. void print(raw_ostream &OS) const {
  508. if (!Total)
  509. OS << "never executed";
  510. else if (Options.BranchCount)
  511. OS << "taken " << Count;
  512. else
  513. OS << "taken " << branchDiv(Count, Total) << "%";
  514. }
  515. const GCOV::Options &Options;
  516. uint64_t Count;
  517. uint64_t Total;
  518. };
  519. static raw_ostream &operator<<(raw_ostream &OS, const formatBranchInfo &FBI) {
  520. FBI.print(OS);
  521. return OS;
  522. }
  523. class LineConsumer {
  524. std::unique_ptr<MemoryBuffer> Buffer;
  525. StringRef Remaining;
  526. public:
  527. LineConsumer() = default;
  528. LineConsumer(StringRef Filename) {
  529. // Open source files without requiring a NUL terminator. The concurrent
  530. // modification may nullify the NUL terminator condition.
  531. ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
  532. MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/false,
  533. /*RequiresNullTerminator=*/false);
  534. if (std::error_code EC = BufferOrErr.getError()) {
  535. errs() << Filename << ": " << EC.message() << "\n";
  536. Remaining = "";
  537. } else {
  538. Buffer = std::move(BufferOrErr.get());
  539. Remaining = Buffer->getBuffer();
  540. }
  541. }
  542. bool empty() { return Remaining.empty(); }
  543. void printNext(raw_ostream &OS, uint32_t LineNum) {
  544. StringRef Line;
  545. if (empty())
  546. Line = "/*EOF*/";
  547. else
  548. std::tie(Line, Remaining) = Remaining.split("\n");
  549. OS << format("%5u:", LineNum) << Line << "\n";
  550. }
  551. };
  552. } // end anonymous namespace
  553. /// Convert a path to a gcov filename. If PreservePaths is true, this
  554. /// translates "/" to "#", ".." to "^", and drops ".", to match gcov.
  555. static std::string mangleCoveragePath(StringRef Filename, bool PreservePaths) {
  556. if (!PreservePaths)
  557. return sys::path::filename(Filename).str();
  558. // This behaviour is defined by gcov in terms of text replacements, so it's
  559. // not likely to do anything useful on filesystems with different textual
  560. // conventions.
  561. llvm::SmallString<256> Result("");
  562. StringRef::iterator I, S, E;
  563. for (I = S = Filename.begin(), E = Filename.end(); I != E; ++I) {
  564. if (*I != '/')
  565. continue;
  566. if (I - S == 1 && *S == '.') {
  567. // ".", the current directory, is skipped.
  568. } else if (I - S == 2 && *S == '.' && *(S + 1) == '.') {
  569. // "..", the parent directory, is replaced with "^".
  570. Result.append("^#");
  571. } else {
  572. if (S < I)
  573. // Leave other components intact,
  574. Result.append(S, I);
  575. // And separate with "#".
  576. Result.push_back('#');
  577. }
  578. S = I + 1;
  579. }
  580. if (S < I)
  581. Result.append(S, I);
  582. return std::string(Result.str());
  583. }
  584. std::string Context::getCoveragePath(StringRef filename,
  585. StringRef mainFilename) const {
  586. if (options.NoOutput)
  587. // This is probably a bug in gcov, but when -n is specified, paths aren't
  588. // mangled at all, and the -l and -p options are ignored. Here, we do the
  589. // same.
  590. return std::string(filename);
  591. std::string CoveragePath;
  592. if (options.LongFileNames && !filename.equals(mainFilename))
  593. CoveragePath =
  594. mangleCoveragePath(mainFilename, options.PreservePaths) + "##";
  595. CoveragePath += mangleCoveragePath(filename, options.PreservePaths);
  596. if (options.HashFilenames) {
  597. MD5 Hasher;
  598. MD5::MD5Result Result;
  599. Hasher.update(filename.str());
  600. Hasher.final(Result);
  601. CoveragePath += "##" + std::string(Result.digest());
  602. }
  603. CoveragePath += ".gcov";
  604. return CoveragePath;
  605. }
  606. void Context::collectFunction(GCOVFunction &f, Summary &summary) {
  607. SourceInfo &si = sources[f.srcIdx];
  608. if (f.startLine >= si.startLineToFunctions.size())
  609. si.startLineToFunctions.resize(f.startLine + 1);
  610. si.startLineToFunctions[f.startLine].push_back(&f);
  611. SmallSet<uint32_t, 16> lines;
  612. SmallSet<uint32_t, 16> linesExec;
  613. for (const GCOVBlock &b : f.blocksRange()) {
  614. if (b.lines.empty())
  615. continue;
  616. uint32_t maxLineNum = *std::max_element(b.lines.begin(), b.lines.end());
  617. if (maxLineNum >= si.lines.size())
  618. si.lines.resize(maxLineNum + 1);
  619. for (uint32_t lineNum : b.lines) {
  620. LineInfo &line = si.lines[lineNum];
  621. if (lines.insert(lineNum).second)
  622. ++summary.lines;
  623. if (b.count && linesExec.insert(lineNum).second)
  624. ++summary.linesExec;
  625. line.exists = true;
  626. line.count += b.count;
  627. line.blocks.push_back(&b);
  628. }
  629. }
  630. }
  631. void Context::collectSourceLine(SourceInfo &si, Summary *summary,
  632. LineInfo &line, size_t lineNum) const {
  633. uint64_t count = 0;
  634. for (const GCOVBlock *b : line.blocks) {
  635. if (b->number == 0) {
  636. // For nonstandard control flows, arcs into the exit block may be
  637. // duplicately counted (fork) or not be counted (abnormal exit), and thus
  638. // the (exit,entry) counter may be inaccurate. Count the entry block with
  639. // the outgoing arcs.
  640. for (const GCOVArc *arc : b->succ)
  641. count += arc->count;
  642. } else {
  643. // Add counts from predecessors that are not on the same line.
  644. for (const GCOVArc *arc : b->pred)
  645. if (!llvm::is_contained(line.blocks, &arc->src))
  646. count += arc->count;
  647. }
  648. for (GCOVArc *arc : b->succ)
  649. arc->cycleCount = arc->count;
  650. }
  651. count += GCOVBlock::getCyclesCount(line.blocks);
  652. line.count = count;
  653. if (line.exists) {
  654. ++summary->lines;
  655. if (line.count != 0)
  656. ++summary->linesExec;
  657. }
  658. if (options.BranchInfo)
  659. for (const GCOVBlock *b : line.blocks) {
  660. if (b->getLastLine() != lineNum)
  661. continue;
  662. int branches = 0, execBranches = 0, takenBranches = 0;
  663. for (const GCOVArc *arc : b->succ) {
  664. ++branches;
  665. if (count != 0)
  666. ++execBranches;
  667. if (arc->count != 0)
  668. ++takenBranches;
  669. }
  670. if (branches > 1) {
  671. summary->branches += branches;
  672. summary->branchesExec += execBranches;
  673. summary->branchesTaken += takenBranches;
  674. }
  675. }
  676. }
  677. void Context::collectSource(SourceInfo &si, Summary &summary) const {
  678. size_t lineNum = 0;
  679. for (LineInfo &line : si.lines) {
  680. collectSourceLine(si, &summary, line, lineNum);
  681. ++lineNum;
  682. }
  683. }
  684. void Context::annotateSource(SourceInfo &si, const GCOVFile &file,
  685. StringRef gcno, StringRef gcda,
  686. raw_ostream &os) const {
  687. auto source =
  688. options.Intermediate ? LineConsumer() : LineConsumer(si.filename);
  689. os << " -: 0:Source:" << si.displayName << '\n';
  690. os << " -: 0:Graph:" << gcno << '\n';
  691. os << " -: 0:Data:" << gcda << '\n';
  692. os << " -: 0:Runs:" << file.runCount << '\n';
  693. if (file.version < GCOV::V900)
  694. os << " -: 0:Programs:" << file.programCount << '\n';
  695. for (size_t lineNum = 1; !source.empty(); ++lineNum) {
  696. if (lineNum >= si.lines.size()) {
  697. os << " -:";
  698. source.printNext(os, lineNum);
  699. continue;
  700. }
  701. const LineInfo &line = si.lines[lineNum];
  702. if (options.BranchInfo && lineNum < si.startLineToFunctions.size())
  703. for (const auto *f : si.startLineToFunctions[lineNum])
  704. printFunctionDetails(*f, os);
  705. if (!line.exists)
  706. os << " -:";
  707. else if (line.count == 0)
  708. os << " #####:";
  709. else
  710. os << format("%9" PRIu64 ":", line.count);
  711. source.printNext(os, lineNum);
  712. uint32_t blockIdx = 0, edgeIdx = 0;
  713. for (const GCOVBlock *b : line.blocks) {
  714. if (b->getLastLine() != lineNum)
  715. continue;
  716. if (options.AllBlocks) {
  717. if (b->getCount() == 0)
  718. os << " $$$$$:";
  719. else
  720. os << format("%9" PRIu64 ":", b->count);
  721. os << format("%5u-block %2u\n", lineNum, blockIdx++);
  722. }
  723. if (options.BranchInfo) {
  724. size_t NumEdges = b->succ.size();
  725. if (NumEdges > 1)
  726. printBranchInfo(*b, edgeIdx, os);
  727. else if (options.UncondBranch && NumEdges == 1) {
  728. uint64_t count = b->succ[0]->count;
  729. os << format("unconditional %2u ", edgeIdx++)
  730. << formatBranchInfo(options, count, count) << '\n';
  731. }
  732. }
  733. }
  734. }
  735. }
  736. void Context::printSourceToIntermediate(const SourceInfo &si,
  737. raw_ostream &os) const {
  738. os << "file:" << si.filename << '\n';
  739. for (const auto &fs : si.startLineToFunctions)
  740. for (const GCOVFunction *f : fs)
  741. os << "function:" << f->startLine << ',' << f->getEntryCount() << ','
  742. << f->getName(options.Demangle) << '\n';
  743. for (size_t lineNum = 1, size = si.lines.size(); lineNum < size; ++lineNum) {
  744. const LineInfo &line = si.lines[lineNum];
  745. if (line.blocks.empty())
  746. continue;
  747. // GCC 8 (r254259) added third third field for Ada:
  748. // lcount:<line>,<count>,<has_unexecuted_blocks>
  749. // We don't need the third field.
  750. os << "lcount:" << lineNum << ',' << line.count << '\n';
  751. if (!options.BranchInfo)
  752. continue;
  753. for (const GCOVBlock *b : line.blocks) {
  754. if (b->succ.size() < 2 || b->getLastLine() != lineNum)
  755. continue;
  756. for (const GCOVArc *arc : b->succ) {
  757. const char *type =
  758. b->getCount() ? arc->count ? "taken" : "nottaken" : "notexec";
  759. os << "branch:" << lineNum << ',' << type << '\n';
  760. }
  761. }
  762. }
  763. }
  764. void Context::print(StringRef filename, StringRef gcno, StringRef gcda,
  765. GCOVFile &file) {
  766. for (StringRef filename : file.filenames) {
  767. sources.emplace_back(filename);
  768. SourceInfo &si = sources.back();
  769. si.displayName = si.filename;
  770. if (!options.SourcePrefix.empty() &&
  771. sys::path::replace_path_prefix(si.displayName, options.SourcePrefix,
  772. "") &&
  773. !si.displayName.empty()) {
  774. // TODO replace_path_prefix may strip the prefix even if the remaining
  775. // part does not start with a separator.
  776. if (sys::path::is_separator(si.displayName[0]))
  777. si.displayName.erase(si.displayName.begin());
  778. else
  779. si.displayName = si.filename;
  780. }
  781. if (options.RelativeOnly && sys::path::is_absolute(si.displayName))
  782. si.ignored = true;
  783. }
  784. raw_ostream &os = llvm::outs();
  785. for (GCOVFunction &f : make_pointee_range(file.functions)) {
  786. Summary summary(f.getName(options.Demangle));
  787. collectFunction(f, summary);
  788. if (options.FuncCoverage && !options.UseStdout) {
  789. os << "Function '" << summary.Name << "'\n";
  790. printSummary(summary, os);
  791. os << '\n';
  792. }
  793. }
  794. for (SourceInfo &si : sources) {
  795. if (si.ignored)
  796. continue;
  797. Summary summary(si.displayName);
  798. collectSource(si, summary);
  799. // Print file summary unless -t is specified.
  800. std::string gcovName = getCoveragePath(si.filename, filename);
  801. if (!options.UseStdout) {
  802. os << "File '" << summary.Name << "'\n";
  803. printSummary(summary, os);
  804. if (!options.NoOutput && !options.Intermediate)
  805. os << "Creating '" << gcovName << "'\n";
  806. os << '\n';
  807. }
  808. if (options.NoOutput || options.Intermediate)
  809. continue;
  810. std::optional<raw_fd_ostream> os;
  811. if (!options.UseStdout) {
  812. std::error_code ec;
  813. os.emplace(gcovName, ec, sys::fs::OF_TextWithCRLF);
  814. if (ec) {
  815. errs() << ec.message() << '\n';
  816. continue;
  817. }
  818. }
  819. annotateSource(si, file, gcno, gcda,
  820. options.UseStdout ? llvm::outs() : *os);
  821. }
  822. if (options.Intermediate && !options.NoOutput) {
  823. // gcov 7.* unexpectedly create multiple .gcov files, which was fixed in 8.0
  824. // (PR GCC/82702). We create just one file.
  825. std::string outputPath(sys::path::filename(filename));
  826. std::error_code ec;
  827. raw_fd_ostream os(outputPath + ".gcov", ec, sys::fs::OF_TextWithCRLF);
  828. if (ec) {
  829. errs() << ec.message() << '\n';
  830. return;
  831. }
  832. for (const SourceInfo &si : sources)
  833. printSourceToIntermediate(si, os);
  834. }
  835. }
  836. void Context::printFunctionDetails(const GCOVFunction &f,
  837. raw_ostream &os) const {
  838. const uint64_t entryCount = f.getEntryCount();
  839. uint32_t blocksExec = 0;
  840. const GCOVBlock &exitBlock = f.getExitBlock();
  841. uint64_t exitCount = 0;
  842. for (const GCOVArc *arc : exitBlock.pred)
  843. exitCount += arc->count;
  844. for (const GCOVBlock &b : f.blocksRange())
  845. if (b.number != 0 && &b != &exitBlock && b.getCount())
  846. ++blocksExec;
  847. os << "function " << f.getName(options.Demangle) << " called " << entryCount
  848. << " returned " << formatPercentage(exitCount, entryCount)
  849. << "% blocks executed "
  850. << formatPercentage(blocksExec, f.blocks.size() - 2) << "%\n";
  851. }
  852. /// printBranchInfo - Print conditional branch probabilities.
  853. void Context::printBranchInfo(const GCOVBlock &Block, uint32_t &edgeIdx,
  854. raw_ostream &os) const {
  855. uint64_t total = 0;
  856. for (const GCOVArc *arc : Block.dsts())
  857. total += arc->count;
  858. for (const GCOVArc *arc : Block.dsts())
  859. os << format("branch %2u ", edgeIdx++)
  860. << formatBranchInfo(options, arc->count, total) << '\n';
  861. }
  862. void Context::printSummary(const Summary &summary, raw_ostream &os) const {
  863. os << format("Lines executed:%.2f%% of %" PRIu64 "\n",
  864. double(summary.linesExec) * 100 / summary.lines, summary.lines);
  865. if (options.BranchInfo) {
  866. if (summary.branches == 0) {
  867. os << "No branches\n";
  868. } else {
  869. os << format("Branches executed:%.2f%% of %" PRIu64 "\n",
  870. double(summary.branchesExec) * 100 / summary.branches,
  871. summary.branches);
  872. os << format("Taken at least once:%.2f%% of %" PRIu64 "\n",
  873. double(summary.branchesTaken) * 100 / summary.branches,
  874. summary.branches);
  875. }
  876. os << "No calls\n";
  877. }
  878. }
  879. void llvm::gcovOneInput(const GCOV::Options &options, StringRef filename,
  880. StringRef gcno, StringRef gcda, GCOVFile &file) {
  881. Context fi(options);
  882. fi.print(filename, gcno, gcda, file);
  883. }