]>
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 | |
45f2492c NB |
670 | if (CPP_OPTION (pfile, warn_unused_macros)) |
671 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
a69cbaac | 672 | |
45f2492c NB |
673 | _cpp_free_definition (node); |
674 | } | |
9e62c811 | 675 | } |
45f2492c | 676 | |
a5cb563b | 677 | check_eol (pfile, false); |
7f2935c7 PB |
678 | } |
679 | ||
d1bd0ded GK |
680 | /* Undefine a single macro/assertion/whatever. */ |
681 | ||
682 | static int | |
c6e83800 | 683 | undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h, |
d1bd0ded GK |
684 | void *data_p ATTRIBUTE_UNUSED) |
685 | { | |
c6e83800 ZW |
686 | /* Body of _cpp_free_definition inlined here for speed. |
687 | Macros and assertions no longer have anything to free. */ | |
688 | h->type = NT_VOID; | |
a570d97f NS |
689 | h->value.answers = NULL; |
690 | h->flags &= ~(NODE_POISONED|NODE_DISABLED|NODE_USED); | |
d1bd0ded GK |
691 | return 1; |
692 | } | |
693 | ||
694 | /* Undefine all macros and assertions. */ | |
695 | ||
696 | void | |
697 | cpp_undef_all (cpp_reader *pfile) | |
698 | { | |
699 | cpp_forall_identifiers (pfile, undefine_macros, NULL); | |
700 | } | |
701 | ||
702 | ||
93c80368 NB |
703 | /* Helper routine used by parse_include. Reinterpret the current line |
704 | as an h-char-sequence (< ... >); we are looking at the first token | |
74eb4b3e NB |
705 | after the <. Returns a malloced filename. */ |
706 | static char * | |
6cf87ca4 | 707 | glue_header_name (cpp_reader *pfile) |
93c80368 | 708 | { |
4ed5bcfb | 709 | const cpp_token *token; |
74eb4b3e | 710 | char *buffer; |
2450e0b8 | 711 | size_t len, total_len = 0, capacity = 1024; |
93c80368 NB |
712 | |
713 | /* To avoid lexed tokens overwriting our glued name, we can only | |
714 | allocate from the string pool once we've lexed everything. */ | |
c3f829c1 | 715 | buffer = XNEWVEC (char, capacity); |
93c80368 NB |
716 | for (;;) |
717 | { | |
a8d0ddaf | 718 | token = get_token_no_padding (pfile); |
93c80368 | 719 | |
74eb4b3e | 720 | if (token->type == CPP_GREATER) |
93c80368 | 721 | break; |
74eb4b3e NB |
722 | if (token->type == CPP_EOF) |
723 | { | |
0527bc4e | 724 | cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character"); |
74eb4b3e NB |
725 | break; |
726 | } | |
93c80368 | 727 | |
59325650 | 728 | len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */ |
2450e0b8 | 729 | if (total_len + len > capacity) |
93c80368 | 730 | { |
2450e0b8 | 731 | capacity = (capacity + len) * 2; |
c3f829c1 | 732 | buffer = XRESIZEVEC (char, buffer, capacity); |
93c80368 NB |
733 | } |
734 | ||
4ed5bcfb | 735 | if (token->flags & PREV_WHITE) |
2450e0b8 | 736 | buffer[total_len++] = ' '; |
93c80368 | 737 | |
47e20491 GK |
738 | total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len], |
739 | true) | |
74eb4b3e | 740 | - (uchar *) buffer); |
93c80368 | 741 | } |
041c3194 | 742 | |
74eb4b3e NB |
743 | buffer[total_len] = '\0'; |
744 | return buffer; | |
93c80368 | 745 | } |
7f2935c7 | 746 | |
74eb4b3e NB |
747 | /* Returns the file name of #include, #include_next, #import and |
748 | #pragma dependency. The string is malloced and the caller should | |
a28fbdba MLI |
749 | free it. Returns NULL on error. LOCATION is the source location |
750 | of the file name. */ | |
751 | ||
74eb4b3e | 752 | static const char * |
cbc43ae0 | 753 | parse_include (cpp_reader *pfile, int *pangle_brackets, |
620e594b | 754 | const cpp_token ***buf, location_t *location) |
7f2935c7 | 755 | { |
74eb4b3e | 756 | char *fname; |
4ed5bcfb | 757 | const cpp_token *header; |
7f2935c7 | 758 | |
93c80368 | 759 | /* Allow macro expansion. */ |
a8d0ddaf | 760 | header = get_token_no_padding (pfile); |
a28fbdba | 761 | *location = header->src_loc; |
2c6e3f55 JJ |
762 | if ((header->type == CPP_STRING && header->val.str.text[0] != 'R') |
763 | || header->type == CPP_HEADER_NAME) | |
7f2935c7 | 764 | { |
c3f829c1 | 765 | fname = XNEWVEC (char, header->val.str.len - 1); |
6338b358 NB |
766 | memcpy (fname, header->val.str.text + 1, header->val.str.len - 2); |
767 | fname[header->val.str.len - 2] = '\0'; | |
74eb4b3e | 768 | *pangle_brackets = header->type == CPP_HEADER_NAME; |
7f2935c7 | 769 | } |
74eb4b3e | 770 | else if (header->type == CPP_LESS) |
7f2935c7 | 771 | { |
74eb4b3e NB |
772 | fname = glue_header_name (pfile); |
773 | *pangle_brackets = 1; | |
774 | } | |
775 | else | |
776 | { | |
777 | const unsigned char *dir; | |
778 | ||
779 | if (pfile->directive == &dtable[T_PRAGMA]) | |
b6baa67d | 780 | dir = UC"pragma dependency"; |
74eb4b3e NB |
781 | else |
782 | dir = pfile->directive->name; | |
0527bc4e | 783 | cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>", |
74eb4b3e NB |
784 | dir); |
785 | ||
4ed5bcfb | 786 | return NULL; |
7f2935c7 | 787 | } |
7f2935c7 | 788 | |
cda5e672 TT |
789 | if (pfile->directive == &dtable[T_PRAGMA]) |
790 | { | |
791 | /* This pragma allows extra tokens after the file name. */ | |
792 | } | |
793 | else if (buf == NULL || CPP_OPTION (pfile, discard_comments)) | |
61cc8223 | 794 | check_eol (pfile, true); |
cbc43ae0 ILT |
795 | else |
796 | { | |
797 | /* If we are not discarding comments, then gather them while | |
798 | doing the eol check. */ | |
799 | *buf = check_eol_return_comments (pfile); | |
800 | } | |
801 | ||
74eb4b3e | 802 | return fname; |
168d3732 | 803 | } |
3caee4a8 | 804 | |
ba133c96 | 805 | /* Handle #include, #include_next and #import. */ |
711b8824 | 806 | static void |
6cf87ca4 | 807 | do_include_common (cpp_reader *pfile, enum include_type type) |
168d3732 | 808 | { |
74eb4b3e NB |
809 | const char *fname; |
810 | int angle_brackets; | |
cbc43ae0 | 811 | const cpp_token **buf = NULL; |
620e594b | 812 | location_t location; |
cbc43ae0 ILT |
813 | |
814 | /* Re-enable saving of comments if requested, so that the include | |
815 | callback can dump comments which follow #include. */ | |
816 | pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments); | |
74eb4b3e | 817 | |
056f95ec NS |
818 | /* Tell the lexer this is an include directive -- we want it to |
819 | increment the line number even if this is the last line of a file. */ | |
820 | pfile->state.in_directive = 2; | |
821 | ||
a28fbdba | 822 | fname = parse_include (pfile, &angle_brackets, &buf, &location); |
74eb4b3e | 823 | if (!fname) |
705b0c05 | 824 | goto done; |
7f2935c7 | 825 | |
28303828 | 826 | if (!*fname) |
705b0c05 NS |
827 | { |
828 | cpp_error_with_line (pfile, CPP_DL_ERROR, location, 0, | |
829 | "empty filename in #%s", | |
830 | pfile->directive->name); | |
831 | goto done; | |
832 | } | |
28303828 | 833 | |
3963c2e0 | 834 | /* Prevent #include recursion. */ |
1c6ffbab QZ |
835 | if (pfile->line_table->depth >= CPP_OPTION (pfile, max_include_depth)) |
836 | cpp_error (pfile, | |
837 | CPP_DL_ERROR, | |
838 | "#include nested depth %u exceeds maximum of %u" | |
839 | " (use -fmax-include-depth=DEPTH to increase the maximum)", | |
840 | pfile->line_table->depth, | |
841 | CPP_OPTION (pfile, max_include_depth)); | |
74eb4b3e | 842 | else |
09b82253 | 843 | { |
74eb4b3e NB |
844 | /* Get out of macro context, if we are. */ |
845 | skip_rest_of_line (pfile); | |
09b82253 | 846 | |
74eb4b3e | 847 | if (pfile->cb.include) |
6cf87ca4 | 848 | pfile->cb.include (pfile, pfile->directive_line, |
cbc43ae0 ILT |
849 | pfile->directive->name, fname, angle_brackets, |
850 | buf); | |
3963c2e0 | 851 | |
ac81cf0b | 852 | _cpp_stack_include (pfile, fname, angle_brackets, type, location); |
74eb4b3e | 853 | } |
3963c2e0 | 854 | |
705b0c05 | 855 | done: |
cbc43ae0 ILT |
856 | XDELETEVEC (fname); |
857 | if (buf) | |
858 | XDELETEVEC (buf); | |
168d3732 | 859 | } |
e8037d57 | 860 | |
711b8824 | 861 | static void |
6cf87ca4 | 862 | do_include (cpp_reader *pfile) |
168d3732 | 863 | { |
ba133c96 NB |
864 | do_include_common (pfile, IT_INCLUDE); |
865 | } | |
168d3732 | 866 | |
ba133c96 | 867 | static void |
6cf87ca4 | 868 | do_import (cpp_reader *pfile) |
ba133c96 | 869 | { |
ba133c96 | 870 | do_include_common (pfile, IT_IMPORT); |
168d3732 | 871 | } |
7f2935c7 | 872 | |
711b8824 | 873 | static void |
6cf87ca4 | 874 | do_include_next (cpp_reader *pfile) |
168d3732 | 875 | { |
3963c2e0 ZW |
876 | enum include_type type = IT_INCLUDE_NEXT; |
877 | ||
878 | /* If this is the primary source file, warn and use the normal | |
879 | search logic. */ | |
705e2d28 | 880 | if (cpp_in_primary_file (pfile)) |
3963c2e0 | 881 | { |
0527bc4e | 882 | cpp_error (pfile, CPP_DL_WARNING, |
3963c2e0 ZW |
883 | "#include_next in primary source file"); |
884 | type = IT_INCLUDE; | |
885 | } | |
886 | do_include_common (pfile, type); | |
7f2935c7 | 887 | } |
7f2935c7 | 888 | |
dcc229e5 ZW |
889 | /* Subroutine of do_linemarker. Read possible flags after file name. |
890 | LAST is the last flag seen; 0 if this is the first flag. Return the | |
891 | flag if it is valid, 0 at the end of the directive. Otherwise | |
892 | complain. */ | |
642ce434 | 893 | static unsigned int |
6cf87ca4 | 894 | read_flag (cpp_reader *pfile, unsigned int last) |
d3a34a0a | 895 | { |
345894b4 | 896 | const cpp_token *token = _cpp_lex_token (pfile); |
d3a34a0a | 897 | |
345894b4 | 898 | if (token->type == CPP_NUMBER && token->val.str.len == 1) |
d3a34a0a | 899 | { |
345894b4 | 900 | unsigned int flag = token->val.str.text[0] - '0'; |
28e0f040 NB |
901 | |
902 | if (flag > last && flag <= 4 | |
903 | && (flag != 4 || last == 3) | |
904 | && (flag != 2 || last == 0)) | |
905 | return flag; | |
d3a34a0a | 906 | } |
93c80368 | 907 | |
345894b4 | 908 | if (token->type != CPP_EOF) |
0527bc4e | 909 | cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive", |
345894b4 | 910 | cpp_token_as_text (pfile, token)); |
93c80368 | 911 | return 0; |
d3a34a0a JM |
912 | } |
913 | ||
dcc229e5 | 914 | /* Subroutine of do_line and do_linemarker. Convert a number in STR, |
3b8f20a1 MLI |
915 | of length LEN, to binary; store it in NUMP, and return false if the |
916 | number was well-formed, true if not. WRAPPED is set to true if the | |
917 | number did not fit into 'unsigned long'. */ | |
918 | static bool | |
919 | strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped) | |
041c3194 | 920 | { |
1bb64668 | 921 | linenum_type reg = 0; |
3b8f20a1 MLI |
922 | linenum_type reg_prev = 0; |
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; |
041c3194 ZW |
931 | reg *= 10; |
932 | reg += c - '0'; | |
3b8f20a1 MLI |
933 | if (reg < reg_prev) |
934 | *wrapped = true; | |
935 | reg_prev = reg; | |
041c3194 ZW |
936 | } |
937 | *nump = reg; | |
3b8f20a1 | 938 | return false; |
041c3194 ZW |
939 | } |
940 | ||
6de1e2a9 | 941 | /* Interpret #line command. |
dcc229e5 ZW |
942 | Note that the filename string (if any) is a true string constant |
943 | (escapes are interpreted), unlike in #line. */ | |
711b8824 | 944 | static void |
6cf87ca4 | 945 | do_line (cpp_reader *pfile) |
7f2935c7 | 946 | { |
99b1c316 | 947 | class line_maps *line_table = pfile->line_table; |
0e50b624 | 948 | const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
2203a881 DP |
949 | |
950 | /* skip_rest_of_line() may cause line table to be realloc()ed so note down | |
951 | sysp right now. */ | |
952 | ||
46427374 | 953 | unsigned char map_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
4ed5bcfb | 954 | const cpp_token *token; |
46427374 | 955 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
1bb64668 | 956 | linenum_type new_lineno; |
93c80368 | 957 | |
27e2564a | 958 | /* C99 raised the minimum limit on #line numbers. */ |
1bb64668 | 959 | linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767; |
3b8f20a1 | 960 | bool wrapped; |
18a9d8ff | 961 | |
93c80368 | 962 | /* #line commands expand macros. */ |
4ed5bcfb NB |
963 | token = cpp_get_token (pfile); |
964 | if (token->type != CPP_NUMBER | |
1bb64668 | 965 | || strtolinenum (token->val.str.text, token->val.str.len, |
3b8f20a1 | 966 | &new_lineno, &wrapped)) |
7f2935c7 | 967 | { |
33ae4837 TT |
968 | if (token->type == CPP_EOF) |
969 | cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line"); | |
970 | else | |
971 | cpp_error (pfile, CPP_DL_ERROR, | |
972 | "\"%s\" after #line is not a positive integer", | |
973 | cpp_token_as_text (pfile, token)); | |
9ec7291f | 974 | return; |
df383483 | 975 | } |
7f2935c7 | 976 | |
3b8f20a1 | 977 | if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped)) |
0527bc4e | 978 | cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range"); |
3b8f20a1 MLI |
979 | else if (wrapped) |
980 | cpp_error (pfile, CPP_DL_WARNING, "line number out of range"); | |
7f2935c7 | 981 | |
4ed5bcfb NB |
982 | token = cpp_get_token (pfile); |
983 | if (token->type == CPP_STRING) | |
5538ada6 | 984 | { |
e6cc3a24 | 985 | cpp_string s = { 0, 0 }; |
423e95e2 | 986 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1, |
f1bf410c | 987 | &s, CPP_STRING)) |
e6cc3a24 | 988 | new_file = (const char *)s.text; |
a5cb563b | 989 | check_eol (pfile, true); |
dcc229e5 ZW |
990 | } |
991 | else if (token->type != CPP_EOF) | |
992 | { | |
0527bc4e | 993 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
dcc229e5 ZW |
994 | cpp_token_as_text (pfile, token)); |
995 | return; | |
996 | } | |
997 | ||
998 | skip_rest_of_line (pfile); | |
c7f9c0b9 | 999 | _cpp_do_file_change (pfile, LC_RENAME_VERBATIM, new_file, new_lineno, |
2203a881 | 1000 | map_sysp); |
c3388e62 | 1001 | line_table->seen_line_directive = true; |
dcc229e5 ZW |
1002 | } |
1003 | ||
1004 | /* Interpret the # 44 "file" [flags] notation, which has slightly | |
1005 | different syntax and semantics from #line: Flags are allowed, | |
1006 | and we never complain about the line number being too big. */ | |
1007 | static void | |
6cf87ca4 | 1008 | do_linemarker (cpp_reader *pfile) |
dcc229e5 | 1009 | { |
99b1c316 | 1010 | class line_maps *line_table = pfile->line_table; |
0e50b624 | 1011 | const line_map_ordinary *map = LINEMAPS_LAST_ORDINARY_MAP (line_table); |
dcc229e5 | 1012 | const cpp_token *token; |
46427374 | 1013 | const char *new_file = ORDINARY_MAP_FILE_NAME (map); |
1bb64668 | 1014 | linenum_type new_lineno; |
46427374 | 1015 | unsigned int new_sysp = ORDINARY_MAP_IN_SYSTEM_HEADER_P (map); |
c7f9c0b9 | 1016 | enum lc_reason reason = LC_RENAME_VERBATIM; |
dcc229e5 | 1017 | int flag; |
3b8f20a1 | 1018 | bool wrapped; |
dcc229e5 ZW |
1019 | |
1020 | /* Back up so we can get the number again. Putting this in | |
1021 | _cpp_handle_directive risks two calls to _cpp_backup_tokens in | |
1022 | some circumstances, which can segfault. */ | |
1023 | _cpp_backup_tokens (pfile, 1); | |
1024 | ||
1025 | /* #line commands expand macros. */ | |
1026 | token = cpp_get_token (pfile); | |
1027 | if (token->type != CPP_NUMBER | |
1bb64668 | 1028 | || strtolinenum (token->val.str.text, token->val.str.len, |
3b8f20a1 | 1029 | &new_lineno, &wrapped)) |
dcc229e5 | 1030 | { |
33ae4837 TT |
1031 | /* Unlike #line, there does not seem to be a way to get an EOF |
1032 | here. So, it should be safe to always spell the token. */ | |
0527bc4e JDA |
1033 | cpp_error (pfile, CPP_DL_ERROR, |
1034 | "\"%s\" after # is not a positive integer", | |
dcc229e5 ZW |
1035 | cpp_token_as_text (pfile, token)); |
1036 | return; | |
df383483 | 1037 | } |
941e09b6 | 1038 | |
dcc229e5 ZW |
1039 | token = cpp_get_token (pfile); |
1040 | if (token->type == CPP_STRING) | |
1041 | { | |
e6cc3a24 | 1042 | cpp_string s = { 0, 0 }; |
423e95e2 | 1043 | if (cpp_interpret_string_notranslate (pfile, &token->val.str, |
f1bf410c | 1044 | 1, &s, CPP_STRING)) |
e6cc3a24 | 1045 | new_file = (const char *)s.text; |
cf551fba | 1046 | |
dcc229e5 ZW |
1047 | new_sysp = 0; |
1048 | flag = read_flag (pfile, 0); | |
1049 | if (flag == 1) | |
1050 | { | |
1051 | reason = LC_ENTER; | |
1052 | /* Fake an include for cpp_included (). */ | |
1053 | _cpp_fake_include (pfile, new_file); | |
1054 | flag = read_flag (pfile, flag); | |
1055 | } | |
1056 | else if (flag == 2) | |
1057 | { | |
1058 | reason = LC_LEAVE; | |
1059 | flag = read_flag (pfile, flag); | |
1060 | } | |
1061 | if (flag == 3) | |
93c80368 | 1062 | { |
dcc229e5 ZW |
1063 | new_sysp = 1; |
1064 | flag = read_flag (pfile, flag); | |
1065 | if (flag == 4) | |
1066 | new_sysp = 2; | |
93c80368 | 1067 | } |
9d30f270 | 1068 | pfile->buffer->sysp = new_sysp; |
dcc229e5 | 1069 | |
a5cb563b | 1070 | check_eol (pfile, false); |
041c3194 | 1071 | } |
4ed5bcfb | 1072 | else if (token->type != CPP_EOF) |
27e2564a | 1073 | { |
0527bc4e | 1074 | cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename", |
4ed5bcfb | 1075 | cpp_token_as_text (pfile, token)); |
27e2564a NB |
1076 | return; |
1077 | } | |
7f2935c7 | 1078 | |
bdcbe496 | 1079 | skip_rest_of_line (pfile); |
00b0c19b | 1080 | |
3caf0ca1 BS |
1081 | if (reason == LC_LEAVE) |
1082 | { | |
12de2245 BS |
1083 | /* Reread map since cpp_get_token can invalidate it with a |
1084 | reallocation. */ | |
1085 | map = LINEMAPS_LAST_ORDINARY_MAP (line_table); | |
f10a9135 NS |
1086 | const line_map_ordinary *from |
1087 | = linemap_included_from_linemap (line_table, map); | |
400b8274 NS |
1088 | |
1089 | if (!from) | |
1090 | /* Not nested. */; | |
1091 | else if (!new_file[0]) | |
1092 | /* Leaving to "" means fill in the popped-to name. */ | |
1093 | new_file = ORDINARY_MAP_FILE_NAME (from); | |
1094 | else if (filename_cmp (ORDINARY_MAP_FILE_NAME (from), new_file) != 0) | |
1095 | /* It's the wrong name, Grommit! */ | |
1096 | from = NULL; | |
1097 | ||
1098 | if (!from) | |
3caf0ca1 BS |
1099 | { |
1100 | cpp_warning (pfile, CPP_W_NONE, | |
12de2245 BS |
1101 | "file \"%s\" linemarker ignored due to " |
1102 | "incorrect nesting", new_file); | |
3caf0ca1 BS |
1103 | return; |
1104 | } | |
1105 | } | |
400b8274 | 1106 | |
00b0c19b MLI |
1107 | /* Compensate for the increment in linemap_add that occurs in |
1108 | _cpp_do_file_change. We're currently at the start of the line | |
620e594b | 1109 | *following* the #line directive. A separate location_t for this |
00b0c19b MLI |
1110 | location makes no sense (until we do the LC_LEAVE), and |
1111 | complicates LAST_SOURCE_LINE_LOCATION. */ | |
1112 | pfile->line_table->highest_location--; | |
1113 | ||
bb74c963 | 1114 | _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp); |
c3388e62 | 1115 | line_table->seen_line_directive = true; |
27e2564a NB |
1116 | } |
1117 | ||
67821e3a | 1118 | /* Arrange the file_change callback. pfile->line has changed to |
47d89cf3 | 1119 | FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system |
a1f300c0 | 1120 | header, 2 for a system header that needs to be extern "C" protected, |
47d89cf3 | 1121 | and zero otherwise. */ |
eb1f4d9d | 1122 | void |
6cf87ca4 | 1123 | _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason, |
1bb64668 | 1124 | const char *to_file, linenum_type file_line, |
6cf87ca4 | 1125 | unsigned int sysp) |
27e2564a | 1126 | { |
0e50b624 | 1127 | linemap_assert (reason != LC_ENTER_MACRO); |
12f9df4e PB |
1128 | const struct line_map *map = linemap_add (pfile->line_table, reason, sysp, |
1129 | to_file, file_line); | |
0e50b624 | 1130 | const line_map_ordinary *ord_map = NULL; |
500bee0a | 1131 | if (map != NULL) |
0e50b624 DM |
1132 | { |
1133 | ord_map = linemap_check_ordinary (map); | |
1134 | linemap_line_start (pfile->line_table, | |
1135 | ORDINARY_MAP_STARTING_LINE_NUMBER (ord_map), | |
1136 | 127); | |
1137 | } | |
d82fc108 | 1138 | |
eb1f4d9d | 1139 | if (pfile->cb.file_change) |
0e50b624 | 1140 | pfile->cb.file_change (pfile, ord_map); |
7f2935c7 | 1141 | } |
941e09b6 | 1142 | |
5d8ebbd8 NB |
1143 | /* Report a warning or error detected by the program we are |
1144 | processing. Use the directive's tokens in the error message. */ | |
711b8824 | 1145 | static void |
c24300ba DM |
1146 | do_diagnostic (cpp_reader *pfile, enum cpp_diagnostic_level code, |
1147 | enum cpp_warning_reason reason, int print_dir) | |
7f2935c7 | 1148 | { |
5d6342eb TT |
1149 | const unsigned char *dir_name; |
1150 | unsigned char *line; | |
620e594b | 1151 | location_t src_loc = pfile->cur_token[-1].src_loc; |
5d6342eb TT |
1152 | |
1153 | if (print_dir) | |
1154 | dir_name = pfile->directive->name; | |
1155 | else | |
1156 | dir_name = NULL; | |
1157 | pfile->state.prevent_expansion++; | |
1158 | line = cpp_output_line_to_string (pfile, dir_name); | |
1159 | pfile->state.prevent_expansion--; | |
1160 | ||
87cf0651 SB |
1161 | if (code == CPP_DL_WARNING_SYSHDR && reason) |
1162 | cpp_warning_with_line_syshdr (pfile, reason, src_loc, 0, "%s", line); | |
1163 | else if (code == CPP_DL_WARNING && reason) | |
1164 | cpp_warning_with_line (pfile, reason, src_loc, 0, "%s", line); | |
1165 | else | |
1166 | cpp_error_with_line (pfile, code, src_loc, 0, "%s", line); | |
5d6342eb | 1167 | free (line); |
7f2935c7 PB |
1168 | } |
1169 | ||
838f313b | 1170 | static void |
6cf87ca4 | 1171 | do_error (cpp_reader *pfile) |
838f313b | 1172 | { |
c24300ba | 1173 | do_diagnostic (pfile, CPP_DL_ERROR, CPP_W_NONE, 1); |
838f313b | 1174 | } |
7f2935c7 | 1175 | |
711b8824 | 1176 | static void |
6cf87ca4 | 1177 | do_warning (cpp_reader *pfile) |
7f2935c7 | 1178 | { |
2f878973 | 1179 | /* We want #warning diagnostics to be emitted in system headers too. */ |
87cf0651 | 1180 | do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, CPP_W_WARNING_DIRECTIVE, 1); |
7f2935c7 PB |
1181 | } |
1182 | ||
a2a76ce7 | 1183 | /* Report program identification. */ |
711b8824 | 1184 | static void |
6cf87ca4 | 1185 | do_ident (cpp_reader *pfile) |
7f2935c7 | 1186 | { |
4ed5bcfb | 1187 | const cpp_token *str = cpp_get_token (pfile); |
58fea6af | 1188 | |
4ed5bcfb | 1189 | if (str->type != CPP_STRING) |
1ed17cd5 ZW |
1190 | cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive", |
1191 | pfile->directive->name); | |
93c80368 | 1192 | else if (pfile->cb.ident) |
6cf87ca4 | 1193 | pfile->cb.ident (pfile, pfile->directive_line, &str->val.str); |
a2a76ce7 | 1194 | |
a5cb563b | 1195 | check_eol (pfile, false); |
7f2935c7 PB |
1196 | } |
1197 | ||
a5da89c6 NB |
1198 | /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the |
1199 | matching entry, or NULL if none is found. The returned entry could | |
1200 | be the start of a namespace chain, or a pragma. */ | |
1201 | static struct pragma_entry * | |
6cf87ca4 | 1202 | lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma) |
82443371 | 1203 | { |
4b115ff0 NB |
1204 | while (chain && chain->pragma != pragma) |
1205 | chain = chain->next; | |
a5da89c6 NB |
1206 | |
1207 | return chain; | |
1208 | } | |
1209 | ||
bc4071dd RH |
1210 | /* Create and insert a blank pragma entry at the beginning of a |
1211 | singly-linked CHAIN. */ | |
a5da89c6 | 1212 | static struct pragma_entry * |
bc4071dd | 1213 | new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain) |
82443371 | 1214 | { |
c3f829c1 | 1215 | struct pragma_entry *new_entry; |
58fea6af | 1216 | |
c3f829c1 | 1217 | new_entry = (struct pragma_entry *) |
8c3b2693 | 1218 | _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry)); |
58fea6af | 1219 | |
bc4071dd | 1220 | memset (new_entry, 0, sizeof (struct pragma_entry)); |
c3f829c1 | 1221 | new_entry->next = *chain; |
bc4071dd | 1222 | |
c3f829c1 GDR |
1223 | *chain = new_entry; |
1224 | return new_entry; | |
58fea6af | 1225 | } |
82443371 | 1226 | |
a5da89c6 | 1227 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it |
bc4071dd RH |
1228 | goes in the global namespace. */ |
1229 | static struct pragma_entry * | |
1230 | register_pragma_1 (cpp_reader *pfile, const char *space, const char *name, | |
1231 | bool allow_name_expansion) | |
82443371 | 1232 | { |
a5da89c6 NB |
1233 | struct pragma_entry **chain = &pfile->pragmas; |
1234 | struct pragma_entry *entry; | |
4b115ff0 | 1235 | const cpp_hashnode *node; |
a5da89c6 | 1236 | |
a5da89c6 | 1237 | if (space) |
58fea6af | 1238 | { |
b6baa67d | 1239 | node = cpp_lookup (pfile, UC space, strlen (space)); |
4b115ff0 | 1240 | entry = lookup_pragma_entry (*chain, node); |
a5da89c6 | 1241 | if (!entry) |
bc4071dd RH |
1242 | { |
1243 | entry = new_pragma_entry (pfile, chain); | |
1244 | entry->pragma = node; | |
1245 | entry->is_nspace = true; | |
1246 | entry->allow_expansion = allow_name_expansion; | |
1247 | } | |
a5da89c6 NB |
1248 | else if (!entry->is_nspace) |
1249 | goto clash; | |
bc4071dd RH |
1250 | else if (entry->allow_expansion != allow_name_expansion) |
1251 | { | |
1252 | cpp_error (pfile, CPP_DL_ICE, | |
1253 | "registering pragmas in namespace \"%s\" with mismatched " | |
1254 | "name expansion", space); | |
1255 | return NULL; | |
1256 | } | |
a5da89c6 | 1257 | chain = &entry->u.space; |
58fea6af | 1258 | } |
bc4071dd RH |
1259 | else if (allow_name_expansion) |
1260 | { | |
1261 | cpp_error (pfile, CPP_DL_ICE, | |
1262 | "registering pragma \"%s\" with name expansion " | |
1263 | "and no namespace", name); | |
1264 | return NULL; | |
1265 | } | |
58fea6af | 1266 | |
a5da89c6 | 1267 | /* Check for duplicates. */ |
b6baa67d | 1268 | node = cpp_lookup (pfile, UC name, strlen (name)); |
4b115ff0 | 1269 | entry = lookup_pragma_entry (*chain, node); |
bc4071dd | 1270 | if (entry == NULL) |
a5da89c6 | 1271 | { |
bc4071dd RH |
1272 | entry = new_pragma_entry (pfile, chain); |
1273 | entry->pragma = node; | |
1274 | return entry; | |
a5da89c6 | 1275 | } |
bc4071dd RH |
1276 | |
1277 | if (entry->is_nspace) | |
1278 | clash: | |
1279 | cpp_error (pfile, CPP_DL_ICE, | |
1280 | "registering \"%s\" as both a pragma and a pragma namespace", | |
1281 | NODE_NAME (node)); | |
1282 | else if (space) | |
1283 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered", | |
1284 | space, name); | |
a5da89c6 | 1285 | else |
bc4071dd RH |
1286 | cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name); |
1287 | ||
1288 | return NULL; | |
1289 | } | |
1290 | ||
1291 | /* Register a cpplib internal pragma SPACE NAME with HANDLER. */ | |
1292 | static void | |
1293 | register_pragma_internal (cpp_reader *pfile, const char *space, | |
1294 | const char *name, pragma_cb handler) | |
1295 | { | |
1296 | struct pragma_entry *entry; | |
1297 | ||
1298 | entry = register_pragma_1 (pfile, space, name, false); | |
1299 | entry->is_internal = true; | |
1300 | entry->u.handler = handler; | |
21b11495 ZW |
1301 | } |
1302 | ||
1303 | /* Register a pragma NAME in namespace SPACE. If SPACE is null, it | |
1304 | goes in the global namespace. HANDLER is the handler it will call, | |
b5b3e36a DJ |
1305 | which must be non-NULL. If ALLOW_EXPANSION is set, allow macro |
1306 | expansion while parsing pragma NAME. This function is exported | |
1307 | from libcpp. */ | |
21b11495 ZW |
1308 | void |
1309 | cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name, | |
b5b3e36a | 1310 | pragma_cb handler, bool allow_expansion) |
21b11495 | 1311 | { |
bc4071dd RH |
1312 | struct pragma_entry *entry; |
1313 | ||
1314 | if (!handler) | |
1315 | { | |
1316 | cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler"); | |
1317 | return; | |
1318 | } | |
1319 | ||
1320 | entry = register_pragma_1 (pfile, space, name, false); | |
1321 | if (entry) | |
1322 | { | |
1323 | entry->allow_expansion = allow_expansion; | |
1324 | entry->u.handler = handler; | |
1325 | } | |
58fea6af | 1326 | } |
a5da89c6 | 1327 | |
bc4071dd RH |
1328 | /* Similarly, but create mark the pragma for deferred processing. |
1329 | When found, a CPP_PRAGMA token will be insertted into the stream | |
1330 | with IDENT in the token->u.pragma slot. */ | |
1331 | void | |
1332 | cpp_register_deferred_pragma (cpp_reader *pfile, const char *space, | |
1333 | const char *name, unsigned int ident, | |
1334 | bool allow_expansion, bool allow_name_expansion) | |
1335 | { | |
1336 | struct pragma_entry *entry; | |
1337 | ||
1338 | entry = register_pragma_1 (pfile, space, name, allow_name_expansion); | |
1339 | if (entry) | |
1340 | { | |
1341 | entry->is_deferred = true; | |
1342 | entry->allow_expansion = allow_expansion; | |
1343 | entry->u.ident = ident; | |
1344 | } | |
1345 | } | |
1346 | ||
a5da89c6 | 1347 | /* Register the pragmas the preprocessor itself handles. */ |
58fea6af | 1348 | void |
6cf87ca4 | 1349 | _cpp_init_internal_pragmas (cpp_reader *pfile) |
58fea6af | 1350 | { |
a5da89c6 | 1351 | /* Pragmas in the global namespace. */ |
bc4071dd | 1352 | register_pragma_internal (pfile, 0, "once", do_pragma_once); |
17e7cb85 KT |
1353 | register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro); |
1354 | register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro); | |
58fea6af | 1355 | |
a5da89c6 | 1356 | /* New GCC-specific pragmas should be put in the GCC namespace. */ |
bc4071dd RH |
1357 | register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison); |
1358 | register_pragma_internal (pfile, "GCC", "system_header", | |
1359 | do_pragma_system_header); | |
1360 | register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency); | |
f591bd8f FW |
1361 | register_pragma_internal (pfile, "GCC", "warning", do_pragma_warning); |
1362 | register_pragma_internal (pfile, "GCC", "error", do_pragma_error); | |
82443371 | 1363 | } |
7f2935c7 | 1364 | |
17211ab5 GK |
1365 | /* Return the number of registered pragmas in PE. */ |
1366 | ||
1367 | static int | |
6cf87ca4 | 1368 | count_registered_pragmas (struct pragma_entry *pe) |
17211ab5 GK |
1369 | { |
1370 | int ct = 0; | |
1371 | for (; pe != NULL; pe = pe->next) | |
1372 | { | |
1373 | if (pe->is_nspace) | |
1374 | ct += count_registered_pragmas (pe->u.space); | |
1375 | ct++; | |
1376 | } | |
1377 | return ct; | |
1378 | } | |
1379 | ||
1380 | /* Save into SD the names of the registered pragmas referenced by PE, | |
1381 | and return a pointer to the next free space in SD. */ | |
1382 | ||
1383 | static char ** | |
6cf87ca4 | 1384 | save_registered_pragmas (struct pragma_entry *pe, char **sd) |
17211ab5 GK |
1385 | { |
1386 | for (; pe != NULL; pe = pe->next) | |
1387 | { | |
1388 | if (pe->is_nspace) | |
1389 | sd = save_registered_pragmas (pe->u.space, sd); | |
c3f829c1 GDR |
1390 | *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident), |
1391 | HT_LEN (&pe->pragma->ident), | |
1392 | HT_LEN (&pe->pragma->ident) + 1); | |
17211ab5 GK |
1393 | } |
1394 | return sd; | |
1395 | } | |
1396 | ||
1397 | /* Return a newly-allocated array which saves the names of the | |
1398 | registered pragmas. */ | |
1399 | ||
1400 | char ** | |
6cf87ca4 | 1401 | _cpp_save_pragma_names (cpp_reader *pfile) |
17211ab5 GK |
1402 | { |
1403 | int ct = count_registered_pragmas (pfile->pragmas); | |
72bb2c39 | 1404 | char **result = XNEWVEC (char *, ct); |
17211ab5 GK |
1405 | (void) save_registered_pragmas (pfile->pragmas, result); |
1406 | return result; | |
1407 | } | |
1408 | ||
1409 | /* Restore from SD the names of the registered pragmas referenced by PE, | |
1410 | and return a pointer to the next unused name in SD. */ | |
1411 | ||
1412 | static char ** | |
6cf87ca4 ZW |
1413 | restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe, |
1414 | char **sd) | |
17211ab5 GK |
1415 | { |
1416 | for (; pe != NULL; pe = pe->next) | |
1417 | { | |
1418 | if (pe->is_nspace) | |
1419 | sd = restore_registered_pragmas (pfile, pe->u.space, sd); | |
b6baa67d | 1420 | pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd)); |
17211ab5 GK |
1421 | free (*sd); |
1422 | sd++; | |
1423 | } | |
1424 | return sd; | |
1425 | } | |
1426 | ||
1427 | /* Restore the names of the registered pragmas from SAVED. */ | |
1428 | ||
1429 | void | |
6cf87ca4 | 1430 | _cpp_restore_pragma_names (cpp_reader *pfile, char **saved) |
17211ab5 GK |
1431 | { |
1432 | (void) restore_registered_pragmas (pfile, pfile->pragmas, saved); | |
1433 | free (saved); | |
1434 | } | |
1435 | ||
a5da89c6 NB |
1436 | /* Pragmata handling. We handle some, and pass the rest on to the |
1437 | front end. C99 defines three pragmas and says that no macro | |
1438 | expansion is to be performed on them; whether or not macro | |
1439 | expansion happens for other pragmas is implementation defined. | |
bc4071dd RH |
1440 | This implementation allows for a mix of both, since GCC did not |
1441 | traditionally macro expand its (few) pragmas, whereas OpenMP | |
1442 | specifies that macro expansion should happen. */ | |
711b8824 | 1443 | static void |
6cf87ca4 | 1444 | do_pragma (cpp_reader *pfile) |
7f2935c7 | 1445 | { |
a5da89c6 | 1446 | const struct pragma_entry *p = NULL; |
f3d25c65 | 1447 | const cpp_token *token, *pragma_token; |
620e594b | 1448 | location_t pragma_token_virt_loc = 0; |
1c90c6f9 | 1449 | cpp_token ns_token; |
a5da89c6 | 1450 | unsigned int count = 1; |
add7091b | 1451 | |
93c80368 | 1452 | pfile->state.prevent_expansion++; |
58fea6af | 1453 | |
f3d25c65 DS |
1454 | pragma_token = token = cpp_get_token_with_location (pfile, |
1455 | &pragma_token_virt_loc); | |
1c90c6f9 | 1456 | ns_token = *token; |
4ed5bcfb | 1457 | if (token->type == CPP_NAME) |
0172e2bc | 1458 | { |
9a0c6187 | 1459 | p = lookup_pragma_entry (pfile->pragmas, token->val.node.node); |
a5da89c6 | 1460 | if (p && p->is_nspace) |
58fea6af | 1461 | { |
bc4071dd RH |
1462 | bool allow_name_expansion = p->allow_expansion; |
1463 | if (allow_name_expansion) | |
828a7f76 | 1464 | pfile->state.prevent_expansion--; |
38fbfaf6 | 1465 | |
a5da89c6 NB |
1466 | token = cpp_get_token (pfile); |
1467 | if (token->type == CPP_NAME) | |
9a0c6187 | 1468 | p = lookup_pragma_entry (p->u.space, token->val.node.node); |
a5da89c6 NB |
1469 | else |
1470 | p = NULL; | |
bc4071dd RH |
1471 | if (allow_name_expansion) |
1472 | pfile->state.prevent_expansion++; | |
1473 | count = 2; | |
58fea6af | 1474 | } |
58fea6af | 1475 | } |
041c3194 | 1476 | |
3da3d587 | 1477 | if (p) |
e2e1fa50 | 1478 | { |
bc4071dd RH |
1479 | if (p->is_deferred) |
1480 | { | |
f3d25c65 | 1481 | pfile->directive_result.src_loc = pragma_token_virt_loc; |
bc4071dd RH |
1482 | pfile->directive_result.type = CPP_PRAGMA; |
1483 | pfile->directive_result.flags = pragma_token->flags; | |
1484 | pfile->directive_result.val.pragma = p->u.ident; | |
1485 | pfile->state.in_deferred_pragma = true; | |
1486 | pfile->state.pragma_allow_expansion = p->allow_expansion; | |
1487 | if (!p->allow_expansion) | |
1488 | pfile->state.prevent_expansion++; | |
1489 | } | |
1490 | else | |
3da3d587 | 1491 | { |
bc4071dd RH |
1492 | /* Since the handler below doesn't get the line number, that |
1493 | it might need for diagnostics, make sure it has the right | |
3da3d587 ZW |
1494 | numbers in place. */ |
1495 | if (pfile->cb.line_change) | |
1496 | (*pfile->cb.line_change) (pfile, pragma_token, false); | |
bc4071dd | 1497 | if (p->allow_expansion) |
b5b3e36a | 1498 | pfile->state.prevent_expansion--; |
3da3d587 | 1499 | (*p->u.handler) (pfile); |
bc4071dd | 1500 | if (p->allow_expansion) |
b5b3e36a | 1501 | pfile->state.prevent_expansion++; |
3da3d587 | 1502 | } |
21b11495 | 1503 | } |
d82fc108 | 1504 | else if (pfile->cb.def_pragma) |
bdcbe496 | 1505 | { |
1c90c6f9 JJ |
1506 | if (count == 1 || pfile->context->prev == NULL) |
1507 | _cpp_backup_tokens (pfile, count); | |
1508 | else | |
1509 | { | |
1510 | /* Invalid name comes from macro expansion, _cpp_backup_tokens | |
1511 | won't allow backing 2 tokens. */ | |
1512 | /* ??? The token buffer is leaked. Perhaps if def_pragma hook | |
1513 | reads both tokens, we could perhaps free it, but if it doesn't, | |
1514 | we don't know the exact lifespan. */ | |
1515 | cpp_token *toks = XNEWVEC (cpp_token, 2); | |
1516 | toks[0] = ns_token; | |
1517 | toks[0].flags |= NO_EXPAND; | |
1518 | toks[1] = *token; | |
1519 | toks[1].flags |= NO_EXPAND; | |
1520 | _cpp_push_token_context (pfile, NULL, toks, 2); | |
1521 | } | |
6cf87ca4 | 1522 | pfile->cb.def_pragma (pfile, pfile->directive_line); |
bdcbe496 | 1523 | } |
a5da89c6 | 1524 | |
97293897 | 1525 | pfile->state.prevent_expansion--; |
82443371 NS |
1526 | } |
1527 | ||
5d8ebbd8 | 1528 | /* Handle #pragma once. */ |
58fea6af | 1529 | static void |
6cf87ca4 | 1530 | do_pragma_once (cpp_reader *pfile) |
a2a76ce7 | 1531 | { |
705e2d28 | 1532 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1533 | cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file"); |
93c80368 | 1534 | |
a5cb563b | 1535 | check_eol (pfile, false); |
49634b3a | 1536 | _cpp_mark_file_once_only (pfile, pfile->buffer->file); |
a2a76ce7 | 1537 | } |
fc009f96 | 1538 | |
17e7cb85 KT |
1539 | /* Handle #pragma push_macro(STRING). */ |
1540 | static void | |
1541 | do_pragma_push_macro (cpp_reader *pfile) | |
1542 | { | |
d6874138 KT |
1543 | cpp_hashnode *node; |
1544 | size_t defnlen; | |
1545 | const uchar *defn = NULL; | |
17e7cb85 KT |
1546 | char *macroname, *dest; |
1547 | const char *limit, *src; | |
1548 | const cpp_token *txt; | |
1549 | struct def_pragma_macro *c; | |
1550 | ||
1551 | txt = get__Pragma_string (pfile); | |
1552 | if (!txt) | |
1553 | { | |
620e594b | 1554 | location_t src_loc = pfile->cur_token[-1].src_loc; |
17e7cb85 KT |
1555 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, |
1556 | "invalid #pragma push_macro directive"); | |
1557 | check_eol (pfile, false); | |
1558 | skip_rest_of_line (pfile); | |
1559 | return; | |
1560 | } | |
1561 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1562 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1563 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1564 | while (src < limit) | |
1565 | { | |
1566 | /* We know there is a character following the backslash. */ | |
1567 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1568 | src++; | |
1569 | *dest++ = *src++; | |
1570 | } | |
1571 | *dest = 0; | |
1572 | check_eol (pfile, false); | |
1573 | skip_rest_of_line (pfile); | |
1574 | c = XNEW (struct def_pragma_macro); | |
d6874138 | 1575 | memset (c, 0, sizeof (struct def_pragma_macro)); |
17e7cb85 KT |
1576 | c->name = XNEWVAR (char, strlen (macroname) + 1); |
1577 | strcpy (c->name, macroname); | |
1578 | c->next = pfile->pushed_macros; | |
d6874138 KT |
1579 | node = _cpp_lex_identifier (pfile, c->name); |
1580 | if (node->type == NT_VOID) | |
1581 | c->is_undef = 1; | |
aa23e73b JJ |
1582 | else if (node->type == NT_BUILTIN_MACRO) |
1583 | c->is_builtin = 1; | |
d6874138 KT |
1584 | else |
1585 | { | |
1586 | defn = cpp_macro_definition (pfile, node); | |
1587 | defnlen = ustrlen (defn); | |
1588 | c->definition = XNEWVEC (uchar, defnlen + 2); | |
1589 | c->definition[defnlen] = '\n'; | |
1590 | c->definition[defnlen + 1] = 0; | |
1591 | c->line = node->value.macro->line; | |
1592 | c->syshdr = node->value.macro->syshdr; | |
1593 | c->used = node->value.macro->used; | |
1594 | memcpy (c->definition, defn, defnlen); | |
1595 | } | |
1596 | ||
17e7cb85 KT |
1597 | pfile->pushed_macros = c; |
1598 | } | |
1599 | ||
1600 | /* Handle #pragma pop_macro(STRING). */ | |
1601 | static void | |
1602 | do_pragma_pop_macro (cpp_reader *pfile) | |
1603 | { | |
1604 | char *macroname, *dest; | |
1605 | const char *limit, *src; | |
1606 | const cpp_token *txt; | |
1607 | struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros; | |
1608 | txt = get__Pragma_string (pfile); | |
1609 | if (!txt) | |
1610 | { | |
620e594b | 1611 | location_t src_loc = pfile->cur_token[-1].src_loc; |
17e7cb85 KT |
1612 | cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0, |
1613 | "invalid #pragma pop_macro directive"); | |
1614 | check_eol (pfile, false); | |
1615 | skip_rest_of_line (pfile); | |
1616 | return; | |
1617 | } | |
1618 | dest = macroname = (char *) alloca (txt->val.str.len + 2); | |
1619 | src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L')); | |
1620 | limit = (const char *) (txt->val.str.text + txt->val.str.len - 1); | |
1621 | while (src < limit) | |
1622 | { | |
1623 | /* We know there is a character following the backslash. */ | |
1624 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1625 | src++; | |
1626 | *dest++ = *src++; | |
1627 | } | |
1628 | *dest = 0; | |
1629 | check_eol (pfile, false); | |
1630 | skip_rest_of_line (pfile); | |
1631 | ||
1632 | while (c != NULL) | |
1633 | { | |
1634 | if (!strcmp (c->name, macroname)) | |
1635 | { | |
1636 | if (!l) | |
1637 | pfile->pushed_macros = c->next; | |
1638 | else | |
1639 | l->next = c->next; | |
d6874138 KT |
1640 | cpp_pop_definition (pfile, c); |
1641 | free (c->definition); | |
17e7cb85 KT |
1642 | free (c->name); |
1643 | free (c); | |
1644 | break; | |
1645 | } | |
1646 | l = c; | |
1647 | c = c->next; | |
1648 | } | |
1649 | } | |
1650 | ||
c3bf3e6e NB |
1651 | /* Handle #pragma GCC poison, to poison one or more identifiers so |
1652 | that the lexer produces a hard error for each subsequent usage. */ | |
58fea6af | 1653 | static void |
6cf87ca4 | 1654 | do_pragma_poison (cpp_reader *pfile) |
a2a76ce7 | 1655 | { |
345894b4 | 1656 | const cpp_token *tok; |
f8f769ea | 1657 | cpp_hashnode *hp; |
a2a76ce7 | 1658 | |
93c80368 | 1659 | pfile->state.poisoned_ok = 1; |
a2a76ce7 ZW |
1660 | for (;;) |
1661 | { | |
345894b4 NB |
1662 | tok = _cpp_lex_token (pfile); |
1663 | if (tok->type == CPP_EOF) | |
a2a76ce7 | 1664 | break; |
345894b4 | 1665 | if (tok->type != CPP_NAME) |
fc009f96 | 1666 | { |
0527bc4e JDA |
1667 | cpp_error (pfile, CPP_DL_ERROR, |
1668 | "invalid #pragma GCC poison directive"); | |
93c80368 | 1669 | break; |
fc009f96 GK |
1670 | } |
1671 | ||
9a0c6187 | 1672 | hp = tok->val.node.node; |
93c80368 NB |
1673 | if (hp->flags & NODE_POISONED) |
1674 | continue; | |
1675 | ||
3f6677f4 | 1676 | if (cpp_macro_p (hp)) |
0527bc4e | 1677 | cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"", |
ebef4e8c | 1678 | NODE_NAME (hp)); |
93c80368 NB |
1679 | _cpp_free_definition (hp); |
1680 | hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC; | |
fc009f96 | 1681 | } |
93c80368 | 1682 | pfile->state.poisoned_ok = 0; |
7f2935c7 | 1683 | } |
2c0b35cb ZW |
1684 | |
1685 | /* Mark the current header as a system header. This will suppress | |
1686 | some categories of warnings (notably those from -pedantic). It is | |
1687 | intended for use in system libraries that cannot be implemented in | |
1688 | conforming C, but cannot be certain that their headers appear in a | |
1689 | system include directory. To prevent abuse, it is rejected in the | |
1690 | primary source file. */ | |
58fea6af | 1691 | static void |
6cf87ca4 | 1692 | do_pragma_system_header (cpp_reader *pfile) |
2c0b35cb | 1693 | { |
705e2d28 | 1694 | if (cpp_in_primary_file (pfile)) |
0527bc4e | 1695 | cpp_error (pfile, CPP_DL_WARNING, |
ebef4e8c | 1696 | "#pragma system_header ignored outside include file"); |
2c0b35cb | 1697 | else |
d82fc108 | 1698 | { |
a5cb563b | 1699 | check_eol (pfile, false); |
bdcbe496 | 1700 | skip_rest_of_line (pfile); |
d82fc108 NB |
1701 | cpp_make_system_header (pfile, 1, 0); |
1702 | } | |
2c0b35cb | 1703 | } |
f3f751ad NS |
1704 | |
1705 | /* Check the modified date of the current include file against a specified | |
1706 | file. Issue a diagnostic, if the specified file is newer. We use this to | |
1707 | determine if a fixed header should be refixed. */ | |
58fea6af | 1708 | static void |
6cf87ca4 | 1709 | do_pragma_dependency (cpp_reader *pfile) |
f3f751ad | 1710 | { |
74eb4b3e NB |
1711 | const char *fname; |
1712 | int angle_brackets, ordering; | |
620e594b | 1713 | location_t location; |
df383483 | 1714 | |
a28fbdba | 1715 | fname = parse_include (pfile, &angle_brackets, NULL, &location); |
74eb4b3e | 1716 | if (!fname) |
58fea6af | 1717 | return; |
041c3194 | 1718 | |
74eb4b3e | 1719 | ordering = _cpp_compare_file_date (pfile, fname, angle_brackets); |
f3f751ad | 1720 | if (ordering < 0) |
0527bc4e | 1721 | cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname); |
f3f751ad NS |
1722 | else if (ordering > 0) |
1723 | { | |
0527bc4e JDA |
1724 | cpp_error (pfile, CPP_DL_WARNING, |
1725 | "current file is older than %s", fname); | |
4ed5bcfb | 1726 | if (cpp_get_token (pfile)->type != CPP_EOF) |
bdcbe496 NB |
1727 | { |
1728 | _cpp_backup_tokens (pfile, 1); | |
c24300ba | 1729 | do_diagnostic (pfile, CPP_DL_WARNING, CPP_W_NONE, 0); |
bdcbe496 | 1730 | } |
f3f751ad | 1731 | } |
74eb4b3e | 1732 | |
fad205ff | 1733 | free ((void *) fname); |
f3f751ad NS |
1734 | } |
1735 | ||
f591bd8f FW |
1736 | /* Issue a diagnostic with the message taken from the pragma. If |
1737 | ERROR is true, the diagnostic is a warning, otherwise, it is an | |
1738 | error. */ | |
1739 | static void | |
1740 | do_pragma_warning_or_error (cpp_reader *pfile, bool error) | |
1741 | { | |
1742 | const cpp_token *tok = _cpp_lex_token (pfile); | |
1743 | cpp_string str; | |
1744 | if (tok->type != CPP_STRING | |
1745 | || !cpp_interpret_string_notranslate (pfile, &tok->val.str, 1, &str, | |
1746 | CPP_STRING) | |
1747 | || str.len == 0) | |
1748 | { | |
1749 | cpp_error (pfile, CPP_DL_ERROR, "invalid \"#pragma GCC %s\" directive", | |
1750 | error ? "error" : "warning"); | |
1751 | return; | |
1752 | } | |
1753 | cpp_error (pfile, error ? CPP_DL_ERROR : CPP_DL_WARNING, | |
1754 | "%s", str.text); | |
1755 | free ((void *)str.text); | |
1756 | } | |
1757 | ||
1758 | /* Issue a warning diagnostic. */ | |
1759 | static void | |
1760 | do_pragma_warning (cpp_reader *pfile) | |
1761 | { | |
1762 | do_pragma_warning_or_error (pfile, false); | |
1763 | } | |
1764 | ||
1765 | /* Issue an error diagnostic. */ | |
1766 | static void | |
1767 | do_pragma_error (cpp_reader *pfile) | |
1768 | { | |
1769 | do_pragma_warning_or_error (pfile, true); | |
1770 | } | |
1771 | ||
4ed5bcfb NB |
1772 | /* Get a token but skip padding. */ |
1773 | static const cpp_token * | |
6cf87ca4 | 1774 | get_token_no_padding (cpp_reader *pfile) |
a5c3cccd | 1775 | { |
4ed5bcfb NB |
1776 | for (;;) |
1777 | { | |
1778 | const cpp_token *result = cpp_get_token (pfile); | |
1779 | if (result->type != CPP_PADDING) | |
1780 | return result; | |
1781 | } | |
1782 | } | |
a5c3cccd | 1783 | |
4ed5bcfb NB |
1784 | /* Check syntax is "(string-literal)". Returns the string on success, |
1785 | or NULL on failure. */ | |
1786 | static const cpp_token * | |
6cf87ca4 | 1787 | get__Pragma_string (cpp_reader *pfile) |
4ed5bcfb NB |
1788 | { |
1789 | const cpp_token *string; | |
5b9a40df | 1790 | const cpp_token *paren; |
a5c3cccd | 1791 | |
5b9a40df TT |
1792 | paren = get_token_no_padding (pfile); |
1793 | if (paren->type == CPP_EOF) | |
1794 | _cpp_backup_tokens (pfile, 1); | |
1795 | if (paren->type != CPP_OPEN_PAREN) | |
4ed5bcfb NB |
1796 | return NULL; |
1797 | ||
1798 | string = get_token_no_padding (pfile); | |
5b9a40df TT |
1799 | if (string->type == CPP_EOF) |
1800 | _cpp_backup_tokens (pfile, 1); | |
b6baa67d | 1801 | if (string->type != CPP_STRING && string->type != CPP_WSTRING |
2c6e3f55 JJ |
1802 | && string->type != CPP_STRING32 && string->type != CPP_STRING16 |
1803 | && string->type != CPP_UTF8STRING) | |
4ed5bcfb NB |
1804 | return NULL; |
1805 | ||
5b9a40df TT |
1806 | paren = get_token_no_padding (pfile); |
1807 | if (paren->type == CPP_EOF) | |
1808 | _cpp_backup_tokens (pfile, 1); | |
1809 | if (paren->type != CPP_CLOSE_PAREN) | |
4ed5bcfb | 1810 | return NULL; |
a5c3cccd | 1811 | |
4ed5bcfb | 1812 | return string; |
a5c3cccd NB |
1813 | } |
1814 | ||
87062813 NB |
1815 | /* Destringize IN into a temporary buffer, by removing the first \ of |
1816 | \" and \\ sequences, and process the result as a #pragma directive. */ | |
1817 | static void | |
0afff540 | 1818 | destringize_and_run (cpp_reader *pfile, const cpp_string *in, |
620e594b | 1819 | location_t expansion_loc) |
a5c3cccd NB |
1820 | { |
1821 | const unsigned char *src, *limit; | |
87062813 | 1822 | char *dest, *result; |
bc4071dd RH |
1823 | cpp_context *saved_context; |
1824 | cpp_token *saved_cur_token; | |
1825 | tokenrun *saved_cur_run; | |
1826 | cpp_token *toks; | |
1827 | int count; | |
14ccf800 | 1828 | const struct directive *save_directive; |
a5c3cccd | 1829 | |
c3f829c1 | 1830 | dest = result = (char *) alloca (in->len - 1); |
6338b358 NB |
1831 | src = in->text + 1 + (in->text[0] == 'L'); |
1832 | limit = in->text + in->len - 1; | |
1833 | while (src < limit) | |
a5c3cccd NB |
1834 | { |
1835 | /* We know there is a character following the backslash. */ | |
1836 | if (*src == '\\' && (src[1] == '\\' || src[1] == '"')) | |
1837 | src++; | |
1838 | *dest++ = *src++; | |
1839 | } | |
26aea073 | 1840 | *dest = '\n'; |
a5c3cccd | 1841 | |
8128cccf NB |
1842 | /* Ugh; an awful kludge. We are really not set up to be lexing |
1843 | tokens when in the middle of a macro expansion. Use a new | |
1844 | context to force cpp_get_token to lex, and so skip_rest_of_line | |
1845 | doesn't go beyond the end of the text. Also, remember the | |
1846 | current lexing position so we can return to it later. | |
1847 | ||
1848 | Something like line-at-a-time lexing should remove the need for | |
1849 | this. */ | |
bc4071dd RH |
1850 | saved_context = pfile->context; |
1851 | saved_cur_token = pfile->cur_token; | |
1852 | saved_cur_run = pfile->cur_run; | |
79ba5e3b | 1853 | |
3ad64f53 | 1854 | pfile->context = XCNEW (cpp_context); |
79ba5e3b | 1855 | |
bc4071dd RH |
1856 | /* Inline run_directive, since we need to delay the _cpp_pop_buffer |
1857 | until we've read all of the tokens that we want. */ | |
1858 | cpp_push_buffer (pfile, (const uchar *) result, dest - result, | |
1859 | /* from_stage3 */ true); | |
1860 | /* ??? Antique Disgusting Hack. What does this do? */ | |
1861 | if (pfile->buffer->prev) | |
1862 | pfile->buffer->file = pfile->buffer->prev->file; | |
79ba5e3b | 1863 | |
bc4071dd RH |
1864 | start_directive (pfile); |
1865 | _cpp_clean_line (pfile); | |
14ccf800 TT |
1866 | save_directive = pfile->directive; |
1867 | pfile->directive = &dtable[T_PRAGMA]; | |
bc4071dd RH |
1868 | do_pragma (pfile); |
1869 | end_directive (pfile, 1); | |
14ccf800 | 1870 | pfile->directive = save_directive; |
79ba5e3b | 1871 | |
bc4071dd RH |
1872 | /* We always insert at least one token, the directive result. It'll |
1873 | either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we | |
1874 | need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */ | |
1875 | ||
1876 | /* If we're not handling the pragma internally, read all of the tokens from | |
1877 | the string buffer now, while the string buffer is still installed. */ | |
1878 | /* ??? Note that the token buffer allocated here is leaked. It's not clear | |
1879 | to me what the true lifespan of the tokens are. It would appear that | |
1880 | the lifespan is the entire parse of the main input stream, in which case | |
1881 | this may not be wrong. */ | |
1882 | if (pfile->directive_result.type == CPP_PRAGMA) | |
1883 | { | |
1884 | int maxcount; | |
1885 | ||
1886 | count = 1; | |
1887 | maxcount = 50; | |
1888 | toks = XNEWVEC (cpp_token, maxcount); | |
1889 | toks[0] = pfile->directive_result; | |
79ba5e3b | 1890 | |
bc4071dd RH |
1891 | do |
1892 | { | |
1893 | if (count == maxcount) | |
1894 | { | |
1895 | maxcount = maxcount * 3 / 2; | |
1896 | toks = XRESIZEVEC (cpp_token, toks, maxcount); | |
1897 | } | |
1c90c6f9 | 1898 | toks[count] = *cpp_get_token (pfile); |
0afff540 DM |
1899 | /* _Pragma is a builtin, so we're not within a macro-map, and so |
1900 | the token locations are set to bogus ordinary locations | |
1901 | near to, but after that of the "_Pragma". | |
1902 | Paper over this by setting them equal to the location of the | |
1903 | _Pragma itself (PR preprocessor/69126). */ | |
1904 | toks[count].src_loc = expansion_loc; | |
1c90c6f9 JJ |
1905 | /* Macros have been already expanded by cpp_get_token |
1906 | if the pragma allowed expansion. */ | |
1907 | toks[count++].flags |= NO_EXPAND; | |
bc4071dd RH |
1908 | } |
1909 | while (toks[count-1].type != CPP_PRAGMA_EOL); | |
1910 | } | |
1911 | else | |
1912 | { | |
1913 | count = 1; | |
1914 | toks = XNEW (cpp_token); | |
1915 | toks[0] = pfile->directive_result; | |
1916 | ||
1917 | /* If we handled the entire pragma internally, make sure we get the | |
1918 | line number correct for the next token. */ | |
1919 | if (pfile->cb.line_change) | |
1920 | pfile->cb.line_change (pfile, pfile->cur_token, false); | |
1921 | } | |
1922 | ||
1923 | /* Finish inlining run_directive. */ | |
1924 | pfile->buffer->file = NULL; | |
1925 | _cpp_pop_buffer (pfile); | |
1926 | ||
1927 | /* Reset the old macro state before ... */ | |
1928 | XDELETE (pfile->context); | |
1929 | pfile->context = saved_context; | |
1930 | pfile->cur_token = saved_cur_token; | |
1931 | pfile->cur_run = saved_cur_run; | |
1932 | ||
1933 | /* ... inserting the new tokens we collected. */ | |
1934 | _cpp_push_token_context (pfile, NULL, toks, count); | |
a5c3cccd NB |
1935 | } |
1936 | ||
5b9a40df TT |
1937 | /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */ |
1938 | int | |
620e594b | 1939 | _cpp_do__Pragma (cpp_reader *pfile, location_t expansion_loc) |
a5c3cccd | 1940 | { |
4ed5bcfb | 1941 | const cpp_token *string = get__Pragma_string (pfile); |
21b11495 | 1942 | pfile->directive_result.type = CPP_PADDING; |
a5c3cccd | 1943 | |
79ba5e3b | 1944 | if (string) |
5b9a40df | 1945 | { |
0afff540 | 1946 | destringize_and_run (pfile, &string->val.str, expansion_loc); |
5b9a40df TT |
1947 | return 1; |
1948 | } | |
1949 | cpp_error (pfile, CPP_DL_ERROR, | |
1950 | "_Pragma takes a parenthesized string literal"); | |
1951 | return 0; | |
a5c3cccd | 1952 | } |
21b11495 | 1953 | |
5d8ebbd8 | 1954 | /* Handle #ifdef. */ |
711b8824 | 1955 | static void |
6cf87ca4 | 1956 | do_ifdef (cpp_reader *pfile) |
168d3732 | 1957 | { |
93c80368 | 1958 | int skip = 1; |
041c3194 | 1959 | |
cef0d199 | 1960 | if (! pfile->state.skipping) |
93c80368 | 1961 | { |
93d45d9e | 1962 | cpp_hashnode *node = lex_macro_node (pfile, false); |
041c3194 | 1963 | |
93c80368 | 1964 | if (node) |
a69cbaac | 1965 | { |
ad1a3914 | 1966 | skip = !_cpp_defined_macro_p (node); |
a69cbaac | 1967 | _cpp_mark_macro_used (node); |
3f6677f4 | 1968 | _cpp_maybe_notify_macro_use (pfile, node); |
3de8a540 AC |
1969 | if (pfile->cb.used) |
1970 | pfile->cb.used (pfile, pfile->directive_line, node); | |
a5cb563b | 1971 | check_eol (pfile, false); |
a69cbaac | 1972 | } |
93c80368 | 1973 | } |
168d3732 | 1974 | |
93c80368 NB |
1975 | push_conditional (pfile, skip, T_IFDEF, 0); |
1976 | } | |
168d3732 | 1977 | |
5d8ebbd8 | 1978 | /* Handle #ifndef. */ |
711b8824 | 1979 | static void |
6cf87ca4 | 1980 | do_ifndef (cpp_reader *pfile) |
168d3732 | 1981 | { |
93c80368 | 1982 | int skip = 1; |
93d45d9e | 1983 | cpp_hashnode *node = 0; |
168d3732 | 1984 | |
cef0d199 | 1985 | if (! pfile->state.skipping) |
5af7e2c2 | 1986 | { |
ee1c2a10 | 1987 | node = lex_macro_node (pfile, false); |
b43db0b3 GK |
1988 | |
1989 | if (node) | |
a69cbaac | 1990 | { |
f3c33d9d | 1991 | /* Do not treat conditional macros as being defined. This is due to |
2f2aeda9 UW |
1992 | the powerpc port using conditional macros for 'vector', 'bool', |
1993 | and 'pixel' to act as conditional keywords. This messes up tests | |
1994 | like #ifndef bool. */ | |
ad1a3914 | 1995 | skip = _cpp_defined_macro_p (node); |
a69cbaac | 1996 | _cpp_mark_macro_used (node); |
3f6677f4 | 1997 | _cpp_maybe_notify_macro_use (pfile, node); |
3de8a540 AC |
1998 | if (pfile->cb.used) |
1999 | pfile->cb.used (pfile, pfile->directive_line, node); | |
a5cb563b | 2000 | check_eol (pfile, false); |
a69cbaac | 2001 | } |
5af7e2c2 | 2002 | } |
041c3194 | 2003 | |
93c80368 | 2004 | push_conditional (pfile, skip, T_IFNDEF, node); |
7f2935c7 PB |
2005 | } |
2006 | ||
6d18adbc NB |
2007 | /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in |
2008 | pfile->mi_ind_cmacro so we can handle multiple-include | |
6356f892 | 2009 | optimizations. If macro expansion occurs in the expression, we |
6d18adbc NB |
2010 | cannot treat it as a controlling conditional, since the expansion |
2011 | could change in the future. That is handled by cpp_get_token. */ | |
711b8824 | 2012 | static void |
6cf87ca4 | 2013 | do_if (cpp_reader *pfile) |
7f2935c7 | 2014 | { |
93c80368 | 2015 | int skip = 1; |
7f2935c7 | 2016 | |
cef0d199 | 2017 | if (! pfile->state.skipping) |
d750887f | 2018 | skip = _cpp_parse_expr (pfile, true) == false; |
93c80368 | 2019 | |
6d18adbc | 2020 | push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro); |
7f2935c7 PB |
2021 | } |
2022 | ||
b528a07e | 2023 | /* Flip skipping state if appropriate and continue without changing |
ea4a453b ZW |
2024 | if_stack; this is so that the error message for missing #endif's |
2025 | etc. will point to the original #if. */ | |
711b8824 | 2026 | static void |
6cf87ca4 | 2027 | do_else (cpp_reader *pfile) |
ed705a82 | 2028 | { |
b528a07e NB |
2029 | cpp_buffer *buffer = pfile->buffer; |
2030 | struct if_stack *ifs = buffer->if_stack; | |
ff2b53ef | 2031 | |
ea4a453b | 2032 | if (ifs == NULL) |
0527bc4e | 2033 | cpp_error (pfile, CPP_DL_ERROR, "#else without #if"); |
93c80368 | 2034 | else |
ff2b53ef | 2035 | { |
93c80368 NB |
2036 | if (ifs->type == T_ELSE) |
2037 | { | |
0527bc4e JDA |
2038 | cpp_error (pfile, CPP_DL_ERROR, "#else after #else"); |
2039 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
93c80368 NB |
2040 | "the conditional began here"); |
2041 | } | |
b528a07e NB |
2042 | ifs->type = T_ELSE; |
2043 | ||
cef0d199 NB |
2044 | /* Skip any future (erroneous) #elses or #elifs. */ |
2045 | pfile->state.skipping = ifs->skip_elses; | |
2046 | ifs->skip_elses = true; | |
7f2935c7 | 2047 | |
93c80368 NB |
2048 | /* Invalidate any controlling macro. */ |
2049 | ifs->mi_cmacro = 0; | |
93c80368 | 2050 | |
cef0d199 | 2051 | /* Only check EOL if was not originally skipping. */ |
909de5da | 2052 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
81b5d104 | 2053 | check_eol_endif_labels (pfile); |
cef0d199 | 2054 | } |
7f2935c7 PB |
2055 | } |
2056 | ||
5d8ebbd8 | 2057 | /* Handle a #elif directive by not changing if_stack either. See the |
93c80368 | 2058 | comment above do_else. */ |
711b8824 | 2059 | static void |
6cf87ca4 | 2060 | do_elif (cpp_reader *pfile) |
7f2935c7 | 2061 | { |
b528a07e NB |
2062 | cpp_buffer *buffer = pfile->buffer; |
2063 | struct if_stack *ifs = buffer->if_stack; | |
7f2935c7 | 2064 | |
ea4a453b | 2065 | if (ifs == NULL) |
0527bc4e | 2066 | cpp_error (pfile, CPP_DL_ERROR, "#elif without #if"); |
b528a07e | 2067 | else |
40ea76de | 2068 | { |
b528a07e NB |
2069 | if (ifs->type == T_ELSE) |
2070 | { | |
0527bc4e JDA |
2071 | cpp_error (pfile, CPP_DL_ERROR, "#elif after #else"); |
2072 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, | |
b528a07e NB |
2073 | "the conditional began here"); |
2074 | } | |
2075 | ifs->type = T_ELIF; | |
93c80368 | 2076 | |
10ef8f28 MP |
2077 | /* See DR#412: "Only the first group whose control condition |
2078 | evaluates to true (nonzero) is processed; any following groups | |
2079 | are skipped and their controlling directives are processed as | |
2080 | if they were in a group that is skipped." */ | |
2081 | if (ifs->skip_elses) | |
2082 | pfile->state.skipping = 1; | |
2083 | else | |
b528a07e | 2084 | { |
10ef8f28 MP |
2085 | pfile->state.skipping = ! _cpp_parse_expr (pfile, false); |
2086 | ifs->skip_elses = ! pfile->state.skipping; | |
b528a07e | 2087 | } |
cef0d199 NB |
2088 | |
2089 | /* Invalidate any controlling macro. */ | |
2090 | ifs->mi_cmacro = 0; | |
40ea76de | 2091 | } |
7f2935c7 PB |
2092 | } |
2093 | ||
cef0d199 | 2094 | /* #endif pops the if stack and resets pfile->state.skipping. */ |
711b8824 | 2095 | static void |
6cf87ca4 | 2096 | do_endif (cpp_reader *pfile) |
7f2935c7 | 2097 | { |
b528a07e NB |
2098 | cpp_buffer *buffer = pfile->buffer; |
2099 | struct if_stack *ifs = buffer->if_stack; | |
ea4a453b | 2100 | |
ea4a453b | 2101 | if (ifs == NULL) |
0527bc4e | 2102 | cpp_error (pfile, CPP_DL_ERROR, "#endif without #if"); |
7f2935c7 PB |
2103 | else |
2104 | { | |
cef0d199 | 2105 | /* Only check EOL if was not originally skipping. */ |
909de5da | 2106 | if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels)) |
81b5d104 | 2107 | check_eol_endif_labels (pfile); |
cef0d199 | 2108 | |
93c80368 NB |
2109 | /* If potential control macro, we go back outside again. */ |
2110 | if (ifs->next == 0 && ifs->mi_cmacro) | |
2111 | { | |
6d18adbc | 2112 | pfile->mi_valid = true; |
93c80368 NB |
2113 | pfile->mi_cmacro = ifs->mi_cmacro; |
2114 | } | |
2115 | ||
b528a07e | 2116 | buffer->if_stack = ifs->next; |
cef0d199 | 2117 | pfile->state.skipping = ifs->was_skipping; |
2a967f3d | 2118 | obstack_free (&pfile->buffer_ob, ifs); |
7f2935c7 | 2119 | } |
93c80368 | 2120 | } |
041c3194 | 2121 | |
5d8ebbd8 NB |
2122 | /* Push an if_stack entry for a preprocessor conditional, and set |
2123 | pfile->state.skipping to SKIP. If TYPE indicates the conditional | |
2124 | is #if or #ifndef, CMACRO is a potentially controlling macro, and | |
2125 | we need to check here that we are at the top of the file. */ | |
ea4a453b | 2126 | static void |
6cf87ca4 ZW |
2127 | push_conditional (cpp_reader *pfile, int skip, int type, |
2128 | const cpp_hashnode *cmacro) | |
ea4a453b ZW |
2129 | { |
2130 | struct if_stack *ifs; | |
b528a07e | 2131 | cpp_buffer *buffer = pfile->buffer; |
ea4a453b | 2132 | |
72bb2c39 | 2133 | ifs = XOBNEW (&pfile->buffer_ob, struct if_stack); |
50410426 | 2134 | ifs->line = pfile->directive_line; |
b528a07e | 2135 | ifs->next = buffer->if_stack; |
cef0d199 NB |
2136 | ifs->skip_elses = pfile->state.skipping || !skip; |
2137 | ifs->was_skipping = pfile->state.skipping; | |
ea4a453b | 2138 | ifs->type = type; |
6d18adbc NB |
2139 | /* This condition is effectively a test for top-of-file. */ |
2140 | if (pfile->mi_valid && pfile->mi_cmacro == 0) | |
93c80368 NB |
2141 | ifs->mi_cmacro = cmacro; |
2142 | else | |
2143 | ifs->mi_cmacro = 0; | |
ea4a453b | 2144 | |
cef0d199 | 2145 | pfile->state.skipping = skip; |
b528a07e | 2146 | buffer->if_stack = ifs; |
782331f4 | 2147 | } |
7061aa5a | 2148 | |
5d8ebbd8 NB |
2149 | /* Read the tokens of the answer into the macro pool, in a directive |
2150 | of type TYPE. Only commit the memory if we intend it as permanent | |
2151 | storage, i.e. the #assert case. Returns 0 on success, and sets | |
a28fbdba MLI |
2152 | ANSWERP to point to the answer. PRED_LOC is the location of the |
2153 | predicate. */ | |
3fb558b1 | 2154 | static bool |
620e594b | 2155 | parse_answer (cpp_reader *pfile, int type, location_t pred_loc, |
3fb558b1 | 2156 | cpp_macro **answer_ptr) |
7f2935c7 | 2157 | { |
93c80368 NB |
2158 | /* In a conditional, it is legal to not have an open paren. We |
2159 | should save the following token in this case. */ | |
3fb558b1 | 2160 | const cpp_token *paren = cpp_get_token (pfile); |
93c80368 NB |
2161 | |
2162 | /* If not a paren, see if we're OK. */ | |
4ed5bcfb | 2163 | if (paren->type != CPP_OPEN_PAREN) |
041c3194 | 2164 | { |
93c80368 NB |
2165 | /* In a conditional no answer is a test for any answer. It |
2166 | could be followed by any token. */ | |
2167 | if (type == T_IF) | |
bdcbe496 NB |
2168 | { |
2169 | _cpp_backup_tokens (pfile, 1); | |
3fb558b1 | 2170 | return true; |
bdcbe496 | 2171 | } |
93c80368 NB |
2172 | |
2173 | /* #unassert with no answer is valid - it removes all answers. */ | |
4ed5bcfb | 2174 | if (type == T_UNASSERT && paren->type == CPP_EOF) |
3fb558b1 | 2175 | return true; |
15dad1d9 | 2176 | |
a28fbdba MLI |
2177 | cpp_error_with_line (pfile, CPP_DL_ERROR, pred_loc, 0, |
2178 | "missing '(' after predicate"); | |
3fb558b1 | 2179 | return false; |
041c3194 | 2180 | } |
7061aa5a | 2181 | |
3fb558b1 NS |
2182 | cpp_macro *answer = _cpp_new_macro (pfile, cmk_assert, |
2183 | _cpp_reserve_room (pfile, 0, | |
2184 | sizeof (cpp_macro))); | |
2185 | answer->parm.next = NULL; | |
2186 | unsigned count = 0; | |
2187 | for (;;) | |
041c3194 | 2188 | { |
8c3b2693 | 2189 | const cpp_token *token = cpp_get_token (pfile); |
93c80368 | 2190 | |
041c3194 ZW |
2191 | if (token->type == CPP_CLOSE_PAREN) |
2192 | break; | |
a7abcbbf | 2193 | |
93c80368 | 2194 | if (token->type == CPP_EOF) |
041c3194 | 2195 | { |
0527bc4e | 2196 | cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer"); |
3fb558b1 | 2197 | return false; |
041c3194 | 2198 | } |
8c3b2693 | 2199 | |
3fb558b1 NS |
2200 | answer = (cpp_macro *)_cpp_reserve_room |
2201 | (pfile, sizeof (cpp_macro) + count * sizeof (cpp_token), | |
2202 | sizeof (cpp_token)); | |
2203 | answer->exp.tokens[count++] = *token; | |
041c3194 | 2204 | } |
15dad1d9 | 2205 | |
3fb558b1 | 2206 | if (!count) |
7061aa5a | 2207 | { |
0527bc4e | 2208 | cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty"); |
3fb558b1 | 2209 | return false; |
7f2935c7 | 2210 | } |
041c3194 | 2211 | |
3fb558b1 NS |
2212 | /* Drop whitespace at start, for answer equivalence purposes. */ |
2213 | answer->exp.tokens[0].flags &= ~PREV_WHITE; | |
041c3194 | 2214 | |
3fb558b1 NS |
2215 | answer->count = count; |
2216 | *answer_ptr = answer; | |
2217 | ||
2218 | return true; | |
93c80368 | 2219 | } |
041c3194 | 2220 | |
5d8ebbd8 | 2221 | /* Parses an assertion directive of type TYPE, returning a pointer to |
a570d97f NS |
2222 | the hash node of the predicate, or 0 on error. The node is |
2223 | guaranteed to be disjoint from the macro namespace, so can only | |
2224 | have type 'NT_VOID'. If an answer was supplied, it is placed in | |
2225 | *ANSWER_PTR, which is otherwise set to 0. */ | |
93c80368 | 2226 | static cpp_hashnode * |
3fb558b1 | 2227 | parse_assertion (cpp_reader *pfile, int type, cpp_macro **answer_ptr) |
93c80368 NB |
2228 | { |
2229 | cpp_hashnode *result = 0; | |
93c80368 NB |
2230 | |
2231 | /* We don't expand predicates or answers. */ | |
2232 | pfile->state.prevent_expansion++; | |
2233 | ||
3fb558b1 NS |
2234 | *answer_ptr = NULL; |
2235 | ||
2236 | const cpp_token *predicate = cpp_get_token (pfile); | |
4ed5bcfb | 2237 | if (predicate->type == CPP_EOF) |
0527bc4e | 2238 | cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate"); |
4ed5bcfb | 2239 | else if (predicate->type != CPP_NAME) |
a28fbdba MLI |
2240 | cpp_error_with_line (pfile, CPP_DL_ERROR, predicate->src_loc, 0, |
2241 | "predicate must be an identifier"); | |
3fb558b1 | 2242 | else if (parse_answer (pfile, type, predicate->src_loc, answer_ptr)) |
93c80368 | 2243 | { |
9a0c6187 | 2244 | unsigned int len = NODE_LEN (predicate->val.node.node); |
c3f829c1 | 2245 | unsigned char *sym = (unsigned char *) alloca (len + 1); |
041c3194 | 2246 | |
93c80368 NB |
2247 | /* Prefix '#' to get it out of macro namespace. */ |
2248 | sym[0] = '#'; | |
9a0c6187 | 2249 | memcpy (sym + 1, NODE_NAME (predicate->val.node.node), len); |
93c80368 NB |
2250 | result = cpp_lookup (pfile, sym, len + 1); |
2251 | } | |
7061aa5a | 2252 | |
93c80368 | 2253 | pfile->state.prevent_expansion--; |
3fb558b1 | 2254 | |
93c80368 | 2255 | return result; |
7f2935c7 | 2256 | } |
7061aa5a | 2257 | |
5d8ebbd8 | 2258 | /* Returns a pointer to the pointer to CANDIDATE in the answer chain, |
041c3194 | 2259 | or a pointer to NULL if the answer is not in the chain. */ |
3fb558b1 NS |
2260 | static cpp_macro ** |
2261 | find_answer (cpp_hashnode *node, const cpp_macro *candidate) | |
7f2935c7 | 2262 | { |
93c80368 | 2263 | unsigned int i; |
3fb558b1 | 2264 | cpp_macro **result = NULL; |
7f2935c7 | 2265 | |
3fb558b1 | 2266 | for (result = &node->value.answers; *result; result = &(*result)->parm.next) |
93c80368 | 2267 | { |
3fb558b1 | 2268 | cpp_macro *answer = *result; |
93c80368 NB |
2269 | |
2270 | if (answer->count == candidate->count) | |
2271 | { | |
2272 | for (i = 0; i < answer->count; i++) | |
3fb558b1 NS |
2273 | if (!_cpp_equiv_tokens (&answer->exp.tokens[i], |
2274 | &candidate->exp.tokens[i])) | |
93c80368 NB |
2275 | break; |
2276 | ||
2277 | if (i == answer->count) | |
2278 | break; | |
2279 | } | |
2280 | } | |
ff2b53ef | 2281 | |
041c3194 ZW |
2282 | return result; |
2283 | } | |
15dad1d9 | 2284 | |
93c80368 | 2285 | /* Test an assertion within a preprocessor conditional. Returns |
da7d8304 | 2286 | nonzero on failure, zero on success. On success, the result of |
2402645b | 2287 | the test is written into VALUE, otherwise the value 0. */ |
93c80368 | 2288 | int |
6cf87ca4 | 2289 | _cpp_test_assertion (cpp_reader *pfile, unsigned int *value) |
93c80368 | 2290 | { |
3fb558b1 NS |
2291 | cpp_macro *answer; |
2292 | cpp_hashnode *node = parse_assertion (pfile, T_IF, &answer); | |
2402645b HPN |
2293 | |
2294 | /* For recovery, an erroneous assertion expression is handled as a | |
2295 | failing assertion. */ | |
2296 | *value = 0; | |
2297 | ||
93c80368 | 2298 | if (node) |
3fb558b1 | 2299 | { |
a570d97f | 2300 | if (node->value.answers) |
3fb558b1 NS |
2301 | *value = !answer || *find_answer (node, answer); |
2302 | } | |
91318908 NB |
2303 | else if (pfile->cur_token[-1].type == CPP_EOF) |
2304 | _cpp_backup_tokens (pfile, 1); | |
93c80368 NB |
2305 | |
2306 | /* We don't commit the memory for the answer - it's temporary only. */ | |
2307 | return node == 0; | |
2308 | } | |
2309 | ||
5d8ebbd8 | 2310 | /* Handle #assert. */ |
711b8824 | 2311 | static void |
6cf87ca4 | 2312 | do_assert (cpp_reader *pfile) |
041c3194 | 2313 | { |
3fb558b1 NS |
2314 | cpp_macro *answer; |
2315 | cpp_hashnode *node = parse_assertion (pfile, T_ASSERT, &answer); | |
df383483 | 2316 | |
041c3194 | 2317 | if (node) |
ff2b53ef | 2318 | { |
93c80368 NB |
2319 | /* Place the new answer in the answer list. First check there |
2320 | is not a duplicate. */ | |
a570d97f | 2321 | if (*find_answer (node, answer)) |
041c3194 | 2322 | { |
3fb558b1 NS |
2323 | cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted", |
2324 | NODE_NAME (node) + 1); | |
2325 | return; | |
041c3194 | 2326 | } |
8c3b2693 | 2327 | |
3fb558b1 NS |
2328 | /* Commit or allocate storage for the answer. */ |
2329 | answer = (cpp_macro *)_cpp_commit_buff | |
2330 | (pfile, sizeof (cpp_macro) - sizeof (cpp_token) | |
2331 | + sizeof (cpp_token) * answer->count); | |
2332 | ||
a570d97f NS |
2333 | /* Chain into the list. */ |
2334 | answer->parm.next = node->value.answers; | |
3fb558b1 NS |
2335 | node->value.answers = answer; |
2336 | ||
a5cb563b | 2337 | check_eol (pfile, false); |
ff2b53ef | 2338 | } |
041c3194 | 2339 | } |
15dad1d9 | 2340 | |
5d8ebbd8 | 2341 | /* Handle #unassert. */ |
711b8824 | 2342 | static void |
6cf87ca4 | 2343 | do_unassert (cpp_reader *pfile) |
041c3194 | 2344 | { |
3fb558b1 NS |
2345 | cpp_macro *answer; |
2346 | cpp_hashnode *node = parse_assertion (pfile, T_UNASSERT, &answer); | |
df383483 | 2347 | |
93c80368 | 2348 | /* It isn't an error to #unassert something that isn't asserted. */ |
a570d97f | 2349 | if (node) |
7061aa5a | 2350 | { |
93c80368 | 2351 | if (answer) |
15dad1d9 | 2352 | { |
3fb558b1 | 2353 | cpp_macro **p = find_answer (node, answer); |
a7abcbbf | 2354 | |
3fb558b1 NS |
2355 | /* Remove the assert from the list. */ |
2356 | if (cpp_macro *temp = *p) | |
a570d97f | 2357 | *p = temp->parm.next; |
8c3b2693 | 2358 | |
a5cb563b | 2359 | check_eol (pfile, false); |
93c80368 NB |
2360 | } |
2361 | else | |
2362 | _cpp_free_definition (node); | |
041c3194 | 2363 | } |
93c80368 NB |
2364 | |
2365 | /* We don't commit the memory for the answer - it's temporary only. */ | |
7f2935c7 | 2366 | } |
7f2935c7 | 2367 | |
45b966db ZW |
2368 | /* These are for -D, -U, -A. */ |
2369 | ||
2370 | /* Process the string STR as if it appeared as the body of a #define. | |
2371 | If STR is just an identifier, define it with value 1. | |
2372 | If STR has anything after the identifier, then it should | |
ec5c56db | 2373 | be identifier=definition. */ |
0b22d65c | 2374 | void |
6cf87ca4 | 2375 | cpp_define (cpp_reader *pfile, const char *str) |
0b22d65c | 2376 | { |
86373e7e JM |
2377 | char *buf; |
2378 | const char *p; | |
45b966db ZW |
2379 | size_t count; |
2380 | ||
df383483 | 2381 | /* Copy the entire option so we can modify it. |
45b966db | 2382 | Change the first "=" in the string to a space. If there is none, |
86368122 NB |
2383 | tack " 1" on the end. */ |
2384 | ||
86368122 | 2385 | count = strlen (str); |
c3f829c1 | 2386 | buf = (char *) alloca (count + 3); |
86368122 NB |
2387 | memcpy (buf, str, count); |
2388 | ||
2389 | p = strchr (str, '='); | |
45b966db | 2390 | if (p) |
86368122 | 2391 | buf[p - str] = ' '; |
45b966db ZW |
2392 | else |
2393 | { | |
86368122 NB |
2394 | buf[count++] = ' '; |
2395 | buf[count++] = '1'; | |
0b22d65c | 2396 | } |
26aea073 | 2397 | buf[count] = '\n'; |
cf4ed945 | 2398 | |
29401c30 | 2399 | run_directive (pfile, T_DEFINE, buf, count); |
2c8f0515 ZW |
2400 | } |
2401 | ||
28f68625 DF |
2402 | |
2403 | /* Use to build macros to be run through cpp_define() as | |
2404 | described above. | |
2405 | Example: cpp_define_formatted (pfile, "MACRO=%d", value); */ | |
2406 | ||
2407 | void | |
2408 | cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...) | |
2409 | { | |
01ca36af | 2410 | char *ptr; |
28f68625 DF |
2411 | |
2412 | va_list ap; | |
2413 | va_start (ap, fmt); | |
01ca36af | 2414 | ptr = xvasprintf (fmt, ap); |
28f68625 DF |
2415 | va_end (ap); |
2416 | ||
2417 | cpp_define (pfile, ptr); | |
2418 | free (ptr); | |
2419 | } | |
2420 | ||
2421 | ||
ad2a084d | 2422 | /* Slight variant of the above for use by initialize_builtins. */ |
2c8f0515 | 2423 | void |
6cf87ca4 | 2424 | _cpp_define_builtin (cpp_reader *pfile, const char *str) |
2c8f0515 | 2425 | { |
26aea073 | 2426 | size_t len = strlen (str); |
c3f829c1 | 2427 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2428 | memcpy (buf, str, len); |
2429 | buf[len] = '\n'; | |
2430 | run_directive (pfile, T_DEFINE, buf, len); | |
45b966db | 2431 | } |
0f41302f | 2432 | |
45b966db ZW |
2433 | /* Process MACRO as if it appeared as the body of an #undef. */ |
2434 | void | |
6cf87ca4 | 2435 | cpp_undef (cpp_reader *pfile, const char *macro) |
7f2935c7 | 2436 | { |
26aea073 | 2437 | size_t len = strlen (macro); |
c3f829c1 | 2438 | char *buf = (char *) alloca (len + 1); |
26aea073 NB |
2439 | memcpy (buf, macro, len); |
2440 | buf[len] = '\n'; | |
2441 | run_directive (pfile, T_UNDEF, buf, len); | |
7f2935c7 PB |
2442 | } |
2443 | ||
d6874138 KT |
2444 | /* Replace a previous definition DEF of the macro STR. If DEF is NULL, |
2445 | or first element is zero, then the macro should be undefined. */ | |
2446 | static void | |
2447 | cpp_pop_definition (cpp_reader *pfile, struct def_pragma_macro *c) | |
121de39f | 2448 | { |
d6874138 | 2449 | cpp_hashnode *node = _cpp_lex_identifier (pfile, c->name); |
121de39f RH |
2450 | if (node == NULL) |
2451 | return; | |
2452 | ||
93d45d9e JM |
2453 | if (pfile->cb.before_define) |
2454 | pfile->cb.before_define (pfile); | |
2455 | ||
3f6677f4 | 2456 | if (cpp_macro_p (node)) |
121de39f RH |
2457 | { |
2458 | if (pfile->cb.undef) | |
2459 | pfile->cb.undef (pfile, pfile->directive_line, node); | |
2460 | if (CPP_OPTION (pfile, warn_unused_macros)) | |
2461 | _cpp_warn_if_unused_macro (pfile, node, NULL); | |
3f6677f4 | 2462 | _cpp_free_definition (node); |
121de39f | 2463 | } |
121de39f | 2464 | |
d6874138 KT |
2465 | if (c->is_undef) |
2466 | return; | |
aa23e73b JJ |
2467 | if (c->is_builtin) |
2468 | { | |
2469 | _cpp_restore_special_builtin (pfile, c); | |
2470 | return; | |
2471 | } | |
3f6677f4 | 2472 | |
d6874138 KT |
2473 | { |
2474 | size_t namelen; | |
2475 | const uchar *dn; | |
2476 | cpp_hashnode *h = NULL; | |
2477 | cpp_buffer *nbuf; | |
2478 | ||
2479 | namelen = ustrcspn (c->definition, "( \n"); | |
2480 | h = cpp_lookup (pfile, c->definition, namelen); | |
2481 | dn = c->definition + namelen; | |
2482 | ||
d6874138 KT |
2483 | nbuf = cpp_push_buffer (pfile, dn, ustrchr (dn, '\n') - dn, true); |
2484 | if (nbuf != NULL) | |
2485 | { | |
2486 | _cpp_clean_line (pfile); | |
2487 | nbuf->sysp = 1; | |
2488 | if (!_cpp_create_definition (pfile, h)) | |
2489 | abort (); | |
2490 | _cpp_pop_buffer (pfile); | |
2491 | } | |
2492 | else | |
2493 | abort (); | |
2494 | h->value.macro->line = c->line; | |
2495 | h->value.macro->syshdr = c->syshdr; | |
2496 | h->value.macro->used = c->used; | |
2497 | } | |
121de39f RH |
2498 | } |
2499 | ||
ec5c56db | 2500 | /* Process the string STR as if it appeared as the body of a #assert. */ |
45b966db | 2501 | void |
6cf87ca4 | 2502 | cpp_assert (cpp_reader *pfile, const char *str) |
45b966db | 2503 | { |
86368122 | 2504 | handle_assertion (pfile, str, T_ASSERT); |
45b966db | 2505 | } |
7f2935c7 | 2506 | |
ec5c56db | 2507 | /* Process STR as if it appeared as the body of an #unassert. */ |
45b966db | 2508 | void |
6cf87ca4 | 2509 | cpp_unassert (cpp_reader *pfile, const char *str) |
7f2935c7 | 2510 | { |
86368122 | 2511 | handle_assertion (pfile, str, T_UNASSERT); |
df383483 | 2512 | } |
3fdc651f | 2513 | |
86368122 NB |
2514 | /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */ |
2515 | static void | |
6cf87ca4 | 2516 | handle_assertion (cpp_reader *pfile, const char *str, int type) |
86368122 NB |
2517 | { |
2518 | size_t count = strlen (str); | |
2519 | const char *p = strchr (str, '='); | |
2520 | ||
26aea073 NB |
2521 | /* Copy the entire option so we can modify it. Change the first |
2522 | "=" in the string to a '(', and tack a ')' on the end. */ | |
c3f829c1 | 2523 | char *buf = (char *) alloca (count + 2); |
26aea073 NB |
2524 | |
2525 | memcpy (buf, str, count); | |
86368122 NB |
2526 | if (p) |
2527 | { | |
86368122 NB |
2528 | buf[p - str] = '('; |
2529 | buf[count++] = ')'; | |
86368122 | 2530 | } |
26aea073 NB |
2531 | buf[count] = '\n'; |
2532 | str = buf; | |
86368122 | 2533 | |
29401c30 | 2534 | run_directive (pfile, type, str, count); |
86368122 NB |
2535 | } |
2536 | ||
7e96d768 NB |
2537 | /* The options structure. */ |
2538 | cpp_options * | |
6cf87ca4 | 2539 | cpp_get_options (cpp_reader *pfile) |
7e96d768 NB |
2540 | { |
2541 | return &pfile->opts; | |
2542 | } | |
2543 | ||
2544 | /* The callbacks structure. */ | |
2545 | cpp_callbacks * | |
6cf87ca4 | 2546 | cpp_get_callbacks (cpp_reader *pfile) |
7e96d768 NB |
2547 | { |
2548 | return &pfile->cb; | |
2549 | } | |
2550 | ||
2551 | /* Copy the given callbacks structure to our own. */ | |
2552 | void | |
6cf87ca4 | 2553 | cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb) |
7e96d768 NB |
2554 | { |
2555 | pfile->cb = *cb; | |
2556 | } | |
2557 | ||
c6e83800 | 2558 | /* The dependencies structure. (Creates one if it hasn't already been.) */ |
99b1c316 | 2559 | class mkdeps * |
c6e83800 ZW |
2560 | cpp_get_deps (cpp_reader *pfile) |
2561 | { | |
2562 | if (!pfile->deps) | |
2563 | pfile->deps = deps_init (); | |
2564 | return pfile->deps; | |
2565 | } | |
2566 | ||
eb1f4d9d NB |
2567 | /* Push a new buffer on the buffer stack. Returns the new buffer; it |
2568 | doesn't fail. It does not generate a file change call back; that | |
2569 | is the responsibility of the caller. */ | |
c71f835b | 2570 | cpp_buffer * |
6cf87ca4 | 2571 | cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len, |
40de9f76 | 2572 | int from_stage3) |
c71f835b | 2573 | { |
c3f829c1 | 2574 | cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer); |
93c80368 | 2575 | |
fde84349 | 2576 | /* Clears, amongst other things, if_stack and mi_cmacro. */ |
c3f829c1 | 2577 | memset (new_buffer, 0, sizeof (cpp_buffer)); |
fde84349 | 2578 | |
c3f829c1 GDR |
2579 | new_buffer->next_line = new_buffer->buf = buffer; |
2580 | new_buffer->rlimit = buffer + len; | |
2581 | new_buffer->from_stage3 = from_stage3; | |
2582 | new_buffer->prev = pfile->buffer; | |
2583 | new_buffer->need_line = true; | |
0bda4760 | 2584 | |
c3f829c1 | 2585 | pfile->buffer = new_buffer; |
cf551fba | 2586 | |
c3f829c1 | 2587 | return new_buffer; |
c71f835b ZW |
2588 | } |
2589 | ||
af0d16cd NB |
2590 | /* Pops a single buffer, with a file change call-back if appropriate. |
2591 | Then pushes the next -include file, if any remain. */ | |
ef6e958a | 2592 | void |
6cf87ca4 | 2593 | _cpp_pop_buffer (cpp_reader *pfile) |
c71f835b | 2594 | { |
fde84349 | 2595 | cpp_buffer *buffer = pfile->buffer; |
8f9b4009 | 2596 | struct _cpp_file *inc = buffer->file; |
ad2a084d | 2597 | struct if_stack *ifs; |
28937f11 | 2598 | const unsigned char *to_free; |
c71f835b | 2599 | |
fde84349 NB |
2600 | /* Walk back up the conditional stack till we reach its level at |
2601 | entry to this file, issuing error messages. */ | |
2602 | for (ifs = buffer->if_stack; ifs; ifs = ifs->next) | |
0527bc4e | 2603 | cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0, |
fde84349 | 2604 | "unterminated #%s", dtable[ifs->type].name); |
eb1f4d9d | 2605 | |
97293897 | 2606 | /* In case of a missing #endif. */ |
67821e3a | 2607 | pfile->state.skipping = 0; |
29401c30 | 2608 | |
af0d16cd | 2609 | /* _cpp_do_file_change expects pfile->buffer to be the new one. */ |
29401c30 NB |
2610 | pfile->buffer = buffer->prev; |
2611 | ||
28937f11 | 2612 | to_free = buffer->to_free; |
26aea073 NB |
2613 | free (buffer->notes); |
2614 | ||
af0d16cd NB |
2615 | /* Free the buffer object now; we may want to push a new buffer |
2616 | in _cpp_push_next_include_file. */ | |
2617 | obstack_free (&pfile->buffer_ob, buffer); | |
29401c30 | 2618 | |
af0d16cd NB |
2619 | if (inc) |
2620 | { | |
28937f11 | 2621 | _cpp_pop_file_buffer (pfile, inc, to_free); |
af0d16cd | 2622 | |
40de9f76 | 2623 | _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0); |
af0d16cd | 2624 | } |
705b0c05 NS |
2625 | else if (to_free) |
2626 | free ((void *)to_free); | |
c71f835b ZW |
2627 | } |
2628 | ||
05713b80 | 2629 | /* Enter all recognized directives in the hash table. */ |
c71f835b | 2630 | void |
6cf87ca4 | 2631 | _cpp_init_directives (cpp_reader *pfile) |
c71f835b | 2632 | { |
ad1a3914 | 2633 | for (int i = 0; i < N_DIRECTIVES; i++) |
93c80368 | 2634 | { |
ad1a3914 | 2635 | cpp_hashnode *node = cpp_lookup (pfile, dtable[i].name, dtable[i].length); |
4977bab6 ZW |
2636 | node->is_directive = 1; |
2637 | node->directive_index = i; | |
93c80368 | 2638 | } |
c71f835b | 2639 | } |
a15f7cb8 ESR |
2640 | |
2641 | /* Extract header file from a bracket include. Parsing starts after '<'. | |
2642 | The string is malloced and must be freed by the caller. */ | |
2643 | char * | |
2644 | _cpp_bracket_include(cpp_reader *pfile) | |
2645 | { | |
2646 | return glue_header_name (pfile); | |
2647 | } | |
2648 |