[Mesa-dev] [PATCH] Avoid spurious GCC warnings in STATIC_ASSERT() macro.

Eric Anholt eric at anholt.net
Tue Apr 2 17:49:02 PDT 2013


Brian Paul <brianp at vmware.com> writes:

> On 04/02/2013 04:16 PM, Paul Berry wrote:
>> GCC 4.8 now warns about typedefs that are local to a scope and not
>> used anywhere within that scope.  This produces spurious warnings with
>> the STATIC_ASSERT() macro (which uses a typedef to provoke a compile
>> error in the event of an assertion failure).
>>
>> This patch avoids the warning using the GCC __attribute__((unused))
>> syntax.
>> ---
>>   src/mesa/main/compiler.h | 8 +++++++-
>>   1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/src/mesa/main/compiler.h b/src/mesa/main/compiler.h
>> index 8b23665..ddeb61d 100644
>> --- a/src/mesa/main/compiler.h
>> +++ b/src/mesa/main/compiler.h
>> @@ -249,6 +249,12 @@ static INLINE GLuint CPU_TO_LE32(GLuint x)
>>   #endif
>>
>>
>> +#if (__GNUC__>= 3)
>> +#define GCC_ATTRIBUTE_UNUSED __attribute__((unused))
>> +#else
>> +#define GCC_ATTRIBUTE_UNUSED
>> +#endif
>> +
>>   /**
>>    * Static (compile-time) assertion.
>>    * Basically, use COND to dimension an array.  If COND is false/zero the
>> @@ -256,7 +262,7 @@ static INLINE GLuint CPU_TO_LE32(GLuint x)
>>    */
>>   #define STATIC_ASSERT(COND) \
>>      do { \
>> -      typedef int static_assertion_failed[(!!(COND))*2-1]; \
>> +      typedef int static_assertion_failed[(!!(COND))*2-1] GCC_ATTRIBUTE_UNUSED; \
>>      } while (0)
>>
>>
>
> Without using gcc-isms, I think this would work too:
>
> #define STATIC_ASSERT(COND) \
>     do { \
>        int static_assertion_failed[(!!(COND))*2-1]; \
>        (void) static_assertion_failed; \
>     } while (0)
>
> I don't recall why I used the typedef.
>
> Also, the same macro should probably be updated in 
> src/gallium/include/pipe/p_compiler.h

Rusty's CCAN is often a good reference for stuff like this:

http://git.ozlabs.org/?p=ccan;a=blob;f=ccan/build_assert/build_assert.h;h=b9ecd84028e3fbebd1bf009c3c57e8a193e45646;hb=HEAD
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20130402/b3ced6e5/attachment.pgp>


More information about the mesa-dev mailing list