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

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