SValBuilder.h revision c35fb7d67d515659ad2325b4f6ec97c9fe64fb63
1// SValBuilder.h - Construction of SVals from evaluating expressions -*- C++ -*- 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines SValBuilder, a class that defines the interface for 11// "symbolical evaluators" which construct an SVal from an expression. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_GR_SVALBUILDER 16#define LLVM_CLANG_GR_SVALBUILDER 17 18#include "clang/AST/Expr.h" 19#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 20#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 21#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 22 23namespace clang { 24 25class CXXBoolLiteralExpr; 26 27namespace ento { 28 29class SValBuilder { 30 virtual void anchor(); 31protected: 32 ASTContext &Context; 33 34 /// Manager of APSInt values. 35 BasicValueFactory BasicVals; 36 37 /// Manages the creation of symbols. 38 SymbolManager SymMgr; 39 40 /// Manages the creation of memory regions. 41 MemRegionManager MemMgr; 42 43 ProgramStateManager &StateMgr; 44 45 /// The scalar type to use for array indices. 46 const QualType ArrayIndexTy; 47 48 /// The width of the scalar type used for array indices. 49 const unsigned ArrayIndexWidth; 50 51 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0; 52 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0; 53 54public: 55 // FIXME: Make these protected again once RegionStoreManager correctly 56 // handles loads from different bound value types. 57 virtual SVal dispatchCast(SVal val, QualType castTy) = 0; 58 59public: 60 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 61 ProgramStateManager &stateMgr) 62 : Context(context), BasicVals(context, alloc), 63 SymMgr(context, BasicVals, alloc), 64 MemMgr(context, alloc), 65 StateMgr(stateMgr), 66 ArrayIndexTy(context.IntTy), 67 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {} 68 69 virtual ~SValBuilder() {} 70 71 bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) { 72 return haveSameType(Sym1->getType(Context), Sym2->getType(Context)); 73 } 74 75 bool haveSameType(QualType Ty1, QualType Ty2) { 76 // FIXME: Remove the second disjunct when we support symbolic 77 // truncation/extension. 78 return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) || 79 (Ty2->isIntegerType() && Ty2->isIntegerType())); 80 } 81 82 SVal evalCast(SVal val, QualType castTy, QualType originalType); 83 84 virtual SVal evalMinus(NonLoc val) = 0; 85 86 virtual SVal evalComplement(NonLoc val) = 0; 87 88 /// Create a new value which represents a binary expression with two non 89 /// location operands. 90 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, 91 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0; 92 93 /// Create a new value which represents a binary expression with two memory 94 /// location operands. 95 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, 96 Loc lhs, Loc rhs, QualType resultTy) = 0; 97 98 /// Create a new value which represents a binary expression with a memory 99 /// location and non location operands. For example, this would be used to 100 /// evaluate a pointer arithmetic operation. 101 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, 102 Loc lhs, NonLoc rhs, QualType resultTy) = 0; 103 104 /// Evaluates a given SVal. If the SVal has only one possible (integer) value, 105 /// that value is returned. Otherwise, returns NULL. 106 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0; 107 108 /// Handles generation of the value in case the builder is not smart enough to 109 /// handle the given binary expression. Depending on the state, decides to 110 /// either keep the expression or forget the history and generate an 111 /// UnknownVal. 112 SVal makeGenericVal(ProgramStateRef state, BinaryOperator::Opcode op, 113 NonLoc lhs, NonLoc rhs, QualType resultTy); 114 115 SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 116 SVal lhs, SVal rhs, QualType type); 117 118 DefinedOrUnknownSVal evalEQ(ProgramStateRef state, DefinedOrUnknownSVal lhs, 119 DefinedOrUnknownSVal rhs); 120 121 ASTContext &getContext() { return Context; } 122 const ASTContext &getContext() const { return Context; } 123 124 ProgramStateManager &getStateManager() { return StateMgr; } 125 126 QualType getConditionType() const { 127 return getContext().IntTy; 128 } 129 130 QualType getArrayIndexType() const { 131 return ArrayIndexTy; 132 } 133 134 BasicValueFactory &getBasicValueFactory() { return BasicVals; } 135 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; } 136 137 SymbolManager &getSymbolManager() { return SymMgr; } 138 const SymbolManager &getSymbolManager() const { return SymMgr; } 139 140 MemRegionManager &getRegionManager() { return MemMgr; } 141 const MemRegionManager &getRegionManager() const { return MemMgr; } 142 143 // Forwarding methods to SymbolManager. 144 145 const SymbolConjured* getConjuredSymbol(const Stmt *stmt, QualType type, 146 unsigned visitCount, 147 const void *symbolTag = 0) { 148 return SymMgr.getConjuredSymbol(stmt, type, visitCount, symbolTag); 149 } 150 151 const SymbolConjured* getConjuredSymbol(const Expr *expr, unsigned visitCount, 152 const void *symbolTag = 0) { 153 return SymMgr.getConjuredSymbol(expr, visitCount, symbolTag); 154 } 155 156 /// Construct an SVal representing '0' for the specified type. 157 DefinedOrUnknownSVal makeZeroVal(QualType type); 158 159 /// Make a unique symbol for value of region. 160 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region); 161 162 /// \brief Create a new symbol with a unique 'name'. 163 /// 164 /// We resort to conjured symbols when we cannot construct a derived symbol. 165 /// The advantage of symbols derived/built from other symbols is that we 166 /// preserve the relation between related(or even equivalent) expressions, so 167 /// conjured symbols should be used sparingly. 168 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag, 169 const Expr *expr, unsigned count); 170 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag, 171 const Expr *expr, QualType type, 172 unsigned count); 173 174 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal( 175 SymbolRef parentSymbol, const TypedValueRegion *region); 176 177 DefinedSVal getMetadataSymbolVal( 178 const void *symbolTag, const MemRegion *region, 179 const Expr *expr, QualType type, unsigned count); 180 181 DefinedSVal getFunctionPointer(const FunctionDecl *func); 182 183 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, 184 const LocationContext *locContext); 185 186 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) { 187 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals)); 188 } 189 190 NonLoc makeLazyCompoundVal(const StoreRef &store, 191 const TypedValueRegion *region) { 192 return nonloc::LazyCompoundVal( 193 BasicVals.getLazyCompoundValData(store, region)); 194 } 195 196 NonLoc makeZeroArrayIndex() { 197 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy)); 198 } 199 200 NonLoc makeArrayIndex(uint64_t idx) { 201 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy)); 202 } 203 204 SVal convertToArrayIndex(SVal val); 205 206 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) { 207 return nonloc::ConcreteInt( 208 BasicVals.getValue(integer->getValue(), 209 integer->getType()->isUnsignedIntegerOrEnumerationType())); 210 } 211 212 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean); 213 214 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) { 215 return nonloc::ConcreteInt(BasicVals.getValue(integer)); 216 } 217 218 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) { 219 return loc::ConcreteInt(BasicVals.getValue(integer)); 220 } 221 222 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) { 223 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned)); 224 } 225 226 DefinedSVal makeIntVal(uint64_t integer, QualType type) { 227 if (Loc::isLocType(type)) 228 return loc::ConcreteInt(BasicVals.getValue(integer, type)); 229 230 return nonloc::ConcreteInt(BasicVals.getValue(integer, type)); 231 } 232 233 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) { 234 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned)); 235 } 236 237 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) { 238 return nonloc::ConcreteInt( 239 BasicVals.getIntWithPtrWidth(integer, isUnsigned)); 240 } 241 242 NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) { 243 return nonloc::ConcreteInt( 244 BasicVals.getValue(integer, bitWidth, isUnsigned)); 245 } 246 247 NonLoc makeLocAsInteger(Loc loc, unsigned bits) { 248 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits)); 249 } 250 251 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 252 const llvm::APSInt& rhs, QualType type); 253 254 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op, 255 const SymExpr *lhs, QualType type); 256 257 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 258 const SymExpr *rhs, QualType type); 259 260 /// \brief Create a NonLoc value for cast. 261 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy); 262 263 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) { 264 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type)); 265 } 266 267 nonloc::ConcreteInt makeTruthVal(bool b) { 268 return nonloc::ConcreteInt(BasicVals.getTruthValue(b)); 269 } 270 271 Loc makeNull() { 272 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth()); 273 } 274 275 Loc makeLoc(SymbolRef sym) { 276 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 277 } 278 279 Loc makeLoc(const MemRegion* region) { 280 return loc::MemRegionVal(region); 281 } 282 283 Loc makeLoc(const AddrLabelExpr *expr) { 284 return loc::GotoLabel(expr->getLabel()); 285 } 286 287 Loc makeLoc(const llvm::APSInt& integer) { 288 return loc::ConcreteInt(BasicVals.getValue(integer)); 289 } 290 291}; 292 293SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 294 ASTContext &context, 295 ProgramStateManager &stateMgr); 296 297} // end GR namespace 298 299} // end clang namespace 300 301#endif 302