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"
32 #include "hard-reg-set.h"
34 #include "function.h" /* For cfun. FIXME: Does the parser know
35 when it is inside a function, so that
36 we don't have to look at cfun? */
41 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
42 this not a target hook?). */
44 #include "diagnostic.h"
49 #include "plugin-api.h"
53 #define GCC_BAD(gmsgid) \
54 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
55 #define GCC_BAD2(gmsgid, arg) \
56 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
58 typedef struct GTY(()) align_stack
{
61 struct align_stack
* prev
;
64 static GTY(()) struct align_stack
* alignment_stack
;
66 static void handle_pragma_pack (cpp_reader
*);
68 /* If we have a "global" #pragma pack(<n>) in effect when the first
69 #pragma pack(push,<n>) is encountered, this stores the value of
70 maximum_field_alignment in effect. When the final pop_alignment()
71 happens, we restore the value to this, not to a value of 0 for
72 maximum_field_alignment. Value is in bits. */
73 static int default_alignment
;
74 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
75 ? &default_alignment \
76 : &alignment_stack->alignment) = (ALIGN))
78 static void push_alignment (int, tree
);
79 static void pop_alignment (tree
);
81 /* Push an alignment value onto the stack. */
83 push_alignment (int alignment
, tree id
)
85 align_stack
* entry
= ggc_alloc
<align_stack
> ();
87 entry
->alignment
= alignment
;
89 entry
->prev
= alignment_stack
;
91 /* The current value of maximum_field_alignment is not necessarily
92 0 since there may be a #pragma pack(<n>) in effect; remember it
93 so that we can restore it after the final #pragma pop(). */
94 if (alignment_stack
== NULL
)
95 default_alignment
= maximum_field_alignment
;
97 alignment_stack
= entry
;
99 maximum_field_alignment
= alignment
;
102 /* Undo a push of an alignment onto the stack. */
104 pop_alignment (tree id
)
108 if (alignment_stack
== NULL
)
109 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
111 /* If we got an identifier, strip away everything above the target
112 entry so that the next step will restore the state just below it. */
115 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
118 alignment_stack
= entry
;
122 warning (OPT_Wpragmas
, "\
123 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
127 entry
= alignment_stack
->prev
;
129 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
131 alignment_stack
= entry
;
138 #pragma pack (push, N)
139 #pragma pack (push, ID)
140 #pragma pack (push, ID, N)
142 #pragma pack (pop, ID) */
144 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
148 enum cpp_ttype token
;
149 enum { set
, push
, pop
} action
;
151 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
152 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
154 token
= pragma_lex (&x
);
155 if (token
== CPP_CLOSE_PAREN
)
158 align
= initial_max_fld_align
;
160 else if (token
== CPP_NUMBER
)
162 if (TREE_CODE (x
) != INTEGER_CST
)
163 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
164 align
= TREE_INT_CST_LOW (x
);
166 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
167 GCC_BAD ("malformed %<#pragma pack%> - ignored");
169 else if (token
== CPP_NAME
)
171 #define GCC_BAD_ACTION do { if (action != pop) \
172 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
174 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
177 const char *op
= IDENTIFIER_POINTER (x
);
178 if (!strcmp (op
, "push"))
180 else if (!strcmp (op
, "pop"))
183 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
185 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
187 token
= pragma_lex (&x
);
188 if (token
== CPP_NAME
&& id
== 0)
192 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
194 if (TREE_CODE (x
) != INTEGER_CST
)
195 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
196 align
= TREE_INT_CST_LOW (x
);
204 if (token
!= CPP_CLOSE_PAREN
)
206 #undef GCC_BAD_ACTION
209 GCC_BAD ("malformed %<#pragma pack%> - ignored");
211 if (pragma_lex (&x
) != CPP_EOF
)
212 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
214 if (flag_pack_struct
)
215 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
226 align
*= BITS_PER_UNIT
;
231 align
= maximum_field_alignment
;
235 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
240 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
241 case push
: push_alignment (align
, id
); break;
242 case pop
: pop_alignment (id
); break;
246 typedef struct GTY(()) pending_weak_d
253 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
255 static void apply_pragma_weak (tree
, tree
);
256 static void handle_pragma_weak (cpp_reader
*);
259 apply_pragma_weak (tree decl
, tree value
)
263 value
= build_string (IDENTIFIER_LENGTH (value
),
264 IDENTIFIER_POINTER (value
));
265 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
266 build_tree_list (NULL
, value
)),
270 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
271 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
272 && DECL_ASSEMBLER_NAME_SET_P (decl
)
273 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
274 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
275 "results in unspecified behavior", decl
);
281 maybe_apply_pragma_weak (tree decl
)
287 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
289 /* No weak symbols pending, take the short-cut. */
290 if (vec_safe_is_empty (pending_weaks
))
292 /* If it's not visible outside this file, it doesn't matter whether
294 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
296 /* If it's not a function or a variable, it can't be weak.
297 FIXME: what kinds of things are visible outside this file but
298 aren't functions or variables? Should this be an assert instead? */
299 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
302 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
303 id
= DECL_ASSEMBLER_NAME (decl
);
306 id
= DECL_ASSEMBLER_NAME (decl
);
307 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
310 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
313 apply_pragma_weak (decl
, pe
->value
);
314 pending_weaks
->unordered_remove (i
);
319 /* Process all "#pragma weak A = B" directives where we have not seen
322 maybe_apply_pending_pragma_weaks (void)
324 tree alias_id
, id
, decl
;
329 if (vec_safe_is_empty (pending_weaks
))
332 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
340 target
= symtab_node::get_for_asmname (id
);
341 decl
= build_decl (UNKNOWN_LOCATION
,
342 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
343 alias_id
, default_function_type
);
345 DECL_ARTIFICIAL (decl
) = 1;
346 TREE_PUBLIC (decl
) = 1;
347 DECL_WEAK (decl
) = 1;
348 if (TREE_CODE (decl
) == VAR_DECL
)
349 TREE_STATIC (decl
) = 1;
352 error ("%q+D aliased to undefined symbol %qE",
357 assemble_alias (decl
, id
);
361 /* #pragma weak name [= value] */
363 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
365 tree name
, value
, x
, decl
;
370 if (pragma_lex (&name
) != CPP_NAME
)
371 GCC_BAD ("malformed #pragma weak, ignored");
375 if (pragma_lex (&value
) != CPP_NAME
)
376 GCC_BAD ("malformed #pragma weak, ignored");
380 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
382 decl
= identifier_global_value (name
);
383 if (decl
&& DECL_P (decl
))
385 apply_pragma_weak (decl
, value
);
388 DECL_EXTERNAL (decl
) = 0;
389 if (TREE_CODE (decl
) == VAR_DECL
)
390 TREE_STATIC (decl
) = 1;
391 assemble_alias (decl
, value
);
396 pending_weak pe
= {name
, value
};
397 vec_safe_push (pending_weaks
, pe
);
401 /* GCC supports two #pragma directives for renaming the external
402 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
403 compatibility with the Solaris and VMS system headers. GCC also
404 has its own notation for this, __asm__("name") annotations.
406 Corner cases of these features and their interaction:
408 1) Both pragmas silently apply only to declarations with external
409 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
410 do not have this restriction.
412 2) In C++, both #pragmas silently apply only to extern "C" declarations.
413 Asm labels do not have this restriction.
415 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
416 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
417 new name is different, a warning issues and the name does not change.
419 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
420 *not* the DECL_ASSEMBLER_NAME.
422 5) If #pragma extern_prefix is in effect and a declaration occurs
423 with an __asm__ name, the #pragma extern_prefix is silently
424 ignored for that declaration.
426 6) If #pragma extern_prefix and #pragma redefine_extname apply to
427 the same declaration, whichever triggered first wins, and a warning
428 is issued. (We would like to have #pragma redefine_extname always
429 win, but it can appear either before or after the declaration, and
430 if it appears afterward, we have no way of knowing whether a modified
431 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
433 typedef struct GTY(()) pending_redefinition_d
{
436 } pending_redefinition
;
439 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
441 static void handle_pragma_redefine_extname (cpp_reader
*);
443 /* #pragma redefine_extname oldname newname */
445 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
447 tree oldname
, newname
, decls
, x
;
451 if (pragma_lex (&oldname
) != CPP_NAME
)
452 GCC_BAD ("malformed #pragma redefine_extname, ignored");
453 if (pragma_lex (&newname
) != CPP_NAME
)
454 GCC_BAD ("malformed #pragma redefine_extname, ignored");
457 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
460 for (decls
= c_linkage_bindings (oldname
);
464 if (TREE_CODE (decls
) == TREE_LIST
)
466 decl
= TREE_VALUE (decls
);
467 decls
= TREE_CHAIN (decls
);
475 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
476 && (TREE_CODE (decl
) == FUNCTION_DECL
477 || TREE_CODE (decl
) == VAR_DECL
))
480 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
482 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
483 name
= targetm
.strip_name_encoding (name
);
485 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
486 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
487 "conflict with previous rename");
490 symtab
->change_decl_assembler_name (decl
, newname
);
495 /* We have to add this to the rename list even if there's already
496 a global value that doesn't meet the above criteria, because in
497 C++ "struct foo {...};" puts "foo" in the current namespace but
498 does *not* conflict with a subsequent declaration of a function
499 or variable foo. See g++.dg/other/pragma-re-2.C. */
500 add_to_renaming_pragma_list (oldname
, newname
);
503 /* This is called from here and from ia64-c.c. */
505 add_to_renaming_pragma_list (tree oldname
, tree newname
)
508 pending_redefinition
*p
;
510 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
511 if (oldname
== p
->oldname
)
513 if (p
->newname
!= newname
)
514 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
515 "conflict with previous #pragma redefine_extname");
519 pending_redefinition e
= {oldname
, newname
};
520 vec_safe_push (pending_redefine_extname
, e
);
523 /* The current prefix set by #pragma extern_prefix. */
524 GTY(()) tree pragma_extern_prefix
;
526 /* Hook from the front ends to apply the results of one of the preceding
527 pragmas that rename variables. */
530 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
533 pending_redefinition
*p
;
535 /* The renaming pragmas are only applied to declarations with
537 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
538 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
539 || !has_c_linkage (decl
))
542 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
543 but we may warn about a rename that conflicts. */
544 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
546 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
547 oldname
= targetm
.strip_name_encoding (oldname
);
549 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
550 warning (OPT_Wpragmas
, "asm declaration ignored due to "
551 "conflict with previous rename");
553 /* Take any pending redefine_extname off the list. */
554 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
555 if (DECL_NAME (decl
) == p
->oldname
)
557 /* Only warn if there is a conflict. */
558 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
559 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
560 "conflict with previous rename");
562 pending_redefine_extname
->unordered_remove (ix
);
568 /* Find out if we have a pending #pragma redefine_extname. */
569 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
570 if (DECL_NAME (decl
) == p
->oldname
)
572 tree newname
= p
->newname
;
573 pending_redefine_extname
->unordered_remove (ix
);
575 /* If we already have an asmname, #pragma redefine_extname is
576 ignored (with a warning if it conflicts). */
579 if (strcmp (TREE_STRING_POINTER (asmname
),
580 IDENTIFIER_POINTER (newname
)) != 0)
581 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
582 "conflict with __asm__ declaration");
586 /* Otherwise we use what we've got; #pragma extern_prefix is
588 return build_string (IDENTIFIER_LENGTH (newname
),
589 IDENTIFIER_POINTER (newname
));
592 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
596 /* If #pragma extern_prefix is in effect, apply it. */
597 if (pragma_extern_prefix
)
599 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
600 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
602 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
603 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
605 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
607 memcpy (newname
, prefix
, plen
);
608 memcpy (newname
+ plen
, id
, ilen
+ 1);
610 return build_string (plen
+ ilen
, newname
);
618 static void handle_pragma_visibility (cpp_reader
*);
620 static vec
<int> visstack
;
622 /* Push the visibility indicated by STR onto the top of the #pragma
623 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
624 C++ namespace with visibility attribute and 2 for C++ builtin
625 ABI namespace. push_visibility/pop_visibility calls must have
626 matching KIND, it is not allowed to push visibility using one
627 KIND and pop using a different one. */
630 push_visibility (const char *str
, int kind
)
632 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
633 if (!strcmp (str
, "default"))
634 default_visibility
= VISIBILITY_DEFAULT
;
635 else if (!strcmp (str
, "internal"))
636 default_visibility
= VISIBILITY_INTERNAL
;
637 else if (!strcmp (str
, "hidden"))
638 default_visibility
= VISIBILITY_HIDDEN
;
639 else if (!strcmp (str
, "protected"))
640 default_visibility
= VISIBILITY_PROTECTED
;
642 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
643 visibility_options
.inpragma
= 1;
646 /* Pop a level of the #pragma visibility stack. Return true if
650 pop_visibility (int kind
)
652 if (!visstack
.length ())
654 if ((visstack
.last () >> 8) != kind
)
657 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
658 visibility_options
.inpragma
659 = visstack
.length () != 0;
663 /* Sets the default visibility for symbols to something other than that
664 specified on the command line. */
667 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
669 /* Form is #pragma GCC visibility push(hidden)|pop */
671 enum cpp_ttype token
;
672 enum { bad
, push
, pop
} action
= bad
;
674 token
= pragma_lex (&x
);
675 if (token
== CPP_NAME
)
677 const char *op
= IDENTIFIER_POINTER (x
);
678 if (!strcmp (op
, "push"))
680 else if (!strcmp (op
, "pop"))
684 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
689 if (! pop_visibility (0))
690 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
694 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
695 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
696 token
= pragma_lex (&x
);
697 if (token
!= CPP_NAME
)
698 GCC_BAD ("malformed #pragma GCC visibility push");
700 push_visibility (IDENTIFIER_POINTER (x
), 0);
701 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
702 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
705 if (pragma_lex (&x
) != CPP_EOF
)
706 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
710 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
712 const char *kind_string
, *option_string
;
713 unsigned int option_index
;
714 enum cpp_ttype token
;
717 struct cl_option_handlers handlers
;
719 token
= pragma_lex (&x
);
720 if (token
!= CPP_NAME
)
721 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
722 kind_string
= IDENTIFIER_POINTER (x
);
723 if (strcmp (kind_string
, "error") == 0)
725 else if (strcmp (kind_string
, "warning") == 0)
727 else if (strcmp (kind_string
, "ignored") == 0)
729 else if (strcmp (kind_string
, "push") == 0)
731 diagnostic_push_diagnostics (global_dc
, input_location
);
734 else if (strcmp (kind_string
, "pop") == 0)
736 diagnostic_pop_diagnostics (global_dc
, input_location
);
740 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
742 token
= pragma_lex (&x
);
743 if (token
!= CPP_STRING
)
744 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
745 option_string
= TREE_STRING_POINTER (x
);
746 set_default_handlers (&handlers
);
747 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
748 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
750 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
751 input_location
, c_family_lang_mask
, &handlers
,
752 &global_options
, &global_options_set
,
756 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
759 /* Parse #pragma GCC target (xxx) to set target specific options. */
761 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
763 enum cpp_ttype token
;
765 bool close_paren_needed_p
= false;
769 error ("#pragma GCC option is not allowed inside functions");
773 token
= pragma_lex (&x
);
774 if (token
== CPP_OPEN_PAREN
)
776 close_paren_needed_p
= true;
777 token
= pragma_lex (&x
);
780 if (token
!= CPP_STRING
)
782 GCC_BAD ("%<#pragma GCC option%> is not a string");
786 /* Strings are user options. */
789 tree args
= NULL_TREE
;
793 /* Build up the strings now as a tree linked list. Skip empty
795 if (TREE_STRING_LENGTH (x
) > 0)
796 args
= tree_cons (NULL_TREE
, x
, args
);
798 token
= pragma_lex (&x
);
799 while (token
== CPP_COMMA
)
800 token
= pragma_lex (&x
);
802 while (token
== CPP_STRING
);
804 if (close_paren_needed_p
)
806 if (token
== CPP_CLOSE_PAREN
)
807 token
= pragma_lex (&x
);
809 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
810 "not have a final %<)%>");
813 if (token
!= CPP_EOF
)
815 error ("#pragma GCC target string... is badly formed");
819 /* put arguments in the order the user typed them. */
820 args
= nreverse (args
);
822 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
823 current_target_pragma
= args
;
827 /* Handle #pragma GCC optimize to set optimization options. */
829 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
831 enum cpp_ttype token
;
833 bool close_paren_needed_p
= false;
834 tree optimization_previous_node
= optimization_current_node
;
838 error ("#pragma GCC optimize is not allowed inside functions");
842 token
= pragma_lex (&x
);
843 if (token
== CPP_OPEN_PAREN
)
845 close_paren_needed_p
= true;
846 token
= pragma_lex (&x
);
849 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
851 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
855 /* Strings/numbers are user options. */
858 tree args
= NULL_TREE
;
862 /* Build up the numbers/strings now as a list. */
863 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
864 args
= tree_cons (NULL_TREE
, x
, args
);
866 token
= pragma_lex (&x
);
867 while (token
== CPP_COMMA
)
868 token
= pragma_lex (&x
);
870 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
872 if (close_paren_needed_p
)
874 if (token
== CPP_CLOSE_PAREN
)
875 token
= pragma_lex (&x
);
877 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
878 "not have a final %<)%>");
881 if (token
!= CPP_EOF
)
883 error ("#pragma GCC optimize string... is badly formed");
887 /* put arguments in the order the user typed them. */
888 args
= nreverse (args
);
890 parse_optimize_options (args
, false);
891 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
892 optimization_current_node
= build_optimization_node (&global_options
);
893 c_cpp_builtins_optimize_pragma (parse_in
,
894 optimization_previous_node
,
895 optimization_current_node
);
899 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
900 both the binary representation of the options and the TREE_LIST of
901 strings that will be added to the function's attribute list. */
902 typedef struct GTY(()) opt_stack
{
903 struct opt_stack
*prev
;
906 tree optimize_binary
;
907 tree optimize_strings
;
910 static GTY(()) struct opt_stack
* options_stack
;
912 /* Handle #pragma GCC push_options to save the current target and optimization
916 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
918 enum cpp_ttype token
;
921 token
= pragma_lex (&x
);
922 if (token
!= CPP_EOF
)
924 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
928 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
929 p
->prev
= options_stack
;
932 /* Save optimization and target flags in binary format. */
933 p
->optimize_binary
= build_optimization_node (&global_options
);
934 p
->target_binary
= build_target_option_node (&global_options
);
936 /* Save optimization and target flags in string list format. */
937 p
->optimize_strings
= copy_list (current_optimize_pragma
);
938 p
->target_strings
= copy_list (current_target_pragma
);
941 /* Handle #pragma GCC pop_options to restore the current target and
942 optimization options from a previous push_options. */
945 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
947 enum cpp_ttype token
;
951 token
= pragma_lex (&x
);
952 if (token
!= CPP_EOF
)
954 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
960 warning (OPT_Wpragmas
,
961 "%<#pragma GCC pop_options%> without a corresponding "
962 "%<#pragma GCC push_options%>");
967 options_stack
= p
->prev
;
969 if (p
->target_binary
!= target_option_current_node
)
971 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
972 target_option_current_node
= p
->target_binary
;
975 if (p
->optimize_binary
!= optimization_current_node
)
977 tree old_optimize
= optimization_current_node
;
978 cl_optimization_restore (&global_options
,
979 TREE_OPTIMIZATION (p
->optimize_binary
));
980 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
982 optimization_current_node
= p
->optimize_binary
;
985 current_target_pragma
= p
->target_strings
;
986 current_optimize_pragma
= p
->optimize_strings
;
989 /* Handle #pragma GCC reset_options to restore the current target and
990 optimization options to the original options used on the command line. */
993 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
995 enum cpp_ttype token
;
997 tree new_optimize
= optimization_default_node
;
998 tree new_target
= target_option_default_node
;
1000 token
= pragma_lex (&x
);
1001 if (token
!= CPP_EOF
)
1003 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1007 if (new_target
!= target_option_current_node
)
1009 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1010 target_option_current_node
= new_target
;
1013 if (new_optimize
!= optimization_current_node
)
1015 tree old_optimize
= optimization_current_node
;
1016 cl_optimization_restore (&global_options
,
1017 TREE_OPTIMIZATION (new_optimize
));
1018 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1019 optimization_current_node
= new_optimize
;
1022 current_target_pragma
= NULL_TREE
;
1023 current_optimize_pragma
= NULL_TREE
;
1026 /* Print a plain user-specified message. */
1029 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1031 enum cpp_ttype token
;
1032 tree x
, message
= 0;
1034 token
= pragma_lex (&x
);
1035 if (token
== CPP_OPEN_PAREN
)
1037 token
= pragma_lex (&x
);
1038 if (token
== CPP_STRING
)
1041 GCC_BAD ("expected a string after %<#pragma message%>");
1042 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1043 GCC_BAD ("malformed %<#pragma message%>, ignored");
1045 else if (token
== CPP_STRING
)
1048 GCC_BAD ("expected a string after %<#pragma message%>");
1050 gcc_assert (message
);
1052 if (pragma_lex (&x
) != CPP_EOF
)
1053 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1055 if (TREE_STRING_LENGTH (message
) > 1)
1056 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1059 /* Mark whether the current location is valid for a STDC pragma. */
1061 static bool valid_location_for_stdc_pragma
;
1064 mark_valid_location_for_stdc_pragma (bool flag
)
1066 valid_location_for_stdc_pragma
= flag
;
1069 /* Return true if the current location is valid for a STDC pragma. */
1072 valid_location_for_stdc_pragma_p (void)
1074 return valid_location_for_stdc_pragma
;
1077 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1079 /* A STDC pragma must appear outside of external declarations or
1080 preceding all explicit declarations and statements inside a compound
1081 statement; its behavior is undefined if used in any other context.
1082 It takes a switch of ON, OFF, or DEFAULT. */
1084 static enum pragma_switch_t
1085 handle_stdc_pragma (const char *pname
)
1089 enum pragma_switch_t ret
;
1091 if (!valid_location_for_stdc_pragma_p ())
1093 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1098 if (pragma_lex (&t
) != CPP_NAME
)
1100 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1104 arg
= IDENTIFIER_POINTER (t
);
1106 if (!strcmp (arg
, "ON"))
1108 else if (!strcmp (arg
, "OFF"))
1110 else if (!strcmp (arg
, "DEFAULT"))
1111 ret
= PRAGMA_DEFAULT
;
1114 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1118 if (pragma_lex (&t
) != CPP_EOF
)
1120 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1127 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1128 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1129 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1132 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1134 if (c_dialect_cxx ())
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 if (!targetm
.decimal_float_supported_p ())
1145 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1146 warning (OPT_Wunknown_pragmas
,
1147 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1152 pedwarn (input_location
, OPT_Wpedantic
,
1153 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1155 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1158 set_float_const_decimal64 ();
1161 case PRAGMA_DEFAULT
:
1162 clear_float_const_decimal64 ();
1169 /* A vector of registered pragma callbacks, which is never freed. */
1171 static vec
<internal_pragma_handler
> registered_pragmas
;
1180 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1182 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1183 static const struct omp_pragma_def omp_pragmas
[] = {
1184 { "atomic", PRAGMA_OMP_ATOMIC
},
1185 { "barrier", PRAGMA_OMP_BARRIER
},
1186 { "cancel", PRAGMA_OMP_CANCEL
},
1187 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1188 { "critical", PRAGMA_OMP_CRITICAL
},
1189 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1190 { "flush", PRAGMA_OMP_FLUSH
},
1191 { "master", PRAGMA_OMP_MASTER
},
1192 { "ordered", PRAGMA_OMP_ORDERED
},
1193 { "section", PRAGMA_OMP_SECTION
},
1194 { "sections", PRAGMA_OMP_SECTIONS
},
1195 { "single", PRAGMA_OMP_SINGLE
},
1196 { "task", PRAGMA_OMP_TASK
},
1197 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1198 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1199 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1200 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1202 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1203 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1204 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1205 { "for", PRAGMA_OMP_FOR
},
1206 { "parallel", PRAGMA_OMP_PARALLEL
},
1207 { "simd", PRAGMA_OMP_SIMD
},
1208 { "target", PRAGMA_OMP_TARGET
},
1209 { "teams", PRAGMA_OMP_TEAMS
},
1213 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1215 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1216 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1217 / sizeof (*omp_pragmas
);
1220 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1221 if (omp_pragmas
[i
].id
== id
)
1224 *name
= omp_pragmas
[i
].name
;
1228 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1229 if (omp_pragmas_simd
[i
].id
== id
)
1232 *name
= omp_pragmas_simd
[i
].name
;
1236 if (id
== PRAGMA_CILK_SIMD
)
1243 if (id
>= PRAGMA_FIRST_EXTERNAL
1244 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1246 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1247 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1254 /* Front-end wrappers for pragma registration to avoid dragging
1255 cpplib.h in almost everywhere. */
1258 c_register_pragma_1 (const char *space
, const char *name
,
1259 internal_pragma_handler ihandler
, bool allow_expansion
)
1263 if (flag_preprocess_only
)
1265 pragma_ns_name ns_name
;
1267 if (!allow_expansion
)
1270 ns_name
.space
= space
;
1271 ns_name
.name
= name
;
1272 registered_pp_pragmas
.safe_push (ns_name
);
1273 id
= registered_pp_pragmas
.length ();
1274 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1278 registered_pragmas
.safe_push (ihandler
);
1279 id
= registered_pragmas
.length ();
1280 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1282 /* The C++ front end allocates 6 bits in cp_token; the C front end
1283 allocates 7 bits in c_token. At present this is sufficient. */
1284 gcc_assert (id
< 64);
1287 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1288 allow_expansion
, false);
1291 /* Register a C pragma handler, using a space and a name. It disallows pragma
1292 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1294 c_register_pragma (const char *space
, const char *name
,
1295 pragma_handler_1arg handler
)
1297 internal_pragma_handler ihandler
;
1299 ihandler
.handler
.handler_1arg
= handler
;
1300 ihandler
.extra_data
= false;
1301 ihandler
.data
= NULL
;
1302 c_register_pragma_1 (space
, name
, ihandler
, false);
1305 /* Register a C pragma handler, using a space and a name, it also carries an
1306 extra data field which can be used by the handler. It disallows pragma
1307 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1310 c_register_pragma_with_data (const char *space
, const char *name
,
1311 pragma_handler_2arg handler
, void * data
)
1313 internal_pragma_handler ihandler
;
1315 ihandler
.handler
.handler_2arg
= handler
;
1316 ihandler
.extra_data
= true;
1317 ihandler
.data
= data
;
1318 c_register_pragma_1 (space
, name
, ihandler
, false);
1321 /* Register a C pragma handler, using a space and a name. It allows pragma
1322 expansion as in the following example:
1325 #pragma count (NUMBER)
1327 Name expansion is still disallowed. */
1329 c_register_pragma_with_expansion (const char *space
, const char *name
,
1330 pragma_handler_1arg handler
)
1332 internal_pragma_handler ihandler
;
1334 ihandler
.handler
.handler_1arg
= handler
;
1335 ihandler
.extra_data
= false;
1336 ihandler
.data
= NULL
;
1337 c_register_pragma_1 (space
, name
, ihandler
, true);
1340 /* Register a C pragma handler, using a space and a name, it also carries an
1341 extra data field which can be used by the handler. It allows pragma
1342 expansion as in the following example:
1345 #pragma count (NUMBER)
1347 Name expansion is still disallowed. */
1349 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1350 pragma_handler_2arg handler
,
1353 internal_pragma_handler ihandler
;
1355 ihandler
.handler
.handler_2arg
= handler
;
1356 ihandler
.extra_data
= true;
1357 ihandler
.data
= data
;
1358 c_register_pragma_1 (space
, name
, ihandler
, true);
1362 c_invoke_pragma_handler (unsigned int id
)
1364 internal_pragma_handler
*ihandler
;
1365 pragma_handler_1arg handler_1arg
;
1366 pragma_handler_2arg handler_2arg
;
1368 id
-= PRAGMA_FIRST_EXTERNAL
;
1369 ihandler
= ®istered_pragmas
[id
];
1370 if (ihandler
->extra_data
)
1372 handler_2arg
= ihandler
->handler
.handler_2arg
;
1373 handler_2arg (parse_in
, ihandler
->data
);
1377 handler_1arg
= ihandler
->handler
.handler_1arg
;
1378 handler_1arg (parse_in
);
1382 /* Set up front-end pragmas. */
1388 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1391 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1392 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1393 omp_pragmas
[i
].id
, true, true);
1395 if (flag_openmp
|| flag_openmp_simd
)
1397 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1398 / sizeof (*omp_pragmas
);
1401 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1402 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1403 omp_pragmas_simd
[i
].id
, true, true);
1407 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1410 if (!flag_preprocess_only
)
1411 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1412 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1414 if (!flag_preprocess_only
)
1415 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1418 if (flag_cilkplus
&& !flag_preprocess_only
)
1419 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1420 PRAGMA_CILK_GRAINSIZE
, true, false);
1422 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1423 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1425 c_register_pragma (0, "pack", handle_pragma_pack
);
1427 c_register_pragma (0, "weak", handle_pragma_weak
);
1428 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1430 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1431 c_register_pragma ("GCC", "target", handle_pragma_target
);
1432 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1433 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1434 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1435 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1437 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1438 handle_pragma_float_const_decimal64
);
1440 c_register_pragma_with_expansion (0, "redefine_extname",
1441 handle_pragma_redefine_extname
);
1443 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1445 #ifdef REGISTER_TARGET_PRAGMAS
1446 REGISTER_TARGET_PRAGMAS ();
1449 /* Allow plugins to register their own pragmas. */
1450 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1453 #include "gt-c-family-c-pragma.h"