blob: 8bc95941c0159525ef95ebb1fac8acab341b2f80 [file] [log] [blame]
Ted Kremenek846eabd2010-12-01 21:28:31 +00001// SValBuilder.cpp - Basic class for all SValBuilder implementations -*- C++ -*-
Ted Kremenek32c3fa42009-07-21 21:03:30 +00002//
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//
Ted Kremenek846eabd2010-12-01 21:28:31 +000010// This file defines SValBuilder, the base class for all (complete) SValBuilder
Ted Kremenek32c3fa42009-07-21 21:03:30 +000011// implementations.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek846eabd2010-12-01 21:28:31 +000015#include "clang/Checker/PathSensitive/SValBuilder.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRState.h"
Ted Kremenek32c3fa42009-07-21 21:03:30 +000017
18using namespace clang;
19
Ted Kremenekff4264d2009-08-25 18:44:25 +000020
Ted Kremenek846eabd2010-12-01 21:28:31 +000021SVal SValBuilder::EvalBinOp(const GRState *ST, BinaryOperator::Opcode Op,
Ted Kremenekff4264d2009-08-25 18:44:25 +000022 SVal L, SVal R, QualType T) {
23
24 if (L.isUndef() || R.isUndef())
25 return UndefinedVal();
Mike Stump1eb44332009-09-09 15:08:12 +000026
Ted Kremenekff4264d2009-08-25 18:44:25 +000027 if (L.isUnknown() || R.isUnknown())
28 return UnknownVal();
Mike Stump1eb44332009-09-09 15:08:12 +000029
Ted Kremenekff4264d2009-08-25 18:44:25 +000030 if (isa<Loc>(L)) {
31 if (isa<Loc>(R))
Jordy Roseeac4a002010-06-28 08:26:15 +000032 return EvalBinOpLL(ST, Op, cast<Loc>(L), cast<Loc>(R), T);
Ted Kremenekff4264d2009-08-25 18:44:25 +000033
34 return EvalBinOpLN(ST, Op, cast<Loc>(L), cast<NonLoc>(R), T);
35 }
Mike Stump1eb44332009-09-09 15:08:12 +000036
Ted Kremenekff4264d2009-08-25 18:44:25 +000037 if (isa<Loc>(R)) {
Jordy Roseeac4a002010-06-28 08:26:15 +000038 // Support pointer arithmetic where the addend is on the left
39 // and the pointer on the right.
John McCall2de56d12010-08-25 11:45:40 +000040 assert(Op == BO_Add);
Mike Stump1eb44332009-09-09 15:08:12 +000041
Ted Kremenekff4264d2009-08-25 18:44:25 +000042 // Commute the operands.
43 return EvalBinOpLN(ST, Op, cast<Loc>(R), cast<NonLoc>(L), T);
44 }
45
Ted Kremenekcd8f6ac2009-10-06 01:39:48 +000046 return EvalBinOpNN(ST, Op, cast<NonLoc>(L), cast<NonLoc>(R), T);
Ted Kremenekff4264d2009-08-25 18:44:25 +000047}
48
Ted Kremenek846eabd2010-12-01 21:28:31 +000049DefinedOrUnknownSVal SValBuilder::EvalEQ(const GRState *ST,
Ted Kremenek5b9bd212009-09-11 22:07:28 +000050 DefinedOrUnknownSVal L,
51 DefinedOrUnknownSVal R) {
John McCall2de56d12010-08-25 11:45:40 +000052 return cast<DefinedOrUnknownSVal>(EvalBinOp(ST, BO_EQ, L, R,
Ted Kremenek5b9bd212009-09-11 22:07:28 +000053 ValMgr.getContext().IntTy));
54}
55
Zhongxing Xudc1ad2c2010-11-26 07:15:40 +000056// FIXME: should rewrite according to the cast kind.
Ted Kremenek846eabd2010-12-01 21:28:31 +000057SVal SValBuilder::EvalCast(SVal val, QualType castTy, QualType originalTy) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +000058 if (val.isUnknownOrUndef() || castTy == originalTy)
Zhongxing Xu814e6b92010-02-04 04:56:43 +000059 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000060
Ted Kremenek32c3fa42009-07-21 21:03:30 +000061 ASTContext &C = ValMgr.getContext();
Mike Stump1eb44332009-09-09 15:08:12 +000062
Ted Kremenek32c3fa42009-07-21 21:03:30 +000063 // For const casts, just propagate the value.
Zhongxing Xu5ea95fc2010-01-05 09:27:03 +000064 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType())
65 if (C.hasSameUnqualifiedType(castTy, originalTy))
Zhongxing Xu814e6b92010-02-04 04:56:43 +000066 return val;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Ted Kremenekf6817042010-02-02 21:11:40 +000068 // Check for casts to real or complex numbers. We don't handle these at all
69 // right now.
70 if (castTy->isFloatingType() || castTy->isAnyComplexType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +000071 return UnknownVal();
Ted Kremenekf6817042010-02-02 21:11:40 +000072
73 // Check for casts from integers to integers.
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000074 if (castTy->isIntegerType() && originalTy->isIntegerType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +000075 return EvalCastNL(cast<NonLoc>(val), castTy);
Zhongxing Xu7b81e8f2010-01-14 03:45:06 +000076
Ted Kremenek32c3fa42009-07-21 21:03:30 +000077 // Check for casts from pointers to integers.
78 if (castTy->isIntegerType() && Loc::IsLocType(originalTy))
Zhongxing Xu814e6b92010-02-04 04:56:43 +000079 return EvalCastL(cast<Loc>(val), castTy);
Mike Stump1eb44332009-09-09 15:08:12 +000080
Ted Kremenek32c3fa42009-07-21 21:03:30 +000081 // Check for casts from integers to pointers.
82 if (Loc::IsLocType(castTy) && originalTy->isIntegerType()) {
83 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) {
Ted Kremenek5bbc8e72009-12-23 02:52:14 +000084 if (const MemRegion *R = LV->getLoc().getAsRegion()) {
85 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
86 R = storeMgr.CastRegion(R, castTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +000087 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
Ted Kremenek5bbc8e72009-12-23 02:52:14 +000088 }
Zhongxing Xu814e6b92010-02-04 04:56:43 +000089 return LV->getLoc();
Ted Kremenek32c3fa42009-07-21 21:03:30 +000090 }
Ted Kremenek32c3fa42009-07-21 21:03:30 +000091 goto DispatchCast;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Ted Kremenek32c3fa42009-07-21 21:03:30 +000094 // Just pass through function and block pointers.
95 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) {
96 assert(Loc::IsLocType(castTy));
Zhongxing Xu814e6b92010-02-04 04:56:43 +000097 return val;
Ted Kremenek32c3fa42009-07-21 21:03:30 +000098 }
Mike Stump1eb44332009-09-09 15:08:12 +000099
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000100 // Check for casts from array type to another type.
101 if (originalTy->isArrayType()) {
102 // We will always decay to a pointer.
103 val = ValMgr.getStateManager().ArrayToPointer(cast<Loc>(val));
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000105 // Are we casting from an array to a pointer? If so just pass on
106 // the decayed value.
107 if (castTy->isPointerType())
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000108 return val;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000110 // Are we casting from an array to an integer? If so, cast the decayed
111 // pointer value to an integer.
112 assert(castTy->isIntegerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000114 // FIXME: Keep these here for now in case we decide soon that we
115 // need the original decayed type.
116 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
117 // QualType pointerTy = C.getPointerType(elemTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000118 return EvalCastL(cast<Loc>(val), castTy);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000119 }
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000121 // Check for casts from a region to a specific type.
122 if (const MemRegion *R = val.getAsRegion()) {
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000123 // FIXME: We should handle the case where we strip off view layers to get
124 // to a desugared type.
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Ted Kremenek948163b2010-11-15 20:09:42 +0000126 if (!Loc::IsLocType(castTy)) {
127 // FIXME: There can be gross cases where one casts the result of a function
128 // (that returns a pointer) to some other value that happens to fit
129 // within that pointer value. We currently have no good way to
130 // model such operations. When this happens, the underlying operation
131 // is that the caller is reasoning about bits. Conceptually we are
132 // layering a "view" of a location on top of those bits. Perhaps
133 // we need to be more lazy about mutual possible views, even on an
134 // SVal? This may be necessary for bit-level reasoning as well.
135 return UnknownVal();
136 }
137
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000138 // We get a symbolic function pointer for a dereference of a function
139 // pointer, but it is of function type. Example:
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000141 // struct FPRec {
Mike Stump1eb44332009-09-09 15:08:12 +0000142 // void (*my_func)(int * x);
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000143 // };
144 //
145 // int bar(int x);
146 //
147 // int f1_a(struct FPRec* foo) {
148 // int x;
149 // (*foo->my_func)(&x);
150 // return bar(x)+1; // no-warning
151 // }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000153 assert(Loc::IsLocType(originalTy) || originalTy->isFunctionType() ||
154 originalTy->isBlockPointerType());
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000156 StoreManager &storeMgr = ValMgr.getStateManager().getStoreManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Zhongxing Xu09270cc2009-10-14 06:55:01 +0000158 // Delegate to store manager to get the result of casting a region to a
159 // different type. If the MemRegion* returned is NULL, this expression
160 // evaluates to UnknownVal.
161 R = storeMgr.CastRegion(R, castTy);
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000162 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal();
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000163 }
Mike Stump1eb44332009-09-09 15:08:12 +0000164
Ted Kremenek32c3fa42009-07-21 21:03:30 +0000165DispatchCast:
Ted Kremenek5bbc8e72009-12-23 02:52:14 +0000166 // All other cases.
Zhongxing Xu814e6b92010-02-04 04:56:43 +0000167 return isa<Loc>(val) ? EvalCastL(cast<Loc>(val), castTy)
168 : EvalCastNL(cast<NonLoc>(val), castTy);
Ted Kremenek5b9bd212009-09-11 22:07:28 +0000169}