GCC Bugzilla – Attachment 30873 Details for
Bug 53001
-Wfloat-conversion should be available to warn about floating point errors
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch to add -Wfloat-conversion option
warn_float_patch.diff (text/plain), 8.39 KB, created by
Joshua Cogliati
on 2013-09-20 17:27:11 UTC
(
hide
)
Description:
Patch to add -Wfloat-conversion option
Filename:
MIME Type:
Creator:
Joshua Cogliati
Created:
2013-09-20 17:27:11 UTC
Size:
8.39 KB
patch
obsolete
>diff -ur gcc-4.8.1_orig/gcc/c-family/c-common.c gcc-4.8.1/gcc/c-family/c-common.c >--- gcc-4.8.1_orig/gcc/c-family/c-common.c 2013-05-14 14:52:27.000000000 -0600 >+++ gcc-4.8.1/gcc/c-family/c-common.c 2013-09-02 13:14:01.000000000 -0600 >@@ -294,6 +294,8 @@ > {NULL, 0, 0}, > }; > >+enum conversion_safety {SAFE_CONVERSION=0,UNSAFE_OTHER,UNSAFE_SIGN,UNSAFE_REAL}; >+ > /* Global visibility options. */ > struct visibility_flags visibility_options; > >@@ -375,6 +377,8 @@ > static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *); > static int resort_field_decl_cmp (const void *, const void *); > >+static enum conversion_safety unsafe_conversion_p (tree, tree, bool); >+ > /* Reserved words. The third field is a mask: keywords are disabled > if they match the mask. > >@@ -2481,10 +2485,10 @@ > Function allows conversions between types of different signedness and > does not return true in that case. Function can produce signedness > warnings if PRODUCE_WARNS is true. */ >-bool >+static enum conversion_safety > unsafe_conversion_p (tree type, tree expr, bool produce_warns) > { >- bool give_warning = false; >+ enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */ > tree expr_type = TREE_TYPE (expr); > location_t loc = EXPR_LOC_OR_HERE (expr); > >@@ -2496,7 +2500,7 @@ > && TREE_CODE (type) == INTEGER_TYPE) > { > if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type))) >- give_warning = true; >+ give_warning = UNSAFE_REAL; > } > /* Warn for an integer constant that does not fit into integer type. */ > else if (TREE_CODE (expr_type) == INTEGER_TYPE >@@ -2517,7 +2521,7 @@ > " constant value to negative integer"); > } > else >- give_warning = true; >+ give_warning = UNSAFE_OTHER; > } > else if (TREE_CODE (type) == REAL_TYPE) > { >@@ -2526,7 +2530,7 @@ > { > REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr); > if (!exact_real_truncate (TYPE_MODE (type), &a)) >- give_warning = true; >+ give_warning = UNSAFE_REAL; > } > /* Warn for a real constant that does not fit into a smaller > real type. */ >@@ -2535,7 +2539,7 @@ > { > REAL_VALUE_TYPE a = TREE_REAL_CST (expr); > if (!exact_real_truncate (TYPE_MODE (type), &a)) >- give_warning = true; >+ give_warning = UNSAFE_REAL; > } > } > } >@@ -2544,7 +2548,7 @@ > /* Warn for real types converted to integer types. */ > if (TREE_CODE (expr_type) == REAL_TYPE > && TREE_CODE (type) == INTEGER_TYPE) >- give_warning = true; >+ give_warning = UNSAFE_REAL; > > else if (TREE_CODE (expr_type) == INTEGER_TYPE > && TREE_CODE (type) == INTEGER_TYPE) >@@ -2582,7 +2586,7 @@ > && int_fits_type_p (op1, c_common_signed_type (type)) > && int_fits_type_p (op1, > c_common_unsigned_type (type)))) >- return false; >+ return SAFE_CONVERSION; > /* If constant is unsigned and fits in the target > type, then the result will also fit. */ > else if ((TREE_CODE (op0) == INTEGER_CST >@@ -2591,12 +2595,12 @@ > || (TREE_CODE (op1) == INTEGER_CST > && unsigned1 > && int_fits_type_p (op1, type))) >- return false; >+ return SAFE_CONVERSION; > } > } > /* Warn for integer types converted to smaller integer types. */ > if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) >- give_warning = true; >+ give_warning = UNSAFE_OTHER; > > /* When they are the same width but different signedness, > then the value may change. */ >@@ -2632,14 +2636,14 @@ > > if (!exact_real_truncate (TYPE_MODE (type), &real_low_bound) > || !exact_real_truncate (TYPE_MODE (type), &real_high_bound)) >- give_warning = true; >+ give_warning = UNSAFE_OTHER; > } > > /* Warn for real types converted to smaller real types. */ > else if (TREE_CODE (expr_type) == REAL_TYPE > && TREE_CODE (type) == REAL_TYPE > && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type)) >- give_warning = true; >+ give_warning = UNSAFE_REAL; > } > > return give_warning; >@@ -2653,8 +2657,9 @@ > { > tree expr_type = TREE_TYPE (expr); > location_t loc = EXPR_LOC_OR_HERE (expr); >+ enum conversion_safety conversion_kind; > >- if (!warn_conversion && !warn_sign_conversion) >+ if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion) > return; > > switch (TREE_CODE (expr)) >@@ -2681,10 +2686,21 @@ > > case REAL_CST: > case INTEGER_CST: >- if (unsafe_conversion_p (type, expr, true)) >- warning_at (loc, OPT_Wconversion, >- "conversion to %qT alters %qT constant value", >- type, expr_type); >+ if ((conversion_kind = unsafe_conversion_p (type, expr, true))) >+ { >+ if(conversion_kind == UNSAFE_REAL) >+ { >+ warning_at (loc, OPT_Wfloat_conversion, >+ "conversion to %qT alters %qT constant value", >+ type, expr_type); >+ } >+ else >+ { >+ warning_at (loc, OPT_Wconversion, >+ "conversion to %qT alters %qT constant value", >+ type, expr_type); >+ } >+ } > return; > > case COND_EXPR: >@@ -2700,10 +2716,21 @@ > } > > default: /* 'expr' is not a constant. */ >- if (unsafe_conversion_p (type, expr, true)) >- warning_at (loc, OPT_Wconversion, >- "conversion to %qT from %qT may alter its value", >- type, expr_type); >+ if ((conversion_kind = unsafe_conversion_p (type, expr, true))) >+ { >+ if(conversion_kind == UNSAFE_REAL) >+ { >+ warning_at (loc, OPT_Wfloat_conversion, >+ "conversion to %qT from %qT may alter its value", >+ type, expr_type); >+ } >+ else >+ { >+ warning_at (loc, OPT_Wconversion, >+ "conversion to %qT from %qT may alter its value", >+ type, expr_type); >+ } >+ } > } > } > >Only in gcc-4.8.1/gcc/c-family: c-common.c~ >diff -ur gcc-4.8.1_orig/gcc/c-family/c-common.h gcc-4.8.1/gcc/c-family/c-common.h >--- gcc-4.8.1_orig/gcc/c-family/c-common.h 2013-01-10 13:38:27.000000000 -0700 >+++ gcc-4.8.1/gcc/c-family/c-common.h 2013-09-02 10:39:01.000000000 -0600 >@@ -735,7 +735,6 @@ > extern tree c_common_signed_or_unsigned_type (int, tree); > extern void c_common_init_ts (void); > extern tree c_build_bitfield_integer_type (unsigned HOST_WIDE_INT, int); >-extern bool unsafe_conversion_p (tree, tree, bool); > extern bool decl_with_nonnull_addr_p (const_tree); > extern tree c_fully_fold (tree, bool, bool *); > extern tree decl_constant_value_for_optimization (tree); >Only in gcc-4.8.1/gcc/c-family: c-common.h~ >diff -ur gcc-4.8.1_orig/gcc/c-family/c.opt gcc-4.8.1/gcc/c-family/c.opt >--- gcc-4.8.1_orig/gcc/c-family/c.opt 2013-01-18 22:25:25.000000000 -0700 >+++ gcc-4.8.1/gcc/c-family/c.opt 2013-09-20 11:08:11.000000000 -0600 >@@ -379,6 +379,10 @@ > C ObjC RejectNegative Warning Alias(Werror=, implicit-function-declaration) > This switch is deprecated; use -Werror=implicit-function-declaration instead > >+Wfloat-conversion >+C ObjC C++ ObjC++ Var(warn_float_conversion) LangEnabledBy(C ObjC C++,Wconversion) EnabledBy(Wextra) >+Warn for implicit type conversions that cause loss of floating point precision >+ > Wfloat-equal > C ObjC C++ ObjC++ Var(warn_float_equal) Warning > Warn if testing floating point numbers for equality >Only in gcc-4.8.1/gcc/c-family: c.opt~ >diff -ur gcc-4.8.1_orig/gcc/doc/invoke.texi gcc-4.8.1/gcc/doc/invoke.texi >--- gcc-4.8.1_orig/gcc/doc/invoke.texi 2013-03-29 07:41:29.000000000 -0600 >+++ gcc-4.8.1/gcc/doc/invoke.texi 2013-09-20 09:49:18.000000000 -0600 >@@ -260,7 +260,8 @@ > -Wpointer-arith -Wno-pointer-to-int-cast @gol > -Wredundant-decls -Wno-return-local-addr @gol > -Wreturn-type -Wsequence-point -Wshadow @gol >--Wsign-compare -Wsign-conversion -Wsizeof-pointer-memaccess @gol >+-Wsign-compare -Wsign-conversion -Wfloat-conversion @gol >+-Wsizeof-pointer-memaccess @gol > -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol > -Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol > -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol >@@ -4384,6 +4385,14 @@ > integer variable. An explicit cast silences the warning. In C, this > option is enabled also by @option{-Wconversion}. > >+@item -Wfloat-conversion >+@opindex Wfloat-conversion >+@opindex Wno-float-conversion >+Warn for implicit conversions that reduce the precision of a real value. >+This includes conversions from real to integer, and from higher precision >+real to lower precision real values. This option is also enabled by >+@option{-Wconversion}. >+ > @item -Wsizeof-pointer-memaccess > @opindex Wsizeof-pointer-memaccess > @opindex Wno-sizeof-pointer-memaccess >Only in gcc-4.8.1/gcc/doc: invoke.texi~
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 53001
:
30870
|
30871
|
30873
|
30882
|
30899
|
30913
|
30937
|
30979
|
30980
|
30994
|
31014
|
31065
|
31097