GNOME Bugzilla – Bug 577231
[PATCH] add G_VALUE_INIT for full GValue variable initialization
Last modified: 2011-08-13 22:19:59 UTC
When a GValue is declared on the stack, the large code base I work with usually does not properly initialize the struct. (e.g. "GValue v = {0,};") This causes gcc to print warnings when 'gcc -Wall -Wextra' is used due to -Wmissing-braces and -Wmissing-field-initializers. I checked gvalue.h and I don't see a #define in there containing the proper initializer list for a GValue to prevent gcc warnings. Rather than duplicating "GValue v = {0, {{0}}};" everywhere, I'd like the following to be added: #define G_VALUE_INIT {0,{{0}}} That way, code everywhere could simple use "GValue v = G_VALUE_INIT;" and be sure that warnings will not be generated and that the initializer list will always be kept up to date in the unlikely case that it ever changes. These are the warnings (or errors with -Werror) that print out if this is not done correctly: // if {0, } is used error: missing initializer for member '_GValue::data' // if {0, 0} is used error: missing braces around initializer for '_GValue::<anonymous union> [2]' // if {0, {0}} is used error: missing braces around initializer for '_GValue::<anonymous union>'
Created attachment 131646 [details] [review] patch for adding G_VALUE_INIT
*** Bug 654793 has been marked as a duplicate of this bug. ***
Created attachment 192145 [details] [review] Add G_VALUE_INIT The implementation of GValue is not public or documented. When allocated on the stack, initializing a GValue is usually done as documented with: GValue value = { 0, }; There is lot code around (including WebKit) that added all the missing fields, resulting in this ugly and non-obvious: GValue value = { 0, { { 0 } } }; However, this doesn't play nice with -Wmissing-field-initializers for example. Instead, we could provide a macro doing the right job. I propose instead this: GValue value = G_VALUE_INIT; Similar to other _INIT macros in GLib. https://bugzilla.gnome.org/show_bug.cgi?id=654793
Review of attachment 131646 [details] [review]: It's missing update to gobject doc section. Mine is more complete, marking this one as obsolete
Review of attachment 192145 [details] [review]: { 0, } is perfectly fine initializer. People who feel that they have to blow that up to t { 0, { { 0 } } } only have themselves to blame. Anyway, no objection to adding G_VALUE_INIT ::: gobject/gvalue.h @@ +172,3 @@ + * </programlisting> + * </informalexample> + * Should use the |[ ]| shorthand for informal examples here.