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 if (TREE_CODE (x
) != INTEGER_CST
)
162 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
163 align
= TREE_INT_CST_LOW (x
);
165 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
166 GCC_BAD ("malformed %<#pragma pack%> - ignored");
168 else if (token
== CPP_NAME
)
170 #define GCC_BAD_ACTION do { if (action != pop) \
171 GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
173 GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
176 const char *op
= IDENTIFIER_POINTER (x
);
177 if (!strcmp (op
, "push"))
179 else if (!strcmp (op
, "pop"))
182 GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op
);
184 while ((token
= c_lex (&x
)) == CPP_COMMA
)
187 if (token
== CPP_NAME
&& id
== 0)
191 else if (token
== CPP_NUMBER
&& action
== push
&& align
== -1)
193 if (TREE_CODE (x
) != INTEGER_CST
)
194 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
195 align
= TREE_INT_CST_LOW (x
);
203 if (token
!= CPP_CLOSE_PAREN
)
205 #undef GCC_BAD_ACTION
208 GCC_BAD ("malformed %<#pragma pack%> - ignored");
210 if (c_lex (&x
) != CPP_EOF
)
211 warning (OPT_Wpragmas
, "junk at end of %<#pragma pack%>");
213 if (flag_pack_struct
)
214 GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
225 align
*= BITS_PER_UNIT
;
230 align
= maximum_field_alignment
;
234 GCC_BAD2 ("alignment must be a small power of two, not %d", align
);
239 case set
: SET_GLOBAL_ALIGNMENT (align
); break;
240 case push
: push_alignment (align
, id
); break;
241 case pop
: pop_alignment (id
); break;
244 #endif /* HANDLE_PRAGMA_PACK */
246 static GTY(()) tree pending_weaks
;
248 #ifdef HANDLE_PRAGMA_WEAK
249 static void apply_pragma_weak (tree
, tree
);
250 static void handle_pragma_weak (cpp_reader
*);
253 apply_pragma_weak (tree decl
, tree value
)
257 value
= build_string (IDENTIFIER_LENGTH (value
),
258 IDENTIFIER_POINTER (value
));
259 decl_attributes (&decl
, build_tree_list (get_identifier ("alias"),
260 build_tree_list (NULL
, value
)),
264 if (SUPPORTS_WEAK
&& DECL_EXTERNAL (decl
) && TREE_USED (decl
)
265 && !DECL_WEAK (decl
) /* Don't complain about a redundant #pragma. */
266 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
267 warning (OPT_Wpragmas
, "applying #pragma weak %q+D after first use "
268 "results in unspecified behavior", decl
);
274 maybe_apply_pragma_weak (tree decl
)
278 /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed. */
280 /* No weak symbols pending, take the short-cut. */
283 /* If it's not visible outside this file, it doesn't matter whether
285 if (!DECL_EXTERNAL (decl
) && !TREE_PUBLIC (decl
))
287 /* If it's not a function or a variable, it can't be weak.
288 FIXME: what kinds of things are visible outside this file but
289 aren't functions or variables? Should this be an assert instead? */
290 if (TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
293 id
= DECL_ASSEMBLER_NAME (decl
);
295 for (p
= &pending_weaks
; (t
= *p
) ; p
= &TREE_CHAIN (t
))
296 if (id
== TREE_PURPOSE (t
))
298 apply_pragma_weak (decl
, TREE_VALUE (t
));
304 /* Process all "#pragma weak A = B" directives where we have not seen
307 maybe_apply_pending_pragma_weaks (void)
309 tree
*p
, t
, alias_id
, id
, decl
, *next
;
311 for (p
= &pending_weaks
; (t
= *p
) ; p
= next
)
313 next
= &TREE_CHAIN (t
);
314 alias_id
= TREE_PURPOSE (t
);
317 if (TREE_VALUE (t
) == NULL
)
320 decl
= build_decl (FUNCTION_DECL
, alias_id
, default_function_type
);
322 DECL_ARTIFICIAL (decl
) = 1;
323 TREE_PUBLIC (decl
) = 1;
324 DECL_EXTERNAL (decl
) = 1;
325 DECL_WEAK (decl
) = 1;
327 assemble_alias (decl
, id
);
331 /* #pragma weak name [= value] */
333 handle_pragma_weak (cpp_reader
* ARG_UNUSED (dummy
))
335 tree name
, value
, x
, decl
;
340 if (c_lex (&name
) != CPP_NAME
)
341 GCC_BAD ("malformed #pragma weak, ignored");
345 if (c_lex (&value
) != CPP_NAME
)
346 GCC_BAD ("malformed #pragma weak, ignored");
350 warning (OPT_Wpragmas
, "junk at end of #pragma weak");
352 decl
= identifier_global_value (name
);
353 if (decl
&& DECL_P (decl
))
355 apply_pragma_weak (decl
, value
);
357 assemble_alias (decl
, value
);
360 pending_weaks
= tree_cons (name
, value
, pending_weaks
);
364 maybe_apply_pragma_weak (tree
ARG_UNUSED (decl
))
369 maybe_apply_pending_pragma_weaks (void)
372 #endif /* HANDLE_PRAGMA_WEAK */
374 /* GCC supports two #pragma directives for renaming the external
375 symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
376 compatibility with the Solaris and Tru64 system headers. GCC also
377 has its own notation for this, __asm__("name") annotations.
379 Corner cases of these features and their interaction:
381 1) Both pragmas silently apply only to declarations with external
382 linkage (that is, TREE_PUBLIC || DECL_EXTERNAL). Asm labels
383 do not have this restriction.
385 2) In C++, both #pragmas silently apply only to extern "C" declarations.
386 Asm labels do not have this restriction.
388 3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
389 applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
390 new name is different, a warning issues and the name does not change.
392 4) The "source name" for #pragma redefine_extname is the DECL_NAME,
393 *not* the DECL_ASSEMBLER_NAME.
395 5) If #pragma extern_prefix is in effect and a declaration occurs
396 with an __asm__ name, the #pragma extern_prefix is silently
397 ignored for that declaration.
399 6) If #pragma extern_prefix and #pragma redefine_extname apply to
400 the same declaration, whichever triggered first wins, and a warning
401 is issued. (We would like to have #pragma redefine_extname always
402 win, but it can appear either before or after the declaration, and
403 if it appears afterward, we have no way of knowing whether a modified
404 DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.) */
406 static GTY(()) tree pending_redefine_extname
;
408 static void handle_pragma_redefine_extname (cpp_reader
*);
410 /* #pragma redefine_extname oldname newname */
412 handle_pragma_redefine_extname (cpp_reader
* ARG_UNUSED (dummy
))
414 tree oldname
, newname
, decl
, x
;
417 if (c_lex (&oldname
) != CPP_NAME
)
418 GCC_BAD ("malformed #pragma redefine_extname, ignored");
419 if (c_lex (&newname
) != CPP_NAME
)
420 GCC_BAD ("malformed #pragma redefine_extname, ignored");
423 warning (OPT_Wpragmas
, "junk at end of #pragma redefine_extname");
425 if (!flag_mudflap
&& !targetm
.handle_pragma_redefine_extname
)
427 if (warn_unknown_pragmas
> in_system_header
)
428 warning (OPT_Wunknown_pragmas
,
429 "#pragma redefine_extname not supported on this target");
433 decl
= identifier_global_value (oldname
);
435 && (TREE_PUBLIC (decl
) || DECL_EXTERNAL (decl
))
436 && (TREE_CODE (decl
) == FUNCTION_DECL
437 || TREE_CODE (decl
) == VAR_DECL
)
438 && has_c_linkage (decl
))
440 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
442 const char *name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
443 name
= targetm
.strip_name_encoding (name
);
445 if (strcmp (name
, IDENTIFIER_POINTER (newname
)))
446 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
447 "conflict with previous rename");
450 change_decl_assembler_name (decl
, newname
);
453 /* We have to add this to the rename list even if there's already
454 a global value that doesn't meet the above criteria, because in
455 C++ "struct foo {...};" puts "foo" in the current namespace but
456 does *not* conflict with a subsequent declaration of a function
457 or variable foo. See g++.dg/other/pragma-re-2.C. */
458 add_to_renaming_pragma_list (oldname
, newname
);
461 /* This is called from here and from ia64.c. */
463 add_to_renaming_pragma_list (tree oldname
, tree newname
)
465 tree previous
= purpose_member (oldname
, pending_redefine_extname
);
468 if (TREE_VALUE (previous
) != newname
)
469 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
470 "conflict with previous #pragma redefine_extname");
474 pending_redefine_extname
475 = tree_cons (oldname
, newname
, pending_redefine_extname
);
478 static GTY(()) tree pragma_extern_prefix
;
480 /* #pragma extern_prefix "prefix" */
482 handle_pragma_extern_prefix (cpp_reader
* ARG_UNUSED (dummy
))
487 if (c_lex (&prefix
) != CPP_STRING
)
488 GCC_BAD ("malformed #pragma extern_prefix, ignored");
491 warning (OPT_Wpragmas
, "junk at end of #pragma extern_prefix");
493 if (targetm
.handle_pragma_extern_prefix
)
494 /* Note that the length includes the null terminator. */
495 pragma_extern_prefix
= (TREE_STRING_LENGTH (prefix
) > 1 ? prefix
: NULL
);
496 else if (warn_unknown_pragmas
> in_system_header
)
497 warning (OPT_Wunknown_pragmas
,
498 "#pragma extern_prefix not supported on this target");
501 /* Hook from the front ends to apply the results of one of the preceding
502 pragmas that rename variables. */
505 maybe_apply_renaming_pragma (tree decl
, tree asmname
)
509 /* The renaming pragmas are only applied to declarations with
511 if ((TREE_CODE (decl
) != FUNCTION_DECL
&& TREE_CODE (decl
) != VAR_DECL
)
512 || (!TREE_PUBLIC (decl
) && !DECL_EXTERNAL (decl
))
513 || !has_c_linkage (decl
))
516 /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
517 but we may warn about a rename that conflicts. */
518 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
520 const char *oldname
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
521 oldname
= targetm
.strip_name_encoding (oldname
);
523 if (asmname
&& strcmp (TREE_STRING_POINTER (asmname
), oldname
))
524 warning (OPT_Wpragmas
, "asm declaration ignored due to "
525 "conflict with previous rename");
527 /* Take any pending redefine_extname off the list. */
528 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
529 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
531 /* Only warn if there is a conflict. */
532 if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t
)), oldname
))
533 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
534 "conflict with previous rename");
542 /* Find out if we have a pending #pragma redefine_extname. */
543 for (p
= &pending_redefine_extname
; (t
= *p
); p
= &TREE_CHAIN (t
))
544 if (DECL_NAME (decl
) == TREE_PURPOSE (t
))
546 tree newname
= TREE_VALUE (t
);
549 /* If we already have an asmname, #pragma redefine_extname is
550 ignored (with a warning if it conflicts). */
553 if (strcmp (TREE_STRING_POINTER (asmname
),
554 IDENTIFIER_POINTER (newname
)) != 0)
555 warning (OPT_Wpragmas
, "#pragma redefine_extname ignored due to "
556 "conflict with __asm__ declaration");
560 /* Otherwise we use what we've got; #pragma extern_prefix is
562 return build_string (IDENTIFIER_LENGTH (newname
),
563 IDENTIFIER_POINTER (newname
));
566 /* If we've got an asmname, #pragma extern_prefix is silently ignored. */
570 /* If #pragma extern_prefix is in effect, apply it. */
571 if (pragma_extern_prefix
)
573 const char *prefix
= TREE_STRING_POINTER (pragma_extern_prefix
);
574 size_t plen
= TREE_STRING_LENGTH (pragma_extern_prefix
) - 1;
576 const char *id
= IDENTIFIER_POINTER (DECL_NAME (decl
));
577 size_t ilen
= IDENTIFIER_LENGTH (DECL_NAME (decl
));
579 char *newname
= (char *) alloca (plen
+ ilen
+ 1);
581 memcpy (newname
, prefix
, plen
);
582 memcpy (newname
+ plen
, id
, ilen
+ 1);
584 return build_string (plen
+ ilen
, newname
);
592 #ifdef HANDLE_PRAGMA_VISIBILITY
593 static void handle_pragma_visibility (cpp_reader
*);
595 typedef enum symbol_visibility visibility
;
596 DEF_VEC_I (visibility
);
597 DEF_VEC_ALLOC_I (visibility
, heap
);
599 /* Sets the default visibility for symbols to something other than that
600 specified on the command line. */
602 handle_pragma_visibility (cpp_reader
*dummy ATTRIBUTE_UNUSED
)
604 /* Form is #pragma GCC visibility push(hidden)|pop */
606 enum cpp_ttype token
;
607 enum { bad
, push
, pop
} action
= bad
;
608 static VEC (visibility
, heap
) *visstack
;
611 if (token
== CPP_NAME
)
613 const char *op
= IDENTIFIER_POINTER (x
);
614 if (!strcmp (op
, "push"))
616 else if (!strcmp (op
, "pop"))
620 GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
625 if (!VEC_length (visibility
, visstack
))
627 GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
631 default_visibility
= VEC_pop (visibility
, visstack
);
632 visibility_options
.inpragma
633 = VEC_length (visibility
, visstack
) != 0;
638 if (c_lex (&x
) != CPP_OPEN_PAREN
)
639 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
641 if (token
!= CPP_NAME
)
643 GCC_BAD ("malformed #pragma GCC visibility push");
647 const char *str
= IDENTIFIER_POINTER (x
);
648 VEC_safe_push (visibility
, heap
, visstack
,
650 if (!strcmp (str
, "default"))
651 default_visibility
= VISIBILITY_DEFAULT
;
652 else if (!strcmp (str
, "internal"))
653 default_visibility
= VISIBILITY_INTERNAL
;
654 else if (!strcmp (str
, "hidden"))
655 default_visibility
= VISIBILITY_HIDDEN
;
656 else if (!strcmp (str
, "protected"))
657 default_visibility
= VISIBILITY_PROTECTED
;
660 GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
662 visibility_options
.inpragma
= 1;
664 if (c_lex (&x
) != CPP_CLOSE_PAREN
)
665 GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
668 if (c_lex (&x
) != CPP_EOF
)
669 warning (OPT_Wpragmas
, "junk at end of %<#pragma GCC visibility%>");
674 /* Front-end wrappers for pragma registration to avoid dragging
675 cpplib.h in almost everywhere. */
677 c_register_pragma (const char *space
, const char *name
,
678 void (*handler
) (struct cpp_reader
*))
680 cpp_register_pragma (parse_in
, space
, name
, handler
, 0);
684 c_register_pragma_with_expansion (const char *space
, const char *name
,
685 void (*handler
) (struct cpp_reader
*))
687 cpp_register_pragma (parse_in
, space
, name
, handler
, 1);
690 /* Set up front-end pragmas. */
694 #ifdef HANDLE_PRAGMA_PACK
695 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
696 c_register_pragma_with_expansion (0, "pack", handle_pragma_pack
);
698 c_register_pragma (0, "pack", handle_pragma_pack
);
701 #ifdef HANDLE_PRAGMA_WEAK
702 c_register_pragma (0, "weak", handle_pragma_weak
);
704 #ifdef HANDLE_PRAGMA_VISIBILITY
705 c_register_pragma ("GCC", "visibility", handle_pragma_visibility
);
708 c_register_pragma (0, "redefine_extname", handle_pragma_redefine_extname
);
709 c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix
);
711 c_register_pragma ("GCC", "pch_preprocess", c_common_pch_pragma
);
713 #ifdef REGISTER_TARGET_PRAGMAS
714 REGISTER_TARGET_PRAGMAS ();
718 #include "gt-c-pragma.h"