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

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