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