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