summaryrefslogtreecommitdiff
path: root/gcc/c-pch.c
blob: 616e002a8284cbeb9d4a35f1255f8c6255ddc095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* Precompiled header implementation for the C languages.
   Copyright (C) 2000, 2002 Free Software Foundation, Inc.

This file is part of GNU CC.

GNU CC 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 later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
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 GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "cpplib.h"
#include "tree.h"
#include "c-common.h"
#include "output.h"
#include "toplev.h"
#include "debug.h"
#include "c-pragma.h"
#include "ggc.h"

struct c_pch_header 
{
  unsigned long asm_size;
};

static const char pch_ident[8] = "gpchC010";

static FILE *pch_outfile;

extern char *asm_file_name;
static off_t asm_file_startpos;

void
pch_init ()
{
  FILE *f;
  
  if (pch_file)
    {
      /* We're precompiling a header file, so when it's actually used,
	 it'll be at least one level deep.  */
      (*debug_hooks->start_source_file) (lineno, input_filename);

      f = fopen (pch_file, "w+b");
      if (f == NULL)
	fatal_io_error ("can't open %s", pch_file);
      pch_outfile = f;
      
      if (fwrite (pch_ident, sizeof (pch_ident), 1, f) != 1)
	fatal_io_error ("can't write to %s", pch_file);

      /* We need to be able to re-read the output.  */
      /* The driver always provides a valid -o option.  */
      if (asm_file_name == NULL
	  || strcmp (asm_file_name, "-") == 0)
	fatal_error ("`%s' is not a valid output file", asm_file_name);

      asm_file_startpos = ftello (asm_out_file);
      
      cpp_save_state (parse_in, f);
    }
}

void
c_common_write_pch ()
{
  char *buf;
  off_t asm_file_end;
  off_t written;
  struct c_pch_header h;

  cpp_write_pch_deps (parse_in, pch_outfile);

  asm_file_end = ftello (asm_out_file);
  h.asm_size = asm_file_end - asm_file_startpos;
  
  if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
    fatal_io_error ("can't write %s", pch_file);
  
  buf = xmalloc (16384);
  fflush (asm_out_file);

  if (fseeko (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
    fatal_io_error ("can't seek in %s", asm_file_name);

  for (written = asm_file_startpos; written < asm_file_end; )
    {
      off_t size = asm_file_end - written;
      if (size > 16384)
	size = 16384;
      if (fread (buf, size, 1, asm_out_file) != 1)
	fatal_io_error ("can't read %s", asm_file_name);
      if (fwrite (buf, size, 1, pch_outfile) != 1)
	fatal_io_error ("can't write %s", pch_file);
      written += size;
    }
  free (buf);

  gt_pch_save (pch_outfile);
  cpp_write_pch_state (parse_in, pch_outfile);

  fclose (pch_outfile);
}

int
c_common_valid_pch (pfile, name, fd)
     cpp_reader *pfile;
     const char *name;
     int fd;
{
  int sizeread;
  int result;
  char ident[sizeof (pch_ident)];

  if (! allow_pch)
    return 2;

  /* Perform a quick test of whether this is a valid
     precompiled header for C.  */

  sizeread = read (fd, ident, sizeof (pch_ident));
  if (sizeread == -1)
    {
      fatal_io_error ("can't read %s", name);
      return 2;
    }
  else if (sizeread != sizeof (pch_ident))
    return 2;
  
  if (memcmp (ident, pch_ident, sizeof (pch_ident)) != 0)
    {
      if (cpp_get_options (pfile)->warn_invalid_pch)
	{
	  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, 
		       "%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 C language", name);
	  else 
	    /* Not any kind of PCH.  */
	    cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
	}
      return 2;
    }

  /* Check the preprocessor macros are the same as when the PCH was
     generated.  */
  
  result = cpp_valid_state (pfile, name, fd);
  if (result == -1)
    return 2;
  else
    return result == 0;
}

void
c_common_read_pch (pfile, name, fd, orig_name)
     cpp_reader *pfile;
     const char *name;
     int fd;
     const char *orig_name;
{
  FILE *f;
  struct c_pch_header h;
  char *buf;
  unsigned long written;
  struct save_macro_data *smd;
  
  /* Before we wrote the file, we started a source file, so we have to start
     one here to match.  */
  (*debug_hooks->start_source_file) (lineno, orig_name);
  
  f = fdopen (fd, "rb");
  if (f == NULL)
    {
      cpp_errno (pfile, DL_ERROR, "calling fdopen");
      return;
    }

  allow_pch = 0;

  if (fread (&h, sizeof (h), 1, f) != 1)
    {
      cpp_errno (pfile, DL_ERROR, "reading");
      return;
    }

  buf = xmalloc (16384);
  for (written = 0; written < h.asm_size; )
    {
      off_t 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;
    }
  free (buf);

  cpp_prepare_state (pfile, &smd);

  gt_pch_restore (f);

  if (cpp_read_state (pfile, name, f, smd) != 0)
    return;

  fclose (f);

  (*debug_hooks->end_source_file) (lineno);
}