merge with trunk @ 139506
[official-gcc.git] / gcc / c-pragma.c
blob63c0acb56494570760d9f25a932c0b091294d81b
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "cpplib.h"
29 #include "c-pragma.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "ggc.h"
33 #include "c-common.h"
34 #include "output.h"
35 #include "tm_p.h"
36 #include "vec.h"
37 #include "target.h"
38 #include "diagnostic.h"
39 #include "opts.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 align_stack GTY(())
48 int alignment;
49 tree id;
50 struct align_stack * prev;
51 } align_stack;
53 static GTY(()) struct align_stack * alignment_stack;
55 #ifdef HANDLE_PRAGMA_PACK
56 static void handle_pragma_pack (cpp_reader *);
58 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
59 /* If we have a "global" #pragma pack(<n>) in effect when the first
60 #pragma pack(push,<n>) is encountered, this stores the value of
61 maximum_field_alignment in effect. When the final pop_alignment()
62 happens, we restore the value to this, not to a value of 0 for
63 maximum_field_alignment. Value is in bits. */
64 static int default_alignment;
65 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
66 ? &default_alignment \
67 : &alignment_stack->alignment) = (ALIGN))
69 static void push_alignment (int, tree);
70 static void pop_alignment (tree);
72 /* Push an alignment value onto the stack. */
73 static void
74 push_alignment (int alignment, tree id)
76 align_stack * entry;
78 entry = GGC_NEW (align_stack);
80 entry->alignment = alignment;
81 entry->id = id;
82 entry->prev = alignment_stack;
84 /* The current value of maximum_field_alignment is not necessarily
85 0 since there may be a #pragma pack(<n>) in effect; remember it
86 so that we can restore it after the final #pragma pop(). */
87 if (alignment_stack == NULL)
88 default_alignment = maximum_field_alignment;
90 alignment_stack = entry;
92 maximum_field_alignment = alignment;
95 /* Undo a push of an alignment onto the stack. */
96 static void
97 pop_alignment (tree id)
99 align_stack * entry;
101 if (alignment_stack == NULL)
102 GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
104 /* If we got an identifier, strip away everything above the target
105 entry so that the next step will restore the state just below it. */
106 if (id)
108 for (entry = alignment_stack; entry; entry = entry->prev)
109 if (entry->id == id)
111 alignment_stack = entry;
112 break;
114 if (entry == NULL)
115 warning (OPT_Wpragmas, "\
116 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
117 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
120 entry = alignment_stack->prev;
122 maximum_field_alignment = entry ? entry->alignment : default_alignment;
124 alignment_stack = entry;
126 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
127 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
128 #define push_alignment(ID, N) \
129 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
130 #define pop_alignment(ID) \
131 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
132 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
134 /* #pragma pack ()
135 #pragma pack (N)
137 #pragma pack (push)
138 #pragma pack (push, N)
139 #pragma pack (push, ID)
140 #pragma pack (push, ID, N)
141 #pragma pack (pop)
142 #pragma pack (pop, ID) */
143 static void
144 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
146 tree x, id = 0;
147 int align = -1;
148 enum cpp_ttype token;
149 enum { set, push, pop } action;
151 if (pragma_lex (&x) != CPP_OPEN_PAREN)
152 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
154 token = pragma_lex (&x);
155 if (token == CPP_CLOSE_PAREN)
157 action = set;
158 align = initial_max_fld_align;
160 else if (token == CPP_NUMBER)
162 if (TREE_CODE (x) != INTEGER_CST)
163 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
164 align = TREE_INT_CST_LOW (x);
165 action = set;
166 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
167 GCC_BAD ("malformed %<#pragma pack%> - ignored");
169 else if (token == CPP_NAME)
171 #define GCC_BAD_ACTION do { if (action != pop) \
172 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
173 else \
174 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
175 } while (0)
177 const char *op = IDENTIFIER_POINTER (x);
178 if (!strcmp (op, "push"))
179 action = push;
180 else if (!strcmp (op, "pop"))
181 action = pop;
182 else
183 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
185 while ((token = pragma_lex (&x)) == CPP_COMMA)
187 token = pragma_lex (&x);
188 if (token == CPP_NAME && id == 0)
190 id = x;
192 else if (token == CPP_NUMBER && action == push && align == -1)
194 if (TREE_CODE (x) != INTEGER_CST)
195 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
196 align = TREE_INT_CST_LOW (x);
197 if (align == -1)
198 action = set;
200 else
201 GCC_BAD_ACTION;
204 if (token != CPP_CLOSE_PAREN)
205 GCC_BAD_ACTION;
206 #undef GCC_BAD_ACTION
208 else
209 GCC_BAD ("malformed %<#pragma pack%> - ignored");
211 if (pragma_lex (&x) != CPP_EOF)
212 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
214 if (flag_pack_struct)
215 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
217 if (action != pop)
218 switch (align)
220 case 0:
221 case 1:
222 case 2:
223 case 4:
224 case 8:
225 case 16:
226 align *= BITS_PER_UNIT;
227 break;
228 case -1:
229 if (action == push)
231 align = maximum_field_alignment;
232 break;
234 default:
235 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
238 switch (action)
240 case set: SET_GLOBAL_ALIGNMENT (align); break;
241 case push: push_alignment (align, id); break;
242 case pop: pop_alignment (id); break;
245 #endif /* HANDLE_PRAGMA_PACK */
247 struct def_pragma_macro_value GTY(())
249 struct def_pragma_macro_value *prev;
250 cpp_macro *value;
253 struct def_pragma_macro GTY(())
255 hashval_t hash;
256 const char *name;
257 struct def_pragma_macro_value value;
260 static GTY((param_is (struct def_pragma_macro))) htab_t pushed_macro_table;
262 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
263 /* Hash table control functions for pushed_macro_table. */
264 static hashval_t
265 dpm_hash (const void *p)
267 return ((const struct def_pragma_macro *)p)->hash;
270 static int
271 dpm_eq (const void *pa, const void *pb)
273 const struct def_pragma_macro *const a = (const struct def_pragma_macro *) pa,
274 *const b = (const struct def_pragma_macro *) pb;
275 return a->hash == b->hash && strcmp (a->name, b->name) == 0;
278 /* #pragma push_macro("MACRO_NAME")
279 #pragma pop_macro("MACRO_NAME") */
281 static void
282 handle_pragma_push_macro (cpp_reader *reader)
284 tree x, id = 0;
285 enum cpp_ttype token;
286 struct def_pragma_macro dummy, *c;
287 const char *macroname;
288 void **slot;
290 if (pragma_lex (&x) != CPP_OPEN_PAREN)
291 GCC_BAD ("missing %<(%> after %<#pragma push_macro%> - ignored");
293 token = pragma_lex (&id);
295 /* Silently ignore */
296 if (token == CPP_CLOSE_PAREN)
297 return;
298 if (token != CPP_STRING)
299 GCC_BAD ("invalid constant in %<#pragma push_macro%> - ignored");
301 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
302 GCC_BAD ("missing %<)%> after %<#pragma push_macro%> - ignored");
304 if (pragma_lex (&x) != CPP_EOF)
305 warning (OPT_Wpragmas, "junk at end of %<#pragma push_macro%>");
307 /* Check for empty string, and silently ignore. */
308 if (TREE_STRING_LENGTH (id) < 1)
309 return;
310 macroname = TREE_STRING_POINTER (id);
312 if (pushed_macro_table == NULL)
313 pushed_macro_table = htab_create_ggc (15, dpm_hash, dpm_eq, 0);
315 dummy.hash = htab_hash_string (macroname);
316 dummy.name = macroname;
317 slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
318 dummy.hash, INSERT);
319 c = (struct def_pragma_macro *) *slot;
320 if (c == NULL)
322 *slot = c = GGC_NEW (struct def_pragma_macro);
323 c->hash = dummy.hash;
324 c->name = ggc_alloc_string (macroname, TREE_STRING_LENGTH (id) - 1);
325 c->value.prev = NULL;
327 else
329 struct def_pragma_macro_value *v;
330 v = GGC_NEW (struct def_pragma_macro_value);
331 *v = c->value;
332 c->value.prev = v;
335 c->value.value = cpp_push_definition (reader, macroname);
338 static void
339 handle_pragma_pop_macro (cpp_reader *reader)
341 tree x, id = 0;
342 enum cpp_ttype token;
343 struct def_pragma_macro dummy, *c;
344 const char *macroname;
345 void **slot = NULL;
347 if (pragma_lex (&x) != CPP_OPEN_PAREN)
348 GCC_BAD ("missing %<(%> after %<#pragma pop_macro%> - ignored");
350 token = pragma_lex (&id);
352 /* Silently ignore */
353 if (token == CPP_CLOSE_PAREN)
354 return;
355 if (token != CPP_STRING)
356 GCC_BAD ("invalid constant in %<#pragma pop_macro%> - ignored");
358 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
359 GCC_BAD ("missing %<)%> after %<#pragma pop_macro%> - ignored");
361 if (pragma_lex (&x) != CPP_EOF)
362 warning (OPT_Wpragmas, "junk at end of %<#pragma pop_macro%>");
364 /* Check for empty string, and silently ignore. */
365 if (TREE_STRING_LENGTH (id) < 1)
366 return;
367 macroname = TREE_STRING_POINTER (id);
369 dummy.hash = htab_hash_string (macroname);
370 dummy.name = macroname;
371 if (pushed_macro_table)
372 slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
373 dummy.hash, NO_INSERT);
374 if (slot == NULL)
375 return;
376 c = (struct def_pragma_macro *) *slot;
378 cpp_pop_definition (reader, c->name, c->value.value);
380 if (c->value.prev)
381 c->value = *c->value.prev;
382 else
383 htab_clear_slot (pushed_macro_table, slot);
385 #endif /* HANDLE_PRAGMA_PUSH_POP_MACRO */
387 static GTY(()) tree pending_weaks;
389 #ifdef HANDLE_PRAGMA_WEAK
390 static void apply_pragma_weak (tree, tree);
391 static void handle_pragma_weak (cpp_reader *);
393 static void
394 apply_pragma_weak (tree decl, tree value)
396 if (value)
398 value = build_string (IDENTIFIER_LENGTH (value),
399 IDENTIFIER_POINTER (value));
400 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
401 build_tree_list (NULL, value)),
405 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
406 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
407 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
408 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
409 "results in unspecified behavior", decl);
411 declare_weak (decl);
414 void
415 maybe_apply_pragma_weak (tree decl)
417 tree *p, t, id;
419 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
421 /* No weak symbols pending, take the short-cut. */
422 if (!pending_weaks)
423 return;
424 /* If it's not visible outside this file, it doesn't matter whether
425 it's weak. */
426 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
427 return;
428 /* If it's not a function or a variable, it can't be weak.
429 FIXME: what kinds of things are visible outside this file but
430 aren't functions or variables? Should this be an assert instead? */
431 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
432 return;
434 id = DECL_ASSEMBLER_NAME (decl);
436 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
437 if (id == TREE_PURPOSE (t))
439 apply_pragma_weak (decl, TREE_VALUE (t));
440 *p = TREE_CHAIN (t);
441 break;
445 /* Process all "#pragma weak A = B" directives where we have not seen
446 a decl for A. */
447 void
448 maybe_apply_pending_pragma_weaks (void)
450 tree *p, t, alias_id, id, decl, *next;
452 for (p = &pending_weaks; (t = *p) ; p = next)
454 next = &TREE_CHAIN (t);
455 alias_id = TREE_PURPOSE (t);
456 id = TREE_VALUE (t);
458 if (TREE_VALUE (t) == NULL)
459 continue;
461 decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
463 DECL_ARTIFICIAL (decl) = 1;
464 TREE_PUBLIC (decl) = 1;
465 DECL_EXTERNAL (decl) = 1;
466 DECL_WEAK (decl) = 1;
468 assemble_alias (decl, id);
472 /* #pragma weak name [= value] */
473 static void
474 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
476 tree name, value, x, decl;
477 enum cpp_ttype t;
479 value = 0;
481 if (pragma_lex (&name) != CPP_NAME)
482 GCC_BAD ("malformed #pragma weak, ignored");
483 t = pragma_lex (&x);
484 if (t == CPP_EQ)
486 if (pragma_lex (&value) != CPP_NAME)
487 GCC_BAD ("malformed #pragma weak, ignored");
488 t = pragma_lex (&x);
490 if (t != CPP_EOF)
491 warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
493 decl = identifier_global_value (name);
494 if (decl && DECL_P (decl))
496 apply_pragma_weak (decl, value);
497 if (value)
498 assemble_alias (decl, value);
500 else
501 pending_weaks = tree_cons (name, value, pending_weaks);
503 #else
504 void
505 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
509 void
510 maybe_apply_pending_pragma_weaks (void)
513 #endif /* HANDLE_PRAGMA_WEAK */
515 /* GCC supports two #pragma directives for renaming the external
516 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
517 compatibility with the Solaris and Tru64 system headers. GCC also
518 has its own notation for this, __asm__("name") annotations.
520 Corner cases of these features and their interaction:
522 1) Both pragmas silently apply only to declarations with external
523 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
524 do not have this restriction.
526 2) In C++, both #pragmas silently apply only to extern "C" declarations.
527 Asm labels do not have this restriction.
529 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
530 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
531 new name is different, a warning issues and the name does not change.
533 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
534 *not* the DECL_ASSEMBLER_NAME.
536 5) If #pragma extern_prefix is in effect and a declaration occurs
537 with an __asm__ name, the #pragma extern_prefix is silently
538 ignored for that declaration.
540 6) If #pragma extern_prefix and #pragma redefine_extname apply to
541 the same declaration, whichever triggered first wins, and a warning
542 is issued. (We would like to have #pragma redefine_extname always
543 win, but it can appear either before or after the declaration, and
544 if it appears afterward, we have no way of knowing whether a modified
545 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
547 static GTY(()) tree pending_redefine_extname;
549 static void handle_pragma_redefine_extname (cpp_reader *);
551 /* #pragma redefine_extname oldname newname */
552 static void
553 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
555 tree oldname, newname, decl, x;
556 enum cpp_ttype t;
558 if (pragma_lex (&oldname) != CPP_NAME)
559 GCC_BAD ("malformed #pragma redefine_extname, ignored");
560 if (pragma_lex (&newname) != CPP_NAME)
561 GCC_BAD ("malformed #pragma redefine_extname, ignored");
562 t = pragma_lex (&x);
563 if (t != CPP_EOF)
564 warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
566 if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
568 if (warn_unknown_pragmas > in_system_header)
569 warning (OPT_Wunknown_pragmas,
570 "#pragma redefine_extname not supported on this target");
571 return;
574 decl = identifier_global_value (oldname);
575 if (decl
576 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
577 && (TREE_CODE (decl) == FUNCTION_DECL
578 || TREE_CODE (decl) == VAR_DECL)
579 && has_c_linkage (decl))
581 if (DECL_ASSEMBLER_NAME_SET_P (decl))
583 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
584 name = targetm.strip_name_encoding (name);
586 if (strcmp (name, IDENTIFIER_POINTER (newname)))
587 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
588 "conflict with previous rename");
590 else
591 change_decl_assembler_name (decl, newname);
593 else
594 /* We have to add this to the rename list even if there's already
595 a global value that doesn't meet the above criteria, because in
596 C++ "struct foo {...};" puts "foo" in the current namespace but
597 does *not* conflict with a subsequent declaration of a function
598 or variable foo. See g++.dg/other/pragma-re-2.C. */
599 add_to_renaming_pragma_list (oldname, newname);
602 /* This is called from here and from ia64.c. */
603 void
604 add_to_renaming_pragma_list (tree oldname, tree newname)
606 tree previous = purpose_member (oldname, pending_redefine_extname);
607 if (previous)
609 if (TREE_VALUE (previous) != newname)
610 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
611 "conflict with previous #pragma redefine_extname");
612 return;
615 pending_redefine_extname
616 = tree_cons (oldname, newname, pending_redefine_extname);
619 static GTY(()) tree pragma_extern_prefix;
621 /* #pragma extern_prefix "prefix" */
622 static void
623 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
625 tree prefix, x;
626 enum cpp_ttype t;
628 if (pragma_lex (&prefix) != CPP_STRING)
629 GCC_BAD ("malformed #pragma extern_prefix, ignored");
630 t = pragma_lex (&x);
631 if (t != CPP_EOF)
632 warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
634 if (targetm.handle_pragma_extern_prefix)
635 /* Note that the length includes the null terminator. */
636 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
637 else if (warn_unknown_pragmas > in_system_header)
638 warning (OPT_Wunknown_pragmas,
639 "#pragma extern_prefix not supported on this target");
642 /* Hook from the front ends to apply the results of one of the preceding
643 pragmas that rename variables. */
645 tree
646 maybe_apply_renaming_pragma (tree decl, tree asmname)
648 tree *p, t;
650 /* The renaming pragmas are only applied to declarations with
651 external linkage. */
652 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
653 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
654 || !has_c_linkage (decl))
655 return asmname;
657 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
658 but we may warn about a rename that conflicts. */
659 if (DECL_ASSEMBLER_NAME_SET_P (decl))
661 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
662 oldname = targetm.strip_name_encoding (oldname);
664 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
665 warning (OPT_Wpragmas, "asm declaration ignored due to "
666 "conflict with previous rename");
668 /* Take any pending redefine_extname off the list. */
669 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
670 if (DECL_NAME (decl) == TREE_PURPOSE (t))
672 /* Only warn if there is a conflict. */
673 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
674 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
675 "conflict with previous rename");
677 *p = TREE_CHAIN (t);
678 break;
680 return 0;
683 /* Find out if we have a pending #pragma redefine_extname. */
684 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
685 if (DECL_NAME (decl) == TREE_PURPOSE (t))
687 tree newname = TREE_VALUE (t);
688 *p = TREE_CHAIN (t);
690 /* If we already have an asmname, #pragma redefine_extname is
691 ignored (with a warning if it conflicts). */
692 if (asmname)
694 if (strcmp (TREE_STRING_POINTER (asmname),
695 IDENTIFIER_POINTER (newname)) != 0)
696 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
697 "conflict with __asm__ declaration");
698 return asmname;
701 /* Otherwise we use what we've got; #pragma extern_prefix is
702 silently ignored. */
703 return build_string (IDENTIFIER_LENGTH (newname),
704 IDENTIFIER_POINTER (newname));
707 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
708 if (asmname)
709 return asmname;
711 /* If #pragma extern_prefix is in effect, apply it. */
712 if (pragma_extern_prefix)
714 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
715 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
717 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
718 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
720 char *newname = (char *) alloca (plen + ilen + 1);
722 memcpy (newname, prefix, plen);
723 memcpy (newname + plen, id, ilen + 1);
725 return build_string (plen + ilen, newname);
728 /* Nada. */
729 return 0;
733 #ifdef HANDLE_PRAGMA_VISIBILITY
734 static void handle_pragma_visibility (cpp_reader *);
736 typedef enum symbol_visibility visibility;
737 DEF_VEC_I (visibility);
738 DEF_VEC_ALLOC_I (visibility, heap);
739 static VEC (visibility, heap) *visstack;
741 /* Push the visibility indicated by STR onto the top of the #pragma
742 visibility stack. */
744 void
745 push_visibility (const char *str)
747 VEC_safe_push (visibility, heap, visstack,
748 default_visibility);
749 if (!strcmp (str, "default"))
750 default_visibility = VISIBILITY_DEFAULT;
751 else if (!strcmp (str, "internal"))
752 default_visibility = VISIBILITY_INTERNAL;
753 else if (!strcmp (str, "hidden"))
754 default_visibility = VISIBILITY_HIDDEN;
755 else if (!strcmp (str, "protected"))
756 default_visibility = VISIBILITY_PROTECTED;
757 else
758 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
759 visibility_options.inpragma = 1;
762 /* Pop a level of the #pragma visibility stack. */
764 void
765 pop_visibility (void)
767 default_visibility = VEC_pop (visibility, visstack);
768 visibility_options.inpragma
769 = VEC_length (visibility, visstack) != 0;
772 /* Sets the default visibility for symbols to something other than that
773 specified on the command line. */
775 static void
776 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
778 /* Form is #pragma GCC visibility push(hidden)|pop */
779 tree x;
780 enum cpp_ttype token;
781 enum { bad, push, pop } action = bad;
783 token = pragma_lex (&x);
784 if (token == CPP_NAME)
786 const char *op = IDENTIFIER_POINTER (x);
787 if (!strcmp (op, "push"))
788 action = push;
789 else if (!strcmp (op, "pop"))
790 action = pop;
792 if (bad == action)
793 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
794 else
796 if (pop == action)
798 if (!VEC_length (visibility, visstack))
799 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
800 else
801 pop_visibility ();
803 else
805 if (pragma_lex (&x) != CPP_OPEN_PAREN)
806 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
807 token = pragma_lex (&x);
808 if (token != CPP_NAME)
809 GCC_BAD ("malformed #pragma GCC visibility push");
810 else
811 push_visibility (IDENTIFIER_POINTER (x));
812 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
813 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
816 if (pragma_lex (&x) != CPP_EOF)
817 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
820 #endif
822 static void
823 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
825 const char *kind_string, *option_string;
826 unsigned int option_index;
827 enum cpp_ttype token;
828 diagnostic_t kind;
829 tree x;
831 if (cfun)
833 error ("#pragma GCC diagnostic not allowed inside functions");
834 return;
837 token = pragma_lex (&x);
838 if (token != CPP_NAME)
839 GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
840 kind_string = IDENTIFIER_POINTER (x);
841 if (strcmp (kind_string, "error") == 0)
842 kind = DK_ERROR;
843 else if (strcmp (kind_string, "warning") == 0)
844 kind = DK_WARNING;
845 else if (strcmp (kind_string, "ignored") == 0)
846 kind = DK_IGNORED;
847 else
848 GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
850 token = pragma_lex (&x);
851 if (token != CPP_STRING)
852 GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
853 option_string = TREE_STRING_POINTER (x);
854 for (option_index = 0; option_index < cl_options_count; option_index++)
855 if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
857 /* This overrides -Werror, for example. */
858 diagnostic_classify_diagnostic (global_dc, option_index, kind);
859 /* This makes sure the option is enabled, like -Wfoo would do. */
860 if (cl_options[option_index].var_type == CLVC_BOOLEAN
861 && cl_options[option_index].flag_var
862 && kind != DK_IGNORED)
863 *(int *) cl_options[option_index].flag_var = 1;
864 return;
866 GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
869 /* Stack of the #pragma GCC options created with #pragma GCC option push. */
870 static GTY(()) VEC(tree,gc) *option_stack;
872 /* Parse #pragma GCC option (xxx) to set target specific options. */
873 static void
874 handle_pragma_option(cpp_reader *ARG_UNUSED(dummy))
876 enum cpp_ttype token;
877 const char *name;
878 tree x;
879 bool close_paren_needed_p = false;
881 if (cfun)
883 error ("#pragma GCC option is not allowed inside functions");
884 return;
887 if (!targetm.target_option.pragma_parse)
889 error ("#pragma GCC option is not supported for this system");
890 return;
893 token = pragma_lex (&x);
894 if (token == CPP_OPEN_PAREN)
896 close_paren_needed_p = true;
897 token = pragma_lex (&x);
900 if (token == CPP_NAME)
902 bool call_pragma_parse_p = false;
903 bool ok_p;
905 name = IDENTIFIER_POINTER (x);
906 if (strcmp (name, "reset") == 0)
908 current_option_pragma = NULL_TREE;
909 call_pragma_parse_p = true;
912 else if (strcmp (name, "push") == 0)
913 VEC_safe_push (tree, gc, option_stack,
914 copy_list (current_option_pragma));
916 else if (strcmp (name, "pop") == 0)
918 int len = VEC_length (tree, option_stack);
919 if (len == 0)
921 GCC_BAD ("%<#pragma GCC option pop%> without a %<#pragma GCC "
922 "option push%>");
923 return;
925 else
927 VEC_truncate (tree, option_stack, len-1);
928 current_option_pragma = ((len > 1)
929 ? VEC_last (tree, option_stack)
930 : NULL_TREE);
932 call_pragma_parse_p = true;
936 else
938 GCC_BAD ("%<#pragma GCC option%> is not a string or "
939 "push/pop/reset");
940 return;
943 token = pragma_lex (&x);
944 if (close_paren_needed_p)
946 if (token == CPP_CLOSE_PAREN)
947 token = pragma_lex (&x);
948 else
949 GCC_BAD ("%<#pragma GCC option ([push|pop|reset])%> does not "
950 "have a final %<)%>.");
953 if (token != CPP_EOF)
955 GCC_BAD ("%<#pragma GCC option [push|pop|reset]%> is badly "
956 "formed");
957 return;
960 /* See if we need to call the pragma_parse hook. This must occur at the
961 end after processing all of the tokens, or we may get spurious errors
962 when we define or undef macros. */
963 ok_p = targetm.target_option.pragma_parse (current_option_pragma);
964 gcc_assert (ok_p);
967 else if (token != CPP_STRING)
969 GCC_BAD ("%<#pragma GCC option%> is not a string or push/pop/reset");
970 return;
973 /* Strings are user options. */
974 else
976 tree args = NULL_TREE;
980 /* Build up the strings now as a tree linked list. Skip empty
981 strings. */
982 if (TREE_STRING_LENGTH (x) > 0)
983 args = tree_cons (NULL_TREE, x, args);
985 token = pragma_lex (&x);
986 while (token == CPP_COMMA)
987 token = pragma_lex (&x);
989 while (token == CPP_STRING);
991 if (close_paren_needed_p)
993 if (token == CPP_CLOSE_PAREN)
994 token = pragma_lex (&x);
995 else
996 GCC_BAD ("%<#pragma GCC option (string [,string]...)%> does "
997 "not have a final %<)%>.");
1000 if (token != CPP_EOF)
1002 error ("#pragma GCC option string... is badly formed");
1003 return;
1006 /* put arguments in the order the user typed them. */
1007 args = nreverse (args);
1009 if (targetm.target_option.pragma_parse (args))
1010 current_option_pragma = args;
1014 /* Stack of the #pragma GCC optimize options created with #pragma GCC optimize
1015 push. */
1016 static GTY(()) VEC(tree,gc) *optimize_stack;
1018 /* Handle #pragma GCC optimize to set optimization options. */
1019 static void
1020 handle_pragma_optimize(cpp_reader *ARG_UNUSED(dummy))
1022 enum cpp_ttype token;
1023 const char *name;
1024 tree x;
1025 bool close_paren_needed_p = false;
1026 tree optimization_previous_node = optimization_current_node;
1028 if (cfun)
1030 error ("#pragma GCC optimize is not allowed inside functions");
1031 return;
1034 token = pragma_lex (&x);
1035 if (token == CPP_OPEN_PAREN)
1037 close_paren_needed_p = true;
1038 token = pragma_lex (&x);
1041 if (token == CPP_NAME)
1043 bool call_opt_p = false;
1045 name = IDENTIFIER_POINTER (x);
1046 if (strcmp (name, "reset") == 0)
1048 struct cl_optimization *def
1049 = TREE_OPTIMIZATION (optimization_default_node);
1050 current_optimize_pragma = NULL_TREE;
1051 optimization_current_node = optimization_default_node;
1052 cl_optimization_restore (def);
1053 call_opt_p = true;
1056 else if (strcmp (name, "push") == 0)
1057 VEC_safe_push (tree, gc, optimize_stack, current_optimize_pragma);
1059 else if (strcmp (name, "pop") == 0)
1061 int len = VEC_length (tree, optimize_stack);
1062 if (len == 0)
1064 GCC_BAD ("%<#pragma GCC optimize pop%> without a %<#pragma "
1065 "GCC optimize push%>");
1066 return;
1068 else
1070 VEC_truncate (tree, optimize_stack, len-1);
1071 current_optimize_pragma
1072 = ((len > 1)
1073 ? VEC_last (tree, optimize_stack)
1074 : NULL_TREE);
1076 call_opt_p = true;
1077 if (current_optimize_pragma)
1079 bool ok_p
1080 = parse_optimize_options (current_optimize_pragma, false);
1081 gcc_assert (ok_p); /* should be parsed previously. */
1082 optimization_current_node = build_optimization_node ();
1084 else
1086 struct cl_optimization *opt
1087 = TREE_OPTIMIZATION (optimization_default_node);
1088 optimization_current_node = optimization_default_node;
1089 cl_optimization_restore (opt);
1094 else
1096 GCC_BAD ("%<#pragma GCC optimize%> is not a string or "
1097 "push/pop/reset");
1098 return;
1101 token = pragma_lex (&x);
1102 if (close_paren_needed_p)
1104 if (token == CPP_CLOSE_PAREN)
1105 token = pragma_lex (&x);
1106 else
1107 GCC_BAD ("%<#pragma GCC optimize ([push|pop|reset])%> does not "
1108 "have a final %<)%>.");
1111 if (token != CPP_EOF)
1113 GCC_BAD ("%<#pragma GCC optimize [push|pop|reset]%> is badly "
1114 "formed");
1115 return;
1118 if (call_opt_p &&
1119 (optimization_previous_node != optimization_current_node))
1120 c_cpp_builtins_optimize_pragma (parse_in,
1121 optimization_previous_node,
1122 optimization_current_node);
1126 else if (token != CPP_STRING && token != CPP_NUMBER)
1128 GCC_BAD ("%<#pragma GCC optimize%> is not a string, number, or "
1129 "push/pop/reset");
1130 return;
1133 /* Strings/numbers are user options. */
1134 else
1136 tree args = NULL_TREE;
1140 /* Build up the numbers/strings now as a list. */
1141 if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
1142 args = tree_cons (NULL_TREE, x, args);
1144 token = pragma_lex (&x);
1145 while (token == CPP_COMMA)
1146 token = pragma_lex (&x);
1148 while (token == CPP_STRING || token == CPP_NUMBER);
1150 if (close_paren_needed_p)
1152 if (token == CPP_CLOSE_PAREN)
1153 token = pragma_lex (&x);
1154 else
1155 GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
1156 "not have a final %<)%>.");
1159 if (token != CPP_EOF)
1161 error ("#pragma GCC optimize string... is badly formed");
1162 return;
1165 /* put arguments in the order the user typed them. */
1166 args = nreverse (args);
1168 parse_optimize_options (args, false);
1169 optimization_current_node = build_optimization_node ();
1170 c_cpp_builtins_optimize_pragma (parse_in,
1171 optimization_previous_node,
1172 optimization_current_node);
1176 /* Print a plain user-specified message. */
1178 static void
1179 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1181 enum cpp_ttype token;
1182 tree x, message = 0;
1184 token = pragma_lex (&x);
1185 if (token == CPP_OPEN_PAREN)
1187 token = pragma_lex (&x);
1188 if (token == CPP_STRING)
1189 message = x;
1190 else
1191 GCC_BAD ("expected a string after %<#pragma message%>");
1192 if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1193 GCC_BAD ("malformed %<#pragma message%>, ignored");
1195 else if (token == CPP_STRING)
1196 message = x;
1197 else
1198 GCC_BAD ("expected a string after %<#pragma message%>");
1200 gcc_assert (message);
1202 if (pragma_lex (&x) != CPP_EOF)
1203 warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1205 if (TREE_STRING_LENGTH (message) > 1)
1206 inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1209 /* A vector of registered pragma callbacks. */
1211 DEF_VEC_O (pragma_handler);
1212 DEF_VEC_ALLOC_O (pragma_handler, heap);
1214 static VEC(pragma_handler, heap) *registered_pragmas;
1216 typedef struct
1218 const char *space;
1219 const char *name;
1220 } pragma_ns_name;
1222 DEF_VEC_O (pragma_ns_name);
1223 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
1225 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
1227 struct omp_pragma_def { const char *name; unsigned int id; };
1228 static const struct omp_pragma_def omp_pragmas[] = {
1229 { "atomic", PRAGMA_OMP_ATOMIC },
1230 { "barrier", PRAGMA_OMP_BARRIER },
1231 { "critical", PRAGMA_OMP_CRITICAL },
1232 { "flush", PRAGMA_OMP_FLUSH },
1233 { "for", PRAGMA_OMP_FOR },
1234 { "master", PRAGMA_OMP_MASTER },
1235 { "ordered", PRAGMA_OMP_ORDERED },
1236 { "parallel", PRAGMA_OMP_PARALLEL },
1237 { "section", PRAGMA_OMP_SECTION },
1238 { "sections", PRAGMA_OMP_SECTIONS },
1239 { "single", PRAGMA_OMP_SINGLE },
1240 { "task", PRAGMA_OMP_TASK },
1241 { "taskwait", PRAGMA_OMP_TASKWAIT },
1242 { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1245 void
1246 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1248 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1249 int i;
1251 for (i = 0; i < n_omp_pragmas; ++i)
1252 if (omp_pragmas[i].id == id)
1254 *space = "omp";
1255 *name = omp_pragmas[i].name;
1256 return;
1259 if (id >= PRAGMA_FIRST_EXTERNAL
1260 && (id < PRAGMA_FIRST_EXTERNAL
1261 + VEC_length (pragma_ns_name, registered_pp_pragmas)))
1263 *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
1264 id - PRAGMA_FIRST_EXTERNAL)->space;
1265 *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
1266 id - PRAGMA_FIRST_EXTERNAL)->name;
1267 return;
1270 gcc_unreachable ();
1273 /* Front-end wrappers for pragma registration to avoid dragging
1274 cpplib.h in almost everywhere. */
1276 static void
1277 c_register_pragma_1 (const char *space, const char *name,
1278 pragma_handler handler, bool allow_expansion)
1280 unsigned id;
1282 if (flag_preprocess_only)
1284 pragma_ns_name ns_name;
1286 if (!allow_expansion)
1287 return;
1289 ns_name.space = space;
1290 ns_name.name = name;
1291 VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
1292 id = VEC_length (pragma_ns_name, registered_pp_pragmas);
1293 id += PRAGMA_FIRST_EXTERNAL - 1;
1295 else
1297 VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
1298 id = VEC_length (pragma_handler, registered_pragmas);
1299 id += PRAGMA_FIRST_EXTERNAL - 1;
1301 /* The C++ front end allocates 6 bits in cp_token; the C front end
1302 allocates 7 bits in c_token. At present this is sufficient. */
1303 gcc_assert (id < 64);
1306 cpp_register_deferred_pragma (parse_in, space, name, id,
1307 allow_expansion, false);
1310 void
1311 c_register_pragma (const char *space, const char *name, pragma_handler handler)
1313 c_register_pragma_1 (space, name, handler, false);
1316 void
1317 c_register_pragma_with_expansion (const char *space, const char *name,
1318 pragma_handler handler)
1320 c_register_pragma_1 (space, name, handler, true);
1323 void
1324 c_invoke_pragma_handler (unsigned int id)
1326 pragma_handler handler;
1328 id -= PRAGMA_FIRST_EXTERNAL;
1329 handler = *VEC_index (pragma_handler, registered_pragmas, id);
1331 handler (parse_in);
1334 /* Set up front-end pragmas. */
1335 void
1336 init_pragma (void)
1338 if (flag_openmp)
1340 const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1341 int i;
1343 for (i = 0; i < n_omp_pragmas; ++i)
1344 cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1345 omp_pragmas[i].id, true, true);
1348 if (!flag_preprocess_only)
1349 cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1350 PRAGMA_GCC_PCH_PREPROCESS, false, false);
1352 #ifdef HANDLE_PRAGMA_PACK
1353 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1354 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1355 #else
1356 c_register_pragma (0, "pack", handle_pragma_pack);
1357 #endif
1358 #endif
1359 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
1360 c_register_pragma (0 ,"push_macro", handle_pragma_push_macro);
1361 c_register_pragma (0 ,"pop_macro", handle_pragma_pop_macro);
1362 #endif
1363 #ifdef HANDLE_PRAGMA_WEAK
1364 c_register_pragma (0, "weak", handle_pragma_weak);
1365 #endif
1366 #ifdef HANDLE_PRAGMA_VISIBILITY
1367 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1368 #endif
1370 c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1371 c_register_pragma ("GCC", "option", handle_pragma_option);
1372 c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1374 c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1375 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1377 c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1379 #ifdef REGISTER_TARGET_PRAGMAS
1380 REGISTER_TARGET_PRAGMAS ();
1381 #endif
1384 #include "gt-c-pragma.h"