Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / c-family / c-pragma.cc
blob0d2b333cebbed32423d5dc6fd2a3ac0ce0bf8b94
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2023 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 *)
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 *)
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 *)
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%>, or %<default%> after "
445 "%<#pragma scalar_storage_order%>");
446 kind_string = IDENTIFIER_POINTER (x);
447 if (strcmp (kind_string, "default") == 0)
448 global_sso = default_sso;
449 else if (strcmp (kind_string, "big") == 0)
450 global_sso = SSO_BIG_ENDIAN;
451 else if (strcmp (kind_string, "little") == 0)
452 global_sso = SSO_LITTLE_ENDIAN;
453 else
454 GCC_BAD ("expected %<big-endian%>, %<little-endian%>, or %<default%> after "
455 "%<#pragma scalar_storage_order%>");
458 /* GCC supports two #pragma directives for renaming the external
459 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
460 compatibility with the Solaris and VMS system headers. GCC also
461 has its own notation for this, __asm__("name") annotations.
463 Corner cases of these features and their interaction:
465 1) Both pragmas silently apply only to declarations with external
466 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
467 do not have this restriction.
469 2) In C++, both #pragmas silently apply only to extern "C" declarations.
470 Asm labels do not have this restriction.
472 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
473 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
474 new name is different, a warning issues and the name does not change.
476 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
477 *not* the DECL_ASSEMBLER_NAME.
479 5) If #pragma extern_prefix is in effect and a declaration occurs
480 with an __asm__ name, the #pragma extern_prefix is silently
481 ignored for that declaration.
483 6) If #pragma extern_prefix and #pragma redefine_extname apply to
484 the same declaration, whichever triggered first wins, and a warning
485 is issued. (We would like to have #pragma redefine_extname always
486 win, but it can appear either before or after the declaration, and
487 if it appears afterward, we have no way of knowing whether a modified
488 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
490 struct GTY(()) pending_redefinition {
491 tree oldname;
492 tree newname;
496 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
498 static void handle_pragma_redefine_extname (cpp_reader *);
500 /* #pragma redefine_extname oldname newname */
501 static void
502 handle_pragma_redefine_extname (cpp_reader *)
504 tree oldname, newname, decls, x;
505 enum cpp_ttype t;
506 bool found;
508 if (pragma_lex (&oldname) != CPP_NAME)
509 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
510 if (pragma_lex (&newname) != CPP_NAME)
511 GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
512 t = pragma_lex (&x);
513 if (t != CPP_EOF)
514 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
516 found = false;
517 for (decls = c_linkage_bindings (oldname);
518 decls; )
520 tree decl;
521 if (TREE_CODE (decls) == TREE_LIST)
523 decl = TREE_VALUE (decls);
524 decls = TREE_CHAIN (decls);
526 else
528 decl = decls;
529 decls = NULL_TREE;
532 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
533 && VAR_OR_FUNCTION_DECL_P (decl))
535 found = true;
536 if (DECL_ASSEMBLER_NAME_SET_P (decl))
538 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
539 name = targetm.strip_name_encoding (name);
541 if (!id_equal (newname, name))
542 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> "
543 "ignored due to conflict with previous rename");
545 else
546 symtab->change_decl_assembler_name (decl, newname);
550 if (!found)
551 /* We have to add this to the rename list even if there's already
552 a global value that doesn't meet the above criteria, because in
553 C++ "struct foo {...};" puts "foo" in the current namespace but
554 does *not* conflict with a subsequent declaration of a function
555 or variable foo. See g++.dg/other/pragma-re-2.C. */
556 add_to_renaming_pragma_list (oldname, newname);
559 /* This is called from here and from ia64-c.cc. */
560 void
561 add_to_renaming_pragma_list (tree oldname, tree newname)
563 unsigned ix;
564 pending_redefinition *p;
566 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
567 if (oldname == p->oldname)
569 if (p->newname != newname)
570 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored due to "
571 "conflict with previous %<#pragma redefine_extname%>");
572 return;
575 pending_redefinition e = {oldname, newname};
576 vec_safe_push (pending_redefine_extname, e);
579 /* The current prefix set by #pragma extern_prefix. */
580 GTY(()) tree pragma_extern_prefix;
582 /* Hook from the front ends to apply the results of one of the preceding
583 pragmas that rename variables. */
585 tree
586 maybe_apply_renaming_pragma (tree decl, tree asmname)
588 unsigned ix;
589 pending_redefinition *p;
591 /* The renaming pragmas are only applied to declarations with
592 external linkage. */
593 if (!VAR_OR_FUNCTION_DECL_P (decl)
594 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
595 || !has_c_linkage (decl))
596 return asmname;
598 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
599 but we may warn about a rename that conflicts. */
600 if (DECL_ASSEMBLER_NAME_SET_P (decl))
602 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
603 oldname = targetm.strip_name_encoding (oldname);
605 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
606 warning (OPT_Wpragmas, "%<asm%> declaration ignored due to "
607 "conflict with previous rename");
609 /* Take any pending redefine_extname off the list. */
610 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
611 if (DECL_NAME (decl) == p->oldname)
613 /* Only warn if there is a conflict. */
614 if (!id_equal (p->newname, oldname))
615 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
616 "due to conflict with previous rename");
618 pending_redefine_extname->unordered_remove (ix);
619 break;
621 return NULL_TREE;
624 /* Find out if we have a pending #pragma redefine_extname. */
625 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
626 if (DECL_NAME (decl) == p->oldname)
628 tree newname = p->newname;
629 pending_redefine_extname->unordered_remove (ix);
631 /* If we already have an asmname, #pragma redefine_extname is
632 ignored (with a warning if it conflicts). */
633 if (asmname)
635 if (strcmp (TREE_STRING_POINTER (asmname),
636 IDENTIFIER_POINTER (newname)) != 0)
637 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
638 "due to conflict with %<asm%> declaration");
639 return asmname;
642 /* Otherwise we use what we've got; #pragma extern_prefix is
643 silently ignored. */
644 return build_string (IDENTIFIER_LENGTH (newname),
645 IDENTIFIER_POINTER (newname));
648 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
649 if (asmname)
650 return asmname;
652 /* If #pragma extern_prefix is in effect, apply it. */
653 if (pragma_extern_prefix)
655 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
656 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
658 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
659 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
661 char *newname = (char *) alloca (plen + ilen + 1);
663 memcpy (newname, prefix, plen);
664 memcpy (newname + plen, id, ilen + 1);
666 return build_string (plen + ilen, newname);
669 /* Nada. */
670 return NULL_TREE;
674 static void handle_pragma_visibility (cpp_reader *);
676 static vec<int> visstack;
678 /* Push the visibility indicated by STR onto the top of the #pragma
679 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
680 C++ namespace with visibility attribute and 2 for C++ builtin
681 ABI namespace. push_visibility/pop_visibility calls must have
682 matching KIND, it is not allowed to push visibility using one
683 KIND and pop using a different one. */
685 void
686 push_visibility (const char *str, int kind)
688 visstack.safe_push (((int) default_visibility) | (kind << 8));
689 if (!strcmp (str, "default"))
690 default_visibility = VISIBILITY_DEFAULT;
691 else if (!strcmp (str, "internal"))
692 default_visibility = VISIBILITY_INTERNAL;
693 else if (!strcmp (str, "hidden"))
694 default_visibility = VISIBILITY_HIDDEN;
695 else if (!strcmp (str, "protected"))
696 default_visibility = VISIBILITY_PROTECTED;
697 else
698 GCC_BAD ("%<#pragma GCC visibility push()%> must specify %<default%>, "
699 "%<internal%>, %<hidden%> or %<protected%>");
700 visibility_options.inpragma = 1;
703 /* Pop a level of the #pragma visibility stack. Return true if
704 successful. */
706 bool
707 pop_visibility (int kind)
709 if (!visstack.length ())
710 return false;
711 if ((visstack.last () >> 8) != kind)
712 return false;
713 default_visibility
714 = (enum symbol_visibility) (visstack.pop () & 0xff);
715 visibility_options.inpragma
716 = visstack.length () != 0;
717 return true;
720 /* Sets the default visibility for symbols to something other than that
721 specified on the command line. */
723 static void
724 handle_pragma_visibility (cpp_reader *)
726 /* Form is #pragma GCC visibility push(hidden)|pop */
727 tree x;
728 enum cpp_ttype token;
729 enum { bad, push, pop } action = bad;
731 token = pragma_lex (&x);
732 if (token == CPP_NAME)
734 const char *op = IDENTIFIER_POINTER (x);
735 if (!strcmp (op, "push"))
736 action = push;
737 else if (!strcmp (op, "pop"))
738 action = pop;
740 if (bad == action)
741 GCC_BAD ("%<#pragma GCC visibility%> must be followed by %<push%> "
742 "or %<pop%>");
743 else
745 if (pop == action)
747 if (! pop_visibility (0))
748 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
750 else
752 if (pragma_lex (&x) != CPP_OPEN_PAREN)
753 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
754 token = pragma_lex (&x);
755 if (token != CPP_NAME)
756 GCC_BAD ("malformed %<#pragma GCC visibility push%>");
757 else
758 push_visibility (IDENTIFIER_POINTER (x), 0);
759 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
760 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
763 if (pragma_lex (&x) != CPP_EOF)
764 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
767 /* Helper routines for parsing #pragma GCC diagnostic. */
768 class pragma_diagnostic_data
770 pragma_diagnostic_data (const pragma_diagnostic_data &) = delete;
771 pragma_diagnostic_data& operator= (const pragma_diagnostic_data &) = delete;
773 public:
774 bool valid;
775 location_t loc_kind, loc_option;
776 enum pd_kind_t
778 PK_INVALID,
779 PK_PUSH,
780 PK_POP,
781 PK_IGNORED_ATTRIBUTES,
782 PK_DIAGNOSTIC,
783 } pd_kind;
784 diagnostic_t diagnostic_kind;
785 const char *kind_str;
786 const char *option_str;
787 bool own_option_str;
789 pragma_diagnostic_data () { clear (); }
790 void clear ()
792 valid = false;
793 loc_kind = loc_option = UNKNOWN_LOCATION;
794 pd_kind = PK_INVALID;
795 diagnostic_kind = DK_UNSPECIFIED;
796 kind_str = option_str = nullptr;
797 own_option_str = false;
800 ~pragma_diagnostic_data ()
802 if (own_option_str && option_str)
803 XDELETEVEC (const_cast<char *> (option_str));
806 void set_kind (const char *kind_string)
808 kind_str = kind_string;
810 pd_kind = PK_INVALID;
811 diagnostic_kind = DK_UNSPECIFIED;
812 if (strcmp (kind_str, "push") == 0)
813 pd_kind = PK_PUSH;
814 else if (strcmp (kind_str, "pop") == 0)
815 pd_kind = PK_POP;
816 else if (strcmp (kind_str, "ignored_attributes") == 0)
817 pd_kind = PK_IGNORED_ATTRIBUTES;
818 else if (strcmp (kind_str, "error") == 0)
820 pd_kind = PK_DIAGNOSTIC;
821 diagnostic_kind = DK_ERROR;
823 else if (strcmp (kind_str, "warning") == 0)
825 pd_kind = PK_DIAGNOSTIC;
826 diagnostic_kind = DK_WARNING;
828 else if (strcmp (kind_str, "ignored") == 0)
830 pd_kind = PK_DIAGNOSTIC;
831 diagnostic_kind = DK_IGNORED;
835 bool needs_option () const
837 return pd_kind == PK_IGNORED_ATTRIBUTES
838 || pd_kind == PK_DIAGNOSTIC;
843 /* When compiling normally, use pragma_lex () to obtain the needed tokens.
844 This will call into either the C or C++ frontends as appropriate. */
846 static void
847 pragma_diagnostic_lex_normal (pragma_diagnostic_data *result)
849 result->clear ();
850 tree x;
851 auto ttype = pragma_lex (&x, &result->loc_kind);
852 if (ttype != CPP_NAME)
853 return;
854 result->set_kind (IDENTIFIER_POINTER (x));
855 if (result->pd_kind == pragma_diagnostic_data::PK_INVALID)
856 return;
858 if (result->needs_option ())
860 ttype = pragma_lex (&x, &result->loc_option);
861 if (ttype != CPP_STRING)
862 return;
863 result->option_str = TREE_STRING_POINTER (x);
866 result->valid = true;
869 /* When preprocessing only, pragma_lex () is not available, so obtain the
870 tokens directly from libcpp. We also need to inform the token streamer
871 about all tokens we lex ourselves here, so it outputs them too; this is
872 done by calling c_pp_stream_token () for each.
874 ??? If we need to support more pragmas in the future, maybe initialize
875 this_parser with the pragma tokens and call pragma_lex () instead? */
877 static void
878 pragma_diagnostic_lex_pp (pragma_diagnostic_data *result)
880 result->clear ();
882 auto tok = cpp_get_token_with_location (parse_in, &result->loc_kind);
883 c_pp_stream_token (parse_in, tok, result->loc_kind);
884 if (!(tok->type == CPP_NAME || tok->type == CPP_KEYWORD))
885 return;
886 const unsigned char *const kind_u = cpp_token_as_text (parse_in, tok);
887 result->set_kind ((const char *)kind_u);
888 if (result->pd_kind == pragma_diagnostic_data::PK_INVALID)
889 return;
891 if (result->needs_option ())
893 tok = cpp_get_token_with_location (parse_in, &result->loc_option);
894 c_pp_stream_token (parse_in, tok, result->loc_option);
895 if (tok->type != CPP_STRING)
896 return;
897 cpp_string str;
898 if (!cpp_interpret_string_notranslate (parse_in, &tok->val.str, 1, &str,
899 CPP_STRING)
900 || !str.len)
901 return;
902 result->option_str = (const char *)str.text;
903 result->own_option_str = true;
906 result->valid = true;
909 /* Handle #pragma GCC diagnostic. Early mode is used by frontends (such as C++)
910 that do not process the deferred pragma while they are consuming tokens; they
911 can use early mode to make sure diagnostics affecting the preprocessor itself
912 are correctly modified by the #pragma. */
913 template<bool early, bool is_pp> static void
914 handle_pragma_diagnostic_impl ()
916 static const bool want_diagnostics = (is_pp || !early);
918 pragma_diagnostic_data data;
919 if (is_pp)
920 pragma_diagnostic_lex_pp (&data);
921 else
922 pragma_diagnostic_lex_normal (&data);
924 if (!data.kind_str)
926 if (want_diagnostics)
927 warning_at (data.loc_kind, OPT_Wpragmas,
928 "missing %<error%>, %<warning%>, %<ignored%>, %<push%>, "
929 "%<pop%>, or %<ignored_attributes%> after "
930 "%<#pragma GCC diagnostic%>");
931 return;
934 switch (data.pd_kind)
937 case pragma_diagnostic_data::PK_PUSH:
938 diagnostic_push_diagnostics (global_dc, input_location);
939 return;
941 case pragma_diagnostic_data::PK_POP:
942 diagnostic_pop_diagnostics (global_dc, input_location);
943 return;
945 case pragma_diagnostic_data::PK_IGNORED_ATTRIBUTES:
947 if (early)
948 return;
949 if (!data.option_str)
951 warning_at (data.loc_option, OPT_Wpragmas,
952 "missing attribute name after %<#pragma GCC diagnostic "
953 "ignored_attributes%>");
954 return;
956 char *args = xstrdup (data.option_str);
957 const size_t l = strlen (args);
958 if (l == 0)
960 warning_at (data.loc_option, OPT_Wpragmas,
961 "missing argument to %<#pragma GCC "
962 "diagnostic ignored_attributes%>");
963 free (args);
964 return;
966 else if (args[l - 1] == ',')
968 warning_at (data.loc_option, OPT_Wpragmas,
969 "trailing %<,%> in arguments for "
970 "%<#pragma GCC diagnostic ignored_attributes%>");
971 free (args);
972 return;
974 auto_vec<char *> v;
975 for (char *p = strtok (args, ","); p; p = strtok (NULL, ","))
976 v.safe_push (p);
977 handle_ignored_attributes_option (&v);
978 free (args);
979 return;
982 case pragma_diagnostic_data::PK_DIAGNOSTIC:
983 if (!data.option_str)
985 if (want_diagnostics)
986 warning_at (data.loc_option, OPT_Wpragmas,
987 "missing option after %<#pragma GCC diagnostic%> kind");
988 return;
990 break;
992 default:
993 if (want_diagnostics)
994 warning_at (data.loc_kind, OPT_Wpragmas,
995 "expected %<error%>, %<warning%>, %<ignored%>, %<push%>, "
996 "%<pop%>, %<ignored_attributes%> after "
997 "%<#pragma GCC diagnostic%>");
998 return;
1002 gcc_assert (data.pd_kind == pragma_diagnostic_data::PK_DIAGNOSTIC);
1003 gcc_assert (data.valid);
1005 unsigned int lang_mask = c_common_option_lang_mask () | CL_COMMON;
1006 /* option_string + 1 to skip the initial '-' */
1007 unsigned int option_index = find_opt (data.option_str + 1, lang_mask);
1009 if (early && !c_option_is_from_cpp_diagnostics (option_index))
1010 return;
1012 if (option_index == OPT_SPECIAL_unknown)
1014 if (want_diagnostics)
1016 auto_diagnostic_group d;
1017 if (warning_at (data.loc_option, OPT_Wpragmas,
1018 "unknown option after %<#pragma GCC diagnostic%> kind"))
1020 option_proposer op;
1021 const char *hint = op.suggest_option (data.option_str + 1);
1022 if (hint)
1023 inform (data.loc_option, "did you mean %<-%s%>?", hint);
1026 return;
1028 else if (!(cl_options[option_index].flags & CL_WARNING))
1030 if (want_diagnostics)
1031 warning_at (data.loc_option, OPT_Wpragmas,
1032 "%qs is not an option that controls warnings",
1033 data.option_str);
1034 return;
1036 else if (!(cl_options[option_index].flags & lang_mask))
1038 if (want_diagnostics)
1040 char *ok_langs = write_langs (cl_options[option_index].flags);
1041 char *bad_lang = write_langs (c_common_option_lang_mask ());
1042 warning_at (data.loc_option, OPT_Wpragmas,
1043 "option %qs is valid for %s but not for %s",
1044 data.option_str, ok_langs, bad_lang);
1045 free (ok_langs);
1046 free (bad_lang);
1048 return;
1051 const char *arg = NULL;
1052 if (cl_options[option_index].flags & CL_JOINED)
1053 arg = data.option_str + 1 + cl_options[option_index].opt_len;
1055 struct cl_option_handlers handlers;
1056 set_default_handlers (&handlers, NULL);
1057 /* FIXME: input_location isn't the best location here, but it is
1058 what we used to do here before and changing it breaks e.g.
1059 PR69543 and PR69558. */
1060 control_warning_option (option_index, (int) data.diagnostic_kind,
1061 arg, data.diagnostic_kind != DK_IGNORED,
1062 input_location, lang_mask, &handlers,
1063 &global_options, &global_options_set,
1064 global_dc);
1067 static void
1068 handle_pragma_diagnostic (cpp_reader *)
1070 handle_pragma_diagnostic_impl<false, false> ();
1073 static void
1074 handle_pragma_diagnostic_early (cpp_reader *)
1076 handle_pragma_diagnostic_impl<true, false> ();
1079 static void
1080 handle_pragma_diagnostic_early_pp (cpp_reader *)
1082 handle_pragma_diagnostic_impl<true, true> ();
1085 /* Parse #pragma GCC target (xxx) to set target specific options. */
1086 static void
1087 handle_pragma_target(cpp_reader *)
1089 location_t loc;
1090 enum cpp_ttype token;
1091 tree x;
1092 bool close_paren_needed_p = false;
1094 if (cfun)
1096 error ("%<#pragma GCC option%> is not allowed inside functions");
1097 return;
1100 token = pragma_lex (&x, &loc);
1101 if (token == CPP_OPEN_PAREN)
1103 close_paren_needed_p = true;
1104 token = pragma_lex (&x, &loc);
1107 if (token != CPP_STRING)
1108 GCC_BAD_AT (loc, "%<#pragma GCC option%> is not a string");
1110 /* Strings are user options. */
1111 else
1113 tree args = NULL_TREE;
1117 /* Build up the strings now as a tree linked list. Skip empty
1118 strings. */
1119 if (TREE_STRING_LENGTH (x) > 0)
1120 args = tree_cons (NULL_TREE, x, args);
1122 token = pragma_lex (&x);
1123 while (token == CPP_COMMA)
1124 token = pragma_lex (&x);
1126 while (token == CPP_STRING);
1128 if (close_paren_needed_p)
1130 if (token == CPP_CLOSE_PAREN)
1131 token = pragma_lex (&x);
1132 else
1133 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
1134 "not have a final %<)%>");
1137 if (token != CPP_EOF)
1139 error ("%<#pragma GCC target%> string is badly formed");
1140 return;
1143 /* put arguments in the order the user typed them. */
1144 args = nreverse (args);
1146 if (targetm.target_option.pragma_parse (args, NULL_TREE))
1147 current_target_pragma = chainon (current_target_pragma, args);
1149 /* A target pragma can also influence optimization options. */
1150 tree current_optimize
1151 = build_optimization_node (&global_options, &global_options_set);
1152 if (current_optimize != optimization_current_node)
1153 optimization_current_node = current_optimize;
1157 /* Handle #pragma GCC optimize to set optimization options. */
1158 static void
1159 handle_pragma_optimize (cpp_reader *)
1161 enum cpp_ttype token;
1162 tree x;
1163 bool close_paren_needed_p = false;
1164 tree optimization_previous_node = optimization_current_node;
1166 if (cfun)
1168 error ("%<#pragma GCC optimize%> is not allowed inside functions");
1169 return;
1172 token = pragma_lex (&x);
1173 if (token == CPP_OPEN_PAREN)
1175 close_paren_needed_p = true;
1176 token = pragma_lex (&x);
1179 if (token != CPP_STRING && token != CPP_NUMBER)
1180 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
1182 /* Strings/numbers are user options. */
1183 else
1185 tree args = NULL_TREE;
1189 /* Build up the numbers/strings now as a list. */
1190 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
1191 args = tree_cons (NULL_TREE, x, args);
1193 token = pragma_lex (&x);
1194 while (token == CPP_COMMA)
1195 token = pragma_lex (&x);
1197 while (token == CPP_STRING || token == CPP_NUMBER);
1199 if (close_paren_needed_p)
1201 if (token == CPP_CLOSE_PAREN)
1202 token = pragma_lex (&x);
1203 else
1204 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
1205 "not have a final %<)%>");
1208 if (token != CPP_EOF)
1210 error ("%<#pragma GCC optimize%> string is badly formed");
1211 return;
1214 /* put arguments in the order the user typed them. */
1215 args = nreverse (args);
1217 parse_optimize_options (args, false);
1218 current_optimize_pragma = chainon (current_optimize_pragma, args);
1219 optimization_current_node
1220 = build_optimization_node (&global_options, &global_options_set);
1221 c_cpp_builtins_optimize_pragma (parse_in,
1222 optimization_previous_node,
1223 optimization_current_node);
1227 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1228 both the binary representation of the options and the TREE_LIST of
1229 strings that will be added to the function's attribute list. */
1230 struct GTY(()) opt_stack {
1231 struct opt_stack *prev;
1232 tree target_binary;
1233 tree target_strings;
1234 tree optimize_binary;
1235 tree optimize_strings;
1236 gcc_options * GTY ((skip)) saved_global_options;
1239 static GTY(()) struct opt_stack * options_stack;
1241 /* Handle #pragma GCC push_options to save the current target and optimization
1242 options. */
1244 static void
1245 handle_pragma_push_options (cpp_reader *)
1247 enum cpp_ttype token;
1248 tree x = 0;
1250 token = pragma_lex (&x);
1251 if (token != CPP_EOF)
1253 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1254 return;
1257 opt_stack *p = ggc_alloc<opt_stack> ();
1258 p->prev = options_stack;
1259 options_stack = p;
1261 /* Save optimization and target flags in binary format. */
1262 if (flag_checking)
1264 p->saved_global_options = XNEW (gcc_options);
1265 *p->saved_global_options = global_options;
1267 p->optimize_binary = build_optimization_node (&global_options,
1268 &global_options_set);
1269 p->target_binary = build_target_option_node (&global_options,
1270 &global_options_set);
1272 /* Save optimization and target flags in string list format. */
1273 p->optimize_strings = copy_list (current_optimize_pragma);
1274 p->target_strings = copy_list (current_target_pragma);
1277 /* Handle #pragma GCC pop_options to restore the current target and
1278 optimization options from a previous push_options. */
1280 static void
1281 handle_pragma_pop_options (cpp_reader *)
1283 enum cpp_ttype token;
1284 tree x = 0;
1285 opt_stack *p;
1287 token = pragma_lex (&x);
1288 if (token != CPP_EOF)
1290 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1291 return;
1294 if (! options_stack)
1296 warning (OPT_Wpragmas,
1297 "%<#pragma GCC pop_options%> without a corresponding "
1298 "%<#pragma GCC push_options%>");
1299 return;
1302 p = options_stack;
1303 options_stack = p->prev;
1305 if (p->target_binary != target_option_current_node)
1307 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1308 target_option_current_node = p->target_binary;
1311 /* Always restore optimization options as optimization_current_node is
1312 * overwritten by invoke_set_current_function_hook. */
1313 cl_optimization_restore (&global_options, &global_options_set,
1314 TREE_OPTIMIZATION (p->optimize_binary));
1315 cl_target_option_restore (&global_options, &global_options_set,
1316 TREE_TARGET_OPTION (p->target_binary));
1318 if (p->optimize_binary != optimization_current_node)
1320 c_cpp_builtins_optimize_pragma (parse_in, optimization_current_node,
1321 p->optimize_binary);
1322 optimization_current_node = p->optimize_binary;
1324 if (flag_checking && !seen_error ())
1326 cl_optimization_compare (p->saved_global_options, &global_options);
1327 free (p->saved_global_options);
1330 current_target_pragma = p->target_strings;
1331 current_optimize_pragma = p->optimize_strings;
1334 /* Handle #pragma GCC reset_options to restore the current target and
1335 optimization options to the original options used on the command line. */
1337 static void
1338 handle_pragma_reset_options (cpp_reader *)
1340 enum cpp_ttype token;
1341 tree x = 0;
1342 tree new_optimize = optimization_default_node;
1343 tree new_target = target_option_default_node;
1345 token = pragma_lex (&x);
1346 if (token != CPP_EOF)
1348 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1349 return;
1352 if (new_target != target_option_current_node)
1354 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1355 target_option_current_node = new_target;
1358 if (new_optimize != optimization_current_node)
1360 tree old_optimize = optimization_current_node;
1361 cl_optimization_restore (&global_options, &global_options_set,
1362 TREE_OPTIMIZATION (new_optimize));
1363 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1364 optimization_current_node = new_optimize;
1367 current_target_pragma = NULL_TREE;
1368 current_optimize_pragma = NULL_TREE;
1371 /* Print a plain user-specified message. */
1373 static void
1374 handle_pragma_message (cpp_reader *)
1376 location_t loc;
1377 enum cpp_ttype token;
1378 tree x, message = 0;
1380 token = pragma_lex (&x);
1381 if (token == CPP_OPEN_PAREN)
1383 token = pragma_lex (&x);
1384 if (token == CPP_STRING)
1385 message = x;
1386 else
1387 GCC_BAD ("expected a string after %<#pragma message%>");
1388 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1389 GCC_BAD ("malformed %<#pragma message%>, ignored");
1391 else if (token == CPP_STRING)
1392 message = x;
1393 else if (token == CPP_STRING_USERDEF)
1394 GCC_BAD ("string literal with user-defined suffix is invalid in this "
1395 "context");
1396 else
1397 GCC_BAD ("expected a string after %<#pragma message%>");
1399 gcc_assert (message);
1401 if (pragma_lex (&x, &loc) != CPP_EOF)
1402 warning_at (loc, OPT_Wpragmas, "junk at end of %<#pragma message%>");
1404 if (TREE_STRING_LENGTH (message) > 1)
1405 inform (input_location, "%<#pragma message: %s%>",
1406 TREE_STRING_POINTER (message));
1409 /* Ignore a no-op pragma that GCC recognizes, but which has no effect. */
1410 static void
1411 handle_pragma_ignore (cpp_reader *)
1415 /* Mark whether the current location is valid for a STDC pragma. */
1417 static bool valid_location_for_stdc_pragma;
1419 void
1420 mark_valid_location_for_stdc_pragma (bool flag)
1422 valid_location_for_stdc_pragma = flag;
1425 /* Return true if the current location is valid for a STDC pragma. */
1427 bool
1428 valid_location_for_stdc_pragma_p (void)
1430 return valid_location_for_stdc_pragma;
1433 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1435 /* A STDC pragma must appear outside of external declarations or
1436 preceding all explicit declarations and statements inside a compound
1437 statement; its behavior is undefined if used in any other context.
1438 It takes a switch of ON, OFF, or DEFAULT. */
1440 static enum pragma_switch_t
1441 handle_stdc_pragma (const char *pname)
1443 const char *arg;
1444 tree t;
1445 enum pragma_switch_t ret;
1447 if (!valid_location_for_stdc_pragma_p ())
1449 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1450 pname);
1451 return PRAGMA_BAD;
1454 if (pragma_lex (&t) != CPP_NAME)
1456 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1457 return PRAGMA_BAD;
1460 arg = IDENTIFIER_POINTER (t);
1462 if (!strcmp (arg, "ON"))
1463 ret = PRAGMA_ON;
1464 else if (!strcmp (arg, "OFF"))
1465 ret = PRAGMA_OFF;
1466 else if (!strcmp (arg, "DEFAULT"))
1467 ret = PRAGMA_DEFAULT;
1468 else
1470 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1471 return PRAGMA_BAD;
1474 if (pragma_lex (&t) != CPP_EOF)
1476 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1477 return PRAGMA_BAD;
1480 return ret;
1483 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1484 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1485 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1487 static void
1488 handle_pragma_float_const_decimal64 (cpp_reader *)
1490 if (c_dialect_cxx ())
1492 if (warn_unknown_pragmas > in_system_header_at (input_location))
1493 warning (OPT_Wunknown_pragmas,
1494 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1495 " for C++");
1496 return;
1499 if (!targetm.decimal_float_supported_p ())
1501 if (warn_unknown_pragmas > in_system_header_at (input_location))
1502 warning (OPT_Wunknown_pragmas,
1503 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1504 " on this target");
1505 return;
1508 pedwarn (input_location, OPT_Wpedantic,
1509 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1511 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1513 case PRAGMA_ON:
1514 set_float_const_decimal64 ();
1515 break;
1516 case PRAGMA_OFF:
1517 case PRAGMA_DEFAULT:
1518 clear_float_const_decimal64 ();
1519 break;
1520 case PRAGMA_BAD:
1521 break;
1525 /* A vector of registered pragma callbacks, which is never freed. */
1527 static vec<internal_pragma_handler> registered_pragmas;
1529 struct pragma_pp_data
1531 const char *space;
1532 const char *name;
1533 pragma_handler_1arg early_handler;
1537 static vec<pragma_pp_data> registered_pp_pragmas;
1539 struct omp_pragma_def { const char *name; unsigned int id; };
1540 static const struct omp_pragma_def oacc_pragmas[] = {
1541 { "atomic", PRAGMA_OACC_ATOMIC },
1542 { "cache", PRAGMA_OACC_CACHE },
1543 { "data", PRAGMA_OACC_DATA },
1544 { "declare", PRAGMA_OACC_DECLARE },
1545 { "enter", PRAGMA_OACC_ENTER_DATA },
1546 { "exit", PRAGMA_OACC_EXIT_DATA },
1547 { "host_data", PRAGMA_OACC_HOST_DATA },
1548 { "kernels", PRAGMA_OACC_KERNELS },
1549 { "loop", PRAGMA_OACC_LOOP },
1550 { "parallel", PRAGMA_OACC_PARALLEL },
1551 { "routine", PRAGMA_OACC_ROUTINE },
1552 { "serial", PRAGMA_OACC_SERIAL },
1553 { "update", PRAGMA_OACC_UPDATE },
1554 { "wait", PRAGMA_OACC_WAIT }
1556 static const struct omp_pragma_def omp_pragmas[] = {
1557 { "allocate", PRAGMA_OMP_ALLOCATE },
1558 { "assumes", PRAGMA_OMP_ASSUMES },
1559 { "atomic", PRAGMA_OMP_ATOMIC },
1560 { "barrier", PRAGMA_OMP_BARRIER },
1561 { "begin", PRAGMA_OMP_BEGIN },
1562 { "cancel", PRAGMA_OMP_CANCEL },
1563 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1564 { "critical", PRAGMA_OMP_CRITICAL },
1565 { "depobj", PRAGMA_OMP_DEPOBJ },
1566 { "error", PRAGMA_OMP_ERROR },
1567 { "end", PRAGMA_OMP_END },
1568 { "flush", PRAGMA_OMP_FLUSH },
1569 { "nothing", PRAGMA_OMP_NOTHING },
1570 { "requires", PRAGMA_OMP_REQUIRES },
1571 { "scope", PRAGMA_OMP_SCOPE },
1572 { "section", PRAGMA_OMP_SECTION },
1573 { "sections", PRAGMA_OMP_SECTIONS },
1574 { "single", PRAGMA_OMP_SINGLE },
1575 { "task", PRAGMA_OMP_TASK },
1576 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1577 { "taskwait", PRAGMA_OMP_TASKWAIT },
1578 { "taskyield", PRAGMA_OMP_TASKYIELD },
1579 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1581 static const struct omp_pragma_def omp_pragmas_simd[] = {
1582 { "assume", PRAGMA_OMP_ASSUME },
1583 { "declare", PRAGMA_OMP_DECLARE },
1584 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1585 { "for", PRAGMA_OMP_FOR },
1586 { "loop", PRAGMA_OMP_LOOP },
1587 { "masked", PRAGMA_OMP_MASKED },
1588 { "master", PRAGMA_OMP_MASTER },
1589 { "ordered", PRAGMA_OMP_ORDERED },
1590 { "parallel", PRAGMA_OMP_PARALLEL },
1591 { "scan", PRAGMA_OMP_SCAN },
1592 { "simd", PRAGMA_OMP_SIMD },
1593 { "target", PRAGMA_OMP_TARGET },
1594 { "taskloop", PRAGMA_OMP_TASKLOOP },
1595 { "teams", PRAGMA_OMP_TEAMS },
1598 void
1599 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1601 const int n_oacc_pragmas = ARRAY_SIZE (oacc_pragmas);
1602 const int n_omp_pragmas = ARRAY_SIZE (omp_pragmas);
1603 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1604 / sizeof (*omp_pragmas);
1605 int i;
1607 for (i = 0; i < n_oacc_pragmas; ++i)
1608 if (oacc_pragmas[i].id == id)
1610 *space = "acc";
1611 *name = oacc_pragmas[i].name;
1612 return;
1615 for (i = 0; i < n_omp_pragmas; ++i)
1616 if (omp_pragmas[i].id == id)
1618 *space = "omp";
1619 *name = omp_pragmas[i].name;
1620 return;
1623 for (i = 0; i < n_omp_pragmas_simd; ++i)
1624 if (omp_pragmas_simd[i].id == id)
1626 *space = "omp";
1627 *name = omp_pragmas_simd[i].name;
1628 return;
1631 if (id >= PRAGMA_FIRST_EXTERNAL
1632 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1634 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1635 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1636 return;
1639 gcc_unreachable ();
1642 /* Front-end wrappers for pragma registration to avoid dragging
1643 cpplib.h in almost everywhere. */
1645 static void
1646 c_register_pragma_1 (const char *space, const char *name,
1647 internal_pragma_handler ihandler, bool allow_expansion)
1649 unsigned id;
1651 if (flag_preprocess_only)
1653 if (cpp_get_options (parse_in)->directives_only
1654 || !(allow_expansion || ihandler.early_handler.handler_1arg))
1655 return;
1657 pragma_pp_data pp_data;
1658 pp_data.space = space;
1659 pp_data.name = name;
1660 pp_data.early_handler = ihandler.early_handler.handler_1arg;
1661 registered_pp_pragmas.safe_push (pp_data);
1662 id = registered_pp_pragmas.length ();
1663 id += PRAGMA_FIRST_EXTERNAL - 1;
1665 else
1667 registered_pragmas.safe_push (ihandler);
1668 id = registered_pragmas.length ();
1669 id += PRAGMA_FIRST_EXTERNAL - 1;
1671 /* The C front end allocates 8 bits in c_token. The C++ front end
1672 keeps the pragma kind in the form of INTEGER_CST, so no small
1673 limit applies. At present this is sufficient. */
1674 gcc_assert (id < 256);
1677 cpp_register_deferred_pragma (parse_in, space, name, id,
1678 allow_expansion, false);
1681 /* Register a C pragma handler, using a space and a name. It disallows pragma
1682 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1683 void
1684 c_register_pragma (const char *space, const char *name,
1685 pragma_handler_1arg handler)
1687 c_register_pragma_with_early_handler (space, name, handler, nullptr);
1689 void c_register_pragma_with_early_handler (const char *space, const char *name,
1690 pragma_handler_1arg handler,
1691 pragma_handler_1arg early_handler)
1693 internal_pragma_handler ihandler;
1695 ihandler.handler.handler_1arg = handler;
1696 ihandler.early_handler.handler_1arg = early_handler;
1697 ihandler.extra_data = false;
1698 ihandler.data = NULL;
1699 c_register_pragma_1 (space, name, ihandler, false);
1702 /* Register a C pragma handler, using a space and a name, it also carries an
1703 extra data field which can be used by the handler. It disallows pragma
1704 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1705 instead). */
1706 void
1707 c_register_pragma_with_data (const char *space, const char *name,
1708 pragma_handler_2arg handler, void * data)
1710 internal_pragma_handler ihandler;
1712 ihandler.handler.handler_2arg = handler;
1713 ihandler.early_handler.handler_2arg = nullptr;
1714 ihandler.extra_data = true;
1715 ihandler.data = data;
1716 c_register_pragma_1 (space, name, ihandler, false);
1719 /* Register a C pragma handler, using a space and a name. It allows pragma
1720 expansion as in the following example:
1722 #define NUMBER 10
1723 #pragma count (NUMBER)
1725 Name expansion is still disallowed. */
1726 void
1727 c_register_pragma_with_expansion (const char *space, const char *name,
1728 pragma_handler_1arg handler)
1730 internal_pragma_handler ihandler;
1732 ihandler.handler.handler_1arg = handler;
1733 ihandler.early_handler.handler_1arg = nullptr;
1734 ihandler.extra_data = false;
1735 ihandler.data = NULL;
1736 c_register_pragma_1 (space, name, ihandler, true);
1739 /* Register a C pragma handler, using a space and a name, it also carries an
1740 extra data field which can be used by the handler. It allows pragma
1741 expansion as in the following example:
1743 #define NUMBER 10
1744 #pragma count (NUMBER)
1746 Name expansion is still disallowed. */
1747 void
1748 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1749 pragma_handler_2arg handler,
1750 void *data)
1752 internal_pragma_handler ihandler;
1754 ihandler.handler.handler_2arg = handler;
1755 ihandler.early_handler.handler_2arg = nullptr;
1756 ihandler.extra_data = true;
1757 ihandler.data = data;
1758 c_register_pragma_1 (space, name, ihandler, true);
1761 void
1762 c_invoke_pragma_handler (unsigned int id)
1764 internal_pragma_handler *ihandler;
1765 pragma_handler_1arg handler_1arg;
1766 pragma_handler_2arg handler_2arg;
1768 id -= PRAGMA_FIRST_EXTERNAL;
1769 ihandler = &registered_pragmas[id];
1770 if (ihandler->extra_data)
1772 handler_2arg = ihandler->handler.handler_2arg;
1773 handler_2arg (parse_in, ihandler->data);
1775 else
1777 handler_1arg = ihandler->handler.handler_1arg;
1778 handler_1arg (parse_in);
1782 /* In contrast to the normal handler, the early handler is optional. */
1783 void
1784 c_invoke_early_pragma_handler (unsigned int id)
1786 internal_pragma_handler *ihandler;
1787 pragma_handler_1arg handler_1arg;
1788 pragma_handler_2arg handler_2arg;
1790 id -= PRAGMA_FIRST_EXTERNAL;
1791 ihandler = &registered_pragmas[id];
1792 if (ihandler->extra_data)
1794 handler_2arg = ihandler->early_handler.handler_2arg;
1795 if (handler_2arg)
1796 handler_2arg (parse_in, ihandler->data);
1798 else
1800 handler_1arg = ihandler->early_handler.handler_1arg;
1801 if (handler_1arg)
1802 handler_1arg (parse_in);
1806 void
1807 c_pp_invoke_early_pragma_handler (unsigned int id)
1809 const auto data = &registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL];
1810 if (data->early_handler)
1811 data->early_handler (parse_in);
1814 /* Set up front-end pragmas. */
1815 void
1816 init_pragma (void)
1819 if (!cpp_get_options (parse_in)->directives_only)
1821 if (flag_openacc)
1823 const int n_oacc_pragmas = ARRAY_SIZE (oacc_pragmas);
1824 int i;
1826 for (i = 0; i < n_oacc_pragmas; ++i)
1827 cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1828 oacc_pragmas[i].id, true, true);
1831 if (flag_openmp)
1833 const int n_omp_pragmas = ARRAY_SIZE (omp_pragmas);
1834 int i;
1836 for (i = 0; i < n_omp_pragmas; ++i)
1837 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1838 omp_pragmas[i].id, true, true);
1840 if (flag_openmp || flag_openmp_simd)
1842 const int n_omp_pragmas_simd
1843 = sizeof (omp_pragmas_simd) / sizeof (*omp_pragmas);
1844 int i;
1846 for (i = 0; i < n_omp_pragmas_simd; ++i)
1847 cpp_register_deferred_pragma (parse_in, "omp",
1848 omp_pragmas_simd[i].name,
1849 omp_pragmas_simd[i].id, true, true);
1853 if (!flag_preprocess_only)
1854 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1855 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1857 if (!flag_preprocess_only)
1858 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1859 false);
1861 if (!flag_preprocess_only)
1862 cpp_register_deferred_pragma (parse_in, "GCC", "unroll", PRAGMA_UNROLL,
1863 false, false);
1865 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1866 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1867 #else
1868 c_register_pragma (0, "pack", handle_pragma_pack);
1869 #endif
1870 c_register_pragma (0, "weak", handle_pragma_weak);
1872 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1874 if (flag_preprocess_only)
1875 c_register_pragma_with_early_handler ("GCC", "diagnostic",
1876 nullptr,
1877 handle_pragma_diagnostic_early_pp);
1878 else
1879 c_register_pragma_with_early_handler ("GCC", "diagnostic",
1880 handle_pragma_diagnostic,
1881 handle_pragma_diagnostic_early);
1882 c_register_pragma ("GCC", "target", handle_pragma_target);
1883 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1884 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1885 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1886 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1888 c_register_pragma (0, "region", handle_pragma_ignore);
1889 c_register_pragma (0, "endregion", handle_pragma_ignore);
1891 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1892 handle_pragma_float_const_decimal64);
1894 c_register_pragma_with_expansion (0, "redefine_extname",
1895 handle_pragma_redefine_extname);
1897 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1899 #ifdef REGISTER_TARGET_PRAGMAS
1900 REGISTER_TARGET_PRAGMAS ();
1901 #endif
1903 global_sso = default_sso;
1904 c_register_pragma (0, "scalar_storage_order",
1905 handle_pragma_scalar_storage_order);
1907 /* Allow plugins to register their own pragmas. */
1908 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1911 #include "gt-c-family-c-pragma.h"