Skip to content

Commit 85e5cbd

Browse files
committed
gh-121647: Define _Py_TYPEOF macro on more compilers
Extend the _Py_TYPEOF macro to more implementations of __typeof__ and standardized equivalents on C23 and C++11. On MSVC, __typeof__ is enabled through _MSC_VER check; for others, an autoconf probe is implemented.
1 parent 65feded commit 85e5cbd

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

Include/pyport.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,14 @@ extern "C" {
554554
//
555555
// Example: _Py_TYPEOF(x) x_copy = (x);
556556
//
557-
// The macro is only defined if GCC or clang compiler is used.
558-
#if defined(__GNUC__) || defined(__clang__)
557+
// Defined if __typeof__ or its equivalents are available.
558+
#if defined(__GNUC__) || defined(__clang__) || defined(HAVE_TYPEOF) || \
559+
(defined(_MSC_VER) && _MSC_VER >= 1939)
559560
# define _Py_TYPEOF(expr) __typeof__(expr)
561+
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
562+
# define _Py_TYPEOF(expr) typeof(expr)
563+
#elif defined(__cplusplus) && __cplusplus >= 201103
564+
# define _Py_TYPEOF(expr) decltype(expr)
560565
#endif
561566

562567

configure

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6983,6 +6983,19 @@ case "$ac_cv_computed_gotos" in yes*)
69836983
[Define if the C compiler supports computed gotos.])
69846984
esac
69856985

6986+
AC_CACHE_CHECK([whether $CC supports __typeof__], [ac_cv_typeof],
6987+
AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
6988+
int i;
6989+
__typeof__(i) *p = &i;
6990+
]])],
6991+
[ac_cv_typeof=yes],
6992+
[ac_cv_typeof=no]))
6993+
if test "$ac_cv_typeof" = yes
6994+
then
6995+
AC_DEFINE([HAVE_TYPEOF], [1],
6996+
[Define if the C compiler supports __typeof__(expr)])
6997+
fi
6998+
69866999
case $ac_sys_system in
69877000
AIX*)
69887001
AC_DEFINE([HAVE_BROKEN_PIPE_BUF], [1],

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,9 @@
14981498
/* Define to 1 if you have the `ttyname' function. */
14991499
#undef HAVE_TTYNAME
15001500

1501+
/* Define if the C compiler supports __typeof__(expr) */
1502+
#undef HAVE_TYPEOF
1503+
15011504
/* Define to 1 if you don't have `tm_zone' but do have the external array
15021505
`tzname'. */
15031506
#undef HAVE_TZNAME

0 commit comments

Comments
 (0)