1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2018 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
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
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/>. */
22 #include "coretypes.h"
24 #include "function.h" /* For cfun. */
27 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS. */
28 #include "stringpool.h"
30 #include "diagnostic.h"
37 #define GCC_BAD(gmsgid) \
38 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
39 #define GCC_BAD2(gmsgid, arg) \
40 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
42 struct GTY(()) align_stack {
45 struct align_stack * prev;
48 static GTY(()) struct align_stack * alignment_stack;
50 static void handle_pragma_pack (cpp_reader *);
52 /* If we have a "global" #pragma pack(<n>) in effect when the first
53 #pragma pack(push,<n>) is encountered, this stores the value of
54 maximum_field_alignment in effect. When the final pop_alignment()
55 happens, we restore the value to this, not to a value of 0 for
56 maximum_field_alignment. Value is in bits. */
57 static int default_alignment;
58 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
59 ? &default_alignment \
60 : &alignment_stack->alignment) = (ALIGN))
62 static void push_alignment (int, tree);
63 static void pop_alignment (tree);
65 /* Push an alignment value onto the stack. */
67 push_alignment (int alignment, tree id)
69 align_stack * entry = ggc_alloc<align_stack> ();
71 entry->alignment = alignment;
73 entry->prev = alignment_stack;
75 /* The current value of maximum_field_alignment is not necessarily
76 0 since there may be a #pragma pack(<n>) in effect; remember it
77 so that we can restore it after the final #pragma pop(). */
78 if (alignment_stack == NULL)
79 default_alignment = maximum_field_alignment;
81 alignment_stack = entry;
83 maximum_field_alignment = alignment;
86 /* Undo a push of an alignment onto the stack. */
88 pop_alignment (tree id)
92 if (alignment_stack == NULL)
93 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
95 /* If we got an identifier, strip away everything above the target
96 entry so that the next step will restore the state just below it. */
99 for (entry = alignment_stack; entry; entry = entry->prev)
102 alignment_stack = entry;
106 warning (OPT_Wpragmas, "\
107 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
111 entry = alignment_stack->prev;
113 maximum_field_alignment = entry ? entry->alignment : default_alignment;
115 alignment_stack = entry;
122 #pragma pack (push, N)
123 #pragma pack (push, ID)
124 #pragma pack (push, ID, N)
126 #pragma pack (pop, ID) */
128 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
132 enum cpp_ttype token;
133 enum { set, push, pop } action;
135 if (pragma_lex (&x) != CPP_OPEN_PAREN)
136 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
138 token = pragma_lex (&x);
139 if (token == CPP_CLOSE_PAREN)
142 align = initial_max_fld_align;
144 else if (token == CPP_NUMBER)
146 if (TREE_CODE (x) != INTEGER_CST)
147 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
148 align = TREE_INT_CST_LOW (x);
150 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
151 GCC_BAD ("malformed %<#pragma pack%> - ignored");
153 else if (token == CPP_NAME)
155 #define GCC_BAD_ACTION do { if (action != pop) \
156 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
158 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
161 const char *op = IDENTIFIER_POINTER (x);
162 if (!strcmp (op, "push"))
164 else if (!strcmp (op, "pop"))
167 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
169 while ((token = pragma_lex (&x)) == CPP_COMMA)
171 token = pragma_lex (&x);
172 if (token == CPP_NAME && id == 0)
176 else if (token == CPP_NUMBER && action == push && align == -1)
178 if (TREE_CODE (x) != INTEGER_CST)
179 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
180 align = TREE_INT_CST_LOW (x);
188 if (token != CPP_CLOSE_PAREN)
190 #undef GCC_BAD_ACTION
193 GCC_BAD ("malformed %<#pragma pack%> - ignored");
195 if (pragma_lex (&x) != CPP_EOF)
196 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
198 if (flag_pack_struct)
199 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
210 align *= BITS_PER_UNIT;
215 align = maximum_field_alignment;
220 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
225 case set: SET_GLOBAL_ALIGNMENT (align); break;
226 case push: push_alignment (align, id); break;
227 case pop: pop_alignment (id); break;
231 struct GTY(()) pending_weak
238 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
240 static void apply_pragma_weak (tree, tree);
241 static void handle_pragma_weak (cpp_reader *);
244 apply_pragma_weak (tree decl, tree value)
248 value = build_string (IDENTIFIER_LENGTH (value),
249 IDENTIFIER_POINTER (value));
250 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
251 build_tree_list (NULL, value)),
255 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
256 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
257 && DECL_ASSEMBLER_NAME_SET_P (decl)
258 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
259 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
260 "results in unspecified behavior", decl);
266 maybe_apply_pragma_weak (tree decl)
272 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
274 /* No weak symbols pending, take the short-cut. */
275 if (vec_safe_is_empty (pending_weaks))
277 /* If it's not visible outside this file, it doesn't matter whether
279 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
281 /* If it's not a function or a variable, it can't be weak.
282 FIXME: what kinds of things are visible outside this file but
283 aren't functions or variables? Should this be an assert instead? */
284 if (!VAR_OR_FUNCTION_DECL_P (decl))
287 if (DECL_ASSEMBLER_NAME_SET_P (decl))
288 id = DECL_ASSEMBLER_NAME (decl);
291 id = DECL_ASSEMBLER_NAME (decl);
292 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
295 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
298 apply_pragma_weak (decl, pe->value);
299 pending_weaks->unordered_remove (i);
304 /* Process all "#pragma weak A = B" directives where we have not seen
307 maybe_apply_pending_pragma_weaks (void)
309 tree alias_id, id, decl;
314 if (vec_safe_is_empty (pending_weaks))
317 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
325 target = symtab_node::get_for_asmname (id);
326 decl = build_decl (UNKNOWN_LOCATION,
327 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
328 alias_id, default_function_type);
330 DECL_ARTIFICIAL (decl) = 1;
331 TREE_PUBLIC (decl) = 1;
332 DECL_WEAK (decl) = 1;
334 TREE_STATIC (decl) = 1;
337 error ("%q+D aliased to undefined symbol %qE",
342 assemble_alias (decl, id);
346 /* #pragma weak name [= value] */
348 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
350 tree name, value, x, decl;
355 if (pragma_lex (&name) != CPP_NAME)
356 GCC_BAD ("malformed #pragma weak, ignored");
360 if (pragma_lex (&value) != CPP_NAME)
361 GCC_BAD ("malformed #pragma weak, ignored");
365 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
367 decl = identifier_global_value (name);
368 if (decl && DECL_P (decl))
370 if (!VAR_OR_FUNCTION_DECL_P (decl))
371 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
373 apply_pragma_weak (decl, value);
376 DECL_EXTERNAL (decl) = 0;
378 TREE_STATIC (decl) = 1;
379 assemble_alias (decl, value);
384 pending_weak pe = {name, value};
385 vec_safe_push (pending_weaks, pe);
389 static enum scalar_storage_order_kind global_sso;
392 maybe_apply_pragma_scalar_storage_order (tree type)
394 if (global_sso == SSO_NATIVE)
397 gcc_assert (RECORD_OR_UNION_TYPE_P (type));
399 if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type)))
402 if (global_sso == SSO_BIG_ENDIAN)
403 TYPE_REVERSE_STORAGE_ORDER (type) = !BYTES_BIG_ENDIAN;
404 else if (global_sso == SSO_LITTLE_ENDIAN)
405 TYPE_REVERSE_STORAGE_ORDER (type) = BYTES_BIG_ENDIAN;
411 handle_pragma_scalar_storage_order (cpp_reader *ARG_UNUSED(dummy))
413 const char *kind_string;
414 enum cpp_ttype token;
417 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
419 error ("scalar_storage_order is not supported because endianness "
424 if (c_dialect_cxx ())
426 if (warn_unknown_pragmas > in_system_header_at (input_location))
427 warning (OPT_Wunknown_pragmas,
428 "%<#pragma scalar_storage_order%> is not supported for C++");
432 token = pragma_lex (&x);
433 if (token != CPP_NAME)
434 GCC_BAD ("missing [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
435 kind_string = IDENTIFIER_POINTER (x);
436 if (strcmp (kind_string, "default") == 0)
437 global_sso = default_sso;
438 else if (strcmp (kind_string, "big") == 0)
439 global_sso = SSO_BIG_ENDIAN;
440 else if (strcmp (kind_string, "little") == 0)
441 global_sso = SSO_LITTLE_ENDIAN;
443 GCC_BAD ("expected [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
446 /* GCC supports two #pragma directives for renaming the external
447 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
448 compatibility with the Solaris and VMS system headers. GCC also
449 has its own notation for this, __asm__("name") annotations.
451 Corner cases of these features and their interaction:
453 1) Both pragmas silently apply only to declarations with external
454 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
455 do not have this restriction.
457 2) In C++, both #pragmas silently apply only to extern "C" declarations.
458 Asm labels do not have this restriction.
460 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
461 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
462 new name is different, a warning issues and the name does not change.
464 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
465 *not* the DECL_ASSEMBLER_NAME.
467 5) If #pragma extern_prefix is in effect and a declaration occurs
468 with an __asm__ name, the #pragma extern_prefix is silently
469 ignored for that declaration.
471 6) If #pragma extern_prefix and #pragma redefine_extname apply to
472 the same declaration, whichever triggered first wins, and a warning
473 is issued. (We would like to have #pragma redefine_extname always
474 win, but it can appear either before or after the declaration, and
475 if it appears afterward, we have no way of knowing whether a modified
476 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
478 struct GTY(()) pending_redefinition {
484 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
486 static void handle_pragma_redefine_extname (cpp_reader *);
488 /* #pragma redefine_extname oldname newname */
490 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
492 tree oldname, newname, decls, x;
496 if (pragma_lex (&oldname) != CPP_NAME)
497 GCC_BAD ("malformed #pragma redefine_extname, ignored");
498 if (pragma_lex (&newname) != CPP_NAME)
499 GCC_BAD ("malformed #pragma redefine_extname, ignored");
502 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
505 for (decls = c_linkage_bindings (oldname);
509 if (TREE_CODE (decls) == TREE_LIST)
511 decl = TREE_VALUE (decls);
512 decls = TREE_CHAIN (decls);
520 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
521 && VAR_OR_FUNCTION_DECL_P (decl))
524 if (DECL_ASSEMBLER_NAME_SET_P (decl))
526 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
527 name = targetm.strip_name_encoding (name);
529 if (!id_equal (newname, name))
530 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
531 "conflict with previous rename");
534 symtab->change_decl_assembler_name (decl, newname);
539 /* We have to add this to the rename list even if there's already
540 a global value that doesn't meet the above criteria, because in
541 C++ "struct foo {...};" puts "foo" in the current namespace but
542 does *not* conflict with a subsequent declaration of a function
543 or variable foo. See g++.dg/other/pragma-re-2.C. */
544 add_to_renaming_pragma_list (oldname, newname);
547 /* This is called from here and from ia64-c.c. */
549 add_to_renaming_pragma_list (tree oldname, tree newname)
552 pending_redefinition *p;
554 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
555 if (oldname == p->oldname)
557 if (p->newname != newname)
558 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
559 "conflict with previous #pragma redefine_extname");
563 pending_redefinition e = {oldname, newname};
564 vec_safe_push (pending_redefine_extname, e);
567 /* The current prefix set by #pragma extern_prefix. */
568 GTY(()) tree pragma_extern_prefix;
570 /* Hook from the front ends to apply the results of one of the preceding
571 pragmas that rename variables. */
574 maybe_apply_renaming_pragma (tree decl, tree asmname)
577 pending_redefinition *p;
579 /* The renaming pragmas are only applied to declarations with
581 if (!VAR_OR_FUNCTION_DECL_P (decl)
582 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
583 || !has_c_linkage (decl))
586 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
587 but we may warn about a rename that conflicts. */
588 if (DECL_ASSEMBLER_NAME_SET_P (decl))
590 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
591 oldname = targetm.strip_name_encoding (oldname);
593 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
594 warning (OPT_Wpragmas, "asm declaration ignored due to "
595 "conflict with previous rename");
597 /* Take any pending redefine_extname off the list. */
598 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
599 if (DECL_NAME (decl) == p->oldname)
601 /* Only warn if there is a conflict. */
602 if (!id_equal (p->newname, oldname))
603 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
604 "conflict with previous rename");
606 pending_redefine_extname->unordered_remove (ix);
612 /* Find out if we have a pending #pragma redefine_extname. */
613 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
614 if (DECL_NAME (decl) == p->oldname)
616 tree newname = p->newname;
617 pending_redefine_extname->unordered_remove (ix);
619 /* If we already have an asmname, #pragma redefine_extname is
620 ignored (with a warning if it conflicts). */
623 if (strcmp (TREE_STRING_POINTER (asmname),
624 IDENTIFIER_POINTER (newname)) != 0)
625 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
626 "conflict with __asm__ declaration");
630 /* Otherwise we use what we've got; #pragma extern_prefix is
632 return build_string (IDENTIFIER_LENGTH (newname),
633 IDENTIFIER_POINTER (newname));
636 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
640 /* If #pragma extern_prefix is in effect, apply it. */
641 if (pragma_extern_prefix)
643 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
644 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
646 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
647 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
649 char *newname = (char *) alloca (plen + ilen + 1);
651 memcpy (newname, prefix, plen);
652 memcpy (newname + plen, id, ilen + 1);
654 return build_string (plen + ilen, newname);
662 static void handle_pragma_visibility (cpp_reader *);
664 static vec<int> visstack;
666 /* Push the visibility indicated by STR onto the top of the #pragma
667 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
668 C++ namespace with visibility attribute and 2 for C++ builtin
669 ABI namespace. push_visibility/pop_visibility calls must have
670 matching KIND, it is not allowed to push visibility using one
671 KIND and pop using a different one. */
674 push_visibility (const char *str, int kind)
676 visstack.safe_push (((int) default_visibility) | (kind << 8));
677 if (!strcmp (str, "default"))
678 default_visibility = VISIBILITY_DEFAULT;
679 else if (!strcmp (str, "internal"))
680 default_visibility = VISIBILITY_INTERNAL;
681 else if (!strcmp (str, "hidden"))
682 default_visibility = VISIBILITY_HIDDEN;
683 else if (!strcmp (str, "protected"))
684 default_visibility = VISIBILITY_PROTECTED;
686 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
687 visibility_options.inpragma = 1;
690 /* Pop a level of the #pragma visibility stack. Return true if
694 pop_visibility (int kind)
696 if (!visstack.length ())
698 if ((visstack.last () >> 8) != kind)
701 = (enum symbol_visibility) (visstack.pop () & 0xff);
702 visibility_options.inpragma
703 = visstack.length () != 0;
707 /* Sets the default visibility for symbols to something other than that
708 specified on the command line. */
711 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
713 /* Form is #pragma GCC visibility push(hidden)|pop */
715 enum cpp_ttype token;
716 enum { bad, push, pop } action = bad;
718 token = pragma_lex (&x);
719 if (token == CPP_NAME)
721 const char *op = IDENTIFIER_POINTER (x);
722 if (!strcmp (op, "push"))
724 else if (!strcmp (op, "pop"))
728 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
733 if (! pop_visibility (0))
734 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
738 if (pragma_lex (&x) != CPP_OPEN_PAREN)
739 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
740 token = pragma_lex (&x);
741 if (token != CPP_NAME)
742 GCC_BAD ("malformed #pragma GCC visibility push");
744 push_visibility (IDENTIFIER_POINTER (x), 0);
745 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
746 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
749 if (pragma_lex (&x) != CPP_EOF)
750 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
754 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
758 enum cpp_ttype token = pragma_lex (&x, &loc);
759 if (token != CPP_NAME)
761 warning_at (loc, OPT_Wpragmas,
762 "missing [error|warning|ignored|push|pop]"
763 " after %<#pragma GCC diagnostic%>");
768 const char *kind_string = IDENTIFIER_POINTER (x);
769 if (strcmp (kind_string, "error") == 0)
771 else if (strcmp (kind_string, "warning") == 0)
773 else if (strcmp (kind_string, "ignored") == 0)
775 else if (strcmp (kind_string, "push") == 0)
777 diagnostic_push_diagnostics (global_dc, input_location);
780 else if (strcmp (kind_string, "pop") == 0)
782 diagnostic_pop_diagnostics (global_dc, input_location);
787 warning_at (loc, OPT_Wpragmas,
788 "expected [error|warning|ignored|push|pop]"
789 " after %<#pragma GCC diagnostic%>");
793 token = pragma_lex (&x, &loc);
794 if (token != CPP_STRING)
796 warning_at (loc, OPT_Wpragmas,
797 "missing option after %<#pragma GCC diagnostic%> kind");
801 const char *option_string = TREE_STRING_POINTER (x);
802 unsigned int lang_mask = c_common_option_lang_mask () | CL_COMMON;
803 /* option_string + 1 to skip the initial '-' */
804 unsigned int option_index = find_opt (option_string + 1, lang_mask);
805 if (option_index == OPT_SPECIAL_unknown)
807 warning_at (loc, OPT_Wpragmas,
808 "unknown option after %<#pragma GCC diagnostic%> kind");
811 else if (!(cl_options[option_index].flags & CL_WARNING))
813 warning_at (loc, OPT_Wpragmas,
814 "%qs is not an option that controls warnings", option_string);
817 else if (!(cl_options[option_index].flags & lang_mask))
819 char *ok_langs = write_langs (cl_options[option_index].flags);
820 char *bad_lang = write_langs (c_common_option_lang_mask ());
821 warning_at (loc, OPT_Wpragmas,
822 "option %qs is valid for %s but not for %s",
823 option_string, ok_langs, bad_lang);
829 struct cl_option_handlers handlers;
830 set_default_handlers (&handlers, NULL);
831 const char *arg = NULL;
832 if (cl_options[option_index].flags & CL_JOINED)
833 arg = option_string + 1 + cl_options[option_index].opt_len;
834 /* FIXME: input_location isn't the best location here, but it is
835 what we used to do here before and changing it breaks e.g.
836 PR69543 and PR69558. */
837 control_warning_option (option_index, (int) kind,
838 arg, kind != DK_IGNORED,
839 input_location, lang_mask, &handlers,
840 &global_options, &global_options_set,
844 /* Parse #pragma GCC target (xxx) to set target specific options. */
846 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
848 enum cpp_ttype token;
850 bool close_paren_needed_p = false;
854 error ("#pragma GCC option is not allowed inside functions");
858 token = pragma_lex (&x);
859 if (token == CPP_OPEN_PAREN)
861 close_paren_needed_p = true;
862 token = pragma_lex (&x);
865 if (token != CPP_STRING)
867 GCC_BAD ("%<#pragma GCC option%> is not a string");
871 /* Strings are user options. */
874 tree args = NULL_TREE;
878 /* Build up the strings now as a tree linked list. Skip empty
880 if (TREE_STRING_LENGTH (x) > 0)
881 args = tree_cons (NULL_TREE, x, args);
883 token = pragma_lex (&x);
884 while (token == CPP_COMMA)
885 token = pragma_lex (&x);
887 while (token == CPP_STRING);
889 if (close_paren_needed_p)
891 if (token == CPP_CLOSE_PAREN)
892 token = pragma_lex (&x);
894 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
895 "not have a final %<)%>");
898 if (token != CPP_EOF)
900 error ("#pragma GCC target string... is badly formed");
904 /* put arguments in the order the user typed them. */
905 args = nreverse (args);
907 if (targetm.target_option.pragma_parse (args, NULL_TREE))
908 current_target_pragma = chainon (current_target_pragma, args);
912 /* Handle #pragma GCC optimize to set optimization options. */
914 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
916 enum cpp_ttype token;
918 bool close_paren_needed_p = false;
919 tree optimization_previous_node = optimization_current_node;
923 error ("#pragma GCC optimize is not allowed inside functions");
927 token = pragma_lex (&x);
928 if (token == CPP_OPEN_PAREN)
930 close_paren_needed_p = true;
931 token = pragma_lex (&x);
934 if (token != CPP_STRING && token != CPP_NUMBER)
936 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
940 /* Strings/numbers are user options. */
943 tree args = NULL_TREE;
947 /* Build up the numbers/strings now as a list. */
948 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
949 args = tree_cons (NULL_TREE, x, args);
951 token = pragma_lex (&x);
952 while (token == CPP_COMMA)
953 token = pragma_lex (&x);
955 while (token == CPP_STRING || token == CPP_NUMBER);
957 if (close_paren_needed_p)
959 if (token == CPP_CLOSE_PAREN)
960 token = pragma_lex (&x);
962 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
963 "not have a final %<)%>");
966 if (token != CPP_EOF)
968 error ("#pragma GCC optimize string... is badly formed");
972 /* put arguments in the order the user typed them. */
973 args = nreverse (args);
975 parse_optimize_options (args, false);
976 current_optimize_pragma = chainon (current_optimize_pragma, args);
977 optimization_current_node = build_optimization_node (&global_options);
978 c_cpp_builtins_optimize_pragma (parse_in,
979 optimization_previous_node,
980 optimization_current_node);
984 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
985 both the binary representation of the options and the TREE_LIST of
986 strings that will be added to the function's attribute list. */
987 struct GTY(()) opt_stack {
988 struct opt_stack *prev;
991 tree optimize_binary;
992 tree optimize_strings;
995 static GTY(()) struct opt_stack * options_stack;
997 /* Handle #pragma GCC push_options to save the current target and optimization
1001 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1003 enum cpp_ttype token;
1006 token = pragma_lex (&x);
1007 if (token != CPP_EOF)
1009 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1013 opt_stack *p = ggc_alloc<opt_stack> ();
1014 p->prev = options_stack;
1017 /* Save optimization and target flags in binary format. */
1018 p->optimize_binary = build_optimization_node (&global_options);
1019 p->target_binary = build_target_option_node (&global_options);
1021 /* Save optimization and target flags in string list format. */
1022 p->optimize_strings = copy_list (current_optimize_pragma);
1023 p->target_strings = copy_list (current_target_pragma);
1026 /* Handle #pragma GCC pop_options to restore the current target and
1027 optimization options from a previous push_options. */
1030 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1032 enum cpp_ttype token;
1036 token = pragma_lex (&x);
1037 if (token != CPP_EOF)
1039 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1043 if (! options_stack)
1045 warning (OPT_Wpragmas,
1046 "%<#pragma GCC pop_options%> without a corresponding "
1047 "%<#pragma GCC push_options%>");
1052 options_stack = p->prev;
1054 if (p->target_binary != target_option_current_node)
1056 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1057 target_option_current_node = p->target_binary;
1060 if (p->optimize_binary != optimization_current_node)
1062 tree old_optimize = optimization_current_node;
1063 cl_optimization_restore (&global_options,
1064 TREE_OPTIMIZATION (p->optimize_binary));
1065 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
1066 p->optimize_binary);
1067 optimization_current_node = p->optimize_binary;
1070 current_target_pragma = p->target_strings;
1071 current_optimize_pragma = p->optimize_strings;
1074 /* Handle #pragma GCC reset_options to restore the current target and
1075 optimization options to the original options used on the command line. */
1078 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1080 enum cpp_ttype token;
1082 tree new_optimize = optimization_default_node;
1083 tree new_target = target_option_default_node;
1085 token = pragma_lex (&x);
1086 if (token != CPP_EOF)
1088 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1092 if (new_target != target_option_current_node)
1094 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1095 target_option_current_node = new_target;
1098 if (new_optimize != optimization_current_node)
1100 tree old_optimize = optimization_current_node;
1101 cl_optimization_restore (&global_options,
1102 TREE_OPTIMIZATION (new_optimize));
1103 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1104 optimization_current_node = new_optimize;
1107 current_target_pragma = NULL_TREE;
1108 current_optimize_pragma = NULL_TREE;
1111 /* Print a plain user-specified message. */
1114 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1116 enum cpp_ttype token;
1117 tree x, message = 0;
1119 token = pragma_lex (&x);
1120 if (token == CPP_OPEN_PAREN)
1122 token = pragma_lex (&x);
1123 if (token == CPP_STRING)
1126 GCC_BAD ("expected a string after %<#pragma message%>");
1127 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1128 GCC_BAD ("malformed %<#pragma message%>, ignored");
1130 else if (token == CPP_STRING)
1133 GCC_BAD ("expected a string after %<#pragma message%>");
1135 gcc_assert (message);
1137 if (pragma_lex (&x) != CPP_EOF)
1138 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1140 if (TREE_STRING_LENGTH (message) > 1)
1141 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1144 /* Mark whether the current location is valid for a STDC pragma. */
1146 static bool valid_location_for_stdc_pragma;
1149 mark_valid_location_for_stdc_pragma (bool flag)
1151 valid_location_for_stdc_pragma = flag;
1154 /* Return true if the current location is valid for a STDC pragma. */
1157 valid_location_for_stdc_pragma_p (void)
1159 return valid_location_for_stdc_pragma;
1162 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1164 /* A STDC pragma must appear outside of external declarations or
1165 preceding all explicit declarations and statements inside a compound
1166 statement; its behavior is undefined if used in any other context.
1167 It takes a switch of ON, OFF, or DEFAULT. */
1169 static enum pragma_switch_t
1170 handle_stdc_pragma (const char *pname)
1174 enum pragma_switch_t ret;
1176 if (!valid_location_for_stdc_pragma_p ())
1178 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1183 if (pragma_lex (&t) != CPP_NAME)
1185 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1189 arg = IDENTIFIER_POINTER (t);
1191 if (!strcmp (arg, "ON"))
1193 else if (!strcmp (arg, "OFF"))
1195 else if (!strcmp (arg, "DEFAULT"))
1196 ret = PRAGMA_DEFAULT;
1199 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1203 if (pragma_lex (&t) != CPP_EOF)
1205 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1212 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1213 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1214 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1217 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1219 if (c_dialect_cxx ())
1221 if (warn_unknown_pragmas > in_system_header_at (input_location))
1222 warning (OPT_Wunknown_pragmas,
1223 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1228 if (!targetm.decimal_float_supported_p ())
1230 if (warn_unknown_pragmas > in_system_header_at (input_location))
1231 warning (OPT_Wunknown_pragmas,
1232 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1237 pedwarn (input_location, OPT_Wpedantic,
1238 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1240 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1243 set_float_const_decimal64 ();
1246 case PRAGMA_DEFAULT:
1247 clear_float_const_decimal64 ();
1254 /* A vector of registered pragma callbacks, which is never freed. */
1256 static vec<internal_pragma_handler> registered_pragmas;
1258 struct pragma_ns_name
1265 static vec<pragma_ns_name> registered_pp_pragmas;
1267 struct omp_pragma_def { const char *name; unsigned int id; };
1268 static const struct omp_pragma_def oacc_pragmas[] = {
1269 { "atomic", PRAGMA_OACC_ATOMIC },
1270 { "cache", PRAGMA_OACC_CACHE },
1271 { "data", PRAGMA_OACC_DATA },
1272 { "declare", PRAGMA_OACC_DECLARE },
1273 { "enter", PRAGMA_OACC_ENTER_DATA },
1274 { "exit", PRAGMA_OACC_EXIT_DATA },
1275 { "host_data", PRAGMA_OACC_HOST_DATA },
1276 { "kernels", PRAGMA_OACC_KERNELS },
1277 { "loop", PRAGMA_OACC_LOOP },
1278 { "parallel", PRAGMA_OACC_PARALLEL },
1279 { "routine", PRAGMA_OACC_ROUTINE },
1280 { "update", PRAGMA_OACC_UPDATE },
1281 { "wait", PRAGMA_OACC_WAIT }
1283 static const struct omp_pragma_def omp_pragmas[] = {
1284 { "atomic", PRAGMA_OMP_ATOMIC },
1285 { "barrier", PRAGMA_OMP_BARRIER },
1286 { "cancel", PRAGMA_OMP_CANCEL },
1287 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1288 { "critical", PRAGMA_OMP_CRITICAL },
1289 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1290 { "flush", PRAGMA_OMP_FLUSH },
1291 { "master", PRAGMA_OMP_MASTER },
1292 { "section", PRAGMA_OMP_SECTION },
1293 { "sections", PRAGMA_OMP_SECTIONS },
1294 { "single", PRAGMA_OMP_SINGLE },
1295 { "task", PRAGMA_OMP_TASK },
1296 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1297 { "taskwait", PRAGMA_OMP_TASKWAIT },
1298 { "taskyield", PRAGMA_OMP_TASKYIELD },
1299 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1301 static const struct omp_pragma_def omp_pragmas_simd[] = {
1302 { "declare", PRAGMA_OMP_DECLARE },
1303 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1304 { "for", PRAGMA_OMP_FOR },
1305 { "ordered", PRAGMA_OMP_ORDERED },
1306 { "parallel", PRAGMA_OMP_PARALLEL },
1307 { "simd", PRAGMA_OMP_SIMD },
1308 { "target", PRAGMA_OMP_TARGET },
1309 { "taskloop", PRAGMA_OMP_TASKLOOP },
1310 { "teams", PRAGMA_OMP_TEAMS },
1314 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1316 const int n_oacc_pragmas = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1317 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1318 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1319 / sizeof (*omp_pragmas);
1322 for (i = 0; i < n_oacc_pragmas; ++i)
1323 if (oacc_pragmas[i].id == id)
1326 *name = oacc_pragmas[i].name;
1330 for (i = 0; i < n_omp_pragmas; ++i)
1331 if (omp_pragmas[i].id == id)
1334 *name = omp_pragmas[i].name;
1338 for (i = 0; i < n_omp_pragmas_simd; ++i)
1339 if (omp_pragmas_simd[i].id == id)
1342 *name = omp_pragmas_simd[i].name;
1346 if (id >= PRAGMA_FIRST_EXTERNAL
1347 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1349 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1350 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1357 /* Front-end wrappers for pragma registration to avoid dragging
1358 cpplib.h in almost everywhere. */
1361 c_register_pragma_1 (const char *space, const char *name,
1362 internal_pragma_handler ihandler, bool allow_expansion)
1366 if (flag_preprocess_only)
1368 pragma_ns_name ns_name;
1370 if (!allow_expansion)
1373 ns_name.space = space;
1374 ns_name.name = name;
1375 registered_pp_pragmas.safe_push (ns_name);
1376 id = registered_pp_pragmas.length ();
1377 id += PRAGMA_FIRST_EXTERNAL - 1;
1381 registered_pragmas.safe_push (ihandler);
1382 id = registered_pragmas.length ();
1383 id += PRAGMA_FIRST_EXTERNAL - 1;
1385 /* The C front end allocates 8 bits in c_token. The C++ front end
1386 keeps the pragma kind in the form of INTEGER_CST, so no small
1387 limit applies. At present this is sufficient. */
1388 gcc_assert (id < 256);
1391 cpp_register_deferred_pragma (parse_in, space, name, id,
1392 allow_expansion, false);
1395 /* Register a C pragma handler, using a space and a name. It disallows pragma
1396 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1398 c_register_pragma (const char *space, const char *name,
1399 pragma_handler_1arg handler)
1401 internal_pragma_handler ihandler;
1403 ihandler.handler.handler_1arg = handler;
1404 ihandler.extra_data = false;
1405 ihandler.data = NULL;
1406 c_register_pragma_1 (space, name, ihandler, false);
1409 /* Register a C pragma handler, using a space and a name, it also carries an
1410 extra data field which can be used by the handler. It disallows pragma
1411 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1414 c_register_pragma_with_data (const char *space, const char *name,
1415 pragma_handler_2arg handler, void * data)
1417 internal_pragma_handler ihandler;
1419 ihandler.handler.handler_2arg = handler;
1420 ihandler.extra_data = true;
1421 ihandler.data = data;
1422 c_register_pragma_1 (space, name, ihandler, false);
1425 /* Register a C pragma handler, using a space and a name. It allows pragma
1426 expansion as in the following example:
1429 #pragma count (NUMBER)
1431 Name expansion is still disallowed. */
1433 c_register_pragma_with_expansion (const char *space, const char *name,
1434 pragma_handler_1arg handler)
1436 internal_pragma_handler ihandler;
1438 ihandler.handler.handler_1arg = handler;
1439 ihandler.extra_data = false;
1440 ihandler.data = NULL;
1441 c_register_pragma_1 (space, name, ihandler, true);
1444 /* Register a C pragma handler, using a space and a name, it also carries an
1445 extra data field which can be used by the handler. It allows pragma
1446 expansion as in the following example:
1449 #pragma count (NUMBER)
1451 Name expansion is still disallowed. */
1453 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1454 pragma_handler_2arg handler,
1457 internal_pragma_handler ihandler;
1459 ihandler.handler.handler_2arg = handler;
1460 ihandler.extra_data = true;
1461 ihandler.data = data;
1462 c_register_pragma_1 (space, name, ihandler, true);
1466 c_invoke_pragma_handler (unsigned int id)
1468 internal_pragma_handler *ihandler;
1469 pragma_handler_1arg handler_1arg;
1470 pragma_handler_2arg handler_2arg;
1472 id -= PRAGMA_FIRST_EXTERNAL;
1473 ihandler = ®istered_pragmas[id];
1474 if (ihandler->extra_data)
1476 handler_2arg = ihandler->handler.handler_2arg;
1477 handler_2arg (parse_in, ihandler->data);
1481 handler_1arg = ihandler->handler.handler_1arg;
1482 handler_1arg (parse_in);
1486 /* Set up front-end pragmas. */
1492 const int n_oacc_pragmas
1493 = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1496 for (i = 0; i < n_oacc_pragmas; ++i)
1497 cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1498 oacc_pragmas[i].id, true, true);
1503 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1506 for (i = 0; i < n_omp_pragmas; ++i)
1507 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1508 omp_pragmas[i].id, true, true);
1510 if (flag_openmp || flag_openmp_simd)
1512 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1513 / sizeof (*omp_pragmas);
1516 for (i = 0; i < n_omp_pragmas_simd; ++i)
1517 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1518 omp_pragmas_simd[i].id, true, true);
1521 if (!flag_preprocess_only)
1522 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1523 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1525 if (!flag_preprocess_only)
1526 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1529 if (!flag_preprocess_only)
1530 cpp_register_deferred_pragma (parse_in, "GCC", "unroll", PRAGMA_UNROLL,
1533 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1534 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1536 c_register_pragma (0, "pack", handle_pragma_pack);
1538 c_register_pragma (0, "weak", handle_pragma_weak);
1540 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1542 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1543 c_register_pragma ("GCC", "target", handle_pragma_target);
1544 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1545 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1546 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1547 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1549 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1550 handle_pragma_float_const_decimal64);
1552 c_register_pragma_with_expansion (0, "redefine_extname",
1553 handle_pragma_redefine_extname);
1555 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1557 #ifdef REGISTER_TARGET_PRAGMAS
1558 REGISTER_TARGET_PRAGMAS ();
1561 global_sso = default_sso;
1562 c_register_pragma (0, "scalar_storage_order",
1563 handle_pragma_scalar_storage_order);
1565 /* Allow plugins to register their own pragmas. */
1566 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1569 #include "gt-c-family-c-pragma.h"