cfgrtl.c (commit_one_edge_insertion): Fix warning.
[official-gcc.git] / gcc / c-pragma.c
blob1a016b8b2201982498daaf658cb5217ba73a29db
1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000, 2001, 2002
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "function.h"
27 #include "cpplib.h"
28 #include "c-pragma.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "ggc.h"
32 #include "c-common.h"
33 #include "output.h"
34 #include "tm_p.h"
36 #define GCC_BAD(msgid) do { warning (msgid); return; } while (0)
37 #define GCC_BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
39 #ifdef HANDLE_PRAGMA_PACK
40 static void handle_pragma_pack PARAMS ((cpp_reader *));
42 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
43 typedef struct align_stack
45 int alignment;
46 unsigned int num_pushes;
47 tree id;
48 struct align_stack * prev;
49 } align_stack;
51 static struct align_stack * alignment_stack = NULL;
53 /* If we have a "global" #pragma pack(<n>) in effect when the first
54 #pragma pack(push,<n>) is encountered, this stores the value of
55 maximum_field_alignment in effect. When the final pop_alignment()
56 happens, we restore the value to this, not to a value of 0 for
57 maximum_field_alignment. Value is in bits. */
58 static int default_alignment;
59 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
60 (default_alignment = maximum_field_alignment = (ALIGN))
62 static void push_alignment PARAMS ((int, tree));
63 static void pop_alignment PARAMS ((tree));
64 static void mark_align_stack PARAMS ((void *));
66 /* Push an alignment value onto the stack. */
67 static void
68 push_alignment (alignment, id)
69 int alignment;
70 tree id;
72 if (alignment_stack == NULL
73 || alignment_stack->alignment != alignment
74 || id != NULL_TREE)
76 align_stack * entry;
78 entry = (align_stack *) xmalloc (sizeof (* entry));
80 entry->alignment = alignment;
81 entry->num_pushes = 1;
82 entry->id = id;
83 entry->prev = alignment_stack;
85 /* The current value of maximum_field_alignment is not necessarily
86 0 since there may be a #pragma pack(<n>) in effect; remember it
87 so that we can restore it after the final #pragma pop(). */
88 if (alignment_stack == NULL)
89 default_alignment = maximum_field_alignment;
91 alignment_stack = entry;
93 maximum_field_alignment = alignment;
95 else
96 alignment_stack->num_pushes ++;
99 /* Undo a push of an alignment onto the stack. */
100 static void
101 pop_alignment (id)
102 tree id;
104 align_stack * entry;
106 if (alignment_stack == NULL)
108 warning ("\
109 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
111 return;
114 /* If we got an identifier, strip away everything above the target
115 entry so that the next step will restore the state just below it. */
116 if (id)
118 for (entry = alignment_stack; entry; entry = entry->prev)
119 if (entry->id == id)
121 entry->num_pushes = 1;
122 alignment_stack = entry;
123 break;
125 if (entry == NULL)
126 warning ("\
127 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
128 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
131 if (-- alignment_stack->num_pushes == 0)
133 entry = alignment_stack->prev;
135 if (entry == NULL)
136 maximum_field_alignment = default_alignment;
137 else
138 maximum_field_alignment = entry->alignment;
140 free (alignment_stack);
142 alignment_stack = entry;
146 static void
147 mark_align_stack (p)
148 void *p;
150 align_stack *a = *(align_stack **) p;
152 while (a)
154 ggc_mark_tree (a->id);
155 a = a->prev;
158 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
159 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
160 #define push_alignment(ID, N) \
161 GCC_BAD("#pragma pack(push[, id], <n>) is not supported on this target")
162 #define pop_alignment(ID) \
163 GCC_BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
164 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
166 /* #pragma pack ()
167 #pragma pack (N)
169 #pragma pack (push, N)
170 #pragma pack (push, ID, N)
171 #pragma pack (pop)
172 #pragma pack (pop, ID) */
173 static void
174 handle_pragma_pack (dummy)
175 cpp_reader *dummy ATTRIBUTE_UNUSED;
177 tree x, id = 0;
178 int align = -1;
179 enum cpp_ttype token;
180 enum { set, push, pop } action;
182 if (c_lex (&x) != CPP_OPEN_PAREN)
183 GCC_BAD ("missing '(' after '#pragma pack' - ignored");
185 token = c_lex (&x);
186 if (token == CPP_CLOSE_PAREN)
188 action = set;
189 align = 0;
191 else if (token == CPP_NUMBER)
193 align = TREE_INT_CST_LOW (x);
194 action = set;
195 if (c_lex (&x) != CPP_CLOSE_PAREN)
196 GCC_BAD ("malformed '#pragma pack' - ignored");
198 else if (token == CPP_NAME)
200 #define GCC_BAD_ACTION do { if (action == push) \
201 GCC_BAD ("malformed '#pragma pack(push[, id], <n>)' - ignored"); \
202 else \
203 GCC_BAD ("malformed '#pragma pack(pop[, id])' - ignored"); \
204 } while (0)
206 const char *op = IDENTIFIER_POINTER (x);
207 if (!strcmp (op, "push"))
208 action = push;
209 else if (!strcmp (op, "pop"))
210 action = pop;
211 else
212 GCC_BAD2 ("unknown action '%s' for '#pragma pack' - ignored", op);
214 token = c_lex (&x);
215 if (token != CPP_COMMA && action == push)
216 GCC_BAD_ACTION;
218 if (token == CPP_COMMA)
220 token = c_lex (&x);
221 if (token == CPP_NAME)
223 id = x;
224 if (action == push && c_lex (&x) != CPP_COMMA)
225 GCC_BAD_ACTION;
226 token = c_lex (&x);
229 if (action == push)
231 if (token == CPP_NUMBER)
233 align = TREE_INT_CST_LOW (x);
234 token = c_lex (&x);
236 else
237 GCC_BAD_ACTION;
241 if (token != CPP_CLOSE_PAREN)
242 GCC_BAD_ACTION;
243 #undef GCC_BAD_ACTION
245 else
246 GCC_BAD ("malformed '#pragma pack' - ignored");
248 if (c_lex (&x) != CPP_EOF)
249 warning ("junk at end of '#pragma pack'");
251 if (action != pop)
252 switch (align)
254 case 0:
255 case 1:
256 case 2:
257 case 4:
258 case 8:
259 case 16:
260 align *= BITS_PER_UNIT;
261 break;
262 default:
263 GCC_BAD2 ("alignment must be a small power of two, not %d", align);
266 switch (action)
268 case set: SET_GLOBAL_ALIGNMENT (align); break;
269 case push: push_alignment (align, id); break;
270 case pop: pop_alignment (id); break;
273 #endif /* HANDLE_PRAGMA_PACK */
275 #ifdef HANDLE_PRAGMA_WEAK
276 static void apply_pragma_weak PARAMS ((tree, tree));
277 static void handle_pragma_weak PARAMS ((cpp_reader *));
279 static tree pending_weaks;
281 static void
282 apply_pragma_weak (decl, value)
283 tree decl, value;
285 if (value)
287 value = build_string (IDENTIFIER_LENGTH (value),
288 IDENTIFIER_POINTER (value));
289 decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
290 build_tree_list (NULL, value)),
294 if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
295 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
296 warning_with_decl (decl, "applying #pragma weak `%s' after first use results in unspecified behavior");
298 declare_weak (decl);
301 void
302 maybe_apply_pragma_weak (decl)
303 tree decl;
305 tree *p, t, id;
307 /* Copied from the check in set_decl_assembler_name. */
308 if (TREE_CODE (decl) == FUNCTION_DECL
309 || (TREE_CODE (decl) == VAR_DECL
310 && (TREE_STATIC (decl)
311 || DECL_EXTERNAL (decl)
312 || TREE_PUBLIC (decl))))
313 id = DECL_ASSEMBLER_NAME (decl);
314 else
315 return;
317 for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
318 if (id == TREE_PURPOSE (t))
320 apply_pragma_weak (decl, TREE_VALUE (t));
321 *p = TREE_CHAIN (t);
322 break;
326 /* #pragma weak name [= value] */
327 static void
328 handle_pragma_weak (dummy)
329 cpp_reader *dummy ATTRIBUTE_UNUSED;
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 ("junk at end of #pragma weak");
348 decl = identifier_global_value (name);
349 if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
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 (decl)
361 tree decl ATTRIBUTE_UNUSED;
364 #endif /* HANDLE_PRAGMA_WEAK */
366 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
367 static void handle_pragma_redefine_extname PARAMS ((cpp_reader *));
369 static tree pending_redefine_extname;
371 /* #pragma redefined_extname oldname newname */
372 static void
373 handle_pragma_redefine_extname (dummy)
374 cpp_reader *dummy ATTRIBUTE_UNUSED;
376 tree oldname, newname, decl, x;
377 enum cpp_ttype t;
379 if (c_lex (&oldname) != CPP_NAME)
381 warning ("malformed #pragma redefine_extname, ignored");
382 return;
384 if (c_lex (&newname) != CPP_NAME)
386 warning ("malformed #pragma redefine_extname, ignored");
387 return;
389 t = c_lex (&x);
390 if (t != CPP_EOF)
391 warning ("junk at end of #pragma redefine_extname");
393 decl = identifier_global_value (oldname);
394 if (decl && TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
396 if (DECL_ASSEMBLER_NAME_SET_P (decl)
397 && DECL_ASSEMBLER_NAME (decl) != newname)
398 warning ("#pragma redefine_extname conflicts with declaration");
399 SET_DECL_ASSEMBLER_NAME (decl, newname);
401 else
402 pending_redefine_extname
403 = tree_cons (oldname, newname, pending_redefine_extname);
405 #endif
407 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
408 static void handle_pragma_extern_prefix PARAMS ((cpp_reader *));
410 static tree pragma_extern_prefix;
412 /* #pragma extern_prefix "prefix" */
413 static void
414 handle_pragma_extern_prefix (dummy)
415 cpp_reader *dummy ATTRIBUTE_UNUSED;
417 tree prefix, x;
418 enum cpp_ttype t;
420 if (c_lex (&prefix) != CPP_STRING)
422 warning ("malformed #pragma extern_prefix, ignored");
423 return;
425 t = c_lex (&x);
426 if (t != CPP_EOF)
427 warning ("junk at end of #pragma extern_prefix");
429 /* Note that the length includes the null terminator. */
430 pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
432 #endif
434 /* Hook from the front ends to apply the results of one of the preceeding
435 pragmas that rename variables. */
437 tree
438 maybe_apply_renaming_pragma (decl, asmname)
439 tree decl, asmname;
441 tree oldname;
443 /* Copied from the check in set_decl_assembler_name. */
444 if (TREE_CODE (decl) == FUNCTION_DECL
445 || (TREE_CODE (decl) == VAR_DECL
446 && (TREE_STATIC (decl)
447 || DECL_EXTERNAL (decl)
448 || TREE_PUBLIC (decl))))
449 oldname = DECL_ASSEMBLER_NAME (decl);
450 else
451 return asmname;
453 /* If the name begins with a *, that's a sign of an asmname attached to
454 a previous declaration. */
455 if (IDENTIFIER_POINTER (oldname)[0] == '*')
457 const char *oldasmname = IDENTIFIER_POINTER (oldname) + 1;
458 if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldasmname) != 0)
459 warning ("asm declaration conficts with previous rename");
460 asmname = build_string (strlen (oldasmname), oldasmname);
463 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
465 tree *p, t;
467 for (p = &pending_redefine_extname; (t = *p) ; p = &TREE_CHAIN (t))
468 if (oldname == TREE_PURPOSE (t))
470 const char *newname = IDENTIFIER_POINTER (TREE_VALUE (t));
472 if (asmname && strcmp (TREE_STRING_POINTER (asmname), newname) != 0)
473 warning ("#pragma redefine_extname conflicts with declaration");
474 *p = TREE_CHAIN (t);
476 return build_string (strlen (newname), newname);
479 #endif
481 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
482 if (pragma_extern_prefix && !asmname)
484 char *x = concat (TREE_STRING_POINTER (pragma_extern_prefix),
485 IDENTIFIER_POINTER (oldname), NULL);
486 asmname = build_string (strlen (x), x);
487 free (x);
488 return asmname;
490 #endif
492 return asmname;
495 void
496 init_pragma ()
498 #ifdef HANDLE_PRAGMA_PACK
499 cpp_register_pragma (parse_in, 0, "pack", handle_pragma_pack);
500 #endif
501 #ifdef HANDLE_PRAGMA_WEAK
502 cpp_register_pragma (parse_in, 0, "weak", handle_pragma_weak);
503 ggc_add_tree_root (&pending_weaks, 1);
504 #endif
505 #ifdef HANDLE_PRAGMA_REDEFINE_EXTNAME
506 cpp_register_pragma (parse_in, 0, "redefine_extname",
507 handle_pragma_redefine_extname);
508 ggc_add_tree_root (&pending_redefine_extname, 1);
509 #endif
510 #ifdef HANDLE_PRAGMA_EXTERN_PREFIX
511 cpp_register_pragma (parse_in, 0, "extern_prefix",
512 handle_pragma_extern_prefix);
513 ggc_add_tree_root (&pragma_extern_prefix, 1);
514 #endif
516 #ifdef REGISTER_TARGET_PRAGMAS
517 REGISTER_TARGET_PRAGMAS (parse_in);
518 #endif
520 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
521 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
522 mark_align_stack);
523 #endif