106f32e7eSjoerg //=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg //  This file defines ExprEngine's support for C expressions.
1006f32e7eSjoerg //
1106f32e7eSjoerg //===----------------------------------------------------------------------===//
1206f32e7eSjoerg 
1306f32e7eSjoerg #include "clang/AST/ExprCXX.h"
1406f32e7eSjoerg #include "clang/AST/DeclCXX.h"
1506f32e7eSjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
1606f32e7eSjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
1706f32e7eSjoerg 
1806f32e7eSjoerg using namespace clang;
1906f32e7eSjoerg using namespace ento;
2006f32e7eSjoerg using llvm::APSInt;
2106f32e7eSjoerg 
2206f32e7eSjoerg /// Optionally conjure and return a symbol for offset when processing
2306f32e7eSjoerg /// an expression \p Expression.
2406f32e7eSjoerg /// If \p Other is a location, conjure a symbol for \p Symbol
2506f32e7eSjoerg /// (offset) if it is unknown so that memory arithmetic always
2606f32e7eSjoerg /// results in an ElementRegion.
2706f32e7eSjoerg /// \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)2806f32e7eSjoerg static SVal conjureOffsetSymbolOnLocation(
2906f32e7eSjoerg     SVal Symbol, SVal Other, Expr* Expression, SValBuilder &svalBuilder,
3006f32e7eSjoerg     unsigned Count, const LocationContext *LCtx) {
3106f32e7eSjoerg   QualType Ty = Expression->getType();
3206f32e7eSjoerg   if (Other.getAs<Loc>() &&
3306f32e7eSjoerg       Ty->isIntegralOrEnumerationType() &&
3406f32e7eSjoerg       Symbol.isUnknown()) {
3506f32e7eSjoerg     return svalBuilder.conjureSymbolVal(Expression, LCtx, Ty, Count);
3606f32e7eSjoerg   }
3706f32e7eSjoerg   return Symbol;
3806f32e7eSjoerg }
3906f32e7eSjoerg 
VisitBinaryOperator(const BinaryOperator * B,ExplodedNode * Pred,ExplodedNodeSet & Dst)4006f32e7eSjoerg void ExprEngine::VisitBinaryOperator(const BinaryOperator* B,
4106f32e7eSjoerg                                      ExplodedNode *Pred,
4206f32e7eSjoerg                                      ExplodedNodeSet &Dst) {
4306f32e7eSjoerg 
4406f32e7eSjoerg   Expr *LHS = B->getLHS()->IgnoreParens();
4506f32e7eSjoerg   Expr *RHS = B->getRHS()->IgnoreParens();
4606f32e7eSjoerg 
4706f32e7eSjoerg   // FIXME: Prechecks eventually go in ::Visit().
4806f32e7eSjoerg   ExplodedNodeSet CheckedSet;
4906f32e7eSjoerg   ExplodedNodeSet Tmp2;
5006f32e7eSjoerg   getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this);
5106f32e7eSjoerg 
5206f32e7eSjoerg   // With both the LHS and RHS evaluated, process the operation itself.
5306f32e7eSjoerg   for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end();
5406f32e7eSjoerg          it != ei; ++it) {
5506f32e7eSjoerg 
5606f32e7eSjoerg     ProgramStateRef state = (*it)->getState();
5706f32e7eSjoerg     const LocationContext *LCtx = (*it)->getLocationContext();
5806f32e7eSjoerg     SVal LeftV = state->getSVal(LHS, LCtx);
5906f32e7eSjoerg     SVal RightV = state->getSVal(RHS, LCtx);
6006f32e7eSjoerg 
6106f32e7eSjoerg     BinaryOperator::Opcode Op = B->getOpcode();
6206f32e7eSjoerg 
6306f32e7eSjoerg     if (Op == BO_Assign) {
6406f32e7eSjoerg       // EXPERIMENTAL: "Conjured" symbols.
6506f32e7eSjoerg       // FIXME: Handle structs.
6606f32e7eSjoerg       if (RightV.isUnknown()) {
6706f32e7eSjoerg         unsigned Count = currBldrCtx->blockCount();
6806f32e7eSjoerg         RightV = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx,
6906f32e7eSjoerg                                               Count);
7006f32e7eSjoerg       }
7106f32e7eSjoerg       // Simulate the effects of a "store":  bind the value of the RHS
7206f32e7eSjoerg       // to the L-Value represented by the LHS.
7306f32e7eSjoerg       SVal ExprVal = B->isGLValue() ? LeftV : RightV;
7406f32e7eSjoerg       evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal),
7506f32e7eSjoerg                 LeftV, RightV);
7606f32e7eSjoerg       continue;
7706f32e7eSjoerg     }
7806f32e7eSjoerg 
7906f32e7eSjoerg     if (!B->isAssignmentOp()) {
8006f32e7eSjoerg       StmtNodeBuilder Bldr(*it, Tmp2, *currBldrCtx);
8106f32e7eSjoerg 
8206f32e7eSjoerg       if (B->isAdditiveOp()) {
8306f32e7eSjoerg         // TODO: This can be removed after we enable history tracking with
8406f32e7eSjoerg         // SymSymExpr.
8506f32e7eSjoerg         unsigned Count = currBldrCtx->blockCount();
8606f32e7eSjoerg         RightV = conjureOffsetSymbolOnLocation(
8706f32e7eSjoerg             RightV, LeftV, RHS, svalBuilder, Count, LCtx);
8806f32e7eSjoerg         LeftV = conjureOffsetSymbolOnLocation(
8906f32e7eSjoerg             LeftV, RightV, LHS, svalBuilder, Count, LCtx);
9006f32e7eSjoerg       }
9106f32e7eSjoerg 
9206f32e7eSjoerg       // Although we don't yet model pointers-to-members, we do need to make
9306f32e7eSjoerg       // sure that the members of temporaries have a valid 'this' pointer for
9406f32e7eSjoerg       // other checks.
9506f32e7eSjoerg       if (B->getOpcode() == BO_PtrMemD)
9606f32e7eSjoerg         state = createTemporaryRegionIfNeeded(state, LCtx, LHS);
9706f32e7eSjoerg 
9806f32e7eSjoerg       // Process non-assignments except commas or short-circuited
9906f32e7eSjoerg       // logical expressions (LAnd and LOr).
10006f32e7eSjoerg       SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType());
10106f32e7eSjoerg       if (!Result.isUnknown()) {
10206f32e7eSjoerg         state = state->BindExpr(B, LCtx, Result);
10306f32e7eSjoerg       } else {
10406f32e7eSjoerg         // If we cannot evaluate the operation escape the operands.
105*13fbcb42Sjoerg         state = escapeValues(state, LeftV, PSK_EscapeOther);
106*13fbcb42Sjoerg         state = escapeValues(state, RightV, PSK_EscapeOther);
10706f32e7eSjoerg       }
10806f32e7eSjoerg 
10906f32e7eSjoerg       Bldr.generateNode(B, *it, state);
11006f32e7eSjoerg       continue;
11106f32e7eSjoerg     }
11206f32e7eSjoerg 
11306f32e7eSjoerg     assert (B->isCompoundAssignmentOp());
11406f32e7eSjoerg 
11506f32e7eSjoerg     switch (Op) {
11606f32e7eSjoerg       default:
11706f32e7eSjoerg         llvm_unreachable("Invalid opcode for compound assignment.");
11806f32e7eSjoerg       case BO_MulAssign: Op = BO_Mul; break;
11906f32e7eSjoerg       case BO_DivAssign: Op = BO_Div; break;
12006f32e7eSjoerg       case BO_RemAssign: Op = BO_Rem; break;
12106f32e7eSjoerg       case BO_AddAssign: Op = BO_Add; break;
12206f32e7eSjoerg       case BO_SubAssign: Op = BO_Sub; break;
12306f32e7eSjoerg       case BO_ShlAssign: Op = BO_Shl; break;
12406f32e7eSjoerg       case BO_ShrAssign: Op = BO_Shr; break;
12506f32e7eSjoerg       case BO_AndAssign: Op = BO_And; break;
12606f32e7eSjoerg       case BO_XorAssign: Op = BO_Xor; break;
12706f32e7eSjoerg       case BO_OrAssign:  Op = BO_Or;  break;
12806f32e7eSjoerg     }
12906f32e7eSjoerg 
13006f32e7eSjoerg     // Perform a load (the LHS).  This performs the checks for
13106f32e7eSjoerg     // null dereferences, and so on.
13206f32e7eSjoerg     ExplodedNodeSet Tmp;
13306f32e7eSjoerg     SVal location = LeftV;
13406f32e7eSjoerg     evalLoad(Tmp, B, LHS, *it, state, location);
13506f32e7eSjoerg 
13606f32e7eSjoerg     for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E;
13706f32e7eSjoerg          ++I) {
13806f32e7eSjoerg 
13906f32e7eSjoerg       state = (*I)->getState();
14006f32e7eSjoerg       const LocationContext *LCtx = (*I)->getLocationContext();
14106f32e7eSjoerg       SVal V = state->getSVal(LHS, LCtx);
14206f32e7eSjoerg 
14306f32e7eSjoerg       // Get the computation type.
14406f32e7eSjoerg       QualType CTy =
14506f32e7eSjoerg         cast<CompoundAssignOperator>(B)->getComputationResultType();
14606f32e7eSjoerg       CTy = getContext().getCanonicalType(CTy);
14706f32e7eSjoerg 
14806f32e7eSjoerg       QualType CLHSTy =
14906f32e7eSjoerg         cast<CompoundAssignOperator>(B)->getComputationLHSType();
15006f32e7eSjoerg       CLHSTy = getContext().getCanonicalType(CLHSTy);
15106f32e7eSjoerg 
15206f32e7eSjoerg       QualType LTy = getContext().getCanonicalType(LHS->getType());
15306f32e7eSjoerg 
15406f32e7eSjoerg       // Promote LHS.
15506f32e7eSjoerg       V = svalBuilder.evalCast(V, CLHSTy, LTy);
15606f32e7eSjoerg 
15706f32e7eSjoerg       // Compute the result of the operation.
15806f32e7eSjoerg       SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy),
15906f32e7eSjoerg                                          B->getType(), CTy);
16006f32e7eSjoerg 
16106f32e7eSjoerg       // EXPERIMENTAL: "Conjured" symbols.
16206f32e7eSjoerg       // FIXME: Handle structs.
16306f32e7eSjoerg 
16406f32e7eSjoerg       SVal LHSVal;
16506f32e7eSjoerg 
16606f32e7eSjoerg       if (Result.isUnknown()) {
16706f32e7eSjoerg         // The symbolic value is actually for the type of the left-hand side
16806f32e7eSjoerg         // expression, not the computation type, as this is the value the
16906f32e7eSjoerg         // LValue on the LHS will bind to.
17006f32e7eSjoerg         LHSVal = svalBuilder.conjureSymbolVal(nullptr, B->getRHS(), LCtx, LTy,
17106f32e7eSjoerg                                               currBldrCtx->blockCount());
17206f32e7eSjoerg         // However, we need to convert the symbol to the computation type.
17306f32e7eSjoerg         Result = svalBuilder.evalCast(LHSVal, CTy, LTy);
17406f32e7eSjoerg       }
17506f32e7eSjoerg       else {
17606f32e7eSjoerg         // The left-hand side may bind to a different value then the
17706f32e7eSjoerg         // computation type.
17806f32e7eSjoerg         LHSVal = svalBuilder.evalCast(Result, LTy, CTy);
17906f32e7eSjoerg       }
18006f32e7eSjoerg 
18106f32e7eSjoerg       // In C++, assignment and compound assignment operators return an
18206f32e7eSjoerg       // lvalue.
18306f32e7eSjoerg       if (B->isGLValue())
18406f32e7eSjoerg         state = state->BindExpr(B, LCtx, location);
18506f32e7eSjoerg       else
18606f32e7eSjoerg         state = state->BindExpr(B, LCtx, Result);
18706f32e7eSjoerg 
18806f32e7eSjoerg       evalStore(Tmp2, B, LHS, *I, state, location, LHSVal);
18906f32e7eSjoerg     }
19006f32e7eSjoerg   }
19106f32e7eSjoerg 
19206f32e7eSjoerg   // FIXME: postvisits eventually go in ::Visit()
19306f32e7eSjoerg   getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this);
19406f32e7eSjoerg }
19506f32e7eSjoerg 
VisitBlockExpr(const BlockExpr * BE,ExplodedNode * Pred,ExplodedNodeSet & Dst)19606f32e7eSjoerg void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred,
19706f32e7eSjoerg                                 ExplodedNodeSet &Dst) {
19806f32e7eSjoerg 
19906f32e7eSjoerg   CanQualType T = getContext().getCanonicalType(BE->getType());
20006f32e7eSjoerg 
20106f32e7eSjoerg   const BlockDecl *BD = BE->getBlockDecl();
20206f32e7eSjoerg   // Get the value of the block itself.
20306f32e7eSjoerg   SVal V = svalBuilder.getBlockPointer(BD, T,
20406f32e7eSjoerg                                        Pred->getLocationContext(),
20506f32e7eSjoerg                                        currBldrCtx->blockCount());
20606f32e7eSjoerg 
20706f32e7eSjoerg   ProgramStateRef State = Pred->getState();
20806f32e7eSjoerg 
20906f32e7eSjoerg   // If we created a new MemRegion for the block, we should explicitly bind
21006f32e7eSjoerg   // the captured variables.
21106f32e7eSjoerg   if (const BlockDataRegion *BDR =
21206f32e7eSjoerg       dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) {
21306f32e7eSjoerg 
21406f32e7eSjoerg     BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(),
21506f32e7eSjoerg                                               E = BDR->referenced_vars_end();
21606f32e7eSjoerg 
21706f32e7eSjoerg     auto CI = BD->capture_begin();
21806f32e7eSjoerg     auto CE = BD->capture_end();
21906f32e7eSjoerg     for (; I != E; ++I) {
22006f32e7eSjoerg       const VarRegion *capturedR = I.getCapturedRegion();
221*13fbcb42Sjoerg       const TypedValueRegion *originalR = I.getOriginalRegion();
22206f32e7eSjoerg 
22306f32e7eSjoerg       // If the capture had a copy expression, use the result of evaluating
22406f32e7eSjoerg       // that expression, otherwise use the original value.
22506f32e7eSjoerg       // We rely on the invariant that the block declaration's capture variables
22606f32e7eSjoerg       // are a prefix of the BlockDataRegion's referenced vars (which may include
22706f32e7eSjoerg       // referenced globals, etc.) to enable fast lookup of the capture for a
22806f32e7eSjoerg       // given referenced var.
22906f32e7eSjoerg       const Expr *copyExpr = nullptr;
23006f32e7eSjoerg       if (CI != CE) {
23106f32e7eSjoerg         assert(CI->getVariable() == capturedR->getDecl());
23206f32e7eSjoerg         copyExpr = CI->getCopyExpr();
23306f32e7eSjoerg         CI++;
23406f32e7eSjoerg       }
23506f32e7eSjoerg 
23606f32e7eSjoerg       if (capturedR != originalR) {
23706f32e7eSjoerg         SVal originalV;
23806f32e7eSjoerg         const LocationContext *LCtx = Pred->getLocationContext();
23906f32e7eSjoerg         if (copyExpr) {
24006f32e7eSjoerg           originalV = State->getSVal(copyExpr, LCtx);
24106f32e7eSjoerg         } else {
24206f32e7eSjoerg           originalV = State->getSVal(loc::MemRegionVal(originalR));
24306f32e7eSjoerg         }
24406f32e7eSjoerg         State = State->bindLoc(loc::MemRegionVal(capturedR), originalV, LCtx);
24506f32e7eSjoerg       }
24606f32e7eSjoerg     }
24706f32e7eSjoerg   }
24806f32e7eSjoerg 
24906f32e7eSjoerg   ExplodedNodeSet Tmp;
25006f32e7eSjoerg   StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
25106f32e7eSjoerg   Bldr.generateNode(BE, Pred,
25206f32e7eSjoerg                     State->BindExpr(BE, Pred->getLocationContext(), V),
25306f32e7eSjoerg                     nullptr, ProgramPoint::PostLValueKind);
25406f32e7eSjoerg 
25506f32e7eSjoerg   // FIXME: Move all post/pre visits to ::Visit().
25606f32e7eSjoerg   getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this);
25706f32e7eSjoerg }
25806f32e7eSjoerg 
handleLValueBitCast(ProgramStateRef state,const Expr * Ex,const LocationContext * LCtx,QualType T,QualType ExTy,const CastExpr * CastE,StmtNodeBuilder & Bldr,ExplodedNode * Pred)25906f32e7eSjoerg ProgramStateRef ExprEngine::handleLValueBitCast(
26006f32e7eSjoerg     ProgramStateRef state, const Expr* Ex, const LocationContext* LCtx,
26106f32e7eSjoerg     QualType T, QualType ExTy, const CastExpr* CastE, StmtNodeBuilder& Bldr,
26206f32e7eSjoerg     ExplodedNode* Pred) {
26306f32e7eSjoerg   if (T->isLValueReferenceType()) {
26406f32e7eSjoerg     assert(!CastE->getType()->isLValueReferenceType());
26506f32e7eSjoerg     ExTy = getContext().getLValueReferenceType(ExTy);
26606f32e7eSjoerg   } else if (T->isRValueReferenceType()) {
26706f32e7eSjoerg     assert(!CastE->getType()->isRValueReferenceType());
26806f32e7eSjoerg     ExTy = getContext().getRValueReferenceType(ExTy);
26906f32e7eSjoerg   }
27006f32e7eSjoerg   // Delegate to SValBuilder to process.
27106f32e7eSjoerg   SVal OrigV = state->getSVal(Ex, LCtx);
27206f32e7eSjoerg   SVal V = svalBuilder.evalCast(OrigV, T, ExTy);
27306f32e7eSjoerg   // Negate the result if we're treating the boolean as a signed i1
27406f32e7eSjoerg   if (CastE->getCastKind() == CK_BooleanToSignedIntegral)
27506f32e7eSjoerg     V = evalMinus(V);
27606f32e7eSjoerg   state = state->BindExpr(CastE, LCtx, V);
27706f32e7eSjoerg   if (V.isUnknown() && !OrigV.isUnknown()) {
278*13fbcb42Sjoerg     state = escapeValues(state, OrigV, PSK_EscapeOther);
27906f32e7eSjoerg   }
28006f32e7eSjoerg   Bldr.generateNode(CastE, Pred, state);
28106f32e7eSjoerg 
28206f32e7eSjoerg   return state;
28306f32e7eSjoerg }
28406f32e7eSjoerg 
handleLVectorSplat(ProgramStateRef state,const LocationContext * LCtx,const CastExpr * CastE,StmtNodeBuilder & Bldr,ExplodedNode * Pred)28506f32e7eSjoerg ProgramStateRef ExprEngine::handleLVectorSplat(
28606f32e7eSjoerg     ProgramStateRef state, const LocationContext* LCtx, const CastExpr* CastE,
28706f32e7eSjoerg     StmtNodeBuilder &Bldr, ExplodedNode* Pred) {
28806f32e7eSjoerg   // Recover some path sensitivity by conjuring a new value.
28906f32e7eSjoerg   QualType resultType = CastE->getType();
29006f32e7eSjoerg   if (CastE->isGLValue())
29106f32e7eSjoerg     resultType = getContext().getPointerType(resultType);
29206f32e7eSjoerg   SVal result = svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx,
29306f32e7eSjoerg                                              resultType,
29406f32e7eSjoerg                                              currBldrCtx->blockCount());
29506f32e7eSjoerg   state = state->BindExpr(CastE, LCtx, result);
29606f32e7eSjoerg   Bldr.generateNode(CastE, Pred, state);
29706f32e7eSjoerg 
29806f32e7eSjoerg   return state;
29906f32e7eSjoerg }
30006f32e7eSjoerg 
VisitCast(const CastExpr * CastE,const Expr * Ex,ExplodedNode * Pred,ExplodedNodeSet & Dst)30106f32e7eSjoerg void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
30206f32e7eSjoerg                            ExplodedNode *Pred, ExplodedNodeSet &Dst) {
30306f32e7eSjoerg 
30406f32e7eSjoerg   ExplodedNodeSet dstPreStmt;
30506f32e7eSjoerg   getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this);
30606f32e7eSjoerg 
30706f32e7eSjoerg   if (CastE->getCastKind() == CK_LValueToRValue) {
30806f32e7eSjoerg     for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
30906f32e7eSjoerg          I!=E; ++I) {
31006f32e7eSjoerg       ExplodedNode *subExprNode = *I;
31106f32e7eSjoerg       ProgramStateRef state = subExprNode->getState();
31206f32e7eSjoerg       const LocationContext *LCtx = subExprNode->getLocationContext();
31306f32e7eSjoerg       evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx));
31406f32e7eSjoerg     }
31506f32e7eSjoerg     return;
31606f32e7eSjoerg   }
31706f32e7eSjoerg 
31806f32e7eSjoerg   // All other casts.
31906f32e7eSjoerg   QualType T = CastE->getType();
32006f32e7eSjoerg   QualType ExTy = Ex->getType();
32106f32e7eSjoerg 
32206f32e7eSjoerg   if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE))
32306f32e7eSjoerg     T = ExCast->getTypeAsWritten();
32406f32e7eSjoerg 
32506f32e7eSjoerg   StmtNodeBuilder Bldr(dstPreStmt, Dst, *currBldrCtx);
32606f32e7eSjoerg   for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end();
32706f32e7eSjoerg        I != E; ++I) {
32806f32e7eSjoerg 
32906f32e7eSjoerg     Pred = *I;
33006f32e7eSjoerg     ProgramStateRef state = Pred->getState();
33106f32e7eSjoerg     const LocationContext *LCtx = Pred->getLocationContext();
33206f32e7eSjoerg 
33306f32e7eSjoerg     switch (CastE->getCastKind()) {
33406f32e7eSjoerg       case CK_LValueToRValue:
33506f32e7eSjoerg         llvm_unreachable("LValueToRValue casts handled earlier.");
33606f32e7eSjoerg       case CK_ToVoid:
33706f32e7eSjoerg         continue;
33806f32e7eSjoerg         // The analyzer doesn't do anything special with these casts,
33906f32e7eSjoerg         // since it understands retain/release semantics already.
34006f32e7eSjoerg       case CK_ARCProduceObject:
34106f32e7eSjoerg       case CK_ARCConsumeObject:
34206f32e7eSjoerg       case CK_ARCReclaimReturnedObject:
34306f32e7eSjoerg       case CK_ARCExtendBlockObject: // Fall-through.
34406f32e7eSjoerg       case CK_CopyAndAutoreleaseBlockObject:
34506f32e7eSjoerg         // The analyser can ignore atomic casts for now, although some future
34606f32e7eSjoerg         // checkers may want to make certain that you're not modifying the same
34706f32e7eSjoerg         // value through atomic and nonatomic pointers.
34806f32e7eSjoerg       case CK_AtomicToNonAtomic:
34906f32e7eSjoerg       case CK_NonAtomicToAtomic:
35006f32e7eSjoerg         // True no-ops.
35106f32e7eSjoerg       case CK_NoOp:
35206f32e7eSjoerg       case CK_ConstructorConversion:
35306f32e7eSjoerg       case CK_UserDefinedConversion:
35406f32e7eSjoerg       case CK_FunctionToPointerDecay:
35506f32e7eSjoerg       case CK_BuiltinFnToFnPtr: {
35606f32e7eSjoerg         // Copy the SVal of Ex to CastE.
35706f32e7eSjoerg         ProgramStateRef state = Pred->getState();
35806f32e7eSjoerg         const LocationContext *LCtx = Pred->getLocationContext();
35906f32e7eSjoerg         SVal V = state->getSVal(Ex, LCtx);
36006f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, V);
36106f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
36206f32e7eSjoerg         continue;
36306f32e7eSjoerg       }
36406f32e7eSjoerg       case CK_MemberPointerToBoolean:
36506f32e7eSjoerg       case CK_PointerToBoolean: {
36606f32e7eSjoerg         SVal V = state->getSVal(Ex, LCtx);
36706f32e7eSjoerg         auto PTMSV = V.getAs<nonloc::PointerToMember>();
36806f32e7eSjoerg         if (PTMSV)
36906f32e7eSjoerg           V = svalBuilder.makeTruthVal(!PTMSV->isNullMemberPointer(), ExTy);
37006f32e7eSjoerg         if (V.isUndef() || PTMSV) {
37106f32e7eSjoerg           state = state->BindExpr(CastE, LCtx, V);
37206f32e7eSjoerg           Bldr.generateNode(CastE, Pred, state);
37306f32e7eSjoerg           continue;
37406f32e7eSjoerg         }
37506f32e7eSjoerg         // Explicitly proceed with default handler for this case cascade.
37606f32e7eSjoerg         state =
37706f32e7eSjoerg             handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
37806f32e7eSjoerg         continue;
37906f32e7eSjoerg       }
38006f32e7eSjoerg       case CK_Dependent:
38106f32e7eSjoerg       case CK_ArrayToPointerDecay:
38206f32e7eSjoerg       case CK_BitCast:
38306f32e7eSjoerg       case CK_LValueToRValueBitCast:
38406f32e7eSjoerg       case CK_AddressSpaceConversion:
38506f32e7eSjoerg       case CK_BooleanToSignedIntegral:
38606f32e7eSjoerg       case CK_IntegralToPointer:
38706f32e7eSjoerg       case CK_PointerToIntegral: {
38806f32e7eSjoerg         SVal V = state->getSVal(Ex, LCtx);
38906f32e7eSjoerg         if (V.getAs<nonloc::PointerToMember>()) {
39006f32e7eSjoerg           state = state->BindExpr(CastE, LCtx, UnknownVal());
39106f32e7eSjoerg           Bldr.generateNode(CastE, Pred, state);
39206f32e7eSjoerg           continue;
39306f32e7eSjoerg         }
39406f32e7eSjoerg         // Explicitly proceed with default handler for this case cascade.
39506f32e7eSjoerg         state =
39606f32e7eSjoerg             handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
39706f32e7eSjoerg         continue;
39806f32e7eSjoerg       }
39906f32e7eSjoerg       case CK_IntegralToBoolean:
40006f32e7eSjoerg       case CK_IntegralToFloating:
40106f32e7eSjoerg       case CK_FloatingToIntegral:
40206f32e7eSjoerg       case CK_FloatingToBoolean:
40306f32e7eSjoerg       case CK_FloatingCast:
40406f32e7eSjoerg       case CK_FloatingRealToComplex:
40506f32e7eSjoerg       case CK_FloatingComplexToReal:
40606f32e7eSjoerg       case CK_FloatingComplexToBoolean:
40706f32e7eSjoerg       case CK_FloatingComplexCast:
40806f32e7eSjoerg       case CK_FloatingComplexToIntegralComplex:
40906f32e7eSjoerg       case CK_IntegralRealToComplex:
41006f32e7eSjoerg       case CK_IntegralComplexToReal:
41106f32e7eSjoerg       case CK_IntegralComplexToBoolean:
41206f32e7eSjoerg       case CK_IntegralComplexCast:
41306f32e7eSjoerg       case CK_IntegralComplexToFloatingComplex:
41406f32e7eSjoerg       case CK_CPointerToObjCPointerCast:
41506f32e7eSjoerg       case CK_BlockPointerToObjCPointerCast:
41606f32e7eSjoerg       case CK_AnyPointerToBlockPointerCast:
41706f32e7eSjoerg       case CK_ObjCObjectLValueCast:
41806f32e7eSjoerg       case CK_ZeroToOCLOpaqueType:
41906f32e7eSjoerg       case CK_IntToOCLSampler:
42006f32e7eSjoerg       case CK_LValueBitCast:
421*13fbcb42Sjoerg       case CK_FloatingToFixedPoint:
422*13fbcb42Sjoerg       case CK_FixedPointToFloating:
42306f32e7eSjoerg       case CK_FixedPointCast:
42406f32e7eSjoerg       case CK_FixedPointToBoolean:
42506f32e7eSjoerg       case CK_FixedPointToIntegral:
42606f32e7eSjoerg       case CK_IntegralToFixedPoint: {
42706f32e7eSjoerg         state =
42806f32e7eSjoerg             handleLValueBitCast(state, Ex, LCtx, T, ExTy, CastE, Bldr, Pred);
42906f32e7eSjoerg         continue;
43006f32e7eSjoerg       }
43106f32e7eSjoerg       case CK_IntegralCast: {
43206f32e7eSjoerg         // Delegate to SValBuilder to process.
43306f32e7eSjoerg         SVal V = state->getSVal(Ex, LCtx);
43406f32e7eSjoerg         V = svalBuilder.evalIntegralCast(state, V, T, ExTy);
43506f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, V);
43606f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
43706f32e7eSjoerg         continue;
43806f32e7eSjoerg       }
43906f32e7eSjoerg       case CK_DerivedToBase:
44006f32e7eSjoerg       case CK_UncheckedDerivedToBase: {
44106f32e7eSjoerg         // For DerivedToBase cast, delegate to the store manager.
44206f32e7eSjoerg         SVal val = state->getSVal(Ex, LCtx);
44306f32e7eSjoerg         val = getStoreManager().evalDerivedToBase(val, CastE);
44406f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, val);
44506f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
44606f32e7eSjoerg         continue;
44706f32e7eSjoerg       }
44806f32e7eSjoerg       // Handle C++ dyn_cast.
44906f32e7eSjoerg       case CK_Dynamic: {
45006f32e7eSjoerg         SVal val = state->getSVal(Ex, LCtx);
45106f32e7eSjoerg 
45206f32e7eSjoerg         // Compute the type of the result.
45306f32e7eSjoerg         QualType resultType = CastE->getType();
45406f32e7eSjoerg         if (CastE->isGLValue())
45506f32e7eSjoerg           resultType = getContext().getPointerType(resultType);
45606f32e7eSjoerg 
45706f32e7eSjoerg         bool Failed = false;
45806f32e7eSjoerg 
45906f32e7eSjoerg         // Check if the value being cast evaluates to 0.
46006f32e7eSjoerg         if (val.isZeroConstant())
46106f32e7eSjoerg           Failed = true;
46206f32e7eSjoerg         // Else, evaluate the cast.
46306f32e7eSjoerg         else
46406f32e7eSjoerg           val = getStoreManager().attemptDownCast(val, T, Failed);
46506f32e7eSjoerg 
46606f32e7eSjoerg         if (Failed) {
46706f32e7eSjoerg           if (T->isReferenceType()) {
46806f32e7eSjoerg             // A bad_cast exception is thrown if input value is a reference.
46906f32e7eSjoerg             // Currently, we model this, by generating a sink.
47006f32e7eSjoerg             Bldr.generateSink(CastE, Pred, state);
47106f32e7eSjoerg             continue;
47206f32e7eSjoerg           } else {
47306f32e7eSjoerg             // If the cast fails on a pointer, bind to 0.
47406f32e7eSjoerg             state = state->BindExpr(CastE, LCtx, svalBuilder.makeNull());
47506f32e7eSjoerg           }
47606f32e7eSjoerg         } else {
47706f32e7eSjoerg           // If we don't know if the cast succeeded, conjure a new symbol.
47806f32e7eSjoerg           if (val.isUnknown()) {
47906f32e7eSjoerg             DefinedOrUnknownSVal NewSym =
48006f32e7eSjoerg               svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
48106f32e7eSjoerg                                            currBldrCtx->blockCount());
48206f32e7eSjoerg             state = state->BindExpr(CastE, LCtx, NewSym);
48306f32e7eSjoerg           } else
48406f32e7eSjoerg             // Else, bind to the derived region value.
48506f32e7eSjoerg             state = state->BindExpr(CastE, LCtx, val);
48606f32e7eSjoerg         }
48706f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
48806f32e7eSjoerg         continue;
48906f32e7eSjoerg       }
49006f32e7eSjoerg       case CK_BaseToDerived: {
49106f32e7eSjoerg         SVal val = state->getSVal(Ex, LCtx);
49206f32e7eSjoerg         QualType resultType = CastE->getType();
49306f32e7eSjoerg         if (CastE->isGLValue())
49406f32e7eSjoerg           resultType = getContext().getPointerType(resultType);
49506f32e7eSjoerg 
49606f32e7eSjoerg         bool Failed = false;
49706f32e7eSjoerg 
49806f32e7eSjoerg         if (!val.isConstant()) {
49906f32e7eSjoerg           val = getStoreManager().attemptDownCast(val, T, Failed);
50006f32e7eSjoerg         }
50106f32e7eSjoerg 
50206f32e7eSjoerg         // Failed to cast or the result is unknown, fall back to conservative.
50306f32e7eSjoerg         if (Failed || val.isUnknown()) {
50406f32e7eSjoerg           val =
50506f32e7eSjoerg             svalBuilder.conjureSymbolVal(nullptr, CastE, LCtx, resultType,
50606f32e7eSjoerg                                          currBldrCtx->blockCount());
50706f32e7eSjoerg         }
50806f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, val);
50906f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
51006f32e7eSjoerg         continue;
51106f32e7eSjoerg       }
51206f32e7eSjoerg       case CK_NullToPointer: {
51306f32e7eSjoerg         SVal V = svalBuilder.makeNull();
51406f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, V);
51506f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
51606f32e7eSjoerg         continue;
51706f32e7eSjoerg       }
51806f32e7eSjoerg       case CK_NullToMemberPointer: {
51906f32e7eSjoerg         SVal V = svalBuilder.getMemberPointer(nullptr);
52006f32e7eSjoerg         state = state->BindExpr(CastE, LCtx, V);
52106f32e7eSjoerg         Bldr.generateNode(CastE, Pred, state);
52206f32e7eSjoerg         continue;
52306f32e7eSjoerg       }
52406f32e7eSjoerg       case CK_DerivedToBaseMemberPointer:
52506f32e7eSjoerg       case CK_BaseToDerivedMemberPointer:
52606f32e7eSjoerg       case CK_ReinterpretMemberPointer: {
52706f32e7eSjoerg         SVal V = state->getSVal(Ex, LCtx);
52806f32e7eSjoerg         if (auto PTMSV = V.getAs<nonloc::PointerToMember>()) {
529*13fbcb42Sjoerg           SVal CastedPTMSV =
530*13fbcb42Sjoerg               svalBuilder.makePointerToMember(getBasicVals().accumCXXBase(
531*13fbcb42Sjoerg                   CastE->path(), *PTMSV, CastE->getCastKind()));
53206f32e7eSjoerg           state = state->BindExpr(CastE, LCtx, CastedPTMSV);
53306f32e7eSjoerg           Bldr.generateNode(CastE, Pred, state);
53406f32e7eSjoerg           continue;
53506f32e7eSjoerg         }
53606f32e7eSjoerg         // Explicitly proceed with default handler for this case cascade.
53706f32e7eSjoerg         state = handleLVectorSplat(state, LCtx, CastE, Bldr, Pred);
53806f32e7eSjoerg         continue;
53906f32e7eSjoerg       }
54006f32e7eSjoerg       // Various C++ casts that are not handled yet.
54106f32e7eSjoerg       case CK_ToUnion:
54206f32e7eSjoerg       case CK_VectorSplat: {
54306f32e7eSjoerg         state = handleLVectorSplat(state, LCtx, CastE, Bldr, Pred);
54406f32e7eSjoerg         continue;
54506f32e7eSjoerg       }
546*13fbcb42Sjoerg       case CK_MatrixCast: {
547*13fbcb42Sjoerg         // TODO: Handle MatrixCast here.
548*13fbcb42Sjoerg         continue;
549*13fbcb42Sjoerg       }
55006f32e7eSjoerg     }
55106f32e7eSjoerg   }
55206f32e7eSjoerg }
55306f32e7eSjoerg 
VisitCompoundLiteralExpr(const CompoundLiteralExpr * CL,ExplodedNode * Pred,ExplodedNodeSet & Dst)55406f32e7eSjoerg void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL,
55506f32e7eSjoerg                                           ExplodedNode *Pred,
55606f32e7eSjoerg                                           ExplodedNodeSet &Dst) {
55706f32e7eSjoerg   StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
55806f32e7eSjoerg 
55906f32e7eSjoerg   ProgramStateRef State = Pred->getState();
56006f32e7eSjoerg   const LocationContext *LCtx = Pred->getLocationContext();
56106f32e7eSjoerg 
56206f32e7eSjoerg   const Expr *Init = CL->getInitializer();
56306f32e7eSjoerg   SVal V = State->getSVal(CL->getInitializer(), LCtx);
56406f32e7eSjoerg 
56506f32e7eSjoerg   if (isa<CXXConstructExpr>(Init) || isa<CXXStdInitializerListExpr>(Init)) {
56606f32e7eSjoerg     // No work needed. Just pass the value up to this expression.
56706f32e7eSjoerg   } else {
56806f32e7eSjoerg     assert(isa<InitListExpr>(Init));
56906f32e7eSjoerg     Loc CLLoc = State->getLValue(CL, LCtx);
57006f32e7eSjoerg     State = State->bindLoc(CLLoc, V, LCtx);
57106f32e7eSjoerg 
57206f32e7eSjoerg     if (CL->isGLValue())
57306f32e7eSjoerg       V = CLLoc;
57406f32e7eSjoerg   }
57506f32e7eSjoerg 
57606f32e7eSjoerg   B.generateNode(CL, Pred, State->BindExpr(CL, LCtx, V));
57706f32e7eSjoerg }
57806f32e7eSjoerg 
VisitDeclStmt(const DeclStmt * DS,ExplodedNode * Pred,ExplodedNodeSet & Dst)57906f32e7eSjoerg void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred,
58006f32e7eSjoerg                                ExplodedNodeSet &Dst) {
581*13fbcb42Sjoerg   if (isa<TypedefNameDecl>(*DS->decl_begin())) {
582*13fbcb42Sjoerg     // C99 6.7.7 "Any array size expressions associated with variable length
583*13fbcb42Sjoerg     // array declarators are evaluated each time the declaration of the typedef
584*13fbcb42Sjoerg     // name is reached in the order of execution."
585*13fbcb42Sjoerg     // The checkers should know about typedef to be able to handle VLA size
586*13fbcb42Sjoerg     // expressions.
587*13fbcb42Sjoerg     ExplodedNodeSet DstPre;
588*13fbcb42Sjoerg     getCheckerManager().runCheckersForPreStmt(DstPre, Pred, DS, *this);
589*13fbcb42Sjoerg     getCheckerManager().runCheckersForPostStmt(Dst, DstPre, DS, *this);
590*13fbcb42Sjoerg     return;
591*13fbcb42Sjoerg   }
592*13fbcb42Sjoerg 
59306f32e7eSjoerg   // Assumption: The CFG has one DeclStmt per Decl.
59406f32e7eSjoerg   const VarDecl *VD = dyn_cast_or_null<VarDecl>(*DS->decl_begin());
59506f32e7eSjoerg 
59606f32e7eSjoerg   if (!VD) {
59706f32e7eSjoerg     //TODO:AZ: remove explicit insertion after refactoring is done.
59806f32e7eSjoerg     Dst.insert(Pred);
59906f32e7eSjoerg     return;
60006f32e7eSjoerg   }
60106f32e7eSjoerg 
60206f32e7eSjoerg   // FIXME: all pre/post visits should eventually be handled by ::Visit().
60306f32e7eSjoerg   ExplodedNodeSet dstPreVisit;
60406f32e7eSjoerg   getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this);
60506f32e7eSjoerg 
60606f32e7eSjoerg   ExplodedNodeSet dstEvaluated;
60706f32e7eSjoerg   StmtNodeBuilder B(dstPreVisit, dstEvaluated, *currBldrCtx);
60806f32e7eSjoerg   for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end();
60906f32e7eSjoerg        I!=E; ++I) {
61006f32e7eSjoerg     ExplodedNode *N = *I;
61106f32e7eSjoerg     ProgramStateRef state = N->getState();
61206f32e7eSjoerg     const LocationContext *LC = N->getLocationContext();
61306f32e7eSjoerg 
61406f32e7eSjoerg     // Decls without InitExpr are not initialized explicitly.
61506f32e7eSjoerg     if (const Expr *InitEx = VD->getInit()) {
61606f32e7eSjoerg 
61706f32e7eSjoerg       // Note in the state that the initialization has occurred.
61806f32e7eSjoerg       ExplodedNode *UpdatedN = N;
61906f32e7eSjoerg       SVal InitVal = state->getSVal(InitEx, LC);
62006f32e7eSjoerg 
62106f32e7eSjoerg       assert(DS->isSingleDecl());
62206f32e7eSjoerg       if (getObjectUnderConstruction(state, DS, LC)) {
62306f32e7eSjoerg         state = finishObjectConstruction(state, DS, LC);
62406f32e7eSjoerg         // We constructed the object directly in the variable.
62506f32e7eSjoerg         // No need to bind anything.
62606f32e7eSjoerg         B.generateNode(DS, UpdatedN, state);
62706f32e7eSjoerg       } else {
62806f32e7eSjoerg         // Recover some path-sensitivity if a scalar value evaluated to
62906f32e7eSjoerg         // UnknownVal.
63006f32e7eSjoerg         if (InitVal.isUnknown()) {
63106f32e7eSjoerg           QualType Ty = InitEx->getType();
63206f32e7eSjoerg           if (InitEx->isGLValue()) {
63306f32e7eSjoerg             Ty = getContext().getPointerType(Ty);
63406f32e7eSjoerg           }
63506f32e7eSjoerg 
63606f32e7eSjoerg           InitVal = svalBuilder.conjureSymbolVal(nullptr, InitEx, LC, Ty,
63706f32e7eSjoerg                                                  currBldrCtx->blockCount());
63806f32e7eSjoerg         }
63906f32e7eSjoerg 
64006f32e7eSjoerg 
64106f32e7eSjoerg         B.takeNodes(UpdatedN);
64206f32e7eSjoerg         ExplodedNodeSet Dst2;
64306f32e7eSjoerg         evalBind(Dst2, DS, UpdatedN, state->getLValue(VD, LC), InitVal, true);
64406f32e7eSjoerg         B.addNodes(Dst2);
64506f32e7eSjoerg       }
64606f32e7eSjoerg     }
64706f32e7eSjoerg     else {
64806f32e7eSjoerg       B.generateNode(DS, N, state);
64906f32e7eSjoerg     }
65006f32e7eSjoerg   }
65106f32e7eSjoerg 
65206f32e7eSjoerg   getCheckerManager().runCheckersForPostStmt(Dst, B.getResults(), DS, *this);
65306f32e7eSjoerg }
65406f32e7eSjoerg 
VisitLogicalExpr(const BinaryOperator * B,ExplodedNode * Pred,ExplodedNodeSet & Dst)65506f32e7eSjoerg void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred,
65606f32e7eSjoerg                                   ExplodedNodeSet &Dst) {
65706f32e7eSjoerg   // This method acts upon CFG elements for logical operators && and ||
65806f32e7eSjoerg   // and attaches the value (true or false) to them as expressions.
65906f32e7eSjoerg   // It doesn't produce any state splits.
66006f32e7eSjoerg   // If we made it that far, we're past the point when we modeled the short
66106f32e7eSjoerg   // circuit. It means that we should have precise knowledge about whether
66206f32e7eSjoerg   // we've short-circuited. If we did, we already know the value we need to
66306f32e7eSjoerg   // bind. If we didn't, the value of the RHS (casted to the boolean type)
66406f32e7eSjoerg   // is the answer.
66506f32e7eSjoerg   // Currently this method tries to figure out whether we've short-circuited
66606f32e7eSjoerg   // by looking at the ExplodedGraph. This method is imperfect because there
66706f32e7eSjoerg   // could inevitably have been merges that would have resulted in multiple
66806f32e7eSjoerg   // potential path traversal histories. We bail out when we fail.
66906f32e7eSjoerg   // Due to this ambiguity, a more reliable solution would have been to
67006f32e7eSjoerg   // track the short circuit operation history path-sensitively until
67106f32e7eSjoerg   // we evaluate the respective logical operator.
67206f32e7eSjoerg   assert(B->getOpcode() == BO_LAnd ||
67306f32e7eSjoerg          B->getOpcode() == BO_LOr);
67406f32e7eSjoerg 
67506f32e7eSjoerg   StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
67606f32e7eSjoerg   ProgramStateRef state = Pred->getState();
67706f32e7eSjoerg 
67806f32e7eSjoerg   if (B->getType()->isVectorType()) {
67906f32e7eSjoerg     // FIXME: We do not model vector arithmetic yet. When adding support for
68006f32e7eSjoerg     // that, note that the CFG-based reasoning below does not apply, because
68106f32e7eSjoerg     // logical operators on vectors are not short-circuit. Currently they are
68206f32e7eSjoerg     // modeled as short-circuit in Clang CFG but this is incorrect.
68306f32e7eSjoerg     // Do not set the value for the expression. It'd be UnknownVal by default.
68406f32e7eSjoerg     Bldr.generateNode(B, Pred, state);
68506f32e7eSjoerg     return;
68606f32e7eSjoerg   }
68706f32e7eSjoerg 
68806f32e7eSjoerg   ExplodedNode *N = Pred;
68906f32e7eSjoerg   while (!N->getLocation().getAs<BlockEntrance>()) {
69006f32e7eSjoerg     ProgramPoint P = N->getLocation();
69106f32e7eSjoerg     assert(P.getAs<PreStmt>()|| P.getAs<PreStmtPurgeDeadSymbols>());
69206f32e7eSjoerg     (void) P;
69306f32e7eSjoerg     if (N->pred_size() != 1) {
69406f32e7eSjoerg       // We failed to track back where we came from.
69506f32e7eSjoerg       Bldr.generateNode(B, Pred, state);
69606f32e7eSjoerg       return;
69706f32e7eSjoerg     }
69806f32e7eSjoerg     N = *N->pred_begin();
69906f32e7eSjoerg   }
70006f32e7eSjoerg 
70106f32e7eSjoerg   if (N->pred_size() != 1) {
70206f32e7eSjoerg     // We failed to track back where we came from.
70306f32e7eSjoerg     Bldr.generateNode(B, Pred, state);
70406f32e7eSjoerg     return;
70506f32e7eSjoerg   }
70606f32e7eSjoerg 
70706f32e7eSjoerg   N = *N->pred_begin();
70806f32e7eSjoerg   BlockEdge BE = N->getLocation().castAs<BlockEdge>();
70906f32e7eSjoerg   SVal X;
71006f32e7eSjoerg 
71106f32e7eSjoerg   // Determine the value of the expression by introspecting how we
71206f32e7eSjoerg   // got this location in the CFG.  This requires looking at the previous
71306f32e7eSjoerg   // block we were in and what kind of control-flow transfer was involved.
71406f32e7eSjoerg   const CFGBlock *SrcBlock = BE.getSrc();
71506f32e7eSjoerg   // The only terminator (if there is one) that makes sense is a logical op.
71606f32e7eSjoerg   CFGTerminator T = SrcBlock->getTerminator();
71706f32e7eSjoerg   if (const BinaryOperator *Term = cast_or_null<BinaryOperator>(T.getStmt())) {
71806f32e7eSjoerg     (void) Term;
71906f32e7eSjoerg     assert(Term->isLogicalOp());
72006f32e7eSjoerg     assert(SrcBlock->succ_size() == 2);
72106f32e7eSjoerg     // Did we take the true or false branch?
72206f32e7eSjoerg     unsigned constant = (*SrcBlock->succ_begin() == BE.getDst()) ? 1 : 0;
72306f32e7eSjoerg     X = svalBuilder.makeIntVal(constant, B->getType());
72406f32e7eSjoerg   }
72506f32e7eSjoerg   else {
72606f32e7eSjoerg     // If there is no terminator, by construction the last statement
72706f32e7eSjoerg     // in SrcBlock is the value of the enclosing expression.
72806f32e7eSjoerg     // However, we still need to constrain that value to be 0 or 1.
72906f32e7eSjoerg     assert(!SrcBlock->empty());
73006f32e7eSjoerg     CFGStmt Elem = SrcBlock->rbegin()->castAs<CFGStmt>();
73106f32e7eSjoerg     const Expr *RHS = cast<Expr>(Elem.getStmt());
73206f32e7eSjoerg     SVal RHSVal = N->getState()->getSVal(RHS, Pred->getLocationContext());
73306f32e7eSjoerg 
73406f32e7eSjoerg     if (RHSVal.isUndef()) {
73506f32e7eSjoerg       X = RHSVal;
73606f32e7eSjoerg     } else {
73706f32e7eSjoerg       // We evaluate "RHSVal != 0" expression which result in 0 if the value is
73806f32e7eSjoerg       // known to be false, 1 if the value is known to be true and a new symbol
73906f32e7eSjoerg       // when the assumption is unknown.
74006f32e7eSjoerg       nonloc::ConcreteInt Zero(getBasicVals().getValue(0, B->getType()));
74106f32e7eSjoerg       X = evalBinOp(N->getState(), BO_NE,
74206f32e7eSjoerg                     svalBuilder.evalCast(RHSVal, B->getType(), RHS->getType()),
74306f32e7eSjoerg                     Zero, B->getType());
74406f32e7eSjoerg     }
74506f32e7eSjoerg   }
74606f32e7eSjoerg   Bldr.generateNode(B, Pred, state->BindExpr(B, Pred->getLocationContext(), X));
74706f32e7eSjoerg }
74806f32e7eSjoerg 
VisitInitListExpr(const InitListExpr * IE,ExplodedNode * Pred,ExplodedNodeSet & Dst)74906f32e7eSjoerg void ExprEngine::VisitInitListExpr(const InitListExpr *IE,
75006f32e7eSjoerg                                    ExplodedNode *Pred,
75106f32e7eSjoerg                                    ExplodedNodeSet &Dst) {
75206f32e7eSjoerg   StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
75306f32e7eSjoerg 
75406f32e7eSjoerg   ProgramStateRef state = Pred->getState();
75506f32e7eSjoerg   const LocationContext *LCtx = Pred->getLocationContext();
75606f32e7eSjoerg   QualType T = getContext().getCanonicalType(IE->getType());
75706f32e7eSjoerg   unsigned NumInitElements = IE->getNumInits();
75806f32e7eSjoerg 
75906f32e7eSjoerg   if (!IE->isGLValue() && !IE->isTransparent() &&
76006f32e7eSjoerg       (T->isArrayType() || T->isRecordType() || T->isVectorType() ||
76106f32e7eSjoerg        T->isAnyComplexType())) {
76206f32e7eSjoerg     llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList();
76306f32e7eSjoerg 
76406f32e7eSjoerg     // Handle base case where the initializer has no elements.
76506f32e7eSjoerg     // e.g: static int* myArray[] = {};
76606f32e7eSjoerg     if (NumInitElements == 0) {
76706f32e7eSjoerg       SVal V = svalBuilder.makeCompoundVal(T, vals);
76806f32e7eSjoerg       B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
76906f32e7eSjoerg       return;
77006f32e7eSjoerg     }
77106f32e7eSjoerg 
77206f32e7eSjoerg     for (InitListExpr::const_reverse_iterator it = IE->rbegin(),
77306f32e7eSjoerg          ei = IE->rend(); it != ei; ++it) {
77406f32e7eSjoerg       SVal V = state->getSVal(cast<Expr>(*it), LCtx);
77506f32e7eSjoerg       vals = getBasicVals().prependSVal(V, vals);
77606f32e7eSjoerg     }
77706f32e7eSjoerg 
77806f32e7eSjoerg     B.generateNode(IE, Pred,
77906f32e7eSjoerg                    state->BindExpr(IE, LCtx,
78006f32e7eSjoerg                                    svalBuilder.makeCompoundVal(T, vals)));
78106f32e7eSjoerg     return;
78206f32e7eSjoerg   }
78306f32e7eSjoerg 
78406f32e7eSjoerg   // Handle scalars: int{5} and int{} and GLvalues.
78506f32e7eSjoerg   // Note, if the InitListExpr is a GLvalue, it means that there is an address
78606f32e7eSjoerg   // representing it, so it must have a single init element.
78706f32e7eSjoerg   assert(NumInitElements <= 1);
78806f32e7eSjoerg 
78906f32e7eSjoerg   SVal V;
79006f32e7eSjoerg   if (NumInitElements == 0)
79106f32e7eSjoerg     V = getSValBuilder().makeZeroVal(T);
79206f32e7eSjoerg   else
79306f32e7eSjoerg     V = state->getSVal(IE->getInit(0), LCtx);
79406f32e7eSjoerg 
79506f32e7eSjoerg   B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V));
79606f32e7eSjoerg }
79706f32e7eSjoerg 
VisitGuardedExpr(const Expr * Ex,const Expr * L,const Expr * R,ExplodedNode * Pred,ExplodedNodeSet & Dst)79806f32e7eSjoerg void ExprEngine::VisitGuardedExpr(const Expr *Ex,
79906f32e7eSjoerg                                   const Expr *L,
80006f32e7eSjoerg                                   const Expr *R,
80106f32e7eSjoerg                                   ExplodedNode *Pred,
80206f32e7eSjoerg                                   ExplodedNodeSet &Dst) {
80306f32e7eSjoerg   assert(L && R);
80406f32e7eSjoerg 
80506f32e7eSjoerg   StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
80606f32e7eSjoerg   ProgramStateRef state = Pred->getState();
80706f32e7eSjoerg   const LocationContext *LCtx = Pred->getLocationContext();
80806f32e7eSjoerg   const CFGBlock *SrcBlock = nullptr;
80906f32e7eSjoerg 
81006f32e7eSjoerg   // Find the predecessor block.
81106f32e7eSjoerg   ProgramStateRef SrcState = state;
81206f32e7eSjoerg   for (const ExplodedNode *N = Pred ; N ; N = *N->pred_begin()) {
81306f32e7eSjoerg     ProgramPoint PP = N->getLocation();
81406f32e7eSjoerg     if (PP.getAs<PreStmtPurgeDeadSymbols>() || PP.getAs<BlockEntrance>()) {
81506f32e7eSjoerg       // If the state N has multiple predecessors P, it means that successors
81606f32e7eSjoerg       // of P are all equivalent.
81706f32e7eSjoerg       // In turn, that means that all nodes at P are equivalent in terms
81806f32e7eSjoerg       // of observable behavior at N, and we can follow any of them.
81906f32e7eSjoerg       // FIXME: a more robust solution which does not walk up the tree.
82006f32e7eSjoerg       continue;
82106f32e7eSjoerg     }
82206f32e7eSjoerg     SrcBlock = PP.castAs<BlockEdge>().getSrc();
82306f32e7eSjoerg     SrcState = N->getState();
82406f32e7eSjoerg     break;
82506f32e7eSjoerg   }
82606f32e7eSjoerg 
82706f32e7eSjoerg   assert(SrcBlock && "missing function entry");
82806f32e7eSjoerg 
82906f32e7eSjoerg   // Find the last expression in the predecessor block.  That is the
83006f32e7eSjoerg   // expression that is used for the value of the ternary expression.
83106f32e7eSjoerg   bool hasValue = false;
83206f32e7eSjoerg   SVal V;
83306f32e7eSjoerg 
83406f32e7eSjoerg   for (CFGElement CE : llvm::reverse(*SrcBlock)) {
83506f32e7eSjoerg     if (Optional<CFGStmt> CS = CE.getAs<CFGStmt>()) {
83606f32e7eSjoerg       const Expr *ValEx = cast<Expr>(CS->getStmt());
83706f32e7eSjoerg       ValEx = ValEx->IgnoreParens();
83806f32e7eSjoerg 
83906f32e7eSjoerg       // For GNU extension '?:' operator, the left hand side will be an
84006f32e7eSjoerg       // OpaqueValueExpr, so get the underlying expression.
84106f32e7eSjoerg       if (const OpaqueValueExpr *OpaqueEx = dyn_cast<OpaqueValueExpr>(L))
84206f32e7eSjoerg         L = OpaqueEx->getSourceExpr();
84306f32e7eSjoerg 
84406f32e7eSjoerg       // If the last expression in the predecessor block matches true or false
84506f32e7eSjoerg       // subexpression, get its the value.
84606f32e7eSjoerg       if (ValEx == L->IgnoreParens() || ValEx == R->IgnoreParens()) {
84706f32e7eSjoerg         hasValue = true;
84806f32e7eSjoerg         V = SrcState->getSVal(ValEx, LCtx);
84906f32e7eSjoerg       }
85006f32e7eSjoerg       break;
85106f32e7eSjoerg     }
85206f32e7eSjoerg   }
85306f32e7eSjoerg 
85406f32e7eSjoerg   if (!hasValue)
85506f32e7eSjoerg     V = svalBuilder.conjureSymbolVal(nullptr, Ex, LCtx,
85606f32e7eSjoerg                                      currBldrCtx->blockCount());
85706f32e7eSjoerg 
85806f32e7eSjoerg   // Generate a new node with the binding from the appropriate path.
85906f32e7eSjoerg   B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, V, true));
86006f32e7eSjoerg }
86106f32e7eSjoerg 
86206f32e7eSjoerg void ExprEngine::
VisitOffsetOfExpr(const OffsetOfExpr * OOE,ExplodedNode * Pred,ExplodedNodeSet & Dst)86306f32e7eSjoerg VisitOffsetOfExpr(const OffsetOfExpr *OOE,
86406f32e7eSjoerg                   ExplodedNode *Pred, ExplodedNodeSet &Dst) {
86506f32e7eSjoerg   StmtNodeBuilder B(Pred, Dst, *currBldrCtx);
86606f32e7eSjoerg   Expr::EvalResult Result;
86706f32e7eSjoerg   if (OOE->EvaluateAsInt(Result, getContext())) {
86806f32e7eSjoerg     APSInt IV = Result.Val.getInt();
86906f32e7eSjoerg     assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType()));
87006f32e7eSjoerg     assert(OOE->getType()->castAs<BuiltinType>()->isInteger());
87106f32e7eSjoerg     assert(IV.isSigned() == OOE->getType()->isSignedIntegerType());
87206f32e7eSjoerg     SVal X = svalBuilder.makeIntVal(IV);
87306f32e7eSjoerg     B.generateNode(OOE, Pred,
87406f32e7eSjoerg                    Pred->getState()->BindExpr(OOE, Pred->getLocationContext(),
87506f32e7eSjoerg                                               X));
87606f32e7eSjoerg   }
87706f32e7eSjoerg   // FIXME: Handle the case where __builtin_offsetof is not a constant.
87806f32e7eSjoerg }
87906f32e7eSjoerg 
88006f32e7eSjoerg 
88106f32e7eSjoerg void ExprEngine::
VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr * Ex,ExplodedNode * Pred,ExplodedNodeSet & Dst)88206f32e7eSjoerg VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex,
88306f32e7eSjoerg                               ExplodedNode *Pred,
88406f32e7eSjoerg                               ExplodedNodeSet &Dst) {
88506f32e7eSjoerg   // FIXME: Prechecks eventually go in ::Visit().
88606f32e7eSjoerg   ExplodedNodeSet CheckedSet;
88706f32e7eSjoerg   getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, Ex, *this);
88806f32e7eSjoerg 
88906f32e7eSjoerg   ExplodedNodeSet EvalSet;
89006f32e7eSjoerg   StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
89106f32e7eSjoerg 
89206f32e7eSjoerg   QualType T = Ex->getTypeOfArgument();
89306f32e7eSjoerg 
89406f32e7eSjoerg   for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
89506f32e7eSjoerg        I != E; ++I) {
89606f32e7eSjoerg     if (Ex->getKind() == UETT_SizeOf) {
89706f32e7eSjoerg       if (!T->isIncompleteType() && !T->isConstantSizeType()) {
89806f32e7eSjoerg         assert(T->isVariableArrayType() && "Unknown non-constant-sized type.");
89906f32e7eSjoerg 
90006f32e7eSjoerg         // FIXME: Add support for VLA type arguments and VLA expressions.
90106f32e7eSjoerg         // When that happens, we should probably refactor VLASizeChecker's code.
90206f32e7eSjoerg         continue;
90306f32e7eSjoerg       } else if (T->getAs<ObjCObjectType>()) {
90406f32e7eSjoerg         // Some code tries to take the sizeof an ObjCObjectType, relying that
90506f32e7eSjoerg         // the compiler has laid out its representation.  Just report Unknown
90606f32e7eSjoerg         // for these.
90706f32e7eSjoerg         continue;
90806f32e7eSjoerg       }
90906f32e7eSjoerg     }
91006f32e7eSjoerg 
91106f32e7eSjoerg     APSInt Value = Ex->EvaluateKnownConstInt(getContext());
91206f32e7eSjoerg     CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue());
91306f32e7eSjoerg 
91406f32e7eSjoerg     ProgramStateRef state = (*I)->getState();
91506f32e7eSjoerg     state = state->BindExpr(Ex, (*I)->getLocationContext(),
91606f32e7eSjoerg                             svalBuilder.makeIntVal(amt.getQuantity(),
91706f32e7eSjoerg                                                    Ex->getType()));
91806f32e7eSjoerg     Bldr.generateNode(Ex, *I, state);
91906f32e7eSjoerg   }
92006f32e7eSjoerg 
92106f32e7eSjoerg   getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, Ex, *this);
92206f32e7eSjoerg }
92306f32e7eSjoerg 
handleUOExtension(ExplodedNodeSet::iterator I,const UnaryOperator * U,StmtNodeBuilder & Bldr)92406f32e7eSjoerg void ExprEngine::handleUOExtension(ExplodedNodeSet::iterator I,
92506f32e7eSjoerg                                    const UnaryOperator *U,
92606f32e7eSjoerg                                    StmtNodeBuilder &Bldr) {
92706f32e7eSjoerg   // FIXME: We can probably just have some magic in Environment::getSVal()
92806f32e7eSjoerg   // that propagates values, instead of creating a new node here.
92906f32e7eSjoerg   //
93006f32e7eSjoerg   // Unary "+" is a no-op, similar to a parentheses.  We still have places
93106f32e7eSjoerg   // where it may be a block-level expression, so we need to
93206f32e7eSjoerg   // generate an extra node that just propagates the value of the
93306f32e7eSjoerg   // subexpression.
93406f32e7eSjoerg   const Expr *Ex = U->getSubExpr()->IgnoreParens();
93506f32e7eSjoerg   ProgramStateRef state = (*I)->getState();
93606f32e7eSjoerg   const LocationContext *LCtx = (*I)->getLocationContext();
93706f32e7eSjoerg   Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
93806f32e7eSjoerg                                            state->getSVal(Ex, LCtx)));
93906f32e7eSjoerg }
94006f32e7eSjoerg 
VisitUnaryOperator(const UnaryOperator * U,ExplodedNode * Pred,ExplodedNodeSet & Dst)94106f32e7eSjoerg void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, ExplodedNode *Pred,
94206f32e7eSjoerg                                     ExplodedNodeSet &Dst) {
94306f32e7eSjoerg   // FIXME: Prechecks eventually go in ::Visit().
94406f32e7eSjoerg   ExplodedNodeSet CheckedSet;
94506f32e7eSjoerg   getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, U, *this);
94606f32e7eSjoerg 
94706f32e7eSjoerg   ExplodedNodeSet EvalSet;
94806f32e7eSjoerg   StmtNodeBuilder Bldr(CheckedSet, EvalSet, *currBldrCtx);
94906f32e7eSjoerg 
95006f32e7eSjoerg   for (ExplodedNodeSet::iterator I = CheckedSet.begin(), E = CheckedSet.end();
95106f32e7eSjoerg        I != E; ++I) {
95206f32e7eSjoerg     switch (U->getOpcode()) {
95306f32e7eSjoerg     default: {
95406f32e7eSjoerg       Bldr.takeNodes(*I);
95506f32e7eSjoerg       ExplodedNodeSet Tmp;
95606f32e7eSjoerg       VisitIncrementDecrementOperator(U, *I, Tmp);
95706f32e7eSjoerg       Bldr.addNodes(Tmp);
95806f32e7eSjoerg       break;
95906f32e7eSjoerg     }
96006f32e7eSjoerg     case UO_Real: {
96106f32e7eSjoerg       const Expr *Ex = U->getSubExpr()->IgnoreParens();
96206f32e7eSjoerg 
96306f32e7eSjoerg       // FIXME: We don't have complex SValues yet.
96406f32e7eSjoerg       if (Ex->getType()->isAnyComplexType()) {
96506f32e7eSjoerg         // Just report "Unknown."
96606f32e7eSjoerg         break;
96706f32e7eSjoerg       }
96806f32e7eSjoerg 
96906f32e7eSjoerg       // For all other types, UO_Real is an identity operation.
97006f32e7eSjoerg       assert (U->getType() == Ex->getType());
97106f32e7eSjoerg       ProgramStateRef state = (*I)->getState();
97206f32e7eSjoerg       const LocationContext *LCtx = (*I)->getLocationContext();
97306f32e7eSjoerg       Bldr.generateNode(U, *I, state->BindExpr(U, LCtx,
97406f32e7eSjoerg                                                state->getSVal(Ex, LCtx)));
97506f32e7eSjoerg       break;
97606f32e7eSjoerg     }
97706f32e7eSjoerg 
97806f32e7eSjoerg     case UO_Imag: {
97906f32e7eSjoerg       const Expr *Ex = U->getSubExpr()->IgnoreParens();
98006f32e7eSjoerg       // FIXME: We don't have complex SValues yet.
98106f32e7eSjoerg       if (Ex->getType()->isAnyComplexType()) {
98206f32e7eSjoerg         // Just report "Unknown."
98306f32e7eSjoerg         break;
98406f32e7eSjoerg       }
98506f32e7eSjoerg       // For all other types, UO_Imag returns 0.
98606f32e7eSjoerg       ProgramStateRef state = (*I)->getState();
98706f32e7eSjoerg       const LocationContext *LCtx = (*I)->getLocationContext();
98806f32e7eSjoerg       SVal X = svalBuilder.makeZeroVal(Ex->getType());
98906f32e7eSjoerg       Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, X));
99006f32e7eSjoerg       break;
99106f32e7eSjoerg     }
99206f32e7eSjoerg 
99306f32e7eSjoerg     case UO_AddrOf: {
99406f32e7eSjoerg       // Process pointer-to-member address operation.
99506f32e7eSjoerg       const Expr *Ex = U->getSubExpr()->IgnoreParens();
99606f32e7eSjoerg       if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Ex)) {
99706f32e7eSjoerg         const ValueDecl *VD = DRE->getDecl();
99806f32e7eSjoerg 
999*13fbcb42Sjoerg         if (isa<CXXMethodDecl>(VD) || isa<FieldDecl>(VD) ||
1000*13fbcb42Sjoerg             isa<IndirectFieldDecl>(VD)) {
100106f32e7eSjoerg           ProgramStateRef State = (*I)->getState();
100206f32e7eSjoerg           const LocationContext *LCtx = (*I)->getLocationContext();
1003*13fbcb42Sjoerg           SVal SV = svalBuilder.getMemberPointer(cast<NamedDecl>(VD));
100406f32e7eSjoerg           Bldr.generateNode(U, *I, State->BindExpr(U, LCtx, SV));
100506f32e7eSjoerg           break;
100606f32e7eSjoerg         }
100706f32e7eSjoerg       }
100806f32e7eSjoerg       // Explicitly proceed with default handler for this case cascade.
100906f32e7eSjoerg       handleUOExtension(I, U, Bldr);
101006f32e7eSjoerg       break;
101106f32e7eSjoerg     }
101206f32e7eSjoerg     case UO_Plus:
101306f32e7eSjoerg       assert(!U->isGLValue());
101406f32e7eSjoerg       LLVM_FALLTHROUGH;
101506f32e7eSjoerg     case UO_Deref:
101606f32e7eSjoerg     case UO_Extension: {
101706f32e7eSjoerg       handleUOExtension(I, U, Bldr);
101806f32e7eSjoerg       break;
101906f32e7eSjoerg     }
102006f32e7eSjoerg 
102106f32e7eSjoerg     case UO_LNot:
102206f32e7eSjoerg     case UO_Minus:
102306f32e7eSjoerg     case UO_Not: {
102406f32e7eSjoerg       assert (!U->isGLValue());
102506f32e7eSjoerg       const Expr *Ex = U->getSubExpr()->IgnoreParens();
102606f32e7eSjoerg       ProgramStateRef state = (*I)->getState();
102706f32e7eSjoerg       const LocationContext *LCtx = (*I)->getLocationContext();
102806f32e7eSjoerg 
102906f32e7eSjoerg       // Get the value of the subexpression.
103006f32e7eSjoerg       SVal V = state->getSVal(Ex, LCtx);
103106f32e7eSjoerg 
103206f32e7eSjoerg       if (V.isUnknownOrUndef()) {
103306f32e7eSjoerg         Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V));
103406f32e7eSjoerg         break;
103506f32e7eSjoerg       }
103606f32e7eSjoerg 
103706f32e7eSjoerg       switch (U->getOpcode()) {
103806f32e7eSjoerg         default:
103906f32e7eSjoerg           llvm_unreachable("Invalid Opcode.");
104006f32e7eSjoerg         case UO_Not:
104106f32e7eSjoerg           // FIXME: Do we need to handle promotions?
104206f32e7eSjoerg           state = state->BindExpr(U, LCtx, evalComplement(V.castAs<NonLoc>()));
104306f32e7eSjoerg           break;
104406f32e7eSjoerg         case UO_Minus:
104506f32e7eSjoerg           // FIXME: Do we need to handle promotions?
104606f32e7eSjoerg           state = state->BindExpr(U, LCtx, evalMinus(V.castAs<NonLoc>()));
104706f32e7eSjoerg           break;
104806f32e7eSjoerg         case UO_LNot:
104906f32e7eSjoerg           // C99 6.5.3.3: "The expression !E is equivalent to (0==E)."
105006f32e7eSjoerg           //
105106f32e7eSjoerg           //  Note: technically we do "E == 0", but this is the same in the
105206f32e7eSjoerg           //    transfer functions as "0 == E".
105306f32e7eSjoerg           SVal Result;
105406f32e7eSjoerg           if (Optional<Loc> LV = V.getAs<Loc>()) {
105506f32e7eSjoerg             Loc X = svalBuilder.makeNullWithType(Ex->getType());
105606f32e7eSjoerg             Result = evalBinOp(state, BO_EQ, *LV, X, U->getType());
105706f32e7eSjoerg           } else if (Ex->getType()->isFloatingType()) {
105806f32e7eSjoerg             // FIXME: handle floating point types.
105906f32e7eSjoerg             Result = UnknownVal();
106006f32e7eSjoerg           } else {
106106f32e7eSjoerg             nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType()));
106206f32e7eSjoerg             Result = evalBinOp(state, BO_EQ, V.castAs<NonLoc>(), X,
106306f32e7eSjoerg                                U->getType());
106406f32e7eSjoerg           }
106506f32e7eSjoerg 
106606f32e7eSjoerg           state = state->BindExpr(U, LCtx, Result);
106706f32e7eSjoerg           break;
106806f32e7eSjoerg       }
106906f32e7eSjoerg       Bldr.generateNode(U, *I, state);
107006f32e7eSjoerg       break;
107106f32e7eSjoerg     }
107206f32e7eSjoerg     }
107306f32e7eSjoerg   }
107406f32e7eSjoerg 
107506f32e7eSjoerg   getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, U, *this);
107606f32e7eSjoerg }
107706f32e7eSjoerg 
VisitIncrementDecrementOperator(const UnaryOperator * U,ExplodedNode * Pred,ExplodedNodeSet & Dst)107806f32e7eSjoerg void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U,
107906f32e7eSjoerg                                                  ExplodedNode *Pred,
108006f32e7eSjoerg                                                  ExplodedNodeSet &Dst) {
108106f32e7eSjoerg   // Handle ++ and -- (both pre- and post-increment).
108206f32e7eSjoerg   assert (U->isIncrementDecrementOp());
108306f32e7eSjoerg   const Expr *Ex = U->getSubExpr()->IgnoreParens();
108406f32e7eSjoerg 
108506f32e7eSjoerg   const LocationContext *LCtx = Pred->getLocationContext();
108606f32e7eSjoerg   ProgramStateRef state = Pred->getState();
108706f32e7eSjoerg   SVal loc = state->getSVal(Ex, LCtx);
108806f32e7eSjoerg 
108906f32e7eSjoerg   // Perform a load.
109006f32e7eSjoerg   ExplodedNodeSet Tmp;
109106f32e7eSjoerg   evalLoad(Tmp, U, Ex, Pred, state, loc);
109206f32e7eSjoerg 
109306f32e7eSjoerg   ExplodedNodeSet Dst2;
109406f32e7eSjoerg   StmtNodeBuilder Bldr(Tmp, Dst2, *currBldrCtx);
109506f32e7eSjoerg   for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) {
109606f32e7eSjoerg 
109706f32e7eSjoerg     state = (*I)->getState();
109806f32e7eSjoerg     assert(LCtx == (*I)->getLocationContext());
109906f32e7eSjoerg     SVal V2_untested = state->getSVal(Ex, LCtx);
110006f32e7eSjoerg 
110106f32e7eSjoerg     // Propagate unknown and undefined values.
110206f32e7eSjoerg     if (V2_untested.isUnknownOrUndef()) {
110306f32e7eSjoerg       state = state->BindExpr(U, LCtx, V2_untested);
110406f32e7eSjoerg 
110506f32e7eSjoerg       // Perform the store, so that the uninitialized value detection happens.
110606f32e7eSjoerg       Bldr.takeNodes(*I);
110706f32e7eSjoerg       ExplodedNodeSet Dst3;
110806f32e7eSjoerg       evalStore(Dst3, U, Ex, *I, state, loc, V2_untested);
110906f32e7eSjoerg       Bldr.addNodes(Dst3);
111006f32e7eSjoerg 
111106f32e7eSjoerg       continue;
111206f32e7eSjoerg     }
111306f32e7eSjoerg     DefinedSVal V2 = V2_untested.castAs<DefinedSVal>();
111406f32e7eSjoerg 
111506f32e7eSjoerg     // Handle all other values.
111606f32e7eSjoerg     BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub;
111706f32e7eSjoerg 
111806f32e7eSjoerg     // If the UnaryOperator has non-location type, use its type to create the
111906f32e7eSjoerg     // constant value. If the UnaryOperator has location type, create the
112006f32e7eSjoerg     // constant with int type and pointer width.
112106f32e7eSjoerg     SVal RHS;
112206f32e7eSjoerg     SVal Result;
112306f32e7eSjoerg 
112406f32e7eSjoerg     if (U->getType()->isAnyPointerType())
112506f32e7eSjoerg       RHS = svalBuilder.makeArrayIndex(1);
112606f32e7eSjoerg     else if (U->getType()->isIntegralOrEnumerationType())
112706f32e7eSjoerg       RHS = svalBuilder.makeIntVal(1, U->getType());
112806f32e7eSjoerg     else
112906f32e7eSjoerg       RHS = UnknownVal();
113006f32e7eSjoerg 
113106f32e7eSjoerg     // The use of an operand of type bool with the ++ operators is deprecated
113206f32e7eSjoerg     // but valid until C++17. And if the operand of the ++ operator is of type
113306f32e7eSjoerg     // bool, it is set to true until C++17. Note that for '_Bool', it is also
113406f32e7eSjoerg     // set to true when it encounters ++ operator.
113506f32e7eSjoerg     if (U->getType()->isBooleanType() && U->isIncrementOp())
113606f32e7eSjoerg       Result = svalBuilder.makeTruthVal(true, U->getType());
113706f32e7eSjoerg     else
113806f32e7eSjoerg       Result = evalBinOp(state, Op, V2, RHS, U->getType());
113906f32e7eSjoerg 
114006f32e7eSjoerg     // Conjure a new symbol if necessary to recover precision.
114106f32e7eSjoerg     if (Result.isUnknown()){
114206f32e7eSjoerg       DefinedOrUnknownSVal SymVal =
114306f32e7eSjoerg         svalBuilder.conjureSymbolVal(nullptr, U, LCtx,
114406f32e7eSjoerg                                      currBldrCtx->blockCount());
114506f32e7eSjoerg       Result = SymVal;
114606f32e7eSjoerg 
114706f32e7eSjoerg       // If the value is a location, ++/-- should always preserve
114806f32e7eSjoerg       // non-nullness.  Check if the original value was non-null, and if so
114906f32e7eSjoerg       // propagate that constraint.
115006f32e7eSjoerg       if (Loc::isLocType(U->getType())) {
115106f32e7eSjoerg         DefinedOrUnknownSVal Constraint =
115206f32e7eSjoerg         svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType()));
115306f32e7eSjoerg 
115406f32e7eSjoerg         if (!state->assume(Constraint, true)) {
115506f32e7eSjoerg           // It isn't feasible for the original value to be null.
115606f32e7eSjoerg           // Propagate this constraint.
115706f32e7eSjoerg           Constraint = svalBuilder.evalEQ(state, SymVal,
115806f32e7eSjoerg                                        svalBuilder.makeZeroVal(U->getType()));
115906f32e7eSjoerg 
116006f32e7eSjoerg           state = state->assume(Constraint, false);
116106f32e7eSjoerg           assert(state);
116206f32e7eSjoerg         }
116306f32e7eSjoerg       }
116406f32e7eSjoerg     }
116506f32e7eSjoerg 
116606f32e7eSjoerg     // Since the lvalue-to-rvalue conversion is explicit in the AST,
116706f32e7eSjoerg     // we bind an l-value if the operator is prefix and an lvalue (in C++).
116806f32e7eSjoerg     if (U->isGLValue())
116906f32e7eSjoerg       state = state->BindExpr(U, LCtx, loc);
117006f32e7eSjoerg     else
117106f32e7eSjoerg       state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result);
117206f32e7eSjoerg 
117306f32e7eSjoerg     // Perform the store.
117406f32e7eSjoerg     Bldr.takeNodes(*I);
117506f32e7eSjoerg     ExplodedNodeSet Dst3;
117606f32e7eSjoerg     evalStore(Dst3, U, Ex, *I, state, loc, Result);
117706f32e7eSjoerg     Bldr.addNodes(Dst3);
117806f32e7eSjoerg   }
117906f32e7eSjoerg   Dst.insert(Dst2);
118006f32e7eSjoerg }
1181