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

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