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