attribs: Implement -Wno-attributes=vendor::attr [PR101940]
[official-gcc.git] / gcc / c-family / c-pragma.c
blob3663eb1cfbbe94acf4b7433b9ef333de782b1f6e
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h" /* For cfun. */
25 #include "c-common.h"
26 #include "memmodel.h"
27 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS. */
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "diagnostic.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "c-pragma.h"
34 #include "opts.h"
35 #include "plugin.h"
36 #include "opt-suggestions.h"
38 #define GCC_BAD(gmsgid) \
39 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
40 #define GCC_BAD2(gmsgid, arg) \
41 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
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)
47 struct GTY(()) align_stack {
48 int alignment;
49 tree id;
50 struct align_stack * prev;
53 static GTY(()) struct align_stack * alignment_stack;
55 static void handle_pragma_pack (cpp_reader *);
57 /* If we have a "global" #pragma pack(<n>) in effect when the first
58 #pragma pack(push,<n>) is encountered, this stores the value of
59 maximum_field_alignment in effect. When the final pop_alignment()
60 happens, we restore the value to this, not to a value of 0 for
61 maximum_field_alignment. Value is in bits. */
62 static int default_alignment;
63 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
64 ? &default_alignment \
65 : &alignment_stack->alignment) = (ALIGN))
67 static void push_alignment (int, tree);
68 static void pop_alignment (tree);
70 /* Push an alignment value onto the stack. */
71 static void
72 push_alignment (int alignment, tree id)
74 align_stack * entry = ggc_alloc<align_stack> ();
76 entry->alignment = alignment;
77 entry->id = id;
78 entry->prev = alignment_stack;
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
82 so that we can restore it after the final #pragma pop(). */
83 if (alignment_stack == NULL)
84 default_alignment = maximum_field_alignment;
86 alignment_stack = entry;
88 maximum_field_alignment = alignment;
91 /* Undo a push of an alignment onto the stack. */
92 static void
93 pop_alignment (tree id)
95 align_stack * entry;
97 if (alignment_stack == NULL)
98 GCC_BAD ("%<#pragma pack (pop)%> encountered without matching "
99 "%<#pragma pack (push)%>");
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)
105 for (entry = alignment_stack; entry; entry = entry->prev)
106 if (entry->id == id)
108 alignment_stack = entry;
109 break;
111 if (entry == NULL)
112 warning (OPT_Wpragmas,
113 "%<#pragma pack(pop, %E)%> encountered without matching "
114 "%<#pragma pack(push, %E)%>"
115 , id, id);
118 entry = alignment_stack->prev;
120 maximum_field_alignment = entry ? entry->alignment : default_alignment;
122 alignment_stack = entry;
125 /* #pragma pack ()
126 #pragma pack (N)
128 #pragma pack (push)
129 #pragma pack (push, N)
130 #pragma pack (push, ID)
131 #pragma pack (push, ID, N)
132 #pragma pack (pop)
133 #pragma pack (pop, ID) */
134 static void
135 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
137 location_t loc;
138 tree x, id = 0;
139 int align = -1;
140 enum cpp_ttype token;
141 enum { set, push, pop } action;
143 if (pragma_lex (&x) != CPP_OPEN_PAREN)
144 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
146 token = pragma_lex (&x, &loc);
147 if (token == CPP_CLOSE_PAREN)
149 action = set;
150 align = initial_max_fld_align;
152 else if (token == CPP_NUMBER)
154 if (TREE_CODE (x) != INTEGER_CST)
155 GCC_BAD_AT (loc, "invalid constant in %<#pragma pack%> - ignored");
156 align = TREE_INT_CST_LOW (x);
157 action = set;
158 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
159 GCC_BAD ("malformed %<#pragma pack%> - ignored");
161 else if (token == CPP_NAME)
163 #define GCC_BAD_ACTION do { if (action != pop) \
164 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
165 else \
166 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
167 } while (0)
169 const char *op = IDENTIFIER_POINTER (x);
170 if (!strcmp (op, "push"))
171 action = push;
172 else if (!strcmp (op, "pop"))
173 action = pop;
174 else
175 GCC_BAD2_AT (loc, "unknown action %qE for %<#pragma pack%> - ignored",
178 while ((token = pragma_lex (&x)) == CPP_COMMA)
180 token = pragma_lex (&x, &loc);
181 if (token == CPP_NAME && id == 0)
183 id = x;
185 else if (token == CPP_NUMBER && action == push && align == -1)
187 if (TREE_CODE (x) != INTEGER_CST)
188 GCC_BAD_AT (loc,
189 "invalid constant in %<#pragma pack%> - ignored");
190 align = TREE_INT_CST_LOW (x);
191 if (align == -1)
192 action = set;
194 else
195 GCC_BAD_ACTION;
198 if (token != CPP_CLOSE_PAREN)
199 GCC_BAD_ACTION;
200 #undef GCC_BAD_ACTION
202 else
203 GCC_BAD ("malformed %<#pragma pack%> - ignored");
205 if (pragma_lex (&x, &loc) != CPP_EOF)
206 warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma pack%>");
208 if (flag_pack_struct)
209 GCC_BAD ("%<#pragma pack%> has no effect with %<-fpack-struct%> - ignored");
211 if (action != pop)
212 switch (align)
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;
222 case -1:
223 if (action == push)
225 align = maximum_field_alignment;
226 break;
228 /* FALLTHRU */
229 default:
230 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
233 switch (action)
235 case set: SET_GLOBAL_ALIGNMENT (align); break;
236 case push: push_alignment (align, id); break;
237 case pop: pop_alignment (id); break;
241 struct GTY(()) pending_weak
243 tree name;
244 tree value;
248 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
250 static void apply_pragma_weak (tree, tree);
251 static void handle_pragma_weak (cpp_reader *);
253 static void
254 apply_pragma_weak (tree decl, tree value)
256 if (value)
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)),
265 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
266 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
267 && DECL_ASSEMBLER_NAME_SET_P (decl)
268 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
269 warning (OPT_Wpragmas, "applying %<#pragma weak %+D%> after first use "
270 "results in unspecified behavior", decl);
272 declare_weak (decl);
275 void
276 maybe_apply_pragma_weak (tree decl)
278 tree id;
279 int i;
280 pending_weak *pe;
282 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
284 /* No weak symbols pending, take the short-cut. */
285 if (vec_safe_is_empty (pending_weaks))
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))
290 return;
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
293 aren't functions or variables? Should this be an assert instead? */
294 if (!VAR_OR_FUNCTION_DECL_P (decl))
295 return;
297 if (DECL_ASSEMBLER_NAME_SET_P (decl))
298 id = DECL_ASSEMBLER_NAME (decl);
299 else
301 id = DECL_ASSEMBLER_NAME (decl);
302 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
305 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
306 if (id == pe->name)
308 apply_pragma_weak (decl, pe->value);
309 pending_weaks->unordered_remove (i);
310 break;
314 /* Process all "#pragma weak A = B" directives where we have not seen
315 a decl for A. */
316 void
317 maybe_apply_pending_pragma_weaks (void)
319 tree alias_id, id, decl;
320 int i;
321 pending_weak *pe;
322 symtab_node *target;
324 if (vec_safe_is_empty (pending_weaks))
325 return;
327 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
329 alias_id = pe->name;
330 id = pe->value;
332 if (id == NULL)
333 continue;
335 target = symtab_node::get_for_asmname (id);
336 decl = build_decl (UNKNOWN_LOCATION,
337 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
338 alias_id, default_function_type);
340 DECL_ARTIFICIAL (decl) = 1;
341 TREE_PUBLIC (decl) = 1;
342 DECL_WEAK (decl) = 1;
343 if (VAR_P (decl))
344 TREE_STATIC (decl) = 1;
345 if (!target)
347 error ("%q+D aliased to undefined symbol %qE",
348 decl, id);
349 continue;
352 assemble_alias (decl, id);
356 /* #pragma weak name [= value] */
357 static void
358 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
360 tree name, value, x, decl;
361 enum cpp_ttype t;
363 value = 0;
365 if (pragma_lex (&name) != CPP_NAME)
366 GCC_BAD ("malformed %<#pragma weak%>, ignored");
367 t = pragma_lex (&x);
368 if (t == CPP_EQ)
370 if (pragma_lex (&value) != CPP_NAME)
371 GCC_BAD ("malformed %<#pragma weak%>, ignored");
372 t = pragma_lex (&x);
374 if (t != CPP_EOF)
375 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
377 decl = identifier_global_value (name);
378 if (decl && DECL_P (decl))
380 if (!VAR_OR_FUNCTION_DECL_P (decl))
381 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
382 " ignored", decl);
383 apply_pragma_weak (decl, value);
384 if (value)
386 DECL_EXTERNAL (decl) = 0;
387 if (VAR_P (decl))
388 TREE_STATIC (decl) = 1;
389 assemble_alias (decl, value);
392 else
394 pending_weak pe = {name, value};
395 vec_safe_push (pending_weaks, pe);
399 static enum scalar_storage_order_kind global_sso;
401 void
402 maybe_apply_pragma_scalar_storage_order (tree type)
404 if (global_sso == SSO_NATIVE)
405 return;
407 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
409 if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type)))
410 return;
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 ();
420 static void
421 handle_pragma_scalar_storage_order (cpp_reader *ARG_UNUSED(dummy))
423 const char *kind_string;
424 enum cpp_ttype token;
425 tree x;
427 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
429 error ("%<scalar_storage_order%> is not supported because endianness "
430 "is not uniform");
431 return;
434 if (c_dialect_cxx ())
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;
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%>");
456 /* GCC supports two #pragma directives for renaming the external
457 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
458 compatibility with the Solaris and VMS system headers. GCC also
459 has its own notation for this, __asm__("name") annotations.
461 Corner cases of these features and their interaction:
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.
467 2) In C++, both #pragmas silently apply only to extern "C" declarations.
468 Asm labels do not have this restriction.
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.
474 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
475 *not* the DECL_ASSEMBLER_NAME.
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.
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.) */
488 struct GTY(()) pending_redefinition {
489 tree oldname;
490 tree newname;
494 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
496 static void handle_pragma_redefine_extname (cpp_reader *);
498 /* #pragma redefine_extname oldname newname */
499 static void
500 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
502 tree oldname, newname, decls, x;
503 enum cpp_ttype t;
504 bool found;
506 if (pragma_lex (&oldname) != CPP_NAME)
507 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
508 if (pragma_lex (&newname) != CPP_NAME)
509 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
510 t = pragma_lex (&x);
511 if (t != CPP_EOF)
512 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
514 found = false;
515 for (decls = c_linkage_bindings (oldname);
516 decls; )
518 tree decl;
519 if (TREE_CODE (decls) == TREE_LIST)
521 decl = TREE_VALUE (decls);
522 decls = TREE_CHAIN (decls);
524 else
526 decl = decls;
527 decls = NULL_TREE;
530 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
531 && VAR_OR_FUNCTION_DECL_P (decl))
533 found = true;
534 if (DECL_ASSEMBLER_NAME_SET_P (decl))
536 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
537 name = targetm.strip_name_encoding (name);
539 if (!id_equal (newname, name))
540 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> "
541 "ignored due to conflict with previous rename");
543 else
544 symtab->change_decl_assembler_name (decl, newname);
548 if (!found)
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);
557 /* This is called from here and from ia64-c.c. */
558 void
559 add_to_renaming_pragma_list (tree oldname, tree newname)
561 unsigned ix;
562 pending_redefinition *p;
564 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
565 if (oldname == p->oldname)
567 if (p->newname != newname)
568 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored due to "
569 "conflict with previous %<#pragma redefine_extname%>");
570 return;
573 pending_redefinition e = {oldname, newname};
574 vec_safe_push (pending_redefine_extname, e);
577 /* The current prefix set by #pragma extern_prefix. */
578 GTY(()) tree pragma_extern_prefix;
580 /* Hook from the front ends to apply the results of one of the preceding
581 pragmas that rename variables. */
583 tree
584 maybe_apply_renaming_pragma (tree decl, tree asmname)
586 unsigned ix;
587 pending_redefinition *p;
589 /* The renaming pragmas are only applied to declarations with
590 external linkage. */
591 if (!VAR_OR_FUNCTION_DECL_P (decl)
592 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
593 || !has_c_linkage (decl))
594 return asmname;
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))
600 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
601 oldname = targetm.strip_name_encoding (oldname);
603 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
604 warning (OPT_Wpragmas, "%<asm%> declaration ignored due to "
605 "conflict with previous rename");
607 /* Take any pending redefine_extname off the list. */
608 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
609 if (DECL_NAME (decl) == p->oldname)
611 /* Only warn if there is a conflict. */
612 if (!id_equal (p->newname, oldname))
613 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
614 "due to conflict with previous rename");
616 pending_redefine_extname->unordered_remove (ix);
617 break;
619 return NULL_TREE;
622 /* Find out if we have a pending #pragma redefine_extname. */
623 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
624 if (DECL_NAME (decl) == p->oldname)
626 tree newname = p->newname;
627 pending_redefine_extname->unordered_remove (ix);
629 /* If we already have an asmname, #pragma redefine_extname is
630 ignored (with a warning if it conflicts). */
631 if (asmname)
633 if (strcmp (TREE_STRING_POINTER (asmname),
634 IDENTIFIER_POINTER (newname)) != 0)
635 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
636 "due to conflict with %<asm%> declaration");
637 return asmname;
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));
646 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
647 if (asmname)
648 return asmname;
650 /* If #pragma extern_prefix is in effect, apply it. */
651 if (pragma_extern_prefix)
653 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
654 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
656 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
657 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
659 char *newname = (char *) alloca (plen + ilen + 1);
661 memcpy (newname, prefix, plen);
662 memcpy (newname + plen, id, ilen + 1);
664 return build_string (plen + ilen, newname);
667 /* Nada. */
668 return NULL_TREE;
672 static void handle_pragma_visibility (cpp_reader *);
674 static vec<int> visstack;
676 /* Push the visibility indicated by STR onto the top of the #pragma
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. */
683 void
684 push_visibility (const char *str, int kind)
686 visstack.safe_push (((int) default_visibility) | (kind << 8));
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"))
692 default_visibility = VISIBILITY_HIDDEN;
693 else if (!strcmp (str, "protected"))
694 default_visibility = VISIBILITY_PROTECTED;
695 else
696 GCC_BAD ("%<#pragma GCC visibility push()%> must specify %<default%>, "
697 "%<internal%>, %<hidden%> or %<protected%>");
698 visibility_options.inpragma = 1;
701 /* Pop a level of the #pragma visibility stack. Return true if
702 successful. */
704 bool
705 pop_visibility (int kind)
707 if (!visstack.length ())
708 return false;
709 if ((visstack.last () >> 8) != kind)
710 return false;
711 default_visibility
712 = (enum symbol_visibility) (visstack.pop () & 0xff);
713 visibility_options.inpragma
714 = visstack.length () != 0;
715 return true;
718 /* Sets the default visibility for symbols to something other than that
719 specified on the command line. */
721 static void
722 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
724 /* Form is #pragma GCC visibility push(hidden)|pop */
725 tree x;
726 enum cpp_ttype token;
727 enum { bad, push, pop } action = bad;
729 token = pragma_lex (&x);
730 if (token == CPP_NAME)
732 const char *op = IDENTIFIER_POINTER (x);
733 if (!strcmp (op, "push"))
734 action = push;
735 else if (!strcmp (op, "pop"))
736 action = pop;
738 if (bad == action)
739 GCC_BAD ("%<#pragma GCC visibility%> must be followed by %<push%> "
740 "or %<pop%>");
741 else
743 if (pop == action)
745 if (! pop_visibility (0))
746 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
748 else
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)
754 GCC_BAD ("malformed %<#pragma GCC visibility push%>");
755 else
756 push_visibility (IDENTIFIER_POINTER (x), 0);
757 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
758 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
761 if (pragma_lex (&x) != CPP_EOF)
762 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
765 static void
766 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
768 tree x;
769 location_t loc;
770 enum cpp_ttype token = pragma_lex (&x, &loc);
771 if (token != CPP_NAME)
773 warning_at (loc, OPT_Wpragmas,
774 "missing [error|warning|ignored|push|pop|ignored_attributes]"
775 " after %<#pragma GCC diagnostic%>");
776 return;
779 diagnostic_t kind;
780 const char *kind_string = IDENTIFIER_POINTER (x);
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;
787 else if (strcmp (kind_string, "push") == 0)
789 diagnostic_push_diagnostics (global_dc, input_location);
790 return;
792 else if (strcmp (kind_string, "pop") == 0)
794 diagnostic_pop_diagnostics (global_dc, input_location);
795 return;
797 else if (strcmp (kind_string, "ignored_attributes") == 0)
799 token = pragma_lex (&x, &loc);
800 if (token != CPP_STRING)
802 warning_at (loc, OPT_Wpragmas,
803 "missing attribute name after %<#pragma GCC diagnostic "
804 "ignored_attributes%>");
805 return;
807 char *args = xstrdup (TREE_STRING_POINTER (x));
808 const size_t l = strlen (args);
809 if (l == 0)
811 warning_at (loc, OPT_Wpragmas, "missing argument to %<#pragma GCC "
812 "diagnostic ignored_attributes%>");
813 free (args);
814 return;
816 else if (args[l - 1] == ',')
818 warning_at (loc, OPT_Wpragmas, "trailing %<,%> in arguments for "
819 "%<#pragma GCC diagnostic ignored_attributes%>");
820 free (args);
821 return;
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;
830 else
832 warning_at (loc, OPT_Wpragmas,
833 "expected [error|warning|ignored|push|pop|ignored_attributes]"
834 " after %<#pragma GCC diagnostic%>");
835 return;
838 token = pragma_lex (&x, &loc);
839 if (token != CPP_STRING)
841 warning_at (loc, OPT_Wpragmas,
842 "missing option after %<#pragma GCC diagnostic%> kind");
843 return;
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)
852 auto_diagnostic_group d;
853 if (warning_at (loc, OPT_Wpragmas,
854 "unknown option after %<#pragma GCC diagnostic%> kind"))
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);
861 return;
863 else if (!(cl_options[option_index].flags & CL_WARNING))
865 warning_at (loc, OPT_Wpragmas,
866 "%qs is not an option that controls warnings", option_string);
867 return;
869 else if (!(cl_options[option_index].flags & lang_mask))
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;
881 struct cl_option_handlers handlers;
882 set_default_handlers (&handlers, NULL);
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;
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. */
889 control_warning_option (option_index, (int) kind,
890 arg, kind != DK_IGNORED,
891 input_location, lang_mask, &handlers,
892 &global_options, &global_options_set,
893 global_dc);
896 /* Parse #pragma GCC target (xxx) to set target specific options. */
897 static void
898 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
900 location_t loc;
901 enum cpp_ttype token;
902 tree x;
903 bool close_paren_needed_p = false;
905 if (cfun)
907 error ("%<#pragma GCC option%> is not allowed inside functions");
908 return;
911 token = pragma_lex (&x, &loc);
912 if (token == CPP_OPEN_PAREN)
914 close_paren_needed_p = true;
915 token = pragma_lex (&x, &loc);
918 if (token != CPP_STRING)
920 GCC_BAD_AT (loc, "%<#pragma GCC option%> is not a string");
921 return;
924 /* Strings are user options. */
925 else
927 tree args = NULL_TREE;
931 /* Build up the strings now as a tree linked list. Skip empty
932 strings. */
933 if (TREE_STRING_LENGTH (x) > 0)
934 args = tree_cons (NULL_TREE, x, args);
936 token = pragma_lex (&x);
937 while (token == CPP_COMMA)
938 token = pragma_lex (&x);
940 while (token == CPP_STRING);
942 if (close_paren_needed_p)
944 if (token == CPP_CLOSE_PAREN)
945 token = pragma_lex (&x);
946 else
947 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
948 "not have a final %<)%>");
951 if (token != CPP_EOF)
953 error ("%<#pragma GCC target%> string is badly formed");
954 return;
957 /* put arguments in the order the user typed them. */
958 args = nreverse (args);
960 if (targetm.target_option.pragma_parse (args, NULL_TREE))
961 current_target_pragma = chainon (current_target_pragma, args);
963 /* A target pragma can also influence optimization options. */
964 tree current_optimize
965 = build_optimization_node (&global_options, &global_options_set);
966 if (current_optimize != optimization_current_node)
967 optimization_current_node = current_optimize;
971 /* Handle #pragma GCC optimize to set optimization options. */
972 static void
973 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
975 enum cpp_ttype token;
976 tree x;
977 bool close_paren_needed_p = false;
978 tree optimization_previous_node = optimization_current_node;
980 if (cfun)
982 error ("%<#pragma GCC optimize%> is not allowed inside functions");
983 return;
986 token = pragma_lex (&x);
987 if (token == CPP_OPEN_PAREN)
989 close_paren_needed_p = true;
990 token = pragma_lex (&x);
993 if (token != CPP_STRING && token != CPP_NUMBER)
995 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
996 return;
999 /* Strings/numbers are user options. */
1000 else
1002 tree args = NULL_TREE;
1006 /* Build up the numbers/strings now as a list. */
1007 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
1008 args = tree_cons (NULL_TREE, x, args);
1010 token = pragma_lex (&x);
1011 while (token == CPP_COMMA)
1012 token = pragma_lex (&x);
1014 while (token == CPP_STRING || token == CPP_NUMBER);
1016 if (close_paren_needed_p)
1018 if (token == CPP_CLOSE_PAREN)
1019 token = pragma_lex (&x);
1020 else
1021 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
1022 "not have a final %<)%>");
1025 if (token != CPP_EOF)
1027 error ("%<#pragma GCC optimize%> string is badly formed");
1028 return;
1031 /* put arguments in the order the user typed them. */
1032 args = nreverse (args);
1034 parse_optimize_options (args, false);
1035 current_optimize_pragma = chainon (current_optimize_pragma, args);
1036 optimization_current_node
1037 = build_optimization_node (&global_options, &global_options_set);
1038 c_cpp_builtins_optimize_pragma (parse_in,
1039 optimization_previous_node,
1040 optimization_current_node);
1044 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1045 both the binary representation of the options and the TREE_LIST of
1046 strings that will be added to the function's attribute list. */
1047 struct GTY(()) opt_stack {
1048 struct opt_stack *prev;
1049 tree target_binary;
1050 tree target_strings;
1051 tree optimize_binary;
1052 tree optimize_strings;
1053 gcc_options * GTY ((skip)) saved_global_options;
1056 static GTY(()) struct opt_stack * options_stack;
1058 /* Handle #pragma GCC push_options to save the current target and optimization
1059 options. */
1061 static void
1062 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1064 enum cpp_ttype token;
1065 tree x = 0;
1067 token = pragma_lex (&x);
1068 if (token != CPP_EOF)
1070 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1071 return;
1074 opt_stack *p = ggc_alloc<opt_stack> ();
1075 p->prev = options_stack;
1076 options_stack = p;
1078 /* Save optimization and target flags in binary format. */
1079 if (flag_checking)
1081 p->saved_global_options = XNEW (gcc_options);
1082 *p->saved_global_options = global_options;
1084 p->optimize_binary = build_optimization_node (&global_options,
1085 &global_options_set);
1086 p->target_binary = build_target_option_node (&global_options,
1087 &global_options_set);
1089 /* Save optimization and target flags in string list format. */
1090 p->optimize_strings = copy_list (current_optimize_pragma);
1091 p->target_strings = copy_list (current_target_pragma);
1094 /* Handle #pragma GCC pop_options to restore the current target and
1095 optimization options from a previous push_options. */
1097 static void
1098 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1100 enum cpp_ttype token;
1101 tree x = 0;
1102 opt_stack *p;
1104 token = pragma_lex (&x);
1105 if (token != CPP_EOF)
1107 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1108 return;
1111 if (! options_stack)
1113 warning (OPT_Wpragmas,
1114 "%<#pragma GCC pop_options%> without a corresponding "
1115 "%<#pragma GCC push_options%>");
1116 return;
1119 p = options_stack;
1120 options_stack = p->prev;
1122 if (p->target_binary != target_option_current_node)
1124 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1125 target_option_current_node = p->target_binary;
1128 /* Always restore optimization options as optimization_current_node is
1129 * overwritten by invoke_set_current_function_hook. */
1130 cl_optimization_restore (&global_options, &global_options_set,
1131 TREE_OPTIMIZATION (p->optimize_binary));
1132 cl_target_option_restore (&global_options, &global_options_set,
1133 TREE_TARGET_OPTION (p->target_binary));
1135 if (p->optimize_binary != optimization_current_node)
1137 c_cpp_builtins_optimize_pragma (parse_in, optimization_current_node,
1138 p->optimize_binary);
1139 optimization_current_node = p->optimize_binary;
1141 if (flag_checking)
1143 cl_optimization_compare (p->saved_global_options, &global_options);
1144 free (p->saved_global_options);
1147 current_target_pragma = p->target_strings;
1148 current_optimize_pragma = p->optimize_strings;
1151 /* Handle #pragma GCC reset_options to restore the current target and
1152 optimization options to the original options used on the command line. */
1154 static void
1155 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1157 enum cpp_ttype token;
1158 tree x = 0;
1159 tree new_optimize = optimization_default_node;
1160 tree new_target = target_option_default_node;
1162 token = pragma_lex (&x);
1163 if (token != CPP_EOF)
1165 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1166 return;
1169 if (new_target != target_option_current_node)
1171 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1172 target_option_current_node = new_target;
1175 if (new_optimize != optimization_current_node)
1177 tree old_optimize = optimization_current_node;
1178 cl_optimization_restore (&global_options, &global_options_set,
1179 TREE_OPTIMIZATION (new_optimize));
1180 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1181 optimization_current_node = new_optimize;
1184 current_target_pragma = NULL_TREE;
1185 current_optimize_pragma = NULL_TREE;
1188 /* Print a plain user-specified message. */
1190 static void
1191 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1193 location_t loc;
1194 enum cpp_ttype token;
1195 tree x, message = 0;
1197 token = pragma_lex (&x);
1198 if (token == CPP_OPEN_PAREN)
1200 token = pragma_lex (&x);
1201 if (token == CPP_STRING)
1202 message = x;
1203 else
1204 GCC_BAD ("expected a string after %<#pragma message%>");
1205 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1206 GCC_BAD ("malformed %<#pragma message%>, ignored");
1208 else if (token == CPP_STRING)
1209 message = x;
1210 else
1211 GCC_BAD ("expected a string after %<#pragma message%>");
1213 gcc_assert (message);
1215 if (pragma_lex (&x, &loc) != CPP_EOF)
1216 warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma message%>");
1218 if (TREE_STRING_LENGTH (message) > 1)
1219 inform (input_location, "%<#pragma message: %s%>",
1220 TREE_STRING_POINTER (message));
1223 /* Mark whether the current location is valid for a STDC pragma. */
1225 static bool valid_location_for_stdc_pragma;
1227 void
1228 mark_valid_location_for_stdc_pragma (bool flag)
1230 valid_location_for_stdc_pragma = flag;
1233 /* Return true if the current location is valid for a STDC pragma. */
1235 bool
1236 valid_location_for_stdc_pragma_p (void)
1238 return valid_location_for_stdc_pragma;
1241 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1243 /* A STDC pragma must appear outside of external declarations or
1244 preceding all explicit declarations and statements inside a compound
1245 statement; its behavior is undefined if used in any other context.
1246 It takes a switch of ON, OFF, or DEFAULT. */
1248 static enum pragma_switch_t
1249 handle_stdc_pragma (const char *pname)
1251 const char *arg;
1252 tree t;
1253 enum pragma_switch_t ret;
1255 if (!valid_location_for_stdc_pragma_p ())
1257 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1258 pname);
1259 return PRAGMA_BAD;
1262 if (pragma_lex (&t) != CPP_NAME)
1264 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1265 return PRAGMA_BAD;
1268 arg = IDENTIFIER_POINTER (t);
1270 if (!strcmp (arg, "ON"))
1271 ret = PRAGMA_ON;
1272 else if (!strcmp (arg, "OFF"))
1273 ret = PRAGMA_OFF;
1274 else if (!strcmp (arg, "DEFAULT"))
1275 ret = PRAGMA_DEFAULT;
1276 else
1278 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1279 return PRAGMA_BAD;
1282 if (pragma_lex (&t) != CPP_EOF)
1284 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1285 return PRAGMA_BAD;
1288 return ret;
1291 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1292 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1293 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1295 static void
1296 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1298 if (c_dialect_cxx ())
1300 if (warn_unknown_pragmas > in_system_header_at (input_location))
1301 warning (OPT_Wunknown_pragmas,
1302 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1303 " for C++");
1304 return;
1307 if (!targetm.decimal_float_supported_p ())
1309 if (warn_unknown_pragmas > in_system_header_at (input_location))
1310 warning (OPT_Wunknown_pragmas,
1311 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1312 " on this target");
1313 return;
1316 pedwarn (input_location, OPT_Wpedantic,
1317 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1319 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1321 case PRAGMA_ON:
1322 set_float_const_decimal64 ();
1323 break;
1324 case PRAGMA_OFF:
1325 case PRAGMA_DEFAULT:
1326 clear_float_const_decimal64 ();
1327 break;
1328 case PRAGMA_BAD:
1329 break;
1333 /* A vector of registered pragma callbacks, which is never freed. */
1335 static vec<internal_pragma_handler> registered_pragmas;
1337 struct pragma_ns_name
1339 const char *space;
1340 const char *name;
1344 static vec<pragma_ns_name> registered_pp_pragmas;
1346 struct omp_pragma_def { const char *name; unsigned int id; };
1347 static const struct omp_pragma_def oacc_pragmas[] = {
1348 { "atomic", PRAGMA_OACC_ATOMIC },
1349 { "cache", PRAGMA_OACC_CACHE },
1350 { "data", PRAGMA_OACC_DATA },
1351 { "declare", PRAGMA_OACC_DECLARE },
1352 { "enter", PRAGMA_OACC_ENTER_DATA },
1353 { "exit", PRAGMA_OACC_EXIT_DATA },
1354 { "host_data", PRAGMA_OACC_HOST_DATA },
1355 { "kernels", PRAGMA_OACC_KERNELS },
1356 { "loop", PRAGMA_OACC_LOOP },
1357 { "parallel", PRAGMA_OACC_PARALLEL },
1358 { "routine", PRAGMA_OACC_ROUTINE },
1359 { "serial", PRAGMA_OACC_SERIAL },
1360 { "update", PRAGMA_OACC_UPDATE },
1361 { "wait", PRAGMA_OACC_WAIT }
1363 static const struct omp_pragma_def omp_pragmas[] = {
1364 { "allocate", PRAGMA_OMP_ALLOCATE },
1365 { "atomic", PRAGMA_OMP_ATOMIC },
1366 { "barrier", PRAGMA_OMP_BARRIER },
1367 { "cancel", PRAGMA_OMP_CANCEL },
1368 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1369 { "critical", PRAGMA_OMP_CRITICAL },
1370 { "depobj", PRAGMA_OMP_DEPOBJ },
1371 { "error", PRAGMA_OMP_ERROR },
1372 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1373 { "flush", PRAGMA_OMP_FLUSH },
1374 { "nothing", PRAGMA_OMP_NOTHING },
1375 { "requires", PRAGMA_OMP_REQUIRES },
1376 { "scope", PRAGMA_OMP_SCOPE },
1377 { "section", PRAGMA_OMP_SECTION },
1378 { "sections", PRAGMA_OMP_SECTIONS },
1379 { "single", PRAGMA_OMP_SINGLE },
1380 { "task", PRAGMA_OMP_TASK },
1381 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1382 { "taskwait", PRAGMA_OMP_TASKWAIT },
1383 { "taskyield", PRAGMA_OMP_TASKYIELD },
1384 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1386 static const struct omp_pragma_def omp_pragmas_simd[] = {
1387 { "declare", PRAGMA_OMP_DECLARE },
1388 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1389 { "for", PRAGMA_OMP_FOR },
1390 { "loop", PRAGMA_OMP_LOOP },
1391 { "masked", PRAGMA_OMP_MASKED },
1392 { "master", PRAGMA_OMP_MASTER },
1393 { "ordered", PRAGMA_OMP_ORDERED },
1394 { "parallel", PRAGMA_OMP_PARALLEL },
1395 { "scan", PRAGMA_OMP_SCAN },
1396 { "simd", PRAGMA_OMP_SIMD },
1397 { "target", PRAGMA_OMP_TARGET },
1398 { "taskloop", PRAGMA_OMP_TASKLOOP },
1399 { "teams", PRAGMA_OMP_TEAMS },
1402 void
1403 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1405 const int n_oacc_pragmas = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1406 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1407 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1408 / sizeof (*omp_pragmas);
1409 int i;
1411 for (i = 0; i < n_oacc_pragmas; ++i)
1412 if (oacc_pragmas[i].id == id)
1414 *space = "acc";
1415 *name = oacc_pragmas[i].name;
1416 return;
1419 for (i = 0; i < n_omp_pragmas; ++i)
1420 if (omp_pragmas[i].id == id)
1422 *space = "omp";
1423 *name = omp_pragmas[i].name;
1424 return;
1427 for (i = 0; i < n_omp_pragmas_simd; ++i)
1428 if (omp_pragmas_simd[i].id == id)
1430 *space = "omp";
1431 *name = omp_pragmas_simd[i].name;
1432 return;
1435 if (id >= PRAGMA_FIRST_EXTERNAL
1436 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1438 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1439 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1440 return;
1443 gcc_unreachable ();
1446 /* Front-end wrappers for pragma registration to avoid dragging
1447 cpplib.h in almost everywhere. */
1449 static void
1450 c_register_pragma_1 (const char *space, const char *name,
1451 internal_pragma_handler ihandler, bool allow_expansion)
1453 unsigned id;
1455 if (flag_preprocess_only)
1457 pragma_ns_name ns_name;
1459 if (!allow_expansion)
1460 return;
1462 ns_name.space = space;
1463 ns_name.name = name;
1464 registered_pp_pragmas.safe_push (ns_name);
1465 id = registered_pp_pragmas.length ();
1466 id += PRAGMA_FIRST_EXTERNAL - 1;
1468 else
1470 registered_pragmas.safe_push (ihandler);
1471 id = registered_pragmas.length ();
1472 id += PRAGMA_FIRST_EXTERNAL - 1;
1474 /* The C front end allocates 8 bits in c_token. The C++ front end
1475 keeps the pragma kind in the form of INTEGER_CST, so no small
1476 limit applies. At present this is sufficient. */
1477 gcc_assert (id < 256);
1480 cpp_register_deferred_pragma (parse_in, space, name, id,
1481 allow_expansion, false);
1484 /* Register a C pragma handler, using a space and a name. It disallows pragma
1485 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1486 void
1487 c_register_pragma (const char *space, const char *name,
1488 pragma_handler_1arg handler)
1490 internal_pragma_handler ihandler;
1492 ihandler.handler.handler_1arg = handler;
1493 ihandler.extra_data = false;
1494 ihandler.data = NULL;
1495 c_register_pragma_1 (space, name, ihandler, false);
1498 /* Register a C pragma handler, using a space and a name, it also carries an
1499 extra data field which can be used by the handler. It disallows pragma
1500 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1501 instead). */
1502 void
1503 c_register_pragma_with_data (const char *space, const char *name,
1504 pragma_handler_2arg handler, void * data)
1506 internal_pragma_handler ihandler;
1508 ihandler.handler.handler_2arg = handler;
1509 ihandler.extra_data = true;
1510 ihandler.data = data;
1511 c_register_pragma_1 (space, name, ihandler, false);
1514 /* Register a C pragma handler, using a space and a name. It allows pragma
1515 expansion as in the following example:
1517 #define NUMBER 10
1518 #pragma count (NUMBER)
1520 Name expansion is still disallowed. */
1521 void
1522 c_register_pragma_with_expansion (const char *space, const char *name,
1523 pragma_handler_1arg handler)
1525 internal_pragma_handler ihandler;
1527 ihandler.handler.handler_1arg = handler;
1528 ihandler.extra_data = false;
1529 ihandler.data = NULL;
1530 c_register_pragma_1 (space, name, ihandler, true);
1533 /* Register a C pragma handler, using a space and a name, it also carries an
1534 extra data field which can be used by the handler. It allows pragma
1535 expansion as in the following example:
1537 #define NUMBER 10
1538 #pragma count (NUMBER)
1540 Name expansion is still disallowed. */
1541 void
1542 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1543 pragma_handler_2arg handler,
1544 void *data)
1546 internal_pragma_handler ihandler;
1548 ihandler.handler.handler_2arg = handler;
1549 ihandler.extra_data = true;
1550 ihandler.data = data;
1551 c_register_pragma_1 (space, name, ihandler, true);
1554 void
1555 c_invoke_pragma_handler (unsigned int id)
1557 internal_pragma_handler *ihandler;
1558 pragma_handler_1arg handler_1arg;
1559 pragma_handler_2arg handler_2arg;
1561 id -= PRAGMA_FIRST_EXTERNAL;
1562 ihandler = &registered_pragmas[id];
1563 if (ihandler->extra_data)
1565 handler_2arg = ihandler->handler.handler_2arg;
1566 handler_2arg (parse_in, ihandler->data);
1568 else
1570 handler_1arg = ihandler->handler.handler_1arg;
1571 handler_1arg (parse_in);
1575 /* Set up front-end pragmas. */
1576 void
1577 init_pragma (void)
1579 if (flag_openacc)
1581 const int n_oacc_pragmas
1582 = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1583 int i;
1585 for (i = 0; i < n_oacc_pragmas; ++i)
1586 cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1587 oacc_pragmas[i].id, true, true);
1590 if (flag_openmp)
1592 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1593 int i;
1595 for (i = 0; i < n_omp_pragmas; ++i)
1596 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1597 omp_pragmas[i].id, true, true);
1599 if (flag_openmp || flag_openmp_simd)
1601 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1602 / sizeof (*omp_pragmas);
1603 int i;
1605 for (i = 0; i < n_omp_pragmas_simd; ++i)
1606 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1607 omp_pragmas_simd[i].id, true, true);
1610 if (!flag_preprocess_only)
1611 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1612 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1614 if (!flag_preprocess_only)
1615 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1616 false);
1618 if (!flag_preprocess_only)
1619 cpp_register_deferred_pragma (parse_in, "GCC", "unroll", PRAGMA_UNROLL,
1620 false, false);
1622 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1623 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1624 #else
1625 c_register_pragma (0, "pack", handle_pragma_pack);
1626 #endif
1627 c_register_pragma (0, "weak", handle_pragma_weak);
1629 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1631 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1632 c_register_pragma ("GCC", "target", handle_pragma_target);
1633 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1634 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1635 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1636 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1638 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1639 handle_pragma_float_const_decimal64);
1641 c_register_pragma_with_expansion (0, "redefine_extname",
1642 handle_pragma_redefine_extname);
1644 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1646 #ifdef REGISTER_TARGET_PRAGMAS
1647 REGISTER_TARGET_PRAGMAS ();
1648 #endif
1650 global_sso = default_sso;
1651 c_register_pragma (0, "scalar_storage_order",
1652 handle_pragma_scalar_storage_order);
1654 /* Allow plugins to register their own pragmas. */
1655 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1658 #include "gt-c-family-c-pragma.h"