Blame SOURCES/0011-Allow-non-logical-expressions-in-IF-statements.patch

9805c9
From f6197d0e59059a172f68a697e25cd585ad158937 Mon Sep 17 00:00:00 2001
9805c9
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
9805c9
Date: Wed, 11 Nov 2015 15:37:00 +0000
9805c9
Subject: [PATCH 11/16] Allow non-logical expressions in IF statements
9805c9
9805c9
Use -fdec-non-logical-if to enable feature. Also enabled using -fdec.
9805c9
---
9805c9
 gcc/fortran/lang.opt                               |  4 ++
9805c9
 gcc/fortran/options.c                              |  1 +
9805c9
 gcc/fortran/resolve.c                              | 60 ++++++++++++++++++----
9805c9
 ...ec_logical_expressions_if_statements_blocks_1.f | 25 +++++++++
9805c9
 ...ec_logical_expressions_if_statements_blocks_2.f | 25 +++++++++
9805c9
 ...ec_logical_expressions_if_statements_blocks_3.f | 25 +++++++++
9805c9
 ...ec_logical_expressions_if_statements_blocks_4.f | 45 ++++++++++++++++
9805c9
 ...ec_logical_expressions_if_statements_blocks_5.f | 45 ++++++++++++++++
9805c9
 ...ec_logical_expressions_if_statements_blocks_6.f | 45 ++++++++++++++++
9805c9
 9 files changed, 266 insertions(+), 9 deletions(-)
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f
9805c9
 create mode 100644 gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f
9805c9
9805c9
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
9805c9
index 38d31e620bf..fa2851ae837 100644
9805c9
--- a/gcc/fortran/lang.opt
9805c9
+++ b/gcc/fortran/lang.opt
9805c9
@@ -482,6 +482,10 @@ fdec-override-kind
9805c9
 Fortran Var(flag_dec_override_kind)
9805c9
 Enable support for per variable kind specification.
9805c9
 
9805c9
+fdec-non-logical-if
9805c9
+Fortran Var(flag_dec_non_logical_if)
9805c9
+Enable support for non-logical expressions in if statements.
9805c9
+
9805c9
 fdec-old-init
9805c9
 Fortran Var(flag_dec_old_init)
9805c9
 Enable support for old style initializers in derived types.
9805c9
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
9805c9
index 720fd25b570..7b04a681f7b 100644
9805c9
--- a/gcc/fortran/options.c
9805c9
+++ b/gcc/fortran/options.c
9805c9
@@ -82,6 +82,7 @@ set_dec_flags (int value)
9805c9
   SET_BITFLAG (flag_dec_non_integer_index, value, value);
9805c9
   SET_BITFLAG (flag_dec_old_init, value, value);
9805c9
   SET_BITFLAG (flag_dec_override_kind, value, value);
9805c9
+  SET_BITFLAG (flag_dec_non_logical_if, value, value);
9805c9
 }
9805c9
 
9805c9
 /* Finalize DEC flags.  */
9805c9
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
9805c9
index 04679d3a15d..a90f7f849b5 100644
9805c9
--- a/gcc/fortran/resolve.c
9805c9
+++ b/gcc/fortran/resolve.c
9805c9
@@ -10398,10 +10398,31 @@ gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
9805c9
       switch (b->op)
9805c9
 	{
9805c9
 	case EXEC_IF:
9805c9
-	  if (t && b->expr1 != NULL
9805c9
-	      && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9805c9
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9805c9
-		       &b->expr1->where);
9805c9
+	  if (t && b->expr1 != NULL)
9805c9
+	    {
9805c9
+	      if (flag_dec_non_logical_if && b->expr1->ts.type != BT_LOGICAL)
9805c9
+		{
9805c9
+		  gfc_expr* cast;
9805c9
+		  cast = gfc_ne (b->expr1,
9805c9
+				 gfc_get_int_expr (1, &gfc_current_locus, 0),
9805c9
+				 INTRINSIC_NE);
9805c9
+		  if (cast == NULL)
9805c9
+		    gfc_internal_error ("gfc_resolve_blocks(): Failed to cast "
9805c9
+					"to LOGICAL in IF");
9805c9
+		  b->expr1 = cast;
9805c9
+                  if (warn_conversion_extra)
9805c9
+		    {
9805c9
+		      gfc_warning (OPT_Wconversion_extra, "Non-LOGICAL type in"
9805c9
+				   " IF statement condition %L will be true if"
9805c9
+				   " it evaluates to nonzero",
9805c9
+				   &b->expr1->where);
9805c9
+		    }
9805c9
+		}
9805c9
+
9805c9
+	      if ((b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
9805c9
+		gfc_error ("IF clause at %L requires a scalar LOGICAL "
9805c9
+			   "expression", &b->expr1->where);
9805c9
+	    }
9805c9
 	  break;
9805c9
 
9805c9
 	case EXEC_WHERE:
9805c9
@@ -11690,11 +11711,32 @@ start:
9805c9
 	  break;
9805c9
 
9805c9
 	case EXEC_IF:
9805c9
-	  if (t && code->expr1 != NULL
9805c9
-	      && (code->expr1->ts.type != BT_LOGICAL
9805c9
-		  || code->expr1->rank != 0))
9805c9
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
9805c9
-		       &code->expr1->where);
9805c9
+	  if (t && code->expr1 != NULL)
9805c9
+	    {
9805c9
+	      if (flag_dec_non_logical_if
9805c9
+		  && code->expr1->ts.type != BT_LOGICAL)
9805c9
+		{
9805c9
+		  gfc_expr* cast;
9805c9
+		  cast = gfc_ne (code->expr1,
9805c9
+				 gfc_get_int_expr (1, &gfc_current_locus, 0),
9805c9
+				 INTRINSIC_NE);
9805c9
+		  if (cast == NULL)
9805c9
+		    gfc_internal_error ("gfc_resolve_code(): Failed to cast "
9805c9
+					"to LOGICAL in IF");
9805c9
+		  code->expr1 = cast;
9805c9
+                  if (warn_conversion_extra)
9805c9
+		    {
9805c9
+		      gfc_warning (OPT_Wconversion_extra, "Non-LOGICAL type in"
9805c9
+				   " IF statement condition %L will be true if"
9805c9
+				   " it evaluates to nonzero",
9805c9
+				   &code->expr1->where);
9805c9
+		    }
9805c9
+		}
9805c9
+
9805c9
+	      if (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank != 0)
9805c9
+		gfc_error ("IF clause at %L requires a scalar LOGICAL "
9805c9
+			   "expression", &code->expr1->where);
9805c9
+	    }
9805c9
 	  break;
9805c9
 
9805c9
 	case EXEC_CALL:
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..0101db893ca
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_1.f
9805c9
@@ -0,0 +1,25 @@
9805c9
+! { dg-do run }
9805c9
+! { dg-options "-fdec -Wconversion-extra" }
9805c9
+!
9805c9
+! Allow logical expressions in if statements and blocks
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+        PROGRAM logical_exp_if_st_bl
9805c9
+          INTEGER ipos/1/
9805c9
+          INTEGER ineg/0/
9805c9
+
9805c9
+          ! Test non logical variables
9805c9
+          if (ineg) STOP 1 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+          if (0) STOP 2 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+
9805c9
+          ! Test non logical expressions in if statements
9805c9
+          if (MOD(ipos, 1)) STOP 3 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+
9805c9
+          ! Test non logical expressions in if blocks
9805c9
+          if (MOD(2 * ipos, 2)) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+            STOP 4
9805c9
+          endif
9805c9
+        END
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..876f4e09508
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_2.f
9805c9
@@ -0,0 +1,25 @@
9805c9
+! { dg-do run }
9805c9
+! { dg-options "-fdec-non-logical-if -Wconversion-extra" }
9805c9
+!
9805c9
+! Allow logical expressions in if statements and blocks
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+        PROGRAM logical_exp_if_st_bl
9805c9
+          INTEGER ipos/1/
9805c9
+          INTEGER ineg/0/
9805c9
+
9805c9
+          ! Test non logical variables
9805c9
+          if (ineg) STOP 1 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+          if (0) STOP 2 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+
9805c9
+          ! Test non logical expressions in if statements
9805c9
+          if (MOD(ipos, 1)) STOP 3 ! { dg-warning "if it evaluates to nonzero" }
9805c9
+
9805c9
+          ! Test non logical expressions in if blocks
9805c9
+          if (MOD(2 * ipos, 2)) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+            STOP 4
9805c9
+          endif
9805c9
+        END
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..35cb4c51b8d
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_3.f
9805c9
@@ -0,0 +1,25 @@
9805c9
+! { dg-do compile }
9805c9
+! { dg-options "-fdec -fno-dec-non-logical-if" }
9805c9
+!
9805c9
+! Allow logical expressions in if statements and blocks
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+        PROGRAM logical_exp_if_st_bl
9805c9
+          INTEGER ipos/1/
9805c9
+          INTEGER ineg/0/
9805c9
+
9805c9
+          ! Test non logical variables
9805c9
+          if (ineg) STOP 1 ! { dg-error "IF clause at" }
9805c9
+          if (0) STOP 2 ! { dg-error "IF clause at" }
9805c9
+
9805c9
+          ! Test non logical expressions in if statements
9805c9
+          if (MOD(ipos, 1)) STOP 3 ! { dg-error "IF clause at" }
9805c9
+
9805c9
+          ! Test non logical expressions in if blocks
9805c9
+          if (MOD(2 * ipos, 2)) then ! { dg-error "IF clause at" }
9805c9
+            STOP 4
9805c9
+          endif
9805c9
+        END
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..7b60b60827f
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_4.f
9805c9
@@ -0,0 +1,45 @@
9805c9
+! { dg-do run }
9805c9
+! { dg-options "-fdec -Wconversion-extra" }
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+       function othersub1()
9805c9
+        integer*4 othersub1
9805c9
+        othersub1 = 9
9805c9
+       end
9805c9
+
9805c9
+       function othersub2()
9805c9
+        integer*4 othersub2
9805c9
+        othersub2 = 0
9805c9
+       end
9805c9
+
9805c9
+       program MAIN
9805c9
+        integer*4 othersub1
9805c9
+        integer*4 othersub2
9805c9
+        integer a /1/
9805c9
+        integer b /2/        
9805c9
+ 
9805c9
+        if (othersub1()) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 1
9805c9
+        end if
9805c9
+        if (othersub2()) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+           stop 2
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+        if (a-b) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 3
9805c9
+        end if
9805c9
+        if (b-(a+1)) then ! { dg-warning "if it evaluates to nonzero" }
9805c9
+           stop 3
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+       end
9805c9
+
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..80336f48ca1
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_5.f
9805c9
@@ -0,0 +1,45 @@
9805c9
+! { dg-do run }
9805c9
+! { dg-options "-fdec-non-logical-if -Wconversion-extra" }
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+       function othersub1()
9805c9
+        integer*4 othersub1
9805c9
+        othersub1 = 9
9805c9
+       end
9805c9
+
9805c9
+       function othersub2()
9805c9
+        integer*4 othersub2
9805c9
+        othersub2 = 0
9805c9
+       end
9805c9
+
9805c9
+       program MAIN
9805c9
+        integer*4 othersub1
9805c9
+        integer*4 othersub2
9805c9
+        integer a /1/
9805c9
+        integer b /2/        
9805c9
+ 
9805c9
+        if (othersub1()) then ! { dg-warning "Non-LOGICAL type in IF statement" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 1
9805c9
+        end if
9805c9
+        if (othersub2()) then ! { dg-warning "Non-LOGICAL type in IF statement" }
9805c9
+           stop 2
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+        if (a-b) then ! { dg-warning "Non-LOGICAL type in IF statement" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 3
9805c9
+        end if
9805c9
+        if (b-(a+1)) then ! { dg-warning "Non-LOGICAL type in IF statement" }
9805c9
+           stop 3
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+       end
9805c9
+
9805c9
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
9805c9
new file mode 100644
9805c9
index 00000000000..e1125ca717a
9805c9
--- /dev/null
9805c9
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks_6.f
9805c9
@@ -0,0 +1,45 @@
9805c9
+! { dg-do compile }
9805c9
+! { dg-options "-fdec -fno-dec-non-logical-if" }
9805c9
+!
9805c9
+! Contributed by Francisco Redondo Marchena <francisco.marchema@codethink.co.uk>
9805c9
+!             and Jeff Law <law@redhat.com>
9805c9
+! Modified by Mark Eggleston <mark.eggleston@codethink.com>
9805c9
+!
9805c9
+       function othersub1()
9805c9
+        integer*4 othersub1
9805c9
+        othersub1 = 9
9805c9
+       end
9805c9
+
9805c9
+       function othersub2()
9805c9
+        integer*4 othersub2
9805c9
+        othersub2 = 0
9805c9
+       end
9805c9
+
9805c9
+       program MAIN
9805c9
+        integer*4 othersub1
9805c9
+        integer*4 othersub2
9805c9
+        integer a /1/
9805c9
+        integer b /2/        
9805c9
+ 
9805c9
+        if (othersub1()) then ! { dg-error "IF clause at" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 1
9805c9
+        end if
9805c9
+        if (othersub2()) then ! { dg-error "IF clause at" }
9805c9
+           stop 2
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+        if (a-b) then ! { dg-error "IF clause at" }
9805c9
+           write(*,*) "OK"
9805c9
+        else
9805c9
+           stop 3
9805c9
+        end if
9805c9
+        if (b-(a+1)) then ! { dg-error "IF clause at" }
9805c9
+           stop 3
9805c9
+        else
9805c9
+           write(*,*) "OK"
9805c9
+        end if
9805c9
+       end
9805c9
+
9805c9
-- 
9805c9
2.11.0
9805c9