SimpleSValBuilder.cpp revision eb2d1f1c88836bd5382e5d7aa8f6b85148a88b27
13a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*- 23a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 33a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// The LLVM Compiler Infrastructure 43a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 53a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// This file is distributed under the University of Illinois Open Source 63a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// License. See LICENSE.TXT for details. 73a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 83a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 93a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 10fc8f0e14ad142ed811e90fbd9a30e419e301c717Chris Lattner// This file defines SimpleSValBuilder, a basic implementation of SValBuilder. 113a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis// 123a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis//===----------------------------------------------------------------------===// 133a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 143a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 15ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 16ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall 17ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallusing namespace clang; 18ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCallusing namespace ento; 193a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 203a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisnamespace { 213a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davisclass SimpleSValBuilder : public SValBuilder { 220bab0cdab751248ca389a5592bcb70eac5d39260John McCallprotected: 239ee494f98610dcd79441dce469d7bf609fcd7b92Charles Davis virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy); 2493d557bc1867b7d7b102f87290194b4be7932c92John McCall virtual SVal evalCastFromLoc(Loc val, QualType castTy); 253a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 26ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topperpublic: 27ba77cb9c82cc9d498e4d6474d128007249f07871Craig Topper SimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 283b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth ProgramStateManager &stateMgr) 293b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth : SValBuilder(alloc, context, stateMgr) {} 303b844ba7d5be205a9b4f5f0b0d1b7978977f4b8cChandler Carruth virtual ~SimpleSValBuilder() {} 313a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis 323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis virtual SVal evalMinus(NonLoc val); 3393d557bc1867b7d7b102f87290194b4be7932c92John McCall virtual SVal evalComplement(NonLoc val); 343a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis virtual SVal evalBinOpNN(const ProgramState *state, BinaryOperator::Opcode op, 353a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis NonLoc lhs, NonLoc rhs, QualType resultTy); 36071cc7deffad608165b1ddd5263e8bf181861520Charles Davis virtual SVal evalBinOpLL(const ProgramState *state, BinaryOperator::Opcode op, 370bab0cdab751248ca389a5592bcb70eac5d39260John McCall Loc lhs, Loc rhs, QualType resultTy); 389cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner virtual SVal evalBinOpLN(const ProgramState *state, BinaryOperator::Opcode op, 3993d557bc1867b7d7b102f87290194b4be7932c92John McCall Loc lhs, NonLoc rhs, QualType resultTy); 40babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 410bab0cdab751248ca389a5592bcb70eac5d39260John McCall /// getKnownValue - evaluates a given SVal. If the SVal has only one possible 420bab0cdab751248ca389a5592bcb70eac5d39260John McCall /// (integer) value, that value is returned. Otherwise, returns NULL. 439cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner virtual const llvm::APSInt *getKnownValue(const ProgramState *state, SVal V); 440bab0cdab751248ca389a5592bcb70eac5d39260John McCall 459cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall SVal MakeSymIntVal(const SymExpr *LHS, BinaryOperator::Opcode op, 469cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner const llvm::APSInt &RHS, QualType resultTy); 470bab0cdab751248ca389a5592bcb70eac5d39260John McCall}; 480bab0cdab751248ca389a5592bcb70eac5d39260John McCall} // end anonymous namespace 490bab0cdab751248ca389a5592bcb70eac5d39260John McCall 500bab0cdab751248ca389a5592bcb70eac5d39260John McCallSValBuilder *ento::createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 510bab0cdab751248ca389a5592bcb70eac5d39260John McCall ASTContext &context, 523a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis ProgramStateManager &stateMgr) { 53babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return new SimpleSValBuilder(alloc, context, stateMgr); 5414110477887e3dc168ffc6c191e72d705051f99ePeter Collingbourne} 5593d557bc1867b7d7b102f87290194b4be7932c92John McCall 56f16aa103d3afd42fbca2ab346f191bf745cec092John McCall//===----------------------------------------------------------------------===// 57cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall// Transfer function for Casts. 589cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner//===----------------------------------------------------------------------===// 590bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6093d557bc1867b7d7b102f87290194b4be7932c92John McCallSVal SimpleSValBuilder::evalCastFromNonLoc(NonLoc val, QualType castTy) { 6193d557bc1867b7d7b102f87290194b4be7932c92John McCall 6293d557bc1867b7d7b102f87290194b4be7932c92John McCall bool isLocType = Loc::isLocType(castTy); 6393d557bc1867b7d7b102f87290194b4be7932c92John McCall 643023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall if (nonloc::LocAsInteger *LI = dyn_cast<nonloc::LocAsInteger>(&val)) { 656c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall if (isLocType) 666c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return LI->getLoc(); 676c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall 686c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // FIXME: Correctly support promotions/truncations. 696c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall unsigned castSize = Context.getTypeSize(castTy); 700bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (castSize == LI->getNumBits()) 710bab0cdab751248ca389a5592bcb70eac5d39260John McCall return val; 720bab0cdab751248ca389a5592bcb70eac5d39260John McCall return makeLocAsInteger(LI->getLoc(), castSize); 734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 75cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall if (const SymExpr *se = val.getAsSymbolicExpression()) { 760bab0cdab751248ca389a5592bcb70eac5d39260John McCall QualType T = Context.getCanonicalType(se->getType(Context)); 77cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall if (T == Context.getCanonicalType(castTy)) 78755d8497e39071aa24acc173ff07083e3256b8f8John McCall return val; 795808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall 805808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall // FIXME: Remove this hack when we support symbolic truncation/extension. 812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // HACK: If both castTy and T are integers, ignore the cast. This is 822d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // not a permanent solution. Eventually we want to precisely handle 832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // extension/truncation of symbolic integers. This prevents us from losing 84875ab10245d3bf37252dd822aa1616bb0a391095John McCall // precision when we assign 'x = y' and 'y' is symbolic and x and y are 850bab0cdab751248ca389a5592bcb70eac5d39260John McCall // different integer types. 860bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (T->isIntegerType() && castTy->isIntegerType()) 870bab0cdab751248ca389a5592bcb70eac5d39260John McCall return val; 880bab0cdab751248ca389a5592bcb70eac5d39260John McCall 890bab0cdab751248ca389a5592bcb70eac5d39260John McCall return UnknownVal(); 90e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 910bab0cdab751248ca389a5592bcb70eac5d39260John McCall 920bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (!isa<nonloc::ConcreteInt>(val)) 930bab0cdab751248ca389a5592bcb70eac5d39260John McCall return UnknownVal(); 944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 95ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // Only handle casts from integers to integers. 96ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall if (!isLocType && !castTy->isIntegerType()) 97ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return UnknownVal(); 98ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall llvm::APSInt i = cast<nonloc::ConcreteInt>(val).getValue(); 1004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall i.setIsUnsigned(castTy->isUnsignedIntegerOrEnumerationType() || 1014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall Loc::isLocType(castTy)); 1025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner i = i.extOrTrunc(Context.getTypeSize(castTy)); 1034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (isLocType) 1054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeIntLocVal(i); 1064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall else 1075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return makeIntVal(i); 1084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 1094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallSVal SimpleSValBuilder::evalCastFromLoc(Loc val, QualType castTy) { 1114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Casts from pointers -> pointers, just return the lval. 1134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // 1141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Casts from pointers -> references, just return the lval. These 115285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos // can be introduced by the frontend for corner cases, e.g 1162eb9a959d24ad757a82ecab61f343635ad67749aDavid Blaikie // casting from va_list* to __builtin_va_list&. 117285baac67d722beb6854f5faa45ee1aa62a7ce92Joao Matos // 118e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (Loc::isLocType(castTy) || castTy->isReferenceType()) 1191e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return val; 1201e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 1211e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // FIXME: Handle transparent unions where a value can be "transparently" 1226ec278d1a354517e20f13a877481453ee7940c78John McCall // lifted into a union type. 1231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (castTy->isUnionType()) 124e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return UnknownVal(); 125e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 126e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (castTy->isIntegerType()) { 1275cd91b513455fd7753e8815b54f0a49bbca6602dJohn McCall unsigned BitWidth = Context.getTypeSize(castTy); 1283030eb82593097502469a8b3fc26112c79c75605John McCall 1290f30a12ce7b3d4d86c9ca9072f587da77c8eef34Chandler Carruth if (!isa<loc::ConcreteInt>(val)) 13020bb175cb8ae5844034828db094fb948c0e3454aJohn McCall return makeLocAsInteger(val, BitWidth); 13120bb175cb8ae5844034828db094fb948c0e3454aJohn McCall 1323a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis llvm::APSInt i = cast<loc::ConcreteInt>(val).getValue(); 133ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall i.setIsUnsigned(castTy->isUnsignedIntegerOrEnumerationType() || 134ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall Loc::isLocType(castTy)); 135ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall i = i.extOrTrunc(BitWidth); 136babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return makeIntVal(i); 1374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 1384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // All other cases: return 'UnknownVal'. This includes casting pointers 1404c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // to floats, which is probably badness it itself, but this is a good 1415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner // intermediate solution until we do something better. 1424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 1434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 1444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall//===----------------------------------------------------------------------===// 1465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner// Transfer function for unary operators. 1474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall//===----------------------------------------------------------------------===// 1484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallSVal SimpleSValBuilder::evalMinus(NonLoc val) { 1504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall switch (val.getSubKind()) { 1514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case nonloc::ConcreteIntKind: 1524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return cast<nonloc::ConcreteInt>(val).evalMinus(*this); 1534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall default: 1544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 1554c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 156e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall} 1571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 1581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallSVal SimpleSValBuilder::evalComplement(NonLoc X) { 1591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall switch (X.getSubKind()) { 1606ec278d1a354517e20f13a877481453ee7940c78John McCall case nonloc::ConcreteIntKind: 1611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return cast<nonloc::ConcreteInt>(X).evalComplement(*this); 162e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall default: 163e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return UnknownVal(); 1644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 1654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall} 1664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall//===----------------------------------------------------------------------===// 1684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall// Transfer function for binary operators. 1694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall//===----------------------------------------------------------------------===// 1704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 1714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCallstatic BinaryOperator::Opcode NegateComparison(BinaryOperator::Opcode op) { 1724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall switch (op) { 173ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall default: 1743a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis llvm_unreachable("Invalid opcode."); 1753a811f1f4286ee3fd0c563c1cfe623956f3caa24Charles Davis case BO_LT: return BO_GE; 176071cc7deffad608165b1ddd5263e8bf181861520Charles Davis case BO_GT: return BO_LE; 17796fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_LE: return BO_GT; 17896fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_GE: return BO_LT; 17996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_EQ: return BO_NE; 18096fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_NE: return BO_EQ; 18196fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall } 18296fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall} 18396fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall 18496fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCallstatic BinaryOperator::Opcode ReverseComparison(BinaryOperator::Opcode op) { 18596fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall switch (op) { 18696fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall default: 18796fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall llvm_unreachable("Invalid opcode."); 18896fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_LT: return BO_GT; 18996fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_GT: return BO_LT; 19096fcde0b8ed8bdf99d326312ca7be6447b0fe5fcJohn McCall case BO_LE: return BO_GE; 191ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall case BO_GE: return BO_LE; 192ee79a4c30e5d1c5285551c9a25b8ec6d45d46aa7John McCall case BO_EQ: 1939cbe4f0ba01ec304e1e3d071c071f7bca33631c0Chris Lattner case BO_NE: 1940bab0cdab751248ca389a5592bcb70eac5d39260John McCall return op; 1950bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 1960bab0cdab751248ca389a5592bcb70eac5d39260John McCall} 1977650d95a1a616ea300f37126a8dfc93dc19a662aChris Lattner 198875ab10245d3bf37252dd822aa1616bb0a391095John McCallSVal SimpleSValBuilder::MakeSymIntVal(const SymExpr *LHS, 199875ab10245d3bf37252dd822aa1616bb0a391095John McCall BinaryOperator::Opcode op, 200babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall const llvm::APSInt &RHS, 201babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall QualType resultTy) { 202babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall bool isIdempotent = false; 203babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 204babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // Check for a few special cases with known reductions first. 205babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall switch (op) { 206babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall default: 207babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // We can't reduce this case; just treat it normally. 208babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall break; 209babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_Mul: 210babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // a*0 and a*1 211babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (RHS == 0) 212babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return makeIntVal(0, resultTy); 213babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall else if (RHS == 1) 214babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall isIdempotent = true; 215babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall break; 216babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_Div: 217babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // a/0 and a/1 218babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (RHS == 0) 219babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // This is also handled elsewhere. 22093d557bc1867b7d7b102f87290194b4be7932c92John McCall return UndefinedVal(); 22193d557bc1867b7d7b102f87290194b4be7932c92John McCall else if (RHS == 1) 22293d557bc1867b7d7b102f87290194b4be7932c92John McCall isIdempotent = true; 22393d557bc1867b7d7b102f87290194b4be7932c92John McCall break; 22493d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_Rem: 22593d557bc1867b7d7b102f87290194b4be7932c92John McCall // a%0 and a%1 22693d557bc1867b7d7b102f87290194b4be7932c92John McCall if (RHS == 0) 22793d557bc1867b7d7b102f87290194b4be7932c92John McCall // This is also handled elsewhere. 22893d557bc1867b7d7b102f87290194b4be7932c92John McCall return UndefinedVal(); 22993d557bc1867b7d7b102f87290194b4be7932c92John McCall else if (RHS == 1) 23093d557bc1867b7d7b102f87290194b4be7932c92John McCall return makeIntVal(0, resultTy); 23193d557bc1867b7d7b102f87290194b4be7932c92John McCall break; 2322acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner case BO_Add: 233de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall case BO_Sub: 234de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall case BO_Shl: 23593d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_Shr: 2362acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner case BO_Xor: 237babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // a+0, a-0, a<<0, a>>0, a^0 23893d557bc1867b7d7b102f87290194b4be7932c92John McCall if (RHS == 0) 239babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall isIdempotent = true; 240babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall break; 241babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_And: 242babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // a&0 and a&(~0) 243d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (RHS == 0) 244d608cdb7c044365cf4e8764ade1e11e99c176078John McCall return makeIntVal(0, resultTy); 245babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall else if (RHS.isAllOnesValue()) 246babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall isIdempotent = true; 247babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall break; 248babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_Or: 249babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall // a|0 and a|(~0) 25093d557bc1867b7d7b102f87290194b4be7932c92John McCall if (RHS == 0) 25193d557bc1867b7d7b102f87290194b4be7932c92John McCall isIdempotent = true; 25293d557bc1867b7d7b102f87290194b4be7932c92John McCall else if (RHS.isAllOnesValue()) { 253babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall const llvm::APSInt &Result = BasicVals.Convert(resultTy, RHS); 254babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return nonloc::ConcreteInt(Result); 255babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall } 25693d557bc1867b7d7b102f87290194b4be7932c92John McCall break; 25793d557bc1867b7d7b102f87290194b4be7932c92John McCall } 258d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 25993d557bc1867b7d7b102f87290194b4be7932c92John McCall // Idempotent ops (like a*1) can still change the type of an expression. 26093d557bc1867b7d7b102f87290194b4be7932c92John McCall // Wrap the LHS up in a NonLoc again and let evalCastFromNonLoc do the 26193d557bc1867b7d7b102f87290194b4be7932c92John McCall // dirty work. 262babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (isIdempotent) { 263babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (SymbolRef LHSSym = dyn_cast<SymbolData>(LHS)) 264babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return evalCastFromNonLoc(nonloc::SymbolVal(LHSSym), resultTy); 265babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall return evalCastFromNonLoc(nonloc::SymExprVal(LHS), resultTy); 266babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall } 267babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 26893d557bc1867b7d7b102f87290194b4be7932c92John McCall // If we reach this point, the expression cannot be simplified. 26993d557bc1867b7d7b102f87290194b4be7932c92John McCall // Make a SymExprVal for the entire thing. 27093d557bc1867b7d7b102f87290194b4be7932c92John McCall return makeNonLoc(LHS, op, RHS, resultTy); 27193d557bc1867b7d7b102f87290194b4be7932c92John McCall} 272babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall 27393d557bc1867b7d7b102f87290194b4be7932c92John McCallSVal SimpleSValBuilder::evalBinOpNN(const ProgramState *state, 27493d557bc1867b7d7b102f87290194b4be7932c92John McCall BinaryOperator::Opcode op, 27593d557bc1867b7d7b102f87290194b4be7932c92John McCall NonLoc lhs, NonLoc rhs, 2762acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner QualType resultTy) { 27793d557bc1867b7d7b102f87290194b4be7932c92John McCall // Handle trivial case where left-side and right-side are the same. 278babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall if (lhs == rhs) 27993d557bc1867b7d7b102f87290194b4be7932c92John McCall switch (op) { 28093d557bc1867b7d7b102f87290194b4be7932c92John McCall default: 281babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall break; 282babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_EQ: 283babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_LE: 28493d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_GE: 28593d557bc1867b7d7b102f87290194b4be7932c92John McCall return makeTruthVal(true, resultTy); 28693d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_LT: 287babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_GT: 28893d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_NE: 28993d557bc1867b7d7b102f87290194b4be7932c92John McCall return makeTruthVal(false, resultTy); 29093d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_Xor: 29193d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_Sub: 29293d557bc1867b7d7b102f87290194b4be7932c92John McCall return makeIntVal(0, resultTy); 29393d557bc1867b7d7b102f87290194b4be7932c92John McCall case BO_Or: 294babc9a9ecc3fbc04edaac5a1f4fb3e869815b25cJohn McCall case BO_And: 29593d557bc1867b7d7b102f87290194b4be7932c92John McCall return evalCastFromNonLoc(lhs, resultTy); 29693d557bc1867b7d7b102f87290194b4be7932c92John McCall } 29793d557bc1867b7d7b102f87290194b4be7932c92John McCall 298bbf3bacb3e0c1ebb3e8a4a8b1330404a7e379315Jay Foad while (1) { 29993d557bc1867b7d7b102f87290194b4be7932c92John McCall switch (lhs.getSubKind()) { 30093d557bc1867b7d7b102f87290194b4be7932c92John McCall default: 30193d557bc1867b7d7b102f87290194b4be7932c92John McCall return UnknownVal(); 30293d557bc1867b7d7b102f87290194b4be7932c92John McCall case nonloc::LocAsIntegerKind: { 3033023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall Loc lhsL = cast<nonloc::LocAsInteger>(lhs).getLoc(); 3046c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall switch (rhs.getSubKind()) { 3056c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall case nonloc::LocAsIntegerKind: 3066c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return evalBinOpLL(state, op, lhsL, 3076c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall cast<nonloc::LocAsInteger>(rhs).getLoc(), 3086c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall resultTy); 3096c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall case nonloc::ConcreteIntKind: { 3106c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // Transform the integer into a location and compare. 3116c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall llvm::APSInt i = cast<nonloc::ConcreteInt>(rhs).getValue(); 3126c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall i.setIsUnsigned(true); 3136c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall i = i.extOrTrunc(Context.getTypeSize(Context.VoidPtrTy)); 314956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy); 3156c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall } 3166c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall default: 3176c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall switch (op) { 3186c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall case BO_EQ: 3196c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return makeTruthVal(false, resultTy); 3206c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall case BO_NE: 3216c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall return makeTruthVal(true, resultTy); 3226c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall default: 3236c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall // This case also handles pointer arithmetic. 3242acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner return UnknownVal(); 325eede61a83e90f3cb03ef8665b67d648dccd6ce42Douglas Gregor } 3266c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall } 3276c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall } 3286c2ab1d578c6cc1f3ddcc948532cd625f1092ef2John McCall case nonloc::SymExprValKind: { 3294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall nonloc::SymExprVal *selhs = cast<nonloc::SymExprVal>(&lhs); 3304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Only handle LHS of the form "$sym op constant", at least for now. 3324d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const SymIntExpr *symIntExpr = 3330bab0cdab751248ca389a5592bcb70eac5d39260John McCall dyn_cast<SymIntExpr>(selhs->getSymbolicExpression()); 3340bab0cdab751248ca389a5592bcb70eac5d39260John McCall 3350bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (!symIntExpr) 3360bab0cdab751248ca389a5592bcb70eac5d39260John McCall return UnknownVal(); 3370bab0cdab751248ca389a5592bcb70eac5d39260John McCall 3380bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Is this a logical not? (!x is represented as x == 0.) 3390bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (op == BO_EQ && rhs.isZeroConstant()) { 3400bab0cdab751248ca389a5592bcb70eac5d39260John McCall // We know how to negate certain expressions. Simplify them here. 3410bab0cdab751248ca389a5592bcb70eac5d39260John McCall 3420bab0cdab751248ca389a5592bcb70eac5d39260John McCall BinaryOperator::Opcode opc = symIntExpr->getOpcode(); 3430bab0cdab751248ca389a5592bcb70eac5d39260John McCall switch (opc) { 3440bab0cdab751248ca389a5592bcb70eac5d39260John McCall default: 3450bab0cdab751248ca389a5592bcb70eac5d39260John McCall // We don't know how to negate this operation. 3460bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Just handle it as if it were a normal comparison to 0. 3470bab0cdab751248ca389a5592bcb70eac5d39260John McCall break; 3480bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_LAnd: 3490bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_LOr: 3500bab0cdab751248ca389a5592bcb70eac5d39260John McCall llvm_unreachable("Logical operators handled by branching logic."); 3510bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_Assign: 352d608cdb7c044365cf4e8764ade1e11e99c176078John McCall case BO_MulAssign: 3530bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_DivAssign: 3540bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_RemAssign: 3554d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_AddAssign: 3562de56d1d0c3a504ad1529de2677628bdfbb95cd4John McCall case BO_SubAssign: 3574d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_ShlAssign: 3584d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_ShrAssign: 3594d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_AndAssign: 3604d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_XorAssign: 3614d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_OrAssign: 3624d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_Comma: 3634d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm_unreachable("'=' and ',' operators handled by ExprEngine."); 3644d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_PtrMemD: 3654d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_PtrMemI: 3664d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall llvm_unreachable("Pointer arithmetic not handled here."); 3674d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_LT: 3684d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_GT: 3693023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall case BO_LE: 3703023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall case BO_GE: 3714d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_EQ: 3724d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_NE: 3734d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Negate the comparison and make a value. 3744d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall opc = NegateComparison(opc); 3753023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall assert(symIntExpr->getType(Context) == resultTy); 3764d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return makeNonLoc(symIntExpr->getLHS(), opc, 3774d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall symIntExpr->getRHS(), resultTy); 3784d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 3794d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 3804d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3814d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // For now, only handle expressions whose RHS is a constant. 3824d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs); 3834d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (!rhsInt) 3843023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall return UnknownVal(); 3854d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 3864d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // If both the LHS and the current expression are additive, 3874d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // fold their constants. 3884d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (BinaryOperator::isAdditiveOp(op)) { 3894d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall BinaryOperator::Opcode lop = symIntExpr->getOpcode(); 3903023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall if (BinaryOperator::isAdditiveOp(lop)) { 3914d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // resultTy may not be the best type to convert to, but it's 3924d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // probably the best choice in expressions with mixed type 3934d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // (such as x+1U+2LL). The rules for implicit conversions should 3944d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // choose a reasonable type to preserve the expression, and will 3954d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // at least match how the value is going to be used. 3964d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const llvm::APSInt &first = 3973023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall BasicVals.Convert(resultTy, symIntExpr->getRHS()); 3984d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const llvm::APSInt &second = 3994d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall BasicVals.Convert(resultTy, rhsInt->getValue()); 4004d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const llvm::APSInt *newRHS; 4014d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (lop == op) 4023023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall newRHS = BasicVals.evalAPSInt(BO_Add, first, second); 4034d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 4044d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall newRHS = BasicVals.evalAPSInt(BO_Sub, first, second); 4054d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return MakeSymIntVal(symIntExpr->getLHS(), lop, *newRHS, resultTy); 4064d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4074d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4084d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall 4094d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Otherwise, make a SymExprVal out of the expression. 4104d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return MakeSymIntVal(symIntExpr, op, rhsInt->getValue(), resultTy); 4114d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4124d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case nonloc::ConcreteIntKind: { 4134d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const nonloc::ConcreteInt& lhsInt = cast<nonloc::ConcreteInt>(lhs); 4143023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall 4154d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // Is the RHS a symbol we can simplify? 4164d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // FIXME: This was mostly copy/pasted from the LHS-is-a-symbol case. 4174d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (const nonloc::SymbolVal *srhs = dyn_cast<nonloc::SymbolVal>(&rhs)) { 4184d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall SymbolRef RSym = srhs->getSymbol(); 4194d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (RSym->getType(Context)->isIntegerType()) { 4204d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (const llvm::APSInt *Constant = state->getSymVal(RSym)) { 4214d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // The symbol evaluates to a constant. 4224d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const llvm::APSInt *rhs_I; 4234d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (BinaryOperator::isRelationalOp(op)) 4244d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall rhs_I = &BasicVals.Convert(lhsInt.getValue(), *Constant); 4254d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall else 426875ab10245d3bf37252dd822aa1616bb0a391095John McCall rhs_I = &BasicVals.Convert(resultTy, *Constant); 4270bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4280bab0cdab751248ca389a5592bcb70eac5d39260John McCall rhs = nonloc::ConcreteInt(*rhs_I); 4294d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4304d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4314d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } 4320bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4334d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall if (isa<nonloc::ConcreteInt>(rhs)) { 4344d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall return lhsInt.evalBinOp(*this, op, cast<nonloc::ConcreteInt>(rhs)); 4354d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall } else { 4364d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall const llvm::APSInt& lhsValue = lhsInt.getValue(); 4370bab0cdab751248ca389a5592bcb70eac5d39260John McCall 4380bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Swap the left and right sides and flip the operator if doing so 439d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // allows us to better reason about the expression (this is a form 440d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // of expression canonicalization). 4414d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall // While we're at it, catch some special cases for non-commutative ops. 4424d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall NonLoc tmp = rhs; 4434d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall rhs = lhs; 444d608cdb7c044365cf4e8764ade1e11e99c176078John McCall lhs = tmp; 445d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 4464d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall switch (op) { 4474d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_LT: 4484d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_GT: 4494d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_LE: 450d608cdb7c044365cf4e8764ade1e11e99c176078John McCall case BO_GE: 4514d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall op = ReverseComparison(op); 452d608cdb7c044365cf4e8764ade1e11e99c176078John McCall continue; 4534d4e5c1ae83f4510caa486b3ad19de13048f9f04John McCall case BO_EQ: 4543023def6bea3af6dbb51eea51f8cb8ea892d26cfJohn McCall case BO_NE: 455cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall case BO_Add: 456cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall case BO_Mul: 4570bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_And: 4582acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner case BO_Xor: 4590bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_Or: 4600bab0cdab751248ca389a5592bcb70eac5d39260John McCall continue; 4610bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_Shr: 4620bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (lhsValue.isAllOnesValue() && lhsValue.isSigned()) 4630bab0cdab751248ca389a5592bcb70eac5d39260John McCall // At this point lhs and rhs have been swapped. 464d608cdb7c044365cf4e8764ade1e11e99c176078John McCall return rhs; 465d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // FALL-THROUGH 466d608cdb7c044365cf4e8764ade1e11e99c176078John McCall case BO_Shl: 467c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris Lattner if (lhsValue == 0) 468cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall // At this point lhs and rhs have been swapped. 469cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall return rhs; 4705808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall return UnknownVal(); 4715808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall default: 4725808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall return UnknownVal(); 4730bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 4740bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 4750bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 4765808ce43f8d7e71f5acacc9ca320268c4f37565aJohn McCall case nonloc::SymbolValKind: { 4770bab0cdab751248ca389a5592bcb70eac5d39260John McCall nonloc::SymbolVal *slhs = cast<nonloc::SymbolVal>(&lhs); 4780bab0cdab751248ca389a5592bcb70eac5d39260John McCall SymbolRef Sym = slhs->getSymbol(); 479755d8497e39071aa24acc173ff07083e3256b8f8John McCall QualType lhsType = Sym->getType(Context); 4802d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 4812d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // The conversion type is usually the result type, but not in the case 4822d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // of relational expressions. 4832d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith QualType conversionType = resultTy; 4842d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (BinaryOperator::isRelationalOp(op)) 485d608cdb7c044365cf4e8764ade1e11e99c176078John McCall conversionType = lhsType; 486d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 487875ab10245d3bf37252dd822aa1616bb0a391095John McCall // Does the symbol simplify to a constant? If so, "fold" the constant 488d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // by setting 'lhs' to a ConcreteInt and try again. 4892acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner if (lhsType->isIntegerType()) 490875ab10245d3bf37252dd822aa1616bb0a391095John McCall if (const llvm::APSInt *Constant = state->getSymVal(Sym)) { 491d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // The symbol evaluates to a constant. If necessary, promote the 492d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // folded constant (LHS) to the result type. 493d608cdb7c044365cf4e8764ade1e11e99c176078John McCall const llvm::APSInt &lhs_I = BasicVals.Convert(conversionType, 4941d2b31710539d705a3850c9fc3aa1804c2a5efeePeter Collingbourne *Constant); 495875ab10245d3bf37252dd822aa1616bb0a391095John McCall lhs = nonloc::ConcreteInt(lhs_I); 4961246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck 4971246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck // Also promote the RHS (if necessary). 498bcfd1f55bfbb3e5944cd5e03d07b343e280838c4Douglas Gregor 4991246ba6f6801390ffc0e1d4b83a2b45e72283b8fKen Dyck // For shifts, it is not necessary to promote the RHS. 500d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (BinaryOperator::isShiftOp(op)) 501d608cdb7c044365cf4e8764ade1e11e99c176078John McCall continue; 502d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 503d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Other operators: do an implicit conversion. This shouldn't be 504d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // necessary once we support truncation/extension of symbolic values. 505d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (nonloc::ConcreteInt *rhs_I = dyn_cast<nonloc::ConcreteInt>(&rhs)){ 506d608cdb7c044365cf4e8764ade1e11e99c176078John McCall rhs = nonloc::ConcreteInt(BasicVals.Convert(conversionType, 507d608cdb7c044365cf4e8764ade1e11e99c176078John McCall rhs_I->getValue())); 508d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 5092d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5102d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith continue; 511d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 512d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 513d608cdb7c044365cf4e8764ade1e11e99c176078John McCall // Is the RHS a symbol we can simplify? 514d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (const nonloc::SymbolVal *srhs = dyn_cast<nonloc::SymbolVal>(&rhs)) { 515d608cdb7c044365cf4e8764ade1e11e99c176078John McCall SymbolRef RSym = srhs->getSymbol(); 516d608cdb7c044365cf4e8764ade1e11e99c176078John McCall if (RSym->getType(Context)->isIntegerType()) { 5172d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (const llvm::APSInt *Constant = state->getSymVal(RSym)) { 5182d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // The symbol evaluates to a constant. 519d608cdb7c044365cf4e8764ade1e11e99c176078John McCall const llvm::APSInt &rhs_I = BasicVals.Convert(conversionType, 520d608cdb7c044365cf4e8764ade1e11e99c176078John McCall *Constant); 521755d8497e39071aa24acc173ff07083e3256b8f8John McCall rhs = nonloc::ConcreteInt(rhs_I); 5222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner } 523755d8497e39071aa24acc173ff07083e3256b8f8John McCall } 524f742eb0196e1b25c0b71e91da4a2b856d16a1dabChris Lattner } 525755d8497e39071aa24acc173ff07083e3256b8f8John McCall 526de5d3c717684f3821b8db58037bc7140acf134aaJohn McCall if (isa<nonloc::ConcreteInt>(rhs)) { 527d608cdb7c044365cf4e8764ade1e11e99c176078John McCall return MakeSymIntVal(slhs->getSymbol(), op, 528755d8497e39071aa24acc173ff07083e3256b8f8John McCall cast<nonloc::ConcreteInt>(rhs).getValue(), 529755d8497e39071aa24acc173ff07083e3256b8f8John McCall resultTy); 530755d8497e39071aa24acc173ff07083e3256b8f8John McCall } 531d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 532755d8497e39071aa24acc173ff07083e3256b8f8John McCall return UnknownVal(); 533d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 534379b5155b4566f63679e1da6b0ceb5fdfa2aec6dJohn McCall } 5352d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith } 5362d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith} 537d608cdb7c044365cf4e8764ade1e11e99c176078John McCall 538d608cdb7c044365cf4e8764ade1e11e99c176078John McCall// FIXME: all this logic will change if/when we have MemRegion::getLocation(). 539c5cbb909e8a27deb8f1a2b6b7bf56a96051af81aChris LattnerSVal SimpleSValBuilder::evalBinOpLL(const ProgramState *state, 540875ab10245d3bf37252dd822aa1616bb0a391095John McCall BinaryOperator::Opcode op, 541875ab10245d3bf37252dd822aa1616bb0a391095John McCall Loc lhs, Loc rhs, 5422d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith QualType resultTy) { 5432d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // Only comparisons and subtractions are valid operations on two pointers. 5442d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15]. 5452d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // However, if a pointer is casted to an integer, evalBinOpNN may end up 5462d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // calling this function with another operation (PR7527). We don't attempt to 5472d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // model this for now, but it could be useful, particularly when the 5482d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // "location" is actually an integer value that's been passed through a void*. 5492d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (!(BinaryOperator::isComparisonOp(op) || op == BO_Sub)) 5502d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return UnknownVal(); 5512d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5522d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith // Special cases for when both sides are identical. 5532d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith if (lhs == rhs) { 5542d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith switch (op) { 5552d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith default: 5562d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm_unreachable("Unimplemented operation for two identical values"); 5572d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_Sub: 5582d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return makeZeroVal(resultTy); 5592d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_EQ: 5602d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_LE: 5612d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_GE: 5622d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return makeTruthVal(true, resultTy); 5632d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_NE: 5642d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_LT: 5652d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith case BO_GT: 5662d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith return makeTruthVal(false, resultTy); 5672d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith } 5682d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith } 5692d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 5702d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith switch (lhs.getSubKind()) { 5712d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith default: 5722d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith llvm_unreachable("Ordering not implemented for this Loc."); 5732d6a5670465cb3f1d811695a9f23e372508240d2Richard Smith 574e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case loc::GotoLabelKind: 575e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // The only thing we know about labels is that they're non-null. 576e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (rhs.isZeroConstant()) { 577e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall switch (op) { 578e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall default: 5790bab0cdab751248ca389a5592bcb70eac5d39260John McCall break; 5800bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_Sub: 5810bab0cdab751248ca389a5592bcb70eac5d39260John McCall return evalCastFromLoc(lhs, resultTy); 5820bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_EQ: 5830bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_LE: 584e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_LT: 585e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return makeTruthVal(false, resultTy); 586e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_NE: 587e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GT: 588e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GE: 589e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return makeTruthVal(true, resultTy); 590e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 591e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 592e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // There may be two labels for the same location, and a function region may 593e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // have the same address as a label at the start of the function (depending 594e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // on the ABI). 595e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // FIXME: we can probably do a comparison against other MemRegions, though. 596e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // FIXME: is there a way to tell if two labels refer to the same location? 597e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return UnknownVal(); 5980bab0cdab751248ca389a5592bcb70eac5d39260John McCall 5990bab0cdab751248ca389a5592bcb70eac5d39260John McCall case loc::ConcreteIntKind: { 6000bab0cdab751248ca389a5592bcb70eac5d39260John McCall // If one of the operands is a symbol and the other is a constant, 6010bab0cdab751248ca389a5592bcb70eac5d39260John McCall // build an expression for use by the constraint manager. 6020bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (SymbolRef rSym = rhs.getAsLocSymbol()) { 6030bab0cdab751248ca389a5592bcb70eac5d39260John McCall // We can only build expressions with symbols on the left, 6040bab0cdab751248ca389a5592bcb70eac5d39260John McCall // so we need a reversible operator. 605de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall if (!BinaryOperator::isComparisonOp(op)) 6060bab0cdab751248ca389a5592bcb70eac5d39260John McCall return UnknownVal(); 607de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall 608de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall const llvm::APSInt &lVal = cast<loc::ConcreteInt>(lhs).getValue(); 609de719f7131e1ece5c9d6b5ffe21b83286d29f10fJohn McCall return makeNonLoc(rSym, ReverseComparison(op), lVal, resultTy); 6100bab0cdab751248ca389a5592bcb70eac5d39260John McCall } 6110bab0cdab751248ca389a5592bcb70eac5d39260John McCall 6120bab0cdab751248ca389a5592bcb70eac5d39260John McCall // If both operands are constants, just perform the operation. 6130bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { 6140bab0cdab751248ca389a5592bcb70eac5d39260John McCall SVal ResultVal = cast<loc::ConcreteInt>(lhs).evalBinOp(BasicVals, op, 6150bab0cdab751248ca389a5592bcb70eac5d39260John McCall *rInt); 616e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (Loc *Result = dyn_cast<Loc>(&ResultVal)) 617e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return evalCastFromLoc(*Result, resultTy); 618e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall else 619e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return UnknownVal(); 620e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 621e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 622e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Special case comparisons against NULL. 623e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // This must come after the test if the RHS is a symbol, which is used to 624e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // build constraints. The address of any non-symbolic region is guaranteed 625e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // to be non-NULL, as is any label. 626e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall assert(isa<loc::MemRegionVal>(rhs) || isa<loc::GotoLabel>(rhs)); 627e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (lhs.isZeroConstant()) { 628d608cdb7c044365cf4e8764ade1e11e99c176078John McCall switch (op) { 629d608cdb7c044365cf4e8764ade1e11e99c176078John McCall default: 630e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall break; 631e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_EQ: 632e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GT: 633e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GE: 634e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return makeTruthVal(false, resultTy); 635e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_NE: 636e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_LT: 637e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_LE: 638e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return makeTruthVal(true, resultTy); 639e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 640e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 641e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 642e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Comparing an arbitrary integer to a region or label address is 643e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // completely unknowable. 644e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return UnknownVal(); 645e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 646e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case loc::MemRegionKind: { 647e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (loc::ConcreteInt *rInt = dyn_cast<loc::ConcreteInt>(&rhs)) { 648e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // If one of the operands is a symbol and the other is a constant, 649e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // build an expression for use by the constraint manager. 650e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (SymbolRef lSym = lhs.getAsLocSymbol()) 651e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return MakeSymIntVal(lSym, op, rInt->getValue(), resultTy); 652e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 6530bab0cdab751248ca389a5592bcb70eac5d39260John McCall // Special case comparisons to NULL. 6540bab0cdab751248ca389a5592bcb70eac5d39260John McCall // This must come after the test if the LHS is a symbol, which is used to 6550bab0cdab751248ca389a5592bcb70eac5d39260John McCall // build constraints. The address of any non-symbolic region is guaranteed 656e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // to be non-NULL. 6570bab0cdab751248ca389a5592bcb70eac5d39260John McCall if (rInt->isZeroConstant()) { 6580bab0cdab751248ca389a5592bcb70eac5d39260John McCall switch (op) { 6590bab0cdab751248ca389a5592bcb70eac5d39260John McCall default: 6600bab0cdab751248ca389a5592bcb70eac5d39260John McCall break; 6610bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_Sub: 6620bab0cdab751248ca389a5592bcb70eac5d39260John McCall return evalCastFromLoc(lhs, resultTy); 6630bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_EQ: 6640bab0cdab751248ca389a5592bcb70eac5d39260John McCall case BO_LT: 665e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_LE: 666db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar return makeTruthVal(false, resultTy); 667d608cdb7c044365cf4e8764ade1e11e99c176078John McCall case BO_NE: 668e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GT: 669e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall case BO_GE: 670e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return makeTruthVal(true, resultTy); 671e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall } 672db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar } 673db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar 674e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // Comparing a region to an arbitrary integer is completely unknowable. 675e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall return UnknownVal(); 676d608cdb7c044365cf4e8764ade1e11e99c176078John McCall } 677e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 678db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar // Get both values as regions, if possible. 679db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar const MemRegion *LeftMR = lhs.getAsRegion(); 680db27b5fb2d7fc50b8962b2c95e4d43b90c69b1f0Daniel Dunbar assert(LeftMR && "MemRegionKind SVal doesn't have a region!"); 681e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall 682e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall const MemRegion *RightMR = rhs.getAsRegion(); 683e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall if (!RightMR) 684e9fd7eb6c67676dc27e84eac429aec4f3be51f26John McCall // The RHS is probably a label, which in theory could address a region. 685875ab10245d3bf37252dd822aa1616bb0a391095John McCall // FIXME: we can probably make a more useful statement about non-code 686f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // regions, though. 687f16aa103d3afd42fbca2ab346f191bf745cec092John McCall return UnknownVal(); 688f16aa103d3afd42fbca2ab346f191bf745cec092John McCall 689f16aa103d3afd42fbca2ab346f191bf745cec092John McCall // If both values wrap regions, see if they're from different base regions. 690cf2c85e76fdafe7e634810a292321a6c8322483dJohn McCall const MemRegion *LeftBase = LeftMR->getBaseRegion(); 6914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const MemRegion *RightBase = RightMR->getBaseRegion(); 692ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall if (LeftBase != RightBase && 693ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall !isa<SymbolicRegion>(LeftBase) && !isa<SymbolicRegion>(RightBase)) { 694ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall switch (op) { 695ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall default: 696ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return UnknownVal(); 697ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall case BO_EQ: 698ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return makeTruthVal(false, resultTy); 699ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall case BO_NE: 700ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall return makeTruthVal(true, resultTy); 701ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall } 702ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall } 703ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 704ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // The two regions are from the same base region. See if they're both a 705ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // type of region we know how to compare. 706ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall 707ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // FIXME: If/when there is a getAsRawOffset() for FieldRegions, this 708ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // ElementRegion path and the FieldRegion path below should be unified. 709ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall if (const ElementRegion *LeftER = dyn_cast<ElementRegion>(LeftMR)) { 710ecd03b447bb0e2ed1954c77441d49a4a17ca8138John McCall // First see if the right region is also an ElementRegion. 7114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const ElementRegion *RightER = dyn_cast<ElementRegion>(RightMR); 7124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!RightER) 7134c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Next, see if the two ERs have the same super-region and matching types. 7165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner // FIXME: This should do something useful even if the types don't match, 7179cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall // though if both indexes are constant the RegionRawOffset path will 7184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // give the correct answer. 7194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (LeftER->getSuperRegion() == RightER->getSuperRegion() && 7204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall LeftER->getElementType() == RightER->getElementType()) { 7214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Get the left index and cast it to the correct type. 7224c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // If the index is unknown or undefined, bail out here. 7234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall SVal LeftIndexVal = LeftER->getIndex(); 7244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall NonLoc *LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); 7254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!LeftIndex) 7264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7274c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall LeftIndexVal = evalCastFromNonLoc(*LeftIndex, resultTy); 7284c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall LeftIndex = dyn_cast<NonLoc>(&LeftIndexVal); 7294c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!LeftIndex) 7305f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return UnknownVal(); 7314c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7324c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Do the same for the right index. 7334c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall SVal RightIndexVal = RightER->getIndex(); 7344c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall NonLoc *RightIndex = dyn_cast<NonLoc>(&RightIndexVal); 7354c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!RightIndex) 7364c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7374c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RightIndexVal = evalCastFromNonLoc(*RightIndex, resultTy); 7384c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RightIndex = dyn_cast<NonLoc>(&RightIndexVal); 7394c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!RightIndex) 7405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner return UnknownVal(); 7419cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall 7424c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Actually perform the operation. 7434c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // evalBinOpNN expects the two indexes to already be the right type. 7444c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return evalBinOpNN(state, op, *LeftIndex, *RightIndex, resultTy); 7454c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7464c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7474c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // If the element indexes aren't comparable, see if the raw offsets are. 7484c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RegionRawOffset LeftOffset = LeftER->getAsArrayOffset(); 7494c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall RegionRawOffset RightOffset = RightER->getAsArrayOffset(); 7504c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7514c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (LeftOffset.getRegion() != NULL && 7524c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall LeftOffset.getRegion() == RightOffset.getRegion()) { 7534c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CharUnits left = LeftOffset.getOffset(); 7544c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall CharUnits right = RightOffset.getOffset(); 7555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner 7564c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall switch (op) { 7574c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall default: 7584c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7594c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_LT: 7604c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left < right, resultTy); 7614c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_GT: 7624c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left > right, resultTy); 7634c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_LE: 7644c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left <= right, resultTy); 7654c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_GE: 7664c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left >= right, resultTy); 7674c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_EQ: 7684c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left == right, resultTy); 7694c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall case BO_NE: 7704c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(left != right, resultTy); 7714c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7724c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7739cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall 7744c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // If we get here, we have no way of comparing the ElementRegions. 7754c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7764c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 7774c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7784c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // See if both regions are fields of the same structure. 7794c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // FIXME: This doesn't handle nesting, inheritance, or Objective-C ivars. 780d26bc76c98006609002d9930f8840490e88ac5b5John McCall if (const FieldRegion *LeftFR = dyn_cast<FieldRegion>(LeftMR)) { 7814c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Only comparisons are meaningful here! 7824c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!BinaryOperator::isComparisonOp(op)) 7834c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7844c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7854c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // First see if the right region is also a FieldRegion. 7864c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const FieldRegion *RightFR = dyn_cast<FieldRegion>(RightMR); 7874c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (!RightFR) 7884c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7894c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7904c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Next, see if the two FRs have the same super-region. 7914c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // FIXME: This doesn't handle casts yet, and simply stripping the casts 792d26bc76c98006609002d9930f8840490e88ac5b5John McCall // doesn't help. 7934c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (LeftFR->getSuperRegion() != RightFR->getSuperRegion()) 7944c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 7954c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 7964c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const FieldDecl *LeftFD = LeftFR->getDecl(); 7974c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const FieldDecl *RightFD = RightFR->getDecl(); 7984c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall const RecordDecl *RD = LeftFD->getParent(); 7994c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8004c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Make sure the two FRs are from the same kind of record. Just in case! 8014c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // FIXME: This is probably where inheritance would be a problem. 8024c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (RD != RightFD->getParent()) 8034c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return UnknownVal(); 8044c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8054c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // We know for sure that the two fields are not the same, since that 8064c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // would have given us the same SVal. 8074c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (op == BO_EQ) 8084c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(false, resultTy); 8094c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (op == BO_NE) 8104c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(true, resultTy); 8114c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8124c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // Iterate through the fields and see which one comes first. 813cec5ebd4a6a89a7023d04cec728fd340b541ed61Eli Friedman // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field 8144c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // members and the units in which bit-fields reside have addresses that 8154c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall // increase in the order in which they are declared." 8164c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall bool leftFirst = (op == BO_LT || op == BO_LE); 8174c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall for (RecordDecl::field_iterator I = RD->field_begin(), 8184c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall E = RD->field_end(); I!=E; ++I) { 8194c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (*I == LeftFD) 8204c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall return makeTruthVal(leftFirst, resultTy); 8214c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall if (*I == RightFD) 8222acc6e3feda5e4f7d9009bdcf8b1cd777fecfe2dChris Lattner return makeTruthVal(!leftFirst, resultTy); 8234c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 8244c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall 8254c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall llvm_unreachable("Fields not found in parent record's definition"); 8264c40d98ab7acf5f27fa89b17bd8fc0ef7683df37John McCall } 8271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // If we get here, we have no way of comparing the regions. 8291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return UnknownVal(); 830e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall } 831e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall } 832e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall} 833e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 834e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCallSVal SimpleSValBuilder::evalBinOpLN(const ProgramState *state, 8351e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall BinaryOperator::Opcode op, 8361e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall Loc lhs, NonLoc rhs, QualType resultTy) { 8371e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8381e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Special case: rhs is a zero constant. 8391e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (rhs.isZeroConstant()) 8406ec278d1a354517e20f13a877481453ee7940c78John McCall return lhs; 8411e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 842e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // Special case: 'rhs' is an integer that has the same width as a pointer and 8431e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // we are using the integer location in a comparison. Normally this cannot be 844956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow // triggered, but transfer functions like those for OSCommpareAndSwapBarrier32 8451e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // can generate comparisons that trigger this code. 8469cb2cee212d708220c52249ceac4cdd9f2b8aeb0John McCall // FIXME: Are all locations guaranteed to have pointer width? 8471e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (BinaryOperator::isComparisonOp(op)) { 8481e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { 8491e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall const llvm::APSInt *x = &rhsInt->getValue(); 8501e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall ASTContext &ctx = Context; 8511e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (ctx.getTypeSize(ctx.VoidPtrTy) == x->getBitWidth()) { 8521e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Convert the signedness of the integer (if necessary). 853e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (x->isSigned()) 8541e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall x = &getBasicValueFactory().getValue(*x, true); 8551e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8561e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return evalBinOpLL(state, op, lhs, loc::ConcreteInt(*x), resultTy); 8571e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 8581e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 8591e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 8601e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8611e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // We are dealing with pointer arithmetic. 8621e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8631e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Handle pointer arithmetic on constant values. 8641e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (nonloc::ConcreteInt *rhsInt = dyn_cast<nonloc::ConcreteInt>(&rhs)) { 8651e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (loc::ConcreteInt *lhsInt = dyn_cast<loc::ConcreteInt>(&lhs)) { 8661e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall const llvm::APSInt &leftI = lhsInt->getValue(); 8671e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall assert(leftI.isUnsigned()); 8681e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall llvm::APSInt rightI(rhsInt->getValue(), /* isUnsigned */ true); 8691e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 8701e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // Convert the bitwidth of rightI. This should deal with overflow 8711e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // since we are dealing with concrete values. 8721e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall rightI = rightI.extOrTrunc(leftI.getBitWidth()); 8731e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 874e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // Offset the increment by the pointer size. 875e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm::APSInt Multiplicand(rightI.getBitWidth(), /* isUnsigned */ true); 876e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall rightI *= Multiplicand; 877e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 878e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall // Compute the adjusted pointer. 879e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall switch (op) { 880e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall case BO_Add: 881e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall rightI = leftI + rightI; 882e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall break; 883e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall case BO_Sub: 884e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall rightI = leftI - rightI; 8851e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall break; 886956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow default: 887e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall llvm_unreachable("Invalid pointer arithmetic operation"); 888e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall } 889e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return loc::ConcreteInt(getBasicValueFactory().getValue(rightI)); 8901e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 8911e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 892e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 893f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall // Handle cases where 'lhs' is a region. 8941e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (const MemRegion *region = lhs.getAsRegion()) { 8951e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall rhs = cast<NonLoc>(convertToArrayIndex(rhs)); 8961e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall SVal index = UnknownVal(); 8971e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall const MemRegion *superR = 0; 898f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall QualType elementType; 899f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall 900f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall if (const ElementRegion *elemReg = dyn_cast<ElementRegion>(region)) { 901f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall assert(op == BO_Add || op == BO_Sub); 902f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall index = evalBinOpNN(state, op, elemReg->getIndex(), rhs, 9031e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall getArrayIndexType()); 9041e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall superR = elemReg->getSuperRegion(); 9051e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall elementType = elemReg->getElementType(); 906f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall } 907f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall else if (isa<SubRegion>(region)) { 9086ec278d1a354517e20f13a877481453ee7940c78John McCall superR = region; 909f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall index = rhs; 910e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (const PointerType *PT = resultTy->getAs<PointerType>()) { 9111e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall elementType = PT->getPointeeType(); 912f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall } 913f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall else { 9141e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall const ObjCObjectPointerType *OT = 9151e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall resultTy->getAs<ObjCObjectPointerType>(); 916f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall elementType = OT->getPointeeType(); 9171e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 9181e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 919f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall 920f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall if (NonLoc *indexV = dyn_cast<NonLoc>(&index)) { 921f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return loc::MemRegionVal(MemMgr.getElementRegion(elementType, *indexV, 922f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall superR, getContext())); 9231e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 9241e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall } 925f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return UnknownVal(); 926f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall} 9271e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall 9281e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCallconst llvm::APSInt *SimpleSValBuilder::getKnownValue(const ProgramState *state, 9291e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall SVal V) { 930f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall if (V.isUnknownOrUndef()) 931f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall return NULL; 932f3bbb155beb69cdad1c6b0472bc0ca20cece6c50John McCall 9331e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V)) 9341e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall return &X->getValue(); 935e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 936e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V)) 937e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return &X->getValue(); 938e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 939e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall if (SymbolRef Sym = V.getAsSymbol()) 940e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall return state->getSymVal(Sym); 941e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall 9421e7fe751466ea82665fd21e9162fd7cc9c5f412dJohn McCall // FIXME: Add support for SymExprs. 943956a5a17713deb1b5b27893303c4f308a1bd2a62Micah Villmow return NULL; 944e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall} 945e2b45e2a43ae46bc00026b63ba7c04ef2b78c3ffJohn McCall