@@ -39,6 +39,11 @@ enum LifetimeKind {
39
39
// / This is a mem-initializer: if it would extend a temporary (other than via
40
40
// / a default member initializer), the program is ill-formed.
41
41
LK_MemInitializer,
42
+
43
+ // / The lifetime of a temporary bound to this entity probably ends too soon,
44
+ // / because the entity is a pointer and we assign the address of a temporary
45
+ // / object to it.
46
+ LK_Assignment,
42
47
};
43
48
using LifetimeResult =
44
49
llvm::PointerIntPair<const InitializedEntity *, 3 , LifetimeKind>;
@@ -971,6 +976,8 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
971
976
const InitializedEntity *ExtendingEntity,
972
977
LifetimeKind LK,
973
978
const AssignedEntity *AEntity, Expr *Init) {
979
+ assert ((AEntity && LK == LK_Assignment) ||
980
+ (InitEntity && LK != LK_Assignment));
974
981
// If this entity doesn't have an interesting lifetime, don't bother looking
975
982
// for temporaries within its initializer.
976
983
if (LK == LK_FullExpression)
@@ -1008,19 +1015,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
1008
1015
return true ;
1009
1016
}
1010
1017
}
1011
- if (AEntity) {
1012
- if (!MTE)
1013
- return false ;
1014
- assert (shouldLifetimeExtendThroughPath (Path) ==
1015
- PathLifetimeKind::NoExtend &&
1016
- " No lifetime extension for assignments" );
1017
- if (!pathContainsInit (Path))
1018
- SemaRef.Diag (DiagLoc, diag::warn_dangling_pointer_assignment)
1019
- << AEntity->LHS << DiagRange;
1020
- return false ;
1021
- }
1022
1018
1023
- assert (InitEntity && " only for initialization" );
1024
1019
switch (LK) {
1025
1020
case LK_FullExpression:
1026
1021
llvm_unreachable (" already handled this" );
@@ -1077,6 +1072,17 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
1077
1072
break ;
1078
1073
}
1079
1074
1075
+ case LK_Assignment: {
1076
+ if (!MTE)
1077
+ return false ;
1078
+ assert (shouldLifetimeExtendThroughPath (Path) ==
1079
+ PathLifetimeKind::NoExtend &&
1080
+ " No lifetime extension for assignments" );
1081
+ if (!pathContainsInit (Path))
1082
+ SemaRef.Diag (DiagLoc, diag::warn_dangling_pointer_assignment)
1083
+ << AEntity->LHS << DiagRange;
1084
+ return false ;
1085
+ }
1080
1086
case LK_MemInitializer: {
1081
1087
if (MTE) {
1082
1088
// Under C++ DR1696, if a mem-initializer (or a default member
@@ -1283,10 +1289,11 @@ void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,
1283
1289
1284
1290
void checkExprLifetime (Sema &SemaRef, const AssignedEntity &Entity,
1285
1291
Expr *Init) {
1286
- LifetimeKind LK = LK_FullExpression;
1287
- if (Entity.LHS ->getType ()->isPointerType ()) // builtin pointer type
1288
- LK = LK_Extended;
1289
- checkExprLifetimeImpl (SemaRef, nullptr , nullptr , LK, &Entity, Init);
1292
+ if (!Entity.LHS ->getType ()->isPointerType ()) // builtin pointer type
1293
+ return ;
1294
+ checkExprLifetimeImpl (SemaRef, /* InitEntity=*/ nullptr ,
1295
+ /* ExtendingEntity=*/ nullptr , LK_Assignment, &Entity,
1296
+ Init);
1290
1297
}
1291
1298
1292
1299
} // namespace clang::sema
0 commit comments