Blame SOURCES/0007-Allow-non-logical-expressions-in-IF-statements.patch

840c13
From a47308b5badceb8038fcf5edd2b93f33f2d7997e Mon Sep 17 00:00:00 2001
840c13
From: Mark Eggleston <markeggleston@gcc.gnu.org>
840c13
Date: Mon, 3 Feb 2020 09:31:05 +0000
840c13
Subject: [PATCH 07/10] Allow non-logical expressions in IF statements
0714f1
0714f1
Use -fdec-non-logical-if to enable feature. Also enabled using -fdec.
0714f1
---
0714f1
 gcc/fortran/lang.opt                               |  4 ++
0714f1
 gcc/fortran/options.c                              |  1 +
0714f1
 gcc/fortran/resolve.c                              | 60 ++++++++++++++++++----
0714f1
 ...ec_logical_expressions_if_statements_blocks_1.f | 25 +++++++++
0714f1
 ...ec_logical_expressions_if_statements_blocks_2.f | 25 +++++++++
0714f1
 ...ec_logical_expressions_if_statements_blocks_3.f | 25 +++++++++
0714f1
 ...ec_logical_expressions_if_statements_blocks_4.f | 45 ++++++++++++++++
0714f1
 ...ec_logical_expressions_if_statements_blocks_5.f | 45 ++++++++++++++++
0714f1
 ...ec_logical_expressions_if_statements_blocks_6.f | 45 ++++++++++++++++
0714f1
 9 files changed, 266 insertions(+), 9 deletions(-)
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f
0714f1
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f
0714f1
0714f1
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
840c13
index b378f467e2f..0a6b4263e22 100644
0714f1
--- a/gcc/fortran/lang.opt
0714f1
+++ b/gcc/fortran/lang.opt
840c13
@@ -493,6 +493,10 @@ fdec-override-kind
0714f1
 Fortran Var(flag_dec_override_kind)
0714f1
 Enable support for per variable kind specification.
0714f1
 
0714f1
+fdec-non-logical-if
0714f1
+Fortran Var(flag_dec_non_logical_if)
0714f1
+Enable support for non-logical expressions in if statements.
0714f1
+
0714f1
 fdec-old-init
0714f1
 Fortran Var(flag_dec_old_init)
0714f1
 Enable support for old style initializers in derived types.
0714f1
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
840c13
index fac23e83d70..86b28cfe3e6 100644
0714f1
--- a/gcc/fortran/options.c
0714f1
+++ b/gcc/fortran/options.c
840c13
@@ -81,6 +81,7 @@ set_dec_flags (int value)
0714f1
   SET_BITFLAG (flag_dec_non_integer_index, value, value);
0714f1
   SET_BITFLAG (flag_dec_old_init, value, value);
0714f1
   SET_BITFLAG (flag_dec_override_kind, value, value);
0714f1
+  SET_BITFLAG (flag_dec_non_logical_if, value, value);
0714f1
 }
0714f1
 
0714f1
 /* Finalize DEC flags.  */
0714f1
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
840c13
index 044eed22c76..e4bb0e79c80 100644
0714f1
--- a/gcc/fortran/resolve.c
0714f1
+++ b/gcc/fortran/resolve.c
840c13
@@ -10721,10 +10721,31 @@ gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
0714f1
       switch (b->op)
0714f1
 	{
0714f1
 	case EXEC_IF:
0714f1
-	  if (t && b->expr1 != NULL
0714f1
-	      && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
0714f1
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
0714f1
-		       &b->expr1->where);
0714f1
+	  if (t && b->expr1 != NULL)
0714f1
+	    {
0714f1
+	      if (flag_dec_non_logical_if && b->expr1->ts.type != BT_LOGICAL)
0714f1
+		{
0714f1
+		  gfc_expr* cast;
0714f1
+		  cast = gfc_ne (b->expr1,
0714f1
+				 gfc_get_int_expr (1, &gfc_current_locus, 0),
0714f1
+				 INTRINSIC_NE);
0714f1
+		  if (cast == NULL)
0714f1
+		    gfc_internal_error ("gfc_resolve_blocks(): Failed to cast "
0714f1
+					"to LOGICAL in IF");
0714f1
+		  b->expr1 = cast;
0714f1
+                  if (warn_conversion_extra)
0714f1
+		    {
0714f1
+		      gfc_warning (OPT_Wconversion_extra, "Non-LOGICAL type in"
0714f1
+				   " IF statement condition %L will be true if"
0714f1
+				   " it evaluates to nonzero",
0714f1
+				   &b->expr1->where);
0714f1
+		    }
0714f1
+		}
0714f1
+
0714f1
+	      if ((b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
0714f1
+		gfc_error ("IF clause at %L requires a scalar LOGICAL "
0714f1
+			   "expression", &b->expr1->where);
0714f1
+	    }
0714f1
 	  break;
0714f1
 
0714f1
 	case EXEC_WHERE:
840c13
@@ -12019,11 +12040,32 @@ start:
0714f1
 	  break;
0714f1
 
0714f1
 	case EXEC_IF:
0714f1
-	  if (t && code->expr1 != NULL
0714f1
-	      && (code->expr1->ts.type != BT_LOGICAL
0714f1
-		  || code->expr1->rank != 0))
0714f1
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
0714f1
-		       &code->expr1->where);
0714f1
+	  if (t && code->expr1 != NULL)
0714f1
+	    {
0714f1
+	      if (flag_dec_non_logical_if
0714f1
+		  && code->expr1->ts.type != BT_LOGICAL)
0714f1
+		{
0714f1
+		  gfc_expr* cast;
0714f1
+		  cast = gfc_ne (code->expr1,
0714f1
+				 gfc_get_int_expr (1, &gfc_current_locus, 0),
0714f1
+				 INTRINSIC_NE);
0714f1
+		  if (cast == NULL)
0714f1
+		    gfc_internal_error ("gfc_resolve_code(): Failed to cast "
0714f1
+					"to LOGICAL in IF");
0714f1
+		  code->expr1 = cast;
0714f1
+                  if (warn_conversion_extra)
0714f1
+		    {
0714f1
+		      gfc_warning (OPT_Wconversion_extra, "Non-LOGICAL type in"
0714f1
+				   " IF statement condition %L will be true if"
0714f1
+				   " it evaluates to nonzero",
0714f1
+				   &code->expr1->where);
0714f1
+		    }
0714f1
+		}
0714f1
+
0714f1
+	      if (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank != 0)
0714f1
+		gfc_error ("IF clause at %L requires a scalar LOGICAL "
0714f1
+			   "expression", &code->expr1->where);
0714f1
+	    }
0714f1
 	  break;
0714f1
 
0714f1
 	case EXEC_CALL:
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f
0714f1
new file mode 100644
0714f1
index 00000000000..0101db893ca
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f
0714f1
@@ -0,0 +1,25 @@
0714f1
+! { dg-do run }
0714f1
+! { dg-options "-fdec -Wconversion-extra" }
0714f1
+!
0714f1
+! Allow logical expressions in if statements and blocks
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+        PROGRAM logical_exp_if_st_bl
0714f1
+          INTEGER ipos/1/
0714f1
+          INTEGER ineg/0/
0714f1
+
0714f1
+          ! Test non logical variables
0714f1
+          if (ineg) STOP 1 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+          if (0) STOP 2 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+
0714f1
+          ! Test non logical expressions in if statements
0714f1
+          if (MOD(ipos, 1)) STOP 3 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+
0714f1
+          ! Test non logical expressions in if blocks
0714f1
+          if (MOD(2 * ipos, 2)) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+            STOP 4
0714f1
+          endif
0714f1
+        END
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f
0714f1
new file mode 100644
0714f1
index 00000000000..876f4e09508
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f
0714f1
@@ -0,0 +1,25 @@
0714f1
+! { dg-do run }
0714f1
+! { dg-options "-fdec-non-logical-if -Wconversion-extra" }
0714f1
+!
0714f1
+! Allow logical expressions in if statements and blocks
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+        PROGRAM logical_exp_if_st_bl
0714f1
+          INTEGER ipos/1/
0714f1
+          INTEGER ineg/0/
0714f1
+
0714f1
+          ! Test non logical variables
0714f1
+          if (ineg) STOP 1 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+          if (0) STOP 2 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+
0714f1
+          ! Test non logical expressions in if statements
0714f1
+          if (MOD(ipos, 1)) STOP 3 ! { dg-warning "if it evaluates to nonzero" }
0714f1
+
0714f1
+          ! Test non logical expressions in if blocks
0714f1
+          if (MOD(2 * ipos, 2)) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+            STOP 4
0714f1
+          endif
0714f1
+        END
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f
0714f1
new file mode 100644
0714f1
index 00000000000..35cb4c51b8d
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f
0714f1
@@ -0,0 +1,25 @@
0714f1
+! { dg-do compile }
0714f1
+! { dg-options "-fdec -fno-dec-non-logical-if" }
0714f1
+!
0714f1
+! Allow logical expressions in if statements and blocks
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+        PROGRAM logical_exp_if_st_bl
0714f1
+          INTEGER ipos/1/
0714f1
+          INTEGER ineg/0/
0714f1
+
0714f1
+          ! Test non logical variables
0714f1
+          if (ineg) STOP 1 ! { dg-error "IF clause at" }
0714f1
+          if (0) STOP 2 ! { dg-error "IF clause at" }
0714f1
+
0714f1
+          ! Test non logical expressions in if statements
0714f1
+          if (MOD(ipos, 1)) STOP 3 ! { dg-error "IF clause at" }
0714f1
+
0714f1
+          ! Test non logical expressions in if blocks
0714f1
+          if (MOD(2 * ipos, 2)) then ! { dg-error "IF clause at" }
0714f1
+            STOP 4
0714f1
+          endif
0714f1
+        END
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f
0714f1
new file mode 100644
0714f1
index 00000000000..7b60b60827f
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f
0714f1
@@ -0,0 +1,45 @@
0714f1
+! { dg-do run }
0714f1
+! { dg-options "-fdec -Wconversion-extra" }
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+       function othersub1()
0714f1
+        integer*4 othersub1
0714f1
+        othersub1 = 9
0714f1
+       end
0714f1
+
0714f1
+       function othersub2()
0714f1
+        integer*4 othersub2
0714f1
+        othersub2 = 0
0714f1
+       end
0714f1
+
0714f1
+       program MAIN
0714f1
+        integer*4 othersub1
0714f1
+        integer*4 othersub2
0714f1
+        integer a /1/
0714f1
+        integer b /2/        
0714f1
+ 
0714f1
+        if (othersub1()) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 1
0714f1
+        end if
0714f1
+        if (othersub2()) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+           stop 2
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+        if (a-b) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 3
0714f1
+        end if
0714f1
+        if (b-(a+1)) then ! { dg-warning "if it evaluates to nonzero" }
0714f1
+           stop 3
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+       end
0714f1
+
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f
0714f1
new file mode 100644
0714f1
index 00000000000..80336f48ca1
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f
0714f1
@@ -0,0 +1,45 @@
0714f1
+! { dg-do run }
0714f1
+! { dg-options "-fdec-non-logical-if -Wconversion-extra" }
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+       function othersub1()
0714f1
+        integer*4 othersub1
0714f1
+        othersub1 = 9
0714f1
+       end
0714f1
+
0714f1
+       function othersub2()
0714f1
+        integer*4 othersub2
0714f1
+        othersub2 = 0
0714f1
+       end
0714f1
+
0714f1
+       program MAIN
0714f1
+        integer*4 othersub1
0714f1
+        integer*4 othersub2
0714f1
+        integer a /1/
0714f1
+        integer b /2/        
0714f1
+ 
0714f1
+        if (othersub1()) then ! { dg-warning "Non-LOGICAL type in IF statement" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 1
0714f1
+        end if
0714f1
+        if (othersub2()) then ! { dg-warning "Non-LOGICAL type in IF statement" }
0714f1
+           stop 2
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+        if (a-b) then ! { dg-warning "Non-LOGICAL type in IF statement" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 3
0714f1
+        end if
0714f1
+        if (b-(a+1)) then ! { dg-warning "Non-LOGICAL type in IF statement" }
0714f1
+           stop 3
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+       end
0714f1
+
0714f1
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f
0714f1
new file mode 100644
0714f1
index 00000000000..e1125ca717a
0714f1
--- /dev/null
0714f1
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f
0714f1
@@ -0,0 +1,45 @@
0714f1
+! { dg-do compile }
0714f1
+! { dg-options "-fdec -fno-dec-non-logical-if" }
0714f1
+!
0714f1
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
0714f1
+!             and Jeff Law <law@redhat.com>
0714f1
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
0714f1
+!
0714f1
+       function othersub1()
0714f1
+        integer*4 othersub1
0714f1
+        othersub1 = 9
0714f1
+       end
0714f1
+
0714f1
+       function othersub2()
0714f1
+        integer*4 othersub2
0714f1
+        othersub2 = 0
0714f1
+       end
0714f1
+
0714f1
+       program MAIN
0714f1
+        integer*4 othersub1
0714f1
+        integer*4 othersub2
0714f1
+        integer a /1/
0714f1
+        integer b /2/        
0714f1
+ 
0714f1
+        if (othersub1()) then ! { dg-error "IF clause at" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 1
0714f1
+        end if
0714f1
+        if (othersub2()) then ! { dg-error "IF clause at" }
0714f1
+           stop 2
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+        if (a-b) then ! { dg-error "IF clause at" }
0714f1
+           write(*,*) "OK"
0714f1
+        else
0714f1
+           stop 3
0714f1
+        end if
0714f1
+        if (b-(a+1)) then ! { dg-error "IF clause at" }
0714f1
+           stop 3
0714f1
+        else
0714f1
+           write(*,*) "OK"
0714f1
+        end if
0714f1
+       end
0714f1
+
0714f1
-- 
0714f1
2.11.0
0714f1