[Ada] Consistently use explicit Entity_Id type instead of alias
[gcc.git] / libcpp / pch.c
index 2fb7ba543e787e52f87d928fe0d04938ddfece8b..fcdf3875d0b214ec4d3d7bf4192c7d33da2c0cee 100644 (file)
@@ -1,6 +1,5 @@
 /* Part of CPP library.  (Precompiled header reading/writing.)
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
-   Free Software Foundation, Inc.
+   Copyright (C) 2000-2020 Free Software Foundation, Inc.
 
 This program is free software; you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the
@@ -51,46 +50,47 @@ static int
 write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
 {
   FILE *f = (FILE *) file_p;
+  bool is_void = false;
   switch (hn->type)
     {
     case NT_VOID:
       if (! (hn->flags & NODE_POISONED))
        return 1;
+      is_void = true;
+      goto poisoned;
 
-    case NT_MACRO:
-      if ((hn->flags & NODE_BUILTIN))
-       return 1;
-
-      {
-       struct macrodef_struct s;
-       const unsigned char *defn;
+    case NT_BUILTIN_MACRO:
+      return 1;
 
-       s.name_length = NODE_LEN (hn);
-       s.flags = hn->flags & NODE_POISONED;
+    case NT_USER_MACRO:
+      if (hn->value.macro->kind != cmk_assert)
+       {
+       poisoned:
+         struct macrodef_struct s;
+         const unsigned char *defn;
 
-       if (hn->type == NT_MACRO)
-         {
-           defn = cpp_macro_definition (pfile, hn);
-           s.definition_length = ustrlen (defn);
-         }
-       else
-         {
-           defn = NODE_NAME (hn);
-           s.definition_length = s.name_length;
-         }
+         s.name_length = NODE_LEN (hn);
+         s.flags = hn->flags & NODE_POISONED;
 
-       if (fwrite (&s, sizeof (s), 1, f) != 1
-           || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
-         {
-           cpp_errno (pfile, CPP_DL_ERROR,
-                      "while writing precompiled header");
-           return 0;
-         }
-      }
-      return 1;
+         if (is_void)
+           {
+             defn = NODE_NAME (hn);
+             s.definition_length = s.name_length;
+           }
+         else
+           {
+             defn = cpp_macro_definition (pfile, hn);
+             s.definition_length = ustrlen (defn);
+           }
 
-    case NT_ASSERTION:
-      /* Not currently implemented.  */
+         if (fwrite (&s, sizeof (s), 1, f) != 1
+             || fwrite (defn, 1, s.definition_length, f) != s.definition_length)
+           {
+             cpp_errno (pfile, CPP_DL_ERROR,
+                        "while writing precompiled header");
+             return 0;
+           }
+       }
       return 1;
 
     default:
@@ -186,6 +186,16 @@ cpp_string_eq (const void *a_p, const void *b_p)
          && memcmp (a->text, b->text, a->len) == 0);
 }
 
+/* Free memory associated with cpp_string.  */
+
+static void
+cpp_string_free (void *a_p)
+{
+  struct cpp_string *a = (struct cpp_string *) a_p;
+  free ((void *) a->text);
+  free (a);
+}
+
 /* Save the current definitions of the cpp_reader for dependency
    checking purposes.  When writing a precompiled header, this should
    be called at the same point in the compilation as cpp_valid_state
@@ -197,7 +207,7 @@ cpp_save_state (cpp_reader *r, FILE *f)
   /* Save the list of non-void identifiers for the dependency checking.  */
   r->savedstate = XNEW (struct cpp_savedstate);
   r->savedstate->definedhash = htab_create (100, cpp_string_hash,
-                                           cpp_string_eq, NULL);
+                                           cpp_string_eq, cpp_string_free);
   cpp_forall_identifiers (r, save_idents, r->savedstate);
 
   /* Write out the list of defined identifiers.  */
@@ -215,11 +225,14 @@ count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
 
   switch (hn->type)
     {
-    case NT_MACRO:
-      if (hn->flags & NODE_BUILTIN)
+    case NT_BUILTIN_MACRO:
+      return 1;
+
+    case NT_USER_MACRO:
+      if (hn->value.macro->kind == cmk_assert)
        return 1;
 
-      /* else fall through.  */
+      /* fall through.  */
 
     case NT_VOID:
       {
@@ -237,10 +250,6 @@ count_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
       }
       return 1;
 
-    case NT_ASSERTION:
-      /* Not currently implemented.  */
-      return 1;
-
     default:
       abort ();
     }
@@ -254,11 +263,14 @@ write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
 
   switch (hn->type)
     {
-    case NT_MACRO:
-      if (hn->flags & NODE_BUILTIN)
+    case NT_BUILTIN_MACRO:
+      return 1;
+
+    case NT_USER_MACRO:
+      if (hn->value.macro->kind == cmk_assert)
        return 1;
 
-      /* else fall through.  */
+      /* fall through.  */
 
     case NT_VOID:
       {
@@ -276,10 +288,6 @@ write_defs (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *hn, void *ss_p)
       }
       return 1;
 
-    case NT_ASSERTION:
-      /* Not currently implemented.  */
-      return 1;
-
     default:
       abort ();
     }
@@ -335,6 +343,8 @@ cpp_write_pch_deps (cpp_reader *r, FILE *f)
       return -1;
     }
   free (ss->definedstrs);
+  free (ss->defs);
+  htab_delete (ss->definedhash);
 
   /* Free the saved state.  */
   free (ss);
@@ -397,8 +407,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;
 
@@ -411,49 +419,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 (location_t), 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;
 }
@@ -464,10 +458,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;
@@ -494,22 +485,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 (location_t), 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;
 }
@@ -619,14 +618,14 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
          goto fail;
        }
 
-      if (h->type != NT_MACRO)
+      if (h->type == NT_VOID)
        {
          /* It's ok if __GCC_HAVE_DWARF2_CFI_ASM becomes undefined,
             as in, when the PCH file is created with -g and we're
             attempting to use it without -g.  Restoring the PCH file
             is supposed to bring in this definition *and* enable the
             generation of call frame information, so that precompiled
-            definitions that take this macro into accout, to decide
+            definitions that take this macro into account, to decide
             what asm to emit, won't issue .cfi directives when the
             compiler doesn't.  */
          if (!(h->flags & NODE_USED)
@@ -711,7 +710,7 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
        cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
                            "%s: not used because `__COUNTER__' is invalid",
                            name);
-       goto fail;
+      goto fail;
     }
 
   /* We win!  */
@@ -719,15 +718,11 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
 
  error:
   cpp_errno (r, CPP_DL_ERROR, "while reading precompiled header");
-  return -1;
 
  fail:
-  if (namebuf != NULL)
-    free (namebuf);
-  if (undeftab != NULL)
-    free (undeftab);
-  if (nl.defs != NULL)
-    free (nl.defs);
+  free (namebuf);
+  free (undeftab);
+  free (nl.defs);
   return 1;
 }
 
@@ -759,8 +754,8 @@ static int
 save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
 {
   struct save_macro_data *data = (struct save_macro_data *)data_p;
-  if (h->type != NT_VOID
-      && (h->flags & NODE_BUILTIN) == 0)
+
+  if (cpp_user_macro_p (h))
     {
       if (data->count == data->array_size)
        {
@@ -768,28 +763,14 @@ save_macros (cpp_reader *r, cpp_hashnode *h, void *data_p)
          data->defns = XRESIZEVEC (uchar *, data->defns, (data->array_size));
        }
 
-      switch (h->type)
-       {
-       case NT_ASSERTION:
-         /* Not currently implemented.  */
-         return 1;
-
-       case NT_MACRO:
-         {
-           const uchar * defn = cpp_macro_definition (r, h);
-           size_t defnlen = ustrlen (defn);
-
-           data->defns[data->count] = (uchar *) xmemdup (defn, defnlen,
-                                                          defnlen + 2);
-           data->defns[data->count][defnlen] = '\n';
-         }
-         break;
+      const uchar * defn = cpp_macro_definition (r, h);
+      size_t defnlen = ustrlen (defn);
 
-       default:
-         abort ();
-       }
+      data->defns[data->count] = (uchar *) xmemdup (defn, defnlen, defnlen + 2);
+      data->defns[data->count][defnlen] = '\n';
       data->count++;
     }
+
   return 1;
 }
 
@@ -829,6 +810,7 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
     s->n_true          = cpp_lookup (r, DSC("true"));
     s->n_false         = cpp_lookup (r, DSC("false"));
     s->n__VA_ARGS__     = cpp_lookup (r, DSC("__VA_ARGS__"));
+    s->n__VA_OPT__      = cpp_lookup (r, DSC("__VA_OPT__"));
   }
 
   old_state = r->state;