Debian Codegraph @

tests/data/gnulib_std_gnu11_2020_08_17.m4

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