Blame m4/c99-backport.m4

Packit Service 584ef9
# AC_PROG_CC_C99 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
Packit Service 584ef9
# ----------------------------------------------------------------
Packit Service 584ef9
# If the C compiler is not in ISO C99 mode by default, try to add an
Packit Service 584ef9
# option to output variable CC to make it so.  This macro tries
Packit Service 584ef9
# various options that select ISO C99 on some system or another.  It
Packit Service 584ef9
# considers the compiler to be in ISO C99 mode if it handles _Bool,
Packit Service 584ef9
# // comments, flexible array members, inline, long long int, mixed
Packit Service 584ef9
# code and declarations, named initialization of structs, restrict,
Packit Service 584ef9
# va_copy, varargs macros, variable declarations in for loops and
Packit Service 584ef9
# variable length arrays.
Packit Service 584ef9
AC_DEFUN([AC_PROG_CC_C99],
Packit Service 584ef9
[AC_C_STD_TRY([c99],
Packit Service 584ef9
[[#include <stdarg.h>
Packit Service 584ef9
#include <stdbool.h>
Packit Service 584ef9
#include <stdlib.h>
Packit Service 584ef9
#include <wchar.h>
Packit Service 584ef9
#include <stdio.h>
Packit Service 584ef9
Packit Service 584ef9
// Check varargs macros.  These examples are taken from C99 6.10.3.5.
Packit Service 584ef9
#define debug(...) fprintf (stderr, __VA_ARGS__)
Packit Service 584ef9
#define showlist(...) puts (#__VA_ARGS__)
Packit Service 584ef9
#define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
Packit Service 584ef9
static void
Packit Service 584ef9
test_varargs_macros (void)
Packit Service 584ef9
{
Packit Service 584ef9
  int x = 1234;
Packit Service 584ef9
  int y = 5678;
Packit Service 584ef9
  debug ("Flag");
Packit Service 584ef9
  debug ("X = %d\n", x);
Packit Service 584ef9
  showlist (The first, second, and third items.);
Packit Service 584ef9
  report (x>y, "x is %d but y is %d", x, y);
Packit Service 584ef9
}
Packit Service 584ef9
Packit Service 584ef9
// Check long long types.
Packit Service 584ef9
#define BIG64 18446744073709551615ull
Packit Service 584ef9
#define BIG32 4294967295ul
Packit Service 584ef9
#define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
Packit Service 584ef9
#if !BIG_OK
Packit Service 584ef9
  your preprocessor is broken;
Packit Service 584ef9
#endif
Packit Service 584ef9
#if BIG_OK
Packit Service 584ef9
#else
Packit Service 584ef9
  your preprocessor is broken;
Packit Service 584ef9
#endif
Packit Service 584ef9
static long long int bignum = -9223372036854775807LL;
Packit Service 584ef9
static unsigned long long int ubignum = BIG64;
Packit Service 584ef9
Packit Service 584ef9
struct incomplete_array
Packit Service 584ef9
{
Packit Service 584ef9
  int datasize;
Packit Service 584ef9
  double data[];
Packit Service 584ef9
};
Packit Service 584ef9
Packit Service 584ef9
struct named_init {
Packit Service 584ef9
  int number;
Packit Service 584ef9
  const wchar_t *name;
Packit Service 584ef9
  double average;
Packit Service 584ef9
};
Packit Service 584ef9
Packit Service 584ef9
typedef const char *ccp;
Packit Service 584ef9
Packit Service 584ef9
static inline int
Packit Service 584ef9
test_restrict (ccp restrict text)
Packit Service 584ef9
{
Packit Service 584ef9
  // See if C++-style comments work.
Packit Service 584ef9
  // Iterate through items via the restricted pointer.
Packit Service 584ef9
  // Also check for declarations in for loops.
Packit Service 584ef9
  for (unsigned int i = 0; *(text+i) != '\0'; ++i)
Packit Service 584ef9
    continue;
Packit Service 584ef9
  return 0;
Packit Service 584ef9
}
Packit Service 584ef9
Packit Service 584ef9
// Check varargs and va_copy.
Packit Service 584ef9
static void
Packit Service 584ef9
test_varargs (const char *format, ...)
Packit Service 584ef9
{
Packit Service 584ef9
  va_list args;
Packit Service 584ef9
  va_start (args, format);
Packit Service 584ef9
  va_list args_copy;
Packit Service 584ef9
  va_copy (args_copy, args);
Packit Service 584ef9
Packit Service 584ef9
  const char *str;
Packit Service 584ef9
  int number;
Packit Service 584ef9
  float fnumber;
Packit Service 584ef9
Packit Service 584ef9
  while (*format)
Packit Service 584ef9
    {
Packit Service 584ef9
      switch (*format++)
Packit Service 584ef9
	{
Packit Service 584ef9
	case 's': // string
Packit Service 584ef9
	  str = va_arg (args_copy, const char *);
Packit Service 584ef9
	  break;
Packit Service 584ef9
	case 'd': // int
Packit Service 584ef9
	  number = va_arg (args_copy, int);
Packit Service 584ef9
	  break;
Packit Service 584ef9
	case 'f': // float
Packit Service 584ef9
	  fnumber = va_arg (args_copy, double);
Packit Service 584ef9
	  break;
Packit Service 584ef9
	default:
Packit Service 584ef9
	  break;
Packit Service 584ef9
	}
Packit Service 584ef9
    }
Packit Service 584ef9
  va_end (args_copy);
Packit Service 584ef9
  va_end (args);
Packit Service 584ef9
}
Packit Service 584ef9
]],
Packit Service 584ef9
[[
Packit Service 584ef9
  // Check bool.
Packit Service 584ef9
  _Bool success = false;
Packit Service 584ef9
Packit Service 584ef9
  // Check restrict.
Packit Service 584ef9
  if (test_restrict ("String literal") == 0)
Packit Service 584ef9
    success = true;
Packit Service 584ef9
  char *restrict newvar = "Another string";
Packit Service 584ef9
Packit Service 584ef9
  // Check varargs.
Packit Service 584ef9
  test_varargs ("s, d' f .", "string", 65, 34.234);
Packit Service 584ef9
  test_varargs_macros ();
Packit Service 584ef9
Packit Service 584ef9
  // Check flexible array members.
Packit Service 584ef9
  struct incomplete_array *ia =
Packit Service 584ef9
    malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
Packit Service 584ef9
  ia->datasize = 10;
Packit Service 584ef9
  for (int i = 0; i < ia->datasize; ++i)
Packit Service 584ef9
    ia->data[i] = i * 1.234;
Packit Service 584ef9
Packit Service 584ef9
  // Check named initializers.
Packit Service 584ef9
  struct named_init ni = {
Packit Service 584ef9
    .number = 34,
Packit Service 584ef9
    .name = L"Test wide string",
Packit Service 584ef9
    .average = 543.34343,
Packit Service 584ef9
  };
Packit Service 584ef9
Packit Service 584ef9
  ni.number = 58;
Packit Service 584ef9
Packit Service 584ef9
  int dynamic_array[ni.number];
Packit Service 584ef9
  dynamic_array[ni.number - 1] = 543;
Packit Service 584ef9
Packit Service 584ef9
  // work around unused variable warnings
Packit Service 584ef9
  return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
Packit Service 584ef9
	  || dynamic_array[ni.number - 1] != 543);
Packit Service 584ef9
]],
Packit Service 584ef9
dnl Try
Packit Service 584ef9
dnl GCC		-std=gnu99 (unused restrictive modes: -std=c99 -std=iso9899:1999)
Packit Service 584ef9
dnl AIX		-qlanglvl=extc99 (unused restrictive mode: -qlanglvl=stdc99)
Packit Service 584ef9
dnl Intel ICC	-c99
Packit Service 584ef9
dnl IRIX	-c99
Packit Service 584ef9
dnl Solaris	(unused because it causes the compiler to assume C99 semantics for
Packit Service 584ef9
dnl		library functions, and this is invalid before Solaris 10: -xc99)
Packit Service 584ef9
dnl Tru64	-c99
Packit Service 584ef9
dnl with extended modes being tried first.
Packit Service 584ef9
[[-std=gnu99 -c99 -qlanglvl=extc99]], [$1], [$2])[]dnl
Packit Service 584ef9
])# AC_PROG_CC_C99
Packit Service 584ef9
Packit Service 584ef9
# AC_C_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
Packit Service 584ef9
#		ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
Packit Service 584ef9
# --------------------------------------------------------------
Packit Service 584ef9
# Check whether the C compiler accepts features of STANDARD (e.g `c89', `c99')
Packit Service 584ef9
# by trying to compile a program of TEST-PROLOGUE and TEST-BODY.  If this fails,
Packit Service 584ef9
# try again with each compiler option in the space-separated OPTION-LIST; if one
Packit Service 584ef9
# helps, append it to CC.  If eventually successful, run ACTION-IF-AVAILABLE,
Packit Service 584ef9
# else ACTION-IF-UNAVAILABLE.
Packit Service 584ef9
AC_DEFUN([AC_C_STD_TRY],
Packit Service 584ef9
[AC_MSG_CHECKING([for $CC option to accept ISO ]m4_translit($1, [c], [C]))
Packit Service 584ef9
AC_CACHE_VAL(ac_cv_prog_cc_$1,
Packit Service 584ef9
[ac_cv_prog_cc_$1=no
Packit Service 584ef9
ac_save_CC=$CC
Packit Service 584ef9
AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
Packit Service 584ef9
for ac_arg in '' $4
Packit Service 584ef9
do
Packit Service 584ef9
  CC="$ac_save_CC $ac_arg"
Packit Service 584ef9
  _AC_COMPILE_IFELSE([], [ac_cv_prog_cc_$1=$ac_arg])
Packit Service 584ef9
  test "x$ac_cv_prog_cc_$1" != "xno" && break
Packit Service 584ef9
done
Packit Service 584ef9
rm -f conftest.$ac_ext
Packit Service 584ef9
CC=$ac_save_CC
Packit Service 584ef9
])# AC_CACHE_VAL
Packit Service 584ef9
case "x$ac_cv_prog_cc_$1" in
Packit Service 584ef9
  x)
Packit Service 584ef9
    AC_MSG_RESULT([none needed]) ;;
Packit Service 584ef9
  xno)
Packit Service 584ef9
    AC_MSG_RESULT([unsupported]) ;;
Packit Service 584ef9
  *)
Packit Service 584ef9
    CC="$CC $ac_cv_prog_cc_$1"
Packit Service 584ef9
    AC_MSG_RESULT([$ac_cv_prog_cc_$1]) ;;
Packit Service 584ef9
esac
Packit Service 584ef9
AS_IF([test "x$ac_cv_prog_cc_$1" != xno], [$5], [$6])
Packit Service 584ef9
])# AC_C_STD_TRY