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, 2009, 2010 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"
26 #include "function.h" /* For cfun. FIXME: Does the parser know
27 when it is inside a function, so that
28 we don't have to look at cfun? */
34 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
35 this not a target hook?). */
39 #include "diagnostic.h"
43 #define GCC_BAD(gmsgid) \
44 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
45 #define GCC_BAD2(gmsgid, arg) \
46 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
48 typedef struct GTY(()) align_stack
{
51 struct align_stack
* prev
;
54 static GTY(()) struct align_stack
* alignment_stack
;
56 static void handle_pragma_pack (cpp_reader
*);
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_alloc_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
;
130 #pragma pack (push, N)
131 #pragma pack (push, ID)
132 #pragma pack (push, ID, N)
134 #pragma pack (pop, ID) */
136 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
140 enum cpp_ttype token
;
141 enum { set
, push
, pop
} action
;
143 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
144 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
146 token
= pragma_lex (&x
);
147 if (token
== CPP_CLOSE_PAREN
)
150 align
= initial_max_fld_align
;
152 else if (token
== CPP_NUMBER
)
154 if (TREE_CODE (x
) != INTEGER_CST
)
155 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
156 align
= TREE_INT_CST_LOW (x
);
158 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
159 GCC_BAD ("malformed %<#pragma pack%> - ignored");
161 else if (token
== CPP_NAME
)
163 #define GCC_BAD_ACTION do { if (action != pop) \
164 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
166 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
169 const char *op
= IDENTIFIER_POINTER (x
);
170 if (!strcmp (op
, "push"))
172 else if (!strcmp (op
, "pop"))
175 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
177 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
179 token
= pragma_lex (&x
);
180 if (token
== CPP_NAME
&& id
== 0)
184 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
186 if (TREE_CODE (x
) != INTEGER_CST
)
187 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
188 align
= TREE_INT_CST_LOW (x
);
196 if (token
!= CPP_CLOSE_PAREN
)
198 #undef GCC_BAD_ACTION
201 GCC_BAD ("malformed %<#pragma pack%> - ignored");
203 if (pragma_lex (&x
) != CPP_EOF
)
204 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
206 if (flag_pack_struct
)
207 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
218 align
*= BITS_PER_UNIT
;
223 align
= maximum_field_alignment
;
227 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
232 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
233 case push
: push_alignment (align
, id
); break;
234 case pop
: pop_alignment (id
); break;
238 typedef struct GTY(()) pending_weak_d
244 DEF_VEC_O(pending_weak
);
245 DEF_VEC_ALLOC_O(pending_weak
,gc
);
247 static GTY(()) VEC(pending_weak
,gc
) *pending_weaks
;
249 static void apply_pragma_weak (tree
, tree
);
250 static void handle_pragma_weak (cpp_reader
*);
253 apply_pragma_weak (tree decl
, tree value
)
257 value
= build_string (IDENTIFIER_LENGTH (value
),
258 IDENTIFIER_POINTER (value
));
259 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
260 build_tree_list (NULL
, value
)),
264 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
265 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
266 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
267 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
268 "results in unspecified behavior", decl
);
274 maybe_apply_pragma_weak (tree decl
)
280 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
282 /* No weak symbols pending, take the short-cut. */
285 /* If it's not visible outside this file, it doesn't matter whether
287 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
289 /* If it's not a function or a variable, it can't be weak.
290 FIXME: what kinds of things are visible outside this file but
291 aren't functions or variables? Should this be an assert instead? */
292 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
295 id
= DECL_ASSEMBLER_NAME (decl
);
297 FOR_EACH_VEC_ELT (pending_weak
, pending_weaks
, i
, pe
)
300 apply_pragma_weak (decl
, pe
->value
);
301 VEC_unordered_remove (pending_weak
, pending_weaks
, i
);
306 /* Process all "#pragma weak A = B" directives where we have not seen
309 maybe_apply_pending_pragma_weaks (void)
311 tree alias_id
, id
, decl
;
315 FOR_EACH_VEC_ELT (pending_weak
, pending_weaks
, i
, pe
)
323 decl
= build_decl (UNKNOWN_LOCATION
,
324 FUNCTION_DECL
, alias_id
, default_function_type
);
326 DECL_ARTIFICIAL (decl
) = 1;
327 TREE_PUBLIC (decl
) = 1;
328 DECL_EXTERNAL (decl
) = 1;
329 DECL_WEAK (decl
) = 1;
331 assemble_alias (decl
, id
);
335 /* #pragma weak name [= value] */
337 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
339 tree name
, value
, x
, decl
;
344 if (pragma_lex (&name
) != CPP_NAME
)
345 GCC_BAD ("malformed #pragma weak, ignored");
349 if (pragma_lex (&value
) != CPP_NAME
)
350 GCC_BAD ("malformed #pragma weak, ignored");
354 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
356 decl
= identifier_global_value (name
);
357 if (decl
&& DECL_P (decl
))
359 apply_pragma_weak (decl
, value
);
361 assemble_alias (decl
, value
);
366 pe
= VEC_safe_push (pending_weak
, gc
, pending_weaks
, NULL
);
372 /* GCC supports two #pragma directives for renaming the external
373 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
374 compatibility with the Solaris and Tru64 system headers. GCC also
375 has its own notation for this, __asm__("name") annotations.
377 Corner cases of these features and their interaction:
379 1) Both pragmas silently apply only to declarations with external
380 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
381 do not have this restriction.
383 2) In C++, both #pragmas silently apply only to extern "C" declarations.
384 Asm labels do not have this restriction.
386 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
387 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
388 new name is different, a warning issues and the name does not change.
390 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
391 *not* the DECL_ASSEMBLER_NAME.
393 5) If #pragma extern_prefix is in effect and a declaration occurs
394 with an __asm__ name, the #pragma extern_prefix is silently
395 ignored for that declaration.
397 6) If #pragma extern_prefix and #pragma redefine_extname apply to
398 the same declaration, whichever triggered first wins, and a warning
399 is issued. (We would like to have #pragma redefine_extname always
400 win, but it can appear either before or after the declaration, and
401 if it appears afterward, we have no way of knowing whether a modified
402 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
404 typedef struct GTY(()) pending_redefinition_d
{
407 } pending_redefinition
;
409 DEF_VEC_O(pending_redefinition
);
410 DEF_VEC_ALLOC_O(pending_redefinition
,gc
);
412 static GTY(()) VEC(pending_redefinition
,gc
) *pending_redefine_extname
;
414 static void handle_pragma_redefine_extname (cpp_reader
*);
416 /* #pragma redefine_extname oldname newname */
418 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
420 tree oldname
, newname
, decls
, x
;
424 if (pragma_lex (&oldname
) != CPP_NAME
)
425 GCC_BAD ("malformed #pragma redefine_extname, ignored");
426 if (pragma_lex (&newname
) != CPP_NAME
)
427 GCC_BAD ("malformed #pragma redefine_extname, ignored");
430 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
433 for (decls
= c_linkage_bindings (oldname
);
437 if (TREE_CODE (decls
) == TREE_LIST
)
439 decl
= TREE_VALUE (decls
);
440 decls
= TREE_CHAIN (decls
);
448 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
449 && (TREE_CODE (decl
) == FUNCTION_DECL
450 || TREE_CODE (decl
) == VAR_DECL
))
453 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
455 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
456 name
= targetm
.strip_name_encoding (name
);
458 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
459 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
460 "conflict with previous rename");
463 change_decl_assembler_name (decl
, newname
);
468 /* We have to add this to the rename list even if there's already
469 a global value that doesn't meet the above criteria, because in
470 C++ "struct foo {...};" puts "foo" in the current namespace but
471 does *not* conflict with a subsequent declaration of a function
472 or variable foo. See g++.dg/other/pragma-re-2.C. */
473 add_to_renaming_pragma_list (oldname
, newname
);
476 /* This is called from here and from ia64.c. */
478 add_to_renaming_pragma_list (tree oldname
, tree newname
)
481 pending_redefinition
*p
;
483 FOR_EACH_VEC_ELT (pending_redefinition
, pending_redefine_extname
, ix
, p
)
484 if (oldname
== p
->oldname
)
486 if (p
->newname
!= newname
)
487 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
488 "conflict with previous #pragma redefine_extname");
492 p
= VEC_safe_push (pending_redefinition
, gc
, pending_redefine_extname
, NULL
);
493 p
->oldname
= oldname
;
494 p
->newname
= newname
;
497 static GTY(()) tree pragma_extern_prefix
;
499 /* #pragma extern_prefix "prefix" */
501 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
506 if (pragma_lex (&prefix
) != CPP_STRING
)
507 GCC_BAD ("malformed #pragma extern_prefix, ignored");
510 warning (OPT_Wpragmas
, "junk at end of %<#pragma extern_prefix%>");
512 if (targetm
.handle_pragma_extern_prefix
)
513 /* Note that the length includes the null terminator. */
514 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
515 else if (warn_unknown_pragmas
> in_system_header
)
516 warning (OPT_Wunknown_pragmas
,
517 "#pragma extern_prefix not supported on this target");
520 /* Hook from the front ends to apply the results of one of the preceding
521 pragmas that rename variables. */
524 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
527 pending_redefinition
*p
;
529 /* The renaming pragmas are only applied to declarations with
531 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
532 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
533 || !has_c_linkage (decl
))
536 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
537 but we may warn about a rename that conflicts. */
538 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
540 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
541 oldname
= targetm
.strip_name_encoding (oldname
);
543 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
544 warning (OPT_Wpragmas
, "asm declaration ignored due to "
545 "conflict with previous rename");
547 /* Take any pending redefine_extname off the list. */
548 FOR_EACH_VEC_ELT (pending_redefinition
, pending_redefine_extname
, ix
, p
)
549 if (DECL_NAME (decl
) == p
->oldname
)
551 /* Only warn if there is a conflict. */
552 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
553 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
554 "conflict with previous rename");
556 VEC_unordered_remove (pending_redefinition
,
557 pending_redefine_extname
, ix
);
563 /* Find out if we have a pending #pragma redefine_extname. */
564 FOR_EACH_VEC_ELT (pending_redefinition
, pending_redefine_extname
, ix
, p
)
565 if (DECL_NAME (decl
) == p
->oldname
)
567 tree newname
= p
->newname
;
568 VEC_unordered_remove (pending_redefinition
,
569 pending_redefine_extname
, ix
);
571 /* If we already have an asmname, #pragma redefine_extname is
572 ignored (with a warning if it conflicts). */
575 if (strcmp (TREE_STRING_POINTER (asmname
),
576 IDENTIFIER_POINTER (newname
)) != 0)
577 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
578 "conflict with __asm__ declaration");
582 /* Otherwise we use what we've got; #pragma extern_prefix is
584 return build_string (IDENTIFIER_LENGTH (newname
),
585 IDENTIFIER_POINTER (newname
));
588 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
592 /* If #pragma extern_prefix is in effect, apply it. */
593 if (pragma_extern_prefix
)
595 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
596 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
598 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
599 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
601 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
603 memcpy (newname
, prefix
, plen
);
604 memcpy (newname
+ plen
, id
, ilen
+ 1);
606 return build_string (plen
+ ilen
, newname
);
614 static void handle_pragma_visibility (cpp_reader
*);
616 static VEC (int, heap
) *visstack
;
618 /* Push the visibility indicated by STR onto the top of the #pragma
619 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
620 C++ namespace with visibility attribute and 2 for C++ builtin
621 ABI namespace. push_visibility/pop_visibility calls must have
622 matching KIND, it is not allowed to push visibility using one
623 KIND and pop using a different one. */
626 push_visibility (const char *str
, int kind
)
628 VEC_safe_push (int, heap
, visstack
,
629 ((int) default_visibility
) | (kind
<< 8));
630 if (!strcmp (str
, "default"))
631 default_visibility
= VISIBILITY_DEFAULT
;
632 else if (!strcmp (str
, "internal"))
633 default_visibility
= VISIBILITY_INTERNAL
;
634 else if (!strcmp (str
, "hidden"))
635 default_visibility
= VISIBILITY_HIDDEN
;
636 else if (!strcmp (str
, "protected"))
637 default_visibility
= VISIBILITY_PROTECTED
;
639 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
640 visibility_options
.inpragma
= 1;
643 /* Pop a level of the #pragma visibility stack. Return true if
647 pop_visibility (int kind
)
649 if (!VEC_length (int, visstack
))
651 if ((VEC_last (int, visstack
) >> 8) != kind
)
654 = (enum symbol_visibility
) (VEC_pop (int, visstack
) & 0xff);
655 visibility_options
.inpragma
656 = VEC_length (int, visstack
) != 0;
660 /* Sets the default visibility for symbols to something other than that
661 specified on the command line. */
664 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
666 /* Form is #pragma GCC visibility push(hidden)|pop */
668 enum cpp_ttype token
;
669 enum { bad
, push
, pop
} action
= bad
;
671 token
= pragma_lex (&x
);
672 if (token
== CPP_NAME
)
674 const char *op
= IDENTIFIER_POINTER (x
);
675 if (!strcmp (op
, "push"))
677 else if (!strcmp (op
, "pop"))
681 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
686 if (! pop_visibility (0))
687 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
691 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
692 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
693 token
= pragma_lex (&x
);
694 if (token
!= CPP_NAME
)
695 GCC_BAD ("malformed #pragma GCC visibility push");
697 push_visibility (IDENTIFIER_POINTER (x
), 0);
698 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
699 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
702 if (pragma_lex (&x
) != CPP_EOF
)
703 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
707 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
709 const char *kind_string
, *option_string
;
710 unsigned int option_index
;
711 enum cpp_ttype token
;
714 struct cl_option_handlers handlers
;
716 token
= pragma_lex (&x
);
717 if (token
!= CPP_NAME
)
718 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
719 kind_string
= IDENTIFIER_POINTER (x
);
720 if (strcmp (kind_string
, "error") == 0)
722 else if (strcmp (kind_string
, "warning") == 0)
724 else if (strcmp (kind_string
, "ignored") == 0)
726 else if (strcmp (kind_string
, "push") == 0)
728 diagnostic_push_diagnostics (global_dc
, input_location
);
731 else if (strcmp (kind_string
, "pop") == 0)
733 diagnostic_pop_diagnostics (global_dc
, input_location
);
737 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
739 token
= pragma_lex (&x
);
740 if (token
!= CPP_STRING
)
741 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
742 option_string
= TREE_STRING_POINTER (x
);
743 set_default_handlers (&handlers
);
744 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
745 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
747 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
748 input_location
, c_family_lang_mask
, &handlers
,
749 &global_options
, &global_options_set
,
753 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
756 /* Parse #pragma GCC target (xxx) to set target specific options. */
758 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
760 enum cpp_ttype token
;
762 bool close_paren_needed_p
= false;
766 error ("#pragma GCC option is not allowed inside functions");
770 token
= pragma_lex (&x
);
771 if (token
== CPP_OPEN_PAREN
)
773 close_paren_needed_p
= true;
774 token
= pragma_lex (&x
);
777 if (token
!= CPP_STRING
)
779 GCC_BAD ("%<#pragma GCC option%> is not a string");
783 /* Strings are user options. */
786 tree args
= NULL_TREE
;
790 /* Build up the strings now as a tree linked list. Skip empty
792 if (TREE_STRING_LENGTH (x
) > 0)
793 args
= tree_cons (NULL_TREE
, x
, args
);
795 token
= pragma_lex (&x
);
796 while (token
== CPP_COMMA
)
797 token
= pragma_lex (&x
);
799 while (token
== CPP_STRING
);
801 if (close_paren_needed_p
)
803 if (token
== CPP_CLOSE_PAREN
)
804 token
= pragma_lex (&x
);
806 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
807 "not have a final %<)%>");
810 if (token
!= CPP_EOF
)
812 error ("#pragma GCC target string... is badly formed");
816 /* put arguments in the order the user typed them. */
817 args
= nreverse (args
);
819 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
820 current_target_pragma
= args
;
824 /* Handle #pragma GCC optimize to set optimization options. */
826 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
828 enum cpp_ttype token
;
830 bool close_paren_needed_p
= false;
831 tree optimization_previous_node
= optimization_current_node
;
835 error ("#pragma GCC optimize is not allowed inside functions");
839 token
= pragma_lex (&x
);
840 if (token
== CPP_OPEN_PAREN
)
842 close_paren_needed_p
= true;
843 token
= pragma_lex (&x
);
846 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
848 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
852 /* Strings/numbers are user options. */
855 tree args
= NULL_TREE
;
859 /* Build up the numbers/strings now as a list. */
860 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
861 args
= tree_cons (NULL_TREE
, x
, args
);
863 token
= pragma_lex (&x
);
864 while (token
== CPP_COMMA
)
865 token
= pragma_lex (&x
);
867 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
869 if (close_paren_needed_p
)
871 if (token
== CPP_CLOSE_PAREN
)
872 token
= pragma_lex (&x
);
874 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
875 "not have a final %<)%>");
878 if (token
!= CPP_EOF
)
880 error ("#pragma GCC optimize string... is badly formed");
884 /* put arguments in the order the user typed them. */
885 args
= nreverse (args
);
887 parse_optimize_options (args
, false);
888 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
889 optimization_current_node
= build_optimization_node ();
890 c_cpp_builtins_optimize_pragma (parse_in
,
891 optimization_previous_node
,
892 optimization_current_node
);
896 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
897 both the binary representation of the options and the TREE_LIST of
898 strings that will be added to the function's attribute list. */
899 typedef struct GTY(()) opt_stack
{
900 struct opt_stack
*prev
;
903 tree optimize_binary
;
904 tree optimize_strings
;
907 static GTY(()) struct opt_stack
* options_stack
;
909 /* Handle #pragma GCC push_options to save the current target and optimization
913 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
915 enum cpp_ttype token
;
919 token
= pragma_lex (&x
);
920 if (token
!= CPP_EOF
)
922 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
926 p
= ggc_alloc_opt_stack ();
927 p
->prev
= options_stack
;
930 /* Save optimization and target flags in binary format. */
931 p
->optimize_binary
= build_optimization_node ();
932 p
->target_binary
= build_target_option_node ();
934 /* Save optimization and target flags in string list format. */
935 p
->optimize_strings
= copy_list (current_optimize_pragma
);
936 p
->target_strings
= copy_list (current_target_pragma
);
939 /* Handle #pragma GCC pop_options to restore the current target and
940 optimization options from a previous push_options. */
943 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
945 enum cpp_ttype token
;
949 token
= pragma_lex (&x
);
950 if (token
!= CPP_EOF
)
952 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
958 warning (OPT_Wpragmas
,
959 "%<#pragma GCC pop_options%> without a corresponding "
960 "%<#pragma GCC push_options%>");
965 options_stack
= p
->prev
;
967 if (p
->target_binary
!= target_option_current_node
)
969 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
970 target_option_current_node
= p
->target_binary
;
973 if (p
->optimize_binary
!= optimization_current_node
)
975 tree old_optimize
= optimization_current_node
;
976 cl_optimization_restore (&global_options
,
977 TREE_OPTIMIZATION (p
->optimize_binary
));
978 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
980 optimization_current_node
= p
->optimize_binary
;
983 current_target_pragma
= p
->target_strings
;
984 current_optimize_pragma
= p
->optimize_strings
;
987 /* Handle #pragma GCC reset_options to restore the current target and
988 optimization options to the original options used on the command line. */
991 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
993 enum cpp_ttype token
;
995 tree new_optimize
= optimization_default_node
;
996 tree new_target
= target_option_default_node
;
998 token
= pragma_lex (&x
);
999 if (token
!= CPP_EOF
)
1001 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1005 if (new_target
!= target_option_current_node
)
1007 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1008 target_option_current_node
= new_target
;
1011 if (new_optimize
!= optimization_current_node
)
1013 tree old_optimize
= optimization_current_node
;
1014 cl_optimization_restore (&global_options
,
1015 TREE_OPTIMIZATION (new_optimize
));
1016 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1017 optimization_current_node
= new_optimize
;
1020 current_target_pragma
= NULL_TREE
;
1021 current_optimize_pragma
= NULL_TREE
;
1024 /* Print a plain user-specified message. */
1027 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1029 enum cpp_ttype token
;
1030 tree x
, message
= 0;
1032 token
= pragma_lex (&x
);
1033 if (token
== CPP_OPEN_PAREN
)
1035 token
= pragma_lex (&x
);
1036 if (token
== CPP_STRING
)
1039 GCC_BAD ("expected a string after %<#pragma message%>");
1040 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1041 GCC_BAD ("malformed %<#pragma message%>, ignored");
1043 else if (token
== CPP_STRING
)
1046 GCC_BAD ("expected a string after %<#pragma message%>");
1048 gcc_assert (message
);
1050 if (pragma_lex (&x
) != CPP_EOF
)
1051 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1053 if (TREE_STRING_LENGTH (message
) > 1)
1054 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1057 /* Mark whether the current location is valid for a STDC pragma. */
1059 static bool valid_location_for_stdc_pragma
;
1062 mark_valid_location_for_stdc_pragma (bool flag
)
1064 valid_location_for_stdc_pragma
= flag
;
1067 /* Return true if the current location is valid for a STDC pragma. */
1070 valid_location_for_stdc_pragma_p (void)
1072 return valid_location_for_stdc_pragma
;
1075 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1077 /* A STDC pragma must appear outside of external declarations or
1078 preceding all explicit declarations and statements inside a compound
1079 statement; its behavior is undefined if used in any other context.
1080 It takes a switch of ON, OFF, or DEFAULT. */
1082 static enum pragma_switch_t
1083 handle_stdc_pragma (const char *pname
)
1087 enum pragma_switch_t ret
;
1089 if (!valid_location_for_stdc_pragma_p ())
1091 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1096 if (pragma_lex (&t
) != CPP_NAME
)
1098 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1102 arg
= IDENTIFIER_POINTER (t
);
1104 if (!strcmp (arg
, "ON"))
1106 else if (!strcmp (arg
, "OFF"))
1108 else if (!strcmp (arg
, "DEFAULT"))
1109 ret
= PRAGMA_DEFAULT
;
1112 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1116 if (pragma_lex (&t
) != CPP_EOF
)
1118 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1125 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1126 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1127 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1130 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1132 if (c_dialect_cxx ())
1134 if (warn_unknown_pragmas
> in_system_header
)
1135 warning (OPT_Wunknown_pragmas
,
1136 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1141 if (!targetm
.decimal_float_supported_p ())
1143 if (warn_unknown_pragmas
> in_system_header
)
1144 warning (OPT_Wunknown_pragmas
,
1145 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1150 pedwarn (input_location
, OPT_pedantic
,
1151 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1153 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1156 set_float_const_decimal64 ();
1159 case PRAGMA_DEFAULT
:
1160 clear_float_const_decimal64 ();
1167 /* A vector of registered pragma callbacks, which is never freed. */
1168 DEF_VEC_O (internal_pragma_handler
);
1169 DEF_VEC_ALLOC_O (internal_pragma_handler
, heap
);
1171 static VEC(internal_pragma_handler
, heap
) *registered_pragmas
;
1179 DEF_VEC_O (pragma_ns_name
);
1180 DEF_VEC_ALLOC_O (pragma_ns_name
, heap
);
1182 static VEC(pragma_ns_name
, heap
) *registered_pp_pragmas
;
1184 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1185 static const struct omp_pragma_def omp_pragmas
[] = {
1186 { "atomic", PRAGMA_OMP_ATOMIC
},
1187 { "barrier", PRAGMA_OMP_BARRIER
},
1188 { "critical", PRAGMA_OMP_CRITICAL
},
1189 { "flush", PRAGMA_OMP_FLUSH
},
1190 { "for", PRAGMA_OMP_FOR
},
1191 { "master", PRAGMA_OMP_MASTER
},
1192 { "ordered", PRAGMA_OMP_ORDERED
},
1193 { "parallel", PRAGMA_OMP_PARALLEL
},
1194 { "section", PRAGMA_OMP_SECTION
},
1195 { "sections", PRAGMA_OMP_SECTIONS
},
1196 { "single", PRAGMA_OMP_SINGLE
},
1197 { "task", PRAGMA_OMP_TASK
},
1198 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1199 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1200 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1204 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1206 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1209 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1210 if (omp_pragmas
[i
].id
== id
)
1213 *name
= omp_pragmas
[i
].name
;
1217 if (id
>= PRAGMA_FIRST_EXTERNAL
1218 && (id
< PRAGMA_FIRST_EXTERNAL
1219 + VEC_length (pragma_ns_name
, registered_pp_pragmas
)))
1221 *space
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1222 id
- PRAGMA_FIRST_EXTERNAL
)->space
;
1223 *name
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1224 id
- PRAGMA_FIRST_EXTERNAL
)->name
;
1231 /* Front-end wrappers for pragma registration to avoid dragging
1232 cpplib.h in almost everywhere. */
1235 c_register_pragma_1 (const char *space
, const char *name
,
1236 internal_pragma_handler ihandler
, bool allow_expansion
)
1240 if (flag_preprocess_only
)
1242 pragma_ns_name ns_name
;
1244 if (!allow_expansion
)
1247 ns_name
.space
= space
;
1248 ns_name
.name
= name
;
1249 VEC_safe_push (pragma_ns_name
, heap
, registered_pp_pragmas
, &ns_name
);
1250 id
= VEC_length (pragma_ns_name
, registered_pp_pragmas
);
1251 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1255 VEC_safe_push (internal_pragma_handler
, heap
, registered_pragmas
,
1257 id
= VEC_length (internal_pragma_handler
, registered_pragmas
);
1258 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1260 /* The C++ front end allocates 6 bits in cp_token; the C front end
1261 allocates 7 bits in c_token. At present this is sufficient. */
1262 gcc_assert (id
< 64);
1265 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1266 allow_expansion
, false);
1269 /* Register a C pragma handler, using a space and a name. It disallows pragma
1270 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1272 c_register_pragma (const char *space
, const char *name
,
1273 pragma_handler_1arg handler
)
1275 internal_pragma_handler ihandler
;
1277 ihandler
.handler
.handler_1arg
= handler
;
1278 ihandler
.extra_data
= false;
1279 ihandler
.data
= NULL
;
1280 c_register_pragma_1 (space
, name
, ihandler
, false);
1283 /* Register a C pragma handler, using a space and a name, it also carries an
1284 extra data field which can be used by the handler. It disallows pragma
1285 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1288 c_register_pragma_with_data (const char *space
, const char *name
,
1289 pragma_handler_2arg handler
, void * data
)
1291 internal_pragma_handler ihandler
;
1293 ihandler
.handler
.handler_2arg
= handler
;
1294 ihandler
.extra_data
= true;
1295 ihandler
.data
= data
;
1296 c_register_pragma_1 (space
, name
, ihandler
, false);
1299 /* Register a C pragma handler, using a space and a name. It allows pragma
1300 expansion as in the following example:
1303 #pragma count (NUMBER)
1305 Name expansion is still disallowed. */
1307 c_register_pragma_with_expansion (const char *space
, const char *name
,
1308 pragma_handler_1arg handler
)
1310 internal_pragma_handler ihandler
;
1312 ihandler
.handler
.handler_1arg
= handler
;
1313 ihandler
.extra_data
= false;
1314 ihandler
.data
= NULL
;
1315 c_register_pragma_1 (space
, name
, ihandler
, true);
1318 /* Register a C pragma handler, using a space and a name, it also carries an
1319 extra data field which can be used by the handler. It allows pragma
1320 expansion as in the following example:
1323 #pragma count (NUMBER)
1325 Name expansion is still disallowed. */
1327 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1328 pragma_handler_2arg handler
,
1331 internal_pragma_handler ihandler
;
1333 ihandler
.handler
.handler_2arg
= handler
;
1334 ihandler
.extra_data
= true;
1335 ihandler
.data
= data
;
1336 c_register_pragma_1 (space
, name
, ihandler
, true);
1340 c_invoke_pragma_handler (unsigned int id
)
1342 internal_pragma_handler
*ihandler
;
1343 pragma_handler_1arg handler_1arg
;
1344 pragma_handler_2arg handler_2arg
;
1346 id
-= PRAGMA_FIRST_EXTERNAL
;
1347 ihandler
= VEC_index (internal_pragma_handler
, registered_pragmas
, id
);
1348 if (ihandler
->extra_data
)
1350 handler_2arg
= ihandler
->handler
.handler_2arg
;
1351 handler_2arg (parse_in
, ihandler
->data
);
1355 handler_1arg
= ihandler
->handler
.handler_1arg
;
1356 handler_1arg (parse_in
);
1360 /* Set up front-end pragmas. */
1366 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1369 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1370 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1371 omp_pragmas
[i
].id
, true, true);
1374 if (!flag_preprocess_only
)
1375 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1376 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1378 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1379 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1381 c_register_pragma (0, "pack", handle_pragma_pack
);
1383 c_register_pragma (0, "weak", handle_pragma_weak
);
1384 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1386 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1387 c_register_pragma ("GCC", "target", handle_pragma_target
);
1388 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1389 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1390 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1391 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1393 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1394 handle_pragma_float_const_decimal64
);
1396 c_register_pragma_with_expansion (0, "redefine_extname",
1397 handle_pragma_redefine_extname
);
1398 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
1400 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1402 #ifdef REGISTER_TARGET_PRAGMAS
1403 REGISTER_TARGET_PRAGMAS ();
1406 /* Allow plugins to register their own pragmas. */
1407 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1410 #include "gt-c-family-c-pragma.h"