Skip to content

[clang] Don't emit the warn_dangling_lifetime_pointer diagnostic for the assignment case. #97408

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 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
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
33 changes: 16 additions & 17 deletions clang/lib/Sema/CheckExprLifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,19 @@ 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 All @@ -1031,8 +1043,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,

switch (shouldLifetimeExtendThroughPath(Path)) {
case PathLifetimeKind::Extend:
assert(InitEntity && "Lifetime extension should happen only for "
"initialization and not assignment");
// Update the storage duration of the materialized temporary.
// FIXME: Rebuild the expression instead of mutating it.
MTE->setExtendingDecl(ExtendingEntity->getDecl(),
Expand All @@ -1041,8 +1051,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return true;

case PathLifetimeKind::ShouldExtend:
assert(InitEntity && "Lifetime extension should happen only for "
"initialization and not assignment");
// We're supposed to lifetime-extend the temporary along this path (per
// the resolution of DR1815), but we don't support that yet.
//
Expand All @@ -1060,23 +1068,16 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
if (pathContainsInit(Path))
return false;

if (InitEntity) {
SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
<< RK << !InitEntity->getParent()
<< ExtendingEntity->getDecl()->isImplicit()
<< ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
} else {
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
<< AEntity->LHS << DiagRange;
}
SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
<< RK << !InitEntity->getParent()
<< ExtendingEntity->getDecl()->isImplicit()
<< ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
break;
}
break;
}

case LK_MemInitializer: {
assert(InitEntity && "Expect only on initializing the entity");

if (MTE) {
// Under C++ DR1696, if a mem-initializer (or a default member
// initializer used by the absence of one) would lifetime-extend a
Expand Down Expand Up @@ -1151,7 +1152,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
}

case LK_New:
assert(InitEntity && "Expect only on initializing the entity");
if (isa<MaterializeTemporaryExpr>(L)) {
if (IsGslPtrInitWithGslTempOwner)
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
Expand All @@ -1169,7 +1169,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,

case LK_Return:
case LK_StmtExprResult:
assert(InitEntity && "Expect only on initializing the entity");
if (auto *DRE = dyn_cast<DeclRefExpr>(L)) {
// We can't determine if the local variable outlives the statement
// expression.
Expand Down
11 changes: 11 additions & 0 deletions clang/test/SemaCXX/warn-dangling-local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@ void g() {
const int a[] = {a[0]};
const int b[] = {a[0]};
}

namespace std {
// std::basic_string has a hard-coded gsl::owner attr.
struct basic_string {
const char* c_str();
};
} // namespace std
void test(const char* a) {
// verify we're emitting the `-Wdangling-assignment` warning.
a = std::basic_string().c_str(); // expected-warning {{object backing the pointer a will be destroyed at the end of the full-expression}}
}
Loading