blob: f69a16da401c3aec10eb18042477886ce82cd17d [file] [log] [blame]
Ted Kremenek8e49dd62008-02-12 18:08:17 +00001//==- 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 Kremenek4d4dd852008-02-13 17:41:41 +000012// analyzed by GRCoreEngine.
Ted Kremenek8e49dd62008-02-12 18:08:17 +000013//
14//===----------------------------------------------------------------------===//
15
16#include "clang/Analysis/PathSensitive/GRBlockCounter.h"
17#include "llvm/ADT/ImmutableMap.h"
18
19using namespace clang;
20
21typedef llvm::ImmutableMap<unsigned,unsigned> CountMap;
22
23static inline CountMap GetMap(void* D) {
24 return CountMap(static_cast<CountMap::TreeTy*>(D));
25}
26
27static inline CountMap::Factory& GetFactory(void* F) {
28 return *static_cast<CountMap::Factory*>(F);
29}
30
31unsigned GRBlockCounter::getNumVisited(unsigned BlockID) const {
32 CountMap M = GetMap(Data);
Ted Kremeneke8fdc832008-07-07 16:21:19 +000033 CountMap::data_type* T = M.lookup(BlockID);
34 return T ? *T : 0;
Ted Kremenek8e49dd62008-02-12 18:08:17 +000035}
36
37GRBlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
38 F = new CountMap::Factory(Alloc);
39}
40
41GRBlockCounter::Factory::~Factory() {
Ted Kremenek5617fae2008-03-06 08:09:22 +000042 delete static_cast<CountMap::Factory*>(F);
Ted Kremenek8e49dd62008-02-12 18:08:17 +000043}
44
45GRBlockCounter
46GRBlockCounter::Factory::IncrementCount(GRBlockCounter BC, unsigned BlockID) {
47 return GRBlockCounter(GetFactory(F).Add(GetMap(BC.Data), BlockID,
48 BC.getNumVisited(BlockID)+1).getRoot());
49}
50
51GRBlockCounter
52GRBlockCounter::Factory::GetEmptyCounter() {
53 return GRBlockCounter(GetFactory(F).GetEmptyMap().getRoot());
54}