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