1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
35 #include "stringpool.h"
42 #include "hard-reg-set.h"
44 #include "function.h" /* For cfun. FIXME: Does the parser know
45 when it is inside a function, so that
46 we don't have to look at cfun? */
51 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
52 this not a target hook?). */
54 #include "diagnostic.h"
59 #include "plugin-api.h"
63 #define GCC_BAD(gmsgid) \
64 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
65 #define GCC_BAD2(gmsgid, arg) \
66 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
68 typedef struct GTY(()) align_stack
{
71 struct align_stack
* prev
;
74 static GTY(()) struct align_stack
* alignment_stack
;
76 static void handle_pragma_pack (cpp_reader
*);
78 /* If we have a "global" #pragma pack(<n>) in effect when the first
79 #pragma pack(push,<n>) is encountered, this stores the value of
80 maximum_field_alignment in effect. When the final pop_alignment()
81 happens, we restore the value to this, not to a value of 0 for
82 maximum_field_alignment. Value is in bits. */
83 static int default_alignment
;
84 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
85 ? &default_alignment \
86 : &alignment_stack->alignment) = (ALIGN))
88 static void push_alignment (int, tree
);
89 static void pop_alignment (tree
);
91 /* Push an alignment value onto the stack. */
93 push_alignment (int alignment
, tree id
)
95 align_stack
* entry
= ggc_alloc
<align_stack
> ();
97 entry
->alignment
= alignment
;
99 entry
->prev
= alignment_stack
;
101 /* The current value of maximum_field_alignment is not necessarily
102 0 since there may be a #pragma pack(<n>) in effect; remember it
103 so that we can restore it after the final #pragma pop(). */
104 if (alignment_stack
== NULL
)
105 default_alignment
= maximum_field_alignment
;
107 alignment_stack
= entry
;
109 maximum_field_alignment
= alignment
;
112 /* Undo a push of an alignment onto the stack. */
114 pop_alignment (tree id
)
118 if (alignment_stack
== NULL
)
119 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
121 /* If we got an identifier, strip away everything above the target
122 entry so that the next step will restore the state just below it. */
125 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
128 alignment_stack
= entry
;
132 warning (OPT_Wpragmas
, "\
133 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
137 entry
= alignment_stack
->prev
;
139 maximum_field_alignment
= entry
? entry
->alignment
: default_alignment
;
141 alignment_stack
= entry
;
148 #pragma pack (push, N)
149 #pragma pack (push, ID)
150 #pragma pack (push, ID, N)
152 #pragma pack (pop, ID) */
154 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
158 enum cpp_ttype token
;
159 enum { set
, push
, pop
} action
;
161 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
162 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
164 token
= pragma_lex (&x
);
165 if (token
== CPP_CLOSE_PAREN
)
168 align
= initial_max_fld_align
;
170 else if (token
== CPP_NUMBER
)
172 if (TREE_CODE (x
) != INTEGER_CST
)
173 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
174 align
= TREE_INT_CST_LOW (x
);
176 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
177 GCC_BAD ("malformed %<#pragma pack%> - ignored");
179 else if (token
== CPP_NAME
)
181 #define GCC_BAD_ACTION do { if (action != pop) \
182 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
184 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
187 const char *op
= IDENTIFIER_POINTER (x
);
188 if (!strcmp (op
, "push"))
190 else if (!strcmp (op
, "pop"))
193 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x
);
195 while ((token
= pragma_lex (&x
)) == CPP_COMMA
)
197 token
= pragma_lex (&x
);
198 if (token
== CPP_NAME
&& id
== 0)
202 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
204 if (TREE_CODE (x
) != INTEGER_CST
)
205 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
206 align
= TREE_INT_CST_LOW (x
);
214 if (token
!= CPP_CLOSE_PAREN
)
216 #undef GCC_BAD_ACTION
219 GCC_BAD ("malformed %<#pragma pack%> - ignored");
221 if (pragma_lex (&x
) != CPP_EOF
)
222 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
224 if (flag_pack_struct
)
225 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
236 align
*= BITS_PER_UNIT
;
241 align
= maximum_field_alignment
;
245 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
250 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
251 case push
: push_alignment (align
, id
); break;
252 case pop
: pop_alignment (id
); break;
256 typedef struct GTY(()) pending_weak_d
263 static GTY(()) vec
<pending_weak
, va_gc
> *pending_weaks
;
265 static void apply_pragma_weak (tree
, tree
);
266 static void handle_pragma_weak (cpp_reader
*);
269 apply_pragma_weak (tree decl
, tree value
)
273 value
= build_string (IDENTIFIER_LENGTH (value
),
274 IDENTIFIER_POINTER (value
));
275 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
276 build_tree_list (NULL
, value
)),
280 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
281 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
282 && DECL_ASSEMBLER_NAME_SET_P (decl
)
283 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
284 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
285 "results in unspecified behavior", decl
);
291 maybe_apply_pragma_weak (tree decl
)
297 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
299 /* No weak symbols pending, take the short-cut. */
300 if (vec_safe_is_empty (pending_weaks
))
302 /* If it's not visible outside this file, it doesn't matter whether
304 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
306 /* If it's not a function or a variable, it can't be weak.
307 FIXME: what kinds of things are visible outside this file but
308 aren't functions or variables? Should this be an assert instead? */
309 if (!VAR_OR_FUNCTION_DECL_P (decl
))
312 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
313 id
= DECL_ASSEMBLER_NAME (decl
);
316 id
= DECL_ASSEMBLER_NAME (decl
);
317 SET_DECL_ASSEMBLER_NAME (decl
, NULL_TREE
);
320 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
323 apply_pragma_weak (decl
, pe
->value
);
324 pending_weaks
->unordered_remove (i
);
329 /* Process all "#pragma weak A = B" directives where we have not seen
332 maybe_apply_pending_pragma_weaks (void)
334 tree alias_id
, id
, decl
;
339 if (vec_safe_is_empty (pending_weaks
))
342 FOR_EACH_VEC_ELT (*pending_weaks
, i
, pe
)
350 target
= symtab_node::get_for_asmname (id
);
351 decl
= build_decl (UNKNOWN_LOCATION
,
352 target
? TREE_CODE (target
->decl
) : FUNCTION_DECL
,
353 alias_id
, default_function_type
);
355 DECL_ARTIFICIAL (decl
) = 1;
356 TREE_PUBLIC (decl
) = 1;
357 DECL_WEAK (decl
) = 1;
358 if (TREE_CODE (decl
) == VAR_DECL
)
359 TREE_STATIC (decl
) = 1;
362 error ("%q+D aliased to undefined symbol %qE",
367 assemble_alias (decl
, id
);
371 /* #pragma weak name [= value] */
373 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
375 tree name
, value
, x
, decl
;
380 if (pragma_lex (&name
) != CPP_NAME
)
381 GCC_BAD ("malformed #pragma weak, ignored");
385 if (pragma_lex (&value
) != CPP_NAME
)
386 GCC_BAD ("malformed #pragma weak, ignored");
390 warning (OPT_Wpragmas
, "junk at end of %<#pragma weak%>");
392 decl
= identifier_global_value (name
);
393 if (decl
&& DECL_P (decl
))
395 if (!VAR_OR_FUNCTION_DECL_P (decl
))
396 GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
398 apply_pragma_weak (decl
, value
);
401 DECL_EXTERNAL (decl
) = 0;
402 if (TREE_CODE (decl
) == VAR_DECL
)
403 TREE_STATIC (decl
) = 1;
404 assemble_alias (decl
, value
);
409 pending_weak pe
= {name
, value
};
410 vec_safe_push (pending_weaks
, pe
);
414 /* GCC supports two #pragma directives for renaming the external
415 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
416 compatibility with the Solaris and VMS system headers. GCC also
417 has its own notation for this, __asm__("name") annotations.
419 Corner cases of these features and their interaction:
421 1) Both pragmas silently apply only to declarations with external
422 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
423 do not have this restriction.
425 2) In C++, both #pragmas silently apply only to extern "C" declarations.
426 Asm labels do not have this restriction.
428 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
429 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
430 new name is different, a warning issues and the name does not change.
432 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
433 *not* the DECL_ASSEMBLER_NAME.
435 5) If #pragma extern_prefix is in effect and a declaration occurs
436 with an __asm__ name, the #pragma extern_prefix is silently
437 ignored for that declaration.
439 6) If #pragma extern_prefix and #pragma redefine_extname apply to
440 the same declaration, whichever triggered first wins, and a warning
441 is issued. (We would like to have #pragma redefine_extname always
442 win, but it can appear either before or after the declaration, and
443 if it appears afterward, we have no way of knowing whether a modified
444 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
446 typedef struct GTY(()) pending_redefinition_d
{
449 } pending_redefinition
;
452 static GTY(()) vec
<pending_redefinition
, va_gc
> *pending_redefine_extname
;
454 static void handle_pragma_redefine_extname (cpp_reader
*);
456 /* #pragma redefine_extname oldname newname */
458 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
460 tree oldname
, newname
, decls
, x
;
464 if (pragma_lex (&oldname
) != CPP_NAME
)
465 GCC_BAD ("malformed #pragma redefine_extname, ignored");
466 if (pragma_lex (&newname
) != CPP_NAME
)
467 GCC_BAD ("malformed #pragma redefine_extname, ignored");
470 warning (OPT_Wpragmas
, "junk at end of %<#pragma redefine_extname%>");
473 for (decls
= c_linkage_bindings (oldname
);
477 if (TREE_CODE (decls
) == TREE_LIST
)
479 decl
= TREE_VALUE (decls
);
480 decls
= TREE_CHAIN (decls
);
488 if ((TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
489 && VAR_OR_FUNCTION_DECL_P (decl
))
492 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
494 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
495 name
= targetm
.strip_name_encoding (name
);
497 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
498 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
499 "conflict with previous rename");
502 symtab
->change_decl_assembler_name (decl
, newname
);
507 /* We have to add this to the rename list even if there's already
508 a global value that doesn't meet the above criteria, because in
509 C++ "struct foo {...};" puts "foo" in the current namespace but
510 does *not* conflict with a subsequent declaration of a function
511 or variable foo. See g++.dg/other/pragma-re-2.C. */
512 add_to_renaming_pragma_list (oldname
, newname
);
515 /* This is called from here and from ia64-c.c. */
517 add_to_renaming_pragma_list (tree oldname
, tree newname
)
520 pending_redefinition
*p
;
522 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
523 if (oldname
== p
->oldname
)
525 if (p
->newname
!= newname
)
526 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
527 "conflict with previous #pragma redefine_extname");
531 pending_redefinition e
= {oldname
, newname
};
532 vec_safe_push (pending_redefine_extname
, e
);
535 /* The current prefix set by #pragma extern_prefix. */
536 GTY(()) tree pragma_extern_prefix
;
538 /* Hook from the front ends to apply the results of one of the preceding
539 pragmas that rename variables. */
542 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
545 pending_redefinition
*p
;
547 /* The renaming pragmas are only applied to declarations with
549 if (!VAR_OR_FUNCTION_DECL_P (decl
)
550 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
551 || !has_c_linkage (decl
))
554 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
555 but we may warn about a rename that conflicts. */
556 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
558 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
559 oldname
= targetm
.strip_name_encoding (oldname
);
561 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
562 warning (OPT_Wpragmas
, "asm declaration ignored due to "
563 "conflict with previous rename");
565 /* Take any pending redefine_extname off the list. */
566 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
567 if (DECL_NAME (decl
) == p
->oldname
)
569 /* Only warn if there is a conflict. */
570 if (strcmp (IDENTIFIER_POINTER (p
->newname
), oldname
))
571 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
572 "conflict with previous rename");
574 pending_redefine_extname
->unordered_remove (ix
);
580 /* Find out if we have a pending #pragma redefine_extname. */
581 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname
, ix
, p
)
582 if (DECL_NAME (decl
) == p
->oldname
)
584 tree newname
= p
->newname
;
585 pending_redefine_extname
->unordered_remove (ix
);
587 /* If we already have an asmname, #pragma redefine_extname is
588 ignored (with a warning if it conflicts). */
591 if (strcmp (TREE_STRING_POINTER (asmname
),
592 IDENTIFIER_POINTER (newname
)) != 0)
593 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
594 "conflict with __asm__ declaration");
598 /* Otherwise we use what we've got; #pragma extern_prefix is
600 return build_string (IDENTIFIER_LENGTH (newname
),
601 IDENTIFIER_POINTER (newname
));
604 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
608 /* If #pragma extern_prefix is in effect, apply it. */
609 if (pragma_extern_prefix
)
611 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
612 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
614 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
615 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
617 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
619 memcpy (newname
, prefix
, plen
);
620 memcpy (newname
+ plen
, id
, ilen
+ 1);
622 return build_string (plen
+ ilen
, newname
);
630 static void handle_pragma_visibility (cpp_reader
*);
632 static vec
<int> visstack
;
634 /* Push the visibility indicated by STR onto the top of the #pragma
635 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
636 C++ namespace with visibility attribute and 2 for C++ builtin
637 ABI namespace. push_visibility/pop_visibility calls must have
638 matching KIND, it is not allowed to push visibility using one
639 KIND and pop using a different one. */
642 push_visibility (const char *str
, int kind
)
644 visstack
.safe_push (((int) default_visibility
) | (kind
<< 8));
645 if (!strcmp (str
, "default"))
646 default_visibility
= VISIBILITY_DEFAULT
;
647 else if (!strcmp (str
, "internal"))
648 default_visibility
= VISIBILITY_INTERNAL
;
649 else if (!strcmp (str
, "hidden"))
650 default_visibility
= VISIBILITY_HIDDEN
;
651 else if (!strcmp (str
, "protected"))
652 default_visibility
= VISIBILITY_PROTECTED
;
654 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
655 visibility_options
.inpragma
= 1;
658 /* Pop a level of the #pragma visibility stack. Return true if
662 pop_visibility (int kind
)
664 if (!visstack
.length ())
666 if ((visstack
.last () >> 8) != kind
)
669 = (enum symbol_visibility
) (visstack
.pop () & 0xff);
670 visibility_options
.inpragma
671 = visstack
.length () != 0;
675 /* Sets the default visibility for symbols to something other than that
676 specified on the command line. */
679 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
681 /* Form is #pragma GCC visibility push(hidden)|pop */
683 enum cpp_ttype token
;
684 enum { bad
, push
, pop
} action
= bad
;
686 token
= pragma_lex (&x
);
687 if (token
== CPP_NAME
)
689 const char *op
= IDENTIFIER_POINTER (x
);
690 if (!strcmp (op
, "push"))
692 else if (!strcmp (op
, "pop"))
696 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
701 if (! pop_visibility (0))
702 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
706 if (pragma_lex (&x
) != CPP_OPEN_PAREN
)
707 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
708 token
= pragma_lex (&x
);
709 if (token
!= CPP_NAME
)
710 GCC_BAD ("malformed #pragma GCC visibility push");
712 push_visibility (IDENTIFIER_POINTER (x
), 0);
713 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
714 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
717 if (pragma_lex (&x
) != CPP_EOF
)
718 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
722 handle_pragma_diagnostic(cpp_reader
*ARG_UNUSED(dummy
))
724 const char *kind_string
, *option_string
;
725 unsigned int option_index
;
726 enum cpp_ttype token
;
729 struct cl_option_handlers handlers
;
731 token
= pragma_lex (&x
);
732 if (token
!= CPP_NAME
)
733 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
734 kind_string
= IDENTIFIER_POINTER (x
);
735 if (strcmp (kind_string
, "error") == 0)
737 else if (strcmp (kind_string
, "warning") == 0)
739 else if (strcmp (kind_string
, "ignored") == 0)
741 else if (strcmp (kind_string
, "push") == 0)
743 diagnostic_push_diagnostics (global_dc
, input_location
);
746 else if (strcmp (kind_string
, "pop") == 0)
748 diagnostic_pop_diagnostics (global_dc
, input_location
);
752 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
754 token
= pragma_lex (&x
);
755 if (token
!= CPP_STRING
)
756 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
757 option_string
= TREE_STRING_POINTER (x
);
758 set_default_handlers (&handlers
);
759 for (option_index
= 0; option_index
< cl_options_count
; option_index
++)
760 if (strcmp (cl_options
[option_index
].opt_text
, option_string
) == 0)
762 control_warning_option (option_index
, (int) kind
, kind
!= DK_IGNORED
,
763 input_location
, c_family_lang_mask
, &handlers
,
764 &global_options
, &global_options_set
,
768 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
771 /* Parse #pragma GCC target (xxx) to set target specific options. */
773 handle_pragma_target(cpp_reader
*ARG_UNUSED(dummy
))
775 enum cpp_ttype token
;
777 bool close_paren_needed_p
= false;
781 error ("#pragma GCC option is not allowed inside functions");
785 token
= pragma_lex (&x
);
786 if (token
== CPP_OPEN_PAREN
)
788 close_paren_needed_p
= true;
789 token
= pragma_lex (&x
);
792 if (token
!= CPP_STRING
)
794 GCC_BAD ("%<#pragma GCC option%> is not a string");
798 /* Strings are user options. */
801 tree args
= NULL_TREE
;
805 /* Build up the strings now as a tree linked list. Skip empty
807 if (TREE_STRING_LENGTH (x
) > 0)
808 args
= tree_cons (NULL_TREE
, x
, args
);
810 token
= pragma_lex (&x
);
811 while (token
== CPP_COMMA
)
812 token
= pragma_lex (&x
);
814 while (token
== CPP_STRING
);
816 if (close_paren_needed_p
)
818 if (token
== CPP_CLOSE_PAREN
)
819 token
= pragma_lex (&x
);
821 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
822 "not have a final %<)%>");
825 if (token
!= CPP_EOF
)
827 error ("#pragma GCC target string... is badly formed");
831 /* put arguments in the order the user typed them. */
832 args
= nreverse (args
);
834 if (targetm
.target_option
.pragma_parse (args
, NULL_TREE
))
835 current_target_pragma
= args
;
839 /* Handle #pragma GCC optimize to set optimization options. */
841 handle_pragma_optimize (cpp_reader
*ARG_UNUSED(dummy
))
843 enum cpp_ttype token
;
845 bool close_paren_needed_p
= false;
846 tree optimization_previous_node
= optimization_current_node
;
850 error ("#pragma GCC optimize is not allowed inside functions");
854 token
= pragma_lex (&x
);
855 if (token
== CPP_OPEN_PAREN
)
857 close_paren_needed_p
= true;
858 token
= pragma_lex (&x
);
861 if (token
!= CPP_STRING
&& token
!= CPP_NUMBER
)
863 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
867 /* Strings/numbers are user options. */
870 tree args
= NULL_TREE
;
874 /* Build up the numbers/strings now as a list. */
875 if (token
!= CPP_STRING
|| TREE_STRING_LENGTH (x
) > 0)
876 args
= tree_cons (NULL_TREE
, x
, args
);
878 token
= pragma_lex (&x
);
879 while (token
== CPP_COMMA
)
880 token
= pragma_lex (&x
);
882 while (token
== CPP_STRING
|| token
== CPP_NUMBER
);
884 if (close_paren_needed_p
)
886 if (token
== CPP_CLOSE_PAREN
)
887 token
= pragma_lex (&x
);
889 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
890 "not have a final %<)%>");
893 if (token
!= CPP_EOF
)
895 error ("#pragma GCC optimize string... is badly formed");
899 /* put arguments in the order the user typed them. */
900 args
= nreverse (args
);
902 parse_optimize_options (args
, false);
903 current_optimize_pragma
= chainon (current_optimize_pragma
, args
);
904 optimization_current_node
= build_optimization_node (&global_options
);
905 c_cpp_builtins_optimize_pragma (parse_in
,
906 optimization_previous_node
,
907 optimization_current_node
);
911 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
912 both the binary representation of the options and the TREE_LIST of
913 strings that will be added to the function's attribute list. */
914 typedef struct GTY(()) opt_stack
{
915 struct opt_stack
*prev
;
918 tree optimize_binary
;
919 tree optimize_strings
;
922 static GTY(()) struct opt_stack
* options_stack
;
924 /* Handle #pragma GCC push_options to save the current target and optimization
928 handle_pragma_push_options (cpp_reader
*ARG_UNUSED(dummy
))
930 enum cpp_ttype token
;
933 token
= pragma_lex (&x
);
934 if (token
!= CPP_EOF
)
936 warning (OPT_Wpragmas
, "junk at end of %<#pragma push_options%>");
940 opt_stack
*p
= ggc_alloc
<opt_stack
> ();
941 p
->prev
= options_stack
;
944 /* Save optimization and target flags in binary format. */
945 p
->optimize_binary
= build_optimization_node (&global_options
);
946 p
->target_binary
= build_target_option_node (&global_options
);
948 /* Save optimization and target flags in string list format. */
949 p
->optimize_strings
= copy_list (current_optimize_pragma
);
950 p
->target_strings
= copy_list (current_target_pragma
);
953 /* Handle #pragma GCC pop_options to restore the current target and
954 optimization options from a previous push_options. */
957 handle_pragma_pop_options (cpp_reader
*ARG_UNUSED(dummy
))
959 enum cpp_ttype token
;
963 token
= pragma_lex (&x
);
964 if (token
!= CPP_EOF
)
966 warning (OPT_Wpragmas
, "junk at end of %<#pragma pop_options%>");
972 warning (OPT_Wpragmas
,
973 "%<#pragma GCC pop_options%> without a corresponding "
974 "%<#pragma GCC push_options%>");
979 options_stack
= p
->prev
;
981 if (p
->target_binary
!= target_option_current_node
)
983 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, p
->target_binary
);
984 target_option_current_node
= p
->target_binary
;
987 if (p
->optimize_binary
!= optimization_current_node
)
989 tree old_optimize
= optimization_current_node
;
990 cl_optimization_restore (&global_options
,
991 TREE_OPTIMIZATION (p
->optimize_binary
));
992 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
,
994 optimization_current_node
= p
->optimize_binary
;
997 current_target_pragma
= p
->target_strings
;
998 current_optimize_pragma
= p
->optimize_strings
;
1001 /* Handle #pragma GCC reset_options to restore the current target and
1002 optimization options to the original options used on the command line. */
1005 handle_pragma_reset_options (cpp_reader
*ARG_UNUSED(dummy
))
1007 enum cpp_ttype token
;
1009 tree new_optimize
= optimization_default_node
;
1010 tree new_target
= target_option_default_node
;
1012 token
= pragma_lex (&x
);
1013 if (token
!= CPP_EOF
)
1015 warning (OPT_Wpragmas
, "junk at end of %<#pragma reset_options%>");
1019 if (new_target
!= target_option_current_node
)
1021 (void) targetm
.target_option
.pragma_parse (NULL_TREE
, new_target
);
1022 target_option_current_node
= new_target
;
1025 if (new_optimize
!= optimization_current_node
)
1027 tree old_optimize
= optimization_current_node
;
1028 cl_optimization_restore (&global_options
,
1029 TREE_OPTIMIZATION (new_optimize
));
1030 c_cpp_builtins_optimize_pragma (parse_in
, old_optimize
, new_optimize
);
1031 optimization_current_node
= new_optimize
;
1034 current_target_pragma
= NULL_TREE
;
1035 current_optimize_pragma
= NULL_TREE
;
1038 /* Print a plain user-specified message. */
1041 handle_pragma_message (cpp_reader
*ARG_UNUSED(dummy
))
1043 enum cpp_ttype token
;
1044 tree x
, message
= 0;
1046 token
= pragma_lex (&x
);
1047 if (token
== CPP_OPEN_PAREN
)
1049 token
= pragma_lex (&x
);
1050 if (token
== CPP_STRING
)
1053 GCC_BAD ("expected a string after %<#pragma message%>");
1054 if (pragma_lex (&x
) != CPP_CLOSE_PAREN
)
1055 GCC_BAD ("malformed %<#pragma message%>, ignored");
1057 else if (token
== CPP_STRING
)
1060 GCC_BAD ("expected a string after %<#pragma message%>");
1062 gcc_assert (message
);
1064 if (pragma_lex (&x
) != CPP_EOF
)
1065 warning (OPT_Wpragmas
, "junk at end of %<#pragma message%>");
1067 if (TREE_STRING_LENGTH (message
) > 1)
1068 inform (input_location
, "#pragma message: %s", TREE_STRING_POINTER (message
));
1071 /* Mark whether the current location is valid for a STDC pragma. */
1073 static bool valid_location_for_stdc_pragma
;
1076 mark_valid_location_for_stdc_pragma (bool flag
)
1078 valid_location_for_stdc_pragma
= flag
;
1081 /* Return true if the current location is valid for a STDC pragma. */
1084 valid_location_for_stdc_pragma_p (void)
1086 return valid_location_for_stdc_pragma
;
1089 enum pragma_switch_t
{ PRAGMA_ON
, PRAGMA_OFF
, PRAGMA_DEFAULT
, PRAGMA_BAD
};
1091 /* A STDC pragma must appear outside of external declarations or
1092 preceding all explicit declarations and statements inside a compound
1093 statement; its behavior is undefined if used in any other context.
1094 It takes a switch of ON, OFF, or DEFAULT. */
1096 static enum pragma_switch_t
1097 handle_stdc_pragma (const char *pname
)
1101 enum pragma_switch_t ret
;
1103 if (!valid_location_for_stdc_pragma_p ())
1105 warning (OPT_Wpragmas
, "invalid location for %<pragma %s%>, ignored",
1110 if (pragma_lex (&t
) != CPP_NAME
)
1112 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1116 arg
= IDENTIFIER_POINTER (t
);
1118 if (!strcmp (arg
, "ON"))
1120 else if (!strcmp (arg
, "OFF"))
1122 else if (!strcmp (arg
, "DEFAULT"))
1123 ret
= PRAGMA_DEFAULT
;
1126 warning (OPT_Wpragmas
, "malformed %<#pragma %s%>, ignored", pname
);
1130 if (pragma_lex (&t
) != CPP_EOF
)
1132 warning (OPT_Wpragmas
, "junk at end of %<#pragma %s%>", pname
);
1139 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1140 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1141 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1144 handle_pragma_float_const_decimal64 (cpp_reader
*ARG_UNUSED (dummy
))
1146 if (c_dialect_cxx ())
1148 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1149 warning (OPT_Wunknown_pragmas
,
1150 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1155 if (!targetm
.decimal_float_supported_p ())
1157 if (warn_unknown_pragmas
> in_system_header_at (input_location
))
1158 warning (OPT_Wunknown_pragmas
,
1159 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1164 pedwarn (input_location
, OPT_Wpedantic
,
1165 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1167 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1170 set_float_const_decimal64 ();
1173 case PRAGMA_DEFAULT
:
1174 clear_float_const_decimal64 ();
1181 /* A vector of registered pragma callbacks, which is never freed. */
1183 static vec
<internal_pragma_handler
> registered_pragmas
;
1192 static vec
<pragma_ns_name
> registered_pp_pragmas
;
1194 struct omp_pragma_def
{ const char *name
; unsigned int id
; };
1195 static const struct omp_pragma_def oacc_pragmas
[] = {
1196 { "cache", PRAGMA_OACC_CACHE
},
1197 { "data", PRAGMA_OACC_DATA
},
1198 { "enter", PRAGMA_OACC_ENTER_DATA
},
1199 { "exit", PRAGMA_OACC_EXIT_DATA
},
1200 { "kernels", PRAGMA_OACC_KERNELS
},
1201 { "loop", PRAGMA_OACC_LOOP
},
1202 { "parallel", PRAGMA_OACC_PARALLEL
},
1203 { "update", PRAGMA_OACC_UPDATE
},
1204 { "wait", PRAGMA_OACC_WAIT
}
1206 static const struct omp_pragma_def omp_pragmas
[] = {
1207 { "atomic", PRAGMA_OMP_ATOMIC
},
1208 { "barrier", PRAGMA_OMP_BARRIER
},
1209 { "cancel", PRAGMA_OMP_CANCEL
},
1210 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT
},
1211 { "critical", PRAGMA_OMP_CRITICAL
},
1212 { "end", PRAGMA_OMP_END_DECLARE_TARGET
},
1213 { "flush", PRAGMA_OMP_FLUSH
},
1214 { "master", PRAGMA_OMP_MASTER
},
1215 { "ordered", PRAGMA_OMP_ORDERED
},
1216 { "section", PRAGMA_OMP_SECTION
},
1217 { "sections", PRAGMA_OMP_SECTIONS
},
1218 { "single", PRAGMA_OMP_SINGLE
},
1219 { "task", PRAGMA_OMP_TASK
},
1220 { "taskgroup", PRAGMA_OMP_TASKGROUP
},
1221 { "taskwait", PRAGMA_OMP_TASKWAIT
},
1222 { "taskyield", PRAGMA_OMP_TASKYIELD
},
1223 { "threadprivate", PRAGMA_OMP_THREADPRIVATE
}
1225 static const struct omp_pragma_def omp_pragmas_simd
[] = {
1226 { "declare", PRAGMA_OMP_DECLARE_REDUCTION
},
1227 { "distribute", PRAGMA_OMP_DISTRIBUTE
},
1228 { "for", PRAGMA_OMP_FOR
},
1229 { "parallel", PRAGMA_OMP_PARALLEL
},
1230 { "simd", PRAGMA_OMP_SIMD
},
1231 { "target", PRAGMA_OMP_TARGET
},
1232 { "teams", PRAGMA_OMP_TEAMS
},
1236 c_pp_lookup_pragma (unsigned int id
, const char **space
, const char **name
)
1238 const int n_oacc_pragmas
= sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1239 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1240 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1241 / sizeof (*omp_pragmas
);
1244 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1245 if (oacc_pragmas
[i
].id
== id
)
1248 *name
= oacc_pragmas
[i
].name
;
1252 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1253 if (omp_pragmas
[i
].id
== id
)
1256 *name
= omp_pragmas
[i
].name
;
1260 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1261 if (omp_pragmas_simd
[i
].id
== id
)
1264 *name
= omp_pragmas_simd
[i
].name
;
1268 if (id
== PRAGMA_CILK_SIMD
)
1275 if (id
>= PRAGMA_FIRST_EXTERNAL
1276 && (id
< PRAGMA_FIRST_EXTERNAL
+ registered_pp_pragmas
.length ()))
1278 *space
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].space
;
1279 *name
= registered_pp_pragmas
[id
- PRAGMA_FIRST_EXTERNAL
].name
;
1286 /* Front-end wrappers for pragma registration to avoid dragging
1287 cpplib.h in almost everywhere. */
1290 c_register_pragma_1 (const char *space
, const char *name
,
1291 internal_pragma_handler ihandler
, bool allow_expansion
)
1295 if (flag_preprocess_only
)
1297 pragma_ns_name ns_name
;
1299 if (!allow_expansion
)
1302 ns_name
.space
= space
;
1303 ns_name
.name
= name
;
1304 registered_pp_pragmas
.safe_push (ns_name
);
1305 id
= registered_pp_pragmas
.length ();
1306 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1310 registered_pragmas
.safe_push (ihandler
);
1311 id
= registered_pragmas
.length ();
1312 id
+= PRAGMA_FIRST_EXTERNAL
- 1;
1314 /* The C++ front end allocates 6 bits in cp_token; the C front end
1315 allocates 7 bits in c_token. At present this is sufficient. */
1316 gcc_assert (id
< 64);
1319 cpp_register_deferred_pragma (parse_in
, space
, name
, id
,
1320 allow_expansion
, false);
1323 /* Register a C pragma handler, using a space and a name. It disallows pragma
1324 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1326 c_register_pragma (const char *space
, const char *name
,
1327 pragma_handler_1arg handler
)
1329 internal_pragma_handler ihandler
;
1331 ihandler
.handler
.handler_1arg
= handler
;
1332 ihandler
.extra_data
= false;
1333 ihandler
.data
= NULL
;
1334 c_register_pragma_1 (space
, name
, ihandler
, false);
1337 /* Register a C pragma handler, using a space and a name, it also carries an
1338 extra data field which can be used by the handler. It disallows pragma
1339 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1342 c_register_pragma_with_data (const char *space
, const char *name
,
1343 pragma_handler_2arg handler
, void * data
)
1345 internal_pragma_handler ihandler
;
1347 ihandler
.handler
.handler_2arg
= handler
;
1348 ihandler
.extra_data
= true;
1349 ihandler
.data
= data
;
1350 c_register_pragma_1 (space
, name
, ihandler
, false);
1353 /* Register a C pragma handler, using a space and a name. It allows pragma
1354 expansion as in the following example:
1357 #pragma count (NUMBER)
1359 Name expansion is still disallowed. */
1361 c_register_pragma_with_expansion (const char *space
, const char *name
,
1362 pragma_handler_1arg handler
)
1364 internal_pragma_handler ihandler
;
1366 ihandler
.handler
.handler_1arg
= handler
;
1367 ihandler
.extra_data
= false;
1368 ihandler
.data
= NULL
;
1369 c_register_pragma_1 (space
, name
, ihandler
, true);
1372 /* Register a C pragma handler, using a space and a name, it also carries an
1373 extra data field which can be used by the handler. It allows pragma
1374 expansion as in the following example:
1377 #pragma count (NUMBER)
1379 Name expansion is still disallowed. */
1381 c_register_pragma_with_expansion_and_data (const char *space
, const char *name
,
1382 pragma_handler_2arg handler
,
1385 internal_pragma_handler ihandler
;
1387 ihandler
.handler
.handler_2arg
= handler
;
1388 ihandler
.extra_data
= true;
1389 ihandler
.data
= data
;
1390 c_register_pragma_1 (space
, name
, ihandler
, true);
1394 c_invoke_pragma_handler (unsigned int id
)
1396 internal_pragma_handler
*ihandler
;
1397 pragma_handler_1arg handler_1arg
;
1398 pragma_handler_2arg handler_2arg
;
1400 id
-= PRAGMA_FIRST_EXTERNAL
;
1401 ihandler
= ®istered_pragmas
[id
];
1402 if (ihandler
->extra_data
)
1404 handler_2arg
= ihandler
->handler
.handler_2arg
;
1405 handler_2arg (parse_in
, ihandler
->data
);
1409 handler_1arg
= ihandler
->handler
.handler_1arg
;
1410 handler_1arg (parse_in
);
1414 /* Set up front-end pragmas. */
1420 const int n_oacc_pragmas
1421 = sizeof (oacc_pragmas
) / sizeof (*oacc_pragmas
);
1424 for (i
= 0; i
< n_oacc_pragmas
; ++i
)
1425 cpp_register_deferred_pragma (parse_in
, "acc", oacc_pragmas
[i
].name
,
1426 oacc_pragmas
[i
].id
, true, true);
1431 const int n_omp_pragmas
= sizeof (omp_pragmas
) / sizeof (*omp_pragmas
);
1434 for (i
= 0; i
< n_omp_pragmas
; ++i
)
1435 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas
[i
].name
,
1436 omp_pragmas
[i
].id
, true, true);
1438 if (flag_openmp
|| flag_openmp_simd
)
1440 const int n_omp_pragmas_simd
= sizeof (omp_pragmas_simd
)
1441 / sizeof (*omp_pragmas
);
1444 for (i
= 0; i
< n_omp_pragmas_simd
; ++i
)
1445 cpp_register_deferred_pragma (parse_in
, "omp", omp_pragmas_simd
[i
].name
,
1446 omp_pragmas_simd
[i
].id
, true, true);
1450 cpp_register_deferred_pragma (parse_in
, NULL
, "simd", PRAGMA_CILK_SIMD
,
1453 if (!flag_preprocess_only
)
1454 cpp_register_deferred_pragma (parse_in
, "GCC", "pch_preprocess",
1455 PRAGMA_GCC_PCH_PREPROCESS
, false, false);
1457 if (!flag_preprocess_only
)
1458 cpp_register_deferred_pragma (parse_in
, "GCC", "ivdep", PRAGMA_IVDEP
, false,
1461 if (flag_cilkplus
&& !flag_preprocess_only
)
1462 cpp_register_deferred_pragma (parse_in
, "cilk", "grainsize",
1463 PRAGMA_CILK_GRAINSIZE
, true, false);
1465 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1466 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
1468 c_register_pragma (0, "pack", handle_pragma_pack
);
1470 c_register_pragma (0, "weak", handle_pragma_weak
);
1471 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
1473 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic
);
1474 c_register_pragma ("GCC", "target", handle_pragma_target
);
1475 c_register_pragma ("GCC", "optimize", handle_pragma_optimize
);
1476 c_register_pragma ("GCC", "push_options", handle_pragma_push_options
);
1477 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options
);
1478 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options
);
1480 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1481 handle_pragma_float_const_decimal64
);
1483 c_register_pragma_with_expansion (0, "redefine_extname",
1484 handle_pragma_redefine_extname
);
1486 c_register_pragma_with_expansion (0, "message", handle_pragma_message
);
1488 #ifdef REGISTER_TARGET_PRAGMAS
1489 REGISTER_TARGET_PRAGMAS ();
1492 /* Allow plugins to register their own pragmas. */
1493 invoke_plugin_callbacks (PLUGIN_PRAGMAS
, NULL
);
1496 #include "gt-c-family-c-pragma.h"