1 // SimpleSValBuilder.cpp - A basic SValBuilder -----------------------*- C++ -*-
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
7 //===----------------------------------------------------------------------===//
9 // This file defines SimpleSValBuilder, a basic implementation of SValBuilder.
11 //===----------------------------------------------------------------------===//
13 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
14 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
15 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/SValVisitor.h"
20 using namespace clang
;
24 class SimpleSValBuilder
: public SValBuilder
{
26 // Query the constraint manager whether the SVal has only one possible
27 // (integer) value. If that is the case, the value is returned. Otherwise,
29 // This is an implementation detail. Checkers should use `getKnownValue()`
31 static const llvm::APSInt
*getConstValue(ProgramStateRef state
, SVal V
);
33 // Helper function that returns the value stored in a nonloc::ConcreteInt or
35 static const llvm::APSInt
*getConcreteValue(SVal V
);
37 // With one `simplifySValOnce` call, a compound symbols might collapse to
38 // simpler symbol tree that is still possible to further simplify. Thus, we
39 // do the simplification on a new symbol tree until we reach the simplest
40 // form, i.e. the fixpoint.
41 // Consider the following symbol `(b * b) * b * b` which has this tree:
48 // Now, if the `b * b == 1` new constraint is added then during the first
49 // iteration we have the following transformations:
56 // We need another iteration to reach the final result `1`.
57 SVal
simplifyUntilFixpoint(ProgramStateRef State
, SVal Val
);
59 // Recursively descends into symbolic expressions and replaces symbols
60 // with their known values (in the sense of the getConstValue() method).
61 // We traverse the symbol tree and query the constraint values for the
62 // sub-trees and if a value is a constant we do the constant folding.
63 SVal
simplifySValOnce(ProgramStateRef State
, SVal V
);
66 SimpleSValBuilder(llvm::BumpPtrAllocator
&alloc
, ASTContext
&context
,
67 ProgramStateManager
&stateMgr
)
68 : SValBuilder(alloc
, context
, stateMgr
) {}
69 ~SimpleSValBuilder() override
{}
71 SVal
evalBinOpNN(ProgramStateRef state
, BinaryOperator::Opcode op
,
72 NonLoc lhs
, NonLoc rhs
, QualType resultTy
) override
;
73 SVal
evalBinOpLL(ProgramStateRef state
, BinaryOperator::Opcode op
,
74 Loc lhs
, Loc rhs
, QualType resultTy
) override
;
75 SVal
evalBinOpLN(ProgramStateRef state
, BinaryOperator::Opcode op
,
76 Loc lhs
, NonLoc rhs
, QualType resultTy
) override
;
78 /// Evaluates a given SVal by recursively evaluating and
79 /// simplifying the children SVals. If the SVal has only one possible
80 /// (integer) value, that value is returned. Otherwise, returns NULL.
81 const llvm::APSInt
*getKnownValue(ProgramStateRef state
, SVal V
) override
;
83 /// Evaluates a given SVal by recursively evaluating and simplifying the
84 /// children SVals, then returns its minimal possible (integer) value. If the
85 /// constraint manager cannot provide a meaningful answer, this returns NULL.
86 const llvm::APSInt
*getMinValue(ProgramStateRef state
, SVal V
) override
;
88 /// Evaluates a given SVal by recursively evaluating and simplifying the
89 /// children SVals, then returns its maximal possible (integer) value. If the
90 /// constraint manager cannot provide a meaningful answer, this returns NULL.
91 const llvm::APSInt
*getMaxValue(ProgramStateRef state
, SVal V
) override
;
93 SVal
simplifySVal(ProgramStateRef State
, SVal V
) override
;
95 SVal
MakeSymIntVal(const SymExpr
*LHS
, BinaryOperator::Opcode op
,
96 const llvm::APSInt
&RHS
, QualType resultTy
);
98 } // end anonymous namespace
100 SValBuilder
*ento::createSimpleSValBuilder(llvm::BumpPtrAllocator
&alloc
,
102 ProgramStateManager
&stateMgr
) {
103 return new SimpleSValBuilder(alloc
, context
, stateMgr
);
106 // Checks if the negation the value and flipping sign preserve
107 // the semantics on the operation in the resultType
108 static bool isNegationValuePreserving(const llvm::APSInt
&Value
,
109 APSIntType ResultType
) {
110 const unsigned ValueBits
= Value
.getSignificantBits();
111 if (ValueBits
== ResultType
.getBitWidth()) {
112 // The value is the lowest negative value that is representable
113 // in signed integer with bitWith of result type. The
114 // negation is representable if resultType is unsigned.
115 return ResultType
.isUnsigned();
118 // If resultType bitWith is higher that number of bits required
119 // to represent RHS, the sign flip produce same value.
120 return ValueBits
< ResultType
.getBitWidth();
123 //===----------------------------------------------------------------------===//
124 // Transfer function for binary operators.
125 //===----------------------------------------------------------------------===//
127 SVal
SimpleSValBuilder::MakeSymIntVal(const SymExpr
*LHS
,
128 BinaryOperator::Opcode op
,
129 const llvm::APSInt
&RHS
,
131 bool isIdempotent
= false;
133 // Check for a few special cases with known reductions first.
136 // We can't reduce this case; just treat it normally.
141 return makeIntVal(0, resultTy
);
148 // This is also handled elsewhere.
149 return UndefinedVal();
156 // This is also handled elsewhere.
157 return UndefinedVal();
159 return makeIntVal(0, resultTy
);
166 // a+0, a-0, a<<0, a>>0, a^0
173 return makeIntVal(0, resultTy
);
174 else if (RHS
.isAllOnes())
181 else if (RHS
.isAllOnes()) {
182 const llvm::APSInt
&Result
= BasicVals
.Convert(resultTy
, RHS
);
183 return nonloc::ConcreteInt(Result
);
188 // Idempotent ops (like a*1) can still change the type of an expression.
189 // Wrap the LHS up in a NonLoc again and let evalCast do the
192 return evalCast(nonloc::SymbolVal(LHS
), resultTy
, QualType
{});
194 // If we reach this point, the expression cannot be simplified.
195 // Make a SymbolVal for the entire expression, after converting the RHS.
196 const llvm::APSInt
*ConvertedRHS
= &RHS
;
197 if (BinaryOperator::isComparisonOp(op
)) {
198 // We're looking for a type big enough to compare the symbolic value
199 // with the given constant.
200 // FIXME: This is an approximation of Sema::UsualArithmeticConversions.
201 ASTContext
&Ctx
= getContext();
202 QualType SymbolType
= LHS
->getType();
203 uint64_t ValWidth
= RHS
.getBitWidth();
204 uint64_t TypeWidth
= Ctx
.getTypeSize(SymbolType
);
206 if (ValWidth
< TypeWidth
) {
207 // If the value is too small, extend it.
208 ConvertedRHS
= &BasicVals
.Convert(SymbolType
, RHS
);
209 } else if (ValWidth
== TypeWidth
) {
210 // If the value is signed but the symbol is unsigned, do the comparison
211 // in unsigned space. [C99 6.3.1.8]
212 // (For the opposite case, the value is already unsigned.)
213 if (RHS
.isSigned() && !SymbolType
->isSignedIntegerOrEnumerationType())
214 ConvertedRHS
= &BasicVals
.Convert(SymbolType
, RHS
);
216 } else if (BinaryOperator::isAdditiveOp(op
) && RHS
.isNegative()) {
217 // Change a+(-N) into a-N, and a-(-N) into a+N
218 // Adjust addition/subtraction of negative value, to
219 // subtraction/addition of the negated value.
220 APSIntType resultIntTy
= BasicVals
.getAPSIntType(resultTy
);
221 if (isNegationValuePreserving(RHS
, resultIntTy
)) {
222 ConvertedRHS
= &BasicVals
.getValue(-resultIntTy
.convert(RHS
));
223 op
= (op
== BO_Add
) ? BO_Sub
: BO_Add
;
225 ConvertedRHS
= &BasicVals
.Convert(resultTy
, RHS
);
228 ConvertedRHS
= &BasicVals
.Convert(resultTy
, RHS
);
230 return makeNonLoc(LHS
, op
, *ConvertedRHS
, resultTy
);
233 // See if Sym is known to be a relation Rel with Bound.
234 static bool isInRelation(BinaryOperator::Opcode Rel
, SymbolRef Sym
,
235 llvm::APSInt Bound
, ProgramStateRef State
) {
236 SValBuilder
&SVB
= State
->getStateManager().getSValBuilder();
238 SVB
.evalBinOpNN(State
, Rel
, nonloc::SymbolVal(Sym
),
239 nonloc::ConcreteInt(Bound
), SVB
.getConditionType());
240 if (auto DV
= Result
.getAs
<DefinedSVal
>()) {
241 return !State
->assume(*DV
, false);
246 // See if Sym is known to be within [min/4, max/4], where min and max
247 // are the bounds of the symbol's integral type. With such symbols,
248 // some manipulations can be performed without the risk of overflow.
249 // assume() doesn't cause infinite recursion because we should be dealing
250 // with simpler symbols on every recursive call.
251 static bool isWithinConstantOverflowBounds(SymbolRef Sym
,
252 ProgramStateRef State
) {
253 SValBuilder
&SVB
= State
->getStateManager().getSValBuilder();
254 BasicValueFactory
&BV
= SVB
.getBasicValueFactory();
256 QualType T
= Sym
->getType();
257 assert(T
->isSignedIntegerOrEnumerationType() &&
258 "This only works with signed integers!");
259 APSIntType AT
= BV
.getAPSIntType(T
);
261 llvm::APSInt Max
= AT
.getMaxValue() / AT
.getValue(4), Min
= -Max
;
262 return isInRelation(BO_LE
, Sym
, Max
, State
) &&
263 isInRelation(BO_GE
, Sym
, Min
, State
);
266 // Same for the concrete integers: see if I is within [min/4, max/4].
267 static bool isWithinConstantOverflowBounds(llvm::APSInt I
) {
269 assert(!AT
.isUnsigned() &&
270 "This only works with signed integers!");
272 llvm::APSInt Max
= AT
.getMaxValue() / AT
.getValue(4), Min
= -Max
;
273 return (I
<= Max
) && (I
>= -Max
);
276 static std::pair
<SymbolRef
, llvm::APSInt
>
277 decomposeSymbol(SymbolRef Sym
, BasicValueFactory
&BV
) {
278 if (const auto *SymInt
= dyn_cast
<SymIntExpr
>(Sym
))
279 if (BinaryOperator::isAdditiveOp(SymInt
->getOpcode()))
280 return std::make_pair(SymInt
->getLHS(),
281 (SymInt
->getOpcode() == BO_Add
) ?
283 (-SymInt
->getRHS()));
285 // Fail to decompose: "reduce" the problem to the "$x + 0" case.
286 return std::make_pair(Sym
, BV
.getValue(0, Sym
->getType()));
289 // Simplify "(LSym + LInt) Op (RSym + RInt)" assuming all values are of the
290 // same signed integral type and no overflows occur (which should be checked
292 static NonLoc
doRearrangeUnchecked(ProgramStateRef State
,
293 BinaryOperator::Opcode Op
,
294 SymbolRef LSym
, llvm::APSInt LInt
,
295 SymbolRef RSym
, llvm::APSInt RInt
) {
296 SValBuilder
&SVB
= State
->getStateManager().getSValBuilder();
297 BasicValueFactory
&BV
= SVB
.getBasicValueFactory();
298 SymbolManager
&SymMgr
= SVB
.getSymbolManager();
300 QualType SymTy
= LSym
->getType();
301 assert(SymTy
== RSym
->getType() &&
302 "Symbols are not of the same type!");
303 assert(APSIntType(LInt
) == BV
.getAPSIntType(SymTy
) &&
304 "Integers are not of the same type as symbols!");
305 assert(APSIntType(RInt
) == BV
.getAPSIntType(SymTy
) &&
306 "Integers are not of the same type as symbols!");
309 if (BinaryOperator::isComparisonOp(Op
))
310 ResultTy
= SVB
.getConditionType();
311 else if (BinaryOperator::isAdditiveOp(Op
))
314 llvm_unreachable("Operation not suitable for unchecked rearrangement!");
317 return SVB
.evalBinOpNN(State
, Op
, nonloc::ConcreteInt(LInt
),
318 nonloc::ConcreteInt(RInt
), ResultTy
)
321 SymbolRef ResultSym
= nullptr;
322 BinaryOperator::Opcode ResultOp
;
323 llvm::APSInt ResultInt
;
324 if (BinaryOperator::isComparisonOp(Op
)) {
325 // Prefer comparing to a non-negative number.
326 // FIXME: Maybe it'd be better to have consistency in
327 // "$x - $y" vs. "$y - $x" because those are solver's keys.
329 ResultSym
= SymMgr
.getSymSymExpr(RSym
, BO_Sub
, LSym
, SymTy
);
330 ResultOp
= BinaryOperator::reverseComparisonOp(Op
);
331 ResultInt
= LInt
- RInt
; // Opposite order!
333 ResultSym
= SymMgr
.getSymSymExpr(LSym
, BO_Sub
, RSym
, SymTy
);
335 ResultInt
= RInt
- LInt
; // Opposite order!
338 ResultSym
= SymMgr
.getSymSymExpr(LSym
, Op
, RSym
, SymTy
);
339 ResultInt
= (Op
== BO_Add
) ? (LInt
+ RInt
) : (LInt
- RInt
);
341 // Bring back the cosmetic difference.
343 ResultInt
= -ResultInt
;
345 } else if (ResultInt
== 0) {
346 // Shortcut: Simplify "$x + 0" to "$x".
347 return nonloc::SymbolVal(ResultSym
);
350 const llvm::APSInt
&PersistentResultInt
= BV
.getValue(ResultInt
);
351 return nonloc::SymbolVal(
352 SymMgr
.getSymIntExpr(ResultSym
, ResultOp
, PersistentResultInt
, ResultTy
));
355 // Rearrange if symbol type matches the result type and if the operator is a
356 // comparison operator, both symbol and constant must be within constant
358 static bool shouldRearrange(ProgramStateRef State
, BinaryOperator::Opcode Op
,
359 SymbolRef Sym
, llvm::APSInt Int
, QualType Ty
) {
360 return Sym
->getType() == Ty
&&
361 (!BinaryOperator::isComparisonOp(Op
) ||
362 (isWithinConstantOverflowBounds(Sym
, State
) &&
363 isWithinConstantOverflowBounds(Int
)));
366 static std::optional
<NonLoc
> tryRearrange(ProgramStateRef State
,
367 BinaryOperator::Opcode Op
, NonLoc Lhs
,
368 NonLoc Rhs
, QualType ResultTy
) {
369 ProgramStateManager
&StateMgr
= State
->getStateManager();
370 SValBuilder
&SVB
= StateMgr
.getSValBuilder();
372 // We expect everything to be of the same type - this type.
375 // FIXME: After putting complexity threshold to the symbols we can always
376 // rearrange additive operations but rearrange comparisons only if
378 if (!SVB
.getAnalyzerOptions().ShouldAggressivelySimplifyBinaryOperation
)
381 SymbolRef LSym
= Lhs
.getAsSymbol();
385 if (BinaryOperator::isComparisonOp(Op
)) {
386 SingleTy
= LSym
->getType();
387 if (ResultTy
!= SVB
.getConditionType())
389 // Initialize SingleTy later with a symbol's type.
390 } else if (BinaryOperator::isAdditiveOp(Op
)) {
392 if (LSym
->getType() != SingleTy
)
395 // Don't rearrange other operations.
399 assert(!SingleTy
.isNull() && "We should have figured out the type by now!");
401 // Rearrange signed symbolic expressions only
402 if (!SingleTy
->isSignedIntegerOrEnumerationType())
405 SymbolRef RSym
= Rhs
.getAsSymbol();
406 if (!RSym
|| RSym
->getType() != SingleTy
)
409 BasicValueFactory
&BV
= State
->getBasicVals();
410 llvm::APSInt LInt
, RInt
;
411 std::tie(LSym
, LInt
) = decomposeSymbol(LSym
, BV
);
412 std::tie(RSym
, RInt
) = decomposeSymbol(RSym
, BV
);
413 if (!shouldRearrange(State
, Op
, LSym
, LInt
, SingleTy
) ||
414 !shouldRearrange(State
, Op
, RSym
, RInt
, SingleTy
))
417 // We know that no overflows can occur anymore.
418 return doRearrangeUnchecked(State
, Op
, LSym
, LInt
, RSym
, RInt
);
421 SVal
SimpleSValBuilder::evalBinOpNN(ProgramStateRef state
,
422 BinaryOperator::Opcode op
,
423 NonLoc lhs
, NonLoc rhs
,
425 NonLoc InputLHS
= lhs
;
426 NonLoc InputRHS
= rhs
;
428 // Constraints may have changed since the creation of a bound SVal. Check if
429 // the values can be simplified based on those new constraints.
430 SVal simplifiedLhs
= simplifySVal(state
, lhs
);
431 SVal simplifiedRhs
= simplifySVal(state
, rhs
);
432 if (auto simplifiedLhsAsNonLoc
= simplifiedLhs
.getAs
<NonLoc
>())
433 lhs
= *simplifiedLhsAsNonLoc
;
434 if (auto simplifiedRhsAsNonLoc
= simplifiedRhs
.getAs
<NonLoc
>())
435 rhs
= *simplifiedRhsAsNonLoc
;
437 // Handle trivial case where left-side and right-side are the same.
445 return makeTruthVal(true, resultTy
);
449 return makeTruthVal(false, resultTy
);
452 if (resultTy
->isIntegralOrEnumerationType())
453 return makeIntVal(0, resultTy
);
454 return evalCast(makeIntVal(0, /*isUnsigned=*/false), resultTy
,
458 return evalCast(lhs
, resultTy
, QualType
{});
462 switch (lhs
.getKind()) {
464 return makeSymExprValNN(op
, lhs
, rhs
, resultTy
);
465 case nonloc::PointerToMemberKind
: {
466 assert(rhs
.getKind() == nonloc::PointerToMemberKind
&&
467 "Both SVals should have pointer-to-member-type");
468 auto LPTM
= lhs
.castAs
<nonloc::PointerToMember
>(),
469 RPTM
= rhs
.castAs
<nonloc::PointerToMember
>();
470 auto LPTMD
= LPTM
.getPTMData(), RPTMD
= RPTM
.getPTMData();
473 return makeTruthVal(LPTMD
== RPTMD
, resultTy
);
475 return makeTruthVal(LPTMD
!= RPTMD
, resultTy
);
480 case nonloc::LocAsIntegerKind
: {
481 Loc lhsL
= lhs
.castAs
<nonloc::LocAsInteger
>().getLoc();
482 switch (rhs
.getKind()) {
483 case nonloc::LocAsIntegerKind
:
484 // FIXME: at the moment the implementation
485 // of modeling "pointers as integers" is not complete.
486 if (!BinaryOperator::isComparisonOp(op
))
488 return evalBinOpLL(state
, op
, lhsL
,
489 rhs
.castAs
<nonloc::LocAsInteger
>().getLoc(),
491 case nonloc::ConcreteIntKind
: {
492 // FIXME: at the moment the implementation
493 // of modeling "pointers as integers" is not complete.
494 if (!BinaryOperator::isComparisonOp(op
))
496 // Transform the integer into a location and compare.
497 // FIXME: This only makes sense for comparisons. If we want to, say,
498 // add 1 to a LocAsInteger, we'd better unpack the Loc and add to it,
499 // then pack it back into a LocAsInteger.
500 llvm::APSInt i
= rhs
.castAs
<nonloc::ConcreteInt
>().getValue();
501 // If the region has a symbolic base, pay attention to the type; it
502 // might be coming from a non-default address space. For non-symbolic
503 // regions it doesn't matter that much because such comparisons would
504 // most likely evaluate to concrete false anyway. FIXME: We might
505 // still need to handle the non-comparison case.
506 if (SymbolRef lSym
= lhs
.getAsLocSymbol(true))
507 BasicVals
.getAPSIntType(lSym
->getType()).apply(i
);
509 BasicVals
.getAPSIntType(Context
.VoidPtrTy
).apply(i
);
510 return evalBinOpLL(state
, op
, lhsL
, makeLoc(i
), resultTy
);
515 return makeTruthVal(false, resultTy
);
517 return makeTruthVal(true, resultTy
);
519 // This case also handles pointer arithmetic.
520 return makeSymExprValNN(op
, InputLHS
, InputRHS
, resultTy
);
524 case nonloc::ConcreteIntKind
: {
525 llvm::APSInt LHSValue
= lhs
.castAs
<nonloc::ConcreteInt
>().getValue();
527 // If we're dealing with two known constants, just perform the operation.
528 if (const llvm::APSInt
*KnownRHSValue
= getConstValue(state
, rhs
)) {
529 llvm::APSInt RHSValue
= *KnownRHSValue
;
530 if (BinaryOperator::isComparisonOp(op
)) {
531 // We're looking for a type big enough to compare the two values.
532 // FIXME: This is not correct. char + short will result in a promotion
533 // to int. Unfortunately we have lost types by this point.
534 APSIntType CompareType
= std::max(APSIntType(LHSValue
),
535 APSIntType(RHSValue
));
536 CompareType
.apply(LHSValue
);
537 CompareType
.apply(RHSValue
);
538 } else if (!BinaryOperator::isShiftOp(op
)) {
539 APSIntType IntType
= BasicVals
.getAPSIntType(resultTy
);
540 IntType
.apply(LHSValue
);
541 IntType
.apply(RHSValue
);
544 const llvm::APSInt
*Result
=
545 BasicVals
.evalAPSInt(op
, LHSValue
, RHSValue
);
547 if (op
== BO_Shl
|| op
== BO_Shr
) {
548 // FIXME: At this point the constant folding claims that the result
549 // of a bitwise shift is undefined. However, constant folding
550 // relies on the inaccurate type information that is stored in the
551 // bit size of APSInt objects, and if we reached this point, then
552 // the checker core.BitwiseShift already determined that the shift
553 // is valid (in a PreStmt callback, by querying the real type from
555 // To avoid embarrassing false positives, let's just say that we
556 // don't know anything about the result of the shift.
559 return UndefinedVal();
562 return nonloc::ConcreteInt(*Result
);
565 // Swap the left and right sides and flip the operator if doing so
566 // allows us to better reason about the expression (this is a form
567 // of expression canonicalization).
568 // While we're at it, catch some special cases for non-commutative ops.
574 op
= BinaryOperator::reverseComparisonOp(op
);
587 if (LHSValue
.isAllOnes() && LHSValue
.isSigned())
588 return evalCast(lhs
, resultTy
, QualType
{});
593 return evalCast(lhs
, resultTy
, QualType
{});
594 return makeSymExprValNN(op
, InputLHS
, InputRHS
, resultTy
);
600 return makeZeroVal(resultTy
);
603 return makeSymExprValNN(op
, InputLHS
, InputRHS
, resultTy
);
606 case nonloc::SymbolValKind
: {
607 // We only handle LHS as simple symbols or SymIntExprs.
608 SymbolRef Sym
= lhs
.castAs
<nonloc::SymbolVal
>().getSymbol();
610 // LHS is a symbolic expression.
611 if (const SymIntExpr
*symIntExpr
= dyn_cast
<SymIntExpr
>(Sym
)) {
613 // Is this a logical not? (!x is represented as x == 0.)
614 if (op
== BO_EQ
&& rhs
.isZeroConstant()) {
615 // We know how to negate certain expressions. Simplify them here.
617 BinaryOperator::Opcode opc
= symIntExpr
->getOpcode();
620 // We don't know how to negate this operation.
621 // Just handle it as if it were a normal comparison to 0.
625 llvm_unreachable("Logical operators handled by branching logic.");
638 llvm_unreachable("'=' and ',' operators handled by ExprEngine.");
641 llvm_unreachable("Pointer arithmetic not handled here.");
648 assert(resultTy
->isBooleanType() ||
649 resultTy
== getConditionType());
650 assert(symIntExpr
->getType()->isBooleanType() ||
651 getContext().hasSameUnqualifiedType(symIntExpr
->getType(),
652 getConditionType()));
653 // Negate the comparison and make a value.
654 opc
= BinaryOperator::negateComparisonOp(opc
);
655 return makeNonLoc(symIntExpr
->getLHS(), opc
,
656 symIntExpr
->getRHS(), resultTy
);
660 // For now, only handle expressions whose RHS is a constant.
661 if (const llvm::APSInt
*RHSValue
= getConstValue(state
, rhs
)) {
662 // If both the LHS and the current expression are additive,
663 // fold their constants and try again.
664 if (BinaryOperator::isAdditiveOp(op
)) {
665 BinaryOperator::Opcode lop
= symIntExpr
->getOpcode();
666 if (BinaryOperator::isAdditiveOp(lop
)) {
667 // Convert the two constants to a common type, then combine them.
669 // resultTy may not be the best type to convert to, but it's
670 // probably the best choice in expressions with mixed type
671 // (such as x+1U+2LL). The rules for implicit conversions should
672 // choose a reasonable type to preserve the expression, and will
673 // at least match how the value is going to be used.
674 APSIntType IntType
= BasicVals
.getAPSIntType(resultTy
);
675 const llvm::APSInt
&first
= IntType
.convert(symIntExpr
->getRHS());
676 const llvm::APSInt
&second
= IntType
.convert(*RHSValue
);
678 // If the op and lop agrees, then we just need to
679 // sum the constants. Otherwise, we change to operation
680 // type if substraction would produce negative value
681 // (and cause overflow for unsigned integers),
682 // as consequence x+1U-10 produces x-9U, instead
683 // of x+4294967287U, that would be produced without this
685 const llvm::APSInt
*newRHS
;
687 newRHS
= BasicVals
.evalAPSInt(BO_Add
, first
, second
);
688 } else if (first
>= second
) {
689 newRHS
= BasicVals
.evalAPSInt(BO_Sub
, first
, second
);
692 newRHS
= BasicVals
.evalAPSInt(BO_Sub
, second
, first
);
695 assert(newRHS
&& "Invalid operation despite common type!");
696 rhs
= nonloc::ConcreteInt(*newRHS
);
697 lhs
= nonloc::SymbolVal(symIntExpr
->getLHS());
702 // Otherwise, make a SymIntExpr out of the expression.
703 return MakeSymIntVal(symIntExpr
, op
, *RHSValue
, resultTy
);
707 // Is the RHS a constant?
708 if (const llvm::APSInt
*RHSValue
= getConstValue(state
, rhs
))
709 return MakeSymIntVal(Sym
, op
, *RHSValue
, resultTy
);
711 if (std::optional
<NonLoc
> V
= tryRearrange(state
, op
, lhs
, rhs
, resultTy
))
714 // Give up -- this is not a symbolic expression we can handle.
715 return makeSymExprValNN(op
, InputLHS
, InputRHS
, resultTy
);
721 static SVal
evalBinOpFieldRegionFieldRegion(const FieldRegion
*LeftFR
,
722 const FieldRegion
*RightFR
,
723 BinaryOperator::Opcode op
,
725 SimpleSValBuilder
&SVB
) {
726 // Only comparisons are meaningful here!
727 if (!BinaryOperator::isComparisonOp(op
))
730 // Next, see if the two FRs have the same super-region.
731 // FIXME: This doesn't handle casts yet, and simply stripping the casts
733 if (LeftFR
->getSuperRegion() != RightFR
->getSuperRegion())
736 const FieldDecl
*LeftFD
= LeftFR
->getDecl();
737 const FieldDecl
*RightFD
= RightFR
->getDecl();
738 const RecordDecl
*RD
= LeftFD
->getParent();
740 // Make sure the two FRs are from the same kind of record. Just in case!
741 // FIXME: This is probably where inheritance would be a problem.
742 if (RD
!= RightFD
->getParent())
745 // We know for sure that the two fields are not the same, since that
746 // would have given us the same SVal.
748 return SVB
.makeTruthVal(false, resultTy
);
750 return SVB
.makeTruthVal(true, resultTy
);
752 // Iterate through the fields and see which one comes first.
753 // [C99 6.7.2.1.13] "Within a structure object, the non-bit-field
754 // members and the units in which bit-fields reside have addresses that
755 // increase in the order in which they are declared."
756 bool leftFirst
= (op
== BO_LT
|| op
== BO_LE
);
757 for (const auto *I
: RD
->fields()) {
759 return SVB
.makeTruthVal(leftFirst
, resultTy
);
761 return SVB
.makeTruthVal(!leftFirst
, resultTy
);
764 llvm_unreachable("Fields not found in parent record's definition");
767 // This is used in debug builds only for now because some downstream users
768 // may hit this assert in their subsequent merges.
769 // There are still places in the analyzer where equal bitwidth Locs
770 // are compared, and need to be found and corrected. Recent previous fixes have
771 // addressed the known problems of making NULLs with specific bitwidths
772 // for Loc comparisons along with deprecation of APIs for the same purpose.
774 static void assertEqualBitWidths(ProgramStateRef State
, Loc RhsLoc
,
776 // Implements a "best effort" check for RhsLoc and LhsLoc bit widths
777 ASTContext
&Ctx
= State
->getStateManager().getContext();
778 uint64_t RhsBitwidth
=
779 RhsLoc
.getType(Ctx
).isNull() ? 0 : Ctx
.getTypeSize(RhsLoc
.getType(Ctx
));
780 uint64_t LhsBitwidth
=
781 LhsLoc
.getType(Ctx
).isNull() ? 0 : Ctx
.getTypeSize(LhsLoc
.getType(Ctx
));
782 if (RhsBitwidth
&& LhsBitwidth
&& (LhsLoc
.getKind() == RhsLoc
.getKind())) {
783 assert(RhsBitwidth
== LhsBitwidth
&&
784 "RhsLoc and LhsLoc bitwidth must be same!");
788 // FIXME: all this logic will change if/when we have MemRegion::getLocation().
789 SVal
SimpleSValBuilder::evalBinOpLL(ProgramStateRef state
,
790 BinaryOperator::Opcode op
,
794 // Assert that bitwidth of lhs and rhs are the same.
795 // This can happen if two different address spaces are used,
796 // and the bitwidths of the address spaces are different.
797 // See LIT case clang/test/Analysis/cstring-checker-addressspace.c
798 // FIXME: See comment above in the function assertEqualBitWidths
799 assertEqualBitWidths(state
, rhs
, lhs
);
801 // Only comparisons and subtractions are valid operations on two pointers.
802 // See [C99 6.5.5 through 6.5.14] or [C++0x 5.6 through 5.15].
803 // However, if a pointer is casted to an integer, evalBinOpNN may end up
804 // calling this function with another operation (PR7527). We don't attempt to
805 // model this for now, but it could be useful, particularly when the
806 // "location" is actually an integer value that's been passed through a void*.
807 if (!(BinaryOperator::isComparisonOp(op
) || op
== BO_Sub
))
810 // Special cases for when both sides are identical.
814 llvm_unreachable("Unimplemented operation for two identical values");
816 return makeZeroVal(resultTy
);
820 return makeTruthVal(true, resultTy
);
824 return makeTruthVal(false, resultTy
);
828 switch (lhs
.getKind()) {
830 llvm_unreachable("Ordering not implemented for this Loc.");
832 case loc::GotoLabelKind
:
833 // The only thing we know about labels is that they're non-null.
834 if (rhs
.isZeroConstant()) {
839 return evalCast(lhs
, resultTy
, QualType
{});
843 return makeTruthVal(false, resultTy
);
847 return makeTruthVal(true, resultTy
);
850 // There may be two labels for the same location, and a function region may
851 // have the same address as a label at the start of the function (depending
853 // FIXME: we can probably do a comparison against other MemRegions, though.
854 // FIXME: is there a way to tell if two labels refer to the same location?
857 case loc::ConcreteIntKind
: {
858 auto L
= lhs
.castAs
<loc::ConcreteInt
>();
860 // If one of the operands is a symbol and the other is a constant,
861 // build an expression for use by the constraint manager.
862 if (SymbolRef rSym
= rhs
.getAsLocSymbol()) {
863 // We can only build expressions with symbols on the left,
864 // so we need a reversible operator.
865 if (!BinaryOperator::isComparisonOp(op
) || op
== BO_Cmp
)
868 op
= BinaryOperator::reverseComparisonOp(op
);
869 return makeNonLoc(rSym
, op
, L
.getValue(), resultTy
);
872 // If both operands are constants, just perform the operation.
873 if (std::optional
<loc::ConcreteInt
> rInt
= rhs
.getAs
<loc::ConcreteInt
>()) {
874 assert(BinaryOperator::isComparisonOp(op
) || op
== BO_Sub
);
876 if (const auto *ResultInt
=
877 BasicVals
.evalAPSInt(op
, L
.getValue(), rInt
->getValue()))
878 return evalCast(nonloc::ConcreteInt(*ResultInt
), resultTy
, QualType
{});
882 // Special case comparisons against NULL.
883 // This must come after the test if the RHS is a symbol, which is used to
884 // build constraints. The address of any non-symbolic region is guaranteed
885 // to be non-NULL, as is any label.
886 assert((isa
<loc::MemRegionVal
, loc::GotoLabel
>(rhs
)));
887 if (lhs
.isZeroConstant()) {
894 return makeTruthVal(false, resultTy
);
898 return makeTruthVal(true, resultTy
);
902 // Comparing an arbitrary integer to a region or label address is
903 // completely unknowable.
906 case loc::MemRegionValKind
: {
907 if (std::optional
<loc::ConcreteInt
> rInt
= rhs
.getAs
<loc::ConcreteInt
>()) {
908 // If one of the operands is a symbol and the other is a constant,
909 // build an expression for use by the constraint manager.
910 if (SymbolRef lSym
= lhs
.getAsLocSymbol(true)) {
911 if (BinaryOperator::isComparisonOp(op
))
912 return MakeSymIntVal(lSym
, op
, rInt
->getValue(), resultTy
);
915 // Special case comparisons to NULL.
916 // This must come after the test if the LHS is a symbol, which is used to
917 // build constraints. The address of any non-symbolic region is guaranteed
919 if (rInt
->isZeroConstant()) {
921 return evalCast(lhs
, resultTy
, QualType
{});
923 if (BinaryOperator::isComparisonOp(op
)) {
924 QualType boolType
= getContext().BoolTy
;
925 NonLoc l
= evalCast(lhs
, boolType
, QualType
{}).castAs
<NonLoc
>();
926 NonLoc r
= makeTruthVal(false, boolType
).castAs
<NonLoc
>();
927 return evalBinOpNN(state
, op
, l
, r
, resultTy
);
931 // Comparing a region to an arbitrary integer is completely unknowable.
935 // Get both values as regions, if possible.
936 const MemRegion
*LeftMR
= lhs
.getAsRegion();
937 assert(LeftMR
&& "MemRegionValKind SVal doesn't have a region!");
939 const MemRegion
*RightMR
= rhs
.getAsRegion();
941 // The RHS is probably a label, which in theory could address a region.
942 // FIXME: we can probably make a more useful statement about non-code
946 const MemRegion
*LeftBase
= LeftMR
->getBaseRegion();
947 const MemRegion
*RightBase
= RightMR
->getBaseRegion();
948 const MemSpaceRegion
*LeftMS
= LeftBase
->getMemorySpace();
949 const MemSpaceRegion
*RightMS
= RightBase
->getMemorySpace();
950 const MemSpaceRegion
*UnknownMS
= MemMgr
.getUnknownRegion();
952 // If the two regions are from different known memory spaces they cannot be
953 // equal. Also, assume that no symbolic region (whose memory space is
954 // unknown) is on the stack.
955 if (LeftMS
!= RightMS
&&
956 ((LeftMS
!= UnknownMS
&& RightMS
!= UnknownMS
) ||
957 (isa
<StackSpaceRegion
>(LeftMS
) || isa
<StackSpaceRegion
>(RightMS
)))) {
962 return makeTruthVal(false, resultTy
);
964 return makeTruthVal(true, resultTy
);
968 // If both values wrap regions, see if they're from different base regions.
969 // Note, heap base symbolic regions are assumed to not alias with
970 // each other; for example, we assume that malloc returns different address
971 // on each invocation.
972 // FIXME: ObjC object pointers always reside on the heap, but currently
973 // we treat their memory space as unknown, because symbolic pointers
974 // to ObjC objects may alias. There should be a way to construct
975 // possibly-aliasing heap-based regions. For instance, MacOSXApiChecker
976 // guesses memory space for ObjC object pointers manually instead of
978 if (LeftBase
!= RightBase
&&
979 ((!isa
<SymbolicRegion
>(LeftBase
) && !isa
<SymbolicRegion
>(RightBase
)) ||
980 (isa
<HeapSpaceRegion
>(LeftMS
) || isa
<HeapSpaceRegion
>(RightMS
))) ){
985 return makeTruthVal(false, resultTy
);
987 return makeTruthVal(true, resultTy
);
991 // Handle special cases for when both regions are element regions.
992 const ElementRegion
*RightER
= dyn_cast
<ElementRegion
>(RightMR
);
993 const ElementRegion
*LeftER
= dyn_cast
<ElementRegion
>(LeftMR
);
994 if (RightER
&& LeftER
) {
995 // Next, see if the two ERs have the same super-region and matching types.
996 // FIXME: This should do something useful even if the types don't match,
997 // though if both indexes are constant the RegionRawOffset path will
998 // give the correct answer.
999 if (LeftER
->getSuperRegion() == RightER
->getSuperRegion() &&
1000 LeftER
->getElementType() == RightER
->getElementType()) {
1001 // Get the left index and cast it to the correct type.
1002 // If the index is unknown or undefined, bail out here.
1003 SVal LeftIndexVal
= LeftER
->getIndex();
1004 std::optional
<NonLoc
> LeftIndex
= LeftIndexVal
.getAs
<NonLoc
>();
1006 return UnknownVal();
1007 LeftIndexVal
= evalCast(*LeftIndex
, ArrayIndexTy
, QualType
{});
1008 LeftIndex
= LeftIndexVal
.getAs
<NonLoc
>();
1010 return UnknownVal();
1012 // Do the same for the right index.
1013 SVal RightIndexVal
= RightER
->getIndex();
1014 std::optional
<NonLoc
> RightIndex
= RightIndexVal
.getAs
<NonLoc
>();
1016 return UnknownVal();
1017 RightIndexVal
= evalCast(*RightIndex
, ArrayIndexTy
, QualType
{});
1018 RightIndex
= RightIndexVal
.getAs
<NonLoc
>();
1020 return UnknownVal();
1022 // Actually perform the operation.
1023 // evalBinOpNN expects the two indexes to already be the right type.
1024 return evalBinOpNN(state
, op
, *LeftIndex
, *RightIndex
, resultTy
);
1028 // Special handling of the FieldRegions, even with symbolic offsets.
1029 const FieldRegion
*RightFR
= dyn_cast
<FieldRegion
>(RightMR
);
1030 const FieldRegion
*LeftFR
= dyn_cast
<FieldRegion
>(LeftMR
);
1031 if (RightFR
&& LeftFR
) {
1032 SVal R
= evalBinOpFieldRegionFieldRegion(LeftFR
, RightFR
, op
, resultTy
,
1038 // Compare the regions using the raw offsets.
1039 RegionOffset LeftOffset
= LeftMR
->getAsOffset();
1040 RegionOffset RightOffset
= RightMR
->getAsOffset();
1042 if (LeftOffset
.getRegion() != nullptr &&
1043 LeftOffset
.getRegion() == RightOffset
.getRegion() &&
1044 !LeftOffset
.hasSymbolicOffset() && !RightOffset
.hasSymbolicOffset()) {
1045 int64_t left
= LeftOffset
.getOffset();
1046 int64_t right
= RightOffset
.getOffset();
1050 return UnknownVal();
1052 return makeTruthVal(left
< right
, resultTy
);
1054 return makeTruthVal(left
> right
, resultTy
);
1056 return makeTruthVal(left
<= right
, resultTy
);
1058 return makeTruthVal(left
>= right
, resultTy
);
1060 return makeTruthVal(left
== right
, resultTy
);
1062 return makeTruthVal(left
!= right
, resultTy
);
1066 // At this point we're not going to get a good answer, but we can try
1067 // conjuring an expression instead.
1068 SymbolRef LHSSym
= lhs
.getAsLocSymbol();
1069 SymbolRef RHSSym
= rhs
.getAsLocSymbol();
1070 if (LHSSym
&& RHSSym
)
1071 return makeNonLoc(LHSSym
, op
, RHSSym
, resultTy
);
1073 // If we get here, we have no way of comparing the regions.
1074 return UnknownVal();
1079 SVal
SimpleSValBuilder::evalBinOpLN(ProgramStateRef state
,
1080 BinaryOperator::Opcode op
, Loc lhs
,
1081 NonLoc rhs
, QualType resultTy
) {
1082 if (op
>= BO_PtrMemD
&& op
<= BO_PtrMemI
) {
1083 if (auto PTMSV
= rhs
.getAs
<nonloc::PointerToMember
>()) {
1084 if (PTMSV
->isNullMemberPointer())
1085 return UndefinedVal();
1087 auto getFieldLValue
= [&](const auto *FD
) -> SVal
{
1090 for (const auto &I
: *PTMSV
)
1091 Result
= StateMgr
.getStoreManager().evalDerivedToBase(
1092 Result
, I
->getType(), I
->isVirtual());
1094 return state
->getLValue(FD
, Result
);
1097 if (const auto *FD
= PTMSV
->getDeclAs
<FieldDecl
>()) {
1098 return getFieldLValue(FD
);
1100 if (const auto *FD
= PTMSV
->getDeclAs
<IndirectFieldDecl
>()) {
1101 return getFieldLValue(FD
);
1108 assert(!BinaryOperator::isComparisonOp(op
) &&
1109 "arguments to comparison ops must be of the same type");
1111 // Special case: rhs is a zero constant.
1112 if (rhs
.isZeroConstant())
1115 // Perserve the null pointer so that it can be found by the DerefChecker.
1116 if (lhs
.isZeroConstant())
1119 // We are dealing with pointer arithmetic.
1121 // Handle pointer arithmetic on constant values.
1122 if (std::optional
<nonloc::ConcreteInt
> rhsInt
=
1123 rhs
.getAs
<nonloc::ConcreteInt
>()) {
1124 if (std::optional
<loc::ConcreteInt
> lhsInt
=
1125 lhs
.getAs
<loc::ConcreteInt
>()) {
1126 const llvm::APSInt
&leftI
= lhsInt
->getValue();
1127 assert(leftI
.isUnsigned());
1128 llvm::APSInt
rightI(rhsInt
->getValue(), /* isUnsigned */ true);
1130 // Convert the bitwidth of rightI. This should deal with overflow
1131 // since we are dealing with concrete values.
1132 rightI
= rightI
.extOrTrunc(leftI
.getBitWidth());
1134 // Offset the increment by the pointer size.
1135 llvm::APSInt
Multiplicand(rightI
.getBitWidth(), /* isUnsigned */ true);
1136 QualType pointeeType
= resultTy
->getPointeeType();
1137 Multiplicand
= getContext().getTypeSizeInChars(pointeeType
).getQuantity();
1138 rightI
*= Multiplicand
;
1140 // Compute the adjusted pointer.
1143 rightI
= leftI
+ rightI
;
1146 rightI
= leftI
- rightI
;
1149 llvm_unreachable("Invalid pointer arithmetic operation");
1151 return loc::ConcreteInt(getBasicValueFactory().getValue(rightI
));
1155 // Handle cases where 'lhs' is a region.
1156 if (const MemRegion
*region
= lhs
.getAsRegion()) {
1157 rhs
= convertToArrayIndex(rhs
).castAs
<NonLoc
>();
1158 SVal index
= UnknownVal();
1159 const SubRegion
*superR
= nullptr;
1160 // We need to know the type of the pointer in order to add an integer to it.
1161 // Depending on the type, different amount of bytes is added.
1162 QualType elementType
;
1164 if (const ElementRegion
*elemReg
= dyn_cast
<ElementRegion
>(region
)) {
1165 assert(op
== BO_Add
|| op
== BO_Sub
);
1166 index
= evalBinOpNN(state
, op
, elemReg
->getIndex(), rhs
,
1167 getArrayIndexType());
1168 superR
= cast
<SubRegion
>(elemReg
->getSuperRegion());
1169 elementType
= elemReg
->getElementType();
1171 else if (isa
<SubRegion
>(region
)) {
1172 assert(op
== BO_Add
|| op
== BO_Sub
);
1173 index
= (op
== BO_Add
) ? rhs
: evalMinus(rhs
);
1174 superR
= cast
<SubRegion
>(region
);
1175 // TODO: Is this actually reliable? Maybe improving our MemRegion
1176 // hierarchy to provide typed regions for all non-void pointers would be
1177 // better. For instance, we cannot extend this towards LocAsInteger
1178 // operations, where result type of the expression is integer.
1179 if (resultTy
->isAnyPointerType())
1180 elementType
= resultTy
->getPointeeType();
1183 // Represent arithmetic on void pointers as arithmetic on char pointers.
1184 // It is fine when a TypedValueRegion of char value type represents
1185 // a void pointer. Note that arithmetic on void pointers is a GCC extension.
1186 if (elementType
->isVoidType())
1187 elementType
= getContext().CharTy
;
1189 if (std::optional
<NonLoc
> indexV
= index
.getAs
<NonLoc
>()) {
1190 return loc::MemRegionVal(MemMgr
.getElementRegion(elementType
, *indexV
,
1191 superR
, getContext()));
1194 return UnknownVal();
1197 const llvm::APSInt
*SimpleSValBuilder::getConstValue(ProgramStateRef state
,
1199 if (const llvm::APSInt
*Res
= getConcreteValue(V
))
1202 if (SymbolRef Sym
= V
.getAsSymbol())
1203 return state
->getConstraintManager().getSymVal(state
, Sym
);
1208 const llvm::APSInt
*SimpleSValBuilder::getConcreteValue(SVal V
) {
1209 if (std::optional
<loc::ConcreteInt
> X
= V
.getAs
<loc::ConcreteInt
>())
1210 return &X
->getValue();
1212 if (std::optional
<nonloc::ConcreteInt
> X
= V
.getAs
<nonloc::ConcreteInt
>())
1213 return &X
->getValue();
1218 const llvm::APSInt
*SimpleSValBuilder::getKnownValue(ProgramStateRef state
,
1220 return getConstValue(state
, simplifySVal(state
, V
));
1223 const llvm::APSInt
*SimpleSValBuilder::getMinValue(ProgramStateRef state
,
1225 V
= simplifySVal(state
, V
);
1227 if (const llvm::APSInt
*Res
= getConcreteValue(V
))
1230 if (SymbolRef Sym
= V
.getAsSymbol())
1231 return state
->getConstraintManager().getSymMinVal(state
, Sym
);
1236 const llvm::APSInt
*SimpleSValBuilder::getMaxValue(ProgramStateRef state
,
1238 V
= simplifySVal(state
, V
);
1240 if (const llvm::APSInt
*Res
= getConcreteValue(V
))
1243 if (SymbolRef Sym
= V
.getAsSymbol())
1244 return state
->getConstraintManager().getSymMaxVal(state
, Sym
);
1249 SVal
SimpleSValBuilder::simplifyUntilFixpoint(ProgramStateRef State
, SVal Val
) {
1250 SVal SimplifiedVal
= simplifySValOnce(State
, Val
);
1251 while (SimplifiedVal
!= Val
) {
1252 Val
= SimplifiedVal
;
1253 SimplifiedVal
= simplifySValOnce(State
, Val
);
1255 return SimplifiedVal
;
1258 SVal
SimpleSValBuilder::simplifySVal(ProgramStateRef State
, SVal V
) {
1259 return simplifyUntilFixpoint(State
, V
);
1262 SVal
SimpleSValBuilder::simplifySValOnce(ProgramStateRef State
, SVal V
) {
1263 // For now, this function tries to constant-fold symbols inside a
1264 // nonloc::SymbolVal, and does nothing else. More simplifications should
1265 // be possible, such as constant-folding an index in an ElementRegion.
1267 class Simplifier
: public FullSValVisitor
<Simplifier
, SVal
> {
1268 ProgramStateRef State
;
1271 // Cache results for the lifetime of the Simplifier. Results change every
1272 // time new constraints are added to the program state, which is the whole
1273 // point of simplifying, and for that very reason it's pointless to maintain
1274 // the same cache for the duration of the whole analysis.
1275 llvm::DenseMap
<SymbolRef
, SVal
> Cached
;
1277 static bool isUnchanged(SymbolRef Sym
, SVal Val
) {
1278 return Sym
== Val
.getAsSymbol();
1281 SVal
cache(SymbolRef Sym
, SVal V
) {
1286 SVal
skip(SymbolRef Sym
) {
1287 return cache(Sym
, SVB
.makeSymbolVal(Sym
));
1290 // Return the known const value for the Sym if available, or return Undef
1292 SVal
getConst(SymbolRef Sym
) {
1293 const llvm::APSInt
*Const
=
1294 State
->getConstraintManager().getSymVal(State
, Sym
);
1296 return Loc::isLocType(Sym
->getType()) ? (SVal
)SVB
.makeIntLocVal(*Const
)
1297 : (SVal
)SVB
.makeIntVal(*Const
);
1298 return UndefinedVal();
1301 SVal
getConstOrVisit(SymbolRef Sym
) {
1302 const SVal Ret
= getConst(Sym
);
1309 Simplifier(ProgramStateRef State
)
1310 : State(State
), SVB(State
->getStateManager().getSValBuilder()) {}
1312 SVal
VisitSymbolData(const SymbolData
*S
) {
1314 if (const llvm::APSInt
*I
=
1315 State
->getConstraintManager().getSymVal(State
, S
))
1316 return Loc::isLocType(S
->getType()) ? (SVal
)SVB
.makeIntLocVal(*I
)
1317 : (SVal
)SVB
.makeIntVal(*I
);
1318 return SVB
.makeSymbolVal(S
);
1321 SVal
VisitSymIntExpr(const SymIntExpr
*S
) {
1322 auto I
= Cached
.find(S
);
1323 if (I
!= Cached
.end())
1326 SVal LHS
= getConstOrVisit(S
->getLHS());
1327 if (isUnchanged(S
->getLHS(), LHS
))
1331 // By looking at the APSInt in the right-hand side of S, we cannot
1332 // figure out if it should be treated as a Loc or as a NonLoc.
1333 // So make our guess by recalling that we cannot multiply pointers
1334 // or compare a pointer to an integer.
1335 if (Loc::isLocType(S
->getLHS()->getType()) &&
1336 BinaryOperator::isComparisonOp(S
->getOpcode())) {
1337 // The usual conversion of $sym to &SymRegion{$sym}, as they have
1338 // the same meaning for Loc-type symbols, but the latter form
1339 // is preferred in SVal computations for being Loc itself.
1340 if (SymbolRef Sym
= LHS
.getAsSymbol()) {
1341 assert(Loc::isLocType(Sym
->getType()));
1342 LHS
= SVB
.makeLoc(Sym
);
1344 RHS
= SVB
.makeIntLocVal(S
->getRHS());
1346 RHS
= SVB
.makeIntVal(S
->getRHS());
1350 S
, SVB
.evalBinOp(State
, S
->getOpcode(), LHS
, RHS
, S
->getType()));
1353 SVal
VisitIntSymExpr(const IntSymExpr
*S
) {
1354 auto I
= Cached
.find(S
);
1355 if (I
!= Cached
.end())
1358 SVal RHS
= getConstOrVisit(S
->getRHS());
1359 if (isUnchanged(S
->getRHS(), RHS
))
1362 SVal LHS
= SVB
.makeIntVal(S
->getLHS());
1364 S
, SVB
.evalBinOp(State
, S
->getOpcode(), LHS
, RHS
, S
->getType()));
1367 SVal
VisitSymSymExpr(const SymSymExpr
*S
) {
1368 auto I
= Cached
.find(S
);
1369 if (I
!= Cached
.end())
1372 // For now don't try to simplify mixed Loc/NonLoc expressions
1373 // because they often appear from LocAsInteger operations
1374 // and we don't know how to combine a LocAsInteger
1375 // with a concrete value.
1376 if (Loc::isLocType(S
->getLHS()->getType()) !=
1377 Loc::isLocType(S
->getRHS()->getType()))
1380 SVal LHS
= getConstOrVisit(S
->getLHS());
1381 SVal RHS
= getConstOrVisit(S
->getRHS());
1383 if (isUnchanged(S
->getLHS(), LHS
) && isUnchanged(S
->getRHS(), RHS
))
1387 S
, SVB
.evalBinOp(State
, S
->getOpcode(), LHS
, RHS
, S
->getType()));
1390 SVal
VisitSymbolCast(const SymbolCast
*S
) {
1391 auto I
= Cached
.find(S
);
1392 if (I
!= Cached
.end())
1394 const SymExpr
*OpSym
= S
->getOperand();
1395 SVal OpVal
= getConstOrVisit(OpSym
);
1396 if (isUnchanged(OpSym
, OpVal
))
1399 return cache(S
, SVB
.evalCast(OpVal
, S
->getType(), OpSym
->getType()));
1402 SVal
VisitUnarySymExpr(const UnarySymExpr
*S
) {
1403 auto I
= Cached
.find(S
);
1404 if (I
!= Cached
.end())
1406 SVal Op
= getConstOrVisit(S
->getOperand());
1407 if (isUnchanged(S
->getOperand(), Op
))
1411 S
, SVB
.evalUnaryOp(State
, S
->getOpcode(), Op
, S
->getType()));
1414 SVal
VisitSymExpr(SymbolRef S
) { return nonloc::SymbolVal(S
); }
1416 SVal
VisitMemRegion(const MemRegion
*R
) { return loc::MemRegionVal(R
); }
1418 SVal
VisitSymbolVal(nonloc::SymbolVal V
) {
1419 // Simplification is much more costly than computing complexity.
1420 // For high complexity, it may be not worth it.
1421 return Visit(V
.getSymbol());
1424 SVal
VisitSVal(SVal V
) { return V
; }
1427 SVal SimplifiedV
= Simplifier(State
).Visit(V
);