clang 23.0.0git
BlockCounter.cpp
Go to the documentation of this file.
1//==- BlockCounter.h - ADT for counting block visits -------------*- 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 file defines BlockCounter, an abstract data type used to count
10// the number of times a given block has been visited along a path
11// analyzed by CoreEngine.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/ImmutableMap.h"
17
18using namespace clang;
19using namespace ento;
20
21namespace {
22
23class CountKey {
24 const StackFrame *CallSite;
25 unsigned BlockID;
26
27public:
28 CountKey(const StackFrame *CS, unsigned ID) : CallSite(CS), BlockID(ID) {}
29
30 bool operator==(const CountKey &RHS) const {
31 return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID);
32 }
33
34 bool operator<(const CountKey &RHS) const {
35 return std::tie(CallSite, BlockID) < std::tie(RHS.CallSite, RHS.BlockID);
36 }
37
38 void Profile(llvm::FoldingSetNodeID &ID) const {
39 ID.AddPointer(CallSite);
40 ID.AddInteger(BlockID);
41 }
42};
43
44}
45
46typedef llvm::ImmutableMap<CountKey, unsigned> CountMap;
47
48static inline CountMap GetMap(void *D) {
49 return CountMap(static_cast<CountMap::TreeTy*>(D));
50}
51
52static inline CountMap::Factory& GetFactory(void *F) {
53 return *static_cast<CountMap::Factory*>(F);
54}
55
56unsigned BlockCounter::getNumVisited(const StackFrame *CallSite,
57 unsigned BlockID) const {
58 CountMap M = GetMap(Data);
59 CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID));
60 return T ? *T : 0;
61}
62
63BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
64 F = new CountMap::Factory(Alloc);
65}
66
68 delete static_cast<CountMap::Factory*>(F);
69}
70
71BlockCounter BlockCounter::Factory::IncrementCount(BlockCounter BC,
72 const StackFrame *CallSite,
73 unsigned BlockID) {
74 return BlockCounter(GetFactory(F).add(GetMap(BC.Data),
75 CountKey(CallSite, BlockID),
76 BC.getNumVisited(CallSite, BlockID)+1).getRoot());
77}
78
79BlockCounter
81 return BlockCounter(GetFactory(F).getEmptyMap().getRoot());
82}
static CountMap GetMap(void *D)
llvm::ImmutableMap< CountKey, unsigned > CountMap
static CountMap::Factory & GetFactory(void *F)
It represents a stack frame of the call stack (based on CallEvent).
Factory(llvm::BumpPtrAllocator &Alloc)
BlockCounter IncrementCount(BlockCounter BC, const StackFrame *CallSite, unsigned BlockID)
unsigned getNumVisited(const StackFrame *CallSite, unsigned BlockID) const
The JSON file list parser is used to communicate input to InstallAPI.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:206
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.