diff --git a/configure.in b/configure.in index f4651324fe..efb14e7611 100644 --- a/configure.in +++ b/configure.in @@ -2110,7 +2110,7 @@ main(int argc, char **argv) AC_DEFUN([RUBY_CHECK_BUILTIN_FUNC], [dnl AC_CACHE_CHECK([for $1], AS_TR_SH(rb_cv_builtin_$1), [AC_LINK_IFELSE( - [AC_LANG_PROGRAM([], [$2;])], + [AC_LANG_PROGRAM([int foo;], [$2;])], [AS_TR_SH(rb_cv_builtin_$1)=yes], [AS_TR_SH(rb_cv_builtin_$1)=no])]) if test "${AS_TR_SH(rb_cv_builtin_$1)}" != no; then @@ -2123,6 +2123,7 @@ RUBY_CHECK_BUILTIN_FUNC(__builtin_clz, [__builtin_clz(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_clzl, [__builtin_clzl(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_clzll, [__builtin_clzll(0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_choose_expr, [__builtin_choose_expr(0, 0, 0)]) +RUBY_CHECK_BUILTIN_FUNC(__builtin_choose_expr_constant_p, [__builtin_choose_expr(__builtin_constant_p(foo), 0, 0)]) RUBY_CHECK_BUILTIN_FUNC(__builtin_types_compatible_p, [__builtin_types_compatible_p(int, int)]) if test "$ac_cv_func_qsort_r" != no; then diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index dbea8e4c67..539c1b3f63 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -564,11 +564,24 @@ int ruby_safe_level_4_warning(void) __attribute__((warning("$SAFE=4 is obsolete" # ifdef RUBY_EXPORT # define ruby_safe_level_4_warning() ruby_safe_level_4_error() # endif -#define RUBY_SAFE_LEVEL_INVALID_P(level) \ +#if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR_CONSTANT_P) +# define RUBY_SAFE_LEVEL_INVALID_P(level) \ + __extension__(\ + __builtin_choose_expr(\ + __builtin_constant_p(level), \ + ((level) < 0 || RUBY_SAFE_LEVEL_MAX < (level)), 0)) +# define RUBY_SAFE_LEVEL_CHECK(level, type) \ + __extension__(__builtin_choose_expr(RUBY_SAFE_LEVEL_INVALID_P(level), ruby_safe_level_4_##type(), (level))) +#else +/* in gcc 4.8 or earlier, __builtin_choose_expr() does not consider + * __builtin_constant_p(variable) a constant expression. + */ +# define RUBY_SAFE_LEVEL_INVALID_P(level) \ __extension__(__builtin_constant_p(level) && \ ((level) < 0 || RUBY_SAFE_LEVEL_MAX < (level))) -#define RUBY_SAFE_LEVEL_CHECK(level, type) \ - __extension__(__builtin_choose_expr(RUBY_SAFE_LEVEL_INVALID_P(level), ruby_safe_level_4_##type(), (level))) +# define RUBY_SAFE_LEVEL_CHECK(level, type) \ + (RUBY_SAFE_LEVEL_INVALID_P(level) ? ruby_safe_level_4_##type() : (level)) +#endif #define rb_secure(level) rb_secure(RUBY_SAFE_LEVEL_CHECK(level, warning)) #define rb_set_safe_level(level) rb_set_safe_level(RUBY_SAFE_LEVEL_CHECK(level, error)) #endif