blob: 4f8c29c0710cdfc56cb5e23d20bd28c4dfb7a4c8 [file] [log] [blame]
Ted Kremeneke8391722009-06-26 00:25:05 +00001// SimpleSValuator.cpp - A basic SValuator ------------------------*- 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 SimpleSValuator, a basic implementation of SValuator.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Analysis/PathSensitive/SValuator.h"
15#include "clang/Analysis/PathSensitive/GRState.h"
16#include "llvm/Support/Compiler.h"
17
18using namespace clang;
19
20namespace {
21class VISIBILITY_HIDDEN SimpleSValuator : public SValuator {
22public:
23 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
24 virtual ~SimpleSValuator() {}
25
Ted Kremenek46537392009-07-16 01:33:37 +000026 virtual SVal EvalCastNL(NonLoc val, QualType castTy);
27 virtual SVal EvalCastL(Loc val, QualType castTy);
Ted Kremeneke8391722009-06-26 00:25:05 +000028 virtual SVal EvalMinus(NonLoc val);
29 virtual SVal EvalComplement(NonLoc val);
30 virtual SVal EvalBinOpNN(BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs,
31 QualType resultTy);
32 virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
33 QualType resultTy);
34 virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
35 Loc lhs, NonLoc rhs, QualType resultTy);
36};
37} // end anonymous namespace
38
39SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
40 return new SimpleSValuator(valMgr);
41}
42
43//===----------------------------------------------------------------------===//
44// Transfer function for Casts.
45//===----------------------------------------------------------------------===//
46
Ted Kremenekdd661142009-07-20 21:39:27 +000047SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
48
49 bool isLocType = Loc::IsLocType(castTy);
50
51 if (isLocType)
52 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val))
53 return LI->getLoc();
54
Ted Kremeneke8391722009-06-26 00:25:05 +000055 if (!isa<nonloc::ConcreteInt>(val))
56 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +000057
58 // Only handle casts from integers to integers.
59 if (!isLocType && !castTy->isIntegerType())
60 return UnknownVal();
61
62 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
63 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
64 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
65
66 if (isLocType)
67 return ValMgr.makeIntLocVal(i);
68 else
69 return ValMgr.makeIntVal(i);
70}
71
Ted Kremenek46537392009-07-16 01:33:37 +000072SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +000073
74 // Casts from pointers -> pointers, just return the lval.
75 //
76 // Casts from pointers -> references, just return the lval. These
77 // can be introduced by the frontend for corner cases, e.g
78 // casting from va_list* to __builtin_va_list&.
79 //
80 assert(!val.isUnknownOrUndef());
81
82 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
83 return val;
84
85 // FIXME: Handle transparent unions where a value can be "transparently"
86 // lifted into a union type.
87 if (castTy->isUnionType())
88 return UnknownVal();
89
90 assert(castTy->isIntegerType());
91 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
92
93 if (!isa<loc::ConcreteInt>(val))
94 return ValMgr.makeLocAsInteger(val, BitWidth);
95
96 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
97 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
98 i.extOrTrunc(BitWidth);
99 return ValMgr.makeIntVal(i);
100}
101
102//===----------------------------------------------------------------------===//
103// Transfer function for unary operators.
104//===----------------------------------------------------------------------===//
105
106SVal SimpleSValuator::EvalMinus(NonLoc val) {
107 switch (val.getSubKind()) {
108 case nonloc::ConcreteIntKind:
109 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
110 default:
111 return UnknownVal();
112 }
113}
114
115SVal SimpleSValuator::EvalComplement(NonLoc X) {
116 switch (X.getSubKind()) {
117 case nonloc::ConcreteIntKind:
118 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
119 default:
120 return UnknownVal();
121 }
122}
123
124//===----------------------------------------------------------------------===//
125// Transfer function for binary operators.
126//===----------------------------------------------------------------------===//
127
128static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
129 switch (op) {
130 default:
131 assert(false && "Invalid opcode.");
132 case BinaryOperator::LT: return BinaryOperator::GE;
133 case BinaryOperator::GT: return BinaryOperator::LE;
134 case BinaryOperator::LE: return BinaryOperator::GT;
135 case BinaryOperator::GE: return BinaryOperator::LT;
136 case BinaryOperator::EQ: return BinaryOperator::NE;
137 case BinaryOperator::NE: return BinaryOperator::EQ;
138 }
139}
140
141// Equality operators for Locs.
142// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
143// implemented.
144
145static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
146 QualType resultTy) {
147
148 switch (lhs.getSubKind()) {
149 default:
150 assert(false && "EQ/NE not implemented for this Loc.");
151 return UnknownVal();
152
153 case loc::ConcreteIntKind: {
154 if (SymbolRef rSym = rhs.getAsSymbol())
155 return ValMgr.makeNonLoc(rSym,
156 isEqual ? BinaryOperator::EQ
157 : BinaryOperator::NE,
158 cast<loc::ConcreteInt>(lhs).getValue(),
159 resultTy);
160 break;
161 }
162 case loc::MemRegionKind: {
163 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
164 if (isa<loc::ConcreteInt>(rhs)) {
165 return ValMgr.makeNonLoc(lSym,
166 isEqual ? BinaryOperator::EQ
167 : BinaryOperator::NE,
168 cast<loc::ConcreteInt>(rhs).getValue(),
169 resultTy);
170 }
171 }
172 break;
173 }
174
175 case loc::GotoLabelKind:
176 break;
177 }
178
179 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
180}
181
182SVal SimpleSValuator::EvalBinOpNN(BinaryOperator::Opcode op,
183 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000184 QualType resultTy) {
185
186 assert(!lhs.isUnknownOrUndef());
187 assert(!rhs.isUnknownOrUndef());
188
189 // Handle trivial case where left-side and right-side are the same.
190 if (lhs == rhs)
191 switch (op) {
192 default:
193 break;
194 case BinaryOperator::EQ:
195 case BinaryOperator::LE:
196 case BinaryOperator::GE:
197 return ValMgr.makeTruthVal(true, resultTy);
198 case BinaryOperator::LT:
199 case BinaryOperator::GT:
200 case BinaryOperator::NE:
201 return ValMgr.makeTruthVal(false, resultTy);
202 }
203
Ted Kremeneke8391722009-06-26 00:25:05 +0000204 while (1) {
205 switch (lhs.getSubKind()) {
206 default:
207 return UnknownVal();
208 case nonloc::LocAsIntegerKind: {
209 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
210 switch (rhs.getSubKind()) {
211 case nonloc::LocAsIntegerKind:
212 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
213 resultTy);
214 case nonloc::ConcreteIntKind: {
215 // Transform the integer into a location and compare.
216 ASTContext& Ctx = ValMgr.getContext();
217 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
218 i.setIsUnsigned(true);
219 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
220 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
221 }
222 default:
223 switch (op) {
224 case BinaryOperator::EQ:
225 return ValMgr.makeTruthVal(false, resultTy);
226 case BinaryOperator::NE:
227 return ValMgr.makeTruthVal(true, resultTy);
228 default:
229 // This case also handles pointer arithmetic.
230 return UnknownVal();
231 }
232 }
233 }
234 case nonloc::SymExprValKind: {
235 // Logical not?
236 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
237 return UnknownVal();
238
239 const SymExpr *symExpr =
240 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
241
242 // Only handle ($sym op constant) for now.
243 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
244 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
245 switch (opc) {
246 case BinaryOperator::LAnd:
247 case BinaryOperator::LOr:
248 assert(false && "Logical operators handled by branching logic.");
249 return UnknownVal();
250 case BinaryOperator::Assign:
251 case BinaryOperator::MulAssign:
252 case BinaryOperator::DivAssign:
253 case BinaryOperator::RemAssign:
254 case BinaryOperator::AddAssign:
255 case BinaryOperator::SubAssign:
256 case BinaryOperator::ShlAssign:
257 case BinaryOperator::ShrAssign:
258 case BinaryOperator::AndAssign:
259 case BinaryOperator::XorAssign:
260 case BinaryOperator::OrAssign:
261 case BinaryOperator::Comma:
262 assert(false && "'=' and ',' operators handled by GRExprEngine.");
263 return UnknownVal();
264 case BinaryOperator::PtrMemD:
265 case BinaryOperator::PtrMemI:
266 assert(false && "Pointer arithmetic not handled here.");
267 return UnknownVal();
268 case BinaryOperator::Mul:
269 case BinaryOperator::Div:
270 case BinaryOperator::Rem:
271 case BinaryOperator::Add:
272 case BinaryOperator::Sub:
273 case BinaryOperator::Shl:
274 case BinaryOperator::Shr:
275 case BinaryOperator::And:
276 case BinaryOperator::Xor:
277 case BinaryOperator::Or:
278 // Not handled yet.
279 return UnknownVal();
280 case BinaryOperator::LT:
281 case BinaryOperator::GT:
282 case BinaryOperator::LE:
283 case BinaryOperator::GE:
284 case BinaryOperator::EQ:
285 case BinaryOperator::NE:
286 opc = NegateComparison(opc);
287 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
288 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
289 symIntExpr->getRHS(), resultTy);
290 }
291 }
292 }
293 case nonloc::ConcreteIntKind: {
294 if (isa<nonloc::ConcreteInt>(rhs)) {
295 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
296 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
297 }
298 else {
299 // Swap the left and right sides and flip the operator if doing so
300 // allows us to better reason about the expression (this is a form
301 // of expression canonicalization).
302 NonLoc tmp = rhs;
303 rhs = lhs;
304 lhs = tmp;
305
306 switch (op) {
307 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
308 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
309 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
310 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
311 case BinaryOperator::EQ:
312 case BinaryOperator::NE:
313 case BinaryOperator::Add:
314 case BinaryOperator::Mul:
315 continue;
316 default:
317 return UnknownVal();
318 }
319 }
320 }
321 case nonloc::SymbolValKind: {
322 if (isa<nonloc::ConcreteInt>(rhs)) {
323 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(lhs).getSymbol(), op,
324 cast<nonloc::ConcreteInt>(rhs).getValue(),
325 resultTy);
326 }
327
328 return UnknownVal();
329 }
330 }
331 }
332}
333
334SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
335 QualType resultTy) {
336 switch (op) {
337 default:
338 return UnknownVal();
339 case BinaryOperator::EQ:
340 case BinaryOperator::NE:
341 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
342 }
343}
344
345SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
346 BinaryOperator::Opcode op,
347 Loc lhs, NonLoc rhs, QualType resultTy) {
348 // Special case: 'rhs' is an integer that has the same width as a pointer and
349 // we are using the integer location in a comparison. Normally this cannot be
350 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
351 // can generate comparisons that trigger this code.
352 // FIXME: Are all locations guaranteed to have pointer width?
353 if (BinaryOperator::isEqualityOp(op)) {
354 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
355 const llvm::APSInt *x = &rhsInt->getValue();
356 ASTContext &ctx = ValMgr.getContext();
357 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
358 // Convert the signedness of the integer (if necessary).
359 if (x->isSigned())
360 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
361
362 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
363 }
364 }
365 }
366
367 // Delegate pointer arithmetic to the StoreManager.
368 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
369 rhs, resultTy);
370}