You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Clang] Extend lifetime bound analysis to support assignments for the built-in pointer type (#96475)
The lifetime bound warning in Clang currently only considers
initializations. This patch extends the warning to include assignments.
- **Support for assignments of built-in pointer types**: this is done is
by reusing the existing statement-local implementation. Clang now warns
if the pointer is assigned to a temporary object that being destoryed at
the end of the full assignment expression.
With this patch, we will detect more cases under the on-by-default
diagnostic `-Wdangling`. I have added a new category for this specific
diagnostic so that people can temporarily disable it if their codebase
is not yet clean.
This is the first step to address #63310, focusing only on pointer
types. Support for C++ assignment operators will come in a follow-up
patch.
Fixes #54492
Copy file name to clipboardExpand all lines: clang/test/SemaCXX/attr-lifetimebound.cpp
+7Lines changed: 7 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,13 @@ namespace usage_ok {
40
40
int *p = A().class_member(); // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
41
41
int *q = A(); // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}}
42
42
int *r = A(1); // expected-warning {{temporary whose address is used as value of local variable 'r' will be destroyed at the end of the full-expression}}
43
+
44
+
voidtest_assignment() {
45
+
p = A().class_member(); // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
46
+
p = {A().class_member()}; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
47
+
q = A(); // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}
48
+
r = A(1); // expected-warning {{object backing the pointer r will be destroyed at the end of the full-expression}}
Copy file name to clipboardExpand all lines: clang/test/SemaCXX/warn-dangling-local.cpp
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,10 @@ using T = int[];
4
4
5
5
voidf() {
6
6
int *p = &(int&)(int&&)0; // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}}
7
+
p = &(int&)(int&&)0; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}}
7
8
8
9
int *q = (int *const &)T{1, 2, 3}; // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}}
10
+
q = (int *const &)T{1, 2, 3}; // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}}
9
11
10
12
// FIXME: We don't warn here because the 'int*' temporary is not const, but
11
13
// it also can't have actually changed since it was created, so we could
0 commit comments