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 |
|