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

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