OSDN Git Service

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