1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2015 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"
27 #include "double-int.h"
35 #include "stringpool.h"
42 #include "hard-reg-set.h"
44 #include "function.h" /* For cfun. FIXME: Does the parser know
45 when it is inside a function, so that
46 we don't have to look at cfun? */
51 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
52 this not a target hook?). */
54 #include "diagnostic.h"
59 #include "plugin-api.h"
63 #define GCC_BAD(gmsgid) \
64 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
65 #define GCC_BAD2(gmsgid, arg) \
66 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
68 typedef struct GTY(()) align_stack {
71 struct align_stack * prev;
74 static GTY(()) struct align_stack * alignment_stack;
76 static void handle_pragma_pack (cpp_reader *);
78 /* If we have a "global" #pragma pack(<n>) in effect when the first
79 #pragma pack(push,<n>) is encountered, this stores the value of
80 maximum_field_alignment in effect. When the final pop_alignment()
81 happens, we restore the value to this, not to a value of 0 for
82 maximum_field_alignment. Value is in bits. */
83 static int default_alignment;
84 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
85 ? &default_alignment \
86 : &alignment_stack->alignment) = (ALIGN))
88 static void push_alignment (int, tree);
89 static void pop_alignment (tree);
91 /* Push an alignment value onto the stack. */
93 push_alignment (int alignment, tree id)
95 align_stack * entry = ggc_alloc<align_stack> ();
97 entry->alignment = alignment;
99 entry->prev = alignment_stack;
101 /* The current value of maximum_field_alignment is not necessarily
102 0 since there may be a #pragma pack(<n>) in effect; remember it
103 so that we can restore it after the final #pragma pop(). */
104 if (alignment_stack == NULL)
105 default_alignment = maximum_field_alignment;
107 alignment_stack = entry;
109 maximum_field_alignment = alignment;
112 /* Undo a push of an alignment onto the stack. */
114 pop_alignment (tree id)
118 if (alignment_stack == NULL)
119 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
121 /* If we got an identifier, strip away everything above the target
122 entry so that the next step will restore the state just below it. */
125 for (entry = alignment_stack; entry; entry = entry->prev)
128 alignment_stack = entry;
132 warning (OPT_Wpragmas, "\
133 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
137 entry = alignment_stack->prev;
139 maximum_field_alignment = entry ? entry->alignment : default_alignment;
141 alignment_stack = entry;
148 #pragma pack (push, N)
149 #pragma pack (push, ID)
150 #pragma pack (push, ID, N)
152 #pragma pack (pop, ID) */
154 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
158 enum cpp_ttype token;
159 enum { set, push, pop } action;
161 if (pragma_lex (&x) != CPP_OPEN_PAREN)
162 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
164 token = pragma_lex (&x);
165 if (token == CPP_CLOSE_PAREN)
168 align = initial_max_fld_align;
170 else if (token == CPP_NUMBER)
172 if (TREE_CODE (x) != INTEGER_CST)
173 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
174 align = TREE_INT_CST_LOW (x);
176 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
177 GCC_BAD ("malformed %<#pragma pack%> - ignored");
179 else if (token == CPP_NAME)
181 #define GCC_BAD_ACTION do { if (action != pop) \
182 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
184 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
187 const char *op = IDENTIFIER_POINTER (x);
188 if (!strcmp (op, "push"))
190 else if (!strcmp (op, "pop"))
193 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
195 while ((token = pragma_lex (&x)) == CPP_COMMA)
197 token = pragma_lex (&x);
198 if (token == CPP_NAME && id == 0)
202 else if (token == CPP_NUMBER && action == push && align == -1)
204 if (TREE_CODE (x) != INTEGER_CST)
205 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
206 align = TREE_INT_CST_LOW (x);
214 if (token != CPP_CLOSE_PAREN)
216 #undef GCC_BAD_ACTION
219 GCC_BAD ("malformed %<#pragma pack%> - ignored");
221 if (pragma_lex (&x) != CPP_EOF)
222 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
224 if (flag_pack_struct)
225 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
236 align *= BITS_PER_UNIT;
241 align = maximum_field_alignment;
245 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
250 case set: SET_GLOBAL_ALIGNMENT (align); break;
251 case push: push_alignment (align, id); break;
252 case pop: pop_alignment (id); break;
256 typedef struct GTY(()) pending_weak_d
263 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
265 static void apply_pragma_weak (tree, tree);
266 static void handle_pragma_weak (cpp_reader *);
269 apply_pragma_weak (tree decl, tree value)
273 value = build_string (IDENTIFIER_LENGTH (value),
274 IDENTIFIER_POINTER (value));
275 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
276 build_tree_list (NULL, value)),
280 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
281 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
282 && DECL_ASSEMBLER_NAME_SET_P (decl)
283 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
284 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
285 "results in unspecified behavior", decl);
291 maybe_apply_pragma_weak (tree decl)
297 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
299 /* No weak symbols pending, take the short-cut. */
300 if (vec_safe_is_empty (pending_weaks))
302 /* If it's not visible outside this file, it doesn't matter whether
304 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
306 /* If it's not a function or a variable, it can't be weak.
307 FIXME: what kinds of things are visible outside this file but
308 aren't functions or variables? Should this be an assert instead? */
309 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
312 if (DECL_ASSEMBLER_NAME_SET_P (decl))
313 id = DECL_ASSEMBLER_NAME (decl);
316 id = DECL_ASSEMBLER_NAME (decl);
317 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
320 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
323 apply_pragma_weak (decl, pe->value);
324 pending_weaks->unordered_remove (i);
329 /* Process all "#pragma weak A = B" directives where we have not seen
332 maybe_apply_pending_pragma_weaks (void)
334 tree alias_id, id, decl;
339 if (vec_safe_is_empty (pending_weaks))
342 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
350 target = symtab_node::get_for_asmname (id);
351 decl = build_decl (UNKNOWN_LOCATION,
352 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
353 alias_id, default_function_type);
355 DECL_ARTIFICIAL (decl) = 1;
356 TREE_PUBLIC (decl) = 1;
357 DECL_WEAK (decl) = 1;
358 if (TREE_CODE (decl) == VAR_DECL)
359 TREE_STATIC (decl) = 1;
362 error ("%q+D aliased to undefined symbol %qE",
367 assemble_alias (decl, id);
371 /* #pragma weak name [= value] */
373 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
375 tree name, value, x, decl;
380 if (pragma_lex (&name) != CPP_NAME)
381 GCC_BAD ("malformed #pragma weak, ignored");
385 if (pragma_lex (&value) != CPP_NAME)
386 GCC_BAD ("malformed #pragma weak, ignored");
390 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
392 decl = identifier_global_value (name);
393 if (decl && DECL_P (decl))
395 apply_pragma_weak (decl, value);
398 DECL_EXTERNAL (decl) = 0;
399 if (TREE_CODE (decl) == VAR_DECL)
400 TREE_STATIC (decl) = 1;
401 assemble_alias (decl, value);
406 pending_weak pe = {name, value};
407 vec_safe_push (pending_weaks, pe);
411 /* GCC supports two #pragma directives for renaming the external
412 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
413 compatibility with the Solaris and VMS system headers. GCC also
414 has its own notation for this, __asm__("name") annotations.
416 Corner cases of these features and their interaction:
418 1) Both pragmas silently apply only to declarations with external
419 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
420 do not have this restriction.
422 2) In C++, both #pragmas silently apply only to extern "C" declarations.
423 Asm labels do not have this restriction.
425 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
426 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
427 new name is different, a warning issues and the name does not change.
429 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
430 *not* the DECL_ASSEMBLER_NAME.
432 5) If #pragma extern_prefix is in effect and a declaration occurs
433 with an __asm__ name, the #pragma extern_prefix is silently
434 ignored for that declaration.
436 6) If #pragma extern_prefix and #pragma redefine_extname apply to
437 the same declaration, whichever triggered first wins, and a warning
438 is issued. (We would like to have #pragma redefine_extname always
439 win, but it can appear either before or after the declaration, and
440 if it appears afterward, we have no way of knowing whether a modified
441 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
443 typedef struct GTY(()) pending_redefinition_d {
446 } pending_redefinition;
449 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
451 static void handle_pragma_redefine_extname (cpp_reader *);
453 /* #pragma redefine_extname oldname newname */
455 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
457 tree oldname, newname, decls, x;
461 if (pragma_lex (&oldname) != CPP_NAME)
462 GCC_BAD ("malformed #pragma redefine_extname, ignored");
463 if (pragma_lex (&newname) != CPP_NAME)
464 GCC_BAD ("malformed #pragma redefine_extname, ignored");
467 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
470 for (decls = c_linkage_bindings (oldname);
474 if (TREE_CODE (decls) == TREE_LIST)
476 decl = TREE_VALUE (decls);
477 decls = TREE_CHAIN (decls);
485 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
486 && (TREE_CODE (decl) == FUNCTION_DECL
487 || TREE_CODE (decl) == VAR_DECL))
490 if (DECL_ASSEMBLER_NAME_SET_P (decl))
492 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
493 name = targetm.strip_name_encoding (name);
495 if (strcmp (name, IDENTIFIER_POINTER (newname)))
496 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
497 "conflict with previous rename");
500 symtab->change_decl_assembler_name (decl, newname);
505 /* We have to add this to the rename list even if there's already
506 a global value that doesn't meet the above criteria, because in
507 C++ "struct foo {...};" puts "foo" in the current namespace but
508 does *not* conflict with a subsequent declaration of a function
509 or variable foo. See g++.dg/other/pragma-re-2.C. */
510 add_to_renaming_pragma_list (oldname, newname);
513 /* This is called from here and from ia64-c.c. */
515 add_to_renaming_pragma_list (tree oldname, tree newname)
518 pending_redefinition *p;
520 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
521 if (oldname == p->oldname)
523 if (p->newname != newname)
524 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
525 "conflict with previous #pragma redefine_extname");
529 pending_redefinition e = {oldname, newname};
530 vec_safe_push (pending_redefine_extname, e);
533 /* The current prefix set by #pragma extern_prefix. */
534 GTY(()) tree pragma_extern_prefix;
536 /* Hook from the front ends to apply the results of one of the preceding
537 pragmas that rename variables. */
540 maybe_apply_renaming_pragma (tree decl, tree asmname)
543 pending_redefinition *p;
545 /* The renaming pragmas are only applied to declarations with
547 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
548 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
549 || !has_c_linkage (decl))
552 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
553 but we may warn about a rename that conflicts. */
554 if (DECL_ASSEMBLER_NAME_SET_P (decl))
556 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
557 oldname = targetm.strip_name_encoding (oldname);
559 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
560 warning (OPT_Wpragmas, "asm declaration ignored due to "
561 "conflict with previous rename");
563 /* Take any pending redefine_extname off the list. */
564 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
565 if (DECL_NAME (decl) == p->oldname)
567 /* Only warn if there is a conflict. */
568 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
569 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
570 "conflict with previous rename");
572 pending_redefine_extname->unordered_remove (ix);
578 /* Find out if we have a pending #pragma redefine_extname. */
579 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
580 if (DECL_NAME (decl) == p->oldname)
582 tree newname = p->newname;
583 pending_redefine_extname->unordered_remove (ix);
585 /* If we already have an asmname, #pragma redefine_extname is
586 ignored (with a warning if it conflicts). */
589 if (strcmp (TREE_STRING_POINTER (asmname),
590 IDENTIFIER_POINTER (newname)) != 0)
591 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
592 "conflict with __asm__ declaration");
596 /* Otherwise we use what we've got; #pragma extern_prefix is
598 return build_string (IDENTIFIER_LENGTH (newname),
599 IDENTIFIER_POINTER (newname));
602 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
606 /* If #pragma extern_prefix is in effect, apply it. */
607 if (pragma_extern_prefix)
609 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
610 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
612 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
613 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
615 char *newname = (char *) alloca (plen + ilen + 1);
617 memcpy (newname, prefix, plen);
618 memcpy (newname + plen, id, ilen + 1);
620 return build_string (plen + ilen, newname);
628 static void handle_pragma_visibility (cpp_reader *);
630 static vec<int> visstack;
632 /* Push the visibility indicated by STR onto the top of the #pragma
633 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
634 C++ namespace with visibility attribute and 2 for C++ builtin
635 ABI namespace. push_visibility/pop_visibility calls must have
636 matching KIND, it is not allowed to push visibility using one
637 KIND and pop using a different one. */
640 push_visibility (const char *str, int kind)
642 visstack.safe_push (((int) default_visibility) | (kind << 8));
643 if (!strcmp (str, "default"))
644 default_visibility = VISIBILITY_DEFAULT;
645 else if (!strcmp (str, "internal"))
646 default_visibility = VISIBILITY_INTERNAL;
647 else if (!strcmp (str, "hidden"))
648 default_visibility = VISIBILITY_HIDDEN;
649 else if (!strcmp (str, "protected"))
650 default_visibility = VISIBILITY_PROTECTED;
652 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
653 visibility_options.inpragma = 1;
656 /* Pop a level of the #pragma visibility stack. Return true if
660 pop_visibility (int kind)
662 if (!visstack.length ())
664 if ((visstack.last () >> 8) != kind)
667 = (enum symbol_visibility) (visstack.pop () & 0xff);
668 visibility_options.inpragma
669 = visstack.length () != 0;
673 /* Sets the default visibility for symbols to something other than that
674 specified on the command line. */
677 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
679 /* Form is #pragma GCC visibility push(hidden)|pop */
681 enum cpp_ttype token;
682 enum { bad, push, pop } action = bad;
684 token = pragma_lex (&x);
685 if (token == CPP_NAME)
687 const char *op = IDENTIFIER_POINTER (x);
688 if (!strcmp (op, "push"))
690 else if (!strcmp (op, "pop"))
694 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
699 if (! pop_visibility (0))
700 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
704 if (pragma_lex (&x) != CPP_OPEN_PAREN)
705 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
706 token = pragma_lex (&x);
707 if (token != CPP_NAME)
708 GCC_BAD ("malformed #pragma GCC visibility push");
710 push_visibility (IDENTIFIER_POINTER (x), 0);
711 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
712 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
715 if (pragma_lex (&x) != CPP_EOF)
716 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
720 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
722 const char *kind_string, *option_string;
723 unsigned int option_index;
724 enum cpp_ttype token;
727 struct cl_option_handlers handlers;
729 token = pragma_lex (&x);
730 if (token != CPP_NAME)
731 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
732 kind_string = IDENTIFIER_POINTER (x);
733 if (strcmp (kind_string, "error") == 0)
735 else if (strcmp (kind_string, "warning") == 0)
737 else if (strcmp (kind_string, "ignored") == 0)
739 else if (strcmp (kind_string, "push") == 0)
741 diagnostic_push_diagnostics (global_dc, input_location);
744 else if (strcmp (kind_string, "pop") == 0)
746 diagnostic_pop_diagnostics (global_dc, input_location);
750 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
752 token = pragma_lex (&x);
753 if (token != CPP_STRING)
754 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
755 option_string = TREE_STRING_POINTER (x);
756 set_default_handlers (&handlers);
757 for (option_index = 0; option_index < cl_options_count; option_index++)
758 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
760 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
761 input_location, c_family_lang_mask, &handlers,
762 &global_options, &global_options_set,
766 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
769 /* Parse #pragma GCC target (xxx) to set target specific options. */
771 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
773 enum cpp_ttype token;
775 bool close_paren_needed_p = false;
779 error ("#pragma GCC option is not allowed inside functions");
783 token = pragma_lex (&x);
784 if (token == CPP_OPEN_PAREN)
786 close_paren_needed_p = true;
787 token = pragma_lex (&x);
790 if (token != CPP_STRING)
792 GCC_BAD ("%<#pragma GCC option%> is not a string");
796 /* Strings are user options. */
799 tree args = NULL_TREE;
803 /* Build up the strings now as a tree linked list. Skip empty
805 if (TREE_STRING_LENGTH (x) > 0)
806 args = tree_cons (NULL_TREE, x, args);
808 token = pragma_lex (&x);
809 while (token == CPP_COMMA)
810 token = pragma_lex (&x);
812 while (token == CPP_STRING);
814 if (close_paren_needed_p)
816 if (token == CPP_CLOSE_PAREN)
817 token = pragma_lex (&x);
819 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
820 "not have a final %<)%>");
823 if (token != CPP_EOF)
825 error ("#pragma GCC target string... is badly formed");
829 /* put arguments in the order the user typed them. */
830 args = nreverse (args);
832 if (targetm.target_option.pragma_parse (args, NULL_TREE))
833 current_target_pragma = args;
837 /* Handle #pragma GCC optimize to set optimization options. */
839 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
841 enum cpp_ttype token;
843 bool close_paren_needed_p = false;
844 tree optimization_previous_node = optimization_current_node;
848 error ("#pragma GCC optimize is not allowed inside functions");
852 token = pragma_lex (&x);
853 if (token == CPP_OPEN_PAREN)
855 close_paren_needed_p = true;
856 token = pragma_lex (&x);
859 if (token != CPP_STRING && token != CPP_NUMBER)
861 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
865 /* Strings/numbers are user options. */
868 tree args = NULL_TREE;
872 /* Build up the numbers/strings now as a list. */
873 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
874 args = tree_cons (NULL_TREE, x, args);
876 token = pragma_lex (&x);
877 while (token == CPP_COMMA)
878 token = pragma_lex (&x);
880 while (token == CPP_STRING || token == CPP_NUMBER);
882 if (close_paren_needed_p)
884 if (token == CPP_CLOSE_PAREN)
885 token = pragma_lex (&x);
887 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
888 "not have a final %<)%>");
891 if (token != CPP_EOF)
893 error ("#pragma GCC optimize string... is badly formed");
897 /* put arguments in the order the user typed them. */
898 args = nreverse (args);
900 parse_optimize_options (args, false);
901 current_optimize_pragma = chainon (current_optimize_pragma, args);
902 optimization_current_node = build_optimization_node (&global_options);
903 c_cpp_builtins_optimize_pragma (parse_in,
904 optimization_previous_node,
905 optimization_current_node);
909 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
910 both the binary representation of the options and the TREE_LIST of
911 strings that will be added to the function's attribute list. */
912 typedef struct GTY(()) opt_stack {
913 struct opt_stack *prev;
916 tree optimize_binary;
917 tree optimize_strings;
920 static GTY(()) struct opt_stack * options_stack;
922 /* Handle #pragma GCC push_options to save the current target and optimization
926 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
928 enum cpp_ttype token;
931 token = pragma_lex (&x);
932 if (token != CPP_EOF)
934 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
938 opt_stack *p = ggc_alloc<opt_stack> ();
939 p->prev = options_stack;
942 /* Save optimization and target flags in binary format. */
943 p->optimize_binary = build_optimization_node (&global_options);
944 p->target_binary = build_target_option_node (&global_options);
946 /* Save optimization and target flags in string list format. */
947 p->optimize_strings = copy_list (current_optimize_pragma);
948 p->target_strings = copy_list (current_target_pragma);
951 /* Handle #pragma GCC pop_options to restore the current target and
952 optimization options from a previous push_options. */
955 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
957 enum cpp_ttype token;
961 token = pragma_lex (&x);
962 if (token != CPP_EOF)
964 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
970 warning (OPT_Wpragmas,
971 "%<#pragma GCC pop_options%> without a corresponding "
972 "%<#pragma GCC push_options%>");
977 options_stack = p->prev;
979 if (p->target_binary != target_option_current_node)
981 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
982 target_option_current_node = p->target_binary;
985 if (p->optimize_binary != optimization_current_node)
987 tree old_optimize = optimization_current_node;
988 cl_optimization_restore (&global_options,
989 TREE_OPTIMIZATION (p->optimize_binary));
990 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
992 optimization_current_node = p->optimize_binary;
995 current_target_pragma = p->target_strings;
996 current_optimize_pragma = p->optimize_strings;
999 /* Handle #pragma GCC reset_options to restore the current target and
1000 optimization options to the original options used on the command line. */
1003 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1005 enum cpp_ttype token;
1007 tree new_optimize = optimization_default_node;
1008 tree new_target = target_option_default_node;
1010 token = pragma_lex (&x);
1011 if (token != CPP_EOF)
1013 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1017 if (new_target != target_option_current_node)
1019 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1020 target_option_current_node = new_target;
1023 if (new_optimize != optimization_current_node)
1025 tree old_optimize = optimization_current_node;
1026 cl_optimization_restore (&global_options,
1027 TREE_OPTIMIZATION (new_optimize));
1028 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1029 optimization_current_node = new_optimize;
1032 current_target_pragma = NULL_TREE;
1033 current_optimize_pragma = NULL_TREE;
1036 /* Print a plain user-specified message. */
1039 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1041 enum cpp_ttype token;
1042 tree x, message = 0;
1044 token = pragma_lex (&x);
1045 if (token == CPP_OPEN_PAREN)
1047 token = pragma_lex (&x);
1048 if (token == CPP_STRING)
1051 GCC_BAD ("expected a string after %<#pragma message%>");
1052 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1053 GCC_BAD ("malformed %<#pragma message%>, ignored");
1055 else if (token == CPP_STRING)
1058 GCC_BAD ("expected a string after %<#pragma message%>");
1060 gcc_assert (message);
1062 if (pragma_lex (&x) != CPP_EOF)
1063 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1065 if (TREE_STRING_LENGTH (message) > 1)
1066 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1069 /* Mark whether the current location is valid for a STDC pragma. */
1071 static bool valid_location_for_stdc_pragma;
1074 mark_valid_location_for_stdc_pragma (bool flag)
1076 valid_location_for_stdc_pragma = flag;
1079 /* Return true if the current location is valid for a STDC pragma. */
1082 valid_location_for_stdc_pragma_p (void)
1084 return valid_location_for_stdc_pragma;
1087 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1089 /* A STDC pragma must appear outside of external declarations or
1090 preceding all explicit declarations and statements inside a compound
1091 statement; its behavior is undefined if used in any other context.
1092 It takes a switch of ON, OFF, or DEFAULT. */
1094 static enum pragma_switch_t
1095 handle_stdc_pragma (const char *pname)
1099 enum pragma_switch_t ret;
1101 if (!valid_location_for_stdc_pragma_p ())
1103 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1108 if (pragma_lex (&t) != CPP_NAME)
1110 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1114 arg = IDENTIFIER_POINTER (t);
1116 if (!strcmp (arg, "ON"))
1118 else if (!strcmp (arg, "OFF"))
1120 else if (!strcmp (arg, "DEFAULT"))
1121 ret = PRAGMA_DEFAULT;
1124 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1128 if (pragma_lex (&t) != CPP_EOF)
1130 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1137 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1138 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1139 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1142 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1144 if (c_dialect_cxx ())
1146 if (warn_unknown_pragmas > in_system_header_at (input_location))
1147 warning (OPT_Wunknown_pragmas,
1148 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1153 if (!targetm.decimal_float_supported_p ())
1155 if (warn_unknown_pragmas > in_system_header_at (input_location))
1156 warning (OPT_Wunknown_pragmas,
1157 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1162 pedwarn (input_location, OPT_Wpedantic,
1163 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1165 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1168 set_float_const_decimal64 ();
1171 case PRAGMA_DEFAULT:
1172 clear_float_const_decimal64 ();
1179 /* A vector of registered pragma callbacks, which is never freed. */
1181 static vec<internal_pragma_handler> registered_pragmas;
1190 static vec<pragma_ns_name> registered_pp_pragmas;
1192 struct omp_pragma_def { const char *name; unsigned int id; };
1193 static const struct omp_pragma_def oacc_pragmas[] = {
1194 { "cache", PRAGMA_OACC_CACHE },
1195 { "data", PRAGMA_OACC_DATA },
1196 { "enter", PRAGMA_OACC_ENTER_DATA },
1197 { "exit", PRAGMA_OACC_EXIT_DATA },
1198 { "kernels", PRAGMA_OACC_KERNELS },
1199 { "loop", PRAGMA_OACC_LOOP },
1200 { "parallel", PRAGMA_OACC_PARALLEL },
1201 { "update", PRAGMA_OACC_UPDATE },
1202 { "wait", PRAGMA_OACC_WAIT }
1204 static const struct omp_pragma_def omp_pragmas[] = {
1205 { "atomic", PRAGMA_OMP_ATOMIC },
1206 { "barrier", PRAGMA_OMP_BARRIER },
1207 { "cancel", PRAGMA_OMP_CANCEL },
1208 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1209 { "critical", PRAGMA_OMP_CRITICAL },
1210 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1211 { "flush", PRAGMA_OMP_FLUSH },
1212 { "master", PRAGMA_OMP_MASTER },
1213 { "ordered", PRAGMA_OMP_ORDERED },
1214 { "section", PRAGMA_OMP_SECTION },
1215 { "sections", PRAGMA_OMP_SECTIONS },
1216 { "single", PRAGMA_OMP_SINGLE },
1217 { "task", PRAGMA_OMP_TASK },
1218 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1219 { "taskwait", PRAGMA_OMP_TASKWAIT },
1220 { "taskyield", PRAGMA_OMP_TASKYIELD },
1221 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1223 static const struct omp_pragma_def omp_pragmas_simd[] = {
1224 { "declare", PRAGMA_OMP_DECLARE_REDUCTION },
1225 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1226 { "for", PRAGMA_OMP_FOR },
1227 { "parallel", PRAGMA_OMP_PARALLEL },
1228 { "simd", PRAGMA_OMP_SIMD },
1229 { "target", PRAGMA_OMP_TARGET },
1230 { "teams", PRAGMA_OMP_TEAMS },
1234 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1236 const int n_oacc_pragmas = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1237 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1238 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1239 / sizeof (*omp_pragmas);
1242 for (i = 0; i < n_oacc_pragmas; ++i)
1243 if (oacc_pragmas[i].id == id)
1246 *name = oacc_pragmas[i].name;
1250 for (i = 0; i < n_omp_pragmas; ++i)
1251 if (omp_pragmas[i].id == id)
1254 *name = omp_pragmas[i].name;
1258 for (i = 0; i < n_omp_pragmas_simd; ++i)
1259 if (omp_pragmas_simd[i].id == id)
1262 *name = omp_pragmas_simd[i].name;
1266 if (id == PRAGMA_CILK_SIMD)
1273 if (id >= PRAGMA_FIRST_EXTERNAL
1274 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1276 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1277 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1284 /* Front-end wrappers for pragma registration to avoid dragging
1285 cpplib.h in almost everywhere. */
1288 c_register_pragma_1 (const char *space, const char *name,
1289 internal_pragma_handler ihandler, bool allow_expansion)
1293 if (flag_preprocess_only)
1295 pragma_ns_name ns_name;
1297 if (!allow_expansion)
1300 ns_name.space = space;
1301 ns_name.name = name;
1302 registered_pp_pragmas.safe_push (ns_name);
1303 id = registered_pp_pragmas.length ();
1304 id += PRAGMA_FIRST_EXTERNAL - 1;
1308 registered_pragmas.safe_push (ihandler);
1309 id = registered_pragmas.length ();
1310 id += PRAGMA_FIRST_EXTERNAL - 1;
1312 /* The C++ front end allocates 6 bits in cp_token; the C front end
1313 allocates 7 bits in c_token. At present this is sufficient. */
1314 gcc_assert (id < 64);
1317 cpp_register_deferred_pragma (parse_in, space, name, id,
1318 allow_expansion, false);
1321 /* Register a C pragma handler, using a space and a name. It disallows pragma
1322 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1324 c_register_pragma (const char *space, const char *name,
1325 pragma_handler_1arg handler)
1327 internal_pragma_handler ihandler;
1329 ihandler.handler.handler_1arg = handler;
1330 ihandler.extra_data = false;
1331 ihandler.data = NULL;
1332 c_register_pragma_1 (space, name, ihandler, false);
1335 /* Register a C pragma handler, using a space and a name, it also carries an
1336 extra data field which can be used by the handler. It disallows pragma
1337 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1340 c_register_pragma_with_data (const char *space, const char *name,
1341 pragma_handler_2arg handler, void * data)
1343 internal_pragma_handler ihandler;
1345 ihandler.handler.handler_2arg = handler;
1346 ihandler.extra_data = true;
1347 ihandler.data = data;
1348 c_register_pragma_1 (space, name, ihandler, false);
1351 /* Register a C pragma handler, using a space and a name. It allows pragma
1352 expansion as in the following example:
1355 #pragma count (NUMBER)
1357 Name expansion is still disallowed. */
1359 c_register_pragma_with_expansion (const char *space, const char *name,
1360 pragma_handler_1arg handler)
1362 internal_pragma_handler ihandler;
1364 ihandler.handler.handler_1arg = handler;
1365 ihandler.extra_data = false;
1366 ihandler.data = NULL;
1367 c_register_pragma_1 (space, name, ihandler, true);
1370 /* Register a C pragma handler, using a space and a name, it also carries an
1371 extra data field which can be used by the handler. It allows pragma
1372 expansion as in the following example:
1375 #pragma count (NUMBER)
1377 Name expansion is still disallowed. */
1379 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1380 pragma_handler_2arg handler,
1383 internal_pragma_handler ihandler;
1385 ihandler.handler.handler_2arg = handler;
1386 ihandler.extra_data = true;
1387 ihandler.data = data;
1388 c_register_pragma_1 (space, name, ihandler, true);
1392 c_invoke_pragma_handler (unsigned int id)
1394 internal_pragma_handler *ihandler;
1395 pragma_handler_1arg handler_1arg;
1396 pragma_handler_2arg handler_2arg;
1398 id -= PRAGMA_FIRST_EXTERNAL;
1399 ihandler = ®istered_pragmas[id];
1400 if (ihandler->extra_data)
1402 handler_2arg = ihandler->handler.handler_2arg;
1403 handler_2arg (parse_in, ihandler->data);
1407 handler_1arg = ihandler->handler.handler_1arg;
1408 handler_1arg (parse_in);
1412 /* Set up front-end pragmas. */
1418 const int n_oacc_pragmas
1419 = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1422 for (i = 0; i < n_oacc_pragmas; ++i)
1423 cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1424 oacc_pragmas[i].id, true, true);
1429 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1432 for (i = 0; i < n_omp_pragmas; ++i)
1433 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1434 omp_pragmas[i].id, true, true);
1436 if (flag_openmp || flag_openmp_simd)
1438 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1439 / sizeof (*omp_pragmas);
1442 for (i = 0; i < n_omp_pragmas_simd; ++i)
1443 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1444 omp_pragmas_simd[i].id, true, true);
1448 cpp_register_deferred_pragma (parse_in, NULL, "simd", PRAGMA_CILK_SIMD,
1451 if (!flag_preprocess_only)
1452 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1453 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1455 if (!flag_preprocess_only)
1456 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1459 if (flag_cilkplus && !flag_preprocess_only)
1460 cpp_register_deferred_pragma (parse_in, "cilk", "grainsize",
1461 PRAGMA_CILK_GRAINSIZE, true, false);
1463 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1464 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1466 c_register_pragma (0, "pack", handle_pragma_pack);
1468 c_register_pragma (0, "weak", handle_pragma_weak);
1469 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1471 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1472 c_register_pragma ("GCC", "target", handle_pragma_target);
1473 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1474 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1475 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1476 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1478 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1479 handle_pragma_float_const_decimal64);
1481 c_register_pragma_with_expansion (0, "redefine_extname",
1482 handle_pragma_redefine_extname);
1484 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1486 #ifdef REGISTER_TARGET_PRAGMAS
1487 REGISTER_TARGET_PRAGMAS ();
1490 /* Allow plugins to register their own pragmas. */
1491 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1494 #include "gt-c-family-c-pragma.h"