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, 2012 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?). */
38 #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
245 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
247 static void apply_pragma_weak (tree
, tree
);
248 static void handle_pragma_weak (cpp_reader
*);
251 apply_pragma_weak (tree decl
, tree value
)
255 value
= build_string (IDENTIFIER_LENGTH (value
),
256 IDENTIFIER_POINTER (value
));
257 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
258 build_tree_list (NULL
, value
)),
262 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
263 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
264 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
265 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
266 "results in unspecified behavior", decl
);
272 maybe_apply_pragma_weak (tree decl
)
278 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
280 /* No weak symbols pending, take the short-cut. */
283 /* If it's not visible outside this file, it doesn't matter whether
285 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
287 /* If it's not a function or a variable, it can't be weak.
288 FIXME: what kinds of things are visible outside this file but
289 aren't functions or variables? Should this be an assert instead? */
290 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
293 id
= DECL_ASSEMBLER_NAME (decl
);
295 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
298 apply_pragma_weak (decl
, pe
->value
);
299 pending_weaks
->unordered_remove (i
);
304 /* Process all "#pragma weak A = B" directives where we have not seen
307 maybe_apply_pending_pragma_weaks (void)
309 tree alias_id
, id
, decl
;
317 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
325 target
= symtab_node_for_asm (id
);
326 decl
= build_decl (UNKNOWN_LOCATION
,
327 target
? TREE_CODE (target
->symbol
.decl
) : FUNCTION_DECL
,
328 alias_id
, default_function_type
);
330 DECL_ARTIFICIAL (decl
) = 1;
331 TREE_PUBLIC (decl
) = 1;
332 DECL_WEAK (decl
) = 1;
333 if (TREE_CODE (decl
) == VAR_DECL
)
334 TREE_STATIC (decl
) = 1;
337 error ("%q+D aliased to undefined symbol %qE",
342 assemble_alias (decl
, id
);
346 /* #pragma weak name [= value] */
348 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
350 tree name
, value
, x
, decl
;
355 if (pragma_lex (&name
) != CPP_NAME
)
356 GCC_BAD ("malformed #pragma weak, ignored");
360 if (pragma_lex (&value
) != CPP_NAME
)
361 GCC_BAD ("malformed #pragma weak, ignored");
365 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
367 decl
= identifier_global_value (name
);
368 if (decl
&& DECL_P (decl
))
370 apply_pragma_weak (decl
, value
);
372 assemble_alias (decl
, value
);
376 pending_weak pe
= {name
, value
};
377 vec_safe_push (pending_weaks
, pe
);
381 /* GCC supports two #pragma directives for renaming the external
382 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
383 compatibility with the Solaris and VMS system headers. GCC also
384 has its own notation for this, __asm__("name") annotations.
386 Corner cases of these features and their interaction:
388 1) Both pragmas silently apply only to declarations with external
389 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
390 do not have this restriction.
392 2) In C++, both #pragmas silently apply only to extern "C" declarations.
393 Asm labels do not have this restriction.
395 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
396 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
397 new name is different, a warning issues and the name does not change.
399 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
400 *not* the DECL_ASSEMBLER_NAME.
402 5) If #pragma extern_prefix is in effect and a declaration occurs
403 with an __asm__ name, the #pragma extern_prefix is silently
404 ignored for that declaration.
406 6) If #pragma extern_prefix and #pragma redefine_extname apply to
407 the same declaration, whichever triggered first wins, and a warning
408 is issued. (We would like to have #pragma redefine_extname always
409 win, but it can appear either before or after the declaration, and
410 if it appears afterward, we have no way of knowing whether a modified
411 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
413 typedef struct GTY(()) pending_redefinition_d
{
416 } pending_redefinition
;
419 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
421 static void handle_pragma_redefine_extname (cpp_reader
*);
423 /* #pragma redefine_extname oldname newname */
425 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
427 tree oldname
, newname
, decls
, x
;
431 if (pragma_lex (&oldname
) != CPP_NAME
)
432 GCC_BAD ("malformed #pragma redefine_extname, ignored");
433 if (pragma_lex (&newname
) != CPP_NAME
)
434 GCC_BAD ("malformed #pragma redefine_extname, ignored");
437 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
440 for (decls
= c_linkage_bindings (oldname
);
444 if (TREE_CODE (decls
) == TREE_LIST
)
446 decl
= TREE_VALUE (decls
);
447 decls
= TREE_CHAIN (decls
);
455 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
456 && (TREE_CODE (decl
) == FUNCTION_DECL
457 || TREE_CODE (decl
) == VAR_DECL
))
460 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
462 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
463 name
= targetm
.strip_name_encoding (name
);
465 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
466 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
467 "conflict with previous rename");
470 change_decl_assembler_name (decl
, newname
);
475 /* We have to add this to the rename list even if there's already
476 a global value that doesn't meet the above criteria, because in
477 C++ "struct foo {...};" puts "foo" in the current namespace but
478 does *not* conflict with a subsequent declaration of a function
479 or variable foo. See g++.dg/other/pragma-re-2.C. */
480 add_to_renaming_pragma_list (oldname
, newname
);
483 /* This is called from here and from ia64.c. */
485 add_to_renaming_pragma_list (tree oldname
, tree newname
)
488 pending_redefinition
*p
;
490 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
491 if (oldname
== p
->oldname
)
493 if (p
->newname
!= newname
)
494 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
495 "conflict with previous #pragma redefine_extname");
499 pending_redefinition e
= {oldname
, newname
};
500 vec_safe_push (pending_redefine_extname
, e
);
503 /* The current prefix set by #pragma extern_prefix. */
504 GTY(()) tree pragma_extern_prefix
;
506 /* variables used to implement #pragma upc semantics */
507 #ifndef UPC_CMODE_STACK_INCREMENT
508 #define UPC_CMODE_STACK_INCREMENT 32
510 static int pragma_upc_permitted
;
511 static int upc_cmode
;
512 static int *upc_cmode_stack
;
513 static int upc_cmode_stack_in_use
;
514 static int upc_cmode_stack_allocated
;
516 static void init_pragma_upc (void);
517 static void handle_pragma_upc (cpp_reader
* ARG_UNUSED (dummy
));
519 /* Initialize the variables used to manage the current UPC consistency
520 mode (strict/relaxed) */
523 init_pragma_upc (void)
525 pragma_upc_permitted
= 0;
527 upc_cmode_stack
= (int *) xcalloc (UPC_CMODE_STACK_INCREMENT
,
529 upc_cmode_stack_allocated
= UPC_CMODE_STACK_INCREMENT
;
530 upc_cmode_stack_in_use
= 0;
535 * #pragma upc relaxed
536 * #pragma upc upc_code
540 handle_pragma_upc (cpp_reader
* ARG_UNUSED (dummy
))
544 enum upc_pragma_op
{p_strict
, p_relaxed
, p_upc_code
,
545 p_c_code
, p_detect_upc
, p_unknown
};
546 enum upc_pragma_op upc_pragma
= p_unknown
;
550 warning (OPT_Wpragmas
, "#pragma upc found in non-UPC source file");
557 const char *op
= IDENTIFIER_POINTER (x
);
558 if (!strcmp (op
, "strict"))
559 upc_pragma
= p_strict
;
560 else if (!strcmp (op
, "relaxed"))
561 upc_pragma
= p_relaxed
;
562 else if (!strcmp (op
, "upc_code"))
563 upc_pragma
= p_upc_code
;
564 else if (!strcmp (op
, "c_code"))
565 upc_pragma
= p_c_code
;
566 else if (!strcmp (op
, "detect_upc"))
568 const char *detect_op
;
569 upc_pragma
= p_detect_upc
;
572 GCC_BAD ("missing [suspend_insertion|resume_insertion]"
573 " after %<#pragma UPC detect_upc%>");
574 detect_op
= IDENTIFIER_POINTER (x
);
575 if (strcmp (detect_op
, "suspend_insertion") == 0)
577 else if (strcmp (detect_op
, "resume_insertion") == 0)
580 GCC_BAD ("expected [suspend_insertion|resume_insertion]"
581 " after %<#pragma UPC detect_upc%>");
584 GCC_BAD2 ("unknown action '%s' for '#pragma upc' - ignored", op
);
587 warning (OPT_Wpragmas
, "missing parameter after #pragma upc");
591 warning (OPT_Wpragmas
, "junk at end of #pragma upc");
593 if ((upc_pragma
== p_strict
) || (upc_pragma
== p_relaxed
))
595 if (pragma_upc_permitted_p ())
597 int consistency_mode
= (upc_pragma
== p_strict
);
598 set_upc_consistency_mode (consistency_mode
);
601 warning (OPT_Wpragmas
, "#pragma upc not allowed in this context");
603 else if ((upc_pragma
== p_upc_code
) || (upc_pragma
== p_c_code
))
605 compiling_upc
= (upc_pragma
== p_upc_code
);
607 else if (upc_pragma
== p_detect_upc
)
609 /* Skip: this is a Berkeley-specific pragma that requires no action. */
613 /* Set the current setting of the UPC consistency mode
614 that is in effect. */
617 set_upc_consistency_mode (int mode
)
622 /* Return the current setting of the UPC consistency mode. */
625 get_upc_consistency_mode (void)
630 /* Called from the parser just after the bracket that opens a compound
631 statement has been parsed. Set the flag that allows the pragma
635 permit_pragma_upc (void)
637 pragma_upc_permitted
= 1;
640 /* Called just before the body of a compound statement is parsed.
641 Clear the flag that allows the pragma. */
644 deny_pragma_upc (void)
646 pragma_upc_permitted
= 0;
649 /* A #pragma upc is permitted either at the outermost scope,
650 or directly after the bracket that opens a compound statement. */
653 pragma_upc_permitted_p (void)
655 return !current_function_decl
|| pragma_upc_permitted
;
658 /* Called at the beginning of every compound statement.
659 Pushes the old value of the current UPC consistency mode
663 push_upc_consistency_mode (void)
665 if (upc_cmode_stack_in_use
== upc_cmode_stack_allocated
)
667 upc_cmode_stack_allocated
+= UPC_CMODE_STACK_INCREMENT
;
668 upc_cmode_stack
= (int *) xrealloc (upc_cmode_stack
,
669 upc_cmode_stack_allocated
* sizeof (int));
671 upc_cmode_stack
[upc_cmode_stack_in_use
++] = upc_cmode
;
674 /* Called at the end of every compound statement.
675 Sets the current consistency mode to the previously saved value. */
678 pop_upc_consistency_mode (void)
680 if (upc_cmode_stack_in_use
<= 0)
682 upc_cmode
= upc_cmode_stack
[--upc_cmode_stack_in_use
];
685 static int pragma_pupc_on
;
686 static void init_pragma_pupc (void);
687 static void handle_pragma_pupc (cpp_reader
*);
689 /* Pragma pupc defaults to being on */
691 init_pragma_pupc (void)
697 get_upc_pupc_mode (void)
699 return pragma_pupc_on
;
703 disable_pupc_mode (void)
705 int old_pupc
= pragma_pupc_on
;
711 set_pupc_mode (int new_pupc
)
713 pragma_pupc_on
= new_pupc
;
721 handle_pragma_pupc (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
728 const char *op
= IDENTIFIER_POINTER (x
);
729 if (!strcmp (op
, "on"))
731 else if (!strcmp (op
, "off"))
734 GCC_BAD2 ("unknown action '%s' for '#pragma pupc' - ignored", op
);
739 warning (OPT_Wpragmas
, "junk at end of #pragma pupc");
742 /* Hook from the front ends to apply the results of one of the preceding
743 pragmas that rename variables. */
746 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
749 pending_redefinition
*p
;
751 /* The renaming pragmas are only applied to declarations with
753 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
754 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
755 || !has_c_linkage (decl
))
758 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
759 but we may warn about a rename that conflicts. */
760 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
762 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
763 oldname
= targetm
.strip_name_encoding (oldname
);
765 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
766 warning (OPT_Wpragmas
, "asm declaration ignored due to "
767 "conflict with previous rename");
769 /* Take any pending redefine_extname off the list. */
770 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
771 if (DECL_NAME (decl
) == p
->oldname
)
773 /* Only warn if there is a conflict. */
774 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
775 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
776 "conflict with previous rename");
778 pending_redefine_extname
->unordered_remove (ix
);
784 /* Find out if we have a pending #pragma redefine_extname. */
785 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
786 if (DECL_NAME (decl
) == p
->oldname
)
788 tree newname
= p
->newname
;
789 pending_redefine_extname
->unordered_remove (ix
);
791 /* If we already have an asmname, #pragma redefine_extname is
792 ignored (with a warning if it conflicts). */
795 if (strcmp (TREE_STRING_POINTER (asmname
),
796 IDENTIFIER_POINTER (newname
)) != 0)
797 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
798 "conflict with __asm__ declaration");
802 /* Otherwise we use what we've got; #pragma extern_prefix is
804 return build_string (IDENTIFIER_LENGTH (newname
),
805 IDENTIFIER_POINTER (newname
));
808 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
812 /* If #pragma extern_prefix is in effect, apply it. */
813 if (pragma_extern_prefix
)
815 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
816 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
818 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
819 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
821 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
823 memcpy (newname
, prefix
, plen
);
824 memcpy (newname
+ plen
, id
, ilen
+ 1);
826 return build_string (plen
+ ilen
, newname
);
834 static void handle_pragma_visibility (cpp_reader
*);
836 static vec
<int> visstack
;
838 /* Push the visibility indicated by STR onto the top of the #pragma
839 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
840 C++ namespace with visibility attribute and 2 for C++ builtin
841 ABI namespace. push_visibility/pop_visibility calls must have
842 matching KIND, it is not allowed to push visibility using one
843 KIND and pop using a different one. */
846 push_visibility (const char *str
, int kind
)
848 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
849 if (!strcmp (str
, "default"))
850 default_visibility
= VISIBILITY_DEFAULT
;
851 else if (!strcmp (str
, "internal"))
852 default_visibility
= VISIBILITY_INTERNAL
;
853 else if (!strcmp (str
, "hidden"))
854 default_visibility
= VISIBILITY_HIDDEN
;
855 else if (!strcmp (str
, "protected"))
856 default_visibility
= VISIBILITY_PROTECTED
;
858 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
859 visibility_options
.inpragma
= 1;
862 /* Pop a level of the #pragma visibility stack. Return true if
866 pop_visibility (int kind
)
868 if (!visstack
.length ())
870 if ((visstack
.last () >> 8) != kind
)
873 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
874 visibility_options
.inpragma
875 = visstack
.length () != 0;
879 /* Sets the default visibility for symbols to something other than that
880 specified on the command line. */
883 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
885 /* Form is #pragma GCC visibility push(hidden)|pop */
887 enum cpp_ttype token
;
888 enum { bad
, push
, pop
} action
= bad
;
890 token
= pragma_lex (&x
);
891 if (token
== CPP_NAME
)
893 const char *op
= IDENTIFIER_POINTER (x
);
894 if (!strcmp (op
, "push"))
896 else if (!strcmp (op
, "pop"))
900 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
905 if (! pop_visibility (0))
906 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
910 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
911 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
912 token
= pragma_lex (&x
);
913 if (token
!= CPP_NAME
)
914 GCC_BAD ("malformed #pragma GCC visibility push");
916 push_visibility (IDENTIFIER_POINTER (x
), 0);
917 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
918 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
921 if (pragma_lex (&x
) != CPP_EOF
)
922 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
926 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
928 const char *kind_string
, *option_string
;
929 unsigned int option_index
;
930 enum cpp_ttype token
;
933 struct cl_option_handlers handlers
;
935 token
= pragma_lex (&x
);
936 if (token
!= CPP_NAME
)
937 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
938 kind_string
= IDENTIFIER_POINTER (x
);
939 if (strcmp (kind_string
, "error") == 0)
941 else if (strcmp (kind_string
, "warning") == 0)
943 else if (strcmp (kind_string
, "ignored") == 0)
945 else if (strcmp (kind_string
, "push") == 0)
947 diagnostic_push_diagnostics (global_dc
, input_location
);
950 else if (strcmp (kind_string
, "pop") == 0)
952 diagnostic_pop_diagnostics (global_dc
, input_location
);
956 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
958 token
= pragma_lex (&x
);
959 if (token
!= CPP_STRING
)
960 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
961 option_string
= TREE_STRING_POINTER (x
);
962 set_default_handlers (&handlers
);
963 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
964 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
966 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
967 input_location
, c_family_lang_mask
, &handlers
,
968 &global_options
, &global_options_set
,
972 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
975 /* Parse #pragma GCC target (xxx) to set target specific options. */
977 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
979 enum cpp_ttype token
;
981 bool close_paren_needed_p
= false;
985 error ("#pragma GCC option is not allowed inside functions");
989 token
= pragma_lex (&x
);
990 if (token
== CPP_OPEN_PAREN
)
992 close_paren_needed_p
= true;
993 token
= pragma_lex (&x
);
996 if (token
!= CPP_STRING
)
998 GCC_BAD ("%<#pragma GCC option%> is not a string");
1002 /* Strings are user options. */
1005 tree args
= NULL_TREE
;
1009 /* Build up the strings now as a tree linked list. Skip empty
1011 if (TREE_STRING_LENGTH (x
) > 0)
1012 args
= tree_cons (NULL_TREE
, x
, args
);
1014 token
= pragma_lex (&x
);
1015 while (token
== CPP_COMMA
)
1016 token
= pragma_lex (&x
);
1018 while (token
== CPP_STRING
);
1020 if (close_paren_needed_p
)
1022 if (token
== CPP_CLOSE_PAREN
)
1023 token
= pragma_lex (&x
);
1025 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
1026 "not have a final %<)%>");
1029 if (token
!= CPP_EOF
)
1031 error ("#pragma GCC target string... is badly formed");
1035 /* put arguments in the order the user typed them. */
1036 args
= nreverse (args
);
1038 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
1039 current_target_pragma
= args
;
1043 /* Handle #pragma GCC optimize to set optimization options. */
1045 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
1047 enum cpp_ttype token
;
1049 bool close_paren_needed_p
= false;
1050 tree optimization_previous_node
= optimization_current_node
;
1054 error ("#pragma GCC optimize is not allowed inside functions");
1058 token
= pragma_lex (&x
);
1059 if (token
== CPP_OPEN_PAREN
)
1061 close_paren_needed_p
= true;
1062 token
= pragma_lex (&x
);
1065 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
1067 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
1071 /* Strings/numbers are user options. */
1074 tree args
= NULL_TREE
;
1078 /* Build up the numbers/strings now as a list. */
1079 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
1080 args
= tree_cons (NULL_TREE
, x
, args
);
1082 token
= pragma_lex (&x
);
1083 while (token
== CPP_COMMA
)
1084 token
= pragma_lex (&x
);
1086 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
1088 if (close_paren_needed_p
)
1090 if (token
== CPP_CLOSE_PAREN
)
1091 token
= pragma_lex (&x
);
1093 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
1094 "not have a final %<)%>");
1097 if (token
!= CPP_EOF
)
1099 error ("#pragma GCC optimize string... is badly formed");
1103 /* put arguments in the order the user typed them. */
1104 args
= nreverse (args
);
1106 parse_optimize_options (args
, false);
1107 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
1108 optimization_current_node
= build_optimization_node ();
1109 c_cpp_builtins_optimize_pragma (parse_in
,
1110 optimization_previous_node
,
1111 optimization_current_node
);
1115 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1116 both the binary representation of the options and the TREE_LIST of
1117 strings that will be added to the function's attribute list. */
1118 typedef struct GTY(()) opt_stack
{
1119 struct opt_stack
*prev
;
1121 tree target_strings
;
1122 tree optimize_binary
;
1123 tree optimize_strings
;
1126 static GTY(()) struct opt_stack
* options_stack
;
1128 /* Handle #pragma GCC push_options to save the current target and optimization
1132 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
1134 enum cpp_ttype token
;
1138 token
= pragma_lex (&x
);
1139 if (token
!= CPP_EOF
)
1141 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
1145 p
= ggc_alloc_opt_stack ();
1146 p
->prev
= options_stack
;
1149 /* Save optimization and target flags in binary format. */
1150 p
->optimize_binary
= build_optimization_node ();
1151 p
->target_binary
= build_target_option_node ();
1153 /* Save optimization and target flags in string list format. */
1154 p
->optimize_strings
= copy_list (current_optimize_pragma
);
1155 p
->target_strings
= copy_list (current_target_pragma
);
1158 /* Handle #pragma GCC pop_options to restore the current target and
1159 optimization options from a previous push_options. */
1162 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
1164 enum cpp_ttype token
;
1168 token
= pragma_lex (&x
);
1169 if (token
!= CPP_EOF
)
1171 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
1175 if (! options_stack
)
1177 warning (OPT_Wpragmas
,
1178 "%<#pragma GCC pop_options%> without a corresponding "
1179 "%<#pragma GCC push_options%>");
1184 options_stack
= p
->prev
;
1186 if (p
->target_binary
!= target_option_current_node
)
1188 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
1189 target_option_current_node
= p
->target_binary
;
1192 if (p
->optimize_binary
!= optimization_current_node
)
1194 tree old_optimize
= optimization_current_node
;
1195 cl_optimization_restore (&global_options
,
1196 TREE_OPTIMIZATION (p
->optimize_binary
));
1197 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
1198 p
->optimize_binary
);
1199 optimization_current_node
= p
->optimize_binary
;
1202 current_target_pragma
= p
->target_strings
;
1203 current_optimize_pragma
= p
->optimize_strings
;
1206 /* Handle #pragma GCC reset_options to restore the current target and
1207 optimization options to the original options used on the command line. */
1210 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
1212 enum cpp_ttype token
;
1214 tree new_optimize
= optimization_default_node
;
1215 tree new_target
= target_option_default_node
;
1217 token
= pragma_lex (&x
);
1218 if (token
!= CPP_EOF
)
1220 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1224 if (new_target
!= target_option_current_node
)
1226 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1227 target_option_current_node
= new_target
;
1230 if (new_optimize
!= optimization_current_node
)
1232 tree old_optimize
= optimization_current_node
;
1233 cl_optimization_restore (&global_options
,
1234 TREE_OPTIMIZATION (new_optimize
));
1235 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1236 optimization_current_node
= new_optimize
;
1239 current_target_pragma
= NULL_TREE
;
1240 current_optimize_pragma
= NULL_TREE
;
1243 /* Print a plain user-specified message. */
1246 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1248 enum cpp_ttype token
;
1249 tree x
, message
= 0;
1251 token
= pragma_lex (&x
);
1252 if (token
== CPP_OPEN_PAREN
)
1254 token
= pragma_lex (&x
);
1255 if (token
== CPP_STRING
)
1258 GCC_BAD ("expected a string after %<#pragma message%>");
1259 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1260 GCC_BAD ("malformed %<#pragma message%>, ignored");
1262 else if (token
== CPP_STRING
)
1265 GCC_BAD ("expected a string after %<#pragma message%>");
1267 gcc_assert (message
);
1269 if (pragma_lex (&x
) != CPP_EOF
)
1270 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1272 if (TREE_STRING_LENGTH (message
) > 1)
1273 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1276 /* Mark whether the current location is valid for a STDC pragma. */
1278 static bool valid_location_for_stdc_pragma
;
1281 mark_valid_location_for_stdc_pragma (bool flag
)
1283 valid_location_for_stdc_pragma
= flag
;
1286 /* Return true if the current location is valid for a STDC pragma. */
1289 valid_location_for_stdc_pragma_p (void)
1291 return valid_location_for_stdc_pragma
;
1294 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1296 /* A STDC pragma must appear outside of external declarations or
1297 preceding all explicit declarations and statements inside a compound
1298 statement; its behavior is undefined if used in any other context.
1299 It takes a switch of ON, OFF, or DEFAULT. */
1301 static enum pragma_switch_t
1302 handle_stdc_pragma (const char *pname
)
1306 enum pragma_switch_t ret
;
1308 if (!valid_location_for_stdc_pragma_p ())
1310 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1315 if (pragma_lex (&t
) != CPP_NAME
)
1317 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1321 arg
= IDENTIFIER_POINTER (t
);
1323 if (!strcmp (arg
, "ON"))
1325 else if (!strcmp (arg
, "OFF"))
1327 else if (!strcmp (arg
, "DEFAULT"))
1328 ret
= PRAGMA_DEFAULT
;
1331 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1335 if (pragma_lex (&t
) != CPP_EOF
)
1337 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1344 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1345 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1346 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1349 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1351 if (c_dialect_cxx ())
1353 if (warn_unknown_pragmas
> in_system_header
)
1354 warning (OPT_Wunknown_pragmas
,
1355 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1360 if (!targetm
.decimal_float_supported_p ())
1362 if (warn_unknown_pragmas
> in_system_header
)
1363 warning (OPT_Wunknown_pragmas
,
1364 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1369 pedwarn (input_location
, OPT_Wpedantic
,
1370 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1372 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1375 set_float_const_decimal64 ();
1378 case PRAGMA_DEFAULT
:
1379 clear_float_const_decimal64 ();
1386 /* A vector of registered pragma callbacks, which is never freed. */
1388 static vec
<internal_pragma_handler
> registered_pragmas
;
1397 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1399 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1400 static const struct omp_pragma_def omp_pragmas
[] = {
1401 { "atomic", PRAGMA_OMP_ATOMIC
},
1402 { "barrier", PRAGMA_OMP_BARRIER
},
1403 { "critical", PRAGMA_OMP_CRITICAL
},
1404 { "flush", PRAGMA_OMP_FLUSH
},
1405 { "for", PRAGMA_OMP_FOR
},
1406 { "master", PRAGMA_OMP_MASTER
},
1407 { "ordered", PRAGMA_OMP_ORDERED
},
1408 { "parallel", PRAGMA_OMP_PARALLEL
},
1409 { "section", PRAGMA_OMP_SECTION
},
1410 { "sections", PRAGMA_OMP_SECTIONS
},
1411 { "single", PRAGMA_OMP_SINGLE
},
1412 { "task", PRAGMA_OMP_TASK
},
1413 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1414 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1415 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1419 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1421 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1424 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1425 if (omp_pragmas
[i
].id
== id
)
1428 *name
= omp_pragmas
[i
].name
;
1432 if (id
>= PRAGMA_FIRST_EXTERNAL
1433 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1435 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1436 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1443 /* Front-end wrappers for pragma registration to avoid dragging
1444 cpplib.h in almost everywhere. */
1447 c_register_pragma_1 (const char *space
, const char *name
,
1448 internal_pragma_handler ihandler
, bool allow_expansion
)
1452 if (flag_preprocess_only
)
1454 pragma_ns_name ns_name
;
1456 if (!allow_expansion
)
1459 ns_name
.space
= space
;
1460 ns_name
.name
= name
;
1461 registered_pp_pragmas
.safe_push (ns_name
);
1462 id
= registered_pp_pragmas
.length ();
1463 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1467 registered_pragmas
.safe_push (ihandler
);
1468 id
= registered_pragmas
.length ();
1469 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1471 /* The C++ front end allocates 6 bits in cp_token; the C front end
1472 allocates 7 bits in c_token. At present this is sufficient. */
1473 gcc_assert (id
< 64);
1476 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1477 allow_expansion
, false);
1480 /* Register a C pragma handler, using a space and a name. It disallows pragma
1481 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1483 c_register_pragma (const char *space
, const char *name
,
1484 pragma_handler_1arg handler
)
1486 internal_pragma_handler ihandler
;
1488 ihandler
.handler
.handler_1arg
= handler
;
1489 ihandler
.extra_data
= false;
1490 ihandler
.data
= NULL
;
1491 c_register_pragma_1 (space
, name
, ihandler
, false);
1494 /* Register a C pragma handler, using a space and a name, it also carries an
1495 extra data field which can be used by the handler. It disallows pragma
1496 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1499 c_register_pragma_with_data (const char *space
, const char *name
,
1500 pragma_handler_2arg handler
, void * data
)
1502 internal_pragma_handler ihandler
;
1504 ihandler
.handler
.handler_2arg
= handler
;
1505 ihandler
.extra_data
= true;
1506 ihandler
.data
= data
;
1507 c_register_pragma_1 (space
, name
, ihandler
, false);
1510 /* Register a C pragma handler, using a space and a name. It allows pragma
1511 expansion as in the following example:
1514 #pragma count (NUMBER)
1516 Name expansion is still disallowed. */
1518 c_register_pragma_with_expansion (const char *space
, const char *name
,
1519 pragma_handler_1arg handler
)
1521 internal_pragma_handler ihandler
;
1523 ihandler
.handler
.handler_1arg
= handler
;
1524 ihandler
.extra_data
= false;
1525 ihandler
.data
= NULL
;
1526 c_register_pragma_1 (space
, name
, ihandler
, true);
1529 /* Register a C pragma handler, using a space and a name, it also carries an
1530 extra data field which can be used by the handler. It allows pragma
1531 expansion as in the following example:
1534 #pragma count (NUMBER)
1536 Name expansion is still disallowed. */
1538 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1539 pragma_handler_2arg handler
,
1542 internal_pragma_handler ihandler
;
1544 ihandler
.handler
.handler_2arg
= handler
;
1545 ihandler
.extra_data
= true;
1546 ihandler
.data
= data
;
1547 c_register_pragma_1 (space
, name
, ihandler
, true);
1551 c_invoke_pragma_handler (unsigned int id
)
1553 internal_pragma_handler
*ihandler
;
1554 pragma_handler_1arg handler_1arg
;
1555 pragma_handler_2arg handler_2arg
;
1557 id
-= PRAGMA_FIRST_EXTERNAL
;
1558 ihandler
= ®istered_pragmas
[id
];
1559 if (ihandler
->extra_data
)
1561 handler_2arg
= ihandler
->handler
.handler_2arg
;
1562 handler_2arg (parse_in
, ihandler
->data
);
1566 handler_1arg
= ihandler
->handler
.handler_1arg
;
1567 handler_1arg (parse_in
);
1571 /* Set up front-end pragmas. */
1577 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1580 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1581 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1582 omp_pragmas
[i
].id
, true, true);
1585 if (!flag_preprocess_only
)
1586 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1587 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1589 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1590 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1592 c_register_pragma (0, "pack", handle_pragma_pack
);
1594 c_register_pragma (0, "weak", handle_pragma_weak
);
1595 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1597 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1598 c_register_pragma ("GCC", "target", handle_pragma_target
);
1599 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1600 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1601 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1602 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1604 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1605 handle_pragma_float_const_decimal64
);
1607 c_register_pragma_with_expansion (0, "redefine_extname",
1608 handle_pragma_redefine_extname
);
1610 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1614 c_register_pragma (0, "upc", handle_pragma_upc
);
1616 c_register_pragma (0, "pupc", handle_pragma_pupc
);
1617 init_pragma_pupc ();
1620 #ifdef REGISTER_TARGET_PRAGMAS
1621 REGISTER_TARGET_PRAGMAS ();
1624 /* Allow plugins to register their own pragmas. */
1625 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1628 #include "gt-c-family-c-pragma.h"