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

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