clang 24.0.0git
SValBuilder.cpp
Go to the documentation of this file.
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
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclCXX.h"
18#include "clang/AST/ExprCXX.h"
19#include "clang/AST/ExprObjC.h"
20#include "clang/AST/Stmt.h"
21#include "clang/AST/Type.h"
23#include "clang/Basic/LLVM.h"
37#include "llvm/ADT/APSInt.h"
38#include "llvm/Support/Compiler.h"
39#include <cassert>
40#include <optional>
41#include <tuple>
42
43using namespace clang;
44using namespace ento;
45
46//===----------------------------------------------------------------------===//
47// Basic SVal creation.
48//===----------------------------------------------------------------------===//
49
50void SValBuilder::anchor() {}
51
52SValBuilder::SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
53 ProgramStateManager &stateMgr)
54 : Context(context), BasicVals(context, alloc),
55 SymMgr(context, BasicVals, alloc), MemMgr(context, alloc),
56 StateMgr(stateMgr),
57 AnOpts(
58 stateMgr.getOwningEngine().getAnalysisManager().getAnalyzerOptions()),
59 ArrayIndexTy(context.LongLongTy),
60 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {}
61
64 return makeNullWithType(type);
65
66 if (type->isIntegralOrEnumerationType())
67 return makeIntVal(0, type);
68
69 if (type->isArrayType() || type->isRecordType() || type->isVectorType() ||
70 type->isAnyComplexType())
71 return makeCompoundVal(type, BasicVals.getEmptySValList());
72
73 // FIXME: Handle floats.
74 return UnknownVal();
75}
76
79 APSIntPtr rhs, QualType type) {
80 assert(lhs);
81 assert(!Loc::isLocType(type));
82 return nonloc::SymbolVal(SymMgr.acquire<SymIntExpr>(lhs, op, rhs, type));
83}
84
87 const SymExpr *rhs, QualType type) {
88 assert(rhs);
89 assert(!Loc::isLocType(type));
90 return nonloc::SymbolVal(SymMgr.acquire<IntSymExpr>(lhs, op, rhs, type));
91}
92
95 const SymExpr *rhs, QualType type) {
96 assert(lhs && rhs);
97 assert(!Loc::isLocType(type));
98 return nonloc::SymbolVal(SymMgr.acquire<SymSymExpr>(lhs, op, rhs, type));
99}
100
102 QualType type) {
103 assert(operand);
104 assert(!Loc::isLocType(type));
105 return nonloc::SymbolVal(SymMgr.acquire<UnarySymExpr>(operand, op, type));
106}
107
109 QualType fromTy, QualType toTy) {
110 assert(operand);
111 assert(!Loc::isLocType(toTy));
112 if (fromTy == toTy)
113 return nonloc::SymbolVal(operand);
114 return nonloc::SymbolVal(SymMgr.acquire<SymbolCast>(operand, fromTy, toTy));
115}
116
118 if (val.isUnknownOrUndef())
119 return val;
120
121 // Common case: we have an appropriately sized integer.
122 if (std::optional<nonloc::ConcreteInt> CI =
123 val.getAs<nonloc::ConcreteInt>()) {
124 const llvm::APSInt& I = CI->getValue();
125 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned())
126 return val;
127 }
128
129 return evalCast(val, ArrayIndexTy, QualType{});
130}
131
135
138 QualType T = region->getValueType();
139
140 if (T->isNullPtrType())
141 return makeZeroVal(T);
142
144 return UnknownVal();
145
146 SymbolRef sym = SymMgr.acquire<SymbolRegionValue>(region);
147
148 if (Loc::isLocType(T))
149 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
150
151 return nonloc::SymbolVal(sym);
152}
153
156 const StackFrame *SF,
157 unsigned Count) {
158 const Expr *Ex = dyn_cast<Expr>(elem->getAs<CFGStmt>()->getStmt());
159 assert(Ex && "elem must be a CFGStmt containing an Expr");
160 QualType T = Ex->getType();
161
162 if (T->isNullPtrType())
163 return makeZeroVal(T);
164
165 // Compute the type of the result. If the expression is not an R-value, the
166 // result should be a location.
167 QualType ExType = Ex->getType();
168 if (Ex->isGLValue())
170
171 return conjureSymbolVal(SymbolTag, elem, SF, T, Count);
172}
173
176 const StackFrame *SF,
178 unsigned count) {
179 if (type->isNullPtrType())
180 return makeZeroVal(type);
181
183 return UnknownVal();
184
185 SymbolRef sym = SymMgr.conjureSymbol(elem, SF, type, count, symbolTag);
186
187 if (Loc::isLocType(type))
188 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
189
190 return nonloc::SymbolVal(sym);
191}
192
194 const StackFrame *SF,
196 unsigned visitCount) {
197 return conjureSymbolVal(/*symbolTag=*/nullptr, elem, SF, type, visitCount);
198}
199
201 unsigned visitCount,
202 const void *symbolTag) {
203 return conjureSymbolVal(symbolTag, call.getCFGElementRef(),
204 call.getStackFrame(), call.getResultType(),
205 visitCount);
206}
207
210 unsigned visitCount,
211 const void *symbolTag) {
212 return conjureSymbolVal(symbolTag, call.getCFGElementRef(),
213 call.getStackFrame(), type, visitCount);
214}
215
217 const StackFrame *SF,
219 unsigned VisitCount) {
220 assert(Loc::isLocType(type));
222 if (type->isNullPtrType()) {
223 // makeZeroVal() returns UnknownVal only in case of FP number, which
224 // is not the case.
226 }
227
228 SymbolRef sym = SymMgr.conjureSymbol(elem, SF, type, VisitCount);
229 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym));
230}
231
233 const StackFrame *SF,
234 unsigned VisitCount) {
235 const AllocaRegion *R = getRegionManager().getAllocaRegion(E, VisitCount, SF);
236 return loc::MemRegionVal(R);
237}
238
240 const MemRegion *region,
241 const Expr *expr, QualType type,
242 const StackFrame *SF,
243 unsigned count) {
244 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type");
245
246 SymbolRef sym =
247 SymMgr.acquire<SymbolMetadata>(region, expr, type, SF, count, symbolTag);
248
249 if (Loc::isLocType(type))
250 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
251
252 return nonloc::SymbolVal(sym);
253}
254
257 const TypedValueRegion *region) {
258 QualType T = region->getValueType();
259
260 if (T->isNullPtrType())
261 return makeZeroVal(T);
262
264 return UnknownVal();
265
266 SymbolRef sym = SymMgr.acquire<SymbolDerived>(parentSymbol, region);
267
268 if (Loc::isLocType(T))
269 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
270
271 return nonloc::SymbolVal(sym);
272}
273
276
277 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) {
278 // Sema treats pointers to static member functions as have function pointer
279 // type, so return a function pointer for the method.
280 // We don't need to play a similar trick for static member fields
281 // because these are represented as plain VarDecls and not FieldDecls
282 // in the AST.
283 if (!MD->isImplicitObjectMemberFunction())
284 return getFunctionPointer(MD);
285 }
286
287 return nonloc::PointerToMember(ND);
288}
289
291 return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func));
292}
293
295 CanQualType locTy,
296 const StackFrame *SF,
297 unsigned blockCount) {
298 const BlockCodeRegion *BC =
299 MemMgr.getBlockCodeRegion(block, locTy, SF->getAnalysisDeclContext());
300 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, SF, blockCount);
301 return loc::MemRegionVal(BD);
302}
303
304std::optional<loc::MemRegionVal>
306 if (auto OptR = StateMgr.getStoreManager().castRegion(R, Ty))
307 return loc::MemRegionVal(*OptR);
308 return std::nullopt;
309}
310
311/// Return a memory region for the 'this' object reference.
313 const StackFrame *SF) {
314 return loc::MemRegionVal(
315 getRegionManager().getCXXThisRegion(D->getThisType(), SF));
316}
317
318/// Return a memory region for the 'this' object reference.
320 const StackFrame *SF) {
321 CanQualType PT =
322 getContext().getPointerType(getContext().getCanonicalTagType(D));
323 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SF));
324}
325
326std::optional<SVal> SValBuilder::getConstantVal(const Expr *E) {
327 E = E->IgnoreParens();
328
329 // A function used as a constant initializer can either decay to a function
330 // pointer or bind directly to a function reference.
331 if (E->getType()->isFunctionPointerType() || E->getType()->isFunctionType()) {
332 if (const auto *FD =
333 dyn_cast_or_null<FunctionDecl>(E->getReferencedDeclOfCallee()))
334 return getFunctionPointer(FD);
335 }
336
337 switch (E->getStmtClass()) {
338 // Handle expressions that we treat differently from the AST's constant
339 // evaluator.
340 case Stmt::AddrLabelExprClass:
341 return makeLoc(cast<AddrLabelExpr>(E));
342
343 case Stmt::CXXScalarValueInitExprClass:
344 case Stmt::ImplicitValueInitExprClass:
345 return makeZeroVal(E->getType());
346
347 case Stmt::ObjCStringLiteralClass: {
348 const auto *SL = cast<ObjCStringLiteral>(E);
349 return makeLoc(getRegionManager().getObjCStringRegion(SL));
350 }
351
352 case Stmt::StringLiteralClass: {
353 const auto *SL = cast<StringLiteral>(E);
354 return makeLoc(getRegionManager().getStringRegion(SL));
355 }
356
357 case Stmt::PredefinedExprClass: {
358 const auto *PE = cast<PredefinedExpr>(E);
359 assert(PE->getFunctionName() &&
360 "Since we analyze only instantiated functions, PredefinedExpr "
361 "should have a function name.");
362 return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName()));
363 }
364
365 // Fast-path some expressions to avoid the overhead of going through the AST's
366 // constant evaluator
367 case Stmt::CharacterLiteralClass: {
368 const auto *C = cast<CharacterLiteral>(E);
369 return makeIntVal(C->getValue(), C->getType());
370 }
371
372 case Stmt::CXXBoolLiteralExprClass:
374
375 case Stmt::TypeTraitExprClass: {
376 const auto *TE = cast<TypeTraitExpr>(E);
377 if (TE->isStoredAsBoolean())
378 return makeTruthVal(TE->getBoolValue(), TE->getType());
379 if (TE->isStoredAsComparisonResult())
380 return UnknownVal();
381 assert(TE->getAPValue().isInt() && "APValue type not supported");
382 return makeIntVal(TE->getAPValue().getInt());
383 }
384
385 case Stmt::IntegerLiteralClass:
387
388 case Stmt::ObjCBoolLiteralExprClass:
390
391 case Stmt::CXXNullPtrLiteralExprClass:
392 return makeNullWithType(E->getType());
393
394 case Stmt::CStyleCastExprClass:
395 case Stmt::CXXFunctionalCastExprClass:
396 case Stmt::CXXConstCastExprClass:
397 case Stmt::CXXReinterpretCastExprClass:
398 case Stmt::CXXStaticCastExprClass:
399 case Stmt::ImplicitCastExprClass: {
400 const auto *CE = cast<CastExpr>(E);
401 switch (CE->getCastKind()) {
402 default:
403 break;
404 case CK_ArrayToPointerDecay:
405 case CK_IntegralToPointer:
406 case CK_NoOp:
407 case CK_BitCast: {
408 const Expr *SE = CE->getSubExpr();
409 std::optional<SVal> Val = getConstantVal(SE);
410 if (!Val)
411 return std::nullopt;
412 return evalCast(*Val, CE->getType(), SE->getType());
413 }
414 }
415 [[fallthrough]];
416 }
417
418 // If we don't have a special case, fall back to the AST's constant evaluator.
419 default: {
420 // Don't try to come up with a value for materialized temporaries.
421 if (E->isGLValue())
422 return std::nullopt;
423
424 ASTContext &Ctx = getContext();
426 if (E->EvaluateAsInt(Result, Ctx))
427 return makeIntVal(Result.Val.getInt());
428
429 if (Loc::isLocType(E->getType()))
431 return makeNullWithType(E->getType());
432
433 return std::nullopt;
434 }
435 }
436}
437
439 NonLoc LHS, NonLoc RHS,
440 QualType ResultTy) {
441 SymbolRef symLHS = LHS.getAsSymbol();
442 SymbolRef symRHS = RHS.getAsSymbol();
443
444 // TODO: When the Max Complexity is reached, we should conjure a symbol
445 // instead of generating an Unknown value and propagate the taint info to it.
446 const unsigned MaxComp = AnOpts.MaxSymbolComplexity;
447
448 if (symLHS && symRHS &&
449 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
450 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
451
452 if (symLHS && symLHS->computeComplexity() < MaxComp)
453 if (std::optional<nonloc::ConcreteInt> rInt =
455 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
456
457 if (symRHS && symRHS->computeComplexity() < MaxComp)
458 if (std::optional<nonloc::ConcreteInt> lInt =
460 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
461
462 return UnknownVal();
463}
464
466 switch (X.getKind()) {
467 case nonloc::ConcreteIntKind:
468 return makeIntVal(-X.castAs<nonloc::ConcreteInt>().getValue());
469 case nonloc::SymbolValKind:
470 return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Minus,
471 X.getType(Context));
472 default:
473 return UnknownVal();
474 }
475}
476
478 switch (X.getKind()) {
479 case nonloc::ConcreteIntKind:
480 return makeIntVal(~X.castAs<nonloc::ConcreteInt>().getValue());
481 case nonloc::SymbolValKind:
482 return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Not,
483 X.getType(Context));
484 default:
485 return UnknownVal();
486 }
487}
488
490 SVal operand, QualType type) {
491 auto OpN = operand.getAs<NonLoc>();
492 if (!OpN)
493 return UnknownVal();
494
495 if (opc == UO_Minus)
496 return evalMinus(*OpN);
497 if (opc == UO_Not)
498 return evalComplement(*OpN);
499 llvm_unreachable("Unexpected unary operator");
500}
501
503 SVal lhs, SVal rhs, QualType type) {
504 if (lhs.isUndef() || rhs.isUndef())
505 return UndefinedVal();
506
507 if (lhs.isUnknown() || rhs.isUnknown())
508 return UnknownVal();
509
511 return UnknownVal();
512 }
513
514 if (op == BinaryOperatorKind::BO_Cmp) {
515 // We can't reason about C++20 spaceship operator yet.
516 //
517 // FIXME: Support C++20 spaceship operator.
518 // The main problem here is that the result is not integer.
519 return UnknownVal();
520 }
521
522 if (std::optional<Loc> LV = lhs.getAs<Loc>()) {
523 if (std::optional<Loc> RV = rhs.getAs<Loc>())
524 return evalBinOpLL(state, op, *LV, *RV, type);
525
526 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
527 }
528
529 if (const std::optional<Loc> RV = rhs.getAs<Loc>()) {
530 const auto IsCommutative = [](BinaryOperatorKind Op) {
531 return Op == BO_Mul || Op == BO_Add || Op == BO_And || Op == BO_Xor ||
532 Op == BO_Or;
533 };
534
535 if (IsCommutative(op)) {
536 // Swap operands.
537 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
538 }
539
540 // If the right operand is a concrete int location then we have nothing
541 // better but to treat it as a simple nonloc.
542 if (auto RV = rhs.getAs<loc::ConcreteInt>()) {
543 const nonloc::ConcreteInt RhsAsLoc = makeIntVal(RV->getValue());
544 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), RhsAsLoc, type);
545 }
546 }
547
548 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
549 type);
550}
551
553 SVal rhs) {
554 return state->isNonNull(evalEQ(state, lhs, rhs));
555}
556
558 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType());
559}
560
564 return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs))
566}
567
568/// Recursively check if the pointer types are equal modulo const, volatile,
569/// and restrict qualifiers. Also, assume that all types are similar to 'void'.
570/// Assumes the input types are canonical.
571static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
572 QualType FromTy) {
573 while (Context.UnwrapSimilarTypes(ToTy, FromTy)) {
574 Qualifiers Quals1, Quals2;
575 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
576 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
577
578 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
579 // spaces) are identical.
580 Quals1.removeCVRQualifiers();
581 Quals2.removeCVRQualifiers();
582 if (Quals1 != Quals2)
583 return false;
584 }
585
586 // If we are casting to void, the 'From' value can be used to represent the
587 // 'To' value.
588 //
589 // FIXME: Doing this after unwrapping the types doesn't make any sense. A
590 // cast from 'int**' to 'void**' is not special in the way that a cast from
591 // 'int*' to 'void*' is.
592 if (ToTy->isVoidType())
593 return true;
594
595 if (ToTy != FromTy)
596 return false;
597
598 return true;
599}
600
601// Handles casts of type CK_IntegralCast.
602// At the moment, this function will redirect to evalCast, except when the range
603// of the original value is known to be greater than the max of the target type.
605 QualType castTy, QualType originalTy) {
606 // No truncations if target type is big enough.
607 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
608 return evalCast(val, castTy, originalTy);
609
610 auto AsNonLoc = val.getAs<NonLoc>();
611 SymbolRef AsSymbol = val.getAsSymbol();
612 if (!AsSymbol || !AsNonLoc) // Let evalCast handle non symbolic expressions.
613 return evalCast(val, castTy, originalTy);
614
615 // Find the maximum value of the target type.
616 APSIntType ToType(getContext().getTypeSize(castTy),
617 castTy->isUnsignedIntegerType());
618 llvm::APSInt ToTypeMax = ToType.getMaxValue();
619
620 NonLoc ToTypeMaxVal = makeIntVal(ToTypeMax);
621
622 // Check the range of the symbol being casted against the maximum value of the
623 // target type.
624 QualType CmpTy = getConditionType();
625 NonLoc CompVal = evalBinOpNN(state, BO_LE, *AsNonLoc, ToTypeMaxVal, CmpTy)
626 .castAs<NonLoc>();
627 ProgramStateRef IsNotTruncated, IsTruncated;
628 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
629 if (!IsNotTruncated && IsTruncated) {
630 // Symbol is truncated so we evaluate it as a cast.
631 return makeNonLoc(AsSymbol, originalTy, castTy);
632 }
633 return evalCast(val, castTy, originalTy);
634}
635
636//===----------------------------------------------------------------------===//
637// Cast method.
638// `evalCast` and its helper `EvalCastVisitor`
639//===----------------------------------------------------------------------===//
640
641namespace {
642class EvalCastVisitor : public SValVisitor<EvalCastVisitor, SVal> {
643private:
644 SValBuilder &VB;
645 ASTContext &Context;
646 QualType CastTy, OriginalTy;
647
648public:
649 EvalCastVisitor(SValBuilder &VB, QualType CastTy, QualType OriginalTy)
650 : VB(VB), Context(VB.getContext()), CastTy(CastTy),
651 OriginalTy(OriginalTy) {}
652
653 SVal Visit(SVal V) {
654 if (CastTy.isNull())
655 return V;
656
657 CastTy = Context.getCanonicalType(CastTy);
658
659 const bool IsUnknownOriginalType = OriginalTy.isNull();
660 if (!IsUnknownOriginalType) {
661 OriginalTy = Context.getCanonicalType(OriginalTy);
662
663 if (CastTy == OriginalTy)
664 return V;
665
666 // FIXME: Move this check to the most appropriate
667 // evalCastKind/evalCastSubKind function. For const casts, casts to void,
668 // just propagate the value.
669 if (!CastTy->isVariableArrayType() && !OriginalTy->isVariableArrayType())
670 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(CastTy),
671 Context.getPointerType(OriginalTy)))
672 return V;
673 }
674 return SValVisitor::Visit(V);
675 }
676 SVal VisitUndefinedVal(UndefinedVal V) { return V; }
677 SVal VisitUnknownVal(UnknownVal V) { return V; }
678 SVal VisitConcreteInt(loc::ConcreteInt V) {
679 // Pointer to bool.
680 if (CastTy->isBooleanType())
681 return VB.makeTruthVal(V.getValue()->getBoolValue(), CastTy);
682
683 // Pointer to integer.
684 if (CastTy->isIntegralOrEnumerationType()) {
685 llvm::APSInt Value = V.getValue();
687 return VB.makeIntVal(Value);
688 }
689
690 // Pointer to any pointer.
691 if (Loc::isLocType(CastTy)) {
692 llvm::APSInt Value = V.getValue();
694 return loc::ConcreteInt(VB.getBasicValueFactory().getValue(Value));
695 }
696
697 // Pointer to whatever else.
698 return UnknownVal();
699 }
700 SVal VisitGotoLabel(loc::GotoLabel V) {
701 // Pointer to bool.
702 if (CastTy->isBooleanType())
703 // Labels are always true.
704 return VB.makeTruthVal(true, CastTy);
705
706 // Pointer to integer.
707 if (CastTy->isIntegralOrEnumerationType()) {
708 const unsigned BitWidth = Context.getIntWidth(CastTy);
709 return VB.makeLocAsInteger(V, BitWidth);
710 }
711
712 const bool IsUnknownOriginalType = OriginalTy.isNull();
713 if (!IsUnknownOriginalType) {
714 // Array to pointer.
715 if (isa<ArrayType>(OriginalTy))
716 if (CastTy->isPointerType() || CastTy->isReferenceType())
717 return UnknownVal();
718 }
719
720 // Pointer to any pointer.
721 if (Loc::isLocType(CastTy))
722 return V;
723
724 // Pointer to whatever else.
725 return UnknownVal();
726 }
727 SVal VisitMemRegionVal(loc::MemRegionVal V) {
728 // Pointer to bool.
729 if (CastTy->isBooleanType()) {
730 const MemRegion *R = V.getRegion();
731 if (const FunctionCodeRegion *FTR = dyn_cast<FunctionCodeRegion>(R))
732 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FTR->getDecl()))
733 if (FD->isWeak())
734 // FIXME: Currently we are using an extent symbol here,
735 // because there are no generic region address metadata
736 // symbols to use, only content metadata.
737 return nonloc::SymbolVal(
738 VB.getSymbolManager().acquire<SymbolExtent>(FTR));
739
740 if (const SymbolicRegion *SymR = R->getSymbolicBase()) {
741 SymbolRef Sym = SymR->getSymbol();
742 QualType Ty = Sym->getType();
743 // This change is needed for architectures with varying
744 // pointer widths. See the amdgcn opencl reproducer with
745 // this change as an example: solver-sym-simplification-ptr-bool.cl
746 if (!Ty->isReferenceType())
747 return VB.makeNonLoc(
748 Sym, BO_NE, VB.getBasicValueFactory().getZeroWithTypeSize(Ty),
749 CastTy);
750 }
751 // Non-symbolic memory regions are always true.
752 return VB.makeTruthVal(true, CastTy);
753 }
754
755 const bool IsUnknownOriginalType = OriginalTy.isNull();
756 // Try to cast to array
757 const auto *ArrayTy =
758 IsUnknownOriginalType
759 ? nullptr
760 : dyn_cast<ArrayType>(OriginalTy.getCanonicalType());
761
762 // Pointer to integer.
763 if (CastTy->isIntegralOrEnumerationType()) {
764 SVal Val = V;
765 // Array to integer.
766 if (ArrayTy) {
767 // We will always decay to a pointer.
768 QualType ElemTy = ArrayTy->getElementType();
769 Val = VB.getStateManager().ArrayToPointer(V, ElemTy);
770 // FIXME: Keep these here for now in case we decide soon that we
771 // need the original decayed type.
772 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
773 // QualType pointerTy = C.getPointerType(elemTy);
774 }
775 const unsigned BitWidth = Context.getIntWidth(CastTy);
776 return VB.makeLocAsInteger(Val.castAs<Loc>(), BitWidth);
777 }
778
779 // Pointer to pointer.
780 if (Loc::isLocType(CastTy)) {
781
782 if (IsUnknownOriginalType) {
783 // When retrieving symbolic pointer and expecting a non-void pointer,
784 // wrap them into element regions of the expected type if necessary.
785 // It is necessary to make sure that the retrieved value makes sense,
786 // because there's no other cast in the AST that would tell us to cast
787 // it to the correct pointer type. We might need to do that for non-void
788 // pointers as well.
789 // FIXME: We really need a single good function to perform casts for us
790 // correctly every time we need it.
791 const MemRegion *R = V.getRegion();
792 if (CastTy->isPointerType() && !CastTy->isVoidPointerType()) {
793 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
794 QualType SRTy = SR->getSymbol()->getType();
795
796 auto HasSameUnqualifiedPointeeType = [](QualType ty1,
797 QualType ty2) {
798 return ty1->getPointeeType().getCanonicalType().getTypePtr() ==
800 };
801 if (!HasSameUnqualifiedPointeeType(SRTy, CastTy)) {
802 if (auto OptMemRegV = VB.getCastedMemRegionVal(SR, CastTy))
803 return *OptMemRegV;
804 }
805 }
806 }
807 // Next fixes pointer dereference using type different from its initial
808 // one. See PR37503 and PR49007 for details.
809 if (const auto *ER = dyn_cast<ElementRegion>(R)) {
810 if (auto OptMemRegV = VB.getCastedMemRegionVal(ER, CastTy))
811 return *OptMemRegV;
812 }
813
814 return V;
815 }
816
817 if (OriginalTy->isIntegralOrEnumerationType() ||
818 OriginalTy->isBlockPointerType() ||
819 OriginalTy->isFunctionPointerType())
820 return V;
821
822 // Array to pointer.
823 if (ArrayTy) {
824 // Are we casting from an array to a pointer? If so just pass on
825 // the decayed value.
826 if (CastTy->isPointerType() || CastTy->isReferenceType()) {
827 // We will always decay to a pointer.
828 QualType ElemTy = ArrayTy->getElementType();
829 return VB.getStateManager().ArrayToPointer(V, ElemTy);
830 }
831 // Are we casting from an array to an integer? If so, cast the decayed
832 // pointer value to an integer.
833 assert(CastTy->isIntegralOrEnumerationType());
834 }
835
836 // Other pointer to pointer.
837 assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
838 CastTy->isReferenceType());
839
840 // We get a symbolic function pointer for a dereference of a function
841 // pointer, but it is of function type. Example:
842
843 // struct FPRec {
844 // void (*my_func)(int * x);
845 // };
846 //
847 // int bar(int x);
848 //
849 // int f1_a(struct FPRec* foo) {
850 // int x;
851 // (*foo->my_func)(&x);
852 // return bar(x)+1; // no-warning
853 // }
854
855 // Get the result of casting a region to a different type.
856 const MemRegion *R = V.getRegion();
857 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
858 return *OptMemRegV;
859 }
860
861 // Pointer to whatever else.
862 // FIXME: There can be gross cases where one casts the result of a
863 // function (that returns a pointer) to some other value that happens to
864 // fit within that pointer value. We currently have no good way to model
865 // such operations. When this happens, the underlying operation is that
866 // the caller is reasoning about bits. Conceptually we are layering a
867 // "view" of a location on top of those bits. Perhaps we need to be more
868 // lazy about mutual possible views, even on an SVal? This may be
869 // necessary for bit-level reasoning as well.
870 return UnknownVal();
871 }
872 SVal VisitCompoundVal(nonloc::CompoundVal V) {
873 // Compound to whatever.
874 return UnknownVal();
875 }
876 SVal VisitConcreteInt(nonloc::ConcreteInt V) {
877 auto CastedValue = [V, this]() {
878 llvm::APSInt Value = V.getValue();
880 return Value;
881 };
882
883 // Integer to bool.
884 if (CastTy->isBooleanType())
885 return VB.makeTruthVal(V.getValue()->getBoolValue(), CastTy);
886
887 // Integer to pointer.
888 if (CastTy->isIntegralOrEnumerationType())
889 return VB.makeIntVal(CastedValue());
890
891 // Integer to pointer.
892 if (Loc::isLocType(CastTy))
893 return VB.makeIntLocVal(CastedValue());
894
895 // Pointer to whatever else.
896 return UnknownVal();
897 }
898 SVal VisitLazyCompoundVal(nonloc::LazyCompoundVal V) {
899 // LazyCompound to whatever.
900 return UnknownVal();
901 }
902 SVal VisitLocAsInteger(nonloc::LocAsInteger V) {
903 Loc L = V.getLoc();
904
905 // Pointer as integer to bool.
906 if (CastTy->isBooleanType())
907 // Pass to Loc function.
908 return Visit(L);
909
910 const bool IsUnknownOriginalType = OriginalTy.isNull();
911 // Pointer as integer to pointer.
912 if (!IsUnknownOriginalType && Loc::isLocType(CastTy) &&
913 OriginalTy->isIntegralOrEnumerationType()) {
914 if (const MemRegion *R = L.getAsRegion())
915 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
916 return *OptMemRegV;
917 return L;
918 }
919
920 // Pointer as integer with region to integer/pointer.
921 const MemRegion *R = L.getAsRegion();
922 if (!IsUnknownOriginalType && R) {
923 if (CastTy->isIntegralOrEnumerationType())
924 return VisitMemRegionVal(loc::MemRegionVal(R));
925
926 if (Loc::isLocType(CastTy)) {
927 assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
928 CastTy->isReferenceType());
929 // Delegate to store manager to get the result of casting a region to a
930 // different type. If the MemRegion* returned is NULL, this expression
931 // Evaluates to UnknownVal.
932 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
933 return *OptMemRegV;
934 }
935 } else {
936 if (Loc::isLocType(CastTy)) {
937 if (IsUnknownOriginalType)
938 return VisitMemRegionVal(loc::MemRegionVal(R));
939 return L;
940 }
941
942 SymbolRef SE = nullptr;
943 if (R) {
944 if (const SymbolicRegion *SR =
945 dyn_cast<SymbolicRegion>(R->StripCasts())) {
946 SE = SR->getSymbol();
947 }
948 }
949
950 if (!CastTy->isFloatingType() || !SE || SE->getType()->isFloatingType()) {
951 // FIXME: Correctly support promotions/truncations.
952 const unsigned CastSize = Context.getIntWidth(CastTy);
953 if (CastSize == V.getNumBits())
954 return V;
955
956 return VB.makeLocAsInteger(L, CastSize);
957 }
958 }
959
960 // Pointer as integer to whatever else.
961 return UnknownVal();
962 }
963 SVal VisitSymbolVal(nonloc::SymbolVal V) {
964 SymbolRef SE = V.getSymbol();
965
966 const bool IsUnknownOriginalType = OriginalTy.isNull();
967 // Symbol to bool.
968 if (!IsUnknownOriginalType && CastTy->isBooleanType()) {
969 // Non-float to bool.
970 if (Loc::isLocType(OriginalTy) ||
971 OriginalTy->isIntegralOrEnumerationType() ||
972 OriginalTy->isMemberPointerType()) {
973 BasicValueFactory &BVF = VB.getBasicValueFactory();
974 return VB.makeNonLoc(SE, BO_NE, BVF.getValue(0, SE->getType()), CastTy);
975 }
976 } else {
977 // Symbol to integer, float.
978 QualType T = Context.getCanonicalType(SE->getType());
979
980 // Produce SymbolCast if CastTy and T are different integers.
981 // NOTE: In the end the type of SymbolCast shall be equal to CastTy.
984 AnalyzerOptions &Opts = VB.getStateManager()
988 // If appropriate option is disabled, ignore the cast.
989 // NOTE: ShouldSupportSymbolicIntegerCasts is `false` by default.
991 return V;
992 return simplifySymbolCast(V, CastTy);
993 }
994 if (!Loc::isLocType(CastTy))
995 if (!IsUnknownOriginalType || !CastTy->isFloatingType() ||
996 T->isFloatingType())
997 return VB.makeNonLoc(SE, T, CastTy);
998 }
999
1000 // FIXME: We should be able to cast NonLoc -> Loc
1001 // (when Loc::isLocType(CastTy) is true)
1002 // But it's hard to do as SymbolicRegions can't refer to SymbolCasts holding
1003 // generic SymExprs. Check the commit message for the details.
1004
1005 // Symbol to pointer and whatever else.
1006 return UnknownVal();
1007 }
1008 SVal VisitPointerToMember(nonloc::PointerToMember V) {
1009 // Member pointer to whatever.
1010 return V;
1011 }
1012
1013 /// Reduce cast expression by removing redundant intermediate casts.
1014 /// E.g.
1015 /// - (char)(short)(int x) -> (char)(int x)
1016 /// - (int)(int x) -> int x
1017 ///
1018 /// \param V -- SymbolVal, which pressumably contains SymbolCast or any symbol
1019 /// that is applicable for cast operation.
1020 /// \param CastTy -- QualType, which `V` shall be cast to.
1021 /// \return SVal with simplified cast expression.
1022 /// \note: Currently only support integral casts.
1023 nonloc::SymbolVal simplifySymbolCast(nonloc::SymbolVal V, QualType CastTy) {
1024 // We use seven conditions to recognize a simplification case.
1025 // For the clarity let `CastTy` be `C`, SE->getType() - `T`, root type -
1026 // `R`, prefix `u` for unsigned, `s` for signed, no prefix - any sign: E.g.
1027 // (char)(short)(uint x)
1028 // ( sC )( sT )( uR x)
1029 //
1030 // C === R (the same type)
1031 // (char)(char x) -> (char x)
1032 // (long)(long x) -> (long x)
1033 // Note: Comparisons operators below are for bit width.
1034 // C == T
1035 // (short)(short)(int x) -> (short)(int x)
1036 // (int)(long)(char x) -> (int)(char x) (sizeof(long) == sizeof(int))
1037 // (long)(ullong)(char x) -> (long)(char x) (sizeof(long) ==
1038 // sizeof(ullong))
1039 // C < T
1040 // (short)(int)(char x) -> (short)(char x)
1041 // (char)(int)(short x) -> (char)(short x)
1042 // (short)(int)(short x) -> (short x)
1043 // C > T > uR
1044 // (int)(short)(uchar x) -> (int)(uchar x)
1045 // (uint)(short)(uchar x) -> (uint)(uchar x)
1046 // (int)(ushort)(uchar x) -> (int)(uchar x)
1047 // C > sT > sR
1048 // (int)(short)(char x) -> (int)(char x)
1049 // (uint)(short)(char x) -> (uint)(char x)
1050 // C > sT == sR
1051 // (int)(char)(char x) -> (int)(char x)
1052 // (uint)(short)(short x) -> (uint)(short x)
1053 // C > uT == uR
1054 // (int)(uchar)(uchar x) -> (int)(uchar x)
1055 // (uint)(ushort)(ushort x) -> (uint)(ushort x)
1056 // (llong)(ulong)(uint x) -> (llong)(uint x) (sizeof(ulong) ==
1057 // sizeof(uint))
1058
1059 SymbolRef SE = V.getSymbol();
1060 QualType T = Context.getCanonicalType(SE->getType());
1061
1062 if (T == CastTy)
1063 return V;
1064
1065 if (!isa<SymbolCast>(SE))
1066 return VB.makeNonLoc(SE, T, CastTy);
1067
1068 SymbolRef RootSym = cast<SymbolCast>(SE)->getOperand();
1069 QualType RT = RootSym->getType().getCanonicalType();
1070
1071 // FIXME support simplification from non-integers.
1072 if (!RT->isIntegralOrEnumerationType())
1073 return VB.makeNonLoc(SE, T, CastTy);
1074
1075 BasicValueFactory &BVF = VB.getBasicValueFactory();
1076 APSIntType CTy = BVF.getAPSIntType(CastTy);
1077 APSIntType TTy = BVF.getAPSIntType(T);
1078
1079 const auto WC = CTy.getBitWidth();
1080 const auto WT = TTy.getBitWidth();
1081
1082 if (WC <= WT) {
1083 const bool isSameType = (RT == CastTy);
1084 if (isSameType)
1085 return nonloc::SymbolVal(RootSym);
1086 return VB.makeNonLoc(RootSym, RT, CastTy);
1087 }
1088
1089 APSIntType RTy = BVF.getAPSIntType(RT);
1090 const auto WR = RTy.getBitWidth();
1091 const bool UT = TTy.isUnsigned();
1092 const bool UR = RTy.isUnsigned();
1093
1094 if (((WT > WR) && (UR || !UT)) || ((WT == WR) && (UT == UR)))
1095 return VB.makeNonLoc(RootSym, RT, CastTy);
1096
1097 return VB.makeNonLoc(SE, T, CastTy);
1098 }
1099};
1100} // end anonymous namespace
1101
1102/// Cast a given SVal to another SVal using given QualType's.
1103/// \param V -- SVal that should be casted.
1104/// \param CastTy -- QualType that V should be casted according to.
1105/// \param OriginalTy -- QualType which is associated to V. It provides
1106/// additional information about what type the cast performs from.
1107/// \returns the most appropriate casted SVal.
1108/// Note: Many cases don't use an exact OriginalTy. It can be extracted
1109/// from SVal or the cast can performs unconditionaly. Always pass OriginalTy!
1110/// It can be crucial in certain cases and generates different results.
1111/// FIXME: If `OriginalTy.isNull()` is true, then cast performs based on CastTy
1112/// only. This behavior is uncertain and should be improved.
1114 EvalCastVisitor TRV{*this, CastTy, OriginalTy};
1115 return TRV.Visit(V);
1116}
Defines the clang::ASTContext interface.
#define V(N, I)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
#define X(type, name)
Definition Value.h:97
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy, QualType FromTy)
Recursively check if the pointer types are equal modulo const, volatile, and restrict qualifiers.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
unsigned getIntWidth(QualType T) const
static CanQualType getCanonicalType(QualType T)
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
ASTContext & getASTContext() const
bool analyzerSymbolicIntegerCasts() const
BinaryOperatorKind Opcode
Definition Expr.h:4087
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4806
const Stmt * getStmt() const
Definition CFG.h:143
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition ExprCXX.h:727
bool getValue() const
Definition ExprCXX.h:744
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2149
QualType getThisType() const
Return the type of the this pointer.
Definition DeclCXX.cpp:2859
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This represents one expression.
Definition Expr.h:113
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool isGLValue() const
Definition Expr.h:288
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3119
@ NPC_ValueDependentIsNotNull
Specifies that a value-dependent expression should be considered to never be a null pointer constant.
Definition Expr.h:855
Decl * getReferencedDeclOfCallee()
Definition Expr.cpp:1574
NullPointerConstantKind isNullPointerConstant(ASTContext &Ctx, NullPointerConstantValueDependence NPC) const
isNullPointerConstant - C99 6.3.2.3p3 - Test if this reduces down to a Null pointer constant.
Definition Expr.cpp:4104
QualType getType() const
Definition Expr.h:145
Represents a function declaration or definition.
Definition Decl.h:2058
This represents a decl that may have a name.
Definition Decl.h:274
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8502
QualType getCanonicalType() const
Definition TypeBase.h:8554
The collection of all-type qualifiers we support.
Definition TypeBase.h:332
void removeCVRQualifiers(unsigned mask)
Definition TypeBase.h:496
It represents a stack frame of the call stack.
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
StmtClass getStmtClass() const
Definition Stmt.h:1505
bool isBlockPointerType() const
Definition TypeBase.h:8759
bool isVoidType() const
Definition TypeBase.h:9111
bool isBooleanType() const
Definition TypeBase.h:9248
bool isIntegralOrUnscopedEnumerationType() const
Determine whether this type is an integral or unscoped enumeration type.
Definition Type.cpp:2203
bool isVoidPointerType() const
Definition Type.cpp:749
bool isFunctionPointerType() const
Definition TypeBase.h:8806
bool isPointerType() const
Definition TypeBase.h:8739
bool isReferenceType() const
Definition TypeBase.h:8763
bool isVariableArrayType() const
Definition TypeBase.h:8850
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9233
bool isMemberPointerType() const
Definition TypeBase.h:8820
bool isFunctionType() const
Definition TypeBase.h:8735
bool isFloatingType() const
Definition Type.cpp:2421
bool isUnsignedIntegerType() const
Return true if this is an integer type that is unsigned, according to C99 6.2.5p6 [which returns true...
Definition Type.cpp:2364
UnaryOperatorKind Opcode
Definition Expr.h:2302
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
A record of the "type" of an APSInt, used for conversions.
Definition APSIntType.h:19
bool isUnsigned() const
Definition APSIntType.h:31
uint32_t getBitWidth() const
Definition APSIntType.h:30
llvm::APSInt getMaxValue() const LLVM_READONLY
Returns the maximum value for this type.
Definition APSIntType.h:65
void apply(llvm::APSInt &Value) const
Convert a given APSInt, in place, to match this type.
Definition APSIntType.h:37
AllocaRegion - A region that represents an untyped blob of bytes created by a call to 'alloca'.
Definition MemRegion.h:512
AnalyzerOptions & getAnalyzerOptions() override
APSIntPtr getZeroWithTypeSize(QualType T)
APSIntType getAPSIntType(QualType T) const
Returns the type of the APSInt used to store values of the given QualType.
BlockCodeRegion - A region that represents code texts of blocks (closures).
Definition MemRegion.h:665
BlockDataRegion - A region that represents a block instance.
Definition MemRegion.h:712
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
QualType getResultType() const
Returns the result type, adjusted for references.
Definition CallEvent.cpp:70
const StackFrame * getStackFrame() const
The stack frame in which the call is being evaluated.
Definition CallEvent.h:250
const CFGBlock::ConstCFGElementRef & getCFGElementRef() const
Definition CallEvent.h:252
AnalysisManager & getAnalysisManager()
Definition ExprEngine.h:194
static bool isLocType(QualType T)
Definition SVals.h:268
const AllocaRegion * getAllocaRegion(const Expr *Ex, unsigned Cnt, const StackFrame *SF)
getAllocaRegion - Retrieve a region associated with a call to alloca().
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
SVal ArrayToPointer(Loc Array, QualType ElementTy)
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing '0' for the specified type.
DefinedSVal getMemberPointer(const NamedDecl *ND)
SVal evalMinus(NonLoc val)
SVal evalComplement(NonLoc val)
BasicValueFactory & getBasicValueFactory()
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
SymbolManager SymMgr
Manages the creation of symbols.
Definition SValBuilder.h:63
virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with a memory location and non-location opera...
MemRegionManager & getRegionManager()
ProgramStateManager & getStateManager()
SVal makeSymExprValNN(BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)
Constructs a symbolic expression for two non-location values.
virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, Loc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two memory location operands.
const unsigned ArrayIndexWidth
The width of the scalar type used for array indices.
Definition SValBuilder.h:76
DefinedSVal getFunctionPointer(const FunctionDecl *func)
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition SValBuilder.h:73
ASTContext & getContext()
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
SVal convertToArrayIndex(SVal val)
loc::MemRegionVal makeLoc(SymbolRef sym)
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const StackFrame *SF, unsigned blockCount)
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two non- location operands.
SVal evalCast(SVal V, QualType CastTy, QualType OriginalTy)
Cast a given SVal to another SVal using given QualType's.
const AnalyzerOptions & getAnalyzerOptions() const
BasicValueFactory BasicVals
Manager of APSInt values.
Definition SValBuilder.h:60
ConditionTruthVal areEqual(ProgramStateRef state, SVal lhs, SVal rhs)
QualType getConditionType() const
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition SValBuilder.h:66
SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs)
SVal evalUnaryOp(ProgramStateRef state, UnaryOperator::Opcode opc, SVal operand, QualType type)
DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, const TypedValueRegion *region)
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
loc::ConcreteInt makeNullWithType(QualType type)
Create NULL pointer, with proper pointer bit-width for given address space.
loc::MemRegionVal getAllocaRegionVal(const Expr *E, const StackFrame *SF, unsigned Count)
Create an SVal representing the result of an alloca()-like call, that is, an AllocaRegion on the stac...
ProgramStateManager & StateMgr
Definition SValBuilder.h:68
DefinedSVal getConjuredHeapSymbolVal(ConstCFGElementRef elem, const StackFrame *SF, QualType type, unsigned Count)
Conjure a symbol representing heap allocated memory region.
std::optional< SVal > getConstantVal(const Expr *E)
Returns the value of E, if it can be determined in a non-path-sensitive manner.
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy, QualType originalType)
SymbolManager & getSymbolManager()
DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region)
Make a unique symbol for value of region.
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrame *SF)
Return a memory region for the 'this' object reference.
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
DefinedSVal getMetadataSymbolVal(const void *symbolTag, const MemRegion *region, const Expr *expr, QualType type, const StackFrame *SF, unsigned count)
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
nonloc::SymbolVal makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, APSIntPtr rhs, QualType type)
const AnalyzerOptions & AnOpts
Definition SValBuilder.h:70
std::optional< loc::MemRegionVal > getCastedMemRegionVal(const MemRegion *region, QualType type)
Return MemRegionVal on success cast, otherwise return std::nullopt.
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
SValVisitor - this class implements a simple visitor for SVal subclasses.
Definition SValVisitor.h:27
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
bool isUndef() const
Definition SVals.h:113
bool isUnknownOrUndef() const
Definition SVals.h:115
SymbolRef getAsSymbol(bool IncludeBaseRegions=false) const
If this SVal wraps a symbol return that SymbolRef.
Definition SVals.cpp:103
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition SVals.h:88
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:84
bool isUnknown() const
Definition SVals.h:111
Symbolic value.
Definition SymExpr.h:32
virtual QualType getType() const =0
virtual unsigned computeComplexity() const =0
Represents a cast expression.
A symbol representing the value of a MemRegion whose parent region has symbolic value.
const SymExprT * acquire(Args &&...args)
Create or retrieve a SymExpr of type SymExprT for the given arguments.
static bool canSymbolicate(QualType T)
SymbolMetadata - Represents path-dependent metadata about a specific region.
A symbol representing the value stored at a MemRegion.
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:569
virtual QualType getValueType() const =0
Represents a symbolic expression involving a unary operator.
Value representing integer constant.
Definition SVals.h:306
APSIntPtr getValue() const
Definition SVals.h:310
Value representing pointer-to-member.
Definition SVals.h:440
Represents symbolic expression that isn't a location.
Definition SVals.h:285
LLVM_ATTRIBUTE_RETURNS_NONNULL SymbolRef getSymbol() const
Definition SVals.h:294
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
BinarySymExprImpl< APSIntPtr, const SymExpr *, SymExpr::Kind::IntSymExprKind > IntSymExpr
Represents a symbolic expression like 3 - 'x'.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
BinarySymExprImpl< const SymExpr *, const SymExpr *, SymExpr::Kind::SymSymExprKind > SymSymExpr
Represents a symbolic expression like 'x' + 'y'.
BinarySymExprImpl< const SymExpr *, APSIntPtr, SymExpr::Kind::SymIntExprKind > SymIntExpr
Represents a symbolic expression like 'x' + 3.
Top level wrappers for InstallAPI frontend operations.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1248
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Result
The result type of a method or function.
Definition TypeBase.h:906
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:666