From: Thomas Koenig Date: Wed, 1 Jul 2015 16:43:53 +0000 (+0000) Subject: arith.c (gfc_arith_divide): With -Winteger-division... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=c219aeb7dea32513053b905ae584f19cc0813852;p=gcc.git arith.c (gfc_arith_divide): With -Winteger-division... 2015-07-01 Thomas Koenig * arith.c (gfc_arith_divide): With -Winteger-division, warn about contant integer division if there is a non-zero remainder. * invoke.texi: Document -Winteger-division. * lang.opt: Add -Winteger-division. 2015-07-01 Thomas Koenig * gfortran.dg/warn_conversion_8.f90: New test. From-SVN: r225262 --- diff --git a/gcc/fortran/ChangeLog b/gcc/fortran/ChangeLog index 0d2855073b1..c58b6468faa 100644 --- a/gcc/fortran/ChangeLog +++ b/gcc/fortran/ChangeLog @@ -1,3 +1,11 @@ +2015-07-01 Thomas Koenig + + * arith.c (gfc_arith_divide): With -Winteger-division, + warn about contant integer division if there is a non-zero + remainder. + * invoke.texi: Document -Winteger-division. + * lang.opt: Add -Winteger-division. + 2015-06-25 Andrew MacLeod * f95-lang.c: Remove ipa-ref.h and plugin-api.h from include list. diff --git a/gcc/fortran/arith.c b/gcc/fortran/arith.c index d51fbc26be4..6c31b706a96 100644 --- a/gcc/fortran/arith.c +++ b/gcc/fortran/arith.c @@ -731,8 +731,28 @@ gfc_arith_divide (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) break; } - mpz_tdiv_q (result->value.integer, op1->value.integer, - op2->value.integer); + if (warn_integer_division) + { + mpz_t r; + mpz_init (r); + mpz_tdiv_qr (result->value.integer, r, op1->value.integer, + op2->value.integer); + + if (mpz_cmp_si (r, 0) != 0) + { + char *p; + p = mpz_get_str (NULL, 10, result->value.integer); + gfc_warning_now (OPT_Winteger_division, "Integer division " + "truncated to constant %qs at %L", p, + &op1->where); + free (p); + } + mpz_clear (r); + } + else + mpz_tdiv_q (result->value.integer, op1->value.integer, + op2->value.integer); + break; case BT_REAL: diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi index fc9f1b0fa1d..804d2156cb6 100644 --- a/gcc/fortran/invoke.texi +++ b/gcc/fortran/invoke.texi @@ -745,7 +745,7 @@ This currently includes @option{-Waliasing}, @option{-Wampersand}, @option{-Wconversion}, @option{-Wsurprising}, @option{-Wc-binding-type}, @option{-Wintrinsics-std}, @option{-Wno-tabs}, @option{-Wintrinsic-shadow}, @option{-Wline-truncation}, @option{-Wtarget-lifetime}, -@option{-Wreal-q-constant} and @option{-Wunused}. +@option{-Winteger-division}, @option{-Wreal-q-constant} and @option{-Wunused}. @item -Waliasing @opindex @code{Waliasing} @@ -844,6 +844,13 @@ check that the declared interfaces are consistent across program units. Warn if a procedure is called that has neither an explicit interface nor has been declared as @code{EXTERNAL}. +@item -Winteger-division +@opindex @code{Winteger-division} +@cindex warnings, integer division +@cindex warnings, division of integers +Warn if a constant integer division truncates it result. +As an example, 3/5 evaluates to 0. + @item -Wintrinsics-std @opindex @code{Wintrinsics-std} @cindex warnings, non-standard intrinsics diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt index a81c4c52b94..eab3eddae56 100644 --- a/gcc/fortran/lang.opt +++ b/gcc/fortran/lang.opt @@ -249,6 +249,10 @@ Wimplicit-procedure Fortran Warning Var(warn_implicit_procedure) Warn about called procedures not explicitly declared +Winteger-division +Fortran Warning Var(warn_integer_division) LangEnabledBy(Fortran,Wall) +Warn about constant integer divisions with truncated results + Wline-truncation Fortran Warning Var(warn_line_truncation) LangEnabledBy(Fortran,Wall) Init(-1) Warn about truncated source lines diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 4dff233f934..a5ea93313be 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2015-07-01 Thomas Koenig + + * gfortran.dg/warn_conversion_8.f90: New test. + 2015-07-01 Bernd Edlinger PR rtl-optimization/61047 diff --git a/gcc/testsuite/gfortran.dg/warn_conversion_8.f90 b/gcc/testsuite/gfortran.dg/warn_conversion_8.f90 new file mode 100644 index 00000000000..97c380c9381 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/warn_conversion_8.f90 @@ -0,0 +1,8 @@ +! { dg-do compile } +! { dg-options "-Winteger-division" } +program main + integer, parameter :: n = 23 + integer, parameter :: m = n*(n+1)/2 ! No warning + integer, parameter :: i = n*(n+1)/17 ! { dg-warning "Integer division truncated to constant" } + print *, 3/5 ! { dg-warning "Integer division truncated to constant" } +end program main