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