directives.c (check_eol_1): New.
[gcc.git] / libcpp / pch.c
index bf7b9bea36d9aec166f6c515e83389f47a9603d2..cddca837ffd923f81faf7a578292b425d9029160 100644 (file)
@@ -1,10 +1,9 @@
 /* Part of CPP library.  (Precompiled header reading/writing.)
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
-   Free Software Foundation, Inc.
+   Copyright (C) 2000-2014 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
-Free Software Foundation; either version 2, or (at your option) any
+Free Software Foundation; either version 3, or (at your option) any
 later version.
 
 This program is distributed in the hope that it will be useful,
@@ -13,8 +12,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+along with this program; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
@@ -33,6 +32,8 @@ static int comp_hashnodes (const void *, const void *);
 static int collect_ht_nodes (cpp_reader *, cpp_hashnode *, void *);
 static int write_defs (cpp_reader *, cpp_hashnode *, void *);
 static int save_macros (cpp_reader *, cpp_hashnode *, void *);
+static int _cpp_save_pushed_macros (cpp_reader *, FILE *);
+static int _cpp_restore_pushed_macros (cpp_reader *, FILE *);
 
 /* This structure represents a macro definition on disk.  */
 struct macrodef_struct
@@ -56,7 +57,9 @@ write_macdef (cpp_reader *pfile, cpp_hashnode *hn, void *file_p)
        return 1;
 
     case NT_MACRO:
-      if ((hn->flags & NODE_BUILTIN))
+      if ((hn->flags & NODE_BUILTIN)
+         && (!pfile->cb.user_builtin_macro
+             || !pfile->cb.user_builtin_macro (pfile, hn)))
        return 1;
 
       {
@@ -184,6 +187,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
@@ -195,7 +208,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.  */
@@ -333,6 +346,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);
@@ -378,9 +393,129 @@ cpp_write_pch_state (cpp_reader *r, FILE *f)
       return -1;
     }
 
+  /* Write saved macros.  */
+  if (! _cpp_save_pushed_macros (r, f))
+    {
+      cpp_errno (r, CPP_DL_ERROR, "while writing precompiled header");
+      return -1;
+    }
+
   return 0;
 }
 
+static int
+_cpp_restore_pushed_macros (cpp_reader *r, FILE *f)
+{
+  size_t count_saved = 0;
+  size_t i;
+  struct def_pragma_macro *p;
+  size_t nlen;
+  uchar *defn;
+  size_t defnlen;
+
+  if (fread (&count_saved, sizeof (count_saved), 1, f) != 1)
+    return 0;
+  if (! count_saved)
+    return 1;
+  for (i = 0; i < count_saved; i++)
+    {
+      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;
+      if (fread (&defnlen, sizeof (defnlen), 1, f) != 1)
+       return 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;
+
+         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);
+       }
+
+      p->next = r->pushed_macros;
+      r->pushed_macros = p;
+    }
+  return 1;
+}
+
+static int
+_cpp_save_pushed_macros (cpp_reader *r, FILE *f)
+{
+  size_t count_saved = 0;
+  size_t i;
+  struct def_pragma_macro *p,**pp;
+  size_t defnlen;
+
+  /* Get count. */
+  p = r->pushed_macros;
+  while (p != NULL)
+    {
+      count_saved++;
+      p = p->next;
+    }
+  if (fwrite (&count_saved, sizeof (count_saved), 1, f) != 1)
+    return 0;
+  if (!count_saved)
+    return 1;
+
+  pp = (struct def_pragma_macro **) alloca (sizeof (struct def_pragma_macro *)
+                                           * count_saved);
+  /* Store them in reverse order.  */
+  p = r->pushed_macros;
+  i = count_saved;
+  while (p != NULL)
+    {
+      --i;
+      pp[i] = p;
+      p = p->next;
+    }
+  for (i = 0; i < count_saved; i++)
+    {
+      defnlen = strlen (pp[i]->name);
+      if (fwrite (&defnlen, sizeof (size_t), 1, f) != 1
+         || fwrite (pp[i]->name, defnlen, 1, f) != 1)
+       return 0;
+      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;
+}
+
 
 /* Data structure to transform hash table nodes into a sorted list */
 
@@ -477,13 +612,34 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
 
       h = cpp_lookup (r, namebuf, m.name_length);
       if (m.flags & NODE_POISONED
-         || h->type != NT_MACRO
          || h->flags & NODE_POISONED)
        {
          if (CPP_OPTION (r, warn_invalid_pch))
-           cpp_error (r, CPP_DL_WARNING_SYSHDR,
-                      "%s: not used because `%.*s' not defined",
-                      name, m.name_length, namebuf);
+           cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
+                               "%s: not used because `%.*s' is poisoned",
+                               name, m.name_length, namebuf);
+         goto fail;
+       }
+
+      if (h->type != NT_MACRO)
+       {
+         /* 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
+            what asm to emit, won't issue .cfi directives when the
+            compiler doesn't.  */
+         if (!(h->flags & NODE_USED)
+             && m.name_length == sizeof ("__GCC_HAVE_DWARF2_CFI_ASM") - 1
+             && !memcmp (namebuf, "__GCC_HAVE_DWARF2_CFI_ASM", m.name_length))
+           continue;
+
+         if (CPP_OPTION (r, warn_invalid_pch))
+           cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
+                               "%s: not used because `%.*s' not defined",
+                               name, m.name_length, namebuf);
          goto fail;
        }
 
@@ -493,7 +649,7 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
          || memcmp (namebuf, newdefn, m.definition_length) != 0)
        {
          if (CPP_OPTION (r, warn_invalid_pch))
-           cpp_error (r, CPP_DL_WARNING_SYSHDR,
+           cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
               "%s: not used because `%.*s' defined as `%s' not `%.*s'",
                       name, m.name_length, namebuf, newdefn + m.name_length,
                       m.definition_length - m.name_length,
@@ -534,9 +690,9 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
       else
        {
          if (CPP_OPTION (r, warn_invalid_pch))
-           cpp_error (r, CPP_DL_WARNING_SYSHDR,
-                      "%s: not used because `%s' is defined",
-                      name, first);
+           cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
+                               "%s: not used because `%s' is defined",
+                               name, first);
          goto fail;
        }
     }
@@ -554,9 +710,9 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
   if (counter && r->counter)
     {
       if (CPP_OPTION (r, warn_invalid_pch))
-       cpp_error (r, CPP_DL_WARNING_SYSHDR,
-                  "%s: not used because `__COUNTER__' is invalid",
-                  name);
+       cpp_warning_syshdr (r, CPP_W_INVALID_PCH,
+                           "%s: not used because `__COUNTER__' is invalid",
+                           name);
        goto fail;
     }
 
@@ -565,15 +721,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;
 }
 
@@ -605,6 +757,12 @@ 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->flags & NODE_BUILTIN)
+      && h->type == NT_MACRO
+      && r->cb.user_builtin_macro)
+    r->cb.user_builtin_macro (r, h);
+
   if (h->type != NT_VOID
       && (h->flags & NODE_BUILTIN) == 0)
     {
@@ -731,6 +889,9 @@ cpp_read_state (cpp_reader *r, const char *name, FILE *f,
   if (!r->counter)
     r->counter = counter;
 
+  /* Read pushed macros. */
+  if (! _cpp_restore_pushed_macros (r, f))
+    goto error;
   return 0;
 
  error: