================
@@ -1784,3 +1784,141 @@ void test_optional_view_arrow() {
     (void)*p;
 }
 } // namespace OwnerArrowOperator
+
+namespace lambda_captures {
+  auto return_ref_capture() {
+    int local = 1;
+    auto lambda = [&local]() { return local; }; // expected-warning {{address 
of stack memory is returned later}}
+    return lambda; // expected-note {{returned here}}
+  }
+
+  void safe_ref_capture() {
+    int local = 1;
+    auto lambda = [&local]() { return local; };
+    lambda();
+  }
+
+  auto capture_int_by_value() {
+    int x = 1;
+    auto lambda = [x]() { return x; };
+    return lambda;
+  }
+
+  auto capture_view_by_value() {
+    MyObj obj;
+    View v(obj); // expected-warning {{address of stack memory is returned 
later}}
+    auto lambda = [v]() { return v; };
+    return lambda; // expected-note {{returned here}}
+  }
+
+  void capture_view_by_value_safe() {
+    MyObj obj;
+    View v(obj);
+    auto lambda = [v]() { return v; };
+    lambda();
+  }
+
+  auto capture_pointer_by_ref() {
+    MyObj obj;
+    MyObj* p = &obj;
+    auto lambda = [&p]() { return p; }; // expected-warning {{address of stack 
memory is returned later}}
+    return lambda; // expected-note {{returned here}}
+  }
+
+  auto capture_multiple() {
+    int a, b;
+    auto lambda = [
+      &a,  // expected-warning {{address of stack memory is returned later}}
+      &b   // expected-warning {{address of stack memory is returned later}}
+    ]() { return a + b; };
+    return lambda; // expected-note 2 {{returned here}}
+  }
+
+  auto capture_raw_pointer_by_value() {
+    int x;
+    int* p = &x; // expected-warning {{address of stack memory is returned 
later}}
+    auto lambda = [p]() { return p; };
+    return lambda; // expected-note {{returned here}}
+  }
+
+  auto capture_raw_pointer_init_capture() {
+    int x;
+    int* p = &x; // expected-warning {{address of stack memory is returned 
later}}
+    auto lambda = [q = p]() { return q; };
+    return lambda; // expected-note {{returned here}}
+  }
+
+  auto capture_view_init_capture() {
+    MyObj obj;
+    View v(obj); // expected-warning {{address of stack memory is returned 
later}}
+    auto lambda = [w = v]() { return w; };
+    return lambda; // expected-note {{returned here}}
+  }
+
+  auto capture_lambda() {
+    int x;
+    auto inner = [&x]() { return x; }; // expected-warning {{address of stack 
memory is returned later}}
+    auto outer = [inner]() { return inner(); };
+    return outer; // expected-note {{returned here}}
+  }
+
+  auto return_copied_lambda() {
+    int local = 1;
+    auto lambda = [&local]() { return local; }; // expected-warning {{address 
of stack memory is returned later}}
+    auto lambda_copy = lambda;
+    return lambda_copy; // expected-note {{returned here}}
+  }
+
+  auto implicit_ref_capture() {
+    int local = 1, local2 = 2;
+    auto lambda = [&]() { return local; }; // expected-warning {{address of 
stack memory is returned later}}
+    return lambda; // expected-note {{returned here}}
+  }
+
+  // TODO: Include the name of the variable in the diagnostic to improve
+  // clarity, especially for implicit lambda captures where multiple warnings
+  // can point to the same source location.
+  auto implicit_ref_capture_multiple() {
+    int local = 1, local2 = 2;
+    auto lambda = [&]() { return local + local2; }; // expected-warning 2 
{{address of stack memory is returned later}}
+    return lambda; // expected-note 2 {{returned here}}
+  }
+
+  auto implicit_value_capture() {
+    MyObj obj;
+    View v(obj); // expected-warning {{address of stack memory is returned 
later}}
+    auto lambda = [=]() { return v; };
+    return lambda; // expected-note {{returned here}}
+  }
+
+  auto* pointer_to_lambda_outlives() {
+    auto lambda = []() { return 42; };
+    return λ // expected-warning {{address of stack memory is returned 
later}} \
+                    // expected-note {{returned here}}
+  }
+
+  auto capture_static() {
+    static int local = 1;
+    // Only automatic storage duration variables may be captured.
+    // Variables with static storage duration behave like globals and are 
directly accessible.
+    // The below lambdas should not capture `local`.
+    auto lambda = [&]() { return local; };
+    auto lambda2 = []() { return local; };
+    lambda2();
+    return lambda;
+  }
+
+  auto capture_static_address_by_value() {
+      static int local = 1;
+      int* p = &local;
+      auto lambda = [p]() { return p; };
+      return lambda;
+  }
+
+  auto capture_static_address_by_ref() {
----------------
aeft wrote:

Yes. Please see `capture_multilevel_pointer`.

https://github.com/llvm/llvm-project/pull/185216
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to