This is the mail archive of the
fortran@gcc.gnu.org
mailing list for the GNU Fortran project.
[patch, fortran] Warn about integer**(negative integer)
- From: Thomas Koenig <tkoenig at netcologne dot de>
- To: "fortran at gcc dot gnu dot org" <fortran at gcc dot gnu dot org>, gcc-patches <gcc-patches at gcc dot gnu dot org>
- Date: Sun, 22 Jan 2017 10:47:18 +0100
- Subject: [patch, fortran] Warn about integer**(negative integer)
- Authentication-results: sourceware.org; auth=none
Hello world,
a reacent thread on c.l.f showed another common error - expecting
10**(-3) to expect something different from zero. The attached
patch warns about this result if -Winteger-division is active.
Why -Winteger-division? Two reasons: First, 10**(-3) ist just
a shorthand for integer division. Second, I did not want to add
yet another warning flag.
Regression-tested. OK for trunk?
Thomas
2017-01-22 Thomas Koenig <tkoenig@netcologne.de>
* arith.c (arith_power): If simplifying integer power expression
to zero, warn if -Winteger-division is given.
2017-01-22 Thomas Koenig <tkoenig@netcologne.de>
* gfortran.dg/integer_exponentiation_7.f90: New test.
Index: arith.c
===================================================================
--- arith.c (Revision 244747)
+++ arith.c (Arbeitskopie)
@@ -874,6 +874,11 @@ arith_power (gfc_expr *op1, gfc_expr *op2, gfc_exp
{
/* if op2 < 0, op1**op2 == 0 because abs(op1) > 1. */
mpz_set_si (result->value.integer, 0);
+ if (warn_integer_division)
+ gfc_warning_now (OPT_Winteger_division, "Integer "
+ "exponentiation truncated to constant "
+ "%qs at %L", "0", &result->where);
+
}
else if (gfc_extract_int (op2, &power))
{
! { dg-do compile }
! { dg-options "-Winteger-division" }
program main
print *,10**(-3) ! { dg-warning "Integer exponentiation truncated to constant" }
end program main