Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / gcc / c-family / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000-2015 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 "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"
46
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'.  */
50
51 static const struct c_pch_matching
52 {
53   int *flag_var;
54   const char *flag_name;
55 } pch_matching[] = {
56   { &flag_exceptions, "-fexceptions" },
57 };
58
59 enum {
60   MATCH_SIZE = ARRAY_SIZE (pch_matching)
61 };
62
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 };
66
67 /* Information about flags and suchlike that affect PCH validity.
68
69    Before this structure is read, both an initial 8-character identification
70    string, and a 16-byte checksum, have been read and validated.  */
71
72 struct c_pch_validity
73 {
74   unsigned char debug_info_type;
75   signed char match[MATCH_SIZE];
76   void (*pch_init) (void);
77   size_t target_data_length;
78 };
79
80 #define IDENT_LENGTH 8
81
82 /* The file we'll be writing the PCH to.  */
83 static FILE *pch_outfile;
84
85 static const char *get_ident (void);
86
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.  */
91
92 static const char *
93 get_ident (void)
94 {
95   static char result[IDENT_LENGTH];
96   static const char templ[] = "gpch.014";
97   static const char c_language_chars[] = "Co+O";
98
99   memcpy (result, templ, IDENT_LENGTH);
100   result[4] = c_language_chars[c_language];
101
102   return result;
103 }
104
105 /* Whether preprocessor state should be saved by pch_init.  */
106
107 static bool pch_ready_to_save_cpp_state = false;
108
109 /* Prepare to write a PCH file, if one is being written.  This is
110    called at the start of compilation.  */
111
112 void
113 pch_init (void)
114 {
115   FILE *f;
116   struct c_pch_validity v;
117   void *target_validity;
118   static const char partial_pch[] = "gpcWrite";
119
120   if (!pch_file)
121     return;
122
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;
128
129   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
130
131   memset (&v, '\0', sizeof (v));
132   v.debug_info_type = write_symbols;
133   {
134     size_t i;
135     for (i = 0; i < MATCH_SIZE; i++)
136       {
137         v.match[i] = *pch_matching[i].flag_var;
138         gcc_assert (v.match[i] == *pch_matching[i].flag_var);
139       }
140   }
141   v.pch_init = &pch_init;
142   target_validity = targetm.get_pch_validity (&v.target_data_length);
143
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);
149
150   /* Let the debugging format deal with the PCHness.  */
151   (*debug_hooks->handle_pch) (0);
152
153   if (pch_ready_to_save_cpp_state)
154     pch_cpp_save_state ();
155
156   XDELETE (target_validity);
157 }
158
159 /* Whether preprocessor state has been saved in a PCH file.  */
160
161 static bool pch_cpp_state_saved = false;
162
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.  */
166
167 void
168 pch_cpp_save_state (void)
169 {
170   if (!pch_cpp_state_saved)
171     {
172       if (pch_outfile)
173         {
174           cpp_save_state (parse_in, pch_outfile);
175           pch_cpp_state_saved = true;
176         }
177       else
178         pch_ready_to_save_cpp_state = true;
179     }
180 }
181
182 /* Write the PCH file.  This is called at the end of a compilation which
183    will produce a PCH file.  */
184
185 void
186 c_common_write_pch (void)
187 {
188   timevar_push (TV_PCH_SAVE);
189
190   targetm.prepare_pch_save ();
191
192   (*debug_hooks->handle_pch) (1);
193
194   prepare_target_option_nodes_for_pch ();
195
196   cpp_write_pch_deps (parse_in, pch_outfile);
197
198   gt_pch_save (pch_outfile);
199
200   timevar_push (TV_PCH_CPP_SAVE);
201   cpp_write_pch_state (parse_in, pch_outfile);
202   timevar_pop (TV_PCH_CPP_SAVE);
203
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);
207
208   fclose (pch_outfile);
209
210   timevar_pop (TV_PCH_SAVE);
211 }
212
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.  */
217
218 int
219 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
220 {
221   int sizeread;
222   int result;
223   char ident[IDENT_LENGTH + 16];
224   const char *pch_ident;
225   struct c_pch_validity v;
226
227   /* Perform a quick test of whether this is a valid
228      precompiled header for the current language.  */
229
230   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
231
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)
236     {
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;
241     }
242
243   pch_ident = get_ident();
244   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
245     {
246       if (cpp_get_options (pfile)->warn_invalid_pch)
247         {
248           if (memcmp (ident, pch_ident, 5) == 0)
249             /* It's a PCH, for the right language, but has the wrong version.
250              */
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);
260         }
261       return 2;
262     }
263   if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
264     {
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;
269     }
270
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);
276
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)
282     {
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;
289     }
290
291   /* Check flags that must match exactly.  */
292   {
293     size_t i;
294     for (i = 0; i < MATCH_SIZE; i++)
295       if (*pch_matching[i].flag_var != v.match[i])
296         {
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;
302         }
303   }
304
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)
312     {
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;
317     }
318
319   /* Check the target-specific validity data.  */
320   {
321     void *this_file_data = xmalloc (v.target_data_length);
322     const char *msg;
323
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)
330       {
331         if (cpp_get_options (pfile)->warn_invalid_pch)
332           cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
333         return 2;
334       }
335   }
336
337   /* Check the preprocessor macros are the same as when the PCH was
338      generated.  */
339
340   result = cpp_valid_state (pfile, name, fd);
341   if (result == -1)
342     return 2;
343   else
344     return result == 0;
345 }
346
347 /* If non-NULL, this function is called after a precompile header file
348    is loaded.  */
349 void (*lang_post_pch_load) (void);
350
351 /* Load in the PCH file NAME, open on FD.  It was originally searched for
352    by ORIG_NAME.  */
353
354 void
355 c_common_read_pch (cpp_reader *pfile, const char *name,
356                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
357 {
358   FILE *f;
359   struct save_macro_data *smd;
360   expanded_location saved_loc;
361   bool saved_trace_includes;
362
363   timevar_push (TV_PCH_RESTORE);
364
365   f = fdopen (fd, "rb");
366   if (f == NULL)
367     {
368       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
369       close (fd);
370       goto end;
371     }
372
373   cpp_get_callbacks (parse_in)->valid_pch = NULL;
374
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;
378
379   timevar_push (TV_PCH_CPP_RESTORE);
380   cpp_prepare_state (pfile, &smd);
381   timevar_pop (TV_PCH_CPP_RESTORE);
382
383   gt_pch_restore (f);
384   cpp_set_line_map (pfile, line_table);
385   rebuild_location_adhoc_htab (line_table);
386
387   timevar_push (TV_PCH_CPP_RESTORE);
388   if (cpp_read_state (pfile, name, f, smd) != 0)
389     {
390       fclose (f);
391       timevar_pop (TV_PCH_CPP_RESTORE);
392       goto end;
393     }
394   timevar_pop (TV_PCH_CPP_RESTORE);
395
396
397   fclose (f);
398
399   line_table->trace_includes = saved_trace_includes;
400   linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
401
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) ();
406
407 end:
408   timevar_pop (TV_PCH_RESTORE);
409 }
410
411 /* Indicate that no more PCH files should be read.  */
412
413 void
414 c_common_no_more_pch (void)
415 {
416   if (cpp_get_callbacks (parse_in)->valid_pch)
417     {
418       cpp_get_callbacks (parse_in)->valid_pch = NULL;
419       host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
420     }
421 }
422
423 /* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
424
425 void
426 c_common_pch_pragma (cpp_reader *pfile, const char *name)
427 {
428   int fd;
429
430   if (!cpp_get_options (pfile)->preprocessed)
431     {
432       error ("pch_preprocess pragma should only be used with -fpreprocessed");
433       inform (input_location, "use #include instead");
434       return;
435     }
436
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);
440
441   if (c_common_valid_pch (pfile, name, fd) != 1)
442     {
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);
446     }
447
448   c_common_read_pch (pfile, name, fd, name);
449
450   close (fd);
451 }
452