OSDN Git Service

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