blob: feaad191ef4a22a9a6c345bb5e386b09a29547f3 [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
Ted Kremenek45021952009-02-14 17:08:39 +000097 case loc::MemRegionKind: {
98 // FIXME: Should this go into the storemanager?
99
100 const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion();
101 const SubRegion* SubR = dyn_cast<SubRegion>(R);
102
103 while (SubR) {
104 // FIXME: now we only find the first symbolic region.
Zhongxing Xu3330dcb2009-04-10 06:06:13 +0000105 if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(SubR)) {
106 if (Assumption)
107 return AssumeSymNE(St, SymR->getSymbol(),
108 BasicVals.getZeroWithPtrWidth(), isFeasible);
109 else
110 return AssumeSymEQ(St, SymR->getSymbol(),
111 BasicVals.getZeroWithPtrWidth(), isFeasible);
112 }
Ted Kremenek45021952009-02-14 17:08:39 +0000113 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
114 }
115
116 // FALL-THROUGH.
117 }
118
119 case loc::FuncValKind:
120 case loc::GotoLabelKind:
121 isFeasible = Assumption;
122 return St;
123
124 case loc::ConcreteIntKind: {
125 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
126 isFeasible = b ? Assumption : !Assumption;
127 return St;
128 }
129 } // end switch
130}
131
132const GRState*
133SimpleConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption,
134 bool& isFeasible) {
135 St = AssumeAux(St, Cond, Assumption, isFeasible);
136
137 if (!isFeasible)
138 return St;
139
140 // EvalAssume is used to call into the GRTransferFunction object to perform
141 // any checker-specific update of the state based on this assumption being
142 // true or false.
143 return StateMgr.getTransferFuncs().EvalAssume(StateMgr, St, Cond, Assumption,
144 isFeasible);
145}
146
147const GRState*
148SimpleConstraintManager::AssumeAux(const GRState* St,NonLoc Cond,
149 bool Assumption, bool& isFeasible) {
Zhongxing Xua129eb92009-03-25 05:58:37 +0000150 // We cannot reason about SymIntExpr and SymSymExpr.
151 if (!canReasonAbout(Cond)) {
152 isFeasible = true;
153 return St;
154 }
155
Ted Kremenek45021952009-02-14 17:08:39 +0000156 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
157 SymbolManager& SymMgr = StateMgr.getSymbolManager();
158
159 switch (Cond.getSubKind()) {
160 default:
161 assert(false && "'Assume' not implemented for this NonLoc");
162
163 case nonloc::SymbolValKind: {
164 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
165 SymbolRef sym = SV.getSymbol();
166 QualType T = SymMgr.getType(sym);
167
168 if (Assumption)
169 return AssumeSymNE(St, sym, BasicVals.getValue(0, T), isFeasible);
170 else
171 return AssumeSymEQ(St, sym, BasicVals.getValue(0, T), isFeasible);
172 }
173
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000174 case nonloc::SymExprValKind: {
175 nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
176 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()))
177 return AssumeSymInt(St, Assumption, SE, isFeasible);
178
179 isFeasible = true;
180 return St;
181 }
Ted Kremenek45021952009-02-14 17:08:39 +0000182
183 case nonloc::ConcreteIntKind: {
184 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
185 isFeasible = b ? Assumption : !Assumption;
186 return St;
187 }
188
189 case nonloc::LocAsIntegerKind:
190 return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(),
191 Assumption, isFeasible);
192 } // end switch
193}
194
195const GRState*
196SimpleConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000197 const SymIntExpr *SE, bool& isFeasible) {
Ted Kremenek45021952009-02-14 17:08:39 +0000198
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000199
200 // Here we assume that LHS is a symbol. This is consistent with the
201 // rest of the constraint manager logic.
202 SymbolRef Sym = cast<SymbolData>(SE->getLHS());
203 const llvm::APSInt &Int = SE->getRHS();
204
205 switch (SE->getOpcode()) {
Ted Kremenek45021952009-02-14 17:08:39 +0000206 default:
207 // No logic yet for other operators.
208 isFeasible = true;
209 return St;
210
211 case BinaryOperator::EQ:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000212 return Assumption ? AssumeSymEQ(St, Sym, Int, isFeasible)
213 : AssumeSymNE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000214
215 case BinaryOperator::NE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000216 return Assumption ? AssumeSymNE(St, Sym, Int, isFeasible)
217 : AssumeSymEQ(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000218
219 case BinaryOperator::GT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000220 return Assumption ? AssumeSymGT(St, Sym, Int, isFeasible)
221 : AssumeSymLE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000222
223 case BinaryOperator::GE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000224 return Assumption ? AssumeSymGE(St, Sym, Int, isFeasible)
225 : AssumeSymLT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000226
227 case BinaryOperator::LT:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000228 return Assumption ? AssumeSymLT(St, Sym, Int, isFeasible)
229 : AssumeSymGE(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000230
231 case BinaryOperator::LE:
Ted Kremeneke0e4ebf2009-03-26 03:35:11 +0000232 return Assumption ? AssumeSymLE(St, Sym, Int, isFeasible)
233 : AssumeSymGT(St, Sym, Int, isFeasible);
Ted Kremenek45021952009-02-14 17:08:39 +0000234 } // end switch
235}
236
237const GRState*
238SimpleConstraintManager::AssumeInBound(const GRState* St, SVal Idx,
239 SVal UpperBound, bool Assumption,
240 bool& isFeasible) {
241 // Only support ConcreteInt for now.
242 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){
243 isFeasible = true;
244 return St;
245 }
246
247 const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false);
248 llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
249 // IdxV might be too narrow.
250 if (IdxV.getBitWidth() < Zero.getBitWidth())
251 IdxV.extend(Zero.getBitWidth());
252 // UBV might be too narrow, too.
253 llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
254 if (UBV.getBitWidth() < Zero.getBitWidth())
255 UBV.extend(Zero.getBitWidth());
256
257 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
258
259 isFeasible = Assumption ? InBound : !InBound;
260
261 return St;
262}
263
264} // end of namespace clang