clang 23.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 switch (E->getStmtClass()) {
330 // Handle expressions that we treat differently from the AST's constant
331 // evaluator.
332 case Stmt::AddrLabelExprClass:
333 return makeLoc(cast<AddrLabelExpr>(E));
334
335 case Stmt::CXXScalarValueInitExprClass:
336 case Stmt::ImplicitValueInitExprClass:
337 return makeZeroVal(E->getType());
338
339 case Stmt::ObjCStringLiteralClass: {
340 const auto *SL = cast<ObjCStringLiteral>(E);
341 return makeLoc(getRegionManager().getObjCStringRegion(SL));
342 }
343
344 case Stmt::StringLiteralClass: {
345 const auto *SL = cast<StringLiteral>(E);
346 return makeLoc(getRegionManager().getStringRegion(SL));
347 }
348
349 case Stmt::PredefinedExprClass: {
350 const auto *PE = cast<PredefinedExpr>(E);
351 assert(PE->getFunctionName() &&
352 "Since we analyze only instantiated functions, PredefinedExpr "
353 "should have a function name.");
354 return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName()));
355 }
356
357 // Fast-path some expressions to avoid the overhead of going through the AST's
358 // constant evaluator
359 case Stmt::CharacterLiteralClass: {
360 const auto *C = cast<CharacterLiteral>(E);
361 return makeIntVal(C->getValue(), C->getType());
362 }
363
364 case Stmt::CXXBoolLiteralExprClass:
366
367 case Stmt::TypeTraitExprClass: {
368 const auto *TE = cast<TypeTraitExpr>(E);
369 if (TE->isStoredAsBoolean())
370 return makeTruthVal(TE->getBoolValue(), TE->getType());
371 assert(TE->getAPValue().isInt() && "APValue type not supported");
372 return makeIntVal(TE->getAPValue().getInt());
373 }
374
375 case Stmt::IntegerLiteralClass:
377
378 case Stmt::ObjCBoolLiteralExprClass:
380
381 case Stmt::CXXNullPtrLiteralExprClass:
382 return makeNullWithType(E->getType());
383
384 case Stmt::CStyleCastExprClass:
385 case Stmt::CXXFunctionalCastExprClass:
386 case Stmt::CXXConstCastExprClass:
387 case Stmt::CXXReinterpretCastExprClass:
388 case Stmt::CXXStaticCastExprClass:
389 case Stmt::ImplicitCastExprClass: {
390 const auto *CE = cast<CastExpr>(E);
391 switch (CE->getCastKind()) {
392 default:
393 break;
394 case CK_ArrayToPointerDecay:
395 case CK_IntegralToPointer:
396 case CK_NoOp:
397 case CK_BitCast: {
398 const Expr *SE = CE->getSubExpr();
399 std::optional<SVal> Val = getConstantVal(SE);
400 if (!Val)
401 return std::nullopt;
402 return evalCast(*Val, CE->getType(), SE->getType());
403 }
404 }
405 [[fallthrough]];
406 }
407
408 // If we don't have a special case, fall back to the AST's constant evaluator.
409 default: {
410 // Don't try to come up with a value for materialized temporaries.
411 if (E->isGLValue())
412 return std::nullopt;
413
414 ASTContext &Ctx = getContext();
416 if (E->EvaluateAsInt(Result, Ctx))
417 return makeIntVal(Result.Val.getInt());
418
419 if (Loc::isLocType(E->getType()))
421 return makeNullWithType(E->getType());
422
423 return std::nullopt;
424 }
425 }
426}
427
429 NonLoc LHS, NonLoc RHS,
430 QualType ResultTy) {
431 SymbolRef symLHS = LHS.getAsSymbol();
432 SymbolRef symRHS = RHS.getAsSymbol();
433
434 // TODO: When the Max Complexity is reached, we should conjure a symbol
435 // instead of generating an Unknown value and propagate the taint info to it.
436 const unsigned MaxComp = AnOpts.MaxSymbolComplexity;
437
438 if (symLHS && symRHS &&
439 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp)
440 return makeNonLoc(symLHS, Op, symRHS, ResultTy);
441
442 if (symLHS && symLHS->computeComplexity() < MaxComp)
443 if (std::optional<nonloc::ConcreteInt> rInt =
445 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy);
446
447 if (symRHS && symRHS->computeComplexity() < MaxComp)
448 if (std::optional<nonloc::ConcreteInt> lInt =
450 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy);
451
452 return UnknownVal();
453}
454
456 switch (X.getKind()) {
457 case nonloc::ConcreteIntKind:
458 return makeIntVal(-X.castAs<nonloc::ConcreteInt>().getValue());
459 case nonloc::SymbolValKind:
460 return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Minus,
461 X.getType(Context));
462 default:
463 return UnknownVal();
464 }
465}
466
468 switch (X.getKind()) {
469 case nonloc::ConcreteIntKind:
470 return makeIntVal(~X.castAs<nonloc::ConcreteInt>().getValue());
471 case nonloc::SymbolValKind:
472 return makeNonLoc(X.castAs<nonloc::SymbolVal>().getSymbol(), UO_Not,
473 X.getType(Context));
474 default:
475 return UnknownVal();
476 }
477}
478
480 SVal operand, QualType type) {
481 auto OpN = operand.getAs<NonLoc>();
482 if (!OpN)
483 return UnknownVal();
484
485 if (opc == UO_Minus)
486 return evalMinus(*OpN);
487 if (opc == UO_Not)
488 return evalComplement(*OpN);
489 llvm_unreachable("Unexpected unary operator");
490}
491
493 SVal lhs, SVal rhs, QualType type) {
494 if (lhs.isUndef() || rhs.isUndef())
495 return UndefinedVal();
496
497 if (lhs.isUnknown() || rhs.isUnknown())
498 return UnknownVal();
499
501 return UnknownVal();
502 }
503
504 if (op == BinaryOperatorKind::BO_Cmp) {
505 // We can't reason about C++20 spaceship operator yet.
506 //
507 // FIXME: Support C++20 spaceship operator.
508 // The main problem here is that the result is not integer.
509 return UnknownVal();
510 }
511
512 if (std::optional<Loc> LV = lhs.getAs<Loc>()) {
513 if (std::optional<Loc> RV = rhs.getAs<Loc>())
514 return evalBinOpLL(state, op, *LV, *RV, type);
515
516 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type);
517 }
518
519 if (const std::optional<Loc> RV = rhs.getAs<Loc>()) {
520 const auto IsCommutative = [](BinaryOperatorKind Op) {
521 return Op == BO_Mul || Op == BO_Add || Op == BO_And || Op == BO_Xor ||
522 Op == BO_Or;
523 };
524
525 if (IsCommutative(op)) {
526 // Swap operands.
527 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type);
528 }
529
530 // If the right operand is a concrete int location then we have nothing
531 // better but to treat it as a simple nonloc.
532 if (auto RV = rhs.getAs<loc::ConcreteInt>()) {
533 const nonloc::ConcreteInt RhsAsLoc = makeIntVal(RV->getValue());
534 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), RhsAsLoc, type);
535 }
536 }
537
538 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(),
539 type);
540}
541
543 SVal rhs) {
544 return state->isNonNull(evalEQ(state, lhs, rhs));
545}
546
548 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType());
549}
550
554 return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs))
556}
557
558/// Recursively check if the pointer types are equal modulo const, volatile,
559/// and restrict qualifiers. Also, assume that all types are similar to 'void'.
560/// Assumes the input types are canonical.
561static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy,
562 QualType FromTy) {
563 while (Context.UnwrapSimilarTypes(ToTy, FromTy)) {
564 Qualifiers Quals1, Quals2;
565 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1);
566 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2);
567
568 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address
569 // spaces) are identical.
570 Quals1.removeCVRQualifiers();
571 Quals2.removeCVRQualifiers();
572 if (Quals1 != Quals2)
573 return false;
574 }
575
576 // If we are casting to void, the 'From' value can be used to represent the
577 // 'To' value.
578 //
579 // FIXME: Doing this after unwrapping the types doesn't make any sense. A
580 // cast from 'int**' to 'void**' is not special in the way that a cast from
581 // 'int*' to 'void*' is.
582 if (ToTy->isVoidType())
583 return true;
584
585 if (ToTy != FromTy)
586 return false;
587
588 return true;
589}
590
591// Handles casts of type CK_IntegralCast.
592// At the moment, this function will redirect to evalCast, except when the range
593// of the original value is known to be greater than the max of the target type.
595 QualType castTy, QualType originalTy) {
596 // No truncations if target type is big enough.
597 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy))
598 return evalCast(val, castTy, originalTy);
599
600 auto AsNonLoc = val.getAs<NonLoc>();
601 SymbolRef AsSymbol = val.getAsSymbol();
602 if (!AsSymbol || !AsNonLoc) // Let evalCast handle non symbolic expressions.
603 return evalCast(val, castTy, originalTy);
604
605 // Find the maximum value of the target type.
606 APSIntType ToType(getContext().getTypeSize(castTy),
607 castTy->isUnsignedIntegerType());
608 llvm::APSInt ToTypeMax = ToType.getMaxValue();
609
610 NonLoc ToTypeMaxVal = makeIntVal(ToTypeMax);
611
612 // Check the range of the symbol being casted against the maximum value of the
613 // target type.
614 QualType CmpTy = getConditionType();
615 NonLoc CompVal = evalBinOpNN(state, BO_LE, *AsNonLoc, ToTypeMaxVal, CmpTy)
616 .castAs<NonLoc>();
617 ProgramStateRef IsNotTruncated, IsTruncated;
618 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal);
619 if (!IsNotTruncated && IsTruncated) {
620 // Symbol is truncated so we evaluate it as a cast.
621 return makeNonLoc(AsSymbol, originalTy, castTy);
622 }
623 return evalCast(val, castTy, originalTy);
624}
625
626//===----------------------------------------------------------------------===//
627// Cast method.
628// `evalCast` and its helper `EvalCastVisitor`
629//===----------------------------------------------------------------------===//
630
631namespace {
632class EvalCastVisitor : public SValVisitor<EvalCastVisitor, SVal> {
633private:
634 SValBuilder &VB;
635 ASTContext &Context;
636 QualType CastTy, OriginalTy;
637
638public:
639 EvalCastVisitor(SValBuilder &VB, QualType CastTy, QualType OriginalTy)
640 : VB(VB), Context(VB.getContext()), CastTy(CastTy),
641 OriginalTy(OriginalTy) {}
642
643 SVal Visit(SVal V) {
644 if (CastTy.isNull())
645 return V;
646
647 CastTy = Context.getCanonicalType(CastTy);
648
649 const bool IsUnknownOriginalType = OriginalTy.isNull();
650 if (!IsUnknownOriginalType) {
651 OriginalTy = Context.getCanonicalType(OriginalTy);
652
653 if (CastTy == OriginalTy)
654 return V;
655
656 // FIXME: Move this check to the most appropriate
657 // evalCastKind/evalCastSubKind function. For const casts, casts to void,
658 // just propagate the value.
659 if (!CastTy->isVariableArrayType() && !OriginalTy->isVariableArrayType())
660 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(CastTy),
661 Context.getPointerType(OriginalTy)))
662 return V;
663 }
664 return SValVisitor::Visit(V);
665 }
666 SVal VisitUndefinedVal(UndefinedVal V) { return V; }
667 SVal VisitUnknownVal(UnknownVal V) { return V; }
668 SVal VisitConcreteInt(loc::ConcreteInt V) {
669 // Pointer to bool.
670 if (CastTy->isBooleanType())
671 return VB.makeTruthVal(V.getValue()->getBoolValue(), CastTy);
672
673 // Pointer to integer.
674 if (CastTy->isIntegralOrEnumerationType()) {
675 llvm::APSInt Value = V.getValue();
677 return VB.makeIntVal(Value);
678 }
679
680 // Pointer to any pointer.
681 if (Loc::isLocType(CastTy)) {
682 llvm::APSInt Value = V.getValue();
684 return loc::ConcreteInt(VB.getBasicValueFactory().getValue(Value));
685 }
686
687 // Pointer to whatever else.
688 return UnknownVal();
689 }
690 SVal VisitGotoLabel(loc::GotoLabel V) {
691 // Pointer to bool.
692 if (CastTy->isBooleanType())
693 // Labels are always true.
694 return VB.makeTruthVal(true, CastTy);
695
696 // Pointer to integer.
697 if (CastTy->isIntegralOrEnumerationType()) {
698 const unsigned BitWidth = Context.getIntWidth(CastTy);
699 return VB.makeLocAsInteger(V, BitWidth);
700 }
701
702 const bool IsUnknownOriginalType = OriginalTy.isNull();
703 if (!IsUnknownOriginalType) {
704 // Array to pointer.
705 if (isa<ArrayType>(OriginalTy))
706 if (CastTy->isPointerType() || CastTy->isReferenceType())
707 return UnknownVal();
708 }
709
710 // Pointer to any pointer.
711 if (Loc::isLocType(CastTy))
712 return V;
713
714 // Pointer to whatever else.
715 return UnknownVal();
716 }
717 SVal VisitMemRegionVal(loc::MemRegionVal V) {
718 // Pointer to bool.
719 if (CastTy->isBooleanType()) {
720 const MemRegion *R = V.getRegion();
721 if (const FunctionCodeRegion *FTR = dyn_cast<FunctionCodeRegion>(R))
722 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(FTR->getDecl()))
723 if (FD->isWeak())
724 // FIXME: Currently we are using an extent symbol here,
725 // because there are no generic region address metadata
726 // symbols to use, only content metadata.
727 return nonloc::SymbolVal(
728 VB.getSymbolManager().acquire<SymbolExtent>(FTR));
729
730 if (const SymbolicRegion *SymR = R->getSymbolicBase()) {
731 SymbolRef Sym = SymR->getSymbol();
732 QualType Ty = Sym->getType();
733 // This change is needed for architectures with varying
734 // pointer widths. See the amdgcn opencl reproducer with
735 // this change as an example: solver-sym-simplification-ptr-bool.cl
736 if (!Ty->isReferenceType())
737 return VB.makeNonLoc(
738 Sym, BO_NE, VB.getBasicValueFactory().getZeroWithTypeSize(Ty),
739 CastTy);
740 }
741 // Non-symbolic memory regions are always true.
742 return VB.makeTruthVal(true, CastTy);
743 }
744
745 const bool IsUnknownOriginalType = OriginalTy.isNull();
746 // Try to cast to array
747 const auto *ArrayTy =
748 IsUnknownOriginalType
749 ? nullptr
750 : dyn_cast<ArrayType>(OriginalTy.getCanonicalType());
751
752 // Pointer to integer.
753 if (CastTy->isIntegralOrEnumerationType()) {
754 SVal Val = V;
755 // Array to integer.
756 if (ArrayTy) {
757 // We will always decay to a pointer.
758 QualType ElemTy = ArrayTy->getElementType();
759 Val = VB.getStateManager().ArrayToPointer(V, ElemTy);
760 // FIXME: Keep these here for now in case we decide soon that we
761 // need the original decayed type.
762 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType();
763 // QualType pointerTy = C.getPointerType(elemTy);
764 }
765 const unsigned BitWidth = Context.getIntWidth(CastTy);
766 return VB.makeLocAsInteger(Val.castAs<Loc>(), BitWidth);
767 }
768
769 // Pointer to pointer.
770 if (Loc::isLocType(CastTy)) {
771
772 if (IsUnknownOriginalType) {
773 // When retrieving symbolic pointer and expecting a non-void pointer,
774 // wrap them into element regions of the expected type if necessary.
775 // It is necessary to make sure that the retrieved value makes sense,
776 // because there's no other cast in the AST that would tell us to cast
777 // it to the correct pointer type. We might need to do that for non-void
778 // pointers as well.
779 // FIXME: We really need a single good function to perform casts for us
780 // correctly every time we need it.
781 const MemRegion *R = V.getRegion();
782 if (CastTy->isPointerType() && !CastTy->isVoidPointerType()) {
783 if (const auto *SR = dyn_cast<SymbolicRegion>(R)) {
784 QualType SRTy = SR->getSymbol()->getType();
785
786 auto HasSameUnqualifiedPointeeType = [](QualType ty1,
787 QualType ty2) {
788 return ty1->getPointeeType().getCanonicalType().getTypePtr() ==
790 };
791 if (!HasSameUnqualifiedPointeeType(SRTy, CastTy)) {
792 if (auto OptMemRegV = VB.getCastedMemRegionVal(SR, CastTy))
793 return *OptMemRegV;
794 }
795 }
796 }
797 // Next fixes pointer dereference using type different from its initial
798 // one. See PR37503 and PR49007 for details.
799 if (const auto *ER = dyn_cast<ElementRegion>(R)) {
800 if (auto OptMemRegV = VB.getCastedMemRegionVal(ER, CastTy))
801 return *OptMemRegV;
802 }
803
804 return V;
805 }
806
807 if (OriginalTy->isIntegralOrEnumerationType() ||
808 OriginalTy->isBlockPointerType() ||
809 OriginalTy->isFunctionPointerType())
810 return V;
811
812 // Array to pointer.
813 if (ArrayTy) {
814 // Are we casting from an array to a pointer? If so just pass on
815 // the decayed value.
816 if (CastTy->isPointerType() || CastTy->isReferenceType()) {
817 // We will always decay to a pointer.
818 QualType ElemTy = ArrayTy->getElementType();
819 return VB.getStateManager().ArrayToPointer(V, ElemTy);
820 }
821 // Are we casting from an array to an integer? If so, cast the decayed
822 // pointer value to an integer.
823 assert(CastTy->isIntegralOrEnumerationType());
824 }
825
826 // Other pointer to pointer.
827 assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
828 CastTy->isReferenceType());
829
830 // We get a symbolic function pointer for a dereference of a function
831 // pointer, but it is of function type. Example:
832
833 // struct FPRec {
834 // void (*my_func)(int * x);
835 // };
836 //
837 // int bar(int x);
838 //
839 // int f1_a(struct FPRec* foo) {
840 // int x;
841 // (*foo->my_func)(&x);
842 // return bar(x)+1; // no-warning
843 // }
844
845 // Get the result of casting a region to a different type.
846 const MemRegion *R = V.getRegion();
847 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
848 return *OptMemRegV;
849 }
850
851 // Pointer to whatever else.
852 // FIXME: There can be gross cases where one casts the result of a
853 // function (that returns a pointer) to some other value that happens to
854 // fit within that pointer value. We currently have no good way to model
855 // such operations. When this happens, the underlying operation is that
856 // the caller is reasoning about bits. Conceptually we are layering a
857 // "view" of a location on top of those bits. Perhaps we need to be more
858 // lazy about mutual possible views, even on an SVal? This may be
859 // necessary for bit-level reasoning as well.
860 return UnknownVal();
861 }
862 SVal VisitCompoundVal(nonloc::CompoundVal V) {
863 // Compound to whatever.
864 return UnknownVal();
865 }
866 SVal VisitConcreteInt(nonloc::ConcreteInt V) {
867 auto CastedValue = [V, this]() {
868 llvm::APSInt Value = V.getValue();
870 return Value;
871 };
872
873 // Integer to bool.
874 if (CastTy->isBooleanType())
875 return VB.makeTruthVal(V.getValue()->getBoolValue(), CastTy);
876
877 // Integer to pointer.
878 if (CastTy->isIntegralOrEnumerationType())
879 return VB.makeIntVal(CastedValue());
880
881 // Integer to pointer.
882 if (Loc::isLocType(CastTy))
883 return VB.makeIntLocVal(CastedValue());
884
885 // Pointer to whatever else.
886 return UnknownVal();
887 }
888 SVal VisitLazyCompoundVal(nonloc::LazyCompoundVal V) {
889 // LazyCompound to whatever.
890 return UnknownVal();
891 }
892 SVal VisitLocAsInteger(nonloc::LocAsInteger V) {
893 Loc L = V.getLoc();
894
895 // Pointer as integer to bool.
896 if (CastTy->isBooleanType())
897 // Pass to Loc function.
898 return Visit(L);
899
900 const bool IsUnknownOriginalType = OriginalTy.isNull();
901 // Pointer as integer to pointer.
902 if (!IsUnknownOriginalType && Loc::isLocType(CastTy) &&
903 OriginalTy->isIntegralOrEnumerationType()) {
904 if (const MemRegion *R = L.getAsRegion())
905 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
906 return *OptMemRegV;
907 return L;
908 }
909
910 // Pointer as integer with region to integer/pointer.
911 const MemRegion *R = L.getAsRegion();
912 if (!IsUnknownOriginalType && R) {
913 if (CastTy->isIntegralOrEnumerationType())
914 return VisitMemRegionVal(loc::MemRegionVal(R));
915
916 if (Loc::isLocType(CastTy)) {
917 assert(Loc::isLocType(OriginalTy) || OriginalTy->isFunctionType() ||
918 CastTy->isReferenceType());
919 // Delegate to store manager to get the result of casting a region to a
920 // different type. If the MemRegion* returned is NULL, this expression
921 // Evaluates to UnknownVal.
922 if (auto OptMemRegV = VB.getCastedMemRegionVal(R, CastTy))
923 return *OptMemRegV;
924 }
925 } else {
926 if (Loc::isLocType(CastTy)) {
927 if (IsUnknownOriginalType)
928 return VisitMemRegionVal(loc::MemRegionVal(R));
929 return L;
930 }
931
932 SymbolRef SE = nullptr;
933 if (R) {
934 if (const SymbolicRegion *SR =
935 dyn_cast<SymbolicRegion>(R->StripCasts())) {
936 SE = SR->getSymbol();
937 }
938 }
939
940 if (!CastTy->isFloatingType() || !SE || SE->getType()->isFloatingType()) {
941 // FIXME: Correctly support promotions/truncations.
942 const unsigned CastSize = Context.getIntWidth(CastTy);
943 if (CastSize == V.getNumBits())
944 return V;
945
946 return VB.makeLocAsInteger(L, CastSize);
947 }
948 }
949
950 // Pointer as integer to whatever else.
951 return UnknownVal();
952 }
953 SVal VisitSymbolVal(nonloc::SymbolVal V) {
954 SymbolRef SE = V.getSymbol();
955
956 const bool IsUnknownOriginalType = OriginalTy.isNull();
957 // Symbol to bool.
958 if (!IsUnknownOriginalType && CastTy->isBooleanType()) {
959 // Non-float to bool.
960 if (Loc::isLocType(OriginalTy) ||
961 OriginalTy->isIntegralOrEnumerationType() ||
962 OriginalTy->isMemberPointerType()) {
963 BasicValueFactory &BVF = VB.getBasicValueFactory();
964 return VB.makeNonLoc(SE, BO_NE, BVF.getValue(0, SE->getType()), CastTy);
965 }
966 } else {
967 // Symbol to integer, float.
968 QualType T = Context.getCanonicalType(SE->getType());
969
970 // Produce SymbolCast if CastTy and T are different integers.
971 // NOTE: In the end the type of SymbolCast shall be equal to CastTy.
974 AnalyzerOptions &Opts = VB.getStateManager()
978 // If appropriate option is disabled, ignore the cast.
979 // NOTE: ShouldSupportSymbolicIntegerCasts is `false` by default.
980 if (!Opts.ShouldSupportSymbolicIntegerCasts)
981 return V;
982 return simplifySymbolCast(V, CastTy);
983 }
984 if (!Loc::isLocType(CastTy))
985 if (!IsUnknownOriginalType || !CastTy->isFloatingType() ||
986 T->isFloatingType())
987 return VB.makeNonLoc(SE, T, CastTy);
988 }
989
990 // FIXME: We should be able to cast NonLoc -> Loc
991 // (when Loc::isLocType(CastTy) is true)
992 // But it's hard to do as SymbolicRegions can't refer to SymbolCasts holding
993 // generic SymExprs. Check the commit message for the details.
994
995 // Symbol to pointer and whatever else.
996 return UnknownVal();
997 }
998 SVal VisitPointerToMember(nonloc::PointerToMember V) {
999 // Member pointer to whatever.
1000 return V;
1001 }
1002
1003 /// Reduce cast expression by removing redundant intermediate casts.
1004 /// E.g.
1005 /// - (char)(short)(int x) -> (char)(int x)
1006 /// - (int)(int x) -> int x
1007 ///
1008 /// \param V -- SymbolVal, which pressumably contains SymbolCast or any symbol
1009 /// that is applicable for cast operation.
1010 /// \param CastTy -- QualType, which `V` shall be cast to.
1011 /// \return SVal with simplified cast expression.
1012 /// \note: Currently only support integral casts.
1013 nonloc::SymbolVal simplifySymbolCast(nonloc::SymbolVal V, QualType CastTy) {
1014 // We use seven conditions to recognize a simplification case.
1015 // For the clarity let `CastTy` be `C`, SE->getType() - `T`, root type -
1016 // `R`, prefix `u` for unsigned, `s` for signed, no prefix - any sign: E.g.
1017 // (char)(short)(uint x)
1018 // ( sC )( sT )( uR x)
1019 //
1020 // C === R (the same type)
1021 // (char)(char x) -> (char x)
1022 // (long)(long x) -> (long x)
1023 // Note: Comparisons operators below are for bit width.
1024 // C == T
1025 // (short)(short)(int x) -> (short)(int x)
1026 // (int)(long)(char x) -> (int)(char x) (sizeof(long) == sizeof(int))
1027 // (long)(ullong)(char x) -> (long)(char x) (sizeof(long) ==
1028 // sizeof(ullong))
1029 // C < T
1030 // (short)(int)(char x) -> (short)(char x)
1031 // (char)(int)(short x) -> (char)(short x)
1032 // (short)(int)(short x) -> (short x)
1033 // C > T > uR
1034 // (int)(short)(uchar x) -> (int)(uchar x)
1035 // (uint)(short)(uchar x) -> (uint)(uchar x)
1036 // (int)(ushort)(uchar x) -> (int)(uchar x)
1037 // C > sT > sR
1038 // (int)(short)(char x) -> (int)(char x)
1039 // (uint)(short)(char x) -> (uint)(char x)
1040 // C > sT == sR
1041 // (int)(char)(char x) -> (int)(char x)
1042 // (uint)(short)(short x) -> (uint)(short x)
1043 // C > uT == uR
1044 // (int)(uchar)(uchar x) -> (int)(uchar x)
1045 // (uint)(ushort)(ushort x) -> (uint)(ushort x)
1046 // (llong)(ulong)(uint x) -> (llong)(uint x) (sizeof(ulong) ==
1047 // sizeof(uint))
1048
1049 SymbolRef SE = V.getSymbol();
1050 QualType T = Context.getCanonicalType(SE->getType());
1051
1052 if (T == CastTy)
1053 return V;
1054
1055 if (!isa<SymbolCast>(SE))
1056 return VB.makeNonLoc(SE, T, CastTy);
1057
1058 SymbolRef RootSym = cast<SymbolCast>(SE)->getOperand();
1059 QualType RT = RootSym->getType().getCanonicalType();
1060
1061 // FIXME support simplification from non-integers.
1062 if (!RT->isIntegralOrEnumerationType())
1063 return VB.makeNonLoc(SE, T, CastTy);
1064
1065 BasicValueFactory &BVF = VB.getBasicValueFactory();
1066 APSIntType CTy = BVF.getAPSIntType(CastTy);
1067 APSIntType TTy = BVF.getAPSIntType(T);
1068
1069 const auto WC = CTy.getBitWidth();
1070 const auto WT = TTy.getBitWidth();
1071
1072 if (WC <= WT) {
1073 const bool isSameType = (RT == CastTy);
1074 if (isSameType)
1075 return nonloc::SymbolVal(RootSym);
1076 return VB.makeNonLoc(RootSym, RT, CastTy);
1077 }
1078
1079 APSIntType RTy = BVF.getAPSIntType(RT);
1080 const auto WR = RTy.getBitWidth();
1081 const bool UT = TTy.isUnsigned();
1082 const bool UR = RTy.isUnsigned();
1083
1084 if (((WT > WR) && (UR || !UT)) || ((WT == WR) && (UT == UR)))
1085 return VB.makeNonLoc(RootSym, RT, CastTy);
1086
1087 return VB.makeNonLoc(SE, T, CastTy);
1088 }
1089};
1090} // end anonymous namespace
1091
1092/// Cast a given SVal to another SVal using given QualType's.
1093/// \param V -- SVal that should be casted.
1094/// \param CastTy -- QualType that V should be casted according to.
1095/// \param OriginalTy -- QualType which is associated to V. It provides
1096/// additional information about what type the cast performs from.
1097/// \returns the most appropriate casted SVal.
1098/// Note: Many cases don't use an exact OriginalTy. It can be extracted
1099/// from SVal or the cast can performs unconditionaly. Always pass OriginalTy!
1100/// It can be crucial in certain cases and generates different results.
1101/// FIXME: If `OriginalTy.isNull()` is true, then cast performs based on CastTy
1102/// only. This behavior is uncertain and should be improved.
1104 EvalCastVisitor TRV{*this, CastTy, OriginalTy};
1105 return TRV.Visit(V);
1106}
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
BinaryOperatorKind Opcode
Definition Expr.h:4046
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4678
const Stmt * getStmt() const
Definition CFG.h:140
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:2132
QualType getThisType() const
Return the type of the this pointer.
Definition DeclCXX.cpp:2856
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This represents one expression.
Definition Expr.h:112
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:287
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3093
@ NPC_ValueDependentIsNotNull
Specifies that a value-dependent expression should be considered to never be a null pointer constant.
Definition Expr.h:838
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:4075
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2018
This represents a decl that may have a name.
Definition Decl.h:274
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1004
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8440
QualType getCanonicalType() const
Definition TypeBase.h:8492
The collection of all-type qualifiers we support.
Definition TypeBase.h:331
void removeCVRQualifiers(unsigned mask)
Definition TypeBase.h:495
It represents a stack frame of the call stack.
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
StmtClass getStmtClass() const
Definition Stmt.h:1503
bool isBlockPointerType() const
Definition TypeBase.h:8697
bool isVoidType() const
Definition TypeBase.h:9043
bool isBooleanType() const
Definition TypeBase.h:9180
bool isIntegralOrUnscopedEnumerationType() const
Determine whether this type is an integral or unscoped enumeration type.
Definition Type.cpp:2174
bool isVoidPointerType() const
Definition Type.cpp:750
bool isFunctionPointerType() const
Definition TypeBase.h:8744
bool isPointerType() const
Definition TypeBase.h:8677
bool isReferenceType() const
Definition TypeBase.h:8701
bool isVariableArrayType() const
Definition TypeBase.h:8788
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:790
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9165
bool isMemberPointerType() const
Definition TypeBase.h:8758
bool isFunctionType() const
Definition TypeBase.h:8673
bool isFloatingType() const
Definition Type.cpp:2390
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:2333
UnaryOperatorKind Opcode
Definition Expr.h:2261
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:505
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:658
BlockDataRegion - A region that represents a block instance.
Definition MemRegion.h:705
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:216
static bool isLocType(QualType T)
Definition SVals.h:262
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:56
bool isUndef() const
Definition SVals.h:107
bool isUnknownOrUndef() const
Definition SVals.h:109
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:87
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:83
bool isUnknown() const
Definition SVals.h:105
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:562
virtual QualType getValueType() const =0
Represents a symbolic expression involving a unary operator.
Value representing integer constant.
Definition SVals.h:300
APSIntPtr getValue() const
Definition SVals.h:304
Value representing pointer-to-member.
Definition SVals.h:434
Represents symbolic expression that isn't a location.
Definition SVals.h:279
LLVM_ATTRIBUTE_RETURNS_NONNULL SymbolRef getSymbol() const
Definition SVals.h:288
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.
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1235
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:905
U cast(CodeGen::Address addr)
Definition Address.h:327
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648