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

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