* [patch, fortran] Warn about integer**(negative integer)
@ 2017-01-22 10:01 Thomas Koenig
2017-01-22 21:48 ` FX
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Koenig @ 2017-01-22 10:01 UTC (permalink / raw)
To: fortran, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 707 bytes --]
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.
[-- Attachment #2: p1.diff --]
[-- Type: text/x-patch, Size: 591 bytes --]
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))
{
[-- Attachment #3: integer_exponentiation_7.f90 --]
[-- Type: text/x-fortran, Size: 171 bytes --]
! { dg-do compile }
! { dg-options "-Winteger-division" }
program main
print *,10**(-3) ! { dg-warning "Integer exponentiation truncated to constant" }
end program main
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [patch, fortran] Warn about integer**(negative integer)
2017-01-22 10:01 [patch, fortran] Warn about integer**(negative integer) Thomas Koenig
@ 2017-01-22 21:48 ` FX
2017-01-23 19:55 ` Thomas Koenig
0 siblings, 1 reply; 3+ messages in thread
From: FX @ 2017-01-22 21:48 UTC (permalink / raw)
To: Thomas Koenig; +Cc: fortran, gcc-patches
> 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.
The idea seems reasonable, but I am not sure about the wording (“truncated” in particular). “Integer exponentiation truncated to constant 0”? Why not “Negative integer exponent has result value zero”?
Other than that, OK to commit.
FX
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [patch, fortran] Warn about integer**(negative integer)
2017-01-22 21:48 ` FX
@ 2017-01-23 19:55 ` Thomas Koenig
0 siblings, 0 replies; 3+ messages in thread
From: Thomas Koenig @ 2017-01-23 19:55 UTC (permalink / raw)
To: FX; +Cc: fortran, gcc-patches
Hi FX,
> The idea seems reasonable, but I am not sure about the wording
> (âtruncatedâ in particular). âInteger exponentiation truncated
> to constant 0â? Why not âNegative integer exponent has result
> value zeroâ?
>
> Other than that, OK to commit.
You're right, the error message was a bit strange. Here is the
version of the patch I committed with corresponding change in the
test case.
Regards
Thomas
Index: fortran/arith.c
===================================================================
--- fortran/arith.c (Revision 244747)
+++ fortran/arith.c (Arbeitskopie)
@@ -874,6 +874,10 @@ 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, "Negative
"
+ "exponent of integer has zero "
+ "result at %L", &result->where);
}
else if (gfc_extract_int (op2, &power))
{
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2017-01-23 19:45 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-22 10:01 [patch, fortran] Warn about integer**(negative integer) Thomas Koenig
2017-01-22 21:48 ` FX
2017-01-23 19:55 ` Thomas Koenig
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).