Blame SOURCES/gcc11-fortran-fdec-non-logical-if.patch

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