[Mesa-dev] [PATCH 1/5] mesa: add STATIC_ASSERT() macro for compile-time assertions
Brian Paul
brian.e.paul at gmail.com
Mon Nov 28 19:37:31 PST 2011
On Mon, Nov 28, 2011 at 7:06 PM, Eric Anholt <eric at anholt.net> wrote:
> On Mon, 28 Nov 2011 18:15:27 -0700, Brian Paul <brianp at vmware.com> wrote:
>> This can be used to check that tables have the right number of entries,
>> etc. at compile-time. This will hopefully catch things that are missed
>> if particular drivers aren't tested, for example.
>
> I've always wanted to go dig up one of these and start using it. Thanks!
>
>> ---
>> src/mesa/main/compiler.h | 11 +++++++++++
>> 1 files changed, 11 insertions(+), 0 deletions(-)
>>
>> diff --git a/src/mesa/main/compiler.h b/src/mesa/main/compiler.h
>> index 921e302..96e24e4 100644
>> --- a/src/mesa/main/compiler.h
>> +++ b/src/mesa/main/compiler.h
>> @@ -310,6 +310,17 @@ static INLINE GLuint CPU_TO_LE32(GLuint x)
>> #endif
>> #endif
>>
>> +
>> +/**
>> + * Static (compile-time) assertion.
>> + * Basically, use COND to dimension an array. If COND is false/zero the
>> + * array size will be -1 and we'll get a compilation error.
>> + */
>> +#define JOIN(x, y) x##y
>> +#define STATIC_ASSERT_LINE(COND, LINE) do { typedef int JOIN(static_assertion_failed_at_line_,LINE)[(!!(COND))*2-1]; } while (0)
>> +#define STATIC_ASSERT(COND) STATIC_ASSERT_LINE(COND, __LINE__)
>
> Looks like STATIC_ASSERT_LINE could get linewrapped, right?
Yeah, and I'm going to simplify this in general. There's really no
need for the line number stuff since the compiler will already emit
that and since the typedef is inside a do/while block we don't need to
have a unique array name for every instance. I'll post a new patch.
-Brian
More information about the mesa-dev
mailing list