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

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