]> git.ipfire.org Git - thirdparty/autoconf.git/commitdiff
* NEWS: New macros AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS.
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 8 Nov 2006 08:26:43 +0000 (08:26 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 8 Nov 2006 08:26:43 +0000 (08:26 +0000)
* doc/autoconf.texi (C Compiler): Document them.
* lib/autoconf/c.m4 (AC_C_FLEXIBLE_ARRAY_MEMBER, AC_C_VARARRAYS):
New macros, taken from gnulib.

ChangeLog
NEWS
doc/autoconf.texi
lib/autoconf/c.m4

index 46c90f7507f4de248042500c52a81eacc1e0176b..8dcc0c49e522d94cbd0d99b99595be6a9a48b672 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2006-11-08  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * 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  <eggert@cs.ucla.edu>
 
        * lib/autoconf/types.m4 (AC_TYPE_LONG_LONG_INT): Detect bug in
diff --git a/NEWS b/NEWS
index 1d28cabdf5b8d70a60b59add0b6e13781d1fea48..e9c165271b386f136d59fbd9782ffd98bf92385a 100644 (file)
--- 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.
index 80e49e042d075d7f0816e2faf39471ee72017baf..9aa70d9d9823534a90184baee959b860e6943566 100644 (file)
@@ -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
index fd8daf60e2cb2a8091f3a168b11a7fdbe84685f3..e37388979be87a46de3153b0a2e772241ed30c79 100644 (file)
@@ -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 <stdlib.h>
+           #include <stdio.h>
+           #include <stddef.h>
+           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.