View | Details | Return to bug 53001 | Differences between
and this patch

Collapse All | Expand All

(-)gcc/c-family/c-common.c (-21 / +40 lines)
Lines 2517-2526 Link Here
2517
   Function allows conversions between types of different signedness and
2517
   Function allows conversions between types of different signedness and
2518
   does not return true in that case.  Function can produce signedness
2518
   does not return true in that case.  Function can produce signedness
2519
   warnings if PRODUCE_WARNS is true.  */
2519
   warnings if PRODUCE_WARNS is true.  */
2520
bool
2520
enum conversion_safety
2521
unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2521
unsafe_conversion_p (tree type, tree expr, bool produce_warns)
2522
{
2522
{
2523
  bool give_warning = false;
2523
  enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
2524
  tree expr_type = TREE_TYPE (expr);
2524
  tree expr_type = TREE_TYPE (expr);
2525
  location_t loc = EXPR_LOC_OR_HERE (expr);
2525
  location_t loc = EXPR_LOC_OR_HERE (expr);
2526
2526
Lines 2532-2538 Link Here
2532
	  && TREE_CODE (type) == INTEGER_TYPE)
2532
	  && TREE_CODE (type) == INTEGER_TYPE)
2533
	{
2533
	{
2534
	  if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2534
	  if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
2535
	    give_warning = true;
2535
	    give_warning = UNSAFE_REAL;
2536
	}
2536
	}
2537
      /* Warn for an integer constant that does not fit into integer type.  */
2537
      /* Warn for an integer constant that does not fit into integer type.  */
2538
      else if (TREE_CODE (expr_type) == INTEGER_TYPE
2538
      else if (TREE_CODE (expr_type) == INTEGER_TYPE
Lines 2553-2559 Link Here
2553
			    " constant value to negative integer");
2553
			    " constant value to negative integer");
2554
	    }
2554
	    }
2555
	  else
2555
	  else
2556
	    give_warning = true;
2556
	    give_warning = UNSAFE_OTHER;
2557
	}
2557
	}
2558
      else if (TREE_CODE (type) == REAL_TYPE)
2558
      else if (TREE_CODE (type) == REAL_TYPE)
2559
	{
2559
	{
Lines 2562-2568 Link Here
2562
	    {
2562
	    {
2563
	      REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2563
	      REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2564
	      if (!exact_real_truncate (TYPE_MODE (type), &a))
2564
	      if (!exact_real_truncate (TYPE_MODE (type), &a))
2565
		give_warning = true;
2565
		give_warning = UNSAFE_REAL;
2566
	    }
2566
	    }
2567
	  /* Warn for a real constant that does not fit into a smaller
2567
	  /* Warn for a real constant that does not fit into a smaller
2568
	     real type.  */
2568
	     real type.  */
Lines 2571-2577 Link Here
2571
	    {
2571
	    {
2572
	      REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2572
	      REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2573
	      if (!exact_real_truncate (TYPE_MODE (type), &a))
2573
	      if (!exact_real_truncate (TYPE_MODE (type), &a))
2574
		give_warning = true;
2574
		give_warning = UNSAFE_REAL;
2575
	    }
2575
	    }
2576
	}
2576
	}
2577
    }
2577
    }
Lines 2580-2586 Link Here
2580
      /* Warn for real types converted to integer types.  */
2580
      /* Warn for real types converted to integer types.  */
2581
      if (TREE_CODE (expr_type) == REAL_TYPE
2581
      if (TREE_CODE (expr_type) == REAL_TYPE
2582
	  && TREE_CODE (type) == INTEGER_TYPE)
2582
	  && TREE_CODE (type) == INTEGER_TYPE)
2583
	give_warning = true;
2583
	give_warning = UNSAFE_REAL;
2584
2584
2585
      else if (TREE_CODE (expr_type) == INTEGER_TYPE
2585
      else if (TREE_CODE (expr_type) == INTEGER_TYPE
2586
	       && TREE_CODE (type) == INTEGER_TYPE)
2586
	       && TREE_CODE (type) == INTEGER_TYPE)
Lines 2618-2624 Link Here
2618
			  && int_fits_type_p (op1, c_common_signed_type (type))
2618
			  && int_fits_type_p (op1, c_common_signed_type (type))
2619
			  && int_fits_type_p (op1,
2619
			  && int_fits_type_p (op1,
2620
					      c_common_unsigned_type (type))))
2620
					      c_common_unsigned_type (type))))
2621
		    return false;
2621
		    return SAFE_CONVERSION;
2622
		  /* If constant is unsigned and fits in the target
2622
		  /* If constant is unsigned and fits in the target
2623
		     type, then the result will also fit.  */
2623
		     type, then the result will also fit.  */
2624
		  else if ((TREE_CODE (op0) == INTEGER_CST
2624
		  else if ((TREE_CODE (op0) == INTEGER_CST
Lines 2627-2638 Link Here
2627
			   || (TREE_CODE (op1) == INTEGER_CST
2627
			   || (TREE_CODE (op1) == INTEGER_CST
2628
			       && unsigned1
2628
			       && unsigned1
2629
			       && int_fits_type_p (op1, type)))
2629
			       && int_fits_type_p (op1, type)))
2630
		    return false;
2630
		    return SAFE_CONVERSION;
2631
		}
2631
		}
2632
	    }
2632
	    }
2633
	  /* Warn for integer types converted to smaller integer types.  */
2633
	  /* Warn for integer types converted to smaller integer types.  */
2634
	  if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2634
	  if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2635
	    give_warning = true;
2635
	    give_warning = UNSAFE_OTHER;
2636
2636
2637
	  /* When they are the same width but different signedness,
2637
	  /* When they are the same width but different signedness,
2638
	     then the value may change.  */
2638
	     then the value may change.  */
Lines 2668-2681 Link Here
2668
2668
2669
	  if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2669
	  if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound)
2670
	      || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2670
	      || !exact_real_truncate (TYPE_MODE (type), &real_high_bound))
2671
	    give_warning = true;
2671
	    give_warning = UNSAFE_OTHER;
2672
	}
2672
	}
2673
2673
2674
      /* Warn for real types converted to smaller real types.  */
2674
      /* Warn for real types converted to smaller real types.  */
2675
      else if (TREE_CODE (expr_type) == REAL_TYPE
2675
      else if (TREE_CODE (expr_type) == REAL_TYPE
2676
	       && TREE_CODE (type) == REAL_TYPE
2676
	       && TREE_CODE (type) == REAL_TYPE
2677
	       && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2677
	       && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2678
	give_warning = true;
2678
	give_warning = UNSAFE_REAL;
2679
    }
2679
    }
2680
2680
2681
  return give_warning;
2681
  return give_warning;
Lines 2689-2696 Link Here
2689
{
2689
{
2690
  tree expr_type = TREE_TYPE (expr);
2690
  tree expr_type = TREE_TYPE (expr);
2691
  location_t loc = EXPR_LOC_OR_HERE (expr);
2691
  location_t loc = EXPR_LOC_OR_HERE (expr);
2692
  enum conversion_safety conversion_kind;
2692
2693
2693
  if (!warn_conversion && !warn_sign_conversion)
2694
  if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
2694
    return;
2695
    return;
2695
2696
2696
  switch (TREE_CODE (expr))
2697
  switch (TREE_CODE (expr))
Lines 2717-2726 Link Here
2717
2718
2718
    case REAL_CST:
2719
    case REAL_CST:
2719
    case INTEGER_CST:
2720
    case INTEGER_CST:
2720
      if (unsafe_conversion_p (type, expr, true))
2721
      conversion_kind = unsafe_conversion_p (type, expr, true);
2721
	warning_at (loc, OPT_Wconversion,
2722
      if(conversion_kind == UNSAFE_REAL) 
2722
		    "conversion to %qT alters %qT constant value",
2723
	{
2723
		    type, expr_type);
2724
	  warning_at (loc, OPT_Wfloat_conversion,
2725
		      "conversion to %qT alters %qT constant value",
2726
		      type, expr_type);
2727
	} 
2728
      else if(conversion_kind)
2729
	{
2730
	  warning_at (loc, OPT_Wconversion,
2731
		      "conversion to %qT alters %qT constant value",
2732
		      type, expr_type);
2733
	}
2724
      return;
2734
      return;
2725
2735
2726
    case COND_EXPR:
2736
    case COND_EXPR:
Lines 2736-2745 Link Here
2736
      }
2746
      }
2737
2747
2738
    default: /* 'expr' is not a constant.  */
2748
    default: /* 'expr' is not a constant.  */
2739
      if (unsafe_conversion_p (type, expr, true))
2749
      conversion_kind = unsafe_conversion_p (type, expr, true);
2740
	warning_at (loc, OPT_Wconversion,
2750
      if(conversion_kind == UNSAFE_REAL)
2741
		    "conversion to %qT from %qT may alter its value",
2751
	{
2742
		    type, expr_type);
2752
	  warning_at (loc, OPT_Wfloat_conversion,
2753
		      "conversion to %qT from %qT may alter its value",
2754
		      type, expr_type);
2755
	} 
2756
      else if(conversion_kind)
2757
	{
2758
	  warning_at (loc, OPT_Wconversion,
2759
		      "conversion to %qT from %qT may alter its value",
2760
		      type, expr_type);
2761
	}
2743
    }
2762
    }
2744
}
2763
}
2745
2764
(-)gcc/c-family/c-common.h (-1 / +11 lines)
Lines 685-690 Link Here
685
  unsigned inlines_hidden : 1;	/* True when -finlineshidden in effect.  */
685
  unsigned inlines_hidden : 1;	/* True when -finlineshidden in effect.  */
686
};
686
};
687
687
688
/* These variables are possible types of unsafe conversions. 
689
   SAFE_CONVERSION The conversion is safe
690
   UNSAFE_OTHER Another type of conversion with problems
691
   UNSAFE_SIGN Conversion between signed and unsigned integers 
692
    which are all warned about immediately, so this is unused
693
   UNSAFE_REAL Conversions that reduce the precision of reals 
694
    including conversions from reals to integers
695
 */
696
enum conversion_safety { SAFE_CONVERSION = 0, UNSAFE_OTHER, UNSAFE_SIGN, UNSAFE_REAL };
697
688
/* Global visibility options.  */
698
/* Global visibility options.  */
689
extern struct visibility_flags visibility_options;
699
extern struct visibility_flags visibility_options;
690
700
Lines 738-744 Link Here
738
extern tree c_common_signed_or_unsigned_type (int, tree);
748
extern tree c_common_signed_or_unsigned_type (int, tree);
739
extern void c_common_init_ts (void);
749
extern void c_common_init_ts (void);
740
extern tree c_build_bitfield_integer_type (unsigned HOST_WIDE_INT, int);
750
extern tree c_build_bitfield_integer_type (unsigned HOST_WIDE_INT, int);
741
extern bool unsafe_conversion_p (tree, tree, bool);
751
extern enum conversion_safety unsafe_conversion_p (tree, tree, bool);
742
extern bool decl_with_nonnull_addr_p (const_tree);
752
extern bool decl_with_nonnull_addr_p (const_tree);
743
extern tree c_fully_fold (tree, bool, bool *);
753
extern tree c_fully_fold (tree, bool, bool *);
744
extern tree decl_constant_value_for_optimization (tree);
754
extern tree decl_constant_value_for_optimization (tree);
(-)gcc/c-family/c-cppbuiltin.c (-5 / +5 lines)
Lines 157-163 Link Here
157
	p log10 b			if b is a power of 10
157
	p log10 b			if b is a power of 10
158
	floor((p - 1) log10 b)		otherwise
158
	floor((p - 1) log10 b)		otherwise
159
  */
159
  */
160
  dig = (fmt->p - 1) * log10_b;
160
  dig = (int)((fmt->p - 1) * log10_b);
161
  sprintf (name, "__%s_DIG__", name_prefix);
161
  sprintf (name, "__%s_DIG__", name_prefix);
162
  builtin_define_with_int_value (name, dig);
162
  builtin_define_with_int_value (name, dig);
163
163
Lines 173-179 Link Here
173
173
174
     Recall that emin is negative, so the integer truncation calculates
174
     Recall that emin is negative, so the integer truncation calculates
175
     the ceiling, not the floor, in this case.  */
175
     the ceiling, not the floor, in this case.  */
176
  min_10_exp = (fmt->emin - 1) * log10_b;
176
  min_10_exp = (int)((fmt->emin - 1) * log10_b);
177
  sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
177
  sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
178
  sprintf (buf, "(%d)", min_10_exp);
178
  sprintf (buf, "(%d)", min_10_exp);
179
  builtin_define_with_value (name, buf, 0);
179
  builtin_define_with_value (name, buf, 0);
Lines 208-214 Link Here
208
     Hand-waving aside, crunching all of the sets of constants above by hand
208
     Hand-waving aside, crunching all of the sets of constants above by hand
209
     does not yield a case for which the first term is significant, which
209
     does not yield a case for which the first term is significant, which
210
     in the end is all that matters.  */
210
     in the end is all that matters.  */
211
  max_10_exp = fmt->emax * log10_b;
211
  max_10_exp = (int)(fmt->emax * log10_b);
212
  sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
212
  sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
213
  builtin_define_with_int_value (name, max_10_exp);
213
  builtin_define_with_int_value (name, max_10_exp);
214
214
Lines 224-237 Link Here
224
  {
224
  {
225
    double d_decimal_dig
225
    double d_decimal_dig
226
      = 1 + (fmt->p < ldfmt->p ? ldfmt->p : fmt->p) * log10_b;
226
      = 1 + (fmt->p < ldfmt->p ? ldfmt->p : fmt->p) * log10_b;
227
    decimal_dig = d_decimal_dig;
227
    decimal_dig = (int)d_decimal_dig;
228
    if (decimal_dig < d_decimal_dig)
228
    if (decimal_dig < d_decimal_dig)
229
      decimal_dig++;
229
      decimal_dig++;
230
  }
230
  }
231
  /* Similar, for this type rather than long double.  */
231
  /* Similar, for this type rather than long double.  */
232
  {
232
  {
233
    double type_d_decimal_dig = 1 + fmt->p * log10_b;
233
    double type_d_decimal_dig = 1 + fmt->p * log10_b;
234
    type_decimal_dig = type_d_decimal_dig;
234
    type_decimal_dig = (int)type_d_decimal_dig;
235
    if (type_decimal_dig < type_d_decimal_dig)
235
    if (type_decimal_dig < type_d_decimal_dig)
236
      type_decimal_dig++;
236
      type_decimal_dig++;
237
  }
237
  }
(-)gcc/c-family/c.opt (+4 lines)
Lines 387-392 Link Here
387
C ObjC RejectNegative Warning Alias(Werror=, implicit-function-declaration)
387
C ObjC RejectNegative Warning Alias(Werror=, implicit-function-declaration)
388
This switch is deprecated; use -Werror=implicit-function-declaration instead
388
This switch is deprecated; use -Werror=implicit-function-declaration instead
389
389
390
Wfloat-conversion
391
C ObjC C++ ObjC++ Var(warn_float_conversion) LangEnabledBy(C ObjC C++ ObjC++,Wconversion) EnabledBy(Wextra)
392
Warn for implicit type conversions that cause loss of floating point precision
393
390
Wfloat-equal
394
Wfloat-equal
391
C ObjC C++ ObjC++ Var(warn_float_equal) Warning
395
C ObjC C++ ObjC++ Var(warn_float_equal) Warning
392
Warn if testing floating point numbers for equality
396
Warn if testing floating point numbers for equality
(-)gcc/doc/invoke.texi (-1 / +11 lines)
Lines 263-269 Link Here
263
-Wpointer-arith  -Wno-pointer-to-int-cast @gol
263
-Wpointer-arith  -Wno-pointer-to-int-cast @gol
264
-Wredundant-decls  -Wno-return-local-addr @gol
264
-Wredundant-decls  -Wno-return-local-addr @gol
265
-Wreturn-type  -Wsequence-point  -Wshadow @gol
265
-Wreturn-type  -Wsequence-point  -Wshadow @gol
266
-Wsign-compare  -Wsign-conversion  -Wsizeof-pointer-memaccess @gol
266
-Wsign-compare  -Wsign-conversion -Wfloat-conversion @gol 
267
-Wsizeof-pointer-memaccess @gol
267
-Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
268
-Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
268
-Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol
269
-Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol
269
-Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol
270
-Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol
Lines 3334-3339 Link Here
3334
-Wuninitialized  @gol
3335
-Wuninitialized  @gol
3335
-Wunused-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)} @gol
3336
-Wunused-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)} @gol
3336
-Wunused-but-set-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)}  @gol
3337
-Wunused-but-set-parameter @r{(only with} @option{-Wunused} @r{or} @option{-Wall}@r{)}  @gol
3338
-Wfloat-conversion @gol
3337
}
3339
}
3338
3340
3339
The option @option{-Wextra} also prints warning messages for the
3341
The option @option{-Wextra} also prints warning messages for the
Lines 4553-4558 Link Here
4553
integer variable. An explicit cast silences the warning. In C, this
4555
integer variable. An explicit cast silences the warning. In C, this
4554
option is enabled also by @option{-Wconversion}.
4556
option is enabled also by @option{-Wconversion}.
4555
4557
4558
@item -Wfloat-conversion
4559
@opindex Wfloat-conversion
4560
@opindex Wno-float-conversion
4561
Warn for implicit conversions that reduce the precision of a real value. 
4562
This includes conversions from real to integer, and from higher precision
4563
real to lower precision real values.  This option is also enabled by 
4564
@option{-Wconversion} or @option{-Wextra}.
4565
4556
@item -Wsizeof-pointer-memaccess
4566
@item -Wsizeof-pointer-memaccess
4557
@opindex Wsizeof-pointer-memaccess
4567
@opindex Wsizeof-pointer-memaccess
4558
@opindex Wno-sizeof-pointer-memaccess
4568
@opindex Wno-sizeof-pointer-memaccess
(-)gcc/mcf.c (-2 / +2 lines)
Lines 348-355 Link Here
348
348
349
  gcc_assert (x >= 0);
349
  gcc_assert (x >= 0);
350
350
351
  convertor.floatPart = x;
351
  convertor.floatPart = (float)x;
352
  convertor2.floatPart = x;
352
  convertor2.floatPart = (float)x;
353
  convertor.intPart = MAGIC_CONST1 + (convertor.intPart >> 1);
353
  convertor.intPart = MAGIC_CONST1 + (convertor.intPart >> 1);
354
  convertor2.intPart = MAGIC_CONST2 - (convertor2.intPart >> 1);
354
  convertor2.intPart = MAGIC_CONST2 - (convertor2.intPart >> 1);
355
355
(-)gcc/predict.c (-2 / +2 lines)
Lines 792-798 Link Here
792
	  /* If one probability is 0% and one 100%, avoid division by zero.  */
792
	  /* If one probability is 0% and one 100%, avoid division by zero.  */
793
	  combined_probability = REG_BR_PROB_BASE / 2;
793
	  combined_probability = REG_BR_PROB_BASE / 2;
794
	else
794
	else
795
	  combined_probability = (((double) combined_probability) * probability
795
	  combined_probability = (int)(((double) combined_probability) * probability
796
				  * REG_BR_PROB_BASE / d + 0.5);
796
				  * REG_BR_PROB_BASE / d + 0.5);
797
      }
797
      }
798
798
Lines 957-963 Link Here
957
	    /* If one probability is 0% and one 100%, avoid division by zero.  */
957
	    /* If one probability is 0% and one 100%, avoid division by zero.  */
958
	    combined_probability = REG_BR_PROB_BASE / 2;
958
	    combined_probability = REG_BR_PROB_BASE / 2;
959
	  else
959
	  else
960
	    combined_probability = (((double) combined_probability)
960
	    combined_probability = (int)(((double) combined_probability)
961
				    * probability
961
				    * probability
962
		    		    * REG_BR_PROB_BASE / d + 0.5);
962
		    		    * REG_BR_PROB_BASE / d + 0.5);
963
	}
963
	}
(-)gcc/real.c (-4 / +4 lines)
Lines 1550-1563 Link Here
1550
    }
1550
    }
1551
1551
1552
  /* Bound the number of digits printed by the size of the representation.  */
1552
  /* Bound the number of digits printed by the size of the representation.  */
1553
  max_digits = SIGNIFICAND_BITS * M_LOG10_2;
1553
  max_digits = (int)(SIGNIFICAND_BITS * M_LOG10_2);
1554
  if (digits == 0 || digits > max_digits)
1554
  if (digits == 0 || digits > max_digits)
1555
    digits = max_digits;
1555
    digits = max_digits;
1556
1556
1557
  /* Estimate the decimal exponent, and compute the length of the string it
1557
  /* Estimate the decimal exponent, and compute the length of the string it
1558
     will print as.  Be conservative and add one to account for possible
1558
     will print as.  Be conservative and add one to account for possible
1559
     overflow or rounding error.  */
1559
     overflow or rounding error.  */
1560
  dec_exp = REAL_EXP (&r) * M_LOG10_2;
1560
  dec_exp = (int)(REAL_EXP (&r) * M_LOG10_2);
1561
  for (max_digits = 1; dec_exp ; max_digits++)
1561
  for (max_digits = 1; dec_exp ; max_digits++)
1562
    dec_exp /= 10;
1562
    dec_exp /= 10;
1563
1563
Lines 2215-2221 Link Here
2215
  sign = r.sign;
2215
  sign = r.sign;
2216
  r.sign = 0;
2216
  r.sign = 0;
2217
2217
2218
  dec_exp = REAL_EXP (&r) * M_LOG10_2;
2218
  dec_exp = (int)(REAL_EXP (&r) * M_LOG10_2);
2219
  digits = dec_exp + 1;
2219
  digits = dec_exp + 1;
2220
  gcc_assert ((digits + 2) < (int)buf_size);
2220
  gcc_assert ((digits + 2) < (int)buf_size);
2221
2221
Lines 2818-2824 Link Here
2818
	 than the number of bits required to hold the largest coefficient
2818
	 than the number of bits required to hold the largest coefficient
2819
	 of this mode.  */
2819
	 of this mode.  */
2820
      double log2_10 = 3.3219281;
2820
      double log2_10 = 3.3219281;
2821
      return fmt->p * log2_10;
2821
      return (int)(fmt->p * log2_10);
2822
    }
2822
    }
2823
  return fmt->p;
2823
  return fmt->p;
2824
}
2824
}

Return to bug 53001