PR libstdc++/65978
[official-gcc.git] / gcc / c-family / c-pch.c
blob0ede92ab7ccf9856f4e45cff8053d30e17e65948
1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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 3, or (at your option)
9 any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "version.h"
24 #include "cpplib.h"
25 #include "options.h"
26 #include "hash-set.h"
27 #include "machmode.h"
28 #include "vec.h"
29 #include "double-int.h"
30 #include "input.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "wide-int.h"
34 #include "inchash.h"
35 #include "tree.h"
36 #include "flags.h"
37 #include "c-common.h"
38 #include "debug.h"
39 #include "c-pragma.h"
40 #include "ggc.h"
41 #include "langhooks.h"
42 #include "hosthooks.h"
43 #include "target.h"
44 #include "opts.h"
45 #include "timevar.h"
47 /* This is a list of flag variables that must match exactly, and their
48 names for the error message. The possible values for *flag_var must
49 fit in a 'signed char'. */
51 static const struct c_pch_matching
53 int *flag_var;
54 const char *flag_name;
55 } pch_matching[] = {
56 { &flag_exceptions, "-fexceptions" },
59 enum {
60 MATCH_SIZE = ARRAY_SIZE (pch_matching)
63 /* The value of the checksum in the dummy compiler that is actually
64 checksummed. That compiler should never be run. */
65 static const char no_checksum[16] = { 0 };
67 /* Information about flags and suchlike that affect PCH validity.
69 Before this structure is read, both an initial 8-character identification
70 string, and a 16-byte checksum, have been read and validated. */
72 struct c_pch_validity
74 unsigned char debug_info_type;
75 signed char match[MATCH_SIZE];
76 void (*pch_init) (void);
77 size_t target_data_length;
80 #define IDENT_LENGTH 8
82 /* The file we'll be writing the PCH to. */
83 static FILE *pch_outfile;
85 static const char *get_ident (void);
87 /* Compute an appropriate 8-byte magic number for the PCH file, so that
88 utilities like file(1) can identify it, and so that GCC can quickly
89 ignore non-PCH files and PCH files that are of a completely different
90 format. */
92 static const char *
93 get_ident (void)
95 static char result[IDENT_LENGTH];
96 static const char templ[] = "gpch.014";
97 static const char c_language_chars[] = "Co+O";
99 memcpy (result, templ, IDENT_LENGTH);
100 result[4] = c_language_chars[c_language];
102 return result;
105 /* Whether preprocessor state should be saved by pch_init. */
107 static bool pch_ready_to_save_cpp_state = false;
109 /* Prepare to write a PCH file, if one is being written. This is
110 called at the start of compilation. */
112 void
113 pch_init (void)
115 FILE *f;
116 struct c_pch_validity v;
117 void *target_validity;
118 static const char partial_pch[] = "gpcWrite";
120 if (!pch_file)
121 return;
123 f = fopen (pch_file, "w+b");
124 if (f == NULL)
125 fatal_error (input_location, "can%'t create precompiled header %s: %m",
126 pch_file);
127 pch_outfile = f;
129 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
131 memset (&v, '\0', sizeof (v));
132 v.debug_info_type = write_symbols;
134 size_t i;
135 for (i = 0; i < MATCH_SIZE; i++)
137 v.match[i] = *pch_matching[i].flag_var;
138 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
141 v.pch_init = &pch_init;
142 target_validity = targetm.get_pch_validity (&v.target_data_length);
144 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
145 || fwrite (executable_checksum, 16, 1, f) != 1
146 || fwrite (&v, sizeof (v), 1, f) != 1
147 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
148 fatal_error (input_location, "can%'t write to %s: %m", pch_file);
150 /* Let the debugging format deal with the PCHness. */
151 (*debug_hooks->handle_pch) (0);
153 if (pch_ready_to_save_cpp_state)
154 pch_cpp_save_state ();
156 XDELETE (target_validity);
159 /* Whether preprocessor state has been saved in a PCH file. */
161 static bool pch_cpp_state_saved = false;
163 /* Save preprocessor state in a PCH file, after implicitly included
164 headers have been read. If the PCH file has not yet been opened,
165 record that state should be saved when it is opened. */
167 void
168 pch_cpp_save_state (void)
170 if (!pch_cpp_state_saved)
172 if (pch_outfile)
174 cpp_save_state (parse_in, pch_outfile);
175 pch_cpp_state_saved = true;
177 else
178 pch_ready_to_save_cpp_state = true;
182 /* Write the PCH file. This is called at the end of a compilation which
183 will produce a PCH file. */
185 void
186 c_common_write_pch (void)
188 timevar_push (TV_PCH_SAVE);
190 targetm.prepare_pch_save ();
192 (*debug_hooks->handle_pch) (1);
194 prepare_target_option_nodes_for_pch ();
196 cpp_write_pch_deps (parse_in, pch_outfile);
198 gt_pch_save (pch_outfile);
200 timevar_push (TV_PCH_CPP_SAVE);
201 cpp_write_pch_state (parse_in, pch_outfile);
202 timevar_pop (TV_PCH_CPP_SAVE);
204 if (fseek (pch_outfile, 0, SEEK_SET) != 0
205 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
206 fatal_error (input_location, "can%'t write %s: %m", pch_file);
208 fclose (pch_outfile);
210 timevar_pop (TV_PCH_SAVE);
213 /* Check the PCH file called NAME, open on FD, to see if it can be
214 used in this compilation. Return 1 if valid, 0 if the file can't
215 be used now but might be if it's seen later in the compilation, and
216 2 if this file could never be used in the compilation. */
219 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
221 int sizeread;
222 int result;
223 char ident[IDENT_LENGTH + 16];
224 const char *pch_ident;
225 struct c_pch_validity v;
227 /* Perform a quick test of whether this is a valid
228 precompiled header for the current language. */
230 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
232 sizeread = read (fd, ident, IDENT_LENGTH + 16);
233 if (sizeread == -1)
234 fatal_error (input_location, "can%'t read %s: %m", name);
235 else if (sizeread != IDENT_LENGTH + 16)
237 if (cpp_get_options (pfile)->warn_invalid_pch)
238 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
239 name);
240 return 2;
243 pch_ident = get_ident();
244 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
246 if (cpp_get_options (pfile)->warn_invalid_pch)
248 if (memcmp (ident, pch_ident, 5) == 0)
249 /* It's a PCH, for the right language, but has the wrong version.
251 cpp_error (pfile, CPP_DL_WARNING,
252 "%s: not compatible with this GCC version", name);
253 else if (memcmp (ident, pch_ident, 4) == 0)
254 /* It's a PCH for the wrong language. */
255 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
256 lang_hooks.name);
257 else
258 /* Not any kind of PCH. */
259 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
261 return 2;
263 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
265 if (cpp_get_options (pfile)->warn_invalid_pch)
266 cpp_error (pfile, CPP_DL_WARNING,
267 "%s: created by a different GCC executable", name);
268 return 2;
271 /* At this point, we know it's a PCH file created by this
272 executable, so it ought to be long enough that we can read a
273 c_pch_validity structure. */
274 if (read (fd, &v, sizeof (v)) != sizeof (v))
275 fatal_error (input_location, "can%'t read %s: %m", name);
277 /* The allowable debug info combinations are that either the PCH file
278 was built with the same as is being used now, or the PCH file was
279 built for some kind of debug info but now none is in use. */
280 if (v.debug_info_type != write_symbols
281 && write_symbols != NO_DEBUG)
283 if (cpp_get_options (pfile)->warn_invalid_pch)
284 cpp_error (pfile, CPP_DL_WARNING,
285 "%s: created with -g%s, but used with -g%s", name,
286 debug_type_names[v.debug_info_type],
287 debug_type_names[write_symbols]);
288 return 2;
291 /* Check flags that must match exactly. */
293 size_t i;
294 for (i = 0; i < MATCH_SIZE; i++)
295 if (*pch_matching[i].flag_var != v.match[i])
297 if (cpp_get_options (pfile)->warn_invalid_pch)
298 cpp_error (pfile, CPP_DL_WARNING,
299 "%s: settings for %s do not match", name,
300 pch_matching[i].flag_name);
301 return 2;
305 /* If the text segment was not loaded at the same address as it was
306 when the PCH file was created, function pointers loaded from the
307 PCH will not be valid. We could in theory remap all the function
308 pointers, but no support for that exists at present.
309 Since we have the same executable, it should only be necessary to
310 check one function. */
311 if (v.pch_init != &pch_init)
313 if (cpp_get_options (pfile)->warn_invalid_pch)
314 cpp_error (pfile, CPP_DL_WARNING,
315 "%s: had text segment at different address", name);
316 return 2;
319 /* Check the target-specific validity data. */
321 void *this_file_data = xmalloc (v.target_data_length);
322 const char *msg;
324 if ((size_t) read (fd, this_file_data, v.target_data_length)
325 != v.target_data_length)
326 fatal_error (input_location, "can%'t read %s: %m", name);
327 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
328 free (this_file_data);
329 if (msg != NULL)
331 if (cpp_get_options (pfile)->warn_invalid_pch)
332 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
333 return 2;
337 /* Check the preprocessor macros are the same as when the PCH was
338 generated. */
340 result = cpp_valid_state (pfile, name, fd);
341 if (result == -1)
342 return 2;
343 else
344 return result == 0;
347 /* If non-NULL, this function is called after a precompile header file
348 is loaded. */
349 void (*lang_post_pch_load) (void);
351 /* Load in the PCH file NAME, open on FD. It was originally searched for
352 by ORIG_NAME. */
354 void
355 c_common_read_pch (cpp_reader *pfile, const char *name,
356 int fd, const char *orig_name ATTRIBUTE_UNUSED)
358 FILE *f;
359 struct save_macro_data *smd;
360 expanded_location saved_loc;
361 bool saved_trace_includes;
363 timevar_push (TV_PCH_RESTORE);
365 f = fdopen (fd, "rb");
366 if (f == NULL)
368 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
369 close (fd);
370 goto end;
373 cpp_get_callbacks (parse_in)->valid_pch = NULL;
375 /* Save the location and then restore it after reading the PCH. */
376 saved_loc = expand_location (line_table->highest_line);
377 saved_trace_includes = line_table->trace_includes;
379 timevar_push (TV_PCH_CPP_RESTORE);
380 cpp_prepare_state (pfile, &smd);
381 timevar_pop (TV_PCH_CPP_RESTORE);
383 gt_pch_restore (f);
384 cpp_set_line_map (pfile, line_table);
385 rebuild_location_adhoc_htab (line_table);
387 timevar_push (TV_PCH_CPP_RESTORE);
388 if (cpp_read_state (pfile, name, f, smd) != 0)
390 fclose (f);
391 timevar_pop (TV_PCH_CPP_RESTORE);
392 goto end;
394 timevar_pop (TV_PCH_CPP_RESTORE);
397 fclose (f);
399 line_table->trace_includes = saved_trace_includes;
400 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
402 /* Give the front end a chance to take action after a PCH file has
403 been loaded. */
404 if (lang_post_pch_load)
405 (*lang_post_pch_load) ();
407 end:
408 timevar_pop (TV_PCH_RESTORE);
411 /* Indicate that no more PCH files should be read. */
413 void
414 c_common_no_more_pch (void)
416 if (cpp_get_callbacks (parse_in)->valid_pch)
418 cpp_get_callbacks (parse_in)->valid_pch = NULL;
419 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
423 /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
425 void
426 c_common_pch_pragma (cpp_reader *pfile, const char *name)
428 int fd;
430 if (!cpp_get_options (pfile)->preprocessed)
432 error ("pch_preprocess pragma should only be used with -fpreprocessed");
433 inform (input_location, "use #include instead");
434 return;
437 fd = open (name, O_RDONLY | O_BINARY, 0666);
438 if (fd == -1)
439 fatal_error (input_location, "%s: couldn%'t open PCH file: %m", name);
441 if (c_common_valid_pch (pfile, name, fd) != 1)
443 if (!cpp_get_options (pfile)->warn_invalid_pch)
444 inform (input_location, "use -Winvalid-pch for more information");
445 fatal_error (input_location, "%s: PCH file was invalid", name);
448 c_common_read_pch (pfile, name, fd, name);
450 close (fd);