opnsense-src/lib/StaticAnalyzer/Core/BlockCounter.cpp

87 lines
2.5 KiB
C++
Raw Normal View History

//==- BlockCounter.h - ADT for counting block visits -------------*- C++ -*-//
2009-10-14 14:03:49 -04:00
//
2009-06-02 13:58:47 -04:00
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines BlockCounter, an abstract data type used to count
2009-06-02 13:58:47 -04:00
// the number of times a given block has been visited along a path
// analyzed by CoreEngine.
2009-06-02 13:58:47 -04:00
//
//===----------------------------------------------------------------------===//
#include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
2009-06-02 13:58:47 -04:00
#include "llvm/ADT/ImmutableMap.h"
using namespace clang;
using namespace ento;
2009-06-02 13:58:47 -04:00
2010-04-02 04:55:10 -04:00
namespace {
class CountKey {
const StackFrameContext *CallSite;
unsigned BlockID;
public:
CountKey(const StackFrameContext *CS, unsigned ID)
: CallSite(CS), BlockID(ID) {}
bool operator==(const CountKey &RHS) const {
return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID);
}
bool operator<(const CountKey &RHS) const {
return (CallSite == RHS.CallSite) ? (BlockID < RHS.BlockID)
: (CallSite < RHS.CallSite);
}
void Profile(llvm::FoldingSetNodeID &ID) const {
ID.AddPointer(CallSite);
ID.AddInteger(BlockID);
}
};
}
typedef llvm::ImmutableMap<CountKey, unsigned> CountMap;
2009-06-02 13:58:47 -04:00
static inline CountMap GetMap(void *D) {
2009-06-02 13:58:47 -04:00
return CountMap(static_cast<CountMap::TreeTy*>(D));
}
static inline CountMap::Factory& GetFactory(void *F) {
2009-06-02 13:58:47 -04:00
return *static_cast<CountMap::Factory*>(F);
}
unsigned BlockCounter::getNumVisited(const StackFrameContext *CallSite,
2010-04-02 04:55:10 -04:00
unsigned BlockID) const {
2009-06-02 13:58:47 -04:00
CountMap M = GetMap(Data);
2010-04-02 04:55:10 -04:00
CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID));
2009-06-02 13:58:47 -04:00
return T ? *T : 0;
}
BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
2009-06-02 13:58:47 -04:00
F = new CountMap::Factory(Alloc);
}
BlockCounter::Factory::~Factory() {
2009-06-02 13:58:47 -04:00
delete static_cast<CountMap::Factory*>(F);
}
BlockCounter
BlockCounter::Factory::IncrementCount(BlockCounter BC,
2010-04-02 04:55:10 -04:00
const StackFrameContext *CallSite,
unsigned BlockID) {
return BlockCounter(GetFactory(F).add(GetMap(BC.Data),
2010-04-02 04:55:10 -04:00
CountKey(CallSite, BlockID),
BC.getNumVisited(CallSite, BlockID)+1).getRoot());
2009-06-02 13:58:47 -04:00
}
BlockCounter
BlockCounter::Factory::GetEmptyCounter() {
return BlockCounter(GetFactory(F).getEmptyMap().getRoot());
2009-06-02 13:58:47 -04:00
}