]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Emit G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC() for structs
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 24 Feb 2022 12:45:18 +0000 (13:45 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 21 Mar 2022 08:06:43 +0000 (09:06 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/1293

codegen/valaccodestructmodule.vala

index 2b685383dda5d1537e225f9de31ac2f83d03afd9..dff0d982789ac448f8fb47130cd5c2f5ec2ff9ab 100644 (file)
@@ -142,6 +142,22 @@ public abstract class Vala.CCodeStructModule : CCodeBaseModule {
                        function.add_parameter (new CCodeParameter ("self", get_ccode_name (st) + "*"));
                        decl_space.add_function_declaration (function);
                }
+
+               if (context.profile == Profile.GOBJECT) {
+                       generate_auto_cleanup_clear (st, decl_space);
+               }
+       }
+
+       void generate_auto_cleanup_clear (Struct st, CCodeFile decl_space) {
+               if (st.is_disposable ()
+                   && (!context.use_header || decl_space.is_header)) {
+                       string auto_cleanup_clear_func = get_ccode_destroy_function (st);
+                       if (auto_cleanup_clear_func == null || auto_cleanup_clear_func == "") {
+                               Report.error (st.source_reference, "internal error: auto_cleanup_clear_func not available");
+                       }
+                       decl_space.add_type_member_declaration (new CCodeIdentifier ("G_DEFINE_AUTO_CLEANUP_CLEAR_FUNC (%s, %s)".printf (get_ccode_name (st), auto_cleanup_clear_func)));
+                       decl_space.add_type_member_declaration (new CCodeNewline ());
+               }
        }
 
        public override void visit_struct (Struct st) {