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