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

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