Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 1 | //==- GRBlockCounter.h - ADT for counting block visits -------------*- C++ -*-// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2 | // |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 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 GRBlockCounter, an abstract data type used to count |
| 11 | // the number of times a given block has been visited along a path |
Ted Kremenek | 4d4dd85 | 2008-02-13 17:41:41 +0000 | [diff] [blame] | 12 | // analyzed by GRCoreEngine. |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 16 | #include "clang/Checker/PathSensitive/GRBlockCounter.h" |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ImmutableMap.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | class CountKey { |
| 24 | const StackFrameContext *CallSite; |
| 25 | unsigned BlockID; |
| 26 | |
| 27 | public: |
| 28 | CountKey(const StackFrameContext *CS, unsigned ID) |
| 29 | : CallSite(CS), BlockID(ID) {} |
| 30 | |
| 31 | bool operator==(const CountKey &RHS) const { |
| 32 | return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID); |
| 33 | } |
| 34 | |
| 35 | bool operator<(const CountKey &RHS) const { |
| 36 | return (CallSite == RHS.CallSite) ? (BlockID < RHS.BlockID) |
| 37 | : (CallSite < RHS.CallSite); |
| 38 | } |
| 39 | |
| 40 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 41 | ID.AddPointer(CallSite); |
| 42 | ID.AddInteger(BlockID); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | typedef llvm::ImmutableMap<CountKey, unsigned> CountMap; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 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 | |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 58 | unsigned GRBlockCounter::getNumVisited(const StackFrameContext *CallSite, |
| 59 | unsigned BlockID) const { |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 60 | CountMap M = GetMap(Data); |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 61 | CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID)); |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 62 | return T ? *T : 0; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | GRBlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) { |
| 66 | F = new CountMap::Factory(Alloc); |
| 67 | } |
| 68 | |
| 69 | GRBlockCounter::Factory::~Factory() { |
Ted Kremenek | 5617fae | 2008-03-06 08:09:22 +0000 | [diff] [blame] | 70 | delete static_cast<CountMap::Factory*>(F); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | GRBlockCounter |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 74 | GRBlockCounter::Factory::IncrementCount(GRBlockCounter BC, |
| 75 | const StackFrameContext *CallSite, |
| 76 | unsigned BlockID) { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 77 | return GRBlockCounter(GetFactory(F).add(GetMap(BC.Data), |
Zhongxing Xu | d9e0c0f | 2010-03-23 05:05:02 +0000 | [diff] [blame] | 78 | CountKey(CallSite, BlockID), |
| 79 | BC.getNumVisited(CallSite, BlockID)+1).getRoot()); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | GRBlockCounter |
| 83 | GRBlockCounter::Factory::GetEmptyCounter() { |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 84 | return GRBlockCounter(GetFactory(F).getEmptyMap().getRoot()); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 85 | } |