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