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"
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? */
35 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
36 this not a target hook?). */
40 #include "diagnostic.h"
44 #define GCC_BAD(gmsgid) \
45 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
46 #define GCC_BAD2(gmsgid, arg) \
47 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
49 typedef struct GTY(()) align_stack
{
52 struct align_stack
* prev
;
55 static GTY(()) struct align_stack
* alignment_stack
;
57 #ifdef HANDLE_PRAGMA_PACK
58 static void handle_pragma_pack (cpp_reader
*);
60 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
61 /* If we have a "global" #pragma pack(<n>) in effect when the first
62 #pragma pack(push,<n>) is encountered, this stores the value of
63 maximum_field_alignment in effect. When the final pop_alignment()
64 happens, we restore the value to this, not to a value of 0 for
65 maximum_field_alignment. Value is in bits. */
66 static int default_alignment
;
67 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
68 ? &default_alignment \
69 : &alignment_stack->alignment) = (ALIGN))
71 static void push_alignment (int, tree
);
72 static void pop_alignment (tree
);
74 /* Push an alignment value onto the stack. */
76 push_alignment (int alignment
, tree id
)
80 entry
= ggc_alloc_align_stack ();
82 entry
->alignment
= alignment
;
84 entry
->prev
= alignment_stack
;
86 /* The current value of maximum_field_alignment is not necessarily
87 0 since there may be a #pragma pack(<n>) in effect; remember it
88 so that we can restore it after the final #pragma pop(). */
89 if (alignment_stack
== NULL
)
90 default_alignment
= maximum_field_alignment
;
92 alignment_stack
= entry
;
94 maximum_field_alignment
= alignment
;
97 /* Undo a push of an alignment onto the stack. */
99 pop_alignment (tree id
)
103 if (alignment_stack
== NULL
)
104 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
106 /* If we got an identifier, strip away everything above the target
107 entry so that the next step will restore the state just below it. */
110 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
113 alignment_stack
= entry
;
117 warning (OPT_Wpragmas
, "\
118 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
122 entry
= alignment_stack
->prev
;
124 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
126 alignment_stack
= entry
;
128 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
129 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
130 #define push_alignment(ID, N) \
131 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
132 #define pop_alignment(ID) \
133 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
134 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
140 #pragma pack (push, N)
141 #pragma pack (push, ID)
142 #pragma pack (push, ID, N)
144 #pragma pack (pop, ID) */
146 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
150 enum cpp_ttype token
;
151 enum { set
, push
, pop
} action
;
153 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
154 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
156 token
= pragma_lex (&x
);
157 if (token
== CPP_CLOSE_PAREN
)
160 align
= initial_max_fld_align
;
162 else if (token
== CPP_NUMBER
)
164 if (TREE_CODE (x
) != INTEGER_CST
)
165 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
166 align
= TREE_INT_CST_LOW (x
);
168 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
169 GCC_BAD ("malformed %<#pragma pack%> - ignored");
171 else if (token
== CPP_NAME
)
173 #define GCC_BAD_ACTION do { if (action != pop) \
174 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
176 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
179 const char *op
= IDENTIFIER_POINTER (x
);
180 if (!strcmp (op
, "push"))
182 else if (!strcmp (op
, "pop"))
185 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
187 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
189 token
= pragma_lex (&x
);
190 if (token
== CPP_NAME
&& id
== 0)
194 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
196 if (TREE_CODE (x
) != INTEGER_CST
)
197 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
198 align
= TREE_INT_CST_LOW (x
);
206 if (token
!= CPP_CLOSE_PAREN
)
208 #undef GCC_BAD_ACTION
211 GCC_BAD ("malformed %<#pragma pack%> - ignored");
213 if (pragma_lex (&x
) != CPP_EOF
)
214 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
216 if (flag_pack_struct
)
217 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
228 align
*= BITS_PER_UNIT
;
233 align
= maximum_field_alignment
;
237 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
242 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
243 case push
: push_alignment (align
, id
); break;
244 case pop
: pop_alignment (id
); break;
247 #endif /* HANDLE_PRAGMA_PACK */
249 typedef struct GTY(()) pending_weak_d
255 DEF_VEC_O(pending_weak
);
256 DEF_VEC_ALLOC_O(pending_weak
,gc
);
258 static GTY(()) VEC(pending_weak
,gc
) *pending_weaks
;
260 #ifdef HANDLE_PRAGMA_WEAK
261 static void apply_pragma_weak (tree
, tree
);
262 static void handle_pragma_weak (cpp_reader
*);
265 apply_pragma_weak (tree decl
, tree value
)
269 value
= build_string (IDENTIFIER_LENGTH (value
),
270 IDENTIFIER_POINTER (value
));
271 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
272 build_tree_list (NULL
, value
)),
276 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
277 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
278 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
279 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
280 "results in unspecified behavior", decl
);
286 maybe_apply_pragma_weak (tree decl
)
292 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
294 /* No weak symbols pending, take the short-cut. */
297 /* If it's not visible outside this file, it doesn't matter whether
299 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
301 /* If it's not a function or a variable, it can't be weak.
302 FIXME: what kinds of things are visible outside this file but
303 aren't functions or variables? Should this be an assert instead? */
304 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
307 id
= DECL_ASSEMBLER_NAME (decl
);
309 for (i
= 0; VEC_iterate (pending_weak
, pending_weaks
, i
, pe
); i
++)
312 apply_pragma_weak (decl
, pe
->value
);
313 VEC_unordered_remove (pending_weak
, pending_weaks
, i
);
318 /* Process all "#pragma weak A = B" directives where we have not seen
321 maybe_apply_pending_pragma_weaks (void)
323 tree alias_id
, id
, decl
;
327 for (i
= 0; VEC_iterate (pending_weak
, pending_weaks
, i
, pe
); i
++)
335 decl
= build_decl (UNKNOWN_LOCATION
,
336 FUNCTION_DECL
, alias_id
, default_function_type
);
338 DECL_ARTIFICIAL (decl
) = 1;
339 TREE_PUBLIC (decl
) = 1;
340 DECL_EXTERNAL (decl
) = 1;
341 DECL_WEAK (decl
) = 1;
343 assemble_alias (decl
, id
);
347 /* #pragma weak name [= value] */
349 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
351 tree name
, value
, x
, decl
;
356 if (pragma_lex (&name
) != CPP_NAME
)
357 GCC_BAD ("malformed #pragma weak, ignored");
361 if (pragma_lex (&value
) != CPP_NAME
)
362 GCC_BAD ("malformed #pragma weak, ignored");
366 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
368 decl
= identifier_global_value (name
);
369 if (decl
&& DECL_P (decl
))
371 apply_pragma_weak (decl
, value
);
373 assemble_alias (decl
, value
);
378 pe
= VEC_safe_push (pending_weak
, gc
, pending_weaks
, NULL
);
385 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
390 maybe_apply_pending_pragma_weaks (void)
393 #endif /* HANDLE_PRAGMA_WEAK */
395 /* GCC supports two #pragma directives for renaming the external
396 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
397 compatibility with the Solaris and Tru64 system headers. GCC also
398 has its own notation for this, __asm__("name") annotations.
400 Corner cases of these features and their interaction:
402 1) Both pragmas silently apply only to declarations with external
403 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
404 do not have this restriction.
406 2) In C++, both #pragmas silently apply only to extern "C" declarations.
407 Asm labels do not have this restriction.
409 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
410 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
411 new name is different, a warning issues and the name does not change.
413 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
414 *not* the DECL_ASSEMBLER_NAME.
416 5) If #pragma extern_prefix is in effect and a declaration occurs
417 with an __asm__ name, the #pragma extern_prefix is silently
418 ignored for that declaration.
420 6) If #pragma extern_prefix and #pragma redefine_extname apply to
421 the same declaration, whichever triggered first wins, and a warning
422 is issued. (We would like to have #pragma redefine_extname always
423 win, but it can appear either before or after the declaration, and
424 if it appears afterward, we have no way of knowing whether a modified
425 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
427 static GTY(()) tree pending_redefine_extname
;
429 static void handle_pragma_redefine_extname (cpp_reader
*);
431 /* #pragma redefine_extname oldname newname */
433 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
435 tree oldname
, newname
, decl
, x
;
438 if (pragma_lex (&oldname
) != CPP_NAME
)
439 GCC_BAD ("malformed #pragma redefine_extname, ignored");
440 if (pragma_lex (&newname
) != CPP_NAME
)
441 GCC_BAD ("malformed #pragma redefine_extname, ignored");
444 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
446 decl
= identifier_global_value (oldname
);
448 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
449 && (TREE_CODE (decl
) == FUNCTION_DECL
450 || TREE_CODE (decl
) == VAR_DECL
)
451 && has_c_linkage (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
);
466 /* We have to add this to the rename list even if there's already
467 a global value that doesn't meet the above criteria, because in
468 C++ "struct foo {...};" puts "foo" in the current namespace but
469 does *not* conflict with a subsequent declaration of a function
470 or variable foo. See g++.dg/other/pragma-re-2.C. */
471 add_to_renaming_pragma_list (oldname
, newname
);
474 /* This is called from here and from ia64.c. */
476 add_to_renaming_pragma_list (tree oldname
, tree newname
)
478 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
481 if (TREE_VALUE (previous
) != newname
)
482 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
483 "conflict with previous #pragma redefine_extname");
487 pending_redefine_extname
488 = tree_cons (oldname
, newname
, pending_redefine_extname
);
491 static GTY(()) tree pragma_extern_prefix
;
493 /* #pragma extern_prefix "prefix" */
495 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
500 if (pragma_lex (&prefix
) != CPP_STRING
)
501 GCC_BAD ("malformed #pragma extern_prefix, ignored");
504 warning (OPT_Wpragmas
, "junk at end of %<#pragma extern_prefix%>");
506 if (targetm
.handle_pragma_extern_prefix
)
507 /* Note that the length includes the null terminator. */
508 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
509 else if (warn_unknown_pragmas
> in_system_header
)
510 warning (OPT_Wunknown_pragmas
,
511 "#pragma extern_prefix not supported on this target");
514 /* Hook from the front ends to apply the results of one of the preceding
515 pragmas that rename variables. */
518 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
522 /* The renaming pragmas are only applied to declarations with
524 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
525 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
526 || !has_c_linkage (decl
))
529 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
530 but we may warn about a rename that conflicts. */
531 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
533 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
534 oldname
= targetm
.strip_name_encoding (oldname
);
536 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
537 warning (OPT_Wpragmas
, "asm declaration ignored due to "
538 "conflict with previous rename");
540 /* Take any pending redefine_extname off the list. */
541 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
542 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
544 /* Only warn if there is a conflict. */
545 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
546 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
547 "conflict with previous rename");
555 /* Find out if we have a pending #pragma redefine_extname. */
556 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
557 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
559 tree newname
= TREE_VALUE (t
);
562 /* If we already have an asmname, #pragma redefine_extname is
563 ignored (with a warning if it conflicts). */
566 if (strcmp (TREE_STRING_POINTER (asmname
),
567 IDENTIFIER_POINTER (newname
)) != 0)
568 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
569 "conflict with __asm__ declaration");
573 /* Otherwise we use what we've got; #pragma extern_prefix is
575 return build_string (IDENTIFIER_LENGTH (newname
),
576 IDENTIFIER_POINTER (newname
));
579 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
583 /* If #pragma extern_prefix is in effect, apply it. */
584 if (pragma_extern_prefix
)
586 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
587 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
589 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
590 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
592 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
594 memcpy (newname
, prefix
, plen
);
595 memcpy (newname
+ plen
, id
, ilen
+ 1);
597 return build_string (plen
+ ilen
, newname
);
605 #ifdef HANDLE_PRAGMA_VISIBILITY
606 static void handle_pragma_visibility (cpp_reader
*);
608 static VEC (int, heap
) *visstack
;
610 /* Push the visibility indicated by STR onto the top of the #pragma
611 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
612 C++ namespace with visibility attribute and 2 for C++ builtin
613 ABI namespace. push_visibility/pop_visibility calls must have
614 matching KIND, it is not allowed to push visibility using one
615 KIND and pop using a different one. */
618 push_visibility (const char *str
, int kind
)
620 VEC_safe_push (int, heap
, visstack
,
621 ((int) default_visibility
) | (kind
<< 8));
622 if (!strcmp (str
, "default"))
623 default_visibility
= VISIBILITY_DEFAULT
;
624 else if (!strcmp (str
, "internal"))
625 default_visibility
= VISIBILITY_INTERNAL
;
626 else if (!strcmp (str
, "hidden"))
627 default_visibility
= VISIBILITY_HIDDEN
;
628 else if (!strcmp (str
, "protected"))
629 default_visibility
= VISIBILITY_PROTECTED
;
631 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
632 visibility_options
.inpragma
= 1;
635 /* Pop a level of the #pragma visibility stack. Return true if
639 pop_visibility (int kind
)
641 if (!VEC_length (int, visstack
))
643 if ((VEC_last (int, visstack
) >> 8) != kind
)
646 = (enum symbol_visibility
) (VEC_pop (int, visstack
) & 0xff);
647 visibility_options
.inpragma
648 = VEC_length (int, visstack
) != 0;
652 /* Sets the default visibility for symbols to something other than that
653 specified on the command line. */
656 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
658 /* Form is #pragma GCC visibility push(hidden)|pop */
660 enum cpp_ttype token
;
661 enum { bad
, push
, pop
} action
= bad
;
663 token
= pragma_lex (&x
);
664 if (token
== CPP_NAME
)
666 const char *op
= IDENTIFIER_POINTER (x
);
667 if (!strcmp (op
, "push"))
669 else if (!strcmp (op
, "pop"))
673 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
678 if (! pop_visibility (0))
679 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
683 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
684 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
685 token
= pragma_lex (&x
);
686 if (token
!= CPP_NAME
)
687 GCC_BAD ("malformed #pragma GCC visibility push");
689 push_visibility (IDENTIFIER_POINTER (x
), 0);
690 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
691 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
694 if (pragma_lex (&x
) != CPP_EOF
)
695 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
701 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
703 const char *kind_string
, *option_string
;
704 unsigned int option_index
;
705 enum cpp_ttype token
;
709 token
= pragma_lex (&x
);
710 if (token
!= CPP_NAME
)
711 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
712 kind_string
= IDENTIFIER_POINTER (x
);
713 if (strcmp (kind_string
, "error") == 0)
715 else if (strcmp (kind_string
, "warning") == 0)
717 else if (strcmp (kind_string
, "ignored") == 0)
719 else if (strcmp (kind_string
, "push") == 0)
721 diagnostic_push_diagnostics (global_dc
, input_location
);
724 else if (strcmp (kind_string
, "pop") == 0)
726 diagnostic_pop_diagnostics (global_dc
, input_location
);
730 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
732 token
= pragma_lex (&x
);
733 if (token
!= CPP_STRING
)
734 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
735 option_string
= TREE_STRING_POINTER (x
);
736 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
737 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
739 /* This overrides -Werror, for example. */
740 diagnostic_classify_diagnostic (global_dc
, option_index
, kind
, input_location
);
741 /* This makes sure the option is enabled, like -Wfoo would do. */
742 if (cl_options
[option_index
].var_type
== CLVC_BOOLEAN
743 && cl_options
[option_index
].flag_var
744 && kind
!= DK_IGNORED
)
745 *(int *) cl_options
[option_index
].flag_var
= 1;
748 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
751 /* Parse #pragma GCC target (xxx) to set target specific options. */
753 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
755 enum cpp_ttype token
;
757 bool close_paren_needed_p
= false;
761 error ("#pragma GCC option is not allowed inside functions");
765 token
= pragma_lex (&x
);
766 if (token
== CPP_OPEN_PAREN
)
768 close_paren_needed_p
= true;
769 token
= pragma_lex (&x
);
772 if (token
!= CPP_STRING
)
774 GCC_BAD ("%<#pragma GCC option%> is not a string");
778 /* Strings are user options. */
781 tree args
= NULL_TREE
;
785 /* Build up the strings now as a tree linked list. Skip empty
787 if (TREE_STRING_LENGTH (x
) > 0)
788 args
= tree_cons (NULL_TREE
, x
, args
);
790 token
= pragma_lex (&x
);
791 while (token
== CPP_COMMA
)
792 token
= pragma_lex (&x
);
794 while (token
== CPP_STRING
);
796 if (close_paren_needed_p
)
798 if (token
== CPP_CLOSE_PAREN
)
799 token
= pragma_lex (&x
);
801 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
802 "not have a final %<)%>.");
805 if (token
!= CPP_EOF
)
807 error ("#pragma GCC target string... is badly formed");
811 /* put arguments in the order the user typed them. */
812 args
= nreverse (args
);
814 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
815 current_target_pragma
= args
;
819 /* Handle #pragma GCC optimize to set optimization options. */
821 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
823 enum cpp_ttype token
;
825 bool close_paren_needed_p
= false;
826 tree optimization_previous_node
= optimization_current_node
;
830 error ("#pragma GCC optimize is not allowed inside functions");
834 token
= pragma_lex (&x
);
835 if (token
== CPP_OPEN_PAREN
)
837 close_paren_needed_p
= true;
838 token
= pragma_lex (&x
);
841 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
843 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
847 /* Strings/numbers are user options. */
850 tree args
= NULL_TREE
;
854 /* Build up the numbers/strings now as a list. */
855 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
856 args
= tree_cons (NULL_TREE
, x
, args
);
858 token
= pragma_lex (&x
);
859 while (token
== CPP_COMMA
)
860 token
= pragma_lex (&x
);
862 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
864 if (close_paren_needed_p
)
866 if (token
== CPP_CLOSE_PAREN
)
867 token
= pragma_lex (&x
);
869 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
870 "not have a final %<)%>.");
873 if (token
!= CPP_EOF
)
875 error ("#pragma GCC optimize string... is badly formed");
879 /* put arguments in the order the user typed them. */
880 args
= nreverse (args
);
882 parse_optimize_options (args
, false);
883 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
884 optimization_current_node
= build_optimization_node ();
885 c_cpp_builtins_optimize_pragma (parse_in
,
886 optimization_previous_node
,
887 optimization_current_node
);
891 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
892 both the binary representation of the options and the TREE_LIST of
893 strings that will be added to the function's attribute list. */
894 typedef struct GTY(()) opt_stack
{
895 struct opt_stack
*prev
;
898 tree optimize_binary
;
899 tree optimize_strings
;
902 static GTY(()) struct opt_stack
* options_stack
;
904 /* Handle #pragma GCC push_options to save the current target and optimization
908 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
910 enum cpp_ttype token
;
914 token
= pragma_lex (&x
);
915 if (token
!= CPP_EOF
)
917 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
921 p
= ggc_alloc_opt_stack ();
922 p
->prev
= options_stack
;
925 /* Save optimization and target flags in binary format. */
926 p
->optimize_binary
= build_optimization_node ();
927 p
->target_binary
= build_target_option_node ();
929 /* Save optimization and target flags in string list format. */
930 p
->optimize_strings
= copy_list (current_optimize_pragma
);
931 p
->target_strings
= copy_list (current_target_pragma
);
934 /* Handle #pragma GCC pop_options to restore the current target and
935 optimization options from a previous push_options. */
938 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
940 enum cpp_ttype token
;
944 token
= pragma_lex (&x
);
945 if (token
!= CPP_EOF
)
947 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
953 warning (OPT_Wpragmas
,
954 "%<#pragma GCC pop_options%> without a corresponding "
955 "%<#pragma GCC push_options%>");
960 options_stack
= p
->prev
;
962 if (p
->target_binary
!= target_option_current_node
)
964 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
965 target_option_current_node
= p
->target_binary
;
968 if (p
->optimize_binary
!= optimization_current_node
)
970 tree old_optimize
= optimization_current_node
;
971 cl_optimization_restore (TREE_OPTIMIZATION (p
->optimize_binary
));
972 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
974 optimization_current_node
= p
->optimize_binary
;
977 current_target_pragma
= p
->target_strings
;
978 current_optimize_pragma
= p
->optimize_strings
;
981 /* Handle #pragma GCC reset_options to restore the current target and
982 optimization options to the original options used on the command line. */
985 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
987 enum cpp_ttype token
;
989 tree new_optimize
= optimization_default_node
;
990 tree new_target
= target_option_default_node
;
992 token
= pragma_lex (&x
);
993 if (token
!= CPP_EOF
)
995 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
999 if (new_target
!= target_option_current_node
)
1001 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1002 target_option_current_node
= new_target
;
1005 if (new_optimize
!= optimization_current_node
)
1007 tree old_optimize
= optimization_current_node
;
1008 cl_optimization_restore (TREE_OPTIMIZATION (new_optimize
));
1009 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1010 optimization_current_node
= new_optimize
;
1013 current_target_pragma
= NULL_TREE
;
1014 current_optimize_pragma
= NULL_TREE
;
1017 /* Print a plain user-specified message. */
1020 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1022 enum cpp_ttype token
;
1023 tree x
, message
= 0;
1025 token
= pragma_lex (&x
);
1026 if (token
== CPP_OPEN_PAREN
)
1028 token
= pragma_lex (&x
);
1029 if (token
== CPP_STRING
)
1032 GCC_BAD ("expected a string after %<#pragma message%>");
1033 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1034 GCC_BAD ("malformed %<#pragma message%>, ignored");
1036 else if (token
== CPP_STRING
)
1039 GCC_BAD ("expected a string after %<#pragma message%>");
1041 gcc_assert (message
);
1043 if (pragma_lex (&x
) != CPP_EOF
)
1044 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1046 if (TREE_STRING_LENGTH (message
) > 1)
1047 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1050 /* Mark whether the current location is valid for a STDC pragma. */
1052 static bool valid_location_for_stdc_pragma
;
1055 mark_valid_location_for_stdc_pragma (bool flag
)
1057 valid_location_for_stdc_pragma
= flag
;
1060 /* Return true if the current location is valid for a STDC pragma. */
1063 valid_location_for_stdc_pragma_p (void)
1065 return valid_location_for_stdc_pragma
;
1068 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1070 /* A STDC pragma must appear outside of external declarations or
1071 preceding all explicit declarations and statements inside a compound
1072 statement; its behavior is undefined if used in any other context.
1073 It takes a switch of ON, OFF, or DEFAULT. */
1075 static enum pragma_switch_t
1076 handle_stdc_pragma (const char *pname
)
1080 enum pragma_switch_t ret
;
1082 if (!valid_location_for_stdc_pragma_p ())
1084 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1089 if (pragma_lex (&t
) != CPP_NAME
)
1091 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1095 arg
= IDENTIFIER_POINTER (t
);
1097 if (!strcmp (arg
, "ON"))
1099 else if (!strcmp (arg
, "OFF"))
1101 else if (!strcmp (arg
, "DEFAULT"))
1102 ret
= PRAGMA_DEFAULT
;
1105 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1109 if (pragma_lex (&t
) != CPP_EOF
)
1111 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1118 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1119 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1120 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1123 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1125 if (c_dialect_cxx ())
1127 if (warn_unknown_pragmas
> in_system_header
)
1128 warning (OPT_Wunknown_pragmas
,
1129 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1134 if (!targetm
.decimal_float_supported_p ())
1136 if (warn_unknown_pragmas
> in_system_header
)
1137 warning (OPT_Wunknown_pragmas
,
1138 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1143 pedwarn (input_location
, OPT_pedantic
,
1144 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1146 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1149 set_float_const_decimal64 ();
1152 case PRAGMA_DEFAULT
:
1153 clear_float_const_decimal64 ();
1160 /* A vector of registered pragma callbacks. */
1162 DEF_VEC_O (pragma_handler
);
1163 DEF_VEC_ALLOC_O (pragma_handler
, heap
);
1165 static VEC(pragma_handler
, heap
) *registered_pragmas
;
1173 DEF_VEC_O (pragma_ns_name
);
1174 DEF_VEC_ALLOC_O (pragma_ns_name
, heap
);
1176 static VEC(pragma_ns_name
, heap
) *registered_pp_pragmas
;
1178 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1179 static const struct omp_pragma_def omp_pragmas
[] = {
1180 { "atomic", PRAGMA_OMP_ATOMIC
},
1181 { "barrier", PRAGMA_OMP_BARRIER
},
1182 { "critical", PRAGMA_OMP_CRITICAL
},
1183 { "flush", PRAGMA_OMP_FLUSH
},
1184 { "for", PRAGMA_OMP_FOR
},
1185 { "master", PRAGMA_OMP_MASTER
},
1186 { "ordered", PRAGMA_OMP_ORDERED
},
1187 { "parallel", PRAGMA_OMP_PARALLEL
},
1188 { "section", PRAGMA_OMP_SECTION
},
1189 { "sections", PRAGMA_OMP_SECTIONS
},
1190 { "single", PRAGMA_OMP_SINGLE
},
1191 { "task", PRAGMA_OMP_TASK
},
1192 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1193 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1197 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1199 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1202 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1203 if (omp_pragmas
[i
].id
== id
)
1206 *name
= omp_pragmas
[i
].name
;
1210 if (id
>= PRAGMA_FIRST_EXTERNAL
1211 && (id
< PRAGMA_FIRST_EXTERNAL
1212 + VEC_length (pragma_ns_name
, registered_pp_pragmas
)))
1214 *space
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1215 id
- PRAGMA_FIRST_EXTERNAL
)->space
;
1216 *name
= VEC_index (pragma_ns_name
, registered_pp_pragmas
,
1217 id
- PRAGMA_FIRST_EXTERNAL
)->name
;
1224 /* Front-end wrappers for pragma registration to avoid dragging
1225 cpplib.h in almost everywhere. */
1228 c_register_pragma_1 (const char *space
, const char *name
,
1229 pragma_handler handler
, bool allow_expansion
)
1233 if (flag_preprocess_only
)
1235 pragma_ns_name ns_name
;
1237 if (!allow_expansion
)
1240 ns_name
.space
= space
;
1241 ns_name
.name
= name
;
1242 VEC_safe_push (pragma_ns_name
, heap
, registered_pp_pragmas
, &ns_name
);
1243 id
= VEC_length (pragma_ns_name
, registered_pp_pragmas
);
1244 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1248 VEC_safe_push (pragma_handler
, heap
, registered_pragmas
, &handler
);
1249 id
= VEC_length (pragma_handler
, registered_pragmas
);
1250 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1252 /* The C++ front end allocates 6 bits in cp_token; the C front end
1253 allocates 7 bits in c_token. At present this is sufficient. */
1254 gcc_assert (id
< 64);
1257 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1258 allow_expansion
, false);
1262 c_register_pragma (const char *space
, const char *name
, pragma_handler handler
)
1264 c_register_pragma_1 (space
, name
, handler
, false);
1268 c_register_pragma_with_expansion (const char *space
, const char *name
,
1269 pragma_handler handler
)
1271 c_register_pragma_1 (space
, name
, handler
, true);
1275 c_invoke_pragma_handler (unsigned int id
)
1277 pragma_handler handler
;
1279 id
-= PRAGMA_FIRST_EXTERNAL
;
1280 handler
= *VEC_index (pragma_handler
, registered_pragmas
, id
);
1285 /* Set up front-end pragmas. */
1291 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1294 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1295 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1296 omp_pragmas
[i
].id
, true, true);
1299 if (!flag_preprocess_only
)
1300 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1301 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1303 #ifdef HANDLE_PRAGMA_PACK
1304 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1305 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1307 c_register_pragma (0, "pack", handle_pragma_pack
);
1310 #ifdef HANDLE_PRAGMA_WEAK
1311 c_register_pragma (0, "weak", handle_pragma_weak
);
1313 #ifdef HANDLE_PRAGMA_VISIBILITY
1314 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1317 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1318 c_register_pragma ("GCC", "target", handle_pragma_target
);
1319 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1320 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1321 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1322 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1324 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1325 handle_pragma_float_const_decimal64
);
1327 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname
);
1328 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
1330 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1332 #ifdef REGISTER_TARGET_PRAGMAS
1333 REGISTER_TARGET_PRAGMAS ();
1336 /* Allow plugins to register their own pragmas. */
1337 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1340 #include "gt-c-family-c-pragma.h"