|
|
00db10 |
commit 8311c83f91a3127ccd2fe684e25bc60c5178d23b
|
|
|
00db10 |
Author: Florian Weimer <fweimer@redhat.com>
|
|
|
00db10 |
Date: Thu Feb 7 09:03:02 2019 +0100
|
|
|
00db10 |
|
|
|
00db10 |
array_length: Make usable as a constant expression
|
|
|
00db10 |
|
|
|
00db10 |
Do not use a statement expression in array_length, so that
|
|
|
00db10 |
array_length can be used at file scope and as a constant expression.
|
|
|
00db10 |
Instead, put the _Static_assert into a struct (as a declaration),
|
|
|
00db10 |
and nest this in the expression using a sizeof expression.
|
|
|
00db10 |
|
|
|
00db10 |
diff --git a/include/array_length.h b/include/array_length.h
|
|
|
00db10 |
index cb4a8b2a56bca3ad..ccb253c1dc1641cf 100644
|
|
|
00db10 |
--- a/include/array_length.h
|
|
|
00db10 |
+++ b/include/array_length.h
|
|
|
00db10 |
@@ -22,12 +22,12 @@
|
|
|
00db10 |
/* array_length (VAR) is the number of elements in the array VAR. VAR
|
|
|
00db10 |
must evaluate to an array, not a pointer. */
|
|
|
00db10 |
#define array_length(var) \
|
|
|
00db10 |
- __extension__ ({ \
|
|
|
00db10 |
- _Static_assert (!__builtin_types_compatible_p \
|
|
|
00db10 |
- (__typeof (var), __typeof (&(var)[0])), \
|
|
|
00db10 |
- "argument must be an array"); \
|
|
|
00db10 |
- sizeof (var) / sizeof ((var)[0]); \
|
|
|
00db10 |
- })
|
|
|
00db10 |
+ (sizeof (var) / sizeof ((var)[0]) \
|
|
|
00db10 |
+ + 0 * sizeof (struct { \
|
|
|
00db10 |
+ _Static_assert (!__builtin_types_compatible_p \
|
|
|
00db10 |
+ (__typeof (var), __typeof (&(var)[0])), \
|
|
|
00db10 |
+ "argument must be an array"); \
|
|
|
00db10 |
+ }))
|
|
|
00db10 |
|
|
|
00db10 |
/* array_end (VAR) is a pointer one past the end of the array VAR.
|
|
|
00db10 |
VAR must evaluate to an array, not a pointer. */
|