1 /* Handle #pragma, system V.4 style. Supports #pragma weak and #pragma pack.
2 Copyright (C) 1992, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
35 #ifdef HANDLE_GENERIC_PRAGMAS
38 extern cpp_reader parse_in;
41 static struct pragma_entry *pragmas;
43 void cpp_register_pragma PARAMS ((cpp_reader *, const char *, const char *,
44 void (*) PARAMS ((cpp_reader *)) ));
45 void cpp_register_pragma_space PARAMS ((cpp_reader *, const char *));
48 #define BAD(msgid) do { warning (msgid); return; } while (0)
49 #define BAD2(msgid, arg) do { warning (msgid, arg); return; } while (0)
51 #ifdef HANDLE_PRAGMA_PACK
52 static void handle_pragma_pack PARAMS ((cpp_reader *));
54 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
55 typedef struct align_stack
58 unsigned int num_pushes;
60 struct align_stack * prev;
63 static struct align_stack * alignment_stack = NULL;
65 /* If we have a "global" #pragma pack(<n>) if effect when the first
66 #pragma push(pack,<n>) is encountered, this stores the the value of
67 maximum_field_alignment in effect. When the final pop_alignment()
68 happens, we restore the value to this, not to a value of 0 for
69 maximum_field_alignment. Value is in bits. */
70 static int default_alignment;
71 #define SET_GLOBAL_ALIGNMENT(ALIGN) \
72 (default_alignment = maximum_field_alignment = (ALIGN))
74 static void push_alignment PARAMS ((int, tree));
75 static void pop_alignment PARAMS ((tree));
76 static void mark_align_stack PARAMS ((void *));
78 /* Push an alignment value onto the stack. */
80 push_alignment (alignment, id)
85 if (alignment_stack == NULL
86 || alignment_stack->alignment != alignment
91 entry = (align_stack *) xmalloc (sizeof (* entry));
93 entry->alignment = alignment;
94 entry->num_pushes = 1;
96 entry->prev = alignment_stack;
98 /* The current value of maximum_field_alignment is not necessarily
99 0 since there may be a #pragma pack(<n>) in effect; remember it
100 so that we can restore it after the final #pragma pop(). */
101 if (alignment_stack == NULL)
102 default_alignment = maximum_field_alignment;
104 alignment_stack = entry;
106 maximum_field_alignment = alignment;
109 alignment_stack->num_pushes ++;
112 /* Undo a push of an alignment onto the stack. */
119 if (alignment_stack == NULL)
122 #pragma pack (pop) encountered without matching #pragma pack (push, <n>)"
127 /* If we got an identifier, strip away everything above the target
128 entry so that the next step will restore the state just below it. */
131 for (entry = alignment_stack; entry; entry = entry->prev)
134 entry->num_pushes = 1;
135 alignment_stack = entry;
140 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s, <n>)"
141 , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
144 if (-- alignment_stack->num_pushes == 0)
146 entry = alignment_stack->prev;
149 maximum_field_alignment = default_alignment;
151 maximum_field_alignment = entry->alignment;
153 free (alignment_stack);
155 alignment_stack = entry;
163 align_stack *a = *(align_stack **) p;
167 ggc_mark_tree (a->id);
171 #else /* not HANDLE_PRAGMA_PACK_PUSH_POP */
172 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
173 #define push_alignment(ID, N) \
174 BAD("#pragma pack(push[, id], <n>) is not supported on this target")
175 #define pop_alignment(ID) \
176 BAD("#pragma pack(pop[, id], <n>) is not supported on this target")
177 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
182 #pragma pack (push, N)
183 #pragma pack (push, ID, N)
185 #pragma pack (pop, ID) */
187 handle_pragma_pack (dummy)
188 cpp_reader *dummy ATTRIBUTE_UNUSED;
192 enum cpp_ttype token;
193 enum { set, reset, push, pop } action;
195 if (c_lex (&x) != CPP_OPEN_PAREN)
196 BAD ("missing '(' after '#pragma pack' - ignored");
199 if (token == CPP_CLOSE_PAREN)
201 else if (token == CPP_NUMBER)
203 align = TREE_INT_CST_LOW (x);
206 else if (token == CPP_NAME)
208 if (!strcmp (IDENTIFIER_POINTER (x), "push"))
210 else if (!strcmp (IDENTIFIER_POINTER (x), "pop"))
213 BAD2 ("unknown action '%s' for '#pragma pack' - ignored",
214 IDENTIFIER_POINTER (x));
217 BAD ("malformed '#pragma pack' - ignored");
220 if ((action == set || action == reset) && token != CPP_CLOSE_PAREN)
221 BAD ("malformed '#pragma pack' - ignored");
222 if ((action == push || action == pop) && token != CPP_COMMA)
223 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
224 action == push ? "push" : "pop");
226 if (action == push || action == pop)
229 if (token == CPP_NAME)
232 if (c_lex (&x) != CPP_COMMA)
233 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
234 action == push ? "push" : "pop");
237 if (token == CPP_NUMBER)
238 align = TREE_INT_CST_LOW (x);
240 BAD2 ("malformed '#pragma pack(%s[, id], <n>)' - ignored",
241 action == push ? "push" : "pop");
243 if (c_lex (&x) != CPP_CLOSE_PAREN)
244 BAD ("malformed '#pragma pack' - ignored");
246 if (c_lex (&x) != CPP_EOF)
247 warning ("junk at end of '#pragma pack'");
257 align *= BITS_PER_UNIT;
260 BAD2 ("alignment must be a small power of two, not %d", align);
265 case set: SET_GLOBAL_ALIGNMENT (align); break;
266 case reset: SET_GLOBAL_ALIGNMENT (0); break;
267 case push: push_alignment (align, id); break;
268 case pop: pop_alignment (id); break;
271 #endif /* HANDLE_PRAGMA_PACK */
273 #ifdef HANDLE_PRAGMA_WEAK
274 static void handle_pragma_weak PARAMS ((cpp_reader *));
276 /* #pragma weak name [= value] */
278 handle_pragma_weak (dummy)
279 cpp_reader *dummy ATTRIBUTE_UNUSED;
286 if (c_lex (&name) != CPP_NAME)
287 BAD ("malformed #pragma weak, ignored");
291 if (c_lex (&value) != CPP_NAME)
292 BAD ("malformed #pragma weak, ignored");
296 warning ("junk at end of #pragma weak");
298 add_weak (IDENTIFIER_POINTER (name), value ? IDENTIFIER_POINTER (value) : 0);
303 /* Glue version of cpplib's pragma registration and dispatch system. */
306 struct pragma_entry *next;
311 void (*handler) PARAMS ((cpp_reader *));
312 struct pragma_entry *space;
317 cpp_register_pragma_space (pfile, space)
318 cpp_reader *pfile ATTRIBUTE_UNUSED;
321 struct pragma_entry *new;
322 const struct pragma_entry *p = pragmas;
323 size_t len = strlen (space);
327 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
332 new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
343 cpp_register_pragma (pfile, space, name, handler)
344 cpp_reader *pfile ATTRIBUTE_UNUSED;
347 void (*handler) PARAMS ((cpp_reader *));
349 struct pragma_entry **x, *new;
355 struct pragma_entry *p = pragmas;
356 len = strlen (space);
359 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
370 new = (struct pragma_entry *) xmalloc (sizeof (struct pragma_entry));
372 new->len = strlen (name);
374 new->u.handler = handler;
380 /* Called from process_directive() for #pragma lines. */
386 const struct pragma_entry *p;
387 const char *name, *space = 0;
399 warning ("malformed #pragma directive");
403 name = IDENTIFIER_POINTER (x);
404 len = IDENTIFIER_LENGTH (x);
407 if (strlen (p->name) == len && !memcmp (p->name, name, len))
424 /* Issue a warning message if we have been asked to do so. Ignore
425 unknown pragmas in system headers unless an explicit
426 -Wunknown-pragmas has been given. */
427 if (warn_unknown_pragmas > in_system_header)
430 warning ("ignoring #pragma %s %s", space, name);
432 warning ("ignoring #pragma %s", name);
442 cpp_reader *pfile = 0;
444 cpp_reader *pfile = &parse_in;
447 #ifdef HANDLE_PRAGMA_PACK
448 cpp_register_pragma (pfile, 0, "pack", handle_pragma_pack);
450 #ifdef HANDLE_PRAGMA_WEAK
451 cpp_register_pragma (pfile, 0, "weak", handle_pragma_weak);
454 #ifdef REGISTER_TARGET_PRAGMAS
455 REGISTER_TARGET_PRAGMAS (pfile);
458 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
459 ggc_add_root (&alignment_stack, 1, sizeof(alignment_stack),
464 #endif /* HANDLE_GENERIC_PRAGMAS */