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
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
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
24 #include "coretypes.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(())
49 struct align_stack
* prev
;
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. */
73 push_alignment (int alignment
, tree id
)
77 entry
= ggc_alloc (sizeof (* entry
));
79 entry
->alignment
= alignment
;
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. */
96 pop_alignment (tree id
)
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. */
107 for (entry
= alignment_stack
; entry
; entry
= entry
->prev
)
110 alignment_stack
= entry
;
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 */
137 #pragma pack (push, N)
138 #pragma pack (push, ID)
139 #pragma pack (push, ID, N)
141 #pragma pack (pop, ID) */
143 handle_pragma_pack (cpp_reader
* ARG_UNUSED (dummy
))
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");
154 if (token
== CPP_CLOSE_PAREN
)
157 align
= initial_max_fld_align
;
159 else if (token
== CPP_NUMBER
)
161 align
= TREE_INT_CST_LOW (x
);
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"); \
171 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
174 const char *op
= IDENTIFIER_POINTER (x
);
175 if (!strcmp (op
, "push"))
177 else if (!strcmp (op
, "pop"))
180 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op
);
182 while ((token
= c_lex (&x
)) == CPP_COMMA
)
185 if (token
== CPP_NAME
&& id
== 0)
189 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
191 align
= TREE_INT_CST_LOW (x
);
199 if (token
!= CPP_CLOSE_PAREN
)
201 #undef GCC_BAD_ACTION
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");
221 align
*= BITS_PER_UNIT
;
226 align
= maximum_field_alignment
;
230 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
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
*);
249 apply_pragma_weak (tree decl
, tree 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
);
270 maybe_apply_pragma_weak (tree decl
)
274 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
276 /* No weak symbols pending, take the short-cut. */
279 /* If it's not visible outside this file, it doesn't matter whether
281 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
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
)
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
));
300 /* Process all "#pragma weak A = B" directives where we have not seen
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
);
313 if (TREE_VALUE (t
) == NULL
)
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] */
329 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
331 tree name
, value
, x
, decl
;
336 if (c_lex (&name
) != CPP_NAME
)
337 GCC_BAD ("malformed #pragma weak, ignored");
341 if (c_lex (&value
) != CPP_NAME
)
342 GCC_BAD ("malformed #pragma weak, ignored");
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
);
353 assemble_alias (decl
, value
);
356 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
360 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
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 */
408 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
410 tree oldname
, newname
, decl
, x
;
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");
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");
429 decl
= identifier_global_value (oldname
);
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");
446 change_decl_assembler_name (decl
, newname
);
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. */
459 add_to_renaming_pragma_list (tree oldname
, tree newname
)
461 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
464 if (TREE_VALUE (previous
) != newname
)
465 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
466 "conflict with previous #pragma redefine_extname");
470 pending_redefine_extname
471 = tree_cons (oldname
, newname
, pending_redefine_extname
);
474 static GTY(()) tree pragma_extern_prefix
;
476 /* #pragma extern_prefix "prefix" */
478 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
483 if (c_lex (&prefix
) != CPP_STRING
)
484 GCC_BAD ("malformed #pragma extern_prefix, ignored");
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. */
501 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
505 /* The renaming pragmas are only applied to declarations with
507 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
508 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
509 || !has_c_linkage (decl
))
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");
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
);
545 /* If we already have an asmname, #pragma redefine_extname is
546 ignored (with a warning if it conflicts). */
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");
556 /* Otherwise we use what we've got; #pragma extern_prefix is
558 return build_string (IDENTIFIER_LENGTH (newname
),
559 IDENTIFIER_POINTER (newname
));
562 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
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
);
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. */
598 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
600 /* Form is #pragma GCC visibility push(hidden)|pop */
602 enum cpp_ttype token
;
603 enum { bad
, push
, pop
} action
= bad
;
604 static VEC (visibility
, heap
) *visstack
;
607 if (token
== CPP_NAME
)
609 const char *op
= IDENTIFIER_POINTER (x
);
610 if (!strcmp (op
, "push"))
612 else if (!strcmp (op
, "pop"))
616 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
621 if (!VEC_length (visibility
, visstack
))
623 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
627 default_visibility
= VEC_pop (visibility
, visstack
);
628 visibility_options
.inpragma
629 = VEC_length (visibility
, visstack
) != 0;
634 if (c_lex (&x
) != CPP_OPEN_PAREN
)
635 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
637 if (token
!= CPP_NAME
)
639 GCC_BAD ("malformed #pragma GCC visibility push");
643 const char *str
= IDENTIFIER_POINTER (x
);
644 VEC_safe_push (visibility
, heap
, visstack
,
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
;
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%>");
670 /* Front-end wrappers for pragma registration to avoid dragging
671 cpplib.h in almost everywhere. */
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);
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. */
690 #ifdef HANDLE_PRAGMA_PACK
691 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
692 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
694 c_register_pragma (0, "pack", handle_pragma_pack
);
697 #ifdef HANDLE_PRAGMA_WEAK
698 c_register_pragma (0, "weak", handle_pragma_weak
);
700 #ifdef HANDLE_PRAGMA_VISIBILITY
701 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
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 ();
714 #include "gt-c-pragma.h"