2014-06-18 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / c-pragma.c
blob5e57de39b137e0d9e599d31a6a976f9a7cb8f99a
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "stringpool.h"
26 #include "attribs.h"
27 #include "varasm.h"
28 #include "gcc-symtab.h"
29 #include "function.h" /* For cfun. FIXME: Does the parser know
30 when it is inside a function, so that
31 we don't have to look at cfun? */
32 #include "cpplib.h"
33 #include "c-pragma.h"
34 #include "flags.h"
35 #include "c-common.h"
36 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
37 this not a target hook?). */
38 #include "vec.h"
39 #include "target.h"
40 #include "diagnostic.h"
41 #include "opts.h"
42 #include "plugin.h"
43 #include "cgraph.h"
45 #define GCC_BAD(gmsgid) \
46 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
47 #define GCC_BAD2(gmsgid, arg) \
48 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
50 typedef struct GTY(()) align_stack {
51 int alignment;
52 tree id;
53 struct align_stack * prev;
54 } align_stack;
56 static GTY(()) struct align_stack * alignment_stack;
58 static void handle_pragma_pack (cpp_reader *);
60 /* If we have a "global" #pragma pack(<n>) in effect when the first
61 #pragma pack(push,<n>) is encountered, this stores the value of
62 maximum_field_alignment in effect. When the final pop_alignment()
63 happens, we restore the value to this, not to a value of 0 for
64 maximum_field_alignment. Value is in bits. */
65 static int default_alignment;
66 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
67 ? &default_alignment \
68 : &alignment_stack->alignment) = (ALIGN))
70 static void push_alignment (int, tree);
71 static void pop_alignment (tree);
73 /* Push an alignment value onto the stack. */
74 static void
75 push_alignment (int alignment, tree id)
77 align_stack * entry = ggc_alloc<align_stack> ();
79 entry->alignment = alignment;
80 entry->id = id;
81 entry->prev = alignment_stack;
83 /* The current value of maximum_field_alignment is not necessarily
84 0 since there may be a #pragma pack(<n>) in effect; remember it
85 so that we can restore it after the final #pragma pop(). */
86 if (alignment_stack == NULL)
87 default_alignment = maximum_field_alignment;
89 alignment_stack = entry;
91 maximum_field_alignment = alignment;
94 /* Undo a push of an alignment onto the stack. */
95 static void
96 pop_alignment (tree id)
98 align_stack * entry;
100 if (alignment_stack == NULL)
101 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
103 /* If we got an identifier, strip away everything above the target
104 entry so that the next step will restore the state just below it. */
105 if (id)
107 for (entry = alignment_stack; entry; entry = entry->prev)
108 if (entry->id == id)
110 alignment_stack = entry;
111 break;
113 if (entry == NULL)
114 warning (OPT_Wpragmas, "\
115 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
116 , id, id);
119 entry = alignment_stack->prev;
121 maximum_field_alignment = entry ? entry->alignment : default_alignment;
123 alignment_stack = entry;
126 /* #pragma pack ()
127 #pragma pack (N)
129 #pragma pack (push)
130 #pragma pack (push, N)
131 #pragma pack (push, ID)
132 #pragma pack (push, ID, N)
133 #pragma pack (pop)
134 #pragma pack (pop, ID) */
135 static void
136 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
138 tree x, id = 0;
139 int align = -1;
140 enum cpp_ttype token;
141 enum { set, push, pop } action;
143 if (pragma_lex (&x) != CPP_OPEN_PAREN)
144 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
146 token = pragma_lex (&x);
147 if (token == CPP_CLOSE_PAREN)
149 action = set;
150 align = initial_max_fld_align;
152 else if (token == CPP_NUMBER)
154 if (TREE_CODE (x) != INTEGER_CST)
155 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
156 align = TREE_INT_CST_LOW (x);
157 action = set;
158 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
159 GCC_BAD ("malformed %<#pragma pack%> - ignored");
161 else if (token == CPP_NAME)
163 #define GCC_BAD_ACTION do { if (action != pop) \
164 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
165 else \
166 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
167 } while (0)
169 const char *op = IDENTIFIER_POINTER (x);
170 if (!strcmp (op, "push"))
171 action = push;
172 else if (!strcmp (op, "pop"))
173 action = pop;
174 else
175 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
177 while ((token = pragma_lex (&x)) == CPP_COMMA)
179 token = pragma_lex (&x);
180 if (token == CPP_NAME && id == 0)
182 id = x;
184 else if (token == CPP_NUMBER && action == push && align == -1)
186 if (TREE_CODE (x) != INTEGER_CST)
187 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
188 align = TREE_INT_CST_LOW (x);
189 if (align == -1)
190 action = set;
192 else
193 GCC_BAD_ACTION;
196 if (token != CPP_CLOSE_PAREN)
197 GCC_BAD_ACTION;
198 #undef GCC_BAD_ACTION
200 else
201 GCC_BAD ("malformed %<#pragma pack%> - ignored");
203 if (pragma_lex (&x) != CPP_EOF)
204 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
206 if (flag_pack_struct)
207 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
209 if (action != pop)
210 switch (align)
212 case 0:
213 case 1:
214 case 2:
215 case 4:
216 case 8:
217 case 16:
218 align *= BITS_PER_UNIT;
219 break;
220 case -1:
221 if (action == push)
223 align = maximum_field_alignment;
224 break;
226 default:
227 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
230 switch (action)
232 case set: SET_GLOBAL_ALIGNMENT (align); break;
233 case push: push_alignment (align, id); break;
234 case pop: pop_alignment (id); break;
238 typedef struct GTY(()) pending_weak_d
240 tree name;
241 tree value;
242 } pending_weak;
245 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
247 static void apply_pragma_weak (tree, tree);
248 static void handle_pragma_weak (cpp_reader *);
250 static void
251 apply_pragma_weak (tree decl, tree value)
253 if (value)
255 value = build_string (IDENTIFIER_LENGTH (value),
256 IDENTIFIER_POINTER (value));
257 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
258 build_tree_list (NULL, value)),
262 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
263 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
264 && DECL_ASSEMBLER_NAME_SET_P (decl)
265 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
266 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
267 "results in unspecified behavior", decl);
269 declare_weak (decl);
272 void
273 maybe_apply_pragma_weak (tree decl)
275 tree id;
276 int i;
277 pending_weak *pe;
279 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
281 /* No weak symbols pending, take the short-cut. */
282 if (vec_safe_is_empty (pending_weaks))
283 return;
284 /* If it's not visible outside this file, it doesn't matter whether
285 it's weak. */
286 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
287 return;
288 /* If it's not a function or a variable, it can't be weak.
289 FIXME: what kinds of things are visible outside this file but
290 aren't functions or variables? Should this be an assert instead? */
291 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
292 return;
294 if (DECL_ASSEMBLER_NAME_SET_P (decl))
295 id = DECL_ASSEMBLER_NAME (decl);
296 else
298 id = DECL_ASSEMBLER_NAME (decl);
299 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
302 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
303 if (id == pe->name)
305 apply_pragma_weak (decl, pe->value);
306 pending_weaks->unordered_remove (i);
307 break;
311 /* Process all "#pragma weak A = B" directives where we have not seen
312 a decl for A. */
313 void
314 maybe_apply_pending_pragma_weaks (void)
316 tree alias_id, id, decl;
317 int i;
318 pending_weak *pe;
319 symtab_node *target;
321 if (vec_safe_is_empty (pending_weaks))
322 return;
324 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
326 alias_id = pe->name;
327 id = pe->value;
329 if (id == NULL)
330 continue;
332 target = symtab_node_for_asm (id);
333 decl = build_decl (UNKNOWN_LOCATION,
334 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
335 alias_id, default_function_type);
337 DECL_ARTIFICIAL (decl) = 1;
338 TREE_PUBLIC (decl) = 1;
339 DECL_WEAK (decl) = 1;
340 if (TREE_CODE (decl) == VAR_DECL)
341 TREE_STATIC (decl) = 1;
342 if (!target)
344 error ("%q+D aliased to undefined symbol %qE",
345 decl, id);
346 continue;
349 assemble_alias (decl, id);
353 /* #pragma weak name [= value] */
354 static void
355 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
357 tree name, value, x, decl;
358 enum cpp_ttype t;
360 value = 0;
362 if (pragma_lex (&name) != CPP_NAME)
363 GCC_BAD ("malformed #pragma weak, ignored");
364 t = pragma_lex (&x);
365 if (t == CPP_EQ)
367 if (pragma_lex (&value) != CPP_NAME)
368 GCC_BAD ("malformed #pragma weak, ignored");
369 t = pragma_lex (&x);
371 if (t != CPP_EOF)
372 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
374 decl = identifier_global_value (name);
375 if (decl && DECL_P (decl))
377 apply_pragma_weak (decl, value);
378 if (value)
380 DECL_EXTERNAL (decl) = 0;
381 if (TREE_CODE (decl) == VAR_DECL)
382 TREE_STATIC (decl) = 1;
383 assemble_alias (decl, value);
386 else
388 pending_weak pe = {name, value};
389 vec_safe_push (pending_weaks, pe);
393 /* GCC supports two #pragma directives for renaming the external
394 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
395 compatibility with the Solaris and VMS system headers. GCC also
396 has its own notation for this, __asm__("name") annotations.
398 Corner cases of these features and their interaction:
400 1) Both pragmas silently apply only to declarations with external
401 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
402 do not have this restriction.
404 2) In C++, both #pragmas silently apply only to extern "C" declarations.
405 Asm labels do not have this restriction.
407 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
408 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
409 new name is different, a warning issues and the name does not change.
411 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
412 *not* the DECL_ASSEMBLER_NAME.
414 5) If #pragma extern_prefix is in effect and a declaration occurs
415 with an __asm__ name, the #pragma extern_prefix is silently
416 ignored for that declaration.
418 6) If #pragma extern_prefix and #pragma redefine_extname apply to
419 the same declaration, whichever triggered first wins, and a warning
420 is issued. (We would like to have #pragma redefine_extname always
421 win, but it can appear either before or after the declaration, and
422 if it appears afterward, we have no way of knowing whether a modified
423 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
425 typedef struct GTY(()) pending_redefinition_d {
426 tree oldname;
427 tree newname;
428 } pending_redefinition;
431 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
433 static void handle_pragma_redefine_extname (cpp_reader *);
435 /* #pragma redefine_extname oldname newname */
436 static void
437 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
439 tree oldname, newname, decls, x;
440 enum cpp_ttype t;
441 bool found;
443 if (pragma_lex (&oldname) != CPP_NAME)
444 GCC_BAD ("malformed #pragma redefine_extname, ignored");
445 if (pragma_lex (&newname) != CPP_NAME)
446 GCC_BAD ("malformed #pragma redefine_extname, ignored");
447 t = pragma_lex (&x);
448 if (t != CPP_EOF)
449 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
451 found = false;
452 for (decls = c_linkage_bindings (oldname);
453 decls; )
455 tree decl;
456 if (TREE_CODE (decls) == TREE_LIST)
458 decl = TREE_VALUE (decls);
459 decls = TREE_CHAIN (decls);
461 else
463 decl = decls;
464 decls = NULL_TREE;
467 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
468 && (TREE_CODE (decl) == FUNCTION_DECL
469 || TREE_CODE (decl) == VAR_DECL))
471 found = true;
472 if (DECL_ASSEMBLER_NAME_SET_P (decl))
474 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
475 name = targetm.strip_name_encoding (name);
477 if (strcmp (name, IDENTIFIER_POINTER (newname)))
478 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
479 "conflict with previous rename");
481 else
482 change_decl_assembler_name (decl, newname);
486 if (!found)
487 /* We have to add this to the rename list even if there's already
488 a global value that doesn't meet the above criteria, because in
489 C++ "struct foo {...};" puts "foo" in the current namespace but
490 does *not* conflict with a subsequent declaration of a function
491 or variable foo. See g++.dg/other/pragma-re-2.C. */
492 add_to_renaming_pragma_list (oldname, newname);
495 /* This is called from here and from ia64-c.c. */
496 void
497 add_to_renaming_pragma_list (tree oldname, tree newname)
499 unsigned ix;
500 pending_redefinition *p;
502 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
503 if (oldname == p->oldname)
505 if (p->newname != newname)
506 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
507 "conflict with previous #pragma redefine_extname");
508 return;
511 pending_redefinition e = {oldname, newname};
512 vec_safe_push (pending_redefine_extname, e);
515 /* The current prefix set by #pragma extern_prefix. */
516 GTY(()) tree pragma_extern_prefix;
518 /* Hook from the front ends to apply the results of one of the preceding
519 pragmas that rename variables. */
521 tree
522 maybe_apply_renaming_pragma (tree decl, tree asmname)
524 unsigned ix;
525 pending_redefinition *p;
527 /* The renaming pragmas are only applied to declarations with
528 external linkage. */
529 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
530 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
531 || !has_c_linkage (decl))
532 return asmname;
534 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
535 but we may warn about a rename that conflicts. */
536 if (DECL_ASSEMBLER_NAME_SET_P (decl))
538 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
539 oldname = targetm.strip_name_encoding (oldname);
541 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
542 warning (OPT_Wpragmas, "asm declaration ignored due to "
543 "conflict with previous rename");
545 /* Take any pending redefine_extname off the list. */
546 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
547 if (DECL_NAME (decl) == p->oldname)
549 /* Only warn if there is a conflict. */
550 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
551 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
552 "conflict with previous rename");
554 pending_redefine_extname->unordered_remove (ix);
555 break;
557 return 0;
560 /* Find out if we have a pending #pragma redefine_extname. */
561 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
562 if (DECL_NAME (decl) == p->oldname)
564 tree newname = p->newname;
565 pending_redefine_extname->unordered_remove (ix);
567 /* If we already have an asmname, #pragma redefine_extname is
568 ignored (with a warning if it conflicts). */
569 if (asmname)
571 if (strcmp (TREE_STRING_POINTER (asmname),
572 IDENTIFIER_POINTER (newname)) != 0)
573 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
574 "conflict with __asm__ declaration");
575 return asmname;
578 /* Otherwise we use what we've got; #pragma extern_prefix is
579 silently ignored. */
580 return build_string (IDENTIFIER_LENGTH (newname),
581 IDENTIFIER_POINTER (newname));
584 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
585 if (asmname)
586 return asmname;
588 /* If #pragma extern_prefix is in effect, apply it. */
589 if (pragma_extern_prefix)
591 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
592 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
594 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
595 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
597 char *newname = (char *) alloca (plen + ilen + 1);
599 memcpy (newname, prefix, plen);
600 memcpy (newname + plen, id, ilen + 1);
602 return build_string (plen + ilen, newname);
605 /* Nada. */
606 return 0;
610 static void handle_pragma_visibility (cpp_reader *);
612 static vec<int> visstack;
614 /* Push the visibility indicated by STR onto the top of the #pragma
615 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
616 C++ namespace with visibility attribute and 2 for C++ builtin
617 ABI namespace. push_visibility/pop_visibility calls must have
618 matching KIND, it is not allowed to push visibility using one
619 KIND and pop using a different one. */
621 void
622 push_visibility (const char *str, int kind)
624 visstack.safe_push (((int) default_visibility) | (kind << 8));
625 if (!strcmp (str, "default"))
626 default_visibility = VISIBILITY_DEFAULT;
627 else if (!strcmp (str, "internal"))
628 default_visibility = VISIBILITY_INTERNAL;
629 else if (!strcmp (str, "hidden"))
630 default_visibility = VISIBILITY_HIDDEN;
631 else if (!strcmp (str, "protected"))
632 default_visibility = VISIBILITY_PROTECTED;
633 else
634 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
635 visibility_options.inpragma = 1;
638 /* Pop a level of the #pragma visibility stack. Return true if
639 successful. */
641 bool
642 pop_visibility (int kind)
644 if (!visstack.length ())
645 return false;
646 if ((visstack.last () >> 8) != kind)
647 return false;
648 default_visibility
649 = (enum symbol_visibility) (visstack.pop () & 0xff);
650 visibility_options.inpragma
651 = visstack.length () != 0;
652 return true;
655 /* Sets the default visibility for symbols to something other than that
656 specified on the command line. */
658 static void
659 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
661 /* Form is #pragma GCC visibility push(hidden)|pop */
662 tree x;
663 enum cpp_ttype token;
664 enum { bad, push, pop } action = bad;
666 token = pragma_lex (&x);
667 if (token == CPP_NAME)
669 const char *op = IDENTIFIER_POINTER (x);
670 if (!strcmp (op, "push"))
671 action = push;
672 else if (!strcmp (op, "pop"))
673 action = pop;
675 if (bad == action)
676 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
677 else
679 if (pop == action)
681 if (! pop_visibility (0))
682 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
684 else
686 if (pragma_lex (&x) != CPP_OPEN_PAREN)
687 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
688 token = pragma_lex (&x);
689 if (token != CPP_NAME)
690 GCC_BAD ("malformed #pragma GCC visibility push");
691 else
692 push_visibility (IDENTIFIER_POINTER (x), 0);
693 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
694 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
697 if (pragma_lex (&x) != CPP_EOF)
698 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
701 static void
702 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
704 const char *kind_string, *option_string;
705 unsigned int option_index;
706 enum cpp_ttype token;
707 diagnostic_t kind;
708 tree x;
709 struct cl_option_handlers handlers;
711 token = pragma_lex (&x);
712 if (token != CPP_NAME)
713 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
714 kind_string = IDENTIFIER_POINTER (x);
715 if (strcmp (kind_string, "error") == 0)
716 kind = DK_ERROR;
717 else if (strcmp (kind_string, "warning") == 0)
718 kind = DK_WARNING;
719 else if (strcmp (kind_string, "ignored") == 0)
720 kind = DK_IGNORED;
721 else if (strcmp (kind_string, "push") == 0)
723 diagnostic_push_diagnostics (global_dc, input_location);
724 return;
726 else if (strcmp (kind_string, "pop") == 0)
728 diagnostic_pop_diagnostics (global_dc, input_location);
729 return;
731 else
732 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
734 token = pragma_lex (&x);
735 if (token != CPP_STRING)
736 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
737 option_string = TREE_STRING_POINTER (x);
738 set_default_handlers (&handlers);
739 for (option_index = 0; option_index < cl_options_count; option_index++)
740 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
742 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
743 input_location, c_family_lang_mask, &handlers,
744 &global_options, &global_options_set,
745 global_dc);
746 return;
748 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
751 /* Parse #pragma GCC target (xxx) to set target specific options. */
752 static void
753 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
755 enum cpp_ttype token;
756 tree x;
757 bool close_paren_needed_p = false;
759 if (cfun)
761 error ("#pragma GCC option is not allowed inside functions");
762 return;
765 token = pragma_lex (&x);
766 if (token == CPP_OPEN_PAREN)
768 close_paren_needed_p = true;
769 token = pragma_lex (&x);
772 if (token != CPP_STRING)
774 GCC_BAD ("%<#pragma GCC option%> is not a string");
775 return;
778 /* Strings are user options. */
779 else
781 tree args = NULL_TREE;
785 /* Build up the strings now as a tree linked list. Skip empty
786 strings. */
787 if (TREE_STRING_LENGTH (x) > 0)
788 args = tree_cons (NULL_TREE, x, args);
790 token = pragma_lex (&x);
791 while (token == CPP_COMMA)
792 token = pragma_lex (&x);
794 while (token == CPP_STRING);
796 if (close_paren_needed_p)
798 if (token == CPP_CLOSE_PAREN)
799 token = pragma_lex (&x);
800 else
801 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
802 "not have a final %<)%>");
805 if (token != CPP_EOF)
807 error ("#pragma GCC target string... is badly formed");
808 return;
811 /* put arguments in the order the user typed them. */
812 args = nreverse (args);
814 if (targetm.target_option.pragma_parse (args, NULL_TREE))
815 current_target_pragma = args;
819 /* Handle #pragma GCC optimize to set optimization options. */
820 static void
821 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
823 enum cpp_ttype token;
824 tree x;
825 bool close_paren_needed_p = false;
826 tree optimization_previous_node = optimization_current_node;
828 if (cfun)
830 error ("#pragma GCC optimize is not allowed inside functions");
831 return;
834 token = pragma_lex (&x);
835 if (token == CPP_OPEN_PAREN)
837 close_paren_needed_p = true;
838 token = pragma_lex (&x);
841 if (token != CPP_STRING && token != CPP_NUMBER)
843 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
844 return;
847 /* Strings/numbers are user options. */
848 else
850 tree args = NULL_TREE;
854 /* Build up the numbers/strings now as a list. */
855 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
856 args = tree_cons (NULL_TREE, x, args);
858 token = pragma_lex (&x);
859 while (token == CPP_COMMA)
860 token = pragma_lex (&x);
862 while (token == CPP_STRING || token == CPP_NUMBER);
864 if (close_paren_needed_p)
866 if (token == CPP_CLOSE_PAREN)
867 token = pragma_lex (&x);
868 else
869 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
870 "not have a final %<)%>");
873 if (token != CPP_EOF)
875 error ("#pragma GCC optimize string... is badly formed");
876 return;
879 /* put arguments in the order the user typed them. */
880 args = nreverse (args);
882 parse_optimize_options (args, false);
883 current_optimize_pragma = chainon (current_optimize_pragma, args);
884 optimization_current_node = build_optimization_node (&global_options);
885 c_cpp_builtins_optimize_pragma (parse_in,
886 optimization_previous_node,
887 optimization_current_node);
891 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
892 both the binary representation of the options and the TREE_LIST of
893 strings that will be added to the function's attribute list. */
894 typedef struct GTY(()) opt_stack {
895 struct opt_stack *prev;
896 tree target_binary;
897 tree target_strings;
898 tree optimize_binary;
899 tree optimize_strings;
900 } opt_stack;
902 static GTY(()) struct opt_stack * options_stack;
904 /* Handle #pragma GCC push_options to save the current target and optimization
905 options. */
907 static void
908 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
910 enum cpp_ttype token;
911 tree x = 0;
913 token = pragma_lex (&x);
914 if (token != CPP_EOF)
916 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
917 return;
920 opt_stack *p = ggc_alloc<opt_stack> ();
921 p->prev = options_stack;
922 options_stack = p;
924 /* Save optimization and target flags in binary format. */
925 p->optimize_binary = build_optimization_node (&global_options);
926 p->target_binary = build_target_option_node (&global_options);
928 /* Save optimization and target flags in string list format. */
929 p->optimize_strings = copy_list (current_optimize_pragma);
930 p->target_strings = copy_list (current_target_pragma);
933 /* Handle #pragma GCC pop_options to restore the current target and
934 optimization options from a previous push_options. */
936 static void
937 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
939 enum cpp_ttype token;
940 tree x = 0;
941 opt_stack *p;
943 token = pragma_lex (&x);
944 if (token != CPP_EOF)
946 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
947 return;
950 if (! options_stack)
952 warning (OPT_Wpragmas,
953 "%<#pragma GCC pop_options%> without a corresponding "
954 "%<#pragma GCC push_options%>");
955 return;
958 p = options_stack;
959 options_stack = p->prev;
961 if (p->target_binary != target_option_current_node)
963 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
964 target_option_current_node = p->target_binary;
967 if (p->optimize_binary != optimization_current_node)
969 tree old_optimize = optimization_current_node;
970 cl_optimization_restore (&global_options,
971 TREE_OPTIMIZATION (p->optimize_binary));
972 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
973 p->optimize_binary);
974 optimization_current_node = p->optimize_binary;
977 current_target_pragma = p->target_strings;
978 current_optimize_pragma = p->optimize_strings;
981 /* Handle #pragma GCC reset_options to restore the current target and
982 optimization options to the original options used on the command line. */
984 static void
985 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
987 enum cpp_ttype token;
988 tree x = 0;
989 tree new_optimize = optimization_default_node;
990 tree new_target = target_option_default_node;
992 token = pragma_lex (&x);
993 if (token != CPP_EOF)
995 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
996 return;
999 if (new_target != target_option_current_node)
1001 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1002 target_option_current_node = new_target;
1005 if (new_optimize != optimization_current_node)
1007 tree old_optimize = optimization_current_node;
1008 cl_optimization_restore (&global_options,
1009 TREE_OPTIMIZATION (new_optimize));
1010 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1011 optimization_current_node = new_optimize;
1014 current_target_pragma = NULL_TREE;
1015 current_optimize_pragma = NULL_TREE;
1018 /* Print a plain user-specified message. */
1020 static void
1021 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1023 enum cpp_ttype token;
1024 tree x, message = 0;
1026 token = pragma_lex (&x);
1027 if (token == CPP_OPEN_PAREN)
1029 token = pragma_lex (&x);
1030 if (token == CPP_STRING)
1031 message = x;
1032 else
1033 GCC_BAD ("expected a string after %<#pragma message%>");
1034 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1035 GCC_BAD ("malformed %<#pragma message%>, ignored");
1037 else if (token == CPP_STRING)
1038 message = x;
1039 else
1040 GCC_BAD ("expected a string after %<#pragma message%>");
1042 gcc_assert (message);
1044 if (pragma_lex (&x) != CPP_EOF)
1045 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1047 if (TREE_STRING_LENGTH (message) > 1)
1048 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1051 /* Mark whether the current location is valid for a STDC pragma. */
1053 static bool valid_location_for_stdc_pragma;
1055 void
1056 mark_valid_location_for_stdc_pragma (bool flag)
1058 valid_location_for_stdc_pragma = flag;
1061 /* Return true if the current location is valid for a STDC pragma. */
1063 bool
1064 valid_location_for_stdc_pragma_p (void)
1066 return valid_location_for_stdc_pragma;
1069 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1071 /* A STDC pragma must appear outside of external declarations or
1072 preceding all explicit declarations and statements inside a compound
1073 statement; its behavior is undefined if used in any other context.
1074 It takes a switch of ON, OFF, or DEFAULT. */
1076 static enum pragma_switch_t
1077 handle_stdc_pragma (const char *pname)
1079 const char *arg;
1080 tree t;
1081 enum pragma_switch_t ret;
1083 if (!valid_location_for_stdc_pragma_p ())
1085 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1086 pname);
1087 return PRAGMA_BAD;
1090 if (pragma_lex (&t) != CPP_NAME)
1092 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1093 return PRAGMA_BAD;
1096 arg = IDENTIFIER_POINTER (t);
1098 if (!strcmp (arg, "ON"))
1099 ret = PRAGMA_ON;
1100 else if (!strcmp (arg, "OFF"))
1101 ret = PRAGMA_OFF;
1102 else if (!strcmp (arg, "DEFAULT"))
1103 ret = PRAGMA_DEFAULT;
1104 else
1106 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1107 return PRAGMA_BAD;
1110 if (pragma_lex (&t) != CPP_EOF)
1112 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1113 return PRAGMA_BAD;
1116 return ret;
1119 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1120 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1121 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1123 static void
1124 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1126 if (c_dialect_cxx ())
1128 if (warn_unknown_pragmas > in_system_header_at (input_location))
1129 warning (OPT_Wunknown_pragmas,
1130 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1131 " for C++");
1132 return;
1135 if (!targetm.decimal_float_supported_p ())
1137 if (warn_unknown_pragmas > in_system_header_at (input_location))
1138 warning (OPT_Wunknown_pragmas,
1139 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1140 " on this target");
1141 return;
1144 pedwarn (input_location, OPT_Wpedantic,
1145 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1147 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1149 case PRAGMA_ON:
1150 set_float_const_decimal64 ();
1151 break;
1152 case PRAGMA_OFF:
1153 case PRAGMA_DEFAULT:
1154 clear_float_const_decimal64 ();
1155 break;
1156 case PRAGMA_BAD:
1157 break;
1161 /* A vector of registered pragma callbacks, which is never freed. */
1163 static vec<internal_pragma_handler> registered_pragmas;
1165 typedef struct
1167 const char *space;
1168 const char *name;
1169 } pragma_ns_name;
1172 static vec<pragma_ns_name> registered_pp_pragmas;
1174 struct omp_pragma_def { const char *name; unsigned int id; };
1175 static const struct omp_pragma_def omp_pragmas[] = {
1176 { "atomic", PRAGMA_OMP_ATOMIC },
1177 { "barrier", PRAGMA_OMP_BARRIER },
1178 { "cancel", PRAGMA_OMP_CANCEL },
1179 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1180 { "critical", PRAGMA_OMP_CRITICAL },
1181 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1182 { "flush", PRAGMA_OMP_FLUSH },
1183 { "master", PRAGMA_OMP_MASTER },
1184 { "ordered", PRAGMA_OMP_ORDERED },
1185 { "section", PRAGMA_OMP_SECTION },
1186 { "sections", PRAGMA_OMP_SECTIONS },
1187 { "single", PRAGMA_OMP_SINGLE },
1188 { "task", PRAGMA_OMP_TASK },
1189 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1190 { "taskwait", PRAGMA_OMP_TASKWAIT },
1191 { "taskyield", PRAGMA_OMP_TASKYIELD },
1192 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1194 static const struct omp_pragma_def omp_pragmas_simd[] = {
1195 { "declare", PRAGMA_OMP_DECLARE_REDUCTION },
1196 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1197 { "for", PRAGMA_OMP_FOR },
1198 { "parallel", PRAGMA_OMP_PARALLEL },
1199 { "simd", PRAGMA_OMP_SIMD },
1200 { "target", PRAGMA_OMP_TARGET },
1201 { "teams", PRAGMA_OMP_TEAMS },
1204 void
1205 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1207 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1208 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1209 / sizeof (*omp_pragmas);
1210 int i;
1212 for (i = 0; i < n_omp_pragmas; ++i)
1213 if (omp_pragmas[i].id == id)
1215 *space = "omp";
1216 *name = omp_pragmas[i].name;
1217 return;
1220 for (i = 0; i < n_omp_pragmas_simd; ++i)
1221 if (omp_pragmas_simd[i].id == id)
1223 *space = "omp";
1224 *name = omp_pragmas_simd[i].name;
1225 return;
1228 if (id == PRAGMA_CILK_SIMD)
1230 *space = NULL;
1231 *name = "simd";
1232 return;
1235 if (id >= PRAGMA_FIRST_EXTERNAL
1236 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1238 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1239 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1240 return;
1243 gcc_unreachable ();
1246 /* Front-end wrappers for pragma registration to avoid dragging
1247 cpplib.h in almost everywhere. */
1249 static void
1250 c_register_pragma_1 (const char *space, const char *name,
1251 internal_pragma_handler ihandler, bool allow_expansion)
1253 unsigned id;
1255 if (flag_preprocess_only)
1257 pragma_ns_name ns_name;
1259 if (!allow_expansion)
1260 return;
1262 ns_name.space = space;
1263 ns_name.name = name;
1264 registered_pp_pragmas.safe_push (ns_name);
1265 id = registered_pp_pragmas.length ();
1266 id += PRAGMA_FIRST_EXTERNAL - 1;
1268 else
1270 registered_pragmas.safe_push (ihandler);
1271 id = registered_pragmas.length ();
1272 id += PRAGMA_FIRST_EXTERNAL - 1;
1274 /* The C++ front end allocates 6 bits in cp_token; the C front end
1275 allocates 7 bits in c_token. At present this is sufficient. */
1276 gcc_assert (id < 64);
1279 cpp_register_deferred_pragma (parse_in, space, name, id,
1280 allow_expansion, false);
1283 /* Register a C pragma handler, using a space and a name. It disallows pragma
1284 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1285 void
1286 c_register_pragma (const char *space, const char *name,
1287 pragma_handler_1arg handler)
1289 internal_pragma_handler ihandler;
1291 ihandler.handler.handler_1arg = handler;
1292 ihandler.extra_data = false;
1293 ihandler.data = NULL;
1294 c_register_pragma_1 (space, name, ihandler, false);
1297 /* Register a C pragma handler, using a space and a name, it also carries an
1298 extra data field which can be used by the handler. It disallows pragma
1299 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1300 instead). */
1301 void
1302 c_register_pragma_with_data (const char *space, const char *name,
1303 pragma_handler_2arg handler, void * data)
1305 internal_pragma_handler ihandler;
1307 ihandler.handler.handler_2arg = handler;
1308 ihandler.extra_data = true;
1309 ihandler.data = data;
1310 c_register_pragma_1 (space, name, ihandler, false);
1313 /* Register a C pragma handler, using a space and a name. It allows pragma
1314 expansion as in the following example:
1316 #define NUMBER 10
1317 #pragma count (NUMBER)
1319 Name expansion is still disallowed. */
1320 void
1321 c_register_pragma_with_expansion (const char *space, const char *name,
1322 pragma_handler_1arg handler)
1324 internal_pragma_handler ihandler;
1326 ihandler.handler.handler_1arg = handler;
1327 ihandler.extra_data = false;
1328 ihandler.data = NULL;
1329 c_register_pragma_1 (space, name, ihandler, true);
1332 /* Register a C pragma handler, using a space and a name, it also carries an
1333 extra data field which can be used by the handler. It allows pragma
1334 expansion as in the following example:
1336 #define NUMBER 10
1337 #pragma count (NUMBER)
1339 Name expansion is still disallowed. */
1340 void
1341 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1342 pragma_handler_2arg handler,
1343 void *data)
1345 internal_pragma_handler ihandler;
1347 ihandler.handler.handler_2arg = handler;
1348 ihandler.extra_data = true;
1349 ihandler.data = data;
1350 c_register_pragma_1 (space, name, ihandler, true);
1353 void
1354 c_invoke_pragma_handler (unsigned int id)
1356 internal_pragma_handler *ihandler;
1357 pragma_handler_1arg handler_1arg;
1358 pragma_handler_2arg handler_2arg;
1360 id -= PRAGMA_FIRST_EXTERNAL;
1361 ihandler = &registered_pragmas[id];
1362 if (ihandler->extra_data)
1364 handler_2arg = ihandler->handler.handler_2arg;
1365 handler_2arg (parse_in, ihandler->data);
1367 else
1369 handler_1arg = ihandler->handler.handler_1arg;
1370 handler_1arg (parse_in);
1374 /* Set up front-end pragmas. */
1375 void
1376 init_pragma (void)
1378 if (flag_openmp)
1380 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1381 int i;
1383 for (i = 0; i < n_omp_pragmas; ++i)
1384 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1385 omp_pragmas[i].id, true, true);
1387 if (flag_openmp || flag_openmp_simd)
1389 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1390 / sizeof (*omp_pragmas);
1391 int i;
1393 for (i = 0; i < n_omp_pragmas_simd; ++i)
1394 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1395 omp_pragmas_simd[i].id, true, true);
1398 if (flag_cilkplus)
1399 cpp_register_deferred_pragma (parse_in, NULL, "simd", PRAGMA_CILK_SIMD,
1400 true, false);
1402 if (!flag_preprocess_only)
1403 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1404 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1406 if (!flag_preprocess_only)
1407 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1408 false);
1409 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1410 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1411 #else
1412 c_register_pragma (0, "pack", handle_pragma_pack);
1413 #endif
1414 c_register_pragma (0, "weak", handle_pragma_weak);
1415 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1417 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1418 c_register_pragma ("GCC", "target", handle_pragma_target);
1419 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1420 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1421 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1422 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1424 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1425 handle_pragma_float_const_decimal64);
1427 c_register_pragma_with_expansion (0, "redefine_extname",
1428 handle_pragma_redefine_extname);
1430 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1432 #ifdef REGISTER_TARGET_PRAGMAS
1433 REGISTER_TARGET_PRAGMAS ();
1434 #endif
1436 /* Allow plugins to register their own pragmas. */
1437 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1440 #include "gt-c-family-c-pragma.h"