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