Bug Summary

File:llvm/lib/ProfileData/GCOV.cpp
Warning:line 174, column 41
Branch condition evaluates to a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name GCOV.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-12/lib/clang/12.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/lib/ProfileData -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/lib/ProfileData -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/include -I /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-12/lib/clang/12.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/build-llvm/lib/ProfileData -fdebug-prefix-map=/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-09-17-195756-12974-1 -x c++ /build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/lib/ProfileData/GCOV.cpp

/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/lib/ProfileData/GCOV.cpp

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

/build/llvm-toolchain-snapshot-12~++20200917111122+b03c2b8395b/llvm/include/llvm/ProfileData/GCOV.h

1//===- GCOV.h - LLVM coverage tool ------------------------------*- C++ -*-===//
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// This header provides the interface to read and write coverage files that
10// use 'gcov' format.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_PROFILEDATA_GCOV_H
15#define LLVM_PROFILEDATA_GCOV_H
16
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/MapVector.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/ADT/iterator.h"
24#include "llvm/ADT/iterator_range.h"
25#include "llvm/Support/DataExtractor.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Support/raw_ostream.h"
28#include <algorithm>
29#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <limits>
33#include <map>
34#include <memory>
35#include <string>
36#include <utility>
37
38namespace llvm {
39
40class GCOVFunction;
41class GCOVBlock;
42
43namespace GCOV {
44
45enum GCOVVersion { V304, V407, V408, V800, V900 };
46
47/// A struct for passing gcov options between functions.
48struct Options {
49 Options(bool A, bool B, bool C, bool F, bool P, bool U, bool I, bool L,
50 bool M, bool N, bool R, bool T, bool X, std::string SourcePrefix)
51 : AllBlocks(A), BranchInfo(B), BranchCount(C), FuncCoverage(F),
52 PreservePaths(P), UncondBranch(U), Intermediate(I), LongFileNames(L),
53 Demangle(M), NoOutput(N), RelativeOnly(R), UseStdout(T),
54 HashFilenames(X), SourcePrefix(std::move(SourcePrefix)) {}
55
56 bool AllBlocks;
57 bool BranchInfo;
58 bool BranchCount;
59 bool FuncCoverage;
60 bool PreservePaths;
61 bool UncondBranch;
62 bool Intermediate;
63 bool LongFileNames;
64 bool Demangle;
65 bool NoOutput;
66 bool RelativeOnly;
67 bool UseStdout;
68 bool HashFilenames;
69 std::string SourcePrefix;
70};
71
72} // end namespace GCOV
73
74/// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
75/// read operations.
76class GCOVBuffer {
77public:
78 GCOVBuffer(MemoryBuffer *B) : Buffer(B) {}
79 ~GCOVBuffer() { consumeError(cursor.takeError()); }
80
81 /// readGCNOFormat - Check GCNO signature is valid at the beginning of buffer.
82 bool readGCNOFormat() {
83 StringRef buf = Buffer->getBuffer();
84 StringRef magic = buf.substr(0, 4);
85 if (magic == "gcno") {
2
Assuming the condition is true
3
Taking true branch
86 de = DataExtractor(buf.substr(4), false, 0);
87 } else if (magic == "oncg") {
88 de = DataExtractor(buf.substr(4), true, 0);
89 } else {
90 errs() << "unexpected magic: " << magic << "\n";
91 return false;
92 }
93 return true;
4
Returning the value 1, which participates in a condition later
94 }
95
96 /// readGCDAFormat - Check GCDA signature is valid at the beginning of buffer.
97 bool readGCDAFormat() {
98 StringRef buf = Buffer->getBuffer();
99 StringRef magic = buf.substr(0, 4);
100 if (magic == "gcda") {
101 de = DataExtractor(buf.substr(4), false, 0);
102 } else if (magic == "adcg") {
103 de = DataExtractor(buf.substr(4), true, 0);
104 } else {
105 return false;
106 }
107 return true;
108 }
109
110 /// readGCOVVersion - Read GCOV version.
111 bool readGCOVVersion(GCOV::GCOVVersion &Version) {
112 std::string str(de.getBytes(cursor, 4));
113 if (str.size() != 4)
8
Assuming the condition is false
9
Taking false branch
114 return false;
115 if (de.isLittleEndian())
10
Taking false branch
116 std::reverse(str.begin(), str.end());
117 int ver = str[0] >= 'A'
11
Assuming the condition is false
12
'?' condition is false
118 ? (str[0] - 'A') * 100 + (str[1] - '0') * 10 + str[2] - '0'
119 : (str[0] - '0') * 10 + str[2] - '0';
120 if (ver >= 90) {
13
Assuming 'ver' is < 90
14
Taking false branch
121 // PR gcov-profile/84846, r269678
122 Version = GCOV::V900;
123 return true;
124 } else if (ver >= 80) {
15
Assuming 'ver' is < 80
16
Taking false branch
125 // PR gcov-profile/48463
126 Version = GCOV::V800;
127 return true;
128 } else if (ver >= 48) {
17
Assuming 'ver' is >= 48
18
Taking true branch
129 // r189778: the exit block moved from the last to the second.
130 Version = GCOV::V408;
131 return true;
19
Returning the value 1, which participates in a condition later
132 } else if (ver >= 47) {
133 // r173147: split checksum into cfg checksum and line checksum.
134 Version = GCOV::V407;
135 return true;
136 } else if (ver >= 34) {
137 Version = GCOV::V304;
138 return true;
139 }
140 errs() << "unexpected version: " << str << "\n";
141 return false;
142 }
143
144 uint32_t getWord() { return de.getU32(cursor); }
145 StringRef getString() {
146 uint32_t len;
147 if (!readInt(len) || len == 0)
148 return {};
149 return de.getBytes(cursor, len * 4).split('\0').first;
150 }
151
152 bool readInt(uint32_t &Val) {
153 if (cursor.tell() + 4 > de.size()) {
27
Assuming the condition is false
28
Taking false branch
154 Val = 0;
155 errs() << "unexpected end of memory buffer: " << cursor.tell() << "\n";
156 return false;
157 }
158 Val = de.getU32(cursor);
159 return true;
29
Returning the value 1, which participates in a condition later
160 }
161
162 bool readInt64(uint64_t &Val) {
163 uint32_t Lo, Hi;
164 if (!readInt(Lo) || !readInt(Hi))
165 return false;
166 Val = ((uint64_t)Hi << 32) | Lo;
167 return true;
168 }
169
170 bool readString(StringRef &Str) {
171 uint32_t len;
172 if (!readInt(len) || len == 0)
173 return false;
174 Str = de.getBytes(cursor, len * 4).split('\0').first;
175 return bool(cursor);
176 }
177
178 DataExtractor de{ArrayRef<uint8_t>{}, false, 0};
179 DataExtractor::Cursor cursor{0};
180
181private:
182 MemoryBuffer *Buffer;
183};
184
185/// GCOVFile - Collects coverage information for one pair of coverage file
186/// (.gcno and .gcda).
187class GCOVFile {
188public:
189 GCOVFile() = default;
190
191 bool readGCNO(GCOVBuffer &Buffer);
192 bool readGCDA(GCOVBuffer &Buffer);
193 GCOV::GCOVVersion getVersion() const { return Version; }
194 void print(raw_ostream &OS) const;
195 void dump() const;
196
197 std::vector<std::string> filenames;
198 StringMap<unsigned> filenameToIdx;
199
200public:
201 bool GCNOInitialized = false;
202 GCOV::GCOVVersion Version;
203 uint32_t Checksum = 0;
204 StringRef cwd;
205 SmallVector<std::unique_ptr<GCOVFunction>, 16> functions;
206 std::map<uint32_t, GCOVFunction *> IdentToFunction;
207 uint32_t RunCount = 0;
208 uint32_t ProgramCount = 0;
209
210 using iterator = pointee_iterator<
211 SmallVectorImpl<std::unique_ptr<GCOVFunction>>::const_iterator>;
212 iterator begin() const { return iterator(functions.begin()); }
213 iterator end() const { return iterator(functions.end()); }
214};
215
216struct GCOVArc {
217 GCOVArc(GCOVBlock &src, GCOVBlock &dst, uint32_t flags)
218 : src(src), dst(dst), flags(flags) {}
219 bool onTree() const;
220
221 GCOVBlock &src;
222 GCOVBlock &dst;
223 uint32_t flags;
224 uint64_t count = 0;
225 uint64_t cycleCount = 0;
226};
227
228/// GCOVFunction - Collects function information.
229class GCOVFunction {
230public:
231 using BlockIterator = pointee_iterator<
232 SmallVectorImpl<std::unique_ptr<GCOVBlock>>::const_iterator>;
233
234 GCOVFunction(GCOVFile &file) : file(file) {}
235
236 StringRef getName(bool demangle) const;
237 StringRef getFilename() const;
238 uint64_t getEntryCount() const;
239 GCOVBlock &getExitBlock() const;
240
241 iterator_range<BlockIterator> blocksRange() const {
242 return make_range(blocks.begin(), blocks.end());
243 }
244
245 uint64_t propagateCounts(const GCOVBlock &v, GCOVArc *pred);
246 void print(raw_ostream &OS) const;
247 void dump() const;
248
249 GCOVFile &file;
250 uint32_t ident = 0;
251 uint32_t linenoChecksum;
252 uint32_t cfgChecksum = 0;
253 uint32_t startLine = 0;
254 uint32_t startColumn = 0;
255 uint32_t endLine = 0;
256 uint32_t endColumn = 0;
257 uint8_t artificial = 0;
258 StringRef Name;
259 mutable SmallString<0> demangled;
260 unsigned srcIdx;
261 SmallVector<std::unique_ptr<GCOVBlock>, 0> blocks;
262 SmallVector<std::unique_ptr<GCOVArc>, 0> arcs, treeArcs;
263 DenseSet<const GCOVBlock *> visited;
264};
265
266/// GCOVBlock - Collects block information.
267class GCOVBlock {
268public:
269 using EdgeIterator = SmallVectorImpl<GCOVArc *>::const_iterator;
270 using BlockVector = SmallVector<const GCOVBlock *, 1>;
271 using BlockVectorLists = SmallVector<BlockVector, 4>;
272 using Edges = SmallVector<GCOVArc *, 4>;
273
274 GCOVBlock(uint32_t N) : number(N) {}
275
276 void addLine(uint32_t N) { lines.push_back(N); }
277 uint32_t getLastLine() const { return lines.back(); }
278 uint64_t getCount() const { return count; }
279
280 void addSrcEdge(GCOVArc *Edge) { pred.push_back(Edge); }
281
282 void addDstEdge(GCOVArc *Edge) { succ.push_back(Edge); }
283
284 iterator_range<EdgeIterator> srcs() const {
285 return make_range(pred.begin(), pred.end());
286 }
287
288 iterator_range<EdgeIterator> dsts() const {
289 return make_range(succ.begin(), succ.end());
290 }
291
292 void print(raw_ostream &OS) const;
293 void dump() const;
294
295 static uint64_t getCycleCount(const Edges &Path);
296 static void unblock(const GCOVBlock *U, BlockVector &Blocked,
297 BlockVectorLists &BlockLists);
298 static bool lookForCircuit(const GCOVBlock *V, const GCOVBlock *Start,
299 Edges &Path, BlockVector &Blocked,
300 BlockVectorLists &BlockLists,
301 const BlockVector &Blocks, uint64_t &Count);
302 static void getCyclesCount(const BlockVector &Blocks, uint64_t &Count);
303 static uint64_t getLineCount(const BlockVector &Blocks);
304
305public:
306 uint32_t number;
307 uint64_t count = 0;
308 SmallVector<GCOVArc *, 2> pred;
309 SmallVector<GCOVArc *, 2> succ;
310 SmallVector<uint32_t, 4> lines;
311};
312
313void gcovOneInput(const GCOV::Options &options, StringRef filename,
314 StringRef gcno, StringRef gcda, GCOVFile &file);
315
316} // end namespace llvm
317
318#endif // LLVM_SUPPORT_GCOV_H