Skip to content

Commit a6d6c00

Browse files
authored
[clang] Lifetimebound in assignment operator should work for non-gsl annotated types. (#113180)
This issue is identified during the discussion of [this comment](#112234 (comment)). There will be no release note for this fix as it is a follow-up to [#106997](#106997).
1 parent deecfa9 commit a6d6c00

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10188,7 +10188,7 @@ def warn_new_dangling_initializer_list : Warning<
1018810188
"will be destroyed at the end of the full-expression">,
1018910189
InGroup<DanglingInitializerList>;
1019010190
def warn_dangling_pointer_assignment : Warning<
10191-
"object backing the pointer %0 "
10191+
"object backing %select{|the pointer }0%1 "
1019210192
"will be destroyed at the end of the full-expression">,
1019310193
InGroup<DanglingAssignment>;
1019410194

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,13 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
12061206
assert(shouldLifetimeExtendThroughPath(Path) ==
12071207
PathLifetimeKind::NoExtend &&
12081208
"No lifetime extension for assignments");
1209-
SemaRef.Diag(DiagLoc,
1210-
IsGslPtrValueFromGslTempOwner
1211-
? diag::warn_dangling_lifetime_pointer_assignment
1212-
: diag::warn_dangling_pointer_assignment)
1213-
<< AEntity->LHS << DiagRange;
1209+
if (IsGslPtrValueFromGslTempOwner)
1210+
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer_assignment)
1211+
<< AEntity->LHS << DiagRange;
1212+
else
1213+
SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
1214+
<< AEntity->LHS->getType()->isPointerType() << AEntity->LHS
1215+
<< DiagRange;
12141216
return false;
12151217
}
12161218
case LK_MemInitializer: {
@@ -1412,8 +1414,14 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
14121414
};
14131415

14141416
llvm::SmallVector<IndirectLocalPathEntry, 8> Path;
1415-
if (LK == LK_Assignment && shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity))
1416-
Path.push_back({IndirectLocalPathEntry::GslPointerAssignment, Init});
1417+
if (LK == LK_Assignment &&
1418+
shouldRunGSLAssignmentAnalysis(SemaRef, *AEntity)) {
1419+
Path.push_back(
1420+
{isAssignmentOperatorLifetimeBound(AEntity->AssignmentOperator)
1421+
? IndirectLocalPathEntry::LifetimeBoundCall
1422+
: IndirectLocalPathEntry::GslPointerAssignment,
1423+
Init});
1424+
}
14171425

14181426
if (Init->isGLValue())
14191427
visitLocalsRetainedByReferenceBinding(Path, Init, RK_ReferenceBinding,

clang/test/SemaCXX/attr-lifetimebound.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ struct StatusOr {
330330
};
331331

332332
void test(StatusOr<FooView> foo1, StatusOr<NonAnnotatedFooView> foo2) {
333-
foo1 = Foo(); // expected-warning {{object backing the pointer foo1 will be destroyed at the end}}
334-
// No warning on non-gsl annotated types.
335-
foo2 = NonAnnotatedFoo();
333+
foo1 = Foo(); // expected-warning {{object backing the foo1 will be destroyed at the end}}
334+
// This warning is triggered by the lifetimebound annotation, regardless of whether the class type is annotated with GSL.
335+
foo2 = NonAnnotatedFoo(); // expected-warning {{object backing the foo2 will be destroyed at the end}}
336336
}
337337
} // namespace GH106372

0 commit comments

Comments
 (0)