[clang] [analyzer][NFC] Add test cases for the lifetime test suite (PR #212254)

Balázs Benics via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 27 07:01:11 PDT 2026


================
@@ -235,17 +289,74 @@ void outer() {
 }
 
 int *danglingLocal() {
-  S s;
-  return s.get();
+  S s; // expected-note {{'s' initialized here}}
+  return s.get(); // expected-note {{Returning value bound to 's' that will go out of scope}}
   // expected-warning at -1 {{Returning value bound to 's' that will go out of scope}}
   // expected-warning at -2 {{Address of stack memory associated with local variable 's' returned}}
-  // expected-warning at -3 {{address of stack memory associated with local variable 's' returned}}
+  // expected-note at -3    {{Address of stack memory associated with local variable 's' returned to caller}}
+  // expected-warning at -4 {{address of stack memory associated with local variable 's' returned}}
 }
 
 int *danglingParam(S param) {
   return param.get();
   // expected-warning at -1 {{Returning value bound to 'param' that will go out of scope}}
-  // expected-warning at -2 {{Address of stack memory associated with local variable 'param' returned}}
-  // expected-warning at -3 {{address of stack memory associated with parameter 'param' returned}}
+  // expected-note at -2    {{Returning value bound to 'param' that will go out of scope}}
+  // expected-warning at -3 {{Address of stack memory associated with local variable 'param' returned}}
+  // expected-note at -4    {{Address of stack memory associated with local variable 'param' returned to caller}}
+  // expected-warning at -5 {{address of stack memory associated with parameter 'param' returned}}
+}
+
+int *getFieldPtr(Pair &p [[clang::lifetimebound]]) { return &p.a; }
+
+int *field_subobject_dangling() {
+  Pair pair{3, 5}; // expected-note {{'pair' initialized here}}
+  return getFieldPtr(pair);
+  // expected-warning at -1 {{Returning value bound to 'pair' that will go out of scope}}
+  // expected-note at -2    {{Returning value bound to 'pair' that will go out of scope}}
+  // expected-warning at -3 {{Address of stack memory associated with local variable 'pair' returned to caller}}
+  // expected-note at -4    {{Address of stack memory associated with local variable 'pair' returned to caller}}
+  // expected-warning at -5    {{address of stack memory associated with local variable 'pair' returned}}
----------------
steakhal wrote:

It hurts to see this misaligned from the rest of the expectations.

https://github.com/llvm/llvm-project/pull/212254


More information about the cfe-commits mailing list