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

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