[clang] [LifetimeSafety] Revamp how accesses and uses are handled (PR #225799)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Thu Sep 24 05:52:42 PDT 2026
================
@@ -4318,6 +4319,324 @@ void foo() {
// expected-note {{result of call to 'get' aliases the storage of local variable 'o' because the implicit object parameter is inferred as lifetimebound}}
up = std::move(o); // expected-note {{potentially moved here}}
} // expected-note {{local variable 'o' is destroyed here}}
- (void)*p; // expected-note {{later used here}}
+ use(*p); // expected-note {{later used here}}
}
} // namespace TakeOwnershipTests
+
+//===----------------------------------------------------------------------===//
+// What counts as a use
+//
+// A use is an access through an lvalue: reading it (an lvalue-to-rvalue
+// conversion) or writing through it. The loans of the accessed lvalue say which
+// objects it may name, so a dereference needs no special handling. Taking an
+// address, naming a variable, or copying a pointer out of one is not an access.
+//===----------------------------------------------------------------------===//
+
+namespace std { class type_info; }
+
+namespace what_is_a_use {
+struct Node {
+ int id;
+ Node *next;
+};
+
+void copying_a_pointer_is_not_a_use() {
+ Node *p;
+ {
+ Node local;
+ p = &local; // expected-warning {{local variable 'local' does not live long enough}}
+ } // expected-note {{local variable 'local' is destroyed here}}
+ Node *q = p; // Reads p, not *p.
+ // expected-note at -1 {{local variable 'p' aliases the storage of local variable 'local'}}
+ use(q); // expected-note {{later used here}}
+}
+
+void taking_an_address_is_not_a_use() {
+ Node *p;
+ {
+ Node local;
+ p = &local;
+ }
+ Node **pp = &p; // no-warning: borrows p's storage, never reads it.
----------------
usx95 wrote:
Could you please add a test that `use(pp)` and we issue a warning here. Maybe same for `reborrow` and `pnext` even if it doesn't work right away.
https://github.com/llvm/llvm-project/pull/225799
More information about the cfe-commits
mailing list