Merge trunk version 194076 into gupc branch.
[official-gcc.git] / gcc / c-family / c-pch.c
blobf945f05b7e7de8b17d83db79870a480445772372
1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "version.h"
25 #include "cpplib.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "c-common.h"
29 #include "debug.h"
30 #include "c-pragma.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "hosthooks.h"
34 #include "target.h"
35 #include "opts.h"
36 #include "timevar.h"
38 /* This is a list of flag variables that must match exactly, and their
39 names for the error message. The possible values for *flag_var must
40 fit in a 'signed char'. */
42 static const struct c_pch_matching
44 int *flag_var;
45 const char *flag_name;
46 } pch_matching[] = {
47 { &flag_exceptions, "-fexceptions" },
50 enum {
51 MATCH_SIZE = ARRAY_SIZE (pch_matching)
54 /* The value of the checksum in the dummy compiler that is actually
55 checksummed. That compiler should never be run. */
56 static const char no_checksum[16] = { 0 };
58 /* Information about flags and suchlike that affect PCH validity.
60 Before this structure is read, both an initial 8-character identification
61 string, and a 16-byte checksum, have been read and validated. */
63 struct c_pch_validity
65 unsigned char debug_info_type;
66 signed char match[MATCH_SIZE];
67 void (*pch_init) (void);
68 size_t target_data_length;
71 #define IDENT_LENGTH 8
73 /* The file we'll be writing the PCH to. */
74 static FILE *pch_outfile;
76 static const char *get_ident (void);
78 /* Compute an appropriate 8-byte magic number for the PCH file, so that
79 utilities like file(1) can identify it, and so that GCC can quickly
80 ignore non-PCH files and PCH files that are of a completely different
81 format. */
83 static const char *
84 get_ident (void)
86 static char result[IDENT_LENGTH];
87 static const char templ[] = "gpch.014";
88 static const char c_language_chars[] = "Co+O";
90 memcpy (result, templ, IDENT_LENGTH);
91 result[4] = c_language_chars[c_language];
93 return result;
96 /* Whether preprocessor state should be saved by pch_init. */
98 static bool pch_ready_to_save_cpp_state = false;
100 /* Prepare to write a PCH file, if one is being written. This is
101 called at the start of compilation. */
103 void
104 pch_init (void)
106 FILE *f;
107 struct c_pch_validity v;
108 void *target_validity;
109 static const char partial_pch[] = "gpcWrite";
111 if (!pch_file)
112 return;
114 f = fopen (pch_file, "w+b");
115 if (f == NULL)
116 fatal_error ("can%'t create precompiled header %s: %m", pch_file);
117 pch_outfile = f;
119 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
121 memset (&v, '\0', sizeof (v));
122 v.debug_info_type = write_symbols;
124 size_t i;
125 for (i = 0; i < MATCH_SIZE; i++)
127 v.match[i] = *pch_matching[i].flag_var;
128 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
131 v.pch_init = &pch_init;
132 target_validity = targetm.get_pch_validity (&v.target_data_length);
134 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
135 || fwrite (executable_checksum, 16, 1, f) != 1
136 || fwrite (&v, sizeof (v), 1, f) != 1
137 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
138 fatal_error ("can%'t write to %s: %m", pch_file);
140 /* Let the debugging format deal with the PCHness. */
141 (*debug_hooks->handle_pch) (0);
143 if (pch_ready_to_save_cpp_state)
144 pch_cpp_save_state ();
147 /* Whether preprocessor state has been saved in a PCH file. */
149 static bool pch_cpp_state_saved = false;
151 /* Save preprocessor state in a PCH file, after implicitly included
152 headers have been read. If the PCH file has not yet been opened,
153 record that state should be saved when it is opened. */
155 void
156 pch_cpp_save_state (void)
158 if (!pch_cpp_state_saved)
160 if (pch_outfile)
162 cpp_save_state (parse_in, pch_outfile);
163 pch_cpp_state_saved = true;
165 else
166 pch_ready_to_save_cpp_state = true;
170 /* Write the PCH file. This is called at the end of a compilation which
171 will produce a PCH file. */
173 void
174 c_common_write_pch (void)
176 timevar_push (TV_PCH_SAVE);
178 targetm.prepare_pch_save ();
180 (*debug_hooks->handle_pch) (1);
182 cpp_write_pch_deps (parse_in, pch_outfile);
184 gt_pch_save (pch_outfile);
186 timevar_push (TV_PCH_CPP_SAVE);
187 cpp_write_pch_state (parse_in, pch_outfile);
188 timevar_pop (TV_PCH_CPP_SAVE);
190 if (fseek (pch_outfile, 0, SEEK_SET) != 0
191 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
192 fatal_error ("can%'t write %s: %m", pch_file);
194 fclose (pch_outfile);
196 timevar_pop (TV_PCH_SAVE);
199 /* Check the PCH file called NAME, open on FD, to see if it can be
200 used in this compilation. Return 1 if valid, 0 if the file can't
201 be used now but might be if it's seen later in the compilation, and
202 2 if this file could never be used in the compilation. */
205 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
207 int sizeread;
208 int result;
209 char ident[IDENT_LENGTH + 16];
210 const char *pch_ident;
211 struct c_pch_validity v;
213 /* Perform a quick test of whether this is a valid
214 precompiled header for the current language. */
216 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
218 sizeread = read (fd, ident, IDENT_LENGTH + 16);
219 if (sizeread == -1)
220 fatal_error ("can%'t read %s: %m", name);
221 else if (sizeread != IDENT_LENGTH + 16)
223 if (cpp_get_options (pfile)->warn_invalid_pch)
224 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
225 name);
226 return 2;
229 pch_ident = get_ident();
230 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
232 if (cpp_get_options (pfile)->warn_invalid_pch)
234 if (memcmp (ident, pch_ident, 5) == 0)
235 /* It's a PCH, for the right language, but has the wrong version.
237 cpp_error (pfile, CPP_DL_WARNING,
238 "%s: not compatible with this GCC version", name);
239 else if (memcmp (ident, pch_ident, 4) == 0)
240 /* It's a PCH for the wrong language. */
241 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
242 lang_hooks.name);
243 else
244 /* Not any kind of PCH. */
245 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
247 return 2;
249 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
251 if (cpp_get_options (pfile)->warn_invalid_pch)
252 cpp_error (pfile, CPP_DL_WARNING,
253 "%s: created by a different GCC executable", name);
254 return 2;
257 /* At this point, we know it's a PCH file created by this
258 executable, so it ought to be long enough that we can read a
259 c_pch_validity structure. */
260 if (read (fd, &v, sizeof (v)) != sizeof (v))
261 fatal_error ("can%'t read %s: %m", name);
263 /* The allowable debug info combinations are that either the PCH file
264 was built with the same as is being used now, or the PCH file was
265 built for some kind of debug info but now none is in use. */
266 if (v.debug_info_type != write_symbols
267 && write_symbols != NO_DEBUG)
269 if (cpp_get_options (pfile)->warn_invalid_pch)
270 cpp_error (pfile, CPP_DL_WARNING,
271 "%s: created with -g%s, but used with -g%s", name,
272 debug_type_names[v.debug_info_type],
273 debug_type_names[write_symbols]);
274 return 2;
277 /* Check flags that must match exactly. */
279 size_t i;
280 for (i = 0; i < MATCH_SIZE; i++)
281 if (*pch_matching[i].flag_var != v.match[i])
283 if (cpp_get_options (pfile)->warn_invalid_pch)
284 cpp_error (pfile, CPP_DL_WARNING,
285 "%s: settings for %s do not match", name,
286 pch_matching[i].flag_name);
287 return 2;
291 /* If the text segment was not loaded at the same address as it was
292 when the PCH file was created, function pointers loaded from the
293 PCH will not be valid. We could in theory remap all the function
294 pointers, but no support for that exists at present.
295 Since we have the same executable, it should only be necessary to
296 check one function. */
297 if (v.pch_init != &pch_init)
299 if (cpp_get_options (pfile)->warn_invalid_pch)
300 cpp_error (pfile, CPP_DL_WARNING,
301 "%s: had text segment at different address", name);
302 return 2;
305 /* Check the target-specific validity data. */
307 void *this_file_data = xmalloc (v.target_data_length);
308 const char *msg;
310 if ((size_t) read (fd, this_file_data, v.target_data_length)
311 != v.target_data_length)
312 fatal_error ("can%'t read %s: %m", name);
313 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
314 free (this_file_data);
315 if (msg != NULL)
317 if (cpp_get_options (pfile)->warn_invalid_pch)
318 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
319 return 2;
323 /* Check the preprocessor macros are the same as when the PCH was
324 generated. */
326 result = cpp_valid_state (pfile, name, fd);
327 if (result == -1)
328 return 2;
329 else
330 return result == 0;
333 /* If non-NULL, this function is called after a precompile header file
334 is loaded. */
335 void (*lang_post_pch_load) (void);
337 /* Load in the PCH file NAME, open on FD. It was originally searched for
338 by ORIG_NAME. */
340 void
341 c_common_read_pch (cpp_reader *pfile, const char *name,
342 int fd, const char *orig_name ATTRIBUTE_UNUSED)
344 FILE *f;
345 struct save_macro_data *smd;
346 expanded_location saved_loc;
347 bool saved_trace_includes;
349 timevar_push (TV_PCH_RESTORE);
351 f = fdopen (fd, "rb");
352 if (f == NULL)
354 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
355 close (fd);
356 goto end;
359 cpp_get_callbacks (parse_in)->valid_pch = NULL;
361 /* Save the location and then restore it after reading the PCH. */
362 saved_loc = expand_location (line_table->highest_line);
363 saved_trace_includes = line_table->trace_includes;
365 timevar_push (TV_PCH_CPP_RESTORE);
366 cpp_prepare_state (pfile, &smd);
367 timevar_pop (TV_PCH_CPP_RESTORE);
369 gt_pch_restore (f);
370 cpp_set_line_map (pfile, line_table);
371 rebuild_location_adhoc_htab (line_table);
373 timevar_push (TV_PCH_CPP_RESTORE);
374 if (cpp_read_state (pfile, name, f, smd) != 0)
376 fclose (f);
377 timevar_pop (TV_PCH_CPP_RESTORE);
378 goto end;
380 timevar_pop (TV_PCH_CPP_RESTORE);
383 fclose (f);
385 line_table->trace_includes = saved_trace_includes;
386 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
388 /* Give the front end a chance to take action after a PCH file has
389 been loaded. */
390 if (lang_post_pch_load)
391 (*lang_post_pch_load) ();
393 end:
394 timevar_pop (TV_PCH_RESTORE);
397 /* Indicate that no more PCH files should be read. */
399 void
400 c_common_no_more_pch (void)
402 if (cpp_get_callbacks (parse_in)->valid_pch)
404 cpp_get_callbacks (parse_in)->valid_pch = NULL;
405 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
409 /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
411 void
412 c_common_pch_pragma (cpp_reader *pfile, const char *name)
414 int fd;
416 if (!cpp_get_options (pfile)->preprocessed)
418 error ("pch_preprocess pragma should only be used with -fpreprocessed");
419 inform (input_location, "use #include instead");
420 return;
423 fd = open (name, O_RDONLY | O_BINARY, 0666);
424 if (fd == -1)
425 fatal_error ("%s: couldn%'t open PCH file: %m", name);
427 if (c_common_valid_pch (pfile, name, fd) != 1)
429 if (!cpp_get_options (pfile)->warn_invalid_pch)
430 inform (input_location, "use -Winvalid-pch for more information");
431 fatal_error ("%s: PCH file was invalid", name);
434 c_common_read_pch (pfile, name, fd, name);
436 close (fd);