24#include "llvm/ADT/ArrayRef.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/Support/Casting.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/TimeProfiler.h"
31using llvm::isa_and_present;
33OriginList *FactsGenerator::getOriginsList(
const ValueDecl &D) {
34 return FactMgr.getOriginMgr().getOrCreateList(&D);
36OriginList *FactsGenerator::getOriginsList(
const Expr &E) {
37 return FactMgr.getOriginMgr().getOrCreateList(&E);
40bool FactsGenerator::hasOrigins(QualType QT)
const {
41 return FactMgr.getOriginMgr().hasOrigins(QT);
44bool FactsGenerator::hasOrigins(
const Expr *E)
const {
45 return FactMgr.getOriginMgr().hasOrigins(E);
70 const CFGBlock *
Block) {
74 "Dst is non-null but Src is null. List must have the same length");
75 assert(Dst->getLength() == Src->getLength() &&
76 "Lists must have the same length");
79 Fact *F = FactMgr.createFact<OriginFlowFact>(Dst->getOuterOriginID(),
80 Src->getOuterOriginID(), Kill);
82 FactMgr.appendBlockFact(
Block, F);
84 CurrentBlockFacts.push_back(F);
85 Dst = Dst->peelOuterOrigin();
86 Src = Src->peelOuterOrigin();
98 const Expr *IssueExpr) {
109 const Expr *IssueExpr =
nullptr) {
111 IssueExpr ? IssueExpr : MTE);
133 llvm::TimeTraceScope TimeProfile(
"FactGenerator");
134 const CFG &Cfg = *AC.getCFG();
139 CurrentBlockFacts.clear();
140 EscapesInCurrentBlock.clear();
141 CurrentBlock =
Block;
143 CurrentBlockFacts.append(PlaceholderLoanFacts.begin(),
144 PlaceholderLoanFacts.end());
145 for (
unsigned I = 0; I <
Block->size(); ++I) {
147 if (std::optional<CFGStmt> CS = Element.
getAs<
CFGStmt>())
148 Visit(CS->getStmt());
149 else if (std::optional<CFGInitializer>
Initializer =
151 handleCXXCtorInitializer(
Initializer->getInitializer());
152 else if (std::optional<CFGLifetimeEnds> LifetimeEnds =
154 handleLifetimeEnds(*LifetimeEnds);
155 else if (std::optional<CFGFullExprCleanup> FullExprCleanup =
157 handleFullExprCleanup(*FullExprCleanup);
163 CurrentBlockFacts.append(EscapesInCurrentBlock.begin(),
164 EscapesInCurrentBlock.end());
165 FactMgr.addBlockFacts(
Block, CurrentBlockFacts);
167 FactMgr.computePersistentOrigins(Cfg);
182 if (
const auto *VD = dyn_cast<VarDecl>(D))
183 if (
const Expr *InitExpr = VD->getInit()) {
187 OriginList *InitList = getOriginsList(*InitExpr);
188 assert(InitList &&
"VarDecl had origins but InitExpr did not");
189 flow(VDList, InitList,
true);
211 "gl-value DRE of non-pointer type should have an origin list");
214 CurrentBlockFacts.push_back(
221 handleGSLPointerConstruction(CCE);
232 flow(getOriginsList(*CCE), ArgList,
true);
242 flow(getOriginsList(*CCE), ArgList,
true);
246 handleFunctionCall(CCE,
false);
254 const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(
260 CurrentBlockFacts.push_back(
266 killAndFlowOrigin(*DIE, *
Init);
273 killAndFlowOrigin(*FD, *CII->
getInit());
278 if (OriginList *InitList = getOriginsList(*CII->
getInit()))
279 CurrentBlockFacts.push_back(FactMgr.
createFact<OriginFlowFact>(
281 InitList->getOuterOriginID(),
false));
290 handleFunctionCall(MCE,
true);
294 if (
const auto *ME = dyn_cast<MemberExpr>(MCE->
getCallee());
295 ME && !ME->isArrow() &&
300 handleFunctionCall(MCE,
false);
309 assert(List &&
"gl-value static member should have an origin list");
310 CurrentBlockFacts.push_back(
315 assert(ME->
isGLValue() &&
"Field member should be GL value");
317 assert(Dst &&
"Field member should have an origin list as it is GL value");
319 assert(Src &&
"Base expression should be a pointer/reference type");
329 handleFunctionCall(CE);
343 OriginList *Src = Dest ? getOriginsList(*SubExpr) :
nullptr;
346 case CK_LValueToRValue:
350 flow(Dest, readValue(SubExpr),
true);
352 case CK_NullToPointer:
356 case CK_ConstructorConversion:
357 case CK_UserDefinedConversion:
358 flow(Dest, Src,
true);
362 handleAccess(SubExpr);
364 case CK_UncheckedDerivedToBase:
365 case CK_DerivedToBase:
366 case CK_BaseToDerived:
372 flow(Dest, Src,
true);
374 case CK_ArrayToPointerDecay:
379 assert(Src &&
"Array expression should have origins as it is GL value");
383 case CK_FunctionToPointerDecay:
384 case CK_BuiltinFnToFnPtr:
391 flow(Dest, Src,
true);
393 case CK_LValueToRValueBitCast:
394 case CK_NonAtomicToAtomic:
395 case CK_AtomicToNonAtomic: {
404 flow(Dest, RVSrc,
true);
424 "Taking address of void is not valid in C++");
429 killAndFlowOrigin(*UO, *SubExpr);
434 killAndFlowOrigin(*UO, *SubExpr);
444 flow(getOriginsList(*UO), readValue(SubExpr),
true);
458 flow(getOriginsList(*UO),
469 if (
OriginList *List = getOriginsList(*RetExpr))
472 L->getOuterOriginID(), RetExpr));
476void FactsGenerator::handleAssignment(
const Expr *TargetExpr,
478 const Expr *RHSExpr) {
480 handleAccess(LHSExpr);
484 if (
const auto *DRE_LHS = dyn_cast<DeclRefExpr>(LHSExpr)) {
485 LHSList = getOriginsList(*DRE_LHS);
486 assert(LHSList &&
"LHS is a DRE and should have an origin list");
491 if (
const auto *ME_LHS = dyn_cast<MemberExpr>(LHSExpr)) {
492 LHSList = getOriginsList(*ME_LHS);
493 assert(LHSList &&
"LHS is a MemberExpr and should have an origin list");
503 CurrentBlockFacts.push_back(
504 FactMgr.
createFact<KillOriginFact>(LHSInner->getOuterOriginID()));
515 flow(getOriginsList(*TargetExpr), LHSList,
true);
518void FactsGenerator::handlePointerArithmetic(
const BinaryOperator *BO) {
519 if (Expr *RHS = BO->getRHS(); RHS->getType()->isPointerType()) {
520 killAndFlowOrigin(*BO, *RHS);
523 Expr *LHS = BO->getLHS();
524 assert(LHS->getType()->isPointerType() &&
525 "Pointer arithmetic must have a pointer operand");
526 killAndFlowOrigin(*BO, *LHS);
542 ? getOriginsList(*BO->
getLHS())
543 : readValue(BO->
getLHS());
550 killAndFlowOrigin(*BO, *BO->
getRHS());
554 handleAccess(BO->
getLHS());
560 flow(getOriginsList(*BO), IsCMode ? LHSList->
peelOuterOrigin() : LHSList,
566 handlePointerArithmetic(BO);
573 const Expr *ArmExpr) {
577 if (
const auto *OVE = dyn_cast<OpaqueValueExpr>(
Target))
578 if (
const Expr *Src = OVE->getSourceExpr())
585 if (
auto CS = Elt.getAs<
CFGStmt>())
586 if (
const auto *E = dyn_cast<Expr>(CS->getStmt()))
609 flow(getOriginsList(*CO), getOriginsList(*TrueExpr),
true, TBPred);
611 flow(getOriginsList(*CO), getOriginsList(*FalseExpr),
true,
615bool FactsGenerator::handleAssignmentOperatorCall(
const CallExpr *CE,
617 const Expr *RHSExpr) {
619 if (!hasOrigins(LHSTy))
636 if (
const auto *MD = dyn_cast_or_null<CXXMethodDecl>(CE->
getDirectCallee());
637 MD && MD->isDefaulted()) {
648 handleAssignmentOperatorCall(OCE, OCE->
getArg(0), OCE->
getArg(1)))
651 handleFunctionCall(OCE);
658 if (handleTestPoint(FCE))
664 if (!hasOrigins(ILE))
671 if (!hasOrigins(ILE->
getInit(0)))
673 killAndFlowOrigin(*ILE, *ILE->
getInit(0));
689 assert((!SubExprList ||
691 "MTE top level origin should contain a loan to the MTE itself");
694 flow(RValMTEList, SubExprList,
true);
697 CurrentBlockFacts.push_back(
705 ExtendedTemporaries[ExtendingDecl].push_back(MTE);
710 if (
C.capturesThis())
711 FactMgr.setThisCapturedByLambda();
712 else if (
C.capturesVariable() &&
C.getCapturedVar()->isInitCapture()) {
718 if (
const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
719 FactMgr.addCapturedField(FD);
731 for (
const Expr *
Init : LE->capture_inits()) {
754 assert(ASE->
isGLValue() &&
"Array subscript should be a GL value");
756 assert(Dst &&
"Array subscript should have origins as it is a GL value");
758 assert(Src &&
"Base of array subscript should have origins");
763bool FactsGenerator::handlePlacementNew(
const CXXNewExpr *NE,
769 if (NE->getNumPlacementArgs() != 1)
773 if (OperatorNew->getNumParams() <= 1)
777 OperatorNew->getParamDecl(1)->getType()->getAs<
PointerType>();
778 if (!Arg || !Arg->isVoidPointerType())
783 const Expr *PlacementArg = NE->getPlacementArg(0);
784 if (
const auto *ICE = dyn_cast<ImplicitCastExpr>(PlacementArg);
785 ICE && ICE->getCastKind() == CK_BitCast &&
786 PlacementArg->getType()->isVoidPointerType())
787 PlacementArg = ICE->getSubExpr();
788 OriginList *PlacementList = getOriginsList(*PlacementArg);
792 handleAccess(PlacementArg);
804 const Expr *
Init = NE->getInitializer();
806 bool HandledAsPlacementNew =
false;
807 if (NE->getNumPlacementArgs() == 1)
808 HandledAsPlacementNew = handlePlacementNew(NE, NewList);
813 if (!HandledAsPlacementNew &&
814 (NE->getNumPlacementArgs() == 0 ||
815 (OperatorNew && OperatorNew->isReplaceableGlobalAllocationFunction()))) {
817 CurrentBlockFacts.push_back(
823 if (!NewList || !
Init)
829 flow(NewList, InitList,
true);
857 CurrentBlockFacts.push_back(
866 if (!CS || CS->body_empty())
868 const auto *
Last = dyn_cast<Expr>(CS->body_back());
873 flow(Dst, Src,
true);
876bool FactsGenerator::escapesViaReturn(
OriginID OID)
const {
877 return llvm::any_of(EscapesInCurrentBlock, [OID](
const Fact *F) {
879 return EF->getEscapedOriginID() == OID;
884void FactsGenerator::handleLifetimeEnds(
const CFGLifetimeEnds &LifetimeEnds) {
892 bool HasCleanup = LifetimeEndsVD->
hasAttr<CleanupAttr>();
893 bool IsDestructorUse =
897 if (HasCleanup || IsDestructorUse)
898 if (OriginList *List = getOriginsList(*LifetimeEndsVD))
899 CurrentBlockFacts.push_back(FactMgr.
createFact<UseFact>(
906 std::optional<OriginID> ExpiredOID;
907 if (OriginList *List = getOriginsList(*LifetimeEndsVD)) {
911 if (!escapesViaReturn(OID))
914 CurrentBlockFacts.push_back(FactMgr.
createFact<ExpireFact>(
915 AccessPath(LifetimeEndsVD), EndLoc, ExpiredOID));
916 if (
auto It = ExtendedTemporaries.find(LifetimeEndsVD);
917 It != ExtendedTemporaries.end())
918 for (
const MaterializeTemporaryExpr *MTE : It->second)
919 CurrentBlockFacts.push_back(
920 FactMgr.
createFact<ExpireFact>(AccessPath(MTE), EndLoc));
923void FactsGenerator::handleFullExprCleanup(
924 const CFGFullExprCleanup &FullExprCleanup) {
925 for (
const auto *MTE : FullExprCleanup.getExpiringMTEs())
926 CurrentBlockFacts.push_back(FactMgr.createFact<ExpireFact>(
927 AccessPath(MTE), FullExprCleanup.getCleanupLoc()));
930void FactsGenerator::handleExitBlock() {
931 bool IsDestructor = isa_and_nonnull<CXXDestructorDecl>(AC.getDecl());
932 for (
const Origin &O : FactMgr.getOriginMgr().getOrigins())
935 if (
auto *FD = dyn_cast_if_present<FieldDecl>(O.getDecl());
937 EscapesInCurrentBlock.push_back(
938 FactMgr.createFact<FieldEscapeFact>(O.ID, FD));
939 else if (
auto *VD = dyn_cast_if_present<VarDecl>(O.getDecl())) {
942 if (VD->hasGlobalStorage()) {
943 EscapesInCurrentBlock.push_back(
944 FactMgr.createFact<GlobalEscapeFact>(O.ID, VD));
954 if (
auto ThisOrigins = FactMgr.getOriginMgr().getThisOrigins();
955 ThisOrigins && !IsDestructor)
956 EscapesInCurrentBlock.push_back(FactMgr.createFact<ThisEscapeFact>(
957 (*ThisOrigins)->getOuterOriginID(), AC.getDecl()->getEndLoc()));
963 if (
const auto *Fn = dyn_cast_if_present<FunctionDecl>(AC.getDecl()))
964 for (
const ParmVarDecl *PVD :
Fn->parameters()) {
965 OriginList *List = getOriginsList(*PVD);
966 if (!List || !PVD->getType()->isReferenceType())
968 for (OriginList *Inner = List->peelOuterOrigin(); Inner;
969 Inner = Inner->peelOuterOrigin())
970 EscapesInCurrentBlock.push_back(FactMgr.createFact<ParamEscapeFact>(
971 Inner->getOuterOriginID(), PVD));
975void FactsGenerator::handleGSLPointerConstruction(
const CXXConstructExpr *CCE) {
979 if (CCE->getNumArgs() != 1) {
980 handleFunctionCall(CCE,
true);
984 const Expr *Arg = CCE->getArg(0);
990 OriginList *ArgList = readValue(Arg);
991 assert(ArgList &&
"GSL pointer argument should have an origin list");
992 flow(getOriginsList(*CCE), ArgList,
true);
993 }
else if (Arg->getType()->isPointerType()) {
998 OriginList *ArgList = getOriginsList(*Arg);
999 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1000 getOriginsList(*CCE)->getOuterOriginID(), ArgList->getOuterOriginID(),
1005 handleFunctionCall(CCE,
true);
1009void FactsGenerator::handleMovedArgsInCall(
const FunctionDecl *FD,
1010 ArrayRef<const Expr *> Args) {
1014 if (
const auto *MD = dyn_cast<CXXMethodDecl>(FD);
1016 const Expr *UniquePtrExpr = Args[0];
1017 OriginList *MovedOrigins = getOriginsList(*UniquePtrExpr);
1019 CurrentBlockFacts.push_back(FactMgr.createFact<MovedOriginFact>(
1020 UniquePtrExpr, MovedOrigins->getOuterOriginID()));
1023 for (
unsigned I = 0; I < Args.size(); ++I) {
1030 if (PVD->isExplicitObjectParameter())
1032 if (!PVD->getType()->isRValueReferenceType())
1037 if (PVD->hasAttr<LifetimeBoundAttr>() ||
1038 PVD->hasAttr<LifetimeCaptureByAttr>())
1040 const Expr *Arg = Args[I];
1041 OriginList *MovedOrigins = getOriginsList(*Arg);
1042 assert(MovedOrigins->getLength() >= 1 &&
1043 "unexpected length for r-value reference param");
1045 CurrentBlockFacts.push_back(FactMgr.createFact<MovedOriginFact>(
1046 Arg, MovedOrigins->getOuterOriginID()));
1050void FactsGenerator::handleInvalidatingCall(
const Expr *
Call,
1051 const FunctionDecl *FD,
1052 ArrayRef<const Expr *> Args) {
1053 const auto *MD = dyn_cast<CXXMethodDecl>(FD);
1054 if (!MD || !MD->isInstance())
1064 if (
const auto *ME = dyn_cast<MemberExpr>(Obj);
1068 OriginList *ThisList = getOriginsList(*Args[0]);
1070 CurrentBlockFacts.push_back(FactMgr.createFact<InvalidateOriginFact>(
1071 ThisList->getOuterOriginID(),
Call));
1074void FactsGenerator::handleDestructiveCall(
const Expr *
Call,
1075 const FunctionDecl *FD,
1076 ArrayRef<const Expr *> Args) {
1079 OriginList *ArgList = getOriginsList(*Args[0]);
1081 CurrentBlockFacts.push_back(FactMgr.createFact<InvalidateOriginFact>(
1082 ArgList->getOuterOriginID(),
Call));
1085void FactsGenerator::handleImplicitObjectFieldUses(
const Expr *
Call,
1086 const FunctionDecl *FD) {
1087 const auto *MemberCall = dyn_cast_or_null<CXXMemberCallExpr>(
Call);
1091 if (!isa_and_present<CXXThisExpr>(
1095 const auto *MD = dyn_cast<CXXMethodDecl>(FD);
1096 assert(MD &&
"Function must be a CXXMethodDecl for member calls");
1098 const auto *ClassDecl = MD->getParent()->getDefinition();
1102 const auto UseFields = [&](
const CXXRecordDecl *RD) {
1103 for (
const auto *Field : RD->fields())
1104 if (
auto *FieldList = getOriginsList(*Field))
1105 CurrentBlockFacts.push_back(
1106 FactMgr.createFact<UseFact>(*
Call, FieldList));
1109 UseFields(ClassDecl);
1111 ClassDecl->forallBases([&](
const CXXRecordDecl *Base) {
1117void FactsGenerator::handleLifetimeCaptureBy(
const FunctionDecl *FD,
1118 ArrayRef<const Expr *> Args) {
1124 auto getArgCaptureByAttr =
1125 [FD](
unsigned I) -> std::pair<const LifetimeCaptureByAttr *, QualType> {
1130 return {PVD->getAttr<LifetimeCaptureByAttr>(), PVD->getType()};
1131 return {
nullptr, QualType()};
1133 for (
unsigned I = 0; I < Args.size(); ++I) {
1134 auto [Attr, ParamType] = getArgCaptureByAttr(I);
1137 OriginList *CapturedOriginList = getOriginsList(*Args[I]);
1138 if (!CapturedOriginList)
1142 if (!ParamType.isNull() &&
1143 (ParamType->isReferenceType() &&
1145 CapturedOriginList->getLength() > 1)
1146 CapturedOriginList = CapturedOriginList->peelOuterOrigin();
1147 for (
int CapturingArgIdx : Attr->params()) {
1149 if (CapturingArgIdx == LifetimeCaptureByAttr::Global ||
1150 CapturingArgIdx == LifetimeCaptureByAttr::Unknown ||
1151 CapturingArgIdx == LifetimeCaptureByAttr::Invalid)
1154 if (CapturingArgIdx != LifetimeCaptureByAttr::This &&
1155 (CapturingArgIdx < 0 ||
1156 static_cast<size_t>(CapturingArgIdx) >= Args.size()))
1158 const Expr *CapturedByArg = Args[CapturingArgIdx];
1159 assert(CapturedByArg &&
"Capturer expression must be valid");
1161 OriginList *Dest = readValue(CapturedByArg);
1168 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1169 Dest->getOuterOriginID(), CapturedOriginList->getOuterOriginID(),
1175void FactsGenerator::handleFreeingCall(
const Expr *
Call,
const FunctionDecl *FD,
1176 ArrayRef<const Expr *> Args) {
1179 auto InvalidateArg = [&](
unsigned ArgIndex) {
1180 assert(ArgIndex < Args.size() &&
"Arg index should be in bounds");
1182 OriginList *ArgList = getOriginsList(*Args[ArgIndex]);
1185 CurrentBlockFacts.push_back(FactMgr.createFact<InvalidateOriginFact>(
1186 ArgList->getOuterOriginID(),
Call));
1189 bool HasOwnershipTakesAttr =
false;
1190 for (
const OwnershipAttr *Attr : FD->specific_attrs<OwnershipAttr>()) {
1191 if (Attr->getOwnKind() != OwnershipAttr::Takes)
1193 HasOwnershipTakesAttr =
true;
1194 for (
const ParamIdx &Idx : Attr->args()) {
1202 if (!HasOwnershipTakesAttr) {
1203 assert(!Args.empty() &&
"freeing builtins take at least one argument");
1208void FactsGenerator::handleAllocatingCall(
const Expr *
Call,
1209 const FunctionDecl *FD) {
1212 const auto *CE = dyn_cast<CallExpr>(
Call);
1215 OriginList *CallList = getOriginsList(*
Call);
1219 CurrentBlockFacts.push_back(
1220 FactMgr.createFact<IssueFact>(L->getID(), CallList->getOuterOriginID()));
1223void FactsGenerator::handleFunctionCall(
const Expr *
Call,
1224 bool IsGslConstruction) {
1225 FunctionCallInfo CallInfo(
Call);
1226 llvm::ArrayRef<const Expr *> Args = CallInfo.Args;
1228 for (
const Expr *Arg : Args)
1232 OriginList *CallList = getOriginsList(*
Call);
1237 handleInvalidatingCall(
Call, FD, Args);
1238 handleDestructiveCall(
Call, FD, Args);
1239 handleMovedArgsInCall(FD, Args);
1240 handleImplicitObjectFieldUses(
Call, FD);
1241 handleLifetimeCaptureBy(FD, Args);
1242 handleAllocatingCall(
Call, FD);
1243 handleFreeingCall(
Call, FD, Args);
1247 assert(Args.size() == 1 &&
1248 "std reference cast builtins take exactly one argument");
1251 flow(CallList, getOriginsList(*Args[0]),
true);
1254 auto shouldTrackPointerImplicitObjectArg = [FD, &Args](
unsigned I) ->
bool {
1268 bool KillSrc =
true;
1269 for (
unsigned I = 0; I < Args.size(); ++I) {
1270 OriginList *ArgList = getOriginsList(*Args[I]);
1274 if (IsGslConstruction) {
1278 assert(!Args[I]->isGLValue() || ArgList->getLength() >= 2);
1279 ArgList = readValue(Args[I]);
1282 (Args.size() == 1 || ShouldTrackArg)) {
1286 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1287 CallList->getOuterOriginID(), ArgList->getOuterOriginID(),
1290 }
else if (ShouldTrackArg) {
1297 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1298 CallList->getOuterOriginID(), ArgList->getOuterOriginID(),
1302 }
else if (shouldTrackPointerImplicitObjectArg(I)) {
1303 assert(ArgList->getLength() >= 2 &&
1304 "Object arg of pointer type should have at least two origins");
1306 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1307 CallList->getOuterOriginID(),
1308 ArgList->peelOuterOrigin()->getOuterOriginID(), KillSrc));
1310 }
else if (ShouldTrackArg) {
1314 CurrentBlockFacts.push_back(FactMgr.createFact<OriginFlowFact>(
1315 CallList->getOuterOriginID(), ArgList->getOuterOriginID(), KillSrc));
1323bool FactsGenerator::handleTestPoint(
const CXXFunctionalCastExpr *FCE) {
1324 if (!FCE->getType()->isVoidType())
1328 if (
const auto *SL = dyn_cast<StringLiteral>(SubExpr)) {
1329 llvm::StringRef LiteralValue = SL->getString();
1330 const std::string Prefix =
"__lifetime_test_point_";
1332 if (LiteralValue.starts_with(Prefix)) {
1333 StringRef Annotation = LiteralValue.drop_front(Prefix.length());
1334 CurrentBlockFacts.push_back(
1335 FactMgr.createFact<TestPointFact>(Annotation));
1342bool FactsGenerator::namesDeclStorage(
const OriginList *List)
const {
1343 return FactMgr.getOriginMgr()
1344 .getOrigin(List->getOuterOriginID())
1348void FactsGenerator::handleAccess(
const Expr *E) {
1349 OriginList *List = getOriginsList(*E);
1350 if (!List || namesDeclStorage(List))
1352 CurrentBlockFacts.push_back(FactMgr.createFact<UseFact>(
1354 FactMgr.getOriginMgr().createSingleOriginList(List->getOuterOriginID())));
1357void FactsGenerator::handleUse(
const Expr *E) {
1358 OriginList *List = getOriginsList(*E);
1359 if (List && namesDeclStorage(List))
1360 List = List->peelOuterOrigin();
1362 CurrentBlockFacts.push_back(FactMgr.createFact<UseFact>(*E, List));
1368void FactsGenerator::issueFieldPlaceholderLoans(
1369 const CXXRecordDecl *ClassDef, llvm::SmallVectorImpl<Fact *> &Facts) {
1370 assert(ClassDef &&
"class methods have a defined parent");
1372 assert(ClassDef->isCompleteDefinition() || ClassDef->isBeingDefined());
1373 auto IssueForRecord = [&](
const CXXRecordDecl *RD) {
1374 for (
const FieldDecl *Field : RD->fields()) {
1379 if (
Field->isAnonymousStructOrUnion()) {
1380 if (
const CXXRecordDecl *Sub =
Field->getType()->getAsCXXRecordDecl()) {
1381 if (
const CXXRecordDecl *SubDef =
Sub->getDefinition()) {
1384 issueFieldPlaceholderLoans(SubDef, Facts);
1389 OriginList *FieldList = getOriginsList(*Field);
1392 const Loan *L = FactMgr.getLoanMgr().createPlaceholderLoan(Field);
1393 Facts.push_back(FactMgr.createFact<IssueFact>(
1394 L->getID(), FieldList->getOuterOriginID()));
1398 IssueForRecord(ClassDef);
1399 ClassDef->forallBases(IssueForRecord);
1405llvm::SmallVector<Fact *> FactsGenerator::issuePlaceholderLoans() {
1406 const auto *FD = dyn_cast<FunctionDecl>(AC.getDecl());
1410 llvm::SmallVector<Fact *> PlaceholderLoanFacts;
1411 if (
auto ThisOrigins = FactMgr.getOriginMgr().getThisOrigins()) {
1412 OriginList *List = *ThisOrigins;
1414 const Loan *L = FactMgr.getLoanMgr().createPlaceholderLoan(MD);
1415 PlaceholderLoanFacts.push_back(
1416 FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
1418 issueFieldPlaceholderLoans(MD->getParent()->getDefinition(),
1419 PlaceholderLoanFacts);
1421 for (
const ParmVarDecl *PVD : FD->parameters()) {
1422 OriginList *List = getOriginsList(*PVD);
1425 const Loan *L = FactMgr.getLoanMgr().createPlaceholderLoan(PVD);
1426 PlaceholderLoanFacts.push_back(
1427 FactMgr.createFact<IssueFact>(L->getID(), List->getOuterOriginID()));
1429 return PlaceholderLoanFacts;
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
Defines the clang::Expr interface and subclasses for C++ expressions.
static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal, QualType LValType, APValue &Val)
Perform an assignment of Val to LVal. Takes ownership of Val.
Defines an enumeration for C++ overloaded operators.
QualType getBaseElementType(const ArrayType *VAT) const
Return the innermost element type of an array type.
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
ASTContext & getASTContext() const
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
bool isOutputPlusConstraint(unsigned i) const
isOutputPlusConstraint - Return true if the specified output constraint is a "+" constraint (which is...
unsigned getNumOutputs() const
A builtin binary operation expression such as "x + y" or "x <= y".
static bool isAdditiveOp(Opcode Opc)
static bool isAssignmentOp(Opcode Opc)
static bool isCompoundAssignmentOp(Opcode Opc)
Represents a single basic block in a source-level CFG.
Represents a top-level expression in a basic block.
std::optional< T > getAs() const
Convert to the specified CFGElement type, returning std::nullopt if this CFGElement is not of the des...
Represents C++ base or member initializer from constructor's initialization list.
Represents the point where the lifetime of an automatic object ends.
const VarDecl * getVarDecl() const
LLVM_ATTRIBUTE_RETURNS_NONNULL const Stmt * getTriggerStmt() const
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Represents binding an expression to a temporary.
const Expr * getSubExpr() const
Represents a call to a C++ constructor.
Expr * getArg(unsigned Arg)
Return the specified argument.
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Represents a C++ base or member initializer.
Expr * getInit() const
Get the initializer.
FieldDecl * getAnyMember() const
A default argument (C++ [dcl.fct.default]).
A use of a default initializer in a constructor or in aggregate initialization.
Expr * getExpr()
Get the initialization expression that will be used.
Represents a delete expression for memory deallocation and destructor calls, e.g.
Represents an explicit C++ type conversion that uses "functional" notation (C++ [expr....
Represents a call to a member function that may be written either with member call syntax (e....
CXXMethodDecl * getMethodDecl() const
Retrieve the declaration of the called method.
Expr * getImplicitObjectArgument() const
Retrieve the implicit object argument for the member call.
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
The null pointer literal (C++11 [lex.nullptr])
A call to an overloaded operator written using operator syntax.
OverloadedOperatorKind getOperator() const
Returns the kind of overloaded operator that this expression refers to.
A C++ throw-expression (C++ [except.throw]).
const Expr * getSubExpr() const
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Expr * getExprOperand() const
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
CastKind getCastKind() const
A reference to a declared variable, function, enum, etc.
NamedDecl * getFoundDecl()
Get the NamedDecl through which this reference occurred.
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Decl - This represents one declaration (or definition), e.g.
bool isFunctionOrFunctionTemplate() const
Whether this declaration is a function or function template.
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Expr * IgnoreParenBaseCasts() LLVM_READONLY
Skip past any parentheses and derived-to-base casts until reaching a fixed point.
Represents a member of a struct/union/class.
Represents a function declaration or definition.
bool isDefaulted() const
Whether this function is defaulted.
OverloadedOperatorKind getOverloadedOperator() const
getOverloadedOperator - Which C++ overloaded operator this function represents, if any.
This represents a GCC inline-assembly statement extension.
Expr * getOutputExpr(unsigned i)
Describes an C or C++ initializer list.
unsigned getNumInits() const
const Expr * getInit(unsigned Init) const
Describes the capture of a variable or of this, or of a C++1y init-capture.
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
PointerType - C99 6.7.5.1 - Pointer Declarators.
A (possibly-)qualified type.
DestructionKind isDestructedType() const
Returns a nonzero value if objects of this type require non-trivial work to clean up after.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Encodes a location in the source.
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
CompoundStmt * getSubStmt()
RetTy Visit(PTR(Stmt) S, ParamTys... P)
SourceLocation getEndLoc() const LLVM_READONLY
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
bool isPointerType() const
bool isPointerOrReferenceType() const
bool isFunctionType() const
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Expr * getSubExpr() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
static UseSite scopeExit(SourceLocation Loc, const VarDecl &VD, UseKind Kind)
Represents the storage location being borrowed, e.g., a specific stack variable or a field within it:...
FactType * createFact(Args &&...args)
OriginManager & getOriginMgr()
LoanManager & getLoanMgr()
An abstract base class for a single, atomic lifetime-relevant event.
void VisitDeclRefExpr(const DeclRefExpr *DRE)
void VisitBinaryOperator(const BinaryOperator *BO)
void VisitCallExpr(const CallExpr *CE)
void VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *MTE)
void VisitCastExpr(const CastExpr *CE)
void VisitReturnStmt(const ReturnStmt *RS)
void VisitCXXConstructExpr(const CXXConstructExpr *CCE)
void VisitCXXTypeidExpr(const CXXTypeidExpr *TE)
void VisitMemberExpr(const MemberExpr *ME)
void VisitAbstractConditionalOperator(const AbstractConditionalOperator *CO)
Visits conditional operators (e.g., cond ?
void VisitCXXDeleteExpr(const CXXDeleteExpr *DE)
void VisitCXXNewExpr(const CXXNewExpr *NE)
void VisitCXXFunctionalCastExpr(const CXXFunctionalCastExpr *FCE)
void VisitGCCAsmStmt(const GCCAsmStmt *AS)
void VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *DIE)
void VisitInitListExpr(const InitListExpr *ILE)
void VisitLambdaExpr(const LambdaExpr *LE)
void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *DAE)
void VisitStmtExpr(const StmtExpr *SE)
void VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *N)
void VisitCXXOperatorCallExpr(const CXXOperatorCallExpr *OCE)
void VisitArraySubscriptExpr(const ArraySubscriptExpr *ASE)
void VisitUnaryOperator(const UnaryOperator *UO)
void VisitCXXThrowExpr(const CXXThrowExpr *TE)
void VisitDeclStmt(const DeclStmt *DS)
void VisitCXXMemberCallExpr(const CXXMemberCallExpr *MCE)
void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *BTE)
Represents that an origin's storage has been invalidated by a container operation (e....
Loan * createLoan(AccessPath Path, const Expr *IssueExpr=nullptr)
Represents a component of an access path: either a named field access or an abstract unnamed interior...
A list of origins representing levels of indirection for pointer-like types.
OriginID getOuterOriginID() const
OriginList * peelOuterOrigin() const
std::optional< OriginList * > getThisOrigins() const
Returns the OriginList for the implicit 'this' parameter if the current declaration is an instance me...
Represents that an origin escapes via a return statement.
bool Sub(InterpState &S, CodePtr OpPC)
utils::ID< struct OriginTag > OriginID
static const Loan * createLoan(FactManager &FactMgr, const ValueDecl &VD, const Expr *IssueExpr)
Creates a loan for the storage path of a given declaration.
static const Loan * createNewAllocLoan(FactManager &FactMgr, const CallExpr *CE)
Creates a loan for a heap allocation made by a call (e.g.
static const CFGBlock * findPredBlockForExpr(const CFGBlock *MergeBlock, const Expr *ArmExpr)
bool doesDeclHaveStorage(const ValueDecl *D)
Returns true if the declaration has its own storage that can be borrowed.
const Expr * ignoreParensAndImpCasts(const Expr *E)
bool isFreeingFunction(const FunctionDecl &FD)
bool isGslPointerType(QualType QT)
bool isStdCallableWrapperType(const CXXRecordDecl *RD)
bool shouldTrackImplicitObjectArg(const Expr &ImplicitObjectArgument, const CXXMethodDecl *Callee, bool RunningUnderLifetimeSafety)
bool isPointerLikeType(QualType QT)
bool hasObjectArg(const FunctionDecl &FD)
bool isUniquePtrRelease(const CXXMethodDecl &MD)
bool isStdReferenceCast(const FunctionDecl *FD)
bool hasImplicitObjectArg(const FunctionDecl &FD)
const FunctionDecl * getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD)
bool isInvalidationMethod(const CXXMethodDecl &MD)
bool destructsFirstArg(const FunctionDecl &FD)
bool isGslOwnerType(QualType QT)
std::optional< LifetimeBoundParamInfo > getTrackedArgInfo(const FunctionDecl *FD, llvm::ArrayRef< const Expr * > Args, unsigned I)
bool isAllocatingFunction(const FunctionDecl &FD)
const LifetimeCaptureByAttr * getCaptureByAttrFromFunctionType(const FunctionDecl *FD)
const ParmVarDecl * getParamForArg(const FunctionDecl &FD, unsigned I)
bool isa(CodeGen::Address addr)
@ SD_Automatic
Automatic storage duration (most local variables).
U cast(CodeGen::Address addr)
bool CheckDestructorUses
Whether to model non-trivial destructors at scope exit as uses.