OSDN Git Service

Revert my previous commit.
[pf3gnuchains/gcc-fork.git] / gcc / c-pragma.c
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    2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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 3, or (at your option) any later
10 version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "function.h"
28 #include "cpplib.h"
29 #include "c-pragma.h"
30 #include "flags.h"
31 #include "toplev.h"
32 #include "ggc.h"
33 #include "c-common.h"
34 #include "output.h"
35 #include "tm_p.h"
36 #include "vec.h"
37 #include "target.h"
38 #include "diagnostic.h"
39 #include "opts.h"
40
41 #define GCC_BAD(gmsgid) \
42   do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
43 #define GCC_BAD2(gmsgid, arg) \
44   do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
45
46 typedef struct align_stack GTY(())
47 {
48   int                  alignment;
49   tree                 id;
50   struct align_stack * prev;
51 } align_stack;
52
53 static GTY(()) struct align_stack * alignment_stack;
54
55 #ifdef HANDLE_PRAGMA_PACK
56 static void handle_pragma_pack (cpp_reader *);
57
58 #ifdef HANDLE_PRAGMA_PACK_PUSH_POP
59 /* If we have a "global" #pragma pack(<n>) in effect when the first
60    #pragma pack(push,<n>) is encountered, this stores the value of
61    maximum_field_alignment in effect.  When the final pop_alignment()
62    happens, we restore the value to this, not to a value of 0 for
63    maximum_field_alignment.  Value is in bits.  */
64 static int default_alignment;
65 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
66         ? &default_alignment \
67         : &alignment_stack->alignment) = (ALIGN))
68
69 static void push_alignment (int, tree);
70 static void pop_alignment (tree);
71
72 /* Push an alignment value onto the stack.  */
73 static void
74 push_alignment (int alignment, tree id)
75 {
76   align_stack * entry;
77
78   entry = GGC_NEW (align_stack);
79
80   entry->alignment  = alignment;
81   entry->id         = id;
82   entry->prev       = alignment_stack;
83
84   /* The current value of maximum_field_alignment is not necessarily
85      0 since there may be a #pragma pack(<n>) in effect; remember it
86      so that we can restore it after the final #pragma pop().  */
87   if (alignment_stack == NULL)
88     default_alignment = maximum_field_alignment;
89
90   alignment_stack = entry;
91
92   maximum_field_alignment = alignment;
93 }
94
95 /* Undo a push of an alignment onto the stack.  */
96 static void
97 pop_alignment (tree id)
98 {
99   align_stack * entry;
100
101   if (alignment_stack == NULL)
102     GCC_BAD ("#pragma pack (pop) encountered without matching #pragma pack (push)");
103
104   /* If we got an identifier, strip away everything above the target
105      entry so that the next step will restore the state just below it.  */
106   if (id)
107     {
108       for (entry = alignment_stack; entry; entry = entry->prev)
109         if (entry->id == id)
110           {
111             alignment_stack = entry;
112             break;
113           }
114       if (entry == NULL)
115         warning (OPT_Wpragmas, "\
116 #pragma pack(pop, %s) encountered without matching #pragma pack(push, %s)"
117                  , IDENTIFIER_POINTER (id), IDENTIFIER_POINTER (id));
118     }
119
120   entry = alignment_stack->prev;
121
122   maximum_field_alignment = entry ? entry->alignment : default_alignment;
123
124   alignment_stack = entry;
125 }
126 #else  /* not HANDLE_PRAGMA_PACK_PUSH_POP */
127 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = (ALIGN))
128 #define push_alignment(ID, N) \
129     GCC_BAD ("#pragma pack(push[, id], <n>) is not supported on this target")
130 #define pop_alignment(ID) \
131     GCC_BAD ("#pragma pack(pop[, id], <n>) is not supported on this target")
132 #endif /* HANDLE_PRAGMA_PACK_PUSH_POP */
133
134 /* #pragma pack ()
135    #pragma pack (N)
136
137    #pragma pack (push)
138    #pragma pack (push, N)
139    #pragma pack (push, ID)
140    #pragma pack (push, ID, N)
141    #pragma pack (pop)
142    #pragma pack (pop, ID) */
143 static void
144 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
145 {
146   tree x, id = 0;
147   int align = -1;
148   enum cpp_ttype token;
149   enum { set, push, pop } action;
150
151   if (pragma_lex (&x) != CPP_OPEN_PAREN)
152     GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
153
154   token = pragma_lex (&x);
155   if (token == CPP_CLOSE_PAREN)
156     {
157       action = set;
158       align = initial_max_fld_align;
159     }
160   else if (token == CPP_NUMBER)
161     {
162       if (TREE_CODE (x) != INTEGER_CST)
163         GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
164       align = TREE_INT_CST_LOW (x);
165       action = set;
166       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
167         GCC_BAD ("malformed %<#pragma pack%> - ignored");
168     }
169   else if (token == CPP_NAME)
170     {
171 #define GCC_BAD_ACTION do { if (action != pop) \
172           GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
173         else \
174           GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
175         } while (0)
176
177       const char *op = IDENTIFIER_POINTER (x);
178       if (!strcmp (op, "push"))
179         action = push;
180       else if (!strcmp (op, "pop"))
181         action = pop;
182       else
183         GCC_BAD2 ("unknown action %qs for %<#pragma pack%> - ignored", op);
184
185       while ((token = pragma_lex (&x)) == CPP_COMMA)
186         {
187           token = pragma_lex (&x);
188           if (token == CPP_NAME && id == 0)
189             {
190               id = x;
191             }
192           else if (token == CPP_NUMBER && action == push && align == -1)
193             {
194               if (TREE_CODE (x) != INTEGER_CST)
195                 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
196               align = TREE_INT_CST_LOW (x);
197               if (align == -1)
198                 action = set;
199             }
200           else
201             GCC_BAD_ACTION;
202         }
203
204       if (token != CPP_CLOSE_PAREN)
205         GCC_BAD_ACTION;
206 #undef GCC_BAD_ACTION
207     }
208   else
209     GCC_BAD ("malformed %<#pragma pack%> - ignored");
210
211   if (pragma_lex (&x) != CPP_EOF)
212     warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
213
214   if (flag_pack_struct)
215     GCC_BAD ("#pragma pack has no effect with -fpack-struct - ignored");
216
217   if (action != pop)
218     switch (align)
219       {
220       case 0:
221       case 1:
222       case 2:
223       case 4:
224       case 8:
225       case 16:
226         align *= BITS_PER_UNIT;
227         break;
228       case -1:
229         if (action == push)
230           {
231             align = maximum_field_alignment;
232             break;
233           }
234       default:
235         GCC_BAD2 ("alignment must be a small power of two, not %d", align);
236       }
237
238   switch (action)
239     {
240     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
241     case push:  push_alignment (align, id);    break;
242     case pop:   pop_alignment (id);            break;
243     }
244 }
245 #endif  /* HANDLE_PRAGMA_PACK */
246
247 struct def_pragma_macro_value GTY(())
248 {
249   struct def_pragma_macro_value *prev;
250   cpp_macro *value;
251 };
252
253 struct def_pragma_macro GTY(())
254 {
255   hashval_t hash;
256   const char *name;
257   struct def_pragma_macro_value value;
258 };
259
260 static GTY((param_is (struct def_pragma_macro))) htab_t pushed_macro_table;
261
262 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
263 /* Hash table control functions for pushed_macro_table.  */
264 static hashval_t
265 dpm_hash (const void *p)
266 {
267   return ((const struct def_pragma_macro *)p)->hash;
268 }
269
270 static int
271 dpm_eq (const void *pa, const void *pb)
272 {
273   const struct def_pragma_macro *a = pa, *b = pb;
274   return a->hash == b->hash && strcmp (a->name, b->name) == 0;
275 }
276
277 /* #pragma push_macro("MACRO_NAME")
278    #pragma pop_macro("MACRO_NAME") */
279
280 static void
281 handle_pragma_push_macro (cpp_reader *reader)
282 {
283   tree x, id = 0;
284   enum cpp_ttype token;
285   struct def_pragma_macro dummy, *c;
286   const char *macroname;
287   void **slot;
288
289   if (pragma_lex (&x) != CPP_OPEN_PAREN)
290     GCC_BAD ("missing %<(%> after %<#pragma push_macro%> - ignored");
291
292   token = pragma_lex (&id);
293
294   /* Silently ignore */
295   if (token == CPP_CLOSE_PAREN)
296     return;
297   if (token != CPP_STRING)
298     GCC_BAD ("invalid constant in %<#pragma push_macro%> - ignored");
299
300   if (pragma_lex (&x) != CPP_CLOSE_PAREN)
301     GCC_BAD ("missing %<)%> after %<#pragma push_macro%> - ignored");
302
303   if (pragma_lex (&x) != CPP_EOF)
304     warning (OPT_Wpragmas, "junk at end of %<#pragma push_macro%>");
305
306   /* Check for empty string, and silently ignore.  */
307   if (TREE_STRING_LENGTH (id) < 1)
308     return;
309   macroname = TREE_STRING_POINTER (id);
310
311   if (pushed_macro_table == NULL)
312     pushed_macro_table = htab_create_ggc (15, dpm_hash, dpm_eq, 0);
313
314   dummy.hash = htab_hash_string (macroname);
315   dummy.name = macroname;
316   slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
317                                    dummy.hash, INSERT);
318   c = *slot;
319   if (c == NULL)
320     {
321       *slot = c = ggc_alloc (sizeof (struct def_pragma_macro));
322       c->hash = dummy.hash;
323       c->name = ggc_alloc_string (macroname, TREE_STRING_LENGTH (id) - 1);
324       c->value.prev = NULL;
325     }
326   else
327     {
328       struct def_pragma_macro_value *v;
329       v = ggc_alloc (sizeof (struct def_pragma_macro_value));
330       *v = c->value;
331       c->value.prev = v;
332     }
333
334   c->value.value = cpp_push_definition (reader, macroname);
335 }
336
337 static void
338 handle_pragma_pop_macro (cpp_reader *reader)
339 {
340   tree x, id = 0;
341   enum cpp_ttype token;
342   struct def_pragma_macro dummy, *c;
343   const char *macroname;
344   void **slot = NULL;
345
346   if (pragma_lex (&x) != CPP_OPEN_PAREN)
347     GCC_BAD ("missing %<(%> after %<#pragma pop_macro%> - ignored");
348
349   token = pragma_lex (&id);
350
351   /* Silently ignore */
352   if (token == CPP_CLOSE_PAREN)
353     return;
354   if (token != CPP_STRING)
355     GCC_BAD ("invalid constant in %<#pragma pop_macro%> - ignored");
356
357   if (pragma_lex (&x) != CPP_CLOSE_PAREN)
358     GCC_BAD ("missing %<)%> after %<#pragma pop_macro%> - ignored");
359
360   if (pragma_lex (&x) != CPP_EOF)
361     warning (OPT_Wpragmas, "junk at end of %<#pragma pop_macro%>");
362
363   /* Check for empty string, and silently ignore.  */
364   if (TREE_STRING_LENGTH (id) < 1)
365     return;
366   macroname = TREE_STRING_POINTER (id);
367
368   dummy.hash = htab_hash_string (macroname);
369   dummy.name = macroname;
370   if (pushed_macro_table)
371     slot = htab_find_slot_with_hash (pushed_macro_table, &dummy,
372                                      dummy.hash, NO_INSERT);
373   if (slot == NULL)
374     return;
375   c = *slot;
376
377   cpp_pop_definition (reader, c->name, c->value.value);
378
379   if (c->value.prev)
380     c->value = *c->value.prev;
381   else
382     htab_clear_slot (pushed_macro_table, slot);
383 }
384 #endif /* HANDLE_PRAGMA_PUSH_POP_MACRO */
385
386 static GTY(()) tree pending_weaks;
387
388 #ifdef HANDLE_PRAGMA_WEAK
389 static void apply_pragma_weak (tree, tree);
390 static void handle_pragma_weak (cpp_reader *);
391
392 static void
393 apply_pragma_weak (tree decl, tree value)
394 {
395   if (value)
396     {
397       value = build_string (IDENTIFIER_LENGTH (value),
398                             IDENTIFIER_POINTER (value));
399       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
400                                                build_tree_list (NULL, value)),
401                        0);
402     }
403
404   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
405       && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma.  */
406       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
407     warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
408              "results in unspecified behavior", decl);
409
410   declare_weak (decl);
411 }
412
413 void
414 maybe_apply_pragma_weak (tree decl)
415 {
416   tree *p, t, id;
417
418   /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed.  */
419
420   /* No weak symbols pending, take the short-cut.  */
421   if (!pending_weaks)
422     return;
423   /* If it's not visible outside this file, it doesn't matter whether
424      it's weak.  */
425   if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
426     return;
427   /* If it's not a function or a variable, it can't be weak.
428      FIXME: what kinds of things are visible outside this file but
429      aren't functions or variables?   Should this be an assert instead?  */
430   if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
431     return;
432
433   id = DECL_ASSEMBLER_NAME (decl);
434
435   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
436     if (id == TREE_PURPOSE (t))
437       {
438         apply_pragma_weak (decl, TREE_VALUE (t));
439         *p = TREE_CHAIN (t);
440         break;
441       }
442 }
443
444 /* Process all "#pragma weak A = B" directives where we have not seen
445    a decl for A.  */
446 void
447 maybe_apply_pending_pragma_weaks (void)
448 {
449   tree *p, t, alias_id, id, decl, *next;
450
451   for (p = &pending_weaks; (t = *p) ; p = next)
452     {
453       next = &TREE_CHAIN (t);
454       alias_id = TREE_PURPOSE (t);
455       id = TREE_VALUE (t);
456
457       if (TREE_VALUE (t) == NULL)
458         continue;
459
460       decl = build_decl (FUNCTION_DECL, alias_id, default_function_type);
461
462       DECL_ARTIFICIAL (decl) = 1;
463       TREE_PUBLIC (decl) = 1;
464       DECL_EXTERNAL (decl) = 1;
465       DECL_WEAK (decl) = 1;
466
467       assemble_alias (decl, id);
468     }
469 }
470
471 /* #pragma weak name [= value] */
472 static void
473 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
474 {
475   tree name, value, x, decl;
476   enum cpp_ttype t;
477
478   value = 0;
479
480   if (pragma_lex (&name) != CPP_NAME)
481     GCC_BAD ("malformed #pragma weak, ignored");
482   t = pragma_lex (&x);
483   if (t == CPP_EQ)
484     {
485       if (pragma_lex (&value) != CPP_NAME)
486         GCC_BAD ("malformed #pragma weak, ignored");
487       t = pragma_lex (&x);
488     }
489   if (t != CPP_EOF)
490     warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
491
492   decl = identifier_global_value (name);
493   if (decl && DECL_P (decl))
494     {
495       apply_pragma_weak (decl, value);
496       if (value)
497         assemble_alias (decl, value);
498     }
499   else
500     pending_weaks = tree_cons (name, value, pending_weaks);
501 }
502 #else
503 void
504 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
505 {
506 }
507
508 void
509 maybe_apply_pending_pragma_weaks (void)
510 {
511 }
512 #endif /* HANDLE_PRAGMA_WEAK */
513
514 /* GCC supports two #pragma directives for renaming the external
515    symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
516    compatibility with the Solaris and Tru64 system headers.  GCC also
517    has its own notation for this, __asm__("name") annotations.
518
519    Corner cases of these features and their interaction:
520
521    1) Both pragmas silently apply only to declarations with external
522       linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).  Asm labels
523       do not have this restriction.
524
525    2) In C++, both #pragmas silently apply only to extern "C" declarations.
526       Asm labels do not have this restriction.
527
528    3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
529       applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
530       new name is different, a warning issues and the name does not change.
531
532    4) The "source name" for #pragma redefine_extname is the DECL_NAME,
533       *not* the DECL_ASSEMBLER_NAME.
534
535    5) If #pragma extern_prefix is in effect and a declaration occurs
536       with an __asm__ name, the #pragma extern_prefix is silently
537       ignored for that declaration.
538
539    6) If #pragma extern_prefix and #pragma redefine_extname apply to
540       the same declaration, whichever triggered first wins, and a warning
541       is issued.  (We would like to have #pragma redefine_extname always
542       win, but it can appear either before or after the declaration, and
543       if it appears afterward, we have no way of knowing whether a modified
544       DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)  */
545
546 static GTY(()) tree pending_redefine_extname;
547
548 static void handle_pragma_redefine_extname (cpp_reader *);
549
550 /* #pragma redefine_extname oldname newname */
551 static void
552 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
553 {
554   tree oldname, newname, decl, x;
555   enum cpp_ttype t;
556
557   if (pragma_lex (&oldname) != CPP_NAME)
558     GCC_BAD ("malformed #pragma redefine_extname, ignored");
559   if (pragma_lex (&newname) != CPP_NAME)
560     GCC_BAD ("malformed #pragma redefine_extname, ignored");
561   t = pragma_lex (&x);
562   if (t != CPP_EOF)
563     warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
564
565   if (!flag_mudflap && !targetm.handle_pragma_redefine_extname)
566     {
567       if (warn_unknown_pragmas > in_system_header)
568         warning (OPT_Wunknown_pragmas,
569                  "#pragma redefine_extname not supported on this target");
570       return;
571     }
572
573   decl = identifier_global_value (oldname);
574   if (decl
575       && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
576       && (TREE_CODE (decl) == FUNCTION_DECL
577           || TREE_CODE (decl) == VAR_DECL)
578       && has_c_linkage (decl))
579     {
580       if (DECL_ASSEMBLER_NAME_SET_P (decl))
581         {
582           const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
583           name = targetm.strip_name_encoding (name);
584
585           if (strcmp (name, IDENTIFIER_POINTER (newname)))
586             warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
587                      "conflict with previous rename");
588         }
589       else
590         change_decl_assembler_name (decl, newname);
591     }
592   else
593     /* We have to add this to the rename list even if there's already
594        a global value that doesn't meet the above criteria, because in
595        C++ "struct foo {...};" puts "foo" in the current namespace but
596        does *not* conflict with a subsequent declaration of a function
597        or variable foo.  See g++.dg/other/pragma-re-2.C.  */
598     add_to_renaming_pragma_list (oldname, newname);
599 }
600
601 /* This is called from here and from ia64.c.  */
602 void
603 add_to_renaming_pragma_list (tree oldname, tree newname)
604 {
605   tree previous = purpose_member (oldname, pending_redefine_extname);
606   if (previous)
607     {
608       if (TREE_VALUE (previous) != newname)
609         warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
610                  "conflict with previous #pragma redefine_extname");
611       return;
612     }
613
614   pending_redefine_extname
615     = tree_cons (oldname, newname, pending_redefine_extname);
616 }
617
618 static GTY(()) tree pragma_extern_prefix;
619
620 /* #pragma extern_prefix "prefix" */
621 static void
622 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
623 {
624   tree prefix, x;
625   enum cpp_ttype t;
626
627   if (pragma_lex (&prefix) != CPP_STRING)
628     GCC_BAD ("malformed #pragma extern_prefix, ignored");
629   t = pragma_lex (&x);
630   if (t != CPP_EOF)
631     warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
632
633   if (targetm.handle_pragma_extern_prefix)
634     /* Note that the length includes the null terminator.  */
635     pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
636   else if (warn_unknown_pragmas > in_system_header)
637     warning (OPT_Wunknown_pragmas,
638              "#pragma extern_prefix not supported on this target");
639 }
640
641 /* Hook from the front ends to apply the results of one of the preceding
642    pragmas that rename variables.  */
643
644 tree
645 maybe_apply_renaming_pragma (tree decl, tree asmname)
646 {
647   tree *p, t;
648
649   /* The renaming pragmas are only applied to declarations with
650      external linkage.  */
651   if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
652       || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
653       || !has_c_linkage (decl))
654     return asmname;
655
656   /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
657      but we may warn about a rename that conflicts.  */
658   if (DECL_ASSEMBLER_NAME_SET_P (decl))
659     {
660       const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
661       oldname = targetm.strip_name_encoding (oldname);
662
663       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
664           warning (OPT_Wpragmas, "asm declaration ignored due to "
665                    "conflict with previous rename");
666
667       /* Take any pending redefine_extname off the list.  */
668       for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
669         if (DECL_NAME (decl) == TREE_PURPOSE (t))
670           {
671             /* Only warn if there is a conflict.  */
672             if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
673               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
674                        "conflict with previous rename");
675
676             *p = TREE_CHAIN (t);
677             break;
678           }
679       return 0;
680     }
681
682   /* Find out if we have a pending #pragma redefine_extname.  */
683   for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
684     if (DECL_NAME (decl) == TREE_PURPOSE (t))
685       {
686         tree newname = TREE_VALUE (t);
687         *p = TREE_CHAIN (t);
688
689         /* If we already have an asmname, #pragma redefine_extname is
690            ignored (with a warning if it conflicts).  */
691         if (asmname)
692           {
693             if (strcmp (TREE_STRING_POINTER (asmname),
694                         IDENTIFIER_POINTER (newname)) != 0)
695               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
696                        "conflict with __asm__ declaration");
697             return asmname;
698           }
699
700         /* Otherwise we use what we've got; #pragma extern_prefix is
701            silently ignored.  */
702         return build_string (IDENTIFIER_LENGTH (newname),
703                              IDENTIFIER_POINTER (newname));
704       }
705
706   /* If we've got an asmname, #pragma extern_prefix is silently ignored.  */
707   if (asmname)
708     return asmname;
709
710   /* If #pragma extern_prefix is in effect, apply it.  */
711   if (pragma_extern_prefix)
712     {
713       const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
714       size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
715
716       const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
717       size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
718
719       char *newname = (char *) alloca (plen + ilen + 1);
720
721       memcpy (newname,        prefix, plen);
722       memcpy (newname + plen, id, ilen + 1);
723
724       return build_string (plen + ilen, newname);
725     }
726
727   /* Nada.  */
728   return 0;
729 }
730
731
732 #ifdef HANDLE_PRAGMA_VISIBILITY
733 static void handle_pragma_visibility (cpp_reader *);
734
735 typedef enum symbol_visibility visibility;
736 DEF_VEC_I (visibility);
737 DEF_VEC_ALLOC_I (visibility, heap);
738 static VEC (visibility, heap) *visstack;
739
740 /* Push the visibility indicated by STR onto the top of the #pragma
741    visibility stack.  */
742
743 void
744 push_visibility (const char *str)
745 {
746   VEC_safe_push (visibility, heap, visstack,
747                  default_visibility);
748   if (!strcmp (str, "default"))
749     default_visibility = VISIBILITY_DEFAULT;
750   else if (!strcmp (str, "internal"))
751     default_visibility = VISIBILITY_INTERNAL;
752   else if (!strcmp (str, "hidden"))
753     default_visibility = VISIBILITY_HIDDEN;
754   else if (!strcmp (str, "protected"))
755     default_visibility = VISIBILITY_PROTECTED;
756   else
757     GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
758   visibility_options.inpragma = 1;
759 }
760
761 /* Pop a level of the #pragma visibility stack.  */
762
763 void
764 pop_visibility (void)
765 {
766   default_visibility = VEC_pop (visibility, visstack);
767   visibility_options.inpragma
768     = VEC_length (visibility, visstack) != 0;
769 }
770
771 /* Sets the default visibility for symbols to something other than that
772    specified on the command line.  */
773
774 static void
775 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
776 {
777   /* Form is #pragma GCC visibility push(hidden)|pop */
778   tree x;
779   enum cpp_ttype token;
780   enum { bad, push, pop } action = bad;
781
782   token = pragma_lex (&x);
783   if (token == CPP_NAME)
784     {
785       const char *op = IDENTIFIER_POINTER (x);
786       if (!strcmp (op, "push"))
787         action = push;
788       else if (!strcmp (op, "pop"))
789         action = pop;
790     }
791   if (bad == action)
792     GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
793   else
794     {
795       if (pop == action)
796         {
797           if (!VEC_length (visibility, visstack))
798             GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
799           else
800             pop_visibility ();
801         }
802       else
803         {
804           if (pragma_lex (&x) != CPP_OPEN_PAREN)
805             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
806           token = pragma_lex (&x);
807           if (token != CPP_NAME)
808             GCC_BAD ("malformed #pragma GCC visibility push");
809           else
810             push_visibility (IDENTIFIER_POINTER (x));
811           if (pragma_lex (&x) != CPP_CLOSE_PAREN)
812             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
813         }
814     }
815   if (pragma_lex (&x) != CPP_EOF)
816     warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
817 }
818
819 #endif
820
821 static void
822 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
823 {
824   const char *kind_string, *option_string;
825   unsigned int option_index;
826   enum cpp_ttype token;
827   diagnostic_t kind;
828   tree x;
829
830   if (cfun)
831     {
832       error ("#pragma GCC diagnostic not allowed inside functions");
833       return;
834     }
835
836   token = pragma_lex (&x);
837   if (token != CPP_NAME)
838     GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
839   kind_string = IDENTIFIER_POINTER (x);
840   if (strcmp (kind_string, "error") == 0)
841     kind = DK_ERROR;
842   else if (strcmp (kind_string, "warning") == 0)
843     kind = DK_WARNING;
844   else if (strcmp (kind_string, "ignored") == 0)
845     kind = DK_IGNORED;
846   else
847     GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
848
849   token = pragma_lex (&x);
850   if (token != CPP_STRING)
851     GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
852   option_string = TREE_STRING_POINTER (x);
853   for (option_index = 0; option_index < cl_options_count; option_index++)
854     if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
855       {
856         /* This overrides -Werror, for example.  */
857         diagnostic_classify_diagnostic (global_dc, option_index, kind);
858         /* This makes sure the option is enabled, like -Wfoo would do.  */
859         if (cl_options[option_index].var_type == CLVC_BOOLEAN
860             && cl_options[option_index].flag_var
861             && kind != DK_IGNORED)
862             *(int *) cl_options[option_index].flag_var = 1;
863         return;
864       }
865   GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
866 }
867
868 /* A vector of registered pragma callbacks.  */
869
870 DEF_VEC_O (pragma_handler);
871 DEF_VEC_ALLOC_O (pragma_handler, heap);
872
873 static VEC(pragma_handler, heap) *registered_pragmas;
874
875 typedef struct
876 {
877   const char *space;
878   const char *name;
879 } pragma_ns_name;
880
881 DEF_VEC_O (pragma_ns_name);
882 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
883
884 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
885
886 struct omp_pragma_def { const char *name; unsigned int id; };
887 static const struct omp_pragma_def omp_pragmas[] = {
888   { "atomic", PRAGMA_OMP_ATOMIC },
889   { "barrier", PRAGMA_OMP_BARRIER },
890   { "critical", PRAGMA_OMP_CRITICAL },
891   { "flush", PRAGMA_OMP_FLUSH },
892   { "for", PRAGMA_OMP_FOR },
893   { "master", PRAGMA_OMP_MASTER },
894   { "ordered", PRAGMA_OMP_ORDERED },
895   { "parallel", PRAGMA_OMP_PARALLEL },
896   { "section", PRAGMA_OMP_SECTION },
897   { "sections", PRAGMA_OMP_SECTIONS },
898   { "single", PRAGMA_OMP_SINGLE },
899   { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
900 };
901
902 void
903 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
904 {
905   const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
906   int i;
907
908   for (i = 0; i < n_omp_pragmas; ++i)
909     if (omp_pragmas[i].id == id)
910       {
911         *space = "omp";
912         *name = omp_pragmas[i].name;
913         return;
914       }
915
916   if (id >= PRAGMA_FIRST_EXTERNAL
917       && (id < PRAGMA_FIRST_EXTERNAL
918           + VEC_length (pragma_ns_name, registered_pp_pragmas)))
919     {
920       *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
921                           id - PRAGMA_FIRST_EXTERNAL)->space;
922       *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
923                          id - PRAGMA_FIRST_EXTERNAL)->name;
924       return;
925     }
926
927   gcc_unreachable ();
928 }
929
930 /* Front-end wrappers for pragma registration to avoid dragging
931    cpplib.h in almost everywhere.  */
932
933 static void
934 c_register_pragma_1 (const char *space, const char *name,
935                      pragma_handler handler, bool allow_expansion)
936 {
937   unsigned id;
938
939   if (flag_preprocess_only)
940     {
941       pragma_ns_name ns_name;
942
943       if (!allow_expansion)
944         return;
945
946       ns_name.space = space;
947       ns_name.name = name;
948       VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
949       id = VEC_length (pragma_ns_name, registered_pp_pragmas);
950       id += PRAGMA_FIRST_EXTERNAL - 1;
951     }
952   else
953     {
954       VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
955       id = VEC_length (pragma_handler, registered_pragmas);
956       id += PRAGMA_FIRST_EXTERNAL - 1;
957
958       /* The C++ front end allocates 6 bits in cp_token; the C front end
959          allocates 7 bits in c_token.  At present this is sufficient.  */
960       gcc_assert (id < 64);
961     }
962
963   cpp_register_deferred_pragma (parse_in, space, name, id,
964                                 allow_expansion, false);
965 }
966
967 void
968 c_register_pragma (const char *space, const char *name, pragma_handler handler)
969 {
970   c_register_pragma_1 (space, name, handler, false);
971 }
972
973 void
974 c_register_pragma_with_expansion (const char *space, const char *name,
975                                   pragma_handler handler)
976 {
977   c_register_pragma_1 (space, name, handler, true);
978 }
979
980 void
981 c_invoke_pragma_handler (unsigned int id)
982 {
983   pragma_handler handler;
984
985   id -= PRAGMA_FIRST_EXTERNAL;
986   handler = *VEC_index (pragma_handler, registered_pragmas, id);
987
988   handler (parse_in);
989 }
990
991 /* Set up front-end pragmas.  */
992 void
993 init_pragma (void)
994 {
995   if (flag_openmp)
996     {
997       const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
998       int i;
999
1000       for (i = 0; i < n_omp_pragmas; ++i)
1001         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1002                                       omp_pragmas[i].id, true, true);
1003     }
1004
1005   if (!flag_preprocess_only)
1006     cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1007                                   PRAGMA_GCC_PCH_PREPROCESS, false, false);
1008
1009 #ifdef HANDLE_PRAGMA_PACK
1010 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1011   c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1012 #else
1013   c_register_pragma (0, "pack", handle_pragma_pack);
1014 #endif
1015 #endif
1016 #ifdef HANDLE_PRAGMA_PUSH_POP_MACRO
1017   c_register_pragma (0 ,"push_macro", handle_pragma_push_macro);
1018   c_register_pragma (0 ,"pop_macro", handle_pragma_pop_macro);
1019 #endif
1020 #ifdef HANDLE_PRAGMA_WEAK
1021   c_register_pragma (0, "weak", handle_pragma_weak);
1022 #endif
1023 #ifdef HANDLE_PRAGMA_VISIBILITY
1024   c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1025 #endif
1026
1027   c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1028
1029   c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1030   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1031
1032 #ifdef REGISTER_TARGET_PRAGMAS
1033   REGISTER_TARGET_PRAGMAS ();
1034 #endif
1035 }
1036
1037 #include "gt-c-pragma.h"