1 # AC_PROG_CC_C99 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
2 # ----------------------------------------------------------------
3 # If the C compiler is not in ISO C99 mode by default, try to add an
4 # option to output variable CC to make it so. This macro tries
5 # various options that select ISO C99 on some system or another. It
6 # considers the compiler to be in ISO C99 mode if it handles _Bool,
7 # // comments, flexible array members, inline, long long int, mixed
8 # code and declarations, named initialization of structs, restrict,
9 # va_copy, varargs macros, variable declarations in for loops and
10 # variable length arrays.
11 AC_DEFUN([AC_PROG_CC_C99],
19 // Check varargs macros. These examples are taken from C99 6.10.3.5.
20 #define debug(...) fprintf (stderr, __VA_ARGS__)
21 #define showlist(...) puts (#__VA_ARGS__)
22 #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
24 test_varargs_macros (void)
29 debug ("X = %d\n", x);
30 showlist (The first, second, and third items.);
31 report (x>y, "x is %d but y is %d", x, y);
34 // Check long long types.
35 #define BIG64 18446744073709551615ull
36 #define BIG32 4294967295ul
37 #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
39 your preprocessor is broken;
43 your preprocessor is broken;
45 static long long int bignum = -9223372036854775807LL;
46 static unsigned long long int ubignum = BIG64;
48 struct incomplete_array
60 typedef const char *ccp;
63 test_restrict (ccp restrict text)
65 // See if C++-style comments work.
66 // Iterate through items via the restricted pointer.
67 // Also check for declarations in for loops.
68 for (unsigned int i = 0; *(text+i) != '\0'; ++i)
73 // Check varargs and va_copy.
75 test_varargs (const char *format, ...)
78 va_start (args, format);
80 va_copy (args_copy, args);
91 str = va_arg (args_copy, const char *);
94 number = va_arg (args_copy, int);
97 fnumber = va_arg (args_copy, double);
109 _Bool success = false;
112 if (test_restrict ("String literal") == 0)
114 char *restrict newvar = "Another string";
117 test_varargs ("s, d' f .", "string", 65, 34.234);
118 test_varargs_macros ();
120 // Check flexible array members.
121 struct incomplete_array *ia =
122 malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
124 for (int i = 0; i < ia->datasize; ++i)
125 ia->data[i] = i * 1.234;
127 // Check named initializers.
128 struct named_init ni = {
130 .name = L"Test wide string",
131 .average = 543.34343,
136 int dynamic_array[ni.number];
137 dynamic_array[ni.number - 1] = 543;
139 // work around unused variable warnings
140 return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
141 || dynamic_array[ni.number - 1] != 543);
144 dnl GCC -std=gnu99 (unused restrictive modes: -std=c99 -std=iso9899:1999)
145 dnl AIX -qlanglvl=extc99 (unused restrictive mode: -qlanglvl=stdc99)
148 dnl Solaris (unused because it causes the compiler to assume C99 semantics for
149 dnl library functions, and this is invalid before Solaris 10: -xc99)
151 dnl with extended modes being tried first.
152 [[-std=gnu99 -c99 -qlanglvl=extc99]], [$1], [$2])[]dnl
155 # AC_C_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
156 # ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
157 # --------------------------------------------------------------
158 # Check whether the C compiler accepts features of STANDARD (e.g `c89', `c99')
159 # by trying to compile a program of TEST-PROLOGUE and TEST-BODY. If this fails,
160 # try again with each compiler option in the space-separated OPTION-LIST; if one
161 # helps, append it to CC. If eventually successful, run ACTION-IF-AVAILABLE,
162 # else ACTION-IF-UNAVAILABLE.
163 AC_DEFUN([AC_C_STD_TRY],
164 [AC_MSG_CHECKING([for $CC option to accept ISO ]m4_translit($1, [c], [C]))
165 AC_CACHE_VAL(ac_cv_prog_cc_$1,
168 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
171 CC="$ac_save_CC $ac_arg"
172 _AC_COMPILE_IFELSE([], [ac_cv_prog_cc_$1=$ac_arg])
173 test "x$ac_cv_prog_cc_$1" != "xno" && break
175 rm -f conftest.$ac_ext
178 case "x$ac_cv_prog_cc_$1" in
180 AC_MSG_RESULT([none needed]) ;;
182 AC_MSG_RESULT([unsupported]) ;;
184 CC="$CC $ac_cv_prog_cc_$1"
185 AC_MSG_RESULT([$ac_cv_prog_cc_$1]) ;;
187 AS_IF([test "x$ac_cv_prog_cc_$1" != xno], [$5], [$6])