blob: 904479c4c3af5987033e2ad9589c7723d1f3e340 [file] [log] [blame]
Ted Kremenek45021952009-02-14 17:08:39 +00001//== SimpleConstraintManager.cpp --------------------------------*- 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 SimpleConstraintManager, a class that holds code shared
11// between BasicConstraintManager and RangeConstraintManager.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SimpleConstraintManager.h"
16#include "clang/Analysis/PathSensitive/GRExprEngine.h"
17#include "clang/Analysis/PathSensitive/GRState.h"
18
19namespace clang {
20
21SimpleConstraintManager::~SimpleConstraintManager() {}
22
Ted Kremenek66b52712009-03-11 02:22:59 +000023bool SimpleConstraintManager::canReasonAbout(SVal X) const {
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +000024 if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) {
25 const SymExpr *SE = SymVal->getSymbolicExpression();
26
27 if (isa<SymbolData>(SE))
28 return true;
29
30 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SE)) {
31 switch (SIE->getOpcode()) {
32 // We don't reason yet about bitwise-constraints on symbolic values.
33 case BinaryOperator::And:
34 case BinaryOperator::Or:
35 case BinaryOperator::Xor:
36 return false;
37 // We don't reason yet about arithmetic constraints on symbolic values.
38 case BinaryOperator::Mul:
39 case BinaryOperator::Div:
40 case BinaryOperator::Rem:
41 case BinaryOperator::Add:
42 case BinaryOperator::Sub:
43 case BinaryOperator::Shl:
44 case BinaryOperator::Shr:
45 return false;
46 // All other cases.
47 default:
48 return true;
49 }
50 }
51
52 return false;
Ted Kremenek7de20fe2009-03-11 02:29:48 +000053 }
54
Ted Kremenek66b52712009-03-11 02:22:59 +000055 return true;
56}
57
Ted Kremenek45021952009-02-14 17:08:39 +000058const GRState*
59SimpleConstraintManager::Assume(const GRState* St, SVal Cond, bool Assumption,
60 bool& isFeasible) {
61 if (Cond.isUnknown()) {
62 isFeasible = true;
63 return St;
64 }
65
66 if (isa<NonLoc>(Cond))
67 return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible);
68 else
69 return Assume(St, cast<Loc>(Cond), Assumption, isFeasible);
70}
71
72const GRState*
73SimpleConstraintManager::Assume(const GRState* St, Loc Cond, bool Assumption,
74 bool& isFeasible) {
75 St = AssumeAux(St, Cond, Assumption, isFeasible);
76
77 if (!isFeasible)
78 return St;
79
80 // EvalAssume is used to call into the GRTransferFunction object to perform
81 // any checker-specific update of the state based on this assumption being
82 // true or false.
83 return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption,
84 isFeasible);
85}
86
87const GRState*
88SimpleConstraintManager::AssumeAux(const GRState* St, Loc Cond, bool Assumption,
89 bool& isFeasible) {
90 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
91
92 switch (Cond.getSubKind()) {
93 default:
94 assert (false && "'Assume' not implemented for this Loc.");
95 return St;
96
97 case loc::SymbolValKind:
98 if (Assumption)
99 return AssumeSymNE(St, cast<loc::SymbolVal>(Cond).getSymbol(),
100 BasicVals.getZeroWithPtrWidth(), isFeasible);
101 else
102 return AssumeSymEQ(St, cast<loc::SymbolVal>(Cond).getSymbol(),
103 BasicVals.getZeroWithPtrWidth(), isFeasible);
104
105 case loc::MemRegionKind: {
106 // FIXME: Should this go into the storemanager?
107
108 const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion();
109 const SubRegion* SubR = dyn_cast<SubRegion>(R);
110
111 while (SubR) {
112 // FIXME: now we only find the first symbolic region.
113 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(SubR))
114 return AssumeAux(St, loc::SymbolVal(SymR->getSymbol()), Assumption,
115 isFeasible);
116 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
117 }
118
119 // FALL-THROUGH.
120 }
121
122 case loc::FuncValKind:
123 case loc::GotoLabelKind:
124 isFeasible = Assumption;
125 return St;
126
127 case loc::ConcreteIntKind: {
128 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
129 isFeasible = b ? Assumption : !Assumption;
130 return St;
131 }
132 } // end switch
133}
134
135const GRState*
136SimpleConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption,
137 bool& isFeasible) {
138 St = AssumeAux(St, Cond, Assumption, isFeasible);
139
140 if (!isFeasible)
141 return St;
142
143 // EvalAssume is used to call into the GRTransferFunction object to perform
144 // any checker-specific update of the state based on this assumption being
145 // true or false.
146 return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption,
147 isFeasible);
148}
149
150const GRState*
151SimpleConstraintManager::AssumeAux(const GRState* St,NonLoc Cond,
152 bool Assumption, bool& isFeasible) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000153 // We cannot reason about SymIntExpr and SymSymExpr.
154 if (!canReasonAbout(Cond)) {
155 isFeasible = true;
156 return St;
157 }
158
Ted Kremenek45021952009-02-14 17:08:39 +0000159 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
160 SymbolManager& SymMgr = StateMgr.getSymbolManager();
161
162 switch (Cond.getSubKind()) {
163 default:
164 assert(false && "'Assume' not implemented for this NonLoc");
165
166 case nonloc::SymbolValKind: {
167 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
168 SymbolRef sym = SV.getSymbol();
169 QualType T = SymMgr.getType(sym);
170
171 if (Assumption)
172 return AssumeSymNE(St, sym, BasicVals.getValue(0, T), isFeasible);
173 else
174 return AssumeSymEQ(St, sym, BasicVals.getValue(0, T), isFeasible);
175 }
176
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000177 case nonloc::SymExprValKind: {
178 nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
179 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()))
180 return AssumeSymInt(St, Assumption, SE, isFeasible);
181
182 isFeasible = true;
183 return St;
184 }
Ted Kremenek45021952009-02-14 17:08:39 +0000185
186 case nonloc::ConcreteIntKind: {
187 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
188 isFeasible = b ? Assumption : !Assumption;
189 return St;
190 }
191
192 case nonloc::LocAsIntegerKind:
193 return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(),
194 Assumption, isFeasible);
195 } // end switch
196}
197
198const GRState*
199SimpleConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000200 const SymIntExpr *SE, bool& isFeasible) {
Ted Kremenek45021952009-02-14 17:08:39 +0000201
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000202
203 // Here we assume that LHS is a symbol. This is consistent with the
204 // rest of the constraint manager logic.
205 SymbolRef Sym = cast<SymbolData>(SE->getLHS());
206 const llvm::APSInt &Int = SE->getRHS();
207
208 switch (SE->getOpcode()) {
Ted Kremenek45021952009-02-14 17:08:39 +0000209 default:
210 // No logic yet for other operators.
211 isFeasible = true;
212 return St;
213
214 case BinaryOperator::EQ:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000215 return Assumption ? AssumeSymEQ(St, Sym, Int, isFeasible)
216 : AssumeSymNE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000217
218 case BinaryOperator::NE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000219 return Assumption ? AssumeSymNE(St, Sym, Int, isFeasible)
220 : AssumeSymEQ(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000221
222 case BinaryOperator::GT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000223 return Assumption ? AssumeSymGT(St, Sym, Int, isFeasible)
224 : AssumeSymLE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000225
226 case BinaryOperator::GE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000227 return Assumption ? AssumeSymGE(St, Sym, Int, isFeasible)
228 : AssumeSymLT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000229
230 case BinaryOperator::LT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000231 return Assumption ? AssumeSymLT(St, Sym, Int, isFeasible)
232 : AssumeSymGE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000233
234 case BinaryOperator::LE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000235 return Assumption ? AssumeSymLE(St, Sym, Int, isFeasible)
236 : AssumeSymGT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000237 } // end switch
238}
239
240const GRState*
241SimpleConstraintManager::AssumeInBound(const GRState* St, SVal Idx,
242 SVal UpperBound, bool Assumption,
243 bool& isFeasible) {
244 // Only support ConcreteInt for now.
245 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){
246 isFeasible = true;
247 return St;
248 }
249
250 const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false);
251 llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
252 // IdxV might be too narrow.
253 if (IdxV.getBitWidth() < Zero.getBitWidth())
254 IdxV.extend(Zero.getBitWidth());
255 // UBV might be too narrow, too.
256 llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
257 if (UBV.getBitWidth() < Zero.getBitWidth())
258 UBV.extend(Zero.getBitWidth());
259
260 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
261
262 isFeasible = Assumption ? InBound : !InBound;
263
264 return St;
265}
266
267} // end of namespace clang