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