SValBuilder.cpp 38 KB

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