OSDN Git Service

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