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"
24 #include "function.h" /* For cfun. */
26 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS. */
27 #include "stringpool.h"
29 #include "diagnostic.h"
36 #define GCC_BAD(gmsgid) \
37 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
38 #define GCC_BAD2(gmsgid, arg) \
39 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
41 struct GTY(()) align_stack
{
44 struct align_stack
* prev
;
47 static GTY(()) struct align_stack
* alignment_stack
;
49 static void handle_pragma_pack (cpp_reader
*);
51 /* If we have a "global" #pragma pack(<n>) in effect when the first
52 #pragma pack(push,<n>) is encountered, this stores the value of
53 maximum_field_alignment in effect. When the final pop_alignment()
54 happens, we restore the value to this, not to a value of 0 for
55 maximum_field_alignment. Value is in bits. */
56 static int default_alignment
;
57 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
58 ? &default_alignment \
59 : &alignment_stack->alignment) = (ALIGN))
61 static void push_alignment (int, tree
);
62 static void pop_alignment (tree
);
64 /* Push an alignment value onto the stack. */
66 push_alignment (int alignment
, tree id
)
68 align_stack
* entry
= ggc_alloc
<align_stack
> ();
70 entry
->alignment
= alignment
;
72 entry
->prev
= alignment_stack
;
74 /* The current value of maximum_field_alignment is not necessarily
75 0 since there may be a #pragma pack(<n>) in effect; remember it
76 so that we can restore it after the final #pragma pop(). */
77 if (alignment_stack
== NULL
)
78 default_alignment
= maximum_field_alignment
;
80 alignment_stack
= entry
;
82 maximum_field_alignment
= alignment
;
85 /* Undo a push of an alignment onto the stack. */
87 pop_alignment (tree id
)
91 if (alignment_stack
== NULL
)
92 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
94 /* If we got an identifier, strip away everything above the target
95 entry so that the next step will restore the state just below it. */
98 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
101 alignment_stack
= entry
;
105 warning (OPT_Wpragmas
, "\
106 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
110 entry
= alignment_stack
->prev
;
112 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
114 alignment_stack
= entry
;
121 #pragma pack (push, N)
122 #pragma pack (push, ID)
123 #pragma pack (push, ID, N)
125 #pragma pack (pop, ID) */
127 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
131 enum cpp_ttype token
;
132 enum { set
, push
, pop
} action
;
134 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
135 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
137 token
= pragma_lex (&x
);
138 if (token
== CPP_CLOSE_PAREN
)
141 align
= initial_max_fld_align
;
143 else if (token
== CPP_NUMBER
)
145 if (TREE_CODE (x
) != INTEGER_CST
)
146 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
147 align
= TREE_INT_CST_LOW (x
);
149 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
150 GCC_BAD ("malformed %<#pragma pack%> - ignored");
152 else if (token
== CPP_NAME
)
154 #define GCC_BAD_ACTION do { if (action != pop) \
155 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
157 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
160 const char *op
= IDENTIFIER_POINTER (x
);
161 if (!strcmp (op
, "push"))
163 else if (!strcmp (op
, "pop"))
166 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
168 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
170 token
= pragma_lex (&x
);
171 if (token
== CPP_NAME
&& id
== 0)
175 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
177 if (TREE_CODE (x
) != INTEGER_CST
)
178 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
179 align
= TREE_INT_CST_LOW (x
);
187 if (token
!= CPP_CLOSE_PAREN
)
189 #undef GCC_BAD_ACTION
192 GCC_BAD ("malformed %<#pragma pack%> - ignored");
194 if (pragma_lex (&x
) != CPP_EOF
)
195 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
197 if (flag_pack_struct
)
198 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
209 align
*= BITS_PER_UNIT
;
214 align
= maximum_field_alignment
;
218 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
223 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
224 case push
: push_alignment (align
, id
); break;
225 case pop
: pop_alignment (id
); break;
229 struct GTY(()) pending_weak
236 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
238 static void apply_pragma_weak (tree
, tree
);
239 static void handle_pragma_weak (cpp_reader
*);
242 apply_pragma_weak (tree decl
, tree value
)
246 value
= build_string (IDENTIFIER_LENGTH (value
),
247 IDENTIFIER_POINTER (value
));
248 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
249 build_tree_list (NULL
, value
)),
253 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
254 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
255 && DECL_ASSEMBLER_NAME_SET_P (decl
)
256 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
257 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
258 "results in unspecified behavior", decl
);
264 maybe_apply_pragma_weak (tree decl
)
270 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
272 /* No weak symbols pending, take the short-cut. */
273 if (vec_safe_is_empty (pending_weaks
))
275 /* If it's not visible outside this file, it doesn't matter whether
277 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
279 /* If it's not a function or a variable, it can't be weak.
280 FIXME: what kinds of things are visible outside this file but
281 aren't functions or variables? Should this be an assert instead? */
282 if (!VAR_OR_FUNCTION_DECL_P (decl
))
285 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
286 id
= DECL_ASSEMBLER_NAME (decl
);
289 id
= DECL_ASSEMBLER_NAME (decl
);
290 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
293 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
296 apply_pragma_weak (decl
, pe
->value
);
297 pending_weaks
->unordered_remove (i
);
302 /* Process all "#pragma weak A = B" directives where we have not seen
305 maybe_apply_pending_pragma_weaks (void)
307 tree alias_id
, id
, decl
;
312 if (vec_safe_is_empty (pending_weaks
))
315 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
323 target
= symtab_node::get_for_asmname (id
);
324 decl
= build_decl (UNKNOWN_LOCATION
,
325 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
326 alias_id
, default_function_type
);
328 DECL_ARTIFICIAL (decl
) = 1;
329 TREE_PUBLIC (decl
) = 1;
330 DECL_WEAK (decl
) = 1;
332 TREE_STATIC (decl
) = 1;
335 error ("%q+D aliased to undefined symbol %qE",
340 assemble_alias (decl
, id
);
344 /* #pragma weak name [= value] */
346 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
348 tree name
, value
, x
, decl
;
353 if (pragma_lex (&name
) != CPP_NAME
)
354 GCC_BAD ("malformed #pragma weak, ignored");
358 if (pragma_lex (&value
) != CPP_NAME
)
359 GCC_BAD ("malformed #pragma weak, ignored");
363 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
365 decl
= identifier_global_value (name
);
366 if (decl
&& DECL_P (decl
))
368 if (!VAR_OR_FUNCTION_DECL_P (decl
))
369 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
371 apply_pragma_weak (decl
, value
);
374 DECL_EXTERNAL (decl
) = 0;
376 TREE_STATIC (decl
) = 1;
377 assemble_alias (decl
, value
);
382 pending_weak pe
= {name
, value
};
383 vec_safe_push (pending_weaks
, pe
);
387 static enum scalar_storage_order_kind global_sso
;
390 maybe_apply_pragma_scalar_storage_order (tree type
)
392 if (global_sso
== SSO_NATIVE
)
395 gcc_assert (RECORD_OR_UNION_TYPE_P (type
));
397 if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type
)))
400 if (global_sso
== SSO_BIG_ENDIAN
)
401 TYPE_REVERSE_STORAGE_ORDER (type
) = !BYTES_BIG_ENDIAN
;
402 else if (global_sso
== SSO_LITTLE_ENDIAN
)
403 TYPE_REVERSE_STORAGE_ORDER (type
) = BYTES_BIG_ENDIAN
;
409 handle_pragma_scalar_storage_order (cpp_reader
*ARG_UNUSED(dummy
))
411 const char *kind_string
;
412 enum cpp_ttype token
;
415 if (BYTES_BIG_ENDIAN
!= WORDS_BIG_ENDIAN
)
416 error ("scalar_storage_order is not supported");
418 token
= pragma_lex (&x
);
419 if (token
!= CPP_NAME
)
420 GCC_BAD ("missing [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
421 kind_string
= IDENTIFIER_POINTER (x
);
422 if (strcmp (kind_string
, "default") == 0)
423 global_sso
= default_sso
;
424 else if (strcmp (kind_string
, "big") == 0)
425 global_sso
= SSO_BIG_ENDIAN
;
426 else if (strcmp (kind_string
, "little") == 0)
427 global_sso
= SSO_LITTLE_ENDIAN
;
429 GCC_BAD ("expected [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
432 /* GCC supports two #pragma directives for renaming the external
433 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
434 compatibility with the Solaris and VMS system headers. GCC also
435 has its own notation for this, __asm__("name") annotations.
437 Corner cases of these features and their interaction:
439 1) Both pragmas silently apply only to declarations with external
440 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
441 do not have this restriction.
443 2) In C++, both #pragmas silently apply only to extern "C" declarations.
444 Asm labels do not have this restriction.
446 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
447 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
448 new name is different, a warning issues and the name does not change.
450 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
451 *not* the DECL_ASSEMBLER_NAME.
453 5) If #pragma extern_prefix is in effect and a declaration occurs
454 with an __asm__ name, the #pragma extern_prefix is silently
455 ignored for that declaration.
457 6) If #pragma extern_prefix and #pragma redefine_extname apply to
458 the same declaration, whichever triggered first wins, and a warning
459 is issued. (We would like to have #pragma redefine_extname always
460 win, but it can appear either before or after the declaration, and
461 if it appears afterward, we have no way of knowing whether a modified
462 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
464 struct GTY(()) pending_redefinition
{
470 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
472 static void handle_pragma_redefine_extname (cpp_reader
*);
474 /* #pragma redefine_extname oldname newname */
476 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
478 tree oldname
, newname
, decls
, x
;
482 if (pragma_lex (&oldname
) != CPP_NAME
)
483 GCC_BAD ("malformed #pragma redefine_extname, ignored");
484 if (pragma_lex (&newname
) != CPP_NAME
)
485 GCC_BAD ("malformed #pragma redefine_extname, ignored");
488 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
491 for (decls
= c_linkage_bindings (oldname
);
495 if (TREE_CODE (decls
) == TREE_LIST
)
497 decl
= TREE_VALUE (decls
);
498 decls
= TREE_CHAIN (decls
);
506 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
507 && VAR_OR_FUNCTION_DECL_P (decl
))
510 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
512 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
513 name
= targetm
.strip_name_encoding (name
);
515 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
516 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
517 "conflict with previous rename");
520 symtab
->change_decl_assembler_name (decl
, newname
);
525 /* We have to add this to the rename list even if there's already
526 a global value that doesn't meet the above criteria, because in
527 C++ "struct foo {...};" puts "foo" in the current namespace but
528 does *not* conflict with a subsequent declaration of a function
529 or variable foo. See g++.dg/other/pragma-re-2.C. */
530 add_to_renaming_pragma_list (oldname
, newname
);
533 /* This is called from here and from ia64-c.c. */
535 add_to_renaming_pragma_list (tree oldname
, tree newname
)
538 pending_redefinition
*p
;
540 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
541 if (oldname
== p
->oldname
)
543 if (p
->newname
!= newname
)
544 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
545 "conflict with previous #pragma redefine_extname");
549 pending_redefinition e
= {oldname
, newname
};
550 vec_safe_push (pending_redefine_extname
, e
);
553 /* The current prefix set by #pragma extern_prefix. */
554 GTY(()) tree pragma_extern_prefix
;
556 /* Hook from the front ends to apply the results of one of the preceding
557 pragmas that rename variables. */
560 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
563 pending_redefinition
*p
;
565 /* The renaming pragmas are only applied to declarations with
567 if (!VAR_OR_FUNCTION_DECL_P (decl
)
568 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
569 || !has_c_linkage (decl
))
572 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
573 but we may warn about a rename that conflicts. */
574 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
576 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
577 oldname
= targetm
.strip_name_encoding (oldname
);
579 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
580 warning (OPT_Wpragmas
, "asm declaration ignored due to "
581 "conflict with previous rename");
583 /* Take any pending redefine_extname off the list. */
584 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
585 if (DECL_NAME (decl
) == p
->oldname
)
587 /* Only warn if there is a conflict. */
588 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
589 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
590 "conflict with previous rename");
592 pending_redefine_extname
->unordered_remove (ix
);
598 /* Find out if we have a pending #pragma redefine_extname. */
599 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
600 if (DECL_NAME (decl
) == p
->oldname
)
602 tree newname
= p
->newname
;
603 pending_redefine_extname
->unordered_remove (ix
);
605 /* If we already have an asmname, #pragma redefine_extname is
606 ignored (with a warning if it conflicts). */
609 if (strcmp (TREE_STRING_POINTER (asmname
),
610 IDENTIFIER_POINTER (newname
)) != 0)
611 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
612 "conflict with __asm__ declaration");
616 /* Otherwise we use what we've got; #pragma extern_prefix is
618 return build_string (IDENTIFIER_LENGTH (newname
),
619 IDENTIFIER_POINTER (newname
));
622 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
626 /* If #pragma extern_prefix is in effect, apply it. */
627 if (pragma_extern_prefix
)
629 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
630 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
632 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
633 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
635 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
637 memcpy (newname
, prefix
, plen
);
638 memcpy (newname
+ plen
, id
, ilen
+ 1);
640 return build_string (plen
+ ilen
, newname
);
648 static void handle_pragma_visibility (cpp_reader
*);
650 static vec
<int> visstack
;
652 /* Push the visibility indicated by STR onto the top of the #pragma
653 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
654 C++ namespace with visibility attribute and 2 for C++ builtin
655 ABI namespace. push_visibility/pop_visibility calls must have
656 matching KIND, it is not allowed to push visibility using one
657 KIND and pop using a different one. */
660 push_visibility (const char *str
, int kind
)
662 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
663 if (!strcmp (str
, "default"))
664 default_visibility
= VISIBILITY_DEFAULT
;
665 else if (!strcmp (str
, "internal"))
666 default_visibility
= VISIBILITY_INTERNAL
;
667 else if (!strcmp (str
, "hidden"))
668 default_visibility
= VISIBILITY_HIDDEN
;
669 else if (!strcmp (str
, "protected"))
670 default_visibility
= VISIBILITY_PROTECTED
;
672 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
673 visibility_options
.inpragma
= 1;
676 /* Pop a level of the #pragma visibility stack. Return true if
680 pop_visibility (int kind
)
682 if (!visstack
.length ())
684 if ((visstack
.last () >> 8) != kind
)
687 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
688 visibility_options
.inpragma
689 = visstack
.length () != 0;
693 /* Sets the default visibility for symbols to something other than that
694 specified on the command line. */
697 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
699 /* Form is #pragma GCC visibility push(hidden)|pop */
701 enum cpp_ttype token
;
702 enum { bad
, push
, pop
} action
= bad
;
704 token
= pragma_lex (&x
);
705 if (token
== CPP_NAME
)
707 const char *op
= IDENTIFIER_POINTER (x
);
708 if (!strcmp (op
, "push"))
710 else if (!strcmp (op
, "pop"))
714 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
719 if (! pop_visibility (0))
720 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
724 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
725 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
726 token
= pragma_lex (&x
);
727 if (token
!= CPP_NAME
)
728 GCC_BAD ("malformed #pragma GCC visibility push");
730 push_visibility (IDENTIFIER_POINTER (x
), 0);
731 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
732 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
735 if (pragma_lex (&x
) != CPP_EOF
)
736 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
740 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
744 enum cpp_ttype token
= pragma_lex (&x
, &loc
);
745 if (token
!= CPP_NAME
)
747 warning_at (loc
, OPT_Wpragmas
,
748 "missing [error|warning|ignored|push|pop]"
749 " after %<#pragma GCC diagnostic%>");
754 const char *kind_string
= IDENTIFIER_POINTER (x
);
755 if (strcmp (kind_string
, "error") == 0)
757 else if (strcmp (kind_string
, "warning") == 0)
759 else if (strcmp (kind_string
, "ignored") == 0)
761 else if (strcmp (kind_string
, "push") == 0)
763 diagnostic_push_diagnostics (global_dc
, input_location
);
766 else if (strcmp (kind_string
, "pop") == 0)
768 diagnostic_pop_diagnostics (global_dc
, input_location
);
773 warning_at (loc
, OPT_Wpragmas
,
774 "expected [error|warning|ignored|push|pop]"
775 " after %<#pragma GCC diagnostic%>");
779 token
= pragma_lex (&x
, &loc
);
780 if (token
!= CPP_STRING
)
782 warning_at (loc
, OPT_Wpragmas
,
783 "missing option after %<#pragma GCC diagnostic%> kind");
787 const char *option_string
= TREE_STRING_POINTER (x
);
788 unsigned int lang_mask
= c_common_option_lang_mask () | CL_COMMON
;
789 /* option_string + 1 to skip the initial '-' */
790 unsigned int option_index
= find_opt (option_string
+ 1, lang_mask
);
791 if (option_index
== OPT_SPECIAL_unknown
)
793 warning_at (loc
, OPT_Wpragmas
,
794 "unknown option after %<#pragma GCC diagnostic%> kind");
797 else if (!(cl_options
[option_index
].flags
& CL_WARNING
))
799 warning_at (loc
, OPT_Wpragmas
,
800 "%qs is not an option that controls warnings", option_string
);
803 else if (!(cl_options
[option_index
].flags
& lang_mask
))
805 char *ok_langs
= write_langs (cl_options
[option_index
].flags
);
806 char *bad_lang
= write_langs (c_common_option_lang_mask ());
807 warning_at (loc
, OPT_Wpragmas
,
808 "option %qs is valid for %s but not for %s",
809 option_string
, ok_langs
, bad_lang
);
815 struct cl_option_handlers handlers
;
816 set_default_handlers (&handlers
);
817 const char *arg
= NULL
;
818 if (cl_options
[option_index
].flags
& CL_JOINED
)
819 arg
= option_string
+ 1 + cl_options
[option_index
].opt_len
;
820 control_warning_option (option_index
, (int) kind
,
821 arg
, kind
!= DK_IGNORED
,
822 loc
, lang_mask
, &handlers
,
823 &global_options
, &global_options_set
,
827 /* Parse #pragma GCC target (xxx) to set target specific options. */
829 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
831 enum cpp_ttype token
;
833 bool close_paren_needed_p
= false;
837 error ("#pragma GCC option is not allowed inside functions");
841 token
= pragma_lex (&x
);
842 if (token
== CPP_OPEN_PAREN
)
844 close_paren_needed_p
= true;
845 token
= pragma_lex (&x
);
848 if (token
!= CPP_STRING
)
850 GCC_BAD ("%<#pragma GCC option%> is not a string");
854 /* Strings are user options. */
857 tree args
= NULL_TREE
;
861 /* Build up the strings now as a tree linked list. Skip empty
863 if (TREE_STRING_LENGTH (x
) > 0)
864 args
= tree_cons (NULL_TREE
, x
, args
);
866 token
= pragma_lex (&x
);
867 while (token
== CPP_COMMA
)
868 token
= pragma_lex (&x
);
870 while (token
== CPP_STRING
);
872 if (close_paren_needed_p
)
874 if (token
== CPP_CLOSE_PAREN
)
875 token
= pragma_lex (&x
);
877 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
878 "not have a final %<)%>");
881 if (token
!= CPP_EOF
)
883 error ("#pragma GCC target string... is badly formed");
887 /* put arguments in the order the user typed them. */
888 args
= nreverse (args
);
890 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
891 current_target_pragma
= args
;
895 /* Handle #pragma GCC optimize to set optimization options. */
897 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
899 enum cpp_ttype token
;
901 bool close_paren_needed_p
= false;
902 tree optimization_previous_node
= optimization_current_node
;
906 error ("#pragma GCC optimize is not allowed inside functions");
910 token
= pragma_lex (&x
);
911 if (token
== CPP_OPEN_PAREN
)
913 close_paren_needed_p
= true;
914 token
= pragma_lex (&x
);
917 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
919 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
923 /* Strings/numbers are user options. */
926 tree args
= NULL_TREE
;
930 /* Build up the numbers/strings now as a list. */
931 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
932 args
= tree_cons (NULL_TREE
, x
, args
);
934 token
= pragma_lex (&x
);
935 while (token
== CPP_COMMA
)
936 token
= pragma_lex (&x
);
938 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
940 if (close_paren_needed_p
)
942 if (token
== CPP_CLOSE_PAREN
)
943 token
= pragma_lex (&x
);
945 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
946 "not have a final %<)%>");
949 if (token
!= CPP_EOF
)
951 error ("#pragma GCC optimize string... is badly formed");
955 /* put arguments in the order the user typed them. */
956 args
= nreverse (args
);
958 parse_optimize_options (args
, false);
959 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
960 optimization_current_node
= build_optimization_node (&global_options
);
961 c_cpp_builtins_optimize_pragma (parse_in
,
962 optimization_previous_node
,
963 optimization_current_node
);
967 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
968 both the binary representation of the options and the TREE_LIST of
969 strings that will be added to the function's attribute list. */
970 struct GTY(()) opt_stack
{
971 struct opt_stack
*prev
;
974 tree optimize_binary
;
975 tree optimize_strings
;
978 static GTY(()) struct opt_stack
* options_stack
;
980 /* Handle #pragma GCC push_options to save the current target and optimization
984 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
986 enum cpp_ttype token
;
989 token
= pragma_lex (&x
);
990 if (token
!= CPP_EOF
)
992 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
996 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
997 p
->prev
= options_stack
;
1000 /* Save optimization and target flags in binary format. */
1001 p
->optimize_binary
= build_optimization_node (&global_options
);
1002 p
->target_binary
= build_target_option_node (&global_options
);
1004 /* Save optimization and target flags in string list format. */
1005 p
->optimize_strings
= copy_list (current_optimize_pragma
);
1006 p
->target_strings
= copy_list (current_target_pragma
);
1009 /* Handle #pragma GCC pop_options to restore the current target and
1010 optimization options from a previous push_options. */
1013 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
1015 enum cpp_ttype token
;
1019 token
= pragma_lex (&x
);
1020 if (token
!= CPP_EOF
)
1022 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
1026 if (! options_stack
)
1028 warning (OPT_Wpragmas
,
1029 "%<#pragma GCC pop_options%> without a corresponding "
1030 "%<#pragma GCC push_options%>");
1035 options_stack
= p
->prev
;
1037 if (p
->target_binary
!= target_option_current_node
)
1039 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
1040 target_option_current_node
= p
->target_binary
;
1043 if (p
->optimize_binary
!= optimization_current_node
)
1045 tree old_optimize
= optimization_current_node
;
1046 cl_optimization_restore (&global_options
,
1047 TREE_OPTIMIZATION (p
->optimize_binary
));
1048 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
1049 p
->optimize_binary
);
1050 optimization_current_node
= p
->optimize_binary
;
1053 current_target_pragma
= p
->target_strings
;
1054 current_optimize_pragma
= p
->optimize_strings
;
1057 /* Handle #pragma GCC reset_options to restore the current target and
1058 optimization options to the original options used on the command line. */
1061 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
1063 enum cpp_ttype token
;
1065 tree new_optimize
= optimization_default_node
;
1066 tree new_target
= target_option_default_node
;
1068 token
= pragma_lex (&x
);
1069 if (token
!= CPP_EOF
)
1071 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1075 if (new_target
!= target_option_current_node
)
1077 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1078 target_option_current_node
= new_target
;
1081 if (new_optimize
!= optimization_current_node
)
1083 tree old_optimize
= optimization_current_node
;
1084 cl_optimization_restore (&global_options
,
1085 TREE_OPTIMIZATION (new_optimize
));
1086 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1087 optimization_current_node
= new_optimize
;
1090 current_target_pragma
= NULL_TREE
;
1091 current_optimize_pragma
= NULL_TREE
;
1094 /* Print a plain user-specified message. */
1097 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1099 enum cpp_ttype token
;
1100 tree x
, message
= 0;
1102 token
= pragma_lex (&x
);
1103 if (token
== CPP_OPEN_PAREN
)
1105 token
= pragma_lex (&x
);
1106 if (token
== CPP_STRING
)
1109 GCC_BAD ("expected a string after %<#pragma message%>");
1110 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1111 GCC_BAD ("malformed %<#pragma message%>, ignored");
1113 else if (token
== CPP_STRING
)
1116 GCC_BAD ("expected a string after %<#pragma message%>");
1118 gcc_assert (message
);
1120 if (pragma_lex (&x
) != CPP_EOF
)
1121 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1123 if (TREE_STRING_LENGTH (message
) > 1)
1124 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1127 /* Mark whether the current location is valid for a STDC pragma. */
1129 static bool valid_location_for_stdc_pragma
;
1132 mark_valid_location_for_stdc_pragma (bool flag
)
1134 valid_location_for_stdc_pragma
= flag
;
1137 /* Return true if the current location is valid for a STDC pragma. */
1140 valid_location_for_stdc_pragma_p (void)
1142 return valid_location_for_stdc_pragma
;
1145 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1147 /* A STDC pragma must appear outside of external declarations or
1148 preceding all explicit declarations and statements inside a compound
1149 statement; its behavior is undefined if used in any other context.
1150 It takes a switch of ON, OFF, or DEFAULT. */
1152 static enum pragma_switch_t
1153 handle_stdc_pragma (const char *pname
)
1157 enum pragma_switch_t ret
;
1159 if (!valid_location_for_stdc_pragma_p ())
1161 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1166 if (pragma_lex (&t
) != CPP_NAME
)
1168 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1172 arg
= IDENTIFIER_POINTER (t
);
1174 if (!strcmp (arg
, "ON"))
1176 else if (!strcmp (arg
, "OFF"))
1178 else if (!strcmp (arg
, "DEFAULT"))
1179 ret
= PRAGMA_DEFAULT
;
1182 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1186 if (pragma_lex (&t
) != CPP_EOF
)
1188 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1195 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1196 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1197 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1200 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1202 if (c_dialect_cxx ())
1204 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1205 warning (OPT_Wunknown_pragmas
,
1206 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1211 if (!targetm
.decimal_float_supported_p ())
1213 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1214 warning (OPT_Wunknown_pragmas
,
1215 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1220 pedwarn (input_location
, OPT_Wpedantic
,
1221 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1223 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1226 set_float_const_decimal64 ();
1229 case PRAGMA_DEFAULT
:
1230 clear_float_const_decimal64 ();
1237 /* A vector of registered pragma callbacks, which is never freed. */
1239 static vec
<internal_pragma_handler
> registered_pragmas
;
1241 struct pragma_ns_name
1248 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1250 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1251 static const struct omp_pragma_def oacc_pragmas
[] = {
1252 { "atomic", PRAGMA_OACC_ATOMIC
},
1253 { "cache", PRAGMA_OACC_CACHE
},
1254 { "data", PRAGMA_OACC_DATA
},
1255 { "declare", PRAGMA_OACC_DECLARE
},
1256 { "enter", PRAGMA_OACC_ENTER_DATA
},
1257 { "exit", PRAGMA_OACC_EXIT_DATA
},
1258 { "host_data", PRAGMA_OACC_HOST_DATA
},
1259 { "kernels", PRAGMA_OACC_KERNELS
},
1260 { "loop", PRAGMA_OACC_LOOP
},
1261 { "parallel", PRAGMA_OACC_PARALLEL
},
1262 { "routine", PRAGMA_OACC_ROUTINE
},
1263 { "update", PRAGMA_OACC_UPDATE
},
1264 { "wait", PRAGMA_OACC_WAIT
}
1266 static const struct omp_pragma_def omp_pragmas
[] = {
1267 { "atomic", PRAGMA_OMP_ATOMIC
},
1268 { "barrier", PRAGMA_OMP_BARRIER
},
1269 { "cancel", PRAGMA_OMP_CANCEL
},
1270 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1271 { "critical", PRAGMA_OMP_CRITICAL
},
1272 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1273 { "flush", PRAGMA_OMP_FLUSH
},
1274 { "master", PRAGMA_OMP_MASTER
},
1275 { "ordered", PRAGMA_OMP_ORDERED
},
1276 { "section", PRAGMA_OMP_SECTION
},
1277 { "sections", PRAGMA_OMP_SECTIONS
},
1278 { "single", PRAGMA_OMP_SINGLE
},
1279 { "task", PRAGMA_OMP_TASK
},
1280 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1281 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1282 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1283 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1285 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1286 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1287 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1288 { "for", PRAGMA_OMP_FOR
},
1289 { "parallel", PRAGMA_OMP_PARALLEL
},
1290 { "simd", PRAGMA_OMP_SIMD
},
1291 { "target", PRAGMA_OMP_TARGET
},
1292 { "taskloop", PRAGMA_OMP_TASKLOOP
},
1293 { "teams", PRAGMA_OMP_TEAMS
},
1297 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1299 const int n_oacc_pragmas
= sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1300 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1301 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1302 / sizeof (*omp_pragmas
);
1305 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1306 if (oacc_pragmas
[i
].id
== id
)
1309 *name
= oacc_pragmas
[i
].name
;
1313 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1314 if (omp_pragmas
[i
].id
== id
)
1317 *name
= omp_pragmas
[i
].name
;
1321 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1322 if (omp_pragmas_simd
[i
].id
== id
)
1325 *name
= omp_pragmas_simd
[i
].name
;
1329 if (id
== PRAGMA_CILK_SIMD
)
1336 if (id
>= PRAGMA_FIRST_EXTERNAL
1337 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1339 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1340 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1347 /* Front-end wrappers for pragma registration to avoid dragging
1348 cpplib.h in almost everywhere. */
1351 c_register_pragma_1 (const char *space
, const char *name
,
1352 internal_pragma_handler ihandler
, bool allow_expansion
)
1356 if (flag_preprocess_only
)
1358 pragma_ns_name ns_name
;
1360 if (!allow_expansion
)
1363 ns_name
.space
= space
;
1364 ns_name
.name
= name
;
1365 registered_pp_pragmas
.safe_push (ns_name
);
1366 id
= registered_pp_pragmas
.length ();
1367 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1371 registered_pragmas
.safe_push (ihandler
);
1372 id
= registered_pragmas
.length ();
1373 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1375 /* The C++ front end allocates 8 bits in cp_token; the C front end
1376 allocates 8 bits in c_token. At present this is sufficient. */
1377 gcc_assert (id
< 256);
1380 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1381 allow_expansion
, false);
1384 /* Register a C pragma handler, using a space and a name. It disallows pragma
1385 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1387 c_register_pragma (const char *space
, const char *name
,
1388 pragma_handler_1arg handler
)
1390 internal_pragma_handler ihandler
;
1392 ihandler
.handler
.handler_1arg
= handler
;
1393 ihandler
.extra_data
= false;
1394 ihandler
.data
= NULL
;
1395 c_register_pragma_1 (space
, name
, ihandler
, false);
1398 /* Register a C pragma handler, using a space and a name, it also carries an
1399 extra data field which can be used by the handler. It disallows pragma
1400 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1403 c_register_pragma_with_data (const char *space
, const char *name
,
1404 pragma_handler_2arg handler
, void * data
)
1406 internal_pragma_handler ihandler
;
1408 ihandler
.handler
.handler_2arg
= handler
;
1409 ihandler
.extra_data
= true;
1410 ihandler
.data
= data
;
1411 c_register_pragma_1 (space
, name
, ihandler
, false);
1414 /* Register a C pragma handler, using a space and a name. It allows pragma
1415 expansion as in the following example:
1418 #pragma count (NUMBER)
1420 Name expansion is still disallowed. */
1422 c_register_pragma_with_expansion (const char *space
, const char *name
,
1423 pragma_handler_1arg handler
)
1425 internal_pragma_handler ihandler
;
1427 ihandler
.handler
.handler_1arg
= handler
;
1428 ihandler
.extra_data
= false;
1429 ihandler
.data
= NULL
;
1430 c_register_pragma_1 (space
, name
, ihandler
, true);
1433 /* Register a C pragma handler, using a space and a name, it also carries an
1434 extra data field which can be used by the handler. It allows pragma
1435 expansion as in the following example:
1438 #pragma count (NUMBER)
1440 Name expansion is still disallowed. */
1442 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1443 pragma_handler_2arg handler
,
1446 internal_pragma_handler ihandler
;
1448 ihandler
.handler
.handler_2arg
= handler
;
1449 ihandler
.extra_data
= true;
1450 ihandler
.data
= data
;
1451 c_register_pragma_1 (space
, name
, ihandler
, true);
1455 c_invoke_pragma_handler (unsigned int id
)
1457 internal_pragma_handler
*ihandler
;
1458 pragma_handler_1arg handler_1arg
;
1459 pragma_handler_2arg handler_2arg
;
1461 id
-= PRAGMA_FIRST_EXTERNAL
;
1462 ihandler
= ®istered_pragmas
[id
];
1463 if (ihandler
->extra_data
)
1465 handler_2arg
= ihandler
->handler
.handler_2arg
;
1466 handler_2arg (parse_in
, ihandler
->data
);
1470 handler_1arg
= ihandler
->handler
.handler_1arg
;
1471 handler_1arg (parse_in
);
1475 /* Set up front-end pragmas. */
1481 const int n_oacc_pragmas
1482 = sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1485 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1486 cpp_register_deferred_pragma (parse_in
, "acc", oacc_pragmas
[i
].name
,
1487 oacc_pragmas
[i
].id
, true, true);
1492 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1495 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1496 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1497 omp_pragmas
[i
].id
, true, true);
1499 if (flag_openmp
|| flag_openmp_simd
)
1501 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1502 / sizeof (*omp_pragmas
);
1505 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1506 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1507 omp_pragmas_simd
[i
].id
, true, true);
1511 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1514 if (!flag_preprocess_only
)
1515 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1516 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1518 if (!flag_preprocess_only
)
1519 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1522 if (flag_cilkplus
&& !flag_preprocess_only
)
1523 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1524 PRAGMA_CILK_GRAINSIZE
, true, false);
1526 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1527 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1529 c_register_pragma (0, "pack", handle_pragma_pack
);
1531 c_register_pragma (0, "weak", handle_pragma_weak
);
1533 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1535 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1536 c_register_pragma ("GCC", "target", handle_pragma_target
);
1537 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1538 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1539 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1540 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1542 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1543 handle_pragma_float_const_decimal64
);
1545 c_register_pragma_with_expansion (0, "redefine_extname",
1546 handle_pragma_redefine_extname
);
1548 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1550 #ifdef REGISTER_TARGET_PRAGMAS
1551 REGISTER_TARGET_PRAGMAS ();
1554 global_sso
= default_sso
;
1555 c_register_pragma (0, "scalar_storage_order",
1556 handle_pragma_scalar_storage_order
);
1558 /* Allow plugins to register their own pragmas. */
1559 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1562 #include "gt-c-family-c-pragma.h"