Re: [PATCH] Use more gcc extensions in the Linux headers

From: Trent Piepho
Date: Sat Mar 10 2007 - 15:37:05 EST


On Fri, 9 Mar 2007, Linus Torvalds wrote:
> On Fri, 9 Mar 2007, Christoph Hellwig wrote:
> Well, since Rusty's macro was hoddible *anyway*, I don't think I'd apply
> it as-is. Breaking icc for something that ugly and not-very-important
> simply makes no sense.
>
> There are better ways to do this.
>
> For one, you could (and should!) abstract these kinds of things out,
> rather than put them in another macro that really does something totally
> different. Then, the macro could have become
>
> #define ARRAY_SIZE (sizeof_expression + 0*error_if_not_array)

/* Error if X is a pointer, 0 otherwise */
#define ERROR_IF_POINTER(x) \
sizeof(int[-__builtin_types_compatible_p(typeof(x), typeof(&x[0]))])

/* Warning (div by zero) if x is a pointer, 0 otherwise */
#define WARN_IF_POINTER(x) \
(0/!__builtin_types_compatible_p(typeof(x), typeof(&x[0])))

The gcc docs say __builtin_types_compatible_p returns 1 or 0, so the !!
isn't necessary. And my gcc at least returns 0 for sizeof(int[0]).
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/