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"
38 #include "diagnostic.h"
41 #define GCC_BAD(gmsgid) \
42 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define GCC_BAD2(gmsgid, arg) \
44 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
46 typedef struct GTY(()) align_stack {
49 struct align_stack * prev;
52 static GTY(()) struct align_stack * alignment_stack;
54 #ifdef HANDLE_PRAGMA_PACK
55 static void handle_pragma_pack (cpp_reader *);
57 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
58 /* If we have a "global" #pragma pack(<n>) in effect when the first
59 #pragma pack(push,<n>) is encountered, this stores the value of
60 maximum_field_alignment in effect. When the final pop_alignment()
61 happens, we restore the value to this, not to a value of 0 for
62 maximum_field_alignment. Value is in bits. */
63 static int default_alignment;
64 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
65 ? &default_alignment \
66 : &alignment_stack->alignment) = (ALIGN))
68 static void push_alignment (int, tree);
69 static void pop_alignment (tree);
71 /* Push an alignment value onto the stack. */
73 push_alignment (int alignment, tree id)
77 entry = GGC_NEW (align_stack);
79 entry->alignment = alignment;
81 entry->prev = alignment_stack;
83 /* The current value of maximum_field_alignment is not necessarily
84 0 since there may be a #pragma pack(<n>) in effect; remember it
85 so that we can restore it after the final #pragma pop(). */
86 if (alignment_stack == NULL)
87 default_alignment = maximum_field_alignment;
89 alignment_stack = entry;
91 maximum_field_alignment = alignment;
94 /* Undo a push of an alignment onto the stack. */
96 pop_alignment (tree id)
100 if (alignment_stack == NULL)
101 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
103 /* If we got an identifier, strip away everything above the target
104 entry so that the next step will restore the state just below it. */
107 for (entry = alignment_stack; entry; entry = entry->prev)
110 alignment_stack = entry;
114 warning (OPT_Wpragmas, "\
115 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
119 entry = alignment_stack->prev;
121 maximum_field_alignment = entry ? entry->alignment : default_alignment;
123 alignment_stack = entry;
125 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
126 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
127 #define push_alignment(ID, N) \
128 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
129 #define pop_alignment(ID) \
130 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
131 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
137 #pragma pack (push, N)
138 #pragma pack (push, ID)
139 #pragma pack (push, ID, N)
141 #pragma pack (pop, ID) */
143 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
147 enum cpp_ttype token;
148 enum { set, push, pop } action;
150 if (pragma_lex (&x) != CPP_OPEN_PAREN)
151 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
153 token = pragma_lex (&x);
154 if (token == CPP_CLOSE_PAREN)
157 align = initial_max_fld_align;
159 else if (token == CPP_NUMBER)
161 if (TREE_CODE (x) != INTEGER_CST)
162 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
163 align = TREE_INT_CST_LOW (x);
165 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
166 GCC_BAD ("malformed %<#pragma pack%> - ignored");
168 else if (token == CPP_NAME)
170 #define GCC_BAD_ACTION do { if (action != pop) \
171 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
173 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
176 const char *op = IDENTIFIER_POINTER (x);
177 if (!strcmp (op, "push"))
179 else if (!strcmp (op, "pop"))
182 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
184 while ((token = pragma_lex (&x)) == CPP_COMMA)
186 token = pragma_lex (&x);
187 if (token == CPP_NAME && id == 0)
191 else if (token == CPP_NUMBER && action == push && align == -1)
193 if (TREE_CODE (x) != INTEGER_CST)
194 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
195 align = TREE_INT_CST_LOW (x);
203 if (token != CPP_CLOSE_PAREN)
205 #undef GCC_BAD_ACTION
208 GCC_BAD ("malformed %<#pragma pack%> - ignored");
210 if (pragma_lex (&x) != CPP_EOF)
211 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
213 if (flag_pack_struct)
214 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
225 align *= BITS_PER_UNIT;
230 align = maximum_field_alignment;
234 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
239 case set: SET_GLOBAL_ALIGNMENT (align); break;
240 case push: push_alignment (align, id); break;
241 case pop: pop_alignment (id); break;
244 #endif /* HANDLE_PRAGMA_PACK */
246 struct GTY(()) def_pragma_macro_value {
247 struct def_pragma_macro_value *prev;
251 struct GTY(()) def_pragma_macro {
254 struct def_pragma_macro_value value;
257 static GTY((param_is (struct def_pragma_macro))) htab_t pushed_macro_table;
259 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
260 /* Hash table control functions for pushed_macro_table. */
262 dpm_hash (const void *p)
264 return ((const struct def_pragma_macro *)p)->hash;
268 dpm_eq (const void *pa, const void *pb)
270 const struct def_pragma_macro *const a = (const struct def_pragma_macro *) pa,
271 *const b = (const struct def_pragma_macro *) pb;
272 return a->hash == b->hash && strcmp (a->name, b->name) == 0;
275 /* #pragma push_macro("MACRO_NAME")
276 #pragma pop_macro("MACRO_NAME") */
279 handle_pragma_push_macro (cpp_reader *reader)
282 enum cpp_ttype token;
283 struct def_pragma_macro dummy, *c;
284 const char *macroname;
287 if (pragma_lex (&x) != CPP_OPEN_PAREN)
288 GCC_BAD ("missing %<(%> after %<#pragma push_macro%> - ignored");
290 token = pragma_lex (&id);
292 /* Silently ignore */
293 if (token == CPP_CLOSE_PAREN)
295 if (token != CPP_STRING)
296 GCC_BAD ("invalid constant in %<#pragma push_macro%> - ignored");
298 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
299 GCC_BAD ("missing %<)%> after %<#pragma push_macro%> - ignored");
301 if (pragma_lex (&x) != CPP_EOF)
302 warning (OPT_Wpragmas, "junk at end of %<#pragma push_macro%>");
304 /* Check for empty string, and silently ignore. */
305 if (TREE_STRING_LENGTH (id) < 1)
307 macroname = TREE_STRING_POINTER (id);
309 if (pushed_macro_table == NULL)
310 pushed_macro_table = htab_create_ggc (15, dpm_hash, dpm_eq, 0);
312 dummy.hash = htab_hash_string (macroname);
313 dummy.name = macroname;
314 slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
316 c = (struct def_pragma_macro *) *slot;
319 *slot = c = GGC_NEW (struct def_pragma_macro);
320 c->hash = dummy.hash;
321 c->name = ggc_alloc_string (macroname, TREE_STRING_LENGTH (id) - 1);
322 c->value.prev = NULL;
326 struct def_pragma_macro_value *v;
327 v = GGC_NEW (struct def_pragma_macro_value);
332 c->value.value = cpp_push_definition (reader, macroname);
336 handle_pragma_pop_macro (cpp_reader *reader)
339 enum cpp_ttype token;
340 struct def_pragma_macro dummy, *c;
341 const char *macroname;
344 if (pragma_lex (&x) != CPP_OPEN_PAREN)
345 GCC_BAD ("missing %<(%> after %<#pragma pop_macro%> - ignored");
347 token = pragma_lex (&id);
349 /* Silently ignore */
350 if (token == CPP_CLOSE_PAREN)
352 if (token != CPP_STRING)
353 GCC_BAD ("invalid constant in %<#pragma pop_macro%> - ignored");
355 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
356 GCC_BAD ("missing %<)%> after %<#pragma pop_macro%> - ignored");
358 if (pragma_lex (&x) != CPP_EOF)
359 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_macro%>");
361 /* Check for empty string, and silently ignore. */
362 if (TREE_STRING_LENGTH (id) < 1)
364 macroname = TREE_STRING_POINTER (id);
366 dummy.hash = htab_hash_string (macroname);
367 dummy.name = macroname;
368 if (pushed_macro_table)
369 slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
370 dummy.hash, NO_INSERT);
373 c = (struct def_pragma_macro *) *slot;
375 cpp_pop_definition (reader, c->name, c->value.value);
378 c->value = *c->value.prev;
380 htab_clear_slot (pushed_macro_table, slot);
382 #endif /* HANDLE_PRAGMA_PUSH_POP_MACRO */
384 static GTY(()) tree pending_weaks;
386 #ifdef HANDLE_PRAGMA_WEAK
387 static void apply_pragma_weak (tree, tree);
388 static void handle_pragma_weak (cpp_reader *);
391 apply_pragma_weak (tree decl, tree value)
395 value = build_string (IDENTIFIER_LENGTH (value),
396 IDENTIFIER_POINTER (value));
397 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
398 build_tree_list (NULL, value)),
402 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
403 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
404 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
405 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
406 "results in unspecified behavior", decl);
412 maybe_apply_pragma_weak (tree decl)
416 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
418 /* No weak symbols pending, take the short-cut. */
421 /* If it's not visible outside this file, it doesn't matter whether
423 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
425 /* If it's not a function or a variable, it can't be weak.
426 FIXME: what kinds of things are visible outside this file but
427 aren't functions or variables? Should this be an assert instead? */
428 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
431 id = DECL_ASSEMBLER_NAME (decl);
433 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
434 if (id == TREE_PURPOSE (t))
436 apply_pragma_weak (decl, TREE_VALUE (t));
442 /* Process all "#pragma weak A = B" directives where we have not seen
445 maybe_apply_pending_pragma_weaks (void)
447 tree *p, t, alias_id, id, decl, *next;
449 for (p = &pending_weaks; (t = *p) ; p = next)
451 next = &TREE_CHAIN (t);
452 alias_id = TREE_PURPOSE (t);
455 if (TREE_VALUE (t) == NULL)
458 decl = build_decl (UNKNOWN_LOCATION,
459 FUNCTION_DECL, alias_id, default_function_type);
461 DECL_ARTIFICIAL (decl) = 1;
462 TREE_PUBLIC (decl) = 1;
463 DECL_EXTERNAL (decl) = 1;
464 DECL_WEAK (decl) = 1;
466 assemble_alias (decl, id);
470 /* #pragma weak name [= value] */
472 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
474 tree name, value, x, decl;
479 if (pragma_lex (&name) != CPP_NAME)
480 GCC_BAD ("malformed #pragma weak, ignored");
484 if (pragma_lex (&value) != CPP_NAME)
485 GCC_BAD ("malformed #pragma weak, ignored");
489 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
491 decl = identifier_global_value (name);
492 if (decl && DECL_P (decl))
494 apply_pragma_weak (decl, value);
496 assemble_alias (decl, value);
499 pending_weaks = tree_cons (name, value, pending_weaks);
503 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
508 maybe_apply_pending_pragma_weaks (void)
511 #endif /* HANDLE_PRAGMA_WEAK */
513 /* GCC supports two #pragma directives for renaming the external
514 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
515 compatibility with the Solaris and Tru64 system headers. GCC also
516 has its own notation for this, __asm__("name") annotations.
518 Corner cases of these features and their interaction:
520 1) Both pragmas silently apply only to declarations with external
521 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
522 do not have this restriction.
524 2) In C++, both #pragmas silently apply only to extern "C" declarations.
525 Asm labels do not have this restriction.
527 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
528 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
529 new name is different, a warning issues and the name does not change.
531 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
532 *not* the DECL_ASSEMBLER_NAME.
534 5) If #pragma extern_prefix is in effect and a declaration occurs
535 with an __asm__ name, the #pragma extern_prefix is silently
536 ignored for that declaration.
538 6) If #pragma extern_prefix and #pragma redefine_extname apply to
539 the same declaration, whichever triggered first wins, and a warning
540 is issued. (We would like to have #pragma redefine_extname always
541 win, but it can appear either before or after the declaration, and
542 if it appears afterward, we have no way of knowing whether a modified
543 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
545 static GTY(()) tree pending_redefine_extname;
547 static void handle_pragma_redefine_extname (cpp_reader *);
549 /* #pragma redefine_extname oldname newname */
551 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
553 tree oldname, newname, decl, x;
556 if (pragma_lex (&oldname) != CPP_NAME)
557 GCC_BAD ("malformed #pragma redefine_extname, ignored");
558 if (pragma_lex (&newname) != CPP_NAME)
559 GCC_BAD ("malformed #pragma redefine_extname, ignored");
562 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
564 decl = identifier_global_value (oldname);
566 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
567 && (TREE_CODE (decl) == FUNCTION_DECL
568 || TREE_CODE (decl) == VAR_DECL)
569 && has_c_linkage (decl))
571 if (DECL_ASSEMBLER_NAME_SET_P (decl))
573 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
574 name = targetm.strip_name_encoding (name);
576 if (strcmp (name, IDENTIFIER_POINTER (newname)))
577 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
578 "conflict with previous rename");
581 change_decl_assembler_name (decl, newname);
584 /* We have to add this to the rename list even if there's already
585 a global value that doesn't meet the above criteria, because in
586 C++ "struct foo {...};" puts "foo" in the current namespace but
587 does *not* conflict with a subsequent declaration of a function
588 or variable foo. See g++.dg/other/pragma-re-2.C. */
589 add_to_renaming_pragma_list (oldname, newname);
592 /* This is called from here and from ia64.c. */
594 add_to_renaming_pragma_list (tree oldname, tree newname)
596 tree previous = purpose_member (oldname, pending_redefine_extname);
599 if (TREE_VALUE (previous) != newname)
600 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
601 "conflict with previous #pragma redefine_extname");
605 pending_redefine_extname
606 = tree_cons (oldname, newname, pending_redefine_extname);
609 static GTY(()) tree pragma_extern_prefix;
611 /* #pragma extern_prefix "prefix" */
613 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
618 if (pragma_lex (&prefix) != CPP_STRING)
619 GCC_BAD ("malformed #pragma extern_prefix, ignored");
622 warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
624 if (targetm.handle_pragma_extern_prefix)
625 /* Note that the length includes the null terminator. */
626 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
627 else if (warn_unknown_pragmas > in_system_header)
628 warning (OPT_Wunknown_pragmas,
629 "#pragma extern_prefix not supported on this target");
632 /* Hook from the front ends to apply the results of one of the preceding
633 pragmas that rename variables. */
636 maybe_apply_renaming_pragma (tree decl, tree asmname)
640 /* The renaming pragmas are only applied to declarations with
642 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
643 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
644 || !has_c_linkage (decl))
647 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
648 but we may warn about a rename that conflicts. */
649 if (DECL_ASSEMBLER_NAME_SET_P (decl))
651 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
652 oldname = targetm.strip_name_encoding (oldname);
654 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
655 warning (OPT_Wpragmas, "asm declaration ignored due to "
656 "conflict with previous rename");
658 /* Take any pending redefine_extname off the list. */
659 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
660 if (DECL_NAME (decl) == TREE_PURPOSE (t))
662 /* Only warn if there is a conflict. */
663 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
664 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
665 "conflict with previous rename");
673 /* Find out if we have a pending #pragma redefine_extname. */
674 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
675 if (DECL_NAME (decl) == TREE_PURPOSE (t))
677 tree newname = TREE_VALUE (t);
680 /* If we already have an asmname, #pragma redefine_extname is
681 ignored (with a warning if it conflicts). */
684 if (strcmp (TREE_STRING_POINTER (asmname),
685 IDENTIFIER_POINTER (newname)) != 0)
686 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
687 "conflict with __asm__ declaration");
691 /* Otherwise we use what we've got; #pragma extern_prefix is
693 return build_string (IDENTIFIER_LENGTH (newname),
694 IDENTIFIER_POINTER (newname));
697 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
701 /* If #pragma extern_prefix is in effect, apply it. */
702 if (pragma_extern_prefix)
704 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
705 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
707 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
708 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
710 char *newname = (char *) alloca (plen + ilen + 1);
712 memcpy (newname, prefix, plen);
713 memcpy (newname + plen, id, ilen + 1);
715 return build_string (plen + ilen, newname);
723 #ifdef HANDLE_PRAGMA_VISIBILITY
724 static void handle_pragma_visibility (cpp_reader *);
726 static VEC (int, heap) *visstack;
728 /* Push the visibility indicated by STR onto the top of the #pragma
729 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
730 C++ namespace with visibility attribute and 2 for C++ builtin
731 ABI namespace. push_visibility/pop_visibility calls must have
732 matching KIND, it is not allowed to push visibility using one
733 KIND and pop using a different one. */
736 push_visibility (const char *str, int kind)
738 VEC_safe_push (int, heap, visstack,
739 ((int) default_visibility) | (kind << 8));
740 if (!strcmp (str, "default"))
741 default_visibility = VISIBILITY_DEFAULT;
742 else if (!strcmp (str, "internal"))
743 default_visibility = VISIBILITY_INTERNAL;
744 else if (!strcmp (str, "hidden"))
745 default_visibility = VISIBILITY_HIDDEN;
746 else if (!strcmp (str, "protected"))
747 default_visibility = VISIBILITY_PROTECTED;
749 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
750 visibility_options.inpragma = 1;
753 /* Pop a level of the #pragma visibility stack. Return true if
757 pop_visibility (int kind)
759 if (!VEC_length (int, visstack))
761 if ((VEC_last (int, visstack) >> 8) != kind)
764 = (enum symbol_visibility) (VEC_pop (int, visstack) & 0xff);
765 visibility_options.inpragma
766 = VEC_length (int, visstack) != 0;
770 /* Sets the default visibility for symbols to something other than that
771 specified on the command line. */
774 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
776 /* Form is #pragma GCC visibility push(hidden)|pop */
778 enum cpp_ttype token;
779 enum { bad, push, pop } action = bad;
781 token = pragma_lex (&x);
782 if (token == CPP_NAME)
784 const char *op = IDENTIFIER_POINTER (x);
785 if (!strcmp (op, "push"))
787 else if (!strcmp (op, "pop"))
791 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
796 if (! pop_visibility (0))
797 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
801 if (pragma_lex (&x) != CPP_OPEN_PAREN)
802 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
803 token = pragma_lex (&x);
804 if (token != CPP_NAME)
805 GCC_BAD ("malformed #pragma GCC visibility push");
807 push_visibility (IDENTIFIER_POINTER (x), 0);
808 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
809 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
812 if (pragma_lex (&x) != CPP_EOF)
813 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
819 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
821 const char *kind_string, *option_string;
822 unsigned int option_index;
823 enum cpp_ttype token;
829 error ("#pragma GCC diagnostic not allowed inside functions");
833 token = pragma_lex (&x);
834 if (token != CPP_NAME)
835 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
836 kind_string = IDENTIFIER_POINTER (x);
837 if (strcmp (kind_string, "error") == 0)
839 else if (strcmp (kind_string, "warning") == 0)
841 else if (strcmp (kind_string, "ignored") == 0)
844 GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
846 token = pragma_lex (&x);
847 if (token != CPP_STRING)
848 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
849 option_string = TREE_STRING_POINTER (x);
850 for (option_index = 0; option_index < cl_options_count; option_index++)
851 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
853 /* This overrides -Werror, for example. */
854 diagnostic_classify_diagnostic (global_dc, option_index, kind);
855 /* This makes sure the option is enabled, like -Wfoo would do. */
856 if (cl_options[option_index].var_type == CLVC_BOOLEAN
857 && cl_options[option_index].flag_var
858 && kind != DK_IGNORED)
859 *(int *) cl_options[option_index].flag_var = 1;
862 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
865 /* Parse #pragma GCC target (xxx) to set target specific options. */
867 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
869 enum cpp_ttype token;
871 bool close_paren_needed_p = false;
875 error ("#pragma GCC option is not allowed inside functions");
879 token = pragma_lex (&x);
880 if (token == CPP_OPEN_PAREN)
882 close_paren_needed_p = true;
883 token = pragma_lex (&x);
886 if (token != CPP_STRING)
888 GCC_BAD ("%<#pragma GCC option%> is not a string");
892 /* Strings are user options. */
895 tree args = NULL_TREE;
899 /* Build up the strings now as a tree linked list. Skip empty
901 if (TREE_STRING_LENGTH (x) > 0)
902 args = tree_cons (NULL_TREE, x, args);
904 token = pragma_lex (&x);
905 while (token == CPP_COMMA)
906 token = pragma_lex (&x);
908 while (token == CPP_STRING);
910 if (close_paren_needed_p)
912 if (token == CPP_CLOSE_PAREN)
913 token = pragma_lex (&x);
915 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
916 "not have a final %<)%>.");
919 if (token != CPP_EOF)
921 error ("#pragma GCC target string... is badly formed");
925 /* put arguments in the order the user typed them. */
926 args = nreverse (args);
928 if (targetm.target_option.pragma_parse (args, NULL_TREE))
929 current_target_pragma = args;
933 /* Handle #pragma GCC optimize to set optimization options. */
935 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
937 enum cpp_ttype token;
939 bool close_paren_needed_p = false;
940 tree optimization_previous_node = optimization_current_node;
944 error ("#pragma GCC optimize is not allowed inside functions");
948 token = pragma_lex (&x);
949 if (token == CPP_OPEN_PAREN)
951 close_paren_needed_p = true;
952 token = pragma_lex (&x);
955 if (token != CPP_STRING && token != CPP_NUMBER)
957 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
961 /* Strings/numbers are user options. */
964 tree args = NULL_TREE;
968 /* Build up the numbers/strings now as a list. */
969 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
970 args = tree_cons (NULL_TREE, x, args);
972 token = pragma_lex (&x);
973 while (token == CPP_COMMA)
974 token = pragma_lex (&x);
976 while (token == CPP_STRING || token == CPP_NUMBER);
978 if (close_paren_needed_p)
980 if (token == CPP_CLOSE_PAREN)
981 token = pragma_lex (&x);
983 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
984 "not have a final %<)%>.");
987 if (token != CPP_EOF)
989 error ("#pragma GCC optimize string... is badly formed");
993 /* put arguments in the order the user typed them. */
994 args = nreverse (args);
996 parse_optimize_options (args, false);
997 current_optimize_pragma = chainon (current_optimize_pragma, args);
998 optimization_current_node = build_optimization_node ();
999 c_cpp_builtins_optimize_pragma (parse_in,
1000 optimization_previous_node,
1001 optimization_current_node);
1005 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1006 both the binary representation of the options and the TREE_LIST of
1007 strings that will be added to the function's attribute list. */
1008 typedef struct GTY(()) opt_stack {
1009 struct opt_stack *prev;
1011 tree target_strings;
1012 tree optimize_binary;
1013 tree optimize_strings;
1016 static GTY(()) struct opt_stack * options_stack;
1018 /* Handle #pragma GCC push_options to save the current target and optimization
1022 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1024 enum cpp_ttype token;
1028 token = pragma_lex (&x);
1029 if (token != CPP_EOF)
1031 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1035 p = GGC_NEW (opt_stack);
1036 p->prev = options_stack;
1039 /* Save optimization and target flags in binary format. */
1040 p->optimize_binary = build_optimization_node ();
1041 p->target_binary = build_target_option_node ();
1043 /* Save optimization and target flags in string list format. */
1044 p->optimize_strings = copy_list (current_optimize_pragma);
1045 p->target_strings = copy_list (current_target_pragma);
1048 /* Handle #pragma GCC pop_options to restore the current target and
1049 optimization options from a previous push_options. */
1052 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1054 enum cpp_ttype token;
1058 token = pragma_lex (&x);
1059 if (token != CPP_EOF)
1061 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1065 if (! options_stack)
1067 warning (OPT_Wpragmas,
1068 "%<#pragma GCC pop_options%> without a corresponding "
1069 "%<#pragma GCC push_options%>");
1074 options_stack = p->prev;
1076 if (p->target_binary != target_option_current_node)
1078 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1079 target_option_current_node = p->target_binary;
1082 if (p->optimize_binary != optimization_current_node)
1084 tree old_optimize = optimization_current_node;
1085 cl_optimization_restore (TREE_OPTIMIZATION (p->optimize_binary));
1086 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
1087 p->optimize_binary);
1088 optimization_current_node = p->optimize_binary;
1091 current_target_pragma = p->target_strings;
1092 current_optimize_pragma = p->optimize_strings;
1095 /* Handle #pragma GCC reset_options to restore the current target and
1096 optimization options to the original options used on the command line. */
1099 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1101 enum cpp_ttype token;
1103 tree new_optimize = optimization_default_node;
1104 tree new_target = target_option_default_node;
1106 token = pragma_lex (&x);
1107 if (token != CPP_EOF)
1109 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1113 if (new_target != target_option_current_node)
1115 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1116 target_option_current_node = new_target;
1119 if (new_optimize != optimization_current_node)
1121 tree old_optimize = optimization_current_node;
1122 cl_optimization_restore (TREE_OPTIMIZATION (new_optimize));
1123 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1124 optimization_current_node = new_optimize;
1127 current_target_pragma = NULL_TREE;
1128 current_optimize_pragma = NULL_TREE;
1131 /* Print a plain user-specified message. */
1134 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1136 enum cpp_ttype token;
1137 tree x, message = 0;
1139 token = pragma_lex (&x);
1140 if (token == CPP_OPEN_PAREN)
1142 token = pragma_lex (&x);
1143 if (token == CPP_STRING)
1146 GCC_BAD ("expected a string after %<#pragma message%>");
1147 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1148 GCC_BAD ("malformed %<#pragma message%>, ignored");
1150 else if (token == CPP_STRING)
1153 GCC_BAD ("expected a string after %<#pragma message%>");
1155 gcc_assert (message);
1157 if (pragma_lex (&x) != CPP_EOF)
1158 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1160 if (TREE_STRING_LENGTH (message) > 1)
1161 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1164 /* Mark whether the current location is valid for a STDC pragma. */
1166 static bool valid_location_for_stdc_pragma;
1169 mark_valid_location_for_stdc_pragma (bool flag)
1171 valid_location_for_stdc_pragma = flag;
1174 /* Return true if the current location is valid for a STDC pragma. */
1177 valid_location_for_stdc_pragma_p (void)
1179 return valid_location_for_stdc_pragma;
1182 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1184 /* A STDC pragma must appear outside of external declarations or
1185 preceding all explicit declarations and statements inside a compound
1186 statement; its behavior is undefined if used in any other context.
1187 It takes a switch of ON, OFF, or DEFAULT. */
1189 static enum pragma_switch_t
1190 handle_stdc_pragma (const char *pname)
1194 enum pragma_switch_t ret;
1196 if (!valid_location_for_stdc_pragma_p ())
1198 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1203 if (pragma_lex (&t) != CPP_NAME)
1205 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1209 arg = IDENTIFIER_POINTER (t);
1211 if (!strcmp (arg, "ON"))
1213 else if (!strcmp (arg, "OFF"))
1215 else if (!strcmp (arg, "DEFAULT"))
1216 ret = PRAGMA_DEFAULT;
1219 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1223 if (pragma_lex (&t) != CPP_EOF)
1225 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1232 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1233 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1234 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1237 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1239 if (c_dialect_cxx ())
1241 if (warn_unknown_pragmas > in_system_header)
1242 warning (OPT_Wunknown_pragmas,
1243 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1248 if (!targetm.decimal_float_supported_p ())
1250 if (warn_unknown_pragmas > in_system_header)
1251 warning (OPT_Wunknown_pragmas,
1252 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1257 pedwarn (input_location, OPT_pedantic,
1258 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1260 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1263 set_float_const_decimal64 ();
1266 case PRAGMA_DEFAULT:
1267 clear_float_const_decimal64 ();
1274 /* A vector of registered pragma callbacks. */
1276 DEF_VEC_O (pragma_handler);
1277 DEF_VEC_ALLOC_O (pragma_handler, heap);
1279 static VEC(pragma_handler, heap) *registered_pragmas;
1287 DEF_VEC_O (pragma_ns_name);
1288 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
1290 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
1292 struct omp_pragma_def { const char *name; unsigned int id; };
1293 static const struct omp_pragma_def omp_pragmas[] = {
1294 { "atomic", PRAGMA_OMP_ATOMIC },
1295 { "barrier", PRAGMA_OMP_BARRIER },
1296 { "critical", PRAGMA_OMP_CRITICAL },
1297 { "flush", PRAGMA_OMP_FLUSH },
1298 { "for", PRAGMA_OMP_FOR },
1299 { "master", PRAGMA_OMP_MASTER },
1300 { "ordered", PRAGMA_OMP_ORDERED },
1301 { "parallel", PRAGMA_OMP_PARALLEL },
1302 { "section", PRAGMA_OMP_SECTION },
1303 { "sections", PRAGMA_OMP_SECTIONS },
1304 { "single", PRAGMA_OMP_SINGLE },
1305 { "task", PRAGMA_OMP_TASK },
1306 { "taskwait", PRAGMA_OMP_TASKWAIT },
1307 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1311 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1313 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1316 for (i = 0; i < n_omp_pragmas; ++i)
1317 if (omp_pragmas[i].id == id)
1320 *name = omp_pragmas[i].name;
1324 if (id >= PRAGMA_FIRST_EXTERNAL
1325 && (id < PRAGMA_FIRST_EXTERNAL
1326 + VEC_length (pragma_ns_name, registered_pp_pragmas)))
1328 *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
1329 id - PRAGMA_FIRST_EXTERNAL)->space;
1330 *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
1331 id - PRAGMA_FIRST_EXTERNAL)->name;
1338 /* Front-end wrappers for pragma registration to avoid dragging
1339 cpplib.h in almost everywhere. */
1342 c_register_pragma_1 (const char *space, const char *name,
1343 pragma_handler handler, bool allow_expansion)
1347 if (flag_preprocess_only)
1349 pragma_ns_name ns_name;
1351 if (!allow_expansion)
1354 ns_name.space = space;
1355 ns_name.name = name;
1356 VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
1357 id = VEC_length (pragma_ns_name, registered_pp_pragmas);
1358 id += PRAGMA_FIRST_EXTERNAL - 1;
1362 VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
1363 id = VEC_length (pragma_handler, registered_pragmas);
1364 id += PRAGMA_FIRST_EXTERNAL - 1;
1366 /* The C++ front end allocates 6 bits in cp_token; the C front end
1367 allocates 7 bits in c_token. At present this is sufficient. */
1368 gcc_assert (id < 64);
1371 cpp_register_deferred_pragma (parse_in, space, name, id,
1372 allow_expansion, false);
1376 c_register_pragma (const char *space, const char *name, pragma_handler handler)
1378 c_register_pragma_1 (space, name, handler, false);
1382 c_register_pragma_with_expansion (const char *space, const char *name,
1383 pragma_handler handler)
1385 c_register_pragma_1 (space, name, handler, true);
1389 c_invoke_pragma_handler (unsigned int id)
1391 pragma_handler handler;
1393 id -= PRAGMA_FIRST_EXTERNAL;
1394 handler = *VEC_index (pragma_handler, registered_pragmas, id);
1399 /* Set up front-end pragmas. */
1405 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1408 for (i = 0; i < n_omp_pragmas; ++i)
1409 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1410 omp_pragmas[i].id, true, true);
1413 if (!flag_preprocess_only)
1414 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1415 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1417 #ifdef HANDLE_PRAGMA_PACK
1418 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1419 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1421 c_register_pragma (0, "pack", handle_pragma_pack);
1424 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
1425 c_register_pragma (0 ,"push_macro", handle_pragma_push_macro);
1426 c_register_pragma (0 ,"pop_macro", handle_pragma_pop_macro);
1428 #ifdef HANDLE_PRAGMA_WEAK
1429 c_register_pragma (0, "weak", handle_pragma_weak);
1431 #ifdef HANDLE_PRAGMA_VISIBILITY
1432 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1435 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1436 c_register_pragma ("GCC", "target", handle_pragma_target);
1437 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1438 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1439 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1440 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1442 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1443 handle_pragma_float_const_decimal64);
1445 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1446 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1448 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1450 #ifdef REGISTER_TARGET_PRAGMAS
1451 REGISTER_TARGET_PRAGMAS ();
1455 #include "gt-c-pragma.h"