[clang] [LifetimeSafety] Model GNU statement expressions (PR #204841)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 21 15:36:58 PDT 2026
================
@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wno-gnu-statement-expression -verify %s
+
+// A GNU statement expression (`({ ...; e; })`) yields the value of its final
+// expression `e`. Its origins forward to `e`, and the value is used at the
+// statement expression's program point (after the body's locals expire), so a
+// borrow `e` carries is tracked: a borrow of a body-local is a use-after-scope,
+// and a borrow forwarded from an outer object propagates.
+
+void use(int *p);
+
+// A borrow of a statement-expression-local escaping via the value.
+void borrow_of_local() {
+ int *p = ({ int x = 7; &x; }); // expected-warning {{local variable 'x' does not live long enough}} expected-note {{destroyed here}} expected-note {{later used here}}
+ use(p);
+}
+
+// Forwarding an outer borrow that dangles.
+void forward_outer_borrow() {
+ int *p;
+ {
+ int local = 0;
+ p = ({ (void)0; &local; }); // expected-warning {{local variable 'local' does not live long enough}}
+ } // expected-note {{destroyed here}}
+ use(p); // expected-note {{later used here}}
+}
+
----------------
usx95 wrote:
test suggestion to have the StmtExpr as an lvalue
```cpp
int* r = &(condition ? ({ int x = 7; x; })
: ({ int y = 7; y; });
(void)r;
```
https://github.com/llvm/llvm-project/pull/204841
More information about the cfe-commits
mailing list