public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch preprocessor]: PR/45362 Dangling reference
@ 2010-09-17 14:00 Kai Tietz
  2010-09-22  3:10 ` Tom Tromey
  0 siblings, 1 reply; 15+ messages in thread
From: Kai Tietz @ 2010-09-17 14:00 UTC (permalink / raw)
  To: GCC Patches; +Cc: Jakub Jelinek, Andrew Pinski, Richard Henderson

[-- Attachment #1: Type: text/plain, Size: 1059 bytes --]

Hello,

I reworked implementation of push_macro/pop_macro by this patch that
it doesn't re-use possible free'ed elements and uses instead textual
representations of saved macro defintion. I ran specific test
especially to check that pch still works.

ChangeLog libcpp

2010-09-17  Kai Tietz

	PR preprocessor/45362
	* directives.c (cpp_pop_definition): Make static.
	(do_pragma_push_macro): Reworked to store text
	definition.
	(do_pragma_pop_macro): Add free text definition.
	(cpp_push_definition): Removed.		
	* include/cpplib.h (cpp_push_definition): Removed.
	(cpp_pop_definition): Likewise.
	* internal.h (ef_pragma_macro): Remove member 'value'
	and add new member 'definition'.
	* pch.c (_cpp_restore_pushed_macros): Rework to work
	on text definition.
	(_cpp_save_pushed_macros): Likewise.

Tested for x86_64-pc-linux-gnu, x86_64-w64-mingw32, and
i686-pc-cygwin. Ok for apply?

Regards,
Kai

-- 
|  (\_/) This is Bunny. Copy and paste
| (='.'=) Bunny into your signature to help
| (")_(") him gain world domination

[-- Attachment #2: fixdangle.diff --]
[-- Type: application/octet-stream, Size: 7426 bytes --]

Index: libcpp/directives.c
===================================================================
--- libcpp.orig/directives.c	2010-09-09 16:24:14.000000000 +0200
+++ libcpp/directives.c	2010-09-17 13:41:41.561691000 +0200
@@ -128,6 +128,7 @@
 static void handle_assertion (cpp_reader *, const char *, int);
 static void do_pragma_push_macro (cpp_reader *);
 static void do_pragma_pop_macro (cpp_reader *);
+static void cpp_pop_definition (cpp_reader *, const char *, const uchar *);
 
 /* This is the table of directive handlers.  It is ordered by
    frequency of occurrence; the numbers at the end are directive
@@ -1436,6 +1437,9 @@
 static void
 do_pragma_push_macro (cpp_reader *pfile)
 {
+  cpp_hashnode *node;
+  size_t defnlen;
+  const uchar *defn = NULL;
   char *macroname, *dest;
   const char *limit, *src;
   const cpp_token *txt;
@@ -1468,7 +1472,26 @@
   c->name = XNEWVAR (char, strlen (macroname) + 1);
   strcpy (c->name, macroname);
   c->next = pfile->pushed_macros;
-  c->value = cpp_push_definition (pfile, c->name);
+  node = _cpp_lex_identifier (pfile, c->name);
+  if (node->type == NT_VOID)
+    defnlen = 0;
+  else
+    {
+      defn = cpp_macro_definition (pfile, node);
+      defnlen = ustrlen (defn);
+    }
+  c->definition = XNEWVEC (uchar, defnlen + 2);
+  if (node->type == NT_VOID)
+    {
+      c->definition[0] = 0;
+    }
+  else
+    {
+      c->definition[defnlen] = '\n';
+      c->definition[defnlen + 1] = 0;
+      memcpy (c->definition, defn, sizeof (uchar) * defnlen);
+    }
+
   pfile->pushed_macros = c;
 }
 
@@ -1512,7 +1535,8 @@
 	    pfile->pushed_macros = c->next;
 	  else
 	    l->next = c->next;
-	  cpp_pop_definition (pfile, c->name, c->value);
+	  cpp_pop_definition (pfile, c->name, c->definition);
+	  free (c->definition);
 	  free (c->name);
 	  free (c);
 	  break;
@@ -2334,21 +2358,10 @@
   run_directive (pfile, T_UNDEF, buf, len);
 }
 
-/* If STR is a defined macro, return its definition node, else return NULL.  */
-cpp_macro *
-cpp_push_definition (cpp_reader *pfile, const char *str)
-{
-  cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
-  if (node && node->type == NT_MACRO)
-    return node->value.macro;
-  else
-    return NULL;
-}
-
 /* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
    then the macro should be undefined.  */
-void
-cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
+static void
+cpp_pop_definition (cpp_reader *pfile, const char *str, const uchar *def)
 {
   cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
   if (node == NULL)
@@ -2367,16 +2380,30 @@
   if (node->type != NT_VOID)
     _cpp_free_definition (node);
 
-  if (dfn)
-    {
-      node->type = NT_MACRO;
-      node->value.macro = dfn;
-      if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
-	node->flags |= NODE_WARN;
-
-      if (pfile->cb.define)
-	pfile->cb.define (pfile, pfile->directive_line, node);
-    }
+  if (!def || *def == 0)
+    return;
+  {
+    size_t namelen;
+    const uchar *dn;
+    cpp_hashnode *h = NULL;
+
+    namelen = ustrcspn (def, "( \n");
+    h = cpp_lookup (pfile, def, namelen);
+    dn = def + namelen;
+
+    h->type = NT_VOID;
+    h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
+    if (cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true)
+	!= NULL)
+      {
+	_cpp_clean_line (pfile);
+	if (!_cpp_create_definition (pfile, h))
+	  abort ();
+	_cpp_pop_buffer (pfile);
+      }
+    else
+      abort ();
+  }
 }
 
 /* Process the string STR as if it appeared as the body of a #assert.  */
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp.orig/include/cpplib.h	2010-09-09 16:24:13.000000000 +0200
+++ libcpp/include/cpplib.h	2010-09-17 13:31:12.937735800 +0200
@@ -758,9 +758,6 @@
 extern void cpp_undef (cpp_reader *, const char *);
 extern void cpp_unassert (cpp_reader *, const char *);
 
-extern cpp_macro *cpp_push_definition (cpp_reader *, const char *);
-extern void cpp_pop_definition (cpp_reader *, const char *, cpp_macro *);
-
 /* Undefine all macros and assertions.  */
 extern void cpp_undef_all (cpp_reader *);
 
Index: libcpp/internal.h
===================================================================
--- libcpp.orig/internal.h	2010-09-09 16:24:14.000000000 +0200
+++ libcpp/internal.h	2010-09-17 13:15:36.960200900 +0200
@@ -313,7 +313,7 @@
   /* Name of the macro.  */
   char *name;
   /* The stored macro content.  */
-  cpp_macro *value;
+  unsigned char *definition;
 };
 
 /* A cpp_reader encapsulates the "state" of a pre-processor run.
Index: libcpp/pch.c
===================================================================
--- libcpp.orig/pch.c	2010-09-09 16:24:14.000000000 +0200
+++ libcpp/pch.c	2010-09-17 13:36:02.853318000 +0200
@@ -399,8 +399,6 @@
   size_t i;
   struct def_pragma_macro *p;
   size_t nlen;
-  cpp_hashnode *h = NULL;
-  cpp_macro *m;
   uchar *defn;
   size_t defnlen;
 
@@ -417,45 +415,17 @@
       p->name[nlen] = 0;
       if (fread (p->name, nlen, 1, f) != 1)
 	return 0;
-      /* Save old state.  */
-      m = cpp_push_definition (r, p->name);
       if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
 	return 0;
-      defn = XNEWVAR (uchar, defnlen + 2);
-      defn[defnlen] = '\n';
-      defn[defnlen + 1] = 0;
-
-      if (fread (defn, defnlen, 1, f) != 1)
-	return 0;
-      cpp_pop_definition (r, p->name, NULL);
-      {
-	size_t namelen;
-	uchar *dn;
-
-	namelen = ustrcspn (defn, "( \n");
-	h = cpp_lookup (r, defn, namelen);
-	dn = defn + namelen;
-
-	h->type = NT_VOID;
-	h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
-	if (cpp_push_buffer (r, dn, ustrchr (dn, '\n') - dn, true)
-	    != NULL)
-	  {
-	    _cpp_clean_line (r);
-	    if (!_cpp_create_definition (r, h))
-	      abort ();
-	    _cpp_pop_buffer (r);
-	  }
-	else
-	  abort ();
-      }
-      p->value = cpp_push_definition (r, p->name);
+      defn = XNEWVEC (uchar, defnlen + 1);
+      defn[defnlen] = 0;
+
+      if (defnlen != 0 && fread (defn, defnlen, 1, f) != 1)
+	return 0;
+      p->definition = defn;
 
-      free (defn);
       p->next = r->pushed_macros;
       r->pushed_macros = p;
-      /* Restore current state.  */
-      cpp_pop_definition (r, p->name, m);
     }
   return 1;
 }
@@ -466,10 +436,7 @@
   size_t count_saved = 0;
   size_t i;
   struct def_pragma_macro *p,**pp;
-  cpp_hashnode *node;
-  cpp_macro *m;
   size_t defnlen;
-  const uchar *defn;
 
   /* Get count. */
   p = r->pushed_macros;
@@ -496,22 +463,14 @@
     }
   for (i = 0; i < count_saved; i++)
     {
-      /* Save old state.  */
-      m = cpp_push_definition (r, pp[i]->name);
-      /* Set temporary macro name to saved state.  */
-      cpp_pop_definition (r, pp[i]->name, pp[i]->value);
-      node = _cpp_lex_identifier (r, pp[i]->name);
       defnlen = strlen (pp[i]->name);
       if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
 	  || fwrite (pp[i]->name, defnlen, 1, f) != 1)
 	return 0;
-      defn = cpp_macro_definition (r, node);
-      defnlen = ustrlen (defn);
+      defnlen = ustrlen (pp[i]->definition);
       if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
-	  || fwrite (defn, defnlen, 1, f) != 1)
+	  || (defnlen != 0 && fwrite (pp[i]->definition, defnlen, 1, f) != 1))
 	return 0;
-      /* Restore current state.  */
-      cpp_pop_definition (r, pp[i]->name, m);
     }
   return 1;
 }

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2010-09-29 18:19 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-17 14:00 [patch preprocessor]: PR/45362 Dangling reference Kai Tietz
2010-09-22  3:10 ` Tom Tromey
2010-09-22  3:32   ` Kai Tietz
2010-09-22  4:30     ` Kai Tietz
2010-09-22  6:16     ` Kai Tietz
2010-09-22  6:25     ` Tom Tromey
2010-09-22 23:20       ` Tom Tromey
2010-09-23  0:13         ` Kai Tietz
2010-09-23  4:55           ` Kai Tietz
2010-09-23  9:17           ` Tom Tromey
2010-09-23 19:10             ` Kai Tietz
2010-09-27 12:17               ` Kai Tietz
2010-09-28 22:41                 ` Kai Tietz
2010-09-30  6:17               ` Tom Tromey
2010-09-30  8:34                 ` Kai Tietz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).