https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119222

--- Comment #13 from Gwen Fu <gwen3293940943 at gmail dot com> ---
    When executing the conversion_warning function, if you do not add any of
the three compilation options -Warn-conversion or -Warn_sign_conversion or
-Warn-float-conversion, the function will return directly. 
   This function is the key function for issuing warnings about type conversion
during compilation. 
   Therefore, if you need to issue a warning during type conversion, you must
add one of the three options. 
   On this basis, I have made some modifications to the conversion_warning
function. Only in: 
switch (TREE_CODE (expr)) 
{ case EQ_EXPR: 
   case NE_EXPR: 
   case LE_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))) 
       warning_at(loc, OPT_Wfloat_conversion,
 "conversion from %qT to %qT changes infinity to maximum or minimum integer
value", expr_type , type) ;
       arith_ops = 2;
       goto default_;
Make the above modifications to case RDIV_EXPR
Compile after modification:
gwne@gwne-KLVG-XX:~/github_repo/gcc/test$ ../objdir/gcc/cc1plus -Wall -Wextra
-Wfloat-conversion 119222.cc
 int main()119222.cc:4:14: warning: conversion from ‘double’ to ‘int’ changes
infinity to maximum or minimum integer value [-Wfloat-conversion]
 4 | int a = 1/static_cast<double>(0);
 | ~^~~~~~~~~~~~~~~~~~~~~~~
119222.cc:4:9: warning: unused variable ‘a’ [-Wunused-variable]
 4 | int a = 1/static_cast<double>(0);
 | ^

Reply via email to