Patch about Bug 119222 - Conversion of inf to integer is not diagnosed [Classifier] a single patch [Tags] conversion warning ;c++; inf to integer [Brief Summary] Add a judgement in the RDIV_EXPR case [Remark] This patch does not support -Wall -Wextra to capture inf to integer warnings . The -Wall -Wextra options do not check this conversion (and do not allow the conversion_warning function to be executed completely), instead you need to add -Wfloat-conversion or -Wconversion》 [Pontential problem] Is there a case where the conversion result type is not limited to INTEGER_TYPE?
From 6924ad5d1a3a96ed46b728a47462a7a054613326 Mon Sep 17 00:00:00 2001 From: Gwen Fu <gwen3293940...@gmail.com> Date: Tue, 18 Mar 2025 11:37:28 +0800 Subject: [PATCH] fix bug119222 Conversion of inf to integer is not diagnosed
--- gcc/c-family/c-common.cc | 5 +++-- gcc/c-family/c-warn.cc | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index 587d76461e9..dfd5c3d673c 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -1652,11 +1652,12 @@ unsafe_conversion_p (tree type, tree expr, tree result, bool check_sign) else { /* Warn for real types converted to integer types. */ - if (SCALAR_FLOAT_TYPE_P (expr_type) + if ((SCALAR_FLOAT_TYPE_P (expr_type) + ||TREE_CODE(expr_type) ==REAL_TYPE) && (TREE_CODE (type) == INTEGER_TYPE || TREE_CODE (type) == BITINT_TYPE)) give_warning = UNSAFE_REAL; - + else if ((TREE_CODE (expr_type) == INTEGER_TYPE || TREE_CODE (expr_type) == BITINT_TYPE) && (TREE_CODE (type) == INTEGER_TYPE diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index d547b08f55d..406b591bfa0 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -1283,6 +1283,17 @@ conversion_warning (location_t loc, tree type, tree expr, tree result) case CEIL_DIV_EXPR: case EXACT_DIV_EXPR: case RDIV_EXPR: + /*Issue a warning about infinity conversion to int*/ + if( TREE_CODE(type) == INTEGER_TYPE + && TREE_CODE (TREE_OPERAND(expr,1)) == REAL_CST && real_zerop (TREE_OPERAND(expr,1))) + { + bool ret = warning_at(loc , OPT_Wfloat_conversion , + "conversion from %qT to %qT changes infinity to maximum or minimum integer value", + expr_type , type) ; + if(!ret) warning_at(loc , OPT_Wconversion , + "conversion from %qT to %qT changes infinity to maximum or minimum integer value", + expr_type , type) ; + } arith_ops = 2; goto default_; -- 2.45.2
int fooo() { return 1/static_cast<double>(0) ; } int main() { int a = 1/static_cast<double>(0) ; int b = static_cast<float>(1) / 0 ; return 0 ; }