1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2015 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"
27 #include "stringpool.h"
30 #include "hard-reg-set.h"
31 #include "function.h" /* For cfun. FIXME: Does the parser know
32 when it is inside a function, so that
33 we don't have to look at cfun? */
38 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
39 this not a target hook?). */
41 #include "diagnostic.h"
46 #define GCC_BAD(gmsgid) \
47 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
48 #define GCC_BAD2(gmsgid, arg) \
49 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
51 typedef struct GTY(()) align_stack
{
54 struct align_stack
* prev
;
57 static GTY(()) struct align_stack
* alignment_stack
;
59 static void handle_pragma_pack (cpp_reader
*);
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
)
78 align_stack
* entry
= ggc_alloc
<align_stack
> ();
80 entry
->alignment
= alignment
;
82 entry
->prev
= alignment_stack
;
84 /* The current value of maximum_field_alignment is not necessarily
85 0 since there may be a #pragma pack(<n>) in effect; remember it
86 so that we can restore it after the final #pragma pop(). */
87 if (alignment_stack
== NULL
)
88 default_alignment
= maximum_field_alignment
;
90 alignment_stack
= entry
;
92 maximum_field_alignment
= alignment
;
95 /* Undo a push of an alignment onto the stack. */
97 pop_alignment (tree id
)
101 if (alignment_stack
== NULL
)
102 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
104 /* If we got an identifier, strip away everything above the target
105 entry so that the next step will restore the state just below it. */
108 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
111 alignment_stack
= entry
;
115 warning (OPT_Wpragmas
, "\
116 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
120 entry
= alignment_stack
->prev
;
122 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
124 alignment_stack
= entry
;
131 #pragma pack (push, N)
132 #pragma pack (push, ID)
133 #pragma pack (push, ID, N)
135 #pragma pack (pop, ID) */
137 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
141 enum cpp_ttype token
;
142 enum { set
, push
, pop
} action
;
144 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
145 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
147 token
= pragma_lex (&x
);
148 if (token
== CPP_CLOSE_PAREN
)
151 align
= initial_max_fld_align
;
153 else if (token
== CPP_NUMBER
)
155 if (TREE_CODE (x
) != INTEGER_CST
)
156 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
157 align
= TREE_INT_CST_LOW (x
);
159 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
160 GCC_BAD ("malformed %<#pragma pack%> - ignored");
162 else if (token
== CPP_NAME
)
164 #define GCC_BAD_ACTION do { if (action != pop) \
165 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
167 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
170 const char *op
= IDENTIFIER_POINTER (x
);
171 if (!strcmp (op
, "push"))
173 else if (!strcmp (op
, "pop"))
176 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
178 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
180 token
= pragma_lex (&x
);
181 if (token
== CPP_NAME
&& id
== 0)
185 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
187 if (TREE_CODE (x
) != INTEGER_CST
)
188 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
189 align
= TREE_INT_CST_LOW (x
);
197 if (token
!= CPP_CLOSE_PAREN
)
199 #undef GCC_BAD_ACTION
202 GCC_BAD ("malformed %<#pragma pack%> - ignored");
204 if (pragma_lex (&x
) != CPP_EOF
)
205 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
207 if (flag_pack_struct
)
208 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
219 align
*= BITS_PER_UNIT
;
224 align
= maximum_field_alignment
;
228 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
233 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
234 case push
: push_alignment (align
, id
); break;
235 case pop
: pop_alignment (id
); break;
239 typedef struct GTY(()) pending_weak_d
246 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
248 static void apply_pragma_weak (tree
, tree
);
249 static void handle_pragma_weak (cpp_reader
*);
252 apply_pragma_weak (tree decl
, tree value
)
256 value
= build_string (IDENTIFIER_LENGTH (value
),
257 IDENTIFIER_POINTER (value
));
258 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
259 build_tree_list (NULL
, value
)),
263 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
264 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
265 && DECL_ASSEMBLER_NAME_SET_P (decl
)
266 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
267 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
268 "results in unspecified behavior", decl
);
274 maybe_apply_pragma_weak (tree decl
)
280 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
282 /* No weak symbols pending, take the short-cut. */
283 if (vec_safe_is_empty (pending_weaks
))
285 /* If it's not visible outside this file, it doesn't matter whether
287 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
289 /* If it's not a function or a variable, it can't be weak.
290 FIXME: what kinds of things are visible outside this file but
291 aren't functions or variables? Should this be an assert instead? */
292 if (!VAR_OR_FUNCTION_DECL_P (decl
))
295 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
296 id
= DECL_ASSEMBLER_NAME (decl
);
299 id
= DECL_ASSEMBLER_NAME (decl
);
300 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
303 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
306 apply_pragma_weak (decl
, pe
->value
);
307 pending_weaks
->unordered_remove (i
);
312 /* Process all "#pragma weak A = B" directives where we have not seen
315 maybe_apply_pending_pragma_weaks (void)
317 tree alias_id
, id
, decl
;
322 if (vec_safe_is_empty (pending_weaks
))
325 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
333 target
= symtab_node::get_for_asmname (id
);
334 decl
= build_decl (UNKNOWN_LOCATION
,
335 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
336 alias_id
, default_function_type
);
338 DECL_ARTIFICIAL (decl
) = 1;
339 TREE_PUBLIC (decl
) = 1;
340 DECL_WEAK (decl
) = 1;
342 TREE_STATIC (decl
) = 1;
345 error ("%q+D aliased to undefined symbol %qE",
350 assemble_alias (decl
, id
);
354 /* #pragma weak name [= value] */
356 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
358 tree name
, value
, x
, decl
;
363 if (pragma_lex (&name
) != CPP_NAME
)
364 GCC_BAD ("malformed #pragma weak, ignored");
368 if (pragma_lex (&value
) != CPP_NAME
)
369 GCC_BAD ("malformed #pragma weak, ignored");
373 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
375 decl
= identifier_global_value (name
);
376 if (decl
&& DECL_P (decl
))
378 if (!VAR_OR_FUNCTION_DECL_P (decl
))
379 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
381 apply_pragma_weak (decl
, value
);
384 DECL_EXTERNAL (decl
) = 0;
386 TREE_STATIC (decl
) = 1;
387 assemble_alias (decl
, value
);
392 pending_weak pe
= {name
, value
};
393 vec_safe_push (pending_weaks
, pe
);
397 /* GCC supports two #pragma directives for renaming the external
398 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
399 compatibility with the Solaris and VMS system headers. GCC also
400 has its own notation for this, __asm__("name") annotations.
402 Corner cases of these features and their interaction:
404 1) Both pragmas silently apply only to declarations with external
405 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
406 do not have this restriction.
408 2) In C++, both #pragmas silently apply only to extern "C" declarations.
409 Asm labels do not have this restriction.
411 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
412 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
413 new name is different, a warning issues and the name does not change.
415 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
416 *not* the DECL_ASSEMBLER_NAME.
418 5) If #pragma extern_prefix is in effect and a declaration occurs
419 with an __asm__ name, the #pragma extern_prefix is silently
420 ignored for that declaration.
422 6) If #pragma extern_prefix and #pragma redefine_extname apply to
423 the same declaration, whichever triggered first wins, and a warning
424 is issued. (We would like to have #pragma redefine_extname always
425 win, but it can appear either before or after the declaration, and
426 if it appears afterward, we have no way of knowing whether a modified
427 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
429 typedef struct GTY(()) pending_redefinition_d
{
432 } pending_redefinition
;
435 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
437 static void handle_pragma_redefine_extname (cpp_reader
*);
439 /* #pragma redefine_extname oldname newname */
441 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
443 tree oldname
, newname
, decls
, x
;
447 if (pragma_lex (&oldname
) != CPP_NAME
)
448 GCC_BAD ("malformed #pragma redefine_extname, ignored");
449 if (pragma_lex (&newname
) != CPP_NAME
)
450 GCC_BAD ("malformed #pragma redefine_extname, ignored");
453 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
456 for (decls
= c_linkage_bindings (oldname
);
460 if (TREE_CODE (decls
) == TREE_LIST
)
462 decl
= TREE_VALUE (decls
);
463 decls
= TREE_CHAIN (decls
);
471 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
472 && VAR_OR_FUNCTION_DECL_P (decl
))
475 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
477 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
478 name
= targetm
.strip_name_encoding (name
);
480 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
481 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
482 "conflict with previous rename");
485 symtab
->change_decl_assembler_name (decl
, newname
);
490 /* We have to add this to the rename list even if there's already
491 a global value that doesn't meet the above criteria, because in
492 C++ "struct foo {...};" puts "foo" in the current namespace but
493 does *not* conflict with a subsequent declaration of a function
494 or variable foo. See g++.dg/other/pragma-re-2.C. */
495 add_to_renaming_pragma_list (oldname
, newname
);
498 /* This is called from here and from ia64-c.c. */
500 add_to_renaming_pragma_list (tree oldname
, tree newname
)
503 pending_redefinition
*p
;
505 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
506 if (oldname
== p
->oldname
)
508 if (p
->newname
!= newname
)
509 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
510 "conflict with previous #pragma redefine_extname");
514 pending_redefinition e
= {oldname
, newname
};
515 vec_safe_push (pending_redefine_extname
, e
);
518 /* The current prefix set by #pragma extern_prefix. */
519 GTY(()) tree pragma_extern_prefix
;
521 /* Hook from the front ends to apply the results of one of the preceding
522 pragmas that rename variables. */
525 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
528 pending_redefinition
*p
;
530 /* The renaming pragmas are only applied to declarations with
532 if (!VAR_OR_FUNCTION_DECL_P (decl
)
533 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
534 || !has_c_linkage (decl
))
537 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
538 but we may warn about a rename that conflicts. */
539 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
541 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
542 oldname
= targetm
.strip_name_encoding (oldname
);
544 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
545 warning (OPT_Wpragmas
, "asm declaration ignored due to "
546 "conflict with previous rename");
548 /* Take any pending redefine_extname off the list. */
549 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
550 if (DECL_NAME (decl
) == p
->oldname
)
552 /* Only warn if there is a conflict. */
553 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
554 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
555 "conflict with previous rename");
557 pending_redefine_extname
->unordered_remove (ix
);
563 /* Find out if we have a pending #pragma redefine_extname. */
564 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
565 if (DECL_NAME (decl
) == p
->oldname
)
567 tree newname
= p
->newname
;
568 pending_redefine_extname
->unordered_remove (ix
);
570 /* If we already have an asmname, #pragma redefine_extname is
571 ignored (with a warning if it conflicts). */
574 if (strcmp (TREE_STRING_POINTER (asmname
),
575 IDENTIFIER_POINTER (newname
)) != 0)
576 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
577 "conflict with __asm__ declaration");
581 /* Otherwise we use what we've got; #pragma extern_prefix is
583 return build_string (IDENTIFIER_LENGTH (newname
),
584 IDENTIFIER_POINTER (newname
));
587 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
591 /* If #pragma extern_prefix is in effect, apply it. */
592 if (pragma_extern_prefix
)
594 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
595 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
597 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
598 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
600 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
602 memcpy (newname
, prefix
, plen
);
603 memcpy (newname
+ plen
, id
, ilen
+ 1);
605 return build_string (plen
+ ilen
, newname
);
613 static void handle_pragma_visibility (cpp_reader
*);
615 static vec
<int> visstack
;
617 /* Push the visibility indicated by STR onto the top of the #pragma
618 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
619 C++ namespace with visibility attribute and 2 for C++ builtin
620 ABI namespace. push_visibility/pop_visibility calls must have
621 matching KIND, it is not allowed to push visibility using one
622 KIND and pop using a different one. */
625 push_visibility (const char *str
, int kind
)
627 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
628 if (!strcmp (str
, "default"))
629 default_visibility
= VISIBILITY_DEFAULT
;
630 else if (!strcmp (str
, "internal"))
631 default_visibility
= VISIBILITY_INTERNAL
;
632 else if (!strcmp (str
, "hidden"))
633 default_visibility
= VISIBILITY_HIDDEN
;
634 else if (!strcmp (str
, "protected"))
635 default_visibility
= VISIBILITY_PROTECTED
;
637 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
638 visibility_options
.inpragma
= 1;
641 /* Pop a level of the #pragma visibility stack. Return true if
645 pop_visibility (int kind
)
647 if (!visstack
.length ())
649 if ((visstack
.last () >> 8) != kind
)
652 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
653 visibility_options
.inpragma
654 = visstack
.length () != 0;
658 /* Sets the default visibility for symbols to something other than that
659 specified on the command line. */
662 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
664 /* Form is #pragma GCC visibility push(hidden)|pop */
666 enum cpp_ttype token
;
667 enum { bad
, push
, pop
} action
= bad
;
669 token
= pragma_lex (&x
);
670 if (token
== CPP_NAME
)
672 const char *op
= IDENTIFIER_POINTER (x
);
673 if (!strcmp (op
, "push"))
675 else if (!strcmp (op
, "pop"))
679 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
684 if (! pop_visibility (0))
685 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
689 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
690 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
691 token
= pragma_lex (&x
);
692 if (token
!= CPP_NAME
)
693 GCC_BAD ("malformed #pragma GCC visibility push");
695 push_visibility (IDENTIFIER_POINTER (x
), 0);
696 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
697 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
700 if (pragma_lex (&x
) != CPP_EOF
)
701 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
705 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
707 const char *kind_string
, *option_string
;
708 unsigned int option_index
;
709 enum cpp_ttype token
;
712 struct cl_option_handlers handlers
;
714 token
= pragma_lex (&x
);
715 if (token
!= CPP_NAME
)
716 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
717 kind_string
= IDENTIFIER_POINTER (x
);
718 if (strcmp (kind_string
, "error") == 0)
720 else if (strcmp (kind_string
, "warning") == 0)
722 else if (strcmp (kind_string
, "ignored") == 0)
724 else if (strcmp (kind_string
, "push") == 0)
726 diagnostic_push_diagnostics (global_dc
, input_location
);
729 else if (strcmp (kind_string
, "pop") == 0)
731 diagnostic_pop_diagnostics (global_dc
, input_location
);
735 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
737 token
= pragma_lex (&x
);
738 if (token
!= CPP_STRING
)
739 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
740 option_string
= TREE_STRING_POINTER (x
);
741 set_default_handlers (&handlers
);
742 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
743 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
745 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
746 input_location
, c_family_lang_mask
, &handlers
,
747 &global_options
, &global_options_set
,
751 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
754 /* Parse #pragma GCC target (xxx) to set target specific options. */
756 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
758 enum cpp_ttype token
;
760 bool close_paren_needed_p
= false;
764 error ("#pragma GCC option is not allowed inside functions");
768 token
= pragma_lex (&x
);
769 if (token
== CPP_OPEN_PAREN
)
771 close_paren_needed_p
= true;
772 token
= pragma_lex (&x
);
775 if (token
!= CPP_STRING
)
777 GCC_BAD ("%<#pragma GCC option%> is not a string");
781 /* Strings are user options. */
784 tree args
= NULL_TREE
;
788 /* Build up the strings now as a tree linked list. Skip empty
790 if (TREE_STRING_LENGTH (x
) > 0)
791 args
= tree_cons (NULL_TREE
, x
, args
);
793 token
= pragma_lex (&x
);
794 while (token
== CPP_COMMA
)
795 token
= pragma_lex (&x
);
797 while (token
== CPP_STRING
);
799 if (close_paren_needed_p
)
801 if (token
== CPP_CLOSE_PAREN
)
802 token
= pragma_lex (&x
);
804 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
805 "not have a final %<)%>");
808 if (token
!= CPP_EOF
)
810 error ("#pragma GCC target string... is badly formed");
814 /* put arguments in the order the user typed them. */
815 args
= nreverse (args
);
817 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
818 current_target_pragma
= args
;
822 /* Handle #pragma GCC optimize to set optimization options. */
824 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
826 enum cpp_ttype token
;
828 bool close_paren_needed_p
= false;
829 tree optimization_previous_node
= optimization_current_node
;
833 error ("#pragma GCC optimize is not allowed inside functions");
837 token
= pragma_lex (&x
);
838 if (token
== CPP_OPEN_PAREN
)
840 close_paren_needed_p
= true;
841 token
= pragma_lex (&x
);
844 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
846 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
850 /* Strings/numbers are user options. */
853 tree args
= NULL_TREE
;
857 /* Build up the numbers/strings now as a list. */
858 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
859 args
= tree_cons (NULL_TREE
, x
, args
);
861 token
= pragma_lex (&x
);
862 while (token
== CPP_COMMA
)
863 token
= pragma_lex (&x
);
865 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
867 if (close_paren_needed_p
)
869 if (token
== CPP_CLOSE_PAREN
)
870 token
= pragma_lex (&x
);
872 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
873 "not have a final %<)%>");
876 if (token
!= CPP_EOF
)
878 error ("#pragma GCC optimize string... is badly formed");
882 /* put arguments in the order the user typed them. */
883 args
= nreverse (args
);
885 parse_optimize_options (args
, false);
886 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
887 optimization_current_node
= build_optimization_node (&global_options
);
888 c_cpp_builtins_optimize_pragma (parse_in
,
889 optimization_previous_node
,
890 optimization_current_node
);
894 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
895 both the binary representation of the options and the TREE_LIST of
896 strings that will be added to the function's attribute list. */
897 typedef struct GTY(()) opt_stack
{
898 struct opt_stack
*prev
;
901 tree optimize_binary
;
902 tree optimize_strings
;
905 static GTY(()) struct opt_stack
* options_stack
;
907 /* Handle #pragma GCC push_options to save the current target and optimization
911 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
913 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 opt_stack
*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 oacc_pragmas
[] = {
1179 { "cache", PRAGMA_OACC_CACHE
},
1180 { "data", PRAGMA_OACC_DATA
},
1181 { "enter", PRAGMA_OACC_ENTER_DATA
},
1182 { "exit", PRAGMA_OACC_EXIT_DATA
},
1183 { "kernels", PRAGMA_OACC_KERNELS
},
1184 { "loop", PRAGMA_OACC_LOOP
},
1185 { "parallel", PRAGMA_OACC_PARALLEL
},
1186 { "update", PRAGMA_OACC_UPDATE
},
1187 { "wait", PRAGMA_OACC_WAIT
}
1189 static const struct omp_pragma_def omp_pragmas
[] = {
1190 { "atomic", PRAGMA_OMP_ATOMIC
},
1191 { "barrier", PRAGMA_OMP_BARRIER
},
1192 { "cancel", PRAGMA_OMP_CANCEL
},
1193 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1194 { "critical", PRAGMA_OMP_CRITICAL
},
1195 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1196 { "flush", PRAGMA_OMP_FLUSH
},
1197 { "master", PRAGMA_OMP_MASTER
},
1198 { "ordered", PRAGMA_OMP_ORDERED
},
1199 { "section", PRAGMA_OMP_SECTION
},
1200 { "sections", PRAGMA_OMP_SECTIONS
},
1201 { "single", PRAGMA_OMP_SINGLE
},
1202 { "task", PRAGMA_OMP_TASK
},
1203 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1204 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1205 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1206 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1208 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1209 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1210 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1211 { "for", PRAGMA_OMP_FOR
},
1212 { "parallel", PRAGMA_OMP_PARALLEL
},
1213 { "simd", PRAGMA_OMP_SIMD
},
1214 { "target", PRAGMA_OMP_TARGET
},
1215 { "teams", PRAGMA_OMP_TEAMS
},
1219 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1221 const int n_oacc_pragmas
= sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1222 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1223 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1224 / sizeof (*omp_pragmas
);
1227 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1228 if (oacc_pragmas
[i
].id
== id
)
1231 *name
= oacc_pragmas
[i
].name
;
1235 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1236 if (omp_pragmas
[i
].id
== id
)
1239 *name
= omp_pragmas
[i
].name
;
1243 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1244 if (omp_pragmas_simd
[i
].id
== id
)
1247 *name
= omp_pragmas_simd
[i
].name
;
1251 if (id
== PRAGMA_CILK_SIMD
)
1258 if (id
>= PRAGMA_FIRST_EXTERNAL
1259 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1261 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1262 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1269 /* Front-end wrappers for pragma registration to avoid dragging
1270 cpplib.h in almost everywhere. */
1273 c_register_pragma_1 (const char *space
, const char *name
,
1274 internal_pragma_handler ihandler
, bool allow_expansion
)
1278 if (flag_preprocess_only
)
1280 pragma_ns_name ns_name
;
1282 if (!allow_expansion
)
1285 ns_name
.space
= space
;
1286 ns_name
.name
= name
;
1287 registered_pp_pragmas
.safe_push (ns_name
);
1288 id
= registered_pp_pragmas
.length ();
1289 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1293 registered_pragmas
.safe_push (ihandler
);
1294 id
= registered_pragmas
.length ();
1295 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1297 /* The C++ front end allocates 6 bits in cp_token; the C front end
1298 allocates 7 bits in c_token. At present this is sufficient. */
1299 gcc_assert (id
< 64);
1302 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1303 allow_expansion
, false);
1306 /* Register a C pragma handler, using a space and a name. It disallows pragma
1307 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1309 c_register_pragma (const char *space
, const char *name
,
1310 pragma_handler_1arg handler
)
1312 internal_pragma_handler ihandler
;
1314 ihandler
.handler
.handler_1arg
= handler
;
1315 ihandler
.extra_data
= false;
1316 ihandler
.data
= NULL
;
1317 c_register_pragma_1 (space
, name
, ihandler
, false);
1320 /* Register a C pragma handler, using a space and a name, it also carries an
1321 extra data field which can be used by the handler. It disallows pragma
1322 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1325 c_register_pragma_with_data (const char *space
, const char *name
,
1326 pragma_handler_2arg handler
, void * data
)
1328 internal_pragma_handler ihandler
;
1330 ihandler
.handler
.handler_2arg
= handler
;
1331 ihandler
.extra_data
= true;
1332 ihandler
.data
= data
;
1333 c_register_pragma_1 (space
, name
, ihandler
, false);
1336 /* Register a C pragma handler, using a space and a name. 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 (const char *space
, const char *name
,
1345 pragma_handler_1arg handler
)
1347 internal_pragma_handler ihandler
;
1349 ihandler
.handler
.handler_1arg
= handler
;
1350 ihandler
.extra_data
= false;
1351 ihandler
.data
= NULL
;
1352 c_register_pragma_1 (space
, name
, ihandler
, true);
1355 /* Register a C pragma handler, using a space and a name, it also carries an
1356 extra data field which can be used by the handler. It allows pragma
1357 expansion as in the following example:
1360 #pragma count (NUMBER)
1362 Name expansion is still disallowed. */
1364 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1365 pragma_handler_2arg handler
,
1368 internal_pragma_handler ihandler
;
1370 ihandler
.handler
.handler_2arg
= handler
;
1371 ihandler
.extra_data
= true;
1372 ihandler
.data
= data
;
1373 c_register_pragma_1 (space
, name
, ihandler
, true);
1377 c_invoke_pragma_handler (unsigned int id
)
1379 internal_pragma_handler
*ihandler
;
1380 pragma_handler_1arg handler_1arg
;
1381 pragma_handler_2arg handler_2arg
;
1383 id
-= PRAGMA_FIRST_EXTERNAL
;
1384 ihandler
= ®istered_pragmas
[id
];
1385 if (ihandler
->extra_data
)
1387 handler_2arg
= ihandler
->handler
.handler_2arg
;
1388 handler_2arg (parse_in
, ihandler
->data
);
1392 handler_1arg
= ihandler
->handler
.handler_1arg
;
1393 handler_1arg (parse_in
);
1397 /* Set up front-end pragmas. */
1403 const int n_oacc_pragmas
1404 = sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1407 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1408 cpp_register_deferred_pragma (parse_in
, "acc", oacc_pragmas
[i
].name
,
1409 oacc_pragmas
[i
].id
, true, true);
1414 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1417 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1418 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1419 omp_pragmas
[i
].id
, true, true);
1421 if (flag_openmp
|| flag_openmp_simd
)
1423 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1424 / sizeof (*omp_pragmas
);
1427 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1428 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1429 omp_pragmas_simd
[i
].id
, true, true);
1433 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1436 if (!flag_preprocess_only
)
1437 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1438 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1440 if (!flag_preprocess_only
)
1441 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1444 if (flag_cilkplus
&& !flag_preprocess_only
)
1445 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1446 PRAGMA_CILK_GRAINSIZE
, true, false);
1448 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1449 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1451 c_register_pragma (0, "pack", handle_pragma_pack
);
1453 c_register_pragma (0, "weak", handle_pragma_weak
);
1454 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1456 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1457 c_register_pragma ("GCC", "target", handle_pragma_target
);
1458 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1459 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1460 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1461 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1463 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1464 handle_pragma_float_const_decimal64
);
1466 c_register_pragma_with_expansion (0, "redefine_extname",
1467 handle_pragma_redefine_extname
);
1469 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1471 #ifdef REGISTER_TARGET_PRAGMAS
1472 REGISTER_TARGET_PRAGMAS ();
1475 /* Allow plugins to register their own pragmas. */
1476 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1479 #include "gt-c-family-c-pragma.h"