[ast-optimizer-branch]: Fix for incorrect scope building

Diego Novillo dnovillo@redhat.com
Mon May 6 06:49:00 GMT 2002


On Sat, 04 May 2002, Daniel Berlin wrote:

> For 
> if (a)
> win:
> return 1;
> 
> we build
> 
> if (a)
> {
> 	win:;
> }
> and lose the return 1.
> 
Yup.  The patch is fine, but I re-wrote tree_build_scope() on
friday, so it's fixed already.

My local tree is a mess right now, so I'm not ready to commit
anything yet.  If you want, apply this to your local
tree:


Index: c-simplify.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/Attic/c-simplify.c,v
retrieving revision 1.1.2.15
diff -d -p -d -u -p -r1.1.2.15 c-simplify.c
--- c-simplify.c	2 May 2002 19:42:00 -0000	1.1.2.15
+++ c-simplify.c	6 May 2002 13:38:50 -0000
@@ -1863,56 +1875,42 @@ void
 tree_build_scope (t)
      tree *t;
 {
-  if (*t == NULL_TREE)
-    {
-      /* Construct a compound statement and a scope.  */
-      tree body, start_scope, end_scope;
+  tree comp_stmt, start_scope, end_scope;
 
-      body = make_node (COMPOUND_STMT);
-      start_scope = make_node (SCOPE_STMT);
-      end_scope = make_node (SCOPE_STMT);
-      SCOPE_BEGIN_P (start_scope) = 1;
-      SCOPE_BEGIN_P (end_scope) = 0;
-      COMPOUND_BODY (body) = start_scope;
-      /* Construct an empty body.  */
-      TREE_CHAIN (start_scope) = end_scope;
-      /* Replace the node by the constructed body.  */
-      *t = body;
-      return;
-    }
+  /* If T already has a proper scope, do nothing.  */
+  if (*t
+      && TREE_CODE (*t) == COMPOUND_STMT
+      && COMPOUND_BODY (*t))
+    return;
 
-  if (TREE_CODE (*t) == COMPOUND_STMT)
+  /* Create a new empty scope.  */
+  comp_stmt = make_node (COMPOUND_STMT);
+
+  start_scope = make_node (SCOPE_STMT);
+  SCOPE_BEGIN_P (start_scope) = 1;
+
+  end_scope = make_node (SCOPE_STMT);
+  SCOPE_BEGIN_P (end_scope) = 0;
+
+  COMPOUND_BODY (comp_stmt) = start_scope;
+
+  if (*t)
     {
-      if (COMPOUND_BODY (*t) == NULL_TREE)
-	{
-	  /* There's a compound statement, but no scope.  */
-	  tree start_scope, end_scope;
-	  start_scope = make_node (SCOPE_STMT);
-	  end_scope = make_node (SCOPE_STMT);
-	  SCOPE_BEGIN_P (start_scope) = 1;
-	  SCOPE_BEGIN_P (end_scope) = 0;
-	  COMPOUND_BODY (*t) = start_scope;
-	  TREE_CHAIN (start_scope) = end_scope;
-	}
-      else
-	/* The given NODE is actually a scope.  */
-	return;
+      /* If T is not empty, insert it inside the newly created scope.  Note
+	 that we can't just join TREE_CHAIN(*T) to the closing scope
+	 because even if T wasn't inside a scope, it might be a list of
+	 statements.  */
+      TREE_CHAIN (start_scope) = *t;
+      chainon (*t, end_scope);
     }
   else
     {
-      /* Construct a compound statement and a scope.  */
-      tree body, start_scope, end_scope;
-
-      body = make_node (COMPOUND_STMT);
-      start_scope = make_node (SCOPE_STMT);
-      end_scope = make_node (SCOPE_STMT);
-      SCOPE_BEGIN_P (start_scope) = 1;
-      SCOPE_BEGIN_P (end_scope) = 0;
-      COMPOUND_BODY (body) = start_scope;
-      TREE_CHAIN (start_scope) = *t;
-      TREE_CHAIN (*t) = end_scope;
-      *t = body;
+      /* T is empty.  Simply join the start/end nodes.  */
+      TREE_CHAIN (start_scope) = end_scope;
     }
+
+  /* Set T to the newly constructed scope.  */
+  *t = comp_stmt;
 }
 
 /* }}} */



More information about the Gcc-patches mailing list