OSDN Git Service

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