]>
Commit | Line | Data |
---|---|---|
e14c5993 | 1 | /* CPP Library. (Directive handling.) |
f1717362 | 2 | Copyright (C) 1986-2016 Free Software Foundation, Inc. |
1f99c786 | 3 | Contributed by Per Bothner, 1994-95. |
0cb8d9ae | 4 | Based on CCCP program by Paul Rubin, June 1986 |
1d6fa33f | 5 | Adapted to ANSI C, Richard Stallman, Jan 1987 |
6 | ||
7 | This program is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU General Public License as published by the | |
6bc9506f | 9 | Free Software Foundation; either version 3, or (at your option) any |
1d6fa33f | 10 | later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
6bc9506f | 18 | along with this program; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
1d6fa33f | 20 | |
ad87de1e | 21 | #include "config.h" |
1486870d | 22 | #include "system.h" |
ad87de1e | 23 | #include "cpplib.h" |
d856c8a6 | 24 | #include "internal.h" |
3eb3f293 | 25 | #include "mkdeps.h" |
f51c2148 | 26 | #include "obstack.h" |
1d6fa33f | 27 | |
1e8b9746 | 28 | /* Stack of conditionals currently in progress |
29 | (including both successful and failing conditionals). */ | |
1e8b9746 | 30 | struct if_stack |
31 | { | |
32 | struct if_stack *next; | |
c219ee5c | 33 | source_location line; /* Line where condition started. */ |
79bd622b | 34 | const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */ |
5e878517 | 35 | bool skip_elses; /* Can future #else / #elif be skipped? */ |
36 | bool was_skipping; /* If were skipping on entry. */ | |
f7fdd7a1 | 37 | int type; /* Most recent conditional for diagnostics. */ |
1e8b9746 | 38 | }; |
1e8b9746 | 39 | |
c6f14ce5 | 40 | /* Contains a registered pragma or pragma namespace. */ |
f7fdd7a1 | 41 | typedef void (*pragma_cb) (cpp_reader *); |
c6f14ce5 | 42 | struct pragma_entry |
43 | { | |
44 | struct pragma_entry *next; | |
5c43d212 | 45 | const cpp_hashnode *pragma; /* Name and length. */ |
d6d3c909 | 46 | bool is_nspace; |
47 | bool is_internal; | |
b75b98aa | 48 | bool is_deferred; |
49 | bool allow_expansion; | |
c6f14ce5 | 50 | union { |
51 | pragma_cb handler; | |
52 | struct pragma_entry *space; | |
b75b98aa | 53 | unsigned int ident; |
c6f14ce5 | 54 | } u; |
55 | }; | |
56 | ||
79bd622b | 57 | /* Values for the origin field of struct directive. KANDR directives |
58 | come from traditional (K&R) C. STDC89 directives come from the | |
59 | 1989 C standard. EXTENSION directives are extensions. */ | |
60 | #define KANDR 0 | |
61 | #define STDC89 1 | |
62 | #define EXTENSION 2 | |
63 | ||
64 | /* Values for the flags field of struct directive. COND indicates a | |
65 | conditional; IF_COND an opening conditional. INCL means to treat | |
66 | "..." and <...> as q-char and h-char sequences respectively. IN_I | |
67 | means this directive should be handled even if -fpreprocessed is in | |
fb83e0d6 | 68 | effect (these are the directives with callback hooks). |
69 | ||
bab5e68f | 70 | EXPAND is set on directives that are always macro-expanded. */ |
79bd622b | 71 | #define COND (1 << 0) |
72 | #define IF_COND (1 << 1) | |
73 | #define INCL (1 << 2) | |
74 | #define IN_I (1 << 3) | |
fb83e0d6 | 75 | #define EXPAND (1 << 4) |
fb9753dc | 76 | #define DEPRECATED (1 << 5) |
79bd622b | 77 | |
78 | /* Defines one #-directive, including how to handle it. */ | |
f7fdd7a1 | 79 | typedef void (*directive_handler) (cpp_reader *); |
79bd622b | 80 | typedef struct directive directive; |
81 | struct directive | |
82 | { | |
83 | directive_handler handler; /* Function to handle directive. */ | |
b6d18b0a | 84 | const uchar *name; /* Name of directive. */ |
79bd622b | 85 | unsigned short length; /* Length of name. */ |
86 | unsigned char origin; /* Origin of directive. */ | |
87 | unsigned char flags; /* Flags describing this directive. */ | |
88 | }; | |
89 | ||
08c74ec4 | 90 | /* Forward declarations. */ |
91 | ||
f7fdd7a1 | 92 | static void skip_rest_of_line (cpp_reader *); |
6cda323a | 93 | static void check_eol (cpp_reader *, bool); |
f7fdd7a1 | 94 | static void start_directive (cpp_reader *); |
95 | static void prepare_directive_trad (cpp_reader *); | |
96 | static void end_directive (cpp_reader *, int); | |
97 | static void directive_diagnostics (cpp_reader *, const directive *, int); | |
98 | static void run_directive (cpp_reader *, int, const char *, size_t); | |
99 | static char *glue_header_name (cpp_reader *); | |
78ed8e3d | 100 | static const char *parse_include (cpp_reader *, int *, const cpp_token ***, |
101 | source_location *); | |
f7fdd7a1 | 102 | static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *); |
103 | static unsigned int read_flag (cpp_reader *, unsigned int); | |
98e29890 | 104 | static bool strtolinenum (const uchar *, size_t, linenum_type *, bool *); |
3a79f5da | 105 | static void do_diagnostic (cpp_reader *, int, int, int); |
3e2a04b5 | 106 | static cpp_hashnode *lex_macro_node (cpp_reader *, bool); |
40109983 | 107 | static int undefine_macros (cpp_reader *, cpp_hashnode *, void *); |
f7fdd7a1 | 108 | static void do_include_common (cpp_reader *, enum include_type); |
109 | static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *, | |
110 | const cpp_hashnode *); | |
f7fdd7a1 | 111 | static int count_registered_pragmas (struct pragma_entry *); |
112 | static char ** save_registered_pragmas (struct pragma_entry *, char **); | |
113 | static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *, | |
114 | char **); | |
115 | static void do_pragma_once (cpp_reader *); | |
116 | static void do_pragma_poison (cpp_reader *); | |
117 | static void do_pragma_system_header (cpp_reader *); | |
118 | static void do_pragma_dependency (cpp_reader *); | |
a1f1781a | 119 | static void do_pragma_warning_or_error (cpp_reader *, bool error); |
120 | static void do_pragma_warning (cpp_reader *); | |
121 | static void do_pragma_error (cpp_reader *); | |
f7fdd7a1 | 122 | static void do_linemarker (cpp_reader *); |
123 | static const cpp_token *get_token_no_padding (cpp_reader *); | |
124 | static const cpp_token *get__Pragma_string (cpp_reader *); | |
1e6115dd | 125 | static void destringize_and_run (cpp_reader *, const cpp_string *, |
126 | source_location); | |
78ed8e3d | 127 | static int parse_answer (cpp_reader *, struct answer **, int, source_location); |
f7fdd7a1 | 128 | static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int); |
129 | static struct answer ** find_answer (cpp_hashnode *, const struct answer *); | |
130 | static void handle_assertion (cpp_reader *, const char *, int); | |
038c21f1 | 131 | static void do_pragma_push_macro (cpp_reader *); |
132 | static void do_pragma_pop_macro (cpp_reader *); | |
0d601ff4 | 133 | static void cpp_pop_definition (cpp_reader *, struct def_pragma_macro *); |
a4b4a940 | 134 | |
4bf559f2 | 135 | /* This is the table of directive handlers. It is ordered by |
136 | frequency of occurrence; the numbers at the end are directive | |
137 | counts from all the source code I have lying around (egcs and libc | |
138 | CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and | |
79bd622b | 139 | pcmcia-cs-3.0.9). This is no longer important as directive lookup |
fb9753dc | 140 | is now O(1). All extensions other than #warning, #include_next, |
141 | and #import are deprecated. The name is where the extension | |
142 | appears to have come from. */ | |
4bf559f2 | 143 | |
79bd622b | 144 | #define DIRECTIVE_TABLE \ |
145 | D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \ | |
bab5e68f | 146 | D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \ |
79bd622b | 147 | D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \ |
148 | D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \ | |
fb83e0d6 | 149 | D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \ |
79bd622b | 150 | D(else, T_ELSE, KANDR, COND) /* 9863 */ \ |
151 | D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \ | |
152 | D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \ | |
fb83e0d6 | 153 | D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \ |
154 | D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \ | |
79bd622b | 155 | D(error, T_ERROR, STDC89, 0) /* 475 */ \ |
156 | D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \ | |
157 | D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \ | |
bab5e68f | 158 | D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \ |
8f2ff12e | 159 | D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \ |
bab5e68f | 160 | D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \ |
fb9753dc | 161 | D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ |
162 | D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \ | |
8f2ff12e | 163 | D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */ |
81e19b31 | 164 | |
165 | /* #sccs is synonymous with #ident. */ | |
166 | #define do_sccs do_ident | |
ef33b55c | 167 | |
4bf559f2 | 168 | /* Use the table to generate a series of prototypes, an enum for the |
169 | directive names, and an array of directive handlers. */ | |
170 | ||
f7fdd7a1 | 171 | #define D(name, t, o, f) static void do_##name (cpp_reader *); |
4bf559f2 | 172 | DIRECTIVE_TABLE |
173 | #undef D | |
174 | ||
f80e83a9 | 175 | #define D(n, tag, o, f) tag, |
4bf559f2 | 176 | enum |
177 | { | |
178 | DIRECTIVE_TABLE | |
179 | N_DIRECTIVES | |
1d6fa33f | 180 | }; |
4bf559f2 | 181 | #undef D |
182 | ||
f80e83a9 | 183 | #define D(name, t, origin, flags) \ |
18e43155 | 184 | { do_##name, (const uchar *) #name, \ |
185 | sizeof #name - 1, origin, flags }, | |
79bd622b | 186 | static const directive dtable[] = |
4bf559f2 | 187 | { |
188 | DIRECTIVE_TABLE | |
189 | }; | |
190 | #undef D | |
191 | #undef DIRECTIVE_TABLE | |
1d6fa33f | 192 | |
25266990 | 193 | /* Wrapper struct directive for linemarkers. |
194 | The origin is more or less true - the original K+R cpp | |
195 | did use this notation in its preprocessed output. */ | |
196 | static const directive linemarker_dir = | |
197 | { | |
924bbf02 | 198 | do_linemarker, UC"#", 1, KANDR, IN_I |
25266990 | 199 | }; |
200 | ||
fb83e0d6 | 201 | #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF) |
1ea7ed21 | 202 | |
79bd622b | 203 | /* Skip any remaining tokens in a directive. */ |
204 | static void | |
f7fdd7a1 | 205 | skip_rest_of_line (cpp_reader *pfile) |
6060326b | 206 | { |
79bd622b | 207 | /* Discard all stacked contexts. */ |
6e5ef531 | 208 | while (pfile->context->prev) |
79bd622b | 209 | _cpp_pop_context (pfile); |
210 | ||
920b5d41 | 211 | /* Sweep up all tokens remaining on the line. */ |
c00e481c | 212 | if (! SEEN_EOL ()) |
213 | while (_cpp_lex_token (pfile)->type != CPP_EOF) | |
214 | ; | |
79bd622b | 215 | } |
6060326b | 216 | |
f0fbe519 | 217 | /* Helper function for check_oel. */ |
218 | ||
79bd622b | 219 | static void |
f0fbe519 | 220 | check_eol_1 (cpp_reader *pfile, bool expand, int reason) |
79bd622b | 221 | { |
6cda323a | 222 | if (! SEEN_EOL () && (expand |
223 | ? cpp_get_token (pfile) | |
224 | : _cpp_lex_token (pfile))->type != CPP_EOF) | |
f0fbe519 | 225 | cpp_pedwarning (pfile, reason, "extra tokens at end of #%s directive", |
226 | pfile->directive->name); | |
227 | } | |
228 | ||
229 | /* Variant of check_eol used for Wendif-labels warnings. */ | |
230 | ||
231 | static void | |
232 | check_eol_endif_labels (cpp_reader *pfile) | |
233 | { | |
234 | check_eol_1 (pfile, false, CPP_W_ENDIF_LABELS); | |
235 | } | |
236 | ||
237 | /* Ensure there are no stray tokens at the end of a directive. If | |
238 | EXPAND is true, tokens macro-expanding to nothing are allowed. */ | |
239 | ||
240 | static void | |
241 | check_eol (cpp_reader *pfile, bool expand) | |
242 | { | |
243 | check_eol_1 (pfile, expand, CPP_W_NONE); | |
79bd622b | 244 | } |
245 | ||
f789fe3e | 246 | /* Ensure there are no stray tokens other than comments at the end of |
247 | a directive, and gather the comments. */ | |
248 | static const cpp_token ** | |
249 | check_eol_return_comments (cpp_reader *pfile) | |
250 | { | |
251 | size_t c; | |
252 | size_t capacity = 8; | |
253 | const cpp_token **buf; | |
254 | ||
255 | buf = XNEWVEC (const cpp_token *, capacity); | |
256 | c = 0; | |
257 | if (! SEEN_EOL ()) | |
258 | { | |
259 | while (1) | |
260 | { | |
261 | const cpp_token *tok; | |
262 | ||
263 | tok = _cpp_lex_token (pfile); | |
264 | if (tok->type == CPP_EOF) | |
265 | break; | |
266 | if (tok->type != CPP_COMMENT) | |
267 | cpp_error (pfile, CPP_DL_PEDWARN, | |
268 | "extra tokens at end of #%s directive", | |
269 | pfile->directive->name); | |
270 | else | |
271 | { | |
272 | if (c + 1 >= capacity) | |
273 | { | |
274 | capacity *= 2; | |
275 | buf = XRESIZEVEC (const cpp_token *, buf, capacity); | |
276 | } | |
277 | buf[c] = tok; | |
278 | ++c; | |
279 | } | |
280 | } | |
281 | } | |
282 | buf[c] = NULL; | |
283 | return buf; | |
284 | } | |
285 | ||
9677c711 | 286 | /* Called when entering a directive, _Pragma or command-line directive. */ |
287 | static void | |
f7fdd7a1 | 288 | start_directive (cpp_reader *pfile) |
79bd622b | 289 | { |
343fd982 | 290 | /* Setup in-directive state. */ |
291 | pfile->state.in_directive = 1; | |
292 | pfile->state.save_comments = 0; | |
d6d3c909 | 293 | pfile->directive_result.type = CPP_PADDING; |
343fd982 | 294 | |
79bd622b | 295 | /* Some handlers need the position of the # for diagnostics. */ |
dbddc569 | 296 | pfile->directive_line = pfile->line_table->highest_line; |
9677c711 | 297 | } |
298 | ||
299 | /* Called when leaving a directive, _Pragma or command-line directive. */ | |
300 | static void | |
f7fdd7a1 | 301 | end_directive (cpp_reader *pfile, int skip_line) |
9677c711 | 302 | { |
65861c3e | 303 | if (CPP_OPTION (pfile, traditional)) |
fb83e0d6 | 304 | { |
bab5e68f | 305 | /* Revert change of prepare_directive_trad. */ |
65861c3e | 306 | if (!pfile->state.in_deferred_pragma) |
307 | pfile->state.prevent_expansion--; | |
bab5e68f | 308 | |
c455fac3 | 309 | if (pfile->directive != &dtable[T_DEFINE]) |
fb83e0d6 | 310 | _cpp_remove_overlay (pfile); |
311 | } | |
65861c3e | 312 | else if (pfile->state.in_deferred_pragma) |
313 | ; | |
9677c711 | 314 | /* We don't skip for an assembler #. */ |
c455fac3 | 315 | else if (skip_line) |
1ea7ed21 | 316 | { |
317 | skip_rest_of_line (pfile); | |
fb5ab82c | 318 | if (!pfile->keep_tokens) |
319 | { | |
320 | pfile->cur_run = &pfile->base_run; | |
321 | pfile->cur_token = pfile->base_run.base; | |
322 | } | |
1ea7ed21 | 323 | } |
9677c711 | 324 | |
325 | /* Restore state. */ | |
9677c711 | 326 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); |
327 | pfile->state.in_directive = 0; | |
bab5e68f | 328 | pfile->state.in_expression = 0; |
9677c711 | 329 | pfile->state.angled_headers = 0; |
330 | pfile->directive = 0; | |
331 | } | |
332 | ||
fb83e0d6 | 333 | /* Prepare to handle the directive in pfile->directive. */ |
334 | static void | |
f7fdd7a1 | 335 | prepare_directive_trad (cpp_reader *pfile) |
fb83e0d6 | 336 | { |
cbe337b6 | 337 | if (pfile->directive != &dtable[T_DEFINE]) |
fb83e0d6 | 338 | { |
c455fac3 | 339 | bool no_expand = (pfile->directive |
340 | && ! (pfile->directive->flags & EXPAND)); | |
01628c3c | 341 | bool was_skipping = pfile->state.skipping; |
fb83e0d6 | 342 | |
bab5e68f | 343 | pfile->state.in_expression = (pfile->directive == &dtable[T_IF] |
344 | || pfile->directive == &dtable[T_ELIF]); | |
8b837dff | 345 | if (pfile->state.in_expression) |
346 | pfile->state.skipping = false; | |
347 | ||
fb83e0d6 | 348 | if (no_expand) |
349 | pfile->state.prevent_expansion++; | |
640a5806 | 350 | _cpp_scan_out_logical_line (pfile, NULL, false); |
fb83e0d6 | 351 | if (no_expand) |
352 | pfile->state.prevent_expansion--; | |
8b837dff | 353 | |
01628c3c | 354 | pfile->state.skipping = was_skipping; |
fb83e0d6 | 355 | _cpp_overlay_buffer (pfile, pfile->out.base, |
356 | pfile->out.cur - pfile->out.base); | |
357 | } | |
bab5e68f | 358 | |
359 | /* Stop ISO C from expanding anything. */ | |
360 | pfile->state.prevent_expansion++; | |
fb83e0d6 | 361 | } |
362 | ||
d10cfa8d | 363 | /* Output diagnostics for a directive DIR. INDENTED is nonzero if |
d0d481d7 | 364 | the '#' was indented. */ |
d0d481d7 | 365 | static void |
f7fdd7a1 | 366 | directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented) |
d0d481d7 | 367 | { |
fb9753dc | 368 | /* Issue -pedantic or deprecated warnings for extensions. We let |
369 | -pedantic take precedence if both are applicable. */ | |
370 | if (! pfile->state.skipping) | |
371 | { | |
372 | if (dir->origin == EXTENSION | |
373 | && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc)) | |
374 | && CPP_PEDANTIC (pfile)) | |
375 | cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name); | |
376 | else if (((dir->flags & DEPRECATED) != 0 | |
377 | || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc))) | |
5ae82d58 | 378 | && CPP_OPTION (pfile, cpp_warn_deprecated)) |
3a79f5da | 379 | cpp_warning (pfile, CPP_W_DEPRECATED, |
380 | "#%s is a deprecated GCC extension", dir->name); | |
fb9753dc | 381 | } |
25266990 | 382 | |
383 | /* Traditionally, a directive is ignored unless its # is in | |
384 | column 1. Therefore in code intended to work with K+R | |
385 | compilers, directives added by C89 must have their # | |
386 | indented, and directives present in traditional C must not. | |
387 | This is true even of directives in skipped conditional | |
388 | blocks. #elif cannot be used at all. */ | |
389 | if (CPP_WTRADITIONAL (pfile)) | |
d0d481d7 | 390 | { |
25266990 | 391 | if (dir == &dtable[T_ELIF]) |
3a79f5da | 392 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
393 | "suggest not using #elif in traditional C"); | |
25266990 | 394 | else if (indented && dir->origin == KANDR) |
3a79f5da | 395 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
396 | "traditional C ignores #%s with the # indented", | |
397 | dir->name); | |
25266990 | 398 | else if (!indented && dir->origin != KANDR) |
3a79f5da | 399 | cpp_warning (pfile, CPP_W_TRADITIONAL, |
400 | "suggest hiding #%s from traditional C with an indented #", | |
401 | dir->name); | |
d0d481d7 | 402 | } |
403 | } | |
404 | ||
d10cfa8d | 405 | /* Check if we have a known directive. INDENTED is nonzero if the |
d0d481d7 | 406 | '#' of the directive was indented. This function is in this file |
f03668bd | 407 | to save unnecessarily exporting dtable etc. to lex.c. Returns |
d10cfa8d | 408 | nonzero if the line of tokens has been handled, zero if we should |
d0d481d7 | 409 | continue processing the line. */ |
9677c711 | 410 | int |
f7fdd7a1 | 411 | _cpp_handle_directive (cpp_reader *pfile, int indented) |
9677c711 | 412 | { |
9677c711 | 413 | const directive *dir = 0; |
c00e481c | 414 | const cpp_token *dname; |
d6af0368 | 415 | bool was_parsing_args = pfile->state.parsing_args; |
3eb3f293 | 416 | bool was_discarding_output = pfile->state.discarding_output; |
9677c711 | 417 | int skip = 1; |
418 | ||
3eb3f293 | 419 | if (was_discarding_output) |
420 | pfile->state.prevent_expansion = 0; | |
421 | ||
d6af0368 | 422 | if (was_parsing_args) |
423 | { | |
5ae82d58 | 424 | if (CPP_OPTION (pfile, cpp_pedantic)) |
d80d2074 | 425 | cpp_error (pfile, CPP_DL_PEDWARN, |
d6af0368 | 426 | "embedding a directive within macro arguments is not portable"); |
427 | pfile->state.parsing_args = 0; | |
428 | pfile->state.prevent_expansion = 0; | |
429 | } | |
9677c711 | 430 | start_directive (pfile); |
c00e481c | 431 | dname = _cpp_lex_token (pfile); |
338fa5f7 | 432 | |
c00e481c | 433 | if (dname->type == CPP_NAME) |
338fa5f7 | 434 | { |
2ee04baa | 435 | if (dname->val.node.node->is_directive) |
436 | dir = &dtable[dname->val.node.node->directive_index]; | |
338fa5f7 | 437 | } |
a2f10574 | 438 | /* We do not recognize the # followed by a number extension in |
d0d481d7 | 439 | assembler code. */ |
c00e481c | 440 | else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM) |
338fa5f7 | 441 | { |
25266990 | 442 | dir = &linemarker_dir; |
443 | if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed) | |
444 | && ! pfile->state.skipping) | |
d80d2074 | 445 | cpp_error (pfile, CPP_DL_PEDWARN, |
73328dce | 446 | "style of line directive is a GCC extension"); |
79bd622b | 447 | } |
338fa5f7 | 448 | |
79bd622b | 449 | if (dir) |
450 | { | |
d0d481d7 | 451 | /* If we have a directive that is not an opening conditional, |
452 | invalidate any control macro. */ | |
453 | if (! (dir->flags & IF_COND)) | |
454 | pfile->mi_valid = false; | |
455 | ||
456 | /* Kluge alert. In order to be sure that code like this | |
457 | ||
458 | #define HASH # | |
459 | HASH define foo bar | |
460 | ||
461 | does not cause '#define foo bar' to get executed when | |
462 | compiled with -save-temps, we recognize directives in | |
f03668bd | 463 | -fpreprocessed mode only if the # is in column 1. macro.c |
fcde64dc | 464 | puts a space in front of any '#' at the start of a macro. |
465 | ||
466 | We exclude the -fdirectives-only case because macro expansion | |
467 | has not been performed yet, and block comments can cause spaces | |
e8aa7eaf | 468 | to precede the directive. */ |
d0d481d7 | 469 | if (CPP_OPTION (pfile, preprocessed) |
fcde64dc | 470 | && !CPP_OPTION (pfile, directives_only) |
d0d481d7 | 471 | && (indented || !(dir->flags & IN_I))) |
946ce1b7 | 472 | { |
d0d481d7 | 473 | skip = 0; |
474 | dir = 0; | |
946ce1b7 | 475 | } |
476 | else | |
79bd622b | 477 | { |
d0d481d7 | 478 | /* In failed conditional groups, all non-conditional |
479 | directives are ignored. Before doing that, whether | |
480 | skipping or not, we should lex angle-bracketed headers | |
481 | correctly, and maybe output some diagnostics. */ | |
482 | pfile->state.angled_headers = dir->flags & INCL; | |
7825551f | 483 | pfile->state.directive_wants_padding = dir->flags & INCL; |
d0d481d7 | 484 | if (! CPP_OPTION (pfile, preprocessed)) |
485 | directive_diagnostics (pfile, dir, indented); | |
486 | if (pfile->state.skipping && !(dir->flags & COND)) | |
487 | dir = 0; | |
79bd622b | 488 | } |
489 | } | |
c00e481c | 490 | else if (dname->type == CPP_EOF) |
d0d481d7 | 491 | ; /* CPP_EOF is the "null directive". */ |
492 | else | |
79bd622b | 493 | { |
494 | /* An unknown directive. Don't complain about it in assembly | |
495 | source: we don't know where the comments are, and # may | |
496 | introduce assembler pseudo-ops. Don't complain about invalid | |
497 | directives in skipped conditional groups (6.10 p4). */ | |
5db5d057 | 498 | if (CPP_OPTION (pfile, lang) == CLK_ASM) |
d0d481d7 | 499 | skip = 0; |
500 | else if (!pfile->state.skipping) | |
d80d2074 | 501 | cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s", |
c00e481c | 502 | cpp_token_as_text (pfile, dname)); |
338fa5f7 | 503 | } |
504 | ||
94fed299 | 505 | pfile->directive = dir; |
506 | if (CPP_OPTION (pfile, traditional)) | |
507 | prepare_directive_trad (pfile); | |
508 | ||
d0d481d7 | 509 | if (dir) |
f7fdd7a1 | 510 | pfile->directive->handler (pfile); |
d0d481d7 | 511 | else if (skip == 0) |
512 | _cpp_backup_tokens (pfile, 1); | |
513 | ||
514 | end_directive (pfile, skip); | |
45f9f140 | 515 | if (was_parsing_args && !pfile->state.in_deferred_pragma) |
d6af0368 | 516 | { |
517 | /* Restore state when within macro args. */ | |
518 | pfile->state.parsing_args = 2; | |
519 | pfile->state.prevent_expansion = 1; | |
d6af0368 | 520 | } |
3eb3f293 | 521 | if (was_discarding_output) |
522 | pfile->state.prevent_expansion = 1; | |
9677c711 | 523 | return skip; |
f80e83a9 | 524 | } |
1d6fa33f | 525 | |
79bd622b | 526 | /* Directive handler wrapper used by the command line option |
a54e0bf8 | 527 | processor. BUF is \n terminated. */ |
79bd622b | 528 | static void |
f7fdd7a1 | 529 | run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count) |
e3630c4b | 530 | { |
b6d18b0a | 531 | cpp_push_buffer (pfile, (const uchar *) buf, count, |
9eb74666 | 532 | /* from_stage3 */ true); |
6cee4464 | 533 | start_directive (pfile); |
a54e0bf8 | 534 | |
535 | /* This is a short-term fix to prevent a leading '#' being | |
536 | interpreted as a directive. */ | |
537 | _cpp_clean_line (pfile); | |
538 | ||
7b5cc295 | 539 | pfile->directive = &dtable[dir_no]; |
fb83e0d6 | 540 | if (CPP_OPTION (pfile, traditional)) |
541 | prepare_directive_trad (pfile); | |
f7fdd7a1 | 542 | pfile->directive->handler (pfile); |
6cee4464 | 543 | end_directive (pfile, 1); |
4dfe8b74 | 544 | _cpp_pop_buffer (pfile); |
79bd622b | 545 | } |
546 | ||
547 | /* Checks for validity the macro name in #define, #undef, #ifdef and | |
3e2a04b5 | 548 | #ifndef directives. IS_DEF_OR_UNDEF is true if this call is |
549 | processing a #define or #undefine directive, and false | |
550 | otherwise. */ | |
f80e83a9 | 551 | static cpp_hashnode * |
3e2a04b5 | 552 | lex_macro_node (cpp_reader *pfile, bool is_def_or_undef) |
f80e83a9 | 553 | { |
fb83e0d6 | 554 | const cpp_token *token = _cpp_lex_token (pfile); |
c4357c92 | 555 | |
31674461 | 556 | /* The token immediately after #define must be an identifier. That |
f17c0828 | 557 | identifier may not be "defined", per C99 6.10.8p4. |
558 | In C++, it may not be any of the "named operators" either, | |
559 | per C++98 [lex.digraph], [lex.key]. | |
560 | Finally, the identifier may not have been poisoned. (In that case | |
9a26b86f | 561 | the lexer has issued the error message for us.) */ |
f15f6c8d | 562 | |
fb83e0d6 | 563 | if (token->type == CPP_NAME) |
564 | { | |
2ee04baa | 565 | cpp_hashnode *node = token->val.node.node; |
31674461 | 566 | |
3e2a04b5 | 567 | if (is_def_or_undef && node == pfile->spec_nodes.n_defined) |
d80d2074 | 568 | cpp_error (pfile, CPP_DL_ERROR, |
fb83e0d6 | 569 | "\"defined\" cannot be used as a macro name"); |
f6751ff2 | 570 | else if (is_def_or_undef |
571 | && (node == pfile->spec_nodes.n__has_include__ | |
572 | || node == pfile->spec_nodes.n__has_include_next__)) | |
573 | cpp_error (pfile, CPP_DL_ERROR, | |
574 | "\"__has_include__\" cannot be used as a macro name"); | |
fb83e0d6 | 575 | else if (! (node->flags & NODE_POISONED)) |
576 | return node; | |
71aa9da4 | 577 | } |
fb83e0d6 | 578 | else if (token->flags & NAMED_OP) |
d80d2074 | 579 | cpp_error (pfile, CPP_DL_ERROR, |
f15f6c8d | 580 | "\"%s\" cannot be used as a macro name as it is an operator in C++", |
2ee04baa | 581 | NODE_NAME (token->val.node.node)); |
fb83e0d6 | 582 | else if (token->type == CPP_EOF) |
d80d2074 | 583 | cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive", |
fb83e0d6 | 584 | pfile->directive->name); |
585 | else | |
d80d2074 | 586 | cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers"); |
ef33b55c | 587 | |
f15f6c8d | 588 | return NULL; |
1d6fa33f | 589 | } |
1d6fa33f | 590 | |
f03668bd | 591 | /* Process a #define directive. Most work is done in macro.c. */ |
69461e0d | 592 | static void |
f7fdd7a1 | 593 | do_define (cpp_reader *pfile) |
1d6fa33f | 594 | { |
3e2a04b5 | 595 | cpp_hashnode *node = lex_macro_node (pfile, true); |
c030ed73 | 596 | |
79bd622b | 597 | if (node) |
598 | { | |
9a26b86f | 599 | /* If we have been requested to expand comments into macros, |
600 | then re-enable saving of comments. */ | |
601 | pfile->state.save_comments = | |
602 | ! CPP_OPTION (pfile, discard_comments_in_macro_exp); | |
603 | ||
34c3de48 | 604 | if (pfile->cb.before_define) |
605 | pfile->cb.before_define (pfile); | |
606 | ||
79bd622b | 607 | if (_cpp_create_definition (pfile, node)) |
608 | if (pfile->cb.define) | |
f7fdd7a1 | 609 | pfile->cb.define (pfile, pfile->directive_line, node); |
34c3de48 | 610 | |
611 | node->flags &= ~NODE_USED; | |
79bd622b | 612 | } |
f80e83a9 | 613 | } |
614 | ||
e484a1cc | 615 | /* Handle #undef. Mark the identifier NT_VOID in the hash table. */ |
69461e0d | 616 | static void |
f7fdd7a1 | 617 | do_undef (cpp_reader *pfile) |
f80e83a9 | 618 | { |
3e2a04b5 | 619 | cpp_hashnode *node = lex_macro_node (pfile, true); |
2c63d6c8 | 620 | |
8b837dff | 621 | if (node) |
2c63d6c8 | 622 | { |
34c3de48 | 623 | if (pfile->cb.before_define) |
624 | pfile->cb.before_define (pfile); | |
625 | ||
6cae2504 | 626 | if (pfile->cb.undef) |
f7fdd7a1 | 627 | pfile->cb.undef (pfile, pfile->directive_line, node); |
2c63d6c8 | 628 | |
8b837dff | 629 | /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified |
630 | identifier is not currently defined as a macro name. */ | |
631 | if (node->type == NT_MACRO) | |
632 | { | |
633 | if (node->flags & NODE_WARN) | |
634 | cpp_error (pfile, CPP_DL_WARNING, | |
635 | "undefining \"%s\"", NODE_NAME (node)); | |
a7d2e480 | 636 | else if ((node->flags & NODE_BUILTIN) |
637 | && CPP_OPTION (pfile, warn_builtin_macro_redefined)) | |
638 | cpp_warning_with_line (pfile, CPP_W_BUILTIN_MACRO_REDEFINED, | |
639 | pfile->directive_line, 0, | |
640 | "undefining \"%s\"", NODE_NAME (node)); | |
2c63d6c8 | 641 | |
8b837dff | 642 | if (CPP_OPTION (pfile, warn_unused_macros)) |
643 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
71a7c282 | 644 | |
8b837dff | 645 | _cpp_free_definition (node); |
646 | } | |
2c63d6c8 | 647 | } |
8b837dff | 648 | |
6cda323a | 649 | check_eol (pfile, false); |
1d6fa33f | 650 | } |
651 | ||
40109983 | 652 | /* Undefine a single macro/assertion/whatever. */ |
653 | ||
654 | static int | |
3eb3f293 | 655 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
40109983 | 656 | void *data_p ATTRIBUTE_UNUSED) |
657 | { | |
3eb3f293 | 658 | /* Body of _cpp_free_definition inlined here for speed. |
659 | Macros and assertions no longer have anything to free. */ | |
660 | h->type = NT_VOID; | |
34c3de48 | 661 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); |
40109983 | 662 | return 1; |
663 | } | |
664 | ||
665 | /* Undefine all macros and assertions. */ | |
666 | ||
667 | void | |
668 | cpp_undef_all (cpp_reader *pfile) | |
669 | { | |
670 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
671 | } | |
672 | ||
673 | ||
79bd622b | 674 | /* Helper routine used by parse_include. Reinterpret the current line |
675 | as an h-char-sequence (< ... >); we are looking at the first token | |
58efbd5a | 676 | after the <. Returns a malloced filename. */ |
677 | static char * | |
f7fdd7a1 | 678 | glue_header_name (cpp_reader *pfile) |
79bd622b | 679 | { |
f9b5f742 | 680 | const cpp_token *token; |
58efbd5a | 681 | char *buffer; |
92b1c81a | 682 | size_t len, total_len = 0, capacity = 1024; |
79bd622b | 683 | |
684 | /* To avoid lexed tokens overwriting our glued name, we can only | |
685 | allocate from the string pool once we've lexed everything. */ | |
720aca92 | 686 | buffer = XNEWVEC (char, capacity); |
79bd622b | 687 | for (;;) |
688 | { | |
7825551f | 689 | token = get_token_no_padding (pfile); |
79bd622b | 690 | |
58efbd5a | 691 | if (token->type == CPP_GREATER) |
79bd622b | 692 | break; |
58efbd5a | 693 | if (token->type == CPP_EOF) |
694 | { | |
d80d2074 | 695 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
58efbd5a | 696 | break; |
697 | } | |
79bd622b | 698 | |
b1280514 | 699 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
92b1c81a | 700 | if (total_len + len > capacity) |
79bd622b | 701 | { |
92b1c81a | 702 | capacity = (capacity + len) * 2; |
720aca92 | 703 | buffer = XRESIZEVEC (char, buffer, capacity); |
79bd622b | 704 | } |
705 | ||
f9b5f742 | 706 | if (token->flags & PREV_WHITE) |
92b1c81a | 707 | buffer[total_len++] = ' '; |
79bd622b | 708 | |
bb1fa6bb | 709 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
710 | true) | |
58efbd5a | 711 | - (uchar *) buffer); |
79bd622b | 712 | } |
f80e83a9 | 713 | |
58efbd5a | 714 | buffer[total_len] = '\0'; |
715 | return buffer; | |
79bd622b | 716 | } |
1d6fa33f | 717 | |
58efbd5a | 718 | /* Returns the file name of #include, #include_next, #import and |
719 | #pragma dependency. The string is malloced and the caller should | |
78ed8e3d | 720 | free it. Returns NULL on error. LOCATION is the source location |
721 | of the file name. */ | |
722 | ||
58efbd5a | 723 | static const char * |
f789fe3e | 724 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
78ed8e3d | 725 | const cpp_token ***buf, source_location *location) |
1d6fa33f | 726 | { |
58efbd5a | 727 | char *fname; |
f9b5f742 | 728 | const cpp_token *header; |
1d6fa33f | 729 | |
79bd622b | 730 | /* Allow macro expansion. */ |
7825551f | 731 | header = get_token_no_padding (pfile); |
78ed8e3d | 732 | *location = header->src_loc; |
538ba11a | 733 | if ((header->type == CPP_STRING && header->val.str.text[0] != 'R') |
734 | || header->type == CPP_HEADER_NAME) | |
1d6fa33f | 735 | { |
720aca92 | 736 | fname = XNEWVEC (char, header->val.str.len - 1); |
4970d4c2 | 737 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
738 | fname[header->val.str.len - 2] = '\0'; | |
58efbd5a | 739 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
1d6fa33f | 740 | } |
58efbd5a | 741 | else if (header->type == CPP_LESS) |
1d6fa33f | 742 | { |
58efbd5a | 743 | fname = glue_header_name (pfile); |
744 | *pangle_brackets = 1; | |
745 | } | |
746 | else | |
747 | { | |
748 | const unsigned char *dir; | |
749 | ||
750 | if (pfile->directive == &dtable[T_PRAGMA]) | |
924bbf02 | 751 | dir = UC"pragma dependency"; |
58efbd5a | 752 | else |
753 | dir = pfile->directive->name; | |
d80d2074 | 754 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
58efbd5a | 755 | dir); |
756 | ||
f9b5f742 | 757 | return NULL; |
1d6fa33f | 758 | } |
1d6fa33f | 759 | |
36ec4203 | 760 | if (pfile->directive == &dtable[T_PRAGMA]) |
761 | { | |
762 | /* This pragma allows extra tokens after the file name. */ | |
763 | } | |
764 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) | |
3e52c418 | 765 | check_eol (pfile, true); |
f789fe3e | 766 | else |
767 | { | |
768 | /* If we are not discarding comments, then gather them while | |
769 | doing the eol check. */ | |
770 | *buf = check_eol_return_comments (pfile); | |
771 | } | |
772 | ||
58efbd5a | 773 | return fname; |
4bf559f2 | 774 | } |
d453a374 | 775 | |
0d6d8dc0 | 776 | /* Handle #include, #include_next and #import. */ |
69461e0d | 777 | static void |
f7fdd7a1 | 778 | do_include_common (cpp_reader *pfile, enum include_type type) |
4bf559f2 | 779 | { |
58efbd5a | 780 | const char *fname; |
781 | int angle_brackets; | |
f789fe3e | 782 | const cpp_token **buf = NULL; |
78ed8e3d | 783 | source_location location; |
f789fe3e | 784 | |
785 | /* Re-enable saving of comments if requested, so that the include | |
786 | callback can dump comments which follow #include. */ | |
787 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
58efbd5a | 788 | |
78ed8e3d | 789 | fname = parse_include (pfile, &angle_brackets, &buf, &location); |
58efbd5a | 790 | if (!fname) |
f789fe3e | 791 | { |
792 | if (buf) | |
793 | XDELETEVEC (buf); | |
794 | return; | |
795 | } | |
1d6fa33f | 796 | |
bd9e37a6 | 797 | if (!*fname) |
798 | { | |
78ed8e3d | 799 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, |
800 | "empty filename in #%s", | |
801 | pfile->directive->name); | |
f789fe3e | 802 | XDELETEVEC (fname); |
803 | if (buf) | |
804 | XDELETEVEC (buf); | |
bd9e37a6 | 805 | return; |
806 | } | |
807 | ||
f4578baa | 808 | /* Prevent #include recursion. */ |
ceec9c13 | 809 | if (pfile->line_table->depth >= CPP_STACK_MAX) |
d80d2074 | 810 | cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply"); |
58efbd5a | 811 | else |
968e3f9d | 812 | { |
58efbd5a | 813 | /* Get out of macro context, if we are. */ |
814 | skip_rest_of_line (pfile); | |
968e3f9d | 815 | |
58efbd5a | 816 | if (pfile->cb.include) |
f7fdd7a1 | 817 | pfile->cb.include (pfile, pfile->directive_line, |
f789fe3e | 818 | pfile->directive->name, fname, angle_brackets, |
819 | buf); | |
f4578baa | 820 | |
e557fc7f | 821 | _cpp_stack_include (pfile, fname, angle_brackets, type, location); |
58efbd5a | 822 | } |
f4578baa | 823 | |
f789fe3e | 824 | XDELETEVEC (fname); |
825 | if (buf) | |
826 | XDELETEVEC (buf); | |
4bf559f2 | 827 | } |
c4e0ec82 | 828 | |
69461e0d | 829 | static void |
f7fdd7a1 | 830 | do_include (cpp_reader *pfile) |
4bf559f2 | 831 | { |
0d6d8dc0 | 832 | do_include_common (pfile, IT_INCLUDE); |
833 | } | |
4bf559f2 | 834 | |
0d6d8dc0 | 835 | static void |
f7fdd7a1 | 836 | do_import (cpp_reader *pfile) |
0d6d8dc0 | 837 | { |
0d6d8dc0 | 838 | do_include_common (pfile, IT_IMPORT); |
4bf559f2 | 839 | } |
1d6fa33f | 840 | |
69461e0d | 841 | static void |
f7fdd7a1 | 842 | do_include_next (cpp_reader *pfile) |
4bf559f2 | 843 | { |
f4578baa | 844 | enum include_type type = IT_INCLUDE_NEXT; |
845 | ||
846 | /* If this is the primary source file, warn and use the normal | |
847 | search logic. */ | |
927b511f | 848 | if (cpp_in_primary_file (pfile)) |
f4578baa | 849 | { |
d80d2074 | 850 | cpp_error (pfile, CPP_DL_WARNING, |
f4578baa | 851 | "#include_next in primary source file"); |
852 | type = IT_INCLUDE; | |
853 | } | |
854 | do_include_common (pfile, type); | |
1d6fa33f | 855 | } |
1d6fa33f | 856 | |
25266990 | 857 | /* Subroutine of do_linemarker. Read possible flags after file name. |
858 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
859 | flag if it is valid, 0 at the end of the directive. Otherwise | |
860 | complain. */ | |
2fd3b10b | 861 | static unsigned int |
f7fdd7a1 | 862 | read_flag (cpp_reader *pfile, unsigned int last) |
314ab6d5 | 863 | { |
c00e481c | 864 | const cpp_token *token = _cpp_lex_token (pfile); |
314ab6d5 | 865 | |
c00e481c | 866 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
314ab6d5 | 867 | { |
c00e481c | 868 | unsigned int flag = token->val.str.text[0] - '0'; |
53c052ca | 869 | |
870 | if (flag > last && flag <= 4 | |
871 | && (flag != 4 || last == 3) | |
872 | && (flag != 2 || last == 0)) | |
873 | return flag; | |
314ab6d5 | 874 | } |
79bd622b | 875 | |
c00e481c | 876 | if (token->type != CPP_EOF) |
d80d2074 | 877 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
c00e481c | 878 | cpp_token_as_text (pfile, token)); |
79bd622b | 879 | return 0; |
314ab6d5 | 880 | } |
881 | ||
25266990 | 882 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
98e29890 | 883 | of length LEN, to binary; store it in NUMP, and return false if the |
884 | number was well-formed, true if not. WRAPPED is set to true if the | |
885 | number did not fit into 'unsigned long'. */ | |
886 | static bool | |
887 | strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped) | |
f80e83a9 | 888 | { |
4999c35b | 889 | linenum_type reg = 0; |
98e29890 | 890 | linenum_type reg_prev = 0; |
891 | ||
b6d18b0a | 892 | uchar c; |
98e29890 | 893 | *wrapped = false; |
f80e83a9 | 894 | while (len--) |
895 | { | |
896 | c = *str++; | |
897 | if (!ISDIGIT (c)) | |
98e29890 | 898 | return true; |
f80e83a9 | 899 | reg *= 10; |
900 | reg += c - '0'; | |
98e29890 | 901 | if (reg < reg_prev) |
902 | *wrapped = true; | |
903 | reg_prev = reg; | |
f80e83a9 | 904 | } |
905 | *nump = reg; | |
98e29890 | 906 | return false; |
f80e83a9 | 907 | } |
908 | ||
6d71dc85 | 909 | /* Interpret #line command. |
25266990 | 910 | Note that the filename string (if any) is a true string constant |
911 | (escapes are interpreted), unlike in #line. */ | |
69461e0d | 912 | static void |
f7fdd7a1 | 913 | do_line (cpp_reader *pfile) |
1d6fa33f | 914 | { |
e5f01cba | 915 | struct line_maps *line_table = pfile->line_table; |
551e34da | 916 | const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
b0d0794d | 917 | |
918 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down | |
919 | sysp right now. */ | |
920 | ||
97bfb9ef | 921 | unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
f9b5f742 | 922 | const cpp_token *token; |
97bfb9ef | 923 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
4999c35b | 924 | linenum_type new_lineno; |
79bd622b | 925 | |
d7503801 | 926 | /* C99 raised the minimum limit on #line numbers. */ |
4999c35b | 927 | linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
98e29890 | 928 | bool wrapped; |
d0d481d7 | 929 | |
79bd622b | 930 | /* #line commands expand macros. */ |
f9b5f742 | 931 | token = cpp_get_token (pfile); |
932 | if (token->type != CPP_NUMBER | |
4999c35b | 933 | || strtolinenum (token->val.str.text, token->val.str.len, |
98e29890 | 934 | &new_lineno, &wrapped)) |
1d6fa33f | 935 | { |
cde59b72 | 936 | if (token->type == CPP_EOF) |
937 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); | |
938 | else | |
939 | cpp_error (pfile, CPP_DL_ERROR, | |
940 | "\"%s\" after #line is not a positive integer", | |
941 | cpp_token_as_text (pfile, token)); | |
0653b94e | 942 | return; |
b1a9ff83 | 943 | } |
1d6fa33f | 944 | |
98e29890 | 945 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped)) |
d80d2074 | 946 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
98e29890 | 947 | else if (wrapped) |
948 | cpp_error (pfile, CPP_DL_WARNING, "line number out of range"); | |
1d6fa33f | 949 | |
f9b5f742 | 950 | token = cpp_get_token (pfile); |
951 | if (token->type == CPP_STRING) | |
a852e3b1 | 952 | { |
ebc03810 | 953 | cpp_string s = { 0, 0 }; |
7bc95b56 | 954 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
723ebaea | 955 | &s, CPP_STRING)) |
ebc03810 | 956 | new_file = (const char *)s.text; |
6cda323a | 957 | check_eol (pfile, true); |
25266990 | 958 | } |
959 | else if (token->type != CPP_EOF) | |
960 | { | |
d80d2074 | 961 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
25266990 | 962 | cpp_token_as_text (pfile, token)); |
963 | return; | |
964 | } | |
965 | ||
966 | skip_rest_of_line (pfile); | |
1eecdb28 | 967 | _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno, |
b0d0794d | 968 | map_sysp); |
e5f01cba | 969 | line_table->seen_line_directive = true; |
25266990 | 970 | } |
971 | ||
972 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
973 | different syntax and semantics from #line: Flags are allowed, | |
974 | and we never complain about the line number being too big. */ | |
975 | static void | |
f7fdd7a1 | 976 | do_linemarker (cpp_reader *pfile) |
25266990 | 977 | { |
e5f01cba | 978 | struct line_maps *line_table = pfile->line_table; |
551e34da | 979 | const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
25266990 | 980 | const cpp_token *token; |
97bfb9ef | 981 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
4999c35b | 982 | linenum_type new_lineno; |
97bfb9ef | 983 | unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
1eecdb28 | 984 | enum lc_reason reason = LC_RENAME_VERBATIM; |
25266990 | 985 | int flag; |
98e29890 | 986 | bool wrapped; |
25266990 | 987 | |
988 | /* Back up so we can get the number again. Putting this in | |
989 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
990 | some circumstances, which can segfault. */ | |
991 | _cpp_backup_tokens (pfile, 1); | |
992 | ||
993 | /* #line commands expand macros. */ | |
994 | token = cpp_get_token (pfile); | |
995 | if (token->type != CPP_NUMBER | |
4999c35b | 996 | || strtolinenum (token->val.str.text, token->val.str.len, |
98e29890 | 997 | &new_lineno, &wrapped)) |
25266990 | 998 | { |
cde59b72 | 999 | /* Unlike #line, there does not seem to be a way to get an EOF |
1000 | here. So, it should be safe to always spell the token. */ | |
d80d2074 | 1001 | cpp_error (pfile, CPP_DL_ERROR, |
1002 | "\"%s\" after # is not a positive integer", | |
25266990 | 1003 | cpp_token_as_text (pfile, token)); |
1004 | return; | |
b1a9ff83 | 1005 | } |
616814f8 | 1006 | |
25266990 | 1007 | token = cpp_get_token (pfile); |
1008 | if (token->type == CPP_STRING) | |
1009 | { | |
ebc03810 | 1010 | cpp_string s = { 0, 0 }; |
7bc95b56 | 1011 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
723ebaea | 1012 | 1, &s, CPP_STRING)) |
ebc03810 | 1013 | new_file = (const char *)s.text; |
787c3d1a | 1014 | |
25266990 | 1015 | new_sysp = 0; |
1016 | flag = read_flag (pfile, 0); | |
1017 | if (flag == 1) | |
1018 | { | |
1019 | reason = LC_ENTER; | |
1020 | /* Fake an include for cpp_included (). */ | |
1021 | _cpp_fake_include (pfile, new_file); | |
1022 | flag = read_flag (pfile, flag); | |
1023 | } | |
1024 | else if (flag == 2) | |
1025 | { | |
1026 | reason = LC_LEAVE; | |
1027 | flag = read_flag (pfile, flag); | |
1028 | } | |
1029 | if (flag == 3) | |
79bd622b | 1030 | { |
25266990 | 1031 | new_sysp = 1; |
1032 | flag = read_flag (pfile, flag); | |
1033 | if (flag == 4) | |
1034 | new_sysp = 2; | |
79bd622b | 1035 | } |
5d70627e | 1036 | pfile->buffer->sysp = new_sysp; |
25266990 | 1037 | |
6cda323a | 1038 | check_eol (pfile, false); |
f80e83a9 | 1039 | } |
f9b5f742 | 1040 | else if (token->type != CPP_EOF) |
d7503801 | 1041 | { |
d80d2074 | 1042 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
f9b5f742 | 1043 | cpp_token_as_text (pfile, token)); |
d7503801 | 1044 | return; |
1045 | } | |
1d6fa33f | 1046 | |
fb5ab82c | 1047 | skip_rest_of_line (pfile); |
5b500807 | 1048 | |
f175ba0f | 1049 | if (reason == LC_LEAVE) |
1050 | { | |
09321e85 | 1051 | /* Reread map since cpp_get_token can invalidate it with a |
1052 | reallocation. */ | |
1053 | map = LINEMAPS_LAST_ORDINARY_MAP (line_table); | |
f175ba0f | 1054 | const line_map_ordinary *from; |
1055 | if (MAIN_FILE_P (map) | |
1056 | || (new_file | |
1057 | && (from = INCLUDED_FROM (pfile->line_table, map)) != NULL | |
1058 | && filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0)) | |
1059 | { | |
1060 | cpp_warning (pfile, CPP_W_NONE, | |
09321e85 | 1061 | "file \"%s\" linemarker ignored due to " |
1062 | "incorrect nesting", new_file); | |
f175ba0f | 1063 | return; |
1064 | } | |
1065 | } | |
5b500807 | 1066 | /* Compensate for the increment in linemap_add that occurs in |
1067 | _cpp_do_file_change. We're currently at the start of the line | |
1068 | *following* the #line directive. A separate source_location for this | |
1069 | location makes no sense (until we do the LC_LEAVE), and | |
1070 | complicates LAST_SOURCE_LINE_LOCATION. */ | |
1071 | pfile->line_table->highest_location--; | |
1072 | ||
5a9f87bb | 1073 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
e5f01cba | 1074 | line_table->seen_line_directive = true; |
d7503801 | 1075 | } |
1076 | ||
1ea7ed21 | 1077 | /* Arrange the file_change callback. pfile->line has changed to |
f85fcf2b | 1078 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
3fb1e43b | 1079 | header, 2 for a system header that needs to be extern "C" protected, |
f85fcf2b | 1080 | and zero otherwise. */ |
4087613d | 1081 | void |
f7fdd7a1 | 1082 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
4999c35b | 1083 | const char *to_file, linenum_type file_line, |
f7fdd7a1 | 1084 | unsigned int sysp) |
d7503801 | 1085 | { |
551e34da | 1086 | linemap_assert (reason != LC_ENTER_MACRO); |
610625e3 | 1087 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
1088 | to_file, file_line); | |
551e34da | 1089 | const line_map_ordinary *ord_map = NULL; |
dbddc569 | 1090 | if (map != NULL) |
551e34da | 1091 | { |
1092 | ord_map = linemap_check_ordinary (map); | |
1093 | linemap_line_start (pfile->line_table, | |
1094 | ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map), | |
1095 | 127); | |
1096 | } | |
38692459 | 1097 | |
4087613d | 1098 | if (pfile->cb.file_change) |
551e34da | 1099 | pfile->cb.file_change (pfile, ord_map); |
1d6fa33f | 1100 | } |
616814f8 | 1101 | |
e484a1cc | 1102 | /* Report a warning or error detected by the program we are |
1103 | processing. Use the directive's tokens in the error message. */ | |
69461e0d | 1104 | static void |
3a79f5da | 1105 | do_diagnostic (cpp_reader *pfile, int code, int reason, int print_dir) |
1d6fa33f | 1106 | { |
c0770282 | 1107 | const unsigned char *dir_name; |
1108 | unsigned char *line; | |
1109 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1110 | ||
1111 | if (print_dir) | |
1112 | dir_name = pfile->directive->name; | |
1113 | else | |
1114 | dir_name = NULL; | |
1115 | pfile->state.prevent_expansion++; | |
1116 | line = cpp_output_line_to_string (pfile, dir_name); | |
1117 | pfile->state.prevent_expansion--; | |
1118 | ||
3a79f5da | 1119 | if (code == CPP_DL_WARNING_SYSHDR && reason) |
1120 | cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line); | |
1121 | else if (code == CPP_DL_WARNING && reason) | |
1122 | cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line); | |
1123 | else | |
1124 | cpp_error_with_line (pfile, code, src_loc, 0, "%s", line); | |
c0770282 | 1125 | free (line); |
1d6fa33f | 1126 | } |
1127 | ||
f3b83e9b | 1128 | static void |
f7fdd7a1 | 1129 | do_error (cpp_reader *pfile) |
f3b83e9b | 1130 | { |
3a79f5da | 1131 | do_diagnostic (pfile, CPP_DL_ERROR, 0, 1); |
f3b83e9b | 1132 | } |
1d6fa33f | 1133 | |
69461e0d | 1134 | static void |
f7fdd7a1 | 1135 | do_warning (cpp_reader *pfile) |
1d6fa33f | 1136 | { |
2ef5e3d3 | 1137 | /* We want #warning diagnostics to be emitted in system headers too. */ |
3a79f5da | 1138 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1); |
1d6fa33f | 1139 | } |
1140 | ||
4e29fcb2 | 1141 | /* Report program identification. */ |
69461e0d | 1142 | static void |
f7fdd7a1 | 1143 | do_ident (cpp_reader *pfile) |
1d6fa33f | 1144 | { |
f9b5f742 | 1145 | const cpp_token *str = cpp_get_token (pfile); |
6cae2504 | 1146 | |
f9b5f742 | 1147 | if (str->type != CPP_STRING) |
81e19b31 | 1148 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
1149 | pfile->directive->name); | |
79bd622b | 1150 | else if (pfile->cb.ident) |
f7fdd7a1 | 1151 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
4e29fcb2 | 1152 | |
6cda323a | 1153 | check_eol (pfile, false); |
1d6fa33f | 1154 | } |
1155 | ||
c6f14ce5 | 1156 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
1157 | matching entry, or NULL if none is found. The returned entry could | |
1158 | be the start of a namespace chain, or a pragma. */ | |
1159 | static struct pragma_entry * | |
f7fdd7a1 | 1160 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
3f075661 | 1161 | { |
5c43d212 | 1162 | while (chain && chain->pragma != pragma) |
1163 | chain = chain->next; | |
c6f14ce5 | 1164 | |
1165 | return chain; | |
1166 | } | |
1167 | ||
b75b98aa | 1168 | /* Create and insert a blank pragma entry at the beginning of a |
1169 | singly-linked CHAIN. */ | |
c6f14ce5 | 1170 | static struct pragma_entry * |
b75b98aa | 1171 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
3f075661 | 1172 | { |
720aca92 | 1173 | struct pragma_entry *new_entry; |
6cae2504 | 1174 | |
720aca92 | 1175 | new_entry = (struct pragma_entry *) |
e6a5f963 | 1176 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
6cae2504 | 1177 | |
b75b98aa | 1178 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
720aca92 | 1179 | new_entry->next = *chain; |
b75b98aa | 1180 | |
720aca92 | 1181 | *chain = new_entry; |
1182 | return new_entry; | |
6cae2504 | 1183 | } |
3f075661 | 1184 | |
c6f14ce5 | 1185 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
b75b98aa | 1186 | goes in the global namespace. */ |
1187 | static struct pragma_entry * | |
1188 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, | |
1189 | bool allow_name_expansion) | |
3f075661 | 1190 | { |
c6f14ce5 | 1191 | struct pragma_entry **chain = &pfile->pragmas; |
1192 | struct pragma_entry *entry; | |
5c43d212 | 1193 | const cpp_hashnode *node; |
c6f14ce5 | 1194 | |
c6f14ce5 | 1195 | if (space) |
6cae2504 | 1196 | { |
924bbf02 | 1197 | node = cpp_lookup (pfile, UC space, strlen (space)); |
5c43d212 | 1198 | entry = lookup_pragma_entry (*chain, node); |
c6f14ce5 | 1199 | if (!entry) |
b75b98aa | 1200 | { |
1201 | entry = new_pragma_entry (pfile, chain); | |
1202 | entry->pragma = node; | |
1203 | entry->is_nspace = true; | |
1204 | entry->allow_expansion = allow_name_expansion; | |
1205 | } | |
c6f14ce5 | 1206 | else if (!entry->is_nspace) |
1207 | goto clash; | |
b75b98aa | 1208 | else if (entry->allow_expansion != allow_name_expansion) |
1209 | { | |
1210 | cpp_error (pfile, CPP_DL_ICE, | |
1211 | "registering pragmas in namespace \"%s\" with mismatched " | |
1212 | "name expansion", space); | |
1213 | return NULL; | |
1214 | } | |
c6f14ce5 | 1215 | chain = &entry->u.space; |
6cae2504 | 1216 | } |
b75b98aa | 1217 | else if (allow_name_expansion) |
1218 | { | |
1219 | cpp_error (pfile, CPP_DL_ICE, | |
1220 | "registering pragma \"%s\" with name expansion " | |
1221 | "and no namespace", name); | |
1222 | return NULL; | |
1223 | } | |
6cae2504 | 1224 | |
c6f14ce5 | 1225 | /* Check for duplicates. */ |
924bbf02 | 1226 | node = cpp_lookup (pfile, UC name, strlen (name)); |
5c43d212 | 1227 | entry = lookup_pragma_entry (*chain, node); |
b75b98aa | 1228 | if (entry == NULL) |
c6f14ce5 | 1229 | { |
b75b98aa | 1230 | entry = new_pragma_entry (pfile, chain); |
1231 | entry->pragma = node; | |
1232 | return entry; | |
c6f14ce5 | 1233 | } |
b75b98aa | 1234 | |
1235 | if (entry->is_nspace) | |
1236 | clash: | |
1237 | cpp_error (pfile, CPP_DL_ICE, | |
1238 | "registering \"%s\" as both a pragma and a pragma namespace", | |
1239 | NODE_NAME (node)); | |
1240 | else if (space) | |
1241 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", | |
1242 | space, name); | |
c6f14ce5 | 1243 | else |
b75b98aa | 1244 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
1245 | ||
1246 | return NULL; | |
1247 | } | |
1248 | ||
1249 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ | |
1250 | static void | |
1251 | register_pragma_internal (cpp_reader *pfile, const char *space, | |
1252 | const char *name, pragma_cb handler) | |
1253 | { | |
1254 | struct pragma_entry *entry; | |
1255 | ||
1256 | entry = register_pragma_1 (pfile, space, name, false); | |
1257 | entry->is_internal = true; | |
1258 | entry->u.handler = handler; | |
d6d3c909 | 1259 | } |
1260 | ||
1261 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it | |
1262 | goes in the global namespace. HANDLER is the handler it will call, | |
68bf2ad9 | 1263 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
1264 | expansion while parsing pragma NAME. This function is exported | |
1265 | from libcpp. */ | |
d6d3c909 | 1266 | void |
1267 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, | |
68bf2ad9 | 1268 | pragma_cb handler, bool allow_expansion) |
d6d3c909 | 1269 | { |
b75b98aa | 1270 | struct pragma_entry *entry; |
1271 | ||
1272 | if (!handler) | |
1273 | { | |
1274 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); | |
1275 | return; | |
1276 | } | |
1277 | ||
1278 | entry = register_pragma_1 (pfile, space, name, false); | |
1279 | if (entry) | |
1280 | { | |
1281 | entry->allow_expansion = allow_expansion; | |
1282 | entry->u.handler = handler; | |
1283 | } | |
6cae2504 | 1284 | } |
c6f14ce5 | 1285 | |
b75b98aa | 1286 | /* Similarly, but create mark the pragma for deferred processing. |
1287 | When found, a CPP_PRAGMA token will be insertted into the stream | |
1288 | with IDENT in the token->u.pragma slot. */ | |
1289 | void | |
1290 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, | |
1291 | const char *name, unsigned int ident, | |
1292 | bool allow_expansion, bool allow_name_expansion) | |
1293 | { | |
1294 | struct pragma_entry *entry; | |
1295 | ||
1296 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); | |
1297 | if (entry) | |
1298 | { | |
1299 | entry->is_deferred = true; | |
1300 | entry->allow_expansion = allow_expansion; | |
1301 | entry->u.ident = ident; | |
1302 | } | |
1303 | } | |
1304 | ||
c6f14ce5 | 1305 | /* Register the pragmas the preprocessor itself handles. */ |
6cae2504 | 1306 | void |
f7fdd7a1 | 1307 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
6cae2504 | 1308 | { |
c6f14ce5 | 1309 | /* Pragmas in the global namespace. */ |
b75b98aa | 1310 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
038c21f1 | 1311 | register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro); |
1312 | register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro); | |
6cae2504 | 1313 | |
c6f14ce5 | 1314 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
b75b98aa | 1315 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
1316 | register_pragma_internal (pfile, "GCC", "system_header", | |
1317 | do_pragma_system_header); | |
1318 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); | |
a1f1781a | 1319 | register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning); |
1320 | register_pragma_internal (pfile, "GCC", "error", do_pragma_error); | |
3f075661 | 1321 | } |
1d6fa33f | 1322 | |
573aba85 | 1323 | /* Return the number of registered pragmas in PE. */ |
1324 | ||
1325 | static int | |
f7fdd7a1 | 1326 | count_registered_pragmas (struct pragma_entry *pe) |
573aba85 | 1327 | { |
1328 | int ct = 0; | |
1329 | for (; pe != NULL; pe = pe->next) | |
1330 | { | |
1331 | if (pe->is_nspace) | |
1332 | ct += count_registered_pragmas (pe->u.space); | |
1333 | ct++; | |
1334 | } | |
1335 | return ct; | |
1336 | } | |
1337 | ||
1338 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1339 | and return a pointer to the next free space in SD. */ | |
1340 | ||
1341 | static char ** | |
f7fdd7a1 | 1342 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
573aba85 | 1343 | { |
1344 | for (; pe != NULL; pe = pe->next) | |
1345 | { | |
1346 | if (pe->is_nspace) | |
1347 | sd = save_registered_pragmas (pe->u.space, sd); | |
720aca92 | 1348 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
1349 | HT_LEN (&pe->pragma->ident), | |
1350 | HT_LEN (&pe->pragma->ident) + 1); | |
573aba85 | 1351 | } |
1352 | return sd; | |
1353 | } | |
1354 | ||
1355 | /* Return a newly-allocated array which saves the names of the | |
1356 | registered pragmas. */ | |
1357 | ||
1358 | char ** | |
f7fdd7a1 | 1359 | _cpp_save_pragma_names (cpp_reader *pfile) |
573aba85 | 1360 | { |
1361 | int ct = count_registered_pragmas (pfile->pragmas); | |
3b298764 | 1362 | char **result = XNEWVEC (char *, ct); |
573aba85 | 1363 | (void) save_registered_pragmas (pfile->pragmas, result); |
1364 | return result; | |
1365 | } | |
1366 | ||
1367 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1368 | and return a pointer to the next unused name in SD. */ | |
1369 | ||
1370 | static char ** | |
f7fdd7a1 | 1371 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1372 | char **sd) | |
573aba85 | 1373 | { |
1374 | for (; pe != NULL; pe = pe->next) | |
1375 | { | |
1376 | if (pe->is_nspace) | |
1377 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
924bbf02 | 1378 | pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd)); |
573aba85 | 1379 | free (*sd); |
1380 | sd++; | |
1381 | } | |
1382 | return sd; | |
1383 | } | |
1384 | ||
1385 | /* Restore the names of the registered pragmas from SAVED. */ | |
1386 | ||
1387 | void | |
f7fdd7a1 | 1388 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
573aba85 | 1389 | { |
1390 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1391 | free (saved); | |
1392 | } | |
1393 | ||
c6f14ce5 | 1394 | /* Pragmata handling. We handle some, and pass the rest on to the |
1395 | front end. C99 defines three pragmas and says that no macro | |
1396 | expansion is to be performed on them; whether or not macro | |
1397 | expansion happens for other pragmas is implementation defined. | |
b75b98aa | 1398 | This implementation allows for a mix of both, since GCC did not |
1399 | traditionally macro expand its (few) pragmas, whereas OpenMP | |
1400 | specifies that macro expansion should happen. */ | |
69461e0d | 1401 | static void |
f7fdd7a1 | 1402 | do_pragma (cpp_reader *pfile) |
1d6fa33f | 1403 | { |
c6f14ce5 | 1404 | const struct pragma_entry *p = NULL; |
307df3d0 | 1405 | const cpp_token *token, *pragma_token; |
1406 | source_location pragma_token_virt_loc = 0; | |
add258d7 | 1407 | cpp_token ns_token; |
c6f14ce5 | 1408 | unsigned int count = 1; |
2e398cc7 | 1409 | |
79bd622b | 1410 | pfile->state.prevent_expansion++; |
6cae2504 | 1411 | |
307df3d0 | 1412 | pragma_token = token = cpp_get_token_with_location (pfile, |
1413 | &pragma_token_virt_loc); | |
add258d7 | 1414 | ns_token = *token; |
f9b5f742 | 1415 | if (token->type == CPP_NAME) |
c0b823af | 1416 | { |
2ee04baa | 1417 | p = lookup_pragma_entry (pfile->pragmas, token->val.node.node); |
c6f14ce5 | 1418 | if (p && p->is_nspace) |
6cae2504 | 1419 | { |
b75b98aa | 1420 | bool allow_name_expansion = p->allow_expansion; |
1421 | if (allow_name_expansion) | |
8c6425eb | 1422 | pfile->state.prevent_expansion--; |
2c694d44 | 1423 | |
c6f14ce5 | 1424 | token = cpp_get_token (pfile); |
1425 | if (token->type == CPP_NAME) | |
2ee04baa | 1426 | p = lookup_pragma_entry (p->u.space, token->val.node.node); |
c6f14ce5 | 1427 | else |
1428 | p = NULL; | |
b75b98aa | 1429 | if (allow_name_expansion) |
1430 | pfile->state.prevent_expansion++; | |
1431 | count = 2; | |
6cae2504 | 1432 | } |
6cae2504 | 1433 | } |
f80e83a9 | 1434 | |
a095c1b5 | 1435 | if (p) |
9b3d6630 | 1436 | { |
b75b98aa | 1437 | if (p->is_deferred) |
1438 | { | |
307df3d0 | 1439 | pfile->directive_result.src_loc = pragma_token_virt_loc; |
b75b98aa | 1440 | pfile->directive_result.type = CPP_PRAGMA; |
1441 | pfile->directive_result.flags = pragma_token->flags; | |
1442 | pfile->directive_result.val.pragma = p->u.ident; | |
1443 | pfile->state.in_deferred_pragma = true; | |
1444 | pfile->state.pragma_allow_expansion = p->allow_expansion; | |
1445 | if (!p->allow_expansion) | |
1446 | pfile->state.prevent_expansion++; | |
1447 | } | |
1448 | else | |
a095c1b5 | 1449 | { |
b75b98aa | 1450 | /* Since the handler below doesn't get the line number, that |
1451 | it might need for diagnostics, make sure it has the right | |
a095c1b5 | 1452 | numbers in place. */ |
1453 | if (pfile->cb.line_change) | |
1454 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
b75b98aa | 1455 | if (p->allow_expansion) |
68bf2ad9 | 1456 | pfile->state.prevent_expansion--; |
a095c1b5 | 1457 | (*p->u.handler) (pfile); |
b75b98aa | 1458 | if (p->allow_expansion) |
68bf2ad9 | 1459 | pfile->state.prevent_expansion++; |
a095c1b5 | 1460 | } |
d6d3c909 | 1461 | } |
38692459 | 1462 | else if (pfile->cb.def_pragma) |
fb5ab82c | 1463 | { |
add258d7 | 1464 | if (count == 1 || pfile->context->prev == NULL) |
1465 | _cpp_backup_tokens (pfile, count); | |
1466 | else | |
1467 | { | |
1468 | /* Invalid name comes from macro expansion, _cpp_backup_tokens | |
1469 | won't allow backing 2 tokens. */ | |
1470 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook | |
1471 | reads both tokens, we could perhaps free it, but if it doesn't, | |
1472 | we don't know the exact lifespan. */ | |
1473 | cpp_token *toks = XNEWVEC (cpp_token, 2); | |
1474 | toks[0] = ns_token; | |
1475 | toks[0].flags |= NO_EXPAND; | |
1476 | toks[1] = *token; | |
1477 | toks[1].flags |= NO_EXPAND; | |
1478 | _cpp_push_token_context (pfile, NULL, toks, 2); | |
1479 | } | |
f7fdd7a1 | 1480 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
fb5ab82c | 1481 | } |
c6f14ce5 | 1482 | |
5621a364 | 1483 | pfile->state.prevent_expansion--; |
3f075661 | 1484 | } |
1485 | ||
e484a1cc | 1486 | /* Handle #pragma once. */ |
6cae2504 | 1487 | static void |
f7fdd7a1 | 1488 | do_pragma_once (cpp_reader *pfile) |
4e29fcb2 | 1489 | { |
927b511f | 1490 | if (cpp_in_primary_file (pfile)) |
d80d2074 | 1491 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
79bd622b | 1492 | |
6cda323a | 1493 | check_eol (pfile, false); |
68faebf4 | 1494 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
4e29fcb2 | 1495 | } |
c9d838e1 | 1496 | |
038c21f1 | 1497 | /* Handle #pragma push_macro(STRING). */ |
1498 | static void | |
1499 | do_pragma_push_macro (cpp_reader *pfile) | |
1500 | { | |
0d601ff4 | 1501 | cpp_hashnode *node; |
1502 | size_t defnlen; | |
1503 | const uchar *defn = NULL; | |
038c21f1 | 1504 | char *macroname, *dest; |
1505 | const char *limit, *src; | |
1506 | const cpp_token *txt; | |
1507 | struct def_pragma_macro *c; | |
1508 | ||
1509 | txt = get__Pragma_string (pfile); | |
1510 | if (!txt) | |
1511 | { | |
1512 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1513 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, | |
1514 | "invalid #pragma push_macro directive"); | |
1515 | check_eol (pfile, false); | |
1516 | skip_rest_of_line (pfile); | |
1517 | return; | |
1518 | } | |
1519 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1520 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1521 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1522 | while (src < limit) | |
1523 | { | |
1524 | /* We know there is a character following the backslash. */ | |
1525 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1526 | src++; | |
1527 | *dest++ = *src++; | |
1528 | } | |
1529 | *dest = 0; | |
1530 | check_eol (pfile, false); | |
1531 | skip_rest_of_line (pfile); | |
1532 | c = XNEW (struct def_pragma_macro); | |
0d601ff4 | 1533 | memset (c, 0, sizeof (struct def_pragma_macro)); |
038c21f1 | 1534 | c->name = XNEWVAR (char, strlen (macroname) + 1); |
1535 | strcpy (c->name, macroname); | |
1536 | c->next = pfile->pushed_macros; | |
0d601ff4 | 1537 | node = _cpp_lex_identifier (pfile, c->name); |
1538 | if (node->type == NT_VOID) | |
1539 | c->is_undef = 1; | |
1540 | else | |
1541 | { | |
1542 | defn = cpp_macro_definition (pfile, node); | |
1543 | defnlen = ustrlen (defn); | |
1544 | c->definition = XNEWVEC (uchar, defnlen + 2); | |
1545 | c->definition[defnlen] = '\n'; | |
1546 | c->definition[defnlen + 1] = 0; | |
1547 | c->line = node->value.macro->line; | |
1548 | c->syshdr = node->value.macro->syshdr; | |
1549 | c->used = node->value.macro->used; | |
1550 | memcpy (c->definition, defn, defnlen); | |
1551 | } | |
1552 | ||
038c21f1 | 1553 | pfile->pushed_macros = c; |
1554 | } | |
1555 | ||
1556 | /* Handle #pragma pop_macro(STRING). */ | |
1557 | static void | |
1558 | do_pragma_pop_macro (cpp_reader *pfile) | |
1559 | { | |
1560 | char *macroname, *dest; | |
1561 | const char *limit, *src; | |
1562 | const cpp_token *txt; | |
1563 | struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros; | |
1564 | txt = get__Pragma_string (pfile); | |
1565 | if (!txt) | |
1566 | { | |
1567 | source_location src_loc = pfile->cur_token[-1].src_loc; | |
1568 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, | |
1569 | "invalid #pragma pop_macro directive"); | |
1570 | check_eol (pfile, false); | |
1571 | skip_rest_of_line (pfile); | |
1572 | return; | |
1573 | } | |
1574 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1575 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1576 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1577 | while (src < limit) | |
1578 | { | |
1579 | /* We know there is a character following the backslash. */ | |
1580 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1581 | src++; | |
1582 | *dest++ = *src++; | |
1583 | } | |
1584 | *dest = 0; | |
1585 | check_eol (pfile, false); | |
1586 | skip_rest_of_line (pfile); | |
1587 | ||
1588 | while (c != NULL) | |
1589 | { | |
1590 | if (!strcmp (c->name, macroname)) | |
1591 | { | |
1592 | if (!l) | |
1593 | pfile->pushed_macros = c->next; | |
1594 | else | |
1595 | l->next = c->next; | |
0d601ff4 | 1596 | cpp_pop_definition (pfile, c); |
1597 | free (c->definition); | |
038c21f1 | 1598 | free (c->name); |
1599 | free (c); | |
1600 | break; | |
1601 | } | |
1602 | l = c; | |
1603 | c = c->next; | |
1604 | } | |
1605 | } | |
1606 | ||
8e1db61c | 1607 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1608 | that the lexer produces a hard error for each subsequent usage. */ | |
6cae2504 | 1609 | static void |
f7fdd7a1 | 1610 | do_pragma_poison (cpp_reader *pfile) |
4e29fcb2 | 1611 | { |
c00e481c | 1612 | const cpp_token *tok; |
c4abf88d | 1613 | cpp_hashnode *hp; |
4e29fcb2 | 1614 | |
79bd622b | 1615 | pfile->state.poisoned_ok = 1; |
4e29fcb2 | 1616 | for (;;) |
1617 | { | |
c00e481c | 1618 | tok = _cpp_lex_token (pfile); |
1619 | if (tok->type == CPP_EOF) | |
4e29fcb2 | 1620 | break; |
c00e481c | 1621 | if (tok->type != CPP_NAME) |
c9d838e1 | 1622 | { |
d80d2074 | 1623 | cpp_error (pfile, CPP_DL_ERROR, |
1624 | "invalid #pragma GCC poison directive"); | |
79bd622b | 1625 | break; |
c9d838e1 | 1626 | } |
1627 | ||
2ee04baa | 1628 | hp = tok->val.node.node; |
79bd622b | 1629 | if (hp->flags & NODE_POISONED) |
1630 | continue; | |
1631 | ||
1632 | if (hp->type == NT_MACRO) | |
d80d2074 | 1633 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
73328dce | 1634 | NODE_NAME (hp)); |
79bd622b | 1635 | _cpp_free_definition (hp); |
1636 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
c9d838e1 | 1637 | } |
79bd622b | 1638 | pfile->state.poisoned_ok = 0; |
1d6fa33f | 1639 | } |
e35f919d | 1640 | |
1641 | /* Mark the current header as a system header. This will suppress | |
1642 | some categories of warnings (notably those from -pedantic). It is | |
1643 | intended for use in system libraries that cannot be implemented in | |
1644 | conforming C, but cannot be certain that their headers appear in a | |
1645 | system include directory. To prevent abuse, it is rejected in the | |
1646 | primary source file. */ | |
6cae2504 | 1647 | static void |
f7fdd7a1 | 1648 | do_pragma_system_header (cpp_reader *pfile) |
e35f919d | 1649 | { |
927b511f | 1650 | if (cpp_in_primary_file (pfile)) |
d80d2074 | 1651 | cpp_error (pfile, CPP_DL_WARNING, |
73328dce | 1652 | "#pragma system_header ignored outside include file"); |
e35f919d | 1653 | else |
38692459 | 1654 | { |
6cda323a | 1655 | check_eol (pfile, false); |
fb5ab82c | 1656 | skip_rest_of_line (pfile); |
38692459 | 1657 | cpp_make_system_header (pfile, 1, 0); |
1658 | } | |
e35f919d | 1659 | } |
e485814b | 1660 | |
1661 | /* Check the modified date of the current include file against a specified | |
1662 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1663 | determine if a fixed header should be refixed. */ | |
6cae2504 | 1664 | static void |
f7fdd7a1 | 1665 | do_pragma_dependency (cpp_reader *pfile) |
e485814b | 1666 | { |
58efbd5a | 1667 | const char *fname; |
1668 | int angle_brackets, ordering; | |
78ed8e3d | 1669 | source_location location; |
b1a9ff83 | 1670 | |
78ed8e3d | 1671 | fname = parse_include (pfile, &angle_brackets, NULL, &location); |
58efbd5a | 1672 | if (!fname) |
6cae2504 | 1673 | return; |
f80e83a9 | 1674 | |
58efbd5a | 1675 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
e485814b | 1676 | if (ordering < 0) |
d80d2074 | 1677 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
e485814b | 1678 | else if (ordering > 0) |
1679 | { | |
d80d2074 | 1680 | cpp_error (pfile, CPP_DL_WARNING, |
1681 | "current file is older than %s", fname); | |
f9b5f742 | 1682 | if (cpp_get_token (pfile)->type != CPP_EOF) |
fb5ab82c | 1683 | { |
1684 | _cpp_backup_tokens (pfile, 1); | |
3a79f5da | 1685 | do_diagnostic (pfile, CPP_DL_WARNING, 0, 0); |
fb5ab82c | 1686 | } |
e485814b | 1687 | } |
58efbd5a | 1688 | |
b9a7cc69 | 1689 | free ((void *) fname); |
e485814b | 1690 | } |
1691 | ||
a1f1781a | 1692 | /* Issue a diagnostic with the message taken from the pragma. If |
1693 | ERROR is true, the diagnostic is a warning, otherwise, it is an | |
1694 | error. */ | |
1695 | static void | |
1696 | do_pragma_warning_or_error (cpp_reader *pfile, bool error) | |
1697 | { | |
1698 | const cpp_token *tok = _cpp_lex_token (pfile); | |
1699 | cpp_string str; | |
1700 | if (tok->type != CPP_STRING | |
1701 | || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str, | |
1702 | CPP_STRING) | |
1703 | || str.len == 0) | |
1704 | { | |
1705 | cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive", | |
1706 | error ? "error" : "warning"); | |
1707 | return; | |
1708 | } | |
1709 | cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING, | |
1710 | "%s", str.text); | |
1711 | free ((void *)str.text); | |
1712 | } | |
1713 | ||
1714 | /* Issue a warning diagnostic. */ | |
1715 | static void | |
1716 | do_pragma_warning (cpp_reader *pfile) | |
1717 | { | |
1718 | do_pragma_warning_or_error (pfile, false); | |
1719 | } | |
1720 | ||
1721 | /* Issue an error diagnostic. */ | |
1722 | static void | |
1723 | do_pragma_error (cpp_reader *pfile) | |
1724 | { | |
1725 | do_pragma_warning_or_error (pfile, true); | |
1726 | } | |
1727 | ||
f9b5f742 | 1728 | /* Get a token but skip padding. */ |
1729 | static const cpp_token * | |
f7fdd7a1 | 1730 | get_token_no_padding (cpp_reader *pfile) |
396ffa86 | 1731 | { |
f9b5f742 | 1732 | for (;;) |
1733 | { | |
1734 | const cpp_token *result = cpp_get_token (pfile); | |
1735 | if (result->type != CPP_PADDING) | |
1736 | return result; | |
1737 | } | |
1738 | } | |
396ffa86 | 1739 | |
f9b5f742 | 1740 | /* Check syntax is "(string-literal)". Returns the string on success, |
1741 | or NULL on failure. */ | |
1742 | static const cpp_token * | |
f7fdd7a1 | 1743 | get__Pragma_string (cpp_reader *pfile) |
f9b5f742 | 1744 | { |
1745 | const cpp_token *string; | |
2d507e67 | 1746 | const cpp_token *paren; |
396ffa86 | 1747 | |
2d507e67 | 1748 | paren = get_token_no_padding (pfile); |
1749 | if (paren->type == CPP_EOF) | |
1750 | _cpp_backup_tokens (pfile, 1); | |
1751 | if (paren->type != CPP_OPEN_PAREN) | |
f9b5f742 | 1752 | return NULL; |
1753 | ||
1754 | string = get_token_no_padding (pfile); | |
2d507e67 | 1755 | if (string->type == CPP_EOF) |
1756 | _cpp_backup_tokens (pfile, 1); | |
924bbf02 | 1757 | if (string->type != CPP_STRING && string->type != CPP_WSTRING |
538ba11a | 1758 | && string->type != CPP_STRING32 && string->type != CPP_STRING16 |
1759 | && string->type != CPP_UTF8STRING) | |
f9b5f742 | 1760 | return NULL; |
1761 | ||
2d507e67 | 1762 | paren = get_token_no_padding (pfile); |
1763 | if (paren->type == CPP_EOF) | |
1764 | _cpp_backup_tokens (pfile, 1); | |
1765 | if (paren->type != CPP_CLOSE_PAREN) | |
f9b5f742 | 1766 | return NULL; |
396ffa86 | 1767 | |
f9b5f742 | 1768 | return string; |
396ffa86 | 1769 | } |
1770 | ||
1e0ef2fd | 1771 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1772 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1773 | static void | |
eec96426 | 1774 | destringize_and_run (cpp_reader *pfile, const cpp_string *in, |
1775 | source_location expansion_loc) | |
396ffa86 | 1776 | { |
1777 | const unsigned char *src, *limit; | |
1e0ef2fd | 1778 | char *dest, *result; |
b75b98aa | 1779 | cpp_context *saved_context; |
1780 | cpp_token *saved_cur_token; | |
1781 | tokenrun *saved_cur_run; | |
1782 | cpp_token *toks; | |
1783 | int count; | |
8302ff7f | 1784 | const struct directive *save_directive; |
396ffa86 | 1785 | |
720aca92 | 1786 | dest = result = (char *) alloca (in->len - 1); |
4970d4c2 | 1787 | src = in->text + 1 + (in->text[0] == 'L'); |
1788 | limit = in->text + in->len - 1; | |
1789 | while (src < limit) | |
396ffa86 | 1790 | { |
1791 | /* We know there is a character following the backslash. */ | |
1792 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1793 | src++; | |
1794 | *dest++ = *src++; | |
1795 | } | |
a54e0bf8 | 1796 | *dest = '\n'; |
396ffa86 | 1797 | |
6e5ef531 | 1798 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1799 | tokens when in the middle of a macro expansion. Use a new | |
1800 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1801 | doesn't go beyond the end of the text. Also, remember the | |
1802 | current lexing position so we can return to it later. | |
1803 | ||
1804 | Something like line-at-a-time lexing should remove the need for | |
1805 | this. */ | |
b75b98aa | 1806 | saved_context = pfile->context; |
1807 | saved_cur_token = pfile->cur_token; | |
1808 | saved_cur_run = pfile->cur_run; | |
878d9b47 | 1809 | |
6cfa7465 | 1810 | pfile->context = XCNEW (cpp_context); |
878d9b47 | 1811 | |
b75b98aa | 1812 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
1813 | until we've read all of the tokens that we want. */ | |
1814 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, | |
1815 | /* from_stage3 */ true); | |
1816 | /* ??? Antique Disgusting Hack. What does this do? */ | |
1817 | if (pfile->buffer->prev) | |
1818 | pfile->buffer->file = pfile->buffer->prev->file; | |
878d9b47 | 1819 | |
b75b98aa | 1820 | start_directive (pfile); |
1821 | _cpp_clean_line (pfile); | |
8302ff7f | 1822 | save_directive = pfile->directive; |
1823 | pfile->directive = &dtable[T_PRAGMA]; | |
b75b98aa | 1824 | do_pragma (pfile); |
1825 | end_directive (pfile, 1); | |
8302ff7f | 1826 | pfile->directive = save_directive; |
878d9b47 | 1827 | |
b75b98aa | 1828 | /* We always insert at least one token, the directive result. It'll |
1829 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we | |
1830 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ | |
1831 | ||
1832 | /* If we're not handling the pragma internally, read all of the tokens from | |
1833 | the string buffer now, while the string buffer is still installed. */ | |
1834 | /* ??? Note that the token buffer allocated here is leaked. It's not clear | |
1835 | to me what the true lifespan of the tokens are. It would appear that | |
1836 | the lifespan is the entire parse of the main input stream, in which case | |
1837 | this may not be wrong. */ | |
1838 | if (pfile->directive_result.type == CPP_PRAGMA) | |
1839 | { | |
1840 | int maxcount; | |
1841 | ||
1842 | count = 1; | |
1843 | maxcount = 50; | |
1844 | toks = XNEWVEC (cpp_token, maxcount); | |
1845 | toks[0] = pfile->directive_result; | |
878d9b47 | 1846 | |
b75b98aa | 1847 | do |
1848 | { | |
1849 | if (count == maxcount) | |
1850 | { | |
1851 | maxcount = maxcount * 3 / 2; | |
1852 | toks = XRESIZEVEC (cpp_token, toks, maxcount); | |
1853 | } | |
add258d7 | 1854 | toks[count] = *cpp_get_token (pfile); |
eec96426 | 1855 | /* _Pragma is a builtin, so we're not within a macro-map, and so |
1856 | the token locations are set to bogus ordinary locations | |
1857 | near to, but after that of the "_Pragma". | |
1858 | Paper over this by setting them equal to the location of the | |
1859 | _Pragma itself (PR preprocessor/69126). */ | |
1860 | toks[count].src_loc = expansion_loc; | |
add258d7 | 1861 | /* Macros have been already expanded by cpp_get_token |
1862 | if the pragma allowed expansion. */ | |
1863 | toks[count++].flags |= NO_EXPAND; | |
b75b98aa | 1864 | } |
1865 | while (toks[count-1].type != CPP_PRAGMA_EOL); | |
1866 | } | |
1867 | else | |
1868 | { | |
1869 | count = 1; | |
1870 | toks = XNEW (cpp_token); | |
1871 | toks[0] = pfile->directive_result; | |
1872 | ||
1873 | /* If we handled the entire pragma internally, make sure we get the | |
1874 | line number correct for the next token. */ | |
1875 | if (pfile->cb.line_change) | |
1876 | pfile->cb.line_change (pfile, pfile->cur_token, false); | |
1877 | } | |
1878 | ||
1879 | /* Finish inlining run_directive. */ | |
1880 | pfile->buffer->file = NULL; | |
1881 | _cpp_pop_buffer (pfile); | |
1882 | ||
1883 | /* Reset the old macro state before ... */ | |
1884 | XDELETE (pfile->context); | |
1885 | pfile->context = saved_context; | |
1886 | pfile->cur_token = saved_cur_token; | |
1887 | pfile->cur_run = saved_cur_run; | |
1888 | ||
1889 | /* ... inserting the new tokens we collected. */ | |
1890 | _cpp_push_token_context (pfile, NULL, toks, count); | |
396ffa86 | 1891 | } |
1892 | ||
2d507e67 | 1893 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
1894 | int | |
eec96426 | 1895 | _cpp_do__Pragma (cpp_reader *pfile, source_location expansion_loc) |
396ffa86 | 1896 | { |
f9b5f742 | 1897 | const cpp_token *string = get__Pragma_string (pfile); |
d6d3c909 | 1898 | pfile->directive_result.type = CPP_PADDING; |
396ffa86 | 1899 | |
878d9b47 | 1900 | if (string) |
2d507e67 | 1901 | { |
eec96426 | 1902 | destringize_and_run (pfile, &string->val.str, expansion_loc); |
2d507e67 | 1903 | return 1; |
1904 | } | |
1905 | cpp_error (pfile, CPP_DL_ERROR, | |
1906 | "_Pragma takes a parenthesized string literal"); | |
1907 | return 0; | |
396ffa86 | 1908 | } |
d6d3c909 | 1909 | |
e484a1cc | 1910 | /* Handle #ifdef. */ |
69461e0d | 1911 | static void |
f7fdd7a1 | 1912 | do_ifdef (cpp_reader *pfile) |
4bf559f2 | 1913 | { |
79bd622b | 1914 | int skip = 1; |
f80e83a9 | 1915 | |
5e878517 | 1916 | if (! pfile->state.skipping) |
79bd622b | 1917 | { |
34c3de48 | 1918 | cpp_hashnode *node = lex_macro_node (pfile, false); |
f80e83a9 | 1919 | |
79bd622b | 1920 | if (node) |
71a7c282 | 1921 | { |
00e59df9 | 1922 | /* Do not treat conditional macros as being defined. This is due to |
1923 | the powerpc and spu ports using conditional macros for 'vector', | |
1924 | 'bool', and 'pixel' to act as conditional keywords. This messes | |
1925 | up tests like #ifndef bool. */ | |
1926 | skip = (node->type != NT_MACRO | |
1927 | || ((node->flags & NODE_CONDITIONAL) != 0)); | |
71a7c282 | 1928 | _cpp_mark_macro_used (node); |
34c3de48 | 1929 | if (!(node->flags & NODE_USED)) |
1930 | { | |
1931 | node->flags |= NODE_USED; | |
1932 | if (node->type == NT_MACRO) | |
1933 | { | |
df863d33 | 1934 | if ((node->flags & NODE_BUILTIN) |
1935 | && pfile->cb.user_builtin_macro) | |
1936 | pfile->cb.user_builtin_macro (pfile, node); | |
34c3de48 | 1937 | if (pfile->cb.used_define) |
1938 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1939 | } | |
1940 | else | |
1941 | { | |
1942 | if (pfile->cb.used_undef) | |
1943 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1944 | } | |
1945 | } | |
116f0a5f | 1946 | if (pfile->cb.used) |
1947 | pfile->cb.used (pfile, pfile->directive_line, node); | |
6cda323a | 1948 | check_eol (pfile, false); |
71a7c282 | 1949 | } |
79bd622b | 1950 | } |
4bf559f2 | 1951 | |
79bd622b | 1952 | push_conditional (pfile, skip, T_IFDEF, 0); |
1953 | } | |
4bf559f2 | 1954 | |
e484a1cc | 1955 | /* Handle #ifndef. */ |
69461e0d | 1956 | static void |
f7fdd7a1 | 1957 | do_ifndef (cpp_reader *pfile) |
4bf559f2 | 1958 | { |
79bd622b | 1959 | int skip = 1; |
34c3de48 | 1960 | cpp_hashnode *node = 0; |
4bf559f2 | 1961 | |
5e878517 | 1962 | if (! pfile->state.skipping) |
b970ec42 | 1963 | { |
3e2a04b5 | 1964 | node = lex_macro_node (pfile, false); |
f99f369d | 1965 | |
1966 | if (node) | |
71a7c282 | 1967 | { |
00e59df9 | 1968 | /* Do not treat conditional macros as being defined. This is due to |
1969 | the powerpc and spu ports using conditional macros for 'vector', | |
1970 | 'bool', and 'pixel' to act as conditional keywords. This messes | |
1971 | up tests like #ifndef bool. */ | |
1972 | skip = (node->type == NT_MACRO | |
1973 | && ((node->flags & NODE_CONDITIONAL) == 0)); | |
71a7c282 | 1974 | _cpp_mark_macro_used (node); |
34c3de48 | 1975 | if (!(node->flags & NODE_USED)) |
1976 | { | |
1977 | node->flags |= NODE_USED; | |
1978 | if (node->type == NT_MACRO) | |
1979 | { | |
df863d33 | 1980 | if ((node->flags & NODE_BUILTIN) |
1981 | && pfile->cb.user_builtin_macro) | |
1982 | pfile->cb.user_builtin_macro (pfile, node); | |
34c3de48 | 1983 | if (pfile->cb.used_define) |
1984 | pfile->cb.used_define (pfile, pfile->directive_line, node); | |
1985 | } | |
1986 | else | |
1987 | { | |
1988 | if (pfile->cb.used_undef) | |
1989 | pfile->cb.used_undef (pfile, pfile->directive_line, node); | |
1990 | } | |
1991 | } | |
116f0a5f | 1992 | if (pfile->cb.used) |
1993 | pfile->cb.used (pfile, pfile->directive_line, node); | |
6cda323a | 1994 | check_eol (pfile, false); |
71a7c282 | 1995 | } |
b970ec42 | 1996 | } |
f80e83a9 | 1997 | |
79bd622b | 1998 | push_conditional (pfile, skip, T_IFNDEF, node); |
1d6fa33f | 1999 | } |
2000 | ||
fa233610 | 2001 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
2002 | pfile->mi_ind_cmacro so we can handle multiple-include | |
7ef5b942 | 2003 | optimizations. If macro expansion occurs in the expression, we |
fa233610 | 2004 | cannot treat it as a controlling conditional, since the expansion |
2005 | could change in the future. That is handled by cpp_get_token. */ | |
69461e0d | 2006 | static void |
f7fdd7a1 | 2007 | do_if (cpp_reader *pfile) |
1d6fa33f | 2008 | { |
79bd622b | 2009 | int skip = 1; |
1d6fa33f | 2010 | |
5e878517 | 2011 | if (! pfile->state.skipping) |
536a48ee | 2012 | skip = _cpp_parse_expr (pfile, true) == false; |
79bd622b | 2013 | |
fa233610 | 2014 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
1d6fa33f | 2015 | } |
2016 | ||
920b5d41 | 2017 | /* Flip skipping state if appropriate and continue without changing |
c4357c92 | 2018 | if_stack; this is so that the error message for missing #endif's |
2019 | etc. will point to the original #if. */ | |
69461e0d | 2020 | static void |
f7fdd7a1 | 2021 | do_else (cpp_reader *pfile) |
8e33b875 | 2022 | { |
920b5d41 | 2023 | cpp_buffer *buffer = pfile->buffer; |
2024 | struct if_stack *ifs = buffer->if_stack; | |
c030ed73 | 2025 | |
c4357c92 | 2026 | if (ifs == NULL) |
d80d2074 | 2027 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
79bd622b | 2028 | else |
c030ed73 | 2029 | { |
79bd622b | 2030 | if (ifs->type == T_ELSE) |
2031 | { | |
d80d2074 | 2032 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
2033 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
79bd622b | 2034 | "the conditional began here"); |
2035 | } | |
920b5d41 | 2036 | ifs->type = T_ELSE; |
2037 | ||
5e878517 | 2038 | /* Skip any future (erroneous) #elses or #elifs. */ |
2039 | pfile->state.skipping = ifs->skip_elses; | |
2040 | ifs->skip_elses = true; | |
1d6fa33f | 2041 | |
79bd622b | 2042 | /* Invalidate any controlling macro. */ |
2043 | ifs->mi_cmacro = 0; | |
79bd622b | 2044 | |
5e878517 | 2045 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 2046 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
f0fbe519 | 2047 | check_eol_endif_labels (pfile); |
5e878517 | 2048 | } |
1d6fa33f | 2049 | } |
2050 | ||
e484a1cc | 2051 | /* Handle a #elif directive by not changing if_stack either. See the |
79bd622b | 2052 | comment above do_else. */ |
69461e0d | 2053 | static void |
f7fdd7a1 | 2054 | do_elif (cpp_reader *pfile) |
1d6fa33f | 2055 | { |
920b5d41 | 2056 | cpp_buffer *buffer = pfile->buffer; |
2057 | struct if_stack *ifs = buffer->if_stack; | |
1d6fa33f | 2058 | |
c4357c92 | 2059 | if (ifs == NULL) |
d80d2074 | 2060 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
920b5d41 | 2061 | else |
0b60628c | 2062 | { |
920b5d41 | 2063 | if (ifs->type == T_ELSE) |
2064 | { | |
d80d2074 | 2065 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
2066 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
920b5d41 | 2067 | "the conditional began here"); |
2068 | } | |
2069 | ifs->type = T_ELIF; | |
79bd622b | 2070 | |
6ca4fe46 | 2071 | /* See DR#412: "Only the first group whose control condition |
2072 | evaluates to true (nonzero) is processed; any following groups | |
2073 | are skipped and their controlling directives are processed as | |
2074 | if they were in a group that is skipped." */ | |
2075 | if (ifs->skip_elses) | |
2076 | pfile->state.skipping = 1; | |
2077 | else | |
920b5d41 | 2078 | { |
6ca4fe46 | 2079 | pfile->state.skipping = ! _cpp_parse_expr (pfile, false); |
2080 | ifs->skip_elses = ! pfile->state.skipping; | |
920b5d41 | 2081 | } |
5e878517 | 2082 | |
2083 | /* Invalidate any controlling macro. */ | |
2084 | ifs->mi_cmacro = 0; | |
0b60628c | 2085 | } |
1d6fa33f | 2086 | } |
2087 | ||
5e878517 | 2088 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
69461e0d | 2089 | static void |
f7fdd7a1 | 2090 | do_endif (cpp_reader *pfile) |
1d6fa33f | 2091 | { |
920b5d41 | 2092 | cpp_buffer *buffer = pfile->buffer; |
2093 | struct if_stack *ifs = buffer->if_stack; | |
c4357c92 | 2094 | |
c4357c92 | 2095 | if (ifs == NULL) |
d80d2074 | 2096 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
1d6fa33f | 2097 | else |
2098 | { | |
5e878517 | 2099 | /* Only check EOL if was not originally skipping. */ |
923acdd5 | 2100 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
f0fbe519 | 2101 | check_eol_endif_labels (pfile); |
5e878517 | 2102 | |
79bd622b | 2103 | /* If potential control macro, we go back outside again. */ |
2104 | if (ifs->next == 0 && ifs->mi_cmacro) | |
2105 | { | |
fa233610 | 2106 | pfile->mi_valid = true; |
79bd622b | 2107 | pfile->mi_cmacro = ifs->mi_cmacro; |
2108 | } | |
2109 | ||
920b5d41 | 2110 | buffer->if_stack = ifs->next; |
5e878517 | 2111 | pfile->state.skipping = ifs->was_skipping; |
0d086e18 | 2112 | obstack_free (&pfile->buffer_ob, ifs); |
1d6fa33f | 2113 | } |
79bd622b | 2114 | } |
f80e83a9 | 2115 | |
e484a1cc | 2116 | /* Push an if_stack entry for a preprocessor conditional, and set |
2117 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
2118 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
2119 | we need to check here that we are at the top of the file. */ | |
c4357c92 | 2120 | static void |
f7fdd7a1 | 2121 | push_conditional (cpp_reader *pfile, int skip, int type, |
2122 | const cpp_hashnode *cmacro) | |
c4357c92 | 2123 | { |
2124 | struct if_stack *ifs; | |
920b5d41 | 2125 | cpp_buffer *buffer = pfile->buffer; |
c4357c92 | 2126 | |
3b298764 | 2127 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
729d2022 | 2128 | ifs->line = pfile->directive_line; |
920b5d41 | 2129 | ifs->next = buffer->if_stack; |
5e878517 | 2130 | ifs->skip_elses = pfile->state.skipping || !skip; |
2131 | ifs->was_skipping = pfile->state.skipping; | |
c4357c92 | 2132 | ifs->type = type; |
fa233610 | 2133 | /* This condition is effectively a test for top-of-file. */ |
2134 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
79bd622b | 2135 | ifs->mi_cmacro = cmacro; |
2136 | else | |
2137 | ifs->mi_cmacro = 0; | |
c4357c92 | 2138 | |
5e878517 | 2139 | pfile->state.skipping = skip; |
920b5d41 | 2140 | buffer->if_stack = ifs; |
4896abfd | 2141 | } |
69bcbfc6 | 2142 | |
e484a1cc | 2143 | /* Read the tokens of the answer into the macro pool, in a directive |
2144 | of type TYPE. Only commit the memory if we intend it as permanent | |
2145 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
78ed8e3d | 2146 | ANSWERP to point to the answer. PRED_LOC is the location of the |
2147 | predicate. */ | |
79bd622b | 2148 | static int |
78ed8e3d | 2149 | parse_answer (cpp_reader *pfile, struct answer **answerp, int type, |
2150 | source_location pred_loc) | |
1d6fa33f | 2151 | { |
f9b5f742 | 2152 | const cpp_token *paren; |
79bd622b | 2153 | struct answer *answer; |
e6a5f963 | 2154 | unsigned int acount; |
79bd622b | 2155 | |
2156 | /* In a conditional, it is legal to not have an open paren. We | |
2157 | should save the following token in this case. */ | |
f9b5f742 | 2158 | paren = cpp_get_token (pfile); |
79bd622b | 2159 | |
2160 | /* If not a paren, see if we're OK. */ | |
f9b5f742 | 2161 | if (paren->type != CPP_OPEN_PAREN) |
f80e83a9 | 2162 | { |
79bd622b | 2163 | /* In a conditional no answer is a test for any answer. It |
2164 | could be followed by any token. */ | |
2165 | if (type == T_IF) | |
fb5ab82c | 2166 | { |
2167 | _cpp_backup_tokens (pfile, 1); | |
2168 | return 0; | |
2169 | } | |
79bd622b | 2170 | |
2171 | /* #unassert with no answer is valid - it removes all answers. */ | |
f9b5f742 | 2172 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
79bd622b | 2173 | return 0; |
bce8e0c0 | 2174 | |
78ed8e3d | 2175 | cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0, |
2176 | "missing '(' after predicate"); | |
79bd622b | 2177 | return 1; |
f80e83a9 | 2178 | } |
69bcbfc6 | 2179 | |
e6a5f963 | 2180 | for (acount = 0;; acount++) |
f80e83a9 | 2181 | { |
e6a5f963 | 2182 | size_t room_needed; |
2183 | const cpp_token *token = cpp_get_token (pfile); | |
2184 | cpp_token *dest; | |
79bd622b | 2185 | |
f80e83a9 | 2186 | if (token->type == CPP_CLOSE_PAREN) |
2187 | break; | |
3ba8b497 | 2188 | |
79bd622b | 2189 | if (token->type == CPP_EOF) |
f80e83a9 | 2190 | { |
d80d2074 | 2191 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
79bd622b | 2192 | return 1; |
f80e83a9 | 2193 | } |
e6a5f963 | 2194 | |
2195 | /* struct answer includes the space for one token. */ | |
2196 | room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token)); | |
2197 | ||
2198 | if (BUFF_ROOM (pfile->a_buff) < room_needed) | |
2199 | _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer)); | |
2200 | ||
2201 | dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount]; | |
2202 | *dest = *token; | |
2203 | ||
2204 | /* Drop whitespace at start, for answer equivalence purposes. */ | |
2205 | if (acount == 0) | |
2206 | dest->flags &= ~PREV_WHITE; | |
f80e83a9 | 2207 | } |
bce8e0c0 | 2208 | |
e6a5f963 | 2209 | if (acount == 0) |
69bcbfc6 | 2210 | { |
d80d2074 | 2211 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
79bd622b | 2212 | return 1; |
1d6fa33f | 2213 | } |
f80e83a9 | 2214 | |
e6a5f963 | 2215 | answer = (struct answer *) BUFF_FRONT (pfile->a_buff); |
2216 | answer->count = acount; | |
2217 | answer->next = NULL; | |
79bd622b | 2218 | *answerp = answer; |
f80e83a9 | 2219 | |
79bd622b | 2220 | return 0; |
2221 | } | |
f80e83a9 | 2222 | |
e484a1cc | 2223 | /* Parses an assertion directive of type TYPE, returning a pointer to |
2224 | the hash node of the predicate, or 0 on error. If an answer was | |
2225 | supplied, it is placed in ANSWERP, otherwise it is set to 0. */ | |
79bd622b | 2226 | static cpp_hashnode * |
f7fdd7a1 | 2227 | parse_assertion (cpp_reader *pfile, struct answer **answerp, int type) |
79bd622b | 2228 | { |
2229 | cpp_hashnode *result = 0; | |
f9b5f742 | 2230 | const cpp_token *predicate; |
79bd622b | 2231 | |
2232 | /* We don't expand predicates or answers. */ | |
2233 | pfile->state.prevent_expansion++; | |
2234 | ||
79bd622b | 2235 | *answerp = 0; |
f9b5f742 | 2236 | predicate = cpp_get_token (pfile); |
2237 | if (predicate->type == CPP_EOF) | |
d80d2074 | 2238 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
f9b5f742 | 2239 | else if (predicate->type != CPP_NAME) |
78ed8e3d | 2240 | cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0, |
2241 | "predicate must be an identifier"); | |
2242 | else if (parse_answer (pfile, answerp, type, predicate->src_loc) == 0) | |
79bd622b | 2243 | { |
2ee04baa | 2244 | unsigned int len = NODE_LEN (predicate->val.node.node); |
720aca92 | 2245 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
f80e83a9 | 2246 | |
79bd622b | 2247 | /* Prefix '#' to get it out of macro namespace. */ |
2248 | sym[0] = '#'; | |
2ee04baa | 2249 | memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len); |
79bd622b | 2250 | result = cpp_lookup (pfile, sym, len + 1); |
2251 | } | |
69bcbfc6 | 2252 | |
79bd622b | 2253 | pfile->state.prevent_expansion--; |
2254 | return result; | |
1d6fa33f | 2255 | } |
69bcbfc6 | 2256 | |
e484a1cc | 2257 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
f80e83a9 | 2258 | or a pointer to NULL if the answer is not in the chain. */ |
79bd622b | 2259 | static struct answer ** |
f7fdd7a1 | 2260 | find_answer (cpp_hashnode *node, const struct answer *candidate) |
1d6fa33f | 2261 | { |
79bd622b | 2262 | unsigned int i; |
f80e83a9 | 2263 | struct answer **result; |
1d6fa33f | 2264 | |
f80e83a9 | 2265 | for (result = &node->value.answers; *result; result = &(*result)->next) |
79bd622b | 2266 | { |
2267 | struct answer *answer = *result; | |
2268 | ||
2269 | if (answer->count == candidate->count) | |
2270 | { | |
2271 | for (i = 0; i < answer->count; i++) | |
2272 | if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i])) | |
2273 | break; | |
2274 | ||
2275 | if (i == answer->count) | |
2276 | break; | |
2277 | } | |
2278 | } | |
c030ed73 | 2279 | |
f80e83a9 | 2280 | return result; |
2281 | } | |
bce8e0c0 | 2282 | |
79bd622b | 2283 | /* Test an assertion within a preprocessor conditional. Returns |
d10cfa8d | 2284 | nonzero on failure, zero on success. On success, the result of |
9fd28e01 | 2285 | the test is written into VALUE, otherwise the value 0. */ |
79bd622b | 2286 | int |
f7fdd7a1 | 2287 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
79bd622b | 2288 | { |
2289 | struct answer *answer; | |
2290 | cpp_hashnode *node; | |
2291 | ||
2292 | node = parse_assertion (pfile, &answer, T_IF); | |
9fd28e01 | 2293 | |
2294 | /* For recovery, an erroneous assertion expression is handled as a | |
2295 | failing assertion. */ | |
2296 | *value = 0; | |
2297 | ||
79bd622b | 2298 | if (node) |
2299 | *value = (node->type == NT_ASSERTION && | |
2300 | (answer == 0 || *find_answer (node, answer) != 0)); | |
b444d47a | 2301 | else if (pfile->cur_token[-1].type == CPP_EOF) |
2302 | _cpp_backup_tokens (pfile, 1); | |
79bd622b | 2303 | |
2304 | /* We don't commit the memory for the answer - it's temporary only. */ | |
2305 | return node == 0; | |
2306 | } | |
2307 | ||
e484a1cc | 2308 | /* Handle #assert. */ |
69461e0d | 2309 | static void |
f7fdd7a1 | 2310 | do_assert (cpp_reader *pfile) |
f80e83a9 | 2311 | { |
2312 | struct answer *new_answer; | |
2313 | cpp_hashnode *node; | |
b1a9ff83 | 2314 | |
79bd622b | 2315 | node = parse_assertion (pfile, &new_answer, T_ASSERT); |
f80e83a9 | 2316 | if (node) |
c030ed73 | 2317 | { |
c39ed964 | 2318 | size_t answer_size; |
2319 | ||
79bd622b | 2320 | /* Place the new answer in the answer list. First check there |
2321 | is not a duplicate. */ | |
f80e83a9 | 2322 | new_answer->next = 0; |
79bd622b | 2323 | if (node->type == NT_ASSERTION) |
f80e83a9 | 2324 | { |
79bd622b | 2325 | if (*find_answer (node, new_answer)) |
2326 | { | |
d80d2074 | 2327 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
73328dce | 2328 | NODE_NAME (node) + 1); |
79bd622b | 2329 | return; |
2330 | } | |
f80e83a9 | 2331 | new_answer->next = node->value.answers; |
2332 | } | |
e6a5f963 | 2333 | |
c39ed964 | 2334 | answer_size = sizeof (struct answer) + ((new_answer->count - 1) |
2335 | * sizeof (cpp_token)); | |
2336 | /* Commit or allocate storage for the object. */ | |
2337 | if (pfile->hash_table->alloc_subobject) | |
2338 | { | |
2339 | struct answer *temp_answer = new_answer; | |
720aca92 | 2340 | new_answer = (struct answer *) pfile->hash_table->alloc_subobject |
2341 | (answer_size); | |
c39ed964 | 2342 | memcpy (new_answer, temp_answer, answer_size); |
2343 | } | |
2344 | else | |
2345 | BUFF_FRONT (pfile->a_buff) += answer_size; | |
2346 | ||
79bd622b | 2347 | node->type = NT_ASSERTION; |
f80e83a9 | 2348 | node->value.answers = new_answer; |
6cda323a | 2349 | check_eol (pfile, false); |
c030ed73 | 2350 | } |
f80e83a9 | 2351 | } |
bce8e0c0 | 2352 | |
e484a1cc | 2353 | /* Handle #unassert. */ |
69461e0d | 2354 | static void |
f7fdd7a1 | 2355 | do_unassert (cpp_reader *pfile) |
f80e83a9 | 2356 | { |
2357 | cpp_hashnode *node; | |
79bd622b | 2358 | struct answer *answer; |
b1a9ff83 | 2359 | |
79bd622b | 2360 | node = parse_assertion (pfile, &answer, T_UNASSERT); |
2361 | /* It isn't an error to #unassert something that isn't asserted. */ | |
2362 | if (node && node->type == NT_ASSERTION) | |
69bcbfc6 | 2363 | { |
79bd622b | 2364 | if (answer) |
bce8e0c0 | 2365 | { |
79bd622b | 2366 | struct answer **p = find_answer (node, answer), *temp; |
f80e83a9 | 2367 | |
79bd622b | 2368 | /* Remove the answer from the list. */ |
2369 | temp = *p; | |
2370 | if (temp) | |
2371 | *p = temp->next; | |
3ba8b497 | 2372 | |
79bd622b | 2373 | /* Did we free the last answer? */ |
2374 | if (node->value.answers == 0) | |
2375 | node->type = NT_VOID; | |
e6a5f963 | 2376 | |
6cda323a | 2377 | check_eol (pfile, false); |
79bd622b | 2378 | } |
2379 | else | |
2380 | _cpp_free_definition (node); | |
f80e83a9 | 2381 | } |
79bd622b | 2382 | |
2383 | /* We don't commit the memory for the answer - it's temporary only. */ | |
1d6fa33f | 2384 | } |
1d6fa33f | 2385 | |
0578f103 | 2386 | /* These are for -D, -U, -A. */ |
2387 | ||
2388 | /* Process the string STR as if it appeared as the body of a #define. | |
2389 | If STR is just an identifier, define it with value 1. | |
2390 | If STR has anything after the identifier, then it should | |
2c0e001b | 2391 | be identifier=definition. */ |
9fa36617 | 2392 | void |
f7fdd7a1 | 2393 | cpp_define (cpp_reader *pfile, const char *str) |
9fa36617 | 2394 | { |
3f24af9b | 2395 | char *buf; |
2396 | const char *p; | |
0578f103 | 2397 | size_t count; |
2398 | ||
b1a9ff83 | 2399 | /* Copy the entire option so we can modify it. |
0578f103 | 2400 | Change the first "=" in the string to a space. If there is none, |
90dc47e0 | 2401 | tack " 1" on the end. */ |
2402 | ||
90dc47e0 | 2403 | count = strlen (str); |
720aca92 | 2404 | buf = (char *) alloca (count + 3); |
90dc47e0 | 2405 | memcpy (buf, str, count); |
2406 | ||
2407 | p = strchr (str, '='); | |
0578f103 | 2408 | if (p) |
90dc47e0 | 2409 | buf[p - str] = ' '; |
0578f103 | 2410 | else |
2411 | { | |
90dc47e0 | 2412 | buf[count++] = ' '; |
2413 | buf[count++] = '1'; | |
9fa36617 | 2414 | } |
a54e0bf8 | 2415 | buf[count] = '\n'; |
eb3948d1 | 2416 | |
c808d026 | 2417 | run_directive (pfile, T_DEFINE, buf, count); |
a336277c | 2418 | } |
2419 | ||
85e51d59 | 2420 | |
2421 | /* Use to build macros to be run through cpp_define() as | |
2422 | described above. | |
2423 | Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ | |
2424 | ||
2425 | void | |
2426 | cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) | |
2427 | { | |
5d95297a | 2428 | char *ptr; |
85e51d59 | 2429 | |
2430 | va_list ap; | |
2431 | va_start (ap, fmt); | |
5d95297a | 2432 | ptr = xvasprintf (fmt, ap); |
85e51d59 | 2433 | va_end (ap); |
2434 | ||
2435 | cpp_define (pfile, ptr); | |
2436 | free (ptr); | |
2437 | } | |
2438 | ||
2439 | ||
f0c20935 | 2440 | /* Slight variant of the above for use by initialize_builtins. */ |
a336277c | 2441 | void |
f7fdd7a1 | 2442 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
a336277c | 2443 | { |
a54e0bf8 | 2444 | size_t len = strlen (str); |
720aca92 | 2445 | char *buf = (char *) alloca (len + 1); |
a54e0bf8 | 2446 | memcpy (buf, str, len); |
2447 | buf[len] = '\n'; | |
2448 | run_directive (pfile, T_DEFINE, buf, len); | |
0578f103 | 2449 | } |
a92771b8 | 2450 | |
0578f103 | 2451 | /* Process MACRO as if it appeared as the body of an #undef. */ |
2452 | void | |
f7fdd7a1 | 2453 | cpp_undef (cpp_reader *pfile, const char *macro) |
1d6fa33f | 2454 | { |
a54e0bf8 | 2455 | size_t len = strlen (macro); |
720aca92 | 2456 | char *buf = (char *) alloca (len + 1); |
a54e0bf8 | 2457 | memcpy (buf, macro, len); |
2458 | buf[len] = '\n'; | |
2459 | run_directive (pfile, T_UNDEF, buf, len); | |
1d6fa33f | 2460 | } |
2461 | ||
0d601ff4 | 2462 | /* Replace a previous definition DEF of the macro STR. If DEF is NULL, |
2463 | or first element is zero, then the macro should be undefined. */ | |
2464 | static void | |
2465 | cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c) | |
3fa3949d | 2466 | { |
0d601ff4 | 2467 | cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name); |
3fa3949d | 2468 | if (node == NULL) |
2469 | return; | |
2470 | ||
34c3de48 | 2471 | if (pfile->cb.before_define) |
2472 | pfile->cb.before_define (pfile); | |
2473 | ||
3fa3949d | 2474 | if (node->type == NT_MACRO) |
2475 | { | |
2476 | if (pfile->cb.undef) | |
2477 | pfile->cb.undef (pfile, pfile->directive_line, node); | |
2478 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
2479 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
2480 | } | |
2481 | if (node->type != NT_VOID) | |
2482 | _cpp_free_definition (node); | |
2483 | ||
0d601ff4 | 2484 | if (c->is_undef) |
2485 | return; | |
2486 | { | |
2487 | size_t namelen; | |
2488 | const uchar *dn; | |
2489 | cpp_hashnode *h = NULL; | |
2490 | cpp_buffer *nbuf; | |
2491 | ||
2492 | namelen = ustrcspn (c->definition, "( \n"); | |
2493 | h = cpp_lookup (pfile, c->definition, namelen); | |
2494 | dn = c->definition + namelen; | |
2495 | ||
2496 | h->type = NT_VOID; | |
2497 | h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED); | |
2498 | nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true); | |
2499 | if (nbuf != NULL) | |
2500 | { | |
2501 | _cpp_clean_line (pfile); | |
2502 | nbuf->sysp = 1; | |
2503 | if (!_cpp_create_definition (pfile, h)) | |
2504 | abort (); | |
2505 | _cpp_pop_buffer (pfile); | |
2506 | } | |
2507 | else | |
2508 | abort (); | |
2509 | h->value.macro->line = c->line; | |
2510 | h->value.macro->syshdr = c->syshdr; | |
2511 | h->value.macro->used = c->used; | |
2512 | } | |
3fa3949d | 2513 | } |
2514 | ||
2c0e001b | 2515 | /* Process the string STR as if it appeared as the body of a #assert. */ |
0578f103 | 2516 | void |
f7fdd7a1 | 2517 | cpp_assert (cpp_reader *pfile, const char *str) |
0578f103 | 2518 | { |
90dc47e0 | 2519 | handle_assertion (pfile, str, T_ASSERT); |
0578f103 | 2520 | } |
1d6fa33f | 2521 | |
2c0e001b | 2522 | /* Process STR as if it appeared as the body of an #unassert. */ |
0578f103 | 2523 | void |
f7fdd7a1 | 2524 | cpp_unassert (cpp_reader *pfile, const char *str) |
1d6fa33f | 2525 | { |
90dc47e0 | 2526 | handle_assertion (pfile, str, T_UNASSERT); |
b1a9ff83 | 2527 | } |
e3630c4b | 2528 | |
90dc47e0 | 2529 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
2530 | static void | |
f7fdd7a1 | 2531 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
90dc47e0 | 2532 | { |
2533 | size_t count = strlen (str); | |
2534 | const char *p = strchr (str, '='); | |
2535 | ||
a54e0bf8 | 2536 | /* Copy the entire option so we can modify it. Change the first |
2537 | "=" in the string to a '(', and tack a ')' on the end. */ | |
720aca92 | 2538 | char *buf = (char *) alloca (count + 2); |
a54e0bf8 | 2539 | |
2540 | memcpy (buf, str, count); | |
90dc47e0 | 2541 | if (p) |
2542 | { | |
90dc47e0 | 2543 | buf[p - str] = '('; |
2544 | buf[count++] = ')'; | |
90dc47e0 | 2545 | } |
a54e0bf8 | 2546 | buf[count] = '\n'; |
2547 | str = buf; | |
90dc47e0 | 2548 | |
c808d026 | 2549 | run_directive (pfile, type, str, count); |
90dc47e0 | 2550 | } |
2551 | ||
72b04ca4 | 2552 | /* The options structure. */ |
2553 | cpp_options * | |
f7fdd7a1 | 2554 | cpp_get_options (cpp_reader *pfile) |
72b04ca4 | 2555 | { |
2556 | return &pfile->opts; | |
2557 | } | |
2558 | ||
2559 | /* The callbacks structure. */ | |
2560 | cpp_callbacks * | |
f7fdd7a1 | 2561 | cpp_get_callbacks (cpp_reader *pfile) |
72b04ca4 | 2562 | { |
2563 | return &pfile->cb; | |
2564 | } | |
2565 | ||
2566 | /* Copy the given callbacks structure to our own. */ | |
2567 | void | |
f7fdd7a1 | 2568 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
72b04ca4 | 2569 | { |
2570 | pfile->cb = *cb; | |
2571 | } | |
2572 | ||
3eb3f293 | 2573 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
2574 | struct deps * | |
2575 | cpp_get_deps (cpp_reader *pfile) | |
2576 | { | |
2577 | if (!pfile->deps) | |
2578 | pfile->deps = deps_init (); | |
2579 | return pfile->deps; | |
2580 | } | |
2581 | ||
4087613d | 2582 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
2583 | doesn't fail. It does not generate a file change call back; that | |
2584 | is the responsibility of the caller. */ | |
f51c2148 | 2585 | cpp_buffer * |
f7fdd7a1 | 2586 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
9eb74666 | 2587 | int from_stage3) |
f51c2148 | 2588 | { |
720aca92 | 2589 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
79bd622b | 2590 | |
bd507c05 | 2591 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
720aca92 | 2592 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
bd507c05 | 2593 | |
720aca92 | 2594 | new_buffer->next_line = new_buffer->buf = buffer; |
2595 | new_buffer->rlimit = buffer + len; | |
2596 | new_buffer->from_stage3 = from_stage3; | |
2597 | new_buffer->prev = pfile->buffer; | |
2598 | new_buffer->need_line = true; | |
6cee4464 | 2599 | |
720aca92 | 2600 | pfile->buffer = new_buffer; |
787c3d1a | 2601 | |
720aca92 | 2602 | return new_buffer; |
f51c2148 | 2603 | } |
2604 | ||
02516fb9 | 2605 | /* Pops a single buffer, with a file change call-back if appropriate. |
2606 | Then pushes the next -include file, if any remain. */ | |
4dfe8b74 | 2607 | void |
f7fdd7a1 | 2608 | _cpp_pop_buffer (cpp_reader *pfile) |
f51c2148 | 2609 | { |
bd507c05 | 2610 | cpp_buffer *buffer = pfile->buffer; |
1824e2bd | 2611 | struct _cpp_file *inc = buffer->file; |
f0c20935 | 2612 | struct if_stack *ifs; |
914db4b7 | 2613 | const unsigned char *to_free; |
f51c2148 | 2614 | |
bd507c05 | 2615 | /* Walk back up the conditional stack till we reach its level at |
2616 | entry to this file, issuing error messages. */ | |
2617 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
d80d2074 | 2618 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
bd507c05 | 2619 | "unterminated #%s", dtable[ifs->type].name); |
4087613d | 2620 | |
5621a364 | 2621 | /* In case of a missing #endif. */ |
1ea7ed21 | 2622 | pfile->state.skipping = 0; |
c808d026 | 2623 | |
02516fb9 | 2624 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
c808d026 | 2625 | pfile->buffer = buffer->prev; |
2626 | ||
914db4b7 | 2627 | to_free = buffer->to_free; |
a54e0bf8 | 2628 | free (buffer->notes); |
2629 | ||
02516fb9 | 2630 | /* Free the buffer object now; we may want to push a new buffer |
2631 | in _cpp_push_next_include_file. */ | |
2632 | obstack_free (&pfile->buffer_ob, buffer); | |
c808d026 | 2633 | |
02516fb9 | 2634 | if (inc) |
2635 | { | |
914db4b7 | 2636 | _cpp_pop_file_buffer (pfile, inc, to_free); |
02516fb9 | 2637 | |
9eb74666 | 2638 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
02516fb9 | 2639 | } |
f51c2148 | 2640 | } |
2641 | ||
a2f10574 | 2642 | /* Enter all recognized directives in the hash table. */ |
f51c2148 | 2643 | void |
f7fdd7a1 | 2644 | _cpp_init_directives (cpp_reader *pfile) |
f51c2148 | 2645 | { |
87f5a8f0 | 2646 | unsigned int i; |
79bd622b | 2647 | cpp_hashnode *node; |
76faa4c0 | 2648 | |
ee6c4e4b | 2649 | for (i = 0; i < (unsigned int) N_DIRECTIVES; i++) |
79bd622b | 2650 | { |
87f5a8f0 | 2651 | node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
805e22b2 | 2652 | node->is_directive = 1; |
2653 | node->directive_index = i; | |
79bd622b | 2654 | } |
f51c2148 | 2655 | } |
f6751ff2 | 2656 | |
2657 | /* Extract header file from a bracket include. Parsing starts after '<'. | |
2658 | The string is malloced and must be freed by the caller. */ | |
2659 | char * | |
2660 | _cpp_bracket_include(cpp_reader *pfile) | |
2661 | { | |
2662 | return glue_header_name (pfile); | |
2663 | } | |
2664 |