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