From d687413849f10f957c782eb4de2a54f3e78a7aae Mon Sep 17 00:00:00 2001
From: Kai Tietz <kai.tietz@onevision.com>
Date: Wed, 29 Sep 2010 18:18:38 +0000
Subject: [PATCH] re PR preprocessor/45362 (Dangling reference about saved
 cpp_macro for push/pop macro)

2010-09-29  Kai Tietz  <kai.tietz@onevision.com>

	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 (def_pragma_macro): Remove member 'value'
	and add new members 'definition', 'line',
	'syshdr', 'sued' and 'is_undef'.
	* pch.c (_cpp_restore_pushed_macros): Rework to work
	on text definition and store additional macro flags.
	(_cpp_save_pushed_macros): Likewise.

From-SVN: r164729
---
 libcpp/ChangeLog        | 17 ++++++++
 libcpp/directives.c     | 85 +++++++++++++++++++++++++++-------------
 libcpp/include/cpplib.h |  3 --
 libcpp/internal.h       | 12 +++++-
 libcpp/pch.c            | 87 ++++++++++++++++++-----------------------
 5 files changed, 123 insertions(+), 81 deletions(-)

diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index 3b6f4fae9d8d..cabe7ce91a8c 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,3 +1,20 @@
+2010-09-29  Kai Tietz  <kai.tietz@onevision.com>
+
+	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 (def_pragma_macro): Remove member 'value'
+	and add new members 'definition', 'line',
+	'syshdr', 'sued' and 'is_undef'.
+	* pch.c (_cpp_restore_pushed_macros): Rework to work
+	on text definition and store additional macro flags.
+	(_cpp_save_pushed_macros): Likewise.
+
 2010-09-29  Joseph Myers  <joseph@codesourcery.com>
 
 	* include/cpplib.h (cpp_options): Rename warn_deprecated,
diff --git a/libcpp/directives.c b/libcpp/directives.c
index 3d128554c122..6462605c3f40 100644
--- a/libcpp/directives.c
+++ b/libcpp/directives.c
@@ -128,6 +128,7 @@ static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
 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, defnlen);
+    }
+
   pfile->pushed_macros = c;
 }
 
@@ -1512,7 +1532,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 +2355,12 @@ cpp_undef (cpp_reader *pfile, const char *macro)
   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 +2377,35 @@ cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
   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.  */
diff --git a/libcpp/include/cpplib.h b/libcpp/include/cpplib.h
index 7c39a83b8753..8fa28819bed2 100644
--- a/libcpp/include/cpplib.h
+++ b/libcpp/include/cpplib.h
@@ -758,9 +758,6 @@ extern void cpp_assert (cpp_reader *, const char *);
 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 *);
 
diff --git a/libcpp/internal.h b/libcpp/internal.h
index e0ac285d82c9..d2872c4a11fb 100644
--- a/libcpp/internal.h
+++ b/libcpp/internal.h
@@ -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.
diff --git a/libcpp/pch.c b/libcpp/pch.c
index a6d8cea38d3c..653e5c96adfb 100644
--- a/libcpp/pch.c
+++ b/libcpp/pch.c
@@ -399,8 +399,6 @@ _cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
   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 *r, FILE *f)
       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 (fread (defn, defnlen, 1, f) != 1)
-	return 0;
-      cpp_pop_definition (r, p->name, NULL);
-      {
-	size_t namelen;
-	uchar *dn;
+      if (defnlen == 0)
+        p->is_undef = 1;
+      else
+        {
+	  defn = XNEWVEC (uchar, defnlen + 1);
+	  defn[defnlen] = 0;
 
-	namelen = ustrcspn (defn, "( \n");
-	h = cpp_lookup (r, defn, namelen);
-	dn = defn + namelen;
+	  if (fread (defn, defnlen, 1, f) != 1)
+	    return 0;
 
-	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);
+	  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, FILE *f)
   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, FILE *f)
     }
   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;
 }
-- 
GitLab