/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/clang/lib/StaticAnalyzer/Core/BlockCounter.cpp
Line | Count | Source |
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 | 356k | : CallSite(CS), BlockID(ID) {} |
31 | | |
32 | 1.19M | bool operator==(const CountKey &RHS) const { |
33 | 1.01M | return (CallSite == RHS.CallSite) && (BlockID == RHS.BlockID); |
34 | 1.19M | } |
35 | | |
36 | 623k | bool operator<(const CountKey &RHS) const { |
37 | 623k | return std::tie(CallSite, BlockID) < std::tie(RHS.CallSite, RHS.BlockID); |
38 | 623k | } |
39 | | |
40 | 266k | void Profile(llvm::FoldingSetNodeID &ID) const { |
41 | 266k | ID.AddPointer(CallSite); |
42 | 266k | ID.AddInteger(BlockID); |
43 | 266k | } |
44 | | }; |
45 | | |
46 | | } |
47 | | |
48 | | typedef llvm::ImmutableMap<CountKey, unsigned> CountMap; |
49 | | |
50 | 356k | static inline CountMap GetMap(void *D) { |
51 | 356k | return CountMap(static_cast<CountMap::TreeTy*>(D)); |
52 | 356k | } |
53 | | |
54 | 99.3k | static inline CountMap::Factory& GetFactory(void *F) { |
55 | 99.3k | return *static_cast<CountMap::Factory*>(F); |
56 | 99.3k | } |
57 | | |
58 | | unsigned BlockCounter::getNumVisited(const StackFrameContext *CallSite, |
59 | 264k | unsigned BlockID) const { |
60 | 264k | CountMap M = GetMap(Data); |
61 | 264k | CountMap::data_type* T = M.lookup(CountKey(CallSite, BlockID)); |
62 | 264k | return T ? *T221k : 042.3k ; |
63 | 264k | } |
64 | | |
65 | 6.87k | BlockCounter::Factory::Factory(llvm::BumpPtrAllocator& Alloc) { |
66 | 6.87k | F = new CountMap::Factory(Alloc); |
67 | 6.87k | } |
68 | | |
69 | 6.87k | BlockCounter::Factory::~Factory() { |
70 | 6.87k | delete static_cast<CountMap::Factory*>(F); |
71 | 6.87k | } |
72 | | |
73 | | BlockCounter |
74 | | BlockCounter::Factory::IncrementCount(BlockCounter BC, |
75 | | const StackFrameContext *CallSite, |
76 | 92.4k | unsigned BlockID) { |
77 | 92.4k | return BlockCounter(GetFactory(F).add(GetMap(BC.Data), |
78 | 92.4k | CountKey(CallSite, BlockID), |
79 | 92.4k | BC.getNumVisited(CallSite, BlockID)+1).getRoot()); |
80 | 92.4k | } |
81 | | |
82 | | BlockCounter |
83 | 6.87k | BlockCounter::Factory::GetEmptyCounter() { |
84 | 6.87k | return BlockCounter(GetFactory(F).getEmptyMap().getRoot()); |
85 | 6.87k | } |