Lines 294-299
Link Here
|
294 |
{NULL, 0, 0}, |
294 |
{NULL, 0, 0}, |
295 |
}; |
295 |
}; |
296 |
|
296 |
|
|
|
297 |
enum conversion_safety {SAFE_CONVERSION=0,UNSAFE_OTHER,UNSAFE_SIGN,UNSAFE_REAL}; |
298 |
|
297 |
/* Global visibility options. */ |
299 |
/* Global visibility options. */ |
298 |
struct visibility_flags visibility_options; |
300 |
struct visibility_flags visibility_options; |
299 |
|
301 |
|
Lines 375-380
Link Here
|
375 |
static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); |
377 |
static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); |
376 |
static int resort_field_decl_cmp (const void *, const void *); |
378 |
static int resort_field_decl_cmp (const void *, const void *); |
377 |
|
379 |
|
|
|
380 |
static enum conversion_safety unsafe_conversion_p (tree, tree, bool); |
381 |
|
378 |
/* Reserved words. The third field is a mask: keywords are disabled |
382 |
/* Reserved words. The third field is a mask: keywords are disabled |
379 |
if they match the mask. |
383 |
if they match the mask. |
380 |
|
384 |
|
Lines 2481-2490
Link Here
|
2481 |
Function allows conversions between types of different signedness and |
2485 |
Function allows conversions between types of different signedness and |
2482 |
does not return true in that case. Function can produce signedness |
2486 |
does not return true in that case. Function can produce signedness |
2483 |
warnings if PRODUCE_WARNS is true. */ |
2487 |
warnings if PRODUCE_WARNS is true. */ |
2484 |
bool |
2488 |
static enum conversion_safety |
2485 |
unsafe_conversion_p (tree type, tree expr, bool produce_warns) |
2489 |
unsafe_conversion_p (tree type, tree expr, bool produce_warns) |
2486 |
{ |
2490 |
{ |
2487 |
bool give_warning = false; |
2491 |
enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ |
2488 |
tree expr_type = TREE_TYPE (expr); |
2492 |
tree expr_type = TREE_TYPE (expr); |
2489 |
location_t loc = EXPR_LOC_OR_HERE (expr); |
2493 |
location_t loc = EXPR_LOC_OR_HERE (expr); |
2490 |
|
2494 |
|
Lines 2496-2502
Link Here
|
2496 |
&& TREE_CODE (type) == INTEGER_TYPE) |
2500 |
&& TREE_CODE (type) == INTEGER_TYPE) |
2497 |
{ |
2501 |
{ |
2498 |
if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) |
2502 |
if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) |
2499 |
give_warning = true; |
2503 |
give_warning = UNSAFE_REAL; |
2500 |
} |
2504 |
} |
2501 |
/* Warn for an integer constant that does not fit into integer type. */ |
2505 |
/* Warn for an integer constant that does not fit into integer type. */ |
2502 |
else if (TREE_CODE (expr_type) == INTEGER_TYPE |
2506 |
else if (TREE_CODE (expr_type) == INTEGER_TYPE |
Lines 2517-2523
Link Here
|
2517 |
" constant value to negative integer"); |
2521 |
" constant value to negative integer"); |
2518 |
} |
2522 |
} |
2519 |
else |
2523 |
else |
2520 |
give_warning = true; |
2524 |
give_warning = UNSAFE_OTHER; |
2521 |
} |
2525 |
} |
2522 |
else if (TREE_CODE (type) == REAL_TYPE) |
2526 |
else if (TREE_CODE (type) == REAL_TYPE) |
2523 |
{ |
2527 |
{ |
Lines 2526-2532
Link Here
|
2526 |
{ |
2530 |
{ |
2527 |
REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); |
2531 |
REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); |
2528 |
if (!exact_real_truncate (TYPE_MODE (type), &a)) |
2532 |
if (!exact_real_truncate (TYPE_MODE (type), &a)) |
2529 |
give_warning = true; |
2533 |
give_warning = UNSAFE_REAL; |
2530 |
} |
2534 |
} |
2531 |
/* Warn for a real constant that does not fit into a smaller |
2535 |
/* Warn for a real constant that does not fit into a smaller |
2532 |
real type. */ |
2536 |
real type. */ |
Lines 2535-2541
Link Here
|
2535 |
{ |
2539 |
{ |
2536 |
REAL_VALUE_TYPE a = TREE_REAL_CST (expr); |
2540 |
REAL_VALUE_TYPE a = TREE_REAL_CST (expr); |
2537 |
if (!exact_real_truncate (TYPE_MODE (type), &a)) |
2541 |
if (!exact_real_truncate (TYPE_MODE (type), &a)) |
2538 |
give_warning = true; |
2542 |
give_warning = UNSAFE_REAL; |
2539 |
} |
2543 |
} |
2540 |
} |
2544 |
} |
2541 |
} |
2545 |
} |
Lines 2544-2550
Link Here
|
2544 |
/* Warn for real types converted to integer types. */ |
2548 |
/* Warn for real types converted to integer types. */ |
2545 |
if (TREE_CODE (expr_type) == REAL_TYPE |
2549 |
if (TREE_CODE (expr_type) == REAL_TYPE |
2546 |
&& TREE_CODE (type) == INTEGER_TYPE) |
2550 |
&& TREE_CODE (type) == INTEGER_TYPE) |
2547 |
give_warning = true; |
2551 |
give_warning = UNSAFE_REAL; |
2548 |
|
2552 |
|
2549 |
else if (TREE_CODE (expr_type) == INTEGER_TYPE |
2553 |
else if (TREE_CODE (expr_type) == INTEGER_TYPE |
2550 |
&& TREE_CODE (type) == INTEGER_TYPE) |
2554 |
&& TREE_CODE (type) == INTEGER_TYPE) |
Lines 2582-2588
Link Here
|
2582 |
&& int_fits_type_p (op1, c_common_signed_type (type)) |
2586 |
&& int_fits_type_p (op1, c_common_signed_type (type)) |
2583 |
&& int_fits_type_p (op1, |
2587 |
&& int_fits_type_p (op1, |
2584 |
c_common_unsigned_type (type)))) |
2588 |
c_common_unsigned_type (type)))) |
2585 |
return false; |
2589 |
return SAFE_CONVERSION; |
2586 |
/* If constant is unsigned and fits in the target |
2590 |
/* If constant is unsigned and fits in the target |
2587 |
type, then the result will also fit. */ |
2591 |
type, then the result will also fit. */ |
2588 |
else if ((TREE_CODE (op0) == INTEGER_CST |
2592 |
else if ((TREE_CODE (op0) == INTEGER_CST |
Lines 2591-2602
Link Here
|
2591 |
|| (TREE_CODE (op1) == INTEGER_CST |
2595 |
|| (TREE_CODE (op1) == INTEGER_CST |
2592 |
&& unsigned1 |
2596 |
&& unsigned1 |
2593 |
&& int_fits_type_p (op1, type))) |
2597 |
&& int_fits_type_p (op1, type))) |
2594 |
return false; |
2598 |
return SAFE_CONVERSION; |
2595 |
} |
2599 |
} |
2596 |
} |
2600 |
} |
2597 |
/* Warn for integer types converted to smaller integer types. */ |
2601 |
/* Warn for integer types converted to smaller integer types. */ |
2598 |
if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
2602 |
if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
2599 |
give_warning = true; |
2603 |
give_warning = UNSAFE_OTHER; |
2600 |
|
2604 |
|
2601 |
/* When they are the same width but different signedness, |
2605 |
/* When they are the same width but different signedness, |
2602 |
then the value may change. */ |
2606 |
then the value may change. */ |
Lines 2632-2645
Link Here
|
2632 |
|
2636 |
|
2633 |
if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2637 |
if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) |
2634 |
|| !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) |
2638 |
|| !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) |
2635 |
give_warning = true; |
2639 |
give_warning = UNSAFE_OTHER; |
2636 |
} |
2640 |
} |
2637 |
|
2641 |
|
2638 |
/* Warn for real types converted to smaller real types. */ |
2642 |
/* Warn for real types converted to smaller real types. */ |
2639 |
else if (TREE_CODE (expr_type) == REAL_TYPE |
2643 |
else if (TREE_CODE (expr_type) == REAL_TYPE |
2640 |
&& TREE_CODE (type) == REAL_TYPE |
2644 |
&& TREE_CODE (type) == REAL_TYPE |
2641 |
&& TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
2645 |
&& TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) |
2642 |
give_warning = true; |
2646 |
give_warning = UNSAFE_REAL; |
2643 |
} |
2647 |
} |
2644 |
|
2648 |
|
2645 |
return give_warning; |
2649 |
return give_warning; |
Lines 2653-2660
Link Here
|
2653 |
{ |
2657 |
{ |
2654 |
tree expr_type = TREE_TYPE (expr); |
2658 |
tree expr_type = TREE_TYPE (expr); |
2655 |
location_t loc = EXPR_LOC_OR_HERE (expr); |
2659 |
location_t loc = EXPR_LOC_OR_HERE (expr); |
|
|
2660 |
enum conversion_safety conversion_kind; |
2656 |
|
2661 |
|
2657 |
if (!warn_conversion && !warn_sign_conversion) |
2662 |
if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) |
2658 |
return; |
2663 |
return; |
2659 |
|
2664 |
|
2660 |
switch (TREE_CODE (expr)) |
2665 |
switch (TREE_CODE (expr)) |
Lines 2681-2690
Link Here
|
2681 |
|
2686 |
|
2682 |
case REAL_CST: |
2687 |
case REAL_CST: |
2683 |
case INTEGER_CST: |
2688 |
case INTEGER_CST: |
2684 |
if (unsafe_conversion_p (type, expr, true)) |
2689 |
if ((conversion_kind = unsafe_conversion_p (type, expr, true))) |
2685 |
warning_at (loc, OPT_Wconversion, |
2690 |
{ |
2686 |
"conversion to %qT alters %qT constant value", |
2691 |
if(conversion_kind == UNSAFE_REAL) |
2687 |
type, expr_type); |
2692 |
{ |
|
|
2693 |
warning_at (loc, OPT_Wfloat_conversion, |
2694 |
"conversion to %qT alters %qT constant value", |
2695 |
type, expr_type); |
2696 |
} |
2697 |
else |
2698 |
{ |
2699 |
warning_at (loc, OPT_Wconversion, |
2700 |
"conversion to %qT alters %qT constant value", |
2701 |
type, expr_type); |
2702 |
} |
2703 |
} |
2688 |
return; |
2704 |
return; |
2689 |
|
2705 |
|
2690 |
case COND_EXPR: |
2706 |
case COND_EXPR: |
Lines 2700-2709
Link Here
|
2700 |
} |
2716 |
} |
2701 |
|
2717 |
|
2702 |
default: /* 'expr' is not a constant. */ |
2718 |
default: /* 'expr' is not a constant. */ |
2703 |
if (unsafe_conversion_p (type, expr, true)) |
2719 |
if ((conversion_kind = unsafe_conversion_p (type, expr, true))) |
2704 |
warning_at (loc, OPT_Wconversion, |
2720 |
{ |
2705 |
"conversion to %qT from %qT may alter its value", |
2721 |
if(conversion_kind == UNSAFE_REAL) |
2706 |
type, expr_type); |
2722 |
{ |
|
|
2723 |
warning_at (loc, OPT_Wfloat_conversion, |
2724 |
"conversion to %qT from %qT may alter its value", |
2725 |
type, expr_type); |
2726 |
} |
2727 |
else |
2728 |
{ |
2729 |
warning_at (loc, OPT_Wconversion, |
2730 |
"conversion to %qT from %qT may alter its value", |
2731 |
type, expr_type); |
2732 |
} |
2733 |
} |
2707 |
} |
2734 |
} |
2708 |
} |
2735 |
} |
2709 |
|
2736 |
|