blob: c0bc81deda9a9b77f042b634c573078dba20c412 [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 Kremeneked900212008-02-05 18:17:58 +0000156 // Profiling and equality testing.
157
Ted Kremenek9153f732008-02-05 07:17:49 +0000158 bool operator==(const ValueState& RHS) const {
159 return Data == RHS.Data;
160 }
161
162 static void Profile(llvm::FoldingSetNodeID& ID, const ValueState& V) {
163 ID.AddPointer(V.getImpl());
164 }
165
166 void Profile(llvm::FoldingSetNodeID& ID) const {
167 Profile(ID, *this);
168 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000169
170 void printDOT(std::ostream& Out) const;
171 void print(std::ostream& Out) const;
172 void print() const { print(*llvm::cerr); }
173
Ted Kremenek9153f732008-02-05 07:17:49 +0000174};
175
176template<> struct GRTrait<ValueState> {
177 static inline void* toPtr(ValueState St) {
178 return reinterpret_cast<void*>(St.getImpl());
179 }
180 static inline ValueState toState(void* P) {
181 return ValueState(static_cast<ValueStateImpl*>(P));
182 }
183};
184
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000185
186class ValueStateManager {
187public:
188 typedef ValueState StateTy;
189
190private:
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000191 ValueState::IntSetTy::Factory ISetFactory;
Ted Kremenek016f52f2008-02-08 21:10:02 +0000192 ValueState::ExprBindingsTy::Factory EXFactory;
193 ValueState::VarBindingsTy::Factory VBFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000194 ValueState::ConstantNotEqTy::Factory CNEFactory;
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000195 ValueState::ConstantEqTy::Factory CEFactory;
Ted Kremenek174aea42008-02-05 18:51:06 +0000196
197 /// StateSet - FoldingSet containing all the states created for analyzing
198 /// a particular function. This is used to unique states.
Ted Kremenek9153f732008-02-05 07:17:49 +0000199 llvm::FoldingSet<ValueStateImpl> StateSet;
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000200
201 /// ValueMgr - Object that manages the data for all created RValues.
202 ValueManager ValMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000203
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000204 /// SymMgr - Object that manages the symbol information.
205 SymbolManager SymMgr;
Ted Kremenek9153f732008-02-05 07:17:49 +0000206
207 /// Alloc - A BumpPtrAllocator to allocate states.
208 llvm::BumpPtrAllocator& Alloc;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000209
210private:
211
212 ValueState::ExprBindingsTy Remove(ValueState::ExprBindingsTy B, Expr* E) {
213 return EXFactory.Remove(B, E);
214 }
215
216 ValueState::VarBindingsTy Remove(ValueState::VarBindingsTy B, VarDecl* V) {
217 return VBFactory.Remove(B, V);
218 }
Ted Kremenek9153f732008-02-05 07:17:49 +0000219
Ted Kremeneke7d22112008-02-11 19:21:59 +0000220 inline ValueState::ExprBindingsTy Remove(const ValueStateImpl& V, Expr* E) {
221 return Remove(V.BlockExprBindings, E);
222 }
223
224 inline ValueState::VarBindingsTy Remove(const ValueStateImpl& V, VarDecl* D) {
225 return Remove(V.VarBindings, D);
226 }
227
228 ValueState BindVar(ValueState St, VarDecl* D, const RValue& V);
229 ValueState UnbindVar(ValueState St, VarDecl* D);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000230
231public:
Ted Kremenek9153f732008-02-05 07:17:49 +0000232 ValueStateManager(ASTContext& Ctx, llvm::BumpPtrAllocator& alloc)
Ted Kremenek8158a0e2008-02-11 23:12:59 +0000233 : ISetFactory(alloc),
234 EXFactory(alloc),
235 VBFactory(alloc),
236 CNEFactory(alloc),
237 CEFactory(alloc),
238 ValMgr(Ctx, alloc), Alloc(alloc) {}
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000239
Ted Kremeneke7d22112008-02-11 19:21:59 +0000240 ValueState getInitialState();
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000241
242 ValueManager& getValueManager() { return ValMgr; }
243 SymbolManager& getSymbolManager() { return SymMgr; }
244
Ted Kremeneke7d22112008-02-11 19:21:59 +0000245 ValueState RemoveDeadBindings(ValueState St, Stmt* Loc,
246 const LiveVariables& Liveness);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000247
Ted Kremeneke7d22112008-02-11 19:21:59 +0000248 ValueState RemoveSubExprBindings(ValueState St) {
249 ValueStateImpl NewSt = *St;
250 NewSt.SubExprBindings = EXFactory.GetEmptyMap();
251 return getPersistentState(NewSt);
252 }
253
254
255 ValueState SetValue(ValueState St, Expr* S, bool isBlkExpr, const RValue& V);
256 ValueState SetValue(ValueState St, const LValue& LV, const RValue& V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000257
Ted Kremeneke7d22112008-02-11 19:21:59 +0000258 RValue GetValue(ValueState St, Expr* S, bool* hasVal = NULL);
259 RValue GetValue(ValueState St, const LValue& LV, QualType* T = NULL);
260 LValue GetLValue(ValueState St, Expr* S);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000261
Ted Kremeneke7d22112008-02-11 19:21:59 +0000262 ValueState getPersistentState(const ValueStateImpl& Impl);
Ted Kremenek016f52f2008-02-08 21:10:02 +0000263
Ted Kremeneke7d22112008-02-11 19:21:59 +0000264 ValueState AddEQ(ValueState St, SymbolID sym, const llvm::APSInt& V);
265 ValueState AddNE(ValueState St, SymbolID sym, const llvm::APSInt& V);
Ted Kremeneke070a1d2008-02-04 21:59:01 +0000266};
267
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000268} // end clang namespace
269
Ted Kremeneka90ccfe2008-01-31 19:34:24 +0000270#endif