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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  0 siblings, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2010-09-22  3:10 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:

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

Thanks.

Kai>  /* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
Kai>     then the macro should be undefined.  */
Kai> -void
Kai> -cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
Kai> +static void
Kai> +cpp_pop_definition (cpp_reader *pfile, const char *str, const uchar *def)

The comment before this function should be updated as well.


It seems to me that this patch loses some information currently in
cpp_macro, specifically the 'syshdr' and 'used' fields.  (It also loses
the location, but I am not sure that matters so much at the moment.)

Since a cpp_macro is marked with GTY, wouldn't a fix be to also mark
struct def_pragma_macro, perhaps also with a change to pch.c to write
out the chain?

Tom

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-22  3:10 ` Tom Tromey
@ 2010-09-22  3:32   ` Kai Tietz
  2010-09-22  4:30     ` Kai Tietz
                       ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-22  3:32 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

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

2010/9/21 Tom Tromey <tromey@redhat.com>:
>>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:
>
> Kai> I reworked implementation of push_macro/pop_macro by this patch that
> Kai> it doesn't re-use possible free'ed elements and uses instead textual
> Kai> representations of saved macro defintion. I ran specific test
> Kai> especially to check that pch still works.
>
> Thanks.
>
> Kai>  /* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
> Kai>     then the macro should be undefined.  */
> Kai> -void
> Kai> -cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
> Kai> +static void
> Kai> +cpp_pop_definition (cpp_reader *pfile, const char *str, const uchar *def)
>
> The comment before this function should be updated as well.

Thanks for catching this. Comment adjusted in attached patch.

> It seems to me that this patch loses some information currently in
> cpp_macro, specifically the 'syshdr' and 'used' fields.  (It also loses
> the location, but I am not sure that matters so much at the moment.)

Well, at the moment this seems to me not that important, but you are
right that those informations getting lost.

> Since a cpp_macro is marked with GTY, wouldn't a fix be to also mark
> struct def_pragma_macro, perhaps also with a change to pch.c to write
> out the chain?

Well, not sure if this would solve this. As libcpp can't have a root
gc element, I am not sure if the marking of def_pragma_macro by GTY
would really help.

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: 8037 bytes --]

Index: libcpp/directives.c
===================================================================
--- libcpp.orig/directives.c	2010-09-21 21:51:21.420927200 +0200
+++ libcpp/directives.c	2010-09-21 21:53:37.869064300 +0200
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp
 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 @@ do_pragma_once (cpp_reader *pfile)
 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 @@ do_pragma_push_macro (cpp_reader *pfile)
   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 @@ do_pragma_pop_macro (cpp_reader *pfile)
 	    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 @@ cpp_undef (cpp_reader *pfile, const char
   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)
+/* Replace a previous definition DEF of the macro STR.  If DEF is NULL,
+   or first element is zero, then the macro should be undefined.  */
+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 @@ cpp_pop_definition (cpp_reader *pfile, c
   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-21 21:51:21.509927200 +0200
+++ libcpp/include/cpplib.h	2010-09-21 21:51:39.675304100 +0200
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, co
 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-21 21:51:21.521927200 +0200
+++ libcpp/internal.h	2010-09-21 21:51:39.684304600 +0200
@@ -313,7 +313,7 @@ struct def_pragma_macro {
   /* 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-21 21:51:21.561927200 +0200
+++ libcpp/pch.c	2010-09-21 21:51:39.691305000 +0200
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *
   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 @@ _cpp_restore_pushed_macros (cpp_reader *
       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 @@ _cpp_save_pushed_macros (cpp_reader *r,
   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 @@ _cpp_save_pushed_macros (cpp_reader *r,
     }
   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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  2 siblings, 0 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-22  4:30 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

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

2010/9/21 Tom Tromey <tromey@redhat.com>:
>>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:
>
> Kai> I reworked implementation of push_macro/pop_macro by this patch that
> Kai> it doesn't re-use possible free'ed elements and uses instead textual
> Kai> representations of saved macro defintion. I ran specific test
> Kai> especially to check that pch still works.
>
> Thanks.
>
> Kai>  /* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
> Kai>     then the macro should be undefined.  */
> Kai> -void
> Kai> -cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
> Kai> +static void
> Kai> +cpp_pop_definition (cpp_reader *pfile, const char *str, const uchar *def)
>
> The comment before this function should be updated as well.

Thanks for catching this. Comment adjusted in attached patch.

> It seems to me that this patch loses some information currently in
> cpp_macro, specifically the 'syshdr' and 'used' fields.  (It also loses
> the location, but I am not sure that matters so much at the moment.)

Well, at the moment this seems to me not that important, but you are
right that those informations getting lost.

> Since a cpp_macro is marked with GTY, wouldn't a fix be to also mark
> struct def_pragma_macro, perhaps also with a change to pch.c to write
> out the chain?

Well, not sure if this would solve this. As libcpp can't have a root
gc element, I am not sure if the marking of def_pragma_macro by GTY
would really help.

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: 8037 bytes --]

Index: libcpp/directives.c
===================================================================
--- libcpp.orig/directives.c	2010-09-21 21:51:21.420927200 +0200
+++ libcpp/directives.c	2010-09-21 21:53:37.869064300 +0200
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp
 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 @@ do_pragma_once (cpp_reader *pfile)
 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 @@ do_pragma_push_macro (cpp_reader *pfile)
   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 @@ do_pragma_pop_macro (cpp_reader *pfile)
 	    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 @@ cpp_undef (cpp_reader *pfile, const char
   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)
+/* Replace a previous definition DEF of the macro STR.  If DEF is NULL,
+   or first element is zero, then the macro should be undefined.  */
+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 @@ cpp_pop_definition (cpp_reader *pfile, c
   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-21 21:51:21.509927200 +0200
+++ libcpp/include/cpplib.h	2010-09-21 21:51:39.675304100 +0200
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, co
 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-21 21:51:21.521927200 +0200
+++ libcpp/internal.h	2010-09-21 21:51:39.684304600 +0200
@@ -313,7 +313,7 @@ struct def_pragma_macro {
   /* 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-21 21:51:21.561927200 +0200
+++ libcpp/pch.c	2010-09-21 21:51:39.691305000 +0200
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *
   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 @@ _cpp_restore_pushed_macros (cpp_reader *
       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 @@ _cpp_save_pushed_macros (cpp_reader *r,
   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 @@ _cpp_save_pushed_macros (cpp_reader *r,
     }
   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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  2 siblings, 0 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-22  6:16 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

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

2010/9/21 Tom Tromey <tromey@redhat.com>:
>>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:
>
> Kai> I reworked implementation of push_macro/pop_macro by this patch that
> Kai> it doesn't re-use possible free'ed elements and uses instead textual
> Kai> representations of saved macro defintion. I ran specific test
> Kai> especially to check that pch still works.
>
> Thanks.
>
> Kai>  /* Replace a previous definition DFN of the macro STR.  If DFN is NULL,
> Kai>     then the macro should be undefined.  */
> Kai> -void
> Kai> -cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
> Kai> +static void
> Kai> +cpp_pop_definition (cpp_reader *pfile, const char *str, const uchar *def)
>
> The comment before this function should be updated as well.

Thanks for catching this. Comment adjusted in attached patch.

> It seems to me that this patch loses some information currently in
> cpp_macro, specifically the 'syshdr' and 'used' fields.  (It also loses
> the location, but I am not sure that matters so much at the moment.)

Well, at the moment this seems to me not that important, but you are
right that those informations getting lost.

> Since a cpp_macro is marked with GTY, wouldn't a fix be to also mark
> struct def_pragma_macro, perhaps also with a change to pch.c to write
> out the chain?

Well, not sure if this would solve this. As libcpp can't have a root
gc element, I am not sure if the marking of def_pragma_macro by GTY
would really help.

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: 8037 bytes --]

Index: libcpp/directives.c
===================================================================
--- libcpp.orig/directives.c	2010-09-21 21:51:21.420927200 +0200
+++ libcpp/directives.c	2010-09-21 21:53:37.869064300 +0200
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp
 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 @@ do_pragma_once (cpp_reader *pfile)
 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 @@ do_pragma_push_macro (cpp_reader *pfile)
   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 @@ do_pragma_pop_macro (cpp_reader *pfile)
 	    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 @@ cpp_undef (cpp_reader *pfile, const char
   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)
+/* Replace a previous definition DEF of the macro STR.  If DEF is NULL,
+   or first element is zero, then the macro should be undefined.  */
+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 @@ cpp_pop_definition (cpp_reader *pfile, c
   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-21 21:51:21.509927200 +0200
+++ libcpp/include/cpplib.h	2010-09-21 21:51:39.675304100 +0200
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, co
 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-21 21:51:21.521927200 +0200
+++ libcpp/internal.h	2010-09-21 21:51:39.684304600 +0200
@@ -313,7 +313,7 @@ struct def_pragma_macro {
   /* 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-21 21:51:21.561927200 +0200
+++ libcpp/pch.c	2010-09-21 21:51:39.691305000 +0200
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *
   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 @@ _cpp_restore_pushed_macros (cpp_reader *
       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 @@ _cpp_save_pushed_macros (cpp_reader *r,
   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 @@ _cpp_save_pushed_macros (cpp_reader *r,
     }
   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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  2 siblings, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2010-09-22  6:25 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:

Tom> It seems to me that this patch loses some information currently in
Tom> cpp_macro, specifically the 'syshdr' and 'used' fields.  (It also loses
Tom> the location, but I am not sure that matters so much at the moment.)

Kai> Well, at the moment this seems to me not that important, but you are
Kai> right that those informations getting lost.

It seems to me that it can definitely matter.
syshdr in particular is tested before emitting various kinds of errors.

Tom> Since a cpp_macro is marked with GTY, wouldn't a fix be to also mark
Tom> struct def_pragma_macro, perhaps also with a change to pch.c to write
Tom> out the chain?

Kai> Well, not sure if this would solve this. As libcpp can't have a root
Kai> gc element, I am not sure if the marking of def_pragma_macro by GTY
Kai> would really help.

It also seems like instead of having a separate list for push/pop, we
could simply store a pointer to the pushed macro into the cpp_macro
object itself.  This will grow cpp_macro, but maybe that isn't so bad.

Tom

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-22  6:25     ` Tom Tromey
@ 2010-09-22 23:20       ` Tom Tromey
  2010-09-23  0:13         ` Kai Tietz
  0 siblings, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2010-09-22 23:20 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:

Tom> It also seems like instead of having a separate list for push/pop, we
Tom> could simply store a pointer to the pushed macro into the cpp_macro
Tom> object itself.  This will grow cpp_macro, but maybe that isn't so bad.

Kai points out that this won't work properly because we might see an
#undef, so the cpp_macro object would not be available.

Instead I think it is sufficient for the time being to simply save the
'line', 'syshdr', and 'used' fields along with the macro text.

Tom

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  0 siblings, 2 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-23  0:13 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

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

2010/9/22 Tom Tromey <tromey@redhat.com>:
>>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
>
> Tom> It also seems like instead of having a separate list for push/pop, we
> Tom> could simply store a pointer to the pushed macro into the cpp_macro
> Tom> object itself.  This will grow cpp_macro, but maybe that isn't so bad.
>
> Kai points out that this won't work properly because we might see an
> #undef, so the cpp_macro object would not be available.
>
> Instead I think it is sufficient for the time being to simply save the
> 'line', 'syshdr', and 'used' fields along with the macro text.
>
> Tom
>

So here is extended patch storing additional fields location, syshdr,
and used of cpp_macro. The pch testsuite is just running. I'll give
final result when test is done.

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: 9425 bytes --]

Index: libcpp/directives.c
===================================================================
--- libcpp.orig/directives.c	2010-09-22 20:36:42.335178100 +0200
+++ libcpp/directives.c	2010-09-22 21:41:25.440303800 +0200
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp
 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 *, struct def_pragma_macro *);
 
 /* 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 @@ do_pragma_once (cpp_reader *pfile)
 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,32 @@ do_pragma_push_macro (cpp_reader *pfile)
   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;
+      c->line = 0;
+      c->syshdr = 0;
+      c->used = 0;
+    }
+  else
+    {
+      c->definition[defnlen] = '\n';
+      c->definition[defnlen + 1] = 0;
+      c->line = node->value.macro->line;
+      c->syshdr = node->value.macro->syshdr;
+      c->used = node->value.macro->used;
+      memcpy (c->definition, defn, sizeof (uchar) * defnlen);
+    }
+
   pfile->pushed_macros = c;
 }
 
@@ -1512,7 +1541,8 @@ do_pragma_pop_macro (cpp_reader *pfile)
 	    pfile->pushed_macros = c->next;
 	  else
 	    l->next = c->next;
-	  cpp_pop_definition (pfile, c->name, c->value);
+	  cpp_pop_definition (pfile, c);
+	  free (c->definition);
 	  free (c->name);
 	  free (c);
 	  break;
@@ -2334,23 +2364,12 @@ cpp_undef (cpp_reader *pfile, const char
   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)
+/* Replace a previous definition DEF of the macro STR.  If DEF is NULL,
+   or first element is zero, then the macro should be undefined.  */
+static void
+cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
 {
-  cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
+  cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
   if (node == NULL)
     return;
 
@@ -2367,16 +2386,33 @@ cpp_pop_definition (cpp_reader *pfile, c
   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 (!(c->definition) || c->definition[0] == 0)
+    return;
+  {
+    size_t namelen;
+    const uchar *dn;
+    cpp_hashnode *h = NULL;
+
+    namelen = ustrcspn (c->definition, "( \n");
+    h = cpp_lookup (pfile, c->definition, namelen);
+    dn = c->definition + 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 ();
+    h->value.macro->line = c->line;
+    h->value.macro->syshdr = c->syshdr;
+    h->value.macro->used = c->used;
+  }
 }
 
 /* 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-22 20:36:42.390178100 +0200
+++ libcpp/include/cpplib.h	2010-09-22 20:58:07.848730000 +0200
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, co
 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-22 20:36:42.420178100 +0200
+++ libcpp/internal.h	2010-09-22 21:05:30.802065500 +0200
@@ -313,7 +313,15 @@ struct def_pragma_macro {
   /* Name of the macro.  */
   char *name;
   /* The stored macro content.  */
-  cpp_macro *value;
+  unsigned char *definition;
+  /* Stored fields of cpp_macro.  */
+
+  /* Definition line number.  */
+  source_location line;
+  /* If macro defined in system header.  */
+  unsigned int syshdr   : 1;
+  /* Nonzero if it has been expanded or had its existence tested.  */
+  unsigned int used     : 1;
 };
 
 /* A cpp_reader encapsulates the "state" of a pre-processor run.
Index: libcpp/pch.c
===================================================================
--- libcpp.orig/pch.c	2010-09-22 20:36:42.447178100 +0200
+++ libcpp/pch.c	2010-09-22 21:54:37.769622400 +0200
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *
   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,28 @@ _cpp_restore_pushed_macros (cpp_reader *
       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;
+      /* In case of no undefine.  */
+      if (defnlen != 0)
+        {
+	  if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
+	    return 0;
+	  defnlen = 0;
+	  if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
+	    return 0;
+	  p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
+	  p->used =  ((defnlen & 2) != 0 ? 1 : 0);
+	}
 
-      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 +447,7 @@ _cpp_save_pushed_macros (cpp_reader *r,
   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 +474,25 @@ _cpp_save_pushed_macros (cpp_reader *r,
     }
   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);
+      /* Write additional information for macro definition case.  */
+      if (defnlen != 0)
+	{
+	  if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
+	    return 0;
+	  defnlen = 0;
+	  defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
+	  defnlen |= (pp[i]->used != 0 ? 2 : 0);
+	  if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
+	    return 0;
+	}
     }
   return 1;
 }

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-23  0:13         ` Kai Tietz
@ 2010-09-23  4:55           ` Kai Tietz
  2010-09-23  9:17           ` Tom Tromey
  1 sibling, 0 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-23  4:55 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

2010/9/22 Kai Tietz <ktietz70@googlemail.com>:
> 2010/9/22 Tom Tromey <tromey@redhat.com>:
>>>>>>> "Tom" == Tom Tromey <tromey@redhat.com> writes:
>>
>> Tom> It also seems like instead of having a separate list for push/pop, we
>> Tom> could simply store a pointer to the pushed macro into the cpp_macro
>> Tom> object itself.  This will grow cpp_macro, but maybe that isn't so bad.
>>
>> Kai points out that this won't work properly because we might see an
>> #undef, so the cpp_macro object would not be available.
>>
>> Instead I think it is sufficient for the time being to simply save the
>> 'line', 'syshdr', and 'used' fields along with the macro text.
>>
>> Tom
>>
>
> So here is extended patch storing additional fields location, syshdr,
> and used of cpp_macro. The pch testsuite is just running. I'll give
> final result when test is done.
>
> Regards,
> Kai
>
> --
> |  (\_/) This is Bunny. Copy and paste
> | (='.'=) Bunny into your signature to help
> | (")_(") him gain world domination
>

Ok, test for i686-w64-mingw32 and x86_64-w64-mingw32 about pch are ok
and showed no regression by this patch.

Kai

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

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2010-09-23  9:17 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:

Kai> So here is extended patch storing additional fields location, syshdr,
Kai> and used of cpp_macro. The pch testsuite is just running. I'll give
Kai> final result when test is done.

Thanks.

Kai> +  if (node->type == NT_VOID)
Kai> +    defnlen = 0;

It seems like this could be a flag on the saved macro struct, instead of
a magic length.

Kai> +	_cpp_clean_line (pfile);
Kai> +	if (!_cpp_create_definition (pfile, h))
Kai> +	  abort ();
Kai> +	_cpp_pop_buffer (pfile);
Kai> +      }
Kai> +    else
Kai> +      abort ();
Kai> +    h->value.macro->line = c->line;
Kai> +    h->value.macro->syshdr = c->syshdr;
Kai> +    h->value.macro->used = c->used;

I suspect you want to set the 'sysp' flag on the buffer before calling
_cpp_create_definition.  That may suppress some messages coming from
_cpp_create_definition.

Also I wonder if this will have negative side effects on the current
line as recorded in the cpp reader.

In other respects this looks good to me.

Tom

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-23  9:17           ` Tom Tromey
@ 2010-09-23 19:10             ` Kai Tietz
  2010-09-27 12:17               ` Kai Tietz
  2010-09-30  6:17               ` Tom Tromey
  0 siblings, 2 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-23 19:10 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

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

2010/9/23 Tom Tromey <tromey@redhat.com>:
>>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:
>
> Kai> So here is extended patch storing additional fields location, syshdr,
> Kai> and used of cpp_macro. The pch testsuite is just running. I'll give
> Kai> final result when test is done.
>
> Thanks.
>
> Kai> +  if (node->type == NT_VOID)
> Kai> +    defnlen = 0;
>
> It seems like this could be a flag on the saved macro struct, instead of
> a magic length.

Well, we need still the zero-length store for marking undef-state, but
I added this flag to internal saved structure and avoid allocation of
definition member in such cases.

> Kai> +  _cpp_clean_line (pfile);
> Kai> +  if (!_cpp_create_definition (pfile, h))
> Kai> +    abort ();
> Kai> +  _cpp_pop_buffer (pfile);
> Kai> +      }
> Kai> +    else
> Kai> +      abort ();
> Kai> +    h->value.macro->line = c->line;
> Kai> +    h->value.macro->syshdr = c->syshdr;
> Kai> +    h->value.macro->used = c->used;
>
> I suspect you want to set the 'sysp' flag on the buffer before calling
> _cpp_create_definition.  That may suppress some messages coming from
> _cpp_create_definition.

Yes, I add this sysp setting. This could otherwise leads to unwanted
failure messages.

> Also I wonder if this will have negative side effects on the current
> line as recorded in the cpp reader.

Well, not sure here. At least I didn't noticed here issues.

> In other respects this looks good to me.
>
> Tom
>

Tested for x86_64-w64-mingw32 and i686-w64-mingw32. Ok for apply to 4.6?

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: 9852 bytes --]

Index: gcc/libcpp/directives.c
===================================================================
--- gcc.orig/libcpp/directives.c	2010-09-01 09:19:41.000000000 +0200
+++ gcc/libcpp/directives.c	2010-09-23 11:34:32.364665200 +0200
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp
 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 *, struct def_pragma_macro *);
 
 /* 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 @@ do_pragma_once (cpp_reader *pfile)
 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;
@@ -1465,10 +1469,26 @@ do_pragma_push_macro (cpp_reader *pfile)
   check_eol (pfile, false);
   skip_rest_of_line (pfile);
   c = XNEW (struct def_pragma_macro);
+  memset (c, 0, sizeof (struct def_pragma_macro));
   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)
+    c->is_undef = 1;
+  else
+    {
+      defn = cpp_macro_definition (pfile, node);
+      defnlen = ustrlen (defn);
+      c->definition = XNEWVEC (uchar, defnlen + 2);
+      c->definition[defnlen] = '\n';
+      c->definition[defnlen + 1] = 0;
+      c->line = node->value.macro->line;
+      c->syshdr = node->value.macro->syshdr;
+      c->used = node->value.macro->used;
+      memcpy (c->definition, defn, sizeof (uchar) * defnlen);
+    }
+
   pfile->pushed_macros = c;
 }
 
@@ -1512,7 +1532,9 @@ do_pragma_pop_macro (cpp_reader *pfile)
 	    pfile->pushed_macros = c->next;
 	  else
 	    l->next = c->next;
-	  cpp_pop_definition (pfile, c->name, c->value);
+	  cpp_pop_definition (pfile, c);
+	  if (c->definition)
+	    free (c->definition);
 	  free (c->name);
 	  free (c);
 	  break;
@@ -2334,23 +2356,12 @@ cpp_undef (cpp_reader *pfile, const char
   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)
+/* Replace a previous definition DEF of the macro STR.  If DEF is NULL,
+   or first element is zero, then the macro should be undefined.  */
+static void
+cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c)
 {
-  cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
+  cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name);
   if (node == NULL)
     return;
 
@@ -2367,16 +2378,35 @@ cpp_pop_definition (cpp_reader *pfile, c
   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 (c->is_undef)
+    return;
+  {
+    size_t namelen;
+    const uchar *dn;
+    cpp_hashnode *h = NULL;
+    cpp_buffer *nbuf;
+
+    namelen = ustrcspn (c->definition, "( \n");
+    h = cpp_lookup (pfile, c->definition, namelen);
+    dn = c->definition + namelen;
+
+    h->type = NT_VOID;
+    h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
+    nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true);
+    if (nbuf != NULL)
+      {
+	_cpp_clean_line (pfile);
+	nbuf->sysp = 1;
+	if (!_cpp_create_definition (pfile, h))
+	  abort ();
+	_cpp_pop_buffer (pfile);
+      }
+    else
+      abort ();
+    h->value.macro->line = c->line;
+    h->value.macro->syshdr = c->syshdr;
+    h->value.macro->used = c->used;
+  }
 }
 
 /* Process the string STR as if it appeared as the body of a #assert.  */
Index: gcc/libcpp/include/cpplib.h
===================================================================
--- gcc.orig/libcpp/include/cpplib.h	2010-06-17 10:01:07.000000000 +0200
+++ gcc/libcpp/include/cpplib.h	2010-09-23 11:10:55.549344500 +0200
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, co
 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: gcc/libcpp/internal.h
===================================================================
--- gcc.orig/libcpp/internal.h	2010-03-31 16:41:58.000000000 +0200
+++ gcc/libcpp/internal.h	2010-09-23 11:35:53.349040200 +0200
@@ -313,7 +313,17 @@ struct def_pragma_macro {
   /* Name of the macro.  */
   char *name;
   /* The stored macro content.  */
-  cpp_macro *value;
+  unsigned char *definition;
+
+  /* Definition line number.  */
+  source_location line;
+  /* If macro defined in system header.  */
+  unsigned int syshdr   : 1;
+  /* Nonzero if it has been expanded or had its existence tested.  */
+  unsigned int used     : 1;
+
+  /* Mark if we save an undefined macro.  */
+  unsigned int is_undef : 1;
 };
 
 /* A cpp_reader encapsulates the "state" of a pre-processor run.
Index: gcc/libcpp/pch.c
===================================================================
--- gcc.orig/libcpp/pch.c	2010-06-17 10:01:07.000000000 +0200
+++ gcc/libcpp/pch.c	2010-09-23 11:35:39.895915200 +0200
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *
   size_t i;
   struct def_pragma_macro *p;
   size_t nlen;
-  cpp_hashnode *h = NULL;
-  cpp_macro *m;
   uchar *defn;
   size_t defnlen;
 
@@ -413,49 +411,35 @@ _cpp_restore_pushed_macros (cpp_reader *
       if (fread (&nlen, sizeof (nlen), 1, f) != 1)
 	return 0;
       p = XNEW (struct def_pragma_macro);
+      memset (p, 0, sizeof (struct def_pragma_macro));
       p->name = XNEWVAR (char, nlen + 1);
       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 (defnlen == 0)
+        p->is_undef = 1;
+      else
+        {
+	  defn = XNEWVEC (uchar, defnlen + 1);
+	  defn[defnlen] = 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);
+	  if (fread (defn, defnlen, 1, f) != 1)
+	    return 0;
+
+	  p->definition = defn;
+	  if (fread (&(p->line), sizeof (source_location), 1, f) != 1)
+	    return 0;
+	  defnlen = 0;
+	  if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
+	    return 0;
+	  p->syshdr = ((defnlen & 1) != 0 ? 1 : 0);
+	  p->used =  ((defnlen & 2) != 0 ? 1 : 0);
+	}
 
-      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 +450,7 @@ _cpp_save_pushed_macros (cpp_reader *r, 
   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 +477,30 @@ _cpp_save_pushed_macros (cpp_reader *r, 
     }
   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);
-      if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
-	  || fwrite (defn, defnlen, 1, f) != 1)
-	return 0;
-      /* Restore current state.  */
-      cpp_pop_definition (r, pp[i]->name, m);
+      if (pp[i]->is_undef)
+	{
+	  defnlen = 0;
+	  if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1)
+	    return 0;
+	}
+      else
+        {
+	  defnlen = ustrlen (pp[i]->definition);
+	  if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
+	      || fwrite (pp[i]->definition, defnlen, 1, f) != 1)
+	    return 0;
+	  if (fwrite (&(pp[i]->line), sizeof (source_location), 1, f) != 1)
+	    return 0;
+	  defnlen = 0;
+	  defnlen |= (pp[i]->syshdr != 0 ? 1 : 0);
+	  defnlen |= (pp[i]->used != 0 ? 2 : 0);
+	  if (fwrite (&defnlen, sizeof (defnlen), 1, f) != 1)
+	    return 0;
+	}
     }
   return 1;
 }

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  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
  1 sibling, 1 reply; 15+ messages in thread
From: Kai Tietz @ 2010-09-27 12:17 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

2010/9/23 Kai Tietz <ktietz70@googlemail.com>:
> 2010/9/23 Tom Tromey <tromey@redhat.com>:
>> Also I wonder if this will have negative side effects on the current
>> line as recorded in the cpp reader.
>
> Well, not sure here. At least I didn't noticed here issues.

Well, I did some further test to check if line-numbers are affected by
pop_macro implementation and there are no failures to be found.
Diagnostic shows correct line-numbers. So there are no side-effects
about current line in cpp reader.

Regards,
Kai


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

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-27 12:17               ` Kai Tietz
@ 2010-09-28 22:41                 ` Kai Tietz
  0 siblings, 0 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-28 22:41 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

PING

2010/9/26 Kai Tietz <ktietz70@googlemail.com>:
> 2010/9/23 Kai Tietz <ktietz70@googlemail.com>:
>> 2010/9/23 Tom Tromey <tromey@redhat.com>:
>>> Also I wonder if this will have negative side effects on the current
>>> line as recorded in the cpp reader.
>>
>> Well, not sure here. At least I didn't noticed here issues.
>
> Well, I did some further test to check if line-numbers are affected by
> pop_macro implementation and there are no failures to be found.
> Diagnostic shows correct line-numbers. So there are no side-effects
> about current line in cpp reader.
>
> Regards,
> Kai
>
>
> --
> |  (\_/) This is Bunny. Copy and paste
> | (='.'=) Bunny into your signature to help
> | (")_(") him gain world domination
>



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

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-23 19:10             ` Kai Tietz
  2010-09-27 12:17               ` Kai Tietz
@ 2010-09-30  6:17               ` Tom Tromey
  2010-09-30  8:34                 ` Kai Tietz
  1 sibling, 1 reply; 15+ messages in thread
From: Tom Tromey @ 2010-09-30  6:17 UTC (permalink / raw)
  To: Kai Tietz; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:

Kai> Tested for x86_64-w64-mingw32 and i686-w64-mingw32. Ok for apply to 4.6?

Thanks for persevering.

Kai> +      memcpy (c->definition, defn, sizeof (uchar) * defnlen);

You don't need sizeof (uchar) here.

Kai> +	  if (c->definition)
Kai> +	    free (c->definition);

You don't need the if here.  free works fine with NULL pointers.

This is ok with those changes.  Thanks again.

Tom

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

* Re: [patch preprocessor]: PR/45362 Dangling reference
  2010-09-30  6:17               ` Tom Tromey
@ 2010-09-30  8:34                 ` Kai Tietz
  0 siblings, 0 replies; 15+ messages in thread
From: Kai Tietz @ 2010-09-30  8:34 UTC (permalink / raw)
  To: Tom Tromey; +Cc: GCC Patches, Jakub Jelinek, Andrew Pinski, Richard Henderson

2010/9/29 Tom Tromey <tromey@redhat.com>:
>>>>>> "Kai" == Kai Tietz <ktietz70@googlemail.com> writes:
>
> Kai> Tested for x86_64-w64-mingw32 and i686-w64-mingw32. Ok for apply to 4.6?
>
> Thanks for persevering.
>
> Kai> +      memcpy (c->definition, defn, sizeof (uchar) * defnlen);
>
> You don't need sizeof (uchar) here.
>
> Kai> +    if (c->definition)
> Kai> +      free (c->definition);
>
> You don't need the if here.  free works fine with NULL pointers.
>
> This is ok with those changes.  Thanks again.
>
> Tom
>

Ok, thanks. Adjusted and committed at revision 164729.

Regards,
Kai

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

^ 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).