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