blob: 89f3a716d2472557a7c0c9cc2f73dda6fb6d79dc [file] [log] [blame]
Ted Kremenek45021952009-02-14 17:08:39 +00001//== RangeConstraintManager.cpp - Manage range constraints.------*- 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 RangeConstraintManager, a class that tracks simple
11// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SimpleConstraintManager.h"
16#include "clang/Analysis/PathSensitive/GRState.h"
17#include "clang/Analysis/PathSensitive/GRStateTrait.h"
18#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Daniel Dunbare1bd4e62009-03-02 06:16:29 +000019#include "clang/Frontend/ManagerRegistry.h"
Ted Kremenek45021952009-02-14 17:08:39 +000020#include "llvm/Support/Compiler.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/ADT/FoldingSet.h"
23#include "llvm/ADT/ImmutableSet.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace clang;
27
Ted Kremenek9beefec2009-02-17 19:28:04 +000028namespace { class VISIBILITY_HIDDEN ConstraintRange {}; }
29static int ConstraintRangeIndex = 0;
Ted Kremenek45021952009-02-14 17:08:39 +000030
Ted Kremenek9beefec2009-02-17 19:28:04 +000031/// A Range represents the closed range [from, to]. The caller must
32/// guarantee that from <= to. Note that Range is immutable, so as not
33/// to subvert RangeSet's immutability.
Ted Kremenekb103f012009-02-18 05:22:01 +000034namespace {
35class VISIBILITY_HIDDEN Range : public std::pair<const llvm::APSInt*,
36 const llvm::APSInt*> {
Ted Kremenek45021952009-02-14 17:08:39 +000037public:
38 Range(const llvm::APSInt &from, const llvm::APSInt &to)
Ted Kremenek9beefec2009-02-17 19:28:04 +000039 : std::pair<const llvm::APSInt*, const llvm::APSInt*>(&from, &to) {
Ted Kremenek45021952009-02-14 17:08:39 +000040 assert(from <= to);
41 }
42 bool Includes(const llvm::APSInt &v) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000043 return *first <= v && v <= *second;
Ted Kremenek45021952009-02-14 17:08:39 +000044 }
45 const llvm::APSInt &From() const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000046 return *first;
Ted Kremenek45021952009-02-14 17:08:39 +000047 }
48 const llvm::APSInt &To() const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000049 return *second;
Ted Kremenek45021952009-02-14 17:08:39 +000050 }
Ted Kremenek9beefec2009-02-17 19:28:04 +000051 const llvm::APSInt *getConcreteValue() const {
52 return &From() == &To() ? &From() : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +000053 }
54
55 void Profile(llvm::FoldingSetNodeID &ID) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +000056 ID.AddPointer(&From());
57 ID.AddPointer(&To());
Ted Kremenek45021952009-02-14 17:08:39 +000058 }
59};
60
Ted Kremenekb103f012009-02-18 05:22:01 +000061
62class VISIBILITY_HIDDEN RangeTrait : public llvm::ImutContainerInfo<Range> {
63public:
64 // When comparing if one Range is less than another, we should compare
Ted Kremeneke53f8202009-02-18 17:42:44 +000065 // the actual APSInt values instead of their pointers. This keeps the order
66 // consistent (instead of comparing by pointer values) and can potentially
67 // be used to speed up some of the operations in RangeSet.
Ted Kremenekb103f012009-02-18 05:22:01 +000068 static inline bool isLess(key_type_ref lhs, key_type_ref rhs) {
69 return *lhs.first < *rhs.first || (!(*rhs.first < *lhs.first) &&
70 *lhs.second < *rhs.second);
71 }
72};
73
Ted Kremenek9beefec2009-02-17 19:28:04 +000074/// RangeSet contains a set of ranges. If the set is empty, then
75/// there the value of a symbol is overly constrained and there are no
76/// possible values for that symbol.
Ted Kremenekb103f012009-02-18 05:22:01 +000077class VISIBILITY_HIDDEN RangeSet {
78 typedef llvm::ImmutableSet<Range, RangeTrait> PrimRangeSet;
Ted Kremenek45021952009-02-14 17:08:39 +000079 PrimRangeSet ranges; // no need to make const, since it is an
80 // ImmutableSet - this allows default operator=
Ted Kremenek9beefec2009-02-17 19:28:04 +000081 // to work.
Ted Kremenek45021952009-02-14 17:08:39 +000082public:
Ted Kremenek9beefec2009-02-17 19:28:04 +000083 typedef PrimRangeSet::Factory Factory;
84 typedef PrimRangeSet::iterator iterator;
85
86 RangeSet(PrimRangeSet RS) : ranges(RS) {}
87 RangeSet(Factory& F) : ranges(F.GetEmptySet()) {}
88
89 iterator begin() const { return ranges.begin(); }
90 iterator end() const { return ranges.end(); }
91
92 bool isEmpty() const { return ranges.isEmpty(); }
93
94 /// Construct a new RangeSet representing '{ [from, to] }'.
95 RangeSet(Factory &F, const llvm::APSInt &from, const llvm::APSInt &to)
96 : ranges(F.Add(F.GetEmptySet(), Range(from, to))) {}
97
98 /// Profile - Generates a hash profile of this RangeSet for use
99 /// by FoldingSet.
100 void Profile(llvm::FoldingSetNodeID &ID) const { ranges.Profile(ID); }
101
102 /// getConcreteValue - If a symbol is contrained to equal a specific integer
103 /// constant then this method returns that value. Otherwise, it returns
104 /// NULL.
105 const llvm::APSInt* getConcreteValue() const {
106 return ranges.isSingleton() ? ranges.begin()->getConcreteValue() : 0;
Ted Kremenek45021952009-02-14 17:08:39 +0000107 }
108
Ted Kremenek9beefec2009-02-17 19:28:04 +0000109 /// AddEQ - Create a new RangeSet with the additional constraint that the
110 /// value be equal to V.
111 RangeSet AddEQ(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
112 // Search for a range that includes 'V'. If so, return a new RangeSet
113 // representing { [V, V] }.
114 for (PrimRangeSet::iterator i = begin(), e = end(); i!=e; ++i)
115 if (i->Includes(V))
116 return RangeSet(F, V, V);
117
118 return RangeSet(F);
Ted Kremenek45021952009-02-14 17:08:39 +0000119 }
120
Ted Kremenek9beefec2009-02-17 19:28:04 +0000121 /// AddNE - Create a new RangeSet with the additional constraint that the
122 /// value be not be equal to V.
123 RangeSet AddNE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
124 PrimRangeSet newRanges = ranges;
125
126 // FIXME: We can perhaps enhance ImmutableSet to do this search for us
127 // in log(N) time using the sorted property of the internal AVL tree.
128 for (iterator i = begin(), e = end(); i != e; ++i) {
129 if (i->Includes(V)) {
130 // Remove the old range.
131 newRanges = F.Remove(newRanges, *i);
132 // Split the old range into possibly one or two ranges.
133 if (V != i->From())
134 newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V)));
135 if (V != i->To())
136 newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To()));
137 // All of the ranges are non-overlapping, so we can stop.
138 break;
Ted Kremenek45021952009-02-14 17:08:39 +0000139 }
140 }
Ted Kremenek9beefec2009-02-17 19:28:04 +0000141
142 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000143 }
144
Ted Kremenek9beefec2009-02-17 19:28:04 +0000145 /// AddNE - Create a new RangeSet with the additional constraint that the
146 /// value be less than V.
147 RangeSet AddLT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
148 PrimRangeSet newRanges = F.GetEmptySet();
149
150 for (iterator i = begin(), e = end() ; i != e ; ++i) {
151 if (i->Includes(V) && i->From() < V)
152 newRanges = F.Add(newRanges, Range(i->From(), BV.Sub1(V)));
153 else if (i->To() < V)
154 newRanges = F.Add(newRanges, *i);
155 }
156
157 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000158 }
159
Ted Kremenek9beefec2009-02-17 19:28:04 +0000160 RangeSet AddLE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
161 PrimRangeSet newRanges = F.GetEmptySet();
Ted Kremenek45021952009-02-14 17:08:39 +0000162
Ted Kremenek9beefec2009-02-17 19:28:04 +0000163 for (iterator i = begin(), e = end(); i != e; ++i) {
164 // Strictly we should test for includes *V + 1, but no harm is
Ted Kremenek45021952009-02-14 17:08:39 +0000165 // done by this formulation
Ted Kremenek9beefec2009-02-17 19:28:04 +0000166 if (i->Includes(V))
167 newRanges = F.Add(newRanges, Range(i->From(), V));
168 else if (i->To() <= V)
169 newRanges = F.Add(newRanges, *i);
Ted Kremenek45021952009-02-14 17:08:39 +0000170 }
Ted Kremenek9beefec2009-02-17 19:28:04 +0000171
172 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000173 }
174
Ted Kremenek9beefec2009-02-17 19:28:04 +0000175 RangeSet AddGT(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
176 PrimRangeSet newRanges = F.GetEmptySet();
Ted Kremenek45021952009-02-14 17:08:39 +0000177
Ted Kremenek9beefec2009-02-17 19:28:04 +0000178 for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) {
179 if (i->Includes(V) && i->To() > V)
180 newRanges = F.Add(newRanges, Range(BV.Add1(V), i->To()));
181 else if (i->From() > V)
182 newRanges = F.Add(newRanges, *i);
Ted Kremenek45021952009-02-14 17:08:39 +0000183 }
Ted Kremenek9beefec2009-02-17 19:28:04 +0000184
185 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000186 }
187
Ted Kremenek9beefec2009-02-17 19:28:04 +0000188 RangeSet AddGE(BasicValueFactory &BV, Factory &F, const llvm::APSInt &V) {
189 PrimRangeSet newRanges = F.GetEmptySet();
Ted Kremenek45021952009-02-14 17:08:39 +0000190
Ted Kremenek9beefec2009-02-17 19:28:04 +0000191 for (PrimRangeSet::iterator i = begin(), e = end(); i != e; ++i) {
192 // Strictly we should test for includes *V - 1, but no harm is
Ted Kremenek45021952009-02-14 17:08:39 +0000193 // done by this formulation
Ted Kremenek9beefec2009-02-17 19:28:04 +0000194 if (i->Includes(V))
195 newRanges = F.Add(newRanges, Range(V, i->To()));
196 else if (i->From() >= V)
197 newRanges = F.Add(newRanges, *i);
Ted Kremenek45021952009-02-14 17:08:39 +0000198 }
199
Ted Kremenek9beefec2009-02-17 19:28:04 +0000200 return newRanges;
Ted Kremenek45021952009-02-14 17:08:39 +0000201 }
202
203 void Print(std::ostream &os) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000204 bool isFirst = true;
Ted Kremenek45021952009-02-14 17:08:39 +0000205 os << "{ ";
Ted Kremenek9beefec2009-02-17 19:28:04 +0000206 for (iterator i = begin(), e = end(); i != e; ++i) {
207 if (isFirst)
208 isFirst = false;
209 else
Ted Kremenek45021952009-02-14 17:08:39 +0000210 os << ", ";
Ted Kremenek9beefec2009-02-17 19:28:04 +0000211
Ted Kremenek45021952009-02-14 17:08:39 +0000212 os << '[' << i->From().toString(10) << ", " << i->To().toString(10)
213 << ']';
214 }
Ted Kremenek9beefec2009-02-17 19:28:04 +0000215 os << " }";
216 }
Ted Kremenek45021952009-02-14 17:08:39 +0000217
Ted Kremenek45021952009-02-14 17:08:39 +0000218 bool operator==(const RangeSet &other) const {
219 return ranges == other.ranges;
220 }
221};
Ted Kremenekb103f012009-02-18 05:22:01 +0000222} // end anonymous namespace
Ted Kremenek45021952009-02-14 17:08:39 +0000223
Ted Kremenek9beefec2009-02-17 19:28:04 +0000224typedef llvm::ImmutableMap<SymbolRef,RangeSet> ConstraintRangeTy;
Ted Kremenek45021952009-02-14 17:08:39 +0000225
226namespace clang {
227template<>
Ted Kremenek9beefec2009-02-17 19:28:04 +0000228struct GRStateTrait<ConstraintRange>
229 : public GRStatePartialTrait<ConstraintRangeTy> {
230 static inline void* GDMIndex() { return &ConstraintRangeIndex; }
Ted Kremenek45021952009-02-14 17:08:39 +0000231};
232}
233
234namespace {
Ted Kremenekb103f012009-02-18 05:22:01 +0000235class VISIBILITY_HIDDEN RangeConstraintManager : public SimpleConstraintManager{
Ted Kremenekc8781382009-06-17 22:28:13 +0000236 RangeSet GetRange(const GRState *state, SymbolRef sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000237public:
238 RangeConstraintManager(GRStateManager& statemgr)
239 : SimpleConstraintManager(statemgr) {}
240
241 const GRState* AssumeSymNE(const GRState* St, SymbolRef sym,
242 const llvm::APSInt& V, bool& isFeasible);
243
244 const GRState* AssumeSymEQ(const GRState* St, SymbolRef sym,
245 const llvm::APSInt& V, bool& isFeasible);
246
247 const GRState* AssumeSymLT(const GRState* St, SymbolRef sym,
248 const llvm::APSInt& V, bool& isFeasible);
249
250 const GRState* AssumeSymGT(const GRState* St, SymbolRef sym,
251 const llvm::APSInt& V, bool& isFeasible);
252
253 const GRState* AssumeSymGE(const GRState* St, SymbolRef sym,
254 const llvm::APSInt& V, bool& isFeasible);
255
256 const GRState* AssumeSymLE(const GRState* St, SymbolRef sym,
257 const llvm::APSInt& V, bool& isFeasible);
258
Ted Kremenek45021952009-02-14 17:08:39 +0000259 const llvm::APSInt* getSymVal(const GRState* St, SymbolRef sym) const;
Ted Kremenek9beefec2009-02-17 19:28:04 +0000260
261 // FIXME: Refactor into SimpleConstraintManager?
262 bool isEqual(const GRState* St, SymbolRef sym, const llvm::APSInt& V) const {
263 const llvm::APSInt *i = getSymVal(St, sym);
264 return i ? *i == V : false;
265 }
Ted Kremenek45021952009-02-14 17:08:39 +0000266
Ted Kremenek45021952009-02-14 17:08:39 +0000267 const GRState* RemoveDeadBindings(const GRState* St, SymbolReaper& SymReaper);
268
269 void print(const GRState* St, std::ostream& Out,
270 const char* nl, const char *sep);
271
272private:
Ted Kremenek9beefec2009-02-17 19:28:04 +0000273 RangeSet::Factory F;
Ted Kremenek45021952009-02-14 17:08:39 +0000274};
275
276} // end anonymous namespace
277
278ConstraintManager* clang::CreateRangeConstraintManager(GRStateManager& StateMgr)
279{
280 return new RangeConstraintManager(StateMgr);
281}
282
Ted Kremenek45021952009-02-14 17:08:39 +0000283const llvm::APSInt* RangeConstraintManager::getSymVal(const GRState* St,
284 SymbolRef sym) const {
Ted Kremenek9beefec2009-02-17 19:28:04 +0000285 const ConstraintRangeTy::data_type *T = St->get<ConstraintRange>(sym);
286 return T ? T->getConcreteValue() : NULL;
Ted Kremenek45021952009-02-14 17:08:39 +0000287}
288
289/// Scan all symbols referenced by the constraints. If the symbol is not alive
290/// as marked in LSymbols, mark it as dead in DSymbols.
291const GRState*
Ted Kremenekc8781382009-06-17 22:28:13 +0000292RangeConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek45021952009-02-14 17:08:39 +0000293 SymbolReaper& SymReaper) {
Ted Kremenek45021952009-02-14 17:08:39 +0000294
Ted Kremenekc8781382009-06-17 22:28:13 +0000295 ConstraintRangeTy CR = state->get<ConstraintRange>();
296 ConstraintRangeTy::Factory& CRFactory = state->get_context<ConstraintRange>();
Ted Kremenek45021952009-02-14 17:08:39 +0000297
Ted Kremenek9beefec2009-02-17 19:28:04 +0000298 for (ConstraintRangeTy::iterator I = CR.begin(), E = CR.end(); I != E; ++I) {
Ted Kremenek45021952009-02-14 17:08:39 +0000299 SymbolRef sym = I.getKey();
300 if (SymReaper.maybeDead(sym))
301 CR = CRFactory.Remove(CR, sym);
302 }
303
Ted Kremenekc8781382009-06-17 22:28:13 +0000304 return state->set<ConstraintRange>(CR);
Ted Kremenek45021952009-02-14 17:08:39 +0000305}
306
Ted Kremenek9beefec2009-02-17 19:28:04 +0000307//===------------------------------------------------------------------------===
308// AssumeSymX methods: public interface for RangeConstraintManager.
309//===------------------------------------------------------------------------===/
310
311RangeSet
Ted Kremenekc8781382009-06-17 22:28:13 +0000312RangeConstraintManager::GetRange(const GRState *state, SymbolRef sym) {
313 if (ConstraintRangeTy::data_type* V = state->get<ConstraintRange>(sym))
Ted Kremenek9beefec2009-02-17 19:28:04 +0000314 return *V;
315
316 // Lazily generate a new RangeSet representing all possible values for the
317 // given symbol type.
Ted Kremenekc8781382009-06-17 22:28:13 +0000318 QualType T = state->getSymbolManager().getType(sym);
319 BasicValueFactory& BV = state->getBasicVals();
Ted Kremenek9beefec2009-02-17 19:28:04 +0000320 return RangeSet(F, BV.getMinValue(T), BV.getMaxValue(T));
321}
322
323//===------------------------------------------------------------------------===
324// AssumeSymX methods: public interface for RangeConstraintManager.
325//===------------------------------------------------------------------------===/
326
327#define AssumeX(OP)\
328const GRState*\
Ted Kremenekc8781382009-06-17 22:28:13 +0000329RangeConstraintManager::AssumeSym ## OP(const GRState* state, SymbolRef sym,\
Ted Kremenek9beefec2009-02-17 19:28:04 +0000330 const llvm::APSInt& V, bool& isFeasible){\
Ted Kremenekc8781382009-06-17 22:28:13 +0000331 const RangeSet& R = GetRange(state, sym).Add##OP(state->getBasicVals(), F, V);\
Ted Kremenek9beefec2009-02-17 19:28:04 +0000332 isFeasible = !R.isEmpty();\
Ted Kremenekc8781382009-06-17 22:28:13 +0000333 return isFeasible ? state->set<ConstraintRange>(sym, R) : 0;\
Ted Kremenek9beefec2009-02-17 19:28:04 +0000334}
335
336AssumeX(EQ)
337AssumeX(NE)
338AssumeX(LT)
339AssumeX(GT)
340AssumeX(LE)
341AssumeX(GE)
342
343//===------------------------------------------------------------------------===
344// Pretty-printing.
345//===------------------------------------------------------------------------===/
346
Ted Kremenek45021952009-02-14 17:08:39 +0000347void RangeConstraintManager::print(const GRState* St, std::ostream& Out,
348 const char* nl, const char *sep) {
Ted Kremenekdd28d002009-02-16 18:42:56 +0000349
Ted Kremenek9beefec2009-02-17 19:28:04 +0000350 ConstraintRangeTy Ranges = St->get<ConstraintRange>();
Ted Kremenekdd28d002009-02-16 18:42:56 +0000351
352 if (Ranges.isEmpty())
353 return;
354
355 Out << nl << sep << "ranges of symbol values:";
356
Ted Kremenek9beefec2009-02-17 19:28:04 +0000357 for (ConstraintRangeTy::iterator I=Ranges.begin(), E=Ranges.end(); I!=E; ++I){
Ted Kremenekec751c42009-04-21 22:37:11 +0000358 Out << nl << ' ' << I.getKey() << " : ";
Ted Kremenekdd28d002009-02-16 18:42:56 +0000359 I.getData().Print(Out);
Ted Kremenek45021952009-02-14 17:08:39 +0000360 }
Ted Kremenek45021952009-02-14 17:08:39 +0000361}