Skip to content

Commit c3079ff

Browse files
authored
[clang] Don't emit the warn_dangling_lifetime_pointer diagnostic for the assignment case. (#97408)
The `lifetime_pointer` case is handled before the assignment case. In scenarios where we have the `gsl::Pointer` attribute, we may emit the `-Wdangling-gsl` warning for assignment cases. This means we cannot use `-Wno-dangling-assignment` to suppress the newly-added warning, this patch fixes it.
1 parent 66d5ca2 commit c3079ff

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,19 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
10081008
return true;
10091009
}
10101010
}
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+
}
10111022

1023+
assert(InitEntity && "only for initialization");
10121024
switch (LK) {
10131025
case LK_FullExpression:
10141026
llvm_unreachable("already handled this");
@@ -1031,8 +1043,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
10311043

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

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

1063-
if (InitEntity) {
1064-
SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
1065-
<< RK << !InitEntity->getParent()
1066-
<< ExtendingEntity->getDecl()->isImplicit()
1067-
<< ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
1068-
} else {
1069-
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
1070-
<< AEntity->LHS << DiagRange;
1071-
}
1071+
SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
1072+
<< RK << !InitEntity->getParent()
1073+
<< ExtendingEntity->getDecl()->isImplicit()
1074+
<< ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
10721075
break;
10731076
}
10741077
break;
10751078
}
10761079

10771080
case LK_MemInitializer: {
1078-
assert(InitEntity && "Expect only on initializing the entity");
1079-
10801081
if (MTE) {
10811082
// Under C++ DR1696, if a mem-initializer (or a default member
10821083
// initializer used by the absence of one) would lifetime-extend a
@@ -1151,7 +1152,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
11511152
}
11521153

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

11701170
case LK_Return:
11711171
case LK_StmtExprResult:
1172-
assert(InitEntity && "Expect only on initializing the entity");
11731172
if (auto *DRE = dyn_cast<DeclRefExpr>(L)) {
11741173
// We can't determine if the local variable outlives the statement
11751174
// expression.

clang/test/SemaCXX/warn-dangling-local.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,14 @@ void g() {
2626
const int a[] = {a[0]};
2727
const int b[] = {a[0]};
2828
}
29+
30+
namespace std {
31+
// std::basic_string has a hard-coded gsl::owner attr.
32+
struct basic_string {
33+
const char* c_str();
34+
};
35+
} // namespace std
36+
void test(const char* a) {
37+
// verify we're emitting the `-Wdangling-assignment` warning.
38+
a = std::basic_string().c_str(); // expected-warning {{object backing the pointer a will be destroyed at the end of the full-expression}}
39+
}

0 commit comments

Comments
 (0)