pr60092.c: Remove default dg-skip-if arguments.
[gcc.git] / gcc / c-family / c-pch.c
1 /* Precompiled header implementation for the C languages.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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"
36
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'. */
40
41 static const struct c_pch_matching
42 {
43 int *flag_var;
44 const char *flag_name;
45 } pch_matching[] = {
46 { &flag_exceptions, "-fexceptions" },
47 };
48
49 enum {
50 MATCH_SIZE = ARRAY_SIZE (pch_matching)
51 };
52
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 };
56
57 /* Information about flags and suchlike that affect PCH validity.
58
59 Before this structure is read, both an initial 8-character identification
60 string, and a 16-byte checksum, have been read and validated. */
61
62 struct c_pch_validity
63 {
64 unsigned char debug_info_type;
65 signed char match[MATCH_SIZE];
66 void (*pch_init) (void);
67 size_t target_data_length;
68 };
69
70 #define IDENT_LENGTH 8
71
72 /* The file we'll be writing the PCH to. */
73 static FILE *pch_outfile;
74
75 static const char *get_ident (void);
76
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. */
81
82 static const char *
83 get_ident (void)
84 {
85 static char result[IDENT_LENGTH];
86 static const char templ[] = "gpch.014";
87 static const char c_language_chars[] = "Co+O";
88
89 memcpy (result, templ, IDENT_LENGTH);
90 result[4] = c_language_chars[c_language];
91
92 return result;
93 }
94
95 /* Whether preprocessor state should be saved by pch_init. */
96
97 static bool pch_ready_to_save_cpp_state = false;
98
99 /* Prepare to write a PCH file, if one is being written. This is
100 called at the start of compilation. */
101
102 void
103 pch_init (void)
104 {
105 FILE *f;
106 struct c_pch_validity v;
107 void *target_validity;
108 static const char partial_pch[] = "gpcWrite";
109
110 if (!pch_file)
111 return;
112
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;
117
118 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
119
120 memset (&v, '\0', sizeof (v));
121 v.debug_info_type = write_symbols;
122 {
123 size_t i;
124 for (i = 0; i < MATCH_SIZE; i++)
125 {
126 v.match[i] = *pch_matching[i].flag_var;
127 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
128 }
129 }
130 v.pch_init = &pch_init;
131 target_validity = targetm.get_pch_validity (&v.target_data_length);
132
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);
138
139 /* Let the debugging format deal with the PCHness. */
140 (*debug_hooks->handle_pch) (0);
141
142 if (pch_ready_to_save_cpp_state)
143 pch_cpp_save_state ();
144
145 XDELETE (target_validity);
146 }
147
148 /* Whether preprocessor state has been saved in a PCH file. */
149
150 static bool pch_cpp_state_saved = false;
151
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. */
155
156 void
157 pch_cpp_save_state (void)
158 {
159 if (!pch_cpp_state_saved)
160 {
161 if (pch_outfile)
162 {
163 cpp_save_state (parse_in, pch_outfile);
164 pch_cpp_state_saved = true;
165 }
166 else
167 pch_ready_to_save_cpp_state = true;
168 }
169 }
170
171 /* Write the PCH file. This is called at the end of a compilation which
172 will produce a PCH file. */
173
174 void
175 c_common_write_pch (void)
176 {
177 timevar_push (TV_PCH_SAVE);
178
179 targetm.prepare_pch_save ();
180
181 (*debug_hooks->handle_pch) (1);
182
183 prepare_target_option_nodes_for_pch ();
184
185 cpp_write_pch_deps (parse_in, pch_outfile);
186
187 gt_pch_save (pch_outfile);
188
189 timevar_push (TV_PCH_CPP_SAVE);
190 cpp_write_pch_state (parse_in, pch_outfile);
191 timevar_pop (TV_PCH_CPP_SAVE);
192
193 if (fseek (pch_outfile, 0, SEEK_SET) != 0
194 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
195 fatal_error ("can%'t write %s: %m", pch_file);
196
197 fclose (pch_outfile);
198
199 timevar_pop (TV_PCH_SAVE);
200 }
201
202 /* Check the PCH file called NAME, open on FD, to see if it can be
203 used in this compilation. Return 1 if valid, 0 if the file can't
204 be used now but might be if it's seen later in the compilation, and
205 2 if this file could never be used in the compilation. */
206
207 int
208 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
209 {
210 int sizeread;
211 int result;
212 char ident[IDENT_LENGTH + 16];
213 const char *pch_ident;
214 struct c_pch_validity v;
215
216 /* Perform a quick test of whether this is a valid
217 precompiled header for the current language. */
218
219 gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
220
221 sizeread = read (fd, ident, IDENT_LENGTH + 16);
222 if (sizeread == -1)
223 fatal_error ("can%'t read %s: %m", name);
224 else if (sizeread != IDENT_LENGTH + 16)
225 {
226 if (cpp_get_options (pfile)->warn_invalid_pch)
227 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
228 name);
229 return 2;
230 }
231
232 pch_ident = get_ident();
233 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
234 {
235 if (cpp_get_options (pfile)->warn_invalid_pch)
236 {
237 if (memcmp (ident, pch_ident, 5) == 0)
238 /* It's a PCH, for the right language, but has the wrong version.
239 */
240 cpp_error (pfile, CPP_DL_WARNING,
241 "%s: not compatible with this GCC version", name);
242 else if (memcmp (ident, pch_ident, 4) == 0)
243 /* It's a PCH for the wrong language. */
244 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
245 lang_hooks.name);
246 else
247 /* Not any kind of PCH. */
248 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
249 }
250 return 2;
251 }
252 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
253 {
254 if (cpp_get_options (pfile)->warn_invalid_pch)
255 cpp_error (pfile, CPP_DL_WARNING,
256 "%s: created by a different GCC executable", name);
257 return 2;
258 }
259
260 /* At this point, we know it's a PCH file created by this
261 executable, so it ought to be long enough that we can read a
262 c_pch_validity structure. */
263 if (read (fd, &v, sizeof (v)) != sizeof (v))
264 fatal_error ("can%'t read %s: %m", name);
265
266 /* The allowable debug info combinations are that either the PCH file
267 was built with the same as is being used now, or the PCH file was
268 built for some kind of debug info but now none is in use. */
269 if (v.debug_info_type != write_symbols
270 && write_symbols != NO_DEBUG)
271 {
272 if (cpp_get_options (pfile)->warn_invalid_pch)
273 cpp_error (pfile, CPP_DL_WARNING,
274 "%s: created with -g%s, but used with -g%s", name,
275 debug_type_names[v.debug_info_type],
276 debug_type_names[write_symbols]);
277 return 2;
278 }
279
280 /* Check flags that must match exactly. */
281 {
282 size_t i;
283 for (i = 0; i < MATCH_SIZE; i++)
284 if (*pch_matching[i].flag_var != v.match[i])
285 {
286 if (cpp_get_options (pfile)->warn_invalid_pch)
287 cpp_error (pfile, CPP_DL_WARNING,
288 "%s: settings for %s do not match", name,
289 pch_matching[i].flag_name);
290 return 2;
291 }
292 }
293
294 /* If the text segment was not loaded at the same address as it was
295 when the PCH file was created, function pointers loaded from the
296 PCH will not be valid. We could in theory remap all the function
297 pointers, but no support for that exists at present.
298 Since we have the same executable, it should only be necessary to
299 check one function. */
300 if (v.pch_init != &pch_init)
301 {
302 if (cpp_get_options (pfile)->warn_invalid_pch)
303 cpp_error (pfile, CPP_DL_WARNING,
304 "%s: had text segment at different address", name);
305 return 2;
306 }
307
308 /* Check the target-specific validity data. */
309 {
310 void *this_file_data = xmalloc (v.target_data_length);
311 const char *msg;
312
313 if ((size_t) read (fd, this_file_data, v.target_data_length)
314 != v.target_data_length)
315 fatal_error ("can%'t read %s: %m", name);
316 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
317 free (this_file_data);
318 if (msg != NULL)
319 {
320 if (cpp_get_options (pfile)->warn_invalid_pch)
321 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
322 return 2;
323 }
324 }
325
326 /* Check the preprocessor macros are the same as when the PCH was
327 generated. */
328
329 result = cpp_valid_state (pfile, name, fd);
330 if (result == -1)
331 return 2;
332 else
333 return result == 0;
334 }
335
336 /* If non-NULL, this function is called after a precompile header file
337 is loaded. */
338 void (*lang_post_pch_load) (void);
339
340 /* Load in the PCH file NAME, open on FD. It was originally searched for
341 by ORIG_NAME. */
342
343 void
344 c_common_read_pch (cpp_reader *pfile, const char *name,
345 int fd, const char *orig_name ATTRIBUTE_UNUSED)
346 {
347 FILE *f;
348 struct save_macro_data *smd;
349 expanded_location saved_loc;
350 bool saved_trace_includes;
351
352 timevar_push (TV_PCH_RESTORE);
353
354 f = fdopen (fd, "rb");
355 if (f == NULL)
356 {
357 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
358 close (fd);
359 goto end;
360 }
361
362 cpp_get_callbacks (parse_in)->valid_pch = NULL;
363
364 /* Save the location and then restore it after reading the PCH. */
365 saved_loc = expand_location (line_table->highest_line);
366 saved_trace_includes = line_table->trace_includes;
367
368 timevar_push (TV_PCH_CPP_RESTORE);
369 cpp_prepare_state (pfile, &smd);
370 timevar_pop (TV_PCH_CPP_RESTORE);
371
372 gt_pch_restore (f);
373 cpp_set_line_map (pfile, line_table);
374 rebuild_location_adhoc_htab (line_table);
375
376 timevar_push (TV_PCH_CPP_RESTORE);
377 if (cpp_read_state (pfile, name, f, smd) != 0)
378 {
379 fclose (f);
380 timevar_pop (TV_PCH_CPP_RESTORE);
381 goto end;
382 }
383 timevar_pop (TV_PCH_CPP_RESTORE);
384
385
386 fclose (f);
387
388 line_table->trace_includes = saved_trace_includes;
389 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
390
391 /* Give the front end a chance to take action after a PCH file has
392 been loaded. */
393 if (lang_post_pch_load)
394 (*lang_post_pch_load) ();
395
396 end:
397 timevar_pop (TV_PCH_RESTORE);
398 }
399
400 /* Indicate that no more PCH files should be read. */
401
402 void
403 c_common_no_more_pch (void)
404 {
405 if (cpp_get_callbacks (parse_in)->valid_pch)
406 {
407 cpp_get_callbacks (parse_in)->valid_pch = NULL;
408 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
409 }
410 }
411
412 /* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
413
414 void
415 c_common_pch_pragma (cpp_reader *pfile, const char *name)
416 {
417 int fd;
418
419 if (!cpp_get_options (pfile)->preprocessed)
420 {
421 error ("pch_preprocess pragma should only be used with -fpreprocessed");
422 inform (input_location, "use #include instead");
423 return;
424 }
425
426 fd = open (name, O_RDONLY | O_BINARY, 0666);
427 if (fd == -1)
428 fatal_error ("%s: couldn%'t open PCH file: %m", name);
429
430 if (c_common_valid_pch (pfile, name, fd) != 1)
431 {
432 if (!cpp_get_options (pfile)->warn_invalid_pch)
433 inform (input_location, "use -Winvalid-pch for more information");
434 fatal_error ("%s: PCH file was invalid", name);
435 }
436
437 c_common_read_pch (pfile, name, fd, name);
438
439 close (fd);
440 }
441