1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "stringpool.h"
28 #include "gcc-symtab.h"
29 #include "function.h" /* For cfun. FIXME: Does the parser know
30 when it is inside a function, so that
31 we don't have to look at cfun? */
36 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
37 this not a target hook?). */
40 #include "diagnostic.h"
45 #define GCC_BAD(gmsgid) \
46 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
47 #define GCC_BAD2(gmsgid, arg) \
48 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
50 typedef struct GTY(()) align_stack
{
53 struct align_stack
* prev
;
56 static GTY(()) struct align_stack
* alignment_stack
;
58 static void handle_pragma_pack (cpp_reader
*);
60 /* If we have a "global" #pragma pack(<n>) in effect when the first
61 #pragma pack(push,<n>) is encountered, this stores the value of
62 maximum_field_alignment in effect. When the final pop_alignment()
63 happens, we restore the value to this, not to a value of 0 for
64 maximum_field_alignment. Value is in bits. */
65 static int default_alignment
;
66 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
67 ? &default_alignment \
68 : &alignment_stack->alignment) = (ALIGN))
70 static void push_alignment (int, tree
);
71 static void pop_alignment (tree
);
73 /* Push an alignment value onto the stack. */
75 push_alignment (int alignment
, tree id
)
79 entry
= ggc_alloc_align_stack ();
81 entry
->alignment
= alignment
;
83 entry
->prev
= alignment_stack
;
85 /* The current value of maximum_field_alignment is not necessarily
86 0 since there may be a #pragma pack(<n>) in effect; remember it
87 so that we can restore it after the final #pragma pop(). */
88 if (alignment_stack
== NULL
)
89 default_alignment
= maximum_field_alignment
;
91 alignment_stack
= entry
;
93 maximum_field_alignment
= alignment
;
96 /* Undo a push of an alignment onto the stack. */
98 pop_alignment (tree id
)
102 if (alignment_stack
== NULL
)
103 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
105 /* If we got an identifier, strip away everything above the target
106 entry so that the next step will restore the state just below it. */
109 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
112 alignment_stack
= entry
;
116 warning (OPT_Wpragmas
, "\
117 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
121 entry
= alignment_stack
->prev
;
123 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
125 alignment_stack
= entry
;
132 #pragma pack (push, N)
133 #pragma pack (push, ID)
134 #pragma pack (push, ID, N)
136 #pragma pack (pop, ID) */
138 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
142 enum cpp_ttype token
;
143 enum { set
, push
, pop
} action
;
145 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
146 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
148 token
= pragma_lex (&x
);
149 if (token
== CPP_CLOSE_PAREN
)
152 align
= initial_max_fld_align
;
154 else if (token
== CPP_NUMBER
)
156 if (TREE_CODE (x
) != INTEGER_CST
)
157 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
158 align
= TREE_INT_CST_LOW (x
);
160 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
161 GCC_BAD ("malformed %<#pragma pack%> - ignored");
163 else if (token
== CPP_NAME
)
165 #define GCC_BAD_ACTION do { if (action != pop) \
166 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
168 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
171 const char *op
= IDENTIFIER_POINTER (x
);
172 if (!strcmp (op
, "push"))
174 else if (!strcmp (op
, "pop"))
177 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
179 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
181 token
= pragma_lex (&x
);
182 if (token
== CPP_NAME
&& id
== 0)
186 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
188 if (TREE_CODE (x
) != INTEGER_CST
)
189 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
190 align
= TREE_INT_CST_LOW (x
);
198 if (token
!= CPP_CLOSE_PAREN
)
200 #undef GCC_BAD_ACTION
203 GCC_BAD ("malformed %<#pragma pack%> - ignored");
205 if (pragma_lex (&x
) != CPP_EOF
)
206 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
208 if (flag_pack_struct
)
209 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
220 align
*= BITS_PER_UNIT
;
225 align
= maximum_field_alignment
;
229 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
234 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
235 case push
: push_alignment (align
, id
); break;
236 case pop
: pop_alignment (id
); break;
240 typedef struct GTY(()) pending_weak_d
247 static GTY(()) vec
<pending_weak
, va_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 && DECL_ASSEMBLER_NAME_SET_P (decl
)
267 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
268 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
269 "results in unspecified behavior", decl
);
275 maybe_apply_pragma_weak (tree decl
)
281 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
283 /* No weak symbols pending, take the short-cut. */
284 if (vec_safe_is_empty (pending_weaks
))
286 /* If it's not visible outside this file, it doesn't matter whether
288 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
290 /* If it's not a function or a variable, it can't be weak.
291 FIXME: what kinds of things are visible outside this file but
292 aren't functions or variables? Should this be an assert instead? */
293 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
296 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
297 id
= DECL_ASSEMBLER_NAME (decl
);
300 id
= DECL_ASSEMBLER_NAME (decl
);
301 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
304 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
307 apply_pragma_weak (decl
, pe
->value
);
308 pending_weaks
->unordered_remove (i
);
313 /* Process all "#pragma weak A = B" directives where we have not seen
316 maybe_apply_pending_pragma_weaks (void)
318 tree alias_id
, id
, decl
;
323 if (vec_safe_is_empty (pending_weaks
))
326 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
334 target
= symtab_node_for_asm (id
);
335 decl
= build_decl (UNKNOWN_LOCATION
,
336 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
337 alias_id
, default_function_type
);
339 DECL_ARTIFICIAL (decl
) = 1;
340 TREE_PUBLIC (decl
) = 1;
341 DECL_WEAK (decl
) = 1;
342 if (TREE_CODE (decl
) == VAR_DECL
)
343 TREE_STATIC (decl
) = 1;
346 error ("%q+D aliased to undefined symbol %qE",
351 assemble_alias (decl
, id
);
355 /* #pragma weak name [= value] */
357 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
359 tree name
, value
, x
, decl
;
364 if (pragma_lex (&name
) != CPP_NAME
)
365 GCC_BAD ("malformed #pragma weak, ignored");
369 if (pragma_lex (&value
) != CPP_NAME
)
370 GCC_BAD ("malformed #pragma weak, ignored");
374 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
376 decl
= identifier_global_value (name
);
377 if (decl
&& DECL_P (decl
))
379 apply_pragma_weak (decl
, value
);
382 DECL_EXTERNAL (decl
) = 0;
383 if (TREE_CODE (decl
) == VAR_DECL
)
384 TREE_STATIC (decl
) = 1;
385 assemble_alias (decl
, value
);
390 pending_weak pe
= {name
, value
};
391 vec_safe_push (pending_weaks
, pe
);
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 VMS 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 typedef struct GTY(()) pending_redefinition_d
{
430 } pending_redefinition
;
433 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
435 static void handle_pragma_redefine_extname (cpp_reader
*);
437 /* #pragma redefine_extname oldname newname */
439 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
441 tree oldname
, newname
, decls
, x
;
445 if (pragma_lex (&oldname
) != CPP_NAME
)
446 GCC_BAD ("malformed #pragma redefine_extname, ignored");
447 if (pragma_lex (&newname
) != CPP_NAME
)
448 GCC_BAD ("malformed #pragma redefine_extname, ignored");
451 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
454 for (decls
= c_linkage_bindings (oldname
);
458 if (TREE_CODE (decls
) == TREE_LIST
)
460 decl
= TREE_VALUE (decls
);
461 decls
= TREE_CHAIN (decls
);
469 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
470 && (TREE_CODE (decl
) == FUNCTION_DECL
471 || TREE_CODE (decl
) == VAR_DECL
))
474 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
476 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
477 name
= targetm
.strip_name_encoding (name
);
479 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
480 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
481 "conflict with previous rename");
484 change_decl_assembler_name (decl
, newname
);
489 /* We have to add this to the rename list even if there's already
490 a global value that doesn't meet the above criteria, because in
491 C++ "struct foo {...};" puts "foo" in the current namespace but
492 does *not* conflict with a subsequent declaration of a function
493 or variable foo. See g++.dg/other/pragma-re-2.C. */
494 add_to_renaming_pragma_list (oldname
, newname
);
497 /* This is called from here and from ia64-c.c. */
499 add_to_renaming_pragma_list (tree oldname
, tree newname
)
502 pending_redefinition
*p
;
504 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
505 if (oldname
== p
->oldname
)
507 if (p
->newname
!= newname
)
508 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
509 "conflict with previous #pragma redefine_extname");
513 pending_redefinition e
= {oldname
, newname
};
514 vec_safe_push (pending_redefine_extname
, e
);
517 /* The current prefix set by #pragma extern_prefix. */
518 GTY(()) tree pragma_extern_prefix
;
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_SAFE_ELT (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 pending_redefine_extname
->unordered_remove (ix
);
562 /* Find out if we have a pending #pragma redefine_extname. */
563 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
564 if (DECL_NAME (decl
) == p
->oldname
)
566 tree newname
= p
->newname
;
567 pending_redefine_extname
->unordered_remove (ix
);
569 /* If we already have an asmname, #pragma redefine_extname is
570 ignored (with a warning if it conflicts). */
573 if (strcmp (TREE_STRING_POINTER (asmname
),
574 IDENTIFIER_POINTER (newname
)) != 0)
575 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
576 "conflict with __asm__ declaration");
580 /* Otherwise we use what we've got; #pragma extern_prefix is
582 return build_string (IDENTIFIER_LENGTH (newname
),
583 IDENTIFIER_POINTER (newname
));
586 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
590 /* If #pragma extern_prefix is in effect, apply it. */
591 if (pragma_extern_prefix
)
593 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
594 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
596 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
597 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
599 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
601 memcpy (newname
, prefix
, plen
);
602 memcpy (newname
+ plen
, id
, ilen
+ 1);
604 return build_string (plen
+ ilen
, newname
);
612 static void handle_pragma_visibility (cpp_reader
*);
614 static vec
<int> visstack
;
616 /* Push the visibility indicated by STR onto the top of the #pragma
617 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
618 C++ namespace with visibility attribute and 2 for C++ builtin
619 ABI namespace. push_visibility/pop_visibility calls must have
620 matching KIND, it is not allowed to push visibility using one
621 KIND and pop using a different one. */
624 push_visibility (const char *str
, int kind
)
626 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
627 if (!strcmp (str
, "default"))
628 default_visibility
= VISIBILITY_DEFAULT
;
629 else if (!strcmp (str
, "internal"))
630 default_visibility
= VISIBILITY_INTERNAL
;
631 else if (!strcmp (str
, "hidden"))
632 default_visibility
= VISIBILITY_HIDDEN
;
633 else if (!strcmp (str
, "protected"))
634 default_visibility
= VISIBILITY_PROTECTED
;
636 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
637 visibility_options
.inpragma
= 1;
640 /* Pop a level of the #pragma visibility stack. Return true if
644 pop_visibility (int kind
)
646 if (!visstack
.length ())
648 if ((visstack
.last () >> 8) != kind
)
651 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
652 visibility_options
.inpragma
653 = visstack
.length () != 0;
657 /* Sets the default visibility for symbols to something other than that
658 specified on the command line. */
661 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
663 /* Form is #pragma GCC visibility push(hidden)|pop */
665 enum cpp_ttype token
;
666 enum { bad
, push
, pop
} action
= bad
;
668 token
= pragma_lex (&x
);
669 if (token
== CPP_NAME
)
671 const char *op
= IDENTIFIER_POINTER (x
);
672 if (!strcmp (op
, "push"))
674 else if (!strcmp (op
, "pop"))
678 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
683 if (! pop_visibility (0))
684 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
688 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
689 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
690 token
= pragma_lex (&x
);
691 if (token
!= CPP_NAME
)
692 GCC_BAD ("malformed #pragma GCC visibility push");
694 push_visibility (IDENTIFIER_POINTER (x
), 0);
695 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
696 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
699 if (pragma_lex (&x
) != CPP_EOF
)
700 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
704 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
706 const char *kind_string
, *option_string
;
707 unsigned int option_index
;
708 enum cpp_ttype token
;
711 struct cl_option_handlers handlers
;
713 token
= pragma_lex (&x
);
714 if (token
!= CPP_NAME
)
715 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
716 kind_string
= IDENTIFIER_POINTER (x
);
717 if (strcmp (kind_string
, "error") == 0)
719 else if (strcmp (kind_string
, "warning") == 0)
721 else if (strcmp (kind_string
, "ignored") == 0)
723 else if (strcmp (kind_string
, "push") == 0)
725 diagnostic_push_diagnostics (global_dc
, input_location
);
728 else if (strcmp (kind_string
, "pop") == 0)
730 diagnostic_pop_diagnostics (global_dc
, input_location
);
734 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
736 token
= pragma_lex (&x
);
737 if (token
!= CPP_STRING
)
738 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
739 option_string
= TREE_STRING_POINTER (x
);
740 set_default_handlers (&handlers
);
741 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
742 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
744 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
745 input_location
, c_family_lang_mask
, &handlers
,
746 &global_options
, &global_options_set
,
750 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
753 /* Parse #pragma GCC target (xxx) to set target specific options. */
755 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
757 enum cpp_ttype token
;
759 bool close_paren_needed_p
= false;
763 error ("#pragma GCC option is not allowed inside functions");
767 token
= pragma_lex (&x
);
768 if (token
== CPP_OPEN_PAREN
)
770 close_paren_needed_p
= true;
771 token
= pragma_lex (&x
);
774 if (token
!= CPP_STRING
)
776 GCC_BAD ("%<#pragma GCC option%> is not a string");
780 /* Strings are user options. */
783 tree args
= NULL_TREE
;
787 /* Build up the strings now as a tree linked list. Skip empty
789 if (TREE_STRING_LENGTH (x
) > 0)
790 args
= tree_cons (NULL_TREE
, x
, args
);
792 token
= pragma_lex (&x
);
793 while (token
== CPP_COMMA
)
794 token
= pragma_lex (&x
);
796 while (token
== CPP_STRING
);
798 if (close_paren_needed_p
)
800 if (token
== CPP_CLOSE_PAREN
)
801 token
= pragma_lex (&x
);
803 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
804 "not have a final %<)%>");
807 if (token
!= CPP_EOF
)
809 error ("#pragma GCC target string... is badly formed");
813 /* put arguments in the order the user typed them. */
814 args
= nreverse (args
);
816 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
817 current_target_pragma
= args
;
821 /* Handle #pragma GCC optimize to set optimization options. */
823 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
825 enum cpp_ttype token
;
827 bool close_paren_needed_p
= false;
828 tree optimization_previous_node
= optimization_current_node
;
832 error ("#pragma GCC optimize is not allowed inside functions");
836 token
= pragma_lex (&x
);
837 if (token
== CPP_OPEN_PAREN
)
839 close_paren_needed_p
= true;
840 token
= pragma_lex (&x
);
843 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
845 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
849 /* Strings/numbers are user options. */
852 tree args
= NULL_TREE
;
856 /* Build up the numbers/strings now as a list. */
857 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
858 args
= tree_cons (NULL_TREE
, x
, args
);
860 token
= pragma_lex (&x
);
861 while (token
== CPP_COMMA
)
862 token
= pragma_lex (&x
);
864 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
866 if (close_paren_needed_p
)
868 if (token
== CPP_CLOSE_PAREN
)
869 token
= pragma_lex (&x
);
871 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
872 "not have a final %<)%>");
875 if (token
!= CPP_EOF
)
877 error ("#pragma GCC optimize string... is badly formed");
881 /* put arguments in the order the user typed them. */
882 args
= nreverse (args
);
884 parse_optimize_options (args
, false);
885 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
886 optimization_current_node
= build_optimization_node (&global_options
);
887 c_cpp_builtins_optimize_pragma (parse_in
,
888 optimization_previous_node
,
889 optimization_current_node
);
893 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
894 both the binary representation of the options and the TREE_LIST of
895 strings that will be added to the function's attribute list. */
896 typedef struct GTY(()) opt_stack
{
897 struct opt_stack
*prev
;
900 tree optimize_binary
;
901 tree optimize_strings
;
904 static GTY(()) struct opt_stack
* options_stack
;
906 /* Handle #pragma GCC push_options to save the current target and optimization
910 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
912 enum cpp_ttype token
;
916 token
= pragma_lex (&x
);
917 if (token
!= CPP_EOF
)
919 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
923 p
= ggc_alloc_opt_stack ();
924 p
->prev
= options_stack
;
927 /* Save optimization and target flags in binary format. */
928 p
->optimize_binary
= build_optimization_node (&global_options
);
929 p
->target_binary
= build_target_option_node (&global_options
);
931 /* Save optimization and target flags in string list format. */
932 p
->optimize_strings
= copy_list (current_optimize_pragma
);
933 p
->target_strings
= copy_list (current_target_pragma
);
936 /* Handle #pragma GCC pop_options to restore the current target and
937 optimization options from a previous push_options. */
940 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
942 enum cpp_ttype token
;
946 token
= pragma_lex (&x
);
947 if (token
!= CPP_EOF
)
949 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
955 warning (OPT_Wpragmas
,
956 "%<#pragma GCC pop_options%> without a corresponding "
957 "%<#pragma GCC push_options%>");
962 options_stack
= p
->prev
;
964 if (p
->target_binary
!= target_option_current_node
)
966 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
967 target_option_current_node
= p
->target_binary
;
970 if (p
->optimize_binary
!= optimization_current_node
)
972 tree old_optimize
= optimization_current_node
;
973 cl_optimization_restore (&global_options
,
974 TREE_OPTIMIZATION (p
->optimize_binary
));
975 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
977 optimization_current_node
= p
->optimize_binary
;
980 current_target_pragma
= p
->target_strings
;
981 current_optimize_pragma
= p
->optimize_strings
;
984 /* Handle #pragma GCC reset_options to restore the current target and
985 optimization options to the original options used on the command line. */
988 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
990 enum cpp_ttype token
;
992 tree new_optimize
= optimization_default_node
;
993 tree new_target
= target_option_default_node
;
995 token
= pragma_lex (&x
);
996 if (token
!= CPP_EOF
)
998 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1002 if (new_target
!= target_option_current_node
)
1004 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1005 target_option_current_node
= new_target
;
1008 if (new_optimize
!= optimization_current_node
)
1010 tree old_optimize
= optimization_current_node
;
1011 cl_optimization_restore (&global_options
,
1012 TREE_OPTIMIZATION (new_optimize
));
1013 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1014 optimization_current_node
= new_optimize
;
1017 current_target_pragma
= NULL_TREE
;
1018 current_optimize_pragma
= NULL_TREE
;
1021 /* Print a plain user-specified message. */
1024 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1026 enum cpp_ttype token
;
1027 tree x
, message
= 0;
1029 token
= pragma_lex (&x
);
1030 if (token
== CPP_OPEN_PAREN
)
1032 token
= pragma_lex (&x
);
1033 if (token
== CPP_STRING
)
1036 GCC_BAD ("expected a string after %<#pragma message%>");
1037 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1038 GCC_BAD ("malformed %<#pragma message%>, ignored");
1040 else if (token
== CPP_STRING
)
1043 GCC_BAD ("expected a string after %<#pragma message%>");
1045 gcc_assert (message
);
1047 if (pragma_lex (&x
) != CPP_EOF
)
1048 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1050 if (TREE_STRING_LENGTH (message
) > 1)
1051 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1054 /* Mark whether the current location is valid for a STDC pragma. */
1056 static bool valid_location_for_stdc_pragma
;
1059 mark_valid_location_for_stdc_pragma (bool flag
)
1061 valid_location_for_stdc_pragma
= flag
;
1064 /* Return true if the current location is valid for a STDC pragma. */
1067 valid_location_for_stdc_pragma_p (void)
1069 return valid_location_for_stdc_pragma
;
1072 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1074 /* A STDC pragma must appear outside of external declarations or
1075 preceding all explicit declarations and statements inside a compound
1076 statement; its behavior is undefined if used in any other context.
1077 It takes a switch of ON, OFF, or DEFAULT. */
1079 static enum pragma_switch_t
1080 handle_stdc_pragma (const char *pname
)
1084 enum pragma_switch_t ret
;
1086 if (!valid_location_for_stdc_pragma_p ())
1088 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1093 if (pragma_lex (&t
) != CPP_NAME
)
1095 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1099 arg
= IDENTIFIER_POINTER (t
);
1101 if (!strcmp (arg
, "ON"))
1103 else if (!strcmp (arg
, "OFF"))
1105 else if (!strcmp (arg
, "DEFAULT"))
1106 ret
= PRAGMA_DEFAULT
;
1109 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1113 if (pragma_lex (&t
) != CPP_EOF
)
1115 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1122 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1123 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1124 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1127 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1129 if (c_dialect_cxx ())
1131 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1132 warning (OPT_Wunknown_pragmas
,
1133 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1138 if (!targetm
.decimal_float_supported_p ())
1140 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1141 warning (OPT_Wunknown_pragmas
,
1142 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1147 pedwarn (input_location
, OPT_Wpedantic
,
1148 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1150 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1153 set_float_const_decimal64 ();
1156 case PRAGMA_DEFAULT
:
1157 clear_float_const_decimal64 ();
1164 /* A vector of registered pragma callbacks, which is never freed. */
1166 static vec
<internal_pragma_handler
> registered_pragmas
;
1175 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1177 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1178 static const struct omp_pragma_def omp_pragmas
[] = {
1179 { "atomic", PRAGMA_OMP_ATOMIC
},
1180 { "barrier", PRAGMA_OMP_BARRIER
},
1181 { "cancel", PRAGMA_OMP_CANCEL
},
1182 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1183 { "critical", PRAGMA_OMP_CRITICAL
},
1184 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1185 { "flush", PRAGMA_OMP_FLUSH
},
1186 { "master", PRAGMA_OMP_MASTER
},
1187 { "ordered", PRAGMA_OMP_ORDERED
},
1188 { "section", PRAGMA_OMP_SECTION
},
1189 { "sections", PRAGMA_OMP_SECTIONS
},
1190 { "single", PRAGMA_OMP_SINGLE
},
1191 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1192 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1193 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1194 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1196 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1197 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1198 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1199 { "for", PRAGMA_OMP_FOR
},
1200 { "parallel", PRAGMA_OMP_PARALLEL
},
1201 { "simd", PRAGMA_OMP_SIMD
},
1202 { "target", PRAGMA_OMP_TARGET
},
1203 { "task", PRAGMA_OMP_TASK
},
1204 { "teams", PRAGMA_OMP_TEAMS
},
1208 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1210 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1211 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1212 / sizeof (*omp_pragmas
);
1215 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1216 if (omp_pragmas
[i
].id
== id
)
1219 *name
= omp_pragmas
[i
].name
;
1223 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1224 if (omp_pragmas_simd
[i
].id
== id
)
1227 *name
= omp_pragmas_simd
[i
].name
;
1231 if (id
== PRAGMA_CILK_SIMD
)
1238 if (id
>= PRAGMA_FIRST_EXTERNAL
1239 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1241 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1242 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1249 /* Front-end wrappers for pragma registration to avoid dragging
1250 cpplib.h in almost everywhere. */
1253 c_register_pragma_1 (const char *space
, const char *name
,
1254 internal_pragma_handler ihandler
, bool allow_expansion
)
1258 if (flag_preprocess_only
)
1260 pragma_ns_name ns_name
;
1262 if (!allow_expansion
)
1265 ns_name
.space
= space
;
1266 ns_name
.name
= name
;
1267 registered_pp_pragmas
.safe_push (ns_name
);
1268 id
= registered_pp_pragmas
.length ();
1269 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1273 registered_pragmas
.safe_push (ihandler
);
1274 id
= registered_pragmas
.length ();
1275 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1277 /* The C++ front end allocates 6 bits in cp_token; the C front end
1278 allocates 7 bits in c_token. At present this is sufficient. */
1279 gcc_assert (id
< 64);
1282 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1283 allow_expansion
, false);
1286 /* Register a C pragma handler, using a space and a name. It disallows pragma
1287 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1289 c_register_pragma (const char *space
, const char *name
,
1290 pragma_handler_1arg handler
)
1292 internal_pragma_handler ihandler
;
1294 ihandler
.handler
.handler_1arg
= handler
;
1295 ihandler
.extra_data
= false;
1296 ihandler
.data
= NULL
;
1297 c_register_pragma_1 (space
, name
, ihandler
, false);
1300 /* Register a C pragma handler, using a space and a name, it also carries an
1301 extra data field which can be used by the handler. It disallows pragma
1302 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1305 c_register_pragma_with_data (const char *space
, const char *name
,
1306 pragma_handler_2arg handler
, void * data
)
1308 internal_pragma_handler ihandler
;
1310 ihandler
.handler
.handler_2arg
= handler
;
1311 ihandler
.extra_data
= true;
1312 ihandler
.data
= data
;
1313 c_register_pragma_1 (space
, name
, ihandler
, false);
1316 /* Register a C pragma handler, using a space and a name. It allows pragma
1317 expansion as in the following example:
1320 #pragma count (NUMBER)
1322 Name expansion is still disallowed. */
1324 c_register_pragma_with_expansion (const char *space
, const char *name
,
1325 pragma_handler_1arg handler
)
1327 internal_pragma_handler ihandler
;
1329 ihandler
.handler
.handler_1arg
= handler
;
1330 ihandler
.extra_data
= false;
1331 ihandler
.data
= NULL
;
1332 c_register_pragma_1 (space
, name
, ihandler
, true);
1335 /* Register a C pragma handler, using a space and a name, it also carries an
1336 extra data field which can be used by the handler. It allows pragma
1337 expansion as in the following example:
1340 #pragma count (NUMBER)
1342 Name expansion is still disallowed. */
1344 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1345 pragma_handler_2arg handler
,
1348 internal_pragma_handler ihandler
;
1350 ihandler
.handler
.handler_2arg
= handler
;
1351 ihandler
.extra_data
= true;
1352 ihandler
.data
= data
;
1353 c_register_pragma_1 (space
, name
, ihandler
, true);
1357 c_invoke_pragma_handler (unsigned int id
)
1359 internal_pragma_handler
*ihandler
;
1360 pragma_handler_1arg handler_1arg
;
1361 pragma_handler_2arg handler_2arg
;
1363 id
-= PRAGMA_FIRST_EXTERNAL
;
1364 ihandler
= ®istered_pragmas
[id
];
1365 if (ihandler
->extra_data
)
1367 handler_2arg
= ihandler
->handler
.handler_2arg
;
1368 handler_2arg (parse_in
, ihandler
->data
);
1372 handler_1arg
= ihandler
->handler
.handler_1arg
;
1373 handler_1arg (parse_in
);
1377 /* Set up front-end pragmas. */
1383 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1386 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1387 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1388 omp_pragmas
[i
].id
, true, true);
1390 if (flag_openmp
|| flag_openmp_simd
)
1392 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1393 / sizeof (*omp_pragmas
);
1396 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1397 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1398 omp_pragmas_simd
[i
].id
, true, true);
1402 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1405 if (!flag_preprocess_only
)
1406 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1407 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1409 if (!flag_preprocess_only
)
1410 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1412 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1413 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1415 c_register_pragma (0, "pack", handle_pragma_pack
);
1417 c_register_pragma (0, "weak", handle_pragma_weak
);
1418 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1420 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1421 c_register_pragma ("GCC", "target", handle_pragma_target
);
1422 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1423 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1424 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1425 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1427 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1428 handle_pragma_float_const_decimal64
);
1430 c_register_pragma_with_expansion (0, "redefine_extname",
1431 handle_pragma_redefine_extname
);
1433 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1435 #ifdef REGISTER_TARGET_PRAGMAS
1436 REGISTER_TARGET_PRAGMAS ();
1439 /* Allow plugins to register their own pragmas. */
1440 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1443 #include "gt-c-family-c-pragma.h"