SValBuilder.h revision 263508
174667Sjedgar// SValBuilder.h - Construction of SVals from evaluating expressions -*- C++ -*- 274667Sjedgar// 374667Sjedgar// The LLVM Compiler Infrastructure 474667Sjedgar// 574667Sjedgar// This file is distributed under the University of Illinois Open Source 674667Sjedgar// License. See LICENSE.TXT for details. 774667Sjedgar// 874667Sjedgar//===----------------------------------------------------------------------===// 974667Sjedgar// 1074667Sjedgar// This file defines SValBuilder, a class that defines the interface for 1174667Sjedgar// "symbolical evaluators" which construct an SVal from an expression. 1274667Sjedgar// 1374667Sjedgar//===----------------------------------------------------------------------===// 1474667Sjedgar 1574667Sjedgar#ifndef LLVM_CLANG_GR_SVALBUILDER 1674667Sjedgar#define LLVM_CLANG_GR_SVALBUILDER 17184607Simp 18184607Simp#include "clang/AST/ASTContext.h" 19184607Simp#include "clang/AST/Expr.h" 20184607Simp#include "clang/AST/ExprObjC.h" 21184607Simp#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 22184607Simp#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 23184607Simp#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 24184607Simp 2574667Sjedgarnamespace clang { 2674667Sjedgar 2774667Sjedgarclass CXXBoolLiteralExpr; 2874667Sjedgar 2974667Sjedgarnamespace ento { 3074667Sjedgar 3174667Sjedgarclass SValBuilder { 3274667Sjedgar virtual void anchor(); 3374684Sruprotected: 3474667Sjedgar ASTContext &Context; 3575222Sru 3674667Sjedgar /// Manager of APSInt values. 3784306Sru BasicValueFactory BasicVals; 3884306Sru 3974667Sjedgar /// Manages the creation of symbols. 4074667Sjedgar SymbolManager SymMgr; 4174667Sjedgar 42108037Sru /// Manages the creation of memory regions. 4374667Sjedgar MemRegionManager MemMgr; 44108037Sru 4574667Sjedgar ProgramStateManager &StateMgr; 4674684Sru 4774667Sjedgar /// The scalar type to use for array indices. 4874684Sru const QualType ArrayIndexTy; 4974667Sjedgar 5074667Sjedgar /// The width of the scalar type used for array indices. 5174667Sjedgar const unsigned ArrayIndexWidth; 5274684Sru 5374667Sjedgar virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0; 5474684Sru virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0; 5574667Sjedgar 5674684Srupublic: 5774667Sjedgar // FIXME: Make these protected again once RegionStoreManager correctly 5874667Sjedgar // handles loads from different bound value types. 5974667Sjedgar virtual SVal dispatchCast(SVal val, QualType castTy) = 0; 6074684Sru 6174667Sjedgarpublic: 6274667Sjedgar SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 6374667Sjedgar ProgramStateManager &stateMgr) 6474667Sjedgar : Context(context), BasicVals(context, alloc), 6574667Sjedgar SymMgr(context, BasicVals, alloc), 6674667Sjedgar MemMgr(context, alloc), 6774667Sjedgar StateMgr(stateMgr), 6874667Sjedgar ArrayIndexTy(context.IntTy), 6974667Sjedgar ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {} 7074667Sjedgar 7174667Sjedgar virtual ~SValBuilder() {} 7274667Sjedgar 7374667Sjedgar bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) { 7474667Sjedgar return haveSameType(Sym1->getType(), Sym2->getType()); 7574667Sjedgar } 7674667Sjedgar 7774667Sjedgar bool haveSameType(QualType Ty1, QualType Ty2) { 7874667Sjedgar // FIXME: Remove the second disjunct when we support symbolic 7974667Sjedgar // truncation/extension. 8074667Sjedgar return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) || 8174667Sjedgar (Ty1->isIntegralOrEnumerationType() && 8274667Sjedgar Ty2->isIntegralOrEnumerationType())); 8374667Sjedgar } 84 85 SVal evalCast(SVal val, QualType castTy, QualType originalType); 86 87 virtual SVal evalMinus(NonLoc val) = 0; 88 89 virtual SVal evalComplement(NonLoc val) = 0; 90 91 /// Create a new value which represents a binary expression with two non 92 /// location operands. 93 virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, 94 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0; 95 96 /// Create a new value which represents a binary expression with two memory 97 /// location operands. 98 virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, 99 Loc lhs, Loc rhs, QualType resultTy) = 0; 100 101 /// Create a new value which represents a binary expression with a memory 102 /// location and non location operands. For example, this would be used to 103 /// evaluate a pointer arithmetic operation. 104 virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, 105 Loc lhs, NonLoc rhs, QualType resultTy) = 0; 106 107 /// Evaluates a given SVal. If the SVal has only one possible (integer) value, 108 /// that value is returned. Otherwise, returns NULL. 109 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0; 110 111 /// Constructs a symbolic expression for two non-location values. 112 SVal makeSymExprValNN(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 Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.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* conjureSymbol(const Stmt *stmt, 146 const LocationContext *LCtx, 147 QualType type, 148 unsigned visitCount, 149 const void *symbolTag = 0) { 150 return SymMgr.conjureSymbol(stmt, LCtx, type, visitCount, symbolTag); 151 } 152 153 const SymbolConjured* conjureSymbol(const Expr *expr, 154 const LocationContext *LCtx, 155 unsigned visitCount, 156 const void *symbolTag = 0) { 157 return SymMgr.conjureSymbol(expr, LCtx, visitCount, symbolTag); 158 } 159 160 /// Construct an SVal representing '0' for the specified type. 161 DefinedOrUnknownSVal makeZeroVal(QualType type); 162 163 /// Make a unique symbol for value of region. 164 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region); 165 166 /// \brief Create a new symbol with a unique 'name'. 167 /// 168 /// We resort to conjured symbols when we cannot construct a derived symbol. 169 /// The advantage of symbols derived/built from other symbols is that we 170 /// preserve the relation between related(or even equivalent) expressions, so 171 /// conjured symbols should be used sparingly. 172 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, 173 const Expr *expr, 174 const LocationContext *LCtx, 175 unsigned count); 176 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, 177 const Expr *expr, 178 const LocationContext *LCtx, 179 QualType type, 180 unsigned count); 181 182 DefinedOrUnknownSVal conjureSymbolVal(const Stmt *stmt, 183 const LocationContext *LCtx, 184 QualType type, 185 unsigned visitCount); 186 /// \brief Conjure a symbol representing heap allocated memory region. 187 /// 188 /// Note, the expression should represent a location. 189 DefinedOrUnknownSVal getConjuredHeapSymbolVal(const Expr *E, 190 const LocationContext *LCtx, 191 unsigned Count); 192 193 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal( 194 SymbolRef parentSymbol, const TypedValueRegion *region); 195 196 DefinedSVal getMetadataSymbolVal( 197 const void *symbolTag, const MemRegion *region, 198 const Expr *expr, QualType type, unsigned count); 199 200 DefinedSVal getFunctionPointer(const FunctionDecl *func); 201 202 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, 203 const LocationContext *locContext, 204 unsigned blockCount); 205 206 /// Returns the value of \p E, if it can be determined in a non-path-sensitive 207 /// manner. 208 /// 209 /// If \p E is not a constant or cannot be modeled, returns \c None. 210 Optional<SVal> getConstantVal(const Expr *E); 211 212 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) { 213 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals)); 214 } 215 216 NonLoc makeLazyCompoundVal(const StoreRef &store, 217 const TypedValueRegion *region) { 218 return nonloc::LazyCompoundVal( 219 BasicVals.getLazyCompoundValData(store, region)); 220 } 221 222 NonLoc makeZeroArrayIndex() { 223 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy)); 224 } 225 226 NonLoc makeArrayIndex(uint64_t idx) { 227 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy)); 228 } 229 230 SVal convertToArrayIndex(SVal val); 231 232 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) { 233 return nonloc::ConcreteInt( 234 BasicVals.getValue(integer->getValue(), 235 integer->getType()->isUnsignedIntegerOrEnumerationType())); 236 } 237 238 nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean) { 239 return makeTruthVal(boolean->getValue(), boolean->getType()); 240 } 241 242 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean); 243 244 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) { 245 return nonloc::ConcreteInt(BasicVals.getValue(integer)); 246 } 247 248 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) { 249 return loc::ConcreteInt(BasicVals.getValue(integer)); 250 } 251 252 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) { 253 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned)); 254 } 255 256 DefinedSVal makeIntVal(uint64_t integer, QualType type) { 257 if (Loc::isLocType(type)) 258 return loc::ConcreteInt(BasicVals.getValue(integer, type)); 259 260 return nonloc::ConcreteInt(BasicVals.getValue(integer, type)); 261 } 262 263 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) { 264 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned)); 265 } 266 267 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) { 268 return nonloc::ConcreteInt( 269 BasicVals.getIntWithPtrWidth(integer, isUnsigned)); 270 } 271 272 NonLoc makeLocAsInteger(Loc loc, unsigned bits) { 273 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits)); 274 } 275 276 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 277 const llvm::APSInt& rhs, QualType type); 278 279 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op, 280 const SymExpr *lhs, QualType type); 281 282 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 283 const SymExpr *rhs, QualType type); 284 285 /// \brief Create a NonLoc value for cast. 286 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy); 287 288 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) { 289 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type)); 290 } 291 292 nonloc::ConcreteInt makeTruthVal(bool b) { 293 return nonloc::ConcreteInt(BasicVals.getTruthValue(b)); 294 } 295 296 Loc makeNull() { 297 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth()); 298 } 299 300 Loc makeLoc(SymbolRef sym) { 301 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 302 } 303 304 Loc makeLoc(const MemRegion* region) { 305 return loc::MemRegionVal(region); 306 } 307 308 Loc makeLoc(const AddrLabelExpr *expr) { 309 return loc::GotoLabel(expr->getLabel()); 310 } 311 312 Loc makeLoc(const llvm::APSInt& integer) { 313 return loc::ConcreteInt(BasicVals.getValue(integer)); 314 } 315 316 /// Return a memory region for the 'this' object reference. 317 loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, 318 const StackFrameContext *SFC); 319 320 /// Return a memory region for the 'this' object reference. 321 loc::MemRegionVal getCXXThis(const CXXRecordDecl *D, 322 const StackFrameContext *SFC); 323}; 324 325SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 326 ASTContext &context, 327 ProgramStateManager &stateMgr); 328 329} // end GR namespace 330 331} // end clang namespace 332 333#endif 334