Hello world,

the rather self-explanatory patch implements the -Wzerotrip
option.  The positive form is not really useful, because
the option is on by default (so the default behavior is
not changed).

The negative form of the option, -Wno-zerotrip, suppresses the
warning.  I have also added information of how to suppress the
warning in the message.

Alternatively, it is also possible to only activate the warning
if it is set explicitly, or with -Wall.  I can easily change the
patch to do so, if that turns out to be the consensus (I have no
strong opinion on the matter either way)

Regression-tested. OK for trunk?

        Thomas

2013-08-03  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/56666
        * gfortran.h (gfc_option_t):  Add warn_zerotrip.
        * invoke.texi (-Wzerotrip):  Document option.
        * lang.opt (Wzerotrip):  Add.
        * options.c (gfc_init_options):  Initialize warn_zerotrip.
        (gfc_handle_option):  Handle OPT_Wzerotrip.
        * resolve.c (gfc_resolve_iterator): Honor
        gfc_option.warn_zerotrip; update error message to show
        how to suppress the warning.

2013-08-03  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/56666
        * gfortran.dg/do_check_10.f90:  New test.
Index: gfortran.h
===================================================================
--- gfortran.h	(Revision 201448)
+++ gfortran.h	(Arbeitskopie)
@@ -2252,6 +2252,7 @@ typedef struct
   int warn_align_commons;
   int warn_real_q_constant;
   int warn_unused_dummy_argument;
+  int warn_zerotrip;
   int warn_realloc_lhs;
   int warn_realloc_lhs_all;
   int warn_compare_reals;
Index: invoke.texi
===================================================================
--- invoke.texi	(Revision 201448)
+++ invoke.texi	(Arbeitskopie)
@@ -954,6 +954,12 @@ This option is implied by @option{-Wextra}.
 Warn if the pointer in a pointer assignment might be longer than the its
 target. This option is implied by @option{-Wall}.
 
+@item -Wzerotrip
+@opindex @code{Wzerotrip}
+Warn if a @code{DO} loop is known to execute zero times at compile
+time.  This option is on by default and can be deactivated by
+@option{-Wno-zerotrip}.
+
 @item -Werror
 @opindex @code{Werror}
 @cindex warnings, to errors
Index: lang.opt
===================================================================
--- lang.opt	(Revision 201448)
+++ lang.opt	(Arbeitskopie)
@@ -293,6 +293,10 @@ Wunused-dummy-argument
 Fortran Warning
 Warn about unused dummy arguments.
 
+Wzerotrip
+Fortran Warning
+Warn about zero-trip DO loops
+
 cpp
 Fortran Negative(nocpp)
 Enable preprocessing
Index: options.c
===================================================================
--- options.c	(Revision 201448)
+++ options.c	(Arbeitskopie)
@@ -109,6 +109,7 @@ gfc_init_options (unsigned int decoded_options_cou
   gfc_option.warn_align_commons = 1;
   gfc_option.warn_real_q_constant = 0;
   gfc_option.warn_unused_dummy_argument = 0;
+  gfc_option.warn_zerotrip = 1;
   gfc_option.warn_realloc_lhs = 0;
   gfc_option.warn_realloc_lhs_all = 0;
   gfc_option.warn_compare_reals = 0;
@@ -747,6 +748,10 @@ gfc_handle_option (size_t scode, const char *arg,
       gfc_option.warn_unused_dummy_argument = value;
       break;
 
+    case OPT_Wzerotrip:
+      gfc_option.warn_zerotrip = value;
+      break;
+
     case OPT_fall_intrinsics:
       gfc_option.flag_all_intrinsics = 1;
       break;
Index: resolve.c
===================================================================
--- resolve.c	(Revision 201448)
+++ resolve.c	(Arbeitskopie)
@@ -6282,8 +6282,10 @@ gfc_resolve_iterator (gfc_iterator *iter, bool rea
 	  sgn = mpfr_sgn (iter->step->value.real);
 	  cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
 	}
-      if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
-	gfc_warning ("DO loop at %L will be executed zero times",
+      if (gfc_option.warn_zerotrip &&
+	  ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
+	gfc_warning ("DO loop at %L will be executed zero times"
+		     " (use -Wno-zerotrip to suppress)",
 		     &iter->step->where);
     }
 
! { dg-do compile }
! { dg-options "-Wno-zerotrip" }
program main
  do i=1,0
    print *,i
  end do
end program main

Reply via email to