clang 23.0.0git
CallEvent.cpp
Go to the documentation of this file.
1//===- CallEvent.cpp - Wrapper for all function and method calls ----------===//
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/// \file This file defines CallEvent and its subclasses, which represent path-
10/// sensitive instances of different kinds of function and method calls
11/// (C, C++, and Objective-C).
12//
13//===----------------------------------------------------------------------===//
14
17#include "clang/AST/Attr.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclBase.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclObjC.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/ExprCXX.h"
24#include "clang/AST/ExprObjC.h"
25#include "clang/AST/ParentMap.h"
26#include "clang/AST/Stmt.h"
27#include "clang/AST/Type.h"
29#include "clang/Analysis/CFG.h"
34#include "clang/Basic/LLVM.h"
49#include "llvm/ADT/ArrayRef.h"
50#include "llvm/ADT/DenseMap.h"
51#include "llvm/ADT/ImmutableList.h"
52#include "llvm/ADT/PointerIntPair.h"
53#include "llvm/ADT/SmallSet.h"
54#include "llvm/ADT/SmallVector.h"
55#include "llvm/ADT/StringExtras.h"
56#include "llvm/ADT/StringRef.h"
57#include "llvm/Support/Compiler.h"
58#include "llvm/Support/Debug.h"
59#include "llvm/Support/ErrorHandling.h"
60#include "llvm/Support/raw_ostream.h"
61#include <cassert>
62#include <optional>
63#include <utility>
64
65#define DEBUG_TYPE "static-analyzer-call-event"
66
67using namespace clang;
68using namespace ento;
69
71 ASTContext &Ctx = getState()->getStateManager().getContext();
72 const Expr *E = getOriginExpr();
73 if (!E)
74 return Ctx.VoidTy;
75 return Ctx.getReferenceQualifiedType(E);
76}
77
78static bool isCallback(QualType T) {
79 // If a parameter is a block or a callback, assume it can modify pointer.
80 if (T->isBlockPointerType() ||
81 T->isFunctionPointerType() ||
82 T->isObjCSelType())
83 return true;
84
85 // Check if a callback is passed inside a struct (for both, struct passed by
86 // reference and by value). Dig just one level into the struct for now.
87
88 if (T->isAnyPointerType() || T->isReferenceType())
89 T = T->getPointeeType();
90
91 if (const RecordType *RT = T->getAsStructureType()) {
92 const RecordDecl *RD = RT->getDecl()->getDefinitionOrSelf();
93 for (const auto *I : RD->fields()) {
94 QualType FieldT = I->getType();
95 if (FieldT->isBlockPointerType() || FieldT->isFunctionPointerType())
96 return true;
97 }
98 }
99 return false;
100}
101
103 if (const auto *PT = T->getAs<PointerType>()) {
104 QualType PointeeTy = PT->getPointeeType();
105 if (PointeeTy.isConstQualified())
106 return false;
107 return PointeeTy->isVoidType();
108 } else
109 return false;
110}
111
113 unsigned NumOfArgs = getNumArgs();
114
115 // If calling using a function pointer, assume the function does not
116 // satisfy the callback.
117 // TODO: We could check the types of the arguments here.
118 if (!getDecl())
119 return false;
120
121 unsigned Idx = 0;
123 E = param_type_end();
124 I != E && Idx < NumOfArgs; ++I, ++Idx) {
125 // If the parameter is 0, it's harmless.
126 if (getArgSVal(Idx).isZeroConstant())
127 continue;
128
129 if (Condition(*I))
130 return true;
131 }
132 return false;
133}
134
138
142
143bool CallEvent::isGlobalCFunction(StringRef FunctionName) const {
144 const auto *FD = dyn_cast_or_null<FunctionDecl>(getDecl());
145 if (!FD)
146 return false;
147
148 return CheckerContext::isCLibraryFunction(FD, FunctionName);
149}
150
152 const Decl *D = getDecl();
153 if (!D)
154 return nullptr;
155
157 LCtx->getAnalysisDeclContext()->getManager()->getContext(D);
158
159 return ADC;
160}
161
162const StackFrame *CallEvent::getCalleeStackFrame(unsigned BlockCount) const {
164 if (!ADC)
165 return nullptr;
166
167 const Expr *E = getOriginExpr();
168 if (!E)
169 return nullptr;
170
171 // Recover CFG block via reverse lookup.
172 // TODO: If we were to keep CFG element information as part of the CallEvent
173 // instead of doing this reverse lookup, we would be able to build the stack
174 // frame for non-expression-based calls, and also we wouldn't need the reverse
175 // lookup.
176 const CFGStmtMap *Map = LCtx->getAnalysisDeclContext()->getCFGStmtMap();
177 const CFGBlock *B = Map->getBlock(E);
178 assert(B);
179
180 // Also recover CFG index by scanning the CFG block.
181 unsigned Idx = 0, Sz = B->size();
182 for (; Idx < Sz; ++Idx)
183 if (auto StmtElem = (*B)[Idx].getAs<CFGStmt>())
184 if (StmtElem->getStmt() == E)
185 break;
186 assert(Idx < Sz);
187
188 return ADC->getStackFrame(LCtx, nullptr, E, B, BlockCount, Idx);
189}
190
191const ParamVarRegion
192*CallEvent::getParameterLocation(unsigned Index, unsigned BlockCount) const {
193 const StackFrame *SF = getCalleeStackFrame(BlockCount);
194 // We cannot construct a VarRegion without a stack frame.
195 if (!SF)
196 return nullptr;
197
198 const ParamVarRegion *PVR =
199 State->getStateManager().getRegionManager().getParamVarRegion(
200 getOriginExpr(), Index, SF);
201 return PVR;
202}
203
204/// Returns true if a type is a pointer-to-const or reference-to-const
205/// with no further indirection.
206static bool isPointerToConst(QualType Ty) {
207 QualType PointeeTy = Ty->getPointeeType();
208 if (PointeeTy == QualType())
209 return false;
210 if (!PointeeTy.isConstQualified())
211 return false;
212 if (PointeeTy->isAnyPointerType())
213 return false;
214 return true;
215}
216
217// Try to retrieve the function declaration and find the function parameter
218// types which are pointers/references to a non-pointer const.
219// We will not invalidate the corresponding argument regions.
220static void findPtrToConstParams(llvm::SmallSet<unsigned, 4> &PreserveArgs,
221 const CallEvent &Call) {
222 unsigned Idx = 0;
223 for (CallEvent::param_type_iterator I = Call.param_type_begin(),
224 E = Call.param_type_end();
225 I != E; ++I, ++Idx) {
226 if (isPointerToConst(*I))
227 PreserveArgs.insert(Idx);
228 }
229}
230
232 if (const auto *CtorCall = dyn_cast<CXXConstructorCall>(&Call)) {
233 if (const MemRegion *R = CtorCall->getCXXThisVal().getAsRegion())
234 return R->getBaseRegion();
235 }
236 return nullptr;
237}
238
240 ProgramStateRef State) const {
241 // Don't invalidate anything if the callee is marked pure/const.
242 if (const Decl *Callee = getDecl())
243 if (Callee->hasAttr<PureAttr>() || Callee->hasAttr<ConstAttr>())
244 return State;
245
246 SmallVector<SVal, 8> ValuesToInvalidate;
248
249 getExtraInvalidatedValues(ValuesToInvalidate, &ETraits);
250
251 // Indexes of arguments whose values will be preserved by the call.
252 llvm::SmallSet<unsigned, 4> PreserveArgs;
253 if (!argumentsMayEscape())
254 findPtrToConstParams(PreserveArgs, *this);
255
256 // We should not preserve the contents of the region pointed by "this" when
257 // constructing the object, even if an argument refers to it.
258 const auto *ThisRegionBaseOrNull = getThisRegionBaseOrNull(*this);
259
260 for (unsigned Idx = 0, Count = getNumArgs(); Idx != Count; ++Idx) {
261 // Mark this region for invalidation. We batch invalidate regions
262 // below for efficiency.
263 if (PreserveArgs.count(Idx)) {
264 if (const MemRegion *ArgBaseR = getArgSVal(Idx).getAsRegion()) {
265 ArgBaseR = ArgBaseR->getBaseRegion();
266
267 // Preserve the contents of the pointee of the argument - except if it
268 // refers to the object under construction (ctor call).
269 if (ArgBaseR != ThisRegionBaseOrNull) {
270 ETraits.setTrait(
272 // TODO: Factor this out + handle the lower level const pointers.
273 }
274 }
275 }
276
277 ValuesToInvalidate.push_back(getArgSVal(Idx));
278
279 // If a function accepts an object by argument (which would of course be a
280 // temporary that isn't lifetime-extended), invalidate the object itself,
281 // not only other objects reachable from it. This is necessary because the
282 // destructor has access to the temporary object after the call.
283 // TODO: Support placement arguments once we start
284 // constructing them directly.
285 // TODO: This is unnecessary when there's no destructor, but that's
286 // currently hard to figure out.
287 if (getKind() != CE_CXXAllocator)
289 if (auto AdjIdx = getAdjustedParameterIndex(Idx))
290 if (const TypedValueRegion *TVR =
291 getParameterLocation(*AdjIdx, BlockCount))
292 ValuesToInvalidate.push_back(loc::MemRegionVal(TVR));
293 }
294
295 // Invalidate designated regions using the batch invalidation API.
296 // NOTE: Even if RegionsToInvalidate is empty, we may still invalidate
297 // global variables.
298 return State->invalidateRegions(ValuesToInvalidate, getCFGElementRef(),
299 BlockCount, getLocationContext(),
300 /*CausedByPointerEscape*/ true,
301 /*Symbols=*/nullptr, this, &ETraits);
302}
303
305 const ProgramPointTag *Tag) const {
306
307 if (const Expr *E = getOriginExpr()) {
308 if (IsPreVisit)
309 return PreStmt(E, getLocationContext(), Tag);
310 return PostStmt(E, getLocationContext(), Tag);
311 }
312
313 const Decl *D = getDecl();
314 assert(D && "Cannot get a program point without a statement or decl");
315 assert(ElemRef.getParent() &&
316 "Cannot get a program point without a CFGElementRef");
317
319 if (IsPreVisit)
320 return PreImplicitCall(D, Loc, getLocationContext(), ElemRef, Tag);
321 return PostImplicitCall(D, Loc, getLocationContext(), ElemRef, Tag);
322}
323
324SVal CallEvent::getArgSVal(unsigned Index) const {
325 const Expr *ArgE = getArgExpr(Index);
326 if (!ArgE)
327 return UnknownVal();
328 return getSVal(ArgE);
329}
330
332 const Expr *ArgE = getArgExpr(Index);
333 if (!ArgE)
334 return {};
335 return ArgE->getSourceRange();
336}
337
339 const Expr *E = getOriginExpr();
340 if (!E)
341 return UndefinedVal();
342 return getSVal(E);
343}
344
345LLVM_DUMP_METHOD void CallEvent::dump() const { dump(llvm::errs()); }
346
347void CallEvent::dump(raw_ostream &Out) const {
348 ASTContext &Ctx = getState()->getStateManager().getContext();
349 if (const Expr *E = getOriginExpr()) {
350 E->printPretty(Out, nullptr, Ctx.getPrintingPolicy());
351 return;
352 }
353
354 if (const Decl *D = getDecl()) {
355 Out << "Call to ";
356 D->print(Out, Ctx.getPrintingPolicy());
357 return;
358 }
359
360 Out << "Unknown call (type " << getKindAsString() << ")";
361}
362
366
368 assert(D);
369 if (const auto *FD = dyn_cast<FunctionDecl>(D))
370 return FD->getReturnType();
371 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
372 return MD->getReturnType();
373 if (const auto *BD = dyn_cast<BlockDecl>(D)) {
374 // Blocks are difficult because the return type may not be stored in the
375 // BlockDecl itself. The AST should probably be enhanced, but for now we
376 // just do what we can.
377 // If the block is declared without an explicit argument list, the
378 // signature-as-written just includes the return type, not the entire
379 // function type.
380 // FIXME: All blocks should have signatures-as-written, even if the return
381 // type is inferred. (That's signified with a dependent result type.)
382 if (const TypeSourceInfo *TSI = BD->getSignatureAsWritten()) {
383 QualType Ty = TSI->getType();
384 if (const FunctionType *FT = Ty->getAs<FunctionType>())
385 Ty = FT->getReturnType();
386 if (!Ty->isDependentType())
387 return Ty;
388 }
389
390 return {};
391 }
392
393 llvm_unreachable("unknown callable kind");
394}
395
397 assert(D);
398
399 if (const auto *FD = dyn_cast<FunctionDecl>(D))
400 return FD->isVariadic();
401 if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
402 return MD->isVariadic();
403 if (const auto *BD = dyn_cast<BlockDecl>(D))
404 return BD->isVariadic();
405
406 llvm_unreachable("unknown callable kind");
407}
408
410 const RecordType *UT = T->getAsUnionType();
411 return UT &&
412 UT->getDecl()->getMostRecentDecl()->hasAttr<TransparentUnionAttr>();
413}
414
415// In some cases, symbolic cases should be transformed before we associate
416// them with parameters. This function incapsulates such cases.
417static SVal processArgument(SVal Value, const Expr *ArgumentExpr,
418 const ParmVarDecl *Parameter, SValBuilder &SVB) {
419 QualType ParamType = Parameter->getType();
420 QualType ArgumentType = ArgumentExpr->getType();
421
422 // Transparent unions allow users to easily convert values of union field
423 // types into union-typed objects.
424 //
425 // Also, more importantly, they allow users to define functions with different
426 // different parameter types, substituting types matching transparent union
427 // field types with the union type itself.
428 //
429 // Here, we check specifically for latter cases and prevent binding
430 // field-typed values to union-typed regions.
431 if (isTransparentUnion(ParamType) &&
432 // Let's check that we indeed trying to bind different types.
433 !isTransparentUnion(ArgumentType)) {
435
436 llvm::ImmutableList<SVal> CompoundSVals = BVF.getEmptySValList();
437 CompoundSVals = BVF.prependSVal(Value, CompoundSVals);
438
439 // Wrap it with compound value.
440 return SVB.makeCompoundVal(ParamType, CompoundSVals);
441 }
442
443 return Value;
444}
445
446/// Cast the argument value to the type of the parameter at the function
447/// declaration.
448/// Returns the argument value if it didn't need a cast.
449/// Or returns the cast argument if it needed a cast.
450/// Or returns 'Unknown' if it would need a cast but the callsite and the
451/// runtime definition don't match in terms of argument and parameter count.
452static SVal castArgToParamTypeIfNeeded(const CallEvent &Call, unsigned ArgIdx,
453 SVal ArgVal, SValBuilder &SVB) {
454 const auto *CallExprDecl = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
455 if (!CallExprDecl)
456 return ArgVal;
457
458 const FunctionDecl *Definition = CallExprDecl;
459 Definition->hasBody(Definition);
460
461 // The function decl of the Call (in the AST) will not have any parameter
462 // declarations, if it was 'only' declared without a prototype. However, the
463 // engine will find the appropriate runtime definition - basically a
464 // redeclaration, which has a function body (and a function prototype).
465 if (CallExprDecl->hasPrototype() || !Definition->hasPrototype())
466 return ArgVal;
467
468 // Only do this cast if the number arguments at the callsite matches with
469 // the parameters at the runtime definition.
470 if (Call.getNumArgs() != Definition->getNumParams())
471 return UnknownVal();
472
473 const Expr *ArgExpr = Call.getArgExpr(ArgIdx);
474 const ParmVarDecl *Param = Definition->getParamDecl(ArgIdx);
475 return SVB.evalCast(ArgVal, Param->getType(), ArgExpr->getType());
476}
477
478static void addParameterValuesToBindings(const StackFrame *CalleeSF,
480 SValBuilder &SVB,
481 const CallEvent &Call,
482 ArrayRef<ParmVarDecl *> parameters) {
483 MemRegionManager &MRMgr = SVB.getRegionManager();
484
485 // If the function has fewer parameters than the call has arguments, we simply
486 // do not bind any values to them.
487 unsigned NumArgs = Call.getNumArgs();
488 unsigned Idx = 0;
489 ArrayRef<ParmVarDecl*>::iterator I = parameters.begin(), E = parameters.end();
490 for (; I != E && Idx < NumArgs; ++I, ++Idx) {
491 assert(*I && "Formal parameter has no decl?");
492
493 // TODO: Support allocator calls.
494 if (Call.getKind() != CE_CXXAllocator)
495 if (Call.isArgumentConstructedDirectly(Call.getASTArgumentIndex(Idx)))
496 continue;
497
498 // TODO: Allocators should receive the correct size and possibly alignment,
499 // determined in compile-time but not represented as arg-expressions,
500 // which makes getArgSVal() fail and return UnknownVal.
501 SVal ArgVal = Call.getArgSVal(Idx);
502 const Expr *ArgExpr = Call.getArgExpr(Idx);
503
504 if (ArgVal.isUnknown())
505 continue;
506
507 // Cast the argument value to match the type of the parameter in some
508 // edge-cases.
509 ArgVal = castArgToParamTypeIfNeeded(Call, Idx, ArgVal, SVB);
510
511 Loc ParamLoc = SVB.makeLoc(
512 MRMgr.getParamVarRegion(Call.getOriginExpr(), Idx, CalleeSF));
513 Bindings.push_back(
514 std::make_pair(ParamLoc, processArgument(ArgVal, ArgExpr, *I, SVB)));
515 }
516
517 // FIXME: Variadic arguments are not handled at all right now.
518}
519
522 if (!StackFrame)
523 return nullptr;
524
526 if (const auto Ctor = Element.getAs<CFGConstructor>()) {
527 return Ctor->getConstructionContext();
528 }
529
530 if (const auto RecCall = Element.getAs<CFGCXXRecordTypedCall>()) {
531 return RecCall->getConstructionContext();
532 }
533
534 return nullptr;
535}
536
538 const auto *CallLocationContext = this->getLocationContext();
539 if (!CallLocationContext || CallLocationContext->inTopFrame())
540 return nullptr;
541
542 const auto *CallSF = CallLocationContext->getStackFrame();
543 if (!CallSF)
544 return nullptr;
545
546 CallEventManager &CEMgr = State->getStateManager().getCallEventManager();
547 return CEMgr.getCaller(CallSF, State);
548}
549
551 if (const CallEventRef<> Caller = getCaller())
552 return Caller->isInSystemHeader();
553
554 return false;
555}
556
558 const auto *CC = getConstructionContext();
559 if (!CC)
560 return std::nullopt;
561
562 EvalCallOptions CallOpts;
563 ExprEngine &Engine = getState()->getStateManager().getOwningEngine();
564 unsigned NumVisitedCall = Engine.getNumVisited(
565 getLocationContext(), getCFGElementRef().getParent());
566 SVal RetVal = Engine.computeObjectUnderConstruction(
567 getOriginExpr(), getState(), NumVisitedCall, getLocationContext(), CC,
568 CallOpts);
569 return RetVal;
570}
571
573 const FunctionDecl *D = getDecl();
574 if (!D)
575 return {};
576 return D->parameters();
577}
578
580 const FunctionDecl *FD = getDecl();
581 if (!FD)
582 return {};
583
584 // Note that the AnalysisDeclContext will have the FunctionDecl with
585 // the definition (if one exists).
588 getManager()->getContext(FD);
589 bool IsAutosynthesized;
590 Stmt* Body = AD->getBody(IsAutosynthesized);
591 LLVM_DEBUG({
592 if (IsAutosynthesized)
593 llvm::dbgs() << "Using autosynthesized body for " << FD->getName()
594 << "\n";
595 });
596
597 ExprEngine &Engine = getState()->getStateManager().getOwningEngine();
599 *Engine.getCrossTranslationUnitContext();
600
601 AnalyzerOptions &Opts = Engine.getAnalysisManager().options;
602
603 if (Body) {
604 const Decl* Decl = AD->getDecl();
605 if (Opts.IsNaiveCTUEnabled && CTUCtx.isImportedAsNew(Decl)) {
606 // A newly created definition, but we had error(s) during the import.
607 if (CTUCtx.hasError(Decl))
608 return {};
609 return RuntimeDefinition(Decl, /*Foreign=*/true);
610 }
611 return RuntimeDefinition(Decl, /*Foreign=*/false);
612 }
613
614 // Try to get CTU definition only if CTUDir is provided.
615 if (!Opts.IsNaiveCTUEnabled)
616 return {};
617
619 CTUCtx.getCrossTUDefinition(FD, Opts.CTUDir, Opts.CTUIndexName,
620 Opts.DisplayCTUProgress);
621
622 if (!CTUDeclOrError) {
623 handleAllErrors(CTUDeclOrError.takeError(),
624 [&](const cross_tu::IndexError &IE) {
625 auto Loc = getOriginExpr() ? getOriginExpr()->getExprLoc()
626 : FD->getLocation();
627 CTUCtx.emitCrossTUDiagnostics(IE, Loc);
628 });
629 return {};
630 }
631
632 return RuntimeDefinition(*CTUDeclOrError, /*Foreign=*/true);
633}
634
636 BindingsTy &Bindings) const {
637 const auto *D = cast<FunctionDecl>(CalleeSF->getDecl());
638 SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
639 addParameterValuesToBindings(CalleeSF, Bindings, SVB, *this, D->parameters());
640}
641
644 return true;
645
646 const FunctionDecl *D = getDecl();
647 if (!D)
648 return true;
649
650 const IdentifierInfo *II = D->getIdentifier();
651 if (!II)
652 return false;
653
654 // This set of "escaping" APIs is
655
656 // - 'int pthread_setspecific(ptheread_key k, const void *)' stores a
657 // value into thread local storage. The value can later be retrieved with
658 // 'void *ptheread_getspecific(pthread_key)'. So even thought the
659 // parameter is 'const void *', the region escapes through the call.
660 if (II->isStr("pthread_setspecific"))
661 return true;
662
663 // - xpc_connection_set_context stores a value which can be retrieved later
664 // with xpc_connection_get_context.
665 if (II->isStr("xpc_connection_set_context"))
666 return true;
667
668 // - funopen - sets a buffer for future IO calls.
669 if (II->isStr("funopen"))
670 return true;
671
672 // - __cxa_demangle - can reallocate memory and can return the pointer to
673 // the input buffer.
674 if (II->isStr("__cxa_demangle"))
675 return true;
676
677 StringRef FName = II->getName();
678
679 // - CoreFoundation functions that end with "NoCopy" can free a passed-in
680 // buffer even if it is const.
681 if (FName.ends_with("NoCopy"))
682 return true;
683
684 // - NSXXInsertXX, for example NSMapInsertIfAbsent, since they can
685 // be deallocated by NSMapRemove.
686 if (FName.starts_with("NS") && FName.contains("Insert"))
687 return true;
688
689 // - Many CF containers allow objects to escape through custom
690 // allocators/deallocators upon container construction. (PR12101)
691 if (FName.starts_with("CF") || FName.starts_with("CG")) {
692 return FName.contains_insensitive("InsertValue") ||
693 FName.contains_insensitive("AddValue") ||
694 FName.contains_insensitive("SetValue") ||
695 FName.contains_insensitive("WithData") ||
696 FName.contains_insensitive("AppendValue") ||
697 FName.contains_insensitive("SetAttribute");
698 }
699
700 return false;
701}
702
705 if (D)
706 return D;
707
709}
710
712 // Clang converts lambdas to function pointers using an implicit conversion
713 // operator, which returns the lambda's '__invoke' method. However, Sema
714 // leaves the body of '__invoke' empty (it is generated later in CodeGen), so
715 // we need to skip '__invoke' and access the lambda's operator() directly.
716 if (const auto *CMD = dyn_cast_if_present<CXXMethodDecl>(getDecl());
717 CMD && CMD->isLambdaStaticInvoker())
718 return RuntimeDefinition{CMD->getParent()->getLambdaCallOperator()};
719
721}
722
724 const auto *CE = cast_or_null<CallExpr>(getOriginExpr());
725 if (!CE)
727
728 const FunctionDecl *D = CE->getDirectCallee();
729 if (D)
730 return D;
731
732 return getSVal(CE->getCallee()).getAsFunctionDecl();
733}
734
736 ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
737 SVal ThisVal = getCXXThisVal();
738 Values.push_back(ThisVal);
739
740 // Don't invalidate if the method is const and there are no mutable fields.
741 if (const auto *D = cast_or_null<CXXMethodDecl>(getDecl())) {
742 if (!D->isConst())
743 return;
744
745 // Get the record decl for the class of 'This'. D->getParent() may return
746 // a base class decl, rather than the class of the instance which needs to
747 // be checked for mutable fields.
748 const CXXRecordDecl *ParentRecord = getDeclForDynamicType().first;
749 if (!ParentRecord || !ParentRecord->hasDefinition())
750 return;
751
752 if (ParentRecord->hasMutableFields())
753 return;
754
755 // Preserve CXXThis.
756 const MemRegion *ThisRegion = ThisVal.getAsRegion();
757 if (!ThisRegion)
758 return;
759
760 ETraits->setTrait(ThisRegion->getBaseRegion(),
762 }
763}
764
766 const Expr *Base = getCXXThisExpr();
767 // FIXME: This doesn't handle an overloaded ->* operator.
768 SVal ThisVal = Base ? getSVal(Base) : UnknownVal();
769
770 if (isa<NonLoc>(ThisVal)) {
771 SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
772 QualType OriginalTy = ThisVal.getType(SVB.getContext());
773 return SVB.evalCast(ThisVal, Base->getType(), OriginalTy);
774 }
775
776 assert(ThisVal.isUnknownOrUndef() || isa<Loc>(ThisVal));
777 return ThisVal;
778}
779
780std::pair<const CXXRecordDecl *, bool>
782 const MemRegion *R = getCXXThisVal().getAsRegion();
783 if (!R)
784 return {};
785
787 if (!DynType.isValid())
788 return {};
789
790 assert(!DynType.getType()->getPointeeType().isNull());
791 return {DynType.getType()->getPointeeCXXRecordDecl(),
792 DynType.canBeASubClass()};
793}
794
796 // Do we have a decl at all?
797 const Decl *D = getDecl();
798 if (!D)
799 return {};
800
801 // If the method is non-virtual, we know we can inline it.
802 const auto *MD = cast<CXXMethodDecl>(D);
803 if (!MD->isVirtual())
805
806 auto [RD, CanBeSubClass] = getDeclForDynamicType();
807 if (!RD || !RD->hasDefinition())
808 return {};
809
810 // Find the decl for this method in that class.
811 const CXXMethodDecl *Result = MD->getCorrespondingMethodInClass(RD, true);
812 if (!Result) {
813 // We might not even get the original statically-resolved method due to
814 // some particularly nasty casting (e.g. casts to sister classes).
815 // However, we should at least be able to search up and down our own class
816 // hierarchy, and some real bugs have been caught by checking this.
817 assert(!RD->isDerivedFrom(MD->getParent()) && "Couldn't find known method");
818
819 // FIXME: This is checking that our DynamicTypeInfo is at least as good as
820 // the static type. However, because we currently don't update
821 // DynamicTypeInfo when an object is cast, we can't actually be sure the
822 // DynamicTypeInfo is up to date. This assert should be re-enabled once
823 // this is fixed.
824 //
825 // assert(!MD->getParent()->isDerivedFrom(RD) && "Bad DynamicTypeInfo");
826
827 return {};
828 }
829
830 // Does the decl that we found have an implementation?
832 if (!Result->hasBody(Definition)) {
833 if (!CanBeSubClass)
835 return {};
836 }
837
838 // We found a definition. If we're not sure that this devirtualization is
839 // actually what will happen at runtime, make sure to provide the region so
840 // that ExprEngine can decide what to do with it.
841 if (CanBeSubClass)
843 getCXXThisVal().getAsRegion()->StripCasts());
844 return RuntimeDefinition(Definition, /*DispatchRegion=*/nullptr);
845}
846
848 BindingsTy &Bindings) const {
850
851 // Handle the binding of 'this' in the new stack frame.
852 SVal ThisVal = getCXXThisVal();
853 if (!ThisVal.isUnknown()) {
854 ProgramStateManager &StateMgr = getState()->getStateManager();
855 SValBuilder &SVB = StateMgr.getSValBuilder();
856
857 const auto *MD = cast<CXXMethodDecl>(CalleeSF->getDecl());
858 Loc ThisLoc = SVB.getCXXThis(MD, CalleeSF);
859
860 // If we devirtualized to a different member function, we need to make sure
861 // we have the proper layering of CXXBaseObjectRegions.
862 if (MD->getCanonicalDecl() != getDecl()->getCanonicalDecl()) {
863 ASTContext &Ctx = SVB.getContext();
864 const CXXRecordDecl *Class = MD->getParent();
866
867 // FIXME: CallEvent maybe shouldn't be directly accessing StoreManager.
868 std::optional<SVal> V =
869 StateMgr.getStoreManager().evalBaseToDerived(ThisVal, Ty);
870 if (!V) {
871 // We might have suffered some sort of placement new earlier, so
872 // we're constructing in a completely unexpected storage.
873 // Fall back to a generic pointer cast for this-value.
874 const CXXMethodDecl *StaticMD = cast<CXXMethodDecl>(getDecl());
875 const CXXRecordDecl *StaticClass = StaticMD->getParent();
876 CanQualType StaticTy =
877 Ctx.getPointerType(Ctx.getCanonicalTagType(StaticClass));
878 ThisVal = SVB.evalCast(ThisVal, Ty, StaticTy);
879 } else
880 ThisVal = *V;
881 }
882
883 if (!ThisVal.isUnknown())
884 Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
885 }
886}
887
891
893 // C++11 [expr.call]p1: ...If the selected function is non-virtual, or if the
894 // id-expression in the class member access expression is a qualified-id,
895 // that function is called. Otherwise, its final overrider in the dynamic type
896 // of the object expression is called.
897 if (const auto *ME = dyn_cast<MemberExpr>(getOriginExpr()->getCallee()))
898 if (ME->hasQualifier())
900
902}
903
905 return getOriginExpr()->getArg(0);
906}
907
909 const Expr *Callee = getOriginExpr()->getCallee();
910 const MemRegion *DataReg = getSVal(Callee).getAsRegion();
911
912 return dyn_cast_or_null<BlockDataRegion>(DataReg);
913}
914
916 const BlockDecl *D = getDecl();
917 if (!D)
918 return {};
919 return D->parameters();
920}
921
923 RegionAndSymbolInvalidationTraits *ETraits) const {
924 // FIXME: This also needs to invalidate captured globals.
925 if (const MemRegion *R = getBlockRegion())
926 Values.push_back(loc::MemRegionVal(R));
927}
928
930 BindingsTy &Bindings) const {
931 SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
934 auto *LambdaOperatorDecl = cast<CXXMethodDecl>(CalleeSF->getDecl());
935 Params = LambdaOperatorDecl->parameters();
936
937 // For blocks converted from a C++ lambda, the callee declaration is the
938 // operator() method on the lambda so we bind "this" to
939 // the lambda captured by the block.
940 const VarRegion *CapturedLambdaRegion = getRegionStoringCapturedLambda();
941 SVal ThisVal = loc::MemRegionVal(CapturedLambdaRegion);
942 Loc ThisLoc = SVB.getCXXThis(LambdaOperatorDecl, CalleeSF);
943 Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
944 } else {
945 Params = cast<BlockDecl>(CalleeSF->getDecl())->parameters();
946 }
947
948 addParameterValuesToBindings(CalleeSF, Bindings, SVB, *this, Params);
949}
950
952 if (Data)
953 return loc::MemRegionVal(static_cast<const MemRegion *>(Data));
954 return UnknownVal();
955}
956
958 RegionAndSymbolInvalidationTraits *ETraits) const {
959 SVal V = getCXXThisVal();
960 if (SymbolRef Sym = V.getAsSymbol(true))
961 ETraits->setTrait(Sym,
963
964 // Standard classes don't reinterpret-cast and modify super regions.
965 const bool IsStdClassCtor = isWithinStdNamespace(getDecl());
966 if (const MemRegion *Obj = V.getAsRegion(); Obj && IsStdClassCtor) {
967 ETraits->setTrait(
969 }
970
971 Values.push_back(V);
972}
973
975 const StackFrame *CalleeSF, BindingsTy &Bindings) const {
977
978 SVal ThisVal = getCXXThisVal();
979 if (!ThisVal.isUnknown()) {
980 SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
981 const auto *MD = cast<CXXMethodDecl>(CalleeSF->getDecl());
982 Loc ThisLoc = SVB.getCXXThis(MD, CalleeSF);
983 Bindings.push_back(std::make_pair(ThisLoc, ThisVal));
984 }
985}
986
993
995 if (Data)
996 return loc::MemRegionVal(DtorDataTy::getFromOpaqueValue(Data).getPointer());
997 return UnknownVal();
998}
999
1001 // Base destructors are always called non-virtually.
1002 // Skip CXXInstanceCall's devirtualization logic in this case.
1003 if (isBaseDestructor())
1005
1007}
1008
1010 const ObjCMethodDecl *D = getDecl();
1011 if (!D)
1012 return {};
1013 return D->parameters();
1014}
1015
1017 ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const {
1018
1019 // If the method call is a setter for property known to be backed by
1020 // an instance variable, don't invalidate the entire receiver, just
1021 // the storage for that instance variable.
1022 if (const ObjCPropertyDecl *PropDecl = getAccessedProperty()) {
1023 if (const ObjCIvarDecl *PropIvar = PropDecl->getPropertyIvarDecl()) {
1024 SVal IvarLVal = getState()->getLValue(PropIvar, getReceiverSVal());
1025 if (const MemRegion *IvarRegion = IvarLVal.getAsRegion()) {
1026 ETraits->setTrait(
1027 IvarRegion,
1029 ETraits->setTrait(
1030 IvarRegion,
1032 Values.push_back(IvarLVal);
1033 }
1034 return;
1035 }
1036 }
1037
1038 Values.push_back(getReceiverSVal());
1039}
1040
1042 // FIXME: Is this the best way to handle class receivers?
1043 if (!isInstanceMessage())
1044 return UnknownVal();
1045
1046 if (const Expr *RecE = getOriginExpr()->getInstanceReceiver())
1047 return getSVal(RecE);
1048
1049 // An instance message with no expression means we are sending to super.
1050 // In this case the object reference is the same as 'self'.
1051 assert(getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance);
1052 SVal SelfVal = getState()->getSelfSVal(getLocationContext());
1053 assert(SelfVal.isValid() && "Calling super but not in ObjC method");
1054 return SelfVal;
1055}
1056
1058 if (getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperInstance ||
1059 getOriginExpr()->getReceiverKind() == ObjCMessageExpr::SuperClass)
1060 return true;
1061
1062 if (!isInstanceMessage())
1063 return false;
1064
1065 SVal RecVal = getSVal(getOriginExpr()->getInstanceReceiver());
1066 SVal SelfVal = getState()->getSelfSVal(getLocationContext());
1067
1068 return (RecVal == SelfVal);
1069}
1070
1072 switch (getMessageKind()) {
1073 case OCM_Message:
1074 return getOriginExpr()->getSourceRange();
1075 case OCM_PropertyAccess:
1076 case OCM_Subscript:
1077 return getContainingPseudoObjectExpr()->getSourceRange();
1078 }
1079 llvm_unreachable("unknown message kind");
1080}
1081
1082using ObjCMessageDataTy = llvm::PointerIntPair<const PseudoObjectExpr *, 2>;
1083
1084const PseudoObjectExpr *ObjCMethodCall::getContainingPseudoObjectExpr() const {
1085 assert(Data && "Lazy lookup not yet performed.");
1086 assert(getMessageKind() != OCM_Message && "Explicit message send.");
1087 return ObjCMessageDataTy::getFromOpaqueValue(Data).getPointer();
1088}
1089
1090static const Expr *
1092 const Expr *Syntactic = POE->getSyntacticForm()->IgnoreParens();
1093
1094 // This handles the funny case of assigning to the result of a getter.
1095 // This can happen if the getter returns a non-const reference.
1096 if (const auto *BO = dyn_cast<BinaryOperator>(Syntactic))
1097 Syntactic = BO->getLHS()->IgnoreParens();
1098
1099 return Syntactic;
1100}
1101
1103 if (!Data) {
1104 // Find the parent, ignoring implicit casts.
1105 const ParentMap &PM = getLocationContext()->getParentMap();
1107
1108 // Check if parent is a PseudoObjectExpr.
1109 if (const auto *POE = dyn_cast_or_null<PseudoObjectExpr>(S)) {
1110 const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
1111
1113 switch (Syntactic->getStmtClass()) {
1114 case Stmt::ObjCPropertyRefExprClass:
1116 break;
1117 case Stmt::ObjCSubscriptRefExprClass:
1118 K = OCM_Subscript;
1119 break;
1120 default:
1121 // FIXME: Can this ever happen?
1122 K = OCM_Message;
1123 break;
1124 }
1125
1126 if (K != OCM_Message) {
1127 const_cast<ObjCMethodCall *>(this)->Data
1128 = ObjCMessageDataTy(POE, K).getOpaqueValue();
1129 assert(getMessageKind() == K);
1130 return K;
1131 }
1132 }
1133
1134 const_cast<ObjCMethodCall *>(this)->Data
1135 = ObjCMessageDataTy(nullptr, 1).getOpaqueValue();
1136 assert(getMessageKind() == OCM_Message);
1137 return OCM_Message;
1138 }
1139
1140 ObjCMessageDataTy Info = ObjCMessageDataTy::getFromOpaqueValue(Data);
1141 if (!Info.getPointer())
1142 return OCM_Message;
1143 return static_cast<ObjCMessageKind>(Info.getInt());
1144}
1145
1147 // Look for properties accessed with property syntax (foo.bar = ...)
1149 const PseudoObjectExpr *POE = getContainingPseudoObjectExpr();
1150 assert(POE && "Property access without PseudoObjectExpr?");
1151
1152 const Expr *Syntactic = getSyntacticFromForPseudoObjectExpr(POE);
1153 auto *RefExpr = cast<ObjCPropertyRefExpr>(Syntactic);
1154
1155 if (RefExpr->isExplicitProperty())
1156 return RefExpr->getExplicitProperty();
1157 }
1158
1159 // Look for properties accessed with method syntax ([foo setBar:...]).
1160 const ObjCMethodDecl *MD = getDecl();
1161 if (!MD || !MD->isPropertyAccessor())
1162 return nullptr;
1163
1164 // Note: This is potentially quite slow.
1165 return MD->findPropertyDecl();
1166}
1167
1169 Selector Sel) const {
1170 assert(IDecl);
1171 AnalysisManager &AMgr =
1172 getState()->getStateManager().getOwningEngine().getAnalysisManager();
1173 // If the class interface is declared inside the main file, assume it is not
1174 // subcassed.
1175 // TODO: It could actually be subclassed if the subclass is private as well.
1176 // This is probably very rare.
1177 SourceLocation InterfLoc = IDecl->getEndOfDefinitionLoc();
1178 if (InterfLoc.isValid() && AMgr.isInCodeFile(InterfLoc))
1179 return false;
1180
1181 // Assume that property accessors are not overridden.
1183 return false;
1184
1185 // We assume that if the method is public (declared outside of main file) or
1186 // has a parent which publicly declares the method, the method could be
1187 // overridden in a subclass.
1188
1189 // Find the first declaration in the class hierarchy that declares
1190 // the selector.
1191 ObjCMethodDecl *D = nullptr;
1192 while (true) {
1193 D = IDecl->lookupMethod(Sel, true);
1194
1195 // Cannot find a public definition.
1196 if (!D)
1197 return false;
1198
1199 // If outside the main file,
1200 if (D->getLocation().isValid() && !AMgr.isInCodeFile(D->getLocation()))
1201 return true;
1202
1203 if (D->isOverriding()) {
1204 // Search in the superclass on the next iteration.
1205 IDecl = D->getClassInterface();
1206 if (!IDecl)
1207 return false;
1208
1209 IDecl = IDecl->getSuperClass();
1210 if (!IDecl)
1211 return false;
1212
1213 continue;
1214 }
1215
1216 return false;
1217 };
1218
1219 llvm_unreachable("The while loop should always terminate.");
1220}
1221
1223 if (!MD)
1224 return MD;
1225
1226 // Find the redeclaration that defines the method.
1227 if (!MD->hasBody()) {
1228 for (auto *I : MD->redecls())
1229 if (I->hasBody())
1230 MD = cast<ObjCMethodDecl>(I);
1231 }
1232 return MD;
1233}
1234
1240
1241namespace llvm {
1242template <> struct DenseMapInfo<PrivateMethodKey> {
1243 using InterfaceInfo = DenseMapInfo<const ObjCInterfaceDecl *>;
1244 using SelectorInfo = DenseMapInfo<Selector>;
1245
1247 return {InterfaceInfo::getEmptyKey(), SelectorInfo::getEmptyKey(), false};
1248 }
1249
1251 return {InterfaceInfo::getTombstoneKey(), SelectorInfo::getTombstoneKey(),
1252 true};
1253 }
1254
1255 static unsigned getHashValue(const PrivateMethodKey &Key) {
1256 return llvm::hash_combine(
1257 llvm::hash_code(InterfaceInfo::getHashValue(Key.Interface)),
1258 llvm::hash_code(SelectorInfo::getHashValue(Key.LookupSelector)),
1259 Key.IsClassMethod);
1260 }
1261
1262 static bool isEqual(const PrivateMethodKey &LHS,
1263 const PrivateMethodKey &RHS) {
1264 return InterfaceInfo::isEqual(LHS.Interface, RHS.Interface) &&
1265 SelectorInfo::isEqual(LHS.LookupSelector, RHS.LookupSelector) &&
1266 LHS.IsClassMethod == RHS.IsClassMethod;
1267 }
1268};
1269} // end namespace llvm
1270
1271// NOTE: This cache is a "global" variable, and it is cleared by
1272// CallEventManager's constructor so we do not keep old entries when
1273// loading/unloading ASTs. If we are worried about concurrency, we may need to
1274// revisit this someday. In terms of memory, this table stays around until clang
1275// quits, which also may be bad if we need to release memory.
1277 llvm::DenseMap<PrivateMethodKey, std::optional<const ObjCMethodDecl *>>;
1279
1280static const ObjCMethodDecl *
1282 Selector LookupSelector, bool InstanceMethod) {
1283 // Repeatedly calling lookupPrivateMethod() is expensive, especially
1284 // when in many cases it returns null. We cache the results so
1285 // that repeated queries on the same ObjCIntefaceDecl and Selector
1286 // don't incur the same cost. On some test cases, we can see the
1287 // same query being issued thousands of times.
1288 std::optional<const ObjCMethodDecl *> &Val =
1289 PrivateMethodCache[{Interface, LookupSelector, InstanceMethod}];
1290
1291 // Query lookupPrivateMethod() if the cache does not hit.
1292 if (!Val) {
1293 Val = Interface->lookupPrivateMethod(LookupSelector, InstanceMethod);
1294
1295 if (!*Val) {
1296 // Query 'lookupMethod' as a backup.
1297 Val = Interface->lookupMethod(LookupSelector, InstanceMethod);
1298 }
1299 }
1300
1301 return *Val;
1302}
1303
1305 const ObjCMessageExpr *E = getOriginExpr();
1306 assert(E);
1307 Selector Sel = E->getSelector();
1308
1309 if (E->isInstanceMessage()) {
1310 // Find the receiver type.
1311 const ObjCObjectType *ReceiverT = nullptr;
1312 bool CanBeSubClassed = false;
1313 bool LookingForInstanceMethod = true;
1314 QualType SupersType = E->getSuperType();
1315 const MemRegion *Receiver = nullptr;
1316
1317 if (!SupersType.isNull()) {
1318 // The receiver is guaranteed to be 'super' in this case.
1319 // Super always means the type of immediate predecessor to the method
1320 // where the call occurs.
1321 ReceiverT = cast<ObjCObjectPointerType>(SupersType)->getObjectType();
1322 } else {
1323 Receiver = getReceiverSVal().getAsRegion();
1324 if (!Receiver)
1325 return {};
1326
1327 DynamicTypeInfo DTI = getDynamicTypeInfo(getState(), Receiver);
1328 if (!DTI.isValid()) {
1329 assert(isa<AllocaRegion>(Receiver) &&
1330 "Unhandled untyped region class!");
1331 return {};
1332 }
1333
1334 QualType DynType = DTI.getType();
1335 CanBeSubClassed = DTI.canBeASubClass();
1336
1337 const auto *ReceiverDynT =
1338 dyn_cast<ObjCObjectPointerType>(DynType.getCanonicalType());
1339
1340 if (ReceiverDynT) {
1341 ReceiverT = ReceiverDynT->getObjectType();
1342
1343 // It can be actually class methods called with Class object as a
1344 // receiver. This type of messages is treated by the compiler as
1345 // instance (not class).
1346 if (ReceiverT->isObjCClass()) {
1347
1348 SVal SelfVal = getState()->getSelfSVal(getLocationContext());
1349 // For [self classMethod], return compiler visible declaration.
1350 if (Receiver == SelfVal.getAsRegion()) {
1352 }
1353
1354 // Otherwise, let's check if we know something about the type
1355 // inside of this class object.
1356 if (SymbolRef ReceiverSym = getReceiverSVal().getAsSymbol()) {
1357 DynamicTypeInfo DTI =
1359 if (DTI.isValid()) {
1360 // Let's use this type for lookup.
1361 ReceiverT =
1363
1364 CanBeSubClassed = DTI.canBeASubClass();
1365 // And it should be a class method instead.
1366 LookingForInstanceMethod = false;
1367 }
1368 }
1369 }
1370
1371 if (CanBeSubClassed)
1372 if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterface())
1373 // Even if `DynamicTypeInfo` told us that it can be
1374 // not necessarily this type, but its descendants, we still want
1375 // to check again if this selector can be actually overridden.
1376 CanBeSubClassed = canBeOverridenInSubclass(IDecl, Sel);
1377 }
1378 }
1379
1380 // Lookup the instance method implementation.
1381 if (ReceiverT)
1382 if (ObjCInterfaceDecl *IDecl = ReceiverT->getInterface()) {
1383 const ObjCMethodDecl *MD =
1384 lookupRuntimeDefinition(IDecl, Sel, LookingForInstanceMethod);
1385
1386 if (MD && !MD->hasBody())
1387 MD = MD->getCanonicalDecl();
1388
1389 if (CanBeSubClassed)
1390 return RuntimeDefinition(MD, Receiver);
1391 else
1392 return RuntimeDefinition(MD, nullptr);
1393 }
1394 } else {
1395 // This is a class method.
1396 // If we have type info for the receiver class, we are calling via
1397 // class name.
1398 if (ObjCInterfaceDecl *IDecl = E->getReceiverInterface()) {
1399 // Find/Return the method implementation.
1400 return RuntimeDefinition(IDecl->lookupPrivateClassMethod(Sel));
1401 }
1402 }
1403
1404 return {};
1405}
1406
1408 if (isInSystemHeader() && !isInstanceMessage()) {
1409 Selector Sel = getSelector();
1410 if (Sel.getNumArgs() == 1 &&
1411 Sel.getIdentifierInfoForSlot(0)->isStr("valueWithPointer"))
1412 return true;
1413 }
1414
1416}
1417
1419 BindingsTy &Bindings) const {
1420 const auto *D = cast<ObjCMethodDecl>(CalleeSF->getDecl());
1421 SValBuilder &SVB = getState()->getStateManager().getSValBuilder();
1422 addParameterValuesToBindings(CalleeSF, Bindings, SVB, *this, D->parameters());
1423
1424 SVal SelfVal = getReceiverSVal();
1425 if (!SelfVal.isUnknown()) {
1426 const VarDecl *SelfD = CalleeSF->getAnalysisDeclContext()->getSelfDecl();
1427 MemRegionManager &MRMgr = SVB.getRegionManager();
1428 Loc SelfLoc = SVB.makeLoc(MRMgr.getVarRegion(SelfD, CalleeSF));
1429 Bindings.push_back(std::make_pair(SelfLoc, SelfVal));
1430 }
1431}
1432
1433CallEventManager::CallEventManager(llvm::BumpPtrAllocator &alloc)
1434 : Alloc(alloc) {
1435 // Clear the method cache to avoid hits when multiple AST are loaded/unloaded
1436 // within a single process. This can happen with unit tests, for instance.
1437 PrivateMethodCache.clear();
1438}
1439
1442 const LocationContext *LCtx,
1444 if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(CE))
1445 return create<CXXMemberCall>(MCE, State, LCtx, ElemRef);
1446
1447 if (const auto *OpCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
1448 const FunctionDecl *DirectCallee = OpCE->getDirectCallee();
1449 if (const auto *MD = dyn_cast<CXXMethodDecl>(DirectCallee)) {
1450 if (MD->isImplicitObjectMemberFunction())
1451 return create<CXXMemberOperatorCall>(OpCE, State, LCtx, ElemRef);
1452 if (MD->isStatic())
1453 return create<CXXStaticOperatorCall>(OpCE, State, LCtx, ElemRef);
1454 }
1455
1456 } else if (CE->getCallee()->getType()->isBlockPointerType()) {
1457 return create<BlockCall>(CE, State, LCtx, ElemRef);
1458 }
1459
1460 // Otherwise, it's a normal function call, static member function call, or
1461 // something we can't reason about.
1462 return create<SimpleFunctionCall>(CE, State, LCtx, ElemRef);
1463}
1464
1466 ProgramStateRef State) {
1467 const LocationContext *ParentCtx = CalleeSF->getParent();
1468 const LocationContext *CallerCtx = ParentCtx->getStackFrame();
1469 CFGBlock::ConstCFGElementRef ElemRef = {CalleeSF->getCallSiteBlock(),
1470 CalleeSF->getIndex()};
1471 assert(CallerCtx && "This should not be used for top-level stack frames");
1472
1473 const Expr *CallSite = CalleeSF->getCallSite();
1474
1475 if (CallSite) {
1476 if (CallEventRef<> Out = getCall(CallSite, State, CallerCtx, ElemRef))
1477 return Out;
1478
1479 SValBuilder &SVB = State->getStateManager().getSValBuilder();
1480 const auto *Ctor = cast<CXXMethodDecl>(CalleeSF->getDecl());
1481 Loc ThisPtr = SVB.getCXXThis(Ctor, CalleeSF);
1482 SVal ThisVal = State->getSVal(ThisPtr);
1483
1484 if (const auto *CE = dyn_cast<CXXConstructExpr>(CallSite))
1485 return getCXXConstructorCall(CE, ThisVal.getAsRegion(), State, CallerCtx,
1486 ElemRef);
1487 if (const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(CallSite))
1488 return getCXXInheritedConstructorCall(CIE, ThisVal.getAsRegion(), State,
1489 CallerCtx, ElemRef);
1490 // All other cases are handled by getCall.
1491 llvm_unreachable("This is not an inlineable statement");
1492 }
1493
1494 // Fall back to the CFG. The only thing we haven't handled yet is
1495 // destructors, though this could change in the future.
1496 const CFGBlock *B = CalleeSF->getCallSiteBlock();
1497 CFGElement E = (*B)[CalleeSF->getIndex()];
1498 assert((E.getAs<CFGImplicitDtor>() || E.getAs<CFGTemporaryDtor>()) &&
1499 "All other CFG elements should have exprs");
1500
1501 SValBuilder &SVB = State->getStateManager().getSValBuilder();
1502 const auto *Dtor = cast<CXXDestructorDecl>(CalleeSF->getDecl());
1503 Loc ThisPtr = SVB.getCXXThis(Dtor, CalleeSF);
1504 SVal ThisVal = State->getSVal(ThisPtr);
1505
1506 const Stmt *Trigger;
1507 if (std::optional<CFGAutomaticObjDtor> AutoDtor =
1509 Trigger = AutoDtor->getTriggerStmt();
1510 else if (std::optional<CFGDeleteDtor> DeleteDtor = E.getAs<CFGDeleteDtor>())
1511 Trigger = DeleteDtor->getDeleteExpr();
1512 else
1513 Trigger = Dtor->getBody();
1514
1515 return getCXXDestructorCall(Dtor, Trigger, ThisVal.getAsRegion(),
1516 E.getAs<CFGBaseDtor>().has_value(), State,
1517 CallerCtx, ElemRef);
1518}
1519
1521 const LocationContext *LC,
1523 if (const auto *CE = dyn_cast<CallExpr>(S)) {
1524 return getSimpleCall(CE, State, LC, ElemRef);
1525 } else if (const auto *NE = dyn_cast<CXXNewExpr>(S)) {
1526 return getCXXAllocatorCall(NE, State, LC, ElemRef);
1527 } else if (const auto *DE = dyn_cast<CXXDeleteExpr>(S)) {
1528 return getCXXDeallocatorCall(DE, State, LC, ElemRef);
1529 } else if (const auto *ME = dyn_cast<ObjCMessageExpr>(S)) {
1530 return getObjCMethodCall(ME, State, LC, ElemRef);
1531 } else {
1532 return nullptr;
1533 }
1534}
Defines the clang::ASTContext interface.
#define V(N, I)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
static bool isZeroConstant(const llvm::Value *Value)
static bool isVoidPointerToNonConst(QualType T)
static const ObjCMethodDecl * findDefiningRedecl(const ObjCMethodDecl *MD)
static const ObjCMethodDecl * lookupRuntimeDefinition(const ObjCInterfaceDecl *Interface, Selector LookupSelector, bool InstanceMethod)
static const MemRegion * getThisRegionBaseOrNull(const CallEvent &Call)
static const Expr * getSyntacticFromForPseudoObjectExpr(const PseudoObjectExpr *POE)
static bool isTransparentUnion(QualType T)
llvm::PointerIntPair< const PseudoObjectExpr *, 2 > ObjCMessageDataTy
static bool isCallback(QualType T)
Definition CallEvent.cpp:78
static SVal castArgToParamTypeIfNeeded(const CallEvent &Call, unsigned ArgIdx, SVal ArgVal, SValBuilder &SVB)
Cast the argument value to the type of the parameter at the function declaration.
llvm::DenseMap< PrivateMethodKey, std::optional< const ObjCMethodDecl * > > PrivateMethodCacheTy
static SVal processArgument(SVal Value, const Expr *ArgumentExpr, const ParmVarDecl *Parameter, SValBuilder &SVB)
static PrivateMethodCacheTy PrivateMethodCache
static void findPtrToConstParams(llvm::SmallSet< unsigned, 4 > &PreserveArgs, const CallEvent &Call)
static void addParameterValuesToBindings(const StackFrame *CalleeSF, CallEvent::BindingsTy &Bindings, SValBuilder &SVB, const CallEvent &Call, ArrayRef< ParmVarDecl * > parameters)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
static const Decl * getCanonicalDecl(const Decl *D)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
llvm::SmallVector< std::pair< const MemRegion *, SVal >, 4 > Bindings
Defines the clang::SourceLocation class and associated facilities.
Defines various enumerations that describe declaration and type specifiers.
C Language Family Type Representation.
static bool isPointerToConst(const QualType &QT)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:227
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
QualType getReferenceQualifiedType(const Expr *e) const
getReferenceQualifiedType - Given an expr, will return the type for that expression,...
const clang::PrintingPolicy & getPrintingPolicy() const
Definition ASTContext.h:858
CanQualType VoidTy
CanQualType getCanonicalTagType(const TagDecl *TD) const
AnalysisDeclContext * getContext(const Decl *D)
AnalysisDeclContext contains the context data for the function, method or block under analysis.
const ImplicitParamDecl * getSelfDecl() const
AnalysisDeclContextManager * getManager() const
const StackFrame * getStackFrame(LocationContext const *ParentLC, const void *Data, const Expr *E, const CFGBlock *Blk, unsigned BlockCount, unsigned Index)
Obtain a context of the call stack using its parent context.
Stores options for the analyzer from the command line.
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4690
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:4776
Represents C++ object destructor implicitly generated for automatic object or temporary bound to cons...
Definition CFG.h:445
Represents C++ object destructor implicitly generated for base object in destructor.
Definition CFG.h:496
Represents a single basic block in a source-level CFG.
Definition CFG.h:632
unsigned size() const
Definition CFG.h:979
ElementRefImpl< true > ConstCFGElementRef
Definition CFG.h:948
Represents a function call that returns a C++ object by value.
Definition CFG.h:187
Represents C++ constructor call.
Definition CFG.h:158
Represents C++ object destructor generated from a call to delete.
Definition CFG.h:470
Represents a top-level expression in a basic block.
Definition CFG.h:55
std::optional< T > getAs() const
Convert to the specified CFGElement type, returning std::nullopt if this CFGElement is not of the des...
Definition CFG.h:110
Represents C++ object destructor implicitly generated by compiler on various occasions.
Definition CFG.h:394
const CFGBlock * getBlock(const Stmt *S) const
Returns the CFGBlock the specified Stmt* appears in.
Represents C++ object destructor implicitly generated at the end of full expression for temporary obj...
Definition CFG.h:538
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
Definition ExprCXX.cpp:729
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2132
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2271
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasMutableFields() const
Determine whether this class, or any of its class subobjects, contains a mutable field.
Definition DeclCXX.h:1233
bool hasDefinition() const
Definition DeclCXX.h:561
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2946
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3150
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3129
Expr * getCallee()
Definition Expr.h:3093
ConstructionContext's subclasses describe different ways of constructing an object in C++.
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
SourceLocation getLocation() const
Definition DeclBase.h:447
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
Definition DeclBase.h:1062
This represents one expression.
Definition Expr.h:112
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3093
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2018
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2792
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition TypeBase.h:4562
One of these records is kept for each identifier that is lexed.
bool isStr(const char(&Str)[StrLen]) const
Return true if this is the identifier for the specified string.
StringRef getName() const
Return the actual identifier string.
const Decl * getDecl() const
const ParentMap & getParentMap() const
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
const LocationContext * getParent() const
It might return null.
const StackFrame * getStackFrame() const
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Definition Decl.h:295
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition Decl.h:301
Represents an ObjC class declaration.
Definition DeclObjC.h:1154
ObjCMethodDecl * lookupMethod(Selector Sel, bool isInstance, bool shallowCategoryLookup=false, bool followSuper=true, const ObjCCategoryDecl *C=nullptr) const
lookupMethod - This method returns an instance/class method by looking in the class,...
Definition DeclObjC.cpp:696
SourceLocation getEndOfDefinitionLoc() const
Definition DeclObjC.h:1878
ObjCInterfaceDecl * getSuperClass() const
Definition DeclObjC.cpp:349
ObjCIvarDecl - Represents an ObjC instance variable.
Definition DeclObjC.h:1952
An expression that sends a message to the given Objective-C object or class.
Definition ExprObjC.h:971
Selector getSelector() const
Definition ExprObjC.cpp:301
@ SuperInstance
The receiver is the instance of the superclass object.
Definition ExprObjC.h:985
@ SuperClass
The receiver is a superclass.
Definition ExprObjC.h:982
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Definition ExprObjC.h:1287
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition ExprObjC.cpp:322
QualType getSuperType() const
Retrieve the type referred to by 'super'.
Definition ExprObjC.h:1375
const ObjCMethodDecl * getMethodDecl() const
Definition ExprObjC.h:1395
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
bool hasBody() const override
Determine whether this method has a body.
Definition DeclObjC.h:523
bool isOverriding() const
Whether this method overrides any other in the class hierarchy.
Definition DeclObjC.h:462
ArrayRef< ParmVarDecl * > parameters() const
Definition DeclObjC.h:373
bool isPropertyAccessor() const
Definition DeclObjC.h:436
const ObjCPropertyDecl * findPropertyDecl(bool CheckOverrides=true) const
Returns the property associated with this method's selector.
ObjCMethodDecl * getCanonicalDecl() override
Retrieves the "canonical" declaration of the given declaration.
ObjCInterfaceDecl * getClassInterface()
Represents one property declaration in an Objective-C interface.
Definition DeclObjC.h:731
Stmt * getParentIgnoreParenCasts(Stmt *) const
Represents a parameter to a function.
Definition Decl.h:1808
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3387
Represents a program point just after an implicit call event.
Represents a program point just before an implicit call event.
ProgramPoints can be "tagged" as representing points specific to a given analysis entity.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition Expr.h:6804
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition Expr.h:6841
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
QualType getCanonicalType() const
Definition TypeBase.h:8492
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8513
Represents a struct/union/class.
Definition Decl.h:4343
field_range fields() const
Definition Decl.h:4546
RecordDecl * getDefinitionOrSelf() const
Definition Decl.h:4531
Smart pointer class that efficiently represents Objective-C method names.
const IdentifierInfo * getIdentifierInfoForSlot(unsigned argIndex) const
Retrieve the identifier at a given position in the selector.
unsigned getNumArgs() const
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
A trivial tuple used to represent a source range.
SourceLocation getBegin() const
It represents a stack frame of the call stack (based on CallEvent).
unsigned getIndex() const
const Expr * getCallSite() const
CFGElement getCallSiteCFGElement() const
const CFGBlock * getCallSiteBlock() const
Stmt - This represents one statement.
Definition Stmt.h:86
StmtClass getStmtClass() const
Definition Stmt.h:1503
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
A container of type source information.
Definition TypeBase.h:8411
bool isBlockPointerType() const
Definition TypeBase.h:8697
bool isVoidType() const
Definition TypeBase.h:9043
bool isFunctionPointerType() const
Definition TypeBase.h:8744
const CXXRecordDecl * getPointeeCXXRecordDecl() const
If this is a pointer or reference to a RecordType, return the CXXRecordDecl that the type refers to.
Definition Type.cpp:1958
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2841
bool isAnyPointerType() const
Definition TypeBase.h:8685
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9270
Represents a variable declaration or definition.
Definition Decl.h:924
This class is used for tools that requires cross translation unit capability.
llvm::Expected< const FunctionDecl * > getCrossTUDefinition(const FunctionDecl *FD, StringRef CrossTUDir, StringRef IndexName, bool DisplayCTUProgress=false)
This function loads a function or variable definition from an external AST file and merges it into th...
bool hasError(const Decl *ToDecl) const
Returns true if the given Decl is mapped (or created) during an import but there was an unrecoverable...
bool isImportedAsNew(const Decl *ToDecl) const
Returns true if the given Decl is newly created during the import.
static bool isInCodeFile(SourceLocation SL, const SourceManager &SM)
void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const override
Used to specify non-argument regions that will be invalidated as a result of this call.
SVal getCXXThisVal() const
Returns the value of the implicit 'this' object.
void getInitialStackFrameContents(const StackFrame *CalleeSF, BindingsTy &Bindings) const override
Populates the given SmallVector with the bindings in the callee's stack frame at the start of this ca...
void getInitialStackFrameContents(const StackFrame *CalleeSF, BindingsTy &Bindings) const override
Populates the given SmallVector with the bindings in the callee's stack frame at the start of this ca...
const FunctionDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
Definition CallEvent.h:525
ArrayRef< ParmVarDecl * > parameters() const override
Return call's formal parameters.
bool argumentsMayEscape() const override
Returns true if any of the arguments are known to escape to long- term storage, even if this method w...
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
llvm::ImmutableList< SVal > getEmptySValList()
llvm::ImmutableList< SVal > prependSVal(SVal X, llvm::ImmutableList< SVal > L)
const BlockDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
Definition CallEvent.h:618
void getInitialStackFrameContents(const StackFrame *CalleeSF, BindingsTy &Bindings) const override
Populates the given SmallVector with the bindings in the callee's stack frame at the start of this ca...
const BlockDataRegion * getBlockRegion() const
Returns the region associated with this instance of the block.
bool isConversionFromLambda() const
Definition CallEvent.h:625
ArrayRef< ParmVarDecl * > parameters() const override
Return call's formal parameters.
void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const override
Used to specify non-argument regions that will be invalidated as a result of this call.
const VarRegion * getRegionStoringCapturedLambda() const
For a block converted from a C++ lambda, returns the block VarRegion for the variable holding the cap...
Definition CallEvent.h:635
const CallExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:603
BlockDataRegion - A region that represents a block instance.
Definition MemRegion.h:706
SVal getCXXThisVal() const override
Returns the value of the implicit 'this' object.
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
bool isBaseDestructor() const
Returns true if this is a call to a base class destructor.
Definition CallEvent.h:952
const StackFrame * getInheritingStackFrame() const
Obtain the stack frame of the inheriting constructor.
std::pair< const CXXRecordDecl *, bool > getDeclForDynamicType() const
Returns the decl refered to by the "dynamic type" of the current object and if the class can be a sub...
void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const override
Used to specify non-argument regions that will be invalidated as a result of this call.
virtual SVal getCXXThisVal() const
Returns the value of the implicit 'this' object.
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
virtual const Expr * getCXXThisExpr() const
Returns the expression representing the implicit 'this' object.
Definition CallEvent.h:713
const FunctionDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
void getInitialStackFrameContents(const StackFrame *CalleeSF, BindingsTy &Bindings) const override
Populates the given SmallVector with the bindings in the callee's stack frame at the start of this ca...
const CXXMemberCallExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:818
const Expr * getCXXThisExpr() const override
Returns the expression representing the implicit 'this' object.
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
const Expr * getCXXThisExpr() const override
Returns the expression representing the implicit 'this' object.
const CXXOperatorCallExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:863
CallEventRef getCaller(const StackFrame *CalleeSF, ProgramStateRef State)
Gets an outside caller given a callee context.
CallEventRef< CXXDestructorCall > getCXXDestructorCall(const CXXDestructorDecl *DD, const Stmt *Trigger, const MemRegion *Target, bool IsBase, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1466
CallEventRef getCall(const Stmt *S, ProgramStateRef State, const LocationContext *LC, CFGBlock::ConstCFGElementRef ElemRef)
Gets a call event for a function call, Objective-C method call, a 'new', or a 'delete' call.
CallEventRef< CXXDeallocatorCall > getCXXDeallocatorCall(const CXXDeleteExpr *E, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1482
CallEventRef getSimpleCall(const CallExpr *E, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
CallEventRef< ObjCMethodCall > getObjCMethodCall(const ObjCMessageExpr *E, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1444
CallEventManager(llvm::BumpPtrAllocator &alloc)
CallEventRef< CXXAllocatorCall > getCXXAllocatorCall(const CXXNewExpr *E, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1475
CallEventRef< CXXConstructorCall > getCXXConstructorCall(const CXXConstructExpr *E, const MemRegion *Target, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1451
CallEventRef< CXXInheritedConstructorCall > getCXXInheritedConstructorCall(const CXXInheritedCtorInitExpr *E, const MemRegion *Target, ProgramStateRef State, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1458
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:153
virtual SourceRange getArgSourceRange(unsigned Index) const
Returns the source range for errors associated with this argument.
virtual void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const
Used to specify non-argument regions that will be invalidated as a result of this call.
Definition CallEvent.h:211
virtual StringRef getKindAsString() const =0
SVal getSVal(const Expr *E) const
Get the value of arbitrary expressions at this point in the path.
Definition CallEvent.h:202
virtual const Expr * getOriginExpr() const
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:263
ProgramStateRef getState() const
A state for looking up relevant Environment entries (arguments, return value), dynamic type informati...
Definition CallEvent.h:222
static bool isCallStmt(const Stmt *S)
Returns true if this is a statement is a function or method call of some kind.
const ConstructionContext * getConstructionContext() const
Returns the construction context of the call, if it is a C++ constructor call or a call of a function...
param_type_iterator param_type_end() const
Definition CallEvent.h:499
const ParamVarRegion * getParameterLocation(unsigned Index, unsigned BlockCount) const
Returns memory location for a parameter variable within the callee stack frame.
bool isCalledFromSystemHeader() const
AnalysisDeclContext * getCalleeAnalysisDeclContext() const
Returns AnalysisDeclContext for the callee stack frame.
virtual std::optional< unsigned > getAdjustedParameterIndex(unsigned ASTArgumentIndex) const
Some calls have parameter numbering mismatched from argument numbering.
Definition CallEvent.h:445
QualType getResultType() const
Returns the result type, adjusted for references.
Definition CallEvent.cpp:70
ProgramStateRef invalidateRegions(unsigned BlockCount, ProgramStateRef State) const
Invalidates the regions (arguments, globals, special regions like 'this') that may have been written ...
friend class CallEventManager
Definition CallEvent.h:182
llvm::mapped_iterator< ArrayRef< ParmVarDecl * >::iterator, GetTypeFn > param_type_iterator
Definition CallEvent.h:487
const StackFrame * getCalleeStackFrame(unsigned BlockCount) const
Returns the callee stack frame.
bool isInSystemHeader() const
Returns true if the callee is known to be from a system header.
Definition CallEvent.h:275
bool isGlobalCFunction(StringRef SpecificName=StringRef()) const
Returns true if the callee is an externally-visible function in the top-level namespace,...
virtual bool argumentsMayEscape() const
Returns true if any of the arguments are known to escape to long- term storage, even if this method w...
Definition CallEvent.h:337
param_type_iterator param_type_begin() const
Returns an iterator over the types of the call's formal parameters.
Definition CallEvent.h:495
ProgramPoint getProgramPoint(bool IsPreVisit=false, const ProgramPointTag *Tag=nullptr) const
Returns an appropriate ProgramPoint for this call.
static QualType getDeclaredResultType(const Decl *D)
Returns the result type of a function or method declaration.
static bool isVariadic(const Decl *D)
Returns true if the given decl is known to be variadic.
virtual SVal getArgSVal(unsigned Index) const
Returns the value of a given argument at the time of the call.
bool hasNonNullArgumentsWithType(bool(*Condition)(QualType)) const
Returns true if the type of any of the non-null arguments satisfies the condition.
std::optional< SVal > getReturnValueUnderConstruction() const
If the call returns a C++ record type then the region of its return value can be retrieved from its c...
virtual const Expr * getArgExpr(unsigned Index) const
Returns the expression associated with a given argument.
Definition CallEvent.h:306
virtual unsigned getNumArgs() const =0
Returns the number of arguments (explicit and implicit).
bool hasVoidPointerToNonConstArg() const
Returns true if any of the arguments is void*.
const CallEventRef getCaller() const
bool isArgumentConstructedDirectly(unsigned Index) const
Returns true if on the current path, the argument was constructed by calling a C++ constructor over i...
Definition CallEvent.h:433
SmallVectorImpl< FrameBindingTy > BindingsTy
Definition CallEvent.h:381
SVal getReturnValue() const
Returns the return value of the call.
virtual const Decl * getDecl() const
Returns the declaration of the function or method that will be called.
Definition CallEvent.h:234
const LocationContext * getLocationContext() const
The context in which the call is being evaluated.
Definition CallEvent.h:251
const CFGBlock::ConstCFGElementRef & getCFGElementRef() const
Definition CallEvent.h:253
bool hasNonZeroCallbackArg() const
Returns true if any of the arguments appear to represent callbacks.
virtual Kind getKind() const =0
Returns the kind of call this is.
SmallVectorImpl< SVal > ValueList
Definition CallEvent.h:206
virtual SourceRange getSourceRange() const
Returns a source range for the entire call, suitable for outputting in diagnostics.
Definition CallEvent.h:297
static bool isCLibraryFunction(const FunctionDecl *FD, StringRef Name=StringRef())
Returns true if the given function is an externally-visible function in the top-level namespace,...
Stores the currently inferred strictest bound on the runtime type of a region in a given state along ...
bool canBeASubClass() const
Returns false if the type information is precise (the type 'DynTy' is the only type in the lattice),...
QualType getType() const
Returns the currently inferred upper bound on the runtime type.
bool isValid() const
Returns true if the dynamic type info is available.
unsigned getNumVisited(const LocationContext *LC, const CFGBlock *Block) const
Definition ExprEngine.h:296
SVal computeObjectUnderConstruction(const Expr *E, ProgramStateRef State, unsigned NumVisitedCaller, const LocationContext *LCtx, const ConstructionContext *CC, EvalCallOptions &CallOpts, unsigned Idx=0)
Find location of the object that is being constructed by a given constructor.
const VarRegion * getVarRegion(const VarDecl *VD, const LocationContext *LC)
getVarRegion - Retrieve or create the memory region associated with a specified VarDecl and LocationC...
const ParamVarRegion * getParamVarRegion(const Expr *OriginExpr, unsigned Index, const LocationContext *LC)
getParamVarRegion - Retrieve or create the memory region associated with a specified CallExpr,...
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:98
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion * getBaseRegion() const
const ObjCMethodDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
Definition CallEvent.h:1291
void getExtraInvalidatedValues(ValueList &Values, RegionAndSymbolInvalidationTraits *ETraits) const override
Used to specify non-argument regions that will be invalidated as a result of this call.
ObjCMessageKind getMessageKind() const
Returns how the message was written in the source (property access, subscript, or explicit message se...
const ObjCMessageExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:1287
ObjCMethodCall(const ObjCMessageExpr *Msg, ProgramStateRef St, const LocationContext *LCtx, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1267
ArrayRef< ParmVarDecl * > parameters() const override
Return call's formal parameters.
void getInitialStackFrameContents(const StackFrame *CalleeSF, BindingsTy &Bindings) const override
Populates the given SmallVector with the bindings in the callee's stack frame at the start of this ca...
SourceRange getSourceRange() const override
Returns a source range for the entire call, suitable for outputting in diagnostics.
virtual bool canBeOverridenInSubclass(ObjCInterfaceDecl *IDecl, Selector Sel) const
Check if the selector may have multiple definitions (may have overrides).
bool argumentsMayEscape() const override
Returns true if any of the arguments are known to escape to long- term storage, even if this method w...
SVal getReceiverSVal() const
Returns the value of the receiver at the time of this call.
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
bool isReceiverSelfOrSuper() const
Checks if the receiver refers to 'self' or 'super'.
Selector getSelector() const
Definition CallEvent.h:1309
const ObjCPropertyDecl * getAccessedProperty() const
ParamVarRegion - Represents a region for parameters.
Definition MemRegion.h:1067
Information about invalidation for a particular region/symbol.
Definition MemRegion.h:1661
@ TK_PreserveContents
Tells that a region's contents is not changed.
Definition MemRegion.h:1676
@ TK_SuppressEscape
Suppress pointer-escaping of a region.
Definition MemRegion.h:1679
void setTrait(SymbolRef Sym, InvalidationKinds IK)
Defines the runtime definition of the called function.
Definition CallEvent.h:110
BasicValueFactory & getBasicValueFactory()
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
MemRegionManager & getRegionManager()
ProgramStateManager & getStateManager()
ASTContext & getContext()
loc::MemRegionVal makeLoc(SymbolRef sym)
SVal evalCast(SVal V, QualType CastTy, QualType OriginalTy)
Cast a given SVal to another SVal using given QualType's.
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrame *SF)
Return a memory region for the 'this' object reference.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
bool isUnknownOrUndef() const
Definition SVals.h:109
const FunctionDecl * getAsFunctionDecl() const
getAsFunctionDecl - If this SVal is a MemRegionVal and wraps a CodeTextRegion wrapping a FunctionDecl...
Definition SVals.cpp:45
QualType getType(const ASTContext &) const
Try to get a reasonable type for the given value.
Definition SVals.cpp:180
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
bool isValid() const
Definition SVals.h:111
bool isUnknown() const
Definition SVals.h:105
RuntimeDefinition getRuntimeDefinition() const override
Returns the definition of the function or method that will be called.
const CallExpr * getOriginExpr() const override
Returns the expression whose value will be the result of this call.
Definition CallEvent.h:562
const FunctionDecl * getDecl() const override
Returns the declaration of the function or method that will be called.
std::optional< SVal > evalBaseToDerived(SVal Base, QualType DerivedPtrType)
Attempts to do a down cast.
Definition Store.cpp:318
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:563
static const FunctionDecl * getCallee(const CXXConstructExpr &D)
bool isWithinStdNamespace(const Decl *D)
Returns true if declaration D is in std namespace or any nested namespace or class scope.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
DynamicTypeInfo getDynamicTypeInfo(ProgramStateRef State, const MemRegion *MR)
Get dynamic type information for the region MR.
@ CE_CXXAllocator
Definition CallEvent.h:72
ObjCMessageKind
Represents the ways an Objective-C message send can occur.
Definition CallEvent.h:1256
DynamicTypeInfo getClassObjectDynamicTypeInfo(ProgramStateRef State, SymbolRef Sym)
Get dynamic type information stored in a class object represented by Sym.
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
@ Parameter
The parameter type of a method or function.
Definition TypeBase.h:908
@ Result
The result type of a method or function.
Definition TypeBase.h:905
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Interface
The "__interface" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5970
@ Class
The "class" keyword introduces the elaborated-type-specifier.
Definition TypeBase.h:5976
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
Selector LookupSelector
const ObjCInterfaceDecl * Interface
Hints for figuring out if a call should be inlined during evalCall().
Definition ExprEngine.h:94
DenseMapInfo< Selector > SelectorInfo
static unsigned getHashValue(const PrivateMethodKey &Key)
static PrivateMethodKey getEmptyKey()
DenseMapInfo< const ObjCInterfaceDecl * > InterfaceInfo
static bool isEqual(const PrivateMethodKey &LHS, const PrivateMethodKey &RHS)
static PrivateMethodKey getTombstoneKey()