* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / c-family / c-pragma.c
blobda928b2abddf1d1749a7f0b6b7967a8169e6745f
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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "attribs.h"
27 #include "varasm.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? */
31 #include "cpplib.h"
32 #include "c-pragma.h"
33 #include "flags.h"
34 #include "c-common.h"
35 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
36 this not a target hook?). */
37 #include "vec.h"
38 #include "target.h"
39 #include "diagnostic.h"
40 #include "opts.h"
41 #include "plugin.h"
42 #include "cgraph.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 {
50 int alignment;
51 tree id;
52 struct align_stack * prev;
53 } align_stack;
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. */
73 static void
74 push_alignment (int alignment, tree id)
76 align_stack * entry = ggc_alloc<align_stack> ();
78 entry->alignment = alignment;
79 entry->id = id;
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. */
94 static void
95 pop_alignment (tree id)
97 align_stack * entry;
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. */
104 if (id)
106 for (entry = alignment_stack; entry; entry = entry->prev)
107 if (entry->id == id)
109 alignment_stack = entry;
110 break;
112 if (entry == NULL)
113 warning (OPT_Wpragmas, "\
114 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
115 , id, id);
118 entry = alignment_stack->prev;
120 maximum_field_alignment = entry ? entry->alignment : default_alignment;
122 alignment_stack = entry;
125 /* #pragma pack ()
126 #pragma pack (N)
128 #pragma pack (push)
129 #pragma pack (push, N)
130 #pragma pack (push, ID)
131 #pragma pack (push, ID, N)
132 #pragma pack (pop)
133 #pragma pack (pop, ID) */
134 static void
135 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
137 tree x, id = 0;
138 int align = -1;
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)
148 action = set;
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);
156 action = set;
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"); \
164 else \
165 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
166 } while (0)
168 const char *op = IDENTIFIER_POINTER (x);
169 if (!strcmp (op, "push"))
170 action = push;
171 else if (!strcmp (op, "pop"))
172 action = pop;
173 else
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)
181 id = x;
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);
188 if (align == -1)
189 action = set;
191 else
192 GCC_BAD_ACTION;
195 if (token != CPP_CLOSE_PAREN)
196 GCC_BAD_ACTION;
197 #undef GCC_BAD_ACTION
199 else
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");
208 if (action != pop)
209 switch (align)
211 case 0:
212 case 1:
213 case 2:
214 case 4:
215 case 8:
216 case 16:
217 align *= BITS_PER_UNIT;
218 break;
219 case -1:
220 if (action == push)
222 align = maximum_field_alignment;
223 break;
225 default:
226 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
229 switch (action)
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
239 tree name;
240 tree value;
241 } pending_weak;
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 *);
249 static void
250 apply_pragma_weak (tree decl, tree value)
252 if (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);
268 declare_weak (decl);
271 void
272 maybe_apply_pragma_weak (tree decl)
274 tree id;
275 int i;
276 pending_weak *pe;
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))
282 return;
283 /* If it's not visible outside this file, it doesn't matter whether
284 it's weak. */
285 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
286 return;
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)
291 return;
293 if (DECL_ASSEMBLER_NAME_SET_P (decl))
294 id = DECL_ASSEMBLER_NAME (decl);
295 else
297 id = DECL_ASSEMBLER_NAME (decl);
298 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
301 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
302 if (id == pe->name)
304 apply_pragma_weak (decl, pe->value);
305 pending_weaks->unordered_remove (i);
306 break;
310 /* Process all "#pragma weak A = B" directives where we have not seen
311 a decl for A. */
312 void
313 maybe_apply_pending_pragma_weaks (void)
315 tree alias_id, id, decl;
316 int i;
317 pending_weak *pe;
318 symtab_node *target;
320 if (vec_safe_is_empty (pending_weaks))
321 return;
323 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
325 alias_id = pe->name;
326 id = pe->value;
328 if (id == NULL)
329 continue;
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;
341 if (!target)
343 error ("%q+D aliased to undefined symbol %qE",
344 decl, id);
345 continue;
348 assemble_alias (decl, id);
352 /* #pragma weak name [= value] */
353 static void
354 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
356 tree name, value, x, decl;
357 enum cpp_ttype t;
359 value = 0;
361 if (pragma_lex (&name) != CPP_NAME)
362 GCC_BAD ("malformed #pragma weak, ignored");
363 t = pragma_lex (&x);
364 if (t == CPP_EQ)
366 if (pragma_lex (&value) != CPP_NAME)
367 GCC_BAD ("malformed #pragma weak, ignored");
368 t = pragma_lex (&x);
370 if (t != CPP_EOF)
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);
377 if (value)
379 DECL_EXTERNAL (decl) = 0;
380 if (TREE_CODE (decl) == VAR_DECL)
381 TREE_STATIC (decl) = 1;
382 assemble_alias (decl, value);
385 else
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 {
425 tree oldname;
426 tree newname;
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 */
435 static void
436 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
438 tree oldname, newname, decls, x;
439 enum cpp_ttype t;
440 bool found;
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");
446 t = pragma_lex (&x);
447 if (t != CPP_EOF)
448 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
450 found = false;
451 for (decls = c_linkage_bindings (oldname);
452 decls; )
454 tree decl;
455 if (TREE_CODE (decls) == TREE_LIST)
457 decl = TREE_VALUE (decls);
458 decls = TREE_CHAIN (decls);
460 else
462 decl = decls;
463 decls = NULL_TREE;
466 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
467 && (TREE_CODE (decl) == FUNCTION_DECL
468 || TREE_CODE (decl) == VAR_DECL))
470 found = true;
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");
480 else
481 symtab->change_decl_assembler_name (decl, newname);
485 if (!found)
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. */
495 void
496 add_to_renaming_pragma_list (tree oldname, tree newname)
498 unsigned ix;
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");
507 return;
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. */
520 tree
521 maybe_apply_renaming_pragma (tree decl, tree asmname)
523 unsigned ix;
524 pending_redefinition *p;
526 /* The renaming pragmas are only applied to declarations with
527 external linkage. */
528 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
529 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
530 || !has_c_linkage (decl))
531 return asmname;
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);
554 break;
556 return 0;
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). */
568 if (asmname)
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");
574 return asmname;
577 /* Otherwise we use what we've got; #pragma extern_prefix is
578 silently ignored. */
579 return build_string (IDENTIFIER_LENGTH (newname),
580 IDENTIFIER_POINTER (newname));
583 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
584 if (asmname)
585 return asmname;
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);
604 /* Nada. */
605 return 0;
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. */
620 void
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;
632 else
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
638 successful. */
640 bool
641 pop_visibility (int kind)
643 if (!visstack.length ())
644 return false;
645 if ((visstack.last () >> 8) != kind)
646 return false;
647 default_visibility
648 = (enum symbol_visibility) (visstack.pop () & 0xff);
649 visibility_options.inpragma
650 = visstack.length () != 0;
651 return true;
654 /* Sets the default visibility for symbols to something other than that
655 specified on the command line. */
657 static void
658 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
660 /* Form is #pragma GCC visibility push(hidden)|pop */
661 tree x;
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"))
670 action = push;
671 else if (!strcmp (op, "pop"))
672 action = pop;
674 if (bad == action)
675 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
676 else
678 if (pop == action)
680 if (! pop_visibility (0))
681 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
683 else
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");
690 else
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%>");
700 static void
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;
706 diagnostic_t kind;
707 tree x;
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)
715 kind = DK_ERROR;
716 else if (strcmp (kind_string, "warning") == 0)
717 kind = DK_WARNING;
718 else if (strcmp (kind_string, "ignored") == 0)
719 kind = DK_IGNORED;
720 else if (strcmp (kind_string, "push") == 0)
722 diagnostic_push_diagnostics (global_dc, input_location);
723 return;
725 else if (strcmp (kind_string, "pop") == 0)
727 diagnostic_pop_diagnostics (global_dc, input_location);
728 return;
730 else
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,
744 global_dc);
745 return;
747 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
750 /* Parse #pragma GCC target (xxx) to set target specific options. */
751 static void
752 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
754 enum cpp_ttype token;
755 tree x;
756 bool close_paren_needed_p = false;
758 if (cfun)
760 error ("#pragma GCC option is not allowed inside functions");
761 return;
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");
774 return;
777 /* Strings are user options. */
778 else
780 tree args = NULL_TREE;
784 /* Build up the strings now as a tree linked list. Skip empty
785 strings. */
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);
799 else
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");
807 return;
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. */
819 static void
820 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
822 enum cpp_ttype token;
823 tree x;
824 bool close_paren_needed_p = false;
825 tree optimization_previous_node = optimization_current_node;
827 if (cfun)
829 error ("#pragma GCC optimize is not allowed inside functions");
830 return;
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");
843 return;
846 /* Strings/numbers are user options. */
847 else
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);
867 else
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");
875 return;
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;
895 tree target_binary;
896 tree target_strings;
897 tree optimize_binary;
898 tree optimize_strings;
899 } opt_stack;
901 static GTY(()) struct opt_stack * options_stack;
903 /* Handle #pragma GCC push_options to save the current target and optimization
904 options. */
906 static void
907 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
909 enum cpp_ttype token;
910 tree x = 0;
912 token = pragma_lex (&x);
913 if (token != CPP_EOF)
915 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
916 return;
919 opt_stack *p = ggc_alloc<opt_stack> ();
920 p->prev = options_stack;
921 options_stack = p;
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. */
935 static void
936 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
938 enum cpp_ttype token;
939 tree x = 0;
940 opt_stack *p;
942 token = pragma_lex (&x);
943 if (token != CPP_EOF)
945 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
946 return;
949 if (! options_stack)
951 warning (OPT_Wpragmas,
952 "%<#pragma GCC pop_options%> without a corresponding "
953 "%<#pragma GCC push_options%>");
954 return;
957 p = options_stack;
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,
972 p->optimize_binary);
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. */
983 static void
984 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
986 enum cpp_ttype token;
987 tree x = 0;
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%>");
995 return;
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. */
1019 static void
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)
1030 message = x;
1031 else
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)
1037 message = x;
1038 else
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;
1054 void
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. */
1062 bool
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)
1078 const char *arg;
1079 tree t;
1080 enum pragma_switch_t ret;
1082 if (!valid_location_for_stdc_pragma_p ())
1084 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1085 pname);
1086 return PRAGMA_BAD;
1089 if (pragma_lex (&t) != CPP_NAME)
1091 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1092 return PRAGMA_BAD;
1095 arg = IDENTIFIER_POINTER (t);
1097 if (!strcmp (arg, "ON"))
1098 ret = PRAGMA_ON;
1099 else if (!strcmp (arg, "OFF"))
1100 ret = PRAGMA_OFF;
1101 else if (!strcmp (arg, "DEFAULT"))
1102 ret = PRAGMA_DEFAULT;
1103 else
1105 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1106 return PRAGMA_BAD;
1109 if (pragma_lex (&t) != CPP_EOF)
1111 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1112 return PRAGMA_BAD;
1115 return ret;
1118 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1119 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1120 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1122 static void
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"
1130 " for C++");
1131 return;
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"
1139 " on this target");
1140 return;
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"))
1148 case PRAGMA_ON:
1149 set_float_const_decimal64 ();
1150 break;
1151 case PRAGMA_OFF:
1152 case PRAGMA_DEFAULT:
1153 clear_float_const_decimal64 ();
1154 break;
1155 case PRAGMA_BAD:
1156 break;
1160 /* A vector of registered pragma callbacks, which is never freed. */
1162 static vec<internal_pragma_handler> registered_pragmas;
1164 typedef struct
1166 const char *space;
1167 const char *name;
1168 } pragma_ns_name;
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 },
1203 void
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);
1209 int i;
1211 for (i = 0; i < n_omp_pragmas; ++i)
1212 if (omp_pragmas[i].id == id)
1214 *space = "omp";
1215 *name = omp_pragmas[i].name;
1216 return;
1219 for (i = 0; i < n_omp_pragmas_simd; ++i)
1220 if (omp_pragmas_simd[i].id == id)
1222 *space = "omp";
1223 *name = omp_pragmas_simd[i].name;
1224 return;
1227 if (id == PRAGMA_CILK_SIMD)
1229 *space = NULL;
1230 *name = "simd";
1231 return;
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;
1239 return;
1242 gcc_unreachable ();
1245 /* Front-end wrappers for pragma registration to avoid dragging
1246 cpplib.h in almost everywhere. */
1248 static void
1249 c_register_pragma_1 (const char *space, const char *name,
1250 internal_pragma_handler ihandler, bool allow_expansion)
1252 unsigned id;
1254 if (flag_preprocess_only)
1256 pragma_ns_name ns_name;
1258 if (!allow_expansion)
1259 return;
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;
1267 else
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). */
1284 void
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
1299 instead). */
1300 void
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:
1315 #define NUMBER 10
1316 #pragma count (NUMBER)
1318 Name expansion is still disallowed. */
1319 void
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:
1335 #define NUMBER 10
1336 #pragma count (NUMBER)
1338 Name expansion is still disallowed. */
1339 void
1340 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1341 pragma_handler_2arg handler,
1342 void *data)
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);
1352 void
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 = &registered_pragmas[id];
1361 if (ihandler->extra_data)
1363 handler_2arg = ihandler->handler.handler_2arg;
1364 handler_2arg (parse_in, ihandler->data);
1366 else
1368 handler_1arg = ihandler->handler.handler_1arg;
1369 handler_1arg (parse_in);
1373 /* Set up front-end pragmas. */
1374 void
1375 init_pragma (void)
1377 if (flag_openmp)
1379 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1380 int i;
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);
1390 int i;
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);
1397 if (flag_cilkplus)
1398 cpp_register_deferred_pragma (parse_in, NULL, "simd", PRAGMA_CILK_SIMD,
1399 true, false);
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,
1407 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);
1415 #else
1416 c_register_pragma (0, "pack", handle_pragma_pack);
1417 #endif
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 ();
1438 #endif
1440 /* Allow plugins to register their own pragmas. */
1441 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1444 #include "gt-c-family-c-pragma.h"