Skip to content

[clang] Refactor: Introduce a new LifetimeKind for the assignment case, NFC #99005

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ enum LifetimeKind {
/// This is a mem-initializer: if it would extend a temporary (other than via
/// a default member initializer), the program is ill-formed.
LK_MemInitializer,

/// The lifetime of a temporary bound to this entity probably ends too soon,
/// because the entity is a pointer and we assign the address of a temporary
/// object to it.
LK_Assignment,
};
using LifetimeResult =
llvm::PointerIntPair<const InitializedEntity *, 3, LifetimeKind>;
Expand Down Expand Up @@ -971,6 +976,8 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
const InitializedEntity *ExtendingEntity,
LifetimeKind LK,
const AssignedEntity *AEntity, Expr *Init) {
assert((AEntity && LK == LK_Assignment) ||
(InitEntity && LK != LK_Assignment));
// If this entity doesn't have an interesting lifetime, don't bother looking
// for temporaries within its initializer.
if (LK == LK_FullExpression)
Expand Down Expand Up @@ -1008,19 +1015,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return true;
}
}
if (AEntity) {
if (!MTE)
return false;
assert(shouldLifetimeExtendThroughPath(Path) ==
PathLifetimeKind::NoExtend &&
"No lifetime extension for assignments");
if (!pathContainsInit(Path))
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
<< AEntity->LHS << DiagRange;
return false;
}

assert(InitEntity && "only for initialization");
switch (LK) {
case LK_FullExpression:
llvm_unreachable("already handled this");
Expand Down Expand Up @@ -1077,6 +1072,17 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
break;
}

case LK_Assignment: {
if (!MTE)
return false;
assert(shouldLifetimeExtendThroughPath(Path) ==
PathLifetimeKind::NoExtend &&
"No lifetime extension for assignments");
if (!pathContainsInit(Path))
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
<< AEntity->LHS << DiagRange;
return false;
}
case LK_MemInitializer: {
if (MTE) {
// Under C++ DR1696, if a mem-initializer (or a default member
Expand Down Expand Up @@ -1283,10 +1289,11 @@ void checkExprLifetime(Sema &SemaRef, const InitializedEntity &Entity,

void checkExprLifetime(Sema &SemaRef, const AssignedEntity &Entity,
Expr *Init) {
LifetimeKind LK = LK_FullExpression;
if (Entity.LHS->getType()->isPointerType()) // builtin pointer type
LK = LK_Extended;
checkExprLifetimeImpl(SemaRef, nullptr, nullptr, LK, &Entity, Init);
if (!Entity.LHS->getType()->isPointerType()) // builtin pointer type
return;
checkExprLifetimeImpl(SemaRef, /*InitEntity=*/nullptr,
/*ExtendingEntity=*/nullptr, LK_Assignment, &Entity,
Init);
}

} // namespace clang::sema
Loading