Debian Codegraph @

tools/gnulib/m4/std-gnu11.m4

raw
1 # std-gnu11.m4
2 # serial 1.1
3
4 # Prefer GNU C11 and C++11 to earlier versions. -*- coding: utf-8 -*-
5
6 # This implementation is taken from GNU Autoconf lib/autoconf/c.m4
7 # commit 017d5ddd82854911f0119691d91ea8a1438824d6
8 # dated Sun Apr 3 13:57:17 2016 -0700
9 # with minor changes to commentary.
10 # This implementation will be obsolete once we can assume Autoconf 2.70
11 # or later is installed everywhere a Gnulib program might be developed.
12
13 m4_version_prereq([2.70], [], [
14
15
16 # Copyright (C) 2001-2024 Free Software Foundation, Inc.
17
18 # This file is part of Autoconf. This program is free
19 # software; you can redistribute it and/or modify it under the
20 # terms of the GNU General Public License as published by the
21 # Free Software Foundation, either version 3 of the License, or
22 # (at your option) any later version.
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 # Under Section 7 of GPL version 3, you are granted additional
30 # permissions described in the Autoconf Configure Script Exception,
31 # version 3.0, as published by the Free Software Foundation.
32 #
33 # You should have received a copy of the GNU General Public License
34 # and a copy of the Autoconf Configure Script Exception along with
35 # this program; see the files COPYINGv3 and COPYING.EXCEPTION
36 # respectively. If not, see <https://www.gnu.org/licenses/> and
37 # <https://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=blob_plain;f=COPYING.EXCEPTION>.
38
39 # Written by David MacKenzie, with help from
40 # Akim Demaille, Paul Eggert,
41 # François Pinard, Karl Berry, Richard Pixley, Ian Lance Taylor,
42 # Roland McGrath, Noah Friedman, david d zuhn, and many others.
43
44
45 # AC_PROG_CC([COMPILER ...])
46 # --------------------------
47 # COMPILER ... is a space separated list of C compilers to search for.
48 # This just gives the user an opportunity to specify an alternative
49 # search list for the C compiler.
50 AC_DEFUN_ONCE([AC_PROG_CC],
51 [AC_LANG_PUSH(C)dnl
52 AC_ARG_VAR([CC], [C compiler command])dnl
53 AC_ARG_VAR([CFLAGS], [C compiler flags])dnl
54 _AC_ARG_VAR_LDFLAGS()dnl
55 _AC_ARG_VAR_LIBS()dnl
56 _AC_ARG_VAR_CPPFLAGS()dnl
57 m4_ifval([$1],
58 [AC_CHECK_TOOLS(CC, [$1])],
59 [AC_CHECK_TOOL(CC, gcc)
60 if test -z "$CC"; then
61 dnl Here we want:
62 dnl AC_CHECK_TOOL(CC, cc)
63 dnl but without the check for a tool without the prefix.
64 dnl Until the check is removed from there, copy the code:
65 if test -n "$ac_tool_prefix"; then
66 AC_CHECK_PROG(CC, [${ac_tool_prefix}cc], [${ac_tool_prefix}cc])
67 fi
68 fi
69 if test -z "$CC"; then
70 AC_CHECK_PROG(CC, cc, cc, , , /usr/ucb/cc)
71 fi
72 if test -z "$CC"; then
73 AC_CHECK_TOOLS(CC, cl.exe)
74 fi
75 if test -z "$CC"; then
76 AC_CHECK_TOOL(CC, clang)
77 fi
78 ])
79
80 test -z "$CC" && AC_MSG_FAILURE([no acceptable C compiler found in \$PATH])
81
82 # Provide some information about the compiler.
83 _AS_ECHO_LOG([checking for _AC_LANG compiler version])
84 set X $ac_compile
85 ac_compiler=$[2]
86 for ac_option in --version -v -V -qversion -version; do
87 _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
88 done
89
90 m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
91 m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
92 _AC_LANG_COMPILER_GNU
93 if test $ac_compiler_gnu = yes; then
94 GCC=yes
95 else
96 GCC=
97 fi
98 _AC_PROG_CC_G
99 dnl
100 dnl Set ac_prog_cc_stdc to the supported C version.
101 dnl Also set the documented variable ac_cv_prog_cc_stdc;
102 dnl its name was chosen when it was cached, but it is no longer cached.
103 _AC_PROG_CC_C11([ac_prog_cc_stdc=c11
104 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c11],
105 [_AC_PROG_CC_C99([ac_prog_cc_stdc=c99
106 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c99],
107 [_AC_PROG_CC_C89([ac_prog_cc_stdc=c89
108 ac_cv_prog_cc_stdc=$ac_cv_prog_cc_c89],
109 [ac_prog_cc_stdc=no
110 ac_cv_prog_cc_stdc=no])])])
111 dnl
112 AC_LANG_POP(C)dnl
113 ])# AC_PROG_CC
114
115
116
117 # AC_PROG_CXX([LIST-OF-COMPILERS])
118 # --------------------------------
119 # LIST-OF-COMPILERS is a space separated list of C++ compilers to search
120 # for (if not specified, a default list is used). This just gives the
121 # user an opportunity to specify an alternative search list for the C++
122 # compiler.
123 # aCC HP-UX C++ compiler much better than `CC', so test before.
124 # FCC Fujitsu C++ compiler
125 # KCC KAI C++ compiler
126 # RCC Rational C++
127 # xlC_r AIX C Set++ (with support for reentrant code)
128 # xlC AIX C Set++
129 AC_DEFUN([AC_PROG_CXX],
130 [AC_LANG_PUSH(C++)dnl
131 AC_ARG_VAR([CXX], [C++ compiler command])dnl
132 AC_ARG_VAR([CXXFLAGS], [C++ compiler flags])dnl
133 _AC_ARG_VAR_LDFLAGS()dnl
134 _AC_ARG_VAR_LIBS()dnl
135 _AC_ARG_VAR_CPPFLAGS()dnl
136 _AC_ARG_VAR_PRECIOUS([CCC])dnl
137 if test -z "$CXX"; then
138 if test -n "$CCC"; then
139 CXX=$CCC
140 else
141 AC_CHECK_TOOLS(CXX,
142 [m4_default([$1],
143 [g++ c++ gpp aCC CC cxx cc++ cl.exe FCC KCC RCC xlC_r xlC clang++])],
144 g++)
145 fi
146 fi
147 # Provide some information about the compiler.
148 _AS_ECHO_LOG([checking for _AC_LANG compiler version])
149 set X $ac_compile
150 ac_compiler=$[2]
151 for ac_option in --version -v -V -qversion; do
152 _AC_DO_LIMIT([$ac_compiler $ac_option >&AS_MESSAGE_LOG_FD])
153 done
154
155 m4_expand_once([_AC_COMPILER_EXEEXT])[]dnl
156 m4_expand_once([_AC_COMPILER_OBJEXT])[]dnl
157 _AC_LANG_COMPILER_GNU
158 if test $ac_compiler_gnu = yes; then
159 GXX=yes
160 else
161 GXX=
162 fi
163 _AC_PROG_CXX_G
164 _AC_PROG_CXX_CXX11([ac_prog_cxx_stdcxx=cxx11
165 ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx11
166 ac_cv_prog_cxx_cxx98=$ac_cv_prog_cxx_cxx11],
167 [_AC_PROG_CXX_CXX98([ac_prog_cxx_stdcxx=cxx98
168 ac_cv_prog_cxx_stdcxx=$ac_cv_prog_cxx_cxx98],
169 [ac_prog_cxx_stdcxx=no
170 ac_cv_prog_cxx_stdcxx=no])])
171 AC_LANG_POP(C++)dnl
172 ])# AC_PROG_CXX
173
174
175 # _AC_C_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
176 # ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
177 # --------------------------------------------------------------
178 # Check whether the C compiler accepts features of STANDARD (e.g `c89', `c99')
179 # by trying to compile a program of TEST-PROLOGUE and TEST-BODY. If this fails,
180 # try again with each compiler option in the space-separated OPTION-LIST; if one
181 # helps, append it to CC. If eventually successful, run ACTION-IF-AVAILABLE,
182 # else ACTION-IF-UNAVAILABLE.
183 AC_DEFUN([_AC_C_STD_TRY],
184 [AC_MSG_CHECKING([for $CC option to enable ]m4_translit($1, [c], [C])[ features])
185 AC_CACHE_VAL(ac_cv_prog_cc_$1,
186 [ac_cv_prog_cc_$1=no
187 ac_save_CC=$CC
188 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
189 for ac_arg in '' $4
190 do
191 CC="$ac_save_CC $ac_arg"
192 _AC_COMPILE_IFELSE([], [ac_cv_prog_cc_$1=$ac_arg])
193 test "x$ac_cv_prog_cc_$1" != "xno" && break
194 done
195 rm -f conftest.$ac_ext
196 CC=$ac_save_CC
197 ])# AC_CACHE_VAL
198 ac_prog_cc_stdc_options=
199 case "x$ac_cv_prog_cc_$1" in
200 x)
201 AC_MSG_RESULT([none needed]) ;;
202 xno)
203 AC_MSG_RESULT([unsupported]) ;;
204 *)
205 ac_prog_cc_stdc_options=" $ac_cv_prog_cc_$1"
206 CC=$CC$ac_prog_cc_stdc_options
207 AC_MSG_RESULT([$ac_cv_prog_cc_$1]) ;;
208 esac
209 AS_IF([test "x$ac_cv_prog_cc_$1" != xno], [$5], [$6])
210 ])# _AC_C_STD_TRY
211
212 # _AC_C_C99_TEST_HEADER
213 # ---------------------
214 # A C header suitable for testing for C99.
215 AC_DEFUN([_AC_C_C99_TEST_HEADER],
216 [[#include <stdarg.h>
217 #include <stdbool.h>
218 #include <stddef.h>
219 #include <stdlib.h>
220 #include <wchar.h>
221 #include <stdio.h>
222
223 // Check varargs macros. These examples are taken from C99 6.10.3.5.
224 #define debug(...) fprintf (stderr, __VA_ARGS__)
225 #define showlist(...) puts (#__VA_ARGS__)
226 #define report(test,...) ((test) ? puts (#test) : printf (__VA_ARGS__))
227 static void
228 test_varargs_macros (void)
229 {
230 int x = 1234;
231 int y = 5678;
232 debug ("Flag");
233 debug ("X = %d\n", x);
234 showlist (The first, second, and third items.);
235 report (x>y, "x is %d but y is %d", x, y);
236 }
237
238 // Check long long types.
239 #define BIG64 18446744073709551615ull
240 #define BIG32 4294967295ul
241 #define BIG_OK (BIG64 / BIG32 == 4294967297ull && BIG64 % BIG32 == 0)
242 #if !BIG_OK
243 your preprocessor is broken;
244 #endif
245 #if BIG_OK
246 #else
247 your preprocessor is broken;
248 #endif
249 static long long int bignum = -9223372036854775807LL;
250 static unsigned long long int ubignum = BIG64;
251
252 struct incomplete_array
253 {
254 int datasize;
255 double data[];
256 };
257
258 struct named_init {
259 int number;
260 const wchar_t *name;
261 double average;
262 };
263
264 typedef const char *ccp;
265
266 static inline int
267 test_restrict (ccp restrict text)
268 {
269 // See if C++-style comments work.
270 // Iterate through items via the restricted pointer.
271 // Also check for declarations in for loops.
272 for (unsigned int i = 0; *(text+i) != '\0'; ++i)
273 continue;
274 return 0;
275 }
276
277 // Check varargs and va_copy.
278 static bool
279 test_varargs (const char *format, ...)
280 {
281 va_list args;
282 va_start (args, format);
283 va_list args_copy;
284 va_copy (args_copy, args);
285
286 const char *str = "";
287 int number = 0;
288 float fnumber = 0;
289
290 while (*format)
291 {
292 switch (*format++)
293 {
294 case 's': // string
295 str = va_arg (args_copy, const char *);
296 break;
297 case 'd': // int
298 number = va_arg (args_copy, int);
299 break;
300 case 'f': // float
301 fnumber = va_arg (args_copy, double);
302 break;
303 default:
304 break;
305 }
306 }
307 va_end (args_copy);
308 va_end (args);
309
310 return *str && number && fnumber;
311 }]])# _AC_C_C99_TEST_HEADER
312
313 # _AC_C_C99_TEST_BODY
314 # -------------------
315 # A C body suitable for testing for C99, assuming the corresponding header.
316 AC_DEFUN([_AC_C_C99_TEST_BODY],
317 [[
318 // Check bool.
319 _Bool success = false;
320
321 // Check restrict.
322 if (test_restrict ("String literal") == 0)
323 success = true;
324 char *restrict newvar = "Another string";
325
326 // Check varargs.
327 success &= test_varargs ("s, d' f .", "string", 65, 34.234);
328 test_varargs_macros ();
329
330 // Check flexible array members.
331 struct incomplete_array *ia =
332 malloc (sizeof (struct incomplete_array) + (sizeof (double) * 10));
333 ia->datasize = 10;
334 for (int i = 0; i < ia->datasize; ++i)
335 ia->data[i] = i * 1.234;
336
337 // Check named initializers.
338 struct named_init ni = {
339 .number = 34,
340 .name = L"Test wide string",
341 .average = 543.34343,
342 };
343
344 ni.number = 58;
345
346 int dynamic_array[ni.number];
347 dynamic_array[ni.number - 1] = 543;
348
349 // work around unused variable warnings
350 return (!success || bignum == 0LL || ubignum == 0uLL || newvar[0] == 'x'
351 || dynamic_array[ni.number - 1] != 543);
352 ]])
353
354 # _AC_PROG_CC_C99 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
355 # ----------------------------------------------------------------
356 # If the C compiler is not in ISO C99 mode by default, try to add an
357 # option to output variable CC to make it so. This macro tries
358 # various options that select ISO C99 on some system or another. It
359 # considers the compiler to be in ISO C99 mode if it handles _Bool,
360 # // comments, flexible array members, inline, long long int, mixed
361 # code and declarations, named initialization of structs, restrict,
362 # va_copy, varargs macros, variable declarations in for loops and
363 # variable length arrays.
364 AC_DEFUN([_AC_PROG_CC_C99],
365 [_AC_C_STD_TRY([c99],
366 [_AC_C_C99_TEST_HEADER],
367 [_AC_C_C99_TEST_BODY],
368 dnl Try
369 dnl GCC -std=gnu99 (unused restrictive modes: -std=c99 -std=iso9899:1999)
370 dnl IBM XL C -qlanglvl=extc1x (V12.1; does not pass C11 test)
371 dnl IBM XL C -qlanglvl=extc99
372 dnl (pre-V12.1; unused restrictive mode: -qlanglvl=stdc99)
373 dnl HP cc -AC99
374 dnl Intel ICC -std=c99, -c99 (deprecated)
375 dnl IRIX -c99
376 dnl Solaris -D_STDC_C99=
377 dnl cc's -xc99 option uses linker magic to define the external
378 dnl symbol __xpg4 as if by "int __xpg4 = 1;", which enables C99
379 dnl behavior for C library functions. This is not wanted here,
380 dnl because it means that a single module compiled with -xc99
381 dnl alters C runtime behavior for the entire program, not for
382 dnl just the module. Instead, define the (private) symbol
383 dnl _STDC_C99, which suppresses a bogus failure in <stdbool.h>.
384 dnl The resulting compiler passes the test case here, and that's
385 dnl good enough. For more, please see the thread starting at:
386 dnl https://lists.gnu.org/r/autoconf/2010-12/msg00059.html
387 dnl Tru64 -c99
388 dnl with extended modes being tried first.
389 [[-std=gnu99 -std=c99 -c99 -AC99 -D_STDC_C99= -qlanglvl=extc1x -qlanglvl=extc99]], [$1], [$2])[]dnl
390 ])# _AC_PROG_CC_C99
391
392
393 # _AC_PROG_CC_C11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
394 # ----------------------------------------------------------------
395 # If the C compiler is not in ISO C11 mode by default, try to add an
396 # option to output variable CC to make it so. This macro tries
397 # various options that select ISO C11 on some system or another. It
398 # considers the compiler to be in ISO C11 mode if it handles _Alignas,
399 # _Alignof, _Noreturn, _Static_assert, UTF-8 string literals,
400 # duplicate typedefs, and anonymous structures and unions.
401 AC_DEFUN([_AC_PROG_CC_C11],
402 [_AC_C_STD_TRY([c11],
403 [_AC_C_C99_TEST_HEADER[
404 // Check _Alignas.
405 char _Alignas (double) aligned_as_double;
406 char _Alignas (0) no_special_alignment;
407 extern char aligned_as_int;
408 char _Alignas (0) _Alignas (int) aligned_as_int;
409
410 // Check _Alignof.
411 enum
412 {
413 int_alignment = _Alignof (int),
414 int_array_alignment = _Alignof (int[100]),
415 char_alignment = _Alignof (char)
416 };
417 _Static_assert (0 < -_Alignof (int), "_Alignof is signed");
418
419 // Check _Noreturn.
420 int _Noreturn does_not_return (void) { for (;;) continue; }
421
422 // Check _Static_assert.
423 struct test_static_assert
424 {
425 int x;
426 _Static_assert (sizeof (int) <= sizeof (long int),
427 "_Static_assert does not work in struct");
428 long int y;
429 };
430
431 // Check UTF-8 literals.
432 #define u8 syntax error!
433 char const utf8_literal[] = u8"happens to be ASCII" "another string";
434
435 // Check duplicate typedefs.
436 typedef long *long_ptr;
437 typedef long int *long_ptr;
438 typedef long_ptr long_ptr;
439
440 // Anonymous structures and unions -- taken from C11 6.7.2.1 Example 1.
441 struct anonymous
442 {
443 union {
444 struct { int i; int j; };
445 struct { int k; long int l; } w;
446 };
447 int m;
448 } v1;
449 ]],
450 [_AC_C_C99_TEST_BODY[
451 v1.i = 2;
452 v1.w.k = 5;
453 _Static_assert ((offsetof (struct anonymous, i)
454 == offsetof (struct anonymous, w.k)),
455 "Anonymous union alignment botch");
456 ]],
457 dnl Try
458 dnl GCC -std=gnu11 (unused restrictive mode: -std=c11)
459 dnl with extended modes being tried first.
460 dnl
461 dnl Do not try -qlanglvl=extc1x, because IBM XL C V12.1 (the latest version as
462 dnl of September 2012) does not pass the C11 test. For now, try extc1x when
463 dnl compiling the C99 test instead, since it enables _Static_assert and
464 dnl _Noreturn, which is a win. If -qlanglvl=extc11 or -qlanglvl=extc1x passes
465 dnl the C11 test in some future version of IBM XL C, we'll add it here,
466 dnl preferably extc11.
467 [[-std=gnu11]], [$1], [$2])[]dnl
468 ])# _AC_PROG_CC_C11
469
470
471 # AC_PROG_CC_C89
472 # --------------
473 # Do not use AU_ALIAS here and in AC_PROG_CC_C99 and AC_PROG_CC_STDC,
474 # as that'd be incompatible with how Automake redefines AC_PROG_CC. See
475 # <https://lists.gnu.org/r/autoconf/2012-10/msg00048.html>.
476 AU_DEFUN([AC_PROG_CC_C89],
477 [AC_REQUIRE([AC_PROG_CC])],
478 [$0 is obsolete; use AC_PROG_CC]
479 )
480
481 # AC_PROG_CC_C99
482 # --------------
483 AU_DEFUN([AC_PROG_CC_C99],
484 [AC_REQUIRE([AC_PROG_CC])],
485 [$0 is obsolete; use AC_PROG_CC]
486 )
487
488 # AC_PROG_CC_STDC
489 # ---------------
490 AU_DEFUN([AC_PROG_CC_STDC],
491 [AC_REQUIRE([AC_PROG_CC])],
492 [$0 is obsolete; use AC_PROG_CC]
493 )
494
495
496 # AC_C_PROTOTYPES
497 # ---------------
498 # Check if the C compiler supports prototypes, included if it needs
499 # options.
500 AC_DEFUN([AC_C_PROTOTYPES],
501 [AC_REQUIRE([AC_PROG_CC])dnl
502 if test "$ac_prog_cc_stdc" != no; then
503 AC_DEFINE(PROTOTYPES, 1,
504 [Define to 1 if the C compiler supports function prototypes.])
505 AC_DEFINE(__PROTOTYPES, 1,
506 [Define like PROTOTYPES; this can be used by system headers.])
507 fi
508 ])# AC_C_PROTOTYPES
509
510
511 # _AC_CXX_STD_TRY(STANDARD, TEST-PROLOGUE, TEST-BODY, OPTION-LIST,
512 # ACTION-IF-AVAILABLE, ACTION-IF-UNAVAILABLE)
513 # ----------------------------------------------------------------
514 # Check whether the C++ compiler accepts features of STANDARD (e.g
515 # `cxx98', `cxx11') by trying to compile a program of TEST-PROLOGUE
516 # and TEST-BODY. If this fails, try again with each compiler option
517 # in the space-separated OPTION-LIST; if one helps, append it to CXX.
518 # If eventually successful, run ACTION-IF-AVAILABLE, else
519 # ACTION-IF-UNAVAILABLE.
520 AC_DEFUN([_AC_CXX_STD_TRY],
521 [AC_MSG_CHECKING([for $CXX option to enable ]m4_translit(m4_translit($1, [x], [+]), [a-z], [A-Z])[ features])
522 AC_LANG_PUSH(C++)dnl
523 AC_CACHE_VAL(ac_cv_prog_cxx_$1,
524 [ac_cv_prog_cxx_$1=no
525 ac_save_CXX=$CXX
526 AC_LANG_CONFTEST([AC_LANG_PROGRAM([$2], [$3])])
527 for ac_arg in '' $4
528 do
529 CXX="$ac_save_CXX $ac_arg"
530 _AC_COMPILE_IFELSE([], [ac_cv_prog_cxx_$1=$ac_arg])
531 test "x$ac_cv_prog_cxx_$1" != "xno" && break
532 done
533 rm -f conftest.$ac_ext
534 CXX=$ac_save_CXX
535 ])# AC_CACHE_VAL
536 ac_prog_cxx_stdcxx_options=
537 case "x$ac_cv_prog_cxx_$1" in
538 x)
539 AC_MSG_RESULT([none needed]) ;;
540 xno)
541 AC_MSG_RESULT([unsupported]) ;;
542 *)
543 ac_prog_cxx_stdcxx_options=" $ac_cv_prog_cxx_$1"
544 CXX=$CXX$ac_prog_cxx_stdcxx_options
545 AC_MSG_RESULT([$ac_cv_prog_cxx_$1]) ;;
546 esac
547 AC_LANG_POP(C++)dnl
548 AS_IF([test "x$ac_cv_prog_cxx_$1" != xno], [$5], [$6])
549 ])# _AC_CXX_STD_TRY
550
551 # _AC_CXX_CXX98_TEST_HEADER
552 # -------------------------
553 # A C++ header suitable for testing for CXX98.
554 AC_DEFUN([_AC_CXX_CXX98_TEST_HEADER],
555 [[
556 #include <algorithm>
557 #include <cstdlib>
558 #include <fstream>
559 #include <iomanip>
560 #include <iostream>
561 #include <list>
562 #include <map>
563 #include <set>
564 #include <sstream>
565 #include <stdexcept>
566 #include <string>
567 #include <utility>
568 #include <vector>
569
570 namespace test {
571 typedef std::vector<std::string> string_vec;
572 typedef std::pair<int,bool> map_value;
573 typedef std::map<std::string,map_value> map_type;
574 typedef std::set<int> set_type;
575
576 template<typename T>
577 class printer {
578 public:
579 printer(std::ostringstream& os): os(os) {}
580 void operator() (T elem) { os << elem << std::endl; }
581 private:
582 std::ostringstream& os;
583 };
584 }
585 ]])# _AC_CXX_CXX98_TEST_HEADER
586
587 # _AC_CXX_CXX98_TEST_BODY
588 # -----------------------
589 # A C++ body suitable for testing for CXX98, assuming the corresponding header.
590 AC_DEFUN([_AC_CXX_CXX98_TEST_BODY],
591 [[
592
593 try {
594 // Basic string.
595 std::string teststr("ASCII text");
596 teststr += " string";
597
598 // Simple vector.
599 test::string_vec testvec;
600 testvec.push_back(teststr);
601 testvec.push_back("foo");
602 testvec.push_back("bar");
603 if (testvec.size() != 3) {
604 throw std::runtime_error("vector size is not 1");
605 }
606
607 // Dump vector into stringstream and obtain string.
608 std::ostringstream os;
609 for (test::string_vec::const_iterator i = testvec.begin();
610 i != testvec.end(); ++i) {
611 if (i + 1 != testvec.end()) {
612 os << teststr << '\n';
613 }
614 }
615 // Check algorithms work.
616 std::for_each(testvec.begin(), testvec.end(), test::printer<std::string>(os));
617 std::string os_out = os.str();
618
619 // Test pair and map.
620 test::map_type testmap;
621 testmap.insert(std::make_pair(std::string("key"),
622 std::make_pair(53,false)));
623
624 // Test set.
625 int values[] = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
626 test::set_type testset(values, values + sizeof(values)/sizeof(values[0]));
627 std::list<int> testlist(testset.begin(), testset.end());
628 std::copy(testset.begin(), testset.end(), std::back_inserter(testlist));
629 } catch (const std::exception& e) {
630 std::cerr << "Caught exception: " << e.what() << std::endl;
631
632 // Test fstream
633 std::ofstream of("test.txt");
634 of << "Test ASCII text\n" << std::flush;
635 of << "N= " << std::hex << std::setw(8) << std::left << 534 << std::endl;
636 of.close();
637 }
638 std::exit(0);
639 ]])
640
641 # _AC_CXX_CXX11_TEST_HEADER
642 # -------------------------
643 # A C++ header suitable for testing for CXX11.
644 AC_DEFUN([_AC_CXX_CXX11_TEST_HEADER],
645 [[
646 #include <deque>
647 #include <functional>
648 #include <memory>
649 #include <tuple>
650 #include <array>
651 #include <regex>
652 #include <iostream>
653
654 namespace cxx11test
655 {
656 typedef std::shared_ptr<std::string> sptr;
657 typedef std::weak_ptr<std::string> wptr;
658
659 typedef std::tuple<std::string,int,double> tp;
660 typedef std::array<int, 20> int_array;
661
662 constexpr int get_val() { return 20; }
663
664 struct testinit
665 {
666 int i;
667 double d;
668 };
669
670 class delegate {
671 public:
672 delegate(int n) : n(n) {}
673 delegate(): delegate(2354) {}
674
675 virtual int getval() { return this->n; };
676 protected:
677 int n;
678 };
679
680 class overridden : public delegate {
681 public:
682 overridden(int n): delegate(n) {}
683 virtual int getval() override final { return this->n * 2; }
684 };
685
686 class nocopy {
687 public:
688 nocopy(int i): i(i) {}
689 nocopy() = default;
690 nocopy(const nocopy&) = delete;
691 nocopy & operator=(const nocopy&) = delete;
692 private:
693 int i;
694 };
695 }
696 ]])# _AC_CXX_CXX11_TEST_HEADER
697
698 # _AC_CXX_CXX11_TEST_BODY
699 # -----------------------
700 # A C++ body suitable for testing for CXX11, assuming the corresponding header.
701 AC_DEFUN([_AC_CXX_CXX11_TEST_BODY],
702 [[
703 {
704 // Test auto and decltype
705 std::deque<int> d;
706 d.push_front(43);
707 d.push_front(484);
708 d.push_front(3);
709 d.push_front(844);
710 int total = 0;
711 for (auto i = d.begin(); i != d.end(); ++i) { total += *i; }
712
713 auto a1 = 6538;
714 auto a2 = 48573953.4;
715 auto a3 = "String literal";
716
717 decltype(a2) a4 = 34895.034;
718 }
719 {
720 // Test constexpr
721 short sa[cxx11test::get_val()] = { 0 };
722 }
723 {
724 // Test initializer lists
725 cxx11test::testinit il = { 4323, 435234.23544 };
726 }
727 {
728 // Test range-based for and lambda
729 cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
730 for (int &x : array) { x += 23; }
731 std::for_each(array.begin(), array.end(), [](int v1){ std::cout << v1; });
732 }
733 {
734 using cxx11test::sptr;
735 using cxx11test::wptr;
736
737 sptr sp(new std::string("ASCII string"));
738 wptr wp(sp);
739 sptr sp2(wp);
740 }
741 {
742 cxx11test::tp tuple("test", 54, 45.53434);
743 double d = std::get<2>(tuple);
744 std::string s;
745 int i;
746 std::tie(s,i,d) = tuple;
747 }
748 {
749 static std::regex filename_regex("^_?([a-z0-9_.]+-)+[a-z0-9]+$");
750 std::string testmatch("Test if this string matches");
751 bool match = std::regex_search(testmatch, filename_regex);
752 }
753 {
754 cxx11test::int_array array = {9, 7, 13, 15, 4, 18, 12, 10, 5, 3, 14, 19, 17, 8, 6, 20, 16, 2, 11, 1};
755 cxx11test::int_array::size_type size = array.size();
756 }
757 {
758 // Test constructor delegation
759 cxx11test::delegate d1;
760 cxx11test::delegate d2();
761 cxx11test::delegate d3(45);
762 }
763 {
764 // Test override and final
765 cxx11test::overridden o1(55464);
766 }
767 {
768 // Test nullptr
769 char *c = nullptr;
770 }
771 {
772 // Test template brackets
773 std::vector<std::pair<int,char*>> v1;
774 }
775 {
776 // Unicode literals
777 char const *utf8 = u8"UTF-8 string \u2500";
778 char16_t const *utf16 = u"UTF-8 string \u2500";
779 char32_t const *utf32 = U"UTF-32 string \u2500";
780 }
781 ]])
782
783 # _AC_PROG_CXX_CXX98 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
784 # -------------------------------------------------------------------
785
786 # If the C++ compiler is not in ISO C++98 mode by default, try to add
787 # an option to output variable CXX to make it so. This macro tries
788 # various options that select ISO C++98 on some system or another. It
789 # considers the compiler to be in ISO C++98 mode if it handles basic
790 # features of the std namespace including: string, containers (list,
791 # map, set, vector), streams (fstreams, iostreams, stringstreams,
792 # iomanip), pair, exceptions and algorithms.
793
794
795 AC_DEFUN([_AC_PROG_CXX_CXX98],
796 [_AC_CXX_STD_TRY([cxx98],
797 [_AC_CXX_CXX98_TEST_HEADER],
798 [_AC_CXX_CXX98_TEST_BODY],
799 dnl Try
800 dnl GCC -std=gnu++98 (unused restrictive mode: -std=c++98)
801 dnl IBM XL C -qlanglvl=extended
802 dnl HP aC++ -AA
803 dnl Intel ICC -std=gnu++98
804 dnl Solaris N/A (default)
805 dnl Tru64 N/A (default, but -std gnu could be used)
806 dnl with extended modes being tried first.
807 [[-std=gnu++98 -std=c++98 -qlanglvl=extended -AA]], [$1], [$2])[]dnl
808 ])# _AC_PROG_CXX_CXX98
809
810 # _AC_PROG_CXX_CXX11 ([ACTION-IF-AVAILABLE], [ACTION-IF-UNAVAILABLE])
811 # -------------------------------------------------------------------
812 # If the C++ compiler is not in ISO CXX11 mode by default, try to add
813 # an option to output variable CXX to make it so. This macro tries
814 # various options that select ISO C++11 on some system or another. It
815 # considers the compiler to be in ISO C++11 mode if it handles all the
816 # tests from the C++98 checks, plus the following: Language features
817 # (auto, constexpr, decltype, default/deleted constructors, delegate
818 # constructors, final, initializer lists, lambda functions, nullptr,
819 # override, range-based for loops, template brackets without spaces,
820 # unicode literals) and library features (array, memory (shared_ptr,
821 # weak_ptr), regex and tuple types).
822 AC_DEFUN([_AC_PROG_CXX_CXX11],
823 [_AC_CXX_STD_TRY([cxx11],
824 [_AC_CXX_CXX11_TEST_HEADER
825 _AC_CXX_CXX98_TEST_HEADER],
826 [_AC_CXX_CXX11_TEST_BODY
827 _AC_CXX_CXX98_TEST_BODY],
828 dnl Try
829 dnl GCC -std=gnu++11 (unused restrictive mode: -std=c++11) [and 0x variants]
830 dnl IBM XL C -qlanglvl=extended0x
831 dnl (pre-V12.1; unused restrictive mode: -qlanglvl=stdcxx11)
832 dnl HP aC++ -AA
833 dnl Intel ICC -std=c++11 -std=c++0x
834 dnl Solaris N/A (no support)
835 dnl Tru64 N/A (no support)
836 dnl with extended modes being tried first.
837 [[-std=gnu++11 -std=c++11 -std=gnu++0x -std=c++0x -qlanglvl=extended0x -AA]], [$1], [$2])[]dnl
838 ])# _AC_PROG_CXX_CXX11
839
840
841 ])# m4_version_prereq