1 //== SimpleConstraintManager.cpp --------------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines SimpleConstraintManager, a class that holds code shared
11 // between BasicConstraintManager and RangeConstraintManager.
13 //===----------------------------------------------------------------------===//
15 #include "SimpleConstraintManager.h"
16 #include "clang/Analysis/PathSensitive/GRExprEngine.h"
17 #include "clang/Analysis/PathSensitive/GRState.h"
21 SimpleConstraintManager::~SimpleConstraintManager() {}
23 bool SimpleConstraintManager::canReasonAbout(SVal X) const {
24 if (nonloc::SymExprVal *SymVal = dyn_cast<nonloc::SymExprVal>(&X)) {
25 const SymExpr *SE = SymVal->getSymbolicExpression();
27 if (isa<SymbolData>(SE))
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:
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:
58 const GRState *SimpleConstraintManager::Assume(const GRState *state,
59 SVal Cond, bool Assumption) {
60 if (Cond.isUnknown()) {
64 if (isa<NonLoc>(Cond))
65 return Assume(state, cast<NonLoc>(Cond), Assumption);
67 return Assume(state, cast<Loc>(Cond), Assumption);
70 const GRState *SimpleConstraintManager::Assume(const GRState *state, Loc Cond,
73 state = AssumeAux(state, Cond, Assumption);
75 // EvalAssume is used to call into the GRTransferFunction object to perform
76 // any checker-specific update of the state based on this assumption being
78 return state ? state->getTransferFuncs().EvalAssume(state, Cond, Assumption)
82 const GRState *SimpleConstraintManager::AssumeAux(const GRState *state,
83 Loc Cond, bool Assumption) {
85 BasicValueFactory &BasicVals = state->getBasicVals();
87 switch (Cond.getSubKind()) {
89 assert (false && "'Assume' not implemented for this Loc.");
92 case loc::MemRegionKind: {
93 // FIXME: Should this go into the storemanager?
95 const MemRegion *R = cast<loc::MemRegionVal>(Cond).getRegion();
96 const SubRegion *SubR = dyn_cast<SubRegion>(R);
99 // FIXME: now we only find the first symbolic region.
100 if (const SymbolicRegion *SymR = dyn_cast<SymbolicRegion>(SubR)) {
102 return AssumeSymNE(state, SymR->getSymbol(),
103 BasicVals.getZeroWithPtrWidth());
105 return AssumeSymEQ(state, SymR->getSymbol(),
106 BasicVals.getZeroWithPtrWidth());
108 SubR = dyn_cast<SubRegion>(SubR->getSuperRegion());
114 case loc::GotoLabelKind:
115 return Assumption ? state : NULL;
117 case loc::ConcreteIntKind: {
118 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
119 bool isFeasible = b ? Assumption : !Assumption;
120 return isFeasible ? state : NULL;
125 const GRState *SimpleConstraintManager::Assume(const GRState *state,
129 state = AssumeAux(state, Cond, Assumption);
131 // EvalAssume is used to call into the GRTransferFunction object to perform
132 // any checker-specific update of the state based on this assumption being
134 return state ? state->getTransferFuncs().EvalAssume(state, Cond, Assumption)
138 const GRState *SimpleConstraintManager::AssumeAux(const GRState *state,
142 // We cannot reason about SymIntExpr and SymSymExpr.
143 if (!canReasonAbout(Cond)) {
144 // Just return the current state indicating that the path is feasible.
145 // This may be an over-approximation of what is possible.
149 BasicValueFactory &BasicVals = state->getBasicVals();
150 SymbolManager &SymMgr = state->getSymbolManager();
152 switch (Cond.getSubKind()) {
154 assert(false && "'Assume' not implemented for this NonLoc");
156 case nonloc::SymbolValKind: {
157 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
158 SymbolRef sym = SV.getSymbol();
159 QualType T = SymMgr.getType(sym);
160 const llvm::APSInt &zero = BasicVals.getValue(0, T);
162 return Assumption ? AssumeSymNE(state, sym, zero)
163 : AssumeSymEQ(state, sym, zero);
166 case nonloc::SymExprValKind: {
167 nonloc::SymExprVal V = cast<nonloc::SymExprVal>(Cond);
168 if (const SymIntExpr *SE = dyn_cast<SymIntExpr>(V.getSymbolicExpression()))
169 return AssumeSymInt(state, Assumption, SE);
171 // For all other symbolic expressions, over-approximate and consider
172 // the constraint feasible.
176 case nonloc::ConcreteIntKind: {
177 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
178 bool isFeasible = b ? Assumption : !Assumption;
179 return isFeasible ? state : NULL;
182 case nonloc::LocAsIntegerKind:
183 return AssumeAux(state, cast<nonloc::LocAsInteger>(Cond).getLoc(),
188 const GRState *SimpleConstraintManager::AssumeSymInt(const GRState *state,
190 const SymIntExpr *SE) {
193 // Here we assume that LHS is a symbol. This is consistent with the
194 // rest of the constraint manager logic.
195 SymbolRef Sym = cast<SymbolData>(SE->getLHS());
196 const llvm::APSInt &Int = SE->getRHS();
198 switch (SE->getOpcode()) {
200 // No logic yet for other operators. Assume the constraint is feasible.
203 case BinaryOperator::EQ:
204 return Assumption ? AssumeSymEQ(state, Sym, Int)
205 : AssumeSymNE(state, Sym, Int);
207 case BinaryOperator::NE:
208 return Assumption ? AssumeSymNE(state, Sym, Int)
209 : AssumeSymEQ(state, Sym, Int);
210 case BinaryOperator::GT:
211 return Assumption ? AssumeSymGT(state, Sym, Int)
212 : AssumeSymLE(state, Sym, Int);
214 case BinaryOperator::GE:
215 return Assumption ? AssumeSymGE(state, Sym, Int)
216 : AssumeSymLT(state, Sym, Int);
218 case BinaryOperator::LT:
219 return Assumption ? AssumeSymLT(state, Sym, Int)
220 : AssumeSymGE(state, Sym, Int);
222 case BinaryOperator::LE:
223 return Assumption ? AssumeSymLE(state, Sym, Int)
224 : AssumeSymGT(state, Sym, Int);
228 const GRState *SimpleConstraintManager::AssumeInBound(const GRState *state,
233 // Only support ConcreteInt for now.
234 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound)))
237 const llvm::APSInt& Zero = state->getBasicVals().getZeroWithPtrWidth(false);
238 llvm::APSInt IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
239 // IdxV might be too narrow.
240 if (IdxV.getBitWidth() < Zero.getBitWidth())
241 IdxV.extend(Zero.getBitWidth());
242 // UBV might be too narrow, too.
243 llvm::APSInt UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
244 if (UBV.getBitWidth() < Zero.getBitWidth())
245 UBV.extend(Zero.getBitWidth());
247 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
248 bool isFeasible = Assumption ? InBound : !InBound;
249 return isFeasible ? state : NULL;
252 } // end of namespace clang