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"
28 #include "stringpool.h"
31 #include "hard-reg-set.h"
32 #include "function.h" /* For cfun. FIXME: Does the parser know
33 when it is inside a function, so that
34 we don't have to look at cfun? */
39 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
40 this not a target hook?). */
42 #include "diagnostic.h"
45 #include "plugin-api.h"
49 #define GCC_BAD(gmsgid) \
50 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
51 #define GCC_BAD2(gmsgid, arg) \
52 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
54 typedef struct GTY(()) align_stack
{
57 struct align_stack
* prev
;
60 static GTY(()) struct align_stack
* alignment_stack
;
62 static void handle_pragma_pack (cpp_reader
*);
64 /* If we have a "global" #pragma pack(<n>) in effect when the first
65 #pragma pack(push,<n>) is encountered, this stores the value of
66 maximum_field_alignment in effect. When the final pop_alignment()
67 happens, we restore the value to this, not to a value of 0 for
68 maximum_field_alignment. Value is in bits. */
69 static int default_alignment
;
70 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
71 ? &default_alignment \
72 : &alignment_stack->alignment) = (ALIGN))
74 static void push_alignment (int, tree
);
75 static void pop_alignment (tree
);
77 /* Push an alignment value onto the stack. */
79 push_alignment (int alignment
, tree id
)
81 align_stack
* entry
= ggc_alloc
<align_stack
> ();
83 entry
->alignment
= alignment
;
85 entry
->prev
= alignment_stack
;
87 /* The current value of maximum_field_alignment is not necessarily
88 0 since there may be a #pragma pack(<n>) in effect; remember it
89 so that we can restore it after the final #pragma pop(). */
90 if (alignment_stack
== NULL
)
91 default_alignment
= maximum_field_alignment
;
93 alignment_stack
= entry
;
95 maximum_field_alignment
= alignment
;
98 /* Undo a push of an alignment onto the stack. */
100 pop_alignment (tree id
)
104 if (alignment_stack
== NULL
)
105 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
107 /* If we got an identifier, strip away everything above the target
108 entry so that the next step will restore the state just below it. */
111 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
114 alignment_stack
= entry
;
118 warning (OPT_Wpragmas
, "\
119 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
123 entry
= alignment_stack
->prev
;
125 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
127 alignment_stack
= entry
;
134 #pragma pack (push, N)
135 #pragma pack (push, ID)
136 #pragma pack (push, ID, N)
138 #pragma pack (pop, ID) */
140 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
144 enum cpp_ttype token
;
145 enum { set
, push
, pop
} action
;
147 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
148 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
150 token
= pragma_lex (&x
);
151 if (token
== CPP_CLOSE_PAREN
)
154 align
= initial_max_fld_align
;
156 else if (token
== CPP_NUMBER
)
158 if (TREE_CODE (x
) != INTEGER_CST
)
159 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
160 align
= TREE_INT_CST_LOW (x
);
162 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
163 GCC_BAD ("malformed %<#pragma pack%> - ignored");
165 else if (token
== CPP_NAME
)
167 #define GCC_BAD_ACTION do { if (action != pop) \
168 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
170 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
173 const char *op
= IDENTIFIER_POINTER (x
);
174 if (!strcmp (op
, "push"))
176 else if (!strcmp (op
, "pop"))
179 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
181 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
183 token
= pragma_lex (&x
);
184 if (token
== CPP_NAME
&& id
== 0)
188 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
190 if (TREE_CODE (x
) != INTEGER_CST
)
191 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
192 align
= TREE_INT_CST_LOW (x
);
200 if (token
!= CPP_CLOSE_PAREN
)
202 #undef GCC_BAD_ACTION
205 GCC_BAD ("malformed %<#pragma pack%> - ignored");
207 if (pragma_lex (&x
) != CPP_EOF
)
208 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
210 if (flag_pack_struct
)
211 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
222 align
*= BITS_PER_UNIT
;
227 align
= maximum_field_alignment
;
231 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
236 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
237 case push
: push_alignment (align
, id
); break;
238 case pop
: pop_alignment (id
); break;
242 typedef struct GTY(()) pending_weak_d
249 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
251 static void apply_pragma_weak (tree
, tree
);
252 static void handle_pragma_weak (cpp_reader
*);
255 apply_pragma_weak (tree decl
, tree value
)
259 value
= build_string (IDENTIFIER_LENGTH (value
),
260 IDENTIFIER_POINTER (value
));
261 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
262 build_tree_list (NULL
, value
)),
266 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
267 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
268 && DECL_ASSEMBLER_NAME_SET_P (decl
)
269 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
270 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
271 "results in unspecified behavior", decl
);
277 maybe_apply_pragma_weak (tree decl
)
283 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
285 /* No weak symbols pending, take the short-cut. */
286 if (vec_safe_is_empty (pending_weaks
))
288 /* If it's not visible outside this file, it doesn't matter whether
290 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
292 /* If it's not a function or a variable, it can't be weak.
293 FIXME: what kinds of things are visible outside this file but
294 aren't functions or variables? Should this be an assert instead? */
295 if (!VAR_OR_FUNCTION_DECL_P (decl
))
298 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
299 id
= DECL_ASSEMBLER_NAME (decl
);
302 id
= DECL_ASSEMBLER_NAME (decl
);
303 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
306 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
309 apply_pragma_weak (decl
, pe
->value
);
310 pending_weaks
->unordered_remove (i
);
315 /* Process all "#pragma weak A = B" directives where we have not seen
318 maybe_apply_pending_pragma_weaks (void)
320 tree alias_id
, id
, decl
;
325 if (vec_safe_is_empty (pending_weaks
))
328 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
336 target
= symtab_node::get_for_asmname (id
);
337 decl
= build_decl (UNKNOWN_LOCATION
,
338 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
339 alias_id
, default_function_type
);
341 DECL_ARTIFICIAL (decl
) = 1;
342 TREE_PUBLIC (decl
) = 1;
343 DECL_WEAK (decl
) = 1;
344 if (TREE_CODE (decl
) == VAR_DECL
)
345 TREE_STATIC (decl
) = 1;
348 error ("%q+D aliased to undefined symbol %qE",
353 assemble_alias (decl
, id
);
357 /* #pragma weak name [= value] */
359 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
361 tree name
, value
, x
, decl
;
366 if (pragma_lex (&name
) != CPP_NAME
)
367 GCC_BAD ("malformed #pragma weak, ignored");
371 if (pragma_lex (&value
) != CPP_NAME
)
372 GCC_BAD ("malformed #pragma weak, ignored");
376 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
378 decl
= identifier_global_value (name
);
379 if (decl
&& DECL_P (decl
))
381 if (!VAR_OR_FUNCTION_DECL_P (decl
))
382 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
384 apply_pragma_weak (decl
, value
);
387 DECL_EXTERNAL (decl
) = 0;
388 if (TREE_CODE (decl
) == VAR_DECL
)
389 TREE_STATIC (decl
) = 1;
390 assemble_alias (decl
, value
);
395 pending_weak pe
= {name
, value
};
396 vec_safe_push (pending_weaks
, pe
);
400 /* GCC supports two #pragma directives for renaming the external
401 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
402 compatibility with the Solaris and VMS system headers. GCC also
403 has its own notation for this, __asm__("name") annotations.
405 Corner cases of these features and their interaction:
407 1) Both pragmas silently apply only to declarations with external
408 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
409 do not have this restriction.
411 2) In C++, both #pragmas silently apply only to extern "C" declarations.
412 Asm labels do not have this restriction.
414 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
415 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
416 new name is different, a warning issues and the name does not change.
418 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
419 *not* the DECL_ASSEMBLER_NAME.
421 5) If #pragma extern_prefix is in effect and a declaration occurs
422 with an __asm__ name, the #pragma extern_prefix is silently
423 ignored for that declaration.
425 6) If #pragma extern_prefix and #pragma redefine_extname apply to
426 the same declaration, whichever triggered first wins, and a warning
427 is issued. (We would like to have #pragma redefine_extname always
428 win, but it can appear either before or after the declaration, and
429 if it appears afterward, we have no way of knowing whether a modified
430 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
432 typedef struct GTY(()) pending_redefinition_d
{
435 } pending_redefinition
;
438 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
440 static void handle_pragma_redefine_extname (cpp_reader
*);
442 /* #pragma redefine_extname oldname newname */
444 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
446 tree oldname
, newname
, decls
, x
;
450 if (pragma_lex (&oldname
) != CPP_NAME
)
451 GCC_BAD ("malformed #pragma redefine_extname, ignored");
452 if (pragma_lex (&newname
) != CPP_NAME
)
453 GCC_BAD ("malformed #pragma redefine_extname, ignored");
456 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
459 for (decls
= c_linkage_bindings (oldname
);
463 if (TREE_CODE (decls
) == TREE_LIST
)
465 decl
= TREE_VALUE (decls
);
466 decls
= TREE_CHAIN (decls
);
474 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
475 && VAR_OR_FUNCTION_DECL_P (decl
))
478 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
480 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
481 name
= targetm
.strip_name_encoding (name
);
483 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
484 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
485 "conflict with previous rename");
488 symtab
->change_decl_assembler_name (decl
, newname
);
493 /* We have to add this to the rename list even if there's already
494 a global value that doesn't meet the above criteria, because in
495 C++ "struct foo {...};" puts "foo" in the current namespace but
496 does *not* conflict with a subsequent declaration of a function
497 or variable foo. See g++.dg/other/pragma-re-2.C. */
498 add_to_renaming_pragma_list (oldname
, newname
);
501 /* This is called from here and from ia64-c.c. */
503 add_to_renaming_pragma_list (tree oldname
, tree newname
)
506 pending_redefinition
*p
;
508 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
509 if (oldname
== p
->oldname
)
511 if (p
->newname
!= newname
)
512 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
513 "conflict with previous #pragma redefine_extname");
517 pending_redefinition e
= {oldname
, newname
};
518 vec_safe_push (pending_redefine_extname
, e
);
521 /* The current prefix set by #pragma extern_prefix. */
522 GTY(()) tree pragma_extern_prefix
;
524 /* Hook from the front ends to apply the results of one of the preceding
525 pragmas that rename variables. */
528 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
531 pending_redefinition
*p
;
533 /* The renaming pragmas are only applied to declarations with
535 if (!VAR_OR_FUNCTION_DECL_P (decl
)
536 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
537 || !has_c_linkage (decl
))
540 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
541 but we may warn about a rename that conflicts. */
542 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
544 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
545 oldname
= targetm
.strip_name_encoding (oldname
);
547 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
548 warning (OPT_Wpragmas
, "asm declaration ignored due to "
549 "conflict with previous rename");
551 /* Take any pending redefine_extname off the list. */
552 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
553 if (DECL_NAME (decl
) == p
->oldname
)
555 /* Only warn if there is a conflict. */
556 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
557 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
558 "conflict with previous rename");
560 pending_redefine_extname
->unordered_remove (ix
);
566 /* Find out if we have a pending #pragma redefine_extname. */
567 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
568 if (DECL_NAME (decl
) == p
->oldname
)
570 tree newname
= p
->newname
;
571 pending_redefine_extname
->unordered_remove (ix
);
573 /* If we already have an asmname, #pragma redefine_extname is
574 ignored (with a warning if it conflicts). */
577 if (strcmp (TREE_STRING_POINTER (asmname
),
578 IDENTIFIER_POINTER (newname
)) != 0)
579 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
580 "conflict with __asm__ declaration");
584 /* Otherwise we use what we've got; #pragma extern_prefix is
586 return build_string (IDENTIFIER_LENGTH (newname
),
587 IDENTIFIER_POINTER (newname
));
590 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
594 /* If #pragma extern_prefix is in effect, apply it. */
595 if (pragma_extern_prefix
)
597 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
598 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
600 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
601 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
603 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
605 memcpy (newname
, prefix
, plen
);
606 memcpy (newname
+ plen
, id
, ilen
+ 1);
608 return build_string (plen
+ ilen
, newname
);
616 static void handle_pragma_visibility (cpp_reader
*);
618 static vec
<int> visstack
;
620 /* Push the visibility indicated by STR onto the top of the #pragma
621 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
622 C++ namespace with visibility attribute and 2 for C++ builtin
623 ABI namespace. push_visibility/pop_visibility calls must have
624 matching KIND, it is not allowed to push visibility using one
625 KIND and pop using a different one. */
628 push_visibility (const char *str
, int kind
)
630 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
631 if (!strcmp (str
, "default"))
632 default_visibility
= VISIBILITY_DEFAULT
;
633 else if (!strcmp (str
, "internal"))
634 default_visibility
= VISIBILITY_INTERNAL
;
635 else if (!strcmp (str
, "hidden"))
636 default_visibility
= VISIBILITY_HIDDEN
;
637 else if (!strcmp (str
, "protected"))
638 default_visibility
= VISIBILITY_PROTECTED
;
640 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
641 visibility_options
.inpragma
= 1;
644 /* Pop a level of the #pragma visibility stack. Return true if
648 pop_visibility (int kind
)
650 if (!visstack
.length ())
652 if ((visstack
.last () >> 8) != kind
)
655 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
656 visibility_options
.inpragma
657 = visstack
.length () != 0;
661 /* Sets the default visibility for symbols to something other than that
662 specified on the command line. */
665 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
667 /* Form is #pragma GCC visibility push(hidden)|pop */
669 enum cpp_ttype token
;
670 enum { bad
, push
, pop
} action
= bad
;
672 token
= pragma_lex (&x
);
673 if (token
== CPP_NAME
)
675 const char *op
= IDENTIFIER_POINTER (x
);
676 if (!strcmp (op
, "push"))
678 else if (!strcmp (op
, "pop"))
682 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
687 if (! pop_visibility (0))
688 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
692 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
693 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
694 token
= pragma_lex (&x
);
695 if (token
!= CPP_NAME
)
696 GCC_BAD ("malformed #pragma GCC visibility push");
698 push_visibility (IDENTIFIER_POINTER (x
), 0);
699 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
700 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
703 if (pragma_lex (&x
) != CPP_EOF
)
704 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
708 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
710 const char *kind_string
, *option_string
;
711 unsigned int option_index
;
712 enum cpp_ttype token
;
715 struct cl_option_handlers handlers
;
717 token
= pragma_lex (&x
);
718 if (token
!= CPP_NAME
)
719 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
720 kind_string
= IDENTIFIER_POINTER (x
);
721 if (strcmp (kind_string
, "error") == 0)
723 else if (strcmp (kind_string
, "warning") == 0)
725 else if (strcmp (kind_string
, "ignored") == 0)
727 else if (strcmp (kind_string
, "push") == 0)
729 diagnostic_push_diagnostics (global_dc
, input_location
);
732 else if (strcmp (kind_string
, "pop") == 0)
734 diagnostic_pop_diagnostics (global_dc
, input_location
);
738 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
740 token
= pragma_lex (&x
);
741 if (token
!= CPP_STRING
)
742 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
743 option_string
= TREE_STRING_POINTER (x
);
744 set_default_handlers (&handlers
);
745 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
746 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
748 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
749 input_location
, c_family_lang_mask
, &handlers
,
750 &global_options
, &global_options_set
,
754 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
757 /* Parse #pragma GCC target (xxx) to set target specific options. */
759 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
761 enum cpp_ttype token
;
763 bool close_paren_needed_p
= false;
767 error ("#pragma GCC option is not allowed inside functions");
771 token
= pragma_lex (&x
);
772 if (token
== CPP_OPEN_PAREN
)
774 close_paren_needed_p
= true;
775 token
= pragma_lex (&x
);
778 if (token
!= CPP_STRING
)
780 GCC_BAD ("%<#pragma GCC option%> is not a string");
784 /* Strings are user options. */
787 tree args
= NULL_TREE
;
791 /* Build up the strings now as a tree linked list. Skip empty
793 if (TREE_STRING_LENGTH (x
) > 0)
794 args
= tree_cons (NULL_TREE
, x
, args
);
796 token
= pragma_lex (&x
);
797 while (token
== CPP_COMMA
)
798 token
= pragma_lex (&x
);
800 while (token
== CPP_STRING
);
802 if (close_paren_needed_p
)
804 if (token
== CPP_CLOSE_PAREN
)
805 token
= pragma_lex (&x
);
807 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
808 "not have a final %<)%>");
811 if (token
!= CPP_EOF
)
813 error ("#pragma GCC target string... is badly formed");
817 /* put arguments in the order the user typed them. */
818 args
= nreverse (args
);
820 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
821 current_target_pragma
= args
;
825 /* Handle #pragma GCC optimize to set optimization options. */
827 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
829 enum cpp_ttype token
;
831 bool close_paren_needed_p
= false;
832 tree optimization_previous_node
= optimization_current_node
;
836 error ("#pragma GCC optimize is not allowed inside functions");
840 token
= pragma_lex (&x
);
841 if (token
== CPP_OPEN_PAREN
)
843 close_paren_needed_p
= true;
844 token
= pragma_lex (&x
);
847 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
849 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
853 /* Strings/numbers are user options. */
856 tree args
= NULL_TREE
;
860 /* Build up the numbers/strings now as a list. */
861 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
862 args
= tree_cons (NULL_TREE
, x
, args
);
864 token
= pragma_lex (&x
);
865 while (token
== CPP_COMMA
)
866 token
= pragma_lex (&x
);
868 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
870 if (close_paren_needed_p
)
872 if (token
== CPP_CLOSE_PAREN
)
873 token
= pragma_lex (&x
);
875 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
876 "not have a final %<)%>");
879 if (token
!= CPP_EOF
)
881 error ("#pragma GCC optimize string... is badly formed");
885 /* put arguments in the order the user typed them. */
886 args
= nreverse (args
);
888 parse_optimize_options (args
, false);
889 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
890 optimization_current_node
= build_optimization_node (&global_options
);
891 c_cpp_builtins_optimize_pragma (parse_in
,
892 optimization_previous_node
,
893 optimization_current_node
);
897 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
898 both the binary representation of the options and the TREE_LIST of
899 strings that will be added to the function's attribute list. */
900 typedef struct GTY(()) opt_stack
{
901 struct opt_stack
*prev
;
904 tree optimize_binary
;
905 tree optimize_strings
;
908 static GTY(()) struct opt_stack
* options_stack
;
910 /* Handle #pragma GCC push_options to save the current target and optimization
914 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
916 enum cpp_ttype token
;
919 token
= pragma_lex (&x
);
920 if (token
!= CPP_EOF
)
922 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
926 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
927 p
->prev
= options_stack
;
930 /* Save optimization and target flags in binary format. */
931 p
->optimize_binary
= build_optimization_node (&global_options
);
932 p
->target_binary
= build_target_option_node (&global_options
);
934 /* Save optimization and target flags in string list format. */
935 p
->optimize_strings
= copy_list (current_optimize_pragma
);
936 p
->target_strings
= copy_list (current_target_pragma
);
939 /* Handle #pragma GCC pop_options to restore the current target and
940 optimization options from a previous push_options. */
943 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
945 enum cpp_ttype token
;
949 token
= pragma_lex (&x
);
950 if (token
!= CPP_EOF
)
952 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
958 warning (OPT_Wpragmas
,
959 "%<#pragma GCC pop_options%> without a corresponding "
960 "%<#pragma GCC push_options%>");
965 options_stack
= p
->prev
;
967 if (p
->target_binary
!= target_option_current_node
)
969 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
970 target_option_current_node
= p
->target_binary
;
973 if (p
->optimize_binary
!= optimization_current_node
)
975 tree old_optimize
= optimization_current_node
;
976 cl_optimization_restore (&global_options
,
977 TREE_OPTIMIZATION (p
->optimize_binary
));
978 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
980 optimization_current_node
= p
->optimize_binary
;
983 current_target_pragma
= p
->target_strings
;
984 current_optimize_pragma
= p
->optimize_strings
;
987 /* Handle #pragma GCC reset_options to restore the current target and
988 optimization options to the original options used on the command line. */
991 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
993 enum cpp_ttype token
;
995 tree new_optimize
= optimization_default_node
;
996 tree new_target
= target_option_default_node
;
998 token
= pragma_lex (&x
);
999 if (token
!= CPP_EOF
)
1001 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1005 if (new_target
!= target_option_current_node
)
1007 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1008 target_option_current_node
= new_target
;
1011 if (new_optimize
!= optimization_current_node
)
1013 tree old_optimize
= optimization_current_node
;
1014 cl_optimization_restore (&global_options
,
1015 TREE_OPTIMIZATION (new_optimize
));
1016 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1017 optimization_current_node
= new_optimize
;
1020 current_target_pragma
= NULL_TREE
;
1021 current_optimize_pragma
= NULL_TREE
;
1024 /* Print a plain user-specified message. */
1027 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1029 enum cpp_ttype token
;
1030 tree x
, message
= 0;
1032 token
= pragma_lex (&x
);
1033 if (token
== CPP_OPEN_PAREN
)
1035 token
= pragma_lex (&x
);
1036 if (token
== CPP_STRING
)
1039 GCC_BAD ("expected a string after %<#pragma message%>");
1040 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1041 GCC_BAD ("malformed %<#pragma message%>, ignored");
1043 else if (token
== CPP_STRING
)
1046 GCC_BAD ("expected a string after %<#pragma message%>");
1048 gcc_assert (message
);
1050 if (pragma_lex (&x
) != CPP_EOF
)
1051 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1053 if (TREE_STRING_LENGTH (message
) > 1)
1054 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1057 /* Mark whether the current location is valid for a STDC pragma. */
1059 static bool valid_location_for_stdc_pragma
;
1062 mark_valid_location_for_stdc_pragma (bool flag
)
1064 valid_location_for_stdc_pragma
= flag
;
1067 /* Return true if the current location is valid for a STDC pragma. */
1070 valid_location_for_stdc_pragma_p (void)
1072 return valid_location_for_stdc_pragma
;
1075 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1077 /* A STDC pragma must appear outside of external declarations or
1078 preceding all explicit declarations and statements inside a compound
1079 statement; its behavior is undefined if used in any other context.
1080 It takes a switch of ON, OFF, or DEFAULT. */
1082 static enum pragma_switch_t
1083 handle_stdc_pragma (const char *pname
)
1087 enum pragma_switch_t ret
;
1089 if (!valid_location_for_stdc_pragma_p ())
1091 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1096 if (pragma_lex (&t
) != CPP_NAME
)
1098 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1102 arg
= IDENTIFIER_POINTER (t
);
1104 if (!strcmp (arg
, "ON"))
1106 else if (!strcmp (arg
, "OFF"))
1108 else if (!strcmp (arg
, "DEFAULT"))
1109 ret
= PRAGMA_DEFAULT
;
1112 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1116 if (pragma_lex (&t
) != CPP_EOF
)
1118 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1125 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1126 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1127 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1130 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1132 if (c_dialect_cxx ())
1134 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1135 warning (OPT_Wunknown_pragmas
,
1136 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1141 if (!targetm
.decimal_float_supported_p ())
1143 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1144 warning (OPT_Wunknown_pragmas
,
1145 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1150 pedwarn (input_location
, OPT_Wpedantic
,
1151 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1153 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1156 set_float_const_decimal64 ();
1159 case PRAGMA_DEFAULT
:
1160 clear_float_const_decimal64 ();
1167 /* A vector of registered pragma callbacks, which is never freed. */
1169 static vec
<internal_pragma_handler
> registered_pragmas
;
1178 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1180 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1181 static const struct omp_pragma_def oacc_pragmas
[] = {
1182 { "cache", PRAGMA_OACC_CACHE
},
1183 { "data", PRAGMA_OACC_DATA
},
1184 { "enter", PRAGMA_OACC_ENTER_DATA
},
1185 { "exit", PRAGMA_OACC_EXIT_DATA
},
1186 { "kernels", PRAGMA_OACC_KERNELS
},
1187 { "loop", PRAGMA_OACC_LOOP
},
1188 { "parallel", PRAGMA_OACC_PARALLEL
},
1189 { "update", PRAGMA_OACC_UPDATE
},
1190 { "wait", PRAGMA_OACC_WAIT
}
1192 static const struct omp_pragma_def omp_pragmas
[] = {
1193 { "atomic", PRAGMA_OMP_ATOMIC
},
1194 { "barrier", PRAGMA_OMP_BARRIER
},
1195 { "cancel", PRAGMA_OMP_CANCEL
},
1196 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1197 { "critical", PRAGMA_OMP_CRITICAL
},
1198 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1199 { "flush", PRAGMA_OMP_FLUSH
},
1200 { "master", PRAGMA_OMP_MASTER
},
1201 { "ordered", PRAGMA_OMP_ORDERED
},
1202 { "section", PRAGMA_OMP_SECTION
},
1203 { "sections", PRAGMA_OMP_SECTIONS
},
1204 { "single", PRAGMA_OMP_SINGLE
},
1205 { "task", PRAGMA_OMP_TASK
},
1206 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1207 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1208 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1209 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1211 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1212 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1213 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1214 { "for", PRAGMA_OMP_FOR
},
1215 { "parallel", PRAGMA_OMP_PARALLEL
},
1216 { "simd", PRAGMA_OMP_SIMD
},
1217 { "target", PRAGMA_OMP_TARGET
},
1218 { "teams", PRAGMA_OMP_TEAMS
},
1222 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1224 const int n_oacc_pragmas
= sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1225 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1226 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1227 / sizeof (*omp_pragmas
);
1230 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1231 if (oacc_pragmas
[i
].id
== id
)
1234 *name
= oacc_pragmas
[i
].name
;
1238 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1239 if (omp_pragmas
[i
].id
== id
)
1242 *name
= omp_pragmas
[i
].name
;
1246 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1247 if (omp_pragmas_simd
[i
].id
== id
)
1250 *name
= omp_pragmas_simd
[i
].name
;
1254 if (id
== PRAGMA_CILK_SIMD
)
1261 if (id
>= PRAGMA_FIRST_EXTERNAL
1262 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1264 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1265 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1272 /* Front-end wrappers for pragma registration to avoid dragging
1273 cpplib.h in almost everywhere. */
1276 c_register_pragma_1 (const char *space
, const char *name
,
1277 internal_pragma_handler ihandler
, bool allow_expansion
)
1281 if (flag_preprocess_only
)
1283 pragma_ns_name ns_name
;
1285 if (!allow_expansion
)
1288 ns_name
.space
= space
;
1289 ns_name
.name
= name
;
1290 registered_pp_pragmas
.safe_push (ns_name
);
1291 id
= registered_pp_pragmas
.length ();
1292 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1296 registered_pragmas
.safe_push (ihandler
);
1297 id
= registered_pragmas
.length ();
1298 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1300 /* The C++ front end allocates 6 bits in cp_token; the C front end
1301 allocates 7 bits in c_token. At present this is sufficient. */
1302 gcc_assert (id
< 64);
1305 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1306 allow_expansion
, false);
1309 /* Register a C pragma handler, using a space and a name. It disallows pragma
1310 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1312 c_register_pragma (const char *space
, const char *name
,
1313 pragma_handler_1arg handler
)
1315 internal_pragma_handler ihandler
;
1317 ihandler
.handler
.handler_1arg
= handler
;
1318 ihandler
.extra_data
= false;
1319 ihandler
.data
= NULL
;
1320 c_register_pragma_1 (space
, name
, ihandler
, false);
1323 /* Register a C pragma handler, using a space and a name, it also carries an
1324 extra data field which can be used by the handler. It disallows pragma
1325 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1328 c_register_pragma_with_data (const char *space
, const char *name
,
1329 pragma_handler_2arg handler
, void * data
)
1331 internal_pragma_handler ihandler
;
1333 ihandler
.handler
.handler_2arg
= handler
;
1334 ihandler
.extra_data
= true;
1335 ihandler
.data
= data
;
1336 c_register_pragma_1 (space
, name
, ihandler
, false);
1339 /* Register a C pragma handler, using a space and a name. It allows pragma
1340 expansion as in the following example:
1343 #pragma count (NUMBER)
1345 Name expansion is still disallowed. */
1347 c_register_pragma_with_expansion (const char *space
, const char *name
,
1348 pragma_handler_1arg handler
)
1350 internal_pragma_handler ihandler
;
1352 ihandler
.handler
.handler_1arg
= handler
;
1353 ihandler
.extra_data
= false;
1354 ihandler
.data
= NULL
;
1355 c_register_pragma_1 (space
, name
, ihandler
, true);
1358 /* Register a C pragma handler, using a space and a name, it also carries an
1359 extra data field which can be used by the handler. It allows pragma
1360 expansion as in the following example:
1363 #pragma count (NUMBER)
1365 Name expansion is still disallowed. */
1367 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1368 pragma_handler_2arg handler
,
1371 internal_pragma_handler ihandler
;
1373 ihandler
.handler
.handler_2arg
= handler
;
1374 ihandler
.extra_data
= true;
1375 ihandler
.data
= data
;
1376 c_register_pragma_1 (space
, name
, ihandler
, true);
1380 c_invoke_pragma_handler (unsigned int id
)
1382 internal_pragma_handler
*ihandler
;
1383 pragma_handler_1arg handler_1arg
;
1384 pragma_handler_2arg handler_2arg
;
1386 id
-= PRAGMA_FIRST_EXTERNAL
;
1387 ihandler
= ®istered_pragmas
[id
];
1388 if (ihandler
->extra_data
)
1390 handler_2arg
= ihandler
->handler
.handler_2arg
;
1391 handler_2arg (parse_in
, ihandler
->data
);
1395 handler_1arg
= ihandler
->handler
.handler_1arg
;
1396 handler_1arg (parse_in
);
1400 /* Set up front-end pragmas. */
1406 const int n_oacc_pragmas
1407 = sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1410 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1411 cpp_register_deferred_pragma (parse_in
, "acc", oacc_pragmas
[i
].name
,
1412 oacc_pragmas
[i
].id
, true, true);
1417 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1420 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1421 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1422 omp_pragmas
[i
].id
, true, true);
1424 if (flag_openmp
|| flag_openmp_simd
)
1426 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1427 / sizeof (*omp_pragmas
);
1430 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1431 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1432 omp_pragmas_simd
[i
].id
, true, true);
1436 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1439 if (!flag_preprocess_only
)
1440 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1441 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1443 if (!flag_preprocess_only
)
1444 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1447 if (flag_cilkplus
&& !flag_preprocess_only
)
1448 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1449 PRAGMA_CILK_GRAINSIZE
, true, false);
1451 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1452 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1454 c_register_pragma (0, "pack", handle_pragma_pack
);
1456 c_register_pragma (0, "weak", handle_pragma_weak
);
1457 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1459 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1460 c_register_pragma ("GCC", "target", handle_pragma_target
);
1461 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1462 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1463 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1464 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1466 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1467 handle_pragma_float_const_decimal64
);
1469 c_register_pragma_with_expansion (0, "redefine_extname",
1470 handle_pragma_redefine_extname
);
1472 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1474 #ifdef REGISTER_TARGET_PRAGMAS
1475 REGISTER_TARGET_PRAGMAS ();
1478 /* Allow plugins to register their own pragmas. */
1479 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1482 #include "gt-c-family-c-pragma.h"