1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2014 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 "function.h" /* For cfun. FIXME: Does the parser know
29 when it is inside a function, so that
30 we don't have to look at cfun? */
35 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
36 this not a target hook?). */
39 #include "diagnostic.h"
44 #define GCC_BAD(gmsgid) \
45 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
46 #define GCC_BAD2(gmsgid, arg) \
47 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
49 typedef struct GTY(()) align_stack
{
52 struct align_stack
* prev
;
55 static GTY(()) struct align_stack
* alignment_stack
;
57 static void handle_pragma_pack (cpp_reader
*);
59 /* If we have a "global" #pragma pack(<n>) in effect when the first
60 #pragma pack(push,<n>) is encountered, this stores the value of
61 maximum_field_alignment in effect. When the final pop_alignment()
62 happens, we restore the value to this, not to a value of 0 for
63 maximum_field_alignment. Value is in bits. */
64 static int default_alignment
;
65 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
66 ? &default_alignment \
67 : &alignment_stack->alignment) = (ALIGN))
69 static void push_alignment (int, tree
);
70 static void pop_alignment (tree
);
72 /* Push an alignment value onto the stack. */
74 push_alignment (int alignment
, tree id
)
76 align_stack
* entry
= ggc_alloc
<align_stack
> ();
78 entry
->alignment
= alignment
;
80 entry
->prev
= alignment_stack
;
82 /* The current value of maximum_field_alignment is not necessarily
83 0 since there may be a #pragma pack(<n>) in effect; remember it
84 so that we can restore it after the final #pragma pop(). */
85 if (alignment_stack
== NULL
)
86 default_alignment
= maximum_field_alignment
;
88 alignment_stack
= entry
;
90 maximum_field_alignment
= alignment
;
93 /* Undo a push of an alignment onto the stack. */
95 pop_alignment (tree id
)
99 if (alignment_stack
== NULL
)
100 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
102 /* If we got an identifier, strip away everything above the target
103 entry so that the next step will restore the state just below it. */
106 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
109 alignment_stack
= entry
;
113 warning (OPT_Wpragmas
, "\
114 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
118 entry
= alignment_stack
->prev
;
120 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
122 alignment_stack
= entry
;
129 #pragma pack (push, N)
130 #pragma pack (push, ID)
131 #pragma pack (push, ID, N)
133 #pragma pack (pop, ID) */
135 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
139 enum cpp_ttype token
;
140 enum { set
, push
, pop
} action
;
142 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
143 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
145 token
= pragma_lex (&x
);
146 if (token
== CPP_CLOSE_PAREN
)
149 align
= initial_max_fld_align
;
151 else if (token
== CPP_NUMBER
)
153 if (TREE_CODE (x
) != INTEGER_CST
)
154 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
155 align
= TREE_INT_CST_LOW (x
);
157 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
158 GCC_BAD ("malformed %<#pragma pack%> - ignored");
160 else if (token
== CPP_NAME
)
162 #define GCC_BAD_ACTION do { if (action != pop) \
163 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
165 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
168 const char *op
= IDENTIFIER_POINTER (x
);
169 if (!strcmp (op
, "push"))
171 else if (!strcmp (op
, "pop"))
174 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
176 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
178 token
= pragma_lex (&x
);
179 if (token
== CPP_NAME
&& id
== 0)
183 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
185 if (TREE_CODE (x
) != INTEGER_CST
)
186 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
187 align
= TREE_INT_CST_LOW (x
);
195 if (token
!= CPP_CLOSE_PAREN
)
197 #undef GCC_BAD_ACTION
200 GCC_BAD ("malformed %<#pragma pack%> - ignored");
202 if (pragma_lex (&x
) != CPP_EOF
)
203 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
205 if (flag_pack_struct
)
206 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
217 align
*= BITS_PER_UNIT
;
222 align
= maximum_field_alignment
;
226 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
231 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
232 case push
: push_alignment (align
, id
); break;
233 case pop
: pop_alignment (id
); break;
237 typedef struct GTY(()) pending_weak_d
244 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
246 static void apply_pragma_weak (tree
, tree
);
247 static void handle_pragma_weak (cpp_reader
*);
250 apply_pragma_weak (tree decl
, tree value
)
254 value
= build_string (IDENTIFIER_LENGTH (value
),
255 IDENTIFIER_POINTER (value
));
256 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
257 build_tree_list (NULL
, value
)),
261 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
262 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
263 && DECL_ASSEMBLER_NAME_SET_P (decl
)
264 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
265 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
266 "results in unspecified behavior", decl
);
272 maybe_apply_pragma_weak (tree decl
)
278 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
280 /* No weak symbols pending, take the short-cut. */
281 if (vec_safe_is_empty (pending_weaks
))
283 /* If it's not visible outside this file, it doesn't matter whether
285 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
287 /* If it's not a function or a variable, it can't be weak.
288 FIXME: what kinds of things are visible outside this file but
289 aren't functions or variables? Should this be an assert instead? */
290 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
293 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
294 id
= DECL_ASSEMBLER_NAME (decl
);
297 id
= DECL_ASSEMBLER_NAME (decl
);
298 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
301 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
304 apply_pragma_weak (decl
, pe
->value
);
305 pending_weaks
->unordered_remove (i
);
310 /* Process all "#pragma weak A = B" directives where we have not seen
313 maybe_apply_pending_pragma_weaks (void)
315 tree alias_id
, id
, decl
;
320 if (vec_safe_is_empty (pending_weaks
))
323 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
331 target
= symtab_node::get_for_asmname (id
);
332 decl
= build_decl (UNKNOWN_LOCATION
,
333 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
334 alias_id
, default_function_type
);
336 DECL_ARTIFICIAL (decl
) = 1;
337 TREE_PUBLIC (decl
) = 1;
338 DECL_WEAK (decl
) = 1;
339 if (TREE_CODE (decl
) == VAR_DECL
)
340 TREE_STATIC (decl
) = 1;
343 error ("%q+D aliased to undefined symbol %qE",
348 assemble_alias (decl
, id
);
352 /* #pragma weak name [= value] */
354 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
356 tree name
, value
, x
, decl
;
361 if (pragma_lex (&name
) != CPP_NAME
)
362 GCC_BAD ("malformed #pragma weak, ignored");
366 if (pragma_lex (&value
) != CPP_NAME
)
367 GCC_BAD ("malformed #pragma weak, ignored");
371 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
373 decl
= identifier_global_value (name
);
374 if (decl
&& DECL_P (decl
))
376 apply_pragma_weak (decl
, value
);
379 DECL_EXTERNAL (decl
) = 0;
380 if (TREE_CODE (decl
) == VAR_DECL
)
381 TREE_STATIC (decl
) = 1;
382 assemble_alias (decl
, value
);
387 pending_weak pe
= {name
, value
};
388 vec_safe_push (pending_weaks
, pe
);
392 /* GCC supports two #pragma directives for renaming the external
393 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
394 compatibility with the Solaris and VMS system headers. GCC also
395 has its own notation for this, __asm__("name") annotations.
397 Corner cases of these features and their interaction:
399 1) Both pragmas silently apply only to declarations with external
400 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
401 do not have this restriction.
403 2) In C++, both #pragmas silently apply only to extern "C" declarations.
404 Asm labels do not have this restriction.
406 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
407 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
408 new name is different, a warning issues and the name does not change.
410 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
411 *not* the DECL_ASSEMBLER_NAME.
413 5) If #pragma extern_prefix is in effect and a declaration occurs
414 with an __asm__ name, the #pragma extern_prefix is silently
415 ignored for that declaration.
417 6) If #pragma extern_prefix and #pragma redefine_extname apply to
418 the same declaration, whichever triggered first wins, and a warning
419 is issued. (We would like to have #pragma redefine_extname always
420 win, but it can appear either before or after the declaration, and
421 if it appears afterward, we have no way of knowing whether a modified
422 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
424 typedef struct GTY(()) pending_redefinition_d
{
427 } pending_redefinition
;
430 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
432 static void handle_pragma_redefine_extname (cpp_reader
*);
434 /* #pragma redefine_extname oldname newname */
436 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
438 tree oldname
, newname
, decls
, x
;
442 if (pragma_lex (&oldname
) != CPP_NAME
)
443 GCC_BAD ("malformed #pragma redefine_extname, ignored");
444 if (pragma_lex (&newname
) != CPP_NAME
)
445 GCC_BAD ("malformed #pragma redefine_extname, ignored");
448 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
451 for (decls
= c_linkage_bindings (oldname
);
455 if (TREE_CODE (decls
) == TREE_LIST
)
457 decl
= TREE_VALUE (decls
);
458 decls
= TREE_CHAIN (decls
);
466 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
467 && (TREE_CODE (decl
) == FUNCTION_DECL
468 || TREE_CODE (decl
) == VAR_DECL
))
471 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
473 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
474 name
= targetm
.strip_name_encoding (name
);
476 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
477 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
478 "conflict with previous rename");
481 symtab
->change_decl_assembler_name (decl
, newname
);
486 /* We have to add this to the rename list even if there's already
487 a global value that doesn't meet the above criteria, because in
488 C++ "struct foo {...};" puts "foo" in the current namespace but
489 does *not* conflict with a subsequent declaration of a function
490 or variable foo. See g++.dg/other/pragma-re-2.C. */
491 add_to_renaming_pragma_list (oldname
, newname
);
494 /* This is called from here and from ia64-c.c. */
496 add_to_renaming_pragma_list (tree oldname
, tree newname
)
499 pending_redefinition
*p
;
501 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
502 if (oldname
== p
->oldname
)
504 if (p
->newname
!= newname
)
505 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
506 "conflict with previous #pragma redefine_extname");
510 pending_redefinition e
= {oldname
, newname
};
511 vec_safe_push (pending_redefine_extname
, e
);
514 /* The current prefix set by #pragma extern_prefix. */
515 GTY(()) tree pragma_extern_prefix
;
517 /* Hook from the front ends to apply the results of one of the preceding
518 pragmas that rename variables. */
521 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
524 pending_redefinition
*p
;
526 /* The renaming pragmas are only applied to declarations with
528 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
529 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
530 || !has_c_linkage (decl
))
533 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
534 but we may warn about a rename that conflicts. */
535 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
537 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
538 oldname
= targetm
.strip_name_encoding (oldname
);
540 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
541 warning (OPT_Wpragmas
, "asm declaration ignored due to "
542 "conflict with previous rename");
544 /* Take any pending redefine_extname off the list. */
545 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
546 if (DECL_NAME (decl
) == p
->oldname
)
548 /* Only warn if there is a conflict. */
549 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
550 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
551 "conflict with previous rename");
553 pending_redefine_extname
->unordered_remove (ix
);
559 /* Find out if we have a pending #pragma redefine_extname. */
560 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
561 if (DECL_NAME (decl
) == p
->oldname
)
563 tree newname
= p
->newname
;
564 pending_redefine_extname
->unordered_remove (ix
);
566 /* If we already have an asmname, #pragma redefine_extname is
567 ignored (with a warning if it conflicts). */
570 if (strcmp (TREE_STRING_POINTER (asmname
),
571 IDENTIFIER_POINTER (newname
)) != 0)
572 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
573 "conflict with __asm__ declaration");
577 /* Otherwise we use what we've got; #pragma extern_prefix is
579 return build_string (IDENTIFIER_LENGTH (newname
),
580 IDENTIFIER_POINTER (newname
));
583 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
587 /* If #pragma extern_prefix is in effect, apply it. */
588 if (pragma_extern_prefix
)
590 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
591 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
593 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
594 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
596 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
598 memcpy (newname
, prefix
, plen
);
599 memcpy (newname
+ plen
, id
, ilen
+ 1);
601 return build_string (plen
+ ilen
, newname
);
609 static void handle_pragma_visibility (cpp_reader
*);
611 static vec
<int> visstack
;
613 /* Push the visibility indicated by STR onto the top of the #pragma
614 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
615 C++ namespace with visibility attribute and 2 for C++ builtin
616 ABI namespace. push_visibility/pop_visibility calls must have
617 matching KIND, it is not allowed to push visibility using one
618 KIND and pop using a different one. */
621 push_visibility (const char *str
, int kind
)
623 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
624 if (!strcmp (str
, "default"))
625 default_visibility
= VISIBILITY_DEFAULT
;
626 else if (!strcmp (str
, "internal"))
627 default_visibility
= VISIBILITY_INTERNAL
;
628 else if (!strcmp (str
, "hidden"))
629 default_visibility
= VISIBILITY_HIDDEN
;
630 else if (!strcmp (str
, "protected"))
631 default_visibility
= VISIBILITY_PROTECTED
;
633 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
634 visibility_options
.inpragma
= 1;
637 /* Pop a level of the #pragma visibility stack. Return true if
641 pop_visibility (int kind
)
643 if (!visstack
.length ())
645 if ((visstack
.last () >> 8) != kind
)
648 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
649 visibility_options
.inpragma
650 = visstack
.length () != 0;
654 /* Sets the default visibility for symbols to something other than that
655 specified on the command line. */
658 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
660 /* Form is #pragma GCC visibility push(hidden)|pop */
662 enum cpp_ttype token
;
663 enum { bad
, push
, pop
} action
= bad
;
665 token
= pragma_lex (&x
);
666 if (token
== CPP_NAME
)
668 const char *op
= IDENTIFIER_POINTER (x
);
669 if (!strcmp (op
, "push"))
671 else if (!strcmp (op
, "pop"))
675 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
680 if (! pop_visibility (0))
681 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
685 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
686 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
687 token
= pragma_lex (&x
);
688 if (token
!= CPP_NAME
)
689 GCC_BAD ("malformed #pragma GCC visibility push");
691 push_visibility (IDENTIFIER_POINTER (x
), 0);
692 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
693 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
696 if (pragma_lex (&x
) != CPP_EOF
)
697 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
701 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
703 const char *kind_string
, *option_string
;
704 unsigned int option_index
;
705 enum cpp_ttype token
;
708 struct cl_option_handlers handlers
;
710 token
= pragma_lex (&x
);
711 if (token
!= CPP_NAME
)
712 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
713 kind_string
= IDENTIFIER_POINTER (x
);
714 if (strcmp (kind_string
, "error") == 0)
716 else if (strcmp (kind_string
, "warning") == 0)
718 else if (strcmp (kind_string
, "ignored") == 0)
720 else if (strcmp (kind_string
, "push") == 0)
722 diagnostic_push_diagnostics (global_dc
, input_location
);
725 else if (strcmp (kind_string
, "pop") == 0)
727 diagnostic_pop_diagnostics (global_dc
, input_location
);
731 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
733 token
= pragma_lex (&x
);
734 if (token
!= CPP_STRING
)
735 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
736 option_string
= TREE_STRING_POINTER (x
);
737 set_default_handlers (&handlers
);
738 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
739 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
741 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
742 input_location
, c_family_lang_mask
, &handlers
,
743 &global_options
, &global_options_set
,
747 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
750 /* Parse #pragma GCC target (xxx) to set target specific options. */
752 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
754 enum cpp_ttype token
;
756 bool close_paren_needed_p
= false;
760 error ("#pragma GCC option is not allowed inside functions");
764 token
= pragma_lex (&x
);
765 if (token
== CPP_OPEN_PAREN
)
767 close_paren_needed_p
= true;
768 token
= pragma_lex (&x
);
771 if (token
!= CPP_STRING
)
773 GCC_BAD ("%<#pragma GCC option%> is not a string");
777 /* Strings are user options. */
780 tree args
= NULL_TREE
;
784 /* Build up the strings now as a tree linked list. Skip empty
786 if (TREE_STRING_LENGTH (x
) > 0)
787 args
= tree_cons (NULL_TREE
, x
, args
);
789 token
= pragma_lex (&x
);
790 while (token
== CPP_COMMA
)
791 token
= pragma_lex (&x
);
793 while (token
== CPP_STRING
);
795 if (close_paren_needed_p
)
797 if (token
== CPP_CLOSE_PAREN
)
798 token
= pragma_lex (&x
);
800 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
801 "not have a final %<)%>");
804 if (token
!= CPP_EOF
)
806 error ("#pragma GCC target string... is badly formed");
810 /* put arguments in the order the user typed them. */
811 args
= nreverse (args
);
813 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
814 current_target_pragma
= args
;
818 /* Handle #pragma GCC optimize to set optimization options. */
820 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
822 enum cpp_ttype token
;
824 bool close_paren_needed_p
= false;
825 tree optimization_previous_node
= optimization_current_node
;
829 error ("#pragma GCC optimize is not allowed inside functions");
833 token
= pragma_lex (&x
);
834 if (token
== CPP_OPEN_PAREN
)
836 close_paren_needed_p
= true;
837 token
= pragma_lex (&x
);
840 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
842 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
846 /* Strings/numbers are user options. */
849 tree args
= NULL_TREE
;
853 /* Build up the numbers/strings now as a list. */
854 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
855 args
= tree_cons (NULL_TREE
, x
, args
);
857 token
= pragma_lex (&x
);
858 while (token
== CPP_COMMA
)
859 token
= pragma_lex (&x
);
861 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
863 if (close_paren_needed_p
)
865 if (token
== CPP_CLOSE_PAREN
)
866 token
= pragma_lex (&x
);
868 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
869 "not have a final %<)%>");
872 if (token
!= CPP_EOF
)
874 error ("#pragma GCC optimize string... is badly formed");
878 /* put arguments in the order the user typed them. */
879 args
= nreverse (args
);
881 parse_optimize_options (args
, false);
882 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
883 optimization_current_node
= build_optimization_node (&global_options
);
884 c_cpp_builtins_optimize_pragma (parse_in
,
885 optimization_previous_node
,
886 optimization_current_node
);
890 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
891 both the binary representation of the options and the TREE_LIST of
892 strings that will be added to the function's attribute list. */
893 typedef struct GTY(()) opt_stack
{
894 struct opt_stack
*prev
;
897 tree optimize_binary
;
898 tree optimize_strings
;
901 static GTY(()) struct opt_stack
* options_stack
;
903 /* Handle #pragma GCC push_options to save the current target and optimization
907 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
909 enum cpp_ttype token
;
912 token
= pragma_lex (&x
);
913 if (token
!= CPP_EOF
)
915 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
919 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
920 p
->prev
= options_stack
;
923 /* Save optimization and target flags in binary format. */
924 p
->optimize_binary
= build_optimization_node (&global_options
);
925 p
->target_binary
= build_target_option_node (&global_options
);
927 /* Save optimization and target flags in string list format. */
928 p
->optimize_strings
= copy_list (current_optimize_pragma
);
929 p
->target_strings
= copy_list (current_target_pragma
);
932 /* Handle #pragma GCC pop_options to restore the current target and
933 optimization options from a previous push_options. */
936 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
938 enum cpp_ttype token
;
942 token
= pragma_lex (&x
);
943 if (token
!= CPP_EOF
)
945 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
951 warning (OPT_Wpragmas
,
952 "%<#pragma GCC pop_options%> without a corresponding "
953 "%<#pragma GCC push_options%>");
958 options_stack
= p
->prev
;
960 if (p
->target_binary
!= target_option_current_node
)
962 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
963 target_option_current_node
= p
->target_binary
;
966 if (p
->optimize_binary
!= optimization_current_node
)
968 tree old_optimize
= optimization_current_node
;
969 cl_optimization_restore (&global_options
,
970 TREE_OPTIMIZATION (p
->optimize_binary
));
971 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
973 optimization_current_node
= p
->optimize_binary
;
976 current_target_pragma
= p
->target_strings
;
977 current_optimize_pragma
= p
->optimize_strings
;
980 /* Handle #pragma GCC reset_options to restore the current target and
981 optimization options to the original options used on the command line. */
984 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
986 enum cpp_ttype token
;
988 tree new_optimize
= optimization_default_node
;
989 tree new_target
= target_option_default_node
;
991 token
= pragma_lex (&x
);
992 if (token
!= CPP_EOF
)
994 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
998 if (new_target
!= target_option_current_node
)
1000 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1001 target_option_current_node
= new_target
;
1004 if (new_optimize
!= optimization_current_node
)
1006 tree old_optimize
= optimization_current_node
;
1007 cl_optimization_restore (&global_options
,
1008 TREE_OPTIMIZATION (new_optimize
));
1009 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1010 optimization_current_node
= new_optimize
;
1013 current_target_pragma
= NULL_TREE
;
1014 current_optimize_pragma
= NULL_TREE
;
1017 /* Print a plain user-specified message. */
1020 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1022 enum cpp_ttype token
;
1023 tree x
, message
= 0;
1025 token
= pragma_lex (&x
);
1026 if (token
== CPP_OPEN_PAREN
)
1028 token
= pragma_lex (&x
);
1029 if (token
== CPP_STRING
)
1032 GCC_BAD ("expected a string after %<#pragma message%>");
1033 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1034 GCC_BAD ("malformed %<#pragma message%>, ignored");
1036 else if (token
== CPP_STRING
)
1039 GCC_BAD ("expected a string after %<#pragma message%>");
1041 gcc_assert (message
);
1043 if (pragma_lex (&x
) != CPP_EOF
)
1044 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1046 if (TREE_STRING_LENGTH (message
) > 1)
1047 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1050 /* Mark whether the current location is valid for a STDC pragma. */
1052 static bool valid_location_for_stdc_pragma
;
1055 mark_valid_location_for_stdc_pragma (bool flag
)
1057 valid_location_for_stdc_pragma
= flag
;
1060 /* Return true if the current location is valid for a STDC pragma. */
1063 valid_location_for_stdc_pragma_p (void)
1065 return valid_location_for_stdc_pragma
;
1068 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1070 /* A STDC pragma must appear outside of external declarations or
1071 preceding all explicit declarations and statements inside a compound
1072 statement; its behavior is undefined if used in any other context.
1073 It takes a switch of ON, OFF, or DEFAULT. */
1075 static enum pragma_switch_t
1076 handle_stdc_pragma (const char *pname
)
1080 enum pragma_switch_t ret
;
1082 if (!valid_location_for_stdc_pragma_p ())
1084 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1089 if (pragma_lex (&t
) != CPP_NAME
)
1091 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1095 arg
= IDENTIFIER_POINTER (t
);
1097 if (!strcmp (arg
, "ON"))
1099 else if (!strcmp (arg
, "OFF"))
1101 else if (!strcmp (arg
, "DEFAULT"))
1102 ret
= PRAGMA_DEFAULT
;
1105 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1109 if (pragma_lex (&t
) != CPP_EOF
)
1111 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1118 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1119 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1120 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1123 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1125 if (c_dialect_cxx ())
1127 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1128 warning (OPT_Wunknown_pragmas
,
1129 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1134 if (!targetm
.decimal_float_supported_p ())
1136 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1137 warning (OPT_Wunknown_pragmas
,
1138 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1143 pedwarn (input_location
, OPT_Wpedantic
,
1144 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1146 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1149 set_float_const_decimal64 ();
1152 case PRAGMA_DEFAULT
:
1153 clear_float_const_decimal64 ();
1160 /* A vector of registered pragma callbacks, which is never freed. */
1162 static vec
<internal_pragma_handler
> registered_pragmas
;
1171 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1173 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1174 static const struct omp_pragma_def omp_pragmas
[] = {
1175 { "atomic", PRAGMA_OMP_ATOMIC
},
1176 { "barrier", PRAGMA_OMP_BARRIER
},
1177 { "cancel", PRAGMA_OMP_CANCEL
},
1178 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1179 { "critical", PRAGMA_OMP_CRITICAL
},
1180 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1181 { "flush", PRAGMA_OMP_FLUSH
},
1182 { "master", PRAGMA_OMP_MASTER
},
1183 { "ordered", PRAGMA_OMP_ORDERED
},
1184 { "section", PRAGMA_OMP_SECTION
},
1185 { "sections", PRAGMA_OMP_SECTIONS
},
1186 { "single", PRAGMA_OMP_SINGLE
},
1187 { "task", PRAGMA_OMP_TASK
},
1188 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1189 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1190 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1191 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1193 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1194 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1195 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1196 { "for", PRAGMA_OMP_FOR
},
1197 { "parallel", PRAGMA_OMP_PARALLEL
},
1198 { "simd", PRAGMA_OMP_SIMD
},
1199 { "target", PRAGMA_OMP_TARGET
},
1200 { "teams", PRAGMA_OMP_TEAMS
},
1204 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1206 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1207 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1208 / sizeof (*omp_pragmas
);
1211 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1212 if (omp_pragmas
[i
].id
== id
)
1215 *name
= omp_pragmas
[i
].name
;
1219 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1220 if (omp_pragmas_simd
[i
].id
== id
)
1223 *name
= omp_pragmas_simd
[i
].name
;
1227 if (id
== PRAGMA_CILK_SIMD
)
1234 if (id
>= PRAGMA_FIRST_EXTERNAL
1235 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1237 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1238 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1245 /* Front-end wrappers for pragma registration to avoid dragging
1246 cpplib.h in almost everywhere. */
1249 c_register_pragma_1 (const char *space
, const char *name
,
1250 internal_pragma_handler ihandler
, bool allow_expansion
)
1254 if (flag_preprocess_only
)
1256 pragma_ns_name ns_name
;
1258 if (!allow_expansion
)
1261 ns_name
.space
= space
;
1262 ns_name
.name
= name
;
1263 registered_pp_pragmas
.safe_push (ns_name
);
1264 id
= registered_pp_pragmas
.length ();
1265 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1269 registered_pragmas
.safe_push (ihandler
);
1270 id
= registered_pragmas
.length ();
1271 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1273 /* The C++ front end allocates 6 bits in cp_token; the C front end
1274 allocates 7 bits in c_token. At present this is sufficient. */
1275 gcc_assert (id
< 64);
1278 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1279 allow_expansion
, false);
1282 /* Register a C pragma handler, using a space and a name. It disallows pragma
1283 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1285 c_register_pragma (const char *space
, const char *name
,
1286 pragma_handler_1arg handler
)
1288 internal_pragma_handler ihandler
;
1290 ihandler
.handler
.handler_1arg
= handler
;
1291 ihandler
.extra_data
= false;
1292 ihandler
.data
= NULL
;
1293 c_register_pragma_1 (space
, name
, ihandler
, false);
1296 /* Register a C pragma handler, using a space and a name, it also carries an
1297 extra data field which can be used by the handler. It disallows pragma
1298 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1301 c_register_pragma_with_data (const char *space
, const char *name
,
1302 pragma_handler_2arg handler
, void * data
)
1304 internal_pragma_handler ihandler
;
1306 ihandler
.handler
.handler_2arg
= handler
;
1307 ihandler
.extra_data
= true;
1308 ihandler
.data
= data
;
1309 c_register_pragma_1 (space
, name
, ihandler
, false);
1312 /* Register a C pragma handler, using a space and a name. It allows pragma
1313 expansion as in the following example:
1316 #pragma count (NUMBER)
1318 Name expansion is still disallowed. */
1320 c_register_pragma_with_expansion (const char *space
, const char *name
,
1321 pragma_handler_1arg handler
)
1323 internal_pragma_handler ihandler
;
1325 ihandler
.handler
.handler_1arg
= handler
;
1326 ihandler
.extra_data
= false;
1327 ihandler
.data
= NULL
;
1328 c_register_pragma_1 (space
, name
, ihandler
, true);
1331 /* Register a C pragma handler, using a space and a name, it also carries an
1332 extra data field which can be used by the handler. It allows pragma
1333 expansion as in the following example:
1336 #pragma count (NUMBER)
1338 Name expansion is still disallowed. */
1340 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1341 pragma_handler_2arg handler
,
1344 internal_pragma_handler ihandler
;
1346 ihandler
.handler
.handler_2arg
= handler
;
1347 ihandler
.extra_data
= true;
1348 ihandler
.data
= data
;
1349 c_register_pragma_1 (space
, name
, ihandler
, true);
1353 c_invoke_pragma_handler (unsigned int id
)
1355 internal_pragma_handler
*ihandler
;
1356 pragma_handler_1arg handler_1arg
;
1357 pragma_handler_2arg handler_2arg
;
1359 id
-= PRAGMA_FIRST_EXTERNAL
;
1360 ihandler
= ®istered_pragmas
[id
];
1361 if (ihandler
->extra_data
)
1363 handler_2arg
= ihandler
->handler
.handler_2arg
;
1364 handler_2arg (parse_in
, ihandler
->data
);
1368 handler_1arg
= ihandler
->handler
.handler_1arg
;
1369 handler_1arg (parse_in
);
1373 /* Set up front-end pragmas. */
1379 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1382 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1383 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1384 omp_pragmas
[i
].id
, true, true);
1386 if (flag_openmp
|| flag_openmp_simd
)
1388 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1389 / sizeof (*omp_pragmas
);
1392 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1393 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1394 omp_pragmas_simd
[i
].id
, true, true);
1398 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1401 if (!flag_preprocess_only
)
1402 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1403 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1405 if (!flag_preprocess_only
)
1406 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1409 if (flag_cilkplus
&& !flag_preprocess_only
)
1410 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1411 PRAGMA_CILK_GRAINSIZE
, true, false);
1413 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1414 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1416 c_register_pragma (0, "pack", handle_pragma_pack
);
1418 c_register_pragma (0, "weak", handle_pragma_weak
);
1419 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1421 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1422 c_register_pragma ("GCC", "target", handle_pragma_target
);
1423 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1424 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1425 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1426 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1428 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1429 handle_pragma_float_const_decimal64
);
1431 c_register_pragma_with_expansion (0, "redefine_extname",
1432 handle_pragma_redefine_extname
);
1434 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1436 #ifdef REGISTER_TARGET_PRAGMAS
1437 REGISTER_TARGET_PRAGMAS ();
1440 /* Allow plugins to register their own pragmas. */
1441 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1444 #include "gt-c-family-c-pragma.h"