Set development and experimental to false. Update version number to 2.40. Add relea...
[binutils-gdb.git] / gdb / ax_cxx_compile_stdcxx.m4
bloba4360ad37b29b034dca8fd3fe9f3ddf795cd2c16
1 # Copyright (c) 2016-2022 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         CXX="$CXX $switch"
98         if test -n "$CXXCPP" ; then
99           CXXCPP="$CXXCPP $switch"
100         fi
101         ac_success=yes
102         break
103       fi
104     done
105   fi])
107   m4_if([$2], [ext], [], [dnl
108   if test x$ac_success = xno; then
109     dnl HP's aCC needs +std=c++11 according to:
110     dnl http://h21007.www2.hp.com/portal/download/files/unprot/aCxx/PDF_Release_Notes/769149-001.pdf
111     dnl Cray's crayCC needs "-h std=c++11"
112     for alternative in ${ax_cxx_compile_alternatives}; do
113       for switch in -std=c++${alternative} +std=c++${alternative} "-h std=c++${alternative}"; do
114         cachevar=AS_TR_SH([ax_cv_cxx_compile_cxx$1_$switch])
115         AC_CACHE_CHECK(whether $CXX supports C++$1 features with $switch,
116                        $cachevar,
117           [ac_save_CXX="$CXX"
118            CXX="$CXX $switch"
119            AC_COMPILE_IFELSE([AC_LANG_SOURCE([_AX_CXX_COMPILE_STDCXX_testbody_$1])],
120             [eval $cachevar=yes],
121             [eval $cachevar=no])
122            CXX="$ac_save_CXX"])
123         if eval test x\$$cachevar = xyes; then
124           CXX_DIALECT="$switch"
125           CXX="$CXX $switch"
126           if test -n "$CXXCPP" ; then
127             CXXCPP="$CXXCPP $switch"
128           fi
129           ac_success=yes
130           break
131         fi
132       done
133       if test x$ac_success = xyes; then
134         break
135       fi
136     done
137   fi])
138   AC_LANG_POP([C++])
139   if test x$ax_cxx_compile_cxx$1_required = xtrue; then
140     if test x$ac_success = xno; then
141       AC_MSG_ERROR([*** A compiler with support for C++$1 language features is required.])
142     fi
143   fi
144   if test x$ac_success = xno; then
145     HAVE_CXX$1=0
146     AC_MSG_NOTICE([No compiler with C++$1 support was found])
147   else
148     HAVE_CXX$1=1
149     AC_DEFINE(HAVE_CXX$1,1,
150               [define if the compiler supports basic C++$1 syntax])
151   fi
152   AC_SUBST(HAVE_CXX$1)
153   AC_SUBST(CXX_DIALECT)
157 dnl  Test body for checking C++11 support
159 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_11],
160   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
164 dnl  Test body for checking C++14 support
166 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_14],
167   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
168   _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
171 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_17],
172   _AX_CXX_COMPILE_STDCXX_testbody_new_in_11
173   _AX_CXX_COMPILE_STDCXX_testbody_new_in_14
174   _AX_CXX_COMPILE_STDCXX_testbody_new_in_17
177 dnl  Tests for new features in C++11
179 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_11], [[
181 // If the compiler admits that it is not ready for C++11, why torture it?
182 // Hopefully, this will speed up the test.
184 #ifndef __cplusplus
186 #error "This is not a C++ compiler"
188 #elif __cplusplus < 201103L
190 #error "This is not a C++11 compiler"
192 #else
194 namespace cxx11
197   namespace test_static_assert
198   {
200     template <typename T>
201     struct check
202     {
203       static_assert(sizeof(int) <= sizeof(T), "not big enough");
204     };
206   }
208   namespace test_final_override
209   {
211     struct Base
212     {
213       virtual void f() {}
214     };
216     struct Derived : public Base
217     {
218       virtual void f() override {}
219     };
221   }
223   namespace test_double_right_angle_brackets
224   {
226     template < typename T >
227     struct check {};
229     typedef check<void> single_type;
230     typedef check<check<void>> double_type;
231     typedef check<check<check<void>>> triple_type;
232     typedef check<check<check<check<void>>>> quadruple_type;
234   }
236   namespace test_decltype
237   {
239     int
240     f()
241     {
242       int a = 1;
243       decltype(a) b = 2;
244       return a + b;
245     }
247   }
249   namespace test_type_deduction
250   {
252     template < typename T1, typename T2 >
253     struct is_same
254     {
255       static const bool value = false;
256     };
258     template < typename T >
259     struct is_same<T, T>
260     {
261       static const bool value = true;
262     };
264     template < typename T1, typename T2 >
265     auto
266     add(T1 a1, T2 a2) -> decltype(a1 + a2)
267     {
268       return a1 + a2;
269     }
271     int
272     test(const int c, volatile int v)
273     {
274       static_assert(is_same<int, decltype(0)>::value == true, "");
275       static_assert(is_same<int, decltype(c)>::value == false, "");
276       static_assert(is_same<int, decltype(v)>::value == false, "");
277       auto ac = c;
278       auto av = v;
279       auto sumi = ac + av + 'x';
280       auto sumf = ac + av + 1.0;
281       static_assert(is_same<int, decltype(ac)>::value == true, "");
282       static_assert(is_same<int, decltype(av)>::value == true, "");
283       static_assert(is_same<int, decltype(sumi)>::value == true, "");
284       static_assert(is_same<int, decltype(sumf)>::value == false, "");
285       static_assert(is_same<int, decltype(add(c, v))>::value == true, "");
286       return (sumf > 0.0) ? sumi : add(c, v);
287     }
289   }
291   namespace test_noexcept
292   {
294     int f() { return 0; }
295     int g() noexcept { return 0; }
297     static_assert(noexcept(f()) == false, "");
298     static_assert(noexcept(g()) == true, "");
300   }
302   namespace test_constexpr
303   {
305     template < typename CharT >
306     unsigned long constexpr
307     strlen_c_r(const CharT *const s, const unsigned long acc) noexcept
308     {
309       return *s ? strlen_c_r(s + 1, acc + 1) : acc;
310     }
312     template < typename CharT >
313     unsigned long constexpr
314     strlen_c(const CharT *const s) noexcept
315     {
316       return strlen_c_r(s, 0UL);
317     }
319     static_assert(strlen_c("") == 0UL, "");
320     static_assert(strlen_c("1") == 1UL, "");
321     static_assert(strlen_c("example") == 7UL, "");
322     static_assert(strlen_c("another\0example") == 7UL, "");
324   }
326   namespace test_rvalue_references
327   {
329     template < int N >
330     struct answer
331     {
332       static constexpr int value = N;
333     };
335     answer<1> f(int&)       { return answer<1>(); }
336     answer<2> f(const int&) { return answer<2>(); }
337     answer<3> f(int&&)      { return answer<3>(); }
339     void
340     test()
341     {
342       int i = 0;
343       const int c = 0;
344       static_assert(decltype(f(i))::value == 1, "");
345       static_assert(decltype(f(c))::value == 2, "");
346       static_assert(decltype(f(0))::value == 3, "");
347     }
349   }
351   namespace test_uniform_initialization
352   {
354     struct test
355     {
356       static const int zero {};
357       static const int one {1};
358     };
360     static_assert(test::zero == 0, "");
361     static_assert(test::one == 1, "");
363   }
365   namespace test_lambdas
366   {
368     void
369     test1()
370     {
371       auto lambda1 = [](){};
372       auto lambda2 = lambda1;
373       lambda1();
374       lambda2();
375     }
377     int
378     test2()
379     {
380       auto a = [](int i, int j){ return i + j; }(1, 2);
381       auto b = []() -> int { return '0'; }();
382       auto c = [=](){ return a + b; }();
383       auto d = [&](){ return c; }();
384       auto e = [a, &b](int x) mutable {
385         const auto identity = [](int y){ return y; };
386         for (auto i = 0; i < a; ++i)
387           a += b--;
388         return x + identity(a + b);
389       }(0);
390       return a + b + c + d + e;
391     }
393     int
394     test3()
395     {
396       const auto nullary = [](){ return 0; };
397       const auto unary = [](int x){ return x; };
398       using nullary_t = decltype(nullary);
399       using unary_t = decltype(unary);
400       const auto higher1st = [](nullary_t f){ return f(); };
401       const auto higher2nd = [unary](nullary_t f1){
402         return [unary, f1](unary_t f2){ return f2(unary(f1())); };
403       };
404       return higher1st(nullary) + higher2nd(nullary)(unary);
405     }
407   }
409   namespace test_variadic_templates
410   {
412     template <int...>
413     struct sum;
415     template <int N0, int... N1toN>
416     struct sum<N0, N1toN...>
417     {
418       static constexpr auto value = N0 + sum<N1toN...>::value;
419     };
421     template <>
422     struct sum<>
423     {
424       static constexpr auto value = 0;
425     };
427     static_assert(sum<>::value == 0, "");
428     static_assert(sum<1>::value == 1, "");
429     static_assert(sum<23>::value == 23, "");
430     static_assert(sum<1, 2>::value == 3, "");
431     static_assert(sum<5, 5, 11>::value == 21, "");
432     static_assert(sum<2, 3, 5, 7, 11, 13>::value == 41, "");
434   }
436   // http://stackoverflow.com/questions/13728184/template-aliases-and-sfinae
437   // Clang 3.1 fails with headers of libstd++ 4.8.3 when using std::function
438   // because of this.
439   namespace test_template_alias_sfinae
440   {
442     struct foo {};
444     template<typename T>
445     using member = typename T::member_type;
447     template<typename T>
448     void func(...) {}
450     template<typename T>
451     void func(member<T>*) {}
453     void test();
455     void test() { func<foo>(0); }
457   }
459 }  // namespace cxx11
461 #endif  // __cplusplus >= 201103L
466 dnl  Tests for new features in C++14
468 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_14], [[
470 // If the compiler admits that it is not ready for C++14, why torture it?
471 // Hopefully, this will speed up the test.
473 #ifndef __cplusplus
475 #error "This is not a C++ compiler"
477 #elif __cplusplus < 201402L
479 #error "This is not a C++14 compiler"
481 #else
483 namespace cxx14
486   namespace test_polymorphic_lambdas
487   {
489     int
490     test()
491     {
492       const auto lambda = [](auto&&... args){
493         const auto istiny = [](auto x){
494           return (sizeof(x) == 1UL) ? 1 : 0;
495         };
496         const int aretiny[] = { istiny(args)... };
497         return aretiny[0];
498       };
499       return lambda(1, 1L, 1.0f, '1');
500     }
502   }
504   namespace test_binary_literals
505   {
507     constexpr auto ivii = 0b0000000000101010;
508     static_assert(ivii == 42, "wrong value");
510   }
512   namespace test_generalized_constexpr
513   {
515     template < typename CharT >
516     constexpr unsigned long
517     strlen_c(const CharT *const s) noexcept
518     {
519       auto length = 0UL;
520       for (auto p = s; *p; ++p)
521         ++length;
522       return length;
523     }
525     static_assert(strlen_c("") == 0UL, "");
526     static_assert(strlen_c("x") == 1UL, "");
527     static_assert(strlen_c("test") == 4UL, "");
528     static_assert(strlen_c("another\0test") == 7UL, "");
530   }
532   namespace test_lambda_init_capture
533   {
535     int
536     test()
537     {
538       auto x = 0;
539       const auto lambda1 = [a = x](int b){ return a + b; };
540       const auto lambda2 = [a = lambda1(x)](){ return a; };
541       return lambda2();
542     }
544   }
546   namespace test_digit_separators
547   {
549     constexpr auto ten_million = 100'000'000;
550     static_assert(ten_million == 100000000, "");
552   }
554   namespace test_return_type_deduction
555   {
557     auto f(int& x) { return x; }
558     decltype(auto) g(int& x) { return x; }
560     template < typename T1, typename T2 >
561     struct is_same
562     {
563       static constexpr auto value = false;
564     };
566     template < typename T >
567     struct is_same<T, T>
568     {
569       static constexpr auto value = true;
570     };
572     int
573     test()
574     {
575       auto x = 0;
576       static_assert(is_same<int, decltype(f(x))>::value, "");
577       static_assert(is_same<int&, decltype(g(x))>::value, "");
578       return x;
579     }
581   }
583 }  // namespace cxx14
585 #endif  // __cplusplus >= 201402L
590 dnl  Tests for new features in C++17
592 m4_define([_AX_CXX_COMPILE_STDCXX_testbody_new_in_17], [[
594 // If the compiler admits that it is not ready for C++17, why torture it?
595 // Hopefully, this will speed up the test.
597 #ifndef __cplusplus
599 #error "This is not a C++ compiler"
601 #elif __cplusplus <= 201402L
603 #error "This is not a C++17 compiler"
605 #else
607 #if defined(__clang__)
608   #define REALLY_CLANG
609 #else
610   #if defined(__GNUC__)
611     #define REALLY_GCC
612   #endif
613 #endif
615 #include <initializer_list>
616 #include <utility>
617 #include <type_traits>
619 namespace cxx17
622 #if !defined(REALLY_CLANG)
623   namespace test_constexpr_lambdas
624   {
626     // TODO: test it with clang++ from git
628     constexpr int foo = [](){return 42;}();
630   }
631 #endif // !defined(REALLY_CLANG)
633   namespace test::nested_namespace::definitions
634   {
636   }
638   namespace test_fold_expression
639   {
641     template<typename... Args>
642     int multiply(Args... args)
643     {
644       return (args * ... * 1);
645     }
647     template<typename... Args>
648     bool all(Args... args)
649     {
650       return (args && ...);
651     }
653   }
655   namespace test_extended_static_assert
656   {
658     static_assert (true);
660   }
662   namespace test_auto_brace_init_list
663   {
665     auto foo = {5};
666     auto bar {5};
668     static_assert(std::is_same<std::initializer_list<int>, decltype(foo)>::value);
669     static_assert(std::is_same<int, decltype(bar)>::value);
670   }
672   namespace test_typename_in_template_template_parameter
673   {
675     template<template<typename> typename X> struct D;
677   }
679   namespace test_fallthrough_nodiscard_maybe_unused_attributes
680   {
682     int f1()
683     {
684       return 42;
685     }
687     [[nodiscard]] int f2()
688     {
689       [[maybe_unused]] auto unused = f1();
691       switch (f1())
692       {
693       case 17:
694         f1();
695         [[fallthrough]];
696       case 42:
697         f1();
698       }
699       return f1();
700     }
702   }
704   namespace test_extended_aggregate_initialization
705   {
707     struct base1
708     {
709       int b1, b2 = 42;
710     };
712     struct base2
713     {
714       base2() {
715         b3 = 42;
716       }
717       int b3;
718     };
720     struct derived : base1, base2
721     {
722         int d;
723     };
725     derived d1 {{1, 2}, {}, 4};  // full initialization
726     derived d2 {{}, {}, 4};      // value-initialized bases
728   }
730   namespace test_general_range_based_for_loop
731   {
733     struct iter
734     {
735       int i;
737       int& operator* ()
738       {
739         return i;
740       }
742       const int& operator* () const
743       {
744         return i;
745       }
747       iter& operator++()
748       {
749         ++i;
750         return *this;
751       }
752     };
754     struct sentinel
755     {
756       int i;
757     };
759     bool operator== (const iter& i, const sentinel& s)
760     {
761       return i.i == s.i;
762     }
764     bool operator!= (const iter& i, const sentinel& s)
765     {
766       return !(i == s);
767     }
769     struct range
770     {
771       iter begin() const
772       {
773         return {0};
774       }
776       sentinel end() const
777       {
778         return {5};
779       }
780     };
782     void f()
783     {
784       range r {};
786       for (auto i : r)
787       {
788         [[maybe_unused]] auto v = i;
789       }
790     }
792   }
794   namespace test_lambda_capture_asterisk_this_by_value
795   {
797     struct t
798     {
799       int i;
800       int foo()
801       {
802         return [*this]()
803         {
804           return i;
805         }();
806       }
807     };
809   }
811   namespace test_enum_class_construction
812   {
814     enum class byte : unsigned char
815     {};
817     byte foo {42};
819   }
821   namespace test_constexpr_if
822   {
824     template <bool cond>
825     int f ()
826     {
827       if constexpr(cond)
828       {
829         return 13;
830       }
831       else
832       {
833         return 42;
834       }
835     }
837   }
839   namespace test_selection_statement_with_initializer
840   {
842     int f()
843     {
844       return 13;
845     }
847     int f2()
848     {
849       if (auto i = f(); i > 0)
850       {
851         return 3;
852       }
854       switch (auto i = f(); i + 4)
855       {
856       case 17:
857         return 2;
859       default:
860         return 1;
861       }
862     }
864   }
866 #if !defined(REALLY_CLANG)
867   namespace test_template_argument_deduction_for_class_templates
868   {
870     // TODO: test it with clang++ from git
872     template <typename T1, typename T2>
873     struct pair
874     {
875       pair (T1 p1, T2 p2)
876         : m1 {p1},
877           m2 {p2}
878       {}
880       T1 m1;
881       T2 m2;
882     };
884     void f()
885     {
886       [[maybe_unused]] auto p = pair{13, 42u};
887     }
889   }
890 #endif // !defined(REALLY_CLANG)
892   namespace test_non_type_auto_template_parameters
893   {
895     template <auto n>
896     struct B
897     {};
899     B<5> b1;
900     B<'a'> b2;
902   }
904 #if !defined(REALLY_CLANG)
905   namespace test_structured_bindings
906   {
908     // TODO: test it with clang++ from git
910     int arr[2] = { 1, 2 };
911     std::pair<int, int> pr = { 1, 2 };
913     auto f1() -> int(&)[2]
914     {
915       return arr;
916     }
918     auto f2() -> std::pair<int, int>&
919     {
920       return pr;
921     }
923     struct S
924     {
925       int x1 : 2;
926       volatile double y1;
927     };
929     S f3()
930     {
931       return {};
932     }
934     auto [ x1, y1 ] = f1();
935     auto& [ xr1, yr1 ] = f1();
936     auto [ x2, y2 ] = f2();
937     auto& [ xr2, yr2 ] = f2();
938     const auto [ x3, y3 ] = f3();
940   }
941 #endif // !defined(REALLY_CLANG)
943 #if !defined(REALLY_CLANG)
944   namespace test_exception_spec_type_system
945   {
947     // TODO: test it with clang++ from git
949     struct Good {};
950     struct Bad {};
952     void g1() noexcept;
953     void g2();
955     template<typename T>
956     Bad
957     f(T*, T*);
959     template<typename T1, typename T2>
960     Good
961     f(T1*, T2*);
963     static_assert (std::is_same_v<Good, decltype(f(g1, g2))>);
965   }
966 #endif // !defined(REALLY_CLANG)
968   namespace test_inline_variables
969   {
971     template<class T> void f(T)
972     {}
974     template<class T> inline T g(T)
975     {
976       return T{};
977     }
979     template<> inline void f<>(int)
980     {}
982     template<> int g<>(int)
983     {
984       return 5;
985     }
987   }
989 }  // namespace cxx17
991 #endif  // __cplusplus <= 201402L