From d4c32e1d76fcbda6dc861b1867d55d5ca0acce8e Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Tue, 1 Apr 2008 12:58:02 +0200
Subject: [PATCH] re PR pch/13675 (#including a precompiled header more than
 once in the same unit fails)

	PR pch/13675
	* files.c (struct _cpp_file): Remove pch field.
	(pch_open_file): Don't set file->pch, just file->pchname.
	(should_stack_file): After pfile->cb.read_pch call
	free pchname and clear pchname, don't close file->fd.
	Test file->pchname instead of file->pch.  Don't close fd after cb.
	(_cpp_stack_include): Test file->pchname instead of file->pch.

	* c-pch.c (c_common_read_pch): On error close (fd) resp. fclose (f).

From-SVN: r133790
---
 gcc/ChangeLog    |  5 +++++
 gcc/c-pch.c      |  7 ++++++-
 libcpp/ChangeLog | 10 ++++++++++
 libcpp/files.c   | 16 ++++++----------
 4 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/gcc/ChangeLog b/gcc/ChangeLog
index f5ccc97d0630..0ba7058737f0 100644
--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,8 @@
+2008-04-01  Jakub Jelinek  <jakub@redhat.com>
+
+	PR pch/13675
+	* c-pch.c (c_common_read_pch): On error close (fd) resp. fclose (f).
+
 2008-04-01  Rafael Espindola  <espindola@google.com>
 
 	* tree-vrp.c (extract_code_and_val_from_cond_with_ops): New.
diff --git a/gcc/c-pch.c b/gcc/c-pch.c
index 691e12a2f797..0da17f7f24ba 100644
--- a/gcc/c-pch.c
+++ b/gcc/c-pch.c
@@ -373,6 +373,7 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
   if (f == NULL)
     {
       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
+      close (fd);
       return;
     }
 
@@ -381,6 +382,7 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
   if (fread (&h, sizeof (h), 1, f) != 1)
     {
       cpp_errno (pfile, CPP_DL_ERROR, "reading");
+      fclose (f);
       return;
     }
 
@@ -417,7 +419,10 @@ c_common_read_pch (cpp_reader *pfile, const char *name,
   gt_pch_restore (f);
 
   if (cpp_read_state (pfile, name, f, smd) != 0)
-    return;
+    {
+      fclose (f);
+      return;
+    }
 
   fclose (f);
 
diff --git a/libcpp/ChangeLog b/libcpp/ChangeLog
index 0b3ce8b3a0c0..e8719d931e1b 100644
--- a/libcpp/ChangeLog
+++ b/libcpp/ChangeLog
@@ -1,3 +1,13 @@
+2008-04-01  Jakub Jelinek  <jakub@redhat.com>
+
+	PR pch/13675
+	* files.c (struct _cpp_file): Remove pch field.
+	(pch_open_file): Don't set file->pch, just file->pchname.
+	(should_stack_file): After pfile->cb.read_pch call
+	free pchname and clear pchname, don't close file->fd.
+	Test file->pchname instead of file->pch.  Don't close fd after cb.
+	(_cpp_stack_include): Test file->pchname instead of file->pch.
+
 2008-03-28  Tom Tromey  <tromey@redhat.com>
 
 	* Makefile.in (POSTCOMPILE): New variable.
diff --git a/libcpp/files.c b/libcpp/files.c
index 467bb145ab9d..2bc3a801e356 100644
--- a/libcpp/files.c
+++ b/libcpp/files.c
@@ -1,6 +1,6 @@
 /* Part of CPP library.  File handling.
    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1998,
-   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
    Written by Per Bothner, 1994.
    Based on CCCP program by Paul Rubin, June 1986
@@ -106,9 +106,6 @@ struct _cpp_file
 
   /* If BUFFER above contains the true contents of the file.  */
   bool buffer_valid;
-
-  /* File is a PCH (on return from find_include_file).  */
-  bool pch;
 };
 
 /* A singly-linked list for all searches for a given file name, with
@@ -322,9 +319,7 @@ pch_open_file (cpp_reader *pfile, _cpp_file *file, bool *invalid_pch)
 	    }
 	  closedir (pchdir);
 	}
-      if (valid)
-	file->pch = true;
-      else
+      if (!valid)
 	*invalid_pch = true;
     }
 
@@ -703,11 +698,12 @@ should_stack_file (cpp_reader *pfile, _cpp_file *file, bool import)
     return false;
 
   /* Handle PCH files immediately; don't stack them.  */
-  if (file->pch)
+  if (file->pchname)
     {
       pfile->cb.read_pch (pfile, file->pchname, file->fd, file->path);
-      close (file->fd);
       file->fd = -1;
+      free ((void *) file->pchname);
+      file->pchname = NULL;
       return false;
     }
 
@@ -916,7 +912,7 @@ _cpp_stack_include (cpp_reader *pfile, const char *fname, int angle_brackets,
      complicates LAST_SOURCE_LINE_LOCATION.  This does not apply if we
      found a PCH file (in which case linemap_add is not called) or we
      were included from the command-line.  */
-  if (! file->pch && file->err_no == 0 && type != IT_CMDLINE)
+  if (file->pchname == NULL && file->err_no == 0 && type != IT_CMDLINE)
     pfile->line_table->highest_location--;
 
   return _cpp_stack_file (pfile, file, type == IT_IMPORT);
-- 
GitLab