blob: 38c34cb84a3a1cf2c1f689a3adaf6d94cb08e49d [file] [log] [blame]
Ted Kremeneka90ccfe2008-01-31 19:34:24 +00001//== ValueState.h - Path-Sens. "State" for tracking valuues -----*- 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//
Ted Kremenekd70b62e2008-02-08 20:29:23 +000010// This files defines SymbolID, ExprBindKey, and ValueState.
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000011//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_ANALYSIS_VALUESTATE_H
15#define LLVM_CLANG_ANALYSIS_VALUESTATE_H
16
17// FIXME: Reduce the number of includes.
18
Ted Kremenekcc409b72008-02-14 17:30:51 +000019#include "clang/Analysis/PathSensitive/RValues.h"
Ted Kremenek4d4dd852008-02-13 17:41:41 +000020#include "clang/Analysis/PathSensitive/GRCoreEngine.h"
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000021#include "clang/AST/Expr.h"
22#include "clang/AST/Decl.h"
23#include "clang/AST/ASTContext.h"
24#include "clang/Analysis/Analyses/LiveVariables.h"
25
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/DataTypes.h"
28#include "llvm/ADT/APSInt.h"
29#include "llvm/ADT/FoldingSet.h"
30#include "llvm/ADT/ImmutableMap.h"
31#include "llvm/ADT/SmallVector.h"
32#include "llvm/ADT/SmallPtrSet.h"
33#include "llvm/Support/Allocator.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/Support/Streams.h"
36
37#include <functional>
38
Ted Kremeneke7d22112008-02-11 19:21:59 +000039namespace clang {
Ted Kremeneka90ccfe2008-01-31 19:34:24 +000040
41//===----------------------------------------------------------------------===//
42// ValueState - An ImmutableMap type Stmt*/Decl*/Symbols to RValues.
43//===----------------------------------------------------------------------===//
44
Ted Kremenek9153f732008-02-05 07:17:49 +000045namespace vstate {
Ted Kremenek174aea42008-02-05 18:51:06 +000046 typedef llvm::ImmutableSet<llvm::APSInt*> IntSetTy;
47
Ted Kremeneke7d22112008-02-11 19:21:59 +000048 typedef llvm::ImmutableMap<Expr*,RValue> ExprBindingsTy;
Ted Kremenek016f52f2008-02-08 21:10:02 +000049 typedef llvm::ImmutableMap<VarDecl*,RValue> VarBindingsTy;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000050 typedef llvm::ImmutableMap<SymbolID,IntSetTy> ConstantNotEqTy;
51 typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstantEqTy;
Ted Kremenek9153f732008-02-05 07:17:49 +000052}
Ted Kremenek6f886bd2008-02-05 18:24:17 +000053
54/// ValueStateImpl - This class encapsulates the actual data values for
55/// for a "state" in our symbolic value tracking. It is intended to be
56/// used as a functional object; that is once it is created and made
57/// "persistent" in a FoldingSet its values will never change.
Ted Kremeneka40ba022008-02-06 02:50:36 +000058class ValueStateImpl : public llvm::FoldingSetNode {
59private:
60 void operator=(const ValueStateImpl& R) const;
61
62public:
Ted Kremeneke7d22112008-02-11 19:21:59 +000063 vstate::ExprBindingsTy SubExprBindings;
64 vstate::ExprBindingsTy BlockExprBindings;
Ted Kremenek53c641a2008-02-08 03:02:48 +000065 vstate::VarBindingsTy VarBindings;
Ted Kremenek174aea42008-02-05 18:51:06 +000066 vstate::ConstantNotEqTy ConstantNotEq;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000067 vstate::ConstantEqTy ConstantEq;
Ted Kremenek9153f732008-02-05 07:17:49 +000068
Ted Kremenek174aea42008-02-05 18:51:06 +000069 /// This ctor is used when creating the first ValueStateImpl object.
Ted Kremenek016f52f2008-02-08 21:10:02 +000070 ValueStateImpl(vstate::ExprBindingsTy EB,
71 vstate::VarBindingsTy VB,
Ted Kremenek862d5bb2008-02-06 00:54:14 +000072 vstate::ConstantNotEqTy CNE,
73 vstate::ConstantEqTy CE)
Ted Kremeneke7d22112008-02-11 19:21:59 +000074 : SubExprBindings(EB),
75 BlockExprBindings(EB),
76 VarBindings(VB),
77 ConstantNotEq(CNE),
78 ConstantEq(CE) {}
Ted Kremenek9153f732008-02-05 07:17:49 +000079
Ted Kremenek174aea42008-02-05 18:51:06 +000080 /// Copy ctor - We must explicitly define this or else the "Next" ptr
81 /// in FoldingSetNode will also get copied.
Ted Kremenek9153f732008-02-05 07:17:49 +000082 ValueStateImpl(const ValueStateImpl& RHS)
Ted Kremenek6f886bd2008-02-05 18:24:17 +000083 : llvm::FoldingSetNode(),
Ted Kremeneke7d22112008-02-11 19:21:59 +000084 SubExprBindings(RHS.SubExprBindings),
85 BlockExprBindings(RHS.BlockExprBindings),
Ted Kremenek53c641a2008-02-08 03:02:48 +000086 VarBindings(RHS.VarBindings),
Ted Kremenek862d5bb2008-02-06 00:54:14 +000087 ConstantNotEq(RHS.ConstantNotEq),
88 ConstantEq(RHS.ConstantEq) {}
Ted Kremenek9153f732008-02-05 07:17:49 +000089
Ted Kremeneka40ba022008-02-06 02:50:36 +000090
91
Ted Kremenek174aea42008-02-05 18:51:06 +000092 /// Profile - Profile the contents of a ValueStateImpl object for use
93 /// in a FoldingSet.
Ted Kremenek9153f732008-02-05 07:17:49 +000094 static void Profile(llvm::FoldingSetNodeID& ID, const ValueStateImpl& V) {
Ted Kremeneke7d22112008-02-11 19:21:59 +000095 V.SubExprBindings.Profile(ID);
96 V.BlockExprBindings.Profile(ID);
Ted Kremenek53c641a2008-02-08 03:02:48 +000097 V.VarBindings.Profile(ID);
Ted Kremenek862d5bb2008-02-06 00:54:14 +000098 V.ConstantNotEq.Profile(ID);
99 V.ConstantEq.Profile(ID);
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000100 }
Ted Kremenek174aea42008-02-05 18:51:06 +0000101
102 /// Profile - Used to profile the contents of this object for inclusion
103 /// in a FoldingSet.
Ted Kremenek9153f732008-02-05 07:17:49 +0000104 void Profile(llvm::FoldingSetNodeID& ID) const {
105 Profile(ID, *this);
106 }
107
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000108};
109
Ted Kremenek6f886bd2008-02-05 18:24:17 +0000110/// ValueState - This class represents a "state" in our symbolic value
111/// tracking. It is really just a "smart pointer", wrapping a pointer
112/// to ValueStateImpl object. Making this class a smart pointer means that its
113/// size is always the size of a pointer, which allows easy conversion to
Ted Kremenek4d4dd852008-02-13 17:41:41 +0000114/// void* when being handled by GRCoreEngine. It also forces us to unique states;
Ted Kremenek6f886bd2008-02-05 18:24:17 +0000115/// consequently, a ValueStateImpl* with a specific address will always refer
116/// to the unique state with those values.
Ted Kremeneka40ba022008-02-06 02:50:36 +0000117class ValueState {
Ted Kremenek9153f732008-02-05 07:17:49 +0000118 ValueStateImpl* Data;
119public:
Ted Kremeneked900212008-02-05 18:17:58 +0000120 ValueState(ValueStateImpl* D) : Data(D) {}
Ted Kremeneka40ba022008-02-06 02:50:36 +0000121 ValueState() : Data(0) {}
Ted Kremeneked900212008-02-05 18:17:58 +0000122
Ted Kremenekcba2e432008-02-05 19:35:18 +0000123 // Accessors.
Ted Kremeneked900212008-02-05 18:17:58 +0000124 ValueStateImpl* getImpl() const { return Data; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000125 ValueStateImpl& operator*() { return *Data; }
126 ValueStateImpl* operator->() { return Data; }
Ted Kremenek174aea42008-02-05 18:51:06 +0000127
Ted Kremenekcba2e432008-02-05 19:35:18 +0000128 // Typedefs.
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000129 typedef vstate::IntSetTy IntSetTy;
Ted Kremenek016f52f2008-02-08 21:10:02 +0000130 typedef vstate::ExprBindingsTy ExprBindingsTy;
131 typedef vstate::VarBindingsTy VarBindingsTy;
Ted Kremenekcba2e432008-02-05 19:35:18 +0000132 typedef vstate::ConstantNotEqTy ConstantNotEqTy;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000133 typedef vstate::ConstantEqTy ConstantEqTy;
134
Ted Kremenekcba2e432008-02-05 19:35:18 +0000135 typedef llvm::SmallVector<ValueState,5> BufferTy;
Ted Kremenek174aea42008-02-05 18:51:06 +0000136
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000137 // Queries.
138
139 bool isNotEqual(SymbolID sym, const llvm::APSInt& V) const;
140 const llvm::APSInt* getSymVal(SymbolID sym) const;
141
Ted Kremenek174aea42008-02-05 18:51:06 +0000142 // Iterators.
143
Ted Kremeneke7d22112008-02-11 19:21:59 +0000144 typedef VarBindingsTy::iterator vb_iterator;
145 vb_iterator vb_begin() const { return Data->VarBindings.begin(); }
146 vb_iterator vb_end() const { return Data->VarBindings.end(); }
147
148 typedef ExprBindingsTy::iterator seb_iterator;
149 seb_iterator seb_begin() const { return Data->SubExprBindings.begin(); }
150 seb_iterator seb_end() const { return Data->SubExprBindings.end(); }
Ted Kremenek016f52f2008-02-08 21:10:02 +0000151
Ted Kremeneke7d22112008-02-11 19:21:59 +0000152 typedef ExprBindingsTy::iterator beb_iterator;
153 beb_iterator beb_begin() const { return Data->BlockExprBindings.begin(); }
154 beb_iterator beb_end() const { return Data->BlockExprBindings.end(); }
Ted Kremenek9153f732008-02-05 07:17:49 +0000155
Ted Kremenek90e14812008-02-14 23:25:54 +0000156 typedef ConstantNotEqTy::iterator cne_iterator;
157 cne_iterator cne_begin() const { return Data->ConstantNotEq.begin(); }
158 cne_iterator cne_end() const { return Data->ConstantNotEq.end(); }
159
160 typedef ConstantEqTy::iterator ce_iterator;
161 ce_iterator ce_begin() const { return Data->ConstantEq.begin(); }
162 ce_iterator ce_end() const { return Data->ConstantEq.end(); }
163
Ted Kremeneked900212008-02-05 18:17:58 +0000164 // Profiling and equality testing.
165
Ted Kremenek9153f732008-02-05 07:17:49 +0000166 bool operator==(const ValueState& RHS) const {
167 return Data == RHS.Data;
168 }
169
170 static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) {
171 ID.AddPointer(V.getImpl());
172 }
173
174 void Profile(llvm::FoldingSetNodeID& ID) const {
175 Profile(ID, *this);
176 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000177
178 void printDOT(std::ostream& Out) const;
179 void print(std::ostream& Out) const;
180 void print() const { print(*llvm::cerr); }
181
Ted Kremenek9153f732008-02-05 07:17:49 +0000182};
183
184template<> struct GRTrait<ValueState> {
185 static inline void* toPtr(ValueState St) {
186 return reinterpret_cast<void*>(St.getImpl());
187 }
188 static inline ValueState toState(void* P) {
189 return ValueState(static_cast<ValueStateImpl*>(P));
190 }
191};
192
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000193
194class ValueStateManager {
195public:
196 typedef ValueState StateTy;
197
198private:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000199 ValueState::IntSetTy::Factory ISetFactory;
Ted Kremenek016f52f2008-02-08 21:10:02 +0000200 ValueState::ExprBindingsTy::Factory EXFactory;
201 ValueState::VarBindingsTy::Factory VBFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000202 ValueState::ConstantNotEqTy::Factory CNEFactory;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000203 ValueState::ConstantEqTy::Factory CEFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000204
205 /// StateSet - FoldingSet containing all the states created for analyzing
206 /// a particular function. This is used to unique states.
Ted Kremenek9153f732008-02-05 07:17:49 +0000207 llvm::FoldingSet<ValueStateImpl> StateSet;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000208
209 /// ValueMgr - Object that manages the data for all created RValues.
210 ValueManager ValMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000211
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000212 /// SymMgr - Object that manages the symbol information.
213 SymbolManager SymMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000214
215 /// Alloc - A BumpPtrAllocator to allocate states.
216 llvm::BumpPtrAllocator& Alloc;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000217
218private:
219
220 ValueState::ExprBindingsTy Remove(ValueState::ExprBindingsTy B, Expr* E) {
221 return EXFactory.Remove(B, E);
222 }
223
224 ValueState::VarBindingsTy Remove(ValueState::VarBindingsTy B, VarDecl* V) {
225 return VBFactory.Remove(B, V);
226 }
Ted Kremenek9153f732008-02-05 07:17:49 +0000227
Ted Kremeneke7d22112008-02-11 19:21:59 +0000228 inline ValueState::ExprBindingsTy Remove(const ValueStateImpl& V, Expr* E) {
229 return Remove(V.BlockExprBindings, E);
230 }
231
232 inline ValueState::VarBindingsTy Remove(const ValueStateImpl& V, VarDecl* D) {
233 return Remove(V.VarBindings, D);
234 }
235
236 ValueState BindVar(ValueState St, VarDecl* D, const RValue& V);
237 ValueState UnbindVar(ValueState St, VarDecl* D);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000238
239public:
Ted Kremenek9153f732008-02-05 07:17:49 +0000240 ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc)
Ted Kremenek8158a0e2008-02-11 23:12:59 +0000241 : ISetFactory(alloc),
242 EXFactory(alloc),
243 VBFactory(alloc),
244 CNEFactory(alloc),
245 CEFactory(alloc),
246 ValMgr(Ctx, alloc), Alloc(alloc) {}
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000247
Ted Kremeneke7d22112008-02-11 19:21:59 +0000248 ValueState getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000249
250 ValueManager& getValueManager() { return ValMgr; }
251 SymbolManager& getSymbolManager() { return SymMgr; }
252
Ted Kremeneke7d22112008-02-11 19:21:59 +0000253 ValueState RemoveDeadBindings(ValueState St, Stmt* Loc,
254 const LiveVariables& Liveness);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000255
Ted Kremeneke7d22112008-02-11 19:21:59 +0000256 ValueState RemoveSubExprBindings(ValueState St) {
257 ValueStateImpl NewSt = *St;
258 NewSt.SubExprBindings = EXFactory.GetEmptyMap();
259 return getPersistentState(NewSt);
260 }
261
262
263 ValueState SetValue(ValueState St, Expr* S, bool isBlkExpr, const RValue& V);
264 ValueState SetValue(ValueState St, const LValue& LV, const RValue& V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000265
Ted Kremeneke7d22112008-02-11 19:21:59 +0000266 RValue GetValue(ValueState St, Expr* S, bool* hasVal = NULL);
267 RValue GetValue(ValueState St, const LValue& LV, QualType* T = NULL);
268 LValue GetLValue(ValueState St, Expr* S);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000269
Ted Kremeneke7d22112008-02-11 19:21:59 +0000270 ValueState getPersistentState(const ValueStateImpl& Impl);
Ted Kremenek016f52f2008-02-08 21:10:02 +0000271
Ted Kremeneke7d22112008-02-11 19:21:59 +0000272 ValueState AddEQ(ValueState St, SymbolID sym, const llvm::APSInt& V);
273 ValueState AddNE(ValueState St, SymbolID sym, const llvm::APSInt& V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000274};
275
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000276} // end clang namespace
277
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000278#endif