Blame m4/flexmember.m4

Packit 33f14e
# serial 5
Packit 33f14e
# Check for flexible array member support.
Packit 33f14e
Packit 33f14e
# Copyright (C) 2006, 2009-2017 Free Software Foundation, Inc.
Packit 33f14e
# This file is free software; the Free Software Foundation
Packit 33f14e
# gives unlimited permission to copy and/or distribute it,
Packit 33f14e
# with or without modifications, as long as this notice is preserved.
Packit 33f14e
Packit 33f14e
# Written by Paul Eggert.
Packit 33f14e
Packit 33f14e
AC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER],
Packit 33f14e
[
Packit 33f14e
  AC_CACHE_CHECK([for flexible array member],
Packit 33f14e
    ac_cv_c_flexmember,
Packit 33f14e
    [AC_COMPILE_IFELSE(
Packit 33f14e
       [AC_LANG_PROGRAM(
Packit 33f14e
          [[#include <stdlib.h>
Packit 33f14e
            #include <stdio.h>
Packit 33f14e
            #include <stddef.h>
Packit 33f14e
            struct m { struct m *next, **list; char name[]; };
Packit 33f14e
            struct s { struct s *p; struct m *m; int n; double d[]; };]],
Packit 33f14e
          [[int m = getchar ();
Packit 33f14e
            size_t nbytes = offsetof (struct s, d) + m * sizeof (double);
Packit 33f14e
            nbytes += sizeof (struct s) - 1;
Packit 33f14e
            nbytes -= nbytes % sizeof (struct s);
Packit 33f14e
            struct s *p = malloc (nbytes);
Packit 33f14e
            p->p = p;
Packit 33f14e
            p->m = NULL;
Packit 33f14e
            p->d[0] = 0.0;
Packit 33f14e
            return p->d != (double *) NULL;]])],
Packit 33f14e
       [ac_cv_c_flexmember=yes],
Packit 33f14e
       [ac_cv_c_flexmember=no])])
Packit 33f14e
  if test $ac_cv_c_flexmember = yes; then
Packit 33f14e
    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [],
Packit 33f14e
      [Define to nothing if C supports flexible array members, and to
Packit 33f14e
       1 if it does not.  That way, with a declaration like 'struct s
Packit 33f14e
       { int n; double d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack
Packit 33f14e
       can be used with pre-C99 compilers.
Packit 33f14e
       When computing the size of such an object, don't use 'sizeof (struct s)'
Packit 33f14e
       as it overestimates the size.  Use 'offsetof (struct s, d)' instead.
Packit 33f14e
       Don't use 'offsetof (struct s, d@<:@0@:>@)', as this doesn't work with
Packit 33f14e
       MSVC and with C++ compilers.])
Packit 33f14e
  else
Packit 33f14e
    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1])
Packit 33f14e
  fi
Packit 33f14e
])