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