1e5dd7070Spatrick //=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This file defines ExprEngine's support for C expressions.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12e5dd7070Spatrick
13e5dd7070Spatrick #include "clang/AST/ExprCXX.h"
14e5dd7070Spatrick #include "clang/AST/DeclCXX.h"
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
16e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
17*12c85518Srobert #include <optional>
18e5dd7070Spatrick
19e5dd7070Spatrick using namespace clang;
20e5dd7070Spatrick using namespace ento;
21e5dd7070Spatrick using llvm::APSInt;
22e5dd7070Spatrick
23e5dd7070Spatrick /// Optionally conjure and return a symbol for offset when processing
24e5dd7070Spatrick /// an expression \p Expression.
25e5dd7070Spatrick /// If \p Other is a location, conjure a symbol for \p Symbol
26e5dd7070Spatrick /// (offset) if it is unknown so that memory arithmetic always
27e5dd7070Spatrick /// results in an ElementRegion.
28e5dd7070Spatrick /// \p Count The number of times the current basic block was visited.
conjureOffsetSymbolOnLocation(SVal Symbol,SVal Other,Expr * Expression,SValBuilder & svalBuilder,unsigned Count,const LocationContext * LCtx)29e5dd7070Spatrick static SVal conjureOffsetSymbolOnLocation(
30e5dd7070Spatrick SVal Symbol, SVal Other, Expr* Expression, SValBuilder &svalBuilder,
31e5dd7070Spatrick unsigned Count, const LocationContext *LCtx) {
32e5dd7070Spatrick QualType Ty = Expression->getType();
33*12c85518Srobert if (isa<Loc>(Other) && Ty->isIntegralOrEnumerationType() &&
34e5dd7070Spatrick Symbol.isUnknown()) {
35e5dd7070Spatrick return svalBuilder.conjureSymbolVal(Expression, LCtx, Ty, Count);
36e5dd7070Spatrick }
37e5dd7070Spatrick return Symbol;
38e5dd7070Spatrick }
39e5dd7070Spatrick
VisitBinaryOperator(const BinaryOperator * B,ExplodedNode * Pred,ExplodedNodeSet & Dst)40e5dd7070Spatrick void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
41e5dd7070Spatrick ExplodedNode *Pred,
42e5dd7070Spatrick ExplodedNodeSet &Dst) {
43e5dd7070Spatrick
44e5dd7070Spatrick Expr *LHS = B->getLHS()->IgnoreParens();
45e5dd7070Spatrick Expr *RHS = B->getRHS()->IgnoreParens();
46e5dd7070Spatrick
47e5dd7070Spatrick // FIXME: Prechecks eventually go in ::Visit().
48e5dd7070Spatrick ExplodedNodeSet CheckedSet;
49e5dd7070Spatrick ExplodedNodeSet Tmp2;
50e5dd7070Spatrick getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this);
51e5dd7070Spatrick
52e5dd7070Spatrick // With both the LHS and RHS evaluated, process the operation itself.
53e5dd7070Spatrick for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end();
54e5dd7070Spatrick it != ei; ++it) {
55e5dd7070Spatrick
56e5dd7070Spatrick ProgramStateRef state = (*it)->getState();
57e5dd7070Spatrick const LocationContext *LCtx = (*it)->getLocationContext();
58e5dd7070Spatrick SVal LeftV = state->getSVal(LHS, LCtx);
59e5dd7070Spatrick SVal RightV = state->getSVal(RHS, LCtx);
60e5dd7070Spatrick
61e5dd7070Spatrick BinaryOperator::Opcode Op = B->getOpcode();
62e5dd7070Spatrick
63e5dd7070Spatrick if (Op == BO_Assign) {
64e5dd7070Spatrick // EXPERIMENTAL: "Conjured" symbols.
65e5dd7070Spatrick // FIXME: Handle structs.
66e5dd7070Spatrick if (RightV.isUnknown()) {
67e5dd7070Spatrick unsigned Count = currBldrCtx->blockCount();
68e5dd7070Spatrick RightV = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx,
69e5dd7070Spatrick Count);
70e5dd7070Spatrick }
71e5dd7070Spatrick // Simulate the effects of a "store": bind the value of the RHS
72e5dd7070Spatrick // to the L-Value represented by the LHS.
73e5dd7070Spatrick SVal ExprVal = B->isGLValue() ? LeftV : RightV;
74e5dd7070Spatrick evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal),
75e5dd7070Spatrick LeftV, RightV);
76e5dd7070Spatrick continue;
77e5dd7070Spatrick }
78e5dd7070Spatrick
79e5dd7070Spatrick if (!B->isAssignmentOp()) {
80e5dd7070Spatrick StmtNodeBuilder Bldr(*it, Tmp2, *currBldrCtx);
81e5dd7070Spatrick
82e5dd7070Spatrick if (B->isAdditiveOp()) {
83e5dd7070Spatrick // TODO: This can be removed after we enable history tracking with
84e5dd7070Spatrick // SymSymExpr.
85e5dd7070Spatrick unsigned Count = currBldrCtx->blockCount();
86e5dd7070Spatrick RightV = conjureOffsetSymbolOnLocation(
87e5dd7070Spatrick RightV, LeftV, RHS, svalBuilder, Count, LCtx);
88e5dd7070Spatrick LeftV = conjureOffsetSymbolOnLocation(
89e5dd7070Spatrick LeftV, RightV, LHS, svalBuilder, Count, LCtx);
90e5dd7070Spatrick }
91e5dd7070Spatrick
92e5dd7070Spatrick // Although we don't yet model pointers-to-members, we do need to make
93e5dd7070Spatrick // sure that the members of temporaries have a valid 'this' pointer for
94e5dd7070Spatrick // other checks.
95e5dd7070Spatrick if (B->getOpcode() == BO_PtrMemD)
96e5dd7070Spatrick state = createTemporaryRegionIfNeeded(state, LCtx, LHS);
97e5dd7070Spatrick
98e5dd7070Spatrick // Process non-assignments except commas or short-circuited
99e5dd7070Spatrick // logical expressions (LAnd and LOr).
100e5dd7070Spatrick SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
101e5dd7070Spatrick if (!Result.isUnknown()) {
102e5dd7070Spatrick state = state->BindExpr(B, LCtx, Result);
103e5dd7070Spatrick } else {
104e5dd7070Spatrick // If we cannot evaluate the operation escape the operands.
105e5dd7070Spatrick state = escapeValues(state, LeftV, PSK_EscapeOther);
106e5dd7070Spatrick state = escapeValues(state, RightV, PSK_EscapeOther);
107e5dd7070Spatrick }
108e5dd7070Spatrick
109e5dd7070Spatrick Bldr.generateNode(B, *it, state);
110e5dd7070Spatrick continue;
111e5dd7070Spatrick }
112e5dd7070Spatrick
113e5dd7070Spatrick assert (B->isCompoundAssignmentOp());
114e5dd7070Spatrick
115e5dd7070Spatrick switch (Op) {
116e5dd7070Spatrick default:
117e5dd7070Spatrick llvm_unreachable("Invalid opcode for compound assignment.");
118e5dd7070Spatrick case BO_MulAssign: Op = BO_Mul; break;
119e5dd7070Spatrick case BO_DivAssign: Op = BO_Div; break;
120e5dd7070Spatrick case BO_RemAssign: Op = BO_Rem; break;
121e5dd7070Spatrick case BO_AddAssign: Op = BO_Add; break;
122e5dd7070Spatrick case BO_SubAssign: Op = BO_Sub; break;
123e5dd7070Spatrick case BO_ShlAssign: Op = BO_Shl; break;
124e5dd7070Spatrick case BO_ShrAssign: Op = BO_Shr; break;
125e5dd7070Spatrick case BO_AndAssign: Op = BO_And; break;
126e5dd7070Spatrick case BO_XorAssign: Op = BO_Xor; break;
127e5dd7070Spatrick case BO_OrAssign: Op = BO_Or; break;
128e5dd7070Spatrick }
129e5dd7070Spatrick
130e5dd7070Spatrick // Perform a load (the LHS). This performs the checks for
131e5dd7070Spatrick // null dereferences, and so on.
132e5dd7070Spatrick ExplodedNodeSet Tmp;
133e5dd7070Spatrick SVal location = LeftV;
134e5dd7070Spatrick evalLoad(Tmp, B, LHS, *it, state, location);
135e5dd7070Spatrick
136e5dd7070Spatrick for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
137e5dd7070Spatrick ++I) {
138e5dd7070Spatrick
139e5dd7070Spatrick state = (*I)->getState();
140e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
141e5dd7070Spatrick SVal V = state->getSVal(LHS, LCtx);
142e5dd7070Spatrick
143e5dd7070Spatrick // Get the computation type.
144e5dd7070Spatrick QualType CTy =
145e5dd7070Spatrick cast<CompoundAssignOperator>(B)->getComputationResultType();
146e5dd7070Spatrick CTy = getContext().getCanonicalType(CTy);
147e5dd7070Spatrick
148e5dd7070Spatrick QualType CLHSTy =
149e5dd7070Spatrick cast<CompoundAssignOperator>(B)->getComputationLHSType();
150e5dd7070Spatrick CLHSTy = getContext().getCanonicalType(CLHSTy);
151e5dd7070Spatrick
152e5dd7070Spatrick QualType LTy = getContext().getCanonicalType(LHS->getType());
153e5dd7070Spatrick
154e5dd7070Spatrick // Promote LHS.
155e5dd7070Spatrick V = svalBuilder.evalCast(V, CLHSTy, LTy);
156e5dd7070Spatrick
157e5dd7070Spatrick // Compute the result of the operation.
158e5dd7070Spatrick SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
159e5dd7070Spatrick B->getType(), CTy);
160e5dd7070Spatrick
161e5dd7070Spatrick // EXPERIMENTAL: "Conjured" symbols.
162e5dd7070Spatrick // FIXME: Handle structs.
163e5dd7070Spatrick
164e5dd7070Spatrick SVal LHSVal;
165e5dd7070Spatrick
166e5dd7070Spatrick if (Result.isUnknown()) {
167e5dd7070Spatrick // The symbolic value is actually for the type of the left-hand side
168e5dd7070Spatrick // expression, not the computation type, as this is the value the
169e5dd7070Spatrick // LValue on the LHS will bind to.
170e5dd7070Spatrick LHSVal = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx, LTy,
171e5dd7070Spatrick currBldrCtx->blockCount());
172e5dd7070Spatrick // However, we need to convert the symbol to the computation type.
173e5dd7070Spatrick Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
174e5dd7070Spatrick }
175e5dd7070Spatrick else {
176e5dd7070Spatrick // The left-hand side may bind to a different value then the
177e5dd7070Spatrick // computation type.
178e5dd7070Spatrick LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
179e5dd7070Spatrick }
180e5dd7070Spatrick
181e5dd7070Spatrick // In C++, assignment and compound assignment operators return an
182e5dd7070Spatrick // lvalue.
183e5dd7070Spatrick if (B->isGLValue())
184e5dd7070Spatrick state = state->BindExpr(B, LCtx, location);
185e5dd7070Spatrick else
186e5dd7070Spatrick state = state->BindExpr(B, LCtx, Result);
187e5dd7070Spatrick
188e5dd7070Spatrick evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
189e5dd7070Spatrick }
190e5dd7070Spatrick }
191e5dd7070Spatrick
192e5dd7070Spatrick // FIXME: postvisits eventually go in ::Visit()
193e5dd7070Spatrick getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
194e5dd7070Spatrick }
195e5dd7070Spatrick
VisitBlockExpr(const BlockExpr * BE,ExplodedNode * Pred,ExplodedNodeSet & Dst)196e5dd7070Spatrick void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
197e5dd7070Spatrick ExplodedNodeSet &Dst) {
198e5dd7070Spatrick
199e5dd7070Spatrick CanQualType T = getContext().getCanonicalType(BE->getType());
200e5dd7070Spatrick
201e5dd7070Spatrick const BlockDecl *BD = BE->getBlockDecl();
202e5dd7070Spatrick // Get the value of the block itself.
203e5dd7070Spatrick SVal V = svalBuilder.getBlockPointer(BD, T,
204e5dd7070Spatrick Pred->getLocationContext(),
205e5dd7070Spatrick currBldrCtx->blockCount());
206e5dd7070Spatrick
207e5dd7070Spatrick ProgramStateRef State = Pred->getState();
208e5dd7070Spatrick
209e5dd7070Spatrick // If we created a new MemRegion for the block, we should explicitly bind
210e5dd7070Spatrick // the captured variables.
211e5dd7070Spatrick if (const BlockDataRegion *BDR =
212e5dd7070Spatrick dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
213e5dd7070Spatrick
214e5dd7070Spatrick BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
215e5dd7070Spatrick E = BDR->referenced_vars_end();
216e5dd7070Spatrick
217e5dd7070Spatrick auto CI = BD->capture_begin();
218e5dd7070Spatrick auto CE = BD->capture_end();
219e5dd7070Spatrick for (; I != E; ++I) {
220e5dd7070Spatrick const VarRegion *capturedR = I.getCapturedRegion();
221ec727ea7Spatrick const TypedValueRegion *originalR = I.getOriginalRegion();
222e5dd7070Spatrick
223e5dd7070Spatrick // If the capture had a copy expression, use the result of evaluating
224e5dd7070Spatrick // that expression, otherwise use the original value.
225e5dd7070Spatrick // We rely on the invariant that the block declaration's capture variables
226e5dd7070Spatrick // are a prefix of the BlockDataRegion's referenced vars (which may include
227e5dd7070Spatrick // referenced globals, etc.) to enable fast lookup of the capture for a
228e5dd7070Spatrick // given referenced var.
229e5dd7070Spatrick const Expr *copyExpr = nullptr;
230e5dd7070Spatrick if (CI != CE) {
231e5dd7070Spatrick assert(CI->getVariable() == capturedR->getDecl());
232e5dd7070Spatrick copyExpr = CI->getCopyExpr();
233e5dd7070Spatrick CI++;
234e5dd7070Spatrick }
235e5dd7070Spatrick
236e5dd7070Spatrick if (capturedR != originalR) {
237e5dd7070Spatrick SVal originalV;
238e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
239e5dd7070Spatrick if (copyExpr) {
240e5dd7070Spatrick originalV = State->getSVal(copyExpr, LCtx);
241e5dd7070Spatrick } else {
242e5dd7070Spatrick originalV = State->getSVal(loc::MemRegionVal(originalR));
243e5dd7070Spatrick }
244e5dd7070Spatrick State = State->bindLoc(loc::MemRegionVal(capturedR), originalV, LCtx);
245e5dd7070Spatrick }
246e5dd7070Spatrick }
247e5dd7070Spatrick }
248e5dd7070Spatrick
249e5dd7070Spatrick ExplodedNodeSet Tmp;
250e5dd7070Spatrick StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
251e5dd7070Spatrick Bldr.generateNode(BE, Pred,
252e5dd7070Spatrick State->BindExpr(BE, Pred->getLocationContext(), V),
253e5dd7070Spatrick nullptr, ProgramPoint::PostLValueKind);
254e5dd7070Spatrick
255e5dd7070Spatrick // FIXME: Move all post/pre visits to ::Visit().
256e5dd7070Spatrick getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
257e5dd7070Spatrick }
258e5dd7070Spatrick
handleLValueBitCast(ProgramStateRef state,const Expr * Ex,const LocationContext * LCtx,QualType T,QualType ExTy,const CastExpr * CastE,StmtNodeBuilder & Bldr,ExplodedNode * Pred)259e5dd7070Spatrick ProgramStateRef ExprEngine::handleLValueBitCast(
260e5dd7070Spatrick ProgramStateRef state, const Expr* Ex, const LocationContext* LCtx,
261e5dd7070Spatrick QualType T, QualType ExTy, const CastExpr* CastE, StmtNodeBuilder& Bldr,
262e5dd7070Spatrick ExplodedNode* Pred) {
263e5dd7070Spatrick if (T->isLValueReferenceType()) {
264e5dd7070Spatrick assert(!CastE->getType()->isLValueReferenceType());
265e5dd7070Spatrick ExTy = getContext().getLValueReferenceType(ExTy);
266e5dd7070Spatrick } else if (T->isRValueReferenceType()) {
267e5dd7070Spatrick assert(!CastE->getType()->isRValueReferenceType());
268e5dd7070Spatrick ExTy = getContext().getRValueReferenceType(ExTy);
269e5dd7070Spatrick }
270e5dd7070Spatrick // Delegate to SValBuilder to process.
271e5dd7070Spatrick SVal OrigV = state->getSVal(Ex, LCtx);
272e5dd7070Spatrick SVal V = svalBuilder.evalCast(OrigV, T, ExTy);
273e5dd7070Spatrick // Negate the result if we're treating the boolean as a signed i1
274*12c85518Srobert if (CastE->getCastKind() == CK_BooleanToSignedIntegral && V.isValid())
275*12c85518Srobert V = svalBuilder.evalMinus(V.castAs<NonLoc>());
276*12c85518Srobert
277e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
278e5dd7070Spatrick if (V.isUnknown() && !OrigV.isUnknown()) {
279e5dd7070Spatrick state = escapeValues(state, OrigV, PSK_EscapeOther);
280e5dd7070Spatrick }
281e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
282e5dd7070Spatrick
283e5dd7070Spatrick return state;
284e5dd7070Spatrick }
285e5dd7070Spatrick
VisitCast(const CastExpr * CastE,const Expr * Ex,ExplodedNode * Pred,ExplodedNodeSet & Dst)286e5dd7070Spatrick void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
287e5dd7070Spatrick ExplodedNode *Pred, ExplodedNodeSet &Dst) {
288e5dd7070Spatrick
289e5dd7070Spatrick ExplodedNodeSet dstPreStmt;
290e5dd7070Spatrick getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
291e5dd7070Spatrick
292a9ac8606Spatrick if (CastE->getCastKind() == CK_LValueToRValue ||
293a9ac8606Spatrick CastE->getCastKind() == CK_LValueToRValueBitCast) {
294e5dd7070Spatrick for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
295e5dd7070Spatrick I!=E; ++I) {
296e5dd7070Spatrick ExplodedNode *subExprNode = *I;
297e5dd7070Spatrick ProgramStateRef state = subExprNode->getState();
298e5dd7070Spatrick const LocationContext *LCtx = subExprNode->getLocationContext();
299e5dd7070Spatrick evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
300e5dd7070Spatrick }
301e5dd7070Spatrick return;
302e5dd7070Spatrick }
303e5dd7070Spatrick
304e5dd7070Spatrick // All other casts.
305e5dd7070Spatrick QualType T = CastE->getType();
306e5dd7070Spatrick QualType ExTy = Ex->getType();
307e5dd7070Spatrick
308e5dd7070Spatrick if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
309e5dd7070Spatrick T = ExCast->getTypeAsWritten();
310e5dd7070Spatrick
311e5dd7070Spatrick StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx);
312e5dd7070Spatrick for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
313e5dd7070Spatrick I != E; ++I) {
314e5dd7070Spatrick
315e5dd7070Spatrick Pred = *I;
316e5dd7070Spatrick ProgramStateRef state = Pred->getState();
317e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
318e5dd7070Spatrick
319e5dd7070Spatrick switch (CastE->getCastKind()) {
320e5dd7070Spatrick case CK_LValueToRValue:
321a9ac8606Spatrick case CK_LValueToRValueBitCast:
322e5dd7070Spatrick llvm_unreachable("LValueToRValue casts handled earlier.");
323e5dd7070Spatrick case CK_ToVoid:
324e5dd7070Spatrick continue;
325e5dd7070Spatrick // The analyzer doesn't do anything special with these casts,
326e5dd7070Spatrick // since it understands retain/release semantics already.
327e5dd7070Spatrick case CK_ARCProduceObject:
328e5dd7070Spatrick case CK_ARCConsumeObject:
329e5dd7070Spatrick case CK_ARCReclaimReturnedObject:
330e5dd7070Spatrick case CK_ARCExtendBlockObject: // Fall-through.
331e5dd7070Spatrick case CK_CopyAndAutoreleaseBlockObject:
332e5dd7070Spatrick // The analyser can ignore atomic casts for now, although some future
333e5dd7070Spatrick // checkers may want to make certain that you're not modifying the same
334e5dd7070Spatrick // value through atomic and nonatomic pointers.
335e5dd7070Spatrick case CK_AtomicToNonAtomic:
336e5dd7070Spatrick case CK_NonAtomicToAtomic:
337e5dd7070Spatrick // True no-ops.
338e5dd7070Spatrick case CK_NoOp:
339e5dd7070Spatrick case CK_ConstructorConversion:
340e5dd7070Spatrick case CK_UserDefinedConversion:
341e5dd7070Spatrick case CK_FunctionToPointerDecay:
342e5dd7070Spatrick case CK_BuiltinFnToFnPtr: {
343e5dd7070Spatrick // Copy the SVal of Ex to CastE.
344e5dd7070Spatrick ProgramStateRef state = Pred->getState();
345e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
346e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
347e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
348e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
349e5dd7070Spatrick continue;
350e5dd7070Spatrick }
351e5dd7070Spatrick case CK_MemberPointerToBoolean:
352e5dd7070Spatrick case CK_PointerToBoolean: {
353e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
354e5dd7070Spatrick auto PTMSV = V.getAs<nonloc::PointerToMember>();
355e5dd7070Spatrick if (PTMSV)
356e5dd7070Spatrick V = svalBuilder.makeTruthVal(!PTMSV->isNullMemberPointer(), ExTy);
357e5dd7070Spatrick if (V.isUndef() || PTMSV) {
358e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
359e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
360e5dd7070Spatrick continue;
361e5dd7070Spatrick }
362e5dd7070Spatrick // Explicitly proceed with default handler for this case cascade.
363e5dd7070Spatrick state =
364e5dd7070Spatrick handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
365e5dd7070Spatrick continue;
366e5dd7070Spatrick }
367e5dd7070Spatrick case CK_Dependent:
368e5dd7070Spatrick case CK_ArrayToPointerDecay:
369e5dd7070Spatrick case CK_BitCast:
370e5dd7070Spatrick case CK_AddressSpaceConversion:
371e5dd7070Spatrick case CK_BooleanToSignedIntegral:
372e5dd7070Spatrick case CK_IntegralToPointer:
373e5dd7070Spatrick case CK_PointerToIntegral: {
374e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
375*12c85518Srobert if (isa<nonloc::PointerToMember>(V)) {
376e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, UnknownVal());
377e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
378e5dd7070Spatrick continue;
379e5dd7070Spatrick }
380e5dd7070Spatrick // Explicitly proceed with default handler for this case cascade.
381e5dd7070Spatrick state =
382e5dd7070Spatrick handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
383e5dd7070Spatrick continue;
384e5dd7070Spatrick }
385e5dd7070Spatrick case CK_IntegralToBoolean:
386e5dd7070Spatrick case CK_IntegralToFloating:
387e5dd7070Spatrick case CK_FloatingToIntegral:
388e5dd7070Spatrick case CK_FloatingToBoolean:
389e5dd7070Spatrick case CK_FloatingCast:
390e5dd7070Spatrick case CK_FloatingRealToComplex:
391e5dd7070Spatrick case CK_FloatingComplexToReal:
392e5dd7070Spatrick case CK_FloatingComplexToBoolean:
393e5dd7070Spatrick case CK_FloatingComplexCast:
394e5dd7070Spatrick case CK_FloatingComplexToIntegralComplex:
395e5dd7070Spatrick case CK_IntegralRealToComplex:
396e5dd7070Spatrick case CK_IntegralComplexToReal:
397e5dd7070Spatrick case CK_IntegralComplexToBoolean:
398e5dd7070Spatrick case CK_IntegralComplexCast:
399e5dd7070Spatrick case CK_IntegralComplexToFloatingComplex:
400e5dd7070Spatrick case CK_CPointerToObjCPointerCast:
401e5dd7070Spatrick case CK_BlockPointerToObjCPointerCast:
402e5dd7070Spatrick case CK_AnyPointerToBlockPointerCast:
403e5dd7070Spatrick case CK_ObjCObjectLValueCast:
404e5dd7070Spatrick case CK_ZeroToOCLOpaqueType:
405e5dd7070Spatrick case CK_IntToOCLSampler:
406e5dd7070Spatrick case CK_LValueBitCast:
407a9ac8606Spatrick case CK_FloatingToFixedPoint:
408a9ac8606Spatrick case CK_FixedPointToFloating:
409e5dd7070Spatrick case CK_FixedPointCast:
410e5dd7070Spatrick case CK_FixedPointToBoolean:
411e5dd7070Spatrick case CK_FixedPointToIntegral:
412e5dd7070Spatrick case CK_IntegralToFixedPoint: {
413e5dd7070Spatrick state =
414e5dd7070Spatrick handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
415e5dd7070Spatrick continue;
416e5dd7070Spatrick }
417e5dd7070Spatrick case CK_IntegralCast: {
418e5dd7070Spatrick // Delegate to SValBuilder to process.
419e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
420*12c85518Srobert if (AMgr.options.ShouldSupportSymbolicIntegerCasts)
421*12c85518Srobert V = svalBuilder.evalCast(V, T, ExTy);
422*12c85518Srobert else
423e5dd7070Spatrick V = svalBuilder.evalIntegralCast(state, V, T, ExTy);
424e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
425e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
426e5dd7070Spatrick continue;
427e5dd7070Spatrick }
428e5dd7070Spatrick case CK_DerivedToBase:
429e5dd7070Spatrick case CK_UncheckedDerivedToBase: {
430e5dd7070Spatrick // For DerivedToBase cast, delegate to the store manager.
431e5dd7070Spatrick SVal val = state->getSVal(Ex, LCtx);
432e5dd7070Spatrick val = getStoreManager().evalDerivedToBase(val, CastE);
433e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, val);
434e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
435e5dd7070Spatrick continue;
436e5dd7070Spatrick }
437e5dd7070Spatrick // Handle C++ dyn_cast.
438e5dd7070Spatrick case CK_Dynamic: {
439e5dd7070Spatrick SVal val = state->getSVal(Ex, LCtx);
440e5dd7070Spatrick
441e5dd7070Spatrick // Compute the type of the result.
442e5dd7070Spatrick QualType resultType = CastE->getType();
443e5dd7070Spatrick if (CastE->isGLValue())
444e5dd7070Spatrick resultType = getContext().getPointerType(resultType);
445e5dd7070Spatrick
446*12c85518Srobert bool Failed = true;
447e5dd7070Spatrick
448*12c85518Srobert // Check if the value being cast does not evaluates to 0.
449*12c85518Srobert if (!val.isZeroConstant())
450*12c85518Srobert if (std::optional<SVal> V =
451*12c85518Srobert StateMgr.getStoreManager().evalBaseToDerived(val, T)) {
452*12c85518Srobert val = *V;
453*12c85518Srobert Failed = false;
454*12c85518Srobert }
455e5dd7070Spatrick
456e5dd7070Spatrick if (Failed) {
457e5dd7070Spatrick if (T->isReferenceType()) {
458e5dd7070Spatrick // A bad_cast exception is thrown if input value is a reference.
459e5dd7070Spatrick // Currently, we model this, by generating a sink.
460e5dd7070Spatrick Bldr.generateSink(CastE, Pred, state);
461e5dd7070Spatrick continue;
462e5dd7070Spatrick } else {
463e5dd7070Spatrick // If the cast fails on a pointer, bind to 0.
464*12c85518Srobert state = state->BindExpr(CastE, LCtx,
465*12c85518Srobert svalBuilder.makeNullWithType(resultType));
466e5dd7070Spatrick }
467e5dd7070Spatrick } else {
468e5dd7070Spatrick // If we don't know if the cast succeeded, conjure a new symbol.
469e5dd7070Spatrick if (val.isUnknown()) {
470e5dd7070Spatrick DefinedOrUnknownSVal NewSym =
471e5dd7070Spatrick svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
472e5dd7070Spatrick currBldrCtx->blockCount());
473e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, NewSym);
474e5dd7070Spatrick } else
475e5dd7070Spatrick // Else, bind to the derived region value.
476e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, val);
477e5dd7070Spatrick }
478e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
479e5dd7070Spatrick continue;
480e5dd7070Spatrick }
481e5dd7070Spatrick case CK_BaseToDerived: {
482e5dd7070Spatrick SVal val = state->getSVal(Ex, LCtx);
483e5dd7070Spatrick QualType resultType = CastE->getType();
484e5dd7070Spatrick if (CastE->isGLValue())
485e5dd7070Spatrick resultType = getContext().getPointerType(resultType);
486e5dd7070Spatrick
487e5dd7070Spatrick if (!val.isConstant()) {
488*12c85518Srobert std::optional<SVal> V = getStoreManager().evalBaseToDerived(val, T);
489*12c85518Srobert val = V ? *V : UnknownVal();
490e5dd7070Spatrick }
491e5dd7070Spatrick
492e5dd7070Spatrick // Failed to cast or the result is unknown, fall back to conservative.
493*12c85518Srobert if (val.isUnknown()) {
494e5dd7070Spatrick val =
495e5dd7070Spatrick svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
496e5dd7070Spatrick currBldrCtx->blockCount());
497e5dd7070Spatrick }
498e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, val);
499e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
500e5dd7070Spatrick continue;
501e5dd7070Spatrick }
502e5dd7070Spatrick case CK_NullToPointer: {
503*12c85518Srobert SVal V = svalBuilder.makeNullWithType(CastE->getType());
504e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
505e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
506e5dd7070Spatrick continue;
507e5dd7070Spatrick }
508e5dd7070Spatrick case CK_NullToMemberPointer: {
509e5dd7070Spatrick SVal V = svalBuilder.getMemberPointer(nullptr);
510e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, V);
511e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
512e5dd7070Spatrick continue;
513e5dd7070Spatrick }
514e5dd7070Spatrick case CK_DerivedToBaseMemberPointer:
515e5dd7070Spatrick case CK_BaseToDerivedMemberPointer:
516e5dd7070Spatrick case CK_ReinterpretMemberPointer: {
517e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
518e5dd7070Spatrick if (auto PTMSV = V.getAs<nonloc::PointerToMember>()) {
519a9ac8606Spatrick SVal CastedPTMSV =
520a9ac8606Spatrick svalBuilder.makePointerToMember(getBasicVals().accumCXXBase(
521a9ac8606Spatrick CastE->path(), *PTMSV, CastE->getCastKind()));
522e5dd7070Spatrick state = state->BindExpr(CastE, LCtx, CastedPTMSV);
523e5dd7070Spatrick Bldr.generateNode(CastE, Pred, state);
524e5dd7070Spatrick continue;
525e5dd7070Spatrick }
526e5dd7070Spatrick // Explicitly proceed with default handler for this case cascade.
527e5dd7070Spatrick }
528*12c85518Srobert [[fallthrough]];
529e5dd7070Spatrick // Various C++ casts that are not handled yet.
530e5dd7070Spatrick case CK_ToUnion:
531a9ac8606Spatrick case CK_MatrixCast:
532e5dd7070Spatrick case CK_VectorSplat: {
533a9ac8606Spatrick QualType resultType = CastE->getType();
534a9ac8606Spatrick if (CastE->isGLValue())
535a9ac8606Spatrick resultType = getContext().getPointerType(resultType);
536a9ac8606Spatrick SVal result = svalBuilder.conjureSymbolVal(
537a9ac8606Spatrick /*symbolTag=*/nullptr, CastE, LCtx, resultType,
538a9ac8606Spatrick currBldrCtx->blockCount());
539a9ac8606Spatrick state = state->BindExpr(CastE, LCtx, result);
540a9ac8606Spatrick Bldr.generateNode(CastE, Pred, state);
541e5dd7070Spatrick continue;
542e5dd7070Spatrick }
543e5dd7070Spatrick }
544e5dd7070Spatrick }
545e5dd7070Spatrick }
546e5dd7070Spatrick
VisitCompoundLiteralExpr(const CompoundLiteralExpr * CL,ExplodedNode * Pred,ExplodedNodeSet & Dst)547e5dd7070Spatrick void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
548e5dd7070Spatrick ExplodedNode *Pred,
549e5dd7070Spatrick ExplodedNodeSet &Dst) {
550e5dd7070Spatrick StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
551e5dd7070Spatrick
552e5dd7070Spatrick ProgramStateRef State = Pred->getState();
553e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
554e5dd7070Spatrick
555e5dd7070Spatrick const Expr *Init = CL->getInitializer();
556e5dd7070Spatrick SVal V = State->getSVal(CL->getInitializer(), LCtx);
557e5dd7070Spatrick
558*12c85518Srobert if (isa<CXXConstructExpr, CXXStdInitializerListExpr>(Init)) {
559e5dd7070Spatrick // No work needed. Just pass the value up to this expression.
560e5dd7070Spatrick } else {
561e5dd7070Spatrick assert(isa<InitListExpr>(Init));
562e5dd7070Spatrick Loc CLLoc = State->getLValue(CL, LCtx);
563e5dd7070Spatrick State = State->bindLoc(CLLoc, V, LCtx);
564e5dd7070Spatrick
565e5dd7070Spatrick if (CL->isGLValue())
566e5dd7070Spatrick V = CLLoc;
567e5dd7070Spatrick }
568e5dd7070Spatrick
569e5dd7070Spatrick B.generateNode(CL, Pred, State->BindExpr(CL, LCtx, V));
570e5dd7070Spatrick }
571e5dd7070Spatrick
VisitDeclStmt(const DeclStmt * DS,ExplodedNode * Pred,ExplodedNodeSet & Dst)572e5dd7070Spatrick void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
573e5dd7070Spatrick ExplodedNodeSet &Dst) {
574ec727ea7Spatrick if (isa<TypedefNameDecl>(*DS->decl_begin())) {
575ec727ea7Spatrick // C99 6.7.7 "Any array size expressions associated with variable length
576ec727ea7Spatrick // array declarators are evaluated each time the declaration of the typedef
577ec727ea7Spatrick // name is reached in the order of execution."
578ec727ea7Spatrick // The checkers should know about typedef to be able to handle VLA size
579ec727ea7Spatrick // expressions.
580ec727ea7Spatrick ExplodedNodeSet DstPre;
581ec727ea7Spatrick getCheckerManager().runCheckersForPreStmt(DstPre, Pred, DS, *this);
582ec727ea7Spatrick getCheckerManager().runCheckersForPostStmt(Dst, DstPre, DS, *this);
583ec727ea7Spatrick return;
584ec727ea7Spatrick }
585ec727ea7Spatrick
586e5dd7070Spatrick // Assumption: The CFG has one DeclStmt per Decl.
587e5dd7070Spatrick const VarDecl *VD = dyn_cast_or_null<VarDecl>(*DS->decl_begin());
588e5dd7070Spatrick
589e5dd7070Spatrick if (!VD) {
590e5dd7070Spatrick //TODO:AZ: remove explicit insertion after refactoring is done.
591e5dd7070Spatrick Dst.insert(Pred);
592e5dd7070Spatrick return;
593e5dd7070Spatrick }
594e5dd7070Spatrick
595e5dd7070Spatrick // FIXME: all pre/post visits should eventually be handled by ::Visit().
596e5dd7070Spatrick ExplodedNodeSet dstPreVisit;
597e5dd7070Spatrick getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
598e5dd7070Spatrick
599e5dd7070Spatrick ExplodedNodeSet dstEvaluated;
600e5dd7070Spatrick StmtNodeBuilder B(dstPreVisit, dstEvaluated, *currBldrCtx);
601e5dd7070Spatrick for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
602e5dd7070Spatrick I!=E; ++I) {
603e5dd7070Spatrick ExplodedNode *N = *I;
604e5dd7070Spatrick ProgramStateRef state = N->getState();
605e5dd7070Spatrick const LocationContext *LC = N->getLocationContext();
606e5dd7070Spatrick
607e5dd7070Spatrick // Decls without InitExpr are not initialized explicitly.
608e5dd7070Spatrick if (const Expr *InitEx = VD->getInit()) {
609e5dd7070Spatrick
610e5dd7070Spatrick // Note in the state that the initialization has occurred.
611e5dd7070Spatrick ExplodedNode *UpdatedN = N;
612e5dd7070Spatrick SVal InitVal = state->getSVal(InitEx, LC);
613e5dd7070Spatrick
614e5dd7070Spatrick assert(DS->isSingleDecl());
615e5dd7070Spatrick if (getObjectUnderConstruction(state, DS, LC)) {
616e5dd7070Spatrick state = finishObjectConstruction(state, DS, LC);
617e5dd7070Spatrick // We constructed the object directly in the variable.
618e5dd7070Spatrick // No need to bind anything.
619e5dd7070Spatrick B.generateNode(DS, UpdatedN, state);
620e5dd7070Spatrick } else {
621e5dd7070Spatrick // Recover some path-sensitivity if a scalar value evaluated to
622e5dd7070Spatrick // UnknownVal.
623e5dd7070Spatrick if (InitVal.isUnknown()) {
624e5dd7070Spatrick QualType Ty = InitEx->getType();
625e5dd7070Spatrick if (InitEx->isGLValue()) {
626e5dd7070Spatrick Ty = getContext().getPointerType(Ty);
627e5dd7070Spatrick }
628e5dd7070Spatrick
629e5dd7070Spatrick InitVal = svalBuilder.conjureSymbolVal(nullptr, InitEx, LC, Ty,
630e5dd7070Spatrick currBldrCtx->blockCount());
631e5dd7070Spatrick }
632e5dd7070Spatrick
633e5dd7070Spatrick
634e5dd7070Spatrick B.takeNodes(UpdatedN);
635e5dd7070Spatrick ExplodedNodeSet Dst2;
636e5dd7070Spatrick evalBind(Dst2, DS, UpdatedN, state->getLValue(VD, LC), InitVal, true);
637e5dd7070Spatrick B.addNodes(Dst2);
638e5dd7070Spatrick }
639e5dd7070Spatrick }
640e5dd7070Spatrick else {
641e5dd7070Spatrick B.generateNode(DS, N, state);
642e5dd7070Spatrick }
643e5dd7070Spatrick }
644e5dd7070Spatrick
645e5dd7070Spatrick getCheckerManager().runCheckersForPostStmt(Dst, B.getResults(), DS, *this);
646e5dd7070Spatrick }
647e5dd7070Spatrick
VisitLogicalExpr(const BinaryOperator * B,ExplodedNode * Pred,ExplodedNodeSet & Dst)648e5dd7070Spatrick void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
649e5dd7070Spatrick ExplodedNodeSet &Dst) {
650e5dd7070Spatrick // This method acts upon CFG elements for logical operators && and ||
651e5dd7070Spatrick // and attaches the value (true or false) to them as expressions.
652e5dd7070Spatrick // It doesn't produce any state splits.
653e5dd7070Spatrick // If we made it that far, we're past the point when we modeled the short
654e5dd7070Spatrick // circuit. It means that we should have precise knowledge about whether
655e5dd7070Spatrick // we've short-circuited. If we did, we already know the value we need to
656e5dd7070Spatrick // bind. If we didn't, the value of the RHS (casted to the boolean type)
657e5dd7070Spatrick // is the answer.
658e5dd7070Spatrick // Currently this method tries to figure out whether we've short-circuited
659e5dd7070Spatrick // by looking at the ExplodedGraph. This method is imperfect because there
660e5dd7070Spatrick // could inevitably have been merges that would have resulted in multiple
661e5dd7070Spatrick // potential path traversal histories. We bail out when we fail.
662e5dd7070Spatrick // Due to this ambiguity, a more reliable solution would have been to
663e5dd7070Spatrick // track the short circuit operation history path-sensitively until
664e5dd7070Spatrick // we evaluate the respective logical operator.
665e5dd7070Spatrick assert(B->getOpcode() == BO_LAnd ||
666e5dd7070Spatrick B->getOpcode() == BO_LOr);
667e5dd7070Spatrick
668e5dd7070Spatrick StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
669e5dd7070Spatrick ProgramStateRef state = Pred->getState();
670e5dd7070Spatrick
671e5dd7070Spatrick if (B->getType()->isVectorType()) {
672e5dd7070Spatrick // FIXME: We do not model vector arithmetic yet. When adding support for
673e5dd7070Spatrick // that, note that the CFG-based reasoning below does not apply, because
674e5dd7070Spatrick // logical operators on vectors are not short-circuit. Currently they are
675e5dd7070Spatrick // modeled as short-circuit in Clang CFG but this is incorrect.
676e5dd7070Spatrick // Do not set the value for the expression. It'd be UnknownVal by default.
677e5dd7070Spatrick Bldr.generateNode(B, Pred, state);
678e5dd7070Spatrick return;
679e5dd7070Spatrick }
680e5dd7070Spatrick
681e5dd7070Spatrick ExplodedNode *N = Pred;
682e5dd7070Spatrick while (!N->getLocation().getAs<BlockEntrance>()) {
683e5dd7070Spatrick ProgramPoint P = N->getLocation();
684e5dd7070Spatrick assert(P.getAs<PreStmt>()|| P.getAs<PreStmtPurgeDeadSymbols>());
685e5dd7070Spatrick (void) P;
686e5dd7070Spatrick if (N->pred_size() != 1) {
687e5dd7070Spatrick // We failed to track back where we came from.
688e5dd7070Spatrick Bldr.generateNode(B, Pred, state);
689e5dd7070Spatrick return;
690e5dd7070Spatrick }
691e5dd7070Spatrick N = *N->pred_begin();
692e5dd7070Spatrick }
693e5dd7070Spatrick
694e5dd7070Spatrick if (N->pred_size() != 1) {
695e5dd7070Spatrick // We failed to track back where we came from.
696e5dd7070Spatrick Bldr.generateNode(B, Pred, state);
697e5dd7070Spatrick return;
698e5dd7070Spatrick }
699e5dd7070Spatrick
700e5dd7070Spatrick N = *N->pred_begin();
701e5dd7070Spatrick BlockEdge BE = N->getLocation().castAs<BlockEdge>();
702e5dd7070Spatrick SVal X;
703e5dd7070Spatrick
704e5dd7070Spatrick // Determine the value of the expression by introspecting how we
705e5dd7070Spatrick // got this location in the CFG. This requires looking at the previous
706e5dd7070Spatrick // block we were in and what kind of control-flow transfer was involved.
707e5dd7070Spatrick const CFGBlock *SrcBlock = BE.getSrc();
708e5dd7070Spatrick // The only terminator (if there is one) that makes sense is a logical op.
709e5dd7070Spatrick CFGTerminator T = SrcBlock->getTerminator();
710e5dd7070Spatrick if (const BinaryOperator *Term = cast_or_null<BinaryOperator>(T.getStmt())) {
711e5dd7070Spatrick (void) Term;
712e5dd7070Spatrick assert(Term->isLogicalOp());
713e5dd7070Spatrick assert(SrcBlock->succ_size() == 2);
714e5dd7070Spatrick // Did we take the true or false branch?
715e5dd7070Spatrick unsigned constant = (*SrcBlock->succ_begin() == BE.getDst()) ? 1 : 0;
716e5dd7070Spatrick X = svalBuilder.makeIntVal(constant, B->getType());
717e5dd7070Spatrick }
718e5dd7070Spatrick else {
719e5dd7070Spatrick // If there is no terminator, by construction the last statement
720e5dd7070Spatrick // in SrcBlock is the value of the enclosing expression.
721e5dd7070Spatrick // However, we still need to constrain that value to be 0 or 1.
722e5dd7070Spatrick assert(!SrcBlock->empty());
723e5dd7070Spatrick CFGStmt Elem = SrcBlock->rbegin()->castAs<CFGStmt>();
724e5dd7070Spatrick const Expr *RHS = cast<Expr>(Elem.getStmt());
725e5dd7070Spatrick SVal RHSVal = N->getState()->getSVal(RHS, Pred->getLocationContext());
726e5dd7070Spatrick
727e5dd7070Spatrick if (RHSVal.isUndef()) {
728e5dd7070Spatrick X = RHSVal;
729e5dd7070Spatrick } else {
730e5dd7070Spatrick // We evaluate "RHSVal != 0" expression which result in 0 if the value is
731e5dd7070Spatrick // known to be false, 1 if the value is known to be true and a new symbol
732e5dd7070Spatrick // when the assumption is unknown.
733e5dd7070Spatrick nonloc::ConcreteInt Zero(getBasicVals().getValue(0, B->getType()));
734e5dd7070Spatrick X = evalBinOp(N->getState(), BO_NE,
735e5dd7070Spatrick svalBuilder.evalCast(RHSVal, B->getType(), RHS->getType()),
736e5dd7070Spatrick Zero, B->getType());
737e5dd7070Spatrick }
738e5dd7070Spatrick }
739e5dd7070Spatrick Bldr.generateNode(B, Pred, state->BindExpr(B, Pred->getLocationContext(), X));
740e5dd7070Spatrick }
741e5dd7070Spatrick
VisitInitListExpr(const InitListExpr * IE,ExplodedNode * Pred,ExplodedNodeSet & Dst)742e5dd7070Spatrick void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
743e5dd7070Spatrick ExplodedNode *Pred,
744e5dd7070Spatrick ExplodedNodeSet &Dst) {
745e5dd7070Spatrick StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
746e5dd7070Spatrick
747e5dd7070Spatrick ProgramStateRef state = Pred->getState();
748e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
749e5dd7070Spatrick QualType T = getContext().getCanonicalType(IE->getType());
750e5dd7070Spatrick unsigned NumInitElements = IE->getNumInits();
751e5dd7070Spatrick
752e5dd7070Spatrick if (!IE->isGLValue() && !IE->isTransparent() &&
753e5dd7070Spatrick (T->isArrayType() || T->isRecordType() || T->isVectorType() ||
754e5dd7070Spatrick T->isAnyComplexType())) {
755e5dd7070Spatrick llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
756e5dd7070Spatrick
757e5dd7070Spatrick // Handle base case where the initializer has no elements.
758e5dd7070Spatrick // e.g: static int* myArray[] = {};
759e5dd7070Spatrick if (NumInitElements == 0) {
760e5dd7070Spatrick SVal V = svalBuilder.makeCompoundVal(T, vals);
761e5dd7070Spatrick B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
762e5dd7070Spatrick return;
763e5dd7070Spatrick }
764e5dd7070Spatrick
765*12c85518Srobert for (const Stmt *S : llvm::reverse(*IE)) {
766*12c85518Srobert SVal V = state->getSVal(cast<Expr>(S), LCtx);
767e5dd7070Spatrick vals = getBasicVals().prependSVal(V, vals);
768e5dd7070Spatrick }
769e5dd7070Spatrick
770e5dd7070Spatrick B.generateNode(IE, Pred,
771e5dd7070Spatrick state->BindExpr(IE, LCtx,
772e5dd7070Spatrick svalBuilder.makeCompoundVal(T, vals)));
773e5dd7070Spatrick return;
774e5dd7070Spatrick }
775e5dd7070Spatrick
776e5dd7070Spatrick // Handle scalars: int{5} and int{} and GLvalues.
777e5dd7070Spatrick // Note, if the InitListExpr is a GLvalue, it means that there is an address
778e5dd7070Spatrick // representing it, so it must have a single init element.
779e5dd7070Spatrick assert(NumInitElements <= 1);
780e5dd7070Spatrick
781e5dd7070Spatrick SVal V;
782e5dd7070Spatrick if (NumInitElements == 0)
783e5dd7070Spatrick V = getSValBuilder().makeZeroVal(T);
784e5dd7070Spatrick else
785e5dd7070Spatrick V = state->getSVal(IE->getInit(0), LCtx);
786e5dd7070Spatrick
787e5dd7070Spatrick B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
788e5dd7070Spatrick }
789e5dd7070Spatrick
VisitGuardedExpr(const Expr * Ex,const Expr * L,const Expr * R,ExplodedNode * Pred,ExplodedNodeSet & Dst)790e5dd7070Spatrick void ExprEngine::VisitGuardedExpr(const Expr *Ex,
791e5dd7070Spatrick const Expr *L,
792e5dd7070Spatrick const Expr *R,
793e5dd7070Spatrick ExplodedNode *Pred,
794e5dd7070Spatrick ExplodedNodeSet &Dst) {
795e5dd7070Spatrick assert(L && R);
796e5dd7070Spatrick
797e5dd7070Spatrick StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
798e5dd7070Spatrick ProgramStateRef state = Pred->getState();
799e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
800e5dd7070Spatrick const CFGBlock *SrcBlock = nullptr;
801e5dd7070Spatrick
802e5dd7070Spatrick // Find the predecessor block.
803e5dd7070Spatrick ProgramStateRef SrcState = state;
804e5dd7070Spatrick for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
805e5dd7070Spatrick ProgramPoint PP = N->getLocation();
806e5dd7070Spatrick if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) {
807e5dd7070Spatrick // If the state N has multiple predecessors P, it means that successors
808e5dd7070Spatrick // of P are all equivalent.
809e5dd7070Spatrick // In turn, that means that all nodes at P are equivalent in terms
810e5dd7070Spatrick // of observable behavior at N, and we can follow any of them.
811e5dd7070Spatrick // FIXME: a more robust solution which does not walk up the tree.
812e5dd7070Spatrick continue;
813e5dd7070Spatrick }
814e5dd7070Spatrick SrcBlock = PP.castAs<BlockEdge>().getSrc();
815e5dd7070Spatrick SrcState = N->getState();
816e5dd7070Spatrick break;
817e5dd7070Spatrick }
818e5dd7070Spatrick
819e5dd7070Spatrick assert(SrcBlock && "missing function entry");
820e5dd7070Spatrick
821e5dd7070Spatrick // Find the last expression in the predecessor block. That is the
822e5dd7070Spatrick // expression that is used for the value of the ternary expression.
823e5dd7070Spatrick bool hasValue = false;
824e5dd7070Spatrick SVal V;
825e5dd7070Spatrick
826e5dd7070Spatrick for (CFGElement CE : llvm::reverse(*SrcBlock)) {
827*12c85518Srobert if (std::optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
828e5dd7070Spatrick const Expr *ValEx = cast<Expr>(CS->getStmt());
829e5dd7070Spatrick ValEx = ValEx->IgnoreParens();
830e5dd7070Spatrick
831e5dd7070Spatrick // For GNU extension '?:' operator, the left hand side will be an
832e5dd7070Spatrick // OpaqueValueExpr, so get the underlying expression.
833e5dd7070Spatrick if (const OpaqueValueExpr *OpaqueEx = dyn_cast<OpaqueValueExpr>(L))
834e5dd7070Spatrick L = OpaqueEx->getSourceExpr();
835e5dd7070Spatrick
836e5dd7070Spatrick // If the last expression in the predecessor block matches true or false
837e5dd7070Spatrick // subexpression, get its the value.
838e5dd7070Spatrick if (ValEx == L->IgnoreParens() || ValEx == R->IgnoreParens()) {
839e5dd7070Spatrick hasValue = true;
840e5dd7070Spatrick V = SrcState->getSVal(ValEx, LCtx);
841e5dd7070Spatrick }
842e5dd7070Spatrick break;
843e5dd7070Spatrick }
844e5dd7070Spatrick }
845e5dd7070Spatrick
846e5dd7070Spatrick if (!hasValue)
847e5dd7070Spatrick V = svalBuilder.conjureSymbolVal(nullptr, Ex, LCtx,
848e5dd7070Spatrick currBldrCtx->blockCount());
849e5dd7070Spatrick
850e5dd7070Spatrick // Generate a new node with the binding from the appropriate path.
851e5dd7070Spatrick B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V, true));
852e5dd7070Spatrick }
853e5dd7070Spatrick
854e5dd7070Spatrick void ExprEngine::
VisitOffsetOfExpr(const OffsetOfExpr * OOE,ExplodedNode * Pred,ExplodedNodeSet & Dst)855e5dd7070Spatrick VisitOffsetOfExpr(const OffsetOfExpr *OOE,
856e5dd7070Spatrick ExplodedNode *Pred, ExplodedNodeSet &Dst) {
857e5dd7070Spatrick StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
858e5dd7070Spatrick Expr::EvalResult Result;
859e5dd7070Spatrick if (OOE->EvaluateAsInt(Result, getContext())) {
860e5dd7070Spatrick APSInt IV = Result.Val.getInt();
861e5dd7070Spatrick assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
862e5dd7070Spatrick assert(OOE->getType()->castAs<BuiltinType>()->isInteger());
863e5dd7070Spatrick assert(IV.isSigned() == OOE->getType()->isSignedIntegerType());
864e5dd7070Spatrick SVal X = svalBuilder.makeIntVal(IV);
865e5dd7070Spatrick B.generateNode(OOE, Pred,
866e5dd7070Spatrick Pred->getState()->BindExpr(OOE, Pred->getLocationContext(),
867e5dd7070Spatrick X));
868e5dd7070Spatrick }
869e5dd7070Spatrick // FIXME: Handle the case where __builtin_offsetof is not a constant.
870e5dd7070Spatrick }
871e5dd7070Spatrick
872e5dd7070Spatrick
873e5dd7070Spatrick void ExprEngine::
VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr * Ex,ExplodedNode * Pred,ExplodedNodeSet & Dst)874e5dd7070Spatrick VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
875e5dd7070Spatrick ExplodedNode *Pred,
876e5dd7070Spatrick ExplodedNodeSet &Dst) {
877e5dd7070Spatrick // FIXME: Prechecks eventually go in ::Visit().
878e5dd7070Spatrick ExplodedNodeSet CheckedSet;
879e5dd7070Spatrick getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, Ex, *this);
880e5dd7070Spatrick
881e5dd7070Spatrick ExplodedNodeSet EvalSet;
882e5dd7070Spatrick StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
883e5dd7070Spatrick
884e5dd7070Spatrick QualType T = Ex->getTypeOfArgument();
885e5dd7070Spatrick
886e5dd7070Spatrick for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
887e5dd7070Spatrick I != E; ++I) {
888e5dd7070Spatrick if (Ex->getKind() == UETT_SizeOf) {
889e5dd7070Spatrick if (!T->isIncompleteType() && !T->isConstantSizeType()) {
890e5dd7070Spatrick assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
891e5dd7070Spatrick
892e5dd7070Spatrick // FIXME: Add support for VLA type arguments and VLA expressions.
893e5dd7070Spatrick // When that happens, we should probably refactor VLASizeChecker's code.
894e5dd7070Spatrick continue;
895e5dd7070Spatrick } else if (T->getAs<ObjCObjectType>()) {
896e5dd7070Spatrick // Some code tries to take the sizeof an ObjCObjectType, relying that
897e5dd7070Spatrick // the compiler has laid out its representation. Just report Unknown
898e5dd7070Spatrick // for these.
899e5dd7070Spatrick continue;
900e5dd7070Spatrick }
901e5dd7070Spatrick }
902e5dd7070Spatrick
903e5dd7070Spatrick APSInt Value = Ex->EvaluateKnownConstInt(getContext());
904e5dd7070Spatrick CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue());
905e5dd7070Spatrick
906e5dd7070Spatrick ProgramStateRef state = (*I)->getState();
907e5dd7070Spatrick state = state->BindExpr(Ex, (*I)->getLocationContext(),
908e5dd7070Spatrick svalBuilder.makeIntVal(amt.getQuantity(),
909e5dd7070Spatrick Ex->getType()));
910e5dd7070Spatrick Bldr.generateNode(Ex, *I, state);
911e5dd7070Spatrick }
912e5dd7070Spatrick
913e5dd7070Spatrick getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this);
914e5dd7070Spatrick }
915e5dd7070Spatrick
handleUOExtension(ExplodedNodeSet::iterator I,const UnaryOperator * U,StmtNodeBuilder & Bldr)916e5dd7070Spatrick void ExprEngine::handleUOExtension(ExplodedNodeSet::iterator I,
917e5dd7070Spatrick const UnaryOperator *U,
918e5dd7070Spatrick StmtNodeBuilder &Bldr) {
919e5dd7070Spatrick // FIXME: We can probably just have some magic in Environment::getSVal()
920e5dd7070Spatrick // that propagates values, instead of creating a new node here.
921e5dd7070Spatrick //
922e5dd7070Spatrick // Unary "+" is a no-op, similar to a parentheses. We still have places
923e5dd7070Spatrick // where it may be a block-level expression, so we need to
924e5dd7070Spatrick // generate an extra node that just propagates the value of the
925e5dd7070Spatrick // subexpression.
926e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
927e5dd7070Spatrick ProgramStateRef state = (*I)->getState();
928e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
929e5dd7070Spatrick Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
930e5dd7070Spatrick state->getSVal(Ex, LCtx)));
931e5dd7070Spatrick }
932e5dd7070Spatrick
VisitUnaryOperator(const UnaryOperator * U,ExplodedNode * Pred,ExplodedNodeSet & Dst)933e5dd7070Spatrick void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
934e5dd7070Spatrick ExplodedNodeSet &Dst) {
935e5dd7070Spatrick // FIXME: Prechecks eventually go in ::Visit().
936e5dd7070Spatrick ExplodedNodeSet CheckedSet;
937e5dd7070Spatrick getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, U, *this);
938e5dd7070Spatrick
939e5dd7070Spatrick ExplodedNodeSet EvalSet;
940e5dd7070Spatrick StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
941e5dd7070Spatrick
942e5dd7070Spatrick for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
943e5dd7070Spatrick I != E; ++I) {
944e5dd7070Spatrick switch (U->getOpcode()) {
945e5dd7070Spatrick default: {
946e5dd7070Spatrick Bldr.takeNodes(*I);
947e5dd7070Spatrick ExplodedNodeSet Tmp;
948e5dd7070Spatrick VisitIncrementDecrementOperator(U, *I, Tmp);
949e5dd7070Spatrick Bldr.addNodes(Tmp);
950e5dd7070Spatrick break;
951e5dd7070Spatrick }
952e5dd7070Spatrick case UO_Real: {
953e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
954e5dd7070Spatrick
955e5dd7070Spatrick // FIXME: We don't have complex SValues yet.
956e5dd7070Spatrick if (Ex->getType()->isAnyComplexType()) {
957e5dd7070Spatrick // Just report "Unknown."
958e5dd7070Spatrick break;
959e5dd7070Spatrick }
960e5dd7070Spatrick
961e5dd7070Spatrick // For all other types, UO_Real is an identity operation.
962e5dd7070Spatrick assert (U->getType() == Ex->getType());
963e5dd7070Spatrick ProgramStateRef state = (*I)->getState();
964e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
965e5dd7070Spatrick Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
966e5dd7070Spatrick state->getSVal(Ex, LCtx)));
967e5dd7070Spatrick break;
968e5dd7070Spatrick }
969e5dd7070Spatrick
970e5dd7070Spatrick case UO_Imag: {
971e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
972e5dd7070Spatrick // FIXME: We don't have complex SValues yet.
973e5dd7070Spatrick if (Ex->getType()->isAnyComplexType()) {
974e5dd7070Spatrick // Just report "Unknown."
975e5dd7070Spatrick break;
976e5dd7070Spatrick }
977e5dd7070Spatrick // For all other types, UO_Imag returns 0.
978e5dd7070Spatrick ProgramStateRef state = (*I)->getState();
979e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
980e5dd7070Spatrick SVal X = svalBuilder.makeZeroVal(Ex->getType());
981e5dd7070Spatrick Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, X));
982e5dd7070Spatrick break;
983e5dd7070Spatrick }
984e5dd7070Spatrick
985e5dd7070Spatrick case UO_AddrOf: {
986e5dd7070Spatrick // Process pointer-to-member address operation.
987e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
988e5dd7070Spatrick if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex)) {
989e5dd7070Spatrick const ValueDecl *VD = DRE->getDecl();
990e5dd7070Spatrick
991*12c85518Srobert if (isa<CXXMethodDecl, FieldDecl, IndirectFieldDecl>(VD)) {
992e5dd7070Spatrick ProgramStateRef State = (*I)->getState();
993e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
994a9ac8606Spatrick SVal SV = svalBuilder.getMemberPointer(cast<NamedDecl>(VD));
995e5dd7070Spatrick Bldr.generateNode(U, *I, State->BindExpr(U, LCtx, SV));
996e5dd7070Spatrick break;
997e5dd7070Spatrick }
998e5dd7070Spatrick }
999e5dd7070Spatrick // Explicitly proceed with default handler for this case cascade.
1000e5dd7070Spatrick handleUOExtension(I, U, Bldr);
1001e5dd7070Spatrick break;
1002e5dd7070Spatrick }
1003e5dd7070Spatrick case UO_Plus:
1004e5dd7070Spatrick assert(!U->isGLValue());
1005*12c85518Srobert [[fallthrough]];
1006e5dd7070Spatrick case UO_Deref:
1007e5dd7070Spatrick case UO_Extension: {
1008e5dd7070Spatrick handleUOExtension(I, U, Bldr);
1009e5dd7070Spatrick break;
1010e5dd7070Spatrick }
1011e5dd7070Spatrick
1012e5dd7070Spatrick case UO_LNot:
1013e5dd7070Spatrick case UO_Minus:
1014e5dd7070Spatrick case UO_Not: {
1015e5dd7070Spatrick assert (!U->isGLValue());
1016e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
1017e5dd7070Spatrick ProgramStateRef state = (*I)->getState();
1018e5dd7070Spatrick const LocationContext *LCtx = (*I)->getLocationContext();
1019e5dd7070Spatrick
1020e5dd7070Spatrick // Get the value of the subexpression.
1021e5dd7070Spatrick SVal V = state->getSVal(Ex, LCtx);
1022e5dd7070Spatrick
1023e5dd7070Spatrick if (V.isUnknownOrUndef()) {
1024e5dd7070Spatrick Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V));
1025e5dd7070Spatrick break;
1026e5dd7070Spatrick }
1027e5dd7070Spatrick
1028e5dd7070Spatrick switch (U->getOpcode()) {
1029e5dd7070Spatrick default:
1030e5dd7070Spatrick llvm_unreachable("Invalid Opcode.");
1031e5dd7070Spatrick case UO_Not:
1032e5dd7070Spatrick // FIXME: Do we need to handle promotions?
1033*12c85518Srobert state = state->BindExpr(
1034*12c85518Srobert U, LCtx, svalBuilder.evalComplement(V.castAs<NonLoc>()));
1035e5dd7070Spatrick break;
1036e5dd7070Spatrick case UO_Minus:
1037e5dd7070Spatrick // FIXME: Do we need to handle promotions?
1038*12c85518Srobert state = state->BindExpr(U, LCtx,
1039*12c85518Srobert svalBuilder.evalMinus(V.castAs<NonLoc>()));
1040e5dd7070Spatrick break;
1041e5dd7070Spatrick case UO_LNot:
1042e5dd7070Spatrick // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
1043e5dd7070Spatrick //
1044e5dd7070Spatrick // Note: technically we do "E == 0", but this is the same in the
1045e5dd7070Spatrick // transfer functions as "0 == E".
1046e5dd7070Spatrick SVal Result;
1047*12c85518Srobert if (std::optional<Loc> LV = V.getAs<Loc>()) {
1048e5dd7070Spatrick Loc X = svalBuilder.makeNullWithType(Ex->getType());
1049e5dd7070Spatrick Result = evalBinOp(state, BO_EQ, *LV, X, U->getType());
1050e5dd7070Spatrick } else if (Ex->getType()->isFloatingType()) {
1051e5dd7070Spatrick // FIXME: handle floating point types.
1052e5dd7070Spatrick Result = UnknownVal();
1053e5dd7070Spatrick } else {
1054e5dd7070Spatrick nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
1055*12c85518Srobert Result = evalBinOp(state, BO_EQ, V.castAs<NonLoc>(), X, U->getType());
1056e5dd7070Spatrick }
1057e5dd7070Spatrick
1058e5dd7070Spatrick state = state->BindExpr(U, LCtx, Result);
1059e5dd7070Spatrick break;
1060e5dd7070Spatrick }
1061e5dd7070Spatrick Bldr.generateNode(U, *I, state);
1062e5dd7070Spatrick break;
1063e5dd7070Spatrick }
1064e5dd7070Spatrick }
1065e5dd7070Spatrick }
1066e5dd7070Spatrick
1067e5dd7070Spatrick getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, U, *this);
1068e5dd7070Spatrick }
1069e5dd7070Spatrick
VisitIncrementDecrementOperator(const UnaryOperator * U,ExplodedNode * Pred,ExplodedNodeSet & Dst)1070e5dd7070Spatrick void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
1071e5dd7070Spatrick ExplodedNode *Pred,
1072e5dd7070Spatrick ExplodedNodeSet &Dst) {
1073e5dd7070Spatrick // Handle ++ and -- (both pre- and post-increment).
1074e5dd7070Spatrick assert (U->isIncrementDecrementOp());
1075e5dd7070Spatrick const Expr *Ex = U->getSubExpr()->IgnoreParens();
1076e5dd7070Spatrick
1077e5dd7070Spatrick const LocationContext *LCtx = Pred->getLocationContext();
1078e5dd7070Spatrick ProgramStateRef state = Pred->getState();
1079e5dd7070Spatrick SVal loc = state->getSVal(Ex, LCtx);
1080e5dd7070Spatrick
1081e5dd7070Spatrick // Perform a load.
1082e5dd7070Spatrick ExplodedNodeSet Tmp;
1083e5dd7070Spatrick evalLoad(Tmp, U, Ex, Pred, state, loc);
1084e5dd7070Spatrick
1085e5dd7070Spatrick ExplodedNodeSet Dst2;
1086e5dd7070Spatrick StmtNodeBuilder Bldr(Tmp, Dst2, *currBldrCtx);
1087e5dd7070Spatrick for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) {
1088e5dd7070Spatrick
1089e5dd7070Spatrick state = (*I)->getState();
1090e5dd7070Spatrick assert(LCtx == (*I)->getLocationContext());
1091e5dd7070Spatrick SVal V2_untested = state->getSVal(Ex, LCtx);
1092e5dd7070Spatrick
1093e5dd7070Spatrick // Propagate unknown and undefined values.
1094e5dd7070Spatrick if (V2_untested.isUnknownOrUndef()) {
1095e5dd7070Spatrick state = state->BindExpr(U, LCtx, V2_untested);
1096e5dd7070Spatrick
1097e5dd7070Spatrick // Perform the store, so that the uninitialized value detection happens.
1098e5dd7070Spatrick Bldr.takeNodes(*I);
1099e5dd7070Spatrick ExplodedNodeSet Dst3;
1100e5dd7070Spatrick evalStore(Dst3, U, Ex, *I, state, loc, V2_untested);
1101e5dd7070Spatrick Bldr.addNodes(Dst3);
1102e5dd7070Spatrick
1103e5dd7070Spatrick continue;
1104e5dd7070Spatrick }
1105e5dd7070Spatrick DefinedSVal V2 = V2_untested.castAs<DefinedSVal>();
1106e5dd7070Spatrick
1107e5dd7070Spatrick // Handle all other values.
1108e5dd7070Spatrick BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub;
1109e5dd7070Spatrick
1110e5dd7070Spatrick // If the UnaryOperator has non-location type, use its type to create the
1111e5dd7070Spatrick // constant value. If the UnaryOperator has location type, create the
1112e5dd7070Spatrick // constant with int type and pointer width.
1113e5dd7070Spatrick SVal RHS;
1114e5dd7070Spatrick SVal Result;
1115e5dd7070Spatrick
1116e5dd7070Spatrick if (U->getType()->isAnyPointerType())
1117e5dd7070Spatrick RHS = svalBuilder.makeArrayIndex(1);
1118e5dd7070Spatrick else if (U->getType()->isIntegralOrEnumerationType())
1119e5dd7070Spatrick RHS = svalBuilder.makeIntVal(1, U->getType());
1120e5dd7070Spatrick else
1121e5dd7070Spatrick RHS = UnknownVal();
1122e5dd7070Spatrick
1123e5dd7070Spatrick // The use of an operand of type bool with the ++ operators is deprecated
1124e5dd7070Spatrick // but valid until C++17. And if the operand of the ++ operator is of type
1125e5dd7070Spatrick // bool, it is set to true until C++17. Note that for '_Bool', it is also
1126e5dd7070Spatrick // set to true when it encounters ++ operator.
1127e5dd7070Spatrick if (U->getType()->isBooleanType() && U->isIncrementOp())
1128e5dd7070Spatrick Result = svalBuilder.makeTruthVal(true, U->getType());
1129e5dd7070Spatrick else
1130e5dd7070Spatrick Result = evalBinOp(state, Op, V2, RHS, U->getType());
1131e5dd7070Spatrick
1132e5dd7070Spatrick // Conjure a new symbol if necessary to recover precision.
1133e5dd7070Spatrick if (Result.isUnknown()){
1134e5dd7070Spatrick DefinedOrUnknownSVal SymVal =
1135e5dd7070Spatrick svalBuilder.conjureSymbolVal(nullptr, U, LCtx,
1136e5dd7070Spatrick currBldrCtx->blockCount());
1137e5dd7070Spatrick Result = SymVal;
1138e5dd7070Spatrick
1139e5dd7070Spatrick // If the value is a location, ++/-- should always preserve
1140e5dd7070Spatrick // non-nullness. Check if the original value was non-null, and if so
1141e5dd7070Spatrick // propagate that constraint.
1142e5dd7070Spatrick if (Loc::isLocType(U->getType())) {
1143e5dd7070Spatrick DefinedOrUnknownSVal Constraint =
1144e5dd7070Spatrick svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
1145e5dd7070Spatrick
1146e5dd7070Spatrick if (!state->assume(Constraint, true)) {
1147e5dd7070Spatrick // It isn't feasible for the original value to be null.
1148e5dd7070Spatrick // Propagate this constraint.
1149e5dd7070Spatrick Constraint = svalBuilder.evalEQ(state, SymVal,
1150e5dd7070Spatrick svalBuilder.makeZeroVal(U->getType()));
1151e5dd7070Spatrick
1152e5dd7070Spatrick state = state->assume(Constraint, false);
1153e5dd7070Spatrick assert(state);
1154e5dd7070Spatrick }
1155e5dd7070Spatrick }
1156e5dd7070Spatrick }
1157e5dd7070Spatrick
1158e5dd7070Spatrick // Since the lvalue-to-rvalue conversion is explicit in the AST,
1159e5dd7070Spatrick // we bind an l-value if the operator is prefix and an lvalue (in C++).
1160e5dd7070Spatrick if (U->isGLValue())
1161e5dd7070Spatrick state = state->BindExpr(U, LCtx, loc);
1162e5dd7070Spatrick else
1163e5dd7070Spatrick state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result);
1164e5dd7070Spatrick
1165e5dd7070Spatrick // Perform the store.
1166e5dd7070Spatrick Bldr.takeNodes(*I);
1167e5dd7070Spatrick ExplodedNodeSet Dst3;
1168e5dd7070Spatrick evalStore(Dst3, U, Ex, *I, state, loc, Result);
1169e5dd7070Spatrick Bldr.addNodes(Dst3);
1170e5dd7070Spatrick }
1171e5dd7070Spatrick Dst.insert(Dst2);
1172e5dd7070Spatrick }
1173