configure.in (CC_FOR_TARGET, [...]): Find them with NCN_STRICT_CHECK_TARGET_TOOL...
[official-gcc.git] / gcc / c-pragma.c
blob1407a114c52513029d7e77bafe7056294c188705
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 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "function.h"
29 #include "cpplib.h"
30 #include "c-pragma.h"
31 #include "flags.h"
32 #include "toplev.h"
33 #include "ggc.h"
34 #include "c-common.h"
35 #include "output.h"
36 #include "tm_p.h"
37 #include "vec.h"
38 #include "target.h"
40 #define GCC_BAD(gmsgid) \
41 do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
42 #define GCC_BAD2(gmsgid, arg) \
43 do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
45 typedef struct align_stack GTY(())
47 int alignment;
48 tree id;
49 struct align_stack * prev;
50 } align_stack;
52 static GTY(()) struct align_stack * alignment_stack;
54 #ifdef HANDLE_PRAGMA_PACK
55 static void handle_pragma_pack (cpp_reader *);
57 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
58 /* If we have a "global" #pragma pack(<n>) in effect when the first
59 #pragma pack(push,<n>) is encountered, this stores the value of
60 maximum_field_alignment in effect. When the final pop_alignment()
61 happens, we restore the value to this, not to a value of 0 for
62 maximum_field_alignment. Value is in bits. */
63 static int default_alignment;
64 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
65 ? &default_alignment \
66 : &alignment_stack->alignment) = (ALIGN))
68 static void push_alignment (int, tree);
69 static void pop_alignment (tree);
71 /* Push an alignment value onto the stack. */
72 static void
73 push_alignment (int alignment, tree id)
75 align_stack * entry;
77 entry = ggc_alloc (sizeof (* entry));
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, %s) encountered without matching #pragma pack(push, %s)"
116 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
119 entry = alignment_stack->prev;
121 maximum_field_alignment = entry ? entry->alignment : default_alignment;
123 alignment_stack = entry;
125 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
126 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
127 #define push_alignment(ID, N) \
128 GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
129 #define pop_alignment(ID) \
130 GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
131 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
133 /* #pragma pack ()
134 #pragma pack (N)
136 #pragma pack (push)
137 #pragma pack (push, N)
138 #pragma pack (push, ID)
139 #pragma pack (push, ID, N)
140 #pragma pack (pop)
141 #pragma pack (pop, ID) */
142 static void
143 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
145 tree x, id = 0;
146 int align = -1;
147 enum cpp_ttype token;
148 enum { set, push, pop } action;
150 if (c_lex (&x) != CPP_OPEN_PAREN)
151 GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
153 token = c_lex (&x);
154 if (token == CPP_CLOSE_PAREN)
156 action = set;
157 align = initial_max_fld_align;
159 else if (token == CPP_NUMBER)
161 align = TREE_INT_CST_LOW (x);
162 action = set;
163 if (c_lex (&x) != CPP_CLOSE_PAREN)
164 GCC_BAD ("malformed %<#pragma pack%> - ignored");
166 else if (token == CPP_NAME)
168 #define GCC_BAD_ACTION do { if (action != pop) \
169 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
170 else \
171 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
172 } while (0)
174 const char *op = IDENTIFIER_POINTER (x);
175 if (!strcmp (op, "push"))
176 action = push;
177 else if (!strcmp (op, "pop"))
178 action = pop;
179 else
180 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
182 while ((token = c_lex (&x)) == CPP_COMMA)
184 token = c_lex (&x);
185 if (token == CPP_NAME && id == 0)
187 id = x;
189 else if (token == CPP_NUMBER && action == push && align == -1)
191 align = TREE_INT_CST_LOW (x);
192 if (align == -1)
193 action = set;
195 else
196 GCC_BAD_ACTION;
199 if (token != CPP_CLOSE_PAREN)
200 GCC_BAD_ACTION;
201 #undef GCC_BAD_ACTION
203 else
204 GCC_BAD ("malformed %<#pragma pack%> - ignored");
206 if (c_lex (&x) != CPP_EOF)
207 warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
209 if (flag_pack_struct)
210 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
212 if (action != pop)
213 switch (align)
215 case 0:
216 case 1:
217 case 2:
218 case 4:
219 case 8:
220 case 16:
221 align *= BITS_PER_UNIT;
222 break;
223 case -1:
224 if (action == push)
226 align = maximum_field_alignment;
227 break;
229 default:
230 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
233 switch (action)
235 case set: SET_GLOBAL_ALIGNMENT (align); break;
236 case push: push_alignment (align, id); break;
237 case pop: pop_alignment (id); break;
240 #endif /* HANDLE_PRAGMA_PACK */
242 static GTY(()) tree pending_weaks;
244 #ifdef HANDLE_PRAGMA_WEAK
245 static void apply_pragma_weak (tree, tree);
246 static void handle_pragma_weak (cpp_reader *);
248 static void
249 apply_pragma_weak (tree decl, tree value)
251 if (value)
253 value = build_string (IDENTIFIER_LENGTH (value),
254 IDENTIFIER_POINTER (value));
255 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
256 build_tree_list (NULL, value)),
260 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
261 && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma. */
262 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
263 warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
264 "results in unspecified behavior", decl);
266 declare_weak (decl);
269 void
270 maybe_apply_pragma_weak (tree decl)
272 tree *p, t, id;
274 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
276 /* No weak symbols pending, take the short-cut. */
277 if (!pending_weaks)
278 return;
279 /* If it's not visible outside this file, it doesn't matter whether
280 it's weak. */
281 if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
282 return;
283 /* If it's not a function or a variable, it can't be weak.
284 FIXME: what kinds of things are visible outside this file but
285 aren't functions or variables? Should this be an assert instead? */
286 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
287 return;
289 id = DECL_ASSEMBLER_NAME (decl);
291 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
292 if (id == TREE_PURPOSE (t))
294 apply_pragma_weak (decl, TREE_VALUE (t));
295 *p = TREE_CHAIN (t);
296 break;
300 /* Process all "#pragma weak A = B" directives where we have not seen
301 a decl for A. */
302 void
303 maybe_apply_pending_pragma_weaks (void)
305 tree *p, t, alias_id, id, decl, *next;
307 for (p = &pending_weaks; (t = *p) ; p = next)
309 next = &TREE_CHAIN (t);
310 alias_id = TREE_PURPOSE (t);
311 id = TREE_VALUE (t);
313 if (TREE_VALUE (t) == NULL)
314 continue;
316 decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
318 DECL_ARTIFICIAL (decl) = 1;
319 TREE_PUBLIC (decl) = 1;
320 DECL_EXTERNAL (decl) = 1;
321 DECL_WEAK (decl) = 1;
323 assemble_alias (decl, id);
327 /* #pragma weak name [= value] */
328 static void
329 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
331 tree name, value, x, decl;
332 enum cpp_ttype t;
334 value = 0;
336 if (c_lex (&name) != CPP_NAME)
337 GCC_BAD ("malformed #pragma weak, ignored");
338 t = c_lex (&x);
339 if (t == CPP_EQ)
341 if (c_lex (&value) != CPP_NAME)
342 GCC_BAD ("malformed #pragma weak, ignored");
343 t = c_lex (&x);
345 if (t != CPP_EOF)
346 warning (OPT_Wpragmas, "junk at end of #pragma weak");
348 decl = identifier_global_value (name);
349 if (decl && DECL_P (decl))
351 apply_pragma_weak (decl, value);
352 if (value)
353 assemble_alias (decl, value);
355 else
356 pending_weaks = tree_cons (name, value, pending_weaks);
358 #else
359 void
360 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
364 void
365 maybe_apply_pending_pragma_weaks (void)
368 #endif /* HANDLE_PRAGMA_WEAK */
370 /* GCC supports two #pragma directives for renaming the external
371 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
372 compatibility with the Solaris and Tru64 system headers. GCC also
373 has its own notation for this, __asm__("name") annotations.
375 Corner cases of these features and their interaction:
377 1) Both pragmas silently apply only to declarations with external
378 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
379 do not have this restriction.
381 2) In C++, both #pragmas silently apply only to extern "C" declarations.
382 Asm labels do not have this restriction.
384 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
385 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
386 new name is different, a warning issues and the name does not change.
388 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
389 *not* the DECL_ASSEMBLER_NAME.
391 5) If #pragma extern_prefix is in effect and a declaration occurs
392 with an __asm__ name, the #pragma extern_prefix is silently
393 ignored for that declaration.
395 6) If #pragma extern_prefix and #pragma redefine_extname apply to
396 the same declaration, whichever triggered first wins, and a warning
397 is issued. (We would like to have #pragma redefine_extname always
398 win, but it can appear either before or after the declaration, and
399 if it appears afterward, we have no way of knowing whether a modified
400 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
402 static GTY(()) tree pending_redefine_extname;
404 static void handle_pragma_redefine_extname (cpp_reader *);
406 /* #pragma redefine_extname oldname newname */
407 static void
408 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
410 tree oldname, newname, decl, x;
411 enum cpp_ttype t;
413 if (c_lex (&oldname) != CPP_NAME)
414 GCC_BAD ("malformed #pragma redefine_extname, ignored");
415 if (c_lex (&newname) != CPP_NAME)
416 GCC_BAD ("malformed #pragma redefine_extname, ignored");
417 t = c_lex (&x);
418 if (t != CPP_EOF)
419 warning (OPT_Wpragmas, "junk at end of #pragma redefine_extname");
421 if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
423 if (warn_unknown_pragmas > in_system_header)
424 warning (OPT_Wunknown_pragmas,
425 "#pragma redefine_extname not supported on this target");
426 return;
429 decl = identifier_global_value (oldname);
430 if (decl
431 && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
432 && (TREE_CODE (decl) == FUNCTION_DECL
433 || TREE_CODE (decl) == VAR_DECL)
434 && has_c_linkage (decl))
436 if (DECL_ASSEMBLER_NAME_SET_P (decl))
438 const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
439 name = targetm.strip_name_encoding (name);
441 if (strcmp (name, IDENTIFIER_POINTER (newname)))
442 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
443 "conflict with previous rename");
445 else
446 change_decl_assembler_name (decl, newname);
448 else
449 /* We have to add this to the rename list even if there's already
450 a global value that doesn't meet the above criteria, because in
451 C++ "struct foo {...};" puts "foo" in the current namespace but
452 does *not* conflict with a subsequent declaration of a function
453 or variable foo. See g++.dg/other/pragma-re-2.C. */
454 add_to_renaming_pragma_list (oldname, newname);
457 /* This is called from here and from ia64.c. */
458 void
459 add_to_renaming_pragma_list (tree oldname, tree newname)
461 tree previous = purpose_member (oldname, pending_redefine_extname);
462 if (previous)
464 if (TREE_VALUE (previous) != newname)
465 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
466 "conflict with previous #pragma redefine_extname");
467 return;
470 pending_redefine_extname
471 = tree_cons (oldname, newname, pending_redefine_extname);
474 static GTY(()) tree pragma_extern_prefix;
476 /* #pragma extern_prefix "prefix" */
477 static void
478 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
480 tree prefix, x;
481 enum cpp_ttype t;
483 if (c_lex (&prefix) != CPP_STRING)
484 GCC_BAD ("malformed #pragma extern_prefix, ignored");
485 t = c_lex (&x);
486 if (t != CPP_EOF)
487 warning (OPT_Wpragmas, "junk at end of #pragma extern_prefix");
489 if (targetm.handle_pragma_extern_prefix)
490 /* Note that the length includes the null terminator. */
491 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
492 else if (warn_unknown_pragmas > in_system_header)
493 warning (OPT_Wunknown_pragmas,
494 "#pragma extern_prefix not supported on this target");
497 /* Hook from the front ends to apply the results of one of the preceding
498 pragmas that rename variables. */
500 tree
501 maybe_apply_renaming_pragma (tree decl, tree asmname)
503 tree *p, t;
505 /* The renaming pragmas are only applied to declarations with
506 external linkage. */
507 if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
508 || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
509 || !has_c_linkage (decl))
510 return asmname;
512 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
513 but we may warn about a rename that conflicts. */
514 if (DECL_ASSEMBLER_NAME_SET_P (decl))
516 const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
517 oldname = targetm.strip_name_encoding (oldname);
519 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
520 warning (OPT_Wpragmas, "asm declaration ignored due to "
521 "conflict with previous rename");
523 /* Take any pending redefine_extname off the list. */
524 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
525 if (DECL_NAME (decl) == TREE_PURPOSE (t))
527 /* Only warn if there is a conflict. */
528 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
529 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
530 "conflict with previous rename");
532 *p = TREE_CHAIN (t);
533 break;
535 return 0;
538 /* Find out if we have a pending #pragma redefine_extname. */
539 for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
540 if (DECL_NAME (decl) == TREE_PURPOSE (t))
542 tree newname = TREE_VALUE (t);
543 *p = TREE_CHAIN (t);
545 /* If we already have an asmname, #pragma redefine_extname is
546 ignored (with a warning if it conflicts). */
547 if (asmname)
549 if (strcmp (TREE_STRING_POINTER (asmname),
550 IDENTIFIER_POINTER (newname)) != 0)
551 warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
552 "conflict with __asm__ declaration");
553 return asmname;
556 /* Otherwise we use what we've got; #pragma extern_prefix is
557 silently ignored. */
558 return build_string (IDENTIFIER_LENGTH (newname),
559 IDENTIFIER_POINTER (newname));
562 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
563 if (asmname)
564 return asmname;
566 /* If #pragma extern_prefix is in effect, apply it. */
567 if (pragma_extern_prefix)
569 const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
570 size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
572 const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
573 size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
575 char *newname = (char *) alloca (plen + ilen + 1);
577 memcpy (newname, prefix, plen);
578 memcpy (newname + plen, id, ilen + 1);
580 return build_string (plen + ilen, newname);
583 /* Nada. */
584 return 0;
588 #ifdef HANDLE_PRAGMA_VISIBILITY
589 static void handle_pragma_visibility (cpp_reader *);
591 typedef enum symbol_visibility visibility;
592 DEF_VEC_I (visibility);
593 DEF_VEC_ALLOC_I (visibility, heap);
595 /* Sets the default visibility for symbols to something other than that
596 specified on the command line. */
597 static void
598 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
600 /* Form is #pragma GCC visibility push(hidden)|pop */
601 tree x;
602 enum cpp_ttype token;
603 enum { bad, push, pop } action = bad;
604 static VEC (visibility, heap) *visstack;
606 token = c_lex (&x);
607 if (token == CPP_NAME)
609 const char *op = IDENTIFIER_POINTER (x);
610 if (!strcmp (op, "push"))
611 action = push;
612 else if (!strcmp (op, "pop"))
613 action = pop;
615 if (bad == action)
616 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
617 else
619 if (pop == action)
621 if (!VEC_length (visibility, visstack))
623 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
625 else
627 default_visibility = VEC_pop (visibility, visstack);
628 visibility_options.inpragma
629 = VEC_length (visibility, visstack) != 0;
632 else
634 if (c_lex (&x) != CPP_OPEN_PAREN)
635 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
636 token = c_lex (&x);
637 if (token != CPP_NAME)
639 GCC_BAD ("malformed #pragma GCC visibility push");
641 else
643 const char *str = IDENTIFIER_POINTER (x);
644 VEC_safe_push (visibility, heap, visstack,
645 default_visibility);
646 if (!strcmp (str, "default"))
647 default_visibility = VISIBILITY_DEFAULT;
648 else if (!strcmp (str, "internal"))
649 default_visibility = VISIBILITY_INTERNAL;
650 else if (!strcmp (str, "hidden"))
651 default_visibility = VISIBILITY_HIDDEN;
652 else if (!strcmp (str, "protected"))
653 default_visibility = VISIBILITY_PROTECTED;
654 else
656 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
658 visibility_options.inpragma = 1;
660 if (c_lex (&x) != CPP_CLOSE_PAREN)
661 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
664 if (c_lex (&x) != CPP_EOF)
665 warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
668 #endif
670 /* Front-end wrappers for pragma registration to avoid dragging
671 cpplib.h in almost everywhere. */
672 void
673 c_register_pragma (const char *space, const char *name,
674 void (*handler) (struct cpp_reader *))
676 cpp_register_pragma (parse_in, space, name, handler, 0);
679 void
680 c_register_pragma_with_expansion (const char *space, const char *name,
681 void (*handler) (struct cpp_reader *))
683 cpp_register_pragma (parse_in, space, name, handler, 1);
686 /* Set up front-end pragmas. */
687 void
688 init_pragma (void)
690 #ifdef HANDLE_PRAGMA_PACK
691 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
692 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
693 #else
694 c_register_pragma (0, "pack", handle_pragma_pack);
695 #endif
696 #endif
697 #ifdef HANDLE_PRAGMA_WEAK
698 c_register_pragma (0, "weak", handle_pragma_weak);
699 #endif
700 #ifdef HANDLE_PRAGMA_VISIBILITY
701 c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
702 #endif
704 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname);
705 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
707 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma);
709 #ifdef REGISTER_TARGET_PRAGMAS
710 REGISTER_TARGET_PRAGMAS ();
711 #endif
714 #include "gt-c-pragma.h"