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

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