c-pch.cc revision 1.1 1 1.1 mrg /* Precompiled header implementation for the C languages.
2 1.1 mrg Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 1.1 mrg
4 1.1 mrg This file is part of GCC.
5 1.1 mrg
6 1.1 mrg GCC is free software; you can redistribute it and/or modify
7 1.1 mrg it under the terms of the GNU General Public License as published by
8 1.1 mrg the Free Software Foundation; either version 3, or (at your option)
9 1.1 mrg any later version.
10 1.1 mrg
11 1.1 mrg GCC is distributed in the hope that it will be useful,
12 1.1 mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
13 1.1 mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 1.1 mrg GNU General Public License for more details.
15 1.1 mrg
16 1.1 mrg You should have received a copy of the GNU General Public License
17 1.1 mrg along with GCC; see the file COPYING3. If not see
18 1.1 mrg <http://www.gnu.org/licenses/>. */
19 1.1 mrg
20 1.1 mrg #include "config.h"
21 1.1 mrg #include "system.h"
22 1.1 mrg #include "coretypes.h"
23 1.1 mrg #include "target.h"
24 1.1 mrg #include "c-common.h"
25 1.1 mrg #include "timevar.h"
26 1.1 mrg #include "flags.h"
27 1.1 mrg #include "debug.h"
28 1.1 mrg #include "c-pragma.h"
29 1.1 mrg #include "langhooks.h"
30 1.1 mrg #include "hosthooks.h"
31 1.1 mrg
32 1.1 mrg /* This is a list of flag variables that must match exactly, and their
33 1.1 mrg names for the error message. The possible values for *flag_var must
34 1.1 mrg fit in a 'signed char'. */
35 1.1 mrg
36 1.1 mrg static const struct c_pch_matching
37 1.1 mrg {
38 1.1 mrg int *flag_var;
39 1.1 mrg const char *flag_name;
40 1.1 mrg } pch_matching[] = {
41 1.1 mrg { &flag_exceptions, "-fexceptions" },
42 1.1 mrg };
43 1.1 mrg
44 1.1 mrg enum {
45 1.1 mrg MATCH_SIZE = ARRAY_SIZE (pch_matching)
46 1.1 mrg };
47 1.1 mrg
48 1.1 mrg /* Information about flags and suchlike that affect PCH validity.
49 1.1 mrg
50 1.1 mrg Before this structure is read, both an initial 8-character identification
51 1.1 mrg string, and a 16-byte checksum, have been read and validated. */
52 1.1 mrg
53 1.1 mrg struct c_pch_validity
54 1.1 mrg {
55 1.1 mrg uint32_t pch_write_symbols;
56 1.1 mrg signed char match[MATCH_SIZE];
57 1.1 mrg size_t target_data_length;
58 1.1 mrg };
59 1.1 mrg
60 1.1 mrg #define IDENT_LENGTH 8
61 1.1 mrg
62 1.1 mrg /* The file we'll be writing the PCH to. */
63 1.1 mrg static FILE *pch_outfile;
64 1.1 mrg
65 1.1 mrg static const char *get_ident (void);
66 1.1 mrg
67 1.1 mrg /* Compute an appropriate 8-byte magic number for the PCH file, so that
68 1.1 mrg utilities like file(1) can identify it, and so that GCC can quickly
69 1.1 mrg ignore non-PCH files and PCH files that are of a completely different
70 1.1 mrg format. */
71 1.1 mrg
72 1.1 mrg static const char *
73 1.1 mrg get_ident (void)
74 1.1 mrg {
75 1.1 mrg static char result[IDENT_LENGTH];
76 1.1 mrg static const char templ[] = "gpch.014";
77 1.1 mrg static const char c_language_chars[] = "Co+O";
78 1.1 mrg
79 1.1 mrg memcpy (result, templ, IDENT_LENGTH);
80 1.1 mrg result[4] = c_language_chars[c_language];
81 1.1 mrg
82 1.1 mrg return result;
83 1.1 mrg }
84 1.1 mrg
85 1.1 mrg /* Whether preprocessor state should be saved by pch_init. */
86 1.1 mrg
87 1.1 mrg static bool pch_ready_to_save_cpp_state = false;
88 1.1 mrg
89 1.1 mrg /* Prepare to write a PCH file, if one is being written. This is
90 1.1 mrg called at the start of compilation. */
91 1.1 mrg
92 1.1 mrg void
93 1.1 mrg pch_init (void)
94 1.1 mrg {
95 1.1 mrg FILE *f;
96 1.1 mrg struct c_pch_validity v;
97 1.1 mrg void *target_validity;
98 1.1 mrg static const char partial_pch[] = "gpcWrite";
99 1.1 mrg
100 1.1 mrg if (!pch_file)
101 1.1 mrg return;
102 1.1 mrg
103 1.1 mrg f = fopen (pch_file, "w+b");
104 1.1 mrg if (f == NULL)
105 1.1 mrg fatal_error (input_location, "cannot create precompiled header %s: %m",
106 1.1 mrg pch_file);
107 1.1 mrg pch_outfile = f;
108 1.1 mrg
109 1.1 mrg memset (&v, '\0', sizeof (v));
110 1.1 mrg v.pch_write_symbols = write_symbols;
111 1.1 mrg {
112 1.1 mrg size_t i;
113 1.1 mrg for (i = 0; i < MATCH_SIZE; i++)
114 1.1 mrg {
115 1.1 mrg v.match[i] = *pch_matching[i].flag_var;
116 1.1 mrg gcc_assert (v.match[i] == *pch_matching[i].flag_var);
117 1.1 mrg }
118 1.1 mrg }
119 1.1 mrg target_validity = targetm.get_pch_validity (&v.target_data_length);
120 1.1 mrg
121 1.1 mrg if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
122 1.1 mrg || fwrite (executable_checksum, 16, 1, f) != 1
123 1.1 mrg || fwrite (&v, sizeof (v), 1, f) != 1
124 1.1 mrg || fwrite (target_validity, v.target_data_length, 1, f) != 1)
125 1.1 mrg fatal_error (input_location, "cannot write to %s: %m", pch_file);
126 1.1 mrg
127 1.1 mrg /* Let the debugging format deal with the PCHness. */
128 1.1 mrg (*debug_hooks->handle_pch) (0);
129 1.1 mrg
130 1.1 mrg if (pch_ready_to_save_cpp_state)
131 1.1 mrg pch_cpp_save_state ();
132 1.1 mrg
133 1.1 mrg XDELETE (target_validity);
134 1.1 mrg }
135 1.1 mrg
136 1.1 mrg /* Whether preprocessor state has been saved in a PCH file. */
137 1.1 mrg
138 1.1 mrg static bool pch_cpp_state_saved = false;
139 1.1 mrg
140 1.1 mrg /* Save preprocessor state in a PCH file, after implicitly included
141 1.1 mrg headers have been read. If the PCH file has not yet been opened,
142 1.1 mrg record that state should be saved when it is opened. */
143 1.1 mrg
144 1.1 mrg void
145 1.1 mrg pch_cpp_save_state (void)
146 1.1 mrg {
147 1.1 mrg if (!pch_cpp_state_saved)
148 1.1 mrg {
149 1.1 mrg if (pch_outfile)
150 1.1 mrg {
151 1.1 mrg cpp_save_state (parse_in, pch_outfile);
152 1.1 mrg pch_cpp_state_saved = true;
153 1.1 mrg }
154 1.1 mrg else
155 1.1 mrg pch_ready_to_save_cpp_state = true;
156 1.1 mrg }
157 1.1 mrg }
158 1.1 mrg
159 1.1 mrg /* Write the PCH file. This is called at the end of a compilation which
160 1.1 mrg will produce a PCH file. */
161 1.1 mrg
162 1.1 mrg void
163 1.1 mrg c_common_write_pch (void)
164 1.1 mrg {
165 1.1 mrg timevar_push (TV_PCH_SAVE);
166 1.1 mrg
167 1.1 mrg targetm.prepare_pch_save ();
168 1.1 mrg
169 1.1 mrg (*debug_hooks->handle_pch) (1);
170 1.1 mrg
171 1.1 mrg prepare_target_option_nodes_for_pch ();
172 1.1 mrg
173 1.1 mrg cpp_write_pch_deps (parse_in, pch_outfile);
174 1.1 mrg
175 1.1 mrg gt_pch_save (pch_outfile);
176 1.1 mrg
177 1.1 mrg timevar_push (TV_PCH_CPP_SAVE);
178 1.1 mrg cpp_write_pch_state (parse_in, pch_outfile);
179 1.1 mrg timevar_pop (TV_PCH_CPP_SAVE);
180 1.1 mrg
181 1.1 mrg if (fseek (pch_outfile, 0, SEEK_SET) != 0
182 1.1 mrg || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
183 1.1 mrg fatal_error (input_location, "cannot write %s: %m", pch_file);
184 1.1 mrg
185 1.1 mrg fclose (pch_outfile);
186 1.1 mrg
187 1.1 mrg timevar_pop (TV_PCH_SAVE);
188 1.1 mrg }
189 1.1 mrg
190 1.1 mrg /* Check the PCH file called NAME, open on FD, to see if it can be
191 1.1 mrg used in this compilation. Return 1 if valid, 0 if the file can't
192 1.1 mrg be used now but might be if it's seen later in the compilation, and
193 1.1 mrg 2 if this file could never be used in the compilation. */
194 1.1 mrg
195 1.1 mrg int
196 1.1 mrg c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
197 1.1 mrg {
198 1.1 mrg int sizeread;
199 1.1 mrg int result;
200 1.1 mrg char ident[IDENT_LENGTH + 16];
201 1.1 mrg const char *pch_ident;
202 1.1 mrg struct c_pch_validity v;
203 1.1 mrg
204 1.1 mrg /* Perform a quick test of whether this is a valid
205 1.1 mrg precompiled header for the current language. */
206 1.1 mrg
207 1.1 mrg /* C++ modules and PCH don't play together. */
208 1.1 mrg if (flag_modules)
209 1.1 mrg return 2;
210 1.1 mrg
211 1.1 mrg sizeread = read (fd, ident, IDENT_LENGTH + 16);
212 1.1 mrg if (sizeread == -1)
213 1.1 mrg fatal_error (input_location, "cannot read %s: %m", name);
214 1.1 mrg else if (sizeread != IDENT_LENGTH + 16)
215 1.1 mrg {
216 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH, "%s: too short to be a PCH file",
217 1.1 mrg name);
218 1.1 mrg return 2;
219 1.1 mrg }
220 1.1 mrg
221 1.1 mrg pch_ident = get_ident();
222 1.1 mrg if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
223 1.1 mrg {
224 1.1 mrg if (memcmp (ident, pch_ident, 5) == 0)
225 1.1 mrg /* It's a PCH, for the right language, but has the wrong version. */
226 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH,
227 1.1 mrg "%s: not compatible with this GCC version", name);
228 1.1 mrg else if (memcmp (ident, pch_ident, 4) == 0)
229 1.1 mrg /* It's a PCH for the wrong language. */
230 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH, "%s: not for %s", name,
231 1.1 mrg lang_hooks.name);
232 1.1 mrg else
233 1.1 mrg /* Not any kind of PCH. */
234 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH, "%s: not a PCH file", name);
235 1.1 mrg return 2;
236 1.1 mrg }
237 1.1 mrg if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
238 1.1 mrg {
239 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH,
240 1.1 mrg "%s: created by a different GCC executable", name);
241 1.1 mrg return 2;
242 1.1 mrg }
243 1.1 mrg
244 1.1 mrg /* At this point, we know it's a PCH file created by this
245 1.1 mrg executable, so it ought to be long enough that we can read a
246 1.1 mrg c_pch_validity structure. */
247 1.1 mrg if (read (fd, &v, sizeof (v)) != sizeof (v))
248 1.1 mrg fatal_error (input_location, "cannot read %s: %m", name);
249 1.1 mrg
250 1.1 mrg /* The allowable debug info combinations are that either the PCH file
251 1.1 mrg was built with the same as is being used now, or the PCH file was
252 1.1 mrg built for some kind of debug info but now none is in use. */
253 1.1 mrg if (v.pch_write_symbols != write_symbols
254 1.1 mrg && write_symbols != NO_DEBUG)
255 1.1 mrg {
256 1.1 mrg char *created_str = xstrdup (debug_set_names (v.pch_write_symbols));
257 1.1 mrg char *used_str = xstrdup (debug_set_names (write_symbols));
258 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH,
259 1.1 mrg "%s: created with '%s' debug info, but used with '%s'", name,
260 1.1 mrg created_str, used_str);
261 1.1 mrg free (created_str);
262 1.1 mrg free (used_str);
263 1.1 mrg return 2;
264 1.1 mrg }
265 1.1 mrg
266 1.1 mrg /* Check flags that must match exactly. */
267 1.1 mrg {
268 1.1 mrg size_t i;
269 1.1 mrg for (i = 0; i < MATCH_SIZE; i++)
270 1.1 mrg if (*pch_matching[i].flag_var != v.match[i])
271 1.1 mrg {
272 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH,
273 1.1 mrg "%s: settings for %s do not match", name,
274 1.1 mrg pch_matching[i].flag_name);
275 1.1 mrg return 2;
276 1.1 mrg }
277 1.1 mrg }
278 1.1 mrg
279 1.1 mrg /* Check the target-specific validity data. */
280 1.1 mrg {
281 1.1 mrg void *this_file_data = xmalloc (v.target_data_length);
282 1.1 mrg const char *msg;
283 1.1 mrg
284 1.1 mrg if ((size_t) read (fd, this_file_data, v.target_data_length)
285 1.1 mrg != v.target_data_length)
286 1.1 mrg fatal_error (input_location, "cannot read %s: %m", name);
287 1.1 mrg msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
288 1.1 mrg free (this_file_data);
289 1.1 mrg if (msg != NULL)
290 1.1 mrg {
291 1.1 mrg cpp_warning (pfile, CPP_W_INVALID_PCH, "%s: %s", name, msg);
292 1.1 mrg return 2;
293 1.1 mrg }
294 1.1 mrg }
295 1.1 mrg
296 1.1 mrg /* Check the preprocessor macros are the same as when the PCH was
297 1.1 mrg generated. */
298 1.1 mrg
299 1.1 mrg result = cpp_valid_state (pfile, name, fd);
300 1.1 mrg if (result == -1)
301 1.1 mrg return 2;
302 1.1 mrg else
303 1.1 mrg return result == 0;
304 1.1 mrg }
305 1.1 mrg
306 1.1 mrg /* If non-NULL, this function is called after a precompile header file
307 1.1 mrg is loaded. */
308 1.1 mrg void (*lang_post_pch_load) (void);
309 1.1 mrg
310 1.1 mrg /* Load in the PCH file NAME, open on FD. It was originally searched for
311 1.1 mrg by ORIG_NAME. */
312 1.1 mrg
313 1.1 mrg void
314 1.1 mrg c_common_read_pch (cpp_reader *pfile, const char *name,
315 1.1 mrg int fd, const char *orig_name ATTRIBUTE_UNUSED)
316 1.1 mrg {
317 1.1 mrg FILE *f;
318 1.1 mrg struct save_macro_data *smd;
319 1.1 mrg expanded_location saved_loc;
320 1.1 mrg bool saved_trace_includes;
321 1.1 mrg
322 1.1 mrg timevar_push (TV_PCH_RESTORE);
323 1.1 mrg
324 1.1 mrg f = fdopen (fd, "rb");
325 1.1 mrg if (f == NULL)
326 1.1 mrg {
327 1.1 mrg cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
328 1.1 mrg close (fd);
329 1.1 mrg goto end;
330 1.1 mrg }
331 1.1 mrg
332 1.1 mrg cpp_get_callbacks (parse_in)->valid_pch = NULL;
333 1.1 mrg
334 1.1 mrg /* Save the location and then restore it after reading the PCH. */
335 1.1 mrg saved_loc = expand_location (line_table->highest_line);
336 1.1 mrg saved_trace_includes = line_table->trace_includes;
337 1.1 mrg
338 1.1 mrg timevar_push (TV_PCH_CPP_RESTORE);
339 1.1 mrg cpp_prepare_state (pfile, &smd);
340 1.1 mrg timevar_pop (TV_PCH_CPP_RESTORE);
341 1.1 mrg
342 1.1 mrg gt_pch_restore (f);
343 1.1 mrg cpp_set_line_map (pfile, line_table);
344 1.1 mrg rebuild_location_adhoc_htab (line_table);
345 1.1 mrg line_table->trace_includes = saved_trace_includes;
346 1.1 mrg linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
347 1.1 mrg
348 1.1 mrg timevar_push (TV_PCH_CPP_RESTORE);
349 1.1 mrg if (cpp_read_state (pfile, name, f, smd) != 0)
350 1.1 mrg {
351 1.1 mrg fclose (f);
352 1.1 mrg timevar_pop (TV_PCH_CPP_RESTORE);
353 1.1 mrg goto end;
354 1.1 mrg }
355 1.1 mrg timevar_pop (TV_PCH_CPP_RESTORE);
356 1.1 mrg
357 1.1 mrg
358 1.1 mrg fclose (f);
359 1.1 mrg
360 1.1 mrg /* Give the front end a chance to take action after a PCH file has
361 1.1 mrg been loaded. */
362 1.1 mrg if (lang_post_pch_load)
363 1.1 mrg (*lang_post_pch_load) ();
364 1.1 mrg
365 1.1 mrg end:
366 1.1 mrg timevar_pop (TV_PCH_RESTORE);
367 1.1 mrg }
368 1.1 mrg
369 1.1 mrg /* Indicate that no more PCH files should be read. */
370 1.1 mrg
371 1.1 mrg void
372 1.1 mrg c_common_no_more_pch (void)
373 1.1 mrg {
374 1.1 mrg if (cpp_get_callbacks (parse_in)->valid_pch)
375 1.1 mrg {
376 1.1 mrg cpp_get_callbacks (parse_in)->valid_pch = NULL;
377 1.1 mrg void *addr = NULL;
378 1.1 mrg host_hooks.gt_pch_use_address (addr, 0, -1, 0);
379 1.1 mrg }
380 1.1 mrg }
381 1.1 mrg
382 1.1 mrg /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
383 1.1 mrg
384 1.1 mrg void
385 1.1 mrg c_common_pch_pragma (cpp_reader *pfile, const char *name)
386 1.1 mrg {
387 1.1 mrg int fd;
388 1.1 mrg
389 1.1 mrg if (!cpp_get_options (pfile)->preprocessed)
390 1.1 mrg {
391 1.1 mrg error ("%<pch_preprocess%> pragma should only be used "
392 1.1 mrg "with %<-fpreprocessed%>");
393 1.1 mrg inform (input_location, "use %<#include%> instead");
394 1.1 mrg return;
395 1.1 mrg }
396 1.1 mrg
397 1.1 mrg fd = open (name, O_RDONLY | O_BINARY, 0666);
398 1.1 mrg if (fd == -1)
399 1.1 mrg fatal_error (input_location, "%s: couldn%'t open PCH file: %m", name);
400 1.1 mrg
401 1.1 mrg if (c_common_valid_pch (pfile, name, fd) != 1)
402 1.1 mrg {
403 1.1 mrg if (!cpp_get_options (pfile)->warn_invalid_pch)
404 1.1 mrg inform (input_location, "use %<-Winvalid-pch%> for more information");
405 1.1 mrg fatal_error (input_location, "%s: PCH file was invalid", name);
406 1.1 mrg }
407 1.1 mrg
408 1.1 mrg c_common_read_pch (pfile, name, fd, name);
409 1.1 mrg
410 1.1 mrg close (fd);
411 1.1 mrg }
412 1.1 mrg
413