gcc-defs.exp (dg-additional-files-options): Extend regsub for dg-additional-files...
[official-gcc.git] / gcc / c-family / c-pragma.c
blobc75b0872b446c935e76457ab8fab05a62334a5e2
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->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)
371 DECL_EXTERNAL (decl) = 0;
372 if (TREE_CODE (decl) == VAR_DECL)
373 TREE_STATIC (decl) = 1;
374 assemble_alias (decl, value);
377 else
379 pending_weak pe = {name, value};
380 vec_safe_push (pending_weaks, pe);
384 /* GCC supports two #pragma directives for renaming the external
385 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
386 compatibility with the Solaris and VMS system headers. GCC also
387 has its own notation for this, __asm__("name") annotations.
389 Corner cases of these features and their interaction:
391 1) Both pragmas silently apply only to declarations with external
392 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
393 do not have this restriction.
395 2) In C++, both #pragmas silently apply only to extern "C" declarations.
396 Asm labels do not have this restriction.
398 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
399 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
400 new name is different, a warning issues and the name does not change.
402 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
403 *not* the DECL_ASSEMBLER_NAME.
405 5) If #pragma extern_prefix is in effect and a declaration occurs
406 with an __asm__ name, the #pragma extern_prefix is silently
407 ignored for that declaration.
409 6) If #pragma extern_prefix and #pragma redefine_extname apply to
410 the same declaration, whichever triggered first wins, and a warning
411 is issued. (We would like to have #pragma redefine_extname always
412 win, but it can appear either before or after the declaration, and
413 if it appears afterward, we have no way of knowing whether a modified
414 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
416 typedef struct GTY(()) pending_redefinition_d {
417 tree oldname;
418 tree newname;
419 } pending_redefinition;
422 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
424 static void handle_pragma_redefine_extname (cpp_reader *);
426 /* #pragma redefine_extname oldname newname */
427 static void
428 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
430 tree oldname, newname, decls, x;
431 enum cpp_ttype t;
432 bool found;
434 if (pragma_lex (&oldname) != CPP_NAME)
435 GCC_BAD ("malformed #pragma redefine_extname, ignored");
436 if (pragma_lex (&newname) != CPP_NAME)
437 GCC_BAD ("malformed #pragma redefine_extname, ignored");
438 t = pragma_lex (&x);
439 if (t != CPP_EOF)
440 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
442 found = false;
443 for (decls = c_linkage_bindings (oldname);
444 decls; )
446 tree decl;
447 if (TREE_CODE (decls) == TREE_LIST)
449 decl = TREE_VALUE (decls);
450 decls = TREE_CHAIN (decls);
452 else
454 decl = decls;
455 decls = NULL_TREE;
458 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
459 && (TREE_CODE (decl) == FUNCTION_DECL
460 || TREE_CODE (decl) == VAR_DECL))
462 found = true;
463 if (DECL_ASSEMBLER_NAME_SET_P (decl))
465 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
466 name = targetm.strip_name_encoding (name);
468 if (strcmp (name, IDENTIFIER_POINTER (newname)))
469 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
470 "conflict with previous rename");
472 else
473 change_decl_assembler_name (decl, newname);
477 if (!found)
478 /* We have to add this to the rename list even if there's already
479 a global value that doesn't meet the above criteria, because in
480 C++ "struct foo {...};" puts "foo" in the current namespace but
481 does *not* conflict with a subsequent declaration of a function
482 or variable foo. See g++.dg/other/pragma-re-2.C. */
483 add_to_renaming_pragma_list (oldname, newname);
486 /* This is called from here and from ia64-c.c. */
487 void
488 add_to_renaming_pragma_list (tree oldname, tree newname)
490 unsigned ix;
491 pending_redefinition *p;
493 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
494 if (oldname == p->oldname)
496 if (p->newname != newname)
497 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
498 "conflict with previous #pragma redefine_extname");
499 return;
502 pending_redefinition e = {oldname, newname};
503 vec_safe_push (pending_redefine_extname, e);
506 /* The current prefix set by #pragma extern_prefix. */
507 GTY(()) tree pragma_extern_prefix;
509 /* Hook from the front ends to apply the results of one of the preceding
510 pragmas that rename variables. */
512 tree
513 maybe_apply_renaming_pragma (tree decl, tree asmname)
515 unsigned ix;
516 pending_redefinition *p;
518 /* The renaming pragmas are only applied to declarations with
519 external linkage. */
520 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
521 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
522 || !has_c_linkage (decl))
523 return asmname;
525 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
526 but we may warn about a rename that conflicts. */
527 if (DECL_ASSEMBLER_NAME_SET_P (decl))
529 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
530 oldname = targetm.strip_name_encoding (oldname);
532 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
533 warning (OPT_Wpragmas, "asm declaration ignored due to "
534 "conflict with previous rename");
536 /* Take any pending redefine_extname off the list. */
537 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
538 if (DECL_NAME (decl) == p->oldname)
540 /* Only warn if there is a conflict. */
541 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
542 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
543 "conflict with previous rename");
545 pending_redefine_extname->unordered_remove (ix);
546 break;
548 return 0;
551 /* Find out if we have a pending #pragma redefine_extname. */
552 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
553 if (DECL_NAME (decl) == p->oldname)
555 tree newname = p->newname;
556 pending_redefine_extname->unordered_remove (ix);
558 /* If we already have an asmname, #pragma redefine_extname is
559 ignored (with a warning if it conflicts). */
560 if (asmname)
562 if (strcmp (TREE_STRING_POINTER (asmname),
563 IDENTIFIER_POINTER (newname)) != 0)
564 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
565 "conflict with __asm__ declaration");
566 return asmname;
569 /* Otherwise we use what we've got; #pragma extern_prefix is
570 silently ignored. */
571 return build_string (IDENTIFIER_LENGTH (newname),
572 IDENTIFIER_POINTER (newname));
575 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
576 if (asmname)
577 return asmname;
579 /* If #pragma extern_prefix is in effect, apply it. */
580 if (pragma_extern_prefix)
582 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
583 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
585 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
586 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
588 char *newname = (char *) alloca (plen + ilen + 1);
590 memcpy (newname, prefix, plen);
591 memcpy (newname + plen, id, ilen + 1);
593 return build_string (plen + ilen, newname);
596 /* Nada. */
597 return 0;
601 static void handle_pragma_visibility (cpp_reader *);
603 static vec<int> visstack;
605 /* Push the visibility indicated by STR onto the top of the #pragma
606 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
607 C++ namespace with visibility attribute and 2 for C++ builtin
608 ABI namespace. push_visibility/pop_visibility calls must have
609 matching KIND, it is not allowed to push visibility using one
610 KIND and pop using a different one. */
612 void
613 push_visibility (const char *str, int kind)
615 visstack.safe_push (((int) default_visibility) | (kind << 8));
616 if (!strcmp (str, "default"))
617 default_visibility = VISIBILITY_DEFAULT;
618 else if (!strcmp (str, "internal"))
619 default_visibility = VISIBILITY_INTERNAL;
620 else if (!strcmp (str, "hidden"))
621 default_visibility = VISIBILITY_HIDDEN;
622 else if (!strcmp (str, "protected"))
623 default_visibility = VISIBILITY_PROTECTED;
624 else
625 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
626 visibility_options.inpragma = 1;
629 /* Pop a level of the #pragma visibility stack. Return true if
630 successful. */
632 bool
633 pop_visibility (int kind)
635 if (!visstack.length ())
636 return false;
637 if ((visstack.last () >> 8) != kind)
638 return false;
639 default_visibility
640 = (enum symbol_visibility) (visstack.pop () & 0xff);
641 visibility_options.inpragma
642 = visstack.length () != 0;
643 return true;
646 /* Sets the default visibility for symbols to something other than that
647 specified on the command line. */
649 static void
650 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
652 /* Form is #pragma GCC visibility push(hidden)|pop */
653 tree x;
654 enum cpp_ttype token;
655 enum { bad, push, pop } action = bad;
657 token = pragma_lex (&x);
658 if (token == CPP_NAME)
660 const char *op = IDENTIFIER_POINTER (x);
661 if (!strcmp (op, "push"))
662 action = push;
663 else if (!strcmp (op, "pop"))
664 action = pop;
666 if (bad == action)
667 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
668 else
670 if (pop == action)
672 if (! pop_visibility (0))
673 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
675 else
677 if (pragma_lex (&x) != CPP_OPEN_PAREN)
678 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
679 token = pragma_lex (&x);
680 if (token != CPP_NAME)
681 GCC_BAD ("malformed #pragma GCC visibility push");
682 else
683 push_visibility (IDENTIFIER_POINTER (x), 0);
684 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
685 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
688 if (pragma_lex (&x) != CPP_EOF)
689 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
692 static void
693 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
695 const char *kind_string, *option_string;
696 unsigned int option_index;
697 enum cpp_ttype token;
698 diagnostic_t kind;
699 tree x;
700 struct cl_option_handlers handlers;
702 token = pragma_lex (&x);
703 if (token != CPP_NAME)
704 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
705 kind_string = IDENTIFIER_POINTER (x);
706 if (strcmp (kind_string, "error") == 0)
707 kind = DK_ERROR;
708 else if (strcmp (kind_string, "warning") == 0)
709 kind = DK_WARNING;
710 else if (strcmp (kind_string, "ignored") == 0)
711 kind = DK_IGNORED;
712 else if (strcmp (kind_string, "push") == 0)
714 diagnostic_push_diagnostics (global_dc, input_location);
715 return;
717 else if (strcmp (kind_string, "pop") == 0)
719 diagnostic_pop_diagnostics (global_dc, input_location);
720 return;
722 else
723 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
725 token = pragma_lex (&x);
726 if (token != CPP_STRING)
727 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
728 option_string = TREE_STRING_POINTER (x);
729 set_default_handlers (&handlers);
730 for (option_index = 0; option_index < cl_options_count; option_index++)
731 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
733 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
734 input_location, c_family_lang_mask, &handlers,
735 &global_options, &global_options_set,
736 global_dc);
737 return;
739 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
742 /* Parse #pragma GCC target (xxx) to set target specific options. */
743 static void
744 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
746 enum cpp_ttype token;
747 tree x;
748 bool close_paren_needed_p = false;
750 if (cfun)
752 error ("#pragma GCC option is not allowed inside functions");
753 return;
756 token = pragma_lex (&x);
757 if (token == CPP_OPEN_PAREN)
759 close_paren_needed_p = true;
760 token = pragma_lex (&x);
763 if (token != CPP_STRING)
765 GCC_BAD ("%<#pragma GCC option%> is not a string");
766 return;
769 /* Strings are user options. */
770 else
772 tree args = NULL_TREE;
776 /* Build up the strings now as a tree linked list. Skip empty
777 strings. */
778 if (TREE_STRING_LENGTH (x) > 0)
779 args = tree_cons (NULL_TREE, x, args);
781 token = pragma_lex (&x);
782 while (token == CPP_COMMA)
783 token = pragma_lex (&x);
785 while (token == CPP_STRING);
787 if (close_paren_needed_p)
789 if (token == CPP_CLOSE_PAREN)
790 token = pragma_lex (&x);
791 else
792 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
793 "not have a final %<)%>");
796 if (token != CPP_EOF)
798 error ("#pragma GCC target string... is badly formed");
799 return;
802 /* put arguments in the order the user typed them. */
803 args = nreverse (args);
805 if (targetm.target_option.pragma_parse (args, NULL_TREE))
806 current_target_pragma = args;
810 /* Handle #pragma GCC optimize to set optimization options. */
811 static void
812 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
814 enum cpp_ttype token;
815 tree x;
816 bool close_paren_needed_p = false;
817 tree optimization_previous_node = optimization_current_node;
819 if (cfun)
821 error ("#pragma GCC optimize is not allowed inside functions");
822 return;
825 token = pragma_lex (&x);
826 if (token == CPP_OPEN_PAREN)
828 close_paren_needed_p = true;
829 token = pragma_lex (&x);
832 if (token != CPP_STRING && token != CPP_NUMBER)
834 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
835 return;
838 /* Strings/numbers are user options. */
839 else
841 tree args = NULL_TREE;
845 /* Build up the numbers/strings now as a list. */
846 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
847 args = tree_cons (NULL_TREE, x, args);
849 token = pragma_lex (&x);
850 while (token == CPP_COMMA)
851 token = pragma_lex (&x);
853 while (token == CPP_STRING || token == CPP_NUMBER);
855 if (close_paren_needed_p)
857 if (token == CPP_CLOSE_PAREN)
858 token = pragma_lex (&x);
859 else
860 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
861 "not have a final %<)%>");
864 if (token != CPP_EOF)
866 error ("#pragma GCC optimize string... is badly formed");
867 return;
870 /* put arguments in the order the user typed them. */
871 args = nreverse (args);
873 parse_optimize_options (args, false);
874 current_optimize_pragma = chainon (current_optimize_pragma, args);
875 optimization_current_node = build_optimization_node (&global_options);
876 c_cpp_builtins_optimize_pragma (parse_in,
877 optimization_previous_node,
878 optimization_current_node);
882 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
883 both the binary representation of the options and the TREE_LIST of
884 strings that will be added to the function's attribute list. */
885 typedef struct GTY(()) opt_stack {
886 struct opt_stack *prev;
887 tree target_binary;
888 tree target_strings;
889 tree optimize_binary;
890 tree optimize_strings;
891 } opt_stack;
893 static GTY(()) struct opt_stack * options_stack;
895 /* Handle #pragma GCC push_options to save the current target and optimization
896 options. */
898 static void
899 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
901 enum cpp_ttype token;
902 tree x = 0;
903 opt_stack *p;
905 token = pragma_lex (&x);
906 if (token != CPP_EOF)
908 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
909 return;
912 p = ggc_alloc_opt_stack ();
913 p->prev = options_stack;
914 options_stack = p;
916 /* Save optimization and target flags in binary format. */
917 p->optimize_binary = build_optimization_node (&global_options);
918 p->target_binary = build_target_option_node (&global_options);
920 /* Save optimization and target flags in string list format. */
921 p->optimize_strings = copy_list (current_optimize_pragma);
922 p->target_strings = copy_list (current_target_pragma);
925 /* Handle #pragma GCC pop_options to restore the current target and
926 optimization options from a previous push_options. */
928 static void
929 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
931 enum cpp_ttype token;
932 tree x = 0;
933 opt_stack *p;
935 token = pragma_lex (&x);
936 if (token != CPP_EOF)
938 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
939 return;
942 if (! options_stack)
944 warning (OPT_Wpragmas,
945 "%<#pragma GCC pop_options%> without a corresponding "
946 "%<#pragma GCC push_options%>");
947 return;
950 p = options_stack;
951 options_stack = p->prev;
953 if (p->target_binary != target_option_current_node)
955 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
956 target_option_current_node = p->target_binary;
959 if (p->optimize_binary != optimization_current_node)
961 tree old_optimize = optimization_current_node;
962 cl_optimization_restore (&global_options,
963 TREE_OPTIMIZATION (p->optimize_binary));
964 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
965 p->optimize_binary);
966 optimization_current_node = p->optimize_binary;
969 current_target_pragma = p->target_strings;
970 current_optimize_pragma = p->optimize_strings;
973 /* Handle #pragma GCC reset_options to restore the current target and
974 optimization options to the original options used on the command line. */
976 static void
977 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
979 enum cpp_ttype token;
980 tree x = 0;
981 tree new_optimize = optimization_default_node;
982 tree new_target = target_option_default_node;
984 token = pragma_lex (&x);
985 if (token != CPP_EOF)
987 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
988 return;
991 if (new_target != target_option_current_node)
993 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
994 target_option_current_node = new_target;
997 if (new_optimize != optimization_current_node)
999 tree old_optimize = optimization_current_node;
1000 cl_optimization_restore (&global_options,
1001 TREE_OPTIMIZATION (new_optimize));
1002 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1003 optimization_current_node = new_optimize;
1006 current_target_pragma = NULL_TREE;
1007 current_optimize_pragma = NULL_TREE;
1010 /* Print a plain user-specified message. */
1012 static void
1013 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1015 enum cpp_ttype token;
1016 tree x, message = 0;
1018 token = pragma_lex (&x);
1019 if (token == CPP_OPEN_PAREN)
1021 token = pragma_lex (&x);
1022 if (token == CPP_STRING)
1023 message = x;
1024 else
1025 GCC_BAD ("expected a string after %<#pragma message%>");
1026 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1027 GCC_BAD ("malformed %<#pragma message%>, ignored");
1029 else if (token == CPP_STRING)
1030 message = x;
1031 else
1032 GCC_BAD ("expected a string after %<#pragma message%>");
1034 gcc_assert (message);
1036 if (pragma_lex (&x) != CPP_EOF)
1037 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1039 if (TREE_STRING_LENGTH (message) > 1)
1040 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1043 /* Mark whether the current location is valid for a STDC pragma. */
1045 static bool valid_location_for_stdc_pragma;
1047 void
1048 mark_valid_location_for_stdc_pragma (bool flag)
1050 valid_location_for_stdc_pragma = flag;
1053 /* Return true if the current location is valid for a STDC pragma. */
1055 bool
1056 valid_location_for_stdc_pragma_p (void)
1058 return valid_location_for_stdc_pragma;
1061 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1063 /* A STDC pragma must appear outside of external declarations or
1064 preceding all explicit declarations and statements inside a compound
1065 statement; its behavior is undefined if used in any other context.
1066 It takes a switch of ON, OFF, or DEFAULT. */
1068 static enum pragma_switch_t
1069 handle_stdc_pragma (const char *pname)
1071 const char *arg;
1072 tree t;
1073 enum pragma_switch_t ret;
1075 if (!valid_location_for_stdc_pragma_p ())
1077 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1078 pname);
1079 return PRAGMA_BAD;
1082 if (pragma_lex (&t) != CPP_NAME)
1084 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1085 return PRAGMA_BAD;
1088 arg = IDENTIFIER_POINTER (t);
1090 if (!strcmp (arg, "ON"))
1091 ret = PRAGMA_ON;
1092 else if (!strcmp (arg, "OFF"))
1093 ret = PRAGMA_OFF;
1094 else if (!strcmp (arg, "DEFAULT"))
1095 ret = PRAGMA_DEFAULT;
1096 else
1098 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1099 return PRAGMA_BAD;
1102 if (pragma_lex (&t) != CPP_EOF)
1104 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1105 return PRAGMA_BAD;
1108 return ret;
1111 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1112 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1113 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1115 static void
1116 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1118 if (c_dialect_cxx ())
1120 if (warn_unknown_pragmas > in_system_header)
1121 warning (OPT_Wunknown_pragmas,
1122 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1123 " for C++");
1124 return;
1127 if (!targetm.decimal_float_supported_p ())
1129 if (warn_unknown_pragmas > in_system_header)
1130 warning (OPT_Wunknown_pragmas,
1131 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1132 " on this target");
1133 return;
1136 pedwarn (input_location, OPT_Wpedantic,
1137 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1139 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1141 case PRAGMA_ON:
1142 set_float_const_decimal64 ();
1143 break;
1144 case PRAGMA_OFF:
1145 case PRAGMA_DEFAULT:
1146 clear_float_const_decimal64 ();
1147 break;
1148 case PRAGMA_BAD:
1149 break;
1153 /* A vector of registered pragma callbacks, which is never freed. */
1155 static vec<internal_pragma_handler> registered_pragmas;
1157 typedef struct
1159 const char *space;
1160 const char *name;
1161 } pragma_ns_name;
1164 static vec<pragma_ns_name> registered_pp_pragmas;
1166 struct omp_pragma_def { const char *name; unsigned int id; };
1167 static const struct omp_pragma_def omp_pragmas[] = {
1168 { "atomic", PRAGMA_OMP_ATOMIC },
1169 { "barrier", PRAGMA_OMP_BARRIER },
1170 { "cancel", PRAGMA_OMP_CANCEL },
1171 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1172 { "critical", PRAGMA_OMP_CRITICAL },
1173 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1174 { "flush", PRAGMA_OMP_FLUSH },
1175 { "master", PRAGMA_OMP_MASTER },
1176 { "ordered", PRAGMA_OMP_ORDERED },
1177 { "section", PRAGMA_OMP_SECTION },
1178 { "sections", PRAGMA_OMP_SECTIONS },
1179 { "single", PRAGMA_OMP_SINGLE },
1180 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1181 { "taskwait", PRAGMA_OMP_TASKWAIT },
1182 { "taskyield", PRAGMA_OMP_TASKYIELD },
1183 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1185 static const struct omp_pragma_def omp_pragmas_simd[] = {
1186 { "declare", PRAGMA_OMP_DECLARE_REDUCTION },
1187 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1188 { "for", PRAGMA_OMP_FOR },
1189 { "parallel", PRAGMA_OMP_PARALLEL },
1190 { "simd", PRAGMA_OMP_SIMD },
1191 { "target", PRAGMA_OMP_TARGET },
1192 { "task", PRAGMA_OMP_TASK },
1193 { "teams", PRAGMA_OMP_TEAMS },
1196 void
1197 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1199 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1200 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1201 / sizeof (*omp_pragmas);
1202 int i;
1204 for (i = 0; i < n_omp_pragmas; ++i)
1205 if (omp_pragmas[i].id == id)
1207 *space = "omp";
1208 *name = omp_pragmas[i].name;
1209 return;
1212 for (i = 0; i < n_omp_pragmas_simd; ++i)
1213 if (omp_pragmas_simd[i].id == id)
1215 *space = "omp";
1216 *name = omp_pragmas_simd[i].name;
1217 return;
1220 if (id >= PRAGMA_FIRST_EXTERNAL
1221 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1223 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1224 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1225 return;
1228 gcc_unreachable ();
1231 /* Front-end wrappers for pragma registration to avoid dragging
1232 cpplib.h in almost everywhere. */
1234 static void
1235 c_register_pragma_1 (const char *space, const char *name,
1236 internal_pragma_handler ihandler, bool allow_expansion)
1238 unsigned id;
1240 if (flag_preprocess_only)
1242 pragma_ns_name ns_name;
1244 if (!allow_expansion)
1245 return;
1247 ns_name.space = space;
1248 ns_name.name = name;
1249 registered_pp_pragmas.safe_push (ns_name);
1250 id = registered_pp_pragmas.length ();
1251 id += PRAGMA_FIRST_EXTERNAL - 1;
1253 else
1255 registered_pragmas.safe_push (ihandler);
1256 id = registered_pragmas.length ();
1257 id += PRAGMA_FIRST_EXTERNAL - 1;
1259 /* The C++ front end allocates 6 bits in cp_token; the C front end
1260 allocates 7 bits in c_token. At present this is sufficient. */
1261 gcc_assert (id < 64);
1264 cpp_register_deferred_pragma (parse_in, space, name, id,
1265 allow_expansion, false);
1268 /* Register a C pragma handler, using a space and a name. It disallows pragma
1269 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1270 void
1271 c_register_pragma (const char *space, const char *name,
1272 pragma_handler_1arg handler)
1274 internal_pragma_handler ihandler;
1276 ihandler.handler.handler_1arg = handler;
1277 ihandler.extra_data = false;
1278 ihandler.data = NULL;
1279 c_register_pragma_1 (space, name, ihandler, false);
1282 /* Register a C pragma handler, using a space and a name, it also carries an
1283 extra data field which can be used by the handler. It disallows pragma
1284 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1285 instead). */
1286 void
1287 c_register_pragma_with_data (const char *space, const char *name,
1288 pragma_handler_2arg handler, void * data)
1290 internal_pragma_handler ihandler;
1292 ihandler.handler.handler_2arg = handler;
1293 ihandler.extra_data = true;
1294 ihandler.data = data;
1295 c_register_pragma_1 (space, name, ihandler, false);
1298 /* Register a C pragma handler, using a space and a name. It allows pragma
1299 expansion as in the following example:
1301 #define NUMBER 10
1302 #pragma count (NUMBER)
1304 Name expansion is still disallowed. */
1305 void
1306 c_register_pragma_with_expansion (const char *space, const char *name,
1307 pragma_handler_1arg handler)
1309 internal_pragma_handler ihandler;
1311 ihandler.handler.handler_1arg = handler;
1312 ihandler.extra_data = false;
1313 ihandler.data = NULL;
1314 c_register_pragma_1 (space, name, ihandler, true);
1317 /* Register a C pragma handler, using a space and a name, it also carries an
1318 extra data field which can be used by the handler. It allows pragma
1319 expansion as in the following example:
1321 #define NUMBER 10
1322 #pragma count (NUMBER)
1324 Name expansion is still disallowed. */
1325 void
1326 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1327 pragma_handler_2arg handler,
1328 void *data)
1330 internal_pragma_handler ihandler;
1332 ihandler.handler.handler_2arg = handler;
1333 ihandler.extra_data = true;
1334 ihandler.data = data;
1335 c_register_pragma_1 (space, name, ihandler, true);
1338 void
1339 c_invoke_pragma_handler (unsigned int id)
1341 internal_pragma_handler *ihandler;
1342 pragma_handler_1arg handler_1arg;
1343 pragma_handler_2arg handler_2arg;
1345 id -= PRAGMA_FIRST_EXTERNAL;
1346 ihandler = &registered_pragmas[id];
1347 if (ihandler->extra_data)
1349 handler_2arg = ihandler->handler.handler_2arg;
1350 handler_2arg (parse_in, ihandler->data);
1352 else
1354 handler_1arg = ihandler->handler.handler_1arg;
1355 handler_1arg (parse_in);
1359 /* Set up front-end pragmas. */
1360 void
1361 init_pragma (void)
1363 if (flag_openmp)
1365 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1366 int i;
1368 for (i = 0; i < n_omp_pragmas; ++i)
1369 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1370 omp_pragmas[i].id, true, true);
1372 if (flag_openmp || flag_openmp_simd)
1374 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1375 / sizeof (*omp_pragmas);
1376 int i;
1378 for (i = 0; i < n_omp_pragmas_simd; ++i)
1379 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1380 omp_pragmas_simd[i].id, true, true);
1383 if (!flag_preprocess_only)
1384 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1385 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1387 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1388 false);
1389 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1390 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1391 #else
1392 c_register_pragma (0, "pack", handle_pragma_pack);
1393 #endif
1394 c_register_pragma (0, "weak", handle_pragma_weak);
1395 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1397 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1398 c_register_pragma ("GCC", "target", handle_pragma_target);
1399 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1400 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1401 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1402 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1404 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1405 handle_pragma_float_const_decimal64);
1407 c_register_pragma_with_expansion (0, "redefine_extname",
1408 handle_pragma_redefine_extname);
1410 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1412 #ifdef REGISTER_TARGET_PRAGMAS
1413 REGISTER_TARGET_PRAGMAS ();
1414 #endif
1416 /* Allow plugins to register their own pragmas. */
1417 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1420 #include "gt-c-family-c-pragma.h"