Blame m4/flexmember.m4

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