[Mesa-dev] [PATCH] nir: Make C++ more happy with NIR_SRC_INIT and NIR_DEST_INIT

Jason Ekstrand jason at jlekstrand.net
Fri Jun 26 14:31:39 PDT 2015


On Fri, Jun 26, 2015 at 12:08 PM, Francisco Jerez <currojerez at riseup.net> wrote:
> Jason Ekstrand <jason at jlekstrand.net> writes:
>
>> In C, if you partially initialize a structure, the rest of the struct gets
>> set to 0.  C++, however, does not have this rule so GCC throws warnings
>> whenver NIR_SRC_INIT or NIR_DEST_INIT is used in C++.
>
> I don't think that's right, in C++ initializers missing from an
> aggregate initializer list are also defined to be initialized
> (value-initialized to be more precise, what would set them to zero in
> this case just like in C).

Yes, that is correct.  I just did a second attempt that, instead,
defines a static const variable named NIR_SRC_INIT with a partial
initializer.  C++ still gets grumpy and gives me a pile of "missing
initializer" warnings.

>> Since nir.h contains a static inline that uses NIR_SRC_INIT, every C++
>> file that includes nir.h complains about this.
>>
> I suspect the reason why this causes a warning may be that you're using
> compound literals? (which are a C99-specific feature and not part of C++)
>
>> This patch adds a small static inline function that makes a struct,
>> memsets it to 0, and returns it.  NIR_SRC_INIT and NIR_DEST_INIT are then
>> wrappers around this function.
>
> In C++ you could just call the implicitly defined default constructor
> for nir_src or nir_dest, like 'nir_src()'.

The implicitly defined default constructor does nothing to POD types,
so doing so would explicitly *not* perform the desired action of
zeroing out the data.

>> ---
>>  src/glsl/nir/nir.h | 22 ++++++++++++++++++++++
>>  1 file changed, 22 insertions(+)
>>
>> diff --git a/src/glsl/nir/nir.h b/src/glsl/nir/nir.h
>> index c666d93..3634f30 100644
>> --- a/src/glsl/nir/nir.h
>> +++ b/src/glsl/nir/nir.h
>> @@ -511,7 +511,18 @@ typedef struct nir_src {
>>     bool is_ssa;
>>  } nir_src;
>>
>> +#ifdef __cplusplus
>> +static inline nir_src
>> +__nir_src_init(void)
>> +{
>> +   nir_src src;
>> +   memset(&src, 0, sizeof(src));
>> +   return src;
>> +}
>> +#define NIR_SRC_INIT (__nir_src_init())
>> +#else
>>  #define NIR_SRC_INIT (nir_src) { { NULL } }
>> +#endif
>>
>>  #define nir_foreach_use(reg_or_ssa_def, src) \
>>     list_for_each_entry(nir_src, src, &(reg_or_ssa_def)->uses, use_link)
>> @@ -534,7 +545,18 @@ typedef struct {
>>     bool is_ssa;
>>  } nir_dest;
>>
>> +#ifdef __cplusplus
>> +static inline nir_dest
>> +__nir_dest_init(void)
>> +{
>> +   nir_dest dest;
>> +   memset(&dest, 0, sizeof(dest));
>> +   return dest;
>> +}
>> +#define NIR_DEST_INIT (__nir_dest_init())
>> +#else
>>  #define NIR_DEST_INIT (nir_dest) { { { NULL } } }
>> +#endif
>>
>>  #define nir_foreach_def(reg, dest) \
>>     list_for_each_entry(nir_dest, dest, &(reg)->defs, reg.def_link)
>> --
>> 2.4.3
>>
>> _______________________________________________
>> mesa-dev mailing list
>> mesa-dev at lists.freedesktop.org
>> http://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list