blob: 52a591927dfe2045059736d903a245f0dd683fee [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;
Ted Kremenek80417472009-09-25 00:18:15 +000071
72 // FIXME: Remove this hack when we support symbolic truncation/extension.
73 // HACK: If both castTy and T are integers, ignore the cast. This is
74 // not a permanent solution. Eventually we want to precisely handle
75 // extension/truncation of symbolic integers. This prevents us from losing
76 // precision when we assign 'x = y' and 'y' is symbolic and x and y are
77 // different integer types.
78 if (T->isIntegerType() && castTy->isIntegerType())
79 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Ted Kremenek9031dd72009-07-21 00:12:07 +000081 return UnknownVal();
82 }
Mike Stump1eb44332009-09-09 15:08:12 +000083
Ted Kremeneke8391722009-06-26 00:25:05 +000084 if (!isa<nonloc::ConcreteInt>(val))
85 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremeneke8391722009-06-26 00:25:05 +000087 // Only handle casts from integers to integers.
88 if (!isLocType && !castTy->isIntegerType())
89 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000090
Ted Kremeneke8391722009-06-26 00:25:05 +000091 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
92 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
93 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
94
95 if (isLocType)
96 return ValMgr.makeIntLocVal(i);
97 else
98 return ValMgr.makeIntVal(i);
99}
100
Ted Kremenek46537392009-07-16 01:33:37 +0000101SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremeneke8391722009-06-26 00:25:05 +0000103 // Casts from pointers -> pointers, just return the lval.
104 //
105 // Casts from pointers -> references, just return the lval. These
106 // can be introduced by the frontend for corner cases, e.g
107 // casting from va_list* to __builtin_va_list&.
108 //
Ted Kremeneke8391722009-06-26 00:25:05 +0000109 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
110 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Ted Kremeneke8391722009-06-26 00:25:05 +0000112 // FIXME: Handle transparent unions where a value can be "transparently"
113 // lifted into a union type.
114 if (castTy->isUnionType())
115 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Ted Kremeneke8391722009-06-26 00:25:05 +0000117 assert(castTy->isIntegerType());
118 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
119
120 if (!isa<loc::ConcreteInt>(val))
121 return ValMgr.makeLocAsInteger(val, BitWidth);
Mike Stump1eb44332009-09-09 15:08:12 +0000122
Ted Kremeneke8391722009-06-26 00:25:05 +0000123 llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue();
124 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
125 i.extOrTrunc(BitWidth);
126 return ValMgr.makeIntVal(i);
127}
128
129//===----------------------------------------------------------------------===//
130// Transfer function for unary operators.
131//===----------------------------------------------------------------------===//
132
133SVal SimpleSValuator::EvalMinus(NonLoc val) {
Mike Stump1eb44332009-09-09 15:08:12 +0000134 switch (val.getSubKind()) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000135 case nonloc::ConcreteIntKind:
136 return cast<nonloc::ConcreteInt>(val).evalMinus(ValMgr);
137 default:
138 return UnknownVal();
139 }
140}
141
142SVal SimpleSValuator::EvalComplement(NonLoc X) {
143 switch (X.getSubKind()) {
144 case nonloc::ConcreteIntKind:
145 return cast<nonloc::ConcreteInt>(X).evalComplement(ValMgr);
146 default:
147 return UnknownVal();
148 }
149}
150
151//===----------------------------------------------------------------------===//
152// Transfer function for binary operators.
153//===----------------------------------------------------------------------===//
154
155static BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) {
156 switch (op) {
157 default:
158 assert(false && "Invalid opcode.");
159 case BinaryOperator::LT: return BinaryOperator::GE;
160 case BinaryOperator::GT: return BinaryOperator::LE;
161 case BinaryOperator::LE: return BinaryOperator::GT;
162 case BinaryOperator::GE: return BinaryOperator::LT;
163 case BinaryOperator::EQ: return BinaryOperator::NE;
164 case BinaryOperator::NE: return BinaryOperator::EQ;
165 }
166}
167
Mike Stump1eb44332009-09-09 15:08:12 +0000168// Equality operators for Locs.
Ted Kremeneke8391722009-06-26 00:25:05 +0000169// FIXME: All this logic will be revamped when we have MemRegion::getLocation()
170// implemented.
171
172static SVal EvalEquality(ValueManager &ValMgr, Loc lhs, Loc rhs, bool isEqual,
173 QualType resultTy) {
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Ted Kremeneke8391722009-06-26 00:25:05 +0000175 switch (lhs.getSubKind()) {
176 default:
177 assert(false && "EQ/NE not implemented for this Loc.");
178 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Ted Kremeneke8391722009-06-26 00:25:05 +0000180 case loc::ConcreteIntKind: {
181 if (SymbolRef rSym = rhs.getAsSymbol())
182 return ValMgr.makeNonLoc(rSym,
183 isEqual ? BinaryOperator::EQ
184 : BinaryOperator::NE,
185 cast<loc::ConcreteInt>(lhs).getValue(),
186 resultTy);
187 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000188 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000189 case loc::MemRegionKind: {
190 if (SymbolRef lSym = lhs.getAsLocSymbol()) {
191 if (isa<loc::ConcreteInt>(rhs)) {
192 return ValMgr.makeNonLoc(lSym,
193 isEqual ? BinaryOperator::EQ
194 : BinaryOperator::NE,
195 cast<loc::ConcreteInt>(rhs).getValue(),
196 resultTy);
197 }
198 }
199 break;
200 }
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Ted Kremeneke8391722009-06-26 00:25:05 +0000202 case loc::GotoLabelKind:
203 break;
204 }
Mike Stump1eb44332009-09-09 15:08:12 +0000205
Ted Kremeneke8391722009-06-26 00:25:05 +0000206 return ValMgr.makeTruthVal(isEqual ? lhs == rhs : lhs != rhs, resultTy);
207}
208
209SVal SimpleSValuator::EvalBinOpNN(BinaryOperator::Opcode op,
210 NonLoc lhs, NonLoc rhs,
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000211 QualType resultTy) {
Ted Kremenek54ca9b12009-07-13 21:55:12 +0000212 // Handle trivial case where left-side and right-side are the same.
213 if (lhs == rhs)
214 switch (op) {
215 default:
216 break;
217 case BinaryOperator::EQ:
218 case BinaryOperator::LE:
219 case BinaryOperator::GE:
220 return ValMgr.makeTruthVal(true, resultTy);
221 case BinaryOperator::LT:
222 case BinaryOperator::GT:
223 case BinaryOperator::NE:
224 return ValMgr.makeTruthVal(false, resultTy);
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Ted Kremeneke8391722009-06-26 00:25:05 +0000227 while (1) {
228 switch (lhs.getSubKind()) {
229 default:
Mike Stump1eb44332009-09-09 15:08:12 +0000230 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +0000231 case nonloc::LocAsIntegerKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000232 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
Ted Kremeneke8391722009-06-26 00:25:05 +0000233 switch (rhs.getSubKind()) {
234 case nonloc::LocAsIntegerKind:
235 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
Mike Stump1eb44332009-09-09 15:08:12 +0000236 resultTy);
Ted Kremeneke8391722009-06-26 00:25:05 +0000237 case nonloc::ConcreteIntKind: {
238 // Transform the integer into a location and compare.
239 ASTContext& Ctx = ValMgr.getContext();
240 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
241 i.setIsUnsigned(true);
242 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
243 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
244 }
Mike Stump1eb44332009-09-09 15:08:12 +0000245 default:
Ted Kremeneke8391722009-06-26 00:25:05 +0000246 switch (op) {
247 case BinaryOperator::EQ:
248 return ValMgr.makeTruthVal(false, resultTy);
249 case BinaryOperator::NE:
250 return ValMgr.makeTruthVal(true, resultTy);
251 default:
252 // This case also handles pointer arithmetic.
253 return UnknownVal();
254 }
255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000257 case nonloc::SymExprValKind: {
Mike Stump1eb44332009-09-09 15:08:12 +0000258 // Logical not?
Ted Kremeneke8391722009-06-26 00:25:05 +0000259 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
260 return UnknownVal();
261
262 const SymExpr *symExpr =
263 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Ted Kremeneke8391722009-06-26 00:25:05 +0000265 // Only handle ($sym op constant) for now.
266 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
267 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
268 switch (opc) {
269 case BinaryOperator::LAnd:
270 case BinaryOperator::LOr:
271 assert(false && "Logical operators handled by branching logic.");
272 return UnknownVal();
273 case BinaryOperator::Assign:
274 case BinaryOperator::MulAssign:
275 case BinaryOperator::DivAssign:
276 case BinaryOperator::RemAssign:
277 case BinaryOperator::AddAssign:
278 case BinaryOperator::SubAssign:
279 case BinaryOperator::ShlAssign:
280 case BinaryOperator::ShrAssign:
281 case BinaryOperator::AndAssign:
282 case BinaryOperator::XorAssign:
283 case BinaryOperator::OrAssign:
284 case BinaryOperator::Comma:
285 assert(false && "'=' and ',' operators handled by GRExprEngine.");
286 return UnknownVal();
287 case BinaryOperator::PtrMemD:
288 case BinaryOperator::PtrMemI:
289 assert(false && "Pointer arithmetic not handled here.");
290 return UnknownVal();
291 case BinaryOperator::Mul:
292 case BinaryOperator::Div:
293 case BinaryOperator::Rem:
294 case BinaryOperator::Add:
295 case BinaryOperator::Sub:
296 case BinaryOperator::Shl:
297 case BinaryOperator::Shr:
298 case BinaryOperator::And:
299 case BinaryOperator::Xor:
300 case BinaryOperator::Or:
301 // Not handled yet.
302 return UnknownVal();
303 case BinaryOperator::LT:
304 case BinaryOperator::GT:
305 case BinaryOperator::LE:
306 case BinaryOperator::GE:
Mike Stump1eb44332009-09-09 15:08:12 +0000307 case BinaryOperator::EQ:
Ted Kremeneke8391722009-06-26 00:25:05 +0000308 case BinaryOperator::NE:
309 opc = NegateComparison(opc);
310 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
311 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
312 symIntExpr->getRHS(), resultTy);
313 }
314 }
315 }
Mike Stump1eb44332009-09-09 15:08:12 +0000316 case nonloc::ConcreteIntKind: {
Ted Kremeneke8391722009-06-26 00:25:05 +0000317 if (isa<nonloc::ConcreteInt>(rhs)) {
318 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
319 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
320 }
321 else {
322 // Swap the left and right sides and flip the operator if doing so
323 // allows us to better reason about the expression (this is a form
324 // of expression canonicalization).
325 NonLoc tmp = rhs;
326 rhs = lhs;
327 lhs = tmp;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Ted Kremeneke8391722009-06-26 00:25:05 +0000329 switch (op) {
330 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
331 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
332 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
333 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
334 case BinaryOperator::EQ:
335 case BinaryOperator::NE:
336 case BinaryOperator::Add:
337 case BinaryOperator::Mul:
338 continue;
339 default:
340 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +0000341 }
Ted Kremeneke8391722009-06-26 00:25:05 +0000342 }
343 }
344 case nonloc::SymbolValKind: {
345 if (isa<nonloc::ConcreteInt>(rhs)) {
346 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(lhs).getSymbol(), op,
347 cast<nonloc::ConcreteInt>(rhs).getValue(),
348 resultTy);
349 }
350
351 return UnknownVal();
352 }
353 }
354 }
355}
356
357SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
Mike Stump1eb44332009-09-09 15:08:12 +0000358 QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000359 switch (op) {
360 default:
361 return UnknownVal();
362 case BinaryOperator::EQ:
363 case BinaryOperator::NE:
364 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
365 }
366}
367
368SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
369 BinaryOperator::Opcode op,
Mike Stump1eb44332009-09-09 15:08:12 +0000370 Loc lhs, NonLoc rhs, QualType resultTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +0000371 // Special case: 'rhs' is an integer that has the same width as a pointer and
372 // we are using the integer location in a comparison. Normally this cannot be
373 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
374 // can generate comparisons that trigger this code.
375 // FIXME: Are all locations guaranteed to have pointer width?
376 if (BinaryOperator::isEqualityOp(op)) {
377 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
378 const llvm::APSInt *x = &rhsInt->getValue();
379 ASTContext &ctx = ValMgr.getContext();
380 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
381 // Convert the signedness of the integer (if necessary).
382 if (x->isSigned())
Mike Stump1eb44332009-09-09 15:08:12 +0000383 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
Ted Kremeneke8391722009-06-26 00:25:05 +0000384
385 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
386 }
387 }
388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Ted Kremeneke8391722009-06-26 00:25:05 +0000390 // Delegate pointer arithmetic to the StoreManager.
391 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
392 rhs, resultTy);
393}