1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "function.h" /* For cfun. FIXME: Does the parser know
28 when it is inside a function, so that
29 we don't have to look at cfun? */
36 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
37 this not a target hook?). */
41 #include "diagnostic.h"
45 #define GCC_BAD(gmsgid) \
46 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
47 #define GCC_BAD2(gmsgid, arg) \
48 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
50 typedef struct GTY(()) align_stack {
53 struct align_stack * prev;
56 static GTY(()) struct align_stack * alignment_stack;
58 #ifdef HANDLE_PRAGMA_PACK
59 static void handle_pragma_pack (cpp_reader *);
61 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
62 /* If we have a "global" #pragma pack(<n>) in effect when the first
63 #pragma pack(push,<n>) is encountered, this stores the value of
64 maximum_field_alignment in effect. When the final pop_alignment()
65 happens, we restore the value to this, not to a value of 0 for
66 maximum_field_alignment. Value is in bits. */
67 static int default_alignment;
68 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
69 ? &default_alignment \
70 : &alignment_stack->alignment) = (ALIGN))
72 static void push_alignment (int, tree);
73 static void pop_alignment (tree);
75 /* Push an alignment value onto the stack. */
77 push_alignment (int alignment, tree id)
81 entry = GGC_NEW (align_stack);
83 entry->alignment = alignment;
85 entry->prev = alignment_stack;
87 /* The current value of maximum_field_alignment is not necessarily
88 0 since there may be a #pragma pack(<n>) in effect; remember it
89 so that we can restore it after the final #pragma pop(). */
90 if (alignment_stack == NULL)
91 default_alignment = maximum_field_alignment;
93 alignment_stack = entry;
95 maximum_field_alignment = alignment;
98 /* Undo a push of an alignment onto the stack. */
100 pop_alignment (tree id)
104 if (alignment_stack == NULL)
105 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
107 /* If we got an identifier, strip away everything above the target
108 entry so that the next step will restore the state just below it. */
111 for (entry = alignment_stack; entry; entry = entry->prev)
114 alignment_stack = entry;
118 warning (OPT_Wpragmas, "\
119 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
123 entry = alignment_stack->prev;
125 maximum_field_alignment = entry ? entry->alignment : default_alignment;
127 alignment_stack = entry;
129 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
130 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
131 #define push_alignment(ID, N) \
132 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
133 #define pop_alignment(ID) \
134 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
135 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
141 #pragma pack (push, N)
142 #pragma pack (push, ID)
143 #pragma pack (push, ID, N)
145 #pragma pack (pop, ID) */
147 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
151 enum cpp_ttype token;
152 enum { set, push, pop } action;
154 if (pragma_lex (&x) != CPP_OPEN_PAREN)
155 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
157 token = pragma_lex (&x);
158 if (token == CPP_CLOSE_PAREN)
161 align = initial_max_fld_align;
163 else if (token == CPP_NUMBER)
165 if (TREE_CODE (x) != INTEGER_CST)
166 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
167 align = TREE_INT_CST_LOW (x);
169 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
170 GCC_BAD ("malformed %<#pragma pack%> - ignored");
172 else if (token == CPP_NAME)
174 #define GCC_BAD_ACTION do { if (action != pop) \
175 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
177 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
180 const char *op = IDENTIFIER_POINTER (x);
181 if (!strcmp (op, "push"))
183 else if (!strcmp (op, "pop"))
186 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
188 while ((token = pragma_lex (&x)) == CPP_COMMA)
190 token = pragma_lex (&x);
191 if (token == CPP_NAME && id == 0)
195 else if (token == CPP_NUMBER && action == push && align == -1)
197 if (TREE_CODE (x) != INTEGER_CST)
198 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
199 align = TREE_INT_CST_LOW (x);
207 if (token != CPP_CLOSE_PAREN)
209 #undef GCC_BAD_ACTION
212 GCC_BAD ("malformed %<#pragma pack%> - ignored");
214 if (pragma_lex (&x) != CPP_EOF)
215 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
217 if (flag_pack_struct)
218 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
229 align *= BITS_PER_UNIT;
234 align = maximum_field_alignment;
238 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
243 case set: SET_GLOBAL_ALIGNMENT (align); break;
244 case push: push_alignment (align, id); break;
245 case pop: pop_alignment (id); break;
248 #endif /* HANDLE_PRAGMA_PACK */
250 typedef struct GTY(()) pending_weak_d
256 DEF_VEC_O(pending_weak);
257 DEF_VEC_ALLOC_O(pending_weak,gc);
259 static GTY(()) VEC(pending_weak,gc) *pending_weaks;
261 #ifdef HANDLE_PRAGMA_WEAK
262 static void apply_pragma_weak (tree, tree);
263 static void handle_pragma_weak (cpp_reader *);
266 apply_pragma_weak (tree decl, tree value)
270 value = build_string (IDENTIFIER_LENGTH (value),
271 IDENTIFIER_POINTER (value));
272 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
273 build_tree_list (NULL, value)),
277 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
278 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
279 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
280 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
281 "results in unspecified behavior", decl);
287 maybe_apply_pragma_weak (tree decl)
293 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
295 /* No weak symbols pending, take the short-cut. */
298 /* If it's not visible outside this file, it doesn't matter whether
300 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
302 /* If it's not a function or a variable, it can't be weak.
303 FIXME: what kinds of things are visible outside this file but
304 aren't functions or variables? Should this be an assert instead? */
305 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
308 id = DECL_ASSEMBLER_NAME (decl);
310 for (i = 0; VEC_iterate (pending_weak, pending_weaks, i, pe); i++)
313 apply_pragma_weak (decl, pe->value);
314 VEC_unordered_remove (pending_weak, pending_weaks, i);
319 /* Process all "#pragma weak A = B" directives where we have not seen
322 maybe_apply_pending_pragma_weaks (void)
324 tree alias_id, id, decl;
328 for (i = 0; VEC_iterate (pending_weak, pending_weaks, i, pe); i++)
336 decl = build_decl (UNKNOWN_LOCATION,
337 FUNCTION_DECL, alias_id, default_function_type);
339 DECL_ARTIFICIAL (decl) = 1;
340 TREE_PUBLIC (decl) = 1;
341 DECL_EXTERNAL (decl) = 1;
342 DECL_WEAK (decl) = 1;
344 assemble_alias (decl, id);
348 /* #pragma weak name [= value] */
350 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
352 tree name, value, x, decl;
357 if (pragma_lex (&name) != CPP_NAME)
358 GCC_BAD ("malformed #pragma weak, ignored");
362 if (pragma_lex (&value) != CPP_NAME)
363 GCC_BAD ("malformed #pragma weak, ignored");
367 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
369 decl = identifier_global_value (name);
370 if (decl && DECL_P (decl))
372 apply_pragma_weak (decl, value);
374 assemble_alias (decl, value);
379 pe = VEC_safe_push (pending_weak, gc, pending_weaks, NULL);
386 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
391 maybe_apply_pending_pragma_weaks (void)
394 #endif /* HANDLE_PRAGMA_WEAK */
396 /* GCC supports two #pragma directives for renaming the external
397 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
398 compatibility with the Solaris and Tru64 system headers. GCC also
399 has its own notation for this, __asm__("name") annotations.
401 Corner cases of these features and their interaction:
403 1) Both pragmas silently apply only to declarations with external
404 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
405 do not have this restriction.
407 2) In C++, both #pragmas silently apply only to extern "C" declarations.
408 Asm labels do not have this restriction.
410 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
411 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
412 new name is different, a warning issues and the name does not change.
414 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
415 *not* the DECL_ASSEMBLER_NAME.
417 5) If #pragma extern_prefix is in effect and a declaration occurs
418 with an __asm__ name, the #pragma extern_prefix is silently
419 ignored for that declaration.
421 6) If #pragma extern_prefix and #pragma redefine_extname apply to
422 the same declaration, whichever triggered first wins, and a warning
423 is issued. (We would like to have #pragma redefine_extname always
424 win, but it can appear either before or after the declaration, and
425 if it appears afterward, we have no way of knowing whether a modified
426 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
428 static GTY(()) tree pending_redefine_extname;
430 static void handle_pragma_redefine_extname (cpp_reader *);
432 /* #pragma redefine_extname oldname newname */
434 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
436 tree oldname, newname, decl, x;
439 if (pragma_lex (&oldname) != CPP_NAME)
440 GCC_BAD ("malformed #pragma redefine_extname, ignored");
441 if (pragma_lex (&newname) != CPP_NAME)
442 GCC_BAD ("malformed #pragma redefine_extname, ignored");
445 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
447 decl = identifier_global_value (oldname);
449 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
450 && (TREE_CODE (decl) == FUNCTION_DECL
451 || TREE_CODE (decl) == VAR_DECL)
452 && has_c_linkage (decl))
454 if (DECL_ASSEMBLER_NAME_SET_P (decl))
456 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
457 name = targetm.strip_name_encoding (name);
459 if (strcmp (name, IDENTIFIER_POINTER (newname)))
460 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
461 "conflict with previous rename");
464 change_decl_assembler_name (decl, newname);
467 /* We have to add this to the rename list even if there's already
468 a global value that doesn't meet the above criteria, because in
469 C++ "struct foo {...};" puts "foo" in the current namespace but
470 does *not* conflict with a subsequent declaration of a function
471 or variable foo. See g++.dg/other/pragma-re-2.C. */
472 add_to_renaming_pragma_list (oldname, newname);
475 /* This is called from here and from ia64.c. */
477 add_to_renaming_pragma_list (tree oldname, tree newname)
479 tree previous = purpose_member (oldname, pending_redefine_extname);
482 if (TREE_VALUE (previous) != newname)
483 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
484 "conflict with previous #pragma redefine_extname");
488 pending_redefine_extname
489 = tree_cons (oldname, newname, pending_redefine_extname);
492 static GTY(()) tree pragma_extern_prefix;
494 /* #pragma extern_prefix "prefix" */
496 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
501 if (pragma_lex (&prefix) != CPP_STRING)
502 GCC_BAD ("malformed #pragma extern_prefix, ignored");
505 warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
507 if (targetm.handle_pragma_extern_prefix)
508 /* Note that the length includes the null terminator. */
509 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
510 else if (warn_unknown_pragmas > in_system_header)
511 warning (OPT_Wunknown_pragmas,
512 "#pragma extern_prefix not supported on this target");
515 /* Hook from the front ends to apply the results of one of the preceding
516 pragmas that rename variables. */
519 maybe_apply_renaming_pragma (tree decl, tree asmname)
523 /* The renaming pragmas are only applied to declarations with
525 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
526 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
527 || !has_c_linkage (decl))
530 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
531 but we may warn about a rename that conflicts. */
532 if (DECL_ASSEMBLER_NAME_SET_P (decl))
534 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
535 oldname = targetm.strip_name_encoding (oldname);
537 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
538 warning (OPT_Wpragmas, "asm declaration ignored due to "
539 "conflict with previous rename");
541 /* Take any pending redefine_extname off the list. */
542 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
543 if (DECL_NAME (decl) == TREE_PURPOSE (t))
545 /* Only warn if there is a conflict. */
546 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
547 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
548 "conflict with previous rename");
556 /* Find out if we have a pending #pragma redefine_extname. */
557 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
558 if (DECL_NAME (decl) == TREE_PURPOSE (t))
560 tree newname = TREE_VALUE (t);
563 /* If we already have an asmname, #pragma redefine_extname is
564 ignored (with a warning if it conflicts). */
567 if (strcmp (TREE_STRING_POINTER (asmname),
568 IDENTIFIER_POINTER (newname)) != 0)
569 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
570 "conflict with __asm__ declaration");
574 /* Otherwise we use what we've got; #pragma extern_prefix is
576 return build_string (IDENTIFIER_LENGTH (newname),
577 IDENTIFIER_POINTER (newname));
580 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
584 /* If #pragma extern_prefix is in effect, apply it. */
585 if (pragma_extern_prefix)
587 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
588 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
590 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
591 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
593 char *newname = (char *) alloca (plen + ilen + 1);
595 memcpy (newname, prefix, plen);
596 memcpy (newname + plen, id, ilen + 1);
598 return build_string (plen + ilen, newname);
606 #ifdef HANDLE_PRAGMA_VISIBILITY
607 static void handle_pragma_visibility (cpp_reader *);
609 static VEC (int, heap) *visstack;
611 /* Push the visibility indicated by STR onto the top of the #pragma
612 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
613 C++ namespace with visibility attribute and 2 for C++ builtin
614 ABI namespace. push_visibility/pop_visibility calls must have
615 matching KIND, it is not allowed to push visibility using one
616 KIND and pop using a different one. */
619 push_visibility (const char *str, int kind)
621 VEC_safe_push (int, heap, visstack,
622 ((int) default_visibility) | (kind << 8));
623 if (!strcmp (str, "default"))
624 default_visibility = VISIBILITY_DEFAULT;
625 else if (!strcmp (str, "internal"))
626 default_visibility = VISIBILITY_INTERNAL;
627 else if (!strcmp (str, "hidden"))
628 default_visibility = VISIBILITY_HIDDEN;
629 else if (!strcmp (str, "protected"))
630 default_visibility = VISIBILITY_PROTECTED;
632 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
633 visibility_options.inpragma = 1;
636 /* Pop a level of the #pragma visibility stack. Return true if
640 pop_visibility (int kind)
642 if (!VEC_length (int, visstack))
644 if ((VEC_last (int, visstack) >> 8) != kind)
647 = (enum symbol_visibility) (VEC_pop (int, visstack) & 0xff);
648 visibility_options.inpragma
649 = VEC_length (int, visstack) != 0;
653 /* Sets the default visibility for symbols to something other than that
654 specified on the command line. */
657 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
659 /* Form is #pragma GCC visibility push(hidden)|pop */
661 enum cpp_ttype token;
662 enum { bad, push, pop } action = bad;
664 token = pragma_lex (&x);
665 if (token == CPP_NAME)
667 const char *op = IDENTIFIER_POINTER (x);
668 if (!strcmp (op, "push"))
670 else if (!strcmp (op, "pop"))
674 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
679 if (! pop_visibility (0))
680 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
684 if (pragma_lex (&x) != CPP_OPEN_PAREN)
685 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
686 token = pragma_lex (&x);
687 if (token != CPP_NAME)
688 GCC_BAD ("malformed #pragma GCC visibility push");
690 push_visibility (IDENTIFIER_POINTER (x), 0);
691 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
692 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
695 if (pragma_lex (&x) != CPP_EOF)
696 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
702 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
704 const char *kind_string, *option_string;
705 unsigned int option_index;
706 enum cpp_ttype token;
712 error ("#pragma GCC diagnostic not allowed inside functions");
716 token = pragma_lex (&x);
717 if (token != CPP_NAME)
718 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
719 kind_string = IDENTIFIER_POINTER (x);
720 if (strcmp (kind_string, "error") == 0)
722 else if (strcmp (kind_string, "warning") == 0)
724 else if (strcmp (kind_string, "ignored") == 0)
727 GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
729 token = pragma_lex (&x);
730 if (token != CPP_STRING)
731 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
732 option_string = TREE_STRING_POINTER (x);
733 for (option_index = 0; option_index < cl_options_count; option_index++)
734 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
736 /* This overrides -Werror, for example. */
737 diagnostic_classify_diagnostic (global_dc, option_index, kind);
738 /* This makes sure the option is enabled, like -Wfoo would do. */
739 if (cl_options[option_index].var_type == CLVC_BOOLEAN
740 && cl_options[option_index].flag_var
741 && kind != DK_IGNORED)
742 *(int *) cl_options[option_index].flag_var = 1;
745 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
748 /* Parse #pragma GCC target (xxx) to set target specific options. */
750 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
752 enum cpp_ttype token;
754 bool close_paren_needed_p = false;
758 error ("#pragma GCC option is not allowed inside functions");
762 token = pragma_lex (&x);
763 if (token == CPP_OPEN_PAREN)
765 close_paren_needed_p = true;
766 token = pragma_lex (&x);
769 if (token != CPP_STRING)
771 GCC_BAD ("%<#pragma GCC option%> is not a string");
775 /* Strings are user options. */
778 tree args = NULL_TREE;
782 /* Build up the strings now as a tree linked list. Skip empty
784 if (TREE_STRING_LENGTH (x) > 0)
785 args = tree_cons (NULL_TREE, x, args);
787 token = pragma_lex (&x);
788 while (token == CPP_COMMA)
789 token = pragma_lex (&x);
791 while (token == CPP_STRING);
793 if (close_paren_needed_p)
795 if (token == CPP_CLOSE_PAREN)
796 token = pragma_lex (&x);
798 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
799 "not have a final %<)%>.");
802 if (token != CPP_EOF)
804 error ("#pragma GCC target string... is badly formed");
808 /* put arguments in the order the user typed them. */
809 args = nreverse (args);
811 if (targetm.target_option.pragma_parse (args, NULL_TREE))
812 current_target_pragma = args;
816 /* Handle #pragma GCC optimize to set optimization options. */
818 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
820 enum cpp_ttype token;
822 bool close_paren_needed_p = false;
823 tree optimization_previous_node = optimization_current_node;
827 error ("#pragma GCC optimize is not allowed inside functions");
831 token = pragma_lex (&x);
832 if (token == CPP_OPEN_PAREN)
834 close_paren_needed_p = true;
835 token = pragma_lex (&x);
838 if (token != CPP_STRING && token != CPP_NUMBER)
840 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
844 /* Strings/numbers are user options. */
847 tree args = NULL_TREE;
851 /* Build up the numbers/strings now as a list. */
852 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
853 args = tree_cons (NULL_TREE, x, args);
855 token = pragma_lex (&x);
856 while (token == CPP_COMMA)
857 token = pragma_lex (&x);
859 while (token == CPP_STRING || token == CPP_NUMBER);
861 if (close_paren_needed_p)
863 if (token == CPP_CLOSE_PAREN)
864 token = pragma_lex (&x);
866 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
867 "not have a final %<)%>.");
870 if (token != CPP_EOF)
872 error ("#pragma GCC optimize string... is badly formed");
876 /* put arguments in the order the user typed them. */
877 args = nreverse (args);
879 parse_optimize_options (args, false);
880 current_optimize_pragma = chainon (current_optimize_pragma, args);
881 optimization_current_node = build_optimization_node ();
882 c_cpp_builtins_optimize_pragma (parse_in,
883 optimization_previous_node,
884 optimization_current_node);
888 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
889 both the binary representation of the options and the TREE_LIST of
890 strings that will be added to the function's attribute list. */
891 typedef struct GTY(()) opt_stack {
892 struct opt_stack *prev;
895 tree optimize_binary;
896 tree optimize_strings;
899 static GTY(()) struct opt_stack * options_stack;
901 /* Handle #pragma GCC push_options to save the current target and optimization
905 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
907 enum cpp_ttype token;
911 token = pragma_lex (&x);
912 if (token != CPP_EOF)
914 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
918 p = GGC_NEW (opt_stack);
919 p->prev = options_stack;
922 /* Save optimization and target flags in binary format. */
923 p->optimize_binary = build_optimization_node ();
924 p->target_binary = build_target_option_node ();
926 /* Save optimization and target flags in string list format. */
927 p->optimize_strings = copy_list (current_optimize_pragma);
928 p->target_strings = copy_list (current_target_pragma);
931 /* Handle #pragma GCC pop_options to restore the current target and
932 optimization options from a previous push_options. */
935 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
937 enum cpp_ttype token;
941 token = pragma_lex (&x);
942 if (token != CPP_EOF)
944 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
950 warning (OPT_Wpragmas,
951 "%<#pragma GCC pop_options%> without a corresponding "
952 "%<#pragma GCC push_options%>");
957 options_stack = p->prev;
959 if (p->target_binary != target_option_current_node)
961 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
962 target_option_current_node = p->target_binary;
965 if (p->optimize_binary != optimization_current_node)
967 tree old_optimize = optimization_current_node;
968 cl_optimization_restore (TREE_OPTIMIZATION (p->optimize_binary));
969 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
971 optimization_current_node = p->optimize_binary;
974 current_target_pragma = p->target_strings;
975 current_optimize_pragma = p->optimize_strings;
978 /* Handle #pragma GCC reset_options to restore the current target and
979 optimization options to the original options used on the command line. */
982 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
984 enum cpp_ttype token;
986 tree new_optimize = optimization_default_node;
987 tree new_target = target_option_default_node;
989 token = pragma_lex (&x);
990 if (token != CPP_EOF)
992 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
996 if (new_target != target_option_current_node)
998 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
999 target_option_current_node = new_target;
1002 if (new_optimize != optimization_current_node)
1004 tree old_optimize = optimization_current_node;
1005 cl_optimization_restore (TREE_OPTIMIZATION (new_optimize));
1006 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1007 optimization_current_node = new_optimize;
1010 current_target_pragma = NULL_TREE;
1011 current_optimize_pragma = NULL_TREE;
1014 /* Print a plain user-specified message. */
1017 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1019 enum cpp_ttype token;
1020 tree x, message = 0;
1022 token = pragma_lex (&x);
1023 if (token == CPP_OPEN_PAREN)
1025 token = pragma_lex (&x);
1026 if (token == CPP_STRING)
1029 GCC_BAD ("expected a string after %<#pragma message%>");
1030 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1031 GCC_BAD ("malformed %<#pragma message%>, ignored");
1033 else if (token == CPP_STRING)
1036 GCC_BAD ("expected a string after %<#pragma message%>");
1038 gcc_assert (message);
1040 if (pragma_lex (&x) != CPP_EOF)
1041 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1043 if (TREE_STRING_LENGTH (message) > 1)
1044 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1047 /* Mark whether the current location is valid for a STDC pragma. */
1049 static bool valid_location_for_stdc_pragma;
1052 mark_valid_location_for_stdc_pragma (bool flag)
1054 valid_location_for_stdc_pragma = flag;
1057 /* Return true if the current location is valid for a STDC pragma. */
1060 valid_location_for_stdc_pragma_p (void)
1062 return valid_location_for_stdc_pragma;
1065 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1067 /* A STDC pragma must appear outside of external declarations or
1068 preceding all explicit declarations and statements inside a compound
1069 statement; its behavior is undefined if used in any other context.
1070 It takes a switch of ON, OFF, or DEFAULT. */
1072 static enum pragma_switch_t
1073 handle_stdc_pragma (const char *pname)
1077 enum pragma_switch_t ret;
1079 if (!valid_location_for_stdc_pragma_p ())
1081 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1086 if (pragma_lex (&t) != CPP_NAME)
1088 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1092 arg = IDENTIFIER_POINTER (t);
1094 if (!strcmp (arg, "ON"))
1096 else if (!strcmp (arg, "OFF"))
1098 else if (!strcmp (arg, "DEFAULT"))
1099 ret = PRAGMA_DEFAULT;
1102 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1106 if (pragma_lex (&t) != CPP_EOF)
1108 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1115 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1116 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1117 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1120 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1122 if (c_dialect_cxx ())
1124 if (warn_unknown_pragmas > in_system_header)
1125 warning (OPT_Wunknown_pragmas,
1126 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1131 if (!targetm.decimal_float_supported_p ())
1133 if (warn_unknown_pragmas > in_system_header)
1134 warning (OPT_Wunknown_pragmas,
1135 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1140 pedwarn (input_location, OPT_pedantic,
1141 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1143 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1146 set_float_const_decimal64 ();
1149 case PRAGMA_DEFAULT:
1150 clear_float_const_decimal64 ();
1157 /* A vector of registered pragma callbacks. */
1159 DEF_VEC_O (pragma_handler);
1160 DEF_VEC_ALLOC_O (pragma_handler, heap);
1162 static VEC(pragma_handler, heap) *registered_pragmas;
1170 DEF_VEC_O (pragma_ns_name);
1171 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
1173 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
1175 struct omp_pragma_def { const char *name; unsigned int id; };
1176 static const struct omp_pragma_def omp_pragmas[] = {
1177 { "atomic", PRAGMA_OMP_ATOMIC },
1178 { "barrier", PRAGMA_OMP_BARRIER },
1179 { "critical", PRAGMA_OMP_CRITICAL },
1180 { "flush", PRAGMA_OMP_FLUSH },
1181 { "for", PRAGMA_OMP_FOR },
1182 { "master", PRAGMA_OMP_MASTER },
1183 { "ordered", PRAGMA_OMP_ORDERED },
1184 { "parallel", PRAGMA_OMP_PARALLEL },
1185 { "section", PRAGMA_OMP_SECTION },
1186 { "sections", PRAGMA_OMP_SECTIONS },
1187 { "single", PRAGMA_OMP_SINGLE },
1188 { "task", PRAGMA_OMP_TASK },
1189 { "taskwait", PRAGMA_OMP_TASKWAIT },
1190 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1194 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1196 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1199 for (i = 0; i < n_omp_pragmas; ++i)
1200 if (omp_pragmas[i].id == id)
1203 *name = omp_pragmas[i].name;
1207 if (id >= PRAGMA_FIRST_EXTERNAL
1208 && (id < PRAGMA_FIRST_EXTERNAL
1209 + VEC_length (pragma_ns_name, registered_pp_pragmas)))
1211 *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
1212 id - PRAGMA_FIRST_EXTERNAL)->space;
1213 *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
1214 id - PRAGMA_FIRST_EXTERNAL)->name;
1221 /* Front-end wrappers for pragma registration to avoid dragging
1222 cpplib.h in almost everywhere. */
1225 c_register_pragma_1 (const char *space, const char *name,
1226 pragma_handler handler, bool allow_expansion)
1230 if (flag_preprocess_only)
1232 pragma_ns_name ns_name;
1234 if (!allow_expansion)
1237 ns_name.space = space;
1238 ns_name.name = name;
1239 VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
1240 id = VEC_length (pragma_ns_name, registered_pp_pragmas);
1241 id += PRAGMA_FIRST_EXTERNAL - 1;
1245 VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
1246 id = VEC_length (pragma_handler, registered_pragmas);
1247 id += PRAGMA_FIRST_EXTERNAL - 1;
1249 /* The C++ front end allocates 6 bits in cp_token; the C front end
1250 allocates 7 bits in c_token. At present this is sufficient. */
1251 gcc_assert (id < 64);
1254 cpp_register_deferred_pragma (parse_in, space, name, id,
1255 allow_expansion, false);
1259 c_register_pragma (const char *space, const char *name, pragma_handler handler)
1261 c_register_pragma_1 (space, name, handler, false);
1265 c_register_pragma_with_expansion (const char *space, const char *name,
1266 pragma_handler handler)
1268 c_register_pragma_1 (space, name, handler, true);
1272 c_invoke_pragma_handler (unsigned int id)
1274 pragma_handler handler;
1276 id -= PRAGMA_FIRST_EXTERNAL;
1277 handler = *VEC_index (pragma_handler, registered_pragmas, id);
1282 /* Set up front-end pragmas. */
1288 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1291 for (i = 0; i < n_omp_pragmas; ++i)
1292 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1293 omp_pragmas[i].id, true, true);
1296 if (!flag_preprocess_only)
1297 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1298 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1300 #ifdef HANDLE_PRAGMA_PACK
1301 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1302 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1304 c_register_pragma (0, "pack", handle_pragma_pack);
1307 #ifdef HANDLE_PRAGMA_WEAK
1308 c_register_pragma (0, "weak", handle_pragma_weak);
1310 #ifdef HANDLE_PRAGMA_VISIBILITY
1311 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1314 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1315 c_register_pragma ("GCC", "target", handle_pragma_target);
1316 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1317 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1318 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1319 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1321 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1322 handle_pragma_float_const_decimal64);
1324 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1325 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1327 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1329 #ifdef REGISTER_TARGET_PRAGMAS
1330 REGISTER_TARGET_PRAGMAS ();
1333 /* Allow plugins to register their own pragmas. */
1334 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1337 #include "gt-c-pragma.h"