OSDN Git Service

optimize
[pf3gnuchains/gcc-fork.git] / gcc / c-pch.c
index 2a29075..eb043bd 100644 (file)
@@ -1,5 +1,5 @@
 /* Precompiled header implementation for the C languages.
-   Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -35,6 +35,23 @@ Boston, MA 02111-1307, USA.  */
 #include "hosthooks.h"
 #include "target.h"
 
+/* This is a list of flag variables that must match exactly, and their
+   names for the error message.  The possible values for *flag_var must
+   fit in a 'signed char'.  */
+
+static const struct c_pch_matching 
+{
+  int *flag_var;
+  const char *flag_name;
+} pch_matching[] = {
+  { &flag_exceptions, "-fexceptions" },
+  { &flag_unit_at_a_time, "-funit-at-a-time" }
+};
+
+enum {
+  MATCH_SIZE = ARRAY_SIZE (pch_matching)
+};
+
 /* This structure is read very early when validating the PCH, and
    might be read for a PCH which is for a completely different compiler
    for a different operating system.  Thus, it should really only contain
@@ -52,6 +69,7 @@ struct c_pch_validity
   unsigned char target_machine_length;
   unsigned char version_length;
   unsigned char debug_info_type;
+  signed char match[MATCH_SIZE];
   void (*pch_init) (void);
   size_t target_data_length;
 };
@@ -102,13 +120,14 @@ pch_init (void)
   FILE *f;
   struct c_pch_validity v;
   void *target_validity;
+  static const char partial_pch[IDENT_LENGTH] = "gpcWrite";
   
   if (! pch_file)
     return;
   
   f = fopen (pch_file, "w+b");
   if (f == NULL)
-    fatal_error ("can't open %s: %m", pch_file);
+    fatal_error ("can't create precompiled header %s: %m", pch_file);
   pch_outfile = f;
   
   if (strlen (host_machine) > 255 || strlen (target_machine) > 255
@@ -119,10 +138,19 @@ pch_init (void)
   v.target_machine_length = strlen (target_machine);
   v.version_length = strlen (version_string);
   v.debug_info_type = write_symbols;
+  {
+    size_t i;
+    for (i = 0; i < MATCH_SIZE; i++)
+      {
+       v.match[i] = *pch_matching[i].flag_var;
+       if (v.match[i] != *pch_matching[i].flag_var)
+         abort ();
+      }
+  }
   v.pch_init = &pch_init;
   target_validity = targetm.get_pch_validity (&v.target_data_length);
   
-  if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1
+  if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
       || fwrite (&v, sizeof (v), 1, f) != 1
       || fwrite (host_machine, v.host_machine_length, 1, f) != 1
       || fwrite (target_machine, v.target_machine_length, 1, f) != 1
@@ -166,7 +194,6 @@ c_common_write_pch (void)
     fatal_error ("can't write %s: %m", pch_file);
   
   buf = xmalloc (16384);
-  fflush (asm_out_file);
 
   if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
     fatal_error ("can't seek in %s: %m", asm_file_name);
@@ -183,12 +210,18 @@ c_common_write_pch (void)
       written += size;
     }
   free (buf);
-  /* asm_out_file can be written afterwards, so must be flushed first.  */
-  fflush (asm_out_file);
+  /* asm_out_file can be written afterwards, so fseek to clear
+     _IOREAD flag.  */
+  if (fseek (asm_out_file, 0, SEEK_END) != 0)
+    fatal_error ("can't seek in %s: %m", asm_file_name);
 
   gt_pch_save (pch_outfile);
   cpp_write_pch_state (parse_in, pch_outfile);
 
+  if (fseek (pch_outfile, 0, SEEK_SET) != 0
+      || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
+    fatal_error ("can't write %s: %m", pch_file);
+
   fclose (pch_outfile);
 }
 
@@ -225,15 +258,15 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
          if (memcmp (ident, pch_ident, 5) == 0)
            /* It's a PCH, for the right language, but has the wrong version.
             */
-           cpp_error (pfile, DL_WARNING, 
+           cpp_error (pfile, CPP_DL_WARNING, 
                       "%s: not compatible with this GCC version", name);
          else if (memcmp (ident, pch_ident, 4) == 0)
            /* It's a PCH for the wrong language.  */
-           cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
+           cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
                       lang_hooks.name);
          else 
            /* Not any kind of PCH.  */
-           cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
+           cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
        }
       return 2;
     }
@@ -251,7 +284,7 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
       || memcmp (host_machine, short_strings, strlen (host_machine)) != 0)
     {
       if (cpp_get_options (pfile)->warn_invalid_pch)
-       cpp_error (pfile, DL_WARNING, 
+       cpp_error (pfile, CPP_DL_WARNING, 
                   "%s: created on host `%.*s', but used on host `%s'", name,
                   v.host_machine_length, short_strings, host_machine);
       return 2;
@@ -261,7 +294,7 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
                 strlen (target_machine)) != 0)
     {
       if (cpp_get_options (pfile)->warn_invalid_pch)
-       cpp_error (pfile, DL_WARNING, 
+       cpp_error (pfile, CPP_DL_WARNING, 
                   "%s: created for target `%.*s', but used for target `%s'", 
                   name, v.target_machine_length, 
                   short_strings + v.host_machine_length, target_machine);
@@ -274,7 +307,7 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
                 v.version_length) != 0)
     {
       if (cpp_get_options (pfile)->warn_invalid_pch)
-       cpp_error (pfile, DL_WARNING,
+       cpp_error (pfile, CPP_DL_WARNING,
                   "%s: created by version `%.*s', but this is version `%s'", 
                   name, v.version_length, 
                   (short_strings + v.host_machine_length 
@@ -290,13 +323,27 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
       && write_symbols != NO_DEBUG)
     {
       if (cpp_get_options (pfile)->warn_invalid_pch)
-       cpp_error (pfile, DL_WARNING, 
+       cpp_error (pfile, CPP_DL_WARNING, 
                   "%s: created with -g%s, but used with -g%s", name,
                   debug_type_names[v.debug_info_type],
                   debug_type_names[write_symbols]);
       return 2;
     }
 
+  /* Check flags that must match exactly.  */
+  {
+    size_t i;
+    for (i = 0; i < MATCH_SIZE; i++)
+      if (*pch_matching[i].flag_var != v.match[i])
+       {
+         if (cpp_get_options (pfile)->warn_invalid_pch)
+           cpp_error (pfile, CPP_DL_WARNING, 
+                      "%s: settings for %s do not match", name,
+                      pch_matching[i].flag_name);
+         return 2;
+       }
+  }
+
   /* If the text segment was not loaded at the same address as it was
      when the PCH file was created, function pointers loaded from the
      PCH will not be valid.  We could in theory remap all the function
@@ -304,7 +351,7 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
   if (v.pch_init != &pch_init)
     {
       if (cpp_get_options (pfile)->warn_invalid_pch)
-       cpp_error (pfile, DL_WARNING, 
+       cpp_error (pfile, CPP_DL_WARNING, 
                   "%s: had text segment at different address", name);
       return 2;
     }
@@ -322,7 +369,7 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
     if (msg != NULL)
       {
        if (cpp_get_options (pfile)->warn_invalid_pch)
-         cpp_error (pfile, DL_WARNING, "%s: %s", name, msg);
+         cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
        return 2;
       }
   }
@@ -346,14 +393,12 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
 {
   FILE *f;
   struct c_pch_header h;
-  char *buf;
-  unsigned long written;
   struct save_macro_data *smd;
   
   f = fdopen (fd, "rb");
   if (f == NULL)
     {
-      cpp_errno (pfile, DL_ERROR, "calling fdopen");
+      cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
       return;
     }
 
@@ -361,22 +406,34 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
 
   if (fread (&h, sizeof (h), 1, f) != 1)
     {
-      cpp_errno (pfile, DL_ERROR, "reading");
+      cpp_errno (pfile, CPP_DL_ERROR, "reading");
       return;
     }
 
-  buf = xmalloc (16384);
-  for (written = 0; written < h.asm_size; )
+  if (!flag_preprocess_only)
     {
-      long size = h.asm_size - written;
-      if (size > 16384)
-       size = 16384;
-      if (fread (buf, size, 1, f) != 1
-         || fwrite (buf, size, 1, asm_out_file) != 1)
-       cpp_errno (pfile, DL_ERROR, "reading");
-      written += size;
+      unsigned long written;
+      char * buf = xmalloc (16384);
+
+      for (written = 0; written < h.asm_size; )
+       {
+         long size = h.asm_size - written;
+         if (size > 16384)
+           size = 16384;
+         if (fread (buf, size, 1, f) != 1
+             || fwrite (buf, size, 1, asm_out_file) != 1)
+           cpp_errno (pfile, CPP_DL_ERROR, "reading");
+         written += size;
+       }
+      free (buf);
+    }
+  else
+    {
+      /* If we're preprocessing, don't write to a NULL
+        asm_out_file.  */
+      if (fseek (f, h.asm_size, SEEK_CUR) != 0)
+       cpp_errno (pfile, CPP_DL_ERROR, "seeking");
     }
-  free (buf);
 
   cpp_prepare_state (pfile, &smd);
 
@@ -396,6 +453,50 @@ c_common_no_more_pch (void)
   if (cpp_get_callbacks (parse_in)->valid_pch)
     {
       cpp_get_callbacks (parse_in)->valid_pch = NULL;
-      host_hooks.gt_pch_use_address (NULL, 0);
+      host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
     }
 }
+
+/* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
+
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif
+
+void
+c_common_pch_pragma (cpp_reader *pfile)
+{
+  tree name_t;
+  const char *name;
+  int fd;
+
+  if (c_lex (&name_t) != CPP_STRING)
+    {
+      error ("malformed #pragma GCC pch_preprocess, ignored");
+      return;
+    }
+
+  if (! cpp_get_options (pfile)->preprocessed)
+    {
+      error ("pch_preprocess pragma should only be used with -fpreprocessed");
+      inform ("use #include instead");
+      return;
+    }
+
+  name = TREE_STRING_POINTER (name_t);
+  
+  fd = open (name, O_RDONLY | O_BINARY, 0666);
+  if (fd == -1)
+    fatal_error ("%s: couldn't open PCH file: %m\n", name);
+  
+  if (c_common_valid_pch (pfile, name, fd) != 1)
+    {
+      if (!cpp_get_options (pfile)->warn_invalid_pch)
+       inform ("use -Winvalid-pch for more information");
+      fatal_error ("%s: PCH file was invalid", name);
+    }
+  
+  c_common_read_pch (pfile, name, fd, name);
+  
+  close (fd);
+}