[Ada] Various typo fixes and reformatting of comments
[platform/upstream/gcc.git] / gcc / c-family / c-pragma.c
1 /* Handle #pragma, system V.4 style.  Supports #pragma weak and #pragma pack.
2    Copyright (C) 1992-2020 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "target.h"
24 #include "function.h"           /* For cfun.  */
25 #include "c-common.h"
26 #include "memmodel.h"
27 #include "tm_p.h"               /* For REGISTER_TARGET_PRAGMAS.  */
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "diagnostic.h"
31 #include "attribs.h"
32 #include "varasm.h"
33 #include "c-pragma.h"
34 #include "opts.h"
35 #include "plugin.h"
36 #include "opt-suggestions.h"
37
38 #define GCC_BAD(gmsgid) \
39   do { warning (OPT_Wpragmas, gmsgid); return; } while (0)
40 #define GCC_BAD2(gmsgid, arg) \
41   do { warning (OPT_Wpragmas, gmsgid, arg); return; } while (0)
42
43 struct GTY(()) align_stack {
44   int                  alignment;
45   tree                 id;
46   struct align_stack * prev;
47 };
48
49 static GTY(()) struct align_stack * alignment_stack;
50
51 static void handle_pragma_pack (cpp_reader *);
52
53 /* If we have a "global" #pragma pack(<n>) in effect when the first
54    #pragma pack(push,<n>) is encountered, this stores the value of
55    maximum_field_alignment in effect.  When the final pop_alignment()
56    happens, we restore the value to this, not to a value of 0 for
57    maximum_field_alignment.  Value is in bits.  */
58 static int default_alignment;
59 #define SET_GLOBAL_ALIGNMENT(ALIGN) (maximum_field_alignment = *(alignment_stack == NULL \
60         ? &default_alignment \
61         : &alignment_stack->alignment) = (ALIGN))
62
63 static void push_alignment (int, tree);
64 static void pop_alignment (tree);
65
66 /* Push an alignment value onto the stack.  */
67 static void
68 push_alignment (int alignment, tree id)
69 {
70   align_stack * entry = ggc_alloc<align_stack> ();
71
72   entry->alignment  = alignment;
73   entry->id         = id;
74   entry->prev       = alignment_stack;
75
76   /* The current value of maximum_field_alignment is not necessarily
77      0 since there may be a #pragma pack(<n>) in effect; remember it
78      so that we can restore it after the final #pragma pop().  */
79   if (alignment_stack == NULL)
80     default_alignment = maximum_field_alignment;
81
82   alignment_stack = entry;
83
84   maximum_field_alignment = alignment;
85 }
86
87 /* Undo a push of an alignment onto the stack.  */
88 static void
89 pop_alignment (tree id)
90 {
91   align_stack * entry;
92
93   if (alignment_stack == NULL)
94     GCC_BAD ("%<#pragma pack (pop)%> encountered without matching "
95              "%<#pragma pack (push)%>");
96
97   /* If we got an identifier, strip away everything above the target
98      entry so that the next step will restore the state just below it.  */
99   if (id)
100     {
101       for (entry = alignment_stack; entry; entry = entry->prev)
102         if (entry->id == id)
103           {
104             alignment_stack = entry;
105             break;
106           }
107       if (entry == NULL)
108         warning (OPT_Wpragmas,
109                  "%<#pragma pack(pop, %E)%> encountered without matching "
110                  "%<#pragma pack(push, %E)%>"
111                  , id, id);
112     }
113
114   entry = alignment_stack->prev;
115
116   maximum_field_alignment = entry ? entry->alignment : default_alignment;
117
118   alignment_stack = entry;
119 }
120
121 /* #pragma pack ()
122    #pragma pack (N)
123
124    #pragma pack (push)
125    #pragma pack (push, N)
126    #pragma pack (push, ID)
127    #pragma pack (push, ID, N)
128    #pragma pack (pop)
129    #pragma pack (pop, ID) */
130 static void
131 handle_pragma_pack (cpp_reader * ARG_UNUSED (dummy))
132 {
133   tree x, id = 0;
134   int align = -1;
135   enum cpp_ttype token;
136   enum { set, push, pop } action;
137
138   if (pragma_lex (&x) != CPP_OPEN_PAREN)
139     GCC_BAD ("missing %<(%> after %<#pragma pack%> - ignored");
140
141   token = pragma_lex (&x);
142   if (token == CPP_CLOSE_PAREN)
143     {
144       action = set;
145       align = initial_max_fld_align;
146     }
147   else if (token == CPP_NUMBER)
148     {
149       if (TREE_CODE (x) != INTEGER_CST)
150         GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
151       align = TREE_INT_CST_LOW (x);
152       action = set;
153       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
154         GCC_BAD ("malformed %<#pragma pack%> - ignored");
155     }
156   else if (token == CPP_NAME)
157     {
158 #define GCC_BAD_ACTION do { if (action != pop) \
159           GCC_BAD ("malformed %<#pragma pack(push[, id][, <n>])%> - ignored"); \
160         else \
161           GCC_BAD ("malformed %<#pragma pack(pop[, id])%> - ignored"); \
162         } while (0)
163
164       const char *op = IDENTIFIER_POINTER (x);
165       if (!strcmp (op, "push"))
166         action = push;
167       else if (!strcmp (op, "pop"))
168         action = pop;
169       else
170         GCC_BAD2 ("unknown action %qE for %<#pragma pack%> - ignored", x);
171
172       while ((token = pragma_lex (&x)) == CPP_COMMA)
173         {
174           token = pragma_lex (&x);
175           if (token == CPP_NAME && id == 0)
176             {
177               id = x;
178             }
179           else if (token == CPP_NUMBER && action == push && align == -1)
180             {
181               if (TREE_CODE (x) != INTEGER_CST)
182                 GCC_BAD ("invalid constant in %<#pragma pack%> - ignored");
183               align = TREE_INT_CST_LOW (x);
184               if (align == -1)
185                 action = set;
186             }
187           else
188             GCC_BAD_ACTION;
189         }
190
191       if (token != CPP_CLOSE_PAREN)
192         GCC_BAD_ACTION;
193 #undef GCC_BAD_ACTION
194     }
195   else
196     GCC_BAD ("malformed %<#pragma pack%> - ignored");
197
198   if (pragma_lex (&x) != CPP_EOF)
199     warning (OPT_Wpragmas, "junk at end of %<#pragma pack%>");
200
201   if (flag_pack_struct)
202     GCC_BAD ("%<#pragma pack%> has no effect with %<-fpack-struct%> - ignored");
203
204   if (action != pop)
205     switch (align)
206       {
207       case 0:
208       case 1:
209       case 2:
210       case 4:
211       case 8:
212       case 16:
213         align *= BITS_PER_UNIT;
214         break;
215       case -1:
216         if (action == push)
217           {
218             align = maximum_field_alignment;
219             break;
220           }
221         /* FALLTHRU */
222       default:
223         GCC_BAD2 ("alignment must be a small power of two, not %d", align);
224       }
225
226   switch (action)
227     {
228     case set:   SET_GLOBAL_ALIGNMENT (align);  break;
229     case push:  push_alignment (align, id);    break;
230     case pop:   pop_alignment (id);            break;
231     }
232 }
233
234 struct GTY(()) pending_weak
235 {
236   tree name;
237   tree value;
238 };
239
240
241 static GTY(()) vec<pending_weak, va_gc> *pending_weaks;
242
243 static void apply_pragma_weak (tree, tree);
244 static void handle_pragma_weak (cpp_reader *);
245
246 static void
247 apply_pragma_weak (tree decl, tree value)
248 {
249   if (value)
250     {
251       value = build_string (IDENTIFIER_LENGTH (value),
252                             IDENTIFIER_POINTER (value));
253       decl_attributes (&decl, build_tree_list (get_identifier ("alias"),
254                                                build_tree_list (NULL, value)),
255                        0);
256     }
257
258   if (SUPPORTS_WEAK && DECL_EXTERNAL (decl) && TREE_USED (decl)
259       && !DECL_WEAK (decl) /* Don't complain about a redundant #pragma.  */
260       && DECL_ASSEMBLER_NAME_SET_P (decl)
261       && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl)))
262     warning (OPT_Wpragmas, "applying %<#pragma weak %+D%> after first use "
263              "results in unspecified behavior", decl);
264
265   declare_weak (decl);
266 }
267
268 void
269 maybe_apply_pragma_weak (tree decl)
270 {
271   tree id;
272   int i;
273   pending_weak *pe;
274
275   /* Avoid asking for DECL_ASSEMBLER_NAME when it's not needed.  */
276
277   /* No weak symbols pending, take the short-cut.  */
278   if (vec_safe_is_empty (pending_weaks))
279     return;
280   /* If it's not visible outside this file, it doesn't matter whether
281      it's weak.  */
282   if (!DECL_EXTERNAL (decl) && !TREE_PUBLIC (decl))
283     return;
284   /* If it's not a function or a variable, it can't be weak.
285      FIXME: what kinds of things are visible outside this file but
286      aren't functions or variables?   Should this be an assert instead?  */
287   if (!VAR_OR_FUNCTION_DECL_P (decl))
288     return;
289
290   if (DECL_ASSEMBLER_NAME_SET_P (decl))
291     id = DECL_ASSEMBLER_NAME (decl);
292   else
293     {
294       id = DECL_ASSEMBLER_NAME (decl);
295       SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
296     }
297
298   FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
299     if (id == pe->name)
300       {
301         apply_pragma_weak (decl, pe->value);
302         pending_weaks->unordered_remove (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   symtab_node *target;
316
317   if (vec_safe_is_empty (pending_weaks))
318     return;
319
320   FOR_EACH_VEC_ELT (*pending_weaks, i, pe)
321     {
322       alias_id = pe->name;
323       id = pe->value;
324
325       if (id == NULL)
326         continue;
327
328       target = symtab_node::get_for_asmname (id);
329       decl = build_decl (UNKNOWN_LOCATION,
330                          target ? TREE_CODE (target->decl) : FUNCTION_DECL,
331                          alias_id, default_function_type);
332
333       DECL_ARTIFICIAL (decl) = 1;
334       TREE_PUBLIC (decl) = 1;
335       DECL_WEAK (decl) = 1;
336       if (VAR_P (decl))
337         TREE_STATIC (decl) = 1;
338       if (!target)
339         {
340           error ("%q+D aliased to undefined symbol %qE",
341                  decl, id);
342           continue;
343         }
344
345       assemble_alias (decl, id);
346     }
347 }
348
349 /* #pragma weak name [= value] */
350 static void
351 handle_pragma_weak (cpp_reader * ARG_UNUSED (dummy))
352 {
353   tree name, value, x, decl;
354   enum cpp_ttype t;
355
356   value = 0;
357
358   if (pragma_lex (&name) != CPP_NAME)
359     GCC_BAD ("malformed %<#pragma weak%>, ignored");
360   t = pragma_lex (&x);
361   if (t == CPP_EQ)
362     {
363       if (pragma_lex (&value) != CPP_NAME)
364         GCC_BAD ("malformed %<#pragma weak%>, ignored");
365       t = pragma_lex (&x);
366     }
367   if (t != CPP_EOF)
368     warning (OPT_Wpragmas, "junk at end of %<#pragma weak%>");
369
370   decl = identifier_global_value (name);
371   if (decl && DECL_P (decl))
372     {
373       if (!VAR_OR_FUNCTION_DECL_P (decl))
374         GCC_BAD2 ("%<#pragma weak%> declaration of %q+D not allowed,"
375                   " ignored", decl);
376       apply_pragma_weak (decl, value);
377       if (value)
378         {
379           DECL_EXTERNAL (decl) = 0;
380           if (VAR_P (decl))
381             TREE_STATIC (decl) = 1;
382           assemble_alias (decl, value);
383         }
384     }
385   else
386     {
387       pending_weak pe = {name, value};
388       vec_safe_push (pending_weaks, pe);
389     }
390 }
391
392 static enum scalar_storage_order_kind global_sso;
393
394 void
395 maybe_apply_pragma_scalar_storage_order (tree type)
396 {
397   if (global_sso == SSO_NATIVE)
398     return;
399
400   gcc_assert (RECORD_OR_UNION_TYPE_P (type));
401
402   if (lookup_attribute ("scalar_storage_order", TYPE_ATTRIBUTES (type)))
403     return;
404
405   if (global_sso == SSO_BIG_ENDIAN)
406     TYPE_REVERSE_STORAGE_ORDER (type) = !BYTES_BIG_ENDIAN;
407   else if (global_sso == SSO_LITTLE_ENDIAN)
408     TYPE_REVERSE_STORAGE_ORDER (type) = BYTES_BIG_ENDIAN;
409   else
410     gcc_unreachable ();
411 }
412
413 static void
414 handle_pragma_scalar_storage_order (cpp_reader *ARG_UNUSED(dummy))
415 {
416   const char *kind_string;
417   enum cpp_ttype token;
418   tree x;
419
420   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
421     {
422       error ("%<scalar_storage_order%> is not supported because endianness "
423              "is not uniform");
424       return;
425     }
426
427   if (c_dialect_cxx ())
428     {
429       if (warn_unknown_pragmas > in_system_header_at (input_location))
430         warning (OPT_Wunknown_pragmas,
431                  "%<#pragma scalar_storage_order%> is not supported for C++");
432       return;
433     }
434
435   token = pragma_lex (&x);
436   if (token != CPP_NAME)
437     GCC_BAD ("missing [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
438   kind_string = IDENTIFIER_POINTER (x);
439   if (strcmp (kind_string, "default") == 0)
440     global_sso = default_sso;
441   else if (strcmp (kind_string, "big") == 0)
442     global_sso = SSO_BIG_ENDIAN;
443   else if (strcmp (kind_string, "little") == 0)
444     global_sso = SSO_LITTLE_ENDIAN;
445   else
446     GCC_BAD ("expected [big-endian|little-endian|default] after %<#pragma scalar_storage_order%>");
447 }
448
449 /* GCC supports two #pragma directives for renaming the external
450    symbol associated with a declaration (DECL_ASSEMBLER_NAME), for
451    compatibility with the Solaris and VMS system headers.  GCC also
452    has its own notation for this, __asm__("name") annotations.
453
454    Corner cases of these features and their interaction:
455
456    1) Both pragmas silently apply only to declarations with external
457       linkage (that is, TREE_PUBLIC || DECL_EXTERNAL).  Asm labels
458       do not have this restriction.
459
460    2) In C++, both #pragmas silently apply only to extern "C" declarations.
461       Asm labels do not have this restriction.
462
463    3) If any of the three ways of changing DECL_ASSEMBLER_NAME is
464       applied to a decl whose DECL_ASSEMBLER_NAME is already set, and the
465       new name is different, a warning issues and the name does not change.
466
467    4) The "source name" for #pragma redefine_extname is the DECL_NAME,
468       *not* the DECL_ASSEMBLER_NAME.
469
470    5) If #pragma extern_prefix is in effect and a declaration occurs
471       with an __asm__ name, the #pragma extern_prefix is silently
472       ignored for that declaration.
473
474    6) If #pragma extern_prefix and #pragma redefine_extname apply to
475       the same declaration, whichever triggered first wins, and a warning
476       is issued.  (We would like to have #pragma redefine_extname always
477       win, but it can appear either before or after the declaration, and
478       if it appears afterward, we have no way of knowing whether a modified
479       DECL_ASSEMBLER_NAME is due to #pragma extern_prefix.)  */
480
481 struct GTY(()) pending_redefinition {
482   tree oldname;
483   tree newname;
484 };
485
486
487 static GTY(()) vec<pending_redefinition, va_gc> *pending_redefine_extname;
488
489 static void handle_pragma_redefine_extname (cpp_reader *);
490
491 /* #pragma redefine_extname oldname newname */
492 static void
493 handle_pragma_redefine_extname (cpp_reader * ARG_UNUSED (dummy))
494 {
495   tree oldname, newname, decls, x;
496   enum cpp_ttype t;
497   bool found;
498
499   if (pragma_lex (&oldname) != CPP_NAME)
500     GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
501   if (pragma_lex (&newname) != CPP_NAME)
502     GCC_BAD ("malformed %<#pragma redefine_extname%>, ignored");
503   t = pragma_lex (&x);
504   if (t != CPP_EOF)
505     warning (OPT_Wpragmas, "junk at end of %<#pragma redefine_extname%>");
506
507   found = false;
508   for (decls = c_linkage_bindings (oldname);
509        decls; )
510     {
511       tree decl;
512       if (TREE_CODE (decls) == TREE_LIST)
513         {
514           decl = TREE_VALUE (decls);
515           decls = TREE_CHAIN (decls);
516         }
517       else
518         {
519           decl = decls;
520           decls = NULL_TREE;
521         }
522
523       if ((TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
524           && VAR_OR_FUNCTION_DECL_P (decl))
525         {
526           found = true;
527           if (DECL_ASSEMBLER_NAME_SET_P (decl))
528             {
529               const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
530               name = targetm.strip_name_encoding (name);
531
532               if (!id_equal (newname, name))
533                 warning (OPT_Wpragmas, "%<#pragma redefine_extname%> "
534                          "ignored due to conflict with previous rename");
535             }
536           else
537             symtab->change_decl_assembler_name (decl, newname);
538         }
539     }
540
541   if (!found)
542     /* We have to add this to the rename list even if there's already
543        a global value that doesn't meet the above criteria, because in
544        C++ "struct foo {...};" puts "foo" in the current namespace but
545        does *not* conflict with a subsequent declaration of a function
546        or variable foo.  See g++.dg/other/pragma-re-2.C.  */
547     add_to_renaming_pragma_list (oldname, newname);
548 }
549
550 /* This is called from here and from ia64-c.c.  */
551 void
552 add_to_renaming_pragma_list (tree oldname, tree newname)
553 {
554   unsigned ix;
555   pending_redefinition *p;
556
557   FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
558     if (oldname == p->oldname)
559       {
560         if (p->newname != newname)
561           warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored due to "
562                    "conflict with previous %<#pragma redefine_extname%>");
563         return;
564       }
565
566   pending_redefinition e = {oldname, newname};
567   vec_safe_push (pending_redefine_extname, e);
568 }
569
570 /* The current prefix set by #pragma extern_prefix.  */
571 GTY(()) tree pragma_extern_prefix;
572
573 /* Hook from the front ends to apply the results of one of the preceding
574    pragmas that rename variables.  */
575
576 tree
577 maybe_apply_renaming_pragma (tree decl, tree asmname)
578 {
579   unsigned ix;
580   pending_redefinition *p;
581
582   /* The renaming pragmas are only applied to declarations with
583      external linkage.  */
584   if (!VAR_OR_FUNCTION_DECL_P (decl)
585       || (!TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
586       || !has_c_linkage (decl))
587     return asmname;
588
589   /* If the DECL_ASSEMBLER_NAME is already set, it does not change,
590      but we may warn about a rename that conflicts.  */
591   if (DECL_ASSEMBLER_NAME_SET_P (decl))
592     {
593       const char *oldname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
594       oldname = targetm.strip_name_encoding (oldname);
595
596       if (asmname && strcmp (TREE_STRING_POINTER (asmname), oldname))
597           warning (OPT_Wpragmas, "%<asm%> declaration ignored due to "
598                    "conflict with previous rename");
599
600       /* Take any pending redefine_extname off the list.  */
601       FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
602         if (DECL_NAME (decl) == p->oldname)
603           {
604             /* Only warn if there is a conflict.  */
605             if (!id_equal (p->newname, oldname))
606               warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
607                        "due to conflict with previous rename");
608
609             pending_redefine_extname->unordered_remove (ix);
610             break;
611           }
612       return NULL_TREE;
613     }
614
615   /* Find out if we have a pending #pragma redefine_extname.  */
616   FOR_EACH_VEC_SAFE_ELT (pending_redefine_extname, ix, p)
617     if (DECL_NAME (decl) == p->oldname)
618       {
619         tree newname = p->newname;
620         pending_redefine_extname->unordered_remove (ix);
621
622         /* If we already have an asmname, #pragma redefine_extname is
623            ignored (with a warning if it conflicts).  */
624         if (asmname)
625           {
626             if (strcmp (TREE_STRING_POINTER (asmname),
627                         IDENTIFIER_POINTER (newname)) != 0)
628               warning (OPT_Wpragmas, "%<#pragma redefine_extname%> ignored "
629                        "due to conflict with %<asm%> declaration");
630             return asmname;
631           }
632
633         /* Otherwise we use what we've got; #pragma extern_prefix is
634            silently ignored.  */
635         return build_string (IDENTIFIER_LENGTH (newname),
636                              IDENTIFIER_POINTER (newname));
637       }
638
639   /* If we've got an asmname, #pragma extern_prefix is silently ignored.  */
640   if (asmname)
641     return asmname;
642
643   /* If #pragma extern_prefix is in effect, apply it.  */
644   if (pragma_extern_prefix)
645     {
646       const char *prefix = TREE_STRING_POINTER (pragma_extern_prefix);
647       size_t plen = TREE_STRING_LENGTH (pragma_extern_prefix) - 1;
648
649       const char *id = IDENTIFIER_POINTER (DECL_NAME (decl));
650       size_t ilen = IDENTIFIER_LENGTH (DECL_NAME (decl));
651
652       char *newname = (char *) alloca (plen + ilen + 1);
653
654       memcpy (newname,        prefix, plen);
655       memcpy (newname + plen, id, ilen + 1);
656
657       return build_string (plen + ilen, newname);
658     }
659
660   /* Nada.  */
661   return NULL_TREE;
662 }
663
664
665 static void handle_pragma_visibility (cpp_reader *);
666
667 static vec<int> visstack;
668
669 /* Push the visibility indicated by STR onto the top of the #pragma
670    visibility stack.  KIND is 0 for #pragma GCC visibility, 1 for
671    C++ namespace with visibility attribute and 2 for C++ builtin
672    ABI namespace.  push_visibility/pop_visibility calls must have
673    matching KIND, it is not allowed to push visibility using one
674    KIND and pop using a different one.  */
675
676 void
677 push_visibility (const char *str, int kind)
678 {
679   visstack.safe_push (((int) default_visibility) | (kind << 8));
680   if (!strcmp (str, "default"))
681     default_visibility = VISIBILITY_DEFAULT;
682   else if (!strcmp (str, "internal"))
683     default_visibility = VISIBILITY_INTERNAL;
684   else if (!strcmp (str, "hidden"))
685     default_visibility = VISIBILITY_HIDDEN;
686   else if (!strcmp (str, "protected"))
687     default_visibility = VISIBILITY_PROTECTED;
688   else
689     GCC_BAD ("%<#pragma GCC visibility push()%> must specify %<default%>, "
690              "%<internal%>, %<hidden%> or %<protected%>");
691   visibility_options.inpragma = 1;
692 }
693
694 /* Pop a level of the #pragma visibility stack.  Return true if
695    successful.  */
696
697 bool
698 pop_visibility (int kind)
699 {
700   if (!visstack.length ())
701     return false;
702   if ((visstack.last () >> 8) != kind)
703     return false;
704   default_visibility
705     = (enum symbol_visibility) (visstack.pop () & 0xff);
706   visibility_options.inpragma
707     = visstack.length () != 0;
708   return true;
709 }
710
711 /* Sets the default visibility for symbols to something other than that
712    specified on the command line.  */
713
714 static void
715 handle_pragma_visibility (cpp_reader *dummy ATTRIBUTE_UNUSED)
716 {
717   /* Form is #pragma GCC visibility push(hidden)|pop */
718   tree x;
719   enum cpp_ttype token;
720   enum { bad, push, pop } action = bad;
721
722   token = pragma_lex (&x);
723   if (token == CPP_NAME)
724     {
725       const char *op = IDENTIFIER_POINTER (x);
726       if (!strcmp (op, "push"))
727         action = push;
728       else if (!strcmp (op, "pop"))
729         action = pop;
730     }
731   if (bad == action)
732     GCC_BAD ("%<#pragma GCC visibility%> must be followed by %<push%> "
733              "or %<pop%>");
734   else
735     {
736       if (pop == action)
737         {
738           if (! pop_visibility (0))
739             GCC_BAD ("no matching push for %<#pragma GCC visibility pop%>");
740         }
741       else
742         {
743           if (pragma_lex (&x) != CPP_OPEN_PAREN)
744             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
745           token = pragma_lex (&x);
746           if (token != CPP_NAME)
747             GCC_BAD ("malformed %<#pragma GCC visibility push%>");
748           else
749             push_visibility (IDENTIFIER_POINTER (x), 0);
750           if (pragma_lex (&x) != CPP_CLOSE_PAREN)
751             GCC_BAD ("missing %<(%> after %<#pragma GCC visibility push%> - ignored");
752         }
753     }
754   if (pragma_lex (&x) != CPP_EOF)
755     warning (OPT_Wpragmas, "junk at end of %<#pragma GCC visibility%>");
756 }
757
758 static void
759 handle_pragma_diagnostic(cpp_reader *ARG_UNUSED(dummy))
760 {
761   tree x;
762   location_t loc;
763   enum cpp_ttype token = pragma_lex (&x, &loc);
764   if (token != CPP_NAME)
765     {
766       warning_at (loc, OPT_Wpragmas,
767                   "missing [error|warning|ignored|push|pop]"
768                   " after %<#pragma GCC diagnostic%>");
769       return;
770     }
771
772   diagnostic_t kind;
773   const char *kind_string = IDENTIFIER_POINTER (x);
774   if (strcmp (kind_string, "error") == 0)
775     kind = DK_ERROR;
776   else if (strcmp (kind_string, "warning") == 0)
777     kind = DK_WARNING;
778   else if (strcmp (kind_string, "ignored") == 0)
779     kind = DK_IGNORED;
780   else if (strcmp (kind_string, "push") == 0)
781     {
782       diagnostic_push_diagnostics (global_dc, input_location);
783       return;
784     }
785   else if (strcmp (kind_string, "pop") == 0)
786     {
787       diagnostic_pop_diagnostics (global_dc, input_location);
788       return;
789     }
790   else
791     {
792       warning_at (loc, OPT_Wpragmas,
793                   "expected [error|warning|ignored|push|pop]"
794                   " after %<#pragma GCC diagnostic%>");
795       return;
796     }
797
798   token = pragma_lex (&x, &loc);
799   if (token != CPP_STRING)
800     {
801       warning_at (loc, OPT_Wpragmas,
802                   "missing option after %<#pragma GCC diagnostic%> kind");
803       return;
804     }
805
806   const char *option_string = TREE_STRING_POINTER (x);
807   unsigned int lang_mask = c_common_option_lang_mask () | CL_COMMON;
808   /* option_string + 1 to skip the initial '-' */
809   unsigned int option_index = find_opt (option_string + 1, lang_mask);
810   if (option_index == OPT_SPECIAL_unknown)
811     {
812       option_proposer op;
813       const char *hint = op.suggest_option (option_string + 1);
814       if (hint)
815         warning_at (loc, OPT_Wpragmas,
816                     "unknown option after %<#pragma GCC diagnostic%> kind;"
817                     " did you mean %<-%s%>?", hint);
818       else
819         warning_at (loc, OPT_Wpragmas,
820                     "unknown option after %<#pragma GCC diagnostic%> kind");
821
822       return;
823     }
824   else if (!(cl_options[option_index].flags & CL_WARNING))
825     {
826       warning_at (loc, OPT_Wpragmas,
827                   "%qs is not an option that controls warnings", option_string);
828       return;
829     }
830   else if (!(cl_options[option_index].flags & lang_mask))
831     {
832       char *ok_langs = write_langs (cl_options[option_index].flags);
833       char *bad_lang = write_langs (c_common_option_lang_mask ());
834       warning_at (loc, OPT_Wpragmas,
835                   "option %qs is valid for %s but not for %s",
836                   option_string, ok_langs, bad_lang);
837       free (ok_langs);
838       free (bad_lang);
839       return;
840     }
841
842   struct cl_option_handlers handlers;
843   set_default_handlers (&handlers, NULL);
844   const char *arg = NULL;
845   if (cl_options[option_index].flags & CL_JOINED)
846     arg = option_string + 1 + cl_options[option_index].opt_len;
847   /* FIXME: input_location isn't the best location here, but it is
848      what we used to do here before and changing it breaks e.g.
849      PR69543 and PR69558.  */
850   control_warning_option (option_index, (int) kind,
851                           arg, kind != DK_IGNORED,
852                           input_location, lang_mask, &handlers,
853                           &global_options, &global_options_set,
854                           global_dc);
855 }
856
857 /*  Parse #pragma GCC target (xxx) to set target specific options.  */
858 static void
859 handle_pragma_target(cpp_reader *ARG_UNUSED(dummy))
860 {
861   enum cpp_ttype token;
862   tree x;
863   bool close_paren_needed_p = false;
864
865   if (cfun)
866     {
867       error ("%<#pragma GCC option%> is not allowed inside functions");
868       return;
869     }
870
871   token = pragma_lex (&x);
872   if (token == CPP_OPEN_PAREN)
873     {
874       close_paren_needed_p = true;
875       token = pragma_lex (&x);
876     }
877
878   if (token != CPP_STRING)
879     {
880       GCC_BAD ("%<#pragma GCC option%> is not a string");
881       return;
882     }
883
884   /* Strings are user options.  */
885   else
886     {
887       tree args = NULL_TREE;
888
889       do
890         {
891           /* Build up the strings now as a tree linked list.  Skip empty
892              strings.  */
893           if (TREE_STRING_LENGTH (x) > 0)
894             args = tree_cons (NULL_TREE, x, args);
895
896           token = pragma_lex (&x);
897           while (token == CPP_COMMA)
898             token = pragma_lex (&x);
899         }
900       while (token == CPP_STRING);
901
902       if (close_paren_needed_p)
903         {
904           if (token == CPP_CLOSE_PAREN)
905             token = pragma_lex (&x);
906           else
907             GCC_BAD ("%<#pragma GCC target (string [,string]...)%> does "
908                      "not have a final %<)%>");
909         }
910
911       if (token != CPP_EOF)
912         {
913           error ("%<#pragma GCC target%> string is badly formed");
914           return;
915         }
916
917       /* put arguments in the order the user typed them.  */
918       args = nreverse (args);
919
920       if (targetm.target_option.pragma_parse (args, NULL_TREE))
921         current_target_pragma = chainon (current_target_pragma, args);
922     }
923 }
924
925 /* Handle #pragma GCC optimize to set optimization options.  */
926 static void
927 handle_pragma_optimize (cpp_reader *ARG_UNUSED(dummy))
928 {
929   enum cpp_ttype token;
930   tree x;
931   bool close_paren_needed_p = false;
932   tree optimization_previous_node = optimization_current_node;
933
934   if (cfun)
935     {
936       error ("%<#pragma GCC optimize%> is not allowed inside functions");
937       return;
938     }
939
940   token = pragma_lex (&x);
941   if (token == CPP_OPEN_PAREN)
942     {
943       close_paren_needed_p = true;
944       token = pragma_lex (&x);
945     }
946
947   if (token != CPP_STRING && token != CPP_NUMBER)
948     {
949       GCC_BAD ("%<#pragma GCC optimize%> is not a string or number");
950       return;
951     }
952
953   /* Strings/numbers are user options.  */
954   else
955     {
956       tree args = NULL_TREE;
957
958       do
959         {
960           /* Build up the numbers/strings now as a list.  */
961           if (token != CPP_STRING || TREE_STRING_LENGTH (x) > 0)
962             args = tree_cons (NULL_TREE, x, args);
963
964           token = pragma_lex (&x);
965           while (token == CPP_COMMA)
966             token = pragma_lex (&x);
967         }
968       while (token == CPP_STRING || token == CPP_NUMBER);
969
970       if (close_paren_needed_p)
971         {
972           if (token == CPP_CLOSE_PAREN)
973             token = pragma_lex (&x);
974           else
975             GCC_BAD ("%<#pragma GCC optimize (string [,string]...)%> does "
976                      "not have a final %<)%>");
977         }
978
979       if (token != CPP_EOF)
980         {
981           error ("%<#pragma GCC optimize%> string is badly formed");
982           return;
983         }
984
985       /* put arguments in the order the user typed them.  */
986       args = nreverse (args);
987
988       parse_optimize_options (args, false);
989       current_optimize_pragma = chainon (current_optimize_pragma, args);
990       optimization_current_node = build_optimization_node (&global_options);
991       c_cpp_builtins_optimize_pragma (parse_in,
992                                       optimization_previous_node,
993                                       optimization_current_node);
994     }
995 }
996
997 /* Stack of the #pragma GCC options created with #pragma GCC push_option.  Save
998    both the binary representation of the options and the TREE_LIST of
999    strings that will be added to the function's attribute list.  */
1000 struct GTY(()) opt_stack {
1001   struct opt_stack *prev;
1002   tree target_binary;
1003   tree target_strings;
1004   tree optimize_binary;
1005   tree optimize_strings;
1006   gcc_options * GTY ((skip)) saved_global_options;
1007 };
1008
1009 static GTY(()) struct opt_stack * options_stack;
1010
1011 /* Handle #pragma GCC push_options to save the current target and optimization
1012    options.  */
1013
1014 static void
1015 handle_pragma_push_options (cpp_reader *ARG_UNUSED(dummy))
1016 {
1017   enum cpp_ttype token;
1018   tree x = 0;
1019
1020   token = pragma_lex (&x);
1021   if (token != CPP_EOF)
1022     {
1023       warning (OPT_Wpragmas, "junk at end of %<#pragma push_options%>");
1024       return;
1025     }
1026
1027   opt_stack *p = ggc_alloc<opt_stack> ();
1028   p->prev = options_stack;
1029   options_stack = p;
1030
1031   /* Save optimization and target flags in binary format.  */
1032   if (flag_checking)
1033     {
1034       p->saved_global_options = XNEW (gcc_options);
1035       *p->saved_global_options = global_options;
1036     }
1037   p->optimize_binary = build_optimization_node (&global_options);
1038   p->target_binary = build_target_option_node (&global_options);
1039
1040   /* Save optimization and target flags in string list format.  */
1041   p->optimize_strings = copy_list (current_optimize_pragma);
1042   p->target_strings = copy_list (current_target_pragma);
1043 }
1044
1045 /* Handle #pragma GCC pop_options to restore the current target and
1046    optimization options from a previous push_options.  */
1047
1048 static void
1049 handle_pragma_pop_options (cpp_reader *ARG_UNUSED(dummy))
1050 {
1051   enum cpp_ttype token;
1052   tree x = 0;
1053   opt_stack *p;
1054
1055   token = pragma_lex (&x);
1056   if (token != CPP_EOF)
1057     {
1058       warning (OPT_Wpragmas, "junk at end of %<#pragma pop_options%>");
1059       return;
1060     }
1061
1062   if (! options_stack)
1063     {
1064       warning (OPT_Wpragmas,
1065                "%<#pragma GCC pop_options%> without a corresponding "
1066                "%<#pragma GCC push_options%>");
1067       return;
1068     }
1069
1070   p = options_stack;
1071   options_stack = p->prev;
1072
1073   if (p->target_binary != target_option_current_node)
1074     {
1075       (void) targetm.target_option.pragma_parse (NULL_TREE, p->target_binary);
1076       target_option_current_node = p->target_binary;
1077     }
1078
1079   if (p->optimize_binary != optimization_current_node)
1080     {
1081       tree old_optimize = optimization_current_node;
1082       cl_optimization_restore (&global_options,
1083                                TREE_OPTIMIZATION (p->optimize_binary));
1084       c_cpp_builtins_optimize_pragma (parse_in, old_optimize,
1085                                       p->optimize_binary);
1086       optimization_current_node = p->optimize_binary;
1087     }
1088   if (flag_checking)
1089     {
1090       cl_optimization_compare (p->saved_global_options, &global_options);
1091       free (p->saved_global_options);
1092     }
1093
1094   current_target_pragma = p->target_strings;
1095   current_optimize_pragma = p->optimize_strings;
1096 }
1097
1098 /* Handle #pragma GCC reset_options to restore the current target and
1099    optimization options to the original options used on the command line.  */
1100
1101 static void
1102 handle_pragma_reset_options (cpp_reader *ARG_UNUSED(dummy))
1103 {
1104   enum cpp_ttype token;
1105   tree x = 0;
1106   tree new_optimize = optimization_default_node;
1107   tree new_target = target_option_default_node;
1108
1109   token = pragma_lex (&x);
1110   if (token != CPP_EOF)
1111     {
1112       warning (OPT_Wpragmas, "junk at end of %<#pragma reset_options%>");
1113       return;
1114     }
1115
1116   if (new_target != target_option_current_node)
1117     {
1118       (void) targetm.target_option.pragma_parse (NULL_TREE, new_target);
1119       target_option_current_node = new_target;
1120     }
1121
1122   if (new_optimize != optimization_current_node)
1123     {
1124       tree old_optimize = optimization_current_node;
1125       cl_optimization_restore (&global_options,
1126                                TREE_OPTIMIZATION (new_optimize));
1127       c_cpp_builtins_optimize_pragma (parse_in, old_optimize, new_optimize);
1128       optimization_current_node = new_optimize;
1129     }
1130
1131   current_target_pragma = NULL_TREE;
1132   current_optimize_pragma = NULL_TREE;
1133 }
1134
1135 /* Print a plain user-specified message.  */
1136
1137 static void
1138 handle_pragma_message (cpp_reader *ARG_UNUSED(dummy))
1139 {
1140   enum cpp_ttype token;
1141   tree x, message = 0;
1142
1143   token = pragma_lex (&x);
1144   if (token == CPP_OPEN_PAREN)
1145     {
1146       token = pragma_lex (&x);
1147       if (token == CPP_STRING)
1148         message = x;
1149       else
1150         GCC_BAD ("expected a string after %<#pragma message%>");
1151       if (pragma_lex (&x) != CPP_CLOSE_PAREN)
1152         GCC_BAD ("malformed %<#pragma message%>, ignored");
1153     }
1154   else if (token == CPP_STRING)
1155     message = x;
1156   else
1157     GCC_BAD ("expected a string after %<#pragma message%>");
1158
1159   gcc_assert (message);
1160
1161   if (pragma_lex (&x) != CPP_EOF)
1162     warning (OPT_Wpragmas, "junk at end of %<#pragma message%>");
1163
1164   if (TREE_STRING_LENGTH (message) > 1)
1165     inform (input_location, "%<#pragma message: %s%>",
1166             TREE_STRING_POINTER (message));
1167 }
1168
1169 /* Mark whether the current location is valid for a STDC pragma.  */
1170
1171 static bool valid_location_for_stdc_pragma;
1172
1173 void
1174 mark_valid_location_for_stdc_pragma (bool flag)
1175 {
1176   valid_location_for_stdc_pragma = flag;
1177 }
1178
1179 /* Return true if the current location is valid for a STDC pragma.  */
1180
1181 bool
1182 valid_location_for_stdc_pragma_p (void)
1183 {
1184   return valid_location_for_stdc_pragma;
1185 }
1186
1187 enum pragma_switch_t { PRAGMA_ON, PRAGMA_OFF, PRAGMA_DEFAULT, PRAGMA_BAD };
1188
1189 /* A STDC pragma must appear outside of external declarations or
1190    preceding all explicit declarations and statements inside a compound
1191    statement; its behavior is undefined if used in any other context.
1192    It takes a switch of ON, OFF, or DEFAULT.  */
1193
1194 static enum pragma_switch_t
1195 handle_stdc_pragma (const char *pname)
1196 {
1197   const char *arg;
1198   tree t;
1199   enum pragma_switch_t ret;
1200
1201   if (!valid_location_for_stdc_pragma_p ())
1202     {
1203       warning (OPT_Wpragmas, "invalid location for %<pragma %s%>, ignored",
1204                pname);
1205       return PRAGMA_BAD;
1206     }
1207
1208   if (pragma_lex (&t) != CPP_NAME)
1209     {
1210       warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1211       return PRAGMA_BAD;
1212     }
1213
1214   arg = IDENTIFIER_POINTER (t);
1215
1216   if (!strcmp (arg, "ON"))
1217     ret = PRAGMA_ON;
1218   else if (!strcmp (arg, "OFF"))
1219     ret = PRAGMA_OFF;
1220   else if (!strcmp (arg, "DEFAULT"))
1221     ret = PRAGMA_DEFAULT;
1222   else
1223     {
1224       warning (OPT_Wpragmas, "malformed %<#pragma %s%>, ignored", pname);
1225       return PRAGMA_BAD;
1226     }
1227
1228   if (pragma_lex (&t) != CPP_EOF)
1229     {
1230       warning (OPT_Wpragmas, "junk at end of %<#pragma %s%>", pname);
1231       return PRAGMA_BAD;
1232     }
1233
1234   return ret;
1235 }
1236
1237 /* #pragma STDC FLOAT_CONST_DECIMAL64 ON
1238    #pragma STDC FLOAT_CONST_DECIMAL64 OFF
1239    #pragma STDC FLOAT_CONST_DECIMAL64 DEFAULT */
1240
1241 static void
1242 handle_pragma_float_const_decimal64 (cpp_reader *ARG_UNUSED (dummy))
1243 {
1244   if (c_dialect_cxx ())
1245     {
1246       if (warn_unknown_pragmas > in_system_header_at (input_location))
1247         warning (OPT_Wunknown_pragmas,
1248                  "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1249                  " for C++");
1250       return;
1251     }
1252
1253   if (!targetm.decimal_float_supported_p ())
1254     {
1255       if (warn_unknown_pragmas > in_system_header_at (input_location))
1256         warning (OPT_Wunknown_pragmas,
1257                  "%<#pragma STDC FLOAT_CONST_DECIMAL64%> is not supported"
1258                  " on this target");
1259       return;
1260     }
1261
1262   pedwarn (input_location, OPT_Wpedantic,
1263            "ISO C does not support %<#pragma STDC FLOAT_CONST_DECIMAL64%>");
1264
1265   switch (handle_stdc_pragma ("STDC FLOAT_CONST_DECIMAL64"))
1266     {
1267     case PRAGMA_ON:
1268       set_float_const_decimal64 ();
1269       break;
1270     case PRAGMA_OFF:
1271     case PRAGMA_DEFAULT:
1272       clear_float_const_decimal64 ();
1273       break;
1274     case PRAGMA_BAD:
1275       break;
1276     }
1277 }
1278
1279 /* A vector of registered pragma callbacks, which is never freed.   */
1280
1281 static vec<internal_pragma_handler> registered_pragmas;
1282
1283 struct pragma_ns_name
1284 {
1285   const char *space;
1286   const char *name;
1287 };
1288
1289
1290 static vec<pragma_ns_name> registered_pp_pragmas;
1291
1292 struct omp_pragma_def { const char *name; unsigned int id; };
1293 static const struct omp_pragma_def oacc_pragmas[] = {
1294   { "atomic", PRAGMA_OACC_ATOMIC },
1295   { "cache", PRAGMA_OACC_CACHE },
1296   { "data", PRAGMA_OACC_DATA },
1297   { "declare", PRAGMA_OACC_DECLARE },
1298   { "enter", PRAGMA_OACC_ENTER_DATA },
1299   { "exit", PRAGMA_OACC_EXIT_DATA },
1300   { "host_data", PRAGMA_OACC_HOST_DATA },
1301   { "kernels", PRAGMA_OACC_KERNELS },
1302   { "loop", PRAGMA_OACC_LOOP },
1303   { "parallel", PRAGMA_OACC_PARALLEL },
1304   { "routine", PRAGMA_OACC_ROUTINE },
1305   { "serial", PRAGMA_OACC_SERIAL },
1306   { "update", PRAGMA_OACC_UPDATE },
1307   { "wait", PRAGMA_OACC_WAIT }
1308 };
1309 static const struct omp_pragma_def omp_pragmas[] = {
1310   { "atomic", PRAGMA_OMP_ATOMIC },
1311   { "barrier", PRAGMA_OMP_BARRIER },
1312   { "cancel", PRAGMA_OMP_CANCEL },
1313   { "cancellation", PRAGMA_OMP_CANCELLATION_POINT },
1314   { "critical", PRAGMA_OMP_CRITICAL },
1315   { "depobj", PRAGMA_OMP_DEPOBJ },
1316   { "end", PRAGMA_OMP_END_DECLARE_TARGET },
1317   { "flush", PRAGMA_OMP_FLUSH },
1318   { "master", PRAGMA_OMP_MASTER },
1319   { "requires", PRAGMA_OMP_REQUIRES },
1320   { "section", PRAGMA_OMP_SECTION },
1321   { "sections", PRAGMA_OMP_SECTIONS },
1322   { "single", PRAGMA_OMP_SINGLE },
1323   { "task", PRAGMA_OMP_TASK },
1324   { "taskgroup", PRAGMA_OMP_TASKGROUP },
1325   { "taskwait", PRAGMA_OMP_TASKWAIT },
1326   { "taskyield", PRAGMA_OMP_TASKYIELD },
1327   { "threadprivate", PRAGMA_OMP_THREADPRIVATE }
1328 };
1329 static const struct omp_pragma_def omp_pragmas_simd[] = {
1330   { "declare", PRAGMA_OMP_DECLARE },
1331   { "distribute", PRAGMA_OMP_DISTRIBUTE },
1332   { "for", PRAGMA_OMP_FOR },
1333   { "loop", PRAGMA_OMP_LOOP },
1334   { "ordered", PRAGMA_OMP_ORDERED },
1335   { "parallel", PRAGMA_OMP_PARALLEL },
1336   { "scan", PRAGMA_OMP_SCAN },
1337   { "simd", PRAGMA_OMP_SIMD },
1338   { "target", PRAGMA_OMP_TARGET },
1339   { "taskloop", PRAGMA_OMP_TASKLOOP },
1340   { "teams", PRAGMA_OMP_TEAMS },
1341 };
1342
1343 void
1344 c_pp_lookup_pragma (unsigned int id, const char **space, const char **name)
1345 {
1346   const int n_oacc_pragmas = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1347   const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1348   const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1349                                  / sizeof (*omp_pragmas);
1350   int i;
1351
1352   for (i = 0; i < n_oacc_pragmas; ++i)
1353     if (oacc_pragmas[i].id == id)
1354       {
1355         *space = "acc";
1356         *name = oacc_pragmas[i].name;
1357         return;
1358       }
1359
1360   for (i = 0; i < n_omp_pragmas; ++i)
1361     if (omp_pragmas[i].id == id)
1362       {
1363         *space = "omp";
1364         *name = omp_pragmas[i].name;
1365         return;
1366       }
1367
1368   for (i = 0; i < n_omp_pragmas_simd; ++i)
1369     if (omp_pragmas_simd[i].id == id)
1370       {
1371         *space = "omp";
1372         *name = omp_pragmas_simd[i].name;
1373         return;
1374       }
1375
1376   if (id >= PRAGMA_FIRST_EXTERNAL
1377       && (id < PRAGMA_FIRST_EXTERNAL + registered_pp_pragmas.length ()))
1378     {
1379       *space = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].space;
1380       *name = registered_pp_pragmas[id - PRAGMA_FIRST_EXTERNAL].name;
1381       return;
1382     }
1383
1384   gcc_unreachable ();
1385 }
1386
1387 /* Front-end wrappers for pragma registration to avoid dragging
1388    cpplib.h in almost everywhere.  */
1389
1390 static void
1391 c_register_pragma_1 (const char *space, const char *name,
1392                      internal_pragma_handler ihandler, bool allow_expansion)
1393 {
1394   unsigned id;
1395
1396   if (flag_preprocess_only)
1397     {
1398       pragma_ns_name ns_name;
1399
1400       if (!allow_expansion)
1401         return;
1402
1403       ns_name.space = space;
1404       ns_name.name = name;
1405       registered_pp_pragmas.safe_push (ns_name);
1406       id = registered_pp_pragmas.length ();
1407       id += PRAGMA_FIRST_EXTERNAL - 1;
1408     }
1409   else
1410     {
1411       registered_pragmas.safe_push (ihandler);
1412       id = registered_pragmas.length ();
1413       id += PRAGMA_FIRST_EXTERNAL - 1;
1414
1415       /* The C front end allocates 8 bits in c_token.  The C++ front end
1416          keeps the pragma kind in the form of INTEGER_CST, so no small
1417          limit applies.  At present this is sufficient.  */
1418       gcc_assert (id < 256);
1419     }
1420
1421   cpp_register_deferred_pragma (parse_in, space, name, id,
1422                                 allow_expansion, false);
1423 }
1424
1425 /* Register a C pragma handler, using a space and a name.  It disallows pragma
1426    expansion (if you want it, use c_register_pragma_with_expansion instead).  */
1427 void
1428 c_register_pragma (const char *space, const char *name,
1429                    pragma_handler_1arg handler)
1430 {
1431   internal_pragma_handler ihandler;
1432
1433   ihandler.handler.handler_1arg = handler;
1434   ihandler.extra_data = false;
1435   ihandler.data = NULL;
1436   c_register_pragma_1 (space, name, ihandler, false);
1437 }
1438
1439 /* Register a C pragma handler, using a space and a name, it also carries an
1440    extra data field which can be used by the handler.  It disallows pragma
1441    expansion (if you want it, use c_register_pragma_with_expansion_and_data
1442    instead).  */
1443 void
1444 c_register_pragma_with_data (const char *space, const char *name,
1445                              pragma_handler_2arg handler, void * data)
1446 {
1447   internal_pragma_handler ihandler;
1448
1449   ihandler.handler.handler_2arg = handler;
1450   ihandler.extra_data = true;
1451   ihandler.data = data;
1452   c_register_pragma_1 (space, name, ihandler, false);
1453 }
1454
1455 /* Register a C pragma handler, using a space and a name.  It allows pragma
1456    expansion as in the following example:
1457
1458    #define NUMBER 10
1459    #pragma count (NUMBER)
1460
1461    Name expansion is still disallowed.  */
1462 void
1463 c_register_pragma_with_expansion (const char *space, const char *name,
1464                                   pragma_handler_1arg handler)
1465 {
1466   internal_pragma_handler ihandler;
1467
1468   ihandler.handler.handler_1arg = handler;
1469   ihandler.extra_data = false;
1470   ihandler.data = NULL;
1471   c_register_pragma_1 (space, name, ihandler, true);
1472 }
1473
1474 /* Register a C pragma handler, using a space and a name, it also carries an
1475    extra data field which can be used by the handler.  It allows pragma
1476    expansion as in the following example:
1477
1478    #define NUMBER 10
1479    #pragma count (NUMBER)
1480
1481    Name expansion is still disallowed.  */
1482 void
1483 c_register_pragma_with_expansion_and_data (const char *space, const char *name,
1484                                            pragma_handler_2arg handler,
1485                                            void *data)
1486 {
1487   internal_pragma_handler ihandler;
1488
1489   ihandler.handler.handler_2arg = handler;
1490   ihandler.extra_data = true;
1491   ihandler.data = data;
1492   c_register_pragma_1 (space, name, ihandler, true);
1493 }
1494
1495 void
1496 c_invoke_pragma_handler (unsigned int id)
1497 {
1498   internal_pragma_handler *ihandler;
1499   pragma_handler_1arg handler_1arg;
1500   pragma_handler_2arg handler_2arg;
1501
1502   id -= PRAGMA_FIRST_EXTERNAL;
1503   ihandler = &registered_pragmas[id];
1504   if (ihandler->extra_data)
1505     {
1506       handler_2arg = ihandler->handler.handler_2arg;
1507       handler_2arg (parse_in, ihandler->data);
1508     }
1509   else
1510     {
1511       handler_1arg = ihandler->handler.handler_1arg;
1512       handler_1arg (parse_in);
1513     }
1514 }
1515
1516 /* Set up front-end pragmas.  */
1517 void
1518 init_pragma (void)
1519 {
1520   if (flag_openacc)
1521     {
1522       const int n_oacc_pragmas
1523         = sizeof (oacc_pragmas) / sizeof (*oacc_pragmas);
1524       int i;
1525
1526       for (i = 0; i < n_oacc_pragmas; ++i)
1527         cpp_register_deferred_pragma (parse_in, "acc", oacc_pragmas[i].name,
1528                                       oacc_pragmas[i].id, true, true);
1529     }
1530
1531   if (flag_openmp)
1532     {
1533       const int n_omp_pragmas = sizeof (omp_pragmas) / sizeof (*omp_pragmas);
1534       int i;
1535
1536       for (i = 0; i < n_omp_pragmas; ++i)
1537         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas[i].name,
1538                                       omp_pragmas[i].id, true, true);
1539     }
1540   if (flag_openmp || flag_openmp_simd)
1541     {
1542       const int n_omp_pragmas_simd = sizeof (omp_pragmas_simd)
1543                                      / sizeof (*omp_pragmas);
1544       int i;
1545
1546       for (i = 0; i < n_omp_pragmas_simd; ++i)
1547         cpp_register_deferred_pragma (parse_in, "omp", omp_pragmas_simd[i].name,
1548                                       omp_pragmas_simd[i].id, true, true);
1549     }
1550
1551   if (!flag_preprocess_only)
1552     cpp_register_deferred_pragma (parse_in, "GCC", "pch_preprocess",
1553                                   PRAGMA_GCC_PCH_PREPROCESS, false, false);
1554
1555   if (!flag_preprocess_only)
1556     cpp_register_deferred_pragma (parse_in, "GCC", "ivdep", PRAGMA_IVDEP, false,
1557                                   false);
1558
1559   if (!flag_preprocess_only)
1560     cpp_register_deferred_pragma (parse_in, "GCC", "unroll", PRAGMA_UNROLL,
1561                                   false, false);
1562
1563 #ifdef HANDLE_PRAGMA_PACK_WITH_EXPANSION
1564   c_register_pragma_with_expansion (0, "pack", handle_pragma_pack);
1565 #else
1566   c_register_pragma (0, "pack", handle_pragma_pack);
1567 #endif
1568   c_register_pragma (0, "weak", handle_pragma_weak);
1569
1570   c_register_pragma ("GCC", "visibility", handle_pragma_visibility);
1571
1572   c_register_pragma ("GCC", "diagnostic", handle_pragma_diagnostic);
1573   c_register_pragma ("GCC", "target", handle_pragma_target);
1574   c_register_pragma ("GCC", "optimize", handle_pragma_optimize);
1575   c_register_pragma ("GCC", "push_options", handle_pragma_push_options);
1576   c_register_pragma ("GCC", "pop_options", handle_pragma_pop_options);
1577   c_register_pragma ("GCC", "reset_options", handle_pragma_reset_options);
1578
1579   c_register_pragma ("STDC", "FLOAT_CONST_DECIMAL64",
1580                      handle_pragma_float_const_decimal64);
1581
1582   c_register_pragma_with_expansion (0, "redefine_extname",
1583                                     handle_pragma_redefine_extname);
1584
1585   c_register_pragma_with_expansion (0, "message", handle_pragma_message);
1586
1587 #ifdef REGISTER_TARGET_PRAGMAS
1588   REGISTER_TARGET_PRAGMAS ();
1589 #endif
1590
1591   global_sso = default_sso;
1592   c_register_pragma (0, "scalar_storage_order", 
1593                      handle_pragma_scalar_storage_order);
1594
1595   /* Allow plugins to register their own pragmas. */
1596   invoke_plugin_callbacks (PLUGIN_PRAGMAS, NULL);
1597 }
1598
1599 #include "gt-c-family-c-pragma.h"