OSDN Git Service

Daily bump.
[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 #include "plugin.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 GTY(()) align_stack {
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, %E) encountered without matching #pragma pack(push, %E)"
117                  , id, 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 %qE for %<#pragma pack%> - ignored", x);
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 static GTY(()) tree pending_weaks;
248
249 #ifdef HANDLE_PRAGMA_WEAK
250 static void apply_pragma_weak (tree, tree);
251 static void handle_pragma_weak (cpp_reader *);
252
253 static void
254 apply_pragma_weak (tree decl, tree value)
255 {
256   if (value)
257     {
258       value = build_string (IDENTIFIER_LENGTH (value),
259                             IDENTIFIER_POINTER (value));
260       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
261                                                build_tree_list (NULL, value)),
262                        0);
263     }
264
265   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
266       && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma.  */
267       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
268     warning (OPT_Wpragmas, "applying #pragma weak %q+D after first use "
269              "results in unspecified behavior", decl);
270
271   declare_weak (decl);
272 }
273
274 void
275 maybe_apply_pragma_weak (tree decl)
276 {
277   tree *p, t, id;
278
279   /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed.  */
280
281   /* No weak symbols pending, take the short-cut.  */
282   if (!pending_weaks)
283     return;
284   /* If it's not visible outside this file, it doesn't matter whether
285      it's weak.  */
286   if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
287     return;
288   /* If it's not a function or a variable, it can't be weak.
289      FIXME: what kinds of things are visible outside this file but
290      aren't functions or variables?   Should this be an assert instead?  */
291   if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
292     return;
293
294   id = DECL_ASSEMBLER_NAME (decl);
295
296   for (p = &pending_weaks; (t = *p) ; p = &TREE_CHAIN (t))
297     if (id == TREE_PURPOSE (t))
298       {
299         apply_pragma_weak (decl, TREE_VALUE (t));
300         *p = TREE_CHAIN (t);
301         break;
302       }
303 }
304
305 /* Process all "#pragma weak A = B" directives where we have not seen
306    a decl for A.  */
307 void
308 maybe_apply_pending_pragma_weaks (void)
309 {
310   tree *p, t, alias_id, id, decl, *next;
311
312   for (p = &pending_weaks; (t = *p) ; p = next)
313     {
314       next = &TREE_CHAIN (t);
315       alias_id = TREE_PURPOSE (t);
316       id = TREE_VALUE (t);
317
318       if (TREE_VALUE (t) == NULL)
319         continue;
320
321       decl = build_decl (UNKNOWN_LOCATION,
322                          FUNCTION_DECL, alias_id, default_function_type);
323
324       DECL_ARTIFICIAL (decl) = 1;
325       TREE_PUBLIC (decl) = 1;
326       DECL_EXTERNAL (decl) = 1;
327       DECL_WEAK (decl) = 1;
328
329       assemble_alias (decl, id);
330     }
331 }
332
333 /* #pragma weak name [= value] */
334 static void
335 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
336 {
337   tree name, value, x, decl;
338   enum cpp_ttype t;
339
340   value = 0;
341
342   if (pragma_lex (&name) != CPP_NAME)
343     GCC_BAD ("malformed #pragma weak, ignored");
344   t = pragma_lex (&x);
345   if (t == CPP_EQ)
346     {
347       if (pragma_lex (&value) != CPP_NAME)
348         GCC_BAD ("malformed #pragma weak, ignored");
349       t = pragma_lex (&x);
350     }
351   if (t != CPP_EOF)
352     warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
353
354   decl = identifier_global_value (name);
355   if (decl && DECL_P (decl))
356     {
357       apply_pragma_weak (decl, value);
358       if (value)
359         assemble_alias (decl, value);
360     }
361   else
362     pending_weaks = tree_cons (name, value, pending_weaks);
363 }
364 #else
365 void
366 maybe_apply_pragma_weak (tree ARG_UNUSED (decl))
367 {
368 }
369
370 void
371 maybe_apply_pending_pragma_weaks (void)
372 {
373 }
374 #endif /* HANDLE_PRAGMA_WEAK */
375
376 /* GCC supports two #pragma directives for renaming the external
377    symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
378    compatibility with the Solaris and Tru64 system headers.  GCC also
379    has its own notation for this, __asm__("name") annotations.
380
381    Corner cases of these features and their interaction:
382
383    1) Both pragmas silently apply only to declarations with external
384       linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).  Asm labels
385       do not have this restriction.
386
387    2) In C++, both #pragmas silently apply only to extern "C" declarations.
388       Asm labels do not have this restriction.
389
390    3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
391       applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
392       new name is different, a warning issues and the name does not change.
393
394    4) The "source name" for #pragma redefine_extname is the DECL_NAME,
395       *not* the DECL_ASSEMBLER_NAME.
396
397    5) If #pragma extern_prefix is in effect and a declaration occurs
398       with an __asm__ name, the #pragma extern_prefix is silently
399       ignored for that declaration.
400
401    6) If #pragma extern_prefix and #pragma redefine_extname apply to
402       the same declaration, whichever triggered first wins, and a warning
403       is issued.  (We would like to have #pragma redefine_extname always
404       win, but it can appear either before or after the declaration, and
405       if it appears afterward, we have no way of knowing whether a modified
406       DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)  */
407
408 static GTY(()) tree pending_redefine_extname;
409
410 static void handle_pragma_redefine_extname (cpp_reader *);
411
412 /* #pragma redefine_extname oldname newname */
413 static void
414 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
415 {
416   tree oldname, newname, decl, x;
417   enum cpp_ttype t;
418
419   if (pragma_lex (&oldname) != CPP_NAME)
420     GCC_BAD ("malformed #pragma redefine_extname, ignored");
421   if (pragma_lex (&newname) != CPP_NAME)
422     GCC_BAD ("malformed #pragma redefine_extname, ignored");
423   t = pragma_lex (&x);
424   if (t != CPP_EOF)
425     warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
426
427   decl = identifier_global_value (oldname);
428   if (decl
429       && (TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
430       && (TREE_CODE (decl) == FUNCTION_DECL
431           || TREE_CODE (decl) == VAR_DECL)
432       && has_c_linkage (decl))
433     {
434       if (DECL_ASSEMBLER_NAME_SET_P (decl))
435         {
436           const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
437           name = targetm.strip_name_encoding (name);
438
439           if (strcmp (name, IDENTIFIER_POINTER (newname)))
440             warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
441                      "conflict with previous rename");
442         }
443       else
444         change_decl_assembler_name (decl, newname);
445     }
446   else
447     /* We have to add this to the rename list even if there's already
448        a global value that doesn't meet the above criteria, because in
449        C++ "struct foo {...};" puts "foo" in the current namespace but
450        does *not* conflict with a subsequent declaration of a function
451        or variable foo.  See g++.dg/other/pragma-re-2.C.  */
452     add_to_renaming_pragma_list (oldname, newname);
453 }
454
455 /* This is called from here and from ia64.c.  */
456 void
457 add_to_renaming_pragma_list (tree oldname, tree newname)
458 {
459   tree previous = purpose_member (oldname, pending_redefine_extname);
460   if (previous)
461     {
462       if (TREE_VALUE (previous) != newname)
463         warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
464                  "conflict with previous #pragma redefine_extname");
465       return;
466     }
467
468   pending_redefine_extname
469     = tree_cons (oldname, newname, pending_redefine_extname);
470 }
471
472 static GTY(()) tree pragma_extern_prefix;
473
474 /* #pragma extern_prefix "prefix" */
475 static void
476 handle_pragma_extern_prefix (cpp_reader * ARG_UNUSED (dummy))
477 {
478   tree prefix, x;
479   enum cpp_ttype t;
480
481   if (pragma_lex (&prefix) != CPP_STRING)
482     GCC_BAD ("malformed #pragma extern_prefix, ignored");
483   t = pragma_lex (&x);
484   if (t != CPP_EOF)
485     warning (OPT_Wpragmas, "junk at end of %<#pragma extern_prefix%>");
486
487   if (targetm.handle_pragma_extern_prefix)
488     /* Note that the length includes the null terminator.  */
489     pragma_extern_prefix = (TREE_STRING_LENGTH (prefix) > 1 ? prefix : NULL);
490   else if (warn_unknown_pragmas > in_system_header)
491     warning (OPT_Wunknown_pragmas,
492              "#pragma extern_prefix not supported on this target");
493 }
494
495 /* Hook from the front ends to apply the results of one of the preceding
496    pragmas that rename variables.  */
497
498 tree
499 maybe_apply_renaming_pragma (tree decl, tree asmname)
500 {
501   tree *p, t;
502
503   /* The renaming pragmas are only applied to declarations with
504      external linkage.  */
505   if ((TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
506       || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
507       || !has_c_linkage (decl))
508     return asmname;
509
510   /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
511      but we may warn about a rename that conflicts.  */
512   if (DECL_ASSEMBLER_NAME_SET_P (decl))
513     {
514       const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
515       oldname = targetm.strip_name_encoding (oldname);
516
517       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
518           warning (OPT_Wpragmas, "asm declaration ignored due to "
519                    "conflict with previous rename");
520
521       /* Take any pending redefine_extname off the list.  */
522       for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
523         if (DECL_NAME (decl) == TREE_PURPOSE (t))
524           {
525             /* Only warn if there is a conflict.  */
526             if (strcmp (IDENTIFIER_POINTER (TREE_VALUE (t)), oldname))
527               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
528                        "conflict with previous rename");
529
530             *p = TREE_CHAIN (t);
531             break;
532           }
533       return 0;
534     }
535
536   /* Find out if we have a pending #pragma redefine_extname.  */
537   for (p = &pending_redefine_extname; (t = *p); p = &TREE_CHAIN (t))
538     if (DECL_NAME (decl) == TREE_PURPOSE (t))
539       {
540         tree newname = TREE_VALUE (t);
541         *p = TREE_CHAIN (t);
542
543         /* If we already have an asmname, #pragma redefine_extname is
544            ignored (with a warning if it conflicts).  */
545         if (asmname)
546           {
547             if (strcmp (TREE_STRING_POINTER (asmname),
548                         IDENTIFIER_POINTER (newname)) != 0)
549               warning (OPT_Wpragmas, "#pragma redefine_extname ignored due to "
550                        "conflict with __asm__ declaration");
551             return asmname;
552           }
553
554         /* Otherwise we use what we've got; #pragma extern_prefix is
555            silently ignored.  */
556         return build_string (IDENTIFIER_LENGTH (newname),
557                              IDENTIFIER_POINTER (newname));
558       }
559
560   /* If we've got an asmname, #pragma extern_prefix is silently ignored.  */
561   if (asmname)
562     return asmname;
563
564   /* If #pragma extern_prefix is in effect, apply it.  */
565   if (pragma_extern_prefix)
566     {
567       const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
568       size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
569
570       const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
571       size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
572
573       char *newname = (char *) alloca (plen + ilen + 1);
574
575       memcpy (newname,        prefix, plen);
576       memcpy (newname + plen, id, ilen + 1);
577
578       return build_string (plen + ilen, newname);
579     }
580
581   /* Nada.  */
582   return 0;
583 }
584
585
586 #ifdef HANDLE_PRAGMA_VISIBILITY
587 static void handle_pragma_visibility (cpp_reader *);
588
589 static VEC (int, heap) *visstack;
590
591 /* Push the visibility indicated by STR onto the top of the #pragma
592    visibility stack.  KIND is 0 for #pragma GCC visibility, 1 for
593    C++ namespace with visibility attribute and 2 for C++ builtin
594    ABI namespace.  push_visibility/pop_visibility calls must have
595    matching KIND, it is not allowed to push visibility using one
596    KIND and pop using a different one.  */
597
598 void
599 push_visibility (const char *str, int kind)
600 {
601   VEC_safe_push (int, heap, visstack,
602                  ((int) default_visibility) | (kind << 8));
603   if (!strcmp (str, "default"))
604     default_visibility = VISIBILITY_DEFAULT;
605   else if (!strcmp (str, "internal"))
606     default_visibility = VISIBILITY_INTERNAL;
607   else if (!strcmp (str, "hidden"))
608     default_visibility = VISIBILITY_HIDDEN;
609   else if (!strcmp (str, "protected"))
610     default_visibility = VISIBILITY_PROTECTED;
611   else
612     GCC_BAD ("#pragma GCC visibility push() must specify default, internal, hidden or protected");
613   visibility_options.inpragma = 1;
614 }
615
616 /* Pop a level of the #pragma visibility stack.  Return true if
617    successful.  */
618
619 bool
620 pop_visibility (int kind)
621 {
622   if (!VEC_length (int, visstack))
623     return false;
624   if ((VEC_last (int, visstack) >> 8) != kind)
625     return false;
626   default_visibility
627     = (enum symbol_visibility) (VEC_pop (int, visstack) & 0xff);
628   visibility_options.inpragma
629     = VEC_length (int, visstack) != 0;
630   return true;
631 }
632
633 /* Sets the default visibility for symbols to something other than that
634    specified on the command line.  */
635
636 static void
637 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
638 {
639   /* Form is #pragma GCC visibility push(hidden)|pop */
640   tree x;
641   enum cpp_ttype token;
642   enum { bad, push, pop } action = bad;
643
644   token = pragma_lex (&x);
645   if (token == CPP_NAME)
646     {
647       const char *op = IDENTIFIER_POINTER (x);
648       if (!strcmp (op, "push"))
649         action = push;
650       else if (!strcmp (op, "pop"))
651         action = pop;
652     }
653   if (bad == action)
654     GCC_BAD ("#pragma GCC visibility must be followed by push or pop");
655   else
656     {
657       if (pop == action)
658         {
659           if (! pop_visibility (0))
660             GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
661         }
662       else
663         {
664           if (pragma_lex (&x) != CPP_OPEN_PAREN)
665             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
666           token = pragma_lex (&x);
667           if (token != CPP_NAME)
668             GCC_BAD ("malformed #pragma GCC visibility push");
669           else
670             push_visibility (IDENTIFIER_POINTER (x), 0);
671           if (pragma_lex (&x) != CPP_CLOSE_PAREN)
672             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
673         }
674     }
675   if (pragma_lex (&x) != CPP_EOF)
676     warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
677 }
678
679 #endif
680
681 static void
682 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
683 {
684   const char *kind_string, *option_string;
685   unsigned int option_index;
686   enum cpp_ttype token;
687   diagnostic_t kind;
688   tree x;
689
690   if (cfun)
691     {
692       error ("#pragma GCC diagnostic not allowed inside functions");
693       return;
694     }
695
696   token = pragma_lex (&x);
697   if (token != CPP_NAME)
698     GCC_BAD ("missing [error|warning|ignored] after %<#pragma GCC diagnostic%>");
699   kind_string = IDENTIFIER_POINTER (x);
700   if (strcmp (kind_string, "error") == 0)
701     kind = DK_ERROR;
702   else if (strcmp (kind_string, "warning") == 0)
703     kind = DK_WARNING;
704   else if (strcmp (kind_string, "ignored") == 0)
705     kind = DK_IGNORED;
706   else
707     GCC_BAD ("expected [error|warning|ignored] after %<#pragma GCC diagnostic%>");
708
709   token = pragma_lex (&x);
710   if (token != CPP_STRING)
711     GCC_BAD ("missing option after %<#pragma GCC diagnostic%> kind");
712   option_string = TREE_STRING_POINTER (x);
713   for (option_index = 0; option_index < cl_options_count; option_index++)
714     if (strcmp (cl_options[option_index].opt_text, option_string) == 0)
715       {
716         /* This overrides -Werror, for example.  */
717         diagnostic_classify_diagnostic (global_dc, option_index, kind);
718         /* This makes sure the option is enabled, like -Wfoo would do.  */
719         if (cl_options[option_index].var_type == CLVC_BOOLEAN
720             && cl_options[option_index].flag_var
721             && kind != DK_IGNORED)
722             *(int *) cl_options[option_index].flag_var = 1;
723         return;
724       }
725   GCC_BAD ("unknown option after %<#pragma GCC diagnostic%> kind");
726 }
727
728 /*  Parse #pragma GCC target (xxx) to set target specific options.  */
729 static void
730 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
731 {
732   enum cpp_ttype token;
733   tree x;
734   bool close_paren_needed_p = false;
735
736   if (cfun)
737     {
738       error ("#pragma GCC option is not allowed inside functions");
739       return;
740     }
741
742   token = pragma_lex (&x);
743   if (token == CPP_OPEN_PAREN)
744     {
745       close_paren_needed_p = true;
746       token = pragma_lex (&x);
747     }
748
749   if (token != CPP_STRING)
750     {
751       GCC_BAD ("%<#pragma GCC option%> is not a string");
752       return;
753     }
754
755   /* Strings are user options.  */
756   else
757     {
758       tree args = NULL_TREE;
759
760       do
761         {
762           /* Build up the strings now as a tree linked list.  Skip empty
763              strings.  */
764           if (TREE_STRING_LENGTH (x) > 0)
765             args = tree_cons (NULL_TREE, x, args);
766
767           token = pragma_lex (&x);
768           while (token == CPP_COMMA)
769             token = pragma_lex (&x);
770         }
771       while (token == CPP_STRING);
772
773       if (close_paren_needed_p)
774         {
775           if (token == CPP_CLOSE_PAREN)
776             token = pragma_lex (&x);
777           else
778             GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
779                      "not have a final %<)%>.");
780         }
781
782       if (token != CPP_EOF)
783         {
784           error ("#pragma GCC target string... is badly formed");
785           return;
786         }
787
788       /* put arguments in the order the user typed them.  */
789       args = nreverse (args);
790
791       if (targetm.target_option.pragma_parse (args, NULL_TREE))
792         current_target_pragma = args;
793     }
794 }
795
796 /* Handle #pragma GCC optimize to set optimization options.  */
797 static void
798 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
799 {
800   enum cpp_ttype token;
801   tree x;
802   bool close_paren_needed_p = false;
803   tree optimization_previous_node = optimization_current_node;
804
805   if (cfun)
806     {
807       error ("#pragma GCC optimize is not allowed inside functions");
808       return;
809     }
810
811   token = pragma_lex (&x);
812   if (token == CPP_OPEN_PAREN)
813     {
814       close_paren_needed_p = true;
815       token = pragma_lex (&x);
816     }
817
818   if (token != CPP_STRING && token != CPP_NUMBER)
819     {
820       GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
821       return;
822     }
823
824   /* Strings/numbers are user options.  */
825   else
826     {
827       tree args = NULL_TREE;
828
829       do
830         {
831           /* Build up the numbers/strings now as a list.  */
832           if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
833             args = tree_cons (NULL_TREE, x, args);
834
835           token = pragma_lex (&x);
836           while (token == CPP_COMMA)
837             token = pragma_lex (&x);
838         }
839       while (token == CPP_STRING || token == CPP_NUMBER);
840
841       if (close_paren_needed_p)
842         {
843           if (token == CPP_CLOSE_PAREN)
844             token = pragma_lex (&x);
845           else
846             GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
847                      "not have a final %<)%>.");
848         }
849
850       if (token != CPP_EOF)
851         {
852           error ("#pragma GCC optimize string... is badly formed");
853           return;
854         }
855
856       /* put arguments in the order the user typed them.  */
857       args = nreverse (args);
858
859       parse_optimize_options (args, false);
860       current_optimize_pragma = chainon (current_optimize_pragma, args);
861       optimization_current_node = build_optimization_node ();
862       c_cpp_builtins_optimize_pragma (parse_in,
863                                       optimization_previous_node,
864                                       optimization_current_node);
865     }
866 }
867
868 /* Stack of the #pragma GCC options created with #pragma GCC push_option.  Save
869    both the binary representation of the options and the TREE_LIST of
870    strings that will be added to the function's attribute list.  */
871 typedef struct GTY(()) opt_stack {
872   struct opt_stack *prev;
873   tree target_binary;
874   tree target_strings;
875   tree optimize_binary;
876   tree optimize_strings;
877 } opt_stack;
878
879 static GTY(()) struct opt_stack * options_stack;
880
881 /* Handle #pragma GCC push_options to save the current target and optimization
882    options.  */
883
884 static void
885 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
886 {
887   enum cpp_ttype token;
888   tree x = 0;
889   opt_stack *p;
890
891   token = pragma_lex (&x);
892   if (token != CPP_EOF)
893     {
894       warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
895       return;
896     }
897
898   p = GGC_NEW (opt_stack);
899   p->prev = options_stack;
900   options_stack = p;
901
902   /* Save optimization and target flags in binary format.  */
903   p->optimize_binary = build_optimization_node ();
904   p->target_binary = build_target_option_node ();
905
906   /* Save optimization and target flags in string list format.  */
907   p->optimize_strings = copy_list (current_optimize_pragma);
908   p->target_strings = copy_list (current_target_pragma);
909 }
910
911 /* Handle #pragma GCC pop_options to restore the current target and
912    optimization options from a previous push_options.  */
913
914 static void
915 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
916 {
917   enum cpp_ttype token;
918   tree x = 0;
919   opt_stack *p;
920
921   token = pragma_lex (&x);
922   if (token != CPP_EOF)
923     {
924       warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
925       return;
926     }
927
928   if (! options_stack)
929     {
930       warning (OPT_Wpragmas,
931                "%<#pragma GCC pop_options%> without a corresponding "
932                "%<#pragma GCC push_options%>");
933       return;
934     }
935
936   p = options_stack;
937   options_stack = p->prev;
938
939   if (p->target_binary != target_option_current_node)
940     {
941       (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
942       target_option_current_node = p->target_binary;
943     }
944
945   if (p->optimize_binary != optimization_current_node)
946     {
947       tree old_optimize = optimization_current_node;
948       cl_optimization_restore (TREE_OPTIMIZATION (p->optimize_binary));
949       c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
950                                       p->optimize_binary);
951       optimization_current_node = p->optimize_binary;
952     }
953
954   current_target_pragma = p->target_strings;
955   current_optimize_pragma = p->optimize_strings;
956 }
957
958 /* Handle #pragma GCC reset_options to restore the current target and
959    optimization options to the original options used on the command line.  */
960
961 static void
962 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
963 {
964   enum cpp_ttype token;
965   tree x = 0;
966   tree new_optimize = optimization_default_node;
967   tree new_target = target_option_default_node;
968
969   token = pragma_lex (&x);
970   if (token != CPP_EOF)
971     {
972       warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
973       return;
974     }
975
976   if (new_target != target_option_current_node)
977     {
978       (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
979       target_option_current_node = new_target;
980     }
981
982   if (new_optimize != optimization_current_node)
983     {
984       tree old_optimize = optimization_current_node;
985       cl_optimization_restore (TREE_OPTIMIZATION (new_optimize));
986       c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
987       optimization_current_node = new_optimize;
988     }
989
990   current_target_pragma = NULL_TREE;
991   current_optimize_pragma = NULL_TREE;
992 }
993
994 /* Print a plain user-specified message.  */
995
996 static void
997 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
998 {
999   enum cpp_ttype token;
1000   tree x, message = 0;
1001
1002   token = pragma_lex (&x);
1003   if (token == CPP_OPEN_PAREN)
1004     {
1005       token = pragma_lex (&x);
1006       if (token == CPP_STRING)
1007         message = x;
1008       else
1009         GCC_BAD ("expected a string after %<#pragma message%>");
1010       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1011         GCC_BAD ("malformed %<#pragma message%>, ignored");
1012     }
1013   else if (token == CPP_STRING)
1014     message = x;
1015   else
1016     GCC_BAD ("expected a string after %<#pragma message%>");
1017
1018   gcc_assert (message);
1019
1020   if (pragma_lex (&x) != CPP_EOF)
1021     warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1022
1023   if (TREE_STRING_LENGTH (message) > 1)
1024     inform (input_location, "#pragma message: %s", TREE_STRING_POINTER (message));
1025 }
1026
1027 /* Mark whether the current location is valid for a STDC pragma.  */
1028
1029 static bool valid_location_for_stdc_pragma;
1030
1031 void
1032 mark_valid_location_for_stdc_pragma (bool flag)
1033 {
1034   valid_location_for_stdc_pragma = flag;
1035 }
1036
1037 /* Return true if the current location is valid for a STDC pragma.  */
1038
1039 bool
1040 valid_location_for_stdc_pragma_p (void)
1041 {
1042   return valid_location_for_stdc_pragma;
1043 }
1044
1045 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1046
1047 /* A STDC pragma must appear outside of external declarations or
1048    preceding all explicit declarations and statements inside a compound
1049    statement; its behavior is undefined if used in any other context.
1050    It takes a switch of ON, OFF, or DEFAULT.  */
1051
1052 static enum pragma_switch_t
1053 handle_stdc_pragma (const char *pname)
1054 {
1055   const char *arg;
1056   tree t;
1057   enum pragma_switch_t ret;
1058
1059   if (!valid_location_for_stdc_pragma_p ())
1060     {
1061       warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1062                pname);
1063       return PRAGMA_BAD;
1064     }
1065
1066   if (pragma_lex (&t) != CPP_NAME)
1067     {
1068       warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1069       return PRAGMA_BAD;
1070     }
1071
1072   arg = IDENTIFIER_POINTER (t);
1073
1074   if (!strcmp (arg, "ON"))
1075     ret = PRAGMA_ON;
1076   else if (!strcmp (arg, "OFF"))
1077     ret = PRAGMA_OFF;
1078   else if (!strcmp (arg, "DEFAULT"))
1079     ret = PRAGMA_DEFAULT;
1080   else
1081     {
1082       warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1083       return PRAGMA_BAD;
1084     }
1085
1086   if (pragma_lex (&t) != CPP_EOF)
1087     {
1088       warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1089       return PRAGMA_BAD;
1090     }
1091
1092   return ret;
1093 }
1094
1095 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1096    #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1097    #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1098
1099 static void
1100 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1101 {
1102   if (c_dialect_cxx ())
1103     {
1104       if (warn_unknown_pragmas > in_system_header)
1105         warning (OPT_Wunknown_pragmas,
1106                  "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1107                  " for C++");
1108       return;
1109     }
1110
1111   if (!targetm.decimal_float_supported_p ())
1112     {
1113       if (warn_unknown_pragmas > in_system_header)
1114         warning (OPT_Wunknown_pragmas,
1115                  "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1116                  " on this target");
1117       return;
1118     }
1119
1120   pedwarn (input_location, OPT_pedantic,
1121            "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1122
1123   switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1124     {
1125     case PRAGMA_ON:
1126       set_float_const_decimal64 ();
1127       break;
1128     case PRAGMA_OFF:
1129     case PRAGMA_DEFAULT:
1130       clear_float_const_decimal64 ();
1131       break;
1132     case PRAGMA_BAD:
1133       break;
1134     }
1135 }
1136
1137 /* A vector of registered pragma callbacks.  */
1138
1139 DEF_VEC_O (pragma_handler);
1140 DEF_VEC_ALLOC_O (pragma_handler, heap);
1141
1142 static VEC(pragma_handler, heap) *registered_pragmas;
1143
1144 typedef struct
1145 {
1146   const char *space;
1147   const char *name;
1148 } pragma_ns_name;
1149
1150 DEF_VEC_O (pragma_ns_name);
1151 DEF_VEC_ALLOC_O (pragma_ns_name, heap);
1152
1153 static VEC(pragma_ns_name, heap) *registered_pp_pragmas;
1154
1155 struct omp_pragma_def { const char *name; unsigned int id; };
1156 static const struct omp_pragma_def omp_pragmas[] = {
1157   { "atomic", PRAGMA_OMP_ATOMIC },
1158   { "barrier", PRAGMA_OMP_BARRIER },
1159   { "critical", PRAGMA_OMP_CRITICAL },
1160   { "flush", PRAGMA_OMP_FLUSH },
1161   { "for", PRAGMA_OMP_FOR },
1162   { "master", PRAGMA_OMP_MASTER },
1163   { "ordered", PRAGMA_OMP_ORDERED },
1164   { "parallel", PRAGMA_OMP_PARALLEL },
1165   { "section", PRAGMA_OMP_SECTION },
1166   { "sections", PRAGMA_OMP_SECTIONS },
1167   { "single", PRAGMA_OMP_SINGLE },
1168   { "task", PRAGMA_OMP_TASK },
1169   { "taskwait", PRAGMA_OMP_TASKWAIT },
1170   { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1171 };
1172
1173 void
1174 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1175 {
1176   const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1177   int i;
1178
1179   for (i = 0; i < n_omp_pragmas; ++i)
1180     if (omp_pragmas[i].id == id)
1181       {
1182         *space = "omp";
1183         *name = omp_pragmas[i].name;
1184         return;
1185       }
1186
1187   if (id >= PRAGMA_FIRST_EXTERNAL
1188       && (id < PRAGMA_FIRST_EXTERNAL
1189           + VEC_length (pragma_ns_name, registered_pp_pragmas)))
1190     {
1191       *space = VEC_index (pragma_ns_name, registered_pp_pragmas,
1192                           id - PRAGMA_FIRST_EXTERNAL)->space;
1193       *name = VEC_index (pragma_ns_name, registered_pp_pragmas,
1194                          id - PRAGMA_FIRST_EXTERNAL)->name;
1195       return;
1196     }
1197
1198   gcc_unreachable ();
1199 }
1200
1201 /* Front-end wrappers for pragma registration to avoid dragging
1202    cpplib.h in almost everywhere.  */
1203
1204 static void
1205 c_register_pragma_1 (const char *space, const char *name,
1206                      pragma_handler handler, bool allow_expansion)
1207 {
1208   unsigned id;
1209
1210   if (flag_preprocess_only)
1211     {
1212       pragma_ns_name ns_name;
1213
1214       if (!allow_expansion)
1215         return;
1216
1217       ns_name.space = space;
1218       ns_name.name = name;
1219       VEC_safe_push (pragma_ns_name, heap, registered_pp_pragmas, &ns_name);
1220       id = VEC_length (pragma_ns_name, registered_pp_pragmas);
1221       id += PRAGMA_FIRST_EXTERNAL - 1;
1222     }
1223   else
1224     {
1225       VEC_safe_push (pragma_handler, heap, registered_pragmas, &handler);
1226       id = VEC_length (pragma_handler, registered_pragmas);
1227       id += PRAGMA_FIRST_EXTERNAL - 1;
1228
1229       /* The C++ front end allocates 6 bits in cp_token; the C front end
1230          allocates 7 bits in c_token.  At present this is sufficient.  */
1231       gcc_assert (id < 64);
1232     }
1233
1234   cpp_register_deferred_pragma (parse_in, space, name, id,
1235                                 allow_expansion, false);
1236 }
1237
1238 void
1239 c_register_pragma (const char *space, const char *name, pragma_handler handler)
1240 {
1241   c_register_pragma_1 (space, name, handler, false);
1242 }
1243
1244 void
1245 c_register_pragma_with_expansion (const char *space, const char *name,
1246                                   pragma_handler handler)
1247 {
1248   c_register_pragma_1 (space, name, handler, true);
1249 }
1250
1251 void
1252 c_invoke_pragma_handler (unsigned int id)
1253 {
1254   pragma_handler handler;
1255
1256   id -= PRAGMA_FIRST_EXTERNAL;
1257   handler = *VEC_index (pragma_handler, registered_pragmas, id);
1258
1259   handler (parse_in);
1260 }
1261
1262 /* Set up front-end pragmas.  */
1263 void
1264 init_pragma (void)
1265 {
1266   if (flag_openmp)
1267     {
1268       const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1269       int i;
1270
1271       for (i = 0; i < n_omp_pragmas; ++i)
1272         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1273                                       omp_pragmas[i].id, true, true);
1274     }
1275
1276   if (!flag_preprocess_only)
1277     cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1278                                   PRAGMA_GCC_PCH_PREPROCESS, false, false);
1279
1280 #ifdef HANDLE_PRAGMA_PACK
1281 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1282   c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1283 #else
1284   c_register_pragma (0, "pack", handle_pragma_pack);
1285 #endif
1286 #endif
1287 #ifdef HANDLE_PRAGMA_WEAK
1288   c_register_pragma (0, "weak", handle_pragma_weak);
1289 #endif
1290 #ifdef HANDLE_PRAGMA_VISIBILITY
1291   c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1292 #endif
1293
1294   c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1295   c_register_pragma ("GCC", "target", handle_pragma_target);
1296   c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1297   c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1298   c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1299   c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1300
1301   c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1302                      handle_pragma_float_const_decimal64);
1303
1304   c_register_pragma_with_expansion (0, "redefine_extname", handle_pragma_redefine_extname);
1305   c_register_pragma (0, "extern_prefix", handle_pragma_extern_prefix);
1306
1307   c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1308
1309 #ifdef REGISTER_TARGET_PRAGMAS
1310   REGISTER_TARGET_PRAGMAS ();
1311 #endif
1312
1313   /* Allow plugins to register their own pragmas. */
1314   invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1315 }
1316
1317 #include "gt-c-pragma.h"