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

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