1 //==- BlockCounter.h - ADT for counting block visits -------------*- C++ -*-//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines BlockCounter, an abstract data type used to count
11 // the number of times a given block has been visited along a path
12 // analyzed by CoreEngine.
14 //===----------------------------------------------------------------------===//
16 #include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
17 #include "llvm/ADT/ImmutableMap.h"
19 using namespace clang;
25 const StackFrameContext *CallSite;
29 CountKey(const StackFrameContext *CS, unsigned ID)
30 : CallSite(CS), BlockID(ID) {}
32 bool operator==(const CountKey &RHS) const {
33 return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID);
36 bool operator<(const CountKey &RHS) const {
37 return std::tie(CallSite, BlockID) < std::tie(RHS.CallSite, RHS.BlockID);
40 void Profile(llvm::FoldingSetNodeID &ID) const {
41 ID.AddPointer(CallSite);
42 ID.AddInteger(BlockID);
48 typedef llvm::ImmutableMap<CountKey, unsigned> CountMap;
50 static inline CountMap GetMap(void *D) {
51 return CountMap(static_cast<CountMap::TreeTy*>(D));
54 static inline CountMap::Factory& GetFactory(void *F) {
55 return *static_cast<CountMap::Factory*>(F);
58 unsigned BlockCounter::getNumVisited(const StackFrameContext *CallSite,
59 unsigned BlockID) const {
60 CountMap M = GetMap(Data);
61 CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID));
65 BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
66 F = new CountMap::Factory(Alloc);
69 BlockCounter::Factory::~Factory() {
70 delete static_cast<CountMap::Factory*>(F);
74 BlockCounter::Factory::IncrementCount(BlockCounter BC,
75 const StackFrameContext *CallSite,
77 return BlockCounter(GetFactory(F).add(GetMap(BC.Data),
78 CountKey(CallSite, BlockID),
79 BC.getNumVisited(CallSite, BlockID)+1).getRoot());
83 BlockCounter::Factory::GetEmptyCounter() {
84 return BlockCounter(GetFactory(F).getEmptyMap().getRoot());