clang 23.0.0git
ExprEngineCXX.cpp
Go to the documentation of this file.
1//===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===//
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 the C++ expression evaluation engine.
10//
11//===----------------------------------------------------------------------===//
12
15#include "clang/AST/DeclCXX.h"
16#include "clang/AST/ParentMap.h"
17#include "clang/AST/StmtCXX.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/Sequence.h"
27#include "llvm/Support/Casting.h"
28#include <optional>
29
30using namespace clang;
31using namespace ento;
32
34 ExplodedNode *Pred,
35 ExplodedNodeSet &Dst) {
36 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
37 const Expr *tempExpr = ME->getSubExpr()->IgnoreParens();
38 ProgramStateRef state = Pred->getState();
39 const StackFrame *SF = Pred->getStackFrame();
40
41 state = createTemporaryRegionIfNeeded(state, SF, tempExpr, ME);
42 Bldr.generateNode(ME, Pred, state);
43}
44
45void ExprEngine::performTrivialCopy(ExplodedNodeSet &Dst, ExplodedNode *Pred,
46 const CallEvent &Call) {
47 SVal ThisVal;
48 bool AlwaysReturnsLValue;
49 [[maybe_unused]] const CXXRecordDecl *ThisRD = nullptr;
50 if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
51 assert(Ctor->getDecl()->isTrivial());
52 assert(Ctor->getDecl()->isCopyOrMoveConstructor());
53 ThisVal = Ctor->getCXXThisVal();
54 ThisRD = Ctor->getDecl()->getParent();
55 AlwaysReturnsLValue = false;
56 } else {
57 assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
58 assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
59 OO_Equal);
60 ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
61 ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent();
62 AlwaysReturnsLValue = true;
63 }
64
65 const StackFrame *SF = Pred->getStackFrame();
66 const Expr *CallExpr = Call.getOriginExpr();
67
68 ExplodedNodeSet DstEval;
69
70 assert(ThisRD);
71
72 if (!ThisRD->isEmpty()) {
73 SVal V = Call.getArgSVal(0);
74 const Expr *VExpr = Call.getArgExpr(0);
75
76 // If the value being copied is not unknown, load from its location to get
77 // an aggregate rvalue.
78 if (std::optional<Loc> L = V.getAs<Loc>())
79 V = Pred->getState()->getSVal(*L);
80 else
81 assert(V.isUnknownOrUndef());
82
84 evalLocation(Tmp, CallExpr, VExpr, Pred, Pred->getState(), V,
85 /*isLoad=*/true);
86 for (ExplodedNode *N : Tmp)
87 evalBind(DstEval, CallExpr, N, ThisVal, V, !AlwaysReturnsLValue);
88 } else {
89 // We can't copy empty classes because of empty base class optimization.
90 // In that case, copying the empty base class subobject would overwrite the
91 // object that it overlaps with - so let's not do that.
92 // See issue-157467.cpp for an example.
93 DstEval.insert(Pred);
94 }
95
96 for (ExplodedNode *N : DstEval) {
97 ProgramStateRef State = N->getState();
98 if (AlwaysReturnsLValue)
99 State = State->BindExpr(CallExpr, SF, ThisVal);
100 else
101 State = bindReturnValue(Call, SF, State);
102 Dst.insert(Engine.makePostStmtNode(CallExpr, State, N));
103 }
104}
105
106SVal ExprEngine::makeElementRegion(ProgramStateRef State, SVal LValue,
107 QualType &Ty, bool &IsArray, unsigned Idx) {
108 SValBuilder &SVB = State->getStateManager().getSValBuilder();
109 ASTContext &Ctx = SVB.getContext();
110
111 if (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
112 while (AT) {
113 Ty = AT->getElementType();
114 AT = dyn_cast<ArrayType>(AT->getElementType());
115 }
116 LValue = State->getLValue(Ty, SVB.makeArrayIndex(Idx), LValue);
117 IsArray = true;
118 }
119
120 return LValue;
121}
122
123// In case when the prvalue is returned from the function (kind is one of
124// SimpleReturnedValueKind, CXX17ElidedCopyReturnedValueKind), then
125// it's materialization happens in context of the caller.
127 const Expr *E, ProgramStateRef State, unsigned NumVisitedCaller,
128 const StackFrame *SF, const ConstructionContext *CC,
129 EvalCallOptions &CallOpts, unsigned Idx) {
130
132 MemRegionManager &MRMgr = SVB.getRegionManager();
133 ASTContext &ACtx = SVB.getContext();
134
135 // Compute the target region by exploring the construction context.
136 if (CC) {
137 switch (CC->getKind()) {
140 const auto *DSCC = cast<VariableConstructionContext>(CC);
141 const auto *DS = DSCC->getDeclStmt();
142 const auto *Var = cast<VarDecl>(DS->getSingleDecl());
143 QualType Ty = Var->getType();
144 return makeElementRegion(State, State->getLValue(Var, SF), Ty,
145 CallOpts.IsArrayCtorOrDtor, Idx);
146 }
150 const auto *Init = ICC->getCXXCtorInitializer();
151 const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(SF->getDecl());
152 Loc ThisPtr = SVB.getCXXThis(CurCtor, SF);
153 SVal ThisVal = State->getSVal(ThisPtr);
154 if (Init->isBaseInitializer()) {
155 const auto *ThisReg = cast<SubRegion>(ThisVal.getAsRegion());
156 const CXXRecordDecl *BaseClass =
157 Init->getBaseClass()->getAsCXXRecordDecl();
158 const auto *BaseReg =
159 MRMgr.getCXXBaseObjectRegion(BaseClass, ThisReg,
160 Init->isBaseVirtual());
161 return SVB.makeLoc(BaseReg);
162 }
163 if (Init->isDelegatingInitializer())
164 return ThisVal;
165
166 const ValueDecl *Field;
167 SVal FieldVal;
168 if (Init->isIndirectMemberInitializer()) {
169 Field = Init->getIndirectMember();
170 FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
171 } else {
172 Field = Init->getMember();
173 FieldVal = State->getLValue(Init->getMember(), ThisVal);
174 }
175
176 QualType Ty = Field->getType();
177 return makeElementRegion(State, FieldVal, Ty, CallOpts.IsArrayCtorOrDtor,
178 Idx);
179 }
181 if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
182 const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC);
183 const auto *NE = NECC->getCXXNewExpr();
184 SVal V = *getObjectUnderConstruction(State, NE, SF);
185 if (const SubRegion *MR =
186 dyn_cast_or_null<SubRegion>(V.getAsRegion())) {
187 if (NE->isArray()) {
188 CallOpts.IsArrayCtorOrDtor = true;
189
190 auto Ty = NE->getType()->getPointeeType();
191 while (const auto *AT = getContext().getAsArrayType(Ty))
192 Ty = AT->getElementType();
193
194 auto R = MRMgr.getElementRegion(Ty, svalBuilder.makeArrayIndex(Idx),
195 MR, SVB.getContext());
196
197 return loc::MemRegionVal(R);
198 }
199 return V;
200 }
201 // TODO: Detect when the allocator returns a null pointer.
202 // Constructor shall not be called in this case.
203 }
204 break;
205 }
208 // The temporary is to be managed by the parent stack frame.
209 // So build it in the parent stack frame if we're not in the
210 // top frame of the analysis.
211 if (const StackFrame *CallerSF = SF->getParent()) {
212 auto RTC = (*SF->getCallSiteBlock())[SF->getIndex()]
213 .getAs<CFGCXXRecordTypedCall>();
214 if (!RTC) {
215 // We were unable to find the correct construction context for the
216 // call in the parent stack frame. This is equivalent to not being
217 // able to find construction context at all.
218 break;
219 }
220
221 unsigned NVCaller = getNumVisited(CallerSF, SF->getCallSiteBlock());
223 SF->getCallSite(), State, NVCaller, CallerSF,
224 RTC->getConstructionContext(), CallOpts);
225 } else {
226 // We are on the top frame of the analysis. We do not know where is the
227 // object returned to. Conjure a symbolic region for the return value.
228 // TODO: We probably need a new MemRegion kind to represent the storage
229 // of that SymbolicRegion, so that we could produce a fancy symbol
230 // instead of an anonymous conjured symbol.
231 // TODO: Do we need to track the region to avoid having it dead
232 // too early? It does die too early, at least in C++17, but because
233 // putting anything into a SymbolicRegion causes an immediate escape,
234 // it doesn't cause any leak false positives.
235 const auto *RCC = cast<ReturnedValueConstructionContext>(CC);
236 // Make sure that this doesn't coincide with any other symbol
237 // conjured for the returned expression.
238 static const int TopLevelSymRegionTag = 0;
239 const Expr *RetE = RCC->getReturnStmt()->getRetValue();
240 assert(RetE && "Void returns should not have a construction context");
241 QualType ReturnTy = RetE->getType();
242 QualType RegionTy = ACtx.getPointerType(ReturnTy);
243 return SVB.conjureSymbolVal(&TopLevelSymRegionTag, getCFGElementRef(),
244 SF, RegionTy, getNumVisitedCurrent());
245 }
246 llvm_unreachable("Unhandled return value construction context!");
247 }
249 assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
251
252 // Support pre-C++17 copy elision. We'll have the elidable copy
253 // constructor in the AST and in the CFG, but we'll skip it
254 // and construct directly into the final object. This call
255 // also sets the CallOpts flags for us.
256 // If the elided copy/move constructor is not supported, there's still
257 // benefit in trying to model the non-elided constructor.
258 // Stash our state before trying to elide, as it'll get overwritten.
259 ProgramStateRef PreElideState = State;
260 EvalCallOptions PreElideCallOpts = CallOpts;
261
263 TCC->getConstructorAfterElision(), State, NumVisitedCaller, SF,
264 TCC->getConstructionContextAfterElision(), CallOpts);
265
266 // FIXME: This definition of "copy elision has not failed" is unreliable.
267 // It doesn't indicate that the constructor will actually be inlined
268 // later; this is still up to evalCall() to decide.
270 return V;
271
272 // Copy elision failed. Revert the changes and proceed as if we have
273 // a simple temporary.
274 CallOpts = PreElideCallOpts;
276 [[fallthrough]];
277 }
279 const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
280 const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr();
281
282 CallOpts.IsTemporaryCtorOrDtor = true;
283 if (MTE) {
284 if (const ValueDecl *VD = MTE->getExtendingDecl()) {
286 assert(SD != SD_FullExpression);
287 if (!VD->getType()->isReferenceType()) {
288 // We're lifetime-extended by a surrounding aggregate.
289 // Automatic destructors aren't quite working in this case
290 // on the CFG side. We should warn the caller about that.
291 // FIXME: Is there a better way to retrieve this information from
292 // the MaterializeTemporaryExpr?
294 }
295
296 if (SD == SD_Static || SD == SD_Thread)
297 return loc::MemRegionVal(
298 MRMgr.getCXXStaticLifetimeExtendedObjectRegion(E, VD));
299
300 return loc::MemRegionVal(
301 MRMgr.getCXXLifetimeExtendedObjectRegion(E, VD, SF));
302 }
303 assert(MTE->getStorageDuration() == SD_FullExpression);
304 }
305
306 return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, SF));
307 }
309 CallOpts.IsTemporaryCtorOrDtor = true;
310
311 const auto *LCC = cast<LambdaCaptureConstructionContext>(CC);
312
314 MRMgr.getCXXTempObjectRegion(LCC->getInitializer(), SF));
315
316 const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E);
317 if (getIndexOfElementToConstruct(State, CE, SF)) {
318 CallOpts.IsArrayCtorOrDtor = true;
319 Base = State->getLValue(E->getType(), svalBuilder.makeArrayIndex(Idx),
320 Base);
321 }
322
323 return Base;
324 }
326 // Arguments are technically temporaries.
327 CallOpts.IsTemporaryCtorOrDtor = true;
328
329 const auto *ACC = cast<ArgumentConstructionContext>(CC);
330 const Expr *E = ACC->getCallLikeExpr();
331 unsigned Idx = ACC->getIndex();
332
334 auto getArgLoc = [&](CallEventRef<> Caller) -> std::optional<SVal> {
335 const StackFrame *FutureSF =
336 Caller->getCalleeStackFrame(NumVisitedCaller);
337 // Return early if we are unable to reliably foresee
338 // the future stack frame.
339 if (!FutureSF)
340 return std::nullopt;
341
342 // This should be equivalent to Caller->getDecl() for now, but
343 // FutureSF->getDecl() is likely to support better stuff (like
344 // virtual functions) earlier.
345 const Decl *CalleeD = FutureSF->getDecl();
346
347 // FIXME: Support for variadic arguments is not implemented here yet.
348 if (CallEvent::isVariadic(CalleeD))
349 return std::nullopt;
350
351 // Operator arguments do not correspond to operator parameters
352 // because this-argument is implemented as a normal argument in
353 // operator call expressions but not in operator declarations.
354 const TypedValueRegion *TVR = Caller->getParameterLocation(
355 *Caller->getAdjustedParameterIndex(Idx), NumVisitedCaller);
356 if (!TVR)
357 return std::nullopt;
358
359 return loc::MemRegionVal(TVR);
360 };
361
362 if (const auto *CE = dyn_cast<CallExpr>(E)) {
363 CallEventRef<> Caller =
364 CEMgr.getSimpleCall(CE, State, SF, getCFGElementRef());
365 if (std::optional<SVal> V = getArgLoc(Caller))
366 return *V;
367 else
368 break;
369 } else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) {
370 // Don't bother figuring out the target region for the future
371 // constructor because we won't need it.
373 CCE, /*Target=*/nullptr, State, SF, getCFGElementRef());
374 if (std::optional<SVal> V = getArgLoc(Caller))
375 return *V;
376 else
377 break;
378 } else if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) {
379 CallEventRef<> Caller =
380 CEMgr.getObjCMethodCall(ME, State, SF, getCFGElementRef());
381 if (std::optional<SVal> V = getArgLoc(Caller))
382 return *V;
383 else
384 break;
385 }
386 }
387 } // switch (CC->getKind())
388 }
389
390 // If we couldn't find an existing region to construct into, assume we're
391 // constructing a temporary. Notify the caller of our failure.
393 return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, SF));
394}
395
397 SVal V, const Expr *E, ProgramStateRef State, const StackFrame *SF,
398 const ConstructionContext *CC, const EvalCallOptions &CallOpts) {
400 // Sounds like we failed to find the target region and therefore
401 // copy elision failed. There's nothing we can do about it here.
402 return State;
403 }
404
405 // See if we're constructing an existing region by looking at the
406 // current construction context.
407 assert(CC && "Computed target region without construction context?");
408 switch (CC->getKind()) {
411 const auto *DSCC = cast<VariableConstructionContext>(CC);
412 return addObjectUnderConstruction(State, DSCC->getDeclStmt(), SF, V);
413 }
417 const auto *Init = ICC->getCXXCtorInitializer();
418 // Base and delegating initializers handled above
419 assert(Init->isAnyMemberInitializer() &&
420 "Base and delegating initializers should have been handled by"
421 "computeObjectUnderConstruction()");
422 return addObjectUnderConstruction(State, Init, SF, V);
423 }
425 return State;
426 }
429 const StackFrame *CallerSF = SF->getParent();
430 if (!CallerSF) {
431 // No extra work is necessary in top frame.
432 return State;
433 }
434
435 auto RTC = (*SF->getCallSiteBlock())[SF->getIndex()]
436 .getAs<CFGCXXRecordTypedCall>();
437 assert(RTC && "Could not have had a target region without it");
438
440 V, SF->getCallSite(), State, CallerSF, RTC->getConstructionContext(),
441 CallOpts);
442 }
444 assert(AMgr.getAnalyzerOptions().ShouldElideConstructors);
448 V, TCC->getConstructorAfterElision(), State, SF,
449 TCC->getConstructionContextAfterElision(), CallOpts);
450
451 // Remember that we've elided the constructor.
452 State = addObjectUnderConstruction(
453 State, TCC->getConstructorAfterElision(), SF, V);
454
455 // Remember that we've elided the destructor.
456 if (const auto *BTE = TCC->getCXXBindTemporaryExpr())
457 State = elideDestructor(State, BTE, SF);
458
459 // Instead of materialization, shamelessly return
460 // the final object destination.
461 if (const auto *MTE = TCC->getMaterializedTemporaryExpr())
462 State = addObjectUnderConstruction(State, MTE, SF, V);
463
464 return State;
465 }
466 // If we decided not to elide the constructor, proceed as if
467 // it's a simple temporary.
468 [[fallthrough]];
469 }
471 const auto *TCC = cast<TemporaryObjectConstructionContext>(CC);
472 if (const auto *BTE = TCC->getCXXBindTemporaryExpr())
473 State = addObjectUnderConstruction(State, BTE, SF, V);
474
475 if (const auto *MTE = TCC->getMaterializedTemporaryExpr())
476 State = addObjectUnderConstruction(State, MTE, SF, V);
477
478 return State;
479 }
481 const auto *LCC = cast<LambdaCaptureConstructionContext>(CC);
482
483 // If we capture and array, we want to store the super region, not a
484 // sub-region.
485 if (const auto *EL = dyn_cast_or_null<ElementRegion>(V.getAsRegion()))
486 V = loc::MemRegionVal(EL->getSuperRegion());
487
488 return addObjectUnderConstruction(
489 State, {LCC->getLambdaExpr(), LCC->getIndex()}, SF, V);
490 }
492 const auto *ACC = cast<ArgumentConstructionContext>(CC);
493 if (const auto *BTE = ACC->getCXXBindTemporaryExpr())
494 State = addObjectUnderConstruction(State, BTE, SF, V);
495
496 return addObjectUnderConstruction(
497 State, {ACC->getCallLikeExpr(), ACC->getIndex()}, SF, V);
498 }
499 }
500 llvm_unreachable("Unhandled construction context!");
501}
502
503static ProgramStateRef
505 const ArrayInitLoopExpr *AILE,
506 const StackFrame *SF, NonLoc Idx) {
508 MemRegionManager &MRMgr = SVB.getRegionManager();
509 ASTContext &Ctx = SVB.getContext();
510
511 // HACK: There is no way we can put the index of the array element into the
512 // CFG unless we unroll the loop, so we manually select and bind the required
513 // parameter to the environment.
514 const Expr *SourceArray = AILE->getCommonExpr()->getSourceExpr();
515 const auto *Ctor =
517
518 const auto *SourceArrayRegion =
519 cast<SubRegion>(State->getSVal(SourceArray, SF).getAsRegion());
521 MRMgr.getElementRegion(Ctor->getType(), Idx, SourceArrayRegion, Ctx);
522
523 return State->BindExpr(Ctor->getArg(0), SF, loc::MemRegionVal(ElementRegion));
524}
525
526void ExprEngine::handleConstructor(const Expr *E,
527 ExplodedNode *Pred,
528 ExplodedNodeSet &destNodes) {
529 const auto *CE = dyn_cast<CXXConstructExpr>(E);
530 const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(E);
531 assert(CE || CIE);
532
533 const StackFrame *SF = Pred->getStackFrame();
534 ProgramStateRef State = Pred->getState();
535
536 SVal Target = UnknownVal();
537
538 if (CE) {
539 if (std::optional<SVal> ElidedTarget =
540 getObjectUnderConstruction(State, CE, SF)) {
541 // We've previously modeled an elidable constructor by pretending that
542 // it in fact constructs into the correct target. This constructor can
543 // therefore be skipped.
544 Target = *ElidedTarget;
545 NodeBuilder Bldr(Pred, destNodes, *currBldrCtx);
546 State = finishObjectConstruction(State, CE, SF);
547 if (auto L = Target.getAs<Loc>())
548 State = State->BindExpr(CE, SF, State->getSVal(*L, CE->getType()));
549 Bldr.generateNode(CE, Pred, State);
550 return;
551 }
552 }
553
554 EvalCallOptions CallOpts;
555 auto C = getCurrentCFGElement().getAs<CFGConstructor>();
556 assert(C || getCurrentCFGElement().getAs<CFGStmt>());
557 const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr;
558
559 const CXXConstructionKind CK =
560 CE ? CE->getConstructionKind() : CIE->getConstructionKind();
561 switch (CK) {
563 // Inherited constructors are always base class constructors.
564 assert(CE && !CIE && "A complete constructor is inherited?!");
565
566 // If the ctor is part of an ArrayInitLoopExpr, we want to handle it
567 // differently.
568 auto *AILE = CC ? CC->getArrayInitLoop() : nullptr;
569
570 unsigned Idx = 0;
571 if (CE->getType()->isArrayType() || AILE) {
572
573 auto isZeroSizeArray = [&] {
574 uint64_t Size = 1;
575
576 if (const auto *CAT = dyn_cast<ConstantArrayType>(CE->getType()))
578 else if (AILE)
580
581 return Size == 0;
582 };
583
584 // No element construction will happen in a 0 size array.
585 if (isZeroSizeArray()) {
586 NodeBuilder Bldr(Pred, destNodes, *currBldrCtx);
587 static SimpleProgramPointTag T{"ExprEngine",
588 "Skipping 0 size array construction"};
589 Bldr.generateNode(CE, Pred, State, &T);
590 return;
591 }
592
593 Idx = getIndexOfElementToConstruct(State, CE, SF).value_or(0u);
594 State = setIndexOfElementToConstruct(State, CE, SF, Idx + 1);
595 }
596
597 if (AILE) {
598 // Only set this once even though we loop through it multiple times.
599 if (!getPendingInitLoop(State, CE, SF))
600 State = setPendingInitLoop(
601 State, CE, SF, getContext().getArrayInitLoopExprElementCount(AILE));
602
604 State, AILE, SF, svalBuilder.makeArrayIndex(Idx));
605 }
606
607 // The target region is found from construction context.
608 std::tie(State, Target) = handleConstructionContext(CE, State, currBldrCtx,
609 SF, CC, CallOpts, Idx);
610 break;
611 }
613 // Make sure we are not calling virtual base class initializers twice.
614 // Only the most-derived object should initialize virtual base classes.
615 const auto *OuterCtor =
616 dyn_cast_or_null<CXXConstructExpr>(SF->getCallSite());
617 assert(
618 (!OuterCtor ||
619 OuterCtor->getConstructionKind() == CXXConstructionKind::Complete ||
620 OuterCtor->getConstructionKind() == CXXConstructionKind::Delegating) &&
621 ("This virtual base should have already been initialized by "
622 "the most derived class!"));
623 (void)OuterCtor;
624 [[fallthrough]];
625 }
627 // In C++17, classes with non-virtual bases may be aggregates, so they would
628 // be initialized as aggregates without a constructor call, so we may have
629 // a base class constructed directly into an initializer list without
630 // having the derived-class constructor call on the previous stack frame.
631 // Initializer lists may be nested into more initializer lists that
632 // correspond to surrounding aggregate initializations.
633 // FIXME: For now this code essentially bails out. We need to find the
634 // correct target region and set it.
635 // FIXME: Instead of relying on the ParentMap, we should have the
636 // trigger-statement (InitListExpr or CXXParenListInitExpr in this case)
637 // passed down from CFG or otherwise always available during construction.
638 if (isa_and_nonnull<InitListExpr, CXXParenListInitExpr>(
639 SF->getParentMap().getParent(E))) {
640 MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
641 Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, SF));
643 break;
644 }
645 [[fallthrough]];
647 const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(SF->getDecl());
648 Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, SF);
649 SVal ThisVal = State->getSVal(ThisPtr);
650
652 Target = ThisVal;
653 } else {
654 // Cast to the base type.
655 bool IsVirtual = (CK == CXXConstructionKind::VirtualBase);
656 SVal BaseVal =
657 getStoreManager().evalDerivedToBase(ThisVal, E->getType(), IsVirtual);
658 Target = BaseVal;
659 }
660 break;
661 }
662 }
663
664 if (State != Pred->getState()) {
665 static SimpleProgramPointTag T("ExprEngine",
666 "Prepare for object construction");
667 ExplodedNodeSet DstPrepare;
668 NodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx);
669 Pred =
670 BldrPrepare.generateNode(E, Pred, State, &T, ProgramPoint::PreStmtKind);
671 if (!Pred)
672 return;
673 }
674
675 const MemRegion *TargetRegion = Target.getAsRegion();
676 CallEventManager &CEMgr = getStateManager().getCallEventManager();
677 CallEventRef<> Call =
678 CIE ? (CallEventRef<>)CEMgr.getCXXInheritedConstructorCall(
679 CIE, TargetRegion, State, SF, getCFGElementRef())
680 : (CallEventRef<>)CEMgr.getCXXConstructorCall(CE, TargetRegion, State,
681 SF, getCFGElementRef());
682
683 ExplodedNodeSet DstPreVisit;
684 getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, E, *this);
685
686 ExplodedNodeSet PreInitialized;
687 if (CE) {
688 // FIXME: Is it possible and/or useful to do this before PreStmt?
689 NodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
690 for (ExplodedNode *N : DstPreVisit) {
691 ProgramStateRef State = N->getState();
692 if (CE->requiresZeroInitialization()) {
693 // FIXME: Once we properly handle constructors in new-expressions, we'll
694 // need to invalidate the region before setting a default value, to make
695 // sure there aren't any lingering bindings around. This probably needs
696 // to happen regardless of whether or not the object is zero-initialized
697 // to handle random fields of a placement-initialized object picking up
698 // old bindings. We might only want to do it when we need to, though.
699 // FIXME: This isn't actually correct for arrays -- we need to zero-
700 // initialize the entire array, not just the first element -- but our
701 // handling of arrays everywhere else is weak as well, so this shouldn't
702 // actually make things worse. Placement new makes this tricky as well,
703 // since it's then possible to be initializing one part of a multi-
704 // dimensional array.
705 const CXXRecordDecl *TargetHeldRecord =
706 dyn_cast_or_null<CXXRecordDecl>(CE->getType()->getAsRecordDecl());
707
708 if (!TargetHeldRecord || !TargetHeldRecord->isEmpty())
709 State = State->bindDefaultZero(Target, SF);
710 }
711
712 Bldr.generateNode(CE, N, State, /*tag=*/nullptr,
714 }
715 } else {
716 PreInitialized = DstPreVisit;
717 }
718
719 ExplodedNodeSet DstPreCall;
720 getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
721 *Call, *this);
722
723 ExplodedNodeSet DstEvaluated;
724
725 if (CE && CE->getConstructor()->isTrivial() &&
726 CE->getConstructor()->isCopyOrMoveConstructor() &&
727 !CallOpts.IsArrayCtorOrDtor) {
728 // FIXME: Handle other kinds of trivial constructors as well.
729 for (ExplodedNode *N : DstPreCall)
730 performTrivialCopy(DstEvaluated, N, *Call);
731
732 } else {
733 for (ExplodedNode *N : DstPreCall)
734 getCheckerManager().runCheckersForEvalCall(DstEvaluated, N, *Call, *this,
735 CallOpts);
736 }
737
738 // If the CFG was constructed without elements for temporary destructors
739 // and the just-called constructor created a temporary object then
740 // stop exploration if the temporary object has a noreturn constructor.
741 // This can lose coverage because the destructor, if it were present
742 // in the CFG, would be called at the end of the full expression or
743 // later (for life-time extended temporaries) -- but avoids infeasible
744 // paths when no-return temporary destructors are used for assertions.
745 ExplodedNodeSet DstEvaluatedPostProcessed;
746 NodeBuilder Bldr(DstEvaluated, DstEvaluatedPostProcessed, *currBldrCtx);
747 const AnalysisDeclContext *ADC = SF->getAnalysisDeclContext();
749 if (llvm::isa_and_nonnull<CXXTempObjectRegion,
750 CXXLifetimeExtendedObjectRegion>(TargetRegion) &&
752 ->getParent()
753 ->isAnyDestructorNoReturn()) {
754
755 // If we've inlined the constructor, then DstEvaluated would be empty.
756 // In this case we still want a sink, which could be implemented
757 // in processCallExit. But we don't have that implemented at the moment,
758 // so if you hit this assertion, see if you can avoid inlining
759 // the respective constructor when analyzer-config cfg-temporary-dtors
760 // is set to false.
761 // Otherwise there's nothing wrong with inlining such constructor.
762 assert(!DstEvaluated.empty() &&
763 "We should not have inlined this constructor!");
764
765 for (ExplodedNode *N : DstEvaluated) {
766 Bldr.generateSink(E, N, N->getState());
767 }
768
769 // There is no need to run the PostCall and PostStmt checker
770 // callbacks because we just generated sinks on all nodes in th
771 // frontier.
772 return;
773 }
774 }
775
776 ExplodedNodeSet DstPostArgumentCleanup;
777 for (ExplodedNode *I : DstEvaluatedPostProcessed)
778 finishArgumentConstruction(DstPostArgumentCleanup, I, *Call);
779
780 // If there were other constructors called for object-type arguments
781 // of this constructor, clean them up.
782 ExplodedNodeSet DstPostCall;
784 DstPostArgumentCleanup,
785 *Call, *this);
786 getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, E, *this);
787}
788
790 ExplodedNode *Pred,
791 ExplodedNodeSet &Dst) {
792 handleConstructor(CE, Pred, Dst);
793}
794
796 const CXXInheritedCtorInitExpr *CE, ExplodedNode *Pred,
797 ExplodedNodeSet &Dst) {
798 handleConstructor(CE, Pred, Dst);
799}
800
802 const MemRegion *Dest,
803 const Stmt *S,
804 bool IsBaseDtor,
805 ExplodedNode *Pred,
806 ExplodedNodeSet &Dst,
807 EvalCallOptions &CallOpts) {
808 assert(S && "A destructor without a trigger!");
809 const StackFrame *SF = Pred->getStackFrame();
810 ProgramStateRef State = Pred->getState();
811
812 const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
813 assert(RecordDecl && "Only CXXRecordDecls should have destructors");
814 const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
815 // FIXME: There should always be a Decl, otherwise the destructor call
816 // shouldn't have been added to the CFG in the first place.
817 if (!DtorDecl) {
818 // Skip the invalid destructor. We cannot simply return because
819 // it would interrupt the analysis instead.
820 static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
821 // FIXME: PostImplicitCall with a null decl may crash elsewhere anyway.
822 PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), SF,
823 getCFGElementRef(), &T);
824 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
825 Bldr.generateNode(PP, Pred->getState(), Pred);
826 return;
827 }
828
829 if (!Dest) {
830 // We're trying to destroy something that is not a region. This may happen
831 // for a variety of reasons (unknown target region, concrete integer instead
832 // of target region, etc.). The current code makes an attempt to recover.
833 // FIXME: We probably don't really need to recover when we're dealing
834 // with concrete integers specifically.
836 if (const Expr *E = dyn_cast_or_null<Expr>(S)) {
837 Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getStackFrame());
838 } else {
839 static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor");
840 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
841 Bldr.generateSink(Pred->getLocation().withTag(&T),
842 Pred->getState(), Pred);
843 return;
844 }
845 }
846
849 DtorDecl, S, Dest, IsBaseDtor, State, SF, getCFGElementRef());
850
851 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
852 Call->getSourceRange().getBegin(),
853 "Error evaluating destructor");
854
855 ExplodedNodeSet DstPreCall;
856 getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
857 *Call, *this);
858
859 ExplodedNodeSet DstInvalidated;
860 for (ExplodedNode *N : DstPreCall)
861 defaultEvalCall(DstInvalidated, N, *Call, CallOpts);
862
863 getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
864 *Call, *this);
865}
866
868 ExplodedNode *Pred,
869 ExplodedNodeSet &Dst) {
870 ProgramStateRef State = Pred->getState();
871 const StackFrame *SF = Pred->getStackFrame();
872 PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
873 CNE->getBeginLoc(),
874 "Error evaluating New Allocator Call");
877 CEMgr.getCXXAllocatorCall(CNE, State, SF, getCFGElementRef());
878
879 ExplodedNodeSet DstPreCall;
880 getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
881 *Call, *this);
882
883 ExplodedNodeSet DstPostCall;
884 for (ExplodedNode *I : DstPreCall) {
885 // Operator new calls (CXXNewExpr) are intentionally not eval-called,
886 // because it does not make sense to eval-call user-provided functions.
887 // 1) If the new operator can be inlined, then don't prevent it from
888 // inlining by having an eval-call of that operator.
889 // 2) If it can't be inlined, then the default conservative modeling
890 // is what we want anyway.
891 // So the best is to not allow eval-calling CXXNewExprs from checkers.
892 // Checkers can provide their pre/post-call callbacks if needed.
893 defaultEvalCall(DstPostCall, I, *Call);
894 }
895 // If the call is inlined, DstPostCall will be empty and we bail out now.
896
897 // Store return value of operator new() for future use, until the actual
898 // CXXNewExpr gets processed.
899 ExplodedNodeSet DstPostValue;
900 NodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx);
901 for (ExplodedNode *I : DstPostCall) {
902 // FIXME: Because CNE serves as the "call site" for the allocator (due to
903 // lack of a better expression in the AST), the conjured return value symbol
904 // is going to be of the same type (C++ object pointer type). Technically
905 // this is not correct because the operator new's prototype always says that
906 // it returns a 'void *'. So we should change the type of the symbol,
907 // and then evaluate the cast over the symbolic pointer from 'void *' to
908 // the object pointer type. But without changing the symbol's type it
909 // is breaking too much to evaluate the no-op symbolic cast over it, so we
910 // skip it for now.
911 ProgramStateRef State = I->getState();
912 SVal RetVal = State->getSVal(CNE, SF);
913 // [basic.stc.dynamic.allocation] (on the return value of an allocation
914 // function):
915 // "The order, contiguity, and initial value of storage allocated by
916 // successive calls to an allocation function are unspecified."
917 State = State->bindDefaultInitial(RetVal, UndefinedVal{}, SF);
918
919 // If this allocation function is not declared as non-throwing, failures
920 // /must/ be signalled by exceptions, and thus the return value will never
921 // be NULL. -fno-exceptions does not influence this semantics.
922 // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
923 // where new can return NULL. If we end up supporting that option, we can
924 // consider adding a check for it here.
925 // C++11 [basic.stc.dynamic.allocation]p3.
926 if (const FunctionDecl *FD = CNE->getOperatorNew()) {
927 QualType Ty = FD->getType();
928 if (const auto *ProtoType = Ty->getAs<FunctionProtoType>())
929 if (!ProtoType->isNothrow())
930 State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true);
931 }
932
933 ValueBldr.generateNode(CNE, I,
934 addObjectUnderConstruction(State, CNE, SF, RetVal));
935 }
936
937 ExplodedNodeSet DstPostPostCallCallback;
938 getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback,
939 DstPostValue, *Call, *this);
940 for (ExplodedNode *I : DstPostPostCallCallback) {
942 }
943}
944
946 ExplodedNodeSet &Dst) {
947 // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
948 // Also, we need to decide how allocators actually work -- they're not
949 // really part of the CXXNewExpr because they happen BEFORE the
950 // CXXConstructExpr subexpression. See PR12014 for some discussion.
951
952 unsigned blockCount = getNumVisitedCurrent();
953 const StackFrame *SF = Pred->getStackFrame();
954 SVal symVal = UnknownVal();
955 FunctionDecl *FD = CNE->getOperatorNew();
956
957 bool IsStandardGlobalOpNewFunction =
959
960 ProgramStateRef State = Pred->getState();
961
962 // Retrieve the stored operator new() return value.
963 if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
964 symVal = *getObjectUnderConstruction(State, CNE, SF);
965 State = finishObjectConstruction(State, CNE, SF);
966 }
967
968 // We assume all standard global 'operator new' functions allocate memory in
969 // heap. We realize this is an approximation that might not correctly model
970 // a custom global allocator.
971 if (symVal.isUnknown()) {
972 if (IsStandardGlobalOpNewFunction)
973 symVal = svalBuilder.getConjuredHeapSymbolVal(getCFGElementRef(), SF,
974 CNE->getType(), blockCount);
975 else
976 symVal = svalBuilder.conjureSymbolVal(
977 /*symbolTag=*/nullptr, getCFGElementRef(), SF, blockCount);
978 }
979
982 CEMgr.getCXXAllocatorCall(CNE, State, SF, getCFGElementRef());
983
984 if (!AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
985 // Invalidate placement args.
986 // FIXME: Once we figure out how we want allocators to work,
987 // we should be using the usual pre-/(default-)eval-/post-call checkers
988 // here.
989 State = Call->invalidateRegions(blockCount, State);
990 if (!State)
991 return;
992
993 // If this allocation function is not declared as non-throwing, failures
994 // /must/ be signalled by exceptions, and thus the return value will never
995 // be NULL. -fno-exceptions does not influence this semantics.
996 // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
997 // where new can return NULL. If we end up supporting that option, we can
998 // consider adding a check for it here.
999 // C++11 [basic.stc.dynamic.allocation]p3.
1000 if (const auto *ProtoType = FD->getType()->getAs<FunctionProtoType>())
1001 if (!ProtoType->isNothrow())
1002 if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>())
1003 State = State->assume(*dSymVal, true);
1004 }
1005
1006 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
1007
1008 SVal Result = symVal;
1009
1010 if (CNE->isArray()) {
1011
1012 if (const auto *NewReg = cast_or_null<SubRegion>(symVal.getAsRegion())) {
1013 // If each element is initialized by their default constructor, the field
1014 // values are properly placed inside the required region, however if an
1015 // initializer list is used, this doesn't happen automatically.
1016 auto *Init = CNE->getInitializer();
1017 bool isInitList =
1018 isa_and_nonnull<InitListExpr, CXXParenListInitExpr>(Init);
1019
1020 QualType ObjTy =
1021 isInitList ? Init->getType() : CNE->getType()->getPointeeType();
1022 const ElementRegion *EleReg =
1023 MRMgr.getElementRegion(ObjTy, svalBuilder.makeArrayIndex(0), NewReg,
1024 svalBuilder.getContext());
1025 Result = loc::MemRegionVal(EleReg);
1026
1027 // If the array is list initialized, we bind the initializer list to the
1028 // memory region here, otherwise we would lose it.
1029 if (isInitList) {
1030 Bldr.takeNodes(Pred);
1031 Pred = Bldr.generateNode(CNE, Pred, State);
1032
1033 SVal V = State->getSVal(Init, SF);
1034 ExplodedNodeSet evaluated;
1035 evalBind(evaluated, CNE, Pred, Result, V, true);
1036
1037 Bldr.takeNodes(Pred);
1038 Bldr.addNodes(evaluated);
1039
1040 Pred = *evaluated.begin();
1041 State = Pred->getState();
1042 }
1043 }
1044
1045 State = State->BindExpr(CNE, Pred->getStackFrame(), Result);
1046 Bldr.generateNode(CNE, Pred, State);
1047 return;
1048 }
1049
1050 // FIXME: Once we have proper support for CXXConstructExprs inside
1051 // CXXNewExpr, we need to make sure that the constructed object is not
1052 // immediately invalidated here. (The placement call should happen before
1053 // the constructor call anyway.)
1055 // Non-array placement new should always return the placement location.
1056 SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), SF);
1057 Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
1058 CNE->getPlacementArg(0)->getType());
1059 }
1060
1061 // Bind the address of the object, then check to see if we cached out.
1062 State = State->BindExpr(CNE, SF, Result);
1063 ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
1064 if (!NewN)
1065 return;
1066
1067 // If the type is not a record, we won't have a CXXConstructExpr as an
1068 // initializer. Copy the value over.
1069 if (const Expr *Init = CNE->getInitializer()) {
1071 assert(Bldr.getResults().size() == 1);
1072 Bldr.takeNodes(NewN);
1073 evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, SF),
1074 /*FirstInit=*/IsStandardGlobalOpNewFunction);
1075 }
1076 }
1077}
1078
1080 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
1081
1084 CDE, Pred->getState(), Pred->getStackFrame(), getCFGElementRef());
1085
1086 ExplodedNodeSet DstPreCall;
1087 getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, *Call, *this);
1088 ExplodedNodeSet DstPostCall;
1089
1090 if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) {
1091 for (ExplodedNode *I : DstPreCall) {
1092 // Intentionally either inline or conservative eval-call the operator
1093 // delete, but avoid triggering an eval-call event for checkers.
1094 // As detailed at handling CXXNewExprs, in short, because it does not
1095 // really make sense to eval-call user-provided functions.
1096 defaultEvalCall(DstPostCall, I, *Call);
1097 }
1098 } else {
1099 DstPostCall = std::move(DstPreCall);
1100 }
1101 getCheckerManager().runCheckersForPostCall(Dst, DstPostCall, *Call, *this);
1102}
1103
1105 ExplodedNodeSet &Dst) {
1106 const VarDecl *VD = CS->getExceptionDecl();
1107 if (!VD) {
1108 Dst.insert(Pred);
1109 return;
1110 }
1111
1112 const StackFrame *SF = Pred->getStackFrame();
1113 SVal V = svalBuilder.conjureSymbolVal(getCFGElementRef(), SF, VD->getType(),
1115 ProgramStateRef state = Pred->getState();
1116 state = state->bindLoc(state->getLValue(VD, SF), V, SF);
1117
1118 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
1119 Bldr.generateNode(CS, Pred, state);
1120}
1121
1123 ExplodedNodeSet &Dst) {
1124 NodeBuilder Bldr(Pred, Dst, *currBldrCtx);
1125
1126 // Get the this object region from StoreManager.
1127 const StackFrame *SF = Pred->getStackFrame();
1128 const MemRegion *R = svalBuilder.getRegionManager().getCXXThisRegion(
1129 getContext().getCanonicalType(TE->getType()), SF);
1130
1131 ProgramStateRef state = Pred->getState();
1132 SVal V = state->getSVal(loc::MemRegionVal(R));
1133 Bldr.generateNode(TE, Pred, state->BindExpr(TE, SF, V));
1134}
1135
1137 ExplodedNodeSet &Dst) {
1138 const StackFrame *SF = Pred->getStackFrame();
1139
1140 // Get the region of the lambda itself.
1141 const MemRegion *R =
1142 svalBuilder.getRegionManager().getCXXTempObjectRegion(LE, SF);
1144
1145 ProgramStateRef State = Pred->getState();
1146
1147 // If we created a new MemRegion for the lambda, we should explicitly bind
1148 // the captures.
1149 for (auto const [Idx, FieldForCapture, InitExpr] :
1150 llvm::zip(llvm::seq<unsigned>(0, -1), LE->getLambdaClass()->fields(),
1151 LE->capture_inits())) {
1152 SVal FieldLoc = State->getLValue(FieldForCapture, V);
1153
1154 SVal InitVal;
1155 if (!FieldForCapture->hasCapturedVLAType()) {
1156 assert(InitExpr && "Capture missing initialization expression");
1157
1158 // Capturing a 0 length array is a no-op, so we ignore it to get a more
1159 // accurate analysis. If it's not ignored, it would set the default
1160 // binding of the lambda to 'Unknown', which can lead to falsely detecting
1161 // 'Uninitialized' values as 'Unknown' and not reporting a warning.
1162 const auto FTy = FieldForCapture->getType();
1163 if (FTy->isConstantArrayType() &&
1164 getContext().getConstantArrayElementCount(
1165 getContext().getAsConstantArrayType(FTy)) == 0)
1166 continue;
1167
1168 // With C++17 copy elision the InitExpr can be anything, so instead of
1169 // pattern matching all cases, we simple check if the current field is
1170 // under construction or not, regardless what it's InitExpr is.
1171 if (const auto OUC = getObjectUnderConstruction(State, {LE, Idx}, SF)) {
1172 InitVal = State->getSVal(OUC->getAsRegion());
1173
1174 State = finishObjectConstruction(State, {LE, Idx}, SF);
1175 } else
1176 InitVal = State->getSVal(InitExpr, SF);
1177
1178 } else {
1179
1180 assert(!getObjectUnderConstruction(State, {LE, Idx}, SF) &&
1181 "VLA capture by value is a compile time error!");
1182
1183 // The field stores the length of a captured variable-length array.
1184 // These captures don't have initialization expressions; instead we
1185 // get the length from the VLAType size expression.
1186 Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
1187 InitVal = State->getSVal(SizeExpr, SF);
1188 }
1189
1190 State = State->bindLoc(FieldLoc, InitVal, SF);
1191 }
1192
1193 // Decay the Loc into an RValue, because there might be a
1194 // MaterializeTemporaryExpr node above this one which expects the bound value
1195 // to be an RValue.
1196 SVal LambdaRVal = State->getSVal(R);
1197
1198 ExplodedNodeSet Tmp;
1199 NodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
1200 // FIXME: is this the right program point kind?
1201 Bldr.generateNode(LE, Pred, State->BindExpr(LE, SF, LambdaRVal), nullptr,
1203
1204 // FIXME: Move all post/pre visits to ::Visit().
1205 getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
1206}
1207
1209 ExplodedNode *Pred, ExplodedNodeSet &Dst) {
1210 const StackFrame *SF = Pred->getStackFrame();
1211 ExplodedNodeSet CheckerPreStmt;
1212 getCheckerManager().runCheckersForPreStmt(CheckerPreStmt, Pred, A, *this);
1213
1214 ExplodedNodeSet EvalSet;
1215
1216 for (ExplodedNode *N : CheckerPreStmt) {
1217 ProgramStateRef State = N->getState();
1218 for (const auto *Attr : getSpecificAttrs<CXXAssumeAttr>(A->getAttrs())) {
1219 SVal AssumedVal = State->getSVal(Attr->getAssumption(), SF);
1220 // This code ignores assumptions that evaluate to UndefinedVal.
1221 // Perhaps there should be a checker that reports this situation.
1222 if (auto ValidAssumedVal = AssumedVal.getAs<DefinedOrUnknownSVal>()) {
1223 State = State->assume(*ValidAssumedVal, true);
1224 }
1225
1226 if (!State)
1227 break;
1228 }
1229
1230 if (State)
1231 EvalSet.insert(Engine.makePostStmtNode(A, State, N));
1232 }
1233
1234 getCheckerManager().runCheckersForPostStmt(Dst, EvalSet, A, *this);
1235}
Defines the clang::ASTContext interface.
#define V(N, I)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
static ProgramStateRef bindRequiredArrayElementToEnvironment(ProgramStateRef State, const ArrayInitLoopExpr *AILE, const StackFrame *SF, NonLoc Idx)
Defines the PrettyStackTraceEntry class, which is used to make crashes give more contextual informati...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const
Return number of constant array elements.
uint64_t getArrayInitLoopExprElementCount(const ArrayInitLoopExpr *AILE) const
Return number of elements initialized in an ArrayInitLoopExpr.
CFG::BuildOptions & getCFGBuildOptions()
Represents a loop initializing the elements of an array.
Definition Expr.h:5980
OpaqueValueExpr * getCommonExpr() const
Get the common subexpression shared by all initializations (the source array).
Definition Expr.h:5995
Attr - This represents one attribute.
Definition Attr.h:46
Represents an attribute applied to a statement.
Definition Stmt.h:2213
ArrayRef< const Attr * > getAttrs() const
Definition Stmt.h:2245
Represents a function call that returns a C++ object by value.
Definition CFG.h:190
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:113
CXXCatchStmt - This represents a C++ catch block.
Definition StmtCXX.h:29
VarDecl * getExceptionDecl() const
Definition StmtCXX.h:50
Represents a call to a C++ constructor.
Definition ExprCXX.h:1552
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2630
Represents a C++ destructor within a class.
Definition DeclCXX.h:2898
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition ExprCXX.h:1755
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2359
bool isArray() const
Definition ExprCXX.h:2468
Expr * getPlacementArg(unsigned I)
Definition ExprCXX.h:2507
SourceLocation getBeginLoc() const
Definition ExprCXX.h:2610
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2463
Expr * getInitializer()
The initializer of this new-expression.
Definition ExprCXX.h:2537
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1191
Represents the this expression in C++.
Definition ExprCXX.h:1158
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:2949
ConstructionContext's subclasses describe different ways of constructing an object in C++.
virtual const ArrayInitLoopExpr * getArrayInitLoop() const
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2126
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
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:3095
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2029
bool isReplaceableGlobalAllocationFunction(UnsignedOrNone *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions:
Definition Decl.h:2632
bool isReservedGlobalPlacementOperator() const
Determines whether this operator new or delete is one of the reserved global placement operators: voi...
Definition Decl.cpp:3381
Represents a prototype with parameter type info, e.g.
Definition TypeBase.h:5371
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1972
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4920
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4945
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4937
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:4970
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition Expr.h:1234
Stmt * getParent(Stmt *) const
Represents a program point just after an implicit call event.
If a crash happens while one of these objects are live, the message is printed out along with the spe...
ProgramPoint withTag(const ProgramPointTag *tag) const
Create a new ProgramPoint object that is the same as the original except for using the specified tag ...
A (possibly-)qualified type.
Definition TypeBase.h:937
Represents a struct/union/class.
Definition Decl.h:4369
It represents a stack frame of the call stack.
const ParentMap & getParentMap() const
unsigned getIndex() const
LLVM_ATTRIBUTE_RETURNS_NONNULL AnalysisDeclContext * getAnalysisDeclContext() const
const Expr * getCallSite() const
const Decl * getDecl() const
const StackFrame * getParent() const
It might return null.
const CFGBlock * getCallSiteBlock() const
Stmt - This represents one statement.
Definition Stmt.h:86
SourceLocation getEndLoc() const LLVM_READONLY
Definition Stmt.cpp:367
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:789
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:712
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:932
Represents a call to a C++ constructor.
Definition CallEvent.h:990
Manages the lifetime of CallEvent objects.
Definition CallEvent.h:1363
CallEventRef< CXXConstructorCall > getCXXConstructorCall(const CXXConstructExpr *E, const MemRegion *Target, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1440
CallEventRef< CXXAllocatorCall > getCXXAllocatorCall(const CXXNewExpr *E, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1464
CallEventRef< CXXInheritedConstructorCall > getCXXInheritedConstructorCall(const CXXInheritedCtorInitExpr *E, const MemRegion *Target, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1447
CallEventRef< CXXDeallocatorCall > getCXXDeallocatorCall(const CXXDeleteExpr *E, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1471
CallEventRef< CXXDestructorCall > getCXXDestructorCall(const CXXDestructorDecl *DD, const Stmt *Trigger, const MemRegion *Target, bool IsBase, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1455
CallEventRef< ObjCMethodCall > getObjCMethodCall(const ObjCMessageExpr *E, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Definition CallEvent.h:1433
CallEventRef getSimpleCall(const CallExpr *E, ProgramStateRef State, const StackFrame *SF, CFGBlock::ConstCFGElementRef ElemRef)
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
static bool isVariadic(const Decl *D)
Returns true if the given decl is known to be variadic.
void runCheckersForPreCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const CallEvent &Call, ExprEngine &Eng)
Run checkers for pre-visiting function calls (including methods, constructors, destructors etc.
void runCheckersForEvalCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const CallEvent &CE, ExprEngine &Eng, const EvalCallOptions &CallOpts)
Run checkers for evaluating a call.
void runCheckersForPostStmt(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng, bool wasInlined=false)
Run checkers for post-visiting Stmts.
void runCheckersForNewAllocator(const CXXAllocatorCall &Call, ExplodedNodeSet &Dst, ExplodedNode *Pred, ExprEngine &Eng, bool wasInlined=false)
Run checkers between C++ operator new and constructor calls.
void runCheckersForPreStmt(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const Stmt *S, ExprEngine &Eng)
Run checkers for pre-visiting Stmts.
void runCheckersForPostCall(ExplodedNodeSet &Dst, const ExplodedNodeSet &Src, const CallEvent &Call, ExprEngine &Eng, bool wasInlined=false)
Run checkers for post-visiting function calls (including methods, constructors, destructors etc.
ElementRegion is used to represent both array elements and casts.
Definition MemRegion.h:1230
ExplodedNodeSet is a set of ExplodedNode * elements with the invariant that its elements cannot be nu...
void insert(ExplodedNode *N)
const ProgramStateRef & getState() const
ProgramPoint getLocation() const
getLocation - Returns the edge associated with the given node.
const StackFrame * getStackFrame() const
ProgramStateManager & getStateManager()
Definition ExprEngine.h:476
void VisitCXXDestructor(QualType ObjectType, const MemRegion *Dest, const Stmt *S, bool IsBaseDtor, ExplodedNode *Pred, ExplodedNodeSet &Dst, EvalCallOptions &Options)
void VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitLambdaExpr - Transfer function logic for LambdaExprs.
CFGElement getCurrentCFGElement()
Return the CFG element corresponding to the worklist element that is currently being processed by Exp...
Definition ExprEngine.h:770
static std::optional< unsigned > getIndexOfElementToConstruct(ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF)
Retrieves which element is being constructed in a non-POD type array.
ProgramStateRef bindReturnValue(const CallEvent &Call, const StackFrame *SF, ProgramStateRef State)
Create a new state in which the call return value is binded to the call origin expression.
static std::optional< unsigned > getPendingInitLoop(ProgramStateRef State, const CXXConstructExpr *E, const StackFrame *SF)
Retrieves the size of the array in the pending ArrayInitLoopExpr.
ASTContext & getContext() const
getContext - Return the ASTContext associated with this analysis.
Definition ExprEngine.h:214
StoreManager & getStoreManager()
Definition ExprEngine.h:479
void VisitCXXNewAllocatorCall(const CXXNewExpr *CNE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, ExplodedNode *Pred, ExplodedNodeSet &Dst)
Create a C++ temporary object for an rvalue.
ProgramStateRef updateObjectsUnderConstruction(SVal V, const Expr *E, ProgramStateRef State, const StackFrame *SF, const ConstructionContext *CC, const EvalCallOptions &CallOpts)
Update the program state with all the path-sensitive information that's necessary to perform construc...
void defaultEvalCall(ExplodedNodeSet &Dst, ExplodedNode *Pred, const CallEvent &Call, const EvalCallOptions &CallOpts={})
Default implementation of call evaluation.
ConstCFGElementRef getCFGElementRef() const
Definition ExprEngine.h:290
CheckerManager & getCheckerManager() const
Definition ExprEngine.h:223
static std::optional< SVal > getObjectUnderConstruction(ProgramStateRef State, const ConstructionContextItem &Item, const StackFrame *SF)
By looking at a certain item that may be potentially part of an object's ConstructionContext,...
SVal computeObjectUnderConstruction(const Expr *E, ProgramStateRef State, unsigned NumVisitedCaller, const StackFrame *SF, const ConstructionContext *CC, EvalCallOptions &CallOpts, unsigned Idx=0)
Find location of the object that is being constructed by a given constructor.
void VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXConstructExpr(const CXXConstructExpr *E, ExplodedNode *Pred, ExplodedNodeSet &Dst)
void VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E, ExplodedNode *Pred, ExplodedNodeSet &Dst)
unsigned getNumVisitedCurrent() const
Definition ExprEngine.h:299
void VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred, ExplodedNodeSet &Dst)
std::pair< ProgramStateRef, SVal > handleConstructionContext(const Expr *E, ProgramStateRef State, const NodeBuilderContext *BldrCtx, const StackFrame *SF, const ConstructionContext *CC, EvalCallOptions &CallOpts, unsigned Idx=0)
A convenient wrapper around computeObjectUnderConstruction and updateObjectsUnderConstruction.
Definition ExprEngine.h:819
SValBuilder & getSValBuilder()
Definition ExprEngine.h:227
void VisitAttributedStmt(const AttributedStmt *A, ExplodedNode *Pred, ExplodedNodeSet &Dst)
VisitAttributedStmt - Transfer function logic for AttributedStmt.
unsigned getNumVisited(const StackFrame *SF, const CFGBlock *Block) const
Definition ExprEngine.h:294
const ElementRegion * getElementRegion(QualType elementType, NonLoc Idx, const SubRegion *superRegion, const ASTContext &Ctx)
getElementRegion - Retrieve the memory region associated with the associated element type,...
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
This is the simplest builder which generates nodes in the ExplodedGraph.
Definition CoreEngine.h:265
void takeNodes(const ExplodedNodeSet &S)
Definition CoreEngine.h:330
ExplodedNode * generateNode(const ProgramPoint &PP, ProgramStateRef State, ExplodedNode *Pred, bool MarkAsSink=false)
Generates a node in the ExplodedGraph.
ExplodedNode * generateSink(const ProgramPoint &PP, ProgramStateRef State, ExplodedNode *Pred)
Generates a sink in the ExplodedGraph.
Definition CoreEngine.h:300
void addNodes(const ExplodedNodeSet &S)
Definition CoreEngine.h:336
const ExplodedNodeSet & getResults() const
Definition CoreEngine.h:326
CallEventManager & getCallEventManager()
MemRegionManager & getRegionManager()
ProgramStateManager & getStateManager()
NonLoc makeArrayIndex(uint64_t idx)
ASTContext & getContext()
loc::MemRegionVal makeLoc(SymbolRef sym)
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrame *SF)
Return a memory region for the 'this' object reference.
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:57
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition SVals.h:88
const MemRegion * getAsRegion() const
Definition SVals.cpp:119
T castAs() const
Convert to the specified SVal type, asserting that this SVal is of the desired type.
Definition SVals.h:84
bool isUnknown() const
Definition SVals.h:111
SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast)
Evaluates a chain of derived-to-base casts through the path specified in Cast.
Definition Store.cpp:254
SubRegion - A region that subsets another larger region.
Definition MemRegion.h:473
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:562
Definition ARM.cpp:1102
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The JSON file list parser is used to communicate input to InstallAPI.
bool isa(CodeGen::Address addr)
Definition Address.h:330
CXXConstructionKind
Definition ExprCXX.h:1544
StorageDuration
The storage duration for an object (per C++ [basic.stc]).
Definition Specifiers.h:340
@ SD_Thread
Thread storage duration.
Definition Specifiers.h:343
@ SD_Static
Static storage duration.
Definition Specifiers.h:344
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition Specifiers.h:341
@ Result
The result type of a method or function.
Definition TypeBase.h:905
auto getSpecificAttrs(const Container &container)
U cast(CodeGen::Address addr)
Definition Address.h:327
Expr * extractElementInitializerFromNestedAILE(const ArrayInitLoopExpr *AILE)
Definition CFG.cpp:1461
unsigned long uint64_t
Hints for figuring out if a call should be inlined during evalCall().
Definition ExprEngine.h:93
bool IsTemporaryLifetimeExtendedViaAggregate
This call is a constructor for a temporary that is lifetime-extended by binding it to a reference-typ...
Definition ExprEngine.h:108
bool IsTemporaryCtorOrDtor
This call is a constructor or a destructor of a temporary value.
Definition ExprEngine.h:103
bool IsArrayCtorOrDtor
This call is a constructor or a destructor for a single element within an array, a part of array cons...
Definition ExprEngine.h:100
bool IsElidableCtorThatHasNotBeenElided
This call is a pre-C++17 elidable constructor that we failed to elide because we failed to compute th...
Definition ExprEngine.h:115
bool IsCtorOrDtorWithImproperlyModeledTargetRegion
This call is a constructor or a destructor for which we do not currently compute the this-region corr...
Definition ExprEngine.h:96