================
@@ -4914,3 +4914,68 @@ struct CaptureByThis {
store(local);
}
};
+
+// A destructor or cleanup callback run at scope exit may read the borrows the
+// object holds.
+namespace ScopeExitUse {
+struct [[gsl::Pointer]] Ref {
+ std::string_view sv;
+ Ref() = default;
+ Ref &operator=(std::string_view s [[clang::lifetime_capture_by_this]]);
+ ~Ref();
+};
+
+void dtor_reverse_order() {
+ Ref r;
+ std::string backing;
+ r = backing; // expected-warning {{local variable 'backing' does not live
long enough}}
+} // expected-note {{local variable 'backing' is destroyed here}}
expected-note {{later used by the destructor of 'r' at scope exit}}
+
+void dtor_safe_order() {
+ std::string backing;
+ Ref r;
+ r = backing;
+}
+
+struct [[gsl::Pointer]] TrivialRef {
+ std::string_view sv;
+ TrivialRef &operator=(std::string_view s
[[clang::lifetime_capture_by_this]]);
+};
+void trivial_dtor_no_use() {
+ TrivialRef r;
+ std::string backing;
+ r = backing;
+}
+
+void cleanup_ref(Ref *r);
+void cleanup_reverse_order() {
+ Ref g __attribute__((cleanup(cleanup_ref)));
+ std::string backing;
+ g = backing; // expected-warning {{local variable 'backing' does not live
long enough}}
+} // expected-note {{local variable 'backing' is destroyed here}}
expected-note {{later used by the cleanup function of 'g' at scope exit}}
+
+void cleanup_safe_order() {
+ std::string backing;
+ Ref g __attribute__((cleanup(cleanup_ref)));
+ g = backing;
+}
+
+// The closure is not a gsl::Pointer, but destroying it runs ~Ref().
+void closure_dtor_reads_captured_borrow() {
+ std::function<void()> fn = [] {};
+ std::string backing;
+ Ref r;
+ r = backing; // expected-warning {{local variable 'backing' does not live
long enough}}
+ fn = [r] {};
+} // expected-note {{local variable 'backing' is destroyed here}}
expected-note {{later used by the destructor of 'fn' at scope exit}}
+
+void use(const Ref &);
----------------
usx95 wrote:
I believe we already have a utility for this and this can be removed.
https://github.com/llvm/llvm-project/pull/204650
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits