Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / c-family / c-pch.c
1 /* Precompiled header implementation for the C languages.
2    Copyright (C) 2000-2013 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   cpp_write_pch_deps (parse_in, pch_outfile);
184
185   gt_pch_save (pch_outfile);
186
187   timevar_push (TV_PCH_CPP_SAVE);
188   cpp_write_pch_state (parse_in, pch_outfile);
189   timevar_pop (TV_PCH_CPP_SAVE);
190
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);
194
195   fclose (pch_outfile);
196
197   timevar_pop (TV_PCH_SAVE);
198 }
199
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.  */
204
205 int
206 c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
207 {
208   int sizeread;
209   int result;
210   char ident[IDENT_LENGTH + 16];
211   const char *pch_ident;
212   struct c_pch_validity v;
213
214   /* Perform a quick test of whether this is a valid
215      precompiled header for the current language.  */
216
217   gcc_assert (memcmp (executable_checksum, no_checksum, 16) != 0);
218
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)
223     {
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;
228     }
229
230   pch_ident = get_ident();
231   if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
232     {
233       if (cpp_get_options (pfile)->warn_invalid_pch)
234         {
235           if (memcmp (ident, pch_ident, 5) == 0)
236             /* It's a PCH, for the right language, but has the wrong version.
237              */
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);
247         }
248       return 2;
249     }
250   if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
251     {
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;
256     }
257
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);
263
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)
269     {
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;
276     }
277
278   /* Check flags that must match exactly.  */
279   {
280     size_t i;
281     for (i = 0; i < MATCH_SIZE; i++)
282       if (*pch_matching[i].flag_var != v.match[i])
283         {
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;
289         }
290   }
291
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)
299     {
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;
304     }
305
306   /* Check the target-specific validity data.  */
307   {
308     void *this_file_data = xmalloc (v.target_data_length);
309     const char *msg;
310
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)
317       {
318         if (cpp_get_options (pfile)->warn_invalid_pch)
319           cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
320         return 2;
321       }
322   }
323
324   /* Check the preprocessor macros are the same as when the PCH was
325      generated.  */
326
327   result = cpp_valid_state (pfile, name, fd);
328   if (result == -1)
329     return 2;
330   else
331     return result == 0;
332 }
333
334 /* If non-NULL, this function is called after a precompile header file
335    is loaded.  */
336 void (*lang_post_pch_load) (void);
337
338 /* Load in the PCH file NAME, open on FD.  It was originally searched for
339    by ORIG_NAME.  */
340
341 void
342 c_common_read_pch (cpp_reader *pfile, const char *name,
343                    int fd, const char *orig_name ATTRIBUTE_UNUSED)
344 {
345   FILE *f;
346   struct save_macro_data *smd;
347   expanded_location saved_loc;
348   bool saved_trace_includes;
349
350   timevar_push (TV_PCH_RESTORE);
351
352   f = fdopen (fd, "rb");
353   if (f == NULL)
354     {
355       cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
356       close (fd);
357       goto end;
358     }
359
360   cpp_get_callbacks (parse_in)->valid_pch = NULL;
361
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;
365
366   timevar_push (TV_PCH_CPP_RESTORE);
367   cpp_prepare_state (pfile, &smd);
368   timevar_pop (TV_PCH_CPP_RESTORE);
369
370   gt_pch_restore (f);
371   cpp_set_line_map (pfile, line_table);
372   rebuild_location_adhoc_htab (line_table);
373
374   timevar_push (TV_PCH_CPP_RESTORE);
375   if (cpp_read_state (pfile, name, f, smd) != 0)
376     {
377       fclose (f);
378       timevar_pop (TV_PCH_CPP_RESTORE);
379       goto end;
380     }
381   timevar_pop (TV_PCH_CPP_RESTORE);
382
383
384   fclose (f);
385
386   line_table->trace_includes = saved_trace_includes;
387   linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
388
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) ();
393
394 end:
395   timevar_pop (TV_PCH_RESTORE);
396 }
397
398 /* Indicate that no more PCH files should be read.  */
399
400 void
401 c_common_no_more_pch (void)
402 {
403   if (cpp_get_callbacks (parse_in)->valid_pch)
404     {
405       cpp_get_callbacks (parse_in)->valid_pch = NULL;
406       host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
407     }
408 }
409
410 /* Handle #pragma GCC pch_preprocess, to load in the PCH file.  */
411
412 void
413 c_common_pch_pragma (cpp_reader *pfile, const char *name)
414 {
415   int fd;
416
417   if (!cpp_get_options (pfile)->preprocessed)
418     {
419       error ("pch_preprocess pragma should only be used with -fpreprocessed");
420       inform (input_location, "use #include instead");
421       return;
422     }
423
424   fd = open (name, O_RDONLY | O_BINARY, 0666);
425   if (fd == -1)
426     fatal_error ("%s: couldn%'t open PCH file: %m", name);
427
428   if (c_common_valid_pch (pfile, name, fd) != 1)
429     {
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);
433     }
434
435   c_common_read_pch (pfile, name, fd, name);
436
437   close (fd);
438 }
439