1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2017 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"
24 #include "function.h" /* For cfun. */
27 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS. */
28 #include "stringpool.h"
30 #include "diagnostic.h"
37 #define GCC_BAD(gmsgid) \
38 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
39 #define GCC_BAD2(gmsgid, arg) \
40 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
42 struct GTY(()) align_stack
{
45 struct align_stack
* prev
;
48 static GTY(()) struct align_stack
* alignment_stack
;
50 static void handle_pragma_pack (cpp_reader
*);
52 /* If we have a "global" #pragma pack(<n>) in effect when the first
53 #pragma pack(push,<n>) is encountered, this stores the value of
54 maximum_field_alignment in effect. When the final pop_alignment()
55 happens, we restore the value to this, not to a value of 0 for
56 maximum_field_alignment. Value is in bits. */
57 static int default_alignment
;
58 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
59 ? &default_alignment \
60 : &alignment_stack->alignment) = (ALIGN))
62 static void push_alignment (int, tree
);
63 static void pop_alignment (tree
);
65 /* Push an alignment value onto the stack. */
67 push_alignment (int alignment
, tree id
)
69 align_stack
* entry
= ggc_alloc
<align_stack
> ();
71 entry
->alignment
= alignment
;
73 entry
->prev
= alignment_stack
;
75 /* The current value of maximum_field_alignment is not necessarily
76 0 since there may be a #pragma pack(<n>) in effect; remember it
77 so that we can restore it after the final #pragma pop(). */
78 if (alignment_stack
== NULL
)
79 default_alignment
= maximum_field_alignment
;
81 alignment_stack
= entry
;
83 maximum_field_alignment
= alignment
;
86 /* Undo a push of an alignment onto the stack. */
88 pop_alignment (tree id
)
92 if (alignment_stack
== NULL
)
93 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
95 /* If we got an identifier, strip away everything above the target
96 entry so that the next step will restore the state just below it. */
99 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
102 alignment_stack
= entry
;
106 warning (OPT_Wpragmas
, "\
107 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
111 entry
= alignment_stack
->prev
;
113 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
115 alignment_stack
= entry
;
122 #pragma pack (push, N)
123 #pragma pack (push, ID)
124 #pragma pack (push, ID, N)
126 #pragma pack (pop, ID) */
128 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
132 enum cpp_ttype token
;
133 enum { set
, push
, pop
} action
;
135 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
136 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
138 token
= pragma_lex (&x
);
139 if (token
== CPP_CLOSE_PAREN
)
142 align
= initial_max_fld_align
;
144 else if (token
== CPP_NUMBER
)
146 if (TREE_CODE (x
) != INTEGER_CST
)
147 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
148 align
= TREE_INT_CST_LOW (x
);
150 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
151 GCC_BAD ("malformed %<#pragma pack%> - ignored");
153 else if (token
== CPP_NAME
)
155 #define GCC_BAD_ACTION do { if (action != pop) \
156 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
158 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
161 const char *op
= IDENTIFIER_POINTER (x
);
162 if (!strcmp (op
, "push"))
164 else if (!strcmp (op
, "pop"))
167 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
169 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
171 token
= pragma_lex (&x
);
172 if (token
== CPP_NAME
&& id
== 0)
176 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
178 if (TREE_CODE (x
) != INTEGER_CST
)
179 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
180 align
= TREE_INT_CST_LOW (x
);
188 if (token
!= CPP_CLOSE_PAREN
)
190 #undef GCC_BAD_ACTION
193 GCC_BAD ("malformed %<#pragma pack%> - ignored");
195 if (pragma_lex (&x
) != CPP_EOF
)
196 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
198 if (flag_pack_struct
)
199 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
210 align
*= BITS_PER_UNIT
;
215 align
= maximum_field_alignment
;
220 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
225 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
226 case push
: push_alignment (align
, id
); break;
227 case pop
: pop_alignment (id
); break;
231 struct GTY(()) pending_weak
238 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
240 static void apply_pragma_weak (tree
, tree
);
241 static void handle_pragma_weak (cpp_reader
*);
244 apply_pragma_weak (tree decl
, tree value
)
248 value
= build_string (IDENTIFIER_LENGTH (value
),
249 IDENTIFIER_POINTER (value
));
250 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
251 build_tree_list (NULL
, value
)),
255 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
256 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
257 && DECL_ASSEMBLER_NAME_SET_P (decl
)
258 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
259 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
260 "results in unspecified behavior", decl
);
266 maybe_apply_pragma_weak (tree decl
)
272 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
274 /* No weak symbols pending, take the short-cut. */
275 if (vec_safe_is_empty (pending_weaks
))
277 /* If it's not visible outside this file, it doesn't matter whether
279 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
281 /* If it's not a function or a variable, it can't be weak.
282 FIXME: what kinds of things are visible outside this file but
283 aren't functions or variables? Should this be an assert instead? */
284 if (!VAR_OR_FUNCTION_DECL_P (decl
))
287 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
288 id
= DECL_ASSEMBLER_NAME (decl
);
291 id
= DECL_ASSEMBLER_NAME (decl
);
292 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
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
;
314 if (vec_safe_is_empty (pending_weaks
))
317 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
325 target
= symtab_node::get_for_asmname (id
);
326 decl
= build_decl (UNKNOWN_LOCATION
,
327 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
328 alias_id
, default_function_type
);
330 DECL_ARTIFICIAL (decl
) = 1;
331 TREE_PUBLIC (decl
) = 1;
332 DECL_WEAK (decl
) = 1;
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 if (!VAR_OR_FUNCTION_DECL_P (decl
))
371 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
373 apply_pragma_weak (decl
, value
);
376 DECL_EXTERNAL (decl
) = 0;
378 TREE_STATIC (decl
) = 1;
379 assemble_alias (decl
, value
);
384 pending_weak pe
= {name
, value
};
385 vec_safe_push (pending_weaks
, pe
);
389 static enum scalar_storage_order_kind global_sso
;
392 maybe_apply_pragma_scalar_storage_order (tree type
)
394 if (global_sso
== SSO_NATIVE
)
397 gcc_assert (RECORD_OR_UNION_TYPE_P (type
));
399 if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type
)))
402 if (global_sso
== SSO_BIG_ENDIAN
)
403 TYPE_REVERSE_STORAGE_ORDER (type
) = !BYTES_BIG_ENDIAN
;
404 else if (global_sso
== SSO_LITTLE_ENDIAN
)
405 TYPE_REVERSE_STORAGE_ORDER (type
) = BYTES_BIG_ENDIAN
;
411 handle_pragma_scalar_storage_order (cpp_reader
*ARG_UNUSED(dummy
))
413 const char *kind_string
;
414 enum cpp_ttype token
;
417 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
418 error ("scalar_storage_order is not supported");
420 token
= pragma_lex (&x
);
421 if (token
!= CPP_NAME
)
422 GCC_BAD ("missing [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
423 kind_string
= IDENTIFIER_POINTER (x
);
424 if (strcmp (kind_string
, "default") == 0)
425 global_sso
= default_sso
;
426 else if (strcmp (kind_string
, "big") == 0)
427 global_sso
= SSO_BIG_ENDIAN
;
428 else if (strcmp (kind_string
, "little") == 0)
429 global_sso
= SSO_LITTLE_ENDIAN
;
431 GCC_BAD ("expected [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
434 /* GCC supports two #pragma directives for renaming the external
435 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
436 compatibility with the Solaris and VMS system headers. GCC also
437 has its own notation for this, __asm__("name") annotations.
439 Corner cases of these features and their interaction:
441 1) Both pragmas silently apply only to declarations with external
442 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
443 do not have this restriction.
445 2) In C++, both #pragmas silently apply only to extern "C" declarations.
446 Asm labels do not have this restriction.
448 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
449 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
450 new name is different, a warning issues and the name does not change.
452 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
453 *not* the DECL_ASSEMBLER_NAME.
455 5) If #pragma extern_prefix is in effect and a declaration occurs
456 with an __asm__ name, the #pragma extern_prefix is silently
457 ignored for that declaration.
459 6) If #pragma extern_prefix and #pragma redefine_extname apply to
460 the same declaration, whichever triggered first wins, and a warning
461 is issued. (We would like to have #pragma redefine_extname always
462 win, but it can appear either before or after the declaration, and
463 if it appears afterward, we have no way of knowing whether a modified
464 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
466 struct GTY(()) pending_redefinition
{
472 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
474 static void handle_pragma_redefine_extname (cpp_reader
*);
476 /* #pragma redefine_extname oldname newname */
478 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
480 tree oldname
, newname
, decls
, x
;
484 if (pragma_lex (&oldname
) != CPP_NAME
)
485 GCC_BAD ("malformed #pragma redefine_extname, ignored");
486 if (pragma_lex (&newname
) != CPP_NAME
)
487 GCC_BAD ("malformed #pragma redefine_extname, ignored");
490 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
493 for (decls
= c_linkage_bindings (oldname
);
497 if (TREE_CODE (decls
) == TREE_LIST
)
499 decl
= TREE_VALUE (decls
);
500 decls
= TREE_CHAIN (decls
);
508 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
509 && VAR_OR_FUNCTION_DECL_P (decl
))
512 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
514 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
515 name
= targetm
.strip_name_encoding (name
);
517 if (!id_equal (newname
, name
))
518 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
519 "conflict with previous rename");
522 symtab
->change_decl_assembler_name (decl
, newname
);
527 /* We have to add this to the rename list even if there's already
528 a global value that doesn't meet the above criteria, because in
529 C++ "struct foo {...};" puts "foo" in the current namespace but
530 does *not* conflict with a subsequent declaration of a function
531 or variable foo. See g++.dg/other/pragma-re-2.C. */
532 add_to_renaming_pragma_list (oldname
, newname
);
535 /* This is called from here and from ia64-c.c. */
537 add_to_renaming_pragma_list (tree oldname
, tree newname
)
540 pending_redefinition
*p
;
542 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
543 if (oldname
== p
->oldname
)
545 if (p
->newname
!= newname
)
546 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
547 "conflict with previous #pragma redefine_extname");
551 pending_redefinition e
= {oldname
, newname
};
552 vec_safe_push (pending_redefine_extname
, e
);
555 /* The current prefix set by #pragma extern_prefix. */
556 GTY(()) tree pragma_extern_prefix
;
558 /* Hook from the front ends to apply the results of one of the preceding
559 pragmas that rename variables. */
562 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
565 pending_redefinition
*p
;
567 /* The renaming pragmas are only applied to declarations with
569 if (!VAR_OR_FUNCTION_DECL_P (decl
)
570 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
571 || !has_c_linkage (decl
))
574 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
575 but we may warn about a rename that conflicts. */
576 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
578 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
579 oldname
= targetm
.strip_name_encoding (oldname
);
581 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
582 warning (OPT_Wpragmas
, "asm declaration ignored due to "
583 "conflict with previous rename");
585 /* Take any pending redefine_extname off the list. */
586 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
587 if (DECL_NAME (decl
) == p
->oldname
)
589 /* Only warn if there is a conflict. */
590 if (!id_equal (p
->newname
, oldname
))
591 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
592 "conflict with previous rename");
594 pending_redefine_extname
->unordered_remove (ix
);
600 /* Find out if we have a pending #pragma redefine_extname. */
601 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
602 if (DECL_NAME (decl
) == p
->oldname
)
604 tree newname
= p
->newname
;
605 pending_redefine_extname
->unordered_remove (ix
);
607 /* If we already have an asmname, #pragma redefine_extname is
608 ignored (with a warning if it conflicts). */
611 if (strcmp (TREE_STRING_POINTER (asmname
),
612 IDENTIFIER_POINTER (newname
)) != 0)
613 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
614 "conflict with __asm__ declaration");
618 /* Otherwise we use what we've got; #pragma extern_prefix is
620 return build_string (IDENTIFIER_LENGTH (newname
),
621 IDENTIFIER_POINTER (newname
));
624 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
628 /* If #pragma extern_prefix is in effect, apply it. */
629 if (pragma_extern_prefix
)
631 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
632 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
634 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
635 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
637 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
639 memcpy (newname
, prefix
, plen
);
640 memcpy (newname
+ plen
, id
, ilen
+ 1);
642 return build_string (plen
+ ilen
, newname
);
650 static void handle_pragma_visibility (cpp_reader
*);
652 static vec
<int> visstack
;
654 /* Push the visibility indicated by STR onto the top of the #pragma
655 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
656 C++ namespace with visibility attribute and 2 for C++ builtin
657 ABI namespace. push_visibility/pop_visibility calls must have
658 matching KIND, it is not allowed to push visibility using one
659 KIND and pop using a different one. */
662 push_visibility (const char *str
, int kind
)
664 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
665 if (!strcmp (str
, "default"))
666 default_visibility
= VISIBILITY_DEFAULT
;
667 else if (!strcmp (str
, "internal"))
668 default_visibility
= VISIBILITY_INTERNAL
;
669 else if (!strcmp (str
, "hidden"))
670 default_visibility
= VISIBILITY_HIDDEN
;
671 else if (!strcmp (str
, "protected"))
672 default_visibility
= VISIBILITY_PROTECTED
;
674 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
675 visibility_options
.inpragma
= 1;
678 /* Pop a level of the #pragma visibility stack. Return true if
682 pop_visibility (int kind
)
684 if (!visstack
.length ())
686 if ((visstack
.last () >> 8) != kind
)
689 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
690 visibility_options
.inpragma
691 = visstack
.length () != 0;
695 /* Sets the default visibility for symbols to something other than that
696 specified on the command line. */
699 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
701 /* Form is #pragma GCC visibility push(hidden)|pop */
703 enum cpp_ttype token
;
704 enum { bad
, push
, pop
} action
= bad
;
706 token
= pragma_lex (&x
);
707 if (token
== CPP_NAME
)
709 const char *op
= IDENTIFIER_POINTER (x
);
710 if (!strcmp (op
, "push"))
712 else if (!strcmp (op
, "pop"))
716 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
721 if (! pop_visibility (0))
722 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
726 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
727 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
728 token
= pragma_lex (&x
);
729 if (token
!= CPP_NAME
)
730 GCC_BAD ("malformed #pragma GCC visibility push");
732 push_visibility (IDENTIFIER_POINTER (x
), 0);
733 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
734 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
737 if (pragma_lex (&x
) != CPP_EOF
)
738 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
742 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
746 enum cpp_ttype token
= pragma_lex (&x
, &loc
);
747 if (token
!= CPP_NAME
)
749 warning_at (loc
, OPT_Wpragmas
,
750 "missing [error|warning|ignored|push|pop]"
751 " after %<#pragma GCC diagnostic%>");
756 const char *kind_string
= IDENTIFIER_POINTER (x
);
757 if (strcmp (kind_string
, "error") == 0)
759 else if (strcmp (kind_string
, "warning") == 0)
761 else if (strcmp (kind_string
, "ignored") == 0)
763 else if (strcmp (kind_string
, "push") == 0)
765 diagnostic_push_diagnostics (global_dc
, input_location
);
768 else if (strcmp (kind_string
, "pop") == 0)
770 diagnostic_pop_diagnostics (global_dc
, input_location
);
775 warning_at (loc
, OPT_Wpragmas
,
776 "expected [error|warning|ignored|push|pop]"
777 " after %<#pragma GCC diagnostic%>");
781 token
= pragma_lex (&x
, &loc
);
782 if (token
!= CPP_STRING
)
784 warning_at (loc
, OPT_Wpragmas
,
785 "missing option after %<#pragma GCC diagnostic%> kind");
789 const char *option_string
= TREE_STRING_POINTER (x
);
790 unsigned int lang_mask
= c_common_option_lang_mask () | CL_COMMON
;
791 /* option_string + 1 to skip the initial '-' */
792 unsigned int option_index
= find_opt (option_string
+ 1, lang_mask
);
793 if (option_index
== OPT_SPECIAL_unknown
)
795 warning_at (loc
, OPT_Wpragmas
,
796 "unknown option after %<#pragma GCC diagnostic%> kind");
799 else if (!(cl_options
[option_index
].flags
& CL_WARNING
))
801 warning_at (loc
, OPT_Wpragmas
,
802 "%qs is not an option that controls warnings", option_string
);
805 else if (!(cl_options
[option_index
].flags
& lang_mask
))
807 char *ok_langs
= write_langs (cl_options
[option_index
].flags
);
808 char *bad_lang
= write_langs (c_common_option_lang_mask ());
809 warning_at (loc
, OPT_Wpragmas
,
810 "option %qs is valid for %s but not for %s",
811 option_string
, ok_langs
, bad_lang
);
817 struct cl_option_handlers handlers
;
818 set_default_handlers (&handlers
, NULL
);
819 const char *arg
= NULL
;
820 if (cl_options
[option_index
].flags
& CL_JOINED
)
821 arg
= option_string
+ 1 + cl_options
[option_index
].opt_len
;
822 /* FIXME: input_location isn't the best location here, but it is
823 what we used to do here before and changing it breaks e.g.
824 PR69543 and PR69558. */
825 control_warning_option (option_index
, (int) kind
,
826 arg
, kind
!= DK_IGNORED
,
827 input_location
, lang_mask
, &handlers
,
828 &global_options
, &global_options_set
,
832 /* Parse #pragma GCC target (xxx) to set target specific options. */
834 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
836 enum cpp_ttype token
;
838 bool close_paren_needed_p
= false;
842 error ("#pragma GCC option is not allowed inside functions");
846 token
= pragma_lex (&x
);
847 if (token
== CPP_OPEN_PAREN
)
849 close_paren_needed_p
= true;
850 token
= pragma_lex (&x
);
853 if (token
!= CPP_STRING
)
855 GCC_BAD ("%<#pragma GCC option%> is not a string");
859 /* Strings are user options. */
862 tree args
= NULL_TREE
;
866 /* Build up the strings now as a tree linked list. Skip empty
868 if (TREE_STRING_LENGTH (x
) > 0)
869 args
= tree_cons (NULL_TREE
, x
, args
);
871 token
= pragma_lex (&x
);
872 while (token
== CPP_COMMA
)
873 token
= pragma_lex (&x
);
875 while (token
== CPP_STRING
);
877 if (close_paren_needed_p
)
879 if (token
== CPP_CLOSE_PAREN
)
880 token
= pragma_lex (&x
);
882 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
883 "not have a final %<)%>");
886 if (token
!= CPP_EOF
)
888 error ("#pragma GCC target string... is badly formed");
892 /* put arguments in the order the user typed them. */
893 args
= nreverse (args
);
895 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
896 current_target_pragma
= chainon (current_target_pragma
, args
);
900 /* Handle #pragma GCC optimize to set optimization options. */
902 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
904 enum cpp_ttype token
;
906 bool close_paren_needed_p
= false;
907 tree optimization_previous_node
= optimization_current_node
;
911 error ("#pragma GCC optimize is not allowed inside functions");
915 token
= pragma_lex (&x
);
916 if (token
== CPP_OPEN_PAREN
)
918 close_paren_needed_p
= true;
919 token
= pragma_lex (&x
);
922 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
924 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
928 /* Strings/numbers are user options. */
931 tree args
= NULL_TREE
;
935 /* Build up the numbers/strings now as a list. */
936 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
937 args
= tree_cons (NULL_TREE
, x
, args
);
939 token
= pragma_lex (&x
);
940 while (token
== CPP_COMMA
)
941 token
= pragma_lex (&x
);
943 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
945 if (close_paren_needed_p
)
947 if (token
== CPP_CLOSE_PAREN
)
948 token
= pragma_lex (&x
);
950 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
951 "not have a final %<)%>");
954 if (token
!= CPP_EOF
)
956 error ("#pragma GCC optimize string... is badly formed");
960 /* put arguments in the order the user typed them. */
961 args
= nreverse (args
);
963 parse_optimize_options (args
, false);
964 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
965 optimization_current_node
= build_optimization_node (&global_options
);
966 c_cpp_builtins_optimize_pragma (parse_in
,
967 optimization_previous_node
,
968 optimization_current_node
);
972 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
973 both the binary representation of the options and the TREE_LIST of
974 strings that will be added to the function's attribute list. */
975 struct GTY(()) opt_stack
{
976 struct opt_stack
*prev
;
979 tree optimize_binary
;
980 tree optimize_strings
;
983 static GTY(()) struct opt_stack
* options_stack
;
985 /* Handle #pragma GCC push_options to save the current target and optimization
989 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
991 enum cpp_ttype token
;
994 token
= pragma_lex (&x
);
995 if (token
!= CPP_EOF
)
997 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
1001 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
1002 p
->prev
= options_stack
;
1005 /* Save optimization and target flags in binary format. */
1006 p
->optimize_binary
= build_optimization_node (&global_options
);
1007 p
->target_binary
= build_target_option_node (&global_options
);
1009 /* Save optimization and target flags in string list format. */
1010 p
->optimize_strings
= copy_list (current_optimize_pragma
);
1011 p
->target_strings
= copy_list (current_target_pragma
);
1014 /* Handle #pragma GCC pop_options to restore the current target and
1015 optimization options from a previous push_options. */
1018 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
1020 enum cpp_ttype token
;
1024 token
= pragma_lex (&x
);
1025 if (token
!= CPP_EOF
)
1027 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
1031 if (! options_stack
)
1033 warning (OPT_Wpragmas
,
1034 "%<#pragma GCC pop_options%> without a corresponding "
1035 "%<#pragma GCC push_options%>");
1040 options_stack
= p
->prev
;
1042 if (p
->target_binary
!= target_option_current_node
)
1044 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
1045 target_option_current_node
= p
->target_binary
;
1048 if (p
->optimize_binary
!= optimization_current_node
)
1050 tree old_optimize
= optimization_current_node
;
1051 cl_optimization_restore (&global_options
,
1052 TREE_OPTIMIZATION (p
->optimize_binary
));
1053 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
1054 p
->optimize_binary
);
1055 optimization_current_node
= p
->optimize_binary
;
1058 current_target_pragma
= p
->target_strings
;
1059 current_optimize_pragma
= p
->optimize_strings
;
1062 /* Handle #pragma GCC reset_options to restore the current target and
1063 optimization options to the original options used on the command line. */
1066 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
1068 enum cpp_ttype token
;
1070 tree new_optimize
= optimization_default_node
;
1071 tree new_target
= target_option_default_node
;
1073 token
= pragma_lex (&x
);
1074 if (token
!= CPP_EOF
)
1076 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1080 if (new_target
!= target_option_current_node
)
1082 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1083 target_option_current_node
= new_target
;
1086 if (new_optimize
!= optimization_current_node
)
1088 tree old_optimize
= optimization_current_node
;
1089 cl_optimization_restore (&global_options
,
1090 TREE_OPTIMIZATION (new_optimize
));
1091 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1092 optimization_current_node
= new_optimize
;
1095 current_target_pragma
= NULL_TREE
;
1096 current_optimize_pragma
= NULL_TREE
;
1099 /* Print a plain user-specified message. */
1102 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1104 enum cpp_ttype token
;
1105 tree x
, message
= 0;
1107 token
= pragma_lex (&x
);
1108 if (token
== CPP_OPEN_PAREN
)
1110 token
= pragma_lex (&x
);
1111 if (token
== CPP_STRING
)
1114 GCC_BAD ("expected a string after %<#pragma message%>");
1115 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1116 GCC_BAD ("malformed %<#pragma message%>, ignored");
1118 else if (token
== CPP_STRING
)
1121 GCC_BAD ("expected a string after %<#pragma message%>");
1123 gcc_assert (message
);
1125 if (pragma_lex (&x
) != CPP_EOF
)
1126 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1128 if (TREE_STRING_LENGTH (message
) > 1)
1129 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1132 /* Mark whether the current location is valid for a STDC pragma. */
1134 static bool valid_location_for_stdc_pragma
;
1137 mark_valid_location_for_stdc_pragma (bool flag
)
1139 valid_location_for_stdc_pragma
= flag
;
1142 /* Return true if the current location is valid for a STDC pragma. */
1145 valid_location_for_stdc_pragma_p (void)
1147 return valid_location_for_stdc_pragma
;
1150 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1152 /* A STDC pragma must appear outside of external declarations or
1153 preceding all explicit declarations and statements inside a compound
1154 statement; its behavior is undefined if used in any other context.
1155 It takes a switch of ON, OFF, or DEFAULT. */
1157 static enum pragma_switch_t
1158 handle_stdc_pragma (const char *pname
)
1162 enum pragma_switch_t ret
;
1164 if (!valid_location_for_stdc_pragma_p ())
1166 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1171 if (pragma_lex (&t
) != CPP_NAME
)
1173 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1177 arg
= IDENTIFIER_POINTER (t
);
1179 if (!strcmp (arg
, "ON"))
1181 else if (!strcmp (arg
, "OFF"))
1183 else if (!strcmp (arg
, "DEFAULT"))
1184 ret
= PRAGMA_DEFAULT
;
1187 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1191 if (pragma_lex (&t
) != CPP_EOF
)
1193 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1200 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1201 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1202 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1205 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1207 if (c_dialect_cxx ())
1209 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1210 warning (OPT_Wunknown_pragmas
,
1211 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1216 if (!targetm
.decimal_float_supported_p ())
1218 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1219 warning (OPT_Wunknown_pragmas
,
1220 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1225 pedwarn (input_location
, OPT_Wpedantic
,
1226 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1228 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1231 set_float_const_decimal64 ();
1234 case PRAGMA_DEFAULT
:
1235 clear_float_const_decimal64 ();
1242 /* A vector of registered pragma callbacks, which is never freed. */
1244 static vec
<internal_pragma_handler
> registered_pragmas
;
1246 struct pragma_ns_name
1253 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1255 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1256 static const struct omp_pragma_def oacc_pragmas
[] = {
1257 { "atomic", PRAGMA_OACC_ATOMIC
},
1258 { "cache", PRAGMA_OACC_CACHE
},
1259 { "data", PRAGMA_OACC_DATA
},
1260 { "declare", PRAGMA_OACC_DECLARE
},
1261 { "enter", PRAGMA_OACC_ENTER_DATA
},
1262 { "exit", PRAGMA_OACC_EXIT_DATA
},
1263 { "host_data", PRAGMA_OACC_HOST_DATA
},
1264 { "kernels", PRAGMA_OACC_KERNELS
},
1265 { "loop", PRAGMA_OACC_LOOP
},
1266 { "parallel", PRAGMA_OACC_PARALLEL
},
1267 { "routine", PRAGMA_OACC_ROUTINE
},
1268 { "update", PRAGMA_OACC_UPDATE
},
1269 { "wait", PRAGMA_OACC_WAIT
}
1271 static const struct omp_pragma_def omp_pragmas
[] = {
1272 { "atomic", PRAGMA_OMP_ATOMIC
},
1273 { "barrier", PRAGMA_OMP_BARRIER
},
1274 { "cancel", PRAGMA_OMP_CANCEL
},
1275 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1276 { "critical", PRAGMA_OMP_CRITICAL
},
1277 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1278 { "flush", PRAGMA_OMP_FLUSH
},
1279 { "master", PRAGMA_OMP_MASTER
},
1280 { "ordered", PRAGMA_OMP_ORDERED
},
1281 { "section", PRAGMA_OMP_SECTION
},
1282 { "sections", PRAGMA_OMP_SECTIONS
},
1283 { "single", PRAGMA_OMP_SINGLE
},
1284 { "task", PRAGMA_OMP_TASK
},
1285 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1286 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1287 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1288 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1290 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1291 { "declare", PRAGMA_OMP_DECLARE
},
1292 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1293 { "for", PRAGMA_OMP_FOR
},
1294 { "parallel", PRAGMA_OMP_PARALLEL
},
1295 { "simd", PRAGMA_OMP_SIMD
},
1296 { "target", PRAGMA_OMP_TARGET
},
1297 { "taskloop", PRAGMA_OMP_TASKLOOP
},
1298 { "teams", PRAGMA_OMP_TEAMS
},
1302 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1304 const int n_oacc_pragmas
= sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1305 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1306 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1307 / sizeof (*omp_pragmas
);
1310 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1311 if (oacc_pragmas
[i
].id
== id
)
1314 *name
= oacc_pragmas
[i
].name
;
1318 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1319 if (omp_pragmas
[i
].id
== id
)
1322 *name
= omp_pragmas
[i
].name
;
1326 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1327 if (omp_pragmas_simd
[i
].id
== id
)
1330 *name
= omp_pragmas_simd
[i
].name
;
1334 if (id
== PRAGMA_CILK_SIMD
)
1341 if (id
== PRAGMA_CILK_GRAINSIZE
)
1344 *name
= "grainsize";
1348 if (id
>= PRAGMA_FIRST_EXTERNAL
1349 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1351 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1352 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1359 /* Front-end wrappers for pragma registration to avoid dragging
1360 cpplib.h in almost everywhere. */
1363 c_register_pragma_1 (const char *space
, const char *name
,
1364 internal_pragma_handler ihandler
, bool allow_expansion
)
1368 if (flag_preprocess_only
)
1370 pragma_ns_name ns_name
;
1372 if (!allow_expansion
)
1375 ns_name
.space
= space
;
1376 ns_name
.name
= name
;
1377 registered_pp_pragmas
.safe_push (ns_name
);
1378 id
= registered_pp_pragmas
.length ();
1379 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1383 registered_pragmas
.safe_push (ihandler
);
1384 id
= registered_pragmas
.length ();
1385 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1387 /* The C front end allocates 8 bits in c_token. The C++ front end
1388 keeps the pragma kind in the form of INTEGER_CST, so no small
1389 limit applies. At present this is sufficient. */
1390 gcc_assert (id
< 256);
1393 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1394 allow_expansion
, false);
1397 /* Register a C pragma handler, using a space and a name. It disallows pragma
1398 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1400 c_register_pragma (const char *space
, const char *name
,
1401 pragma_handler_1arg handler
)
1403 internal_pragma_handler ihandler
;
1405 ihandler
.handler
.handler_1arg
= handler
;
1406 ihandler
.extra_data
= false;
1407 ihandler
.data
= NULL
;
1408 c_register_pragma_1 (space
, name
, ihandler
, false);
1411 /* Register a C pragma handler, using a space and a name, it also carries an
1412 extra data field which can be used by the handler. It disallows pragma
1413 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1416 c_register_pragma_with_data (const char *space
, const char *name
,
1417 pragma_handler_2arg handler
, void * data
)
1419 internal_pragma_handler ihandler
;
1421 ihandler
.handler
.handler_2arg
= handler
;
1422 ihandler
.extra_data
= true;
1423 ihandler
.data
= data
;
1424 c_register_pragma_1 (space
, name
, ihandler
, false);
1427 /* Register a C pragma handler, using a space and a name. It allows pragma
1428 expansion as in the following example:
1431 #pragma count (NUMBER)
1433 Name expansion is still disallowed. */
1435 c_register_pragma_with_expansion (const char *space
, const char *name
,
1436 pragma_handler_1arg handler
)
1438 internal_pragma_handler ihandler
;
1440 ihandler
.handler
.handler_1arg
= handler
;
1441 ihandler
.extra_data
= false;
1442 ihandler
.data
= NULL
;
1443 c_register_pragma_1 (space
, name
, ihandler
, true);
1446 /* Register a C pragma handler, using a space and a name, it also carries an
1447 extra data field which can be used by the handler. It allows pragma
1448 expansion as in the following example:
1451 #pragma count (NUMBER)
1453 Name expansion is still disallowed. */
1455 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1456 pragma_handler_2arg handler
,
1459 internal_pragma_handler ihandler
;
1461 ihandler
.handler
.handler_2arg
= handler
;
1462 ihandler
.extra_data
= true;
1463 ihandler
.data
= data
;
1464 c_register_pragma_1 (space
, name
, ihandler
, true);
1468 c_invoke_pragma_handler (unsigned int id
)
1470 internal_pragma_handler
*ihandler
;
1471 pragma_handler_1arg handler_1arg
;
1472 pragma_handler_2arg handler_2arg
;
1474 id
-= PRAGMA_FIRST_EXTERNAL
;
1475 ihandler
= ®istered_pragmas
[id
];
1476 if (ihandler
->extra_data
)
1478 handler_2arg
= ihandler
->handler
.handler_2arg
;
1479 handler_2arg (parse_in
, ihandler
->data
);
1483 handler_1arg
= ihandler
->handler
.handler_1arg
;
1484 handler_1arg (parse_in
);
1488 /* Set up front-end pragmas. */
1494 const int n_oacc_pragmas
1495 = sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1498 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1499 cpp_register_deferred_pragma (parse_in
, "acc", oacc_pragmas
[i
].name
,
1500 oacc_pragmas
[i
].id
, true, true);
1505 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1508 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1509 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1510 omp_pragmas
[i
].id
, true, true);
1512 if (flag_openmp
|| flag_openmp_simd
)
1514 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1515 / sizeof (*omp_pragmas
);
1518 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1519 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1520 omp_pragmas_simd
[i
].id
, true, true);
1524 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1527 if (!flag_preprocess_only
)
1528 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1529 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1531 if (!flag_preprocess_only
)
1532 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1536 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1537 PRAGMA_CILK_GRAINSIZE
, true, false);
1539 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1540 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1542 c_register_pragma (0, "pack", handle_pragma_pack
);
1544 c_register_pragma (0, "weak", handle_pragma_weak
);
1546 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1548 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1549 c_register_pragma ("GCC", "target", handle_pragma_target
);
1550 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1551 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1552 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1553 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1555 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1556 handle_pragma_float_const_decimal64
);
1558 c_register_pragma_with_expansion (0, "redefine_extname",
1559 handle_pragma_redefine_extname
);
1561 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1563 #ifdef REGISTER_TARGET_PRAGMAS
1564 REGISTER_TARGET_PRAGMAS ();
1567 global_sso
= default_sso
;
1568 c_register_pragma (0, "scalar_storage_order",
1569 handle_pragma_scalar_storage_order
);
1571 /* Allow plugins to register their own pragmas. */
1572 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1575 #include "gt-c-family-c-pragma.h"