* c-family/array-notation-common.c (length_mismatch_in_expr_p): Delete
[official-gcc.git] / gcc / c-family / c-pch.c
blob7b0eca7fde1222e2a73e30518f782a4c2e7debd5
1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000-2013 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 "tree.h"
26 #include "flags.h"
27 #include "c-common.h"
28 #include "debug.h"
29 #include "c-pragma.h"
30 #include "ggc.h"
31 #include "langhooks.h"
32 #include "hosthooks.h"
33 #include "target.h"
34 #include "opts.h"
35 #include "timevar.h"
37 /* This is a list of flag variables that must match exactly, and their
38 names for the error message. The possible values for *flag_var must
39 fit in a 'signed char'. */
41 static const struct c_pch_matching
43 int *flag_var;
44 const char *flag_name;
45 } pch_matching[] = {
46 { &flag_exceptions, "-fexceptions" },
49 enum {
50 MATCH_SIZE = ARRAY_SIZE (pch_matching)
53 /* The value of the checksum in the dummy compiler that is actually
54 checksummed. That compiler should never be run. */
55 static const char no_checksum[16] = { 0 };
57 /* Information about flags and suchlike that affect PCH validity.
59 Before this structure is read, both an initial 8-character identification
60 string, and a 16-byte checksum, have been read and validated. */
62 struct c_pch_validity
64 unsigned char debug_info_type;
65 signed char match[MATCH_SIZE];
66 void (*pch_init) (void);
67 size_t target_data_length;
70 #define IDENT_LENGTH 8
72 /* The file we'll be writing the PCH to. */
73 static FILE *pch_outfile;
75 static const char *get_ident (void);
77 /* Compute an appropriate 8-byte magic number for the PCH file, so that
78 utilities like file(1) can identify it, and so that GCC can quickly
79 ignore non-PCH files and PCH files that are of a completely different
80 format. */
82 static const char *
83 get_ident (void)
85 static char result[IDENT_LENGTH];
86 static const char templ[] = "gpch.014";
87 static const char c_language_chars[] = "Co+O";
89 memcpy (result, templ, IDENT_LENGTH);
90 result[4] = c_language_chars[c_language];
92 return result;
95 /* Whether preprocessor state should be saved by pch_init. */
97 static bool pch_ready_to_save_cpp_state = false;
99 /* Prepare to write a PCH file, if one is being written. This is
100 called at the start of compilation. */
102 void
103 pch_init (void)
105 FILE *f;
106 struct c_pch_validity v;
107 void *target_validity;
108 static const char partial_pch[] = "gpcWrite";
110 if (!pch_file)
111 return;
113 f = fopen (pch_file, "w+b");
114 if (f == NULL)
115 fatal_error ("can%'t create precompiled header %s: %m", pch_file);
116 pch_outfile = f;
118 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
120 memset (&v, '\0', sizeof (v));
121 v.debug_info_type = write_symbols;
123 size_t i;
124 for (i = 0; i < MATCH_SIZE; i++)
126 v.match[i] = *pch_matching[i].flag_var;
127 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
130 v.pch_init = &pch_init;
131 target_validity = targetm.get_pch_validity (&v.target_data_length);
133 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
134 || fwrite (executable_checksum, 16, 1, f) != 1
135 || fwrite (&v, sizeof (v), 1, f) != 1
136 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
137 fatal_error ("can%'t write to %s: %m", pch_file);
139 /* Let the debugging format deal with the PCHness. */
140 (*debug_hooks->handle_pch) (0);
142 if (pch_ready_to_save_cpp_state)
143 pch_cpp_save_state ();
145 XDELETE (target_validity);
148 /* Whether preprocessor state has been saved in a PCH file. */
150 static bool pch_cpp_state_saved = false;
152 /* Save preprocessor state in a PCH file, after implicitly included
153 headers have been read. If the PCH file has not yet been opened,
154 record that state should be saved when it is opened. */
156 void
157 pch_cpp_save_state (void)
159 if (!pch_cpp_state_saved)
161 if (pch_outfile)
163 cpp_save_state (parse_in, pch_outfile);
164 pch_cpp_state_saved = true;
166 else
167 pch_ready_to_save_cpp_state = true;
171 /* Write the PCH file. This is called at the end of a compilation which
172 will produce a PCH file. */
174 void
175 c_common_write_pch (void)
177 timevar_push (TV_PCH_SAVE);
179 targetm.prepare_pch_save ();
181 (*debug_hooks->handle_pch) (1);
183 cpp_write_pch_deps (parse_in, pch_outfile);
185 gt_pch_save (pch_outfile);
187 timevar_push (TV_PCH_CPP_SAVE);
188 cpp_write_pch_state (parse_in, pch_outfile);
189 timevar_pop (TV_PCH_CPP_SAVE);
191 if (fseek (pch_outfile, 0, SEEK_SET) != 0
192 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
193 fatal_error ("can%'t write %s: %m", pch_file);
195 fclose (pch_outfile);
197 timevar_pop (TV_PCH_SAVE);
200 /* Check the PCH file called NAME, open on FD, to see if it can be
201 used in this compilation. Return 1 if valid, 0 if the file can't
202 be used now but might be if it's seen later in the compilation, and
203 2 if this file could never be used in the compilation. */
206 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
208 int sizeread;
209 int result;
210 char ident[IDENT_LENGTH + 16];
211 const char *pch_ident;
212 struct c_pch_validity v;
214 /* Perform a quick test of whether this is a valid
215 precompiled header for the current language. */
217 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
219 sizeread = read (fd, ident, IDENT_LENGTH + 16);
220 if (sizeread == -1)
221 fatal_error ("can%'t read %s: %m", name);
222 else if (sizeread != IDENT_LENGTH + 16)
224 if (cpp_get_options (pfile)->warn_invalid_pch)
225 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
226 name);
227 return 2;
230 pch_ident = get_ident();
231 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
233 if (cpp_get_options (pfile)->warn_invalid_pch)
235 if (memcmp (ident, pch_ident, 5) == 0)
236 /* It's a PCH, for the right language, but has the wrong version.
238 cpp_error (pfile, CPP_DL_WARNING,
239 "%s: not compatible with this GCC version", name);
240 else if (memcmp (ident, pch_ident, 4) == 0)
241 /* It's a PCH for the wrong language. */
242 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
243 lang_hooks.name);
244 else
245 /* Not any kind of PCH. */
246 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
248 return 2;
250 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
252 if (cpp_get_options (pfile)->warn_invalid_pch)
253 cpp_error (pfile, CPP_DL_WARNING,
254 "%s: created by a different GCC executable", name);
255 return 2;
258 /* At this point, we know it's a PCH file created by this
259 executable, so it ought to be long enough that we can read a
260 c_pch_validity structure. */
261 if (read (fd, &v, sizeof (v)) != sizeof (v))
262 fatal_error ("can%'t read %s: %m", name);
264 /* The allowable debug info combinations are that either the PCH file
265 was built with the same as is being used now, or the PCH file was
266 built for some kind of debug info but now none is in use. */
267 if (v.debug_info_type != write_symbols
268 && write_symbols != NO_DEBUG)
270 if (cpp_get_options (pfile)->warn_invalid_pch)
271 cpp_error (pfile, CPP_DL_WARNING,
272 "%s: created with -g%s, but used with -g%s", name,
273 debug_type_names[v.debug_info_type],
274 debug_type_names[write_symbols]);
275 return 2;
278 /* Check flags that must match exactly. */
280 size_t i;
281 for (i = 0; i < MATCH_SIZE; i++)
282 if (*pch_matching[i].flag_var != v.match[i])
284 if (cpp_get_options (pfile)->warn_invalid_pch)
285 cpp_error (pfile, CPP_DL_WARNING,
286 "%s: settings for %s do not match", name,
287 pch_matching[i].flag_name);
288 return 2;
292 /* If the text segment was not loaded at the same address as it was
293 when the PCH file was created, function pointers loaded from the
294 PCH will not be valid. We could in theory remap all the function
295 pointers, but no support for that exists at present.
296 Since we have the same executable, it should only be necessary to
297 check one function. */
298 if (v.pch_init != &pch_init)
300 if (cpp_get_options (pfile)->warn_invalid_pch)
301 cpp_error (pfile, CPP_DL_WARNING,
302 "%s: had text segment at different address", name);
303 return 2;
306 /* Check the target-specific validity data. */
308 void *this_file_data = xmalloc (v.target_data_length);
309 const char *msg;
311 if ((size_t) read (fd, this_file_data, v.target_data_length)
312 != v.target_data_length)
313 fatal_error ("can%'t read %s: %m", name);
314 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
315 free (this_file_data);
316 if (msg != NULL)
318 if (cpp_get_options (pfile)->warn_invalid_pch)
319 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
320 return 2;
324 /* Check the preprocessor macros are the same as when the PCH was
325 generated. */
327 result = cpp_valid_state (pfile, name, fd);
328 if (result == -1)
329 return 2;
330 else
331 return result == 0;
334 /* If non-NULL, this function is called after a precompile header file
335 is loaded. */
336 void (*lang_post_pch_load) (void);
338 /* Load in the PCH file NAME, open on FD. It was originally searched for
339 by ORIG_NAME. */
341 void
342 c_common_read_pch (cpp_reader *pfile, const char *name,
343 int fd, const char *orig_name ATTRIBUTE_UNUSED)
345 FILE *f;
346 struct save_macro_data *smd;
347 expanded_location saved_loc;
348 bool saved_trace_includes;
350 timevar_push (TV_PCH_RESTORE);
352 f = fdopen (fd, "rb");
353 if (f == NULL)
355 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
356 close (fd);
357 goto end;
360 cpp_get_callbacks (parse_in)->valid_pch = NULL;
362 /* Save the location and then restore it after reading the PCH. */
363 saved_loc = expand_location (line_table->highest_line);
364 saved_trace_includes = line_table->trace_includes;
366 timevar_push (TV_PCH_CPP_RESTORE);
367 cpp_prepare_state (pfile, &smd);
368 timevar_pop (TV_PCH_CPP_RESTORE);
370 gt_pch_restore (f);
371 cpp_set_line_map (pfile, line_table);
372 rebuild_location_adhoc_htab (line_table);
374 timevar_push (TV_PCH_CPP_RESTORE);
375 if (cpp_read_state (pfile, name, f, smd) != 0)
377 fclose (f);
378 timevar_pop (TV_PCH_CPP_RESTORE);
379 goto end;
381 timevar_pop (TV_PCH_CPP_RESTORE);
384 fclose (f);
386 line_table->trace_includes = saved_trace_includes;
387 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
389 /* Give the front end a chance to take action after a PCH file has
390 been loaded. */
391 if (lang_post_pch_load)
392 (*lang_post_pch_load) ();
394 end:
395 timevar_pop (TV_PCH_RESTORE);
398 /* Indicate that no more PCH files should be read. */
400 void
401 c_common_no_more_pch (void)
403 if (cpp_get_callbacks (parse_in)->valid_pch)
405 cpp_get_callbacks (parse_in)->valid_pch = NULL;
406 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
410 /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
412 void
413 c_common_pch_pragma (cpp_reader *pfile, const char *name)
415 int fd;
417 if (!cpp_get_options (pfile)->preprocessed)
419 error ("pch_preprocess pragma should only be used with -fpreprocessed");
420 inform (input_location, "use #include instead");
421 return;
424 fd = open (name, O_RDONLY | O_BINARY, 0666);
425 if (fd == -1)
426 fatal_error ("%s: couldn%'t open PCH file: %m", name);
428 if (c_common_valid_pch (pfile, name, fd) != 1)
430 if (!cpp_get_options (pfile)->warn_invalid_pch)
431 inform (input_location, "use -Winvalid-pch for more information");
432 fatal_error ("%s: PCH file was invalid", name);
435 c_common_read_pch (pfile, name, fd, name);
437 close (fd);