Skip to content

Commit c01b1e4

Browse files
hokeinyuxuanchen1997
authored andcommitted
[clang] Refactor: Introduce a new LifetimeKind for the assignment case, NFC (#99005)
Summary: The current implementation for the assignment case uses a combination of the `LK_Extended` lifetime kind and the validity of `AEntity`, which is somewhat messy and doesn't align well with the intended mental model. This patch introduces a dedicated lifetime kind to handle the assignment case, simplifying the implementation and improving clarity. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251682
1 parent fc17f98 commit c01b1e4

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ enum LifetimeKind {
3939
/// This is a mem-initializer: if it would extend a temporary (other than via
4040
/// a default member initializer), the program is ill-formed.
4141
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,
4247
};
4348
using LifetimeResult =
4449
llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>;
@@ -971,6 +976,8 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
971976
const InitializedEntity *ExtendingEntity,
972977
LifetimeKind LK,
973978
const AssignedEntity *AEntity, Expr *Init) {
979+
assert((AEntity && LK == LK_Assignment) ||
980+
(InitEntity && LK != LK_Assignment));
974981
// If this entity doesn't have an interesting lifetime, don't bother looking
975982
// for temporaries within its initializer.
976983
if (LK == LK_FullExpression)
@@ -1008,19 +1015,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
10081015
return true;
10091016
}
10101017
}
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-
}
10221018

1023-
assert(InitEntity && "only for initialization");
10241019
switch (LK) {
10251020
case LK_FullExpression:
10261021
llvm_unreachable("already handled this");
@@ -1077,6 +1072,17 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
10771072
break;
10781073
}
10791074

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+
}
10801086
case LK_MemInitializer: {
10811087
if (MTE) {
10821088
// Under C++ DR1696, if a mem-initializer (or a default member
@@ -1283,10 +1289,11 @@ void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,
12831289

12841290
void checkExprLifetime(Sema &SemaRef, const AssignedEntity &Entity,
12851291
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);
12901297
}
12911298

12921299
} // namespace clang::sema

0 commit comments

Comments
 (0)