OSDN Git Service

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