summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorian Weimer <fweimer@redhat.com>2019-02-07 09:03:02 +0100
committerFlorian Weimer <fweimer@redhat.com>2019-02-07 09:03:02 +0100
commit8311c83f91a3127ccd2fe684e25bc60c5178d23b (patch)
tree795aaabcfdd4b0be64899c653ea74e930be34c3e
parent94b63e66206a9ad38872a9136a623ce73cf7c858 (diff)
downloadglibc-8311c83f91a3127ccd2fe684e25bc60c5178d23b.tar.gz
glibc-8311c83f91a3127ccd2fe684e25bc60c5178d23b.tar.xz
glibc-8311c83f91a3127ccd2fe684e25bc60c5178d23b.zip
array_length: Make usable as a constant expression
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.
-rw-r--r--ChangeLog6
-rw-r--r--include/array_length.h12
2 files changed, 12 insertions, 6 deletions
diff --git a/ChangeLog b/ChangeLog
index 1c477228f3..3270662983 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 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.
 	* support/xdlmopen.c: New file.
 	* support/Makefile (libsupport-routines): Add xdlmopen.
diff --git a/include/array_length.h b/include/array_length.h
index 65f583063d..db98a69899 100644
--- a/include/array_length.h
+++ b/include/array_length.h
@@ -22,12 +22,12 @@
 /* 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.  */