Home | History | Annotate | Download | only in Core
      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 
     16 #include "clang/StaticAnalyzer/Core/PathSensitive/BlockCounter.h"
     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 (CallSite == RHS.CallSite) ? (BlockID < RHS.BlockID)
     38                                       : (CallSite < RHS.CallSite);
     39   }
     40 
     41   void Profile(llvm::FoldingSetNodeID &ID) const {
     42     ID.AddPointer(CallSite);
     43     ID.AddInteger(BlockID);
     44   }
     45 };
     46 
     47 }
     48 
     49 typedef llvm::ImmutableMap<CountKey, unsigned> CountMap;
     50 
     51 static inline CountMap GetMap(void *D) {
     52   return CountMap(static_cast<CountMap::TreeTy*>(D));
     53 }
     54 
     55 static inline CountMap::Factory& GetFactory(void *F) {
     56   return *static_cast<CountMap::Factory*>(F);
     57 }
     58 
     59 unsigned BlockCounter::getNumVisited(const StackFrameContext *CallSite,
     60                                        unsigned BlockID) const {
     61   CountMap M = GetMap(Data);
     62   CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID));
     63   return T ? *T : 0;
     64 }
     65 
     66 BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) {
     67   F = new CountMap::Factory(Alloc);
     68 }
     69 
     70 BlockCounter::Factory::~Factory() {
     71   delete static_cast<CountMap::Factory*>(F);
     72 }
     73 
     74 BlockCounter
     75 BlockCounter::Factory::IncrementCount(BlockCounter BC,
     76                                         const StackFrameContext *CallSite,
     77                                         unsigned BlockID) {
     78   return BlockCounter(GetFactory(F).add(GetMap(BC.Data),
     79                                           CountKey(CallSite, BlockID),
     80                              BC.getNumVisited(CallSite, BlockID)+1).getRoot());
     81 }
     82 
     83 BlockCounter
     84 BlockCounter::Factory::GetEmptyCounter() {
     85   return BlockCounter(GetFactory(F).getEmptyMap().getRoot());
     86 }
     87