dbxout.c (scope_labelno): Add GTY.
[gcc.git] / gcc / c-pch.c
1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cpplib.h"
25 #include "tree.h"
26 #include "c-common.h"
27 #include "output.h"
28 #include "toplev.h"
29 #include "debug.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33
34 struct c_pch_header
35 {
36 unsigned long asm_size;
37 };
38
39 #define IDENT_LENGTH 8
40
41 static FILE *pch_outfile;
42
43 extern char *asm_file_name;
44 static long asm_file_startpos;
45
46 static const char * get_ident PARAMS((void));
47
48 static const char *
49 get_ident()
50 {
51 static char result[IDENT_LENGTH];
52 static const char template[IDENT_LENGTH] = "gpch.010";
53
54 memcpy (result, template, IDENT_LENGTH);
55 if (c_language == clk_c)
56 result[4] = flag_objc ? 'o' : 'C';
57 else if (c_language == clk_cplusplus)
58 result[4] = flag_objc ? 'O' : '+';
59 else
60 abort ();
61 return result;
62 }
63
64 void
65 pch_init ()
66 {
67 FILE *f;
68
69 if (pch_file)
70 {
71 f = fopen (pch_file, "w+b");
72 if (f == NULL)
73 fatal_io_error ("can't open %s", pch_file);
74 pch_outfile = f;
75
76 if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1)
77 fatal_io_error ("can't write to %s", pch_file);
78
79 /* We need to be able to re-read the output. */
80 /* The driver always provides a valid -o option. */
81 if (asm_file_name == NULL
82 || strcmp (asm_file_name, "-") == 0)
83 fatal_error ("`%s' is not a valid output file", asm_file_name);
84
85 asm_file_startpos = ftell (asm_out_file);
86
87 /* Let the debugging format deal with the PCHness. */
88 (*debug_hooks->handle_pch) (0);
89
90 cpp_save_state (parse_in, f);
91 }
92 }
93
94 void
95 c_common_write_pch ()
96 {
97 char *buf;
98 long asm_file_end;
99 long written;
100 struct c_pch_header h;
101
102 (*debug_hooks->handle_pch) (1);
103
104 cpp_write_pch_deps (parse_in, pch_outfile);
105
106 asm_file_end = ftell (asm_out_file);
107 h.asm_size = asm_file_end - asm_file_startpos;
108
109 if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
110 fatal_io_error ("can't write %s", pch_file);
111
112 buf = xmalloc (16384);
113 fflush (asm_out_file);
114
115 if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
116 fatal_io_error ("can't seek in %s", asm_file_name);
117
118 for (written = asm_file_startpos; written < asm_file_end; )
119 {
120 long size = asm_file_end - written;
121 if (size > 16384)
122 size = 16384;
123 if (fread (buf, size, 1, asm_out_file) != 1)
124 fatal_io_error ("can't read %s", asm_file_name);
125 if (fwrite (buf, size, 1, pch_outfile) != 1)
126 fatal_io_error ("can't write %s", pch_file);
127 written += size;
128 }
129 free (buf);
130
131 gt_pch_save (pch_outfile);
132 cpp_write_pch_state (parse_in, pch_outfile);
133
134 fclose (pch_outfile);
135 }
136
137 int
138 c_common_valid_pch (pfile, name, fd)
139 cpp_reader *pfile;
140 const char *name;
141 int fd;
142 {
143 int sizeread;
144 int result;
145 char ident[IDENT_LENGTH];
146 const char *pch_ident;
147
148 if (! allow_pch)
149 return 2;
150
151 /* Perform a quick test of whether this is a valid
152 precompiled header for C. */
153
154 sizeread = read (fd, ident, IDENT_LENGTH);
155 if (sizeread == -1)
156 {
157 fatal_io_error ("can't read %s", name);
158 return 2;
159 }
160 else if (sizeread != IDENT_LENGTH)
161 return 2;
162
163 pch_ident = get_ident();
164 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
165 {
166 if (cpp_get_options (pfile)->warn_invalid_pch)
167 {
168 if (memcmp (ident, pch_ident, 5) == 0)
169 /* It's a PCH, for the right language, but has the wrong version.
170 */
171 cpp_error (pfile, DL_WARNING,
172 "%s: not compatible with this GCC version", name);
173 else if (memcmp (ident, pch_ident, 4) == 0)
174 /* It's a PCH for the wrong language. */
175 cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
176 lang_hooks.name);
177 else
178 /* Not any kind of PCH. */
179 cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
180 }
181 return 2;
182 }
183
184 /* Check the preprocessor macros are the same as when the PCH was
185 generated. */
186
187 result = cpp_valid_state (pfile, name, fd);
188 if (result == -1)
189 return 2;
190 else
191 return result == 0;
192 }
193
194 void
195 c_common_read_pch (pfile, name, fd, orig_name)
196 cpp_reader *pfile;
197 const char *name;
198 int fd;
199 const char *orig_name ATTRIBUTE_UNUSED;
200 {
201 FILE *f;
202 struct c_pch_header h;
203 char *buf;
204 unsigned long written;
205 struct save_macro_data *smd;
206
207 f = fdopen (fd, "rb");
208 if (f == NULL)
209 {
210 cpp_errno (pfile, DL_ERROR, "calling fdopen");
211 return;
212 }
213
214 allow_pch = 0;
215
216 if (fread (&h, sizeof (h), 1, f) != 1)
217 {
218 cpp_errno (pfile, DL_ERROR, "reading");
219 return;
220 }
221
222 buf = xmalloc (16384);
223 for (written = 0; written < h.asm_size; )
224 {
225 long size = h.asm_size - written;
226 if (size > 16384)
227 size = 16384;
228 if (fread (buf, size, 1, f) != 1
229 || fwrite (buf, size, 1, asm_out_file) != 1)
230 cpp_errno (pfile, DL_ERROR, "reading");
231 written += size;
232 }
233 free (buf);
234
235 cpp_prepare_state (pfile, &smd);
236
237 gt_pch_restore (f);
238
239 if (cpp_read_state (pfile, name, f, smd) != 0)
240 return;
241
242 fclose (f);
243 }