OSDN Git Service

* c-tree.h (enum c_storage_class): New.
[pf3gnuchains/gcc-fork.git] / gcc / c-pch.c
index 8e2e6f1..7371dba 100644 (file)
@@ -129,10 +129,10 @@ pch_init (void)
   if (f == NULL)
     fatal_error ("can't create precompiled header %s: %m", pch_file);
   pch_outfile = f;
-  
-  if (strlen (host_machine) > 255 || strlen (target_machine) > 255
-      || strlen (version_string) > 255)
-    abort ();
+
+  gcc_assert (strlen (host_machine) < 256
+             && strlen (target_machine) < 256
+             && strlen (version_string) < 256);
   
   v.host_machine_length = strlen (host_machine);
   v.target_machine_length = strlen (target_machine);
@@ -143,8 +143,7 @@ pch_init (void)
     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 ();
+       gcc_assert (v.match[i] == *pch_matching[i].flag_var);
       }
   }
   v.pch_init = &pch_init;
@@ -194,7 +193,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);
@@ -211,8 +209,10 @@ 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);
@@ -383,6 +383,10 @@ c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
     return result == 0;
 }
 
+/* If non-NULL, this function is called after a precompile header file
+   is loaded.  */
+void (*lang_post_pch_load) (void);
+
 /* Load in the PCH file NAME, open on FD.  It was originally searched for
    by ORIG_NAME.  */
 
@@ -392,8 +396,6 @@ 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");
@@ -411,18 +413,30 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
       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, CPP_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);
 
@@ -432,6 +446,11 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
     return;
 
   fclose (f);
+  
+  /* Give the front end a chance to take action after a PCH file has
+     been loaded.  */
+  if (lang_post_pch_load)
+    (*lang_post_pch_load) ();
 }
 
 /* Indicate that no more PCH files should be read.  */
@@ -445,3 +464,47 @@ c_common_no_more_pch (void)
       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);
+}