After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 766370 - Add a macro for initializing g_auto(GVariantBuilder)
Add a macro for initializing g_auto(GVariantBuilder)
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: gvariant
unspecified
Other Linux
: Normal normal
: ---
Assigned To: Allison Karlitskaya (desrt)
gtkdev
Depends on:
Blocks:
 
 
Reported: 2016-05-13 11:30 UTC by Krzesimir Nowak
Modified: 2016-10-25 16:08 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Adds the G_VARIANT_BUILDER_INIT macro. (1.81 KB, patch)
2016-05-13 11:30 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_BUILDER_CLEARED macro. (1.83 KB, patch)
2016-05-13 12:38 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_BUILDER_CLEARED macro. (1.84 KB, patch)
2016-05-13 16:06 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_DICT_CLEARED macro. (1.65 KB, patch)
2016-05-13 16:06 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_BUILDER_INIT macro. (8.78 KB, patch)
2016-05-13 16:08 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_BUILDER_INIT macro. (7.82 KB, patch)
2016-05-13 20:16 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_DICT_INIT macro. (8.23 KB, patch)
2016-05-13 20:17 UTC, Krzesimir Nowak
none Details | Review
Adds the G_VARIANT_BUILDER_INIT macro. (7.77 KB, patch)
2016-06-04 13:11 UTC, Krzesimir Nowak
committed Details | Review
Adds the G_VARIANT_DICT_INIT macro. (8.30 KB, patch)
2016-06-04 13:11 UTC, Krzesimir Nowak
committed Details | Review
Fixx C++ issues with gvariant.h (2.55 KB, patch)
2016-07-21 10:28 UTC, Krzesimir Nowak
committed Details | Review

Description Krzesimir Nowak 2016-05-13 11:30:58 UTC
Created attachment 327782 [details] [review]
Adds the G_VARIANT_BUILDER_INIT macro.

A macro like G_VARIANT_BUILDER_INIT would be useful for scenarios like:

g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_INIT;

if (!foo)
  return;

g_variant_builder_init (&builder, ...)

Currently I did the initialization with {0,}, but then a pickier compiler started warning about missing branches (should be {{0,}}). So for having a clear situation it would be great to have a macro like G_VALUE_INIT for the GValue type.

The attached patch specifies it.
Comment 1 Allison Karlitskaya (desrt) 2016-05-13 12:09:21 UTC
I don't like that after you assign to _INIT, you must use _init().

I think we could make a better version of this that takes the type as an argument and writes it into the structure in a place that causes the builder to be initialised on first use.

This might be a bit tricky because we will need to have a constant as the type, which means that we cannot use G_VARIANT_TYPE() here.  Perhaps we should allow the possibility of giving a type string directly.

In any case, it would be fast, since it only involves putting a pointer to a string into a struct.
Comment 2 Krzesimir Nowak 2016-05-13 12:38:38 UTC
Created attachment 327790 [details] [review]
Adds the G_VARIANT_BUILDER_CLEARED macro.

For now I'm attaching a revised patch that renames the _INIT macro to _CLEARED, so we don't have _init after _INIT.
Comment 3 Krzesimir Nowak 2016-05-13 16:06:10 UTC
Created attachment 327803 [details] [review]
Adds the G_VARIANT_BUILDER_CLEARED macro.
Comment 4 Krzesimir Nowak 2016-05-13 16:06:38 UTC
Created attachment 327804 [details] [review]
Adds the G_VARIANT_DICT_CLEARED macro.
Comment 5 Krzesimir Nowak 2016-05-13 16:08:49 UTC
Created attachment 327805 [details] [review]
Adds the G_VARIANT_BUILDER_INIT macro.

I just attached three patches, one adds the G_VARIANT_BUILDER_CLEARED macro, one adds the same, but for GVariantDicts and the last adds the G_VARIANT_BUILDER_INIT(variant_type_string).

The last one is a bit rough on the edges and I have still to add some tests. But initial reviews are very welcome.
Comment 6 Allison Karlitskaya (desrt) 2016-05-13 16:43:42 UTC
Review of attachment 327805 [details] [review]:

Great job.  This is pretty much exactly what I had in mind, but I think the changes introduced to the .c file are a bit too drastic.  In particular, I'd take this approach instead:

1) keep the current is_valid_builder() macro

2) add a new function called ensure_valid_builder() which calls is_valid_builder() first, returning TRUE if it is already valid.  if it's not true, then check for x[0] == PARTIAL_MAGIC and read x[1] as a 'const gchar *' via GSIZE_TO_POINTER.  Read from the public struct type here, since this is the one that was filled in.  Don't rely on the congruences between it and the members of the private struct (which someone may move around some day).  Use G_VARIANT_TYPE() to cast that string (with checks) to a GVariantType and just pass that directly to the normal g_variant_builder_init() call.  No need to add a new internal helper for this.  Finally, return is_valid_builder() [which will return false if either G_VARIANT_TYPE() or g_variant_builder_init() had failed for some reason).

3) replace all is_valid_builder() calls with ensure_valid_builder() calls.


We could also discuss another alternative if you have an improved counterproposal.

::: glib/gvariant.h
@@ +35,3 @@
+
+/* magic parts, do not use */
+#define GVSB_MAGIC_PARTIAL ((gsize) 2942751021u)

I don't like this as a public define, even though the alternative is duplication (which I usually like even less).  Please handle it this way instead:

 1) hardcode the value into the _INIT macro

 2) move this define for GVSB_MAGIC_PARTIAL to the .c file

 3) make sure these are kept in sync by way of a testcase which uses G_VARIANT_BUILDER_INIT

@@ +371,3 @@
+ *
+ * |[
+ *   g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_INIT("ay");

I think it might also be nice to allow type constants here, like G_VARIANT_TYPE_VARDICT for example.  I'd also prefer more type safety, so that if you pass things that are neither 'const gchar *' nor 'const GVariantType *' then you will get a warning about it.  Those two goals are at odds with each other, but there is probably some way to get what we want, possibly involving a transparent union...
Comment 7 Allison Karlitskaya (desrt) 2016-05-13 16:44:53 UTC
Review of attachment 327803 [details] [review]:

I think this is not very important if we end up with the _INIT macro.
Comment 8 Krzesimir Nowak 2016-05-13 20:15:40 UTC
(In reply to Allison Ryan Lortie (desrt) from comment #6)
> Review of attachment 327805 [details] [review] [review]:
> 
> Great job.  This is pretty much exactly what I had in mind, but I think the
> changes introduced to the .c file are a bit too drastic.  In particular, I'd
> take this approach instead:
> 
> 1) keep the current is_valid_builder() macro
> 
> 2) add a new function called ensure_valid_builder() which calls
> is_valid_builder() first, returning TRUE if it is already valid.  if it's
> not true, then check for x[0] == PARTIAL_MAGIC and read x[1] as a 'const
> gchar *' via GSIZE_TO_POINTER.  Read from the public struct type here, since
> this is the one that was filled in.  Don't rely on the congruences between
> it and the members of the private struct (which someone may move around some
> day).  Use G_VARIANT_TYPE() to cast that string (with checks) to a
> GVariantType and just pass that directly to the normal
> g_variant_builder_init() call.  No need to add a new internal helper for
> this.  Finally, return is_valid_builder() [which will return false if either
> G_VARIANT_TYPE() or g_variant_builder_init() had failed for some reason).
> 

I would still like to ensure that x[2-15] are zeros to avoid a case, when we call g_variant_builder_open (...) and parent is set to address that is equal to our magic number. This is why I was making sure that GVSB(builder)->magic == 0.

> 3) replace all is_valid_builder() calls with ensure_valid_builder() calls.
> 
> 
> We could also discuss another alternative if you have an improved
> counterproposal.
> 

The plan looks good, it is not very different from what I did in the first place. This scenario has fewer changes even (SLOC-wise).

> ::: glib/gvariant.h
> @@ +35,3 @@
> +
> +/* magic parts, do not use */
> +#define GVSB_MAGIC_PARTIAL ((gsize) 2942751021u)
> 
> I don't like this as a public define, even though the alternative is
> duplication (which I usually like even less).  Please handle it this way
> instead:
> 
>  1) hardcode the value into the _INIT macro
> 
>  2) move this define for GVSB_MAGIC_PARTIAL to the .c file
> 
>  3) make sure these are kept in sync by way of a testcase which uses
> G_VARIANT_BUILDER_INIT
> 

Sure, added.

> @@ +371,3 @@
> + *
> + * |[
> + *   g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_INIT("ay");
> 
> I think it might also be nice to allow type constants here, like
> G_VARIANT_TYPE_VARDICT for example.  I'd also prefer more type safety, so
> that if you pass things that are neither 'const gchar *' nor 'const
> GVariantType *' then you will get a warning about it.  Those two goals are
> at odds with each other, but there is probably some way to get what we want,
> possibly involving a transparent union...

I was able to make it a bit more type-safe by adding a union. But I have no idea how to make the macro to accept only const gchar * and const GVariantType * without warnings.
Comment 9 Krzesimir Nowak 2016-05-13 20:16:48 UTC
Created attachment 327826 [details] [review]
Adds the G_VARIANT_BUILDER_INIT macro.
Comment 10 Krzesimir Nowak 2016-05-13 20:17:34 UTC
Created attachment 327827 [details] [review]
Adds the G_VARIANT_DICT_INIT macro.

Attached also a patch with the G_VARIANT_DICT_INIT macro.
Comment 11 Krzesimir Nowak 2016-05-14 19:50:31 UTC
Ah, and about _CLEARED macros - I dunno, they might be usable in scenarios like:

g_auto(GVariantBuilder) builder = G_VARIANT_BUILDER_CLEARED;

if (do_dict)
  g_variant_builder_init (&builder, G_VARIANT_TYPE("a{sv}"));
else if (do_pseudo_dict)
  g_variant_builder_init (&builder, G_VARIANT_TYPE("a(sv)"));
else
  return;

But arguably this is pretty strained reason. So, if you want, I'll drop the patches introducing the _CLEARED macros and drop the use of them in the patches that add the _INIT macros.

Also, one catch with _INIT macros is the lifetime issue of the parameter passed to the macro:

GVariantType *type = get_copy_of_some_variant_type ();
GVariantBuilder builder = G_VARIANT_BUILDER_INIT (type);

g_variant_type_free (type);
/* GVariantBuilder will try to copy the freed variant type */
g_variant_builder_add_value (&builder, ...);
Comment 12 Allison Karlitskaya (desrt) 2016-05-16 12:24:48 UTC
As far as I'm concerned, this macro is only intended for use with type strings or G_VARIANT_TYPE_VARDICT-style constants (either defined by GLib, or by the user in the same way).  ie: static strings.  We should make the docs very clear on that point.

I also consider the case you list above (pseudo_dict) to be pretty borderline -- particularly considering it would mean that you have to open/close the builder many times... or make use of the pseudo_dict flag elsewhere.  If someone really wants to do that then they can avoid the g_auto() or just clear it manually, or make sure they avoid the early-return.

I'm still holding out hope that one day we will get an attribute in GCC that says "initialise this local variable to all-zeros unless otherwise specified" that we will be able to use for g_auto()....
Comment 13 Krzesimir Nowak 2016-05-16 13:14:56 UTC
(In reply to Allison Ryan Lortie (desrt) from comment #12)
> As far as I'm concerned, this macro is only intended for use with type
> strings or G_VARIANT_TYPE_VARDICT-style constants (either defined by GLib,
> or by the user in the same way).  ie: static strings.  We should make the
> docs very clear on that point.

Sure. What about G_VARIANT_DICT_INIT? It has similar issue with its GVariant* parameter, but without similar solution (other than documenting, that is).

> 
> I also consider the case you list above (pseudo_dict) to be pretty
> borderline -- particularly considering it would mean that you have to
> open/close the builder many times... or make use of the pseudo_dict flag
> elsewhere.  If someone really wants to do that then they can avoid the
> g_auto() or just clear it manually, or make sure they avoid the early-return.
> 

Yeah. Alright, I'll remove the _CLEARED macros.

> I'm still holding out hope that one day we will get an attribute in GCC that
> says "initialise this local variable to all-zeros unless otherwise
> specified" that we will be able to use for g_auto()....

Yup, would be cool to have that.
Comment 14 Krzesimir Nowak 2016-06-04 13:11:26 UTC
Created attachment 329119 [details] [review]
Adds the G_VARIANT_BUILDER_INIT macro.
Comment 15 Krzesimir Nowak 2016-06-04 13:11:51 UTC
Created attachment 329120 [details] [review]
Adds the G_VARIANT_DICT_INIT macro.
Comment 16 Krzesimir Nowak 2016-06-04 13:13:40 UTC
Updated the patches. I dropped the _CLEARED macros. Updated the docs for both G_VARIANT_BUILDER_INIT and G_VARIANT_DICT_INIT, but the docs for the latter are a bit mouthy, so suggestions for improvements are very welcome.
Comment 17 Kjell Ahlstedt 2016-07-17 13:17:15 UTC
When I try to build glibmm, I now get the errors

/opt/gnome/include/glib-2.0/glib/gvariant.h:306:5: error: ISO C++ prohibits anonymous structs [-Werror=pedantic]
     };
     ^
/opt/gnome/include/glib-2.0/glib/gvariant.h:445:5: error: ISO C++ prohibits anonymous structs [-Werror=pedantic]
     };
     ^

It's easy to fix. Like this, for instance:

    struct {
      gsize partial_magic;
      const GVariantType *type;
      gsize y[14];
    } s;              // <-------

    struct {
      GVariant *asv;
      gsize partial_magic;
      gsize y[14];
    } s;              // <------
Comment 18 Murray Cumming 2016-07-21 09:12:21 UTC
I see these warnings too, which breaks the glibmm --enable-warnings=fatal build. It would be great if we could avoid that. Is there any downside to giving these structs names?
Comment 19 Murray Cumming 2016-07-21 09:15:54 UTC
However just giving them names introduces another error:

/opt/gnome/include/glib-2.0/glib/gvariant.h:302:12: error: ‘struct _GVariantBuilder::<anonymous union>::s1’ invalid; an anonymous union can only have non-static data members [-fpermissive]
     struct s1 {
Comment 20 Krzesimir Nowak 2016-07-21 10:28:59 UTC
Created attachment 331867 [details] [review]
Fixx C++ issues with gvariant.h

Murray,

Does this patch help? Quickly testing the following code resulted in no warnings/errors with g++ -c -Wall -Wextra -Wpedantic `pkg-config --cflags glib-2.0` test.cc with gcc 6.1.1

#include <glib.h>

int main()
{
  GVariantBuilder builder = G_VARIANT_BUILDER_INIT(G_VARIANT_TYPE_BYTESTRING);
  (void)builder;
}
Comment 21 Murray Cumming 2016-07-21 10:56:11 UTC
Of course, yes, that seems fine. When using that patch on glibmm, I don't see the warning anymore when building glibmm. Thanks.
Comment 22 Murray Cumming 2016-07-21 10:56:40 UTC
I mean, when using that patch on glib.
Comment 23 Krzesimir Nowak 2016-07-21 12:09:04 UTC
Applied the patch.
Comment 24 Colin Walters 2016-10-25 16:08:54 UTC
FWIW, downstream workaround to have code build both on <2.50 and >= 2.50:

https://github.com/ostreedev/ostree/pull/547