Patchwork [Fortran] PR54234 - Add -Wconversion warning for CMPLX(dp,dp)

login
register
mail settings
Submitter Tobias Burnus
Date Aug. 14, 2012, 9:33 a.m.
Message ID <502A1B4E.2070106@net-b.de>
Download mbox | patch
Permalink /patch/177195/
State New
Headers show

Comments

Tobias Burnus - Aug. 14, 2012, 9:33 a.m.
This patch adds a -Wconversion warning (enabled also by -Wall) for
   CMPLX(real, real)
if the real arguments have a higher kind number/precision as the 
default-kind of complex/real. I think most of the time, this precision 
loss is unintended; it can be silenced when using a kind= parameter (or 
-Wno-conversion).

However, if you believe that the warning is not suitable for -Wall, we 
can also hide it by only enabling it with the talkative 
-Wconversion-extra flag.

Build and regtested on x86-64-linux.
OK for the trunk?

Tobias
Mikael Morin - Aug. 14, 2012, 10:02 a.m.
On 14/08/2012 11:33, Tobias Burnus wrote:
> This patch adds a -Wconversion warning (enabled also by -Wall) for
>   CMPLX(real, real)
> if the real arguments have a higher kind number/precision as the
> default-kind of complex/real. I think most of the time, this precision
> loss is unintended; it can be silenced when using a kind= parameter (or
> -Wno-conversion).
> 
> However, if you believe that the warning is not suitable for -Wall, we
> can also hide it by only enabling it with the talkative
> -Wconversion-extra flag.
> 
> Build and regtested on x86-64-linux.
> OK for the trunk?
> 
> Tobias

Yes, thanks

Mikael

Patch

2012-08-14  Tobias Burnus  <burnus@net-b.de>

	PR fortran/54234
	* check.c (gfc_check_cmplx): Add -Wconversion warning
	when converting higher-precision REAL to default-precision
	CMPLX without kind= parameter.

2012-08-14  Tobias Burnus  <burnus@net-b.de>

	PR fortran/54234
	* gfortran.dg/warn_conversion_4.f90: New.

diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c
index c5bf79b..2235b52 100644
--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -1278,6 +1278,17 @@  gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
   if (kind_check (kind, 2, BT_COMPLEX) == FAILURE)
     return FAILURE;
 
+  if (!kind && gfc_option.gfc_warn_conversion
+      && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
+    gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
+		     "might loose precision, consider using the KIND argument",
+		     gfc_typename (&x->ts), gfc_default_real_kind, &x->where);
+  else if (y && !kind && gfc_option.gfc_warn_conversion
+	   && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
+    gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
+		     "might loose precision, consider using the KIND argument",
+		     gfc_typename (&y->ts), gfc_default_real_kind, &y->where);
+
   return SUCCESS;
 }
 
--- /dev/null	2012-08-08 07:41:43.631684108 +0200
+++ gcc/gcc/testsuite/gfortran.dg/warn_conversion_4.f90	2012-08-14 10:19:56.000000000 +0200
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! { dg-options "-Wconversion" }
+!
+! PR fortran/54234
+!
+!
+module fft_mod
+  implicit none
+  integer, parameter :: dp=kind(0.0d0)
+contains
+  subroutine test
+    integer :: x
+    x = int (abs (cmplx(2.3,0.1)))
+    x = int (abs (cmplx(2.3_dp,0.1))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+    x = int (abs (cmplx(2.3,0.1_dp))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+    x = int (abs (cmplx(2.3_dp,0.1_dp))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+  end subroutine test
+end module fft_mod
--- /dev/null	2012-08-08 07:41:43.631684108 +0200
+++ gcc/gcc/testsuite/gfortran.dg/warn_conversion_4.f90	2012-08-14 10:19:56.000000000 +0200
@@ -0,0 +1,18 @@ 
+! { dg-do compile }
+! { dg-options "-Wconversion" }
+!
+! PR fortran/54234
+!
+!
+module fft_mod
+  implicit none
+  integer, parameter :: dp=kind(0.0d0)
+contains
+  subroutine test
+    integer :: x
+    x = int (abs (cmplx(2.3,0.1)))
+    x = int (abs (cmplx(2.3_dp,0.1))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+    x = int (abs (cmplx(2.3,0.1_dp))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+    x = int (abs (cmplx(2.3_dp,0.1_dp))) ! { dg-warning "Conversion from REAL.8. to default-kind COMPLEX.4. at .1. might loose precision, consider using the KIND argument" }
+  end subroutine test
+end module fft_mod