blob: 6d944da45b0951d9fce56778e2c9f8119eb65960 [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 {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000022protected:
Mike Stump1eb44332009-09-09 15:08:12 +000023 virtual SVal EvalCastNL(NonLoc val, QualType castTy);
24 virtual SVal EvalCastL(Loc val, QualType castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +000025
Ted Kremeneke8391722009-06-26 00:25:05 +000026public:
27 SimpleSValuator(ValueManager &valMgr) : SValuator(valMgr) {}
28 virtual ~SimpleSValuator() {}
Mike Stump1eb44332009-09-09 15:08:12 +000029
30 virtual SVal EvalMinus(NonLoc val);
31 virtual SVal EvalComplement(NonLoc val);
Ted Kremeneke8391722009-06-26 00:25:05 +000032 virtual SVal EvalBinOpNN(BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs,
33 QualType resultTy);
34 virtual SVal EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
35 QualType resultTy);
36 virtual SVal EvalBinOpLN(const GRState *state, BinaryOperator::Opcode op,
37 Loc lhs, NonLoc rhs, QualType resultTy);
Mike Stump1eb44332009-09-09 15:08:12 +000038};
Ted Kremeneke8391722009-06-26 00:25:05 +000039} // end anonymous namespace
40
41SValuator *clang::CreateSimpleSValuator(ValueManager &valMgr) {
42 return new SimpleSValuator(valMgr);
43}
44
45//===----------------------------------------------------------------------===//
46// Transfer function for Casts.
47//===----------------------------------------------------------------------===//
48
Ted Kremenekdd661142009-07-20 21:39:27 +000049SVal SimpleSValuator::EvalCastNL(NonLoc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000050
Ted Kremenekdd661142009-07-20 21:39:27 +000051 bool isLocType = Loc::IsLocType(castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek9031dd72009-07-21 00:12:07 +000053 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
54 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000055 return LI->getLoc();
Mike Stump1eb44332009-09-09 15:08:12 +000056
57 ASTContext &Ctx = ValMgr.getContext();
58
Ted Kremenek9031dd72009-07-21 00:12:07 +000059 // FIXME: Support promotions/truncations.
60 if (Ctx.getTypeSize(castTy) == Ctx.getTypeSize(Ctx.VoidPtrTy))
61 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek9031dd72009-07-21 00:12:07 +000063 return UnknownVal();
64 }
65
66 if (const SymExpr *se = val.getAsSymbolicExpression()) {
67 ASTContext &Ctx = ValMgr.getContext();
68 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
69 if (T == Ctx.getCanonicalType(castTy))
70 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000071
Ted Kremenek9031dd72009-07-21 00:12:07 +000072 return UnknownVal();
73 }
Mike Stump1eb44332009-09-09 15:08:12 +000074
Ted Kremeneke8391722009-06-26 00:25:05 +000075 if (!isa<nonloc::ConcreteInt>(val))
76 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000077
Ted Kremeneke8391722009-06-26 00:25:05 +000078 // Only handle casts from integers to integers.
79 if (!isLocType && !castTy->isIntegerType())
80 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000081
Ted Kremeneke8391722009-06-26 00:25:05 +000082 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
83 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
84 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
85
86 if (isLocType)
87 return ValMgr.makeIntLocVal(i);
88 else
89 return ValMgr.makeIntVal(i);
90}
91
Ted Kremenek46537392009-07-16 01:33:37 +000092SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremeneke8391722009-06-26 00:25:05 +000094 // Casts from pointers -> pointers, just return the lval.
95 //
96 // Casts from pointers -> references, just return the lval. These
97 // can be introduced by the frontend for corner cases, e.g
98 // casting from va_list* to __builtin_va_list&.
99 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000100 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
101 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremeneke8391722009-06-26 00:25:05 +0000103 // FIXME: Handle transparent unions where a value can be "transparently"
104 // lifted into a union type.
105 if (castTy->isUnionType())
106 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremeneke8391722009-06-26 00:25:05 +0000108 assert(castTy->isIntegerType());
109 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
110
111 if (!isa<loc::ConcreteInt>(val))
112 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremeneke8391722009-06-26 00:25:05 +0000114 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
115 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
116 i.extOrTrunc(BitWidth);
117 return ValMgr.makeIntVal(i);
118}
119
120//===----------------------------------------------------------------------===//
121// Transfer function for unary operators.
122//===----------------------------------------------------------------------===//
123
124SVal SimpleSValuator::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000125 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000126 case nonloc::ConcreteIntKind:
127 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
128 default:
129 return UnknownVal();
130 }
131}
132
133SVal SimpleSValuator::EvalComplement(NonLoc X) {
134 switch (X.getSubKind()) {
135 case nonloc::ConcreteIntKind:
136 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
137 default:
138 return UnknownVal();
139 }
140}
141
142//===----------------------------------------------------------------------===//
143// Transfer function for binary operators.
144//===----------------------------------------------------------------------===//
145
146static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
147 switch (op) {
148 default:
149 assert(false && "Invalid opcode.");
150 case BinaryOperator::LT: return BinaryOperator::GE;
151 case BinaryOperator::GT: return BinaryOperator::LE;
152 case BinaryOperator::LE: return BinaryOperator::GT;
153 case BinaryOperator::GE: return BinaryOperator::LT;
154 case BinaryOperator::EQ: return BinaryOperator::NE;
155 case BinaryOperator::NE: return BinaryOperator::EQ;
156 }
157}
158
Mike Stump1eb44332009-09-09 15:08:12 +0000159// Equality operators for Locs.
Ted Kremeneke8391722009-06-26 00:25:05 +0000160// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
161// implemented.
162
163static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
164 QualType resultTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Ted Kremeneke8391722009-06-26 00:25:05 +0000166 switch (lhs.getSubKind()) {
167 default:
168 assert(false && "EQ/NE not implemented for this Loc.");
169 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Ted Kremeneke8391722009-06-26 00:25:05 +0000171 case loc::ConcreteIntKind: {
172 if (SymbolRef rSym = rhs.getAsSymbol())
173 return ValMgr.makeNonLoc(rSym,
174 isEqual ? BinaryOperator::EQ
175 : BinaryOperator::NE,
176 cast<loc::ConcreteInt>(lhs).getValue(),
177 resultTy);
178 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000179 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000180 case loc::MemRegionKind: {
181 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
182 if (isa<loc::ConcreteInt>(rhs)) {
183 return ValMgr.makeNonLoc(lSym,
184 isEqual ? BinaryOperator::EQ
185 : BinaryOperator::NE,
186 cast<loc::ConcreteInt>(rhs).getValue(),
187 resultTy);
188 }
189 }
190 break;
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Ted Kremeneke8391722009-06-26 00:25:05 +0000193 case loc::GotoLabelKind:
194 break;
195 }
Mike Stump1eb44332009-09-09 15:08:12 +0000196
Ted Kremeneke8391722009-06-26 00:25:05 +0000197 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
198}
199
200SVal SimpleSValuator::EvalBinOpNN(BinaryOperator::Opcode op,
201 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000202 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000203 // Handle trivial case where left-side and right-side are the same.
204 if (lhs == rhs)
205 switch (op) {
206 default:
207 break;
208 case BinaryOperator::EQ:
209 case BinaryOperator::LE:
210 case BinaryOperator::GE:
211 return ValMgr.makeTruthVal(true, resultTy);
212 case BinaryOperator::LT:
213 case BinaryOperator::GT:
214 case BinaryOperator::NE:
215 return ValMgr.makeTruthVal(false, resultTy);
216 }
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Ted Kremeneke8391722009-06-26 00:25:05 +0000218 while (1) {
219 switch (lhs.getSubKind()) {
220 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000221 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000222 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000223 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000224 switch (rhs.getSubKind()) {
225 case nonloc::LocAsIntegerKind:
226 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000227 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000228 case nonloc::ConcreteIntKind: {
229 // Transform the integer into a location and compare.
230 ASTContext& Ctx = ValMgr.getContext();
231 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
232 i.setIsUnsigned(true);
233 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
234 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000237 switch (op) {
238 case BinaryOperator::EQ:
239 return ValMgr.makeTruthVal(false, resultTy);
240 case BinaryOperator::NE:
241 return ValMgr.makeTruthVal(true, resultTy);
242 default:
243 // This case also handles pointer arithmetic.
244 return UnknownVal();
245 }
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000248 case nonloc::SymExprValKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000249 // Logical not?
Ted Kremeneke8391722009-06-26 00:25:05 +0000250 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
251 return UnknownVal();
252
253 const SymExpr *symExpr =
254 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Ted Kremeneke8391722009-06-26 00:25:05 +0000256 // Only handle ($sym op constant) for now.
257 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
258 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
259 switch (opc) {
260 case BinaryOperator::LAnd:
261 case BinaryOperator::LOr:
262 assert(false && "Logical operators handled by branching logic.");
263 return UnknownVal();
264 case BinaryOperator::Assign:
265 case BinaryOperator::MulAssign:
266 case BinaryOperator::DivAssign:
267 case BinaryOperator::RemAssign:
268 case BinaryOperator::AddAssign:
269 case BinaryOperator::SubAssign:
270 case BinaryOperator::ShlAssign:
271 case BinaryOperator::ShrAssign:
272 case BinaryOperator::AndAssign:
273 case BinaryOperator::XorAssign:
274 case BinaryOperator::OrAssign:
275 case BinaryOperator::Comma:
276 assert(false && "'=' and ',' operators handled by GRExprEngine.");
277 return UnknownVal();
278 case BinaryOperator::PtrMemD:
279 case BinaryOperator::PtrMemI:
280 assert(false && "Pointer arithmetic not handled here.");
281 return UnknownVal();
282 case BinaryOperator::Mul:
283 case BinaryOperator::Div:
284 case BinaryOperator::Rem:
285 case BinaryOperator::Add:
286 case BinaryOperator::Sub:
287 case BinaryOperator::Shl:
288 case BinaryOperator::Shr:
289 case BinaryOperator::And:
290 case BinaryOperator::Xor:
291 case BinaryOperator::Or:
292 // Not handled yet.
293 return UnknownVal();
294 case BinaryOperator::LT:
295 case BinaryOperator::GT:
296 case BinaryOperator::LE:
297 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000298 case BinaryOperator::EQ:
Ted Kremeneke8391722009-06-26 00:25:05 +0000299 case BinaryOperator::NE:
300 opc = NegateComparison(opc);
301 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
302 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
303 symIntExpr->getRHS(), resultTy);
304 }
305 }
306 }
Mike Stump1eb44332009-09-09 15:08:12 +0000307 case nonloc::ConcreteIntKind: {
Ted Kremeneke8391722009-06-26 00:25:05 +0000308 if (isa<nonloc::ConcreteInt>(rhs)) {
309 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
310 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
311 }
312 else {
313 // Swap the left and right sides and flip the operator if doing so
314 // allows us to better reason about the expression (this is a form
315 // of expression canonicalization).
316 NonLoc tmp = rhs;
317 rhs = lhs;
318 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Ted Kremeneke8391722009-06-26 00:25:05 +0000320 switch (op) {
321 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
322 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
323 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
324 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
325 case BinaryOperator::EQ:
326 case BinaryOperator::NE:
327 case BinaryOperator::Add:
328 case BinaryOperator::Mul:
329 continue;
330 default:
331 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000332 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000333 }
334 }
335 case nonloc::SymbolValKind: {
336 if (isa<nonloc::ConcreteInt>(rhs)) {
337 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(lhs).getSymbol(), op,
338 cast<nonloc::ConcreteInt>(rhs).getValue(),
339 resultTy);
340 }
341
342 return UnknownVal();
343 }
344 }
345 }
346}
347
348SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000349 QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000350 switch (op) {
351 default:
352 return UnknownVal();
353 case BinaryOperator::EQ:
354 case BinaryOperator::NE:
355 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
356 }
357}
358
359SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
360 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000361 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000362 // Special case: 'rhs' is an integer that has the same width as a pointer and
363 // we are using the integer location in a comparison. Normally this cannot be
364 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
365 // can generate comparisons that trigger this code.
366 // FIXME: Are all locations guaranteed to have pointer width?
367 if (BinaryOperator::isEqualityOp(op)) {
368 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
369 const llvm::APSInt *x = &rhsInt->getValue();
370 ASTContext &ctx = ValMgr.getContext();
371 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
372 // Convert the signedness of the integer (if necessary).
373 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000374 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000375
376 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
377 }
378 }
379 }
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Ted Kremeneke8391722009-06-26 00:25:05 +0000381 // Delegate pointer arithmetic to the StoreManager.
382 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
383 rhs, resultTy);
384}