clang  6.0.0svn
BlockCounter.cpp
Go to the documentation of this file.
1 //==- BlockCounter.h - ADT for counting block visits -------------*- C++ -*-//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
17 #include "llvm/ADT/ImmutableMap.h"
18 
19 using namespace clang;
20 using namespace ento;
21 
22 namespace {
23 
24 class CountKey {
25  const StackFrameContext *CallSite;
26  unsigned BlockID;
27 
28 public:
29  CountKey(const StackFrameContext *CS, unsigned ID)
30  : CallSite(CS), BlockID(ID) {}
31 
32  bool operator==(const CountKey &RHS) const {
33  return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID);
34  }
35 
36  bool operator<(const CountKey &RHS) const {
37  return std::tie(CallSite, BlockID) < std::tie(RHS.CallSite, RHS.BlockID);
38  }
39 
40  void Profile(llvm::FoldingSetNodeID &ID) const {
41  ID.AddPointer(CallSite);
42  ID.AddInteger(BlockID);
43  }
44 };
45 
46 }
47 
49 
50 static inline CountMap GetMap(void *D) {
51  return CountMap(static_cast<CountMap::TreeTy*>(D));
52 }
53 
54 static inline CountMap::Factory& GetFactory(void *F) {
55  return *static_cast<CountMap::Factory*>(F);
56 }
57 
59  unsigned BlockID) const {
60  CountMap M = GetMap(Data);
61  CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID));
62  return T ? *T : 0;
63 }
64 
65 BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
66  F = new CountMap::Factory(Alloc);
67 }
68 
70  delete static_cast<CountMap::Factory*>(F);
71 }
72 
75  const StackFrameContext *CallSite,
76  unsigned BlockID) {
77  return BlockCounter(GetFactory(F).add(GetMap(BC.Data),
78  CountKey(CallSite, BlockID),
79  BC.getNumVisited(CallSite, BlockID)+1).getRoot());
80 }
81 
84  return BlockCounter(GetFactory(F).getEmptyMap().getRoot());
85 }
bool operator==(CanQual< T > x, CanQual< U > y)
An abstract data type used to count the number of times a given block has been visited along a path a...
Definition: BlockCounter.h:30
Factory(llvm::BumpPtrAllocator &Alloc)
llvm::ImmutableMap< CountKey, unsigned > CountMap
static CountMap GetMap(void *D)
static CountMap::Factory & GetFactory(void *F)
static DeltaTreeNode * getRoot(void *Root)
Definition: DeltaTree.cpp:383
const FunctionProtoType * T
unsigned getNumVisited(const StackFrameContext *CallSite, unsigned BlockID) const
bool operator<(DeclarationName LHS, DeclarationName RHS)
Ordering on two declaration names.
Dataflow Directional Tag Classes.
BlockCounter IncrementCount(BlockCounter BC, const StackFrameContext *CallSite, unsigned BlockID)