Document the GDB 10.2 release in gdb/ChangeLog
[binutils-gdb.git] / gdb / ax_cxx_compile_stdcxx.m4
blob413755a2e886c69ae8eff75cadb9db5390cf0cb6
1 # Copyright (c) 2016-2021 Free Software Foundation, Inc.
3 # Originally based on the AX_CXX_COMPILE_STDCXX macro found at the url
4 # below.
6 # Local GDB customizations:
8 # - AC_SUBST CXX_DIALECT instead of changing CXX/CXXCPP.
10 # ===========================================================================
11 #  https://www.gnu.org/software/autoconf-archive/ax_cxx_compile_stdcxx.html
12 # ===========================================================================
14 # SYNOPSIS
16 #   AX_CXX_COMPILE_STDCXX(VERSION, [ext|noext], [mandatory|optional])
18 # DESCRIPTION
20 #   Check for baseline language coverage in the compiler for the specified
21 #   version of the C++ standard.  If necessary, add switches to CXX and
22 #   CXXCPP to enable support.  VERSION may be '11' (for the C++11 standard)
23 #   or '14' (for the C++14 standard).
25 #   The second argument, if specified, indicates whether you insist on an
26 #   extended mode (e.g. -std=gnu++11) or a strict conformance mode (e.g.
27 #   -std=c++11).  If neither is specified, you get whatever works, with
28 #   preference for an extended mode.
30 #   The third argument, if specified 'mandatory' or if left unspecified,
31 #   indicates that baseline support for the specified C++ standard is
32 #   required and that the macro should error out if no mode with that
33 #   support is found.  If specified 'optional', then configuration proceeds
34 #   regardless, after defining HAVE_CXX${VERSION} if and only if a
35 #   supporting mode is found.
37 # LICENSE
39 #   Copyright (c) 2008 Benjamin Kosnik <bkoz@redhat.com>
40 #   Copyright (c) 2012 Zack Weinberg <zackw@panix.com>
41 #   Copyright (c) 2013 Roy Stogner <roystgnr@ices.utexas.edu>
42 #   Copyright (c) 2014, 2015 Google Inc.; contributed by Alexey Sokolov <sokolov@google.com>
43 #   Copyright (c) 2015 Paul Norman <penorman@mac.com>
44 #   Copyright (c) 2015 Moritz Klammler <moritz@klammler.eu>
45 #   Copyright (c) 2016 Krzesimir Nowak <qdlacz@gmail.com>
47 #   Copying and distribution of this file, with or without modification, are
48 #   permitted in any medium without royalty provided the copyright notice
49 #   and this notice are preserved.  This file is offered as-is, without any
50 #   warranty.
52 #serial 8
54 dnl  This macro is based on the code from the AX_CXX_COMPILE_STDCXX_11 macro
55 dnl  (serial version number 13).
57 AC_DEFUN([AX_CXX_COMPILE_STDCXX], [dnl
58   m4_if([$1], [11], [ax_cxx_compile_alternatives="11 0x"],
59         [$1], [14], [ax_cxx_compile_alternatives="14 1y"],
60         [$1], [17], [ax_cxx_compile_alternatives="17 1z"],
61         [m4_fatal([invalid first argument `$1' to AX_CXX_COMPILE_STDCXX])])dnl
62   m4_if([$2], [], [],
63         [$2], [ext], [],
64         [$2], [noext], [],
65         [m4_fatal([invalid second argument `$2' to AX_CXX_COMPILE_STDCXX])])dnl
66   m4_if([$3], [], [ax_cxx_compile_cxx$1_required=true],
67         [$3], [mandatory], [ax_cxx_compile_cxx$1_required=true],
68         [$3], [optional], [ax_cxx_compile_cxx$1_required=false],
69         [m4_fatal([invalid third argument `$3' to AX_CXX_COMPILE_STDCXX])])
70   AC_LANG_PUSH([C++])dnl
71   CXX_DIALECT=""
72   ac_success=no
73   AC_CACHE_CHECK(whether $CXX supports C++$1 features by default,
74   ax_cv_cxx_compile_cxx$1,
75   [AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
76     [ax_cv_cxx_compile_cxx$1=yes],
77     [ax_cv_cxx_compile_cxx$1=no])])
78   if test x$ax_cv_cxx_compile_cxx$1 = xyes; then
79     ac_success=yes
80   fi
82   m4_if([$2], [noext], [], [dnl
83   if test x$ac_success = xno; then
84     for alternative in ${ax_cxx_compile_alternatives}; do
85       switch="-std=gnu++${alternative}"
86       cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
87       AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
88                      $cachevar,
89         [ac_save_CXX="$CXX"
90          CXX="$CXX $switch"
91          AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
92           [eval $cachevar=yes],
93           [eval $cachevar=no])
94          CXX="$ac_save_CXX"])
95       if eval test x\$$cachevar = xyes; then
96         CXX_DIALECT="$switch"
97         ac_success=yes
98         break
99       fi
100     done
101   fi])
103   m4_if([$2], [ext], [], [dnl
104   if test x$ac_success = xno; then
105     dnl HP's aCC needs +std=c++11 according to:
106     dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
107     dnl Cray's crayCC needs "-h std=c++11"
108     for alternative in ${ax_cxx_compile_alternatives}; do
109       for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
110         cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
111         AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
112                        $cachevar,
113           [ac_save_CXX="$CXX"
114            CXX="$CXX $switch"
115            AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
116             [eval $cachevar=yes],
117             [eval $cachevar=no])
118            CXX="$ac_save_CXX"])
119         if eval test x\$$cachevar = xyes; then
120           CXX_DIALECT="$switch"
121           ac_success=yes
122           break
123         fi
124       done
125       if test x$ac_success = xyes; then
126         break
127       fi
128     done
129   fi])
130   AC_LANG_POP([C++])
131   if test x$ax_cxx_compile_cxx$1_required = xtrue; then
132     if test x$ac_success = xno; then
133       AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
134     fi
135   fi
136   if test x$ac_success = xno; then
137     HAVE_CXX$1=0
138     AC_MSG_NOTICE([No compiler with C++$1 support was found])
139   else
140     HAVE_CXX$1=1
141     AC_DEFINE(HAVE_CXX$1,1,
142               [define if the compiler supports basic C++$1 syntax])
143   fi
144   AC_SUBST(HAVE_CXX$1)
145   AC_SUBST(CXX_DIALECT)
149 dnl  Test body for checking C++11 support
151 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
152   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
156 dnl  Test body for checking C++14 support
158 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
159   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
160   _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
163 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
164   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
165   _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
166   _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
169 dnl  Tests for new features in C++11
171 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
173 // If the compiler admits that it is not ready for C++11, why torture it?
174 // Hopefully, this will speed up the test.
176 #ifndef __cplusplus
178 #error "This is not a C++ compiler"
180 #elif __cplusplus < 201103L
182 #error "This is not a C++11 compiler"
184 #else
186 namespace cxx11
189   namespace test_static_assert
190   {
192     template <typename T>
193     struct check
194     {
195       static_assert(sizeof(int) <= sizeof(T), "not big enough");
196     };
198   }
200   namespace test_final_override
201   {
203     struct Base
204     {
205       virtual void f() {}
206     };
208     struct Derived : public Base
209     {
210       virtual void f() override {}
211     };
213   }
215   namespace test_double_right_angle_brackets
216   {
218     template < typename T >
219     struct check {};
221     typedef check<void> single_type;
222     typedef check<check<void>> double_type;
223     typedef check<check<check<void>>> triple_type;
224     typedef check<check<check<check<void>>>> quadruple_type;
226   }
228   namespace test_decltype
229   {
231     int
232     f()
233     {
234       int a = 1;
235       decltype(a) b = 2;
236       return a + b;
237     }
239   }
241   namespace test_type_deduction
242   {
244     template < typename T1, typename T2 >
245     struct is_same
246     {
247       static const bool value = false;
248     };
250     template < typename T >
251     struct is_same<T, T>
252     {
253       static const bool value = true;
254     };
256     template < typename T1, typename T2 >
257     auto
258     add(T1 a1, T2 a2) -> decltype(a1 + a2)
259     {
260       return a1 + a2;
261     }
263     int
264     test(const int c, volatile int v)
265     {
266       static_assert(is_same<int, decltype(0)>::value == true, "");
267       static_assert(is_same<int, decltype(c)>::value == false, "");
268       static_assert(is_same<int, decltype(v)>::value == false, "");
269       auto ac = c;
270       auto av = v;
271       auto sumi = ac + av + 'x';
272       auto sumf = ac + av + 1.0;
273       static_assert(is_same<int, decltype(ac)>::value == true, "");
274       static_assert(is_same<int, decltype(av)>::value == true, "");
275       static_assert(is_same<int, decltype(sumi)>::value == true, "");
276       static_assert(is_same<int, decltype(sumf)>::value == false, "");
277       static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
278       return (sumf > 0.0) ? sumi : add(c, v);
279     }
281   }
283   namespace test_noexcept
284   {
286     int f() { return 0; }
287     int g() noexcept { return 0; }
289     static_assert(noexcept(f()) == false, "");
290     static_assert(noexcept(g()) == true, "");
292   }
294   namespace test_constexpr
295   {
297     template < typename CharT >
298     unsigned long constexpr
299     strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
300     {
301       return *s ? strlen_c_r(s + 1, acc + 1) : acc;
302     }
304     template < typename CharT >
305     unsigned long constexpr
306     strlen_c(const CharT *const s) noexcept
307     {
308       return strlen_c_r(s, 0UL);
309     }
311     static_assert(strlen_c("") == 0UL, "");
312     static_assert(strlen_c("1") == 1UL, "");
313     static_assert(strlen_c("example") == 7UL, "");
314     static_assert(strlen_c("another\0example") == 7UL, "");
316   }
318   namespace test_rvalue_references
319   {
321     template < int N >
322     struct answer
323     {
324       static constexpr int value = N;
325     };
327     answer<1> f(int&)       { return answer<1>(); }
328     answer<2> f(const int&) { return answer<2>(); }
329     answer<3> f(int&&)      { return answer<3>(); }
331     void
332     test()
333     {
334       int i = 0;
335       const int c = 0;
336       static_assert(decltype(f(i))::value == 1, "");
337       static_assert(decltype(f(c))::value == 2, "");
338       static_assert(decltype(f(0))::value == 3, "");
339     }
341   }
343   namespace test_uniform_initialization
344   {
346     struct test
347     {
348       static const int zero {};
349       static const int one {1};
350     };
352     static_assert(test::zero == 0, "");
353     static_assert(test::one == 1, "");
355   }
357   namespace test_lambdas
358   {
360     void
361     test1()
362     {
363       auto lambda1 = [](){};
364       auto lambda2 = lambda1;
365       lambda1();
366       lambda2();
367     }
369     int
370     test2()
371     {
372       auto a = [](int i, int j){ return i + j; }(1, 2);
373       auto b = []() -> int { return '0'; }();
374       auto c = [=](){ return a + b; }();
375       auto d = [&](){ return c; }();
376       auto e = [a, &b](int x) mutable {
377         const auto identity = [](int y){ return y; };
378         for (auto i = 0; i < a; ++i)
379           a += b--;
380         return x + identity(a + b);
381       }(0);
382       return a + b + c + d + e;
383     }
385     int
386     test3()
387     {
388       const auto nullary = [](){ return 0; };
389       const auto unary = [](int x){ return x; };
390       using nullary_t = decltype(nullary);
391       using unary_t = decltype(unary);
392       const auto higher1st = [](nullary_t f){ return f(); };
393       const auto higher2nd = [unary](nullary_t f1){
394         return [unary, f1](unary_t f2){ return f2(unary(f1())); };
395       };
396       return higher1st(nullary) + higher2nd(nullary)(unary);
397     }
399   }
401   namespace test_variadic_templates
402   {
404     template <int...>
405     struct sum;
407     template <int N0, int... N1toN>
408     struct sum<N0, N1toN...>
409     {
410       static constexpr auto value = N0 + sum<N1toN...>::value;
411     };
413     template <>
414     struct sum<>
415     {
416       static constexpr auto value = 0;
417     };
419     static_assert(sum<>::value == 0, "");
420     static_assert(sum<1>::value == 1, "");
421     static_assert(sum<23>::value == 23, "");
422     static_assert(sum<1, 2>::value == 3, "");
423     static_assert(sum<5, 5, 11>::value == 21, "");
424     static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
426   }
428   // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
429   // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
430   // because of this.
431   namespace test_template_alias_sfinae
432   {
434     struct foo {};
436     template<typename T>
437     using member = typename T::member_type;
439     template<typename T>
440     void func(...) {}
442     template<typename T>
443     void func(member<T>*) {}
445     void test();
447     void test() { func<foo>(0); }
449   }
451 }  // namespace cxx11
453 #endif  // __cplusplus >= 201103L
458 dnl  Tests for new features in C++14
460 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
462 // If the compiler admits that it is not ready for C++14, why torture it?
463 // Hopefully, this will speed up the test.
465 #ifndef __cplusplus
467 #error "This is not a C++ compiler"
469 #elif __cplusplus < 201402L
471 #error "This is not a C++14 compiler"
473 #else
475 namespace cxx14
478   namespace test_polymorphic_lambdas
479   {
481     int
482     test()
483     {
484       const auto lambda = [](auto&&... args){
485         const auto istiny = [](auto x){
486           return (sizeof(x) == 1UL) ? 1 : 0;
487         };
488         const int aretiny[] = { istiny(args)... };
489         return aretiny[0];
490       };
491       return lambda(1, 1L, 1.0f, '1');
492     }
494   }
496   namespace test_binary_literals
497   {
499     constexpr auto ivii = 0b0000000000101010;
500     static_assert(ivii == 42, "wrong value");
502   }
504   namespace test_generalized_constexpr
505   {
507     template < typename CharT >
508     constexpr unsigned long
509     strlen_c(const CharT *const s) noexcept
510     {
511       auto length = 0UL;
512       for (auto p = s; *p; ++p)
513         ++length;
514       return length;
515     }
517     static_assert(strlen_c("") == 0UL, "");
518     static_assert(strlen_c("x") == 1UL, "");
519     static_assert(strlen_c("test") == 4UL, "");
520     static_assert(strlen_c("another\0test") == 7UL, "");
522   }
524   namespace test_lambda_init_capture
525   {
527     int
528     test()
529     {
530       auto x = 0;
531       const auto lambda1 = [a = x](int b){ return a + b; };
532       const auto lambda2 = [a = lambda1(x)](){ return a; };
533       return lambda2();
534     }
536   }
538   namespace test_digit_separators
539   {
541     constexpr auto ten_million = 100'000'000;
542     static_assert(ten_million == 100000000, "");
544   }
546   namespace test_return_type_deduction
547   {
549     auto f(int& x) { return x; }
550     decltype(auto) g(int& x) { return x; }
552     template < typename T1, typename T2 >
553     struct is_same
554     {
555       static constexpr auto value = false;
556     };
558     template < typename T >
559     struct is_same<T, T>
560     {
561       static constexpr auto value = true;
562     };
564     int
565     test()
566     {
567       auto x = 0;
568       static_assert(is_same<int, decltype(f(x))>::value, "");
569       static_assert(is_same<int&, decltype(g(x))>::value, "");
570       return x;
571     }
573   }
575 }  // namespace cxx14
577 #endif  // __cplusplus >= 201402L
582 dnl  Tests for new features in C++17
584 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
586 // If the compiler admits that it is not ready for C++17, why torture it?
587 // Hopefully, this will speed up the test.
589 #ifndef __cplusplus
591 #error "This is not a C++ compiler"
593 #elif __cplusplus <= 201402L
595 #error "This is not a C++17 compiler"
597 #else
599 #if defined(__clang__)
600   #define REALLY_CLANG
601 #else
602   #if defined(__GNUC__)
603     #define REALLY_GCC
604   #endif
605 #endif
607 #include <initializer_list>
608 #include <utility>
609 #include <type_traits>
611 namespace cxx17
614 #if !defined(REALLY_CLANG)
615   namespace test_constexpr_lambdas
616   {
618     // TODO: test it with clang++ from git
620     constexpr int foo = [](){return 42;}();
622   }
623 #endif // !defined(REALLY_CLANG)
625   namespace test::nested_namespace::definitions
626   {
628   }
630   namespace test_fold_expression
631   {
633     template<typename... Args>
634     int multiply(Args... args)
635     {
636       return (args * ... * 1);
637     }
639     template<typename... Args>
640     bool all(Args... args)
641     {
642       return (args && ...);
643     }
645   }
647   namespace test_extended_static_assert
648   {
650     static_assert (true);
652   }
654   namespace test_auto_brace_init_list
655   {
657     auto foo = {5};
658     auto bar {5};
660     static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
661     static_assert(std::is_same<int, decltype(bar)>::value);
662   }
664   namespace test_typename_in_template_template_parameter
665   {
667     template<template<typename> typename X> struct D;
669   }
671   namespace test_fallthrough_nodiscard_maybe_unused_attributes
672   {
674     int f1()
675     {
676       return 42;
677     }
679     [[nodiscard]] int f2()
680     {
681       [[maybe_unused]] auto unused = f1();
683       switch (f1())
684       {
685       case 17:
686         f1();
687         [[fallthrough]];
688       case 42:
689         f1();
690       }
691       return f1();
692     }
694   }
696   namespace test_extended_aggregate_initialization
697   {
699     struct base1
700     {
701       int b1, b2 = 42;
702     };
704     struct base2
705     {
706       base2() {
707         b3 = 42;
708       }
709       int b3;
710     };
712     struct derived : base1, base2
713     {
714         int d;
715     };
717     derived d1 {{1, 2}, {}, 4};  // full initialization
718     derived d2 {{}, {}, 4};      // value-initialized bases
720   }
722   namespace test_general_range_based_for_loop
723   {
725     struct iter
726     {
727       int i;
729       int& operator* ()
730       {
731         return i;
732       }
734       const int& operator* () const
735       {
736         return i;
737       }
739       iter& operator++()
740       {
741         ++i;
742         return *this;
743       }
744     };
746     struct sentinel
747     {
748       int i;
749     };
751     bool operator== (const iter& i, const sentinel& s)
752     {
753       return i.i == s.i;
754     }
756     bool operator!= (const iter& i, const sentinel& s)
757     {
758       return !(i == s);
759     }
761     struct range
762     {
763       iter begin() const
764       {
765         return {0};
766       }
768       sentinel end() const
769       {
770         return {5};
771       }
772     };
774     void f()
775     {
776       range r {};
778       for (auto i : r)
779       {
780         [[maybe_unused]] auto v = i;
781       }
782     }
784   }
786   namespace test_lambda_capture_asterisk_this_by_value
787   {
789     struct t
790     {
791       int i;
792       int foo()
793       {
794         return [*this]()
795         {
796           return i;
797         }();
798       }
799     };
801   }
803   namespace test_enum_class_construction
804   {
806     enum class byte : unsigned char
807     {};
809     byte foo {42};
811   }
813   namespace test_constexpr_if
814   {
816     template <bool cond>
817     int f ()
818     {
819       if constexpr(cond)
820       {
821         return 13;
822       }
823       else
824       {
825         return 42;
826       }
827     }
829   }
831   namespace test_selection_statement_with_initializer
832   {
834     int f()
835     {
836       return 13;
837     }
839     int f2()
840     {
841       if (auto i = f(); i > 0)
842       {
843         return 3;
844       }
846       switch (auto i = f(); i + 4)
847       {
848       case 17:
849         return 2;
851       default:
852         return 1;
853       }
854     }
856   }
858 #if !defined(REALLY_CLANG)
859   namespace test_template_argument_deduction_for_class_templates
860   {
862     // TODO: test it with clang++ from git
864     template <typename T1, typename T2>
865     struct pair
866     {
867       pair (T1 p1, T2 p2)
868         : m1 {p1},
869           m2 {p2}
870       {}
872       T1 m1;
873       T2 m2;
874     };
876     void f()
877     {
878       [[maybe_unused]] auto p = pair{13, 42u};
879     }
881   }
882 #endif // !defined(REALLY_CLANG)
884   namespace test_non_type_auto_template_parameters
885   {
887     template <auto n>
888     struct B
889     {};
891     B<5> b1;
892     B<'a'> b2;
894   }
896 #if !defined(REALLY_CLANG)
897   namespace test_structured_bindings
898   {
900     // TODO: test it with clang++ from git
902     int arr[2] = { 1, 2 };
903     std::pair<int, int> pr = { 1, 2 };
905     auto f1() -> int(&)[2]
906     {
907       return arr;
908     }
910     auto f2() -> std::pair<int, int>&
911     {
912       return pr;
913     }
915     struct S
916     {
917       int x1 : 2;
918       volatile double y1;
919     };
921     S f3()
922     {
923       return {};
924     }
926     auto [ x1, y1 ] = f1();
927     auto& [ xr1, yr1 ] = f1();
928     auto [ x2, y2 ] = f2();
929     auto& [ xr2, yr2 ] = f2();
930     const auto [ x3, y3 ] = f3();
932   }
933 #endif // !defined(REALLY_CLANG)
935 #if !defined(REALLY_CLANG)
936   namespace test_exception_spec_type_system
937   {
939     // TODO: test it with clang++ from git
941     struct Good {};
942     struct Bad {};
944     void g1() noexcept;
945     void g2();
947     template<typename T>
948     Bad
949     f(T*, T*);
951     template<typename T1, typename T2>
952     Good
953     f(T1*, T2*);
955     static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
957   }
958 #endif // !defined(REALLY_CLANG)
960   namespace test_inline_variables
961   {
963     template<class T> void f(T)
964     {}
966     template<class T> inline T g(T)
967     {
968       return T{};
969     }
971     template<> inline void f<>(int)
972     {}
974     template<> int g<>(int)
975     {
976       return 5;
977     }
979   }
981 }  // namespace cxx17
983 #endif  // __cplusplus <= 201402L