1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
25 #include "stringpool.h"
28 #include "gcc-symtab.h"
29 #include "function.h" /* For cfun. FIXME: Does the parser know
30 when it is inside a function, so that
31 we don't have to look at cfun? */
36 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
37 this not a target hook?). */
40 #include "diagnostic.h"
45 #define GCC_BAD(gmsgid) \
46 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
47 #define GCC_BAD2(gmsgid, arg) \
48 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
50 typedef struct GTY(()) align_stack
{
53 struct align_stack
* prev
;
56 static GTY(()) struct align_stack
* alignment_stack
;
58 static void handle_pragma_pack (cpp_reader
*);
60 /* If we have a "global" #pragma pack(<n>) in effect when the first
61 #pragma pack(push,<n>) is encountered, this stores the value of
62 maximum_field_alignment in effect. When the final pop_alignment()
63 happens, we restore the value to this, not to a value of 0 for
64 maximum_field_alignment. Value is in bits. */
65 static int default_alignment
;
66 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
67 ? &default_alignment \
68 : &alignment_stack->alignment) = (ALIGN))
70 static void push_alignment (int, tree
);
71 static void pop_alignment (tree
);
73 /* Push an alignment value onto the stack. */
75 push_alignment (int alignment
, tree id
)
79 entry
= ggc_alloc_align_stack ();
81 entry
->alignment
= alignment
;
83 entry
->prev
= alignment_stack
;
85 /* The current value of maximum_field_alignment is not necessarily
86 0 since there may be a #pragma pack(<n>) in effect; remember it
87 so that we can restore it after the final #pragma pop(). */
88 if (alignment_stack
== NULL
)
89 default_alignment
= maximum_field_alignment
;
91 alignment_stack
= entry
;
93 maximum_field_alignment
= alignment
;
96 /* Undo a push of an alignment onto the stack. */
98 pop_alignment (tree id
)
102 if (alignment_stack
== NULL
)
103 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
105 /* If we got an identifier, strip away everything above the target
106 entry so that the next step will restore the state just below it. */
109 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
112 alignment_stack
= entry
;
116 warning (OPT_Wpragmas
, "\
117 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
121 entry
= alignment_stack
->prev
;
123 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
125 alignment_stack
= entry
;
132 #pragma pack (push, N)
133 #pragma pack (push, ID)
134 #pragma pack (push, ID, N)
136 #pragma pack (pop, ID) */
138 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
142 enum cpp_ttype token
;
143 enum { set
, push
, pop
} action
;
145 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
146 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
148 token
= pragma_lex (&x
);
149 if (token
== CPP_CLOSE_PAREN
)
152 align
= initial_max_fld_align
;
154 else if (token
== CPP_NUMBER
)
156 if (TREE_CODE (x
) != INTEGER_CST
)
157 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
158 align
= TREE_INT_CST_LOW (x
);
160 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
161 GCC_BAD ("malformed %<#pragma pack%> - ignored");
163 else if (token
== CPP_NAME
)
165 #define GCC_BAD_ACTION do { if (action != pop) \
166 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
168 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
171 const char *op
= IDENTIFIER_POINTER (x
);
172 if (!strcmp (op
, "push"))
174 else if (!strcmp (op
, "pop"))
177 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
179 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
181 token
= pragma_lex (&x
);
182 if (token
== CPP_NAME
&& id
== 0)
186 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
188 if (TREE_CODE (x
) != INTEGER_CST
)
189 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
190 align
= TREE_INT_CST_LOW (x
);
198 if (token
!= CPP_CLOSE_PAREN
)
200 #undef GCC_BAD_ACTION
203 GCC_BAD ("malformed %<#pragma pack%> - ignored");
205 if (pragma_lex (&x
) != CPP_EOF
)
206 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
208 if (flag_pack_struct
)
209 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
220 align
*= BITS_PER_UNIT
;
225 align
= maximum_field_alignment
;
229 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
234 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
235 case push
: push_alignment (align
, id
); break;
236 case pop
: pop_alignment (id
); break;
240 typedef struct GTY(()) pending_weak_d
247 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
249 static void apply_pragma_weak (tree
, tree
);
250 static void handle_pragma_weak (cpp_reader
*);
253 apply_pragma_weak (tree decl
, tree value
)
257 value
= build_string (IDENTIFIER_LENGTH (value
),
258 IDENTIFIER_POINTER (value
));
259 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
260 build_tree_list (NULL
, value
)),
264 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
265 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
266 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
267 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
268 "results in unspecified behavior", decl
);
274 maybe_apply_pragma_weak (tree decl
)
280 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
282 /* No weak symbols pending, take the short-cut. */
285 /* If it's not visible outside this file, it doesn't matter whether
287 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
289 /* If it's not a function or a variable, it can't be weak.
290 FIXME: what kinds of things are visible outside this file but
291 aren't functions or variables? Should this be an assert instead? */
292 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
295 id
= DECL_ASSEMBLER_NAME (decl
);
297 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
300 apply_pragma_weak (decl
, pe
->value
);
301 pending_weaks
->unordered_remove (i
);
306 /* Process all "#pragma weak A = B" directives where we have not seen
309 maybe_apply_pending_pragma_weaks (void)
311 tree alias_id
, id
, decl
;
319 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
327 target
= symtab_node_for_asm (id
);
328 decl
= build_decl (UNKNOWN_LOCATION
,
329 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
330 alias_id
, default_function_type
);
332 DECL_ARTIFICIAL (decl
) = 1;
333 TREE_PUBLIC (decl
) = 1;
334 DECL_WEAK (decl
) = 1;
335 if (TREE_CODE (decl
) == VAR_DECL
)
336 TREE_STATIC (decl
) = 1;
339 error ("%q+D aliased to undefined symbol %qE",
344 assemble_alias (decl
, id
);
348 /* #pragma weak name [= value] */
350 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
352 tree name
, value
, x
, decl
;
357 if (pragma_lex (&name
) != CPP_NAME
)
358 GCC_BAD ("malformed #pragma weak, ignored");
362 if (pragma_lex (&value
) != CPP_NAME
)
363 GCC_BAD ("malformed #pragma weak, ignored");
367 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
369 decl
= identifier_global_value (name
);
370 if (decl
&& DECL_P (decl
))
372 apply_pragma_weak (decl
, value
);
375 DECL_EXTERNAL (decl
) = 0;
376 if (TREE_CODE (decl
) == VAR_DECL
)
377 TREE_STATIC (decl
) = 1;
378 assemble_alias (decl
, value
);
383 pending_weak pe
= {name
, value
};
384 vec_safe_push (pending_weaks
, pe
);
388 /* GCC supports two #pragma directives for renaming the external
389 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
390 compatibility with the Solaris and VMS system headers. GCC also
391 has its own notation for this, __asm__("name") annotations.
393 Corner cases of these features and their interaction:
395 1) Both pragmas silently apply only to declarations with external
396 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
397 do not have this restriction.
399 2) In C++, both #pragmas silently apply only to extern "C" declarations.
400 Asm labels do not have this restriction.
402 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
403 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
404 new name is different, a warning issues and the name does not change.
406 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
407 *not* the DECL_ASSEMBLER_NAME.
409 5) If #pragma extern_prefix is in effect and a declaration occurs
410 with an __asm__ name, the #pragma extern_prefix is silently
411 ignored for that declaration.
413 6) If #pragma extern_prefix and #pragma redefine_extname apply to
414 the same declaration, whichever triggered first wins, and a warning
415 is issued. (We would like to have #pragma redefine_extname always
416 win, but it can appear either before or after the declaration, and
417 if it appears afterward, we have no way of knowing whether a modified
418 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
420 typedef struct GTY(()) pending_redefinition_d
{
423 } pending_redefinition
;
426 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
428 static void handle_pragma_redefine_extname (cpp_reader
*);
430 /* #pragma redefine_extname oldname newname */
432 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
434 tree oldname
, newname
, decls
, x
;
438 if (pragma_lex (&oldname
) != CPP_NAME
)
439 GCC_BAD ("malformed #pragma redefine_extname, ignored");
440 if (pragma_lex (&newname
) != CPP_NAME
)
441 GCC_BAD ("malformed #pragma redefine_extname, ignored");
444 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
447 for (decls
= c_linkage_bindings (oldname
);
451 if (TREE_CODE (decls
) == TREE_LIST
)
453 decl
= TREE_VALUE (decls
);
454 decls
= TREE_CHAIN (decls
);
462 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
463 && (TREE_CODE (decl
) == FUNCTION_DECL
464 || TREE_CODE (decl
) == VAR_DECL
))
467 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
469 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
470 name
= targetm
.strip_name_encoding (name
);
472 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
473 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
474 "conflict with previous rename");
477 change_decl_assembler_name (decl
, newname
);
482 /* We have to add this to the rename list even if there's already
483 a global value that doesn't meet the above criteria, because in
484 C++ "struct foo {...};" puts "foo" in the current namespace but
485 does *not* conflict with a subsequent declaration of a function
486 or variable foo. See g++.dg/other/pragma-re-2.C. */
487 add_to_renaming_pragma_list (oldname
, newname
);
490 /* This is called from here and from ia64-c.c. */
492 add_to_renaming_pragma_list (tree oldname
, tree newname
)
495 pending_redefinition
*p
;
497 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
498 if (oldname
== p
->oldname
)
500 if (p
->newname
!= newname
)
501 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
502 "conflict with previous #pragma redefine_extname");
506 pending_redefinition e
= {oldname
, newname
};
507 vec_safe_push (pending_redefine_extname
, e
);
510 /* The current prefix set by #pragma extern_prefix. */
511 GTY(()) tree pragma_extern_prefix
;
513 /* Hook from the front ends to apply the results of one of the preceding
514 pragmas that rename variables. */
517 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
520 pending_redefinition
*p
;
522 /* The renaming pragmas are only applied to declarations with
524 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
525 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
526 || !has_c_linkage (decl
))
529 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
530 but we may warn about a rename that conflicts. */
531 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
533 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
534 oldname
= targetm
.strip_name_encoding (oldname
);
536 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
537 warning (OPT_Wpragmas
, "asm declaration ignored due to "
538 "conflict with previous rename");
540 /* Take any pending redefine_extname off the list. */
541 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
542 if (DECL_NAME (decl
) == p
->oldname
)
544 /* Only warn if there is a conflict. */
545 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
546 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
547 "conflict with previous rename");
549 pending_redefine_extname
->unordered_remove (ix
);
555 /* Find out if we have a pending #pragma redefine_extname. */
556 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
557 if (DECL_NAME (decl
) == p
->oldname
)
559 tree newname
= p
->newname
;
560 pending_redefine_extname
->unordered_remove (ix
);
562 /* If we already have an asmname, #pragma redefine_extname is
563 ignored (with a warning if it conflicts). */
566 if (strcmp (TREE_STRING_POINTER (asmname
),
567 IDENTIFIER_POINTER (newname
)) != 0)
568 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
569 "conflict with __asm__ declaration");
573 /* Otherwise we use what we've got; #pragma extern_prefix is
575 return build_string (IDENTIFIER_LENGTH (newname
),
576 IDENTIFIER_POINTER (newname
));
579 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
583 /* If #pragma extern_prefix is in effect, apply it. */
584 if (pragma_extern_prefix
)
586 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
587 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
589 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
590 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
592 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
594 memcpy (newname
, prefix
, plen
);
595 memcpy (newname
+ plen
, id
, ilen
+ 1);
597 return build_string (plen
+ ilen
, newname
);
605 static void handle_pragma_visibility (cpp_reader
*);
607 static vec
<int> visstack
;
609 /* Push the visibility indicated by STR onto the top of the #pragma
610 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
611 C++ namespace with visibility attribute and 2 for C++ builtin
612 ABI namespace. push_visibility/pop_visibility calls must have
613 matching KIND, it is not allowed to push visibility using one
614 KIND and pop using a different one. */
617 push_visibility (const char *str
, int kind
)
619 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
620 if (!strcmp (str
, "default"))
621 default_visibility
= VISIBILITY_DEFAULT
;
622 else if (!strcmp (str
, "internal"))
623 default_visibility
= VISIBILITY_INTERNAL
;
624 else if (!strcmp (str
, "hidden"))
625 default_visibility
= VISIBILITY_HIDDEN
;
626 else if (!strcmp (str
, "protected"))
627 default_visibility
= VISIBILITY_PROTECTED
;
629 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
630 visibility_options
.inpragma
= 1;
633 /* Pop a level of the #pragma visibility stack. Return true if
637 pop_visibility (int kind
)
639 if (!visstack
.length ())
641 if ((visstack
.last () >> 8) != kind
)
644 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
645 visibility_options
.inpragma
646 = visstack
.length () != 0;
650 /* Sets the default visibility for symbols to something other than that
651 specified on the command line. */
654 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
656 /* Form is #pragma GCC visibility push(hidden)|pop */
658 enum cpp_ttype token
;
659 enum { bad
, push
, pop
} action
= bad
;
661 token
= pragma_lex (&x
);
662 if (token
== CPP_NAME
)
664 const char *op
= IDENTIFIER_POINTER (x
);
665 if (!strcmp (op
, "push"))
667 else if (!strcmp (op
, "pop"))
671 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
676 if (! pop_visibility (0))
677 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
681 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
682 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
683 token
= pragma_lex (&x
);
684 if (token
!= CPP_NAME
)
685 GCC_BAD ("malformed #pragma GCC visibility push");
687 push_visibility (IDENTIFIER_POINTER (x
), 0);
688 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
689 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
692 if (pragma_lex (&x
) != CPP_EOF
)
693 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
697 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
699 const char *kind_string
, *option_string
;
700 unsigned int option_index
;
701 enum cpp_ttype token
;
704 struct cl_option_handlers handlers
;
706 token
= pragma_lex (&x
);
707 if (token
!= CPP_NAME
)
708 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
709 kind_string
= IDENTIFIER_POINTER (x
);
710 if (strcmp (kind_string
, "error") == 0)
712 else if (strcmp (kind_string
, "warning") == 0)
714 else if (strcmp (kind_string
, "ignored") == 0)
716 else if (strcmp (kind_string
, "push") == 0)
718 diagnostic_push_diagnostics (global_dc
, input_location
);
721 else if (strcmp (kind_string
, "pop") == 0)
723 diagnostic_pop_diagnostics (global_dc
, input_location
);
727 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
729 token
= pragma_lex (&x
);
730 if (token
!= CPP_STRING
)
731 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
732 option_string
= TREE_STRING_POINTER (x
);
733 set_default_handlers (&handlers
);
734 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
735 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
737 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
738 input_location
, c_family_lang_mask
, &handlers
,
739 &global_options
, &global_options_set
,
743 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
746 /* Parse #pragma GCC target (xxx) to set target specific options. */
748 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
750 enum cpp_ttype token
;
752 bool close_paren_needed_p
= false;
756 error ("#pragma GCC option is not allowed inside functions");
760 token
= pragma_lex (&x
);
761 if (token
== CPP_OPEN_PAREN
)
763 close_paren_needed_p
= true;
764 token
= pragma_lex (&x
);
767 if (token
!= CPP_STRING
)
769 GCC_BAD ("%<#pragma GCC option%> is not a string");
773 /* Strings are user options. */
776 tree args
= NULL_TREE
;
780 /* Build up the strings now as a tree linked list. Skip empty
782 if (TREE_STRING_LENGTH (x
) > 0)
783 args
= tree_cons (NULL_TREE
, x
, args
);
785 token
= pragma_lex (&x
);
786 while (token
== CPP_COMMA
)
787 token
= pragma_lex (&x
);
789 while (token
== CPP_STRING
);
791 if (close_paren_needed_p
)
793 if (token
== CPP_CLOSE_PAREN
)
794 token
= pragma_lex (&x
);
796 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
797 "not have a final %<)%>");
800 if (token
!= CPP_EOF
)
802 error ("#pragma GCC target string... is badly formed");
806 /* put arguments in the order the user typed them. */
807 args
= nreverse (args
);
809 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
810 current_target_pragma
= args
;
814 /* Handle #pragma GCC optimize to set optimization options. */
816 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
818 enum cpp_ttype token
;
820 bool close_paren_needed_p
= false;
821 tree optimization_previous_node
= optimization_current_node
;
825 error ("#pragma GCC optimize is not allowed inside functions");
829 token
= pragma_lex (&x
);
830 if (token
== CPP_OPEN_PAREN
)
832 close_paren_needed_p
= true;
833 token
= pragma_lex (&x
);
836 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
838 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
842 /* Strings/numbers are user options. */
845 tree args
= NULL_TREE
;
849 /* Build up the numbers/strings now as a list. */
850 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
851 args
= tree_cons (NULL_TREE
, x
, args
);
853 token
= pragma_lex (&x
);
854 while (token
== CPP_COMMA
)
855 token
= pragma_lex (&x
);
857 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
859 if (close_paren_needed_p
)
861 if (token
== CPP_CLOSE_PAREN
)
862 token
= pragma_lex (&x
);
864 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
865 "not have a final %<)%>");
868 if (token
!= CPP_EOF
)
870 error ("#pragma GCC optimize string... is badly formed");
874 /* put arguments in the order the user typed them. */
875 args
= nreverse (args
);
877 parse_optimize_options (args
, false);
878 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
879 optimization_current_node
= build_optimization_node (&global_options
);
880 c_cpp_builtins_optimize_pragma (parse_in
,
881 optimization_previous_node
,
882 optimization_current_node
);
886 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
887 both the binary representation of the options and the TREE_LIST of
888 strings that will be added to the function's attribute list. */
889 typedef struct GTY(()) opt_stack
{
890 struct opt_stack
*prev
;
893 tree optimize_binary
;
894 tree optimize_strings
;
897 static GTY(()) struct opt_stack
* options_stack
;
899 /* Handle #pragma GCC push_options to save the current target and optimization
903 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
905 enum cpp_ttype token
;
909 token
= pragma_lex (&x
);
910 if (token
!= CPP_EOF
)
912 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
916 p
= ggc_alloc_opt_stack ();
917 p
->prev
= options_stack
;
920 /* Save optimization and target flags in binary format. */
921 p
->optimize_binary
= build_optimization_node (&global_options
);
922 p
->target_binary
= build_target_option_node (&global_options
);
924 /* Save optimization and target flags in string list format. */
925 p
->optimize_strings
= copy_list (current_optimize_pragma
);
926 p
->target_strings
= copy_list (current_target_pragma
);
929 /* Handle #pragma GCC pop_options to restore the current target and
930 optimization options from a previous push_options. */
933 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
935 enum cpp_ttype token
;
939 token
= pragma_lex (&x
);
940 if (token
!= CPP_EOF
)
942 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
948 warning (OPT_Wpragmas
,
949 "%<#pragma GCC pop_options%> without a corresponding "
950 "%<#pragma GCC push_options%>");
955 options_stack
= p
->prev
;
957 if (p
->target_binary
!= target_option_current_node
)
959 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
960 target_option_current_node
= p
->target_binary
;
963 if (p
->optimize_binary
!= optimization_current_node
)
965 tree old_optimize
= optimization_current_node
;
966 cl_optimization_restore (&global_options
,
967 TREE_OPTIMIZATION (p
->optimize_binary
));
968 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
970 optimization_current_node
= p
->optimize_binary
;
973 current_target_pragma
= p
->target_strings
;
974 current_optimize_pragma
= p
->optimize_strings
;
977 /* Handle #pragma GCC reset_options to restore the current target and
978 optimization options to the original options used on the command line. */
981 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
983 enum cpp_ttype token
;
985 tree new_optimize
= optimization_default_node
;
986 tree new_target
= target_option_default_node
;
988 token
= pragma_lex (&x
);
989 if (token
!= CPP_EOF
)
991 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
995 if (new_target
!= target_option_current_node
)
997 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
998 target_option_current_node
= new_target
;
1001 if (new_optimize
!= optimization_current_node
)
1003 tree old_optimize
= optimization_current_node
;
1004 cl_optimization_restore (&global_options
,
1005 TREE_OPTIMIZATION (new_optimize
));
1006 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1007 optimization_current_node
= new_optimize
;
1010 current_target_pragma
= NULL_TREE
;
1011 current_optimize_pragma
= NULL_TREE
;
1014 /* Print a plain user-specified message. */
1017 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1019 enum cpp_ttype token
;
1020 tree x
, message
= 0;
1022 token
= pragma_lex (&x
);
1023 if (token
== CPP_OPEN_PAREN
)
1025 token
= pragma_lex (&x
);
1026 if (token
== CPP_STRING
)
1029 GCC_BAD ("expected a string after %<#pragma message%>");
1030 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1031 GCC_BAD ("malformed %<#pragma message%>, ignored");
1033 else if (token
== CPP_STRING
)
1036 GCC_BAD ("expected a string after %<#pragma message%>");
1038 gcc_assert (message
);
1040 if (pragma_lex (&x
) != CPP_EOF
)
1041 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1043 if (TREE_STRING_LENGTH (message
) > 1)
1044 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1047 /* Mark whether the current location is valid for a STDC pragma. */
1049 static bool valid_location_for_stdc_pragma
;
1052 mark_valid_location_for_stdc_pragma (bool flag
)
1054 valid_location_for_stdc_pragma
= flag
;
1057 /* Return true if the current location is valid for a STDC pragma. */
1060 valid_location_for_stdc_pragma_p (void)
1062 return valid_location_for_stdc_pragma
;
1065 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1067 /* A STDC pragma must appear outside of external declarations or
1068 preceding all explicit declarations and statements inside a compound
1069 statement; its behavior is undefined if used in any other context.
1070 It takes a switch of ON, OFF, or DEFAULT. */
1072 static enum pragma_switch_t
1073 handle_stdc_pragma (const char *pname
)
1077 enum pragma_switch_t ret
;
1079 if (!valid_location_for_stdc_pragma_p ())
1081 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1086 if (pragma_lex (&t
) != CPP_NAME
)
1088 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1092 arg
= IDENTIFIER_POINTER (t
);
1094 if (!strcmp (arg
, "ON"))
1096 else if (!strcmp (arg
, "OFF"))
1098 else if (!strcmp (arg
, "DEFAULT"))
1099 ret
= PRAGMA_DEFAULT
;
1102 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1106 if (pragma_lex (&t
) != CPP_EOF
)
1108 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1115 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1116 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1117 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1120 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1122 if (c_dialect_cxx ())
1124 if (warn_unknown_pragmas
> in_system_header
)
1125 warning (OPT_Wunknown_pragmas
,
1126 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1131 if (!targetm
.decimal_float_supported_p ())
1133 if (warn_unknown_pragmas
> in_system_header
)
1134 warning (OPT_Wunknown_pragmas
,
1135 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1140 pedwarn (input_location
, OPT_Wpedantic
,
1141 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1143 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1146 set_float_const_decimal64 ();
1149 case PRAGMA_DEFAULT
:
1150 clear_float_const_decimal64 ();
1157 /* A vector of registered pragma callbacks, which is never freed. */
1159 static vec
<internal_pragma_handler
> registered_pragmas
;
1168 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1170 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1171 static const struct omp_pragma_def omp_pragmas
[] = {
1172 { "atomic", PRAGMA_OMP_ATOMIC
},
1173 { "barrier", PRAGMA_OMP_BARRIER
},
1174 { "cancel", PRAGMA_OMP_CANCEL
},
1175 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1176 { "critical", PRAGMA_OMP_CRITICAL
},
1177 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1178 { "flush", PRAGMA_OMP_FLUSH
},
1179 { "master", PRAGMA_OMP_MASTER
},
1180 { "ordered", PRAGMA_OMP_ORDERED
},
1181 { "section", PRAGMA_OMP_SECTION
},
1182 { "sections", PRAGMA_OMP_SECTIONS
},
1183 { "single", PRAGMA_OMP_SINGLE
},
1184 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1185 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1186 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1187 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1189 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1190 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1191 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1192 { "for", PRAGMA_OMP_FOR
},
1193 { "parallel", PRAGMA_OMP_PARALLEL
},
1194 { "simd", PRAGMA_OMP_SIMD
},
1195 { "target", PRAGMA_OMP_TARGET
},
1196 { "task", PRAGMA_OMP_TASK
},
1197 { "teams", PRAGMA_OMP_TEAMS
},
1201 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1203 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1204 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1205 / sizeof (*omp_pragmas
);
1208 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1209 if (omp_pragmas
[i
].id
== id
)
1212 *name
= omp_pragmas
[i
].name
;
1216 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1217 if (omp_pragmas_simd
[i
].id
== id
)
1220 *name
= omp_pragmas_simd
[i
].name
;
1224 if (id
>= PRAGMA_FIRST_EXTERNAL
1225 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1227 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1228 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1235 /* Front-end wrappers for pragma registration to avoid dragging
1236 cpplib.h in almost everywhere. */
1239 c_register_pragma_1 (const char *space
, const char *name
,
1240 internal_pragma_handler ihandler
, bool allow_expansion
)
1244 if (flag_preprocess_only
)
1246 pragma_ns_name ns_name
;
1248 if (!allow_expansion
)
1251 ns_name
.space
= space
;
1252 ns_name
.name
= name
;
1253 registered_pp_pragmas
.safe_push (ns_name
);
1254 id
= registered_pp_pragmas
.length ();
1255 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1259 registered_pragmas
.safe_push (ihandler
);
1260 id
= registered_pragmas
.length ();
1261 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1263 /* The C++ front end allocates 6 bits in cp_token; the C front end
1264 allocates 7 bits in c_token. At present this is sufficient. */
1265 gcc_assert (id
< 64);
1268 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1269 allow_expansion
, false);
1272 /* Register a C pragma handler, using a space and a name. It disallows pragma
1273 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1275 c_register_pragma (const char *space
, const char *name
,
1276 pragma_handler_1arg handler
)
1278 internal_pragma_handler ihandler
;
1280 ihandler
.handler
.handler_1arg
= handler
;
1281 ihandler
.extra_data
= false;
1282 ihandler
.data
= NULL
;
1283 c_register_pragma_1 (space
, name
, ihandler
, false);
1286 /* Register a C pragma handler, using a space and a name, it also carries an
1287 extra data field which can be used by the handler. It disallows pragma
1288 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1291 c_register_pragma_with_data (const char *space
, const char *name
,
1292 pragma_handler_2arg handler
, void * data
)
1294 internal_pragma_handler ihandler
;
1296 ihandler
.handler
.handler_2arg
= handler
;
1297 ihandler
.extra_data
= true;
1298 ihandler
.data
= data
;
1299 c_register_pragma_1 (space
, name
, ihandler
, false);
1302 /* Register a C pragma handler, using a space and a name. It allows pragma
1303 expansion as in the following example:
1306 #pragma count (NUMBER)
1308 Name expansion is still disallowed. */
1310 c_register_pragma_with_expansion (const char *space
, const char *name
,
1311 pragma_handler_1arg handler
)
1313 internal_pragma_handler ihandler
;
1315 ihandler
.handler
.handler_1arg
= handler
;
1316 ihandler
.extra_data
= false;
1317 ihandler
.data
= NULL
;
1318 c_register_pragma_1 (space
, name
, ihandler
, true);
1321 /* Register a C pragma handler, using a space and a name, it also carries an
1322 extra data field which can be used by the handler. It allows pragma
1323 expansion as in the following example:
1326 #pragma count (NUMBER)
1328 Name expansion is still disallowed. */
1330 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1331 pragma_handler_2arg handler
,
1334 internal_pragma_handler ihandler
;
1336 ihandler
.handler
.handler_2arg
= handler
;
1337 ihandler
.extra_data
= true;
1338 ihandler
.data
= data
;
1339 c_register_pragma_1 (space
, name
, ihandler
, true);
1343 c_invoke_pragma_handler (unsigned int id
)
1345 internal_pragma_handler
*ihandler
;
1346 pragma_handler_1arg handler_1arg
;
1347 pragma_handler_2arg handler_2arg
;
1349 id
-= PRAGMA_FIRST_EXTERNAL
;
1350 ihandler
= ®istered_pragmas
[id
];
1351 if (ihandler
->extra_data
)
1353 handler_2arg
= ihandler
->handler
.handler_2arg
;
1354 handler_2arg (parse_in
, ihandler
->data
);
1358 handler_1arg
= ihandler
->handler
.handler_1arg
;
1359 handler_1arg (parse_in
);
1363 /* Set up front-end pragmas. */
1369 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1372 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1373 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1374 omp_pragmas
[i
].id
, true, true);
1376 if (flag_openmp
|| flag_openmp_simd
)
1378 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1379 / sizeof (*omp_pragmas
);
1382 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1383 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1384 omp_pragmas_simd
[i
].id
, true, true);
1387 if (flag_enable_cilkplus
&& !flag_preprocess_only
)
1388 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1391 if (!flag_preprocess_only
)
1392 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1393 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1395 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1397 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1398 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1400 c_register_pragma (0, "pack", handle_pragma_pack
);
1402 c_register_pragma (0, "weak", handle_pragma_weak
);
1403 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1405 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1406 c_register_pragma ("GCC", "target", handle_pragma_target
);
1407 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1408 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1409 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1410 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1412 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1413 handle_pragma_float_const_decimal64
);
1415 c_register_pragma_with_expansion (0, "redefine_extname",
1416 handle_pragma_redefine_extname
);
1418 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1420 #ifdef REGISTER_TARGET_PRAGMAS
1421 REGISTER_TARGET_PRAGMAS ();
1424 /* Allow plugins to register their own pragmas. */
1425 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1428 #include "gt-c-family-c-pragma.h"