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

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