]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-pragma.c
Update copyright years.
[thirdparty/gcc.git] / gcc / c-family / c-pragma.c
CommitLineData
a187ac95 1/* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
7adcbafe 2 Copyright (C) 1992-2022 Free Software Foundation, Inc.
a187ac95 3
1322177d 4This file is part of GCC.
a187ac95 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
a187ac95 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
a187ac95
RS
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/>. */
a187ac95 19
4b578ebf 20#include "config.h"
670ee920 21#include "system.h"
4977bab6 22#include "coretypes.h"
2adfab87
AM
23#include "target.h"
24#include "function.h" /* For cfun. */
2adfab87 25#include "c-common.h"
4d0cdd0c 26#include "memmodel.h"
2adfab87 27#include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS. */
d8a2d370 28#include "stringpool.h"
2adfab87
AM
29#include "cgraph.h"
30#include "diagnostic.h"
d8a2d370
DN
31#include "attribs.h"
32#include "varasm.h"
3d6f7931 33#include "c-pragma.h"
79cf5994 34#include "opts.h"
7ac8318c 35#include "plugin.h"
5bcc5a3b 36#include "opt-suggestions.h"
bc4071dd 37
b9b8dde3
DD
38#define GCC_BAD(gmsgid) \
39 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
4b794eaf 40#define GCC_BAD2(gmsgid, arg) \
b9b8dde3 41 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
8722a170
DM
42#define GCC_BAD_AT(loc, gmsgid) \
43 do { warning_at (loc, OPT_Wpragmas, gmsgid); return; } while (0)
44#define GCC_BAD2_AT(loc, gmsgid, arg) \
45 do { warning_at (loc, OPT_Wpragmas, gmsgid, arg); return; } while (0)
0e5921e8 46
a79683d5 47struct GTY(()) align_stack {
c22cacf3
MS
48 int alignment;
49 tree id;
e2af664c 50 struct align_stack * prev;
a79683d5 51};
e2af664c 52
e2500fed
GK
53static GTY(()) struct align_stack * alignment_stack;
54
35b1a6fa 55static void handle_pragma_pack (cpp_reader *);
e2af664c 56
63eb1269 57/* If we have a "global" #pragma pack(<n>) in effect when the first
c22cacf3
MS
58 #pragma pack(push,<n>) is encountered, this stores the value of
59 maximum_field_alignment in effect. When the final pop_alignment()
61e8b354 60 happens, we restore the value to this, not to a value of 0 for
ec5c56db 61 maximum_field_alignment. Value is in bits. */
ecb0eece 62static int default_alignment;
467cecf3
JB
63#define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
64 ? &default_alignment \
65 : &alignment_stack->alignment) = (ALIGN))
61e8b354 66
35b1a6fa
AJ
67static void push_alignment (int, tree);
68static void pop_alignment (tree);
e2af664c
NC
69
70/* Push an alignment value onto the stack. */
0e5921e8 71static void
35b1a6fa 72push_alignment (int alignment, tree id)
e2af664c 73{
766090c2 74 align_stack * entry = ggc_alloc<align_stack> ();
e2af664c 75
467cecf3 76 entry->alignment = alignment;
c22cacf3
MS
77 entry->id = id;
78 entry->prev = alignment_stack;
79
80 /* The current value of maximum_field_alignment is not necessarily
81 0 since there may be a #pragma pack(<n>) in effect; remember it
467cecf3
JB
82 so that we can restore it after the final #pragma pop(). */
83 if (alignment_stack == NULL)
84 default_alignment = maximum_field_alignment;
c22cacf3 85
467cecf3 86 alignment_stack = entry;
e2af664c 87
467cecf3 88 maximum_field_alignment = alignment;
e2af664c
NC
89}
90
91/* Undo a push of an alignment onto the stack. */
0e5921e8 92static void
35b1a6fa 93pop_alignment (tree id)
e2af664c 94{
0f92adae 95 align_stack * entry;
c22cacf3 96
e2af664c 97 if (alignment_stack == NULL)
a9c697b8
MS
98 GCC_BAD ("%<#pragma pack (pop)%> encountered without matching "
99 "%<#pragma pack (push)%>");
e2af664c 100
0f92adae
JM
101 /* If we got an identifier, strip away everything above the target
102 entry so that the next step will restore the state just below it. */
103 if (id)
104 {
105 for (entry = alignment_stack; entry; entry = entry->prev)
106 if (entry->id == id)
107 {
0f92adae
JM
108 alignment_stack = entry;
109 break;
110 }
111 if (entry == NULL)
a9c697b8
MS
112 warning (OPT_Wpragmas,
113 "%<#pragma pack(pop, %E)%> encountered without matching "
114 "%<#pragma pack(push, %E)%>"
88388a52 115 , id, id);
0f92adae
JM
116 }
117
467cecf3 118 entry = alignment_stack->prev;
e2af664c 119
467cecf3 120 maximum_field_alignment = entry ? entry->alignment : default_alignment;
e2af664c 121
467cecf3 122 alignment_stack = entry;
e2af664c 123}
a187ac95 124
0e5921e8
ZW
125/* #pragma pack ()
126 #pragma pack (N)
c22cacf3 127
467cecf3 128 #pragma pack (push)
0e5921e8 129 #pragma pack (push, N)
467cecf3 130 #pragma pack (push, ID)
0e5921e8
ZW
131 #pragma pack (push, ID, N)
132 #pragma pack (pop)
133 #pragma pack (pop, ID) */
134static void
e18476eb 135handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
0e5921e8 136{
8722a170 137 location_t loc;
0e5921e8 138 tree x, id = 0;
63eb1269 139 int align = -1;
0e5921e8 140 enum cpp_ttype token;
4337bc93 141 enum { set, push, pop } action;
0e5921e8 142
75ce3d48 143 if (pragma_lex (&x) != CPP_OPEN_PAREN)
bda67431 144 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
0e5921e8 145
8722a170 146 token = pragma_lex (&x, &loc);
0e5921e8 147 if (token == CPP_CLOSE_PAREN)
4337bc93
ZW
148 {
149 action = set;
467cecf3 150 align = initial_max_fld_align;
4337bc93 151 }
0e5921e8
ZW
152 else if (token == CPP_NUMBER)
153 {
12050e44 154 if (TREE_CODE (x) != INTEGER_CST)
8722a170 155 GCC_BAD_AT (loc, "invalid constant in %<#pragma pack%> - ignored");
0e5921e8
ZW
156 align = TREE_INT_CST_LOW (x);
157 action = set;
75ce3d48 158 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
bda67431 159 GCC_BAD ("malformed %<#pragma pack%> - ignored");
0e5921e8
ZW
160 }
161 else if (token == CPP_NAME)
162 {
467cecf3 163#define GCC_BAD_ACTION do { if (action != pop) \
bda67431 164 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
63eb1269 165 else \
bda67431 166 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
63eb1269
MC
167 } while (0)
168
4337bc93
ZW
169 const char *op = IDENTIFIER_POINTER (x);
170 if (!strcmp (op, "push"))
0e5921e8 171 action = push;
4337bc93 172 else if (!strcmp (op, "pop"))
0e5921e8
ZW
173 action = pop;
174 else
8722a170
DM
175 GCC_BAD2_AT (loc, "unknown action %qE for %<#pragma pack%> - ignored",
176 x);
a187ac95 177
75ce3d48 178 while ((token = pragma_lex (&x)) == CPP_COMMA)
e2af664c 179 {
8722a170 180 token = pragma_lex (&x, &loc);
467cecf3 181 if (token == CPP_NAME && id == 0)
63eb1269
MC
182 {
183 id = x;
63eb1269 184 }
467cecf3 185 else if (token == CPP_NUMBER && action == push && align == -1)
63eb1269 186 {
12050e44 187 if (TREE_CODE (x) != INTEGER_CST)
8722a170
DM
188 GCC_BAD_AT (loc,
189 "invalid constant in %<#pragma pack%> - ignored");
467cecf3
JB
190 align = TREE_INT_CST_LOW (x);
191 if (align == -1)
192 action = set;
63eb1269 193 }
467cecf3
JB
194 else
195 GCC_BAD_ACTION;
e2af664c 196 }
4337bc93 197
63eb1269 198 if (token != CPP_CLOSE_PAREN)
da4083c7
CR
199 GCC_BAD_ACTION;
200#undef GCC_BAD_ACTION
e2af664c 201 }
4337bc93 202 else
bda67431 203 GCC_BAD ("malformed %<#pragma pack%> - ignored");
4337bc93 204
8722a170
DM
205 if (pragma_lex (&x, &loc) != CPP_EOF)
206 warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma pack%>");
0e5921e8 207
467cecf3 208 if (flag_pack_struct)
a9c697b8 209 GCC_BAD ("%<#pragma pack%> has no effect with %<-fpack-struct%> - ignored");
467cecf3 210
63eb1269
MC
211 if (action != pop)
212 switch (align)
213 {
214 case 0:
215 case 1:
216 case 2:
217 case 4:
218 case 8:
219 case 16:
220 align *= BITS_PER_UNIT;
221 break;
467cecf3
JB
222 case -1:
223 if (action == push)
224 {
225 align = maximum_field_alignment;
226 break;
227 }
191816a3 228 /* FALLTHRU */
63eb1269 229 default:
da4083c7 230 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
63eb1269 231 }
fc009f96 232
0e5921e8
ZW
233 switch (action)
234 {
235 case set: SET_GLOBAL_ALIGNMENT (align); break;
0e5921e8 236 case push: push_alignment (align, id); break;
c22cacf3 237 case pop: pop_alignment (id); break;
0e5921e8
ZW
238 }
239}
a187ac95 240
a79683d5 241struct GTY(()) pending_weak
18bc5398
SB
242{
243 tree name;
244 tree value;
a79683d5 245};
18bc5398 246
18bc5398 247
9771b263 248static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
e2500fed 249
35b1a6fa
AJ
250static void apply_pragma_weak (tree, tree);
251static void handle_pragma_weak (cpp_reader *);
a187ac95 252
ecb0eece 253static void
35b1a6fa 254apply_pragma_weak (tree decl, tree value)
ecb0eece
RH
255{
256 if (value)
b53bb376
RH
257 {
258 value = build_string (IDENTIFIER_LENGTH (value),
259 IDENTIFIER_POINTER (value));
260 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
261 build_tree_list (NULL, value)),
262 0);
263 }
264
45806a3f 265 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
e0a21ab9 266 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
39a1ebb3 267 && DECL_ASSEMBLER_NAME_SET_P (decl)
45806a3f 268 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
a9c697b8 269 warning (OPT_Wpragmas, "applying %<#pragma weak %+D%> after first use "
c22cacf3 270 "results in unspecified behavior", decl);
45806a3f 271
ecb0eece
RH
272 declare_weak (decl);
273}
274
275void
35b1a6fa 276maybe_apply_pragma_weak (tree decl)
ecb0eece 277{
18bc5398
SB
278 tree id;
279 int i;
280 pending_weak *pe;
ecb0eece 281
49a64b24
GK
282 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
283
284 /* No weak symbols pending, take the short-cut. */
39a1ebb3 285 if (vec_safe_is_empty (pending_weaks))
49a64b24
GK
286 return;
287 /* If it's not visible outside this file, it doesn't matter whether
288 it's weak. */
289 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
ecb0eece 290 return;
49a64b24
GK
291 /* If it's not a function or a variable, it can't be weak.
292 FIXME: what kinds of things are visible outside this file but
366de0ce 293 aren't functions or variables? Should this be an assert instead? */
21b634ae 294 if (!VAR_OR_FUNCTION_DECL_P (decl))
49a64b24
GK
295 return;
296
39a1ebb3
JJ
297 if (DECL_ASSEMBLER_NAME_SET_P (decl))
298 id = DECL_ASSEMBLER_NAME (decl);
299 else
300 {
301 id = DECL_ASSEMBLER_NAME (decl);
302 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
303 }
ecb0eece 304
9771b263 305 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
18bc5398 306 if (id == pe->name)
ecb0eece 307 {
18bc5398 308 apply_pragma_weak (decl, pe->value);
9771b263 309 pending_weaks->unordered_remove (i);
ecb0eece
RH
310 break;
311 }
312}
313
86f029aa
JM
314/* Process all "#pragma weak A = B" directives where we have not seen
315 a decl for A. */
316void
317maybe_apply_pending_pragma_weaks (void)
318{
18bc5398
SB
319 tree alias_id, id, decl;
320 int i;
321 pending_weak *pe;
5e20cdc9 322 symtab_node *target;
86f029aa 323
39a1ebb3 324 if (vec_safe_is_empty (pending_weaks))
9771b263
DN
325 return;
326
327 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
86f029aa 328 {
18bc5398
SB
329 alias_id = pe->name;
330 id = pe->value;
86f029aa 331
18bc5398 332 if (id == NULL)
86f029aa
JM
333 continue;
334
3dafb85c 335 target = symtab_node::get_for_asmname (id);
c2255bc4 336 decl = build_decl (UNKNOWN_LOCATION,
67348ccc 337 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
65d630d4 338 alias_id, default_function_type);
86f029aa
JM
339
340 DECL_ARTIFICIAL (decl) = 1;
341 TREE_PUBLIC (decl) = 1;
86f029aa 342 DECL_WEAK (decl) = 1;
0ae9bd27 343 if (VAR_P (decl))
65d630d4
JH
344 TREE_STATIC (decl) = 1;
345 if (!target)
346 {
347 error ("%q+D aliased to undefined symbol %qE",
348 decl, id);
349 continue;
350 }
86f029aa
JM
351
352 assemble_alias (decl, id);
353 }
354}
355
0e5921e8
ZW
356/* #pragma weak name [= value] */
357static void
e18476eb 358handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
0e5921e8 359{
ecb0eece 360 tree name, value, x, decl;
0e5921e8 361 enum cpp_ttype t;
a187ac95 362
0e5921e8 363 value = 0;
7169a029 364
75ce3d48 365 if (pragma_lex (&name) != CPP_NAME)
a9c697b8 366 GCC_BAD ("malformed %<#pragma weak%>, ignored");
75ce3d48 367 t = pragma_lex (&x);
0e5921e8
ZW
368 if (t == CPP_EQ)
369 {
75ce3d48 370 if (pragma_lex (&value) != CPP_NAME)
36b34127 371 GCC_BAD ("malformed %<#pragma weak%>, ignored");
75ce3d48 372 t = pragma_lex (&x);
0e5921e8
ZW
373 }
374 if (t != CPP_EOF)
bc4071dd 375 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
0f92adae 376
ecb0eece 377 decl = identifier_global_value (name);
6615c446 378 if (decl && DECL_P (decl))
b53bb376 379 {
d1783ae5
KT
380 if (!VAR_OR_FUNCTION_DECL_P (decl))
381 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
382 " ignored", decl);
b53bb376
RH
383 apply_pragma_weak (decl, value);
384 if (value)
08346abd
JH
385 {
386 DECL_EXTERNAL (decl) = 0;
0ae9bd27 387 if (VAR_P (decl))
08346abd
JH
388 TREE_STATIC (decl) = 1;
389 assemble_alias (decl, value);
390 }
b53bb376 391 }
ecb0eece 392 else
18bc5398 393 {
f32682ca 394 pending_weak pe = {name, value};
9771b263 395 vec_safe_push (pending_weaks, pe);
18bc5398 396 }
0e5921e8 397}
0f92adae 398
ee45a32d
EB
399static enum scalar_storage_order_kind global_sso;
400
401void
402maybe_apply_pragma_scalar_storage_order (tree type)
403{
404 if (global_sso == SSO_NATIVE)
405 return;
406
407 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
408
409 if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type)))
410 return;
411
412 if (global_sso == SSO_BIG_ENDIAN)
413 TYPE_REVERSE_STORAGE_ORDER (type) = !BYTES_BIG_ENDIAN;
414 else if (global_sso == SSO_LITTLE_ENDIAN)
415 TYPE_REVERSE_STORAGE_ORDER (type) = BYTES_BIG_ENDIAN;
416 else
417 gcc_unreachable ();
418}
419
420static void
421handle_pragma_scalar_storage_order (cpp_reader *ARG_UNUSED(dummy))
422{
423 const char *kind_string;
424 enum cpp_ttype token;
425 tree x;
426
427 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
4a8ca690 428 {
a9c697b8 429 error ("%<scalar_storage_order%> is not supported because endianness "
4a8ca690
EB
430 "is not uniform");
431 return;
432 }
433
434 if (c_dialect_cxx ())
435 {
436 if (warn_unknown_pragmas > in_system_header_at (input_location))
437 warning (OPT_Wunknown_pragmas,
438 "%<#pragma scalar_storage_order%> is not supported for C++");
439 return;
440 }
ee45a32d
EB
441
442 token = pragma_lex (&x);
443 if (token != CPP_NAME)
444 GCC_BAD ("missing [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
445 kind_string = IDENTIFIER_POINTER (x);
446 if (strcmp (kind_string, "default") == 0)
447 global_sso = default_sso;
448 else if (strcmp (kind_string, "big") == 0)
449 global_sso = SSO_BIG_ENDIAN;
450 else if (strcmp (kind_string, "little") == 0)
451 global_sso = SSO_LITTLE_ENDIAN;
452 else
453 GCC_BAD ("expected [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
454}
455
84b8b0e0
ZW
456/* GCC supports two #pragma directives for renaming the external
457 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
5c30094f 458 compatibility with the Solaris and VMS system headers. GCC also
84b8b0e0
ZW
459 has its own notation for this, __asm__("name") annotations.
460
461 Corner cases of these features and their interaction:
462
463 1) Both pragmas silently apply only to declarations with external
464 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
465 do not have this restriction.
466
467 2) In C++, both #pragmas silently apply only to extern "C" declarations.
468 Asm labels do not have this restriction.
469
470 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
471 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
472 new name is different, a warning issues and the name does not change.
473
474 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
475 *not* the DECL_ASSEMBLER_NAME.
476
477 5) If #pragma extern_prefix is in effect and a declaration occurs
478 with an __asm__ name, the #pragma extern_prefix is silently
479 ignored for that declaration.
480
481 6) If #pragma extern_prefix and #pragma redefine_extname apply to
482 the same declaration, whichever triggered first wins, and a warning
483 is issued. (We would like to have #pragma redefine_extname always
484 win, but it can appear either before or after the declaration, and
485 if it appears afterward, we have no way of knowing whether a modified
486 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
487
a79683d5 488struct GTY(()) pending_redefinition {
4f8c876d
NF
489 tree oldname;
490 tree newname;
a79683d5 491};
4f8c876d 492
4f8c876d 493
9771b263 494static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
e2500fed 495
35b1a6fa 496static void handle_pragma_redefine_extname (cpp_reader *);
41c64394 497
84b8b0e0 498/* #pragma redefine_extname oldname newname */
41c64394 499static void
e18476eb 500handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
41c64394 501{
3a636414 502 tree oldname, newname, decls, x;
41c64394 503 enum cpp_ttype t;
3a636414 504 bool found;
41c64394 505
75ce3d48 506 if (pragma_lex (&oldname) != CPP_NAME)
a9c697b8 507 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
75ce3d48 508 if (pragma_lex (&newname) != CPP_NAME)
a9c697b8 509 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
75ce3d48 510 t = pragma_lex (&x);
41c64394 511 if (t != CPP_EOF)
bc4071dd 512 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
41c64394 513
3a636414
JM
514 found = false;
515 for (decls = c_linkage_bindings (oldname);
516 decls; )
41c64394 517 {
3a636414
JM
518 tree decl;
519 if (TREE_CODE (decls) == TREE_LIST)
84b8b0e0 520 {
3a636414
JM
521 decl = TREE_VALUE (decls);
522 decls = TREE_CHAIN (decls);
84b8b0e0
ZW
523 }
524 else
3a636414
JM
525 {
526 decl = decls;
527 decls = NULL_TREE;
528 }
529
530 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
21b634ae 531 && VAR_OR_FUNCTION_DECL_P (decl))
3a636414
JM
532 {
533 found = true;
534 if (DECL_ASSEMBLER_NAME_SET_P (decl))
535 {
536 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
537 name = targetm.strip_name_encoding (name);
538
a01f151f 539 if (!id_equal (newname, name))
a9c697b8
MS
540 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> "
541 "ignored due to conflict with previous rename");
3a636414
JM
542 }
543 else
3dafb85c 544 symtab->change_decl_assembler_name (decl, newname);
3a636414 545 }
41c64394 546 }
3a636414
JM
547
548 if (!found)
84b8b0e0
ZW
549 /* We have to add this to the rename list even if there's already
550 a global value that doesn't meet the above criteria, because in
551 C++ "struct foo {...};" puts "foo" in the current namespace but
552 does *not* conflict with a subsequent declaration of a function
553 or variable foo. See g++.dg/other/pragma-re-2.C. */
554 add_to_renaming_pragma_list (oldname, newname);
41c64394 555}
41c64394 556
fb037b5d 557/* This is called from here and from ia64-c.c. */
0c3a2ea0 558void
35b1a6fa 559add_to_renaming_pragma_list (tree oldname, tree newname)
0c3a2ea0 560{
4f8c876d
NF
561 unsigned ix;
562 pending_redefinition *p;
563
9771b263 564 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
4f8c876d
NF
565 if (oldname == p->oldname)
566 {
567 if (p->newname != newname)
a9c697b8
MS
568 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored due to "
569 "conflict with previous %<#pragma redefine_extname%>");
4f8c876d
NF
570 return;
571 }
c22cacf3 572
f32682ca 573 pending_redefinition e = {oldname, newname};
9771b263 574 vec_safe_push (pending_redefine_extname, e);
0c3a2ea0
SE
575}
576
e50e723e
TG
577/* The current prefix set by #pragma extern_prefix. */
578GTY(()) tree pragma_extern_prefix;
e2500fed 579
95bd1dd7 580/* Hook from the front ends to apply the results of one of the preceding
41c64394
RH
581 pragmas that rename variables. */
582
583tree
35b1a6fa 584maybe_apply_renaming_pragma (tree decl, tree asmname)
41c64394 585{
4f8c876d
NF
586 unsigned ix;
587 pending_redefinition *p;
84b8b0e0
ZW
588
589 /* The renaming pragmas are only applied to declarations with
590 external linkage. */
21b634ae 591 if (!VAR_OR_FUNCTION_DECL_P (decl)
84b8b0e0
ZW
592 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
593 || !has_c_linkage (decl))
41c64394
RH
594 return asmname;
595
84b8b0e0
ZW
596 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
597 but we may warn about a rename that conflicts. */
598 if (DECL_ASSEMBLER_NAME_SET_P (decl))
41c64394 599 {
84b8b0e0
ZW
600 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
601 oldname = targetm.strip_name_encoding (oldname);
602
603 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
a9c697b8 604 warning (OPT_Wpragmas, "%<asm%> declaration ignored due to "
84b8b0e0
ZW
605 "conflict with previous rename");
606
607 /* Take any pending redefine_extname off the list. */
9771b263 608 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
4f8c876d 609 if (DECL_NAME (decl) == p->oldname)
84b8b0e0
ZW
610 {
611 /* Only warn if there is a conflict. */
a01f151f 612 if (!id_equal (p->newname, oldname))
a9c697b8
MS
613 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
614 "due to conflict with previous rename");
84b8b0e0 615
9771b263 616 pending_redefine_extname->unordered_remove (ix);
84b8b0e0
ZW
617 break;
618 }
6574d78e 619 return NULL_TREE;
41c64394
RH
620 }
621
84b8b0e0 622 /* Find out if we have a pending #pragma redefine_extname. */
9771b263 623 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
4f8c876d 624 if (DECL_NAME (decl) == p->oldname)
84b8b0e0 625 {
4f8c876d 626 tree newname = p->newname;
9771b263 627 pending_redefine_extname->unordered_remove (ix);
41c64394 628
84b8b0e0 629 /* If we already have an asmname, #pragma redefine_extname is
c22cacf3 630 ignored (with a warning if it conflicts). */
84b8b0e0
ZW
631 if (asmname)
632 {
633 if (strcmp (TREE_STRING_POINTER (asmname),
634 IDENTIFIER_POINTER (newname)) != 0)
a9c697b8
MS
635 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
636 "due to conflict with %<asm%> declaration");
84b8b0e0
ZW
637 return asmname;
638 }
41c64394 639
84b8b0e0
ZW
640 /* Otherwise we use what we've got; #pragma extern_prefix is
641 silently ignored. */
642 return build_string (IDENTIFIER_LENGTH (newname),
643 IDENTIFIER_POINTER (newname));
644 }
41c64394 645
84b8b0e0
ZW
646 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
647 if (asmname)
648 return asmname;
41c64394 649
84b8b0e0
ZW
650 /* If #pragma extern_prefix is in effect, apply it. */
651 if (pragma_extern_prefix)
41c64394 652 {
84b8b0e0
ZW
653 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
654 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
655
656 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
657 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
c22cacf3 658
28dab132 659 char *newname = (char *) alloca (plen + ilen + 1);
84b8b0e0
ZW
660
661 memcpy (newname, prefix, plen);
662 memcpy (newname + plen, id, ilen + 1);
663
664 return build_string (plen + ilen, newname);
41c64394 665 }
41c64394 666
84b8b0e0 667 /* Nada. */
6574d78e 668 return NULL_TREE;
41c64394
RH
669}
670
d7afec4b 671
d7afec4b
ND
672static void handle_pragma_visibility (cpp_reader *);
673
9771b263 674static vec<int> visstack;
0ed5edac
JM
675
676/* Push the visibility indicated by STR onto the top of the #pragma
9789ba46
JJ
677 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
678 C++ namespace with visibility attribute and 2 for C++ builtin
679 ABI namespace. push_visibility/pop_visibility calls must have
680 matching KIND, it is not allowed to push visibility using one
681 KIND and pop using a different one. */
0ed5edac
JM
682
683void
9789ba46 684push_visibility (const char *str, int kind)
0ed5edac 685{
9771b263 686 visstack.safe_push (((int) default_visibility) | (kind << 8));
0ed5edac
JM
687 if (!strcmp (str, "default"))
688 default_visibility = VISIBILITY_DEFAULT;
689 else if (!strcmp (str, "internal"))
690 default_visibility = VISIBILITY_INTERNAL;
691 else if (!strcmp (str, "hidden"))
c22cacf3 692 default_visibility = VISIBILITY_HIDDEN;
0ed5edac
JM
693 else if (!strcmp (str, "protected"))
694 default_visibility = VISIBILITY_PROTECTED;
695 else
a9c697b8
MS
696 GCC_BAD ("%<#pragma GCC visibility push()%> must specify %<default%>, "
697 "%<internal%>, %<hidden%> or %<protected%>");
0ed5edac
JM
698 visibility_options.inpragma = 1;
699}
700
9789ba46
JJ
701/* Pop a level of the #pragma visibility stack. Return true if
702 successful. */
0ed5edac 703
9789ba46
JJ
704bool
705pop_visibility (int kind)
0ed5edac 706{
9771b263 707 if (!visstack.length ())
9789ba46 708 return false;
9771b263 709 if ((visstack.last () >> 8) != kind)
9789ba46
JJ
710 return false;
711 default_visibility
9771b263 712 = (enum symbol_visibility) (visstack.pop () & 0xff);
0ed5edac 713 visibility_options.inpragma
9771b263 714 = visstack.length () != 0;
9789ba46 715 return true;
c22cacf3 716}
be1b1c9b 717
d7afec4b
ND
718/* Sets the default visibility for symbols to something other than that
719 specified on the command line. */
0ed5edac 720
d7afec4b
ND
721static void
722handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
be1b1c9b
L
723{
724 /* Form is #pragma GCC visibility push(hidden)|pop */
d7afec4b
ND
725 tree x;
726 enum cpp_ttype token;
727 enum { bad, push, pop } action = bad;
c22cacf3 728
75ce3d48 729 token = pragma_lex (&x);
d7afec4b
ND
730 if (token == CPP_NAME)
731 {
732 const char *op = IDENTIFIER_POINTER (x);
733 if (!strcmp (op, "push"))
c22cacf3 734 action = push;
d7afec4b 735 else if (!strcmp (op, "pop"))
c22cacf3 736 action = pop;
d7afec4b
ND
737 }
738 if (bad == action)
a9c697b8
MS
739 GCC_BAD ("%<#pragma GCC visibility%> must be followed by %<push%> "
740 "or %<pop%>");
d7afec4b
ND
741 else
742 {
743 if (pop == action)
c22cacf3 744 {
9789ba46 745 if (! pop_visibility (0))
0ed5edac 746 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
c22cacf3 747 }
d7afec4b 748 else
c22cacf3
MS
749 {
750 if (pragma_lex (&x) != CPP_OPEN_PAREN)
751 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
752 token = pragma_lex (&x);
753 if (token != CPP_NAME)
a9c697b8 754 GCC_BAD ("malformed %<#pragma GCC visibility push%>");
c22cacf3 755 else
9789ba46 756 push_visibility (IDENTIFIER_POINTER (x), 0);
c22cacf3
MS
757 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
758 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
759 }
d7afec4b 760 }
75ce3d48 761 if (pragma_lex (&x) != CPP_EOF)
b9b8dde3 762 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
d7afec4b
ND
763}
764
79cf5994
DD
765static void
766handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
767{
79cf5994 768 tree x;
c4914de6
MLI
769 location_t loc;
770 enum cpp_ttype token = pragma_lex (&x, &loc);
79cf5994 771 if (token != CPP_NAME)
c4914de6
MLI
772 {
773 warning_at (loc, OPT_Wpragmas,
a1ad0d84 774 "missing [error|warning|ignored|push|pop|ignored_attributes]"
c4914de6 775 " after %<#pragma GCC diagnostic%>");
eaa797e8 776 return;
c4914de6 777 }
c4914de6
MLI
778
779 diagnostic_t kind;
780 const char *kind_string = IDENTIFIER_POINTER (x);
79cf5994
DD
781 if (strcmp (kind_string, "error") == 0)
782 kind = DK_ERROR;
783 else if (strcmp (kind_string, "warning") == 0)
784 kind = DK_WARNING;
785 else if (strcmp (kind_string, "ignored") == 0)
786 kind = DK_IGNORED;
cd7fe53b
DD
787 else if (strcmp (kind_string, "push") == 0)
788 {
789 diagnostic_push_diagnostics (global_dc, input_location);
790 return;
791 }
792 else if (strcmp (kind_string, "pop") == 0)
793 {
794 diagnostic_pop_diagnostics (global_dc, input_location);
795 return;
796 }
a1ad0d84
MP
797 else if (strcmp (kind_string, "ignored_attributes") == 0)
798 {
799 token = pragma_lex (&x, &loc);
800 if (token != CPP_STRING)
801 {
802 warning_at (loc, OPT_Wpragmas,
803 "missing attribute name after %<#pragma GCC diagnostic "
804 "ignored_attributes%>");
805 return;
806 }
807 char *args = xstrdup (TREE_STRING_POINTER (x));
808 const size_t l = strlen (args);
809 if (l == 0)
810 {
811 warning_at (loc, OPT_Wpragmas, "missing argument to %<#pragma GCC "
812 "diagnostic ignored_attributes%>");
813 free (args);
814 return;
815 }
816 else if (args[l - 1] == ',')
817 {
818 warning_at (loc, OPT_Wpragmas, "trailing %<,%> in arguments for "
819 "%<#pragma GCC diagnostic ignored_attributes%>");
820 free (args);
821 return;
822 }
823 auto_vec<char *> v;
824 for (char *p = strtok (args, ","); p; p = strtok (NULL, ","))
825 v.safe_push (p);
826 handle_ignored_attributes_option (&v);
827 free (args);
828 return;
829 }
79cf5994 830 else
c4914de6
MLI
831 {
832 warning_at (loc, OPT_Wpragmas,
a1ad0d84 833 "expected [error|warning|ignored|push|pop|ignored_attributes]"
c4914de6
MLI
834 " after %<#pragma GCC diagnostic%>");
835 return;
836 }
79cf5994 837
c4914de6 838 token = pragma_lex (&x, &loc);
79cf5994 839 if (token != CPP_STRING)
c4914de6
MLI
840 {
841 warning_at (loc, OPT_Wpragmas,
842 "missing option after %<#pragma GCC diagnostic%> kind");
843 return;
844 }
845
c1822f9c
MLI
846 const char *option_string = TREE_STRING_POINTER (x);
847 unsigned int lang_mask = c_common_option_lang_mask () | CL_COMMON;
848 /* option_string + 1 to skip the initial '-' */
849 unsigned int option_index = find_opt (option_string + 1, lang_mask);
850 if (option_index == OPT_SPECIAL_unknown)
851 {
6624075e
PP
852 auto_diagnostic_group d;
853 if (warning_at (loc, OPT_Wpragmas,
854 "unknown option after %<#pragma GCC diagnostic%> kind"))
855 {
856 option_proposer op;
857 const char *hint = op.suggest_option (option_string + 1);
858 if (hint)
859 inform (loc, "did you mean %<-%s%>?", hint);
860 }
c1822f9c
MLI
861 return;
862 }
863 else if (!(cl_options[option_index].flags & CL_WARNING))
864 {
865 warning_at (loc, OPT_Wpragmas,
866 "%qs is not an option that controls warnings", option_string);
867 return;
868 }
869 else if (!(cl_options[option_index].flags & lang_mask))
870 {
871 char *ok_langs = write_langs (cl_options[option_index].flags);
872 char *bad_lang = write_langs (c_common_option_lang_mask ());
873 warning_at (loc, OPT_Wpragmas,
874 "option %qs is valid for %s but not for %s",
875 option_string, ok_langs, bad_lang);
876 free (ok_langs);
877 free (bad_lang);
878 return;
879 }
880
c4914de6 881 struct cl_option_handlers handlers;
130fcab0 882 set_default_handlers (&handlers, NULL);
63bbf46d
JJ
883 const char *arg = NULL;
884 if (cl_options[option_index].flags & CL_JOINED)
885 arg = option_string + 1 + cl_options[option_index].opt_len;
e1b81f2b
JJ
886 /* FIXME: input_location isn't the best location here, but it is
887 what we used to do here before and changing it breaks e.g.
888 PR69543 and PR69558. */
63bbf46d
JJ
889 control_warning_option (option_index, (int) kind,
890 arg, kind != DK_IGNORED,
e1b81f2b 891 input_location, lang_mask, &handlers,
c1822f9c
MLI
892 &global_options, &global_options_set,
893 global_dc);
79cf5994
DD
894}
895
5779e713 896/* Parse #pragma GCC target (xxx) to set target specific options. */
ab442df7 897static void
5779e713 898handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
ab442df7 899{
8722a170 900 location_t loc;
ab442df7 901 enum cpp_ttype token;
ab442df7
MM
902 tree x;
903 bool close_paren_needed_p = false;
904
905 if (cfun)
906 {
a9c697b8 907 error ("%<#pragma GCC option%> is not allowed inside functions");
ab442df7
MM
908 return;
909 }
910
8722a170 911 token = pragma_lex (&x, &loc);
ab442df7
MM
912 if (token == CPP_OPEN_PAREN)
913 {
914 close_paren_needed_p = true;
8722a170 915 token = pragma_lex (&x, &loc);
ab442df7
MM
916 }
917
5779e713 918 if (token != CPP_STRING)
16507dea 919 GCC_BAD_AT (loc, "%<#pragma GCC option%> is not a string");
ab442df7
MM
920
921 /* Strings are user options. */
922 else
923 {
924 tree args = NULL_TREE;
925
926 do
927 {
928 /* Build up the strings now as a tree linked list. Skip empty
929 strings. */
930 if (TREE_STRING_LENGTH (x) > 0)
931 args = tree_cons (NULL_TREE, x, args);
932
933 token = pragma_lex (&x);
934 while (token == CPP_COMMA)
935 token = pragma_lex (&x);
936 }
937 while (token == CPP_STRING);
938
939 if (close_paren_needed_p)
940 {
941 if (token == CPP_CLOSE_PAREN)
942 token = pragma_lex (&x);
943 else
5779e713 944 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
d8a07487 945 "not have a final %<)%>");
ab442df7
MM
946 }
947
948 if (token != CPP_EOF)
949 {
a9c697b8 950 error ("%<#pragma GCC target%> string is badly formed");
ab442df7
MM
951 return;
952 }
953
954 /* put arguments in the order the user typed them. */
955 args = nreverse (args);
956
5779e713 957 if (targetm.target_option.pragma_parse (args, NULL_TREE))
ec1c5694 958 current_target_pragma = chainon (current_target_pragma, args);
ebd5e86c
ML
959
960 /* A target pragma can also influence optimization options. */
961 tree current_optimize
962 = build_optimization_node (&global_options, &global_options_set);
963 if (current_optimize != optimization_current_node)
964 optimization_current_node = current_optimize;
ab442df7
MM
965 }
966}
967
ab442df7
MM
968/* Handle #pragma GCC optimize to set optimization options. */
969static void
5779e713 970handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
ab442df7
MM
971{
972 enum cpp_ttype token;
ab442df7
MM
973 tree x;
974 bool close_paren_needed_p = false;
975 tree optimization_previous_node = optimization_current_node;
976
977 if (cfun)
978 {
a9c697b8 979 error ("%<#pragma GCC optimize%> is not allowed inside functions");
ab442df7
MM
980 return;
981 }
982
983 token = pragma_lex (&x);
984 if (token == CPP_OPEN_PAREN)
985 {
986 close_paren_needed_p = true;
987 token = pragma_lex (&x);
988 }
989
5779e713 990 if (token != CPP_STRING && token != CPP_NUMBER)
16507dea 991 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
ab442df7
MM
992
993 /* Strings/numbers are user options. */
994 else
995 {
996 tree args = NULL_TREE;
997
998 do
999 {
1000 /* Build up the numbers/strings now as a list. */
1001 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
1002 args = tree_cons (NULL_TREE, x, args);
1003
1004 token = pragma_lex (&x);
1005 while (token == CPP_COMMA)
1006 token = pragma_lex (&x);
1007 }
1008 while (token == CPP_STRING || token == CPP_NUMBER);
1009
1010 if (close_paren_needed_p)
1011 {
1012 if (token == CPP_CLOSE_PAREN)
1013 token = pragma_lex (&x);
1014 else
1015 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
d8a07487 1016 "not have a final %<)%>");
ab442df7
MM
1017 }
1018
1019 if (token != CPP_EOF)
1020 {
a9c697b8 1021 error ("%<#pragma GCC optimize%> string is badly formed");
ab442df7
MM
1022 return;
1023 }
1024
1025 /* put arguments in the order the user typed them. */
1026 args = nreverse (args);
1027
1028 parse_optimize_options (args, false);
5779e713 1029 current_optimize_pragma = chainon (current_optimize_pragma, args);
ba948b37
JJ
1030 optimization_current_node
1031 = build_optimization_node (&global_options, &global_options_set);
ab442df7
MM
1032 c_cpp_builtins_optimize_pragma (parse_in,
1033 optimization_previous_node,
1034 optimization_current_node);
1035 }
1036}
1037
5779e713
MM
1038/* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1039 both the binary representation of the options and the TREE_LIST of
1040 strings that will be added to the function's attribute list. */
a79683d5 1041struct GTY(()) opt_stack {
5779e713
MM
1042 struct opt_stack *prev;
1043 tree target_binary;
1044 tree target_strings;
1045 tree optimize_binary;
1046 tree optimize_strings;
dc6d15ea 1047 gcc_options * GTY ((skip)) saved_global_options;
a79683d5 1048};
5779e713
MM
1049
1050static GTY(()) struct opt_stack * options_stack;
1051
1052/* Handle #pragma GCC push_options to save the current target and optimization
1053 options. */
1054
1055static void
1056handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1057{
1058 enum cpp_ttype token;
1059 tree x = 0;
5779e713
MM
1060
1061 token = pragma_lex (&x);
1062 if (token != CPP_EOF)
1063 {
1064 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1065 return;
1066 }
1067
766090c2 1068 opt_stack *p = ggc_alloc<opt_stack> ();
5779e713
MM
1069 p->prev = options_stack;
1070 options_stack = p;
1071
1072 /* Save optimization and target flags in binary format. */
dc6d15ea
ML
1073 if (flag_checking)
1074 {
1075 p->saved_global_options = XNEW (gcc_options);
1076 *p->saved_global_options = global_options;
1077 }
ba948b37
JJ
1078 p->optimize_binary = build_optimization_node (&global_options,
1079 &global_options_set);
1080 p->target_binary = build_target_option_node (&global_options,
1081 &global_options_set);
5779e713
MM
1082
1083 /* Save optimization and target flags in string list format. */
1084 p->optimize_strings = copy_list (current_optimize_pragma);
1085 p->target_strings = copy_list (current_target_pragma);
1086}
1087
1088/* Handle #pragma GCC pop_options to restore the current target and
1089 optimization options from a previous push_options. */
1090
1091static void
1092handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1093{
1094 enum cpp_ttype token;
1095 tree x = 0;
1096 opt_stack *p;
1097
1098 token = pragma_lex (&x);
1099 if (token != CPP_EOF)
1100 {
1101 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1102 return;
1103 }
1104
1105 if (! options_stack)
1106 {
1107 warning (OPT_Wpragmas,
1108 "%<#pragma GCC pop_options%> without a corresponding "
1109 "%<#pragma GCC push_options%>");
1110 return;
1111 }
1112
1113 p = options_stack;
1114 options_stack = p->prev;
1115
1116 if (p->target_binary != target_option_current_node)
1117 {
1118 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1119 target_option_current_node = p->target_binary;
1120 }
1121
ebd5e86c
ML
1122 /* Always restore optimization options as optimization_current_node is
1123 * overwritten by invoke_set_current_function_hook. */
1124 cl_optimization_restore (&global_options, &global_options_set,
1125 TREE_OPTIMIZATION (p->optimize_binary));
b195d845
ML
1126 cl_target_option_restore (&global_options, &global_options_set,
1127 TREE_TARGET_OPTION (p->target_binary));
ebd5e86c 1128
5779e713
MM
1129 if (p->optimize_binary != optimization_current_node)
1130 {
ebd5e86c 1131 c_cpp_builtins_optimize_pragma (parse_in, optimization_current_node,
5779e713
MM
1132 p->optimize_binary);
1133 optimization_current_node = p->optimize_binary;
1134 }
54e6d3ef 1135 if (flag_checking && !seen_error ())
dc6d15ea
ML
1136 {
1137 cl_optimization_compare (p->saved_global_options, &global_options);
1138 free (p->saved_global_options);
1139 }
5779e713
MM
1140
1141 current_target_pragma = p->target_strings;
1142 current_optimize_pragma = p->optimize_strings;
1143}
1144
1145/* Handle #pragma GCC reset_options to restore the current target and
1146 optimization options to the original options used on the command line. */
1147
1148static void
1149handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1150{
1151 enum cpp_ttype token;
1152 tree x = 0;
1153 tree new_optimize = optimization_default_node;
1154 tree new_target = target_option_default_node;
1155
1156 token = pragma_lex (&x);
1157 if (token != CPP_EOF)
1158 {
1159 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1160 return;
1161 }
1162
1163 if (new_target != target_option_current_node)
1164 {
1165 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1166 target_option_current_node = new_target;
1167 }
1168
1169 if (new_optimize != optimization_current_node)
1170 {
1171 tree old_optimize = optimization_current_node;
ba948b37 1172 cl_optimization_restore (&global_options, &global_options_set,
46625112 1173 TREE_OPTIMIZATION (new_optimize));
5779e713
MM
1174 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1175 optimization_current_node = new_optimize;
1176 }
1177
1178 current_target_pragma = NULL_TREE;
1179 current_optimize_pragma = NULL_TREE;
1180}
1181
0d48657d
SB
1182/* Print a plain user-specified message. */
1183
1184static void
1185handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1186{
8722a170 1187 location_t loc;
0d48657d
SB
1188 enum cpp_ttype token;
1189 tree x, message = 0;
1190
1191 token = pragma_lex (&x);
1192 if (token == CPP_OPEN_PAREN)
1193 {
1194 token = pragma_lex (&x);
1195 if (token == CPP_STRING)
1196 message = x;
1197 else
1198 GCC_BAD ("expected a string after %<#pragma message%>");
1199 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1200 GCC_BAD ("malformed %<#pragma message%>, ignored");
1201 }
1202 else if (token == CPP_STRING)
1203 message = x;
1204 else
1205 GCC_BAD ("expected a string after %<#pragma message%>");
1206
1207 gcc_assert (message);
1208
8722a170
DM
1209 if (pragma_lex (&x, &loc) != CPP_EOF)
1210 warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma message%>");
0d48657d
SB
1211
1212 if (TREE_STRING_LENGTH (message) > 1)
a9c697b8
MS
1213 inform (input_location, "%<#pragma message: %s%>",
1214 TREE_STRING_POINTER (message));
0d48657d
SB
1215}
1216
6ec637a4
JJ
1217/* Mark whether the current location is valid for a STDC pragma. */
1218
1219static bool valid_location_for_stdc_pragma;
1220
1221void
1222mark_valid_location_for_stdc_pragma (bool flag)
1223{
1224 valid_location_for_stdc_pragma = flag;
1225}
1226
1227/* Return true if the current location is valid for a STDC pragma. */
1228
1229bool
1230valid_location_for_stdc_pragma_p (void)
1231{
1232 return valid_location_for_stdc_pragma;
1233}
1234
7de1d221 1235enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
6ec637a4
JJ
1236
1237/* A STDC pragma must appear outside of external declarations or
1238 preceding all explicit declarations and statements inside a compound
1239 statement; its behavior is undefined if used in any other context.
1240 It takes a switch of ON, OFF, or DEFAULT. */
1241
1242static enum pragma_switch_t
1243handle_stdc_pragma (const char *pname)
1244{
1245 const char *arg;
1246 tree t;
1247 enum pragma_switch_t ret;
1248
1249 if (!valid_location_for_stdc_pragma_p ())
1250 {
1251 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1252 pname);
7de1d221 1253 return PRAGMA_BAD;
6ec637a4
JJ
1254 }
1255
1256 if (pragma_lex (&t) != CPP_NAME)
1257 {
1258 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
7de1d221 1259 return PRAGMA_BAD;
6ec637a4
JJ
1260 }
1261
1262 arg = IDENTIFIER_POINTER (t);
1263
1264 if (!strcmp (arg, "ON"))
7de1d221 1265 ret = PRAGMA_ON;
6ec637a4 1266 else if (!strcmp (arg, "OFF"))
7de1d221 1267 ret = PRAGMA_OFF;
6ec637a4 1268 else if (!strcmp (arg, "DEFAULT"))
7de1d221 1269 ret = PRAGMA_DEFAULT;
6ec637a4
JJ
1270 else
1271 {
1272 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
7de1d221 1273 return PRAGMA_BAD;
6ec637a4
JJ
1274 }
1275
1276 if (pragma_lex (&t) != CPP_EOF)
1277 {
1278 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
7de1d221 1279 return PRAGMA_BAD;
6ec637a4
JJ
1280 }
1281
1282 return ret;
1283}
1284
1285/* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1286 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1287 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1288
1289static void
1290handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1291{
1292 if (c_dialect_cxx ())
1293 {
8400e75e 1294 if (warn_unknown_pragmas > in_system_header_at (input_location))
6ec637a4
JJ
1295 warning (OPT_Wunknown_pragmas,
1296 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1297 " for C++");
1298 return;
1299 }
1300
1301 if (!targetm.decimal_float_supported_p ())
1302 {
8400e75e 1303 if (warn_unknown_pragmas > in_system_header_at (input_location))
6ec637a4
JJ
1304 warning (OPT_Wunknown_pragmas,
1305 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1306 " on this target");
1307 return;
1308 }
1309
c1771a20 1310 pedwarn (input_location, OPT_Wpedantic,
6ec637a4
JJ
1311 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1312
1313 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1314 {
7de1d221 1315 case PRAGMA_ON:
6ec637a4
JJ
1316 set_float_const_decimal64 ();
1317 break;
7de1d221
JJ
1318 case PRAGMA_OFF:
1319 case PRAGMA_DEFAULT:
6ec637a4
JJ
1320 clear_float_const_decimal64 ();
1321 break;
7de1d221 1322 case PRAGMA_BAD:
6ec637a4
JJ
1323 break;
1324 }
1325}
1326
dfb43cd5 1327/* A vector of registered pragma callbacks, which is never freed. */
bc4071dd 1328
9771b263 1329static vec<internal_pragma_handler> registered_pragmas;
bc4071dd 1330
a79683d5 1331struct pragma_ns_name
a25a8f3b
JJ
1332{
1333 const char *space;
1334 const char *name;
a79683d5 1335};
a25a8f3b 1336
a25a8f3b 1337
9771b263 1338static vec<pragma_ns_name> registered_pp_pragmas;
a25a8f3b
JJ
1339
1340struct omp_pragma_def { const char *name; unsigned int id; };
41dbbb37 1341static const struct omp_pragma_def oacc_pragmas[] = {
4bf9e5a8 1342 { "atomic", PRAGMA_OACC_ATOMIC },
41dbbb37
TS
1343 { "cache", PRAGMA_OACC_CACHE },
1344 { "data", PRAGMA_OACC_DATA },
6e232ba4 1345 { "declare", PRAGMA_OACC_DECLARE },
41dbbb37
TS
1346 { "enter", PRAGMA_OACC_ENTER_DATA },
1347 { "exit", PRAGMA_OACC_EXIT_DATA },
37d5ad46 1348 { "host_data", PRAGMA_OACC_HOST_DATA },
41dbbb37
TS
1349 { "kernels", PRAGMA_OACC_KERNELS },
1350 { "loop", PRAGMA_OACC_LOOP },
1351 { "parallel", PRAGMA_OACC_PARALLEL },
3a40d81d 1352 { "routine", PRAGMA_OACC_ROUTINE },
62aee289 1353 { "serial", PRAGMA_OACC_SERIAL },
41dbbb37
TS
1354 { "update", PRAGMA_OACC_UPDATE },
1355 { "wait", PRAGMA_OACC_WAIT }
1356};
a25a8f3b 1357static const struct omp_pragma_def omp_pragmas[] = {
aa043200 1358 { "allocate", PRAGMA_OMP_ALLOCATE },
a25a8f3b
JJ
1359 { "atomic", PRAGMA_OMP_ATOMIC },
1360 { "barrier", PRAGMA_OMP_BARRIER },
acf0174b
JJ
1361 { "cancel", PRAGMA_OMP_CANCEL },
1362 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
a25a8f3b 1363 { "critical", PRAGMA_OMP_CRITICAL },
28567c40 1364 { "depobj", PRAGMA_OMP_DEPOBJ },
0d973c0a 1365 { "error", PRAGMA_OMP_ERROR },
acf0174b 1366 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
a25a8f3b 1367 { "flush", PRAGMA_OMP_FLUSH },
5079b778 1368 { "nothing", PRAGMA_OMP_NOTHING },
28567c40 1369 { "requires", PRAGMA_OMP_REQUIRES },
e45483c7 1370 { "scope", PRAGMA_OMP_SCOPE },
a25a8f3b
JJ
1371 { "section", PRAGMA_OMP_SECTION },
1372 { "sections", PRAGMA_OMP_SECTIONS },
1373 { "single", PRAGMA_OMP_SINGLE },
92d28cbb 1374 { "task", PRAGMA_OMP_TASK },
acf0174b 1375 { "taskgroup", PRAGMA_OMP_TASKGROUP },
a68ab351 1376 { "taskwait", PRAGMA_OMP_TASKWAIT },
20906c66 1377 { "taskyield", PRAGMA_OMP_TASKYIELD },
a25a8f3b
JJ
1378 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1379};
6d7f7e0a 1380static const struct omp_pragma_def omp_pragmas_simd[] = {
f9d8d994 1381 { "declare", PRAGMA_OMP_DECLARE },
6d7f7e0a
TB
1382 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1383 { "for", PRAGMA_OMP_FOR },
554a530f 1384 { "loop", PRAGMA_OMP_LOOP },
d0befed7 1385 { "masked", PRAGMA_OMP_MASKED },
31007091 1386 { "master", PRAGMA_OMP_MASTER },
d2e05fcb 1387 { "ordered", PRAGMA_OMP_ORDERED },
6d7f7e0a 1388 { "parallel", PRAGMA_OMP_PARALLEL },
bf38f7e9 1389 { "scan", PRAGMA_OMP_SCAN },
6d7f7e0a
TB
1390 { "simd", PRAGMA_OMP_SIMD },
1391 { "target", PRAGMA_OMP_TARGET },
d9a6bd32 1392 { "taskloop", PRAGMA_OMP_TASKLOOP },
6d7f7e0a
TB
1393 { "teams", PRAGMA_OMP_TEAMS },
1394};
a25a8f3b
JJ
1395
1396void
1397c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1398{
41dbbb37 1399 const int n_oacc_pragmas = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
a25a8f3b 1400 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
6d7f7e0a
TB
1401 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1402 / sizeof (*omp_pragmas);
a25a8f3b
JJ
1403 int i;
1404
41dbbb37
TS
1405 for (i = 0; i < n_oacc_pragmas; ++i)
1406 if (oacc_pragmas[i].id == id)
1407 {
1408 *space = "acc";
1409 *name = oacc_pragmas[i].name;
1410 return;
1411 }
1412
a25a8f3b
JJ
1413 for (i = 0; i < n_omp_pragmas; ++i)
1414 if (omp_pragmas[i].id == id)
1415 {
1416 *space = "omp";
1417 *name = omp_pragmas[i].name;
1418 return;
1419 }
1420
6d7f7e0a
TB
1421 for (i = 0; i < n_omp_pragmas_simd; ++i)
1422 if (omp_pragmas_simd[i].id == id)
1423 {
1424 *space = "omp";
1425 *name = omp_pragmas_simd[i].name;
1426 return;
1427 }
1428
a25a8f3b 1429 if (id >= PRAGMA_FIRST_EXTERNAL
9771b263 1430 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
a25a8f3b 1431 {
9771b263
DN
1432 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1433 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
a25a8f3b
JJ
1434 return;
1435 }
1436
1437 gcc_unreachable ();
1438}
1439
b5b3e36a 1440/* Front-end wrappers for pragma registration to avoid dragging
c58b209a 1441 cpplib.h in almost everywhere. */
bc4071dd
RH
1442
1443static void
1444c_register_pragma_1 (const char *space, const char *name,
dfb43cd5 1445 internal_pragma_handler ihandler, bool allow_expansion)
bc4071dd
RH
1446{
1447 unsigned id;
1448
a25a8f3b
JJ
1449 if (flag_preprocess_only)
1450 {
1451 pragma_ns_name ns_name;
bc4071dd 1452
a25a8f3b
JJ
1453 if (!allow_expansion)
1454 return;
1455
1456 ns_name.space = space;
1457 ns_name.name = name;
9771b263
DN
1458 registered_pp_pragmas.safe_push (ns_name);
1459 id = registered_pp_pragmas.length ();
a25a8f3b
JJ
1460 id += PRAGMA_FIRST_EXTERNAL - 1;
1461 }
1462 else
1463 {
9771b263
DN
1464 registered_pragmas.safe_push (ihandler);
1465 id = registered_pragmas.length ();
a25a8f3b
JJ
1466 id += PRAGMA_FIRST_EXTERNAL - 1;
1467
e0a575ff
JJ
1468 /* The C front end allocates 8 bits in c_token. The C++ front end
1469 keeps the pragma kind in the form of INTEGER_CST, so no small
1470 limit applies. At present this is sufficient. */
4ac93c7c 1471 gcc_assert (id < 256);
a25a8f3b 1472 }
bc4071dd
RH
1473
1474 cpp_register_deferred_pragma (parse_in, space, name, id,
1475 allow_expansion, false);
1476}
1477
dfb43cd5
PV
1478/* Register a C pragma handler, using a space and a name. It disallows pragma
1479 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1480void
1481c_register_pragma (const char *space, const char *name,
1482 pragma_handler_1arg handler)
1483{
1484 internal_pragma_handler ihandler;
1485
1486 ihandler.handler.handler_1arg = handler;
1487 ihandler.extra_data = false;
1488 ihandler.data = NULL;
1489 c_register_pragma_1 (space, name, ihandler, false);
1490}
1491
1492/* Register a C pragma handler, using a space and a name, it also carries an
1493 extra data field which can be used by the handler. It disallows pragma
1494 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1495 instead). */
c58b209a 1496void
dfb43cd5
PV
1497c_register_pragma_with_data (const char *space, const char *name,
1498 pragma_handler_2arg handler, void * data)
c58b209a 1499{
dfb43cd5
PV
1500 internal_pragma_handler ihandler;
1501
1502 ihandler.handler.handler_2arg = handler;
1503 ihandler.extra_data = true;
1504 ihandler.data = data;
1505 c_register_pragma_1 (space, name, ihandler, false);
b5b3e36a
DJ
1506}
1507
dfb43cd5
PV
1508/* Register a C pragma handler, using a space and a name. It allows pragma
1509 expansion as in the following example:
1510
1511 #define NUMBER 10
1512 #pragma count (NUMBER)
1513
1514 Name expansion is still disallowed. */
b5b3e36a
DJ
1515void
1516c_register_pragma_with_expansion (const char *space, const char *name,
dfb43cd5 1517 pragma_handler_1arg handler)
bc4071dd 1518{
dfb43cd5
PV
1519 internal_pragma_handler ihandler;
1520
1521 ihandler.handler.handler_1arg = handler;
1522 ihandler.extra_data = false;
1523 ihandler.data = NULL;
1524 c_register_pragma_1 (space, name, ihandler, true);
1525}
1526
1527/* Register a C pragma handler, using a space and a name, it also carries an
1528 extra data field which can be used by the handler. It allows pragma
1529 expansion as in the following example:
1530
1531 #define NUMBER 10
1532 #pragma count (NUMBER)
1533
1534 Name expansion is still disallowed. */
1535void
1536c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1537 pragma_handler_2arg handler,
1538 void *data)
1539{
1540 internal_pragma_handler ihandler;
1541
1542 ihandler.handler.handler_2arg = handler;
1543 ihandler.extra_data = true;
1544 ihandler.data = data;
1545 c_register_pragma_1 (space, name, ihandler, true);
bc4071dd
RH
1546}
1547
1548void
1549c_invoke_pragma_handler (unsigned int id)
b5b3e36a 1550{
dfb43cd5
PV
1551 internal_pragma_handler *ihandler;
1552 pragma_handler_1arg handler_1arg;
1553 pragma_handler_2arg handler_2arg;
bc4071dd
RH
1554
1555 id -= PRAGMA_FIRST_EXTERNAL;
9771b263 1556 ihandler = &registered_pragmas[id];
dfb43cd5
PV
1557 if (ihandler->extra_data)
1558 {
1559 handler_2arg = ihandler->handler.handler_2arg;
1560 handler_2arg (parse_in, ihandler->data);
1561 }
1562 else
1563 {
1564 handler_1arg = ihandler->handler.handler_1arg;
1565 handler_1arg (parse_in);
1566 }
c58b209a
NB
1567}
1568
1569/* Set up front-end pragmas. */
568767a6 1570void
35b1a6fa 1571init_pragma (void)
568767a6 1572{
41dbbb37
TS
1573 if (flag_openacc)
1574 {
1575 const int n_oacc_pragmas
1576 = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1577 int i;
1578
1579 for (i = 0; i < n_oacc_pragmas; ++i)
1580 cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1581 oacc_pragmas[i].id, true, true);
1582 }
1583
a25a8f3b 1584 if (flag_openmp)
953ff289 1585 {
953ff289
DN
1586 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1587 int i;
1588
1589 for (i = 0; i < n_omp_pragmas; ++i)
1590 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1591 omp_pragmas[i].id, true, true);
1592 }
6d7f7e0a
TB
1593 if (flag_openmp || flag_openmp_simd)
1594 {
1595 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1596 / sizeof (*omp_pragmas);
1597 int i;
1598
1599 for (i = 0; i < n_omp_pragmas_simd; ++i)
1600 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1601 omp_pragmas_simd[i].id, true, true);
1602 }
953ff289 1603
a25a8f3b
JJ
1604 if (!flag_preprocess_only)
1605 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1606 PRAGMA_GCC_PCH_PREPROCESS, false, false);
bc4071dd 1607
28e41874
JJ
1608 if (!flag_preprocess_only)
1609 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1610 false);
9a771876 1611
170a8bd6
EB
1612 if (!flag_preprocess_only)
1613 cpp_register_deferred_pragma (parse_in, "GCC", "unroll", PRAGMA_UNROLL,
1614 false, false);
1615
b5b3e36a
DJ
1616#ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1617 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1618#else
c58b209a 1619 c_register_pragma (0, "pack", handle_pragma_pack);
0e5921e8 1620#endif
c58b209a 1621 c_register_pragma (0, "weak", handle_pragma_weak);
ee45a32d 1622
d7afec4b 1623 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
84b8b0e0 1624
79cf5994 1625 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
5779e713 1626 c_register_pragma ("GCC", "target", handle_pragma_target);
ab442df7 1627 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
5779e713
MM
1628 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1629 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1630 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
79cf5994 1631
6ec637a4
JJ
1632 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1633 handle_pragma_float_const_decimal64);
1634
dfb43cd5
PV
1635 c_register_pragma_with_expansion (0, "redefine_extname",
1636 handle_pragma_redefine_extname);
41c64394 1637
0d48657d
SB
1638 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1639
8b97c5f8 1640#ifdef REGISTER_TARGET_PRAGMAS
c58b209a 1641 REGISTER_TARGET_PRAGMAS ();
8b97c5f8 1642#endif
7ac8318c 1643
ee45a32d
EB
1644 global_sso = default_sso;
1645 c_register_pragma (0, "scalar_storage_order",
1646 handle_pragma_scalar_storage_order);
1647
7ac8318c
BS
1648 /* Allow plugins to register their own pragmas. */
1649 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
568767a6 1650}
e2500fed 1651
39dabefd 1652#include "gt-c-family-c-pragma.h"