xref: /Universal-ctags/m4/flexmember.m4 (revision a939078a69878851c19820eb92e6cb95ba429546)
1*a939078aSHiroo HAYASHI# serial 5
2*a939078aSHiroo HAYASHI# Check for flexible array member support.
3*a939078aSHiroo HAYASHI
4*a939078aSHiroo HAYASHI# Copyright (C) 2006, 2009-2021 Free Software Foundation, Inc.
5*a939078aSHiroo HAYASHI# This file is free software; the Free Software Foundation
6*a939078aSHiroo HAYASHI# gives unlimited permission to copy and/or distribute it,
7*a939078aSHiroo HAYASHI# with or without modifications, as long as this notice is preserved.
8*a939078aSHiroo HAYASHI
9*a939078aSHiroo HAYASHI# Written by Paul Eggert.
10*a939078aSHiroo HAYASHI
11*a939078aSHiroo HAYASHIAC_DEFUN([AC_C_FLEXIBLE_ARRAY_MEMBER],
12*a939078aSHiroo HAYASHI[
13*a939078aSHiroo HAYASHI  AC_CACHE_CHECK([for flexible array member],
14*a939078aSHiroo HAYASHI    ac_cv_c_flexmember,
15*a939078aSHiroo HAYASHI    [AC_COMPILE_IFELSE(
16*a939078aSHiroo HAYASHI       [AC_LANG_PROGRAM(
17*a939078aSHiroo HAYASHI          [[#include <stdlib.h>
18*a939078aSHiroo HAYASHI            #include <stdio.h>
19*a939078aSHiroo HAYASHI            #include <stddef.h>
20*a939078aSHiroo HAYASHI            struct m { struct m *next, **list; char name[]; };
21*a939078aSHiroo HAYASHI            struct s { struct s *p; struct m *m; int n; double d[]; };]],
22*a939078aSHiroo HAYASHI          [[int m = getchar ();
23*a939078aSHiroo HAYASHI            size_t nbytes = offsetof (struct s, d) + m * sizeof (double);
24*a939078aSHiroo HAYASHI            nbytes += sizeof (struct s) - 1;
25*a939078aSHiroo HAYASHI            nbytes -= nbytes % sizeof (struct s);
26*a939078aSHiroo HAYASHI            struct s *p = malloc (nbytes);
27*a939078aSHiroo HAYASHI            p->p = p;
28*a939078aSHiroo HAYASHI            p->m = NULL;
29*a939078aSHiroo HAYASHI            p->d[0] = 0.0;
30*a939078aSHiroo HAYASHI            return p->d != (double *) NULL;]])],
31*a939078aSHiroo HAYASHI       [ac_cv_c_flexmember=yes],
32*a939078aSHiroo HAYASHI       [ac_cv_c_flexmember=no])])
33*a939078aSHiroo HAYASHI  if test $ac_cv_c_flexmember = yes; then
34*a939078aSHiroo HAYASHI    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [],
35*a939078aSHiroo HAYASHI      [Define to nothing if C supports flexible array members, and to
36*a939078aSHiroo HAYASHI       1 if it does not.  That way, with a declaration like 'struct s
37*a939078aSHiroo HAYASHI       { int n; short d@<:@FLEXIBLE_ARRAY_MEMBER@:>@; };', the struct hack
38*a939078aSHiroo HAYASHI       can be used with pre-C99 compilers.
39*a939078aSHiroo HAYASHI       Use 'FLEXSIZEOF (struct s, d, N * sizeof (short))' to calculate
40*a939078aSHiroo HAYASHI       the size in bytes of such a struct containing an N-element array.])
41*a939078aSHiroo HAYASHI  else
42*a939078aSHiroo HAYASHI    AC_DEFINE([FLEXIBLE_ARRAY_MEMBER], [1])
43*a939078aSHiroo HAYASHI  fi
44*a939078aSHiroo HAYASHI])
45