Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 1 | //==- GRBlockCounter.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 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 | |
| 16 | #include "clang/Analysis/PathSensitive/GRBlockCounter.h" |
| 17 | #include "llvm/ADT/ImmutableMap.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | typedef llvm::ImmutableMap<unsigned,unsigned> CountMap; |
| 22 | |
| 23 | static inline CountMap GetMap(void* D) { |
| 24 | return CountMap(static_cast<CountMap::TreeTy*>(D)); |
| 25 | } |
| 26 | |
| 27 | static inline CountMap::Factory& GetFactory(void* F) { |
| 28 | return *static_cast<CountMap::Factory*>(F); |
| 29 | } |
| 30 | |
| 31 | unsigned GRBlockCounter::getNumVisited(unsigned BlockID) const { |
| 32 | CountMap M = GetMap(Data); |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 33 | CountMap::data_type* T = M.lookup(BlockID); |
| 34 | return T ? *T : 0; |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | GRBlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) { |
| 38 | F = new CountMap::Factory(Alloc); |
| 39 | } |
| 40 | |
| 41 | GRBlockCounter::Factory::~Factory() { |
Ted Kremenek | 5617fae | 2008-03-06 08:09:22 +0000 | [diff] [blame] | 42 | delete static_cast<CountMap::Factory*>(F); |
Ted Kremenek | 8e49dd6 | 2008-02-12 18:08:17 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | GRBlockCounter |
| 46 | GRBlockCounter::Factory::IncrementCount(GRBlockCounter BC, unsigned BlockID) { |
| 47 | return GRBlockCounter(GetFactory(F).Add(GetMap(BC.Data), BlockID, |
| 48 | BC.getNumVisited(BlockID)+1).getRoot()); |
| 49 | } |
| 50 | |
| 51 | GRBlockCounter |
| 52 | GRBlockCounter::Factory::GetEmptyCounter() { |
| 53 | return GRBlockCounter(GetFactory(F).GetEmptyMap().getRoot()); |
| 54 | } |