Merge trunk version 204659 into gupc branch.
[official-gcc.git] / gcc / c-family / c-pragma.c
bloba286e97d0d2cbfcaa912166e557fa63d58b39545
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 "c-upc.h"
33 #include "tm_p.h" /* For REGISTER_TARGET_PRAGMAS (why is
34 this not a target hook?). */
35 #include "vec.h"
36 #include "target.h"
37 #include "diagnostic.h"
38 #include "opts.h"
39 #include "plugin.h"
40 #include "cgraph.h"
42 #define GCC_BAD(gmsgid) \
43 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
44 #define GCC_BAD2(gmsgid, arg) \
45 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
47 typedef struct GTY(()) align_stack {
48 int alignment;
49 tree id;
50 struct align_stack * prev;
51 } align_stack;
53 static GTY(()) struct align_stack * alignment_stack;
55 static void handle_pragma_pack (cpp_reader *);
57 /* If we have a "global" #pragma pack(<n>) in effect when the first
58 #pragma pack(push,<n>) is encountered, this stores the value of
59 maximum_field_alignment in effect. When the final pop_alignment()
60 happens, we restore the value to this, not to a value of 0 for
61 maximum_field_alignment. Value is in bits. */
62 static int default_alignment;
63 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
64 ? &default_alignment \
65 : &alignment_stack->alignment) = (ALIGN))
67 static void push_alignment (int, tree);
68 static void pop_alignment (tree);
70 /* Push an alignment value onto the stack. */
71 static void
72 push_alignment (int alignment, tree id)
74 align_stack * entry;
76 entry = ggc_alloc_align_stack ();
78 entry->alignment = alignment;
79 entry->id = id;
80 entry->prev = alignment_stack;
82 /* The current value of maximum_field_alignment is not necessarily
83 0 since there may be a #pragma pack(<n>) in effect; remember it
84 so that we can restore it after the final #pragma pop(). */
85 if (alignment_stack == NULL)
86 default_alignment = maximum_field_alignment;
88 alignment_stack = entry;
90 maximum_field_alignment = alignment;
93 /* Undo a push of an alignment onto the stack. */
94 static void
95 pop_alignment (tree id)
97 align_stack * entry;
99 if (alignment_stack == NULL)
100 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
102 /* If we got an identifier, strip away everything above the target
103 entry so that the next step will restore the state just below it. */
104 if (id)
106 for (entry = alignment_stack; entry; entry = entry->prev)
107 if (entry->id == id)
109 alignment_stack = entry;
110 break;
112 if (entry == NULL)
113 warning (OPT_Wpragmas, "\
114 #pragma pack(pop, %E) encountered without matching #pragma pack(push, %E)"
115 , id, id);
118 entry = alignment_stack->prev;
120 maximum_field_alignment = entry ? entry->alignment : default_alignment;
122 alignment_stack = entry;
125 /* #pragma pack ()
126 #pragma pack (N)
128 #pragma pack (push)
129 #pragma pack (push, N)
130 #pragma pack (push, ID)
131 #pragma pack (push, ID, N)
132 #pragma pack (pop)
133 #pragma pack (pop, ID) */
134 static void
135 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
137 tree x, id = 0;
138 int align = -1;
139 enum cpp_ttype token;
140 enum { set, push, pop } action;
142 if (pragma_lex (&x) != CPP_OPEN_PAREN)
143 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
145 token = pragma_lex (&x);
146 if (token == CPP_CLOSE_PAREN)
148 action = set;
149 align = initial_max_fld_align;
151 else if (token == CPP_NUMBER)
153 if (TREE_CODE (x) != INTEGER_CST)
154 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
155 align = TREE_INT_CST_LOW (x);
156 action = set;
157 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
158 GCC_BAD ("malformed %<#pragma pack%> - ignored");
160 else if (token == CPP_NAME)
162 #define GCC_BAD_ACTION do { if (action != pop) \
163 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
164 else \
165 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
166 } while (0)
168 const char *op = IDENTIFIER_POINTER (x);
169 if (!strcmp (op, "push"))
170 action = push;
171 else if (!strcmp (op, "pop"))
172 action = pop;
173 else
174 GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
176 while ((token = pragma_lex (&x)) == CPP_COMMA)
178 token = pragma_lex (&x);
179 if (token == CPP_NAME && id == 0)
181 id = x;
183 else if (token == CPP_NUMBER && action == push && align == -1)
185 if (TREE_CODE (x) != INTEGER_CST)
186 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
187 align = TREE_INT_CST_LOW (x);
188 if (align == -1)
189 action = set;
191 else
192 GCC_BAD_ACTION;
195 if (token != CPP_CLOSE_PAREN)
196 GCC_BAD_ACTION;
197 #undef GCC_BAD_ACTION
199 else
200 GCC_BAD ("malformed %<#pragma pack%> - ignored");
202 if (pragma_lex (&x) != CPP_EOF)
203 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
205 if (flag_pack_struct)
206 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
208 if (action != pop)
209 switch (align)
211 case 0:
212 case 1:
213 case 2:
214 case 4:
215 case 8:
216 case 16:
217 align *= BITS_PER_UNIT;
218 break;
219 case -1:
220 if (action == push)
222 align = maximum_field_alignment;
223 break;
225 default:
226 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
229 switch (action)
231 case set: SET_GLOBAL_ALIGNMENT (align); break;
232 case push: push_alignment (align, id); break;
233 case pop: pop_alignment (id); break;
237 typedef struct GTY(()) pending_weak_d
239 tree name;
240 tree value;
241 } pending_weak;
244 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
246 static void apply_pragma_weak (tree, tree);
247 static void handle_pragma_weak (cpp_reader *);
249 static void
250 apply_pragma_weak (tree decl, tree value)
252 if (value)
254 value = build_string (IDENTIFIER_LENGTH (value),
255 IDENTIFIER_POINTER (value));
256 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
257 build_tree_list (NULL, value)),
261 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
262 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
263 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
264 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
265 "results in unspecified behavior", decl);
267 declare_weak (decl);
270 void
271 maybe_apply_pragma_weak (tree decl)
273 tree id;
274 int i;
275 pending_weak *pe;
277 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
279 /* No weak symbols pending, take the short-cut. */
280 if (!pending_weaks)
281 return;
282 /* If it's not visible outside this file, it doesn't matter whether
283 it's weak. */
284 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
285 return;
286 /* If it's not a function or a variable, it can't be weak.
287 FIXME: what kinds of things are visible outside this file but
288 aren't functions or variables? Should this be an assert instead? */
289 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
290 return;
292 id = DECL_ASSEMBLER_NAME (decl);
294 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
295 if (id == pe->name)
297 apply_pragma_weak (decl, pe->value);
298 pending_weaks->unordered_remove (i);
299 break;
303 /* Process all "#pragma weak A = B" directives where we have not seen
304 a decl for A. */
305 void
306 maybe_apply_pending_pragma_weaks (void)
308 tree alias_id, id, decl;
309 int i;
310 pending_weak *pe;
311 symtab_node *target;
313 if (!pending_weaks)
314 return;
316 FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
318 alias_id = pe->name;
319 id = pe->value;
321 if (id == NULL)
322 continue;
324 target = symtab_node_for_asm (id);
325 decl = build_decl (UNKNOWN_LOCATION,
326 target ? TREE_CODE (target->decl) : FUNCTION_DECL,
327 alias_id, default_function_type);
329 DECL_ARTIFICIAL (decl) = 1;
330 TREE_PUBLIC (decl) = 1;
331 DECL_WEAK (decl) = 1;
332 if (TREE_CODE (decl) == VAR_DECL)
333 TREE_STATIC (decl) = 1;
334 if (!target)
336 error ("%q+D aliased to undefined symbol %qE",
337 decl, id);
338 continue;
341 assemble_alias (decl, id);
345 /* #pragma weak name [= value] */
346 static void
347 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
349 tree name, value, x, decl;
350 enum cpp_ttype t;
352 value = 0;
354 if (pragma_lex (&name) != CPP_NAME)
355 GCC_BAD ("malformed #pragma weak, ignored");
356 t = pragma_lex (&x);
357 if (t == CPP_EQ)
359 if (pragma_lex (&value) != CPP_NAME)
360 GCC_BAD ("malformed #pragma weak, ignored");
361 t = pragma_lex (&x);
363 if (t != CPP_EOF)
364 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
366 decl = identifier_global_value (name);
367 if (decl && DECL_P (decl))
369 apply_pragma_weak (decl, value);
370 if (value)
372 DECL_EXTERNAL (decl) = 0;
373 if (TREE_CODE (decl) == VAR_DECL)
374 TREE_STATIC (decl) = 1;
375 assemble_alias (decl, value);
378 else
380 pending_weak pe = {name, value};
381 vec_safe_push (pending_weaks, pe);
385 /* GCC supports two #pragma directives for renaming the external
386 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
387 compatibility with the Solaris and VMS system headers. GCC also
388 has its own notation for this, __asm__("name") annotations.
390 Corner cases of these features and their interaction:
392 1) Both pragmas silently apply only to declarations with external
393 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
394 do not have this restriction.
396 2) In C++, both #pragmas silently apply only to extern "C" declarations.
397 Asm labels do not have this restriction.
399 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
400 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
401 new name is different, a warning issues and the name does not change.
403 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
404 *not* the DECL_ASSEMBLER_NAME.
406 5) If #pragma extern_prefix is in effect and a declaration occurs
407 with an __asm__ name, the #pragma extern_prefix is silently
408 ignored for that declaration.
410 6) If #pragma extern_prefix and #pragma redefine_extname apply to
411 the same declaration, whichever triggered first wins, and a warning
412 is issued. (We would like to have #pragma redefine_extname always
413 win, but it can appear either before or after the declaration, and
414 if it appears afterward, we have no way of knowing whether a modified
415 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
417 typedef struct GTY(()) pending_redefinition_d {
418 tree oldname;
419 tree newname;
420 } pending_redefinition;
423 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
425 static void handle_pragma_redefine_extname (cpp_reader *);
427 /* #pragma redefine_extname oldname newname */
428 static void
429 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
431 tree oldname, newname, decls, x;
432 enum cpp_ttype t;
433 bool found;
435 if (pragma_lex (&oldname) != CPP_NAME)
436 GCC_BAD ("malformed #pragma redefine_extname, ignored");
437 if (pragma_lex (&newname) != CPP_NAME)
438 GCC_BAD ("malformed #pragma redefine_extname, ignored");
439 t = pragma_lex (&x);
440 if (t != CPP_EOF)
441 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
443 found = false;
444 for (decls = c_linkage_bindings (oldname);
445 decls; )
447 tree decl;
448 if (TREE_CODE (decls) == TREE_LIST)
450 decl = TREE_VALUE (decls);
451 decls = TREE_CHAIN (decls);
453 else
455 decl = decls;
456 decls = NULL_TREE;
459 if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
460 && (TREE_CODE (decl) == FUNCTION_DECL
461 || TREE_CODE (decl) == VAR_DECL))
463 found = true;
464 if (DECL_ASSEMBLER_NAME_SET_P (decl))
466 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
467 name = targetm.strip_name_encoding (name);
469 if (strcmp (name, IDENTIFIER_POINTER (newname)))
470 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
471 "conflict with previous rename");
473 else
474 change_decl_assembler_name (decl, newname);
478 if (!found)
479 /* We have to add this to the rename list even if there's already
480 a global value that doesn't meet the above criteria, because in
481 C++ "struct foo {...};" puts "foo" in the current namespace but
482 does *not* conflict with a subsequent declaration of a function
483 or variable foo. See g++.dg/other/pragma-re-2.C. */
484 add_to_renaming_pragma_list (oldname, newname);
487 /* This is called from here and from ia64-c.c. */
488 void
489 add_to_renaming_pragma_list (tree oldname, tree newname)
491 unsigned ix;
492 pending_redefinition *p;
494 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
495 if (oldname == p->oldname)
497 if (p->newname != newname)
498 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
499 "conflict with previous #pragma redefine_extname");
500 return;
503 pending_redefinition e = {oldname, newname};
504 vec_safe_push (pending_redefine_extname, e);
507 /* The current prefix set by #pragma extern_prefix. */
508 GTY(()) tree pragma_extern_prefix;
510 /* variables used to implement #pragma upc semantics */
511 #ifndef UPC_CMODE_STACK_INCREMENT
512 #define UPC_CMODE_STACK_INCREMENT 32
513 #endif
514 static int pragma_upc_permitted;
515 static int upc_cmode;
516 static int *upc_cmode_stack;
517 static int upc_cmode_stack_in_use;
518 static int upc_cmode_stack_allocated;
520 static void init_pragma_upc (void);
521 static void handle_pragma_upc (cpp_reader * ARG_UNUSED (dummy));
523 /* Initialize the variables used to manage the current UPC consistency
524 mode (strict/relaxed) */
526 static void
527 init_pragma_upc (void)
529 pragma_upc_permitted = 0;
530 upc_cmode = 0;
531 upc_cmode_stack = (int *) xcalloc (UPC_CMODE_STACK_INCREMENT,
532 sizeof (int));
533 upc_cmode_stack_allocated = UPC_CMODE_STACK_INCREMENT;
534 upc_cmode_stack_in_use = 0;
538 * #pragma upc strict
539 * #pragma upc relaxed
540 * #pragma upc upc_code
541 * #pragma upc c_code
543 static void
544 handle_pragma_upc (cpp_reader * ARG_UNUSED (dummy))
546 tree x;
547 enum cpp_ttype t;
548 enum upc_pragma_op {p_strict, p_relaxed, p_upc_code,
549 p_c_code, p_detect_upc, p_unknown};
550 enum upc_pragma_op upc_pragma = p_unknown;
552 if (!flag_upc)
554 warning (OPT_Wpragmas, "#pragma upc found in non-UPC source file");
555 return;
558 t = pragma_lex (&x);
559 if (t == CPP_NAME)
561 const char *op = IDENTIFIER_POINTER (x);
562 if (!strcmp (op, "strict"))
563 upc_pragma = p_strict;
564 else if (!strcmp (op, "relaxed"))
565 upc_pragma = p_relaxed;
566 else if (!strcmp (op, "upc_code"))
567 upc_pragma = p_upc_code;
568 else if (!strcmp (op, "c_code"))
569 upc_pragma = p_c_code;
570 else if (!strcmp (op, "detect_upc"))
572 const char *detect_op;
573 upc_pragma = p_detect_upc;
574 t = pragma_lex (&x);
575 if (t != CPP_NAME)
576 GCC_BAD ("missing [suspend_insertion|resume_insertion]"
577 " after %<#pragma UPC detect_upc%>");
578 detect_op = IDENTIFIER_POINTER (x);
579 if (strcmp (detect_op, "suspend_insertion") == 0)
580 /* no action */;
581 else if (strcmp (detect_op, "resume_insertion") == 0)
582 /* no action */;
583 else
584 GCC_BAD ("expected [suspend_insertion|resume_insertion]"
585 " after %<#pragma UPC detect_upc%>");
587 else
588 GCC_BAD2 ("unknown action '%s' for '#pragma upc' - ignored", op);
590 else
591 warning (OPT_Wpragmas, "missing parameter after #pragma upc");
593 t = pragma_lex (&x);
594 if (t != CPP_EOF)
595 warning (OPT_Wpragmas, "junk at end of #pragma upc");
597 if ((upc_pragma == p_strict) || (upc_pragma == p_relaxed))
599 if (pragma_upc_permitted_p ())
601 int consistency_mode = (upc_pragma == p_strict);
602 set_upc_consistency_mode (consistency_mode);
604 else
605 warning (OPT_Wpragmas, "#pragma upc not allowed in this context");
607 else if ((upc_pragma == p_upc_code) || (upc_pragma == p_c_code))
609 compiling_upc = (upc_pragma == p_upc_code);
611 else if (upc_pragma == p_detect_upc)
613 /* Skip: this is a Berkeley-specific pragma that requires no action. */
617 /* Set the current setting of the UPC consistency mode
618 that is in effect. */
620 void
621 set_upc_consistency_mode (int mode)
623 upc_cmode = mode;
626 /* Return the current setting of the UPC consistency mode. */
629 get_upc_consistency_mode (void)
631 return upc_cmode;
634 /* Called from the parser just after the bracket that opens a compound
635 statement has been parsed. Set the flag that allows the pragma
636 in this context. */
638 void
639 permit_pragma_upc (void)
641 pragma_upc_permitted = 1;
644 /* Called just before the body of a compound statement is parsed.
645 Clear the flag that allows the pragma. */
647 void
648 deny_pragma_upc (void)
650 pragma_upc_permitted = 0;
653 /* A #pragma upc is permitted either at the outermost scope,
654 or directly after the bracket that opens a compound statement. */
657 pragma_upc_permitted_p (void)
659 return !current_function_decl || pragma_upc_permitted;
662 /* Called at the beginning of every compound statement.
663 Pushes the old value of the current UPC consistency mode
664 onto the stack. */
666 void
667 push_upc_consistency_mode (void)
669 if (upc_cmode_stack_in_use == upc_cmode_stack_allocated)
671 upc_cmode_stack_allocated += UPC_CMODE_STACK_INCREMENT;
672 upc_cmode_stack = (int *) xrealloc (upc_cmode_stack,
673 upc_cmode_stack_allocated * sizeof (int));
675 upc_cmode_stack[upc_cmode_stack_in_use++] = upc_cmode;
678 /* Called at the end of every compound statement.
679 Sets the current consistency mode to the previously saved value. */
681 void
682 pop_upc_consistency_mode (void)
684 if (upc_cmode_stack_in_use <= 0)
685 abort ();
686 upc_cmode = upc_cmode_stack[--upc_cmode_stack_in_use];
689 static int pragma_pupc_on;
690 static void init_pragma_pupc (void);
691 static void handle_pragma_pupc (cpp_reader *);
693 /* Pragma pupc defaults to being on */
694 static void
695 init_pragma_pupc (void)
697 pragma_pupc_on = 1;
701 get_upc_pupc_mode (void)
703 return pragma_pupc_on;
707 disable_pupc_mode (void)
709 int old_pupc = pragma_pupc_on;
710 pragma_pupc_on = 0;
711 return old_pupc;
714 void
715 set_pupc_mode (int new_pupc)
717 pragma_pupc_on = new_pupc;
721 * #pragma pupc on
722 * #pragma pupc off
724 static void
725 handle_pragma_pupc (cpp_reader *dummy ATTRIBUTE_UNUSED)
727 tree x;
728 enum cpp_ttype t;
730 t = pragma_lex(&x);
731 if (t == CPP_NAME) {
732 const char *op = IDENTIFIER_POINTER (x);
733 if (!strcmp (op, "on"))
734 pragma_pupc_on = 1;
735 else if (!strcmp (op, "off"))
736 pragma_pupc_on = 0;
737 else
738 GCC_BAD2 ("unknown action '%s' for '#pragma pupc' - ignored", op);
741 t = pragma_lex (&x);
742 if (t != CPP_EOF)
743 warning (OPT_Wpragmas, "junk at end of #pragma pupc");
746 /* Hook from the front ends to apply the results of one of the preceding
747 pragmas that rename variables. */
749 tree
750 maybe_apply_renaming_pragma (tree decl, tree asmname)
752 unsigned ix;
753 pending_redefinition *p;
755 /* The renaming pragmas are only applied to declarations with
756 external linkage. */
757 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
758 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
759 || !has_c_linkage (decl))
760 return asmname;
762 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
763 but we may warn about a rename that conflicts. */
764 if (DECL_ASSEMBLER_NAME_SET_P (decl))
766 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
767 oldname = targetm.strip_name_encoding (oldname);
769 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
770 warning (OPT_Wpragmas, "asm declaration ignored due to "
771 "conflict with previous rename");
773 /* Take any pending redefine_extname off the list. */
774 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
775 if (DECL_NAME (decl) == p->oldname)
777 /* Only warn if there is a conflict. */
778 if (strcmp (IDENTIFIER_POINTER (p->newname), oldname))
779 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
780 "conflict with previous rename");
782 pending_redefine_extname->unordered_remove (ix);
783 break;
785 return 0;
788 /* Find out if we have a pending #pragma redefine_extname. */
789 FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
790 if (DECL_NAME (decl) == p->oldname)
792 tree newname = p->newname;
793 pending_redefine_extname->unordered_remove (ix);
795 /* If we already have an asmname, #pragma redefine_extname is
796 ignored (with a warning if it conflicts). */
797 if (asmname)
799 if (strcmp (TREE_STRING_POINTER (asmname),
800 IDENTIFIER_POINTER (newname)) != 0)
801 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
802 "conflict with __asm__ declaration");
803 return asmname;
806 /* Otherwise we use what we've got; #pragma extern_prefix is
807 silently ignored. */
808 return build_string (IDENTIFIER_LENGTH (newname),
809 IDENTIFIER_POINTER (newname));
812 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
813 if (asmname)
814 return asmname;
816 /* If #pragma extern_prefix is in effect, apply it. */
817 if (pragma_extern_prefix)
819 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
820 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
822 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
823 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
825 char *newname = (char *) alloca (plen + ilen + 1);
827 memcpy (newname, prefix, plen);
828 memcpy (newname + plen, id, ilen + 1);
830 return build_string (plen + ilen, newname);
833 /* Nada. */
834 return 0;
838 static void handle_pragma_visibility (cpp_reader *);
840 static vec<int> visstack;
842 /* Push the visibility indicated by STR onto the top of the #pragma
843 visibility stack. KIND is 0 for #pragma GCC visibility, 1 for
844 C++ namespace with visibility attribute and 2 for C++ builtin
845 ABI namespace. push_visibility/pop_visibility calls must have
846 matching KIND, it is not allowed to push visibility using one
847 KIND and pop using a different one. */
849 void
850 push_visibility (const char *str, int kind)
852 visstack.safe_push (((int) default_visibility) | (kind << 8));
853 if (!strcmp (str, "default"))
854 default_visibility = VISIBILITY_DEFAULT;
855 else if (!strcmp (str, "internal"))
856 default_visibility = VISIBILITY_INTERNAL;
857 else if (!strcmp (str, "hidden"))
858 default_visibility = VISIBILITY_HIDDEN;
859 else if (!strcmp (str, "protected"))
860 default_visibility = VISIBILITY_PROTECTED;
861 else
862 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
863 visibility_options.inpragma = 1;
866 /* Pop a level of the #pragma visibility stack. Return true if
867 successful. */
869 bool
870 pop_visibility (int kind)
872 if (!visstack.length ())
873 return false;
874 if ((visstack.last () >> 8) != kind)
875 return false;
876 default_visibility
877 = (enum symbol_visibility) (visstack.pop () & 0xff);
878 visibility_options.inpragma
879 = visstack.length () != 0;
880 return true;
883 /* Sets the default visibility for symbols to something other than that
884 specified on the command line. */
886 static void
887 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
889 /* Form is #pragma GCC visibility push(hidden)|pop */
890 tree x;
891 enum cpp_ttype token;
892 enum { bad, push, pop } action = bad;
894 token = pragma_lex (&x);
895 if (token == CPP_NAME)
897 const char *op = IDENTIFIER_POINTER (x);
898 if (!strcmp (op, "push"))
899 action = push;
900 else if (!strcmp (op, "pop"))
901 action = pop;
903 if (bad == action)
904 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
905 else
907 if (pop == action)
909 if (! pop_visibility (0))
910 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
912 else
914 if (pragma_lex (&x) != CPP_OPEN_PAREN)
915 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
916 token = pragma_lex (&x);
917 if (token != CPP_NAME)
918 GCC_BAD ("malformed #pragma GCC visibility push");
919 else
920 push_visibility (IDENTIFIER_POINTER (x), 0);
921 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
922 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
925 if (pragma_lex (&x) != CPP_EOF)
926 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
929 static void
930 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
932 const char *kind_string, *option_string;
933 unsigned int option_index;
934 enum cpp_ttype token;
935 diagnostic_t kind;
936 tree x;
937 struct cl_option_handlers handlers;
939 token = pragma_lex (&x);
940 if (token != CPP_NAME)
941 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
942 kind_string = IDENTIFIER_POINTER (x);
943 if (strcmp (kind_string, "error") == 0)
944 kind = DK_ERROR;
945 else if (strcmp (kind_string, "warning") == 0)
946 kind = DK_WARNING;
947 else if (strcmp (kind_string, "ignored") == 0)
948 kind = DK_IGNORED;
949 else if (strcmp (kind_string, "push") == 0)
951 diagnostic_push_diagnostics (global_dc, input_location);
952 return;
954 else if (strcmp (kind_string, "pop") == 0)
956 diagnostic_pop_diagnostics (global_dc, input_location);
957 return;
959 else
960 GCC_BAD ("expected [error|warning|ignored|push|pop] after %<#pragma GCC diagnostic%>");
962 token = pragma_lex (&x);
963 if (token != CPP_STRING)
964 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
965 option_string = TREE_STRING_POINTER (x);
966 set_default_handlers (&handlers);
967 for (option_index = 0; option_index < cl_options_count; option_index++)
968 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
970 control_warning_option (option_index, (int) kind, kind != DK_IGNORED,
971 input_location, c_family_lang_mask, &handlers,
972 &global_options, &global_options_set,
973 global_dc);
974 return;
976 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
979 /* Parse #pragma GCC target (xxx) to set target specific options. */
980 static void
981 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
983 enum cpp_ttype token;
984 tree x;
985 bool close_paren_needed_p = false;
987 if (cfun)
989 error ("#pragma GCC option is not allowed inside functions");
990 return;
993 token = pragma_lex (&x);
994 if (token == CPP_OPEN_PAREN)
996 close_paren_needed_p = true;
997 token = pragma_lex (&x);
1000 if (token != CPP_STRING)
1002 GCC_BAD ("%<#pragma GCC option%> is not a string");
1003 return;
1006 /* Strings are user options. */
1007 else
1009 tree args = NULL_TREE;
1013 /* Build up the strings now as a tree linked list. Skip empty
1014 strings. */
1015 if (TREE_STRING_LENGTH (x) > 0)
1016 args = tree_cons (NULL_TREE, x, args);
1018 token = pragma_lex (&x);
1019 while (token == CPP_COMMA)
1020 token = pragma_lex (&x);
1022 while (token == CPP_STRING);
1024 if (close_paren_needed_p)
1026 if (token == CPP_CLOSE_PAREN)
1027 token = pragma_lex (&x);
1028 else
1029 GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
1030 "not have a final %<)%>");
1033 if (token != CPP_EOF)
1035 error ("#pragma GCC target string... is badly formed");
1036 return;
1039 /* put arguments in the order the user typed them. */
1040 args = nreverse (args);
1042 if (targetm.target_option.pragma_parse (args, NULL_TREE))
1043 current_target_pragma = args;
1047 /* Handle #pragma GCC optimize to set optimization options. */
1048 static void
1049 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
1051 enum cpp_ttype token;
1052 tree x;
1053 bool close_paren_needed_p = false;
1054 tree optimization_previous_node = optimization_current_node;
1056 if (cfun)
1058 error ("#pragma GCC optimize is not allowed inside functions");
1059 return;
1062 token = pragma_lex (&x);
1063 if (token == CPP_OPEN_PAREN)
1065 close_paren_needed_p = true;
1066 token = pragma_lex (&x);
1069 if (token != CPP_STRING && token != CPP_NUMBER)
1071 GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
1072 return;
1075 /* Strings/numbers are user options. */
1076 else
1078 tree args = NULL_TREE;
1082 /* Build up the numbers/strings now as a list. */
1083 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
1084 args = tree_cons (NULL_TREE, x, args);
1086 token = pragma_lex (&x);
1087 while (token == CPP_COMMA)
1088 token = pragma_lex (&x);
1090 while (token == CPP_STRING || token == CPP_NUMBER);
1092 if (close_paren_needed_p)
1094 if (token == CPP_CLOSE_PAREN)
1095 token = pragma_lex (&x);
1096 else
1097 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
1098 "not have a final %<)%>");
1101 if (token != CPP_EOF)
1103 error ("#pragma GCC optimize string... is badly formed");
1104 return;
1107 /* put arguments in the order the user typed them. */
1108 args = nreverse (args);
1110 parse_optimize_options (args, false);
1111 current_optimize_pragma = chainon (current_optimize_pragma, args);
1112 optimization_current_node = build_optimization_node (&global_options);
1113 c_cpp_builtins_optimize_pragma (parse_in,
1114 optimization_previous_node,
1115 optimization_current_node);
1119 /* Stack of the #pragma GCC options created with #pragma GCC push_option. Save
1120 both the binary representation of the options and the TREE_LIST of
1121 strings that will be added to the function's attribute list. */
1122 typedef struct GTY(()) opt_stack {
1123 struct opt_stack *prev;
1124 tree target_binary;
1125 tree target_strings;
1126 tree optimize_binary;
1127 tree optimize_strings;
1128 } opt_stack;
1130 static GTY(()) struct opt_stack * options_stack;
1132 /* Handle #pragma GCC push_options to save the current target and optimization
1133 options. */
1135 static void
1136 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1138 enum cpp_ttype token;
1139 tree x = 0;
1140 opt_stack *p;
1142 token = pragma_lex (&x);
1143 if (token != CPP_EOF)
1145 warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1146 return;
1149 p = ggc_alloc_opt_stack ();
1150 p->prev = options_stack;
1151 options_stack = p;
1153 /* Save optimization and target flags in binary format. */
1154 p->optimize_binary = build_optimization_node (&global_options);
1155 p->target_binary = build_target_option_node (&global_options);
1157 /* Save optimization and target flags in string list format. */
1158 p->optimize_strings = copy_list (current_optimize_pragma);
1159 p->target_strings = copy_list (current_target_pragma);
1162 /* Handle #pragma GCC pop_options to restore the current target and
1163 optimization options from a previous push_options. */
1165 static void
1166 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1168 enum cpp_ttype token;
1169 tree x = 0;
1170 opt_stack *p;
1172 token = pragma_lex (&x);
1173 if (token != CPP_EOF)
1175 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1176 return;
1179 if (! options_stack)
1181 warning (OPT_Wpragmas,
1182 "%<#pragma GCC pop_options%> without a corresponding "
1183 "%<#pragma GCC push_options%>");
1184 return;
1187 p = options_stack;
1188 options_stack = p->prev;
1190 if (p->target_binary != target_option_current_node)
1192 (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1193 target_option_current_node = p->target_binary;
1196 if (p->optimize_binary != optimization_current_node)
1198 tree old_optimize = optimization_current_node;
1199 cl_optimization_restore (&global_options,
1200 TREE_OPTIMIZATION (p->optimize_binary));
1201 c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
1202 p->optimize_binary);
1203 optimization_current_node = p->optimize_binary;
1206 current_target_pragma = p->target_strings;
1207 current_optimize_pragma = p->optimize_strings;
1210 /* Handle #pragma GCC reset_options to restore the current target and
1211 optimization options to the original options used on the command line. */
1213 static void
1214 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1216 enum cpp_ttype token;
1217 tree x = 0;
1218 tree new_optimize = optimization_default_node;
1219 tree new_target = target_option_default_node;
1221 token = pragma_lex (&x);
1222 if (token != CPP_EOF)
1224 warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1225 return;
1228 if (new_target != target_option_current_node)
1230 (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1231 target_option_current_node = new_target;
1234 if (new_optimize != optimization_current_node)
1236 tree old_optimize = optimization_current_node;
1237 cl_optimization_restore (&global_options,
1238 TREE_OPTIMIZATION (new_optimize));
1239 c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1240 optimization_current_node = new_optimize;
1243 current_target_pragma = NULL_TREE;
1244 current_optimize_pragma = NULL_TREE;
1247 /* Print a plain user-specified message. */
1249 static void
1250 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1252 enum cpp_ttype token;
1253 tree x, message = 0;
1255 token = pragma_lex (&x);
1256 if (token == CPP_OPEN_PAREN)
1258 token = pragma_lex (&x);
1259 if (token == CPP_STRING)
1260 message = x;
1261 else
1262 GCC_BAD ("expected a string after %<#pragma message%>");
1263 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1264 GCC_BAD ("malformed %<#pragma message%>, ignored");
1266 else if (token == CPP_STRING)
1267 message = x;
1268 else
1269 GCC_BAD ("expected a string after %<#pragma message%>");
1271 gcc_assert (message);
1273 if (pragma_lex (&x) != CPP_EOF)
1274 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1276 if (TREE_STRING_LENGTH (message) > 1)
1277 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1280 /* Mark whether the current location is valid for a STDC pragma. */
1282 static bool valid_location_for_stdc_pragma;
1284 void
1285 mark_valid_location_for_stdc_pragma (bool flag)
1287 valid_location_for_stdc_pragma = flag;
1290 /* Return true if the current location is valid for a STDC pragma. */
1292 bool
1293 valid_location_for_stdc_pragma_p (void)
1295 return valid_location_for_stdc_pragma;
1298 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1300 /* A STDC pragma must appear outside of external declarations or
1301 preceding all explicit declarations and statements inside a compound
1302 statement; its behavior is undefined if used in any other context.
1303 It takes a switch of ON, OFF, or DEFAULT. */
1305 static enum pragma_switch_t
1306 handle_stdc_pragma (const char *pname)
1308 const char *arg;
1309 tree t;
1310 enum pragma_switch_t ret;
1312 if (!valid_location_for_stdc_pragma_p ())
1314 warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1315 pname);
1316 return PRAGMA_BAD;
1319 if (pragma_lex (&t) != CPP_NAME)
1321 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1322 return PRAGMA_BAD;
1325 arg = IDENTIFIER_POINTER (t);
1327 if (!strcmp (arg, "ON"))
1328 ret = PRAGMA_ON;
1329 else if (!strcmp (arg, "OFF"))
1330 ret = PRAGMA_OFF;
1331 else if (!strcmp (arg, "DEFAULT"))
1332 ret = PRAGMA_DEFAULT;
1333 else
1335 warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1336 return PRAGMA_BAD;
1339 if (pragma_lex (&t) != CPP_EOF)
1341 warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1342 return PRAGMA_BAD;
1345 return ret;
1348 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1349 #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1350 #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1352 static void
1353 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1355 if (c_dialect_cxx ())
1357 if (warn_unknown_pragmas > in_system_header)
1358 warning (OPT_Wunknown_pragmas,
1359 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1360 " for C++");
1361 return;
1364 if (!targetm.decimal_float_supported_p ())
1366 if (warn_unknown_pragmas > in_system_header)
1367 warning (OPT_Wunknown_pragmas,
1368 "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1369 " on this target");
1370 return;
1373 pedwarn (input_location, OPT_Wpedantic,
1374 "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1376 switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1378 case PRAGMA_ON:
1379 set_float_const_decimal64 ();
1380 break;
1381 case PRAGMA_OFF:
1382 case PRAGMA_DEFAULT:
1383 clear_float_const_decimal64 ();
1384 break;
1385 case PRAGMA_BAD:
1386 break;
1390 /* A vector of registered pragma callbacks, which is never freed. */
1392 static vec<internal_pragma_handler> registered_pragmas;
1394 typedef struct
1396 const char *space;
1397 const char *name;
1398 } pragma_ns_name;
1401 static vec<pragma_ns_name> registered_pp_pragmas;
1403 struct omp_pragma_def { const char *name; unsigned int id; };
1404 static const struct omp_pragma_def omp_pragmas[] = {
1405 { "atomic", PRAGMA_OMP_ATOMIC },
1406 { "barrier", PRAGMA_OMP_BARRIER },
1407 { "cancel", PRAGMA_OMP_CANCEL },
1408 { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1409 { "critical", PRAGMA_OMP_CRITICAL },
1410 { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1411 { "flush", PRAGMA_OMP_FLUSH },
1412 { "master", PRAGMA_OMP_MASTER },
1413 { "ordered", PRAGMA_OMP_ORDERED },
1414 { "section", PRAGMA_OMP_SECTION },
1415 { "sections", PRAGMA_OMP_SECTIONS },
1416 { "single", PRAGMA_OMP_SINGLE },
1417 { "taskgroup", PRAGMA_OMP_TASKGROUP },
1418 { "taskwait", PRAGMA_OMP_TASKWAIT },
1419 { "taskyield", PRAGMA_OMP_TASKYIELD },
1420 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1422 static const struct omp_pragma_def omp_pragmas_simd[] = {
1423 { "declare", PRAGMA_OMP_DECLARE_REDUCTION },
1424 { "distribute", PRAGMA_OMP_DISTRIBUTE },
1425 { "for", PRAGMA_OMP_FOR },
1426 { "parallel", PRAGMA_OMP_PARALLEL },
1427 { "simd", PRAGMA_OMP_SIMD },
1428 { "target", PRAGMA_OMP_TARGET },
1429 { "task", PRAGMA_OMP_TASK },
1430 { "teams", PRAGMA_OMP_TEAMS },
1433 void
1434 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1436 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1437 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1438 / sizeof (*omp_pragmas);
1439 int i;
1441 for (i = 0; i < n_omp_pragmas; ++i)
1442 if (omp_pragmas[i].id == id)
1444 *space = "omp";
1445 *name = omp_pragmas[i].name;
1446 return;
1449 for (i = 0; i < n_omp_pragmas_simd; ++i)
1450 if (omp_pragmas_simd[i].id == id)
1452 *space = "omp";
1453 *name = omp_pragmas_simd[i].name;
1454 return;
1457 if (id >= PRAGMA_FIRST_EXTERNAL
1458 && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1460 *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1461 *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1462 return;
1465 gcc_unreachable ();
1468 /* Front-end wrappers for pragma registration to avoid dragging
1469 cpplib.h in almost everywhere. */
1471 static void
1472 c_register_pragma_1 (const char *space, const char *name,
1473 internal_pragma_handler ihandler, bool allow_expansion)
1475 unsigned id;
1477 if (flag_preprocess_only)
1479 pragma_ns_name ns_name;
1481 if (!allow_expansion)
1482 return;
1484 ns_name.space = space;
1485 ns_name.name = name;
1486 registered_pp_pragmas.safe_push (ns_name);
1487 id = registered_pp_pragmas.length ();
1488 id += PRAGMA_FIRST_EXTERNAL - 1;
1490 else
1492 registered_pragmas.safe_push (ihandler);
1493 id = registered_pragmas.length ();
1494 id += PRAGMA_FIRST_EXTERNAL - 1;
1496 /* The C++ front end allocates 6 bits in cp_token; the C front end
1497 allocates 7 bits in c_token. At present this is sufficient. */
1498 gcc_assert (id < 64);
1501 cpp_register_deferred_pragma (parse_in, space, name, id,
1502 allow_expansion, false);
1505 /* Register a C pragma handler, using a space and a name. It disallows pragma
1506 expansion (if you want it, use c_register_pragma_with_expansion instead). */
1507 void
1508 c_register_pragma (const char *space, const char *name,
1509 pragma_handler_1arg handler)
1511 internal_pragma_handler ihandler;
1513 ihandler.handler.handler_1arg = handler;
1514 ihandler.extra_data = false;
1515 ihandler.data = NULL;
1516 c_register_pragma_1 (space, name, ihandler, false);
1519 /* Register a C pragma handler, using a space and a name, it also carries an
1520 extra data field which can be used by the handler. It disallows pragma
1521 expansion (if you want it, use c_register_pragma_with_expansion_and_data
1522 instead). */
1523 void
1524 c_register_pragma_with_data (const char *space, const char *name,
1525 pragma_handler_2arg handler, void * data)
1527 internal_pragma_handler ihandler;
1529 ihandler.handler.handler_2arg = handler;
1530 ihandler.extra_data = true;
1531 ihandler.data = data;
1532 c_register_pragma_1 (space, name, ihandler, false);
1535 /* Register a C pragma handler, using a space and a name. It allows pragma
1536 expansion as in the following example:
1538 #define NUMBER 10
1539 #pragma count (NUMBER)
1541 Name expansion is still disallowed. */
1542 void
1543 c_register_pragma_with_expansion (const char *space, const char *name,
1544 pragma_handler_1arg handler)
1546 internal_pragma_handler ihandler;
1548 ihandler.handler.handler_1arg = handler;
1549 ihandler.extra_data = false;
1550 ihandler.data = NULL;
1551 c_register_pragma_1 (space, name, ihandler, true);
1554 /* Register a C pragma handler, using a space and a name, it also carries an
1555 extra data field which can be used by the handler. It allows pragma
1556 expansion as in the following example:
1558 #define NUMBER 10
1559 #pragma count (NUMBER)
1561 Name expansion is still disallowed. */
1562 void
1563 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1564 pragma_handler_2arg handler,
1565 void *data)
1567 internal_pragma_handler ihandler;
1569 ihandler.handler.handler_2arg = handler;
1570 ihandler.extra_data = true;
1571 ihandler.data = data;
1572 c_register_pragma_1 (space, name, ihandler, true);
1575 void
1576 c_invoke_pragma_handler (unsigned int id)
1578 internal_pragma_handler *ihandler;
1579 pragma_handler_1arg handler_1arg;
1580 pragma_handler_2arg handler_2arg;
1582 id -= PRAGMA_FIRST_EXTERNAL;
1583 ihandler = &registered_pragmas[id];
1584 if (ihandler->extra_data)
1586 handler_2arg = ihandler->handler.handler_2arg;
1587 handler_2arg (parse_in, ihandler->data);
1589 else
1591 handler_1arg = ihandler->handler.handler_1arg;
1592 handler_1arg (parse_in);
1596 /* Set up front-end pragmas. */
1597 void
1598 init_pragma (void)
1600 if (flag_openmp)
1602 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1603 int i;
1605 for (i = 0; i < n_omp_pragmas; ++i)
1606 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1607 omp_pragmas[i].id, true, true);
1609 if (flag_openmp || flag_openmp_simd)
1611 const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1612 / sizeof (*omp_pragmas);
1613 int i;
1615 for (i = 0; i < n_omp_pragmas_simd; ++i)
1616 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1617 omp_pragmas_simd[i].id, true, true);
1620 if (!flag_preprocess_only)
1621 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1622 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1624 cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1625 false);
1626 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1627 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1628 #else
1629 c_register_pragma (0, "pack", handle_pragma_pack);
1630 #endif
1631 c_register_pragma (0, "weak", handle_pragma_weak);
1632 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1634 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1635 c_register_pragma ("GCC", "target", handle_pragma_target);
1636 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1637 c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1638 c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1639 c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1641 c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1642 handle_pragma_float_const_decimal64);
1644 c_register_pragma_with_expansion (0, "redefine_extname",
1645 handle_pragma_redefine_extname);
1647 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1649 if (compiling_upc)
1651 c_register_pragma (0, "upc", handle_pragma_upc);
1652 init_pragma_upc ();
1653 c_register_pragma (0, "pupc", handle_pragma_pupc);
1654 init_pragma_pupc ();
1657 #ifdef REGISTER_TARGET_PRAGMAS
1658 REGISTER_TARGET_PRAGMAS ();
1659 #endif
1661 /* Allow plugins to register their own pragmas. */
1662 invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1665 #include "gt-c-family-c-pragma.h"