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