]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-pch.c
trans.c (check_inlining_for_nested_subprog): Quote reserved names.
[thirdparty/gcc.git] / gcc / c-family / c-pch.c
CommitLineData
17211ab5 1/* Precompiled header implementation for the C languages.
a5544970 2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
17211ab5 3
54a7b573 4This file is part of GCC.
17211ab5 5
54a7b573 6GCC is free software; you can redistribute it and/or modify
17211ab5 7it under the terms of the GNU General Public License as published by
9dcd6f09 8the Free Software Foundation; either version 3, or (at your option)
17211ab5
GK
9any later version.
10
54a7b573 11GCC is distributed in the hope that it will be useful,
17211ab5
GK
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
17211ab5
GK
19
20#include "config.h"
21#include "system.h"
22#include "coretypes.h"
2adfab87 23#include "target.h"
2adfab87
AM
24#include "c-common.h"
25#include "timevar.h"
df026186 26#include "flags.h"
17211ab5
GK
27#include "debug.h"
28#include "c-pragma.h"
8643e92d 29#include "langhooks.h"
18c81520 30#include "hosthooks.h"
17211ab5 31
54e109ed
GK
32/* This is a list of flag variables that must match exactly, and their
33 names for the error message. The possible values for *flag_var must
34 fit in a 'signed char'. */
35
c22cacf3 36static const struct c_pch_matching
54e109ed
GK
37{
38 int *flag_var;
39 const char *flag_name;
40} pch_matching[] = {
41 { &flag_exceptions, "-fexceptions" },
54e109ed
GK
42};
43
44enum {
45 MATCH_SIZE = ARRAY_SIZE (pch_matching)
46};
47
3fd30b88 48/* Information about flags and suchlike that affect PCH validity.
7bb1ad93 49
3fd30b88
GK
50 Before this structure is read, both an initial 8-character identification
51 string, and a 16-byte checksum, have been read and validated. */
7451756f 52
df026186
GK
53struct c_pch_validity
54{
55 unsigned char debug_info_type;
54e109ed 56 signed char match[MATCH_SIZE];
926a822f 57 void (*pch_init) (void);
7bb1ad93 58 size_t target_data_length;
df026186
GK
59};
60
8643e92d 61#define IDENT_LENGTH 8
17211ab5 62
7451756f 63/* The file we'll be writing the PCH to. */
17211ab5
GK
64static FILE *pch_outfile;
65
2f6e4e97 66static const char *get_ident (void);
8643e92d 67
df026186
GK
68/* Compute an appropriate 8-byte magic number for the PCH file, so that
69 utilities like file(1) can identify it, and so that GCC can quickly
70 ignore non-PCH files and PCH files that are of a completely different
71 format. */
72
8643e92d 73static const char *
3f75a254 74get_ident (void)
8643e92d
GK
75{
76 static char result[IDENT_LENGTH];
8ca92d04 77 static const char templ[] = "gpch.014";
37fa72e9 78 static const char c_language_chars[] = "Co+O";
c22cacf3 79
48c54229 80 memcpy (result, templ, IDENT_LENGTH);
37fa72e9
NB
81 result[4] = c_language_chars[c_language];
82
8643e92d
GK
83 return result;
84}
85
1efcb8c6
JM
86/* Whether preprocessor state should be saved by pch_init. */
87
88static bool pch_ready_to_save_cpp_state = false;
89
3fd30b88 90/* Prepare to write a PCH file, if one is being written. This is
ee7b28eb 91 called at the start of compilation. */
df026186 92
17211ab5 93void
2f6e4e97 94pch_init (void)
17211ab5
GK
95{
96 FILE *f;
df026186 97 struct c_pch_validity v;
7bb1ad93 98 void *target_validity;
5eb4df45 99 static const char partial_pch[] = "gpcWrite";
c22cacf3 100
3f75a254 101 if (!pch_file)
df026186 102 return;
c22cacf3 103
df026186
GK
104 f = fopen (pch_file, "w+b");
105 if (f == NULL)
a9c697b8 106 fatal_error (input_location, "cannot create precompiled header %s: %m",
40fecdd6 107 pch_file);
df026186 108 pch_outfile = f;
366de0ce 109
ed5bdeb6 110 memset (&v, '\0', sizeof (v));
df026186 111 v.debug_info_type = write_symbols;
54e109ed
GK
112 {
113 size_t i;
114 for (i = 0; i < MATCH_SIZE; i++)
115 {
116 v.match[i] = *pch_matching[i].flag_var;
366de0ce 117 gcc_assert (v.match[i] == *pch_matching[i].flag_var);
54e109ed
GK
118 }
119 }
926a822f 120 v.pch_init = &pch_init;
7bb1ad93 121 target_validity = targetm.get_pch_validity (&v.target_data_length);
c22cacf3 122
7a681365 123 if (fwrite (partial_pch, IDENT_LENGTH, 1, f) != 1
3fd30b88 124 || fwrite (executable_checksum, 16, 1, f) != 1
7451756f 125 || fwrite (&v, sizeof (v), 1, f) != 1
7bb1ad93 126 || fwrite (target_validity, v.target_data_length, 1, f) != 1)
a9c697b8 127 fatal_error (input_location, "cannot write to %s: %m", pch_file);
df026186 128
df026186
GK
129 /* Let the debugging format deal with the PCHness. */
130 (*debug_hooks->handle_pch) (0);
c22cacf3 131
1efcb8c6
JM
132 if (pch_ready_to_save_cpp_state)
133 pch_cpp_save_state ();
0b50e654
JJ
134
135 XDELETE (target_validity);
1efcb8c6
JM
136}
137
138/* Whether preprocessor state has been saved in a PCH file. */
139
140static bool pch_cpp_state_saved = false;
141
142/* Save preprocessor state in a PCH file, after implicitly included
143 headers have been read. If the PCH file has not yet been opened,
144 record that state should be saved when it is opened. */
145
146void
147pch_cpp_save_state (void)
148{
149 if (!pch_cpp_state_saved)
150 {
151 if (pch_outfile)
152 {
153 cpp_save_state (parse_in, pch_outfile);
154 pch_cpp_state_saved = true;
155 }
156 else
157 pch_ready_to_save_cpp_state = true;
158 }
17211ab5
GK
159}
160
df026186
GK
161/* Write the PCH file. This is called at the end of a compilation which
162 will produce a PCH file. */
163
17211ab5 164void
2f6e4e97 165c_common_write_pch (void)
17211ab5 166{
10d43c2d
DN
167 timevar_push (TV_PCH_SAVE);
168
e32ea2d1
RS
169 targetm.prepare_pch_save ();
170
33b49800
GK
171 (*debug_hooks->handle_pch) (1);
172
e83b8e2e
JJ
173 prepare_target_option_nodes_for_pch ();
174
17211ab5
GK
175 cpp_write_pch_deps (parse_in, pch_outfile);
176
17211ab5 177 gt_pch_save (pch_outfile);
10d43c2d
DN
178
179 timevar_push (TV_PCH_CPP_SAVE);
17211ab5 180 cpp_write_pch_state (parse_in, pch_outfile);
10d43c2d 181 timevar_pop (TV_PCH_CPP_SAVE);
17211ab5 182
7a681365
GK
183 if (fseek (pch_outfile, 0, SEEK_SET) != 0
184 || fwrite (get_ident (), IDENT_LENGTH, 1, pch_outfile) != 1)
a9c697b8 185 fatal_error (input_location, "cannot write %s: %m", pch_file);
7a681365 186
17211ab5 187 fclose (pch_outfile);
10d43c2d
DN
188
189 timevar_pop (TV_PCH_SAVE);
17211ab5
GK
190}
191
7bb1ad93
GK
192/* Check the PCH file called NAME, open on FD, to see if it can be
193 used in this compilation. Return 1 if valid, 0 if the file can't
194 be used now but might be if it's seen later in the compilation, and
195 2 if this file could never be used in the compilation. */
df026186 196
17211ab5 197int
2f6e4e97 198c_common_valid_pch (cpp_reader *pfile, const char *name, int fd)
17211ab5
GK
199{
200 int sizeread;
201 int result;
3fd30b88 202 char ident[IDENT_LENGTH + 16];
8643e92d 203 const char *pch_ident;
df026186 204 struct c_pch_validity v;
17211ab5 205
17211ab5 206 /* Perform a quick test of whether this is a valid
df026186 207 precompiled header for the current language. */
17211ab5 208
3fd30b88 209 sizeread = read (fd, ident, IDENT_LENGTH + 16);
17211ab5 210 if (sizeread == -1)
a9c697b8 211 fatal_error (input_location, "cannot read %s: %m", name);
3fd30b88
GK
212 else if (sizeread != IDENT_LENGTH + 16)
213 {
bb1f73c2
AP
214 if (cpp_get_options (pfile)->warn_invalid_pch)
215 cpp_error (pfile, CPP_DL_WARNING, "%s: too short to be a PCH file",
216 name);
3fd30b88
GK
217 return 2;
218 }
c22cacf3 219
8643e92d
GK
220 pch_ident = get_ident();
221 if (memcmp (ident, pch_ident, IDENT_LENGTH) != 0)
17211ab5
GK
222 {
223 if (cpp_get_options (pfile)->warn_invalid_pch)
224 {
225 if (memcmp (ident, pch_ident, 5) == 0)
226 /* It's a PCH, for the right language, but has the wrong version.
227 */
c22cacf3 228 cpp_error (pfile, CPP_DL_WARNING,
17211ab5
GK
229 "%s: not compatible with this GCC version", name);
230 else if (memcmp (ident, pch_ident, 4) == 0)
231 /* It's a PCH for the wrong language. */
0527bc4e 232 cpp_error (pfile, CPP_DL_WARNING, "%s: not for %s", name,
8643e92d 233 lang_hooks.name);
c22cacf3 234 else
17211ab5 235 /* Not any kind of PCH. */
0527bc4e 236 cpp_error (pfile, CPP_DL_WARNING, "%s: not a PCH file", name);
17211ab5
GK
237 }
238 return 2;
239 }
3fd30b88 240 if (memcmp (ident + IDENT_LENGTH, executable_checksum, 16) != 0)
7451756f
GK
241 {
242 if (cpp_get_options (pfile)->warn_invalid_pch)
0527bc4e 243 cpp_error (pfile, CPP_DL_WARNING,
3fd30b88 244 "%s: created by a different GCC executable", name);
7451756f
GK
245 return 2;
246 }
247
3fd30b88
GK
248 /* At this point, we know it's a PCH file created by this
249 executable, so it ought to be long enough that we can read a
250 c_pch_validity structure. */
251 if (read (fd, &v, sizeof (v)) != sizeof (v))
a9c697b8 252 fatal_error (input_location, "cannot read %s: %m", name);
3fd30b88 253
df026186
GK
254 /* The allowable debug info combinations are that either the PCH file
255 was built with the same as is being used now, or the PCH file was
256 built for some kind of debug info but now none is in use. */
257 if (v.debug_info_type != write_symbols
258 && write_symbols != NO_DEBUG)
259 {
260 if (cpp_get_options (pfile)->warn_invalid_pch)
c22cacf3 261 cpp_error (pfile, CPP_DL_WARNING,
df026186
GK
262 "%s: created with -g%s, but used with -g%s", name,
263 debug_type_names[v.debug_info_type],
264 debug_type_names[write_symbols]);
265 return 2;
266 }
267
54e109ed
GK
268 /* Check flags that must match exactly. */
269 {
270 size_t i;
271 for (i = 0; i < MATCH_SIZE; i++)
272 if (*pch_matching[i].flag_var != v.match[i])
273 {
274 if (cpp_get_options (pfile)->warn_invalid_pch)
c22cacf3 275 cpp_error (pfile, CPP_DL_WARNING,
54e109ed
GK
276 "%s: settings for %s do not match", name,
277 pch_matching[i].flag_name);
278 return 2;
279 }
280 }
281
926a822f
MM
282 /* If the text segment was not loaded at the same address as it was
283 when the PCH file was created, function pointers loaded from the
284 PCH will not be valid. We could in theory remap all the function
c22cacf3 285 pointers, but no support for that exists at present.
3fd30b88
GK
286 Since we have the same executable, it should only be necessary to
287 check one function. */
926a822f
MM
288 if (v.pch_init != &pch_init)
289 {
290 if (cpp_get_options (pfile)->warn_invalid_pch)
c22cacf3 291 cpp_error (pfile, CPP_DL_WARNING,
926a822f
MM
292 "%s: had text segment at different address", name);
293 return 2;
294 }
295
7bb1ad93
GK
296 /* Check the target-specific validity data. */
297 {
298 void *this_file_data = xmalloc (v.target_data_length);
299 const char *msg;
c22cacf3 300
7bb1ad93
GK
301 if ((size_t) read (fd, this_file_data, v.target_data_length)
302 != v.target_data_length)
a9c697b8 303 fatal_error (input_location, "cannot read %s: %m", name);
7bb1ad93
GK
304 msg = targetm.pch_valid_p (this_file_data, v.target_data_length);
305 free (this_file_data);
306 if (msg != NULL)
307 {
308 if (cpp_get_options (pfile)->warn_invalid_pch)
0527bc4e 309 cpp_error (pfile, CPP_DL_WARNING, "%s: %s", name, msg);
7bb1ad93
GK
310 return 2;
311 }
312 }
313
17211ab5
GK
314 /* Check the preprocessor macros are the same as when the PCH was
315 generated. */
c22cacf3 316
17211ab5
GK
317 result = cpp_valid_state (pfile, name, fd);
318 if (result == -1)
319 return 2;
320 else
321 return result == 0;
322}
323
4684cd27
MM
324/* If non-NULL, this function is called after a precompile header file
325 is loaded. */
326void (*lang_post_pch_load) (void);
327
df026186
GK
328/* Load in the PCH file NAME, open on FD. It was originally searched for
329 by ORIG_NAME. */
330
17211ab5 331void
2f6e4e97
AJ
332c_common_read_pch (cpp_reader *pfile, const char *name,
333 int fd, const char *orig_name ATTRIBUTE_UNUSED)
17211ab5
GK
334{
335 FILE *f;
17211ab5 336 struct save_macro_data *smd;
5ffeb913 337 expanded_location saved_loc;
b3e200e1 338 bool saved_trace_includes;
c22cacf3 339
10d43c2d
DN
340 timevar_push (TV_PCH_RESTORE);
341
17211ab5
GK
342 f = fdopen (fd, "rb");
343 if (f == NULL)
344 {
0527bc4e 345 cpp_errno (pfile, CPP_DL_ERROR, "calling fdopen");
d4c32e1d 346 close (fd);
10d43c2d 347 goto end;
17211ab5
GK
348 }
349
18c81520 350 cpp_get_callbacks (parse_in)->valid_pch = NULL;
17211ab5 351
5ffeb913 352 /* Save the location and then restore it after reading the PCH. */
5ffeb913 353 saved_loc = expand_location (line_table->highest_line);
b3e200e1 354 saved_trace_includes = line_table->trace_includes;
5ffeb913 355
10d43c2d 356 timevar_push (TV_PCH_CPP_RESTORE);
17211ab5 357 cpp_prepare_state (pfile, &smd);
10d43c2d 358 timevar_pop (TV_PCH_CPP_RESTORE);
17211ab5
GK
359
360 gt_pch_restore (f);
671d9f12 361 cpp_set_line_map (pfile, line_table);
52187008 362 rebuild_location_adhoc_htab (line_table);
17211ab5 363
10d43c2d 364 timevar_push (TV_PCH_CPP_RESTORE);
17211ab5 365 if (cpp_read_state (pfile, name, f, smd) != 0)
d4c32e1d
JJ
366 {
367 fclose (f);
10d43c2d
DN
368 timevar_pop (TV_PCH_CPP_RESTORE);
369 goto end;
d4c32e1d 370 }
10d43c2d
DN
371 timevar_pop (TV_PCH_CPP_RESTORE);
372
17211ab5
GK
373
374 fclose (f);
c22cacf3 375
b3e200e1 376 line_table->trace_includes = saved_trace_includes;
892a371f 377 linemap_add (line_table, LC_ENTER, 0, saved_loc.file, saved_loc.line);
5ffeb913 378
4684cd27 379 /* Give the front end a chance to take action after a PCH file has
6cb38cd4 380 been loaded. */
4684cd27
MM
381 if (lang_post_pch_load)
382 (*lang_post_pch_load) ();
10d43c2d
DN
383
384end:
385 timevar_pop (TV_PCH_RESTORE);
17211ab5 386}
18c81520
GK
387
388/* Indicate that no more PCH files should be read. */
389
390void
391c_common_no_more_pch (void)
392{
393 if (cpp_get_callbacks (parse_in)->valid_pch)
394 {
395 cpp_get_callbacks (parse_in)->valid_pch = NULL;
4d0c31e6 396 host_hooks.gt_pch_use_address (NULL, 0, -1, 0);
18c81520
GK
397 }
398}
c0d578e6
GK
399
400/* Handle #pragma GCC pch_preprocess, to load in the PCH file. */
401
c0d578e6 402void
bc4071dd 403c_common_pch_pragma (cpp_reader *pfile, const char *name)
c0d578e6 404{
c0d578e6
GK
405 int fd;
406
3f75a254 407 if (!cpp_get_options (pfile)->preprocessed)
c0d578e6 408 {
a9c697b8 409 error ("%<pch_preprocess%> pragma should only be used "
a3f9f006 410 "with %<-fpreprocessed%>");
a9c697b8 411 inform (input_location, "use %<#include%> instead");
c0d578e6
GK
412 return;
413 }
414
c0d578e6
GK
415 fd = open (name, O_RDONLY | O_BINARY, 0666);
416 if (fd == -1)
40fecdd6 417 fatal_error (input_location, "%s: couldn%'t open PCH file: %m", name);
c22cacf3 418
c0d578e6
GK
419 if (c_common_valid_pch (pfile, name, fd) != 1)
420 {
421 if (!cpp_get_options (pfile)->warn_invalid_pch)
a3f9f006 422 inform (input_location, "use %<-Winvalid-pch%> for more information");
40fecdd6 423 fatal_error (input_location, "%s: PCH file was invalid", name);
c0d578e6 424 }
c22cacf3 425
c0d578e6 426 c_common_read_pch (pfile, name, fd, name);
c22cacf3 427
c0d578e6
GK
428 close (fd);
429}
3fd30b88 430