From: Paul Eggert Date: Wed, 8 Nov 2006 08:26:43 +0000 (+0000) Subject: * NEWS: New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS. X-Git-Tag: AUTOCONF-2.61~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=aebb1f2c2c0f251c9e1c2a35187dc64f909011dd;p=thirdparty%2Fautoconf.git * NEWS: New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS. * doc/autoconf.texi (C Compiler): Document them. * lib/autoconf/c.m4 (AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS): New macros, taken from gnulib. --- diff --git a/ChangeLog b/ChangeLog index 46c90f75..8dcc0c49 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2006-11-08 Paul Eggert + + * NEWS: New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS. + * doc/autoconf.texi (C Compiler): Document them. + * lib/autoconf/c.m4 (AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS): + New macros, taken from gnulib. + 2006-11-07 Paul Eggert * lib/autoconf/types.m4 (AC_TYPE_LONG_LONG_INT): Detect bug in diff --git a/NEWS b/NEWS index 1d28cabd..e9c16527 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,8 @@ ** AC_COMPUTE_INT no longer caches or reports results. +** New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS. + ** AC_CHECK_DECL now also works with aggregate objects. ** AC_USE_SYSTEM_EXTENSIONS now defines _TANDEM_SOURCE for NonStop platforms. diff --git a/doc/autoconf.texi b/doc/autoconf.texi index 80e49e04..9aa70d9d 100644 --- a/doc/autoconf.texi +++ b/doc/autoconf.texi @@ -6588,6 +6588,47 @@ This macro is obsolescent, as current C compilers support the stringizing operator. New programs need not use this macro. @end defmac +@defmac AC_C_FLEXIBLE_ARRAY_MEMBER +@acindex{C_FLEXIBLE_ARRAY_MEMBER} +@cvindex FLEXIBLE_ARRAY_MEMBER +If the C compiler supports flexible array members, define +@code{FLEXIBLE_ARRAY_MEMBER} to nothing; otherwise define it to 1. +That way, a declaration like this: + +@example +struct s + @{ + size_t n_vals; + double val[FLEXIBLE_ARRAY_MEMBER]; + @}; +@end example + +@noindent +will let applications use the ``struct hack'' even with compilers that +do not support flexible array members. To allocate and use such an +object, you can use code like this: + +@example +size_t i; +size_t n = compute_value_count (); +struct s *p = + malloc (offsetof (struct s, val) + + n * sizeof (double)); +p->n_vals = n; +for (i = 0; i < n; i++) + p->val[i] = compute_value (i); +@end example +@end defmac + +@defmac AC_C_VARARRAYS +@acindex{C_VARARRAYS} +@cvindex HAVE_C_VARARRAYS +If the C compiler supports variable-length arrays, define +@code{HAVE_C_VARRAYS}. A variable-length array is an array of automatic +storage duration whose length is determined at run time, when the array +is declared. +@end defmac + @defmac AC_C_TYPEOF @acindex{C_TYPEOF} @cvindex HAVE_TYPEOF diff --git a/lib/autoconf/c.m4 b/lib/autoconf/c.m4 index fd8daf60..e3738897 100644 --- a/lib/autoconf/c.m4 +++ b/lib/autoconf/c.m4 @@ -1643,6 +1643,61 @@ fi ])# AC_C_PROTOTYPES +# AC_C_FLEXIBLE_ARRAY_MEMBER +# -------------------------- +# Check whether the C compiler supports flexible array members. +AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER], +[ + AC_CACHE_CHECK([for flexible array members], + ac_cv_c_flexmember, + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM( + [[#include + #include + #include + struct s { int n; double d[]; };]], + [[int m = getchar (); + struct s *p = malloc (offsetof (struct s, d) + + m * sizeof (double)); + p->d[0] = 0.0; + return p->d != (double *) NULL;]])], + [ac_cv_c_flexmember=yes], + [ac_cv_c_flexmember=no])]) + if test $ac_cv_c_flexmember = yes; then + AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [], + [Define to nothing if C supports flexible array members, and to + 1 if it does not. That way, with a declaration like `struct s + { int n; double d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack + can be used with pre-C99 compilers. + When computing the size of such an object, don't use 'sizeof (struct s)' + as it overestimates the size. Use 'offsetof (struct s, d)' instead. + Don't use 'offsetof (struct s, d@<:@0@:>@)', as this doesn't work with + MSVC and with C++ compilers.]) + else + AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], 1) + fi +]) + + +# AC_C_VARARRAYS +# -------------- +# Check whether the C compiler supports variable-length arrays. +AC_DEFUN([AC_C_VARARRAYS], +[ + AC_CACHE_CHECK([for variable-length arrays], + ac_cv_c_vararrays, + [AC_COMPILE_IFELSE( + [AC_LANG_PROGRAM([], + [[static int x; char a[++x]; a[sizeof a - 1] = 0; return a[0];]])], + [ac_cv_c_vararrays=yes], + [ac_cv_c_vararrays=no])]) + if test $ac_cv_c_vararrays = yes; then + AC_DEFINE([HAVE_C_VARARRAYS], 1, + [Define to 1 if C supports variable-length arrays.]) + fi +]) + + # AC_C_TYPEOF # ----------- # Check if the C compiler supports GCC's typeof syntax.