Makefile.def (install-target-libsanitizer): Depend on install-target-libstdc++-v3.
[official-gcc.git] / gcc / c-family / c-pragma.c
blob7d8a1a6058cb76254640ef6f22a057cc96d06b54
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 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
263 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
264 "results in unspecified behavior", decl);
266 declare_weak (decl);
269 void
270 maybe_apply_pragma_weak (tree decl)
272 tree id;
273 int i;
274 pending_weak *pe;
276 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
278 /* No weak symbols pending, take the short-cut. */
279 if (!pending_weaks)
280 return;
281 /* If it's not visible outside this file, it doesn't matter whether
282 it's weak. */
283 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
284 return;
285 /* If it's not a function or a variable, it can't be weak.
286 FIXME: what kinds of things are visible outside this file but
287 aren't functions or variables? Should this be an assert instead? */
288 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
289 return;
291 id = DECL_ASSEMBLER_NAME (decl);
293 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
294 if (id == pe->name)
296 apply_pragma_weak (decl, pe->value);
297 pending_weaks->unordered_remove (i);
298 break;
302 /* Process all "#pragma weak A = B" directives where we have not seen
303 a decl for A. */
304 void
305 maybe_apply_pending_pragma_weaks (void)
307 tree alias_id, id, decl;
308 int i;
309 pending_weak *pe;
310 symtab_node target;
312 if (!pending_weaks)
313 return;
315 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
317 alias_id = pe->name;
318 id = pe->value;
320 if (id == NULL)
321 continue;
323 target = symtab_node_for_asm (id);
324 decl = build_decl (UNKNOWN_LOCATION,
325 target ? TREE_CODE (target->symbol.decl) : FUNCTION_DECL,
326 alias_id, default_function_type);
328 DECL_ARTIFICIAL (decl) = 1;
329 TREE_PUBLIC (decl) = 1;
330 DECL_WEAK (decl) = 1;
331 if (TREE_CODE (decl) == VAR_DECL)
332 TREE_STATIC (decl) = 1;
333 if (!target)
335 error ("%q+D aliased to undefined symbol %qE",
336 decl, id);
337 continue;
340 assemble_alias (decl, id);
344 /* #pragma weak name [= value] */
345 static void
346 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
348 tree name, value, x, decl;
349 enum cpp_ttype t;
351 value = 0;
353 if (pragma_lex (&name) != CPP_NAME)
354 GCC_BAD ("malformed #pragma weak, ignored");
355 t = pragma_lex (&x);
356 if (t == CPP_EQ)
358 if (pragma_lex (&value) != CPP_NAME)
359 GCC_BAD ("malformed #pragma weak, ignored");
360 t = pragma_lex (&x);
362 if (t != CPP_EOF)
363 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
365 decl = identifier_global_value (name);
366 if (decl && DECL_P (decl))
368 apply_pragma_weak (decl, value);
369 if (value)
370 assemble_alias (decl, value);
372 else
374 pending_weak pe = {name, value};
375 vec_safe_push (pending_weaks, pe);
379 /* GCC supports two #pragma directives for renaming the external
380 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
381 compatibility with the Solaris and VMS system headers. GCC also
382 has its own notation for this, __asm__("name") annotations.
384 Corner cases of these features and their interaction:
386 1) Both pragmas silently apply only to declarations with external
387 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
388 do not have this restriction.
390 2) In C++, both #pragmas silently apply only to extern "C" declarations.
391 Asm labels do not have this restriction.
393 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
394 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
395 new name is different, a warning issues and the name does not change.
397 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
398 *not* the DECL_ASSEMBLER_NAME.
400 5) If #pragma extern_prefix is in effect and a declaration occurs
401 with an __asm__ name, the #pragma extern_prefix is silently
402 ignored for that declaration.
404 6) If #pragma extern_prefix and #pragma redefine_extname apply to
405 the same declaration, whichever triggered first wins, and a warning
406 is issued. (We would like to have #pragma redefine_extname always
407 win, but it can appear either before or after the declaration, and
408 if it appears afterward, we have no way of knowing whether a modified
409 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
411 typedef struct GTY(()) pending_redefinition_d {
412 tree oldname;
413 tree newname;
414 } pending_redefinition;
417 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
419 static void handle_pragma_redefine_extname (cpp_reader *);
421 /* #pragma redefine_extname oldname newname */
422 static void
423 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
425 tree oldname, newname, decls, x;
426 enum cpp_ttype t;
427 bool found;
429 if (pragma_lex (&oldname) != CPP_NAME)
430 GCC_BAD ("malformed #pragma redefine_extname, ignored");
431 if (pragma_lex (&newname) != CPP_NAME)
432 GCC_BAD ("malformed #pragma redefine_extname, ignored");
433 t = pragma_lex (&x);
434 if (t != CPP_EOF)
435 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
437 found = false;
438 for (decls = c_linkage_bindings (oldname);
439 decls; )
441 tree decl;
442 if (TREE_CODE (decls) == TREE_LIST)
444 decl = TREE_VALUE (decls);
445 decls = TREE_CHAIN (decls);
447 else
449 decl = decls;
450 decls = NULL_TREE;
453 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
454 && (TREE_CODE (decl) == FUNCTION_DECL
455 || TREE_CODE (decl) == VAR_DECL))
457 found = true;
458 if (DECL_ASSEMBLER_NAME_SET_P (decl))
460 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
461 name = targetm.strip_name_encoding (name);
463 if (strcmp (name, IDENTIFIER_POINTER (newname)))
464 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
465 "conflict with previous rename");
467 else
468 change_decl_assembler_name (decl, newname);
472 if (!found)
473 /* We have to add this to the rename list even if there's already
474 a global value that doesn't meet the above criteria, because in
475 C++ "struct foo {...};" puts "foo" in the current namespace but
476 does *not* conflict with a subsequent declaration of a function
477 or variable foo. See g++.dg/other/pragma-re-2.C. */
478 add_to_renaming_pragma_list (oldname, newname);
481 /* This is called from here and from ia64.c. */
482 void
483 add_to_renaming_pragma_list (tree oldname, tree newname)
485 unsigned ix;
486 pending_redefinition *p;
488 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
489 if (oldname == p->oldname)
491 if (p->newname != newname)
492 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
493 "conflict with previous #pragma redefine_extname");
494 return;
497 pending_redefinition e = {oldname, newname};
498 vec_safe_push (pending_redefine_extname, e);
501 /* The current prefix set by #pragma extern_prefix. */
502 GTY(()) tree pragma_extern_prefix;
504 /* Hook from the front ends to apply the results of one of the preceding
505 pragmas that rename variables. */
507 tree
508 maybe_apply_renaming_pragma (tree decl, tree asmname)
510 unsigned ix;
511 pending_redefinition *p;
513 /* The renaming pragmas are only applied to declarations with
514 external linkage. */
515 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
516 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
517 || !has_c_linkage (decl))
518 return asmname;
520 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
521 but we may warn about a rename that conflicts. */
522 if (DECL_ASSEMBLER_NAME_SET_P (decl))
524 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
525 oldname = targetm.strip_name_encoding (oldname);
527 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
528 warning (OPT_Wpragmas, "asm declaration ignored due to "
529 "conflict with previous rename");
531 /* Take any pending redefine_extname off the list. */
532 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
533 if (DECL_NAME (decl) == p->oldname)
535 /* Only warn if there is a conflict. */
536 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
537 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
538 "conflict with previous rename");
540 pending_redefine_extname->unordered_remove (ix);
541 break;
543 return 0;
546 /* Find out if we have a pending #pragma redefine_extname. */
547 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
548 if (DECL_NAME (decl) == p->oldname)
550 tree newname = p->newname;
551 pending_redefine_extname->unordered_remove (ix);
553 /* If we already have an asmname, #pragma redefine_extname is
554 ignored (with a warning if it conflicts). */
555 if (asmname)
557 if (strcmp (TREE_STRING_POINTER (asmname),
558 IDENTIFIER_POINTER (newname)) != 0)
559 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
560 "conflict with __asm__ declaration");
561 return asmname;
564 /* Otherwise we use what we've got; #pragma extern_prefix is
565 silently ignored. */
566 return build_string (IDENTIFIER_LENGTH (newname),
567 IDENTIFIER_POINTER (newname));
570 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
571 if (asmname)
572 return asmname;
574 /* If #pragma extern_prefix is in effect, apply it. */
575 if (pragma_extern_prefix)
577 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
578 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
580 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
581 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
583 char *newname = (char *) alloca (plen + ilen + 1);
585 memcpy (newname, prefix, plen);
586 memcpy (newname + plen, id, ilen + 1);
588 return build_string (plen + ilen, newname);
591 /* Nada. */
592 return 0;
596 static void handle_pragma_visibility (cpp_reader *);
598 static vec<int> visstack;
600 /* Push the visibility indicated by STR onto the top of the #pragma
601 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
602 C++ namespace with visibility attribute and 2 for C++ builtin
603 ABI namespace. push_visibility/pop_visibility calls must have
604 matching KIND, it is not allowed to push visibility using one
605 KIND and pop using a different one. */
607 void
608 push_visibility (const char *str, int kind)
610 visstack.safe_push (((int) default_visibility) | (kind << 8));
611 if (!strcmp (str, "default"))
612 default_visibility = VISIBILITY_DEFAULT;
613 else if (!strcmp (str, "internal"))
614 default_visibility = VISIBILITY_INTERNAL;
615 else if (!strcmp (str, "hidden"))
616 default_visibility = VISIBILITY_HIDDEN;
617 else if (!strcmp (str, "protected"))
618 default_visibility = VISIBILITY_PROTECTED;
619 else
620 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
621 visibility_options.inpragma = 1;
624 /* Pop a level of the #pragma visibility stack. Return true if
625 successful. */
627 bool
628 pop_visibility (int kind)
630 if (!visstack.length ())
631 return false;
632 if ((visstack.last () >> 8) != kind)
633 return false;
634 default_visibility
635 = (enum symbol_visibility) (visstack.pop () & 0xff);
636 visibility_options.inpragma
637 = visstack.length () != 0;
638 return true;
641 /* Sets the default visibility for symbols to something other than that
642 specified on the command line. */
644 static void
645 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
647 /* Form is #pragma GCC visibility push(hidden)|pop */
648 tree x;
649 enum cpp_ttype token;
650 enum { bad, push, pop } action = bad;
652 token = pragma_lex (&x);
653 if (token == CPP_NAME)
655 const char *op = IDENTIFIER_POINTER (x);
656 if (!strcmp (op, "push"))
657 action = push;
658 else if (!strcmp (op, "pop"))
659 action = pop;
661 if (bad == action)
662 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
663 else
665 if (pop == action)
667 if (! pop_visibility (0))
668 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
670 else
672 if (pragma_lex (&x) != CPP_OPEN_PAREN)
673 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
674 token = pragma_lex (&x);
675 if (token != CPP_NAME)
676 GCC_BAD ("malformed #pragma GCC visibility push");
677 else
678 push_visibility (IDENTIFIER_POINTER (x), 0);
679 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
680 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
683 if (pragma_lex (&x) != CPP_EOF)
684 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
687 static void
688 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
690 const char *kind_string, *option_string;
691 unsigned int option_index;
692 enum cpp_ttype token;
693 diagnostic_t kind;
694 tree x;
695 struct cl_option_handlers handlers;
697 token = pragma_lex (&x);
698 if (token != CPP_NAME)
699 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
700 kind_string = IDENTIFIER_POINTER (x);
701 if (strcmp (kind_string, "error") == 0)
702 kind = DK_ERROR;
703 else if (strcmp (kind_string, "warning") == 0)
704 kind = DK_WARNING;
705 else if (strcmp (kind_string, "ignored") == 0)
706 kind = DK_IGNORED;
707 else if (strcmp (kind_string, "push") == 0)
709 diagnostic_push_diagnostics (global_dc, input_location);
710 return;
712 else if (strcmp (kind_string, "pop") == 0)
714 diagnostic_pop_diagnostics (global_dc, input_location);
715 return;
717 else
718 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
720 token = pragma_lex (&x);
721 if (token != CPP_STRING)
722 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
723 option_string = TREE_STRING_POINTER (x);
724 set_default_handlers (&handlers);
725 for (option_index = 0; option_index < cl_options_count; option_index++)
726 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
728 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
729 input_location, c_family_lang_mask, &handlers,
730 &global_options, &global_options_set,
731 global_dc);
732 return;
734 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
737 /* Parse #pragma GCC target (xxx) to set target specific options. */
738 static void
739 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
741 enum cpp_ttype token;
742 tree x;
743 bool close_paren_needed_p = false;
745 if (cfun)
747 error ("#pragma GCC option is not allowed inside functions");
748 return;
751 token = pragma_lex (&x);
752 if (token == CPP_OPEN_PAREN)
754 close_paren_needed_p = true;
755 token = pragma_lex (&x);
758 if (token != CPP_STRING)
760 GCC_BAD ("%<#pragma GCC option%> is not a string");
761 return;
764 /* Strings are user options. */
765 else
767 tree args = NULL_TREE;
771 /* Build up the strings now as a tree linked list. Skip empty
772 strings. */
773 if (TREE_STRING_LENGTH (x) > 0)
774 args = tree_cons (NULL_TREE, x, args);
776 token = pragma_lex (&x);
777 while (token == CPP_COMMA)
778 token = pragma_lex (&x);
780 while (token == CPP_STRING);
782 if (close_paren_needed_p)
784 if (token == CPP_CLOSE_PAREN)
785 token = pragma_lex (&x);
786 else
787 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
788 "not have a final %<)%>");
791 if (token != CPP_EOF)
793 error ("#pragma GCC target string... is badly formed");
794 return;
797 /* put arguments in the order the user typed them. */
798 args = nreverse (args);
800 if (targetm.target_option.pragma_parse (args, NULL_TREE))
801 current_target_pragma = args;
805 /* Handle #pragma GCC optimize to set optimization options. */
806 static void
807 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
809 enum cpp_ttype token;
810 tree x;
811 bool close_paren_needed_p = false;
812 tree optimization_previous_node = optimization_current_node;
814 if (cfun)
816 error ("#pragma GCC optimize is not allowed inside functions");
817 return;
820 token = pragma_lex (&x);
821 if (token == CPP_OPEN_PAREN)
823 close_paren_needed_p = true;
824 token = pragma_lex (&x);
827 if (token != CPP_STRING && token != CPP_NUMBER)
829 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
830 return;
833 /* Strings/numbers are user options. */
834 else
836 tree args = NULL_TREE;
840 /* Build up the numbers/strings now as a list. */
841 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
842 args = tree_cons (NULL_TREE, x, args);
844 token = pragma_lex (&x);
845 while (token == CPP_COMMA)
846 token = pragma_lex (&x);
848 while (token == CPP_STRING || token == CPP_NUMBER);
850 if (close_paren_needed_p)
852 if (token == CPP_CLOSE_PAREN)
853 token = pragma_lex (&x);
854 else
855 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
856 "not have a final %<)%>");
859 if (token != CPP_EOF)
861 error ("#pragma GCC optimize string... is badly formed");
862 return;
865 /* put arguments in the order the user typed them. */
866 args = nreverse (args);
868 parse_optimize_options (args, false);
869 current_optimize_pragma = chainon (current_optimize_pragma, args);
870 optimization_current_node = build_optimization_node ();
871 c_cpp_builtins_optimize_pragma (parse_in,
872 optimization_previous_node,
873 optimization_current_node);
877 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
878 both the binary representation of the options and the TREE_LIST of
879 strings that will be added to the function's attribute list. */
880 typedef struct GTY(()) opt_stack {
881 struct opt_stack *prev;
882 tree target_binary;
883 tree target_strings;
884 tree optimize_binary;
885 tree optimize_strings;
886 } opt_stack;
888 static GTY(()) struct opt_stack * options_stack;
890 /* Handle #pragma GCC push_options to save the current target and optimization
891 options. */
893 static void
894 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
896 enum cpp_ttype token;
897 tree x = 0;
898 opt_stack *p;
900 token = pragma_lex (&x);
901 if (token != CPP_EOF)
903 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
904 return;
907 p = ggc_alloc_opt_stack ();
908 p->prev = options_stack;
909 options_stack = p;
911 /* Save optimization and target flags in binary format. */
912 p->optimize_binary = build_optimization_node ();
913 p->target_binary = build_target_option_node ();
915 /* Save optimization and target flags in string list format. */
916 p->optimize_strings = copy_list (current_optimize_pragma);
917 p->target_strings = copy_list (current_target_pragma);
920 /* Handle #pragma GCC pop_options to restore the current target and
921 optimization options from a previous push_options. */
923 static void
924 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
926 enum cpp_ttype token;
927 tree x = 0;
928 opt_stack *p;
930 token = pragma_lex (&x);
931 if (token != CPP_EOF)
933 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
934 return;
937 if (! options_stack)
939 warning (OPT_Wpragmas,
940 "%<#pragma GCC pop_options%> without a corresponding "
941 "%<#pragma GCC push_options%>");
942 return;
945 p = options_stack;
946 options_stack = p->prev;
948 if (p->target_binary != target_option_current_node)
950 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
951 target_option_current_node = p->target_binary;
954 if (p->optimize_binary != optimization_current_node)
956 tree old_optimize = optimization_current_node;
957 cl_optimization_restore (&global_options,
958 TREE_OPTIMIZATION (p->optimize_binary));
959 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
960 p->optimize_binary);
961 optimization_current_node = p->optimize_binary;
964 current_target_pragma = p->target_strings;
965 current_optimize_pragma = p->optimize_strings;
968 /* Handle #pragma GCC reset_options to restore the current target and
969 optimization options to the original options used on the command line. */
971 static void
972 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
974 enum cpp_ttype token;
975 tree x = 0;
976 tree new_optimize = optimization_default_node;
977 tree new_target = target_option_default_node;
979 token = pragma_lex (&x);
980 if (token != CPP_EOF)
982 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
983 return;
986 if (new_target != target_option_current_node)
988 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
989 target_option_current_node = new_target;
992 if (new_optimize != optimization_current_node)
994 tree old_optimize = optimization_current_node;
995 cl_optimization_restore (&global_options,
996 TREE_OPTIMIZATION (new_optimize));
997 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
998 optimization_current_node = new_optimize;
1001 current_target_pragma = NULL_TREE;
1002 current_optimize_pragma = NULL_TREE;
1005 /* Print a plain user-specified message. */
1007 static void
1008 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1010 enum cpp_ttype token;
1011 tree x, message = 0;
1013 token = pragma_lex (&x);
1014 if (token == CPP_OPEN_PAREN)
1016 token = pragma_lex (&x);
1017 if (token == CPP_STRING)
1018 message = x;
1019 else
1020 GCC_BAD ("expected a string after %<#pragma message%>");
1021 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1022 GCC_BAD ("malformed %<#pragma message%>, ignored");
1024 else if (token == CPP_STRING)
1025 message = x;
1026 else
1027 GCC_BAD ("expected a string after %<#pragma message%>");
1029 gcc_assert (message);
1031 if (pragma_lex (&x) != CPP_EOF)
1032 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1034 if (TREE_STRING_LENGTH (message) > 1)
1035 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1038 /* Mark whether the current location is valid for a STDC pragma. */
1040 static bool valid_location_for_stdc_pragma;
1042 void
1043 mark_valid_location_for_stdc_pragma (bool flag)
1045 valid_location_for_stdc_pragma = flag;
1048 /* Return true if the current location is valid for a STDC pragma. */
1050 bool
1051 valid_location_for_stdc_pragma_p (void)
1053 return valid_location_for_stdc_pragma;
1056 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1058 /* A STDC pragma must appear outside of external declarations or
1059 preceding all explicit declarations and statements inside a compound
1060 statement; its behavior is undefined if used in any other context.
1061 It takes a switch of ON, OFF, or DEFAULT. */
1063 static enum pragma_switch_t
1064 handle_stdc_pragma (const char *pname)
1066 const char *arg;
1067 tree t;
1068 enum pragma_switch_t ret;
1070 if (!valid_location_for_stdc_pragma_p ())
1072 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1073 pname);
1074 return PRAGMA_BAD;
1077 if (pragma_lex (&t) != CPP_NAME)
1079 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1080 return PRAGMA_BAD;
1083 arg = IDENTIFIER_POINTER (t);
1085 if (!strcmp (arg, "ON"))
1086 ret = PRAGMA_ON;
1087 else if (!strcmp (arg, "OFF"))
1088 ret = PRAGMA_OFF;
1089 else if (!strcmp (arg, "DEFAULT"))
1090 ret = PRAGMA_DEFAULT;
1091 else
1093 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1094 return PRAGMA_BAD;
1097 if (pragma_lex (&t) != CPP_EOF)
1099 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1100 return PRAGMA_BAD;
1103 return ret;
1106 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1107 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1108 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1110 static void
1111 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1113 if (c_dialect_cxx ())
1115 if (warn_unknown_pragmas > in_system_header)
1116 warning (OPT_Wunknown_pragmas,
1117 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1118 " for C++");
1119 return;
1122 if (!targetm.decimal_float_supported_p ())
1124 if (warn_unknown_pragmas > in_system_header)
1125 warning (OPT_Wunknown_pragmas,
1126 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1127 " on this target");
1128 return;
1131 pedwarn (input_location, OPT_Wpedantic,
1132 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1134 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1136 case PRAGMA_ON:
1137 set_float_const_decimal64 ();
1138 break;
1139 case PRAGMA_OFF:
1140 case PRAGMA_DEFAULT:
1141 clear_float_const_decimal64 ();
1142 break;
1143 case PRAGMA_BAD:
1144 break;
1148 /* A vector of registered pragma callbacks, which is never freed. */
1150 static vec<internal_pragma_handler> registered_pragmas;
1152 typedef struct
1154 const char *space;
1155 const char *name;
1156 } pragma_ns_name;
1159 static vec<pragma_ns_name> registered_pp_pragmas;
1161 struct omp_pragma_def { const char *name; unsigned int id; };
1162 static const struct omp_pragma_def omp_pragmas[] = {
1163 { "atomic", PRAGMA_OMP_ATOMIC },
1164 { "barrier", PRAGMA_OMP_BARRIER },
1165 { "critical", PRAGMA_OMP_CRITICAL },
1166 { "flush", PRAGMA_OMP_FLUSH },
1167 { "for", PRAGMA_OMP_FOR },
1168 { "master", PRAGMA_OMP_MASTER },
1169 { "ordered", PRAGMA_OMP_ORDERED },
1170 { "parallel", PRAGMA_OMP_PARALLEL },
1171 { "section", PRAGMA_OMP_SECTION },
1172 { "sections", PRAGMA_OMP_SECTIONS },
1173 { "single", PRAGMA_OMP_SINGLE },
1174 { "task", PRAGMA_OMP_TASK },
1175 { "taskwait", PRAGMA_OMP_TASKWAIT },
1176 { "taskyield", PRAGMA_OMP_TASKYIELD },
1177 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1180 void
1181 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1183 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1184 int i;
1186 for (i = 0; i < n_omp_pragmas; ++i)
1187 if (omp_pragmas[i].id == id)
1189 *space = "omp";
1190 *name = omp_pragmas[i].name;
1191 return;
1194 if (id >= PRAGMA_FIRST_EXTERNAL
1195 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1197 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1198 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1199 return;
1202 gcc_unreachable ();
1205 /* Front-end wrappers for pragma registration to avoid dragging
1206 cpplib.h in almost everywhere. */
1208 static void
1209 c_register_pragma_1 (const char *space, const char *name,
1210 internal_pragma_handler ihandler, bool allow_expansion)
1212 unsigned id;
1214 if (flag_preprocess_only)
1216 pragma_ns_name ns_name;
1218 if (!allow_expansion)
1219 return;
1221 ns_name.space = space;
1222 ns_name.name = name;
1223 registered_pp_pragmas.safe_push (ns_name);
1224 id = registered_pp_pragmas.length ();
1225 id += PRAGMA_FIRST_EXTERNAL - 1;
1227 else
1229 registered_pragmas.safe_push (ihandler);
1230 id = registered_pragmas.length ();
1231 id += PRAGMA_FIRST_EXTERNAL - 1;
1233 /* The C++ front end allocates 6 bits in cp_token; the C front end
1234 allocates 7 bits in c_token. At present this is sufficient. */
1235 gcc_assert (id < 64);
1238 cpp_register_deferred_pragma (parse_in, space, name, id,
1239 allow_expansion, false);
1242 /* Register a C pragma handler, using a space and a name. It disallows pragma
1243 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1244 void
1245 c_register_pragma (const char *space, const char *name,
1246 pragma_handler_1arg handler)
1248 internal_pragma_handler ihandler;
1250 ihandler.handler.handler_1arg = handler;
1251 ihandler.extra_data = false;
1252 ihandler.data = NULL;
1253 c_register_pragma_1 (space, name, ihandler, false);
1256 /* Register a C pragma handler, using a space and a name, it also carries an
1257 extra data field which can be used by the handler. It disallows pragma
1258 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1259 instead). */
1260 void
1261 c_register_pragma_with_data (const char *space, const char *name,
1262 pragma_handler_2arg handler, void * data)
1264 internal_pragma_handler ihandler;
1266 ihandler.handler.handler_2arg = handler;
1267 ihandler.extra_data = true;
1268 ihandler.data = data;
1269 c_register_pragma_1 (space, name, ihandler, false);
1272 /* Register a C pragma handler, using a space and a name. It allows pragma
1273 expansion as in the following example:
1275 #define NUMBER 10
1276 #pragma count (NUMBER)
1278 Name expansion is still disallowed. */
1279 void
1280 c_register_pragma_with_expansion (const char *space, const char *name,
1281 pragma_handler_1arg handler)
1283 internal_pragma_handler ihandler;
1285 ihandler.handler.handler_1arg = handler;
1286 ihandler.extra_data = false;
1287 ihandler.data = NULL;
1288 c_register_pragma_1 (space, name, ihandler, true);
1291 /* Register a C pragma handler, using a space and a name, it also carries an
1292 extra data field which can be used by the handler. It allows pragma
1293 expansion as in the following example:
1295 #define NUMBER 10
1296 #pragma count (NUMBER)
1298 Name expansion is still disallowed. */
1299 void
1300 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1301 pragma_handler_2arg handler,
1302 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, true);
1312 void
1313 c_invoke_pragma_handler (unsigned int id)
1315 internal_pragma_handler *ihandler;
1316 pragma_handler_1arg handler_1arg;
1317 pragma_handler_2arg handler_2arg;
1319 id -= PRAGMA_FIRST_EXTERNAL;
1320 ihandler = &registered_pragmas[id];
1321 if (ihandler->extra_data)
1323 handler_2arg = ihandler->handler.handler_2arg;
1324 handler_2arg (parse_in, ihandler->data);
1326 else
1328 handler_1arg = ihandler->handler.handler_1arg;
1329 handler_1arg (parse_in);
1333 /* Set up front-end pragmas. */
1334 void
1335 init_pragma (void)
1337 if (flag_openmp)
1339 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1340 int i;
1342 for (i = 0; i < n_omp_pragmas; ++i)
1343 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1344 omp_pragmas[i].id, true, true);
1347 if (!flag_preprocess_only)
1348 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1349 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1351 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1352 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1353 #else
1354 c_register_pragma (0, "pack", handle_pragma_pack);
1355 #endif
1356 c_register_pragma (0, "weak", handle_pragma_weak);
1357 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1359 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1360 c_register_pragma ("GCC", "target", handle_pragma_target);
1361 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1362 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1363 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1364 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1366 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1367 handle_pragma_float_const_decimal64);
1369 c_register_pragma_with_expansion (0, "redefine_extname",
1370 handle_pragma_redefine_extname);
1372 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1374 #ifdef REGISTER_TARGET_PRAGMAS
1375 REGISTER_TARGET_PRAGMAS ();
1376 #endif
1378 /* Allow plugins to register their own pragmas. */
1379 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1382 #include "gt-c-family-c-pragma.h"