This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[GSoC] use obstack in parse_c_expr


Hi,
   There was a comment in parse_c_expr, mentioning to use obstack to
build c-code string. I have attached patch for the same.
OK to commit ?

* genmatch.c (parse_c_expr): Use obstack to build c code string.

Thanks and Regards,
Prathamesh
Index: gcc/genmatch.c
===================================================================
--- gcc/genmatch.c	(revision 209470)
+++ gcc/genmatch.c	(working copy)
@@ -689,15 +689,18 @@ parse_c_expr (cpp_reader *r, cpp_ttype s
   cpp_ttype end;
   unsigned opencnt;
   char *code;
+  struct obstack ob;
+ 
+  obstack_init (&ob);
   eat_token (r, start);
   if (start == CPP_OPEN_PAREN)
     {
-      code = xstrdup ("(");
+      obstack_grow (&ob, "(", 1);
       end = CPP_CLOSE_PAREN;
     }
   else if (start == CPP_OPEN_BRACE)
     {
-      code = xstrdup ("({");
+      obstack_grow (&ob, "({", 2);
       end = CPP_CLOSE_BRACE;
     }
   else
@@ -711,16 +714,16 @@ parse_c_expr (cpp_reader *r, cpp_ttype s
       if (token->type == CPP_ATSIGN)
 	{
 	  const cpp_token *n = peek (r);
+	  const char *number;
 	  if (n->type == CPP_NUMBER
 	      && !(n->flags & PREV_WHITE))
 	    {
-	      code = (char *)xrealloc (code, strlen (code)
-				       + strlen ("captures[") + 4);
 	      if (token->flags & PREV_WHITE)
-		strcat (code, " ");
-	      strcat (code, "captures[");
-	      strcat (code, get_number (r));
-	      strcat (code, "]");
+		obstack_grow (&ob, " ", 1);
+	      number = get_number (r);
+	      obstack_grow (&ob, "captures[", strlen ("captures["));
+	      obstack_grow (&ob, number, strlen (number)); 
+	      obstack_grow (&ob, "]", 1);
 	      continue;
 	    }
 	}
@@ -734,24 +737,19 @@ parse_c_expr (cpp_reader *r, cpp_ttype s
 
       /* Output the token as string.  */
       char *tk = (char *)cpp_token_as_text (r, token);
-      code = (char *)xrealloc (code, strlen (code) + strlen (tk) + 2);
       if (token->flags & PREV_WHITE)
-	strcat (code, " ");
-      strcat (code, tk);
+	obstack_grow (&ob, " ", 1);	
+      obstack_grow (&ob, tk, strlen (tk));
     }
   while (1);
   if (end == CPP_CLOSE_PAREN)
-    {
-      code = (char *)xrealloc (code, strlen (code) + 1 + 1);
-      strcat (code, ")");
-    }
+    obstack_grow0 (&ob, ")", 1);
   else if (end == CPP_CLOSE_BRACE)
-    {
-      code = (char *)xrealloc (code, strlen (code) + 1 + 2);
-      strcat (code, "})");
-    }
+    obstack_grow0 (&ob, "})", 2);
   else
     gcc_unreachable ();
+
+  code = (char *) obstack_finish (&ob);
   return new c_expr (code);
 }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]