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 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 align_stack
GTY(())
50 struct align_stack
* prev
;
53 static GTY(()) struct align_stack
* alignment_stack
;
55 #ifdef HANDLE_PRAGMA_PACK
56 static void handle_pragma_pack (cpp_reader
*);
58 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
59 /* If we have a "global" #pragma pack(<n>) in effect when the first
60 #pragma pack(push,<n>) is encountered, this stores the value of
61 maximum_field_alignment in effect. When the final pop_alignment()
62 happens, we restore the value to this, not to a value of 0 for
63 maximum_field_alignment. Value is in bits. */
64 static int default_alignment
;
65 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
66 ? &default_alignment \
67 : &alignment_stack->alignment) = (ALIGN))
69 static void push_alignment (int, tree
);
70 static void pop_alignment (tree
);
72 /* Push an alignment value onto the stack. */
74 push_alignment (int alignment
, tree id
)
78 entry
= GGC_NEW (align_stack
);
80 entry
->alignment
= alignment
;
82 entry
->prev
= alignment_stack
;
84 /* The current value of maximum_field_alignment is not necessarily
85 0 since there may be a #pragma pack(<n>) in effect; remember it
86 so that we can restore it after the final #pragma pop(). */
87 if (alignment_stack
== NULL
)
88 default_alignment
= maximum_field_alignment
;
90 alignment_stack
= entry
;
92 maximum_field_alignment
= alignment
;
95 /* Undo a push of an alignment onto the stack. */
97 pop_alignment (tree id
)
101 if (alignment_stack
== NULL
)
102 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
104 /* If we got an identifier, strip away everything above the target
105 entry so that the next step will restore the state just below it. */
108 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
111 alignment_stack
= entry
;
115 warning (OPT_Wpragmas
, "\
116 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
117 , IDENTIFIER_POINTER (id
), IDENTIFIER_POINTER (id
));
120 entry
= alignment_stack
->prev
;
122 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
124 alignment_stack
= entry
;
126 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
127 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
128 #define push_alignment(ID, N) \
129 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
130 #define pop_alignment(ID) \
131 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
132 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
138 #pragma pack (push, N)
139 #pragma pack (push, ID)
140 #pragma pack (push, ID, N)
142 #pragma pack (pop, ID) */
144 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
148 enum cpp_ttype token
;
149 enum { set
, push
, pop
} action
;
151 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
152 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
154 token
= pragma_lex (&x
);
155 if (token
== CPP_CLOSE_PAREN
)
158 align
= initial_max_fld_align
;
160 else if (token
== CPP_NUMBER
)
162 if (TREE_CODE (x
) != INTEGER_CST
)
163 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
164 align
= TREE_INT_CST_LOW (x
);
166 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
167 GCC_BAD ("malformed %<#pragma pack%> - ignored");
169 else if (token
== CPP_NAME
)
171 #define GCC_BAD_ACTION do { if (action != pop) \
172 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
174 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
177 const char *op
= IDENTIFIER_POINTER (x
);
178 if (!strcmp (op
, "push"))
180 else if (!strcmp (op
, "pop"))
183 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op
);
185 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
187 token
= pragma_lex (&x
);
188 if (token
== CPP_NAME
&& id
== 0)
192 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
194 if (TREE_CODE (x
) != INTEGER_CST
)
195 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
196 align
= TREE_INT_CST_LOW (x
);
204 if (token
!= CPP_CLOSE_PAREN
)
206 #undef GCC_BAD_ACTION
209 GCC_BAD ("malformed %<#pragma pack%> - ignored");
211 if (pragma_lex (&x
) != CPP_EOF
)
212 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
214 if (flag_pack_struct
)
215 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
226 align
*= BITS_PER_UNIT
;
231 align
= maximum_field_alignment
;
235 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
240 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
241 case push
: push_alignment (align
, id
); break;
242 case pop
: pop_alignment (id
); break;
245 #endif /* HANDLE_PRAGMA_PACK */
247 struct def_pragma_macro_value
GTY(())
249 struct def_pragma_macro_value
*prev
;
253 struct def_pragma_macro
GTY(())
257 struct def_pragma_macro_value value
;
260 static GTY((param_is (struct def_pragma_macro
))) htab_t pushed_macro_table
;
262 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
263 /* Hash table control functions for pushed_macro_table. */
265 dpm_hash (const void *p
)
267 return ((const struct def_pragma_macro
*)p
)->hash
;
271 dpm_eq (const void *pa
, const void *pb
)
273 const struct def_pragma_macro
*a
= pa
, *b
= pb
;
274 return a
->hash
== b
->hash
&& strcmp (a
->name
, b
->name
) == 0;
277 /* #pragma push_macro("MACRO_NAME")
278 #pragma pop_macro("MACRO_NAME") */
281 handle_pragma_push_macro (cpp_reader
*reader
)
284 enum cpp_ttype token
;
285 struct def_pragma_macro dummy
, *c
;
286 const char *macroname
;
289 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
290 GCC_BAD ("missing %<(%> after %<#pragma push_macro%> - ignored");
292 token
= pragma_lex (&id
);
294 /* Silently ignore */
295 if (token
== CPP_CLOSE_PAREN
)
297 if (token
!= CPP_STRING
)
298 GCC_BAD ("invalid constant in %<#pragma push_macro%> - ignored");
300 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
301 GCC_BAD ("missing %<)%> after %<#pragma push_macro%> - ignored");
303 if (pragma_lex (&x
) != CPP_EOF
)
304 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_macro%>");
306 /* Check for empty string, and silently ignore. */
307 if (TREE_STRING_LENGTH (id
) < 1)
309 macroname
= TREE_STRING_POINTER (id
);
311 if (pushed_macro_table
== NULL
)
312 pushed_macro_table
= htab_create_ggc (15, dpm_hash
, dpm_eq
, 0);
314 dummy
.hash
= htab_hash_string (macroname
);
315 dummy
.name
= macroname
;
316 slot
= htab_find_slot_with_hash (pushed_macro_table
, &dummy
,
321 *slot
= c
= ggc_alloc (sizeof (struct def_pragma_macro
));
322 c
->hash
= dummy
.hash
;
323 c
->name
= ggc_alloc_string (macroname
, TREE_STRING_LENGTH (id
) - 1);
324 c
->value
.prev
= NULL
;
328 struct def_pragma_macro_value
*v
;
329 v
= ggc_alloc (sizeof (struct def_pragma_macro_value
));
334 c
->value
.value
= cpp_push_definition (reader
, macroname
);
338 handle_pragma_pop_macro (cpp_reader
*reader
)
341 enum cpp_ttype token
;
342 struct def_pragma_macro dummy
, *c
;
343 const char *macroname
;
346 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
347 GCC_BAD ("missing %<(%> after %<#pragma pop_macro%> - ignored");
349 token
= pragma_lex (&id
);
351 /* Silently ignore */
352 if (token
== CPP_CLOSE_PAREN
)
354 if (token
!= CPP_STRING
)
355 GCC_BAD ("invalid constant in %<#pragma pop_macro%> - ignored");
357 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
358 GCC_BAD ("missing %<)%> after %<#pragma pop_macro%> - ignored");
360 if (pragma_lex (&x
) != CPP_EOF
)
361 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_macro%>");
363 /* Check for empty string, and silently ignore. */
364 if (TREE_STRING_LENGTH (id
) < 1)
366 macroname
= TREE_STRING_POINTER (id
);
368 dummy
.hash
= htab_hash_string (macroname
);
369 dummy
.name
= macroname
;
370 slot
= htab_find_slot_with_hash (pushed_macro_table
, &dummy
,
371 dummy
.hash
, NO_INSERT
);
376 cpp_pop_definition (reader
, c
->name
, c
->value
.value
);
379 c
->value
= *c
->value
.prev
;
381 htab_clear_slot (pushed_macro_table
, slot
);
383 #endif /* HANDLE_PRAGMA_PUSH_POP_MACRO */
385 static GTY(()) tree pending_weaks
;
387 #ifdef HANDLE_PRAGMA_WEAK
388 static void apply_pragma_weak (tree
, tree
);
389 static void handle_pragma_weak (cpp_reader
*);
392 apply_pragma_weak (tree decl
, tree value
)
396 value
= build_string (IDENTIFIER_LENGTH (value
),
397 IDENTIFIER_POINTER (value
));
398 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
399 build_tree_list (NULL
, value
)),
403 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
404 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
405 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
406 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
407 "results in unspecified behavior", decl
);
413 maybe_apply_pragma_weak (tree decl
)
417 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
419 /* No weak symbols pending, take the short-cut. */
422 /* If it's not visible outside this file, it doesn't matter whether
424 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
426 /* If it's not a function or a variable, it can't be weak.
427 FIXME: what kinds of things are visible outside this file but
428 aren't functions or variables? Should this be an assert instead? */
429 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
432 id
= DECL_ASSEMBLER_NAME (decl
);
434 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
435 if (id
== TREE_PURPOSE (t
))
437 apply_pragma_weak (decl
, TREE_VALUE (t
));
443 /* Process all "#pragma weak A = B" directives where we have not seen
446 maybe_apply_pending_pragma_weaks (void)
448 tree
*p
, t
, alias_id
, id
, decl
, *next
;
450 for (p
= &pending_weaks
; (t
= *p
) ; p
= next
)
452 next
= &TREE_CHAIN (t
);
453 alias_id
= TREE_PURPOSE (t
);
456 if (TREE_VALUE (t
) == NULL
)
459 decl
= build_decl (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 if (!flag_mudflap
&& !targetm
.handle_pragma_redefine_extname
)
566 if (warn_unknown_pragmas
> in_system_header
)
567 warning (OPT_Wunknown_pragmas
,
568 "#pragma redefine_extname not supported on this target");
572 decl
= identifier_global_value (oldname
);
574 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
575 && (TREE_CODE (decl
) == FUNCTION_DECL
576 || TREE_CODE (decl
) == VAR_DECL
)
577 && has_c_linkage (decl
))
579 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
581 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
582 name
= targetm
.strip_name_encoding (name
);
584 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
585 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
586 "conflict with previous rename");
589 change_decl_assembler_name (decl
, newname
);
592 /* We have to add this to the rename list even if there's already
593 a global value that doesn't meet the above criteria, because in
594 C++ "struct foo {...};" puts "foo" in the current namespace but
595 does *not* conflict with a subsequent declaration of a function
596 or variable foo. See g++.dg/other/pragma-re-2.C. */
597 add_to_renaming_pragma_list (oldname
, newname
);
600 /* This is called from here and from ia64.c. */
602 add_to_renaming_pragma_list (tree oldname
, tree newname
)
604 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
607 if (TREE_VALUE (previous
) != newname
)
608 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
609 "conflict with previous #pragma redefine_extname");
613 pending_redefine_extname
614 = tree_cons (oldname
, newname
, pending_redefine_extname
);
617 static GTY(()) tree pragma_extern_prefix
;
619 /* #pragma extern_prefix "prefix" */
621 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
626 if (pragma_lex (&prefix
) != CPP_STRING
)
627 GCC_BAD ("malformed #pragma extern_prefix, ignored");
630 warning (OPT_Wpragmas
, "junk at end of %<#pragma extern_prefix%>");
632 if (targetm
.handle_pragma_extern_prefix
)
633 /* Note that the length includes the null terminator. */
634 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
635 else if (warn_unknown_pragmas
> in_system_header
)
636 warning (OPT_Wunknown_pragmas
,
637 "#pragma extern_prefix not supported on this target");
640 /* Hook from the front ends to apply the results of one of the preceding
641 pragmas that rename variables. */
644 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
648 /* The renaming pragmas are only applied to declarations with
650 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
651 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
652 || !has_c_linkage (decl
))
655 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
656 but we may warn about a rename that conflicts. */
657 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
659 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
660 oldname
= targetm
.strip_name_encoding (oldname
);
662 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
663 warning (OPT_Wpragmas
, "asm declaration ignored due to "
664 "conflict with previous rename");
666 /* Take any pending redefine_extname off the list. */
667 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
668 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
670 /* Only warn if there is a conflict. */
671 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
672 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
673 "conflict with previous rename");
681 /* Find out if we have a pending #pragma redefine_extname. */
682 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
683 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
685 tree newname
= TREE_VALUE (t
);
688 /* If we already have an asmname, #pragma redefine_extname is
689 ignored (with a warning if it conflicts). */
692 if (strcmp (TREE_STRING_POINTER (asmname
),
693 IDENTIFIER_POINTER (newname
)) != 0)
694 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
695 "conflict with __asm__ declaration");
699 /* Otherwise we use what we've got; #pragma extern_prefix is
701 return build_string (IDENTIFIER_LENGTH (newname
),
702 IDENTIFIER_POINTER (newname
));
705 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
709 /* If #pragma extern_prefix is in effect, apply it. */
710 if (pragma_extern_prefix
)
712 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
713 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
715 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
716 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
718 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
720 memcpy (newname
, prefix
, plen
);
721 memcpy (newname
+ plen
, id
, ilen
+ 1);
723 return build_string (plen
+ ilen
, newname
);
731 #ifdef HANDLE_PRAGMA_VISIBILITY
732 static void handle_pragma_visibility (cpp_reader
*);
734 typedef enum symbol_visibility visibility
;
735 DEF_VEC_I (visibility
);
736 DEF_VEC_ALLOC_I (visibility
, heap
);
737 static VEC (visibility
, heap
) *visstack
;
739 /* Push the visibility indicated by STR onto the top of the #pragma
743 push_visibility (const char *str
)
745 VEC_safe_push (visibility
, heap
, visstack
,
747 if (!strcmp (str
, "default"))
748 default_visibility
= VISIBILITY_DEFAULT
;
749 else if (!strcmp (str
, "internal"))
750 default_visibility
= VISIBILITY_INTERNAL
;
751 else if (!strcmp (str
, "hidden"))
752 default_visibility
= VISIBILITY_HIDDEN
;
753 else if (!strcmp (str
, "protected"))
754 default_visibility
= VISIBILITY_PROTECTED
;
756 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
757 visibility_options
.inpragma
= 1;
760 /* Pop a level of the #pragma visibility stack. */
763 pop_visibility (void)
765 default_visibility
= VEC_pop (visibility
, visstack
);
766 visibility_options
.inpragma
767 = VEC_length (visibility
, 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 (!VEC_length (visibility
, visstack
))
797 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
803 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
804 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
805 token
= pragma_lex (&x
);
806 if (token
!= CPP_NAME
)
807 GCC_BAD ("malformed #pragma GCC visibility push");
809 push_visibility (IDENTIFIER_POINTER (x
));
810 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
811 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
814 if (pragma_lex (&x
) != CPP_EOF
)
815 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
821 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
823 const char *kind_string
, *option_string
;
824 unsigned int option_index
;
825 enum cpp_ttype token
;
831 error ("#pragma GCC diagnostic not allowed inside functions");
835 token
= pragma_lex (&x
);
836 if (token
!= CPP_NAME
)
837 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
838 kind_string
= IDENTIFIER_POINTER (x
);
839 if (strcmp (kind_string
, "error") == 0)
841 else if (strcmp (kind_string
, "warning") == 0)
843 else if (strcmp (kind_string
, "ignored") == 0)
846 GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
848 token
= pragma_lex (&x
);
849 if (token
!= CPP_STRING
)
850 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
851 option_string
= TREE_STRING_POINTER (x
);
852 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
853 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
855 /* This overrides -Werror, for example. */
856 diagnostic_classify_diagnostic (global_dc
, option_index
, kind
);
857 /* This makes sure the option is enabled, like -Wfoo would do. */
858 if (cl_options
[option_index
].var_type
== CLVC_BOOLEAN
859 && cl_options
[option_index
].flag_var
860 && kind
!= DK_IGNORED
)
861 *(int *) cl_options
[option_index
].flag_var
= 1;
864 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
867 /* A vector of registered pragma callbacks. */
869 DEF_VEC_O (pragma_handler
);
870 DEF_VEC_ALLOC_O (pragma_handler
, heap
);
872 static VEC(pragma_handler
, heap
) *registered_pragmas
;
874 /* Front-end wrappers for pragma registration to avoid dragging
875 cpplib.h in almost everywhere. */
878 c_register_pragma_1 (const char *space
, const char *name
,
879 pragma_handler handler
, bool allow_expansion
)
883 VEC_safe_push (pragma_handler
, heap
, registered_pragmas
, &handler
);
884 id
= VEC_length (pragma_handler
, registered_pragmas
);
885 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
887 /* The C++ front end allocates 6 bits in cp_token; the C front end
888 allocates 7 bits in c_token. At present this is sufficient. */
889 gcc_assert (id
< 64);
891 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
892 allow_expansion
, false);
896 c_register_pragma (const char *space
, const char *name
, pragma_handler handler
)
898 c_register_pragma_1 (space
, name
, handler
, false);
902 c_register_pragma_with_expansion (const char *space
, const char *name
,
903 pragma_handler handler
)
905 c_register_pragma_1 (space
, name
, handler
, true);
909 c_invoke_pragma_handler (unsigned int id
)
911 pragma_handler handler
;
913 id
-= PRAGMA_FIRST_EXTERNAL
;
914 handler
= *VEC_index (pragma_handler
, registered_pragmas
, id
);
919 /* Set up front-end pragmas. */
923 if (flag_openmp
&& !flag_preprocess_only
)
925 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
926 static const struct omp_pragma_def omp_pragmas
[] = {
927 { "atomic", PRAGMA_OMP_ATOMIC
},
928 { "barrier", PRAGMA_OMP_BARRIER
},
929 { "critical", PRAGMA_OMP_CRITICAL
},
930 { "flush", PRAGMA_OMP_FLUSH
},
931 { "for", PRAGMA_OMP_FOR
},
932 { "master", PRAGMA_OMP_MASTER
},
933 { "ordered", PRAGMA_OMP_ORDERED
},
934 { "parallel", PRAGMA_OMP_PARALLEL
},
935 { "section", PRAGMA_OMP_SECTION
},
936 { "sections", PRAGMA_OMP_SECTIONS
},
937 { "single", PRAGMA_OMP_SINGLE
},
938 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
941 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
944 for (i
= 0; i
< n_omp_pragmas
; ++i
)
945 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
946 omp_pragmas
[i
].id
, true, true);
949 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
950 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
952 #ifdef HANDLE_PRAGMA_PACK
953 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
954 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
956 c_register_pragma (0, "pack", handle_pragma_pack
);
959 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
960 c_register_pragma (0 ,"push_macro", handle_pragma_push_macro
);
961 c_register_pragma (0 ,"pop_macro", handle_pragma_pop_macro
);
963 #ifdef HANDLE_PRAGMA_WEAK
964 c_register_pragma (0, "weak", handle_pragma_weak
);
966 #ifdef HANDLE_PRAGMA_VISIBILITY
967 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
970 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
972 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname
);
973 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
975 #ifdef REGISTER_TARGET_PRAGMAS
976 REGISTER_TARGET_PRAGMAS ();
980 #include "gt-c-pragma.h"