diff options
author | Thomas Koenig <tkoenig@gcc.gnu.org> | 2012-08-19 15:05:41 +0000 |
---|---|---|
committer | Thomas Koenig <tkoenig@gcc.gnu.org> | 2012-08-19 15:05:41 +0000 |
commit | cf21551eac46a2494613eb3ef8d62699c6d9ab74 (patch) | |
tree | 84b40c19d9c7f98d00864d2e148356bbea76b7b7 /gcc | |
parent | 5da8c011067c5514c5e891fd8b4d854eb4fc2e71 (diff) | |
download | gcc-cf21551eac46a2494613eb3ef8d62699c6d9ab74.zip gcc-cf21551eac46a2494613eb3ef8d62699c6d9ab74.tar.gz gcc-cf21551eac46a2494613eb3ef8d62699c6d9ab74.tar.bz2 |
re PR fortran/54298 (Add warning when doing equal/nonequal floating-point comparisons)
2012-08-19 Thomas König <tkoenig@gcc.gnu.org>
PR fortran/54298
* gfortran.h (struct gfc_option_t): Add warn_compare_reals.
* lang.opt: Add Wcompare-reals.
* invoke.texi: Document -Wcompare-reals.
* resolve.c (resolve_operator): If -Wcompare-reals is in effect,
warn about equality/inequality comparisions for REAL and COMPLEX.
* options.c (gfc_init_options): Set warn_compare_reals.
(set_Wall): Include warn_compare_reals in Wall.
(gfc_handle_option): Handle Wcompare_reals.
2012-08-19 Thomas König <tkoenig@gcc.gnu.org>
PR fortran/54298
* gfortran.dg/real_compare_1.f90: New test case.
* gfortran.dg/bessel_5.f90 Add -Wno-compare-reals to options.
From-SVN: r190516
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/fortran/ChangeLog | 12 | ||||
-rw-r--r-- | gcc/fortran/gfortran.h | 1 | ||||
-rw-r--r-- | gcc/fortran/invoke.texi | 14 | ||||
-rw-r--r-- | gcc/fortran/lang.opt | 4 | ||||
-rw-r--r-- | gcc/fortran/options.c | 6 | ||||
-rw-r--r-- | gcc/fortran/resolve.c | 22 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/testsuite/gfortran.dg/bessel_5.f90 | 2 | ||||
-rw-r--r-- | gcc/testsuite/gfortran.dg/real_compare_1.f90 | 21 |
9 files changed, 83 insertions, 5 deletions
diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 77e98a0..e91f947c 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,15 @@ +2012-08-19 Thomas König <tkoenig@gcc.gnu.org> + + PR fortran/54298 + * gfortran.h (struct gfc_option_t): Add warn_compare_reals. + * lang.opt: Add Wcompare-reals. + * invoke.texi: Document -Wcompare-reals. + * resolve.c (resolve_operator): If -Wcompare-reals is in effect, + warn about equality/inequality comparisions for REAL and COMPLEX. + * options.c (gfc_init_options): Set warn_compare_reals. + (set_Wall): Include warn_compare_reals in Wall. + (gfc_handle_option): Handle Wcompare_reals. + 2012-08-17 Jakub Jelinek <jakub@redhat.com> * array.c (gfc_match_array_ref): Fix up memset arguments. diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 7c4c0a4..c005151 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -2225,6 +2225,7 @@ typedef struct int warn_unused_dummy_argument; int warn_realloc_lhs; int warn_realloc_lhs_all; + int warn_compare_reals; int max_errors; int flag_all_intrinsics; diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi index 658ed23..d962ca0 100644 --- a/gcc/fortran/invoke.texi +++ b/gcc/fortran/invoke.texi @@ -726,10 +726,11 @@ warnings. @cindex warnings, all Enables commonly used warning options pertaining to usage that we recommend avoiding and that we believe are easy to avoid. -This currently includes @option{-Waliasing}, @option{-Wampersand}, -@option{-Wconversion}, @option{-Wsurprising}, @option{-Wintrinsics-std}, -@option{-Wno-tabs}, @option{-Wintrinsic-shadow}, @option{-Wline-truncation}, -@option{-Wreal-q-constant} and @option{-Wunused}. +This currently includes @option{-Waliasing}, @option{-Wampersand}, +@option{-Wconversion}, @option{-Wcompare-reals}, @option{-Wsurprising}, +@option{-Wintrinsics-std}, @option{-Wno-tabs}, @option{-Wintrinsic-shadow}, +@option{-Wline-truncation}, @option{-Wreal-q-constant} and +@option{-Wunused}. @item -Waliasing @opindex @code{Waliasing} @@ -935,6 +936,11 @@ a scalar. See also @option{-frealloc-lhs}. Warn when the compiler inserts code to for allocation or reallocation of an allocatable variable; this includes scalars and derived types. +@item -Wcompare-reals +@opindex @code{Wcompare-reals} +Warn when comparing real or complex types for equality or inequality. +Enabled by @option{-Wall}. + @item -Werror @opindex @code{Werror} @cindex warnings, to errors diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt index 3b9d29b..e0c7cf7 100644 --- a/gcc/fortran/lang.opt +++ b/gcc/fortran/lang.opt @@ -218,6 +218,10 @@ Wcharacter-truncation Fortran Warning Warn about truncated character expressions +Wcompare-reals +Fortran Warning +Warn about equality comparisons involving REAL or COMPLEX expressions + Wconversion Fortran Warning ; Documented in C diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c index 64d4da1..3e4444d 100644 --- a/gcc/fortran/options.c +++ b/gcc/fortran/options.c @@ -113,6 +113,7 @@ gfc_init_options (unsigned int decoded_options_count, gfc_option.warn_unused_dummy_argument = 0; gfc_option.warn_realloc_lhs = 0; gfc_option.warn_realloc_lhs_all = 0; + gfc_option.warn_compare_reals = 0; gfc_option.max_errors = 25; gfc_option.flag_all_intrinsics = 0; @@ -473,6 +474,7 @@ set_Wall (int setting) gfc_option.warn_character_truncation = setting; gfc_option.warn_real_q_constant = setting; gfc_option.warn_unused_dummy_argument = setting; + gfc_option.warn_compare_reals = setting; warn_return_type = setting; warn_switch = setting; @@ -638,6 +640,10 @@ gfc_handle_option (size_t scode, const char *arg, int value, gfc_option.warn_character_truncation = value; break; + case OPT_Wcompare_reals: + gfc_option.warn_compare_reals = value; + break; + case OPT_Wconversion: gfc_option.gfc_warn_conversion = value; break; diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index ac5a362..c9be70e0 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -4034,6 +4034,28 @@ resolve_operator (gfc_expr *e) e->ts.type = BT_LOGICAL; e->ts.kind = gfc_default_logical_kind; + + if (gfc_option.warn_compare_reals) + { + gfc_intrinsic_op op = e->value.op.op; + + /* Type conversion has made sure that the types of op1 and op2 + agree, so it is only necessary to check the first one. */ + if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX) + && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS + || op == INTRINSIC_NE || op == INTRINSIC_NE_OS)) + { + const char *msg; + + if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS) + msg = "Equality comparison for %s at %L"; + else + msg = "Inequality comparison for %s at %L"; + + gfc_warning (msg, gfc_typename (&op1->ts), &op1->where); + } + } + break; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 378e778..1e29433 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2012-08-19 Thomas König <tkoenig@gcc.gnu.org> + + PR fortran/54298 + * gfortran.dg/real_compare_1.f90: New test case. + * gfortran.dg/bessel_5.f90 Add -Wno-compare-reals to options. + 2012-08-18 Jan Hubicka <jh@suse.cz> * gcc.dg/ipa/iinline-1.c: Update testcase to test inline hints. diff --git a/gcc/testsuite/gfortran.dg/bessel_5.f90 b/gcc/testsuite/gfortran.dg/bessel_5.f90 index aab45ca..0027280 100644 --- a/gcc/testsuite/gfortran.dg/bessel_5.f90 +++ b/gcc/testsuite/gfortran.dg/bessel_5.f90 @@ -1,5 +1,5 @@ ! { dg-do run } -! { dg-options "-Wall -fno-range-check" } +! { dg-options "-Wall -fno-range-check -Wno-compare-reals" } ! ! PR fortran/36158 - Transformational BESSEL_JN/YN ! PR fortran/33197 - F2008 math functions diff --git a/gcc/testsuite/gfortran.dg/real_compare_1.f90 b/gcc/testsuite/gfortran.dg/real_compare_1.f90 new file mode 100644 index 0000000..fd84177 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/real_compare_1.f90 @@ -0,0 +1,21 @@ +! { dg-do compile } +! { dg-options "-Wcompare-reals" } +program main + real :: a + complex :: c + read (*,*) a + read (*,*) c + if (a .eq. 3.14) print *,"foo" ! { dg-warning "Equality comparison for REAL" } + if (3.14 == a) print *,"foo" ! { dg-warning "Equality comparison for REAL" } + if (a .eq. 3) print *,"foo" ! { dg-warning "Equality comparison for REAL" } + if (3. == a) print *,"foo" ! { dg-warning "Equality comparison for REAL" } + if (a .ne. 4.14) print *,"foo" ! { dg-warning "Inequality comparison for REAL" } + if (4.14 /= a) print *,"foo" ! { dg-warning "Inequality comparison for REAL" } + if (a .ne. 4) print *,"foo" ! { dg-warning "Inequality comparison for REAL" } + if (4 /= a) print *,"foo" ! { dg-warning "Inequality comparison for REAL" } + + if (c .eq. (3.14, 2.11)) print *,"foo" ! { dg-warning "Equality comparison for COMPLEX" } + if ((3.14, 2.11) == a) print *,"foo" ! { dg-warning "Equality comparison for COMPLEX" } + if (c .ne. (3.14, 2.11)) print *,"foo" ! { dg-warning "Inequality comparison for COMPLEX" } + if ((3.14, 2.11) /= a) print *,"foo" ! { dg-warning "Inequality comparison for COMPLEX" } +end program main |