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

840d93
From 99c791361468b61976d6054e1ec1c81fe43e6559 Mon Sep 17 00:00:00 2001
840d93
From: Jim MacArthur <jim.macarthur@codethink.co.uk>
840d93
Date: Wed, 11 Nov 2015 15:37:00 +0000
840d93
Subject: [PATCH 14/23] Allow non-logical expressions in IF statements
840d93
840d93
This feature is enabled by the `-std=extra-legacy` compiler flag.
840d93
---
840d93
840d93
    0014-Allow-non-logical-expressions-in-IF-statements.patch
840d93
840d93
    Allow non-logical expressions in IF statements
840d93
    
840d93
    This feature is enabled by the `-std=extra-legacy` compiler flag.
840d93
    
840d93
    Signed-off-by: Ben Brewer <ben.brewer@codethink.co.uk>
840d93
    Signed-off-by: Francisco Redondo Marchena <francisco.marchena@codethink.co.uk>
840d93
840d93
diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
840d93
index 33b441aa1bc..f979915e856 100644
840d93
--- a/gcc/fortran/resolve.c
840d93
+++ b/gcc/fortran/resolve.c
840d93
@@ -9919,10 +9919,23 @@ gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
840d93
       switch (b->op)
840d93
 	{
840d93
 	case EXEC_IF:
840d93
-	  if (t && b->expr1 != NULL
840d93
-	      && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
840d93
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
840d93
-		       &b->expr1->where);
840d93
+	  if (t && b->expr1 != NULL)
840d93
+	    {
840d93
+	      if (gfc_option.allow_std & GFC_STD_EXTRA_LEGACY && b->expr1->ts.type != BT_LOGICAL)
840d93
+		{
840d93
+		  gfc_expr* cast;
840d93
+		  cast = gfc_ne (b->expr1, gfc_get_int_expr (1, &gfc_current_locus, 0), INTRINSIC_NE);
840d93
+		  if (cast == NULL)
840d93
+		    gfc_internal_error ("gfc_resolve_blocks(): Failed to cast to LOGICAL in IF");
840d93
+		  b->expr1 = cast;
840d93
+		  gfc_warning (0, "Non-LOGICAL type in IF statement condition %L"
840d93
+			       " will be true if it evaluates to nonzero", &b->expr1->where);
840d93
+		}
840d93
+
840d93
+	      if ((b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
840d93
+		gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
840d93
+			   &b->expr1->where);
840d93
+	    }
840d93
 	  break;
840d93
 
840d93
 	case EXEC_WHERE:
840d93
@@ -11182,11 +11195,23 @@ start:
840d93
 	  break;
840d93
 
840d93
 	case EXEC_IF:
840d93
-	  if (t && code->expr1 != NULL
840d93
-	      && (code->expr1->ts.type != BT_LOGICAL
840d93
-		  || code->expr1->rank != 0))
840d93
-	    gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
840d93
-		       &code->expr1->where);
840d93
+	  if (t && code->expr1 != NULL)
840d93
+            {
840d93
+	      if (gfc_option.allow_std & GFC_STD_EXTRA_LEGACY && code->expr1->ts.type != BT_LOGICAL)
840d93
+		{
840d93
+		  gfc_expr* cast;
840d93
+		  cast = gfc_ne (code->expr1, gfc_get_int_expr (1, &gfc_current_locus, 0), INTRINSIC_NE);
840d93
+		  if (cast == NULL)
840d93
+		    gfc_internal_error ("gfc_resolve_code(): Failed to cast to LOGICAL in IF");
840d93
+		  code->expr1 = cast;
840d93
+		  gfc_warning (0, "Non-LOGICAL type in IF statement condition %L"
840d93
+			       " will be true if it evaluates to nonzero", &code->expr1->where);
840d93
+		}
840d93
+
840d93
+	      if ((code->expr1->ts.type != BT_LOGICAL || code->expr1->rank != 0))
840d93
+		gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
840d93
+			   &code->expr1->where);
840d93
+	    }
840d93
 	  break;
840d93
 
840d93
 	case EXEC_CALL:
840d93
diff --git a/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks.f b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks.f
840d93
new file mode 100644
840d93
index 00000000000..ad23fcfc9af
840d93
--- /dev/null
840d93
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks.f
840d93
@@ -0,0 +1,21 @@
840d93
+! { dg-do compile }
840d93
+! { dg-options "-std=extra-legacy" }
840d93
+!
840d93
+! Allow logical expressions in if statements and blocks
840d93
+!
840d93
+        PROGRAM logical_exp_if_st_bl
840d93
+          INTEGER ipos/1/
840d93
+          INTEGER ineg/0/
840d93
+
840d93
+          ! Test non logical variables
840d93
+          if (ineg) STOP 1 ! { dg-warning "if it evaluates to nonzero" }
840d93
+          if (0) STOP 2 ! { dg-warning "if it evaluates to nonzero" }
840d93
+
840d93
+          ! Test non logical expressions in if statements
840d93
+          if (MOD(ipos, 1)) STOP 3 ! { dg-warning "if it evaluates to nonzero" }
840d93
+
840d93
+          ! Test non logical expressions in if blocks
840d93
+          if (MOD(2 * ipos, 2)) then ! { dg-warning "if it evaluates to nonzero" }
840d93
+            STOP 4
840d93
+          endif
840d93
+        END
840d93
commit cf72338b9468fad669b60600bcce7918a8d4591e
840d93
Author: Jeff Law <law@redhat.com>
840d93
Date:   Tue Jun 5 15:45:41 2018 -0600
840d93
840d93
    Additional test for
840d93
    
840d93
        0014-Allow-non-logical-expressions-in-IF-statements.patch
840d93
        "Allow non-logical expressions in IF statements"
840d93
840d93
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
840d93
new file mode 100644
840d93
index 00000000000..7da6aaceec7
840d93
--- /dev/null
840d93
+++ b/gcc/testsuite/gfortran.dg/dec_logical_expressions_if_statements_blocks-2.f
840d93
@@ -0,0 +1,23 @@
840d93
+! { dg-do compile }
840d93
+! { dg-options "-std=extra-legacy" }
840d93
+
840d93
+       function othersub1()
840d93
+        integer*4 othersub1
840d93
+        othersub1 = 1
840d93
+       end
840d93
+       function othersub2()
840d93
+        integer*4 othersub2
840d93
+        othersub2 = 2
840d93
+       end
840d93
+       program MAIN
840d93
+        integer*4 othersub1
840d93
+        integer*4 othersub2
840d93
+c the if (integer) works here 
840d93
+        if (othersub2()) then		! { dg-warning "" }
840d93
+         write (*,*), 'othersub2 is true'
840d93
+c but fails in the "else if"
840d93
+        else if (othersub1()) then	! { dg-warning "" }
840d93
+         write (*,*), 'othersub2 is false, othersub1 is true'
840d93
+        endif
840d93
+       end
840d93
+