blob: 7462fe65fe8fbd5e62974bbe12e40f5d93e8b172 [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
Ted Kremenek9031dd72009-07-21 00:12:07 +000051 if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) {
52 if (isLocType)
Ted Kremenekdd661142009-07-20 21:39:27 +000053 return LI->getLoc();
Ted Kremenek9031dd72009-07-21 00:12:07 +000054
55 ASTContext &Ctx = ValMgr.getContext();
56
57 // FIXME: Support promotions/truncations.
58 if (Ctx.getTypeSize(castTy) == Ctx.getTypeSize(Ctx.VoidPtrTy))
59 return val;
60
61 return UnknownVal();
62 }
63
64 if (const SymExpr *se = val.getAsSymbolicExpression()) {
65 ASTContext &Ctx = ValMgr.getContext();
66 QualType T = Ctx.getCanonicalType(se->getType(Ctx));
67 if (T == Ctx.getCanonicalType(castTy))
68 return val;
69
70 return UnknownVal();
71 }
Ted Kremenekdd661142009-07-20 21:39:27 +000072
Ted Kremeneke8391722009-06-26 00:25:05 +000073 if (!isa<nonloc::ConcreteInt>(val))
74 return UnknownVal();
Ted Kremeneke8391722009-06-26 00:25:05 +000075
76 // Only handle casts from integers to integers.
77 if (!isLocType && !castTy->isIntegerType())
78 return UnknownVal();
79
80 llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue();
81 i.setIsUnsigned(castTy->isUnsignedIntegerType() || Loc::IsLocType(castTy));
82 i.extOrTrunc(ValMgr.getContext().getTypeSize(castTy));
83
84 if (isLocType)
85 return ValMgr.makeIntLocVal(i);
86 else
87 return ValMgr.makeIntVal(i);
88}
89
Ted Kremenek46537392009-07-16 01:33:37 +000090SVal SimpleSValuator::EvalCastL(Loc val, QualType castTy) {
Ted Kremeneke8391722009-06-26 00:25:05 +000091
92 // Casts from pointers -> pointers, just return the lval.
93 //
94 // Casts from pointers -> references, just return the lval. These
95 // can be introduced by the frontend for corner cases, e.g
96 // casting from va_list* to __builtin_va_list&.
97 //
98 assert(!val.isUnknownOrUndef());
99
100 if (Loc::IsLocType(castTy) || castTy->isReferenceType())
101 return val;
102
103 // FIXME: Handle transparent unions where a value can be "transparently"
104 // lifted into a union type.
105 if (castTy->isUnionType())
106 return UnknownVal();
107
108 assert(castTy->isIntegerType());
109 unsigned BitWidth = ValMgr.getContext().getTypeSize(castTy);
110
111 if (!isa<loc::ConcreteInt>(val))
112 return ValMgr.makeLocAsInteger(val, BitWidth);
113
114 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) {
125 switch (val.getSubKind()) {
126 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
159// Equality operators for Locs.
160// 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) {
165
166 switch (lhs.getSubKind()) {
167 default:
168 assert(false && "EQ/NE not implemented for this Loc.");
169 return UnknownVal();
170
171 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;
179 }
180 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 }
192
193 case loc::GotoLabelKind:
194 break;
195 }
196
197 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) {
203
204 assert(!lhs.isUnknownOrUndef());
205 assert(!rhs.isUnknownOrUndef());
206
207 // Handle trivial case where left-side and right-side are the same.
208 if (lhs == rhs)
209 switch (op) {
210 default:
211 break;
212 case BinaryOperator::EQ:
213 case BinaryOperator::LE:
214 case BinaryOperator::GE:
215 return ValMgr.makeTruthVal(true, resultTy);
216 case BinaryOperator::LT:
217 case BinaryOperator::GT:
218 case BinaryOperator::NE:
219 return ValMgr.makeTruthVal(false, resultTy);
220 }
221
Ted Kremeneke8391722009-06-26 00:25:05 +0000222 while (1) {
223 switch (lhs.getSubKind()) {
224 default:
225 return UnknownVal();
226 case nonloc::LocAsIntegerKind: {
227 Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc();
228 switch (rhs.getSubKind()) {
229 case nonloc::LocAsIntegerKind:
230 return EvalBinOpLL(op, lhsL, cast<nonloc::LocAsInteger>(rhs).getLoc(),
231 resultTy);
232 case nonloc::ConcreteIntKind: {
233 // Transform the integer into a location and compare.
234 ASTContext& Ctx = ValMgr.getContext();
235 llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue();
236 i.setIsUnsigned(true);
237 i.extOrTrunc(Ctx.getTypeSize(Ctx.VoidPtrTy));
238 return EvalBinOpLL(op, lhsL, ValMgr.makeLoc(i), resultTy);
239 }
240 default:
241 switch (op) {
242 case BinaryOperator::EQ:
243 return ValMgr.makeTruthVal(false, resultTy);
244 case BinaryOperator::NE:
245 return ValMgr.makeTruthVal(true, resultTy);
246 default:
247 // This case also handles pointer arithmetic.
248 return UnknownVal();
249 }
250 }
251 }
252 case nonloc::SymExprValKind: {
253 // Logical not?
254 if (!(op == BinaryOperator::EQ && rhs.isZeroConstant()))
255 return UnknownVal();
256
257 const SymExpr *symExpr =
258 cast<nonloc::SymExprVal>(lhs).getSymbolicExpression();
259
260 // Only handle ($sym op constant) for now.
261 if (const SymIntExpr *symIntExpr = dyn_cast<SymIntExpr>(symExpr)) {
262 BinaryOperator::Opcode opc = symIntExpr->getOpcode();
263 switch (opc) {
264 case BinaryOperator::LAnd:
265 case BinaryOperator::LOr:
266 assert(false && "Logical operators handled by branching logic.");
267 return UnknownVal();
268 case BinaryOperator::Assign:
269 case BinaryOperator::MulAssign:
270 case BinaryOperator::DivAssign:
271 case BinaryOperator::RemAssign:
272 case BinaryOperator::AddAssign:
273 case BinaryOperator::SubAssign:
274 case BinaryOperator::ShlAssign:
275 case BinaryOperator::ShrAssign:
276 case BinaryOperator::AndAssign:
277 case BinaryOperator::XorAssign:
278 case BinaryOperator::OrAssign:
279 case BinaryOperator::Comma:
280 assert(false && "'=' and ',' operators handled by GRExprEngine.");
281 return UnknownVal();
282 case BinaryOperator::PtrMemD:
283 case BinaryOperator::PtrMemI:
284 assert(false && "Pointer arithmetic not handled here.");
285 return UnknownVal();
286 case BinaryOperator::Mul:
287 case BinaryOperator::Div:
288 case BinaryOperator::Rem:
289 case BinaryOperator::Add:
290 case BinaryOperator::Sub:
291 case BinaryOperator::Shl:
292 case BinaryOperator::Shr:
293 case BinaryOperator::And:
294 case BinaryOperator::Xor:
295 case BinaryOperator::Or:
296 // Not handled yet.
297 return UnknownVal();
298 case BinaryOperator::LT:
299 case BinaryOperator::GT:
300 case BinaryOperator::LE:
301 case BinaryOperator::GE:
302 case BinaryOperator::EQ:
303 case BinaryOperator::NE:
304 opc = NegateComparison(opc);
305 assert(symIntExpr->getType(ValMgr.getContext()) == resultTy);
306 return ValMgr.makeNonLoc(symIntExpr->getLHS(), opc,
307 symIntExpr->getRHS(), resultTy);
308 }
309 }
310 }
311 case nonloc::ConcreteIntKind: {
312 if (isa<nonloc::ConcreteInt>(rhs)) {
313 const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs);
314 return lhsInt.evalBinOp(ValMgr, op, cast<nonloc::ConcreteInt>(rhs));
315 }
316 else {
317 // Swap the left and right sides and flip the operator if doing so
318 // allows us to better reason about the expression (this is a form
319 // of expression canonicalization).
320 NonLoc tmp = rhs;
321 rhs = lhs;
322 lhs = tmp;
323
324 switch (op) {
325 case BinaryOperator::LT: op = BinaryOperator::GT; continue;
326 case BinaryOperator::GT: op = BinaryOperator::LT; continue;
327 case BinaryOperator::LE: op = BinaryOperator::GE; continue;
328 case BinaryOperator::GE: op = BinaryOperator::LE; continue;
329 case BinaryOperator::EQ:
330 case BinaryOperator::NE:
331 case BinaryOperator::Add:
332 case BinaryOperator::Mul:
333 continue;
334 default:
335 return UnknownVal();
336 }
337 }
338 }
339 case nonloc::SymbolValKind: {
340 if (isa<nonloc::ConcreteInt>(rhs)) {
341 return ValMgr.makeNonLoc(cast<nonloc::SymbolVal>(lhs).getSymbol(), op,
342 cast<nonloc::ConcreteInt>(rhs).getValue(),
343 resultTy);
344 }
345
346 return UnknownVal();
347 }
348 }
349 }
350}
351
352SVal SimpleSValuator::EvalBinOpLL(BinaryOperator::Opcode op, Loc lhs, Loc rhs,
353 QualType resultTy) {
354 switch (op) {
355 default:
356 return UnknownVal();
357 case BinaryOperator::EQ:
358 case BinaryOperator::NE:
359 return EvalEquality(ValMgr, lhs, rhs, op == BinaryOperator::EQ, resultTy);
360 }
361}
362
363SVal SimpleSValuator::EvalBinOpLN(const GRState *state,
364 BinaryOperator::Opcode op,
365 Loc lhs, NonLoc rhs, QualType resultTy) {
366 // Special case: 'rhs' is an integer that has the same width as a pointer and
367 // we are using the integer location in a comparison. Normally this cannot be
368 // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32
369 // can generate comparisons that trigger this code.
370 // FIXME: Are all locations guaranteed to have pointer width?
371 if (BinaryOperator::isEqualityOp(op)) {
372 if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) {
373 const llvm::APSInt *x = &rhsInt->getValue();
374 ASTContext &ctx = ValMgr.getContext();
375 if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) {
376 // Convert the signedness of the integer (if necessary).
377 if (x->isSigned())
378 x = &ValMgr.getBasicValueFactory().getValue(*x, true);
379
380 return EvalBinOpLL(op, lhs, loc::ConcreteInt(*x), resultTy);
381 }
382 }
383 }
384
385 // Delegate pointer arithmetic to the StoreManager.
386 return state->getStateManager().getStoreManager().EvalBinOp(state, op, lhs,
387 rhs, resultTy);
388}