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