c-pch.c: Include langhooks.h.
[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 GNU CC.
5
6 GNU CC 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 GNU CC 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 GNU CC; 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 (strcmp (lang_hooks.name, "GNU C") == 0)
56 result[4] = 'C';
57 else if (strcmp (lang_hooks.name, "GNU C++") == 0)
58 result[4] = '+';
59 else if (strcmp (lang_hooks.name, "GNU Objective-C") == 0)
60 result[4] = 'o';
61 else if (strcmp (lang_hooks.name, "GNU Objective-C++") == 0)
62 result[4] = 'O';
63 else
64 abort ();
65 return result;
66 }
67
68 void
69 pch_init ()
70 {
71 FILE *f;
72
73 if (pch_file)
74 {
75 /* We're precompiling a header file, so when it's actually used,
76 it'll be at least one level deep. */
77 (*debug_hooks->start_source_file) (lineno, input_filename);
78
79 f = fopen (pch_file, "w+b");
80 if (f == NULL)
81 fatal_io_error ("can't open %s", pch_file);
82 pch_outfile = f;
83
84 if (fwrite (get_ident(), IDENT_LENGTH, 1, f) != 1)
85 fatal_io_error ("can't write to %s", pch_file);
86
87 /* We need to be able to re-read the output. */
88 /* The driver always provides a valid -o option. */
89 if (asm_file_name == NULL
90 || strcmp (asm_file_name, "-") == 0)
91 fatal_error ("`%s' is not a valid output file", asm_file_name);
92
93 asm_file_startpos = ftell (asm_out_file);
94
95 cpp_save_state (parse_in, f);
96 }
97 }
98
99 void
100 c_common_write_pch ()
101 {
102 char *buf;
103 long asm_file_end;
104 long written;
105 struct c_pch_header h;
106
107 cpp_write_pch_deps (parse_in, pch_outfile);
108
109 asm_file_end = ftell (asm_out_file);
110 h.asm_size = asm_file_end - asm_file_startpos;
111
112 if (fwrite (&h, sizeof (h), 1, pch_outfile) != 1)
113 fatal_io_error ("can't write %s", pch_file);
114
115 buf = xmalloc (16384);
116 fflush (asm_out_file);
117
118 if (fseek (asm_out_file, asm_file_startpos, SEEK_SET) != 0)
119 fatal_io_error ("can't seek in %s", asm_file_name);
120
121 for (written = asm_file_startpos; written < asm_file_end; )
122 {
123 long size = asm_file_end - written;
124 if (size > 16384)
125 size = 16384;
126 if (fread (buf, size, 1, asm_out_file) != 1)
127 fatal_io_error ("can't read %s", asm_file_name);
128 if (fwrite (buf, size, 1, pch_outfile) != 1)
129 fatal_io_error ("can't write %s", pch_file);
130 written += size;
131 }
132 free (buf);
133
134 gt_pch_save (pch_outfile);
135 cpp_write_pch_state (parse_in, pch_outfile);
136
137 fclose (pch_outfile);
138 }
139
140 int
141 c_common_valid_pch (pfile, name, fd)
142 cpp_reader *pfile;
143 const char *name;
144 int fd;
145 {
146 int sizeread;
147 int result;
148 char ident[IDENT_LENGTH];
149 const char *pch_ident;
150
151 if (! allow_pch)
152 return 2;
153
154 /* Perform a quick test of whether this is a valid
155 precompiled header for C. */
156
157 sizeread = read (fd, ident, IDENT_LENGTH);
158 if (sizeread == -1)
159 {
160 fatal_io_error ("can't read %s", name);
161 return 2;
162 }
163 else if (sizeread != IDENT_LENGTH)
164 return 2;
165
166 pch_ident = get_ident();
167 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
168 {
169 if (cpp_get_options (pfile)->warn_invalid_pch)
170 {
171 if (memcmp (ident, pch_ident, 5) == 0)
172 /* It's a PCH, for the right language, but has the wrong version.
173 */
174 cpp_error (pfile, DL_WARNING,
175 "%s: not compatible with this GCC version", name);
176 else if (memcmp (ident, pch_ident, 4) == 0)
177 /* It's a PCH for the wrong language. */
178 cpp_error (pfile, DL_WARNING, "%s: not for %s", name,
179 lang_hooks.name);
180 else
181 /* Not any kind of PCH. */
182 cpp_error (pfile, DL_WARNING, "%s: not a PCH file", name);
183 }
184 return 2;
185 }
186
187 /* Check the preprocessor macros are the same as when the PCH was
188 generated. */
189
190 result = cpp_valid_state (pfile, name, fd);
191 if (result == -1)
192 return 2;
193 else
194 return result == 0;
195 }
196
197 void
198 c_common_read_pch (pfile, name, fd, orig_name)
199 cpp_reader *pfile;
200 const char *name;
201 int fd;
202 const char *orig_name;
203 {
204 FILE *f;
205 struct c_pch_header h;
206 char *buf;
207 unsigned long written;
208 struct save_macro_data *smd;
209
210 /* Before we wrote the file, we started a source file, so we have to start
211 one here to match. */
212 (*debug_hooks->start_source_file) (lineno, orig_name);
213
214 f = fdopen (fd, "rb");
215 if (f == NULL)
216 {
217 cpp_errno (pfile, DL_ERROR, "calling fdopen");
218 return;
219 }
220
221 allow_pch = 0;
222
223 if (fread (&h, sizeof (h), 1, f) != 1)
224 {
225 cpp_errno (pfile, DL_ERROR, "reading");
226 return;
227 }
228
229 buf = xmalloc (16384);
230 for (written = 0; written < h.asm_size; )
231 {
232 long size = h.asm_size - written;
233 if (size > 16384)
234 size = 16384;
235 if (fread (buf, size, 1, f) != 1
236 || fwrite (buf, size, 1, asm_out_file) != 1)
237 cpp_errno (pfile, DL_ERROR, "reading");
238 written += size;
239 }
240 free (buf);
241
242 cpp_prepare_state (pfile, &smd);
243
244 gt_pch_restore (f);
245
246 if (cpp_read_state (pfile, name, f, smd) != 0)
247 return;
248
249 fclose (f);
250
251 (*debug_hooks->end_source_file) (lineno);
252 }