2014-05-06 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-pragma.c
blobcd90433789230917f67be7294b43af7d3c426661
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
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 "function.h" /* For cfun. FIXME: Does the parser know
26 when it is inside a function, so that
27 we don't have to look at cfun? */
28 #include "cpplib.h"
29 #include "c-pragma.h"
30 #include "flags.h"
31 #include "c-common.h"
32 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
33 this not a target hook?). */
34 #include "vec.h"
35 #include "target.h"
36 #include "diagnostic.h"
37 #include "opts.h"
38 #include "plugin.h"
39 #include "cgraph.h"
41 #define GCC_BAD(gmsgid) \
42 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define GCC_BAD2(gmsgid, arg) \
44 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
46 typedef struct GTY(()) align_stack {
47 int alignment;
48 tree id;
49 struct align_stack * prev;
50 } align_stack;
52 static GTY(()) struct align_stack * alignment_stack;
54 static void handle_pragma_pack (cpp_reader *);
56 /* If we have a "global" #pragma pack(<n>) in effect when the first
57 #pragma pack(push,<n>) is encountered, this stores the value of
58 maximum_field_alignment in effect. When the final pop_alignment()
59 happens, we restore the value to this, not to a value of 0 for
60 maximum_field_alignment. Value is in bits. */
61 static int default_alignment;
62 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
63 ? &default_alignment \
64 : &alignment_stack->alignment) = (ALIGN))
66 static void push_alignment (int, tree);
67 static void pop_alignment (tree);
69 /* Push an alignment value onto the stack. */
70 static void
71 push_alignment (int alignment, tree id)
73 align_stack * entry;
75 entry = ggc_alloc_align_stack ();
77 entry->alignment = alignment;
78 entry->id = id;
79 entry->prev = alignment_stack;
81 /* The current value of maximum_field_alignment is not necessarily
82 0 since there may be a #pragma pack(<n>) in effect; remember it
83 so that we can restore it after the final #pragma pop(). */
84 if (alignment_stack == NULL)
85 default_alignment = maximum_field_alignment;
87 alignment_stack = entry;
89 maximum_field_alignment = alignment;
92 /* Undo a push of an alignment onto the stack. */
93 static void
94 pop_alignment (tree id)
96 align_stack * entry;
98 if (alignment_stack == NULL)
99 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
101 /* If we got an identifier, strip away everything above the target
102 entry so that the next step will restore the state just below it. */
103 if (id)
105 for (entry = alignment_stack; entry; entry = entry->prev)
106 if (entry->id == id)
108 alignment_stack = entry;
109 break;
111 if (entry == NULL)
112 warning (OPT_Wpragmas, "\
113 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
114 , id, id);
117 entry = alignment_stack->prev;
119 maximum_field_alignment = entry ? entry->alignment : default_alignment;
121 alignment_stack = entry;
124 /* #pragma pack ()
125 #pragma pack (N)
127 #pragma pack (push)
128 #pragma pack (push, N)
129 #pragma pack (push, ID)
130 #pragma pack (push, ID, N)
131 #pragma pack (pop)
132 #pragma pack (pop, ID) */
133 static void
134 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
136 tree x, id = 0;
137 int align = -1;
138 enum cpp_ttype token;
139 enum { set, push, pop } action;
141 if (pragma_lex (&x) != CPP_OPEN_PAREN)
142 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
144 token = pragma_lex (&x);
145 if (token == CPP_CLOSE_PAREN)
147 action = set;
148 align = initial_max_fld_align;
150 else if (token == CPP_NUMBER)
152 if (TREE_CODE (x) != INTEGER_CST)
153 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
154 align = TREE_INT_CST_LOW (x);
155 action = set;
156 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
157 GCC_BAD ("malformed %<#pragma pack%> - ignored");
159 else if (token == CPP_NAME)
161 #define GCC_BAD_ACTION do { if (action != pop) \
162 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
163 else \
164 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
165 } while (0)
167 const char *op = IDENTIFIER_POINTER (x);
168 if (!strcmp (op, "push"))
169 action = push;
170 else if (!strcmp (op, "pop"))
171 action = pop;
172 else
173 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
175 while ((token = pragma_lex (&x)) == CPP_COMMA)
177 token = pragma_lex (&x);
178 if (token == CPP_NAME && id == 0)
180 id = x;
182 else if (token == CPP_NUMBER && action == push && align == -1)
184 if (TREE_CODE (x) != INTEGER_CST)
185 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
186 align = TREE_INT_CST_LOW (x);
187 if (align == -1)
188 action = set;
190 else
191 GCC_BAD_ACTION;
194 if (token != CPP_CLOSE_PAREN)
195 GCC_BAD_ACTION;
196 #undef GCC_BAD_ACTION
198 else
199 GCC_BAD ("malformed %<#pragma pack%> - ignored");
201 if (pragma_lex (&x) != CPP_EOF)
202 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
204 if (flag_pack_struct)
205 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
207 if (action != pop)
208 switch (align)
210 case 0:
211 case 1:
212 case 2:
213 case 4:
214 case 8:
215 case 16:
216 align *= BITS_PER_UNIT;
217 break;
218 case -1:
219 if (action == push)
221 align = maximum_field_alignment;
222 break;
224 default:
225 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
228 switch (action)
230 case set: SET_GLOBAL_ALIGNMENT (align); break;
231 case push: push_alignment (align, id); break;
232 case pop: pop_alignment (id); break;
236 typedef struct GTY(()) pending_weak_d
238 tree name;
239 tree value;
240 } pending_weak;
243 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
245 static void apply_pragma_weak (tree, tree);
246 static void handle_pragma_weak (cpp_reader *);
248 static void
249 apply_pragma_weak (tree decl, tree value)
251 if (value)
253 value = build_string (IDENTIFIER_LENGTH (value),
254 IDENTIFIER_POINTER (value));
255 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
256 build_tree_list (NULL, value)),
260 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
261 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
262 && DECL_ASSEMBLER_NAME_SET_P (decl)
263 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
264 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
265 "results in unspecified behavior", decl);
267 declare_weak (decl);
270 void
271 maybe_apply_pragma_weak (tree decl)
273 tree id;
274 int i;
275 pending_weak *pe;
277 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
279 /* No weak symbols pending, take the short-cut. */
280 if (vec_safe_is_empty (pending_weaks))
281 return;
282 /* If it's not visible outside this file, it doesn't matter whether
283 it's weak. */
284 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
285 return;
286 /* If it's not a function or a variable, it can't be weak.
287 FIXME: what kinds of things are visible outside this file but
288 aren't functions or variables? Should this be an assert instead? */
289 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
290 return;
292 if (DECL_ASSEMBLER_NAME_SET_P (decl))
293 id = DECL_ASSEMBLER_NAME (decl);
294 else
296 id = DECL_ASSEMBLER_NAME (decl);
297 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
300 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
301 if (id == pe->name)
303 apply_pragma_weak (decl, pe->value);
304 pending_weaks->unordered_remove (i);
305 break;
309 /* Process all "#pragma weak A = B" directives where we have not seen
310 a decl for A. */
311 void
312 maybe_apply_pending_pragma_weaks (void)
314 tree alias_id, id, decl;
315 int i;
316 pending_weak *pe;
317 symtab_node target;
319 if (vec_safe_is_empty (pending_weaks))
320 return;
322 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
324 alias_id = pe->name;
325 id = pe->value;
327 if (id == NULL)
328 continue;
330 target = symtab_node_for_asm (id);
331 decl = build_decl (UNKNOWN_LOCATION,
332 target ? TREE_CODE (target->symbol.decl) : FUNCTION_DECL,
333 alias_id, default_function_type);
335 DECL_ARTIFICIAL (decl) = 1;
336 TREE_PUBLIC (decl) = 1;
337 DECL_WEAK (decl) = 1;
338 if (TREE_CODE (decl) == VAR_DECL)
339 TREE_STATIC (decl) = 1;
340 if (!target)
342 error ("%q+D aliased to undefined symbol %qE",
343 decl, id);
344 continue;
347 assemble_alias (decl, id);
351 /* #pragma weak name [= value] */
352 static void
353 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
355 tree name, value, x, decl;
356 enum cpp_ttype t;
358 value = 0;
360 if (pragma_lex (&name) != CPP_NAME)
361 GCC_BAD ("malformed #pragma weak, ignored");
362 t = pragma_lex (&x);
363 if (t == CPP_EQ)
365 if (pragma_lex (&value) != CPP_NAME)
366 GCC_BAD ("malformed #pragma weak, ignored");
367 t = pragma_lex (&x);
369 if (t != CPP_EOF)
370 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
372 decl = identifier_global_value (name);
373 if (decl && DECL_P (decl))
375 apply_pragma_weak (decl, value);
376 if (value)
377 assemble_alias (decl, value);
379 else
381 pending_weak pe = {name, value};
382 vec_safe_push (pending_weaks, pe);
386 /* GCC supports two #pragma directives for renaming the external
387 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
388 compatibility with the Solaris and VMS system headers. GCC also
389 has its own notation for this, __asm__("name") annotations.
391 Corner cases of these features and their interaction:
393 1) Both pragmas silently apply only to declarations with external
394 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
395 do not have this restriction.
397 2) In C++, both #pragmas silently apply only to extern "C" declarations.
398 Asm labels do not have this restriction.
400 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
401 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
402 new name is different, a warning issues and the name does not change.
404 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
405 *not* the DECL_ASSEMBLER_NAME.
407 5) If #pragma extern_prefix is in effect and a declaration occurs
408 with an __asm__ name, the #pragma extern_prefix is silently
409 ignored for that declaration.
411 6) If #pragma extern_prefix and #pragma redefine_extname apply to
412 the same declaration, whichever triggered first wins, and a warning
413 is issued. (We would like to have #pragma redefine_extname always
414 win, but it can appear either before or after the declaration, and
415 if it appears afterward, we have no way of knowing whether a modified
416 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
418 typedef struct GTY(()) pending_redefinition_d {
419 tree oldname;
420 tree newname;
421 } pending_redefinition;
424 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
426 static void handle_pragma_redefine_extname (cpp_reader *);
428 /* #pragma redefine_extname oldname newname */
429 static void
430 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
432 tree oldname, newname, decls, x;
433 enum cpp_ttype t;
434 bool found;
436 if (pragma_lex (&oldname) != CPP_NAME)
437 GCC_BAD ("malformed #pragma redefine_extname, ignored");
438 if (pragma_lex (&newname) != CPP_NAME)
439 GCC_BAD ("malformed #pragma redefine_extname, ignored");
440 t = pragma_lex (&x);
441 if (t != CPP_EOF)
442 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
444 found = false;
445 for (decls = c_linkage_bindings (oldname);
446 decls; )
448 tree decl;
449 if (TREE_CODE (decls) == TREE_LIST)
451 decl = TREE_VALUE (decls);
452 decls = TREE_CHAIN (decls);
454 else
456 decl = decls;
457 decls = NULL_TREE;
460 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
461 && (TREE_CODE (decl) == FUNCTION_DECL
462 || TREE_CODE (decl) == VAR_DECL))
464 found = true;
465 if (DECL_ASSEMBLER_NAME_SET_P (decl))
467 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
468 name = targetm.strip_name_encoding (name);
470 if (strcmp (name, IDENTIFIER_POINTER (newname)))
471 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
472 "conflict with previous rename");
474 else
475 change_decl_assembler_name (decl, newname);
479 if (!found)
480 /* We have to add this to the rename list even if there's already
481 a global value that doesn't meet the above criteria, because in
482 C++ "struct foo {...};" puts "foo" in the current namespace but
483 does *not* conflict with a subsequent declaration of a function
484 or variable foo. See g++.dg/other/pragma-re-2.C. */
485 add_to_renaming_pragma_list (oldname, newname);
488 /* This is called from here and from ia64.c. */
489 void
490 add_to_renaming_pragma_list (tree oldname, tree newname)
492 unsigned ix;
493 pending_redefinition *p;
495 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
496 if (oldname == p->oldname)
498 if (p->newname != newname)
499 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
500 "conflict with previous #pragma redefine_extname");
501 return;
504 pending_redefinition e = {oldname, newname};
505 vec_safe_push (pending_redefine_extname, e);
508 /* The current prefix set by #pragma extern_prefix. */
509 GTY(()) tree pragma_extern_prefix;
511 /* Hook from the front ends to apply the results of one of the preceding
512 pragmas that rename variables. */
514 tree
515 maybe_apply_renaming_pragma (tree decl, tree asmname)
517 unsigned ix;
518 pending_redefinition *p;
520 /* The renaming pragmas are only applied to declarations with
521 external linkage. */
522 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
523 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
524 || !has_c_linkage (decl))
525 return asmname;
527 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
528 but we may warn about a rename that conflicts. */
529 if (DECL_ASSEMBLER_NAME_SET_P (decl))
531 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
532 oldname = targetm.strip_name_encoding (oldname);
534 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
535 warning (OPT_Wpragmas, "asm declaration ignored due to "
536 "conflict with previous rename");
538 /* Take any pending redefine_extname off the list. */
539 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
540 if (DECL_NAME (decl) == p->oldname)
542 /* Only warn if there is a conflict. */
543 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
544 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
545 "conflict with previous rename");
547 pending_redefine_extname->unordered_remove (ix);
548 break;
550 return 0;
553 /* Find out if we have a pending #pragma redefine_extname. */
554 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
555 if (DECL_NAME (decl) == p->oldname)
557 tree newname = p->newname;
558 pending_redefine_extname->unordered_remove (ix);
560 /* If we already have an asmname, #pragma redefine_extname is
561 ignored (with a warning if it conflicts). */
562 if (asmname)
564 if (strcmp (TREE_STRING_POINTER (asmname),
565 IDENTIFIER_POINTER (newname)) != 0)
566 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
567 "conflict with __asm__ declaration");
568 return asmname;
571 /* Otherwise we use what we've got; #pragma extern_prefix is
572 silently ignored. */
573 return build_string (IDENTIFIER_LENGTH (newname),
574 IDENTIFIER_POINTER (newname));
577 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
578 if (asmname)
579 return asmname;
581 /* If #pragma extern_prefix is in effect, apply it. */
582 if (pragma_extern_prefix)
584 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
585 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
587 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
588 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
590 char *newname = (char *) alloca (plen + ilen + 1);
592 memcpy (newname, prefix, plen);
593 memcpy (newname + plen, id, ilen + 1);
595 return build_string (plen + ilen, newname);
598 /* Nada. */
599 return 0;
603 static void handle_pragma_visibility (cpp_reader *);
605 static vec<int> visstack;
607 /* Push the visibility indicated by STR onto the top of the #pragma
608 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
609 C++ namespace with visibility attribute and 2 for C++ builtin
610 ABI namespace. push_visibility/pop_visibility calls must have
611 matching KIND, it is not allowed to push visibility using one
612 KIND and pop using a different one. */
614 void
615 push_visibility (const char *str, int kind)
617 visstack.safe_push (((int) default_visibility) | (kind << 8));
618 if (!strcmp (str, "default"))
619 default_visibility = VISIBILITY_DEFAULT;
620 else if (!strcmp (str, "internal"))
621 default_visibility = VISIBILITY_INTERNAL;
622 else if (!strcmp (str, "hidden"))
623 default_visibility = VISIBILITY_HIDDEN;
624 else if (!strcmp (str, "protected"))
625 default_visibility = VISIBILITY_PROTECTED;
626 else
627 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
628 visibility_options.inpragma = 1;
631 /* Pop a level of the #pragma visibility stack. Return true if
632 successful. */
634 bool
635 pop_visibility (int kind)
637 if (!visstack.length ())
638 return false;
639 if ((visstack.last () >> 8) != kind)
640 return false;
641 default_visibility
642 = (enum symbol_visibility) (visstack.pop () & 0xff);
643 visibility_options.inpragma
644 = visstack.length () != 0;
645 return true;
648 /* Sets the default visibility for symbols to something other than that
649 specified on the command line. */
651 static void
652 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
654 /* Form is #pragma GCC visibility push(hidden)|pop */
655 tree x;
656 enum cpp_ttype token;
657 enum { bad, push, pop } action = bad;
659 token = pragma_lex (&x);
660 if (token == CPP_NAME)
662 const char *op = IDENTIFIER_POINTER (x);
663 if (!strcmp (op, "push"))
664 action = push;
665 else if (!strcmp (op, "pop"))
666 action = pop;
668 if (bad == action)
669 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
670 else
672 if (pop == action)
674 if (! pop_visibility (0))
675 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
677 else
679 if (pragma_lex (&x) != CPP_OPEN_PAREN)
680 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
681 token = pragma_lex (&x);
682 if (token != CPP_NAME)
683 GCC_BAD ("malformed #pragma GCC visibility push");
684 else
685 push_visibility (IDENTIFIER_POINTER (x), 0);
686 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
687 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
690 if (pragma_lex (&x) != CPP_EOF)
691 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
694 static void
695 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
697 const char *kind_string, *option_string;
698 unsigned int option_index;
699 enum cpp_ttype token;
700 diagnostic_t kind;
701 tree x;
702 struct cl_option_handlers handlers;
704 token = pragma_lex (&x);
705 if (token != CPP_NAME)
706 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
707 kind_string = IDENTIFIER_POINTER (x);
708 if (strcmp (kind_string, "error") == 0)
709 kind = DK_ERROR;
710 else if (strcmp (kind_string, "warning") == 0)
711 kind = DK_WARNING;
712 else if (strcmp (kind_string, "ignored") == 0)
713 kind = DK_IGNORED;
714 else if (strcmp (kind_string, "push") == 0)
716 diagnostic_push_diagnostics (global_dc, input_location);
717 return;
719 else if (strcmp (kind_string, "pop") == 0)
721 diagnostic_pop_diagnostics (global_dc, input_location);
722 return;
724 else
725 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
727 token = pragma_lex (&x);
728 if (token != CPP_STRING)
729 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
730 option_string = TREE_STRING_POINTER (x);
731 set_default_handlers (&handlers);
732 for (option_index = 0; option_index < cl_options_count; option_index++)
733 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
735 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
736 input_location, c_family_lang_mask, &handlers,
737 &global_options, &global_options_set,
738 global_dc);
739 return;
741 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
744 /* Parse #pragma GCC target (xxx) to set target specific options. */
745 static void
746 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
748 enum cpp_ttype token;
749 tree x;
750 bool close_paren_needed_p = false;
752 if (cfun)
754 error ("#pragma GCC option is not allowed inside functions");
755 return;
758 token = pragma_lex (&x);
759 if (token == CPP_OPEN_PAREN)
761 close_paren_needed_p = true;
762 token = pragma_lex (&x);
765 if (token != CPP_STRING)
767 GCC_BAD ("%<#pragma GCC option%> is not a string");
768 return;
771 /* Strings are user options. */
772 else
774 tree args = NULL_TREE;
778 /* Build up the strings now as a tree linked list. Skip empty
779 strings. */
780 if (TREE_STRING_LENGTH (x) > 0)
781 args = tree_cons (NULL_TREE, x, args);
783 token = pragma_lex (&x);
784 while (token == CPP_COMMA)
785 token = pragma_lex (&x);
787 while (token == CPP_STRING);
789 if (close_paren_needed_p)
791 if (token == CPP_CLOSE_PAREN)
792 token = pragma_lex (&x);
793 else
794 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
795 "not have a final %<)%>");
798 if (token != CPP_EOF)
800 error ("#pragma GCC target string... is badly formed");
801 return;
804 /* put arguments in the order the user typed them. */
805 args = nreverse (args);
807 if (targetm.target_option.pragma_parse (args, NULL_TREE))
808 current_target_pragma = args;
812 /* Handle #pragma GCC optimize to set optimization options. */
813 static void
814 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
816 enum cpp_ttype token;
817 tree x;
818 bool close_paren_needed_p = false;
819 tree optimization_previous_node = optimization_current_node;
821 if (cfun)
823 error ("#pragma GCC optimize is not allowed inside functions");
824 return;
827 token = pragma_lex (&x);
828 if (token == CPP_OPEN_PAREN)
830 close_paren_needed_p = true;
831 token = pragma_lex (&x);
834 if (token != CPP_STRING && token != CPP_NUMBER)
836 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
837 return;
840 /* Strings/numbers are user options. */
841 else
843 tree args = NULL_TREE;
847 /* Build up the numbers/strings now as a list. */
848 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
849 args = tree_cons (NULL_TREE, x, args);
851 token = pragma_lex (&x);
852 while (token == CPP_COMMA)
853 token = pragma_lex (&x);
855 while (token == CPP_STRING || token == CPP_NUMBER);
857 if (close_paren_needed_p)
859 if (token == CPP_CLOSE_PAREN)
860 token = pragma_lex (&x);
861 else
862 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
863 "not have a final %<)%>");
866 if (token != CPP_EOF)
868 error ("#pragma GCC optimize string... is badly formed");
869 return;
872 /* put arguments in the order the user typed them. */
873 args = nreverse (args);
875 parse_optimize_options (args, false);
876 current_optimize_pragma = chainon (current_optimize_pragma, args);
877 optimization_current_node = build_optimization_node ();
878 c_cpp_builtins_optimize_pragma (parse_in,
879 optimization_previous_node,
880 optimization_current_node);
884 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
885 both the binary representation of the options and the TREE_LIST of
886 strings that will be added to the function's attribute list. */
887 typedef struct GTY(()) opt_stack {
888 struct opt_stack *prev;
889 tree target_binary;
890 tree target_strings;
891 tree optimize_binary;
892 tree optimize_strings;
893 } opt_stack;
895 static GTY(()) struct opt_stack * options_stack;
897 /* Handle #pragma GCC push_options to save the current target and optimization
898 options. */
900 static void
901 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
903 enum cpp_ttype token;
904 tree x = 0;
905 opt_stack *p;
907 token = pragma_lex (&x);
908 if (token != CPP_EOF)
910 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
911 return;
914 p = ggc_alloc_opt_stack ();
915 p->prev = options_stack;
916 options_stack = p;
918 /* Save optimization and target flags in binary format. */
919 p->optimize_binary = build_optimization_node ();
920 p->target_binary = build_target_option_node ();
922 /* Save optimization and target flags in string list format. */
923 p->optimize_strings = copy_list (current_optimize_pragma);
924 p->target_strings = copy_list (current_target_pragma);
927 /* Handle #pragma GCC pop_options to restore the current target and
928 optimization options from a previous push_options. */
930 static void
931 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
933 enum cpp_ttype token;
934 tree x = 0;
935 opt_stack *p;
937 token = pragma_lex (&x);
938 if (token != CPP_EOF)
940 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
941 return;
944 if (! options_stack)
946 warning (OPT_Wpragmas,
947 "%<#pragma GCC pop_options%> without a corresponding "
948 "%<#pragma GCC push_options%>");
949 return;
952 p = options_stack;
953 options_stack = p->prev;
955 if (p->target_binary != target_option_current_node)
957 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
958 target_option_current_node = p->target_binary;
961 if (p->optimize_binary != optimization_current_node)
963 tree old_optimize = optimization_current_node;
964 cl_optimization_restore (&global_options,
965 TREE_OPTIMIZATION (p->optimize_binary));
966 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
967 p->optimize_binary);
968 optimization_current_node = p->optimize_binary;
971 current_target_pragma = p->target_strings;
972 current_optimize_pragma = p->optimize_strings;
975 /* Handle #pragma GCC reset_options to restore the current target and
976 optimization options to the original options used on the command line. */
978 static void
979 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
981 enum cpp_ttype token;
982 tree x = 0;
983 tree new_optimize = optimization_default_node;
984 tree new_target = target_option_default_node;
986 token = pragma_lex (&x);
987 if (token != CPP_EOF)
989 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
990 return;
993 if (new_target != target_option_current_node)
995 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
996 target_option_current_node = new_target;
999 if (new_optimize != optimization_current_node)
1001 tree old_optimize = optimization_current_node;
1002 cl_optimization_restore (&global_options,
1003 TREE_OPTIMIZATION (new_optimize));
1004 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1005 optimization_current_node = new_optimize;
1008 current_target_pragma = NULL_TREE;
1009 current_optimize_pragma = NULL_TREE;
1012 /* Print a plain user-specified message. */
1014 static void
1015 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1017 enum cpp_ttype token;
1018 tree x, message = 0;
1020 token = pragma_lex (&x);
1021 if (token == CPP_OPEN_PAREN)
1023 token = pragma_lex (&x);
1024 if (token == CPP_STRING)
1025 message = x;
1026 else
1027 GCC_BAD ("expected a string after %<#pragma message%>");
1028 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1029 GCC_BAD ("malformed %<#pragma message%>, ignored");
1031 else if (token == CPP_STRING)
1032 message = x;
1033 else
1034 GCC_BAD ("expected a string after %<#pragma message%>");
1036 gcc_assert (message);
1038 if (pragma_lex (&x) != CPP_EOF)
1039 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1041 if (TREE_STRING_LENGTH (message) > 1)
1042 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1045 /* Mark whether the current location is valid for a STDC pragma. */
1047 static bool valid_location_for_stdc_pragma;
1049 void
1050 mark_valid_location_for_stdc_pragma (bool flag)
1052 valid_location_for_stdc_pragma = flag;
1055 /* Return true if the current location is valid for a STDC pragma. */
1057 bool
1058 valid_location_for_stdc_pragma_p (void)
1060 return valid_location_for_stdc_pragma;
1063 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1065 /* A STDC pragma must appear outside of external declarations or
1066 preceding all explicit declarations and statements inside a compound
1067 statement; its behavior is undefined if used in any other context.
1068 It takes a switch of ON, OFF, or DEFAULT. */
1070 static enum pragma_switch_t
1071 handle_stdc_pragma (const char *pname)
1073 const char *arg;
1074 tree t;
1075 enum pragma_switch_t ret;
1077 if (!valid_location_for_stdc_pragma_p ())
1079 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1080 pname);
1081 return PRAGMA_BAD;
1084 if (pragma_lex (&t) != CPP_NAME)
1086 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1087 return PRAGMA_BAD;
1090 arg = IDENTIFIER_POINTER (t);
1092 if (!strcmp (arg, "ON"))
1093 ret = PRAGMA_ON;
1094 else if (!strcmp (arg, "OFF"))
1095 ret = PRAGMA_OFF;
1096 else if (!strcmp (arg, "DEFAULT"))
1097 ret = PRAGMA_DEFAULT;
1098 else
1100 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1101 return PRAGMA_BAD;
1104 if (pragma_lex (&t) != CPP_EOF)
1106 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1107 return PRAGMA_BAD;
1110 return ret;
1113 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1114 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1115 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1117 static void
1118 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1120 if (c_dialect_cxx ())
1122 if (warn_unknown_pragmas > in_system_header)
1123 warning (OPT_Wunknown_pragmas,
1124 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1125 " for C++");
1126 return;
1129 if (!targetm.decimal_float_supported_p ())
1131 if (warn_unknown_pragmas > in_system_header)
1132 warning (OPT_Wunknown_pragmas,
1133 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1134 " on this target");
1135 return;
1138 pedwarn (input_location, OPT_Wpedantic,
1139 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1141 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1143 case PRAGMA_ON:
1144 set_float_const_decimal64 ();
1145 break;
1146 case PRAGMA_OFF:
1147 case PRAGMA_DEFAULT:
1148 clear_float_const_decimal64 ();
1149 break;
1150 case PRAGMA_BAD:
1151 break;
1155 /* A vector of registered pragma callbacks, which is never freed. */
1157 static vec<internal_pragma_handler> registered_pragmas;
1159 typedef struct
1161 const char *space;
1162 const char *name;
1163 } pragma_ns_name;
1166 static vec<pragma_ns_name> registered_pp_pragmas;
1168 struct omp_pragma_def { const char *name; unsigned int id; };
1169 static const struct omp_pragma_def omp_pragmas[] = {
1170 { "atomic", PRAGMA_OMP_ATOMIC },
1171 { "barrier", PRAGMA_OMP_BARRIER },
1172 { "critical", PRAGMA_OMP_CRITICAL },
1173 { "flush", PRAGMA_OMP_FLUSH },
1174 { "for", PRAGMA_OMP_FOR },
1175 { "master", PRAGMA_OMP_MASTER },
1176 { "ordered", PRAGMA_OMP_ORDERED },
1177 { "parallel", PRAGMA_OMP_PARALLEL },
1178 { "section", PRAGMA_OMP_SECTION },
1179 { "sections", PRAGMA_OMP_SECTIONS },
1180 { "single", PRAGMA_OMP_SINGLE },
1181 { "task", PRAGMA_OMP_TASK },
1182 { "taskwait", PRAGMA_OMP_TASKWAIT },
1183 { "taskyield", PRAGMA_OMP_TASKYIELD },
1184 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1187 void
1188 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1190 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1191 int i;
1193 for (i = 0; i < n_omp_pragmas; ++i)
1194 if (omp_pragmas[i].id == id)
1196 *space = "omp";
1197 *name = omp_pragmas[i].name;
1198 return;
1201 if (id >= PRAGMA_FIRST_EXTERNAL
1202 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1204 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1205 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1206 return;
1209 gcc_unreachable ();
1212 /* Front-end wrappers for pragma registration to avoid dragging
1213 cpplib.h in almost everywhere. */
1215 static void
1216 c_register_pragma_1 (const char *space, const char *name,
1217 internal_pragma_handler ihandler, bool allow_expansion)
1219 unsigned id;
1221 if (flag_preprocess_only)
1223 pragma_ns_name ns_name;
1225 if (!allow_expansion)
1226 return;
1228 ns_name.space = space;
1229 ns_name.name = name;
1230 registered_pp_pragmas.safe_push (ns_name);
1231 id = registered_pp_pragmas.length ();
1232 id += PRAGMA_FIRST_EXTERNAL - 1;
1234 else
1236 registered_pragmas.safe_push (ihandler);
1237 id = registered_pragmas.length ();
1238 id += PRAGMA_FIRST_EXTERNAL - 1;
1240 /* The C++ front end allocates 6 bits in cp_token; the C front end
1241 allocates 7 bits in c_token. At present this is sufficient. */
1242 gcc_assert (id < 64);
1245 cpp_register_deferred_pragma (parse_in, space, name, id,
1246 allow_expansion, false);
1249 /* Register a C pragma handler, using a space and a name. It disallows pragma
1250 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1251 void
1252 c_register_pragma (const char *space, const char *name,
1253 pragma_handler_1arg handler)
1255 internal_pragma_handler ihandler;
1257 ihandler.handler.handler_1arg = handler;
1258 ihandler.extra_data = false;
1259 ihandler.data = NULL;
1260 c_register_pragma_1 (space, name, ihandler, false);
1263 /* Register a C pragma handler, using a space and a name, it also carries an
1264 extra data field which can be used by the handler. It disallows pragma
1265 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1266 instead). */
1267 void
1268 c_register_pragma_with_data (const char *space, const char *name,
1269 pragma_handler_2arg handler, void * data)
1271 internal_pragma_handler ihandler;
1273 ihandler.handler.handler_2arg = handler;
1274 ihandler.extra_data = true;
1275 ihandler.data = data;
1276 c_register_pragma_1 (space, name, ihandler, false);
1279 /* Register a C pragma handler, using a space and a name. It allows pragma
1280 expansion as in the following example:
1282 #define NUMBER 10
1283 #pragma count (NUMBER)
1285 Name expansion is still disallowed. */
1286 void
1287 c_register_pragma_with_expansion (const char *space, const char *name,
1288 pragma_handler_1arg handler)
1290 internal_pragma_handler ihandler;
1292 ihandler.handler.handler_1arg = handler;
1293 ihandler.extra_data = false;
1294 ihandler.data = NULL;
1295 c_register_pragma_1 (space, name, ihandler, true);
1298 /* Register a C pragma handler, using a space and a name, it also carries an
1299 extra data field which can be used by the handler. It allows pragma
1300 expansion as in the following example:
1302 #define NUMBER 10
1303 #pragma count (NUMBER)
1305 Name expansion is still disallowed. */
1306 void
1307 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1308 pragma_handler_2arg handler,
1309 void *data)
1311 internal_pragma_handler ihandler;
1313 ihandler.handler.handler_2arg = handler;
1314 ihandler.extra_data = true;
1315 ihandler.data = data;
1316 c_register_pragma_1 (space, name, ihandler, true);
1319 void
1320 c_invoke_pragma_handler (unsigned int id)
1322 internal_pragma_handler *ihandler;
1323 pragma_handler_1arg handler_1arg;
1324 pragma_handler_2arg handler_2arg;
1326 id -= PRAGMA_FIRST_EXTERNAL;
1327 ihandler = &registered_pragmas[id];
1328 if (ihandler->extra_data)
1330 handler_2arg = ihandler->handler.handler_2arg;
1331 handler_2arg (parse_in, ihandler->data);
1333 else
1335 handler_1arg = ihandler->handler.handler_1arg;
1336 handler_1arg (parse_in);
1340 /* Set up front-end pragmas. */
1341 void
1342 init_pragma (void)
1344 if (flag_openmp)
1346 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1347 int i;
1349 for (i = 0; i < n_omp_pragmas; ++i)
1350 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1351 omp_pragmas[i].id, true, true);
1354 if (!flag_preprocess_only)
1355 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1356 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1358 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1359 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1360 #else
1361 c_register_pragma (0, "pack", handle_pragma_pack);
1362 #endif
1363 c_register_pragma (0, "weak", handle_pragma_weak);
1364 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1366 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1367 c_register_pragma ("GCC", "target", handle_pragma_target);
1368 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1369 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1370 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1371 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1373 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1374 handle_pragma_float_const_decimal64);
1376 c_register_pragma_with_expansion (0, "redefine_extname",
1377 handle_pragma_redefine_extname);
1379 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1381 #ifdef REGISTER_TARGET_PRAGMAS
1382 REGISTER_TARGET_PRAGMAS ();
1383 #endif
1385 /* Allow plugins to register their own pragmas. */
1386 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1389 #include "gt-c-family-c-pragma.h"