From c0d578e68f92d8add050826588146be0a6fd1ee0 Mon Sep 17 00:00:00 2001
From: Geoffrey Keating <geoffk@apple.com>
Date: Tue, 22 Jun 2004 06:51:56 +0000
Subject: [PATCH] c-opts.c (c_common_handle_option): Handle -fpch-preprocess.

2004-06-21  Geoffrey Keating  <geoffk@apple.com>

	* c-opts.c (c_common_handle_option): Handle -fpch-preprocess.
	* c-common.h (flag_pch_preprocess): Declare.
	(c_common_pch_pragma): Likewise.
	* c-common.c (flag_pch_preprocess): New.
	* c-pch.c (c_common_read_pch): Support -fpreprocess-only.
	(c_common_pch_pragma): New.
	* c-ppoutput.c (cb_read_pch): New.
	(init_pp_output): Support -fpch-preprocess.
	* c-pragma.c (init_pragma): Support #pragma GNUC pch_preprocess.
	* c.opt (fpch-preprocess): New.
	* gcc.c (cpp_options): When save-temps, pass -fpch-preprocess.
	* doc/cppopts.texi: Document -fpch-preprocess.
	* doc/invoke.texi (Precompiled Headers): Mention that
	-fpreprocessed is safe for PCH.  Mention that if an option is
	listed as safe that doesn't mean it does what you expect.

Index: gcc/testsuite/ChangeLog
2004-06-21  Geoffrey Keating  <geoffk@apple.com>

	* gcc.dg/pch/save-temps-1.c: New file.
	* gcc.dg/pch/save-temps-1.hs: New file.

Index: libcpp/ChangeLog
2004-06-21  Geoffrey Keating  <geoffk@apple.com>

	* files.c (should_stack_file): Correct swapped parameters to call
	to cb.read_pch.
	* pch.c (cpp_valid_state): Handle -fpreprocessed.

From-SVN: r83478
---
 gcc/ChangeLog                            | 18 ++++++
 gcc/c-common.c                           |  4 ++
 gcc/c-common.h                           |  7 +++
 gcc/c-opts.c                             |  4 ++
 gcc/c-pch.c                              | 78 ++++++++++++++++++++----
 gcc/c-ppoutput.c                         | 22 +++++++
 gcc/c-pragma.c                           |  2 +
 gcc/c.opt                                |  4 ++
 gcc/doc/cppopts.texi                     | 18 ++++++
 gcc/doc/invoke.texi                      |  8 ++-
 gcc/gcc.c                                |  3 +-
 gcc/testsuite/ChangeLog                  |  5 ++
 gcc/testsuite/gcc.dg/pch/save-temps-1.c  |  7 +++
 gcc/testsuite/gcc.dg/pch/save-temps-1.hs |  5 ++
 libcpp/ChangeLog                         |  6 ++
 libcpp/files.c                           |  2 +-
 libcpp/pch.c                             | 11 +++-
 17 files changed, 188 insertions(+), 16 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pch/save-temps-1.c
 create mode 100644 gcc/testsuite/gcc.dg/pch/save-temps-1.hs

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index b2659c10c290..88a9d9575cb8 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,21 @@
+2004-06-21  Geoffrey Keating  <geoffk@apple.com>
+
+	* c-opts.c (c_common_handle_option): Handle -fpch-preprocess.
+	* c-common.h (flag_pch_preprocess): Declare.
+	(c_common_pch_pragma): Likewise.
+	* c-common.c (flag_pch_preprocess): New.
+	* c-pch.c (c_common_read_pch): Support -fpreprocess-only.
+	(c_common_pch_pragma): New.
+	* c-ppoutput.c (cb_read_pch): New.
+	(init_pp_output): Support -fpch-preprocess.
+	* c-pragma.c (init_pragma): Support #pragma GNUC pch_preprocess.
+	* c.opt (fpch-preprocess): New.
+	* gcc.c (cpp_options): When save-temps, pass -fpch-preprocess.
+	* doc/cppopts.texi: Document -fpch-preprocess.
+	* doc/invoke.texi (Precompiled Headers): Mention that
+	-fpreprocessed is safe for PCH.  Mention that if an option is
+	listed as safe that doesn't mean it does what you expect.
+
 2004-06-22  Ben Elliston  <bje@au.ibm.com>
 
 	* tree-ssa.c (ssa_redirect_edge): Correct leading comment.
diff --git a/gcc/c-common.c b/gcc/c-common.c
index 79d9d9ac8597..ab5b2ae3b499 100644
--- a/gcc/c-common.c
+++ b/gcc/c-common.c
@@ -217,6 +217,10 @@ char flag_dump_macros;
 
 char flag_dump_includes;
 
+/* Nonzero means process PCH files while preprocessing.  */
+
+bool flag_pch_preprocess;
+
 /* The file name to which we should write a precompiled header, or
    NULL if no header will be written in this compile.  */
 
diff --git a/gcc/c-common.h b/gcc/c-common.h
index bd0a5b36a0e1..03c6c75e1ea1 100644
--- a/gcc/c-common.h
+++ b/gcc/c-common.h
@@ -350,6 +350,10 @@ extern char flag_dump_macros;
 
 extern char flag_dump_includes;
 
+/* Nonzero means process PCH files while preprocessing.  */
+
+extern bool flag_pch_preprocess;
+
 /* The file name to which we should write a precompiled header, or
    NULL if no header will be written in this compile.  */
 
@@ -1096,12 +1100,15 @@ extern void c_genericize (tree);
 extern int c_gimplify_expr (tree *, tree *, tree *);
 extern tree c_build_bind_expr (tree, tree);
 
+/* In c-pch.c  */
 extern void pch_init (void);
 extern int c_common_valid_pch (cpp_reader *pfile, const char *name, int fd);
 extern void c_common_read_pch (cpp_reader *pfile, const char *name, int fd,
 			       const char *orig);
 extern void c_common_write_pch (void);
 extern void c_common_no_more_pch (void);
+extern void c_common_pch_pragma (cpp_reader *pfile);
+
 extern void builtin_define_with_value (const char *, const char *, int);
 extern void c_stddef_cpp_builtins (void);
 extern void fe_file_change (const struct line_map *);
diff --git a/gcc/c-opts.c b/gcc/c-opts.c
index 8025cd7897cf..2ae9c1336452 100644
--- a/gcc/c-opts.c
+++ b/gcc/c-opts.c
@@ -879,6 +879,10 @@ c_common_handle_option (size_t scode, const char *arg, int value)
       cpp_opts->restore_pch_deps = value;
       break;
 
+    case OPT_fpch_preprocess:
+      flag_pch_preprocess = value;
+      break;
+
     case OPT_fpermissive:
       flag_permissive = value;
       break;
diff --git a/gcc/c-pch.c b/gcc/c-pch.c
index 03cf3bf08bca..eb043bd089ea 100644
--- a/gcc/c-pch.c
+++ b/gcc/c-pch.c
@@ -393,8 +393,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");
@@ -412,18 +410,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);
 
@@ -446,3 +456,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);
+}
diff --git a/gcc/c-ppoutput.c b/gcc/c-ppoutput.c
index ef6d8e6d4f3a..06816e9b885e 100644
--- a/gcc/c-ppoutput.c
+++ b/gcc/c-ppoutput.c
@@ -57,6 +57,8 @@ static void cb_include (cpp_reader *, fileline, const unsigned char *,
 			const char *, int);
 static void cb_ident (cpp_reader *, fileline, const cpp_string *);
 static void cb_def_pragma (cpp_reader *, fileline);
+static void cb_read_pch (cpp_reader *pfile, const char *name,
+			 int fd, const char *orig_name);
 
 /* Preprocess and output.  */
 void
@@ -106,6 +108,12 @@ init_pp_output (FILE *out_stream)
   if (flag_dump_includes)
     cb->include  = cb_include;
 
+  if (flag_pch_preprocess)
+    {
+      cb->valid_pch = c_common_valid_pch;
+      cb->read_pch = cb_read_pch;
+    }
+
   if (flag_dump_macros == 'N' || flag_dump_macros == 'D')
     {
       cb->define = cb_define;
@@ -416,3 +424,17 @@ dump_macro (cpp_reader *pfile, cpp_hashnode *node, void *v ATTRIBUTE_UNUSED)
 
   return 1;
 }
+
+/* Load in the PCH file NAME, open on FD.  It was originally searched for
+   by ORIG_NAME.  Also, print out a #include command so that the PCH
+   file can be loaded when the preprocessed output is compiled.  */
+
+static void
+cb_read_pch (cpp_reader *pfile, const char *name,
+	     int fd, const char *orig_name ATTRIBUTE_UNUSED)
+{
+  c_common_read_pch (pfile, name, fd, orig_name);
+  
+  fprintf (print.outf, "#pragma GCC pch_preprocess \"%s\"\n", name);
+  print.src_line++;
+}
diff --git a/gcc/c-pragma.c b/gcc/c-pragma.c
index 93836ea8652a..ba59fb3a589a 100644
--- a/gcc/c-pragma.c
+++ b/gcc/c-pragma.c
@@ -589,6 +589,8 @@ init_pragma (void)
   c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
 
+  c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma);
+
 #ifdef REGISTER_TARGET_PRAGMAS
   REGISTER_TARGET_PRAGMAS ();
 #endif
diff --git a/gcc/c.opt b/gcc/c.opt
index f16280300902..8898efdfd3e0 100644
--- a/gcc/c.opt
+++ b/gcc/c.opt
@@ -599,6 +599,10 @@ Enable optional diagnostics
 fpch-deps
 C ObjC C++ ObjC++
 
+fpch-preprocess
+C ObjC C++ ObjC++
+Look for and use PCH files even when preprocessing
+
 fpermissive
 C++ ObjC++
 Downgrade conformance errors to warnings
diff --git a/gcc/doc/cppopts.texi b/gcc/doc/cppopts.texi
index 468f17f13fc8..1b6307b2215d 100644
--- a/gcc/doc/cppopts.texi
+++ b/gcc/doc/cppopts.texi
@@ -323,6 +323,24 @@ precompiled header would be listed and not the files that were used to
 create it because those files are not consulted when a precompiled
 header is used.
 
+@item -fpch-preprocess
+@opindex fpch-preprocess
+This option allows use of a precompiled header (@pxref{Precompiled
+Headers}) together with @option{-E}.  It inserts a special @code{#pragma},
+@code{#pragma GCC pch_preprocess "<filename>"} in the output to mark
+the place where the precompiled header was found, and its filename.  When
+@code{-fpreprocessed} is in use, GCC recognizes this @code{#pragma} and
+loads the PCH.
+
+This option is off by default, because the resulting preprocessed output
+is only really suitable as input to GCC.  It is switched on by
+@option{-save-temps}.
+
+You should not write this @code{#pragma} in your own code, but it is
+safe to edit the filename if the PCH file is available in a different
+location.  The filename may be absolute or it may be relative to GCC's
+current directory.
+
 @end ifclear
 @item -x c
 @itemx -x c++
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 2dd2b8239d75..fcfb521812f3 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -11809,7 +11809,7 @@ which options are safe to change and which are not; the safest choice
 is to use exactly the same options when generating and using the
 precompiled header.  The following are known to be safe:
 
-@gccoptlist{-pedantic-errors}
+@gccoptlist{-fpreprocessed -pedantic-errors}
 
 @end itemize
 
@@ -11819,6 +11819,12 @@ find an option combination that doesn't work and doesn't cause the
 precompiled header to be ignored, please consider filing a bug report,
 see @ref{Bugs}.
 
+If you do use differing options when generating and using the
+precompiled header, the actual behaviour will be a mixture of the
+behaviour for the options.  For instance, if you use @option{-g} to
+generate the precompiled header but not when using it, you may or may
+not get debugging information for routines in the precompiled header.
+
 @node Running Protoize
 @section Running Protoize
 
diff --git a/gcc/gcc.c b/gcc/gcc.c
index e377dc37923c..677d9e74c371 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -783,7 +783,8 @@ static const char *cpp_unique_options =
    in turn cause preprocessor symbols to be defined specially.  */
 static const char *cpp_options =
 "%(cpp_unique_options) %1 %{m*} %{std*} %{ansi} %{W*&pedantic*} %{w} %{f*}\
- %{g*:%{!g0:%{!fno-working-directory:-fworking-directory}}} %{O*} %{undef}";
+ %{g*:%{!g0:%{!fno-working-directory:-fworking-directory}}} %{O*} %{undef}\
+ %{save-temps:-fpch-preprocess}";
 
 /* This contains cpp options which are not passed when the preprocessor
    output will be used by another program.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 73cebd14a476..cbc1a4b84481 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2004-06-21  Geoffrey Keating  <geoffk@apple.com>
+
+	* gcc.dg/pch/save-temps-1.c: New file.
+	* gcc.dg/pch/save-temps-1.hs: New file.
+
 2004-06-22  Janne Blomqvist  <jblomqvi@cc.hut.fi>
 
 	* gfortran.fortran-torture/execute/iolength_1.f90: New test.
diff --git a/gcc/testsuite/gcc.dg/pch/save-temps-1.c b/gcc/testsuite/gcc.dg/pch/save-temps-1.c
new file mode 100644
index 000000000000..87cedb33d80c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pch/save-temps-1.c
@@ -0,0 +1,7 @@
+/* { dg-options "-I. -save-temps" } */
+#include "save-temps-1.h"
+#ifndef T
+#error T not defined
+#endif
+#include <stddef.h>
+int x;
diff --git a/gcc/testsuite/gcc.dg/pch/save-temps-1.hs b/gcc/testsuite/gcc.dg/pch/save-temps-1.hs
new file mode 100644
index 000000000000..2fe5a3ee726d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pch/save-temps-1.hs
@@ -0,0 +1,5 @@
+#define T 123
+int foo (void) 
+{
+  return 3;
+}
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index 236c0685e4db..897d4ac0b268 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,3 +1,9 @@
+2004-06-21  Geoffrey Keating  <geoffk@apple.com>
+
+	* files.c (should_stack_file): Correct swapped parameters to call
+	to cb.read_pch.
+	* pch.c (cpp_valid_state): Handle -fpreprocessed.
+
 2004-06-15  Paolo Bonzini  <bonzini@gnu.org>
 
 	* Makefile.in: Regenerate with automake 1.8.5.
diff --git a/libcpp/files.c b/libcpp/files.c
index 6a18d71a8966..068065279baa 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -625,7 +625,7 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
   /* Handle PCH files immediately; don't stack them.  */
   if (file->pch)
     {
-      pfile->cb.read_pch (pfile, file->path, file->fd, file->pchname);
+      pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path);
       close (file->fd);
       file->fd = -1;
       return false;
diff --git a/libcpp/pch.c b/libcpp/pch.c
index a9d139a30b76..a343cd857f01 100644
--- a/libcpp/pch.c
+++ b/libcpp/pch.c
@@ -438,13 +438,22 @@ cpp_valid_state (cpp_reader *r, const char *name, int fd)
       if (m.name_length == 0)
 	break;
 
+      /* If this file is already preprocessed, there won't be any
+	 macros defined, and that's OK.  */
+      if (CPP_OPTION (r, preprocessed))
+	{
+	  if (lseek (fd, m.definition_length, SEEK_CUR) == -1)
+	    goto error;
+	  continue;
+	}
+
       if (m.definition_length > namebufsz)
 	{
 	  free (namebuf);
 	  namebufsz = m.definition_length + 256;
 	  namebuf = xmalloc (namebufsz);
 	}
-      
+
       if ((size_t)read (fd, namebuf, m.definition_length) 
 	  != m.definition_length)
 	goto error;
-- 
GitLab