SValBuilder.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. //===- SValBuilder.cpp - Basic class for all SValBuilder implementations --===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines SValBuilder, the base class for all (complete) SValBuilder
  10. // implementations.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/DeclCXX.h"
  17. #include "clang/AST/ExprCXX.h"
  18. #include "clang/AST/ExprObjC.h"
  19. #include "clang/AST/Stmt.h"
  20. #include "clang/AST/Type.h"
  21. #include "clang/Analysis/AnalysisDeclContext.h"
  22. #include "clang/Basic/LLVM.h"
  23. #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
  24. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  25. #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
  26. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  27. #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
  28. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  29. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
  30. #include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h"
  31. #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
  32. #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
  33. #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
  34. #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h"
  35. #include "llvm/ADT/APSInt.h"
  36. #include "llvm/Support/Casting.h"
  37. #include "llvm/Support/Compiler.h"
  38. #include <cassert>
  39. #include <optional>
  40. #include <tuple>
  41. using namespace clang;
  42. using namespace ento;
  43. //===----------------------------------------------------------------------===//
  44. // Basic SVal creation.
  45. //===----------------------------------------------------------------------===//
  46. void SValBuilder::anchor() {}
  47. SValBuilder::SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
  48. ProgramStateManager &stateMgr)
  49. : Context(context), BasicVals(context, alloc),
  50. SymMgr(context, BasicVals, alloc), MemMgr(context, alloc),
  51. StateMgr(stateMgr),
  52. AnOpts(
  53. stateMgr.getOwningEngine().getAnalysisManager().getAnalyzerOptions()),
  54. ArrayIndexTy(context.LongLongTy),
  55. ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
  56. DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) {
  57. if (Loc::isLocType(type))
  58. return makeNullWithType(type);
  59. if (type->isIntegralOrEnumerationType())
  60. return makeIntVal(0, type);
  61. if (type->isArrayType() || type->isRecordType() || type->isVectorType() ||
  62. type->isAnyComplexType())
  63. return makeCompoundVal(type, BasicVals.getEmptySValList());
  64. // FIXME: Handle floats.
  65. return UnknownVal();
  66. }
  67. nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
  68. BinaryOperator::Opcode op,
  69. const llvm::APSInt &rhs,
  70. QualType type) {
  71. // The Environment ensures we always get a persistent APSInt in
  72. // BasicValueFactory, so we don't need to get the APSInt from
  73. // BasicValueFactory again.
  74. assert(lhs);
  75. assert(!Loc::isLocType(type));
  76. return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type));
  77. }
  78. nonloc::SymbolVal SValBuilder::makeNonLoc(const llvm::APSInt &lhs,
  79. BinaryOperator::Opcode op,
  80. const SymExpr *rhs, QualType type) {
  81. assert(rhs);
  82. assert(!Loc::isLocType(type));
  83. return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type));
  84. }
  85. nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *lhs,
  86. BinaryOperator::Opcode op,
  87. const SymExpr *rhs, QualType type) {
  88. assert(lhs && rhs);
  89. assert(!Loc::isLocType(type));
  90. return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type));
  91. }
  92. NonLoc SValBuilder::makeNonLoc(const SymExpr *operand, UnaryOperator::Opcode op,
  93. QualType type) {
  94. assert(operand);
  95. assert(!Loc::isLocType(type));
  96. return nonloc::SymbolVal(SymMgr.getUnarySymExpr(operand, op, type));
  97. }
  98. nonloc::SymbolVal SValBuilder::makeNonLoc(const SymExpr *operand,
  99. QualType fromTy, QualType toTy) {
  100. assert(operand);
  101. assert(!Loc::isLocType(toTy));
  102. if (fromTy == toTy)
  103. return operand;
  104. return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy));
  105. }
  106. SVal SValBuilder::convertToArrayIndex(SVal val) {
  107. if (val.isUnknownOrUndef())
  108. return val;
  109. // Common case: we have an appropriately sized integer.
  110. if (std::optional<nonloc::ConcreteInt> CI =
  111. val.getAs<nonloc::ConcreteInt>()) {
  112. const llvm::APSInt& I = CI->getValue();
  113. if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
  114. return val;
  115. }
  116. return evalCast(val, ArrayIndexTy, QualType{});
  117. }
  118. nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){
  119. return makeTruthVal(boolean->getValue());
  120. }
  121. DefinedOrUnknownSVal
  122. SValBuilder::getRegionValueSymbolVal(const TypedValueRegion *region) {
  123. QualType T = region->getValueType();
  124. if (T->isNullPtrType())
  125. return makeZeroVal(T);
  126. if (!SymbolManager::canSymbolicate(T))
  127. return UnknownVal();
  128. SymbolRef sym = SymMgr.getRegionValueSymbol(region);
  129. if (Loc::isLocType(T))
  130. return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
  131. return nonloc::SymbolVal(sym);
  132. }
  133. DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag,
  134. const Expr *Ex,
  135. const LocationContext *LCtx,
  136. unsigned Count) {
  137. QualType T = Ex->getType();
  138. if (T->isNullPtrType())
  139. return makeZeroVal(T);
  140. // Compute the type of the result. If the expression is not an R-value, the
  141. // result should be a location.
  142. QualType ExType = Ex->getType();
  143. if (Ex->isGLValue())
  144. T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType);
  145. return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count);
  146. }
  147. DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag,
  148. const Expr *expr,
  149. const LocationContext *LCtx,
  150. QualType type,
  151. unsigned count) {
  152. if (type->isNullPtrType())
  153. return makeZeroVal(type);
  154. if (!SymbolManager::canSymbolicate(type))
  155. return UnknownVal();
  156. SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag);
  157. if (Loc::isLocType(type))
  158. return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
  159. return nonloc::SymbolVal(sym);
  160. }
  161. DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt,
  162. const LocationContext *LCtx,
  163. QualType type,
  164. unsigned visitCount) {
  165. if (type->isNullPtrType())
  166. return makeZeroVal(type);
  167. if (!SymbolManager::canSymbolicate(type))
  168. return UnknownVal();
  169. SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount);
  170. if (Loc::isLocType(type))
  171. return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
  172. return nonloc::SymbolVal(sym);
  173. }
  174. DefinedOrUnknownSVal
  175. SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
  176. const LocationContext *LCtx,
  177. unsigned VisitCount) {
  178. QualType T = E->getType();
  179. return getConjuredHeapSymbolVal(E, LCtx, T, VisitCount);
  180. }
  181. DefinedOrUnknownSVal
  182. SValBuilder::getConjuredHeapSymbolVal(const Expr *E,
  183. const LocationContext *LCtx,
  184. QualType type, unsigned VisitCount) {
  185. assert(Loc::isLocType(type));
  186. assert(SymbolManager::canSymbolicate(type));
  187. if (type->isNullPtrType())
  188. return makeZeroVal(type);
  189. SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, type, VisitCount);
  190. return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
  191. }
  192. DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag,
  193. const MemRegion *region,
  194. const Expr *expr, QualType type,
  195. const LocationContext *LCtx,
  196. unsigned count) {
  197. assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
  198. SymbolRef sym =
  199. SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag);
  200. if (Loc::isLocType(type))
  201. return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
  202. return nonloc::SymbolVal(sym);
  203. }
  204. DefinedOrUnknownSVal
  205. SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol,
  206. const TypedValueRegion *region) {
  207. QualType T = region->getValueType();
  208. if (T->isNullPtrType())
  209. return makeZeroVal(T);
  210. if (!SymbolManager::canSymbolicate(T))
  211. return UnknownVal();
  212. SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region);
  213. if (Loc::isLocType(T))
  214. return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
  215. return nonloc::SymbolVal(sym);
  216. }
  217. DefinedSVal SValBuilder::getMemberPointer(const NamedDecl *ND) {
  218. assert(!ND || (isa<CXXMethodDecl, FieldDecl, IndirectFieldDecl>(ND)));
  219. if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
  220. // Sema treats pointers to static member functions as have function pointer
  221. // type, so return a function pointer for the method.
  222. // We don't need to play a similar trick for static member fields
  223. // because these are represented as plain VarDecls and not FieldDecls
  224. // in the AST.
  225. if (MD->isStatic())
  226. return getFunctionPointer(MD);
  227. }
  228. return nonloc::PointerToMember(ND);
  229. }
  230. DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) {
  231. return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
  232. }
  233. DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block,
  234. CanQualType locTy,
  235. const LocationContext *locContext,
  236. unsigned blockCount) {
  237. const BlockCodeRegion *BC =
  238. MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext());
  239. const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext,
  240. blockCount);
  241. return loc::MemRegionVal(BD);
  242. }
  243. std::optional<loc::MemRegionVal>
  244. SValBuilder::getCastedMemRegionVal(const MemRegion *R, QualType Ty) {
  245. if (auto OptR = StateMgr.getStoreManager().castRegion(R, Ty))
  246. return loc::MemRegionVal(*OptR);
  247. return std::nullopt;
  248. }
  249. /// Return a memory region for the 'this' object reference.
  250. loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D,
  251. const StackFrameContext *SFC) {
  252. return loc::MemRegionVal(
  253. getRegionManager().getCXXThisRegion(D->getThisType(), SFC));
  254. }
  255. /// Return a memory region for the 'this' object reference.
  256. loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D,
  257. const StackFrameContext *SFC) {
  258. const Type *T = D->getTypeForDecl();
  259. QualType PT = getContext().getPointerType(QualType(T, 0));
  260. return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC));
  261. }
  262. std::optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
  263. E = E->IgnoreParens();
  264. switch (E->getStmtClass()) {
  265. // Handle expressions that we treat differently from the AST's constant
  266. // evaluator.
  267. case Stmt::AddrLabelExprClass:
  268. return makeLoc(cast<AddrLabelExpr>(E));
  269. case Stmt::CXXScalarValueInitExprClass:
  270. case Stmt::ImplicitValueInitExprClass:
  271. return makeZeroVal(E->getType());
  272. case Stmt::ObjCStringLiteralClass: {
  273. const auto *SL = cast<ObjCStringLiteral>(E);
  274. return makeLoc(getRegionManager().getObjCStringRegion(SL));
  275. }
  276. case Stmt::StringLiteralClass: {
  277. const auto *SL = cast<StringLiteral>(E);
  278. return makeLoc(getRegionManager().getStringRegion(SL));
  279. }
  280. case Stmt::PredefinedExprClass: {
  281. const auto *PE = cast<PredefinedExpr>(E);
  282. assert(PE->getFunctionName() &&
  283. "Since we analyze only instantiated functions, PredefinedExpr "
  284. "should have a function name.");
  285. return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName()));
  286. }
  287. // Fast-path some expressions to avoid the overhead of going through the AST's
  288. // constant evaluator
  289. case Stmt::CharacterLiteralClass: {
  290. const auto *C = cast<CharacterLiteral>(E);
  291. return makeIntVal(C->getValue(), C->getType());
  292. }
  293. case Stmt::CXXBoolLiteralExprClass:
  294. return makeBoolVal(cast<CXXBoolLiteralExpr>(E));
  295. case Stmt::TypeTraitExprClass: {
  296. const auto *TE = cast<TypeTraitExpr>(E);
  297. return makeTruthVal(TE->getValue(), TE->getType());
  298. }
  299. case Stmt::IntegerLiteralClass:
  300. return makeIntVal(cast<IntegerLiteral>(E));
  301. case Stmt::ObjCBoolLiteralExprClass:
  302. return makeBoolVal(cast<ObjCBoolLiteralExpr>(E));
  303. case Stmt::CXXNullPtrLiteralExprClass:
  304. return makeNullWithType(E->getType());
  305. case Stmt::CStyleCastExprClass:
  306. case Stmt::CXXFunctionalCastExprClass:
  307. case Stmt::CXXConstCastExprClass:
  308. case Stmt::CXXReinterpretCastExprClass:
  309. case Stmt::CXXStaticCastExprClass:
  310. case Stmt::ImplicitCastExprClass: {
  311. const auto *CE = cast<CastExpr>(E);
  312. switch (CE->getCastKind()) {
  313. default:
  314. break;
  315. case CK_ArrayToPointerDecay:
  316. case CK_IntegralToPointer:
  317. case CK_NoOp:
  318. case CK_BitCast: {
  319. const Expr *SE = CE->getSubExpr();
  320. std::optional<SVal> Val = getConstantVal(SE);
  321. if (!Val)
  322. return std::nullopt;
  323. return evalCast(*Val, CE->getType(), SE->getType());
  324. }
  325. }
  326. // FALLTHROUGH
  327. [[fallthrough]];
  328. }
  329. // If we don't have a special case, fall back to the AST's constant evaluator.
  330. default: {
  331. // Don't try to come up with a value for materialized temporaries.
  332. if (E->isGLValue())
  333. return std::nullopt;
  334. ASTContext &Ctx = getContext();
  335. Expr::EvalResult Result;
  336. if (E->EvaluateAsInt(Result, Ctx))
  337. return makeIntVal(Result.Val.getInt());
  338. if (Loc::isLocType(E->getType()))
  339. if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull))
  340. return makeNullWithType(E->getType());
  341. return std::nullopt;
  342. }
  343. }
  344. }
  345. SVal SValBuilder::makeSymExprValNN(BinaryOperator::Opcode Op,
  346. NonLoc LHS, NonLoc RHS,
  347. QualType ResultTy) {
  348. SymbolRef symLHS = LHS.getAsSymbol();
  349. SymbolRef symRHS = RHS.getAsSymbol();
  350. // TODO: When the Max Complexity is reached, we should conjure a symbol
  351. // instead of generating an Unknown value and propagate the taint info to it.
  352. const unsigned MaxComp = AnOpts.MaxSymbolComplexity;
  353. if (symLHS && symRHS &&
  354. (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
  355. return makeNonLoc(symLHS, Op, symRHS, ResultTy);
  356. if (symLHS && symLHS->computeComplexity() < MaxComp)
  357. if (std::optional<nonloc::ConcreteInt> rInt =
  358. RHS.getAs<nonloc::ConcreteInt>())
  359. return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
  360. if (symRHS && symRHS->computeComplexity() < MaxComp)
  361. if (std::optional<nonloc::ConcreteInt> lInt =
  362. LHS.getAs<nonloc::ConcreteInt>())
  363. return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
  364. return UnknownVal();
  365. }
  366. SVal SValBuilder::evalMinus(NonLoc X) {
  367. switch (X.getSubKind()) {
  368. case nonloc::ConcreteIntKind:
  369. return makeIntVal(-X.castAs<nonloc::ConcreteInt>().getValue());
  370. case nonloc::SymbolValKind:
  371. return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Minus,
  372. X.getType(Context));
  373. default:
  374. return UnknownVal();
  375. }
  376. }
  377. SVal SValBuilder::evalComplement(NonLoc X) {
  378. switch (X.getSubKind()) {
  379. case nonloc::ConcreteIntKind:
  380. return makeIntVal(~X.castAs<nonloc::ConcreteInt>().getValue());
  381. case nonloc::SymbolValKind:
  382. return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Not,
  383. X.getType(Context));
  384. default:
  385. return UnknownVal();
  386. }
  387. }
  388. SVal SValBuilder::evalUnaryOp(ProgramStateRef state, UnaryOperator::Opcode opc,
  389. SVal operand, QualType type) {
  390. auto OpN = operand.getAs<NonLoc>();
  391. if (!OpN)
  392. return UnknownVal();
  393. if (opc == UO_Minus)
  394. return evalMinus(*OpN);
  395. if (opc == UO_Not)
  396. return evalComplement(*OpN);
  397. llvm_unreachable("Unexpected unary operator");
  398. }
  399. SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op,
  400. SVal lhs, SVal rhs, QualType type) {
  401. if (lhs.isUndef() || rhs.isUndef())
  402. return UndefinedVal();
  403. if (lhs.isUnknown() || rhs.isUnknown())
  404. return UnknownVal();
  405. if (isa<nonloc::LazyCompoundVal>(lhs) || isa<nonloc::LazyCompoundVal>(rhs)) {
  406. return UnknownVal();
  407. }
  408. if (op == BinaryOperatorKind::BO_Cmp) {
  409. // We can't reason about C++20 spaceship operator yet.
  410. //
  411. // FIXME: Support C++20 spaceship operator.
  412. // The main problem here is that the result is not integer.
  413. return UnknownVal();
  414. }
  415. if (std::optional<Loc> LV = lhs.getAs<Loc>()) {
  416. if (std::optional<Loc> RV = rhs.getAs<Loc>())
  417. return evalBinOpLL(state, op, *LV, *RV, type);
  418. return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
  419. }
  420. if (const std::optional<Loc> RV = rhs.getAs<Loc>()) {
  421. const auto IsCommutative = [](BinaryOperatorKind Op) {
  422. return Op == BO_Mul || Op == BO_Add || Op == BO_And || Op == BO_Xor ||
  423. Op == BO_Or;
  424. };
  425. if (IsCommutative(op)) {
  426. // Swap operands.
  427. return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
  428. }
  429. // If the right operand is a concrete int location then we have nothing
  430. // better but to treat it as a simple nonloc.
  431. if (auto RV = rhs.getAs<loc::ConcreteInt>()) {
  432. const nonloc::ConcreteInt RhsAsLoc = makeIntVal(RV->getValue());
  433. return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), RhsAsLoc, type);
  434. }
  435. }
  436. return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
  437. type);
  438. }
  439. ConditionTruthVal SValBuilder::areEqual(ProgramStateRef state, SVal lhs,
  440. SVal rhs) {
  441. return state->isNonNull(evalEQ(state, lhs, rhs));
  442. }
  443. SVal SValBuilder::evalEQ(ProgramStateRef state, SVal lhs, SVal rhs) {
  444. return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType());
  445. }
  446. DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state,
  447. DefinedOrUnknownSVal lhs,
  448. DefinedOrUnknownSVal rhs) {
  449. return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs))
  450. .castAs<DefinedOrUnknownSVal>();
  451. }
  452. /// Recursively check if the pointer types are equal modulo const, volatile,
  453. /// and restrict qualifiers. Also, assume that all types are similar to 'void'.
  454. /// Assumes the input types are canonical.
  455. static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
  456. QualType FromTy) {
  457. while (Context.UnwrapSimilarTypes(ToTy, FromTy)) {
  458. Qualifiers Quals1, Quals2;
  459. ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
  460. FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
  461. // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
  462. // spaces) are identical.
  463. Quals1.removeCVRQualifiers();
  464. Quals2.removeCVRQualifiers();
  465. if (Quals1 != Quals2)
  466. return false;
  467. }
  468. // If we are casting to void, the 'From' value can be used to represent the
  469. // 'To' value.
  470. //
  471. // FIXME: Doing this after unwrapping the types doesn't make any sense. A
  472. // cast from 'int**' to 'void**' is not special in the way that a cast from
  473. // 'int*' to 'void*' is.
  474. if (ToTy->isVoidType())
  475. return true;
  476. if (ToTy != FromTy)
  477. return false;
  478. return true;
  479. }
  480. // Handles casts of type CK_IntegralCast.
  481. // At the moment, this function will redirect to evalCast, except when the range
  482. // of the original value is known to be greater than the max of the target type.
  483. SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val,
  484. QualType castTy, QualType originalTy) {
  485. // No truncations if target type is big enough.
  486. if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
  487. return evalCast(val, castTy, originalTy);
  488. SymbolRef se = val.getAsSymbol();
  489. if (!se) // Let evalCast handle non symbolic expressions.
  490. return evalCast(val, castTy, originalTy);
  491. // Find the maximum value of the target type.
  492. APSIntType ToType(getContext().getTypeSize(castTy),
  493. castTy->isUnsignedIntegerType());
  494. llvm::APSInt ToTypeMax = ToType.getMaxValue();
  495. NonLoc ToTypeMaxVal =
  496. makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue()
  497. : ToTypeMax.getSExtValue(),
  498. castTy)
  499. .castAs<NonLoc>();
  500. // Check the range of the symbol being casted against the maximum value of the
  501. // target type.
  502. NonLoc FromVal = val.castAs<NonLoc>();
  503. QualType CmpTy = getConditionType();
  504. NonLoc CompVal =
  505. evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>();
  506. ProgramStateRef IsNotTruncated, IsTruncated;
  507. std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
  508. if (!IsNotTruncated && IsTruncated) {
  509. // Symbol is truncated so we evaluate it as a cast.
  510. return makeNonLoc(se, originalTy, castTy);
  511. }
  512. return evalCast(val, castTy, originalTy);
  513. }
  514. //===----------------------------------------------------------------------===//
  515. // Cast method.
  516. // `evalCast` and its helper `EvalCastVisitor`
  517. //===----------------------------------------------------------------------===//
  518. namespace {
  519. class EvalCastVisitor : public SValVisitor<EvalCastVisitor, SVal> {
  520. private:
  521. SValBuilder &VB;
  522. ASTContext &Context;
  523. QualType CastTy, OriginalTy;
  524. public:
  525. EvalCastVisitor(SValBuilder &VB, QualType CastTy, QualType OriginalTy)
  526. : VB(VB), Context(VB.getContext()), CastTy(CastTy),
  527. OriginalTy(OriginalTy) {}
  528. SVal Visit(SVal V) {
  529. if (CastTy.isNull())
  530. return V;
  531. CastTy = Context.getCanonicalType(CastTy);
  532. const bool IsUnknownOriginalType = OriginalTy.isNull();
  533. if (!IsUnknownOriginalType) {
  534. OriginalTy = Context.getCanonicalType(OriginalTy);
  535. if (CastTy == OriginalTy)
  536. return V;
  537. // FIXME: Move this check to the most appropriate
  538. // evalCastKind/evalCastSubKind function. For const casts, casts to void,
  539. // just propagate the value.
  540. if (!CastTy->isVariableArrayType() && !OriginalTy->isVariableArrayType())
  541. if (shouldBeModeledWithNoOp(Context, Context.getPointerType(CastTy),
  542. Context.getPointerType(OriginalTy)))
  543. return V;
  544. }
  545. return SValVisitor::Visit(V);
  546. }
  547. SVal VisitUndefinedVal(UndefinedVal V) { return V; }
  548. SVal VisitUnknownVal(UnknownVal V) { return V; }
  549. SVal VisitLocConcreteInt(loc::ConcreteInt V) {
  550. // Pointer to bool.
  551. if (CastTy->isBooleanType())
  552. return VB.makeTruthVal(V.getValue().getBoolValue(), CastTy);
  553. // Pointer to integer.
  554. if (CastTy->isIntegralOrEnumerationType()) {
  555. llvm::APSInt Value = V.getValue();
  556. VB.getBasicValueFactory().getAPSIntType(CastTy).apply(Value);
  557. return VB.makeIntVal(Value);
  558. }
  559. // Pointer to any pointer.
  560. if (Loc::isLocType(CastTy)) {
  561. llvm::APSInt Value = V.getValue();
  562. VB.getBasicValueFactory().getAPSIntType(CastTy).apply(Value);
  563. return loc::ConcreteInt(VB.getBasicValueFactory().getValue(Value));
  564. }
  565. // Pointer to whatever else.
  566. return UnknownVal();
  567. }
  568. SVal VisitLocGotoLabel(loc::GotoLabel V) {
  569. // Pointer to bool.
  570. if (CastTy->isBooleanType())
  571. // Labels are always true.
  572. return VB.makeTruthVal(true, CastTy);
  573. // Pointer to integer.
  574. if (CastTy->isIntegralOrEnumerationType()) {
  575. const unsigned BitWidth = Context.getIntWidth(CastTy);
  576. return VB.makeLocAsInteger(V, BitWidth);
  577. }
  578. const bool IsUnknownOriginalType = OriginalTy.isNull();
  579. if (!IsUnknownOriginalType) {
  580. // Array to pointer.
  581. if (isa<ArrayType>(OriginalTy))
  582. if (CastTy->isPointerType() || CastTy->isReferenceType())
  583. return UnknownVal();
  584. }
  585. // Pointer to any pointer.
  586. if (Loc::isLocType(CastTy))
  587. return V;
  588. // Pointer to whatever else.
  589. return UnknownVal();
  590. }
  591. SVal VisitLocMemRegionVal(loc::MemRegionVal V) {
  592. // Pointer to bool.
  593. if (CastTy->isBooleanType()) {
  594. const MemRegion *R = V.getRegion();
  595. if (const FunctionCodeRegion *FTR = dyn_cast<FunctionCodeRegion>(R))
  596. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FTR->getDecl()))
  597. if (FD->isWeak())
  598. // FIXME: Currently we are using an extent symbol here,
  599. // because there are no generic region address metadata
  600. // symbols to use, only content metadata.
  601. return nonloc::SymbolVal(
  602. VB.getSymbolManager().getExtentSymbol(FTR));
  603. if (const SymbolicRegion *SymR = R->getSymbolicBase()) {
  604. SymbolRef Sym = SymR->getSymbol();
  605. QualType Ty = Sym->getType();
  606. // This change is needed for architectures with varying
  607. // pointer widths. See the amdgcn opencl reproducer with
  608. // this change as an example: solver-sym-simplification-ptr-bool.cl
  609. if (!Ty->isReferenceType())
  610. return VB.makeNonLoc(
  611. Sym, BO_NE, VB.getBasicValueFactory().getZeroWithTypeSize(Ty),
  612. CastTy);
  613. }
  614. // Non-symbolic memory regions are always true.
  615. return VB.makeTruthVal(true, CastTy);
  616. }
  617. const bool IsUnknownOriginalType = OriginalTy.isNull();
  618. // Try to cast to array
  619. const auto *ArrayTy =
  620. IsUnknownOriginalType
  621. ? nullptr
  622. : dyn_cast<ArrayType>(OriginalTy.getCanonicalType());
  623. // Pointer to integer.
  624. if (CastTy->isIntegralOrEnumerationType()) {
  625. SVal Val = V;
  626. // Array to integer.
  627. if (ArrayTy) {
  628. // We will always decay to a pointer.
  629. QualType ElemTy = ArrayTy->getElementType();
  630. Val = VB.getStateManager().ArrayToPointer(V, ElemTy);
  631. // FIXME: Keep these here for now in case we decide soon that we
  632. // need the original decayed type.
  633. // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
  634. // QualType pointerTy = C.getPointerType(elemTy);
  635. }
  636. const unsigned BitWidth = Context.getIntWidth(CastTy);
  637. return VB.makeLocAsInteger(Val.castAs<Loc>(), BitWidth);
  638. }
  639. // Pointer to pointer.
  640. if (Loc::isLocType(CastTy)) {
  641. if (IsUnknownOriginalType) {
  642. // When retrieving symbolic pointer and expecting a non-void pointer,
  643. // wrap them into element regions of the expected type if necessary.
  644. // It is necessary to make sure that the retrieved value makes sense,
  645. // because there's no other cast in the AST that would tell us to cast
  646. // it to the correct pointer type. We might need to do that for non-void
  647. // pointers as well.
  648. // FIXME: We really need a single good function to perform casts for us
  649. // correctly every time we need it.
  650. const MemRegion *R = V.getRegion();
  651. if (CastTy->isPointerType() && !CastTy->isVoidPointerType()) {
  652. if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
  653. QualType SRTy = SR->getSymbol()->getType();
  654. auto HasSameUnqualifiedPointeeType = [](QualType ty1,
  655. QualType ty2) {
  656. return ty1->getPointeeType().getCanonicalType().getTypePtr() ==
  657. ty2->getPointeeType().getCanonicalType().getTypePtr();
  658. };
  659. if (!HasSameUnqualifiedPointeeType(SRTy, CastTy)) {
  660. if (auto OptMemRegV = VB.getCastedMemRegionVal(SR, CastTy))
  661. return *OptMemRegV;
  662. }
  663. }
  664. }
  665. // Next fixes pointer dereference using type different from its initial
  666. // one. See PR37503 and PR49007 for details.
  667. if (const auto *ER = dyn_cast<ElementRegion>(R)) {
  668. if (auto OptMemRegV = VB.getCastedMemRegionVal(ER, CastTy))
  669. return *OptMemRegV;
  670. }
  671. return V;
  672. }
  673. if (OriginalTy->isIntegralOrEnumerationType() ||
  674. OriginalTy->isBlockPointerType() ||
  675. OriginalTy->isFunctionPointerType())
  676. return V;
  677. // Array to pointer.
  678. if (ArrayTy) {
  679. // Are we casting from an array to a pointer? If so just pass on
  680. // the decayed value.
  681. if (CastTy->isPointerType() || CastTy->isReferenceType()) {
  682. // We will always decay to a pointer.
  683. QualType ElemTy = ArrayTy->getElementType();
  684. return VB.getStateManager().ArrayToPointer(V, ElemTy);
  685. }
  686. // Are we casting from an array to an integer? If so, cast the decayed
  687. // pointer value to an integer.
  688. assert(CastTy->isIntegralOrEnumerationType());
  689. }
  690. // Other pointer to pointer.
  691. assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
  692. CastTy->isReferenceType());
  693. // We get a symbolic function pointer for a dereference of a function
  694. // pointer, but it is of function type. Example:
  695. // struct FPRec {
  696. // void (*my_func)(int * x);
  697. // };
  698. //
  699. // int bar(int x);
  700. //
  701. // int f1_a(struct FPRec* foo) {
  702. // int x;
  703. // (*foo->my_func)(&x);
  704. // return bar(x)+1; // no-warning
  705. // }
  706. // Get the result of casting a region to a different type.
  707. const MemRegion *R = V.getRegion();
  708. if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
  709. return *OptMemRegV;
  710. }
  711. // Pointer to whatever else.
  712. // FIXME: There can be gross cases where one casts the result of a
  713. // function (that returns a pointer) to some other value that happens to
  714. // fit within that pointer value. We currently have no good way to model
  715. // such operations. When this happens, the underlying operation is that
  716. // the caller is reasoning about bits. Conceptually we are layering a
  717. // "view" of a location on top of those bits. Perhaps we need to be more
  718. // lazy about mutual possible views, even on an SVal? This may be
  719. // necessary for bit-level reasoning as well.
  720. return UnknownVal();
  721. }
  722. SVal VisitNonLocCompoundVal(nonloc::CompoundVal V) {
  723. // Compound to whatever.
  724. return UnknownVal();
  725. }
  726. SVal VisitNonLocConcreteInt(nonloc::ConcreteInt V) {
  727. auto CastedValue = [V, this]() {
  728. llvm::APSInt Value = V.getValue();
  729. VB.getBasicValueFactory().getAPSIntType(CastTy).apply(Value);
  730. return Value;
  731. };
  732. // Integer to bool.
  733. if (CastTy->isBooleanType())
  734. return VB.makeTruthVal(V.getValue().getBoolValue(), CastTy);
  735. // Integer to pointer.
  736. if (CastTy->isIntegralOrEnumerationType())
  737. return VB.makeIntVal(CastedValue());
  738. // Integer to pointer.
  739. if (Loc::isLocType(CastTy))
  740. return VB.makeIntLocVal(CastedValue());
  741. // Pointer to whatever else.
  742. return UnknownVal();
  743. }
  744. SVal VisitNonLocLazyCompoundVal(nonloc::LazyCompoundVal V) {
  745. // LazyCompound to whatever.
  746. return UnknownVal();
  747. }
  748. SVal VisitNonLocLocAsInteger(nonloc::LocAsInteger V) {
  749. Loc L = V.getLoc();
  750. // Pointer as integer to bool.
  751. if (CastTy->isBooleanType())
  752. // Pass to Loc function.
  753. return Visit(L);
  754. const bool IsUnknownOriginalType = OriginalTy.isNull();
  755. // Pointer as integer to pointer.
  756. if (!IsUnknownOriginalType && Loc::isLocType(CastTy) &&
  757. OriginalTy->isIntegralOrEnumerationType()) {
  758. if (const MemRegion *R = L.getAsRegion())
  759. if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
  760. return *OptMemRegV;
  761. return L;
  762. }
  763. // Pointer as integer with region to integer/pointer.
  764. const MemRegion *R = L.getAsRegion();
  765. if (!IsUnknownOriginalType && R) {
  766. if (CastTy->isIntegralOrEnumerationType())
  767. return VisitLocMemRegionVal(loc::MemRegionVal(R));
  768. if (Loc::isLocType(CastTy)) {
  769. assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
  770. CastTy->isReferenceType());
  771. // Delegate to store manager to get the result of casting a region to a
  772. // different type. If the MemRegion* returned is NULL, this expression
  773. // Evaluates to UnknownVal.
  774. if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
  775. return *OptMemRegV;
  776. }
  777. } else {
  778. if (Loc::isLocType(CastTy)) {
  779. if (IsUnknownOriginalType)
  780. return VisitLocMemRegionVal(loc::MemRegionVal(R));
  781. return L;
  782. }
  783. SymbolRef SE = nullptr;
  784. if (R) {
  785. if (const SymbolicRegion *SR =
  786. dyn_cast<SymbolicRegion>(R->StripCasts())) {
  787. SE = SR->getSymbol();
  788. }
  789. }
  790. if (!CastTy->isFloatingType() || !SE || SE->getType()->isFloatingType()) {
  791. // FIXME: Correctly support promotions/truncations.
  792. const unsigned CastSize = Context.getIntWidth(CastTy);
  793. if (CastSize == V.getNumBits())
  794. return V;
  795. return VB.makeLocAsInteger(L, CastSize);
  796. }
  797. }
  798. // Pointer as integer to whatever else.
  799. return UnknownVal();
  800. }
  801. SVal VisitNonLocSymbolVal(nonloc::SymbolVal V) {
  802. SymbolRef SE = V.getSymbol();
  803. const bool IsUnknownOriginalType = OriginalTy.isNull();
  804. // Symbol to bool.
  805. if (!IsUnknownOriginalType && CastTy->isBooleanType()) {
  806. // Non-float to bool.
  807. if (Loc::isLocType(OriginalTy) ||
  808. OriginalTy->isIntegralOrEnumerationType() ||
  809. OriginalTy->isMemberPointerType()) {
  810. BasicValueFactory &BVF = VB.getBasicValueFactory();
  811. return VB.makeNonLoc(SE, BO_NE, BVF.getValue(0, SE->getType()), CastTy);
  812. }
  813. } else {
  814. // Symbol to integer, float.
  815. QualType T = Context.getCanonicalType(SE->getType());
  816. // Produce SymbolCast if CastTy and T are different integers.
  817. // NOTE: In the end the type of SymbolCast shall be equal to CastTy.
  818. if (T->isIntegralOrUnscopedEnumerationType() &&
  819. CastTy->isIntegralOrUnscopedEnumerationType()) {
  820. AnalyzerOptions &Opts = VB.getStateManager()
  821. .getOwningEngine()
  822. .getAnalysisManager()
  823. .getAnalyzerOptions();
  824. // If appropriate option is disabled, ignore the cast.
  825. // NOTE: ShouldSupportSymbolicIntegerCasts is `false` by default.
  826. if (!Opts.ShouldSupportSymbolicIntegerCasts)
  827. return V;
  828. return simplifySymbolCast(V, CastTy);
  829. }
  830. if (!Loc::isLocType(CastTy))
  831. if (!IsUnknownOriginalType || !CastTy->isFloatingType() ||
  832. T->isFloatingType())
  833. return VB.makeNonLoc(SE, T, CastTy);
  834. }
  835. // Symbol to pointer and whatever else.
  836. return UnknownVal();
  837. }
  838. SVal VisitNonLocPointerToMember(nonloc::PointerToMember V) {
  839. // Member pointer to whatever.
  840. return V;
  841. }
  842. /// Reduce cast expression by removing redundant intermediate casts.
  843. /// E.g.
  844. /// - (char)(short)(int x) -> (char)(int x)
  845. /// - (int)(int x) -> int x
  846. ///
  847. /// \param V -- SymbolVal, which pressumably contains SymbolCast or any symbol
  848. /// that is applicable for cast operation.
  849. /// \param CastTy -- QualType, which `V` shall be cast to.
  850. /// \return SVal with simplified cast expression.
  851. /// \note: Currently only support integral casts.
  852. nonloc::SymbolVal simplifySymbolCast(nonloc::SymbolVal V, QualType CastTy) {
  853. // We use seven conditions to recognize a simplification case.
  854. // For the clarity let `CastTy` be `C`, SE->getType() - `T`, root type -
  855. // `R`, prefix `u` for unsigned, `s` for signed, no prefix - any sign: E.g.
  856. // (char)(short)(uint x)
  857. // ( sC )( sT )( uR x)
  858. //
  859. // C === R (the same type)
  860. // (char)(char x) -> (char x)
  861. // (long)(long x) -> (long x)
  862. // Note: Comparisons operators below are for bit width.
  863. // C == T
  864. // (short)(short)(int x) -> (short)(int x)
  865. // (int)(long)(char x) -> (int)(char x) (sizeof(long) == sizeof(int))
  866. // (long)(ullong)(char x) -> (long)(char x) (sizeof(long) ==
  867. // sizeof(ullong))
  868. // C < T
  869. // (short)(int)(char x) -> (short)(char x)
  870. // (char)(int)(short x) -> (char)(short x)
  871. // (short)(int)(short x) -> (short x)
  872. // C > T > uR
  873. // (int)(short)(uchar x) -> (int)(uchar x)
  874. // (uint)(short)(uchar x) -> (uint)(uchar x)
  875. // (int)(ushort)(uchar x) -> (int)(uchar x)
  876. // C > sT > sR
  877. // (int)(short)(char x) -> (int)(char x)
  878. // (uint)(short)(char x) -> (uint)(char x)
  879. // C > sT == sR
  880. // (int)(char)(char x) -> (int)(char x)
  881. // (uint)(short)(short x) -> (uint)(short x)
  882. // C > uT == uR
  883. // (int)(uchar)(uchar x) -> (int)(uchar x)
  884. // (uint)(ushort)(ushort x) -> (uint)(ushort x)
  885. // (llong)(ulong)(uint x) -> (llong)(uint x) (sizeof(ulong) ==
  886. // sizeof(uint))
  887. SymbolRef SE = V.getSymbol();
  888. QualType T = Context.getCanonicalType(SE->getType());
  889. if (T == CastTy)
  890. return V;
  891. if (!isa<SymbolCast>(SE))
  892. return VB.makeNonLoc(SE, T, CastTy);
  893. SymbolRef RootSym = cast<SymbolCast>(SE)->getOperand();
  894. QualType RT = RootSym->getType().getCanonicalType();
  895. // FIXME support simplification from non-integers.
  896. if (!RT->isIntegralOrEnumerationType())
  897. return VB.makeNonLoc(SE, T, CastTy);
  898. BasicValueFactory &BVF = VB.getBasicValueFactory();
  899. APSIntType CTy = BVF.getAPSIntType(CastTy);
  900. APSIntType TTy = BVF.getAPSIntType(T);
  901. const auto WC = CTy.getBitWidth();
  902. const auto WT = TTy.getBitWidth();
  903. if (WC <= WT) {
  904. const bool isSameType = (RT == CastTy);
  905. if (isSameType)
  906. return nonloc::SymbolVal(RootSym);
  907. return VB.makeNonLoc(RootSym, RT, CastTy);
  908. }
  909. APSIntType RTy = BVF.getAPSIntType(RT);
  910. const auto WR = RTy.getBitWidth();
  911. const bool UT = TTy.isUnsigned();
  912. const bool UR = RTy.isUnsigned();
  913. if (((WT > WR) && (UR || !UT)) || ((WT == WR) && (UT == UR)))
  914. return VB.makeNonLoc(RootSym, RT, CastTy);
  915. return VB.makeNonLoc(SE, T, CastTy);
  916. }
  917. };
  918. } // end anonymous namespace
  919. /// Cast a given SVal to another SVal using given QualType's.
  920. /// \param V -- SVal that should be casted.
  921. /// \param CastTy -- QualType that V should be casted according to.
  922. /// \param OriginalTy -- QualType which is associated to V. It provides
  923. /// additional information about what type the cast performs from.
  924. /// \returns the most appropriate casted SVal.
  925. /// Note: Many cases don't use an exact OriginalTy. It can be extracted
  926. /// from SVal or the cast can performs unconditionaly. Always pass OriginalTy!
  927. /// It can be crucial in certain cases and generates different results.
  928. /// FIXME: If `OriginalTy.isNull()` is true, then cast performs based on CastTy
  929. /// only. This behavior is uncertain and should be improved.
  930. SVal SValBuilder::evalCast(SVal V, QualType CastTy, QualType OriginalTy) {
  931. EvalCastVisitor TRV{*this, CastTy, OriginalTy};
  932. return TRV.Visit(V);
  933. }