]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
array_length: Make usable as a constant expression
authorFlorian Weimer <fweimer@redhat.com>
Thu, 7 Feb 2019 08:03:02 +0000 (09:03 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 7 Feb 2019 08:03:02 +0000 (09:03 +0100)
Do not use a statement expression in array_length, so that
array_length can be used at file scope and as a constant expression.
Instead, put the _Static_assert into a struct (as a declaration),
and nest this in the expression using a sizeof expression.

ChangeLog
include/array_length.h

index 1c477228f32c9c029414aa5655c825adc7ac4e02..327066298344708ee0f2c96db4617919f6e1c4df 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-02-07  Florian Weimer  <fweimer@redhat.com>
+
+       * include/array_length.h (array_length): Do not use a statement
+       expression and _Static_assert, so that array_length can be used at
+       file scope and as a constant expression.
+
 2019-02-07  Florian Weimer  <fweimer@redhat.com>
 
        * support/xdlfcn.h (xdlmopen): Declare.
index 65f583063de7bdd04ae00a83f8affc2ecc553245..db98a69899545bb8d49f111da1390510e7c9335e 100644 (file)
 /* array_length (VAR) is the number of elements in the array VAR.  VAR
    must evaluate to an array, not a pointer.  */
 #define array_length(var)                                               \
-  __extension__ ({                                                      \
-    _Static_assert (!__builtin_types_compatible_p                       \
-                    (__typeof (var), __typeof (&(var)[0])),             \
-                    "argument must be an array");                       \
-    sizeof (var) / sizeof ((var)[0]);                                   \
-  })
+  (sizeof (var) / sizeof ((var)[0])                                     \
+   + 0 * sizeof (struct {                                               \
+       _Static_assert (!__builtin_types_compatible_p                    \
+                       (__typeof (var), __typeof (&(var)[0])),          \
+                       "argument must be an array");                    \
+   }))
 
 /* array_end (VAR) is a pointer one past the end of the array VAR.
    VAR must evaluate to an array, not a pointer.  */