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"
42 #define GCC_BAD(gmsgid) \
43 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
44 #define GCC_BAD2(gmsgid, arg) \
45 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
47 typedef struct GTY(()) align_stack
{
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, %E) encountered without matching #pragma pack(push, %E)"
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 %qE for %<#pragma pack%> - ignored", x
);
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 GTY(()) def_pragma_macro_value
{
248 struct def_pragma_macro_value
*prev
;
252 struct GTY(()) def_pragma_macro
{
255 struct def_pragma_macro_value value
;
258 static GTY((param_is (struct def_pragma_macro
))) htab_t pushed_macro_table
;
260 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
261 /* Hash table control functions for pushed_macro_table. */
263 dpm_hash (const void *p
)
265 return ((const struct def_pragma_macro
*)p
)->hash
;
269 dpm_eq (const void *pa
, const void *pb
)
271 const struct def_pragma_macro
*const a
= (const struct def_pragma_macro
*) pa
,
272 *const b
= (const struct def_pragma_macro
*) pb
;
273 return a
->hash
== b
->hash
&& strcmp (a
->name
, b
->name
) == 0;
276 /* #pragma push_macro("MACRO_NAME")
277 #pragma pop_macro("MACRO_NAME") */
280 handle_pragma_push_macro (cpp_reader
*reader
)
283 enum cpp_ttype token
;
284 struct def_pragma_macro dummy
, *c
;
285 const char *macroname
;
288 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
289 GCC_BAD ("missing %<(%> after %<#pragma push_macro%> - ignored");
291 token
= pragma_lex (&id
);
293 /* Silently ignore */
294 if (token
== CPP_CLOSE_PAREN
)
296 if (token
!= CPP_STRING
)
297 GCC_BAD ("invalid constant in %<#pragma push_macro%> - ignored");
299 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
300 GCC_BAD ("missing %<)%> after %<#pragma push_macro%> - ignored");
302 if (pragma_lex (&x
) != CPP_EOF
)
303 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_macro%>");
305 /* Check for empty string, and silently ignore. */
306 if (TREE_STRING_LENGTH (id
) < 1)
308 macroname
= TREE_STRING_POINTER (id
);
310 if (pushed_macro_table
== NULL
)
311 pushed_macro_table
= htab_create_ggc (15, dpm_hash
, dpm_eq
, 0);
313 dummy
.hash
= htab_hash_string (macroname
);
314 dummy
.name
= macroname
;
315 slot
= htab_find_slot_with_hash (pushed_macro_table
, &dummy
,
317 c
= (struct def_pragma_macro
*) *slot
;
320 *slot
= c
= GGC_NEW (struct def_pragma_macro
);
321 c
->hash
= dummy
.hash
;
322 c
->name
= ggc_alloc_string (macroname
, TREE_STRING_LENGTH (id
) - 1);
323 c
->value
.prev
= NULL
;
327 struct def_pragma_macro_value
*v
;
328 v
= GGC_NEW (struct def_pragma_macro_value
);
333 c
->value
.value
= cpp_push_definition (reader
, macroname
);
337 handle_pragma_pop_macro (cpp_reader
*reader
)
340 enum cpp_ttype token
;
341 struct def_pragma_macro dummy
, *c
;
342 const char *macroname
;
345 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
346 GCC_BAD ("missing %<(%> after %<#pragma pop_macro%> - ignored");
348 token
= pragma_lex (&id
);
350 /* Silently ignore */
351 if (token
== CPP_CLOSE_PAREN
)
353 if (token
!= CPP_STRING
)
354 GCC_BAD ("invalid constant in %<#pragma pop_macro%> - ignored");
356 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
357 GCC_BAD ("missing %<)%> after %<#pragma pop_macro%> - ignored");
359 if (pragma_lex (&x
) != CPP_EOF
)
360 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_macro%>");
362 /* Check for empty string, and silently ignore. */
363 if (TREE_STRING_LENGTH (id
) < 1)
365 macroname
= TREE_STRING_POINTER (id
);
367 dummy
.hash
= htab_hash_string (macroname
);
368 dummy
.name
= macroname
;
369 if (pushed_macro_table
)
370 slot
= htab_find_slot_with_hash (pushed_macro_table
, &dummy
,
371 dummy
.hash
, NO_INSERT
);
374 c
= (struct def_pragma_macro
*) *slot
;
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 (UNKNOWN_LOCATION
,
460 FUNCTION_DECL
, alias_id
, default_function_type
);
462 DECL_ARTIFICIAL (decl
) = 1;
463 TREE_PUBLIC (decl
) = 1;
464 DECL_EXTERNAL (decl
) = 1;
465 DECL_WEAK (decl
) = 1;
467 assemble_alias (decl
, id
);
471 /* #pragma weak name [= value] */
473 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
475 tree name
, value
, x
, decl
;
480 if (pragma_lex (&name
) != CPP_NAME
)
481 GCC_BAD ("malformed #pragma weak, ignored");
485 if (pragma_lex (&value
) != CPP_NAME
)
486 GCC_BAD ("malformed #pragma weak, ignored");
490 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
492 decl
= identifier_global_value (name
);
493 if (decl
&& DECL_P (decl
))
495 apply_pragma_weak (decl
, value
);
497 assemble_alias (decl
, value
);
500 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
504 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
509 maybe_apply_pending_pragma_weaks (void)
512 #endif /* HANDLE_PRAGMA_WEAK */
514 /* GCC supports two #pragma directives for renaming the external
515 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
516 compatibility with the Solaris and Tru64 system headers. GCC also
517 has its own notation for this, __asm__("name") annotations.
519 Corner cases of these features and their interaction:
521 1) Both pragmas silently apply only to declarations with external
522 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
523 do not have this restriction.
525 2) In C++, both #pragmas silently apply only to extern "C" declarations.
526 Asm labels do not have this restriction.
528 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
529 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
530 new name is different, a warning issues and the name does not change.
532 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
533 *not* the DECL_ASSEMBLER_NAME.
535 5) If #pragma extern_prefix is in effect and a declaration occurs
536 with an __asm__ name, the #pragma extern_prefix is silently
537 ignored for that declaration.
539 6) If #pragma extern_prefix and #pragma redefine_extname apply to
540 the same declaration, whichever triggered first wins, and a warning
541 is issued. (We would like to have #pragma redefine_extname always
542 win, but it can appear either before or after the declaration, and
543 if it appears afterward, we have no way of knowing whether a modified
544 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
546 static GTY(()) tree pending_redefine_extname
;
548 static void handle_pragma_redefine_extname (cpp_reader
*);
550 /* #pragma redefine_extname oldname newname */
552 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
554 tree oldname
, newname
, decl
, x
;
557 if (pragma_lex (&oldname
) != CPP_NAME
)
558 GCC_BAD ("malformed #pragma redefine_extname, ignored");
559 if (pragma_lex (&newname
) != CPP_NAME
)
560 GCC_BAD ("malformed #pragma redefine_extname, ignored");
563 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
565 decl
= identifier_global_value (oldname
);
567 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
568 && (TREE_CODE (decl
) == FUNCTION_DECL
569 || TREE_CODE (decl
) == VAR_DECL
)
570 && has_c_linkage (decl
))
572 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
574 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
575 name
= targetm
.strip_name_encoding (name
);
577 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
578 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
579 "conflict with previous rename");
582 change_decl_assembler_name (decl
, newname
);
585 /* We have to add this to the rename list even if there's already
586 a global value that doesn't meet the above criteria, because in
587 C++ "struct foo {...};" puts "foo" in the current namespace but
588 does *not* conflict with a subsequent declaration of a function
589 or variable foo. See g++.dg/other/pragma-re-2.C. */
590 add_to_renaming_pragma_list (oldname
, newname
);
593 /* This is called from here and from ia64.c. */
595 add_to_renaming_pragma_list (tree oldname
, tree newname
)
597 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
600 if (TREE_VALUE (previous
) != newname
)
601 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
602 "conflict with previous #pragma redefine_extname");
606 pending_redefine_extname
607 = tree_cons (oldname
, newname
, pending_redefine_extname
);
610 static GTY(()) tree pragma_extern_prefix
;
612 /* #pragma extern_prefix "prefix" */
614 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
619 if (pragma_lex (&prefix
) != CPP_STRING
)
620 GCC_BAD ("malformed #pragma extern_prefix, ignored");
623 warning (OPT_Wpragmas
, "junk at end of %<#pragma extern_prefix%>");
625 if (targetm
.handle_pragma_extern_prefix
)
626 /* Note that the length includes the null terminator. */
627 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
628 else if (warn_unknown_pragmas
> in_system_header
)
629 warning (OPT_Wunknown_pragmas
,
630 "#pragma extern_prefix not supported on this target");
633 /* Hook from the front ends to apply the results of one of the preceding
634 pragmas that rename variables. */
637 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
641 /* The renaming pragmas are only applied to declarations with
643 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
644 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
645 || !has_c_linkage (decl
))
648 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
649 but we may warn about a rename that conflicts. */
650 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
652 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
653 oldname
= targetm
.strip_name_encoding (oldname
);
655 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
656 warning (OPT_Wpragmas
, "asm declaration ignored due to "
657 "conflict with previous rename");
659 /* Take any pending redefine_extname off the list. */
660 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
661 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
663 /* Only warn if there is a conflict. */
664 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
665 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
666 "conflict with previous rename");
674 /* Find out if we have a pending #pragma redefine_extname. */
675 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
676 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
678 tree newname
= TREE_VALUE (t
);
681 /* If we already have an asmname, #pragma redefine_extname is
682 ignored (with a warning if it conflicts). */
685 if (strcmp (TREE_STRING_POINTER (asmname
),
686 IDENTIFIER_POINTER (newname
)) != 0)
687 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
688 "conflict with __asm__ declaration");
692 /* Otherwise we use what we've got; #pragma extern_prefix is
694 return build_string (IDENTIFIER_LENGTH (newname
),
695 IDENTIFIER_POINTER (newname
));
698 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
702 /* If #pragma extern_prefix is in effect, apply it. */
703 if (pragma_extern_prefix
)
705 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
706 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
708 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
709 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
711 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
713 memcpy (newname
, prefix
, plen
);
714 memcpy (newname
+ plen
, id
, ilen
+ 1);
716 return build_string (plen
+ ilen
, newname
);
724 #ifdef HANDLE_PRAGMA_VISIBILITY
725 static void handle_pragma_visibility (cpp_reader
*);
727 static VEC (int, heap
) *visstack
;
729 /* Push the visibility indicated by STR onto the top of the #pragma
730 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
731 C++ namespace with visibility attribute and 2 for C++ builtin
732 ABI namespace. push_visibility/pop_visibility calls must have
733 matching KIND, it is not allowed to push visibility using one
734 KIND and pop using a different one. */
737 push_visibility (const char *str
, int kind
)
739 VEC_safe_push (int, heap
, visstack
,
740 ((int) default_visibility
) | (kind
<< 8));
741 if (!strcmp (str
, "default"))
742 default_visibility
= VISIBILITY_DEFAULT
;
743 else if (!strcmp (str
, "internal"))
744 default_visibility
= VISIBILITY_INTERNAL
;
745 else if (!strcmp (str
, "hidden"))
746 default_visibility
= VISIBILITY_HIDDEN
;
747 else if (!strcmp (str
, "protected"))
748 default_visibility
= VISIBILITY_PROTECTED
;
750 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
751 visibility_options
.inpragma
= 1;
754 /* Pop a level of the #pragma visibility stack. Return true if
758 pop_visibility (int kind
)
760 if (!VEC_length (int, visstack
))
762 if ((VEC_last (int, visstack
) >> 8) != kind
)
765 = (enum symbol_visibility
) (VEC_pop (int, visstack
) & 0xff);
766 visibility_options
.inpragma
767 = VEC_length (int, visstack
) != 0;
771 /* Sets the default visibility for symbols to something other than that
772 specified on the command line. */
775 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
777 /* Form is #pragma GCC visibility push(hidden)|pop */
779 enum cpp_ttype token
;
780 enum { bad
, push
, pop
} action
= bad
;
782 token
= pragma_lex (&x
);
783 if (token
== CPP_NAME
)
785 const char *op
= IDENTIFIER_POINTER (x
);
786 if (!strcmp (op
, "push"))
788 else if (!strcmp (op
, "pop"))
792 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
797 if (! pop_visibility (0))
798 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
802 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
803 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
804 token
= pragma_lex (&x
);
805 if (token
!= CPP_NAME
)
806 GCC_BAD ("malformed #pragma GCC visibility push");
808 push_visibility (IDENTIFIER_POINTER (x
), 0);
809 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
810 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
813 if (pragma_lex (&x
) != CPP_EOF
)
814 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
820 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
822 const char *kind_string
, *option_string
;
823 unsigned int option_index
;
824 enum cpp_ttype token
;
830 error ("#pragma GCC diagnostic not allowed inside functions");
834 token
= pragma_lex (&x
);
835 if (token
!= CPP_NAME
)
836 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
837 kind_string
= IDENTIFIER_POINTER (x
);
838 if (strcmp (kind_string
, "error") == 0)
840 else if (strcmp (kind_string
, "warning") == 0)
842 else if (strcmp (kind_string
, "ignored") == 0)
845 GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
847 token
= pragma_lex (&x
);
848 if (token
!= CPP_STRING
)
849 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
850 option_string
= TREE_STRING_POINTER (x
);
851 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
852 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
854 /* This overrides -Werror, for example. */
855 diagnostic_classify_diagnostic (global_dc
, option_index
, kind
);
856 /* This makes sure the option is enabled, like -Wfoo would do. */
857 if (cl_options
[option_index
].var_type
== CLVC_BOOLEAN
858 && cl_options
[option_index
].flag_var
859 && kind
!= DK_IGNORED
)
860 *(int *) cl_options
[option_index
].flag_var
= 1;
863 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
866 /* Parse #pragma GCC target (xxx) to set target specific options. */
868 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
870 enum cpp_ttype token
;
872 bool close_paren_needed_p
= false;
876 error ("#pragma GCC option is not allowed inside functions");
880 token
= pragma_lex (&x
);
881 if (token
== CPP_OPEN_PAREN
)
883 close_paren_needed_p
= true;
884 token
= pragma_lex (&x
);
887 if (token
!= CPP_STRING
)
889 GCC_BAD ("%<#pragma GCC option%> is not a string");
893 /* Strings are user options. */
896 tree args
= NULL_TREE
;
900 /* Build up the strings now as a tree linked list. Skip empty
902 if (TREE_STRING_LENGTH (x
) > 0)
903 args
= tree_cons (NULL_TREE
, x
, args
);
905 token
= pragma_lex (&x
);
906 while (token
== CPP_COMMA
)
907 token
= pragma_lex (&x
);
909 while (token
== CPP_STRING
);
911 if (close_paren_needed_p
)
913 if (token
== CPP_CLOSE_PAREN
)
914 token
= pragma_lex (&x
);
916 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
917 "not have a final %<)%>.");
920 if (token
!= CPP_EOF
)
922 error ("#pragma GCC target string... is badly formed");
926 /* put arguments in the order the user typed them. */
927 args
= nreverse (args
);
929 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
930 current_target_pragma
= args
;
934 /* Handle #pragma GCC optimize to set optimization options. */
936 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
938 enum cpp_ttype token
;
940 bool close_paren_needed_p
= false;
941 tree optimization_previous_node
= optimization_current_node
;
945 error ("#pragma GCC optimize is not allowed inside functions");
949 token
= pragma_lex (&x
);
950 if (token
== CPP_OPEN_PAREN
)
952 close_paren_needed_p
= true;
953 token
= pragma_lex (&x
);
956 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
958 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
962 /* Strings/numbers are user options. */
965 tree args
= NULL_TREE
;
969 /* Build up the numbers/strings now as a list. */
970 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
971 args
= tree_cons (NULL_TREE
, x
, args
);
973 token
= pragma_lex (&x
);
974 while (token
== CPP_COMMA
)
975 token
= pragma_lex (&x
);
977 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
979 if (close_paren_needed_p
)
981 if (token
== CPP_CLOSE_PAREN
)
982 token
= pragma_lex (&x
);
984 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
985 "not have a final %<)%>.");
988 if (token
!= CPP_EOF
)
990 error ("#pragma GCC optimize string... is badly formed");
994 /* put arguments in the order the user typed them. */
995 args
= nreverse (args
);
997 parse_optimize_options (args
, false);
998 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
999 optimization_current_node
= build_optimization_node ();
1000 c_cpp_builtins_optimize_pragma (parse_in
,
1001 optimization_previous_node
,
1002 optimization_current_node
);
1006 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1007 both the binary representation of the options and the TREE_LIST of
1008 strings that will be added to the function's attribute list. */
1009 typedef struct GTY(()) opt_stack
{
1010 struct opt_stack
*prev
;
1012 tree target_strings
;
1013 tree optimize_binary
;
1014 tree optimize_strings
;
1017 static GTY(()) struct opt_stack
* options_stack
;
1019 /* Handle #pragma GCC push_options to save the current target and optimization
1023 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
1025 enum cpp_ttype token
;
1029 token
= pragma_lex (&x
);
1030 if (token
!= CPP_EOF
)
1032 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
1036 p
= GGC_NEW (opt_stack
);
1037 p
->prev
= options_stack
;
1040 /* Save optimization and target flags in binary format. */
1041 p
->optimize_binary
= build_optimization_node ();
1042 p
->target_binary
= build_target_option_node ();
1044 /* Save optimization and target flags in string list format. */
1045 p
->optimize_strings
= copy_list (current_optimize_pragma
);
1046 p
->target_strings
= copy_list (current_target_pragma
);
1049 /* Handle #pragma GCC pop_options to restore the current target and
1050 optimization options from a previous push_options. */
1053 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
1055 enum cpp_ttype token
;
1059 token
= pragma_lex (&x
);
1060 if (token
!= CPP_EOF
)
1062 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
1066 if (! options_stack
)
1068 warning (OPT_Wpragmas
,
1069 "%<#pragma GCC pop_options%> without a corresponding "
1070 "%<#pragma GCC push_options%>");
1075 options_stack
= p
->prev
;
1077 if (p
->target_binary
!= target_option_current_node
)
1079 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
1080 target_option_current_node
= p
->target_binary
;
1083 if (p
->optimize_binary
!= optimization_current_node
)
1085 tree old_optimize
= optimization_current_node
;
1086 cl_optimization_restore (TREE_OPTIMIZATION (p
->optimize_binary
));
1087 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
1088 p
->optimize_binary
);
1089 optimization_current_node
= p
->optimize_binary
;
1092 current_target_pragma
= p
->target_strings
;
1093 current_optimize_pragma
= p
->optimize_strings
;
1096 /* Handle #pragma GCC reset_options to restore the current target and
1097 optimization options to the original options used on the command line. */
1100 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
1102 enum cpp_ttype token
;
1104 tree new_optimize
= optimization_default_node
;
1105 tree new_target
= target_option_default_node
;
1107 token
= pragma_lex (&x
);
1108 if (token
!= CPP_EOF
)
1110 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1114 if (new_target
!= target_option_current_node
)
1116 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1117 target_option_current_node
= new_target
;
1120 if (new_optimize
!= optimization_current_node
)
1122 tree old_optimize
= optimization_current_node
;
1123 cl_optimization_restore (TREE_OPTIMIZATION (new_optimize
));
1124 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1125 optimization_current_node
= new_optimize
;
1128 current_target_pragma
= NULL_TREE
;
1129 current_optimize_pragma
= NULL_TREE
;
1132 /* Print a plain user-specified message. */
1135 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1137 enum cpp_ttype token
;
1138 tree x
, message
= 0;
1140 token
= pragma_lex (&x
);
1141 if (token
== CPP_OPEN_PAREN
)
1143 token
= pragma_lex (&x
);
1144 if (token
== CPP_STRING
)
1147 GCC_BAD ("expected a string after %<#pragma message%>");
1148 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1149 GCC_BAD ("malformed %<#pragma message%>, ignored");
1151 else if (token
== CPP_STRING
)
1154 GCC_BAD ("expected a string after %<#pragma message%>");
1156 gcc_assert (message
);
1158 if (pragma_lex (&x
) != CPP_EOF
)
1159 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1161 if (TREE_STRING_LENGTH (message
) > 1)
1162 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1165 /* Mark whether the current location is valid for a STDC pragma. */
1167 static bool valid_location_for_stdc_pragma
;
1170 mark_valid_location_for_stdc_pragma (bool flag
)
1172 valid_location_for_stdc_pragma
= flag
;
1175 /* Return true if the current location is valid for a STDC pragma. */
1178 valid_location_for_stdc_pragma_p (void)
1180 return valid_location_for_stdc_pragma
;
1183 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1185 /* A STDC pragma must appear outside of external declarations or
1186 preceding all explicit declarations and statements inside a compound
1187 statement; its behavior is undefined if used in any other context.
1188 It takes a switch of ON, OFF, or DEFAULT. */
1190 static enum pragma_switch_t
1191 handle_stdc_pragma (const char *pname
)
1195 enum pragma_switch_t ret
;
1197 if (!valid_location_for_stdc_pragma_p ())
1199 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1204 if (pragma_lex (&t
) != CPP_NAME
)
1206 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1210 arg
= IDENTIFIER_POINTER (t
);
1212 if (!strcmp (arg
, "ON"))
1214 else if (!strcmp (arg
, "OFF"))
1216 else if (!strcmp (arg
, "DEFAULT"))
1217 ret
= PRAGMA_DEFAULT
;
1220 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1224 if (pragma_lex (&t
) != CPP_EOF
)
1226 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1233 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1234 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1235 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1238 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1240 if (c_dialect_cxx ())
1242 if (warn_unknown_pragmas
> in_system_header
)
1243 warning (OPT_Wunknown_pragmas
,
1244 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1249 if (!targetm
.decimal_float_supported_p ())
1251 if (warn_unknown_pragmas
> in_system_header
)
1252 warning (OPT_Wunknown_pragmas
,
1253 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1258 pedwarn (input_location
, OPT_pedantic
,
1259 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1261 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1264 set_float_const_decimal64 ();
1267 case PRAGMA_DEFAULT
:
1268 clear_float_const_decimal64 ();
1275 /* A vector of registered pragma callbacks. */
1277 DEF_VEC_O (pragma_handler
);
1278 DEF_VEC_ALLOC_O (pragma_handler
, heap
);
1280 static VEC(pragma_handler
, heap
) *registered_pragmas
;
1288 DEF_VEC_O (pragma_ns_name
);
1289 DEF_VEC_ALLOC_O (pragma_ns_name
, heap
);
1291 static VEC(pragma_ns_name
, heap
) *registered_pp_pragmas
;
1293 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1294 static const struct omp_pragma_def omp_pragmas
[] = {
1295 { "atomic", PRAGMA_OMP_ATOMIC
},
1296 { "barrier", PRAGMA_OMP_BARRIER
},
1297 { "critical", PRAGMA_OMP_CRITICAL
},
1298 { "flush", PRAGMA_OMP_FLUSH
},
1299 { "for", PRAGMA_OMP_FOR
},
1300 { "master", PRAGMA_OMP_MASTER
},
1301 { "ordered", PRAGMA_OMP_ORDERED
},
1302 { "parallel", PRAGMA_OMP_PARALLEL
},
1303 { "section", PRAGMA_OMP_SECTION
},
1304 { "sections", PRAGMA_OMP_SECTIONS
},
1305 { "single", PRAGMA_OMP_SINGLE
},
1306 { "task", PRAGMA_OMP_TASK
},
1307 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1308 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1312 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1314 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1317 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1318 if (omp_pragmas
[i
].id
== id
)
1321 *name
= omp_pragmas
[i
].name
;
1325 if (id
>= PRAGMA_FIRST_EXTERNAL
1326 && (id
< PRAGMA_FIRST_EXTERNAL
1327 + VEC_length (pragma_ns_name
, registered_pp_pragmas
)))
1329 *space
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1330 id
- PRAGMA_FIRST_EXTERNAL
)->space
;
1331 *name
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1332 id
- PRAGMA_FIRST_EXTERNAL
)->name
;
1339 /* Front-end wrappers for pragma registration to avoid dragging
1340 cpplib.h in almost everywhere. */
1343 c_register_pragma_1 (const char *space
, const char *name
,
1344 pragma_handler handler
, bool allow_expansion
)
1348 if (flag_preprocess_only
)
1350 pragma_ns_name ns_name
;
1352 if (!allow_expansion
)
1355 ns_name
.space
= space
;
1356 ns_name
.name
= name
;
1357 VEC_safe_push (pragma_ns_name
, heap
, registered_pp_pragmas
, &ns_name
);
1358 id
= VEC_length (pragma_ns_name
, registered_pp_pragmas
);
1359 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1363 VEC_safe_push (pragma_handler
, heap
, registered_pragmas
, &handler
);
1364 id
= VEC_length (pragma_handler
, registered_pragmas
);
1365 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1367 /* The C++ front end allocates 6 bits in cp_token; the C front end
1368 allocates 7 bits in c_token. At present this is sufficient. */
1369 gcc_assert (id
< 64);
1372 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1373 allow_expansion
, false);
1377 c_register_pragma (const char *space
, const char *name
, pragma_handler handler
)
1379 c_register_pragma_1 (space
, name
, handler
, false);
1383 c_register_pragma_with_expansion (const char *space
, const char *name
,
1384 pragma_handler handler
)
1386 c_register_pragma_1 (space
, name
, handler
, true);
1390 c_invoke_pragma_handler (unsigned int id
)
1392 pragma_handler handler
;
1394 id
-= PRAGMA_FIRST_EXTERNAL
;
1395 handler
= *VEC_index (pragma_handler
, registered_pragmas
, id
);
1400 /* Set up front-end pragmas. */
1406 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1409 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1410 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1411 omp_pragmas
[i
].id
, true, true);
1414 if (!flag_preprocess_only
)
1415 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1416 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1418 #ifdef HANDLE_PRAGMA_PACK
1419 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1420 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1422 c_register_pragma (0, "pack", handle_pragma_pack
);
1425 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
1426 c_register_pragma (0 ,"push_macro", handle_pragma_push_macro
);
1427 c_register_pragma (0 ,"pop_macro", handle_pragma_pop_macro
);
1429 #ifdef HANDLE_PRAGMA_WEAK
1430 c_register_pragma (0, "weak", handle_pragma_weak
);
1432 #ifdef HANDLE_PRAGMA_VISIBILITY
1433 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1436 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1437 c_register_pragma ("GCC", "target", handle_pragma_target
);
1438 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1439 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1440 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1441 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1443 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1444 handle_pragma_float_const_decimal64
);
1446 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname
);
1447 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
1449 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1451 #ifdef REGISTER_TARGET_PRAGMAS
1452 REGISTER_TARGET_PRAGMAS ();
1455 /* Allow plugins to register their own pragmas. */
1456 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1459 #include "gt-c-pragma.h"