]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cpplib.c
* config/avr/avr.c (avr_asm_only_p): New variable.
[thirdparty/gcc.git] / gcc / cpplib.c
CommitLineData
e14c5993 1/* CPP Library. (Directive handling.)
00059bc7 2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
e484a1cc 3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
1f99c786 4 Contributed by Per Bothner, 1994-95.
0cb8d9ae 5 Based on CCCP program by Paul Rubin, June 1986
1d6fa33f 6 Adapted to ANSI C, Richard Stallman, Jan 1987
7
8This program is free software; you can redistribute it and/or modify it
9under the terms of the GNU General Public License as published by the
10Free Software Foundation; either version 2, or (at your option) any
11later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
ad87de1e 20Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1d6fa33f 21
ad87de1e 22#include "config.h"
1486870d 23#include "system.h"
1d6fa33f 24
ad87de1e 25#include "cpplib.h"
26#include "cpphash.h"
f51c2148 27#include "obstack.h"
1d6fa33f 28
79bd622b 29/* Chained list of answers to an assertion. */
30struct answer
31{
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
35};
36
1e8b9746 37/* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
1e8b9746 39struct if_stack
40{
41 struct if_stack *next;
729d2022 42 unsigned int line; /* Line where condition started. */
79bd622b 43 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
5e878517 44 bool skip_elses; /* Can future #else / #elif be skipped? */
45 bool was_skipping; /* If were skipping on entry. */
46 int type; /* Most recent conditional, for diagnostics. */
1e8b9746 47};
1e8b9746 48
c6f14ce5 49/* Contains a registered pragma or pragma namespace. */
50typedef void (*pragma_cb) PARAMS ((cpp_reader *));
51struct pragma_entry
52{
53 struct pragma_entry *next;
5c43d212 54 const cpp_hashnode *pragma; /* Name and length. */
c6f14ce5 55 int is_nspace;
56 union {
57 pragma_cb handler;
58 struct pragma_entry *space;
59 } u;
60};
61
79bd622b 62/* Values for the origin field of struct directive. KANDR directives
63 come from traditional (K&R) C. STDC89 directives come from the
64 1989 C standard. EXTENSION directives are extensions. */
65#define KANDR 0
66#define STDC89 1
67#define EXTENSION 2
68
69/* Values for the flags field of struct directive. COND indicates a
70 conditional; IF_COND an opening conditional. INCL means to treat
71 "..." and <...> as q-char and h-char sequences respectively. IN_I
72 means this directive should be handled even if -fpreprocessed is in
73 effect (these are the directives with callback hooks). */
74#define COND (1 << 0)
75#define IF_COND (1 << 1)
76#define INCL (1 << 2)
77#define IN_I (1 << 3)
78
79/* Defines one #-directive, including how to handle it. */
80typedef void (*directive_handler) PARAMS ((cpp_reader *));
81typedef struct directive directive;
82struct directive
83{
84 directive_handler handler; /* Function to handle directive. */
b6d18b0a 85 const uchar *name; /* Name of directive. */
79bd622b 86 unsigned short length; /* Length of name. */
87 unsigned char origin; /* Origin of directive. */
88 unsigned char flags; /* Flags describing this directive. */
89};
90
08c74ec4 91/* Forward declarations. */
92
79bd622b 93static void skip_rest_of_line PARAMS ((cpp_reader *));
94static void check_eol PARAMS ((cpp_reader *));
9677c711 95static void start_directive PARAMS ((cpp_reader *));
96static void end_directive PARAMS ((cpp_reader *, int));
d0d481d7 97static void directive_diagnostics
98 PARAMS ((cpp_reader *, const directive *, int));
79bd622b 99static void run_directive PARAMS ((cpp_reader *, int,
6cee4464 100 const char *, size_t));
f9b5f742 101static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
102static const cpp_token *parse_include PARAMS ((cpp_reader *));
f80e83a9 103static void push_conditional PARAMS ((cpp_reader *, int, int,
104 const cpp_hashnode *));
53c052ca 105static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
b6d18b0a 106static uchar *dequote_string PARAMS ((cpp_reader *, const uchar *,
25266990 107 unsigned int));
b6d18b0a 108static int strtoul_for_line PARAMS ((const uchar *, unsigned int,
f80e83a9 109 unsigned long *));
73328dce 110static void do_diagnostic PARAMS ((cpp_reader *, int, int));
920b5d41 111static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
0d6d8dc0 112static void do_include_common PARAMS ((cpp_reader *, enum include_type));
c6f14ce5 113static struct pragma_entry *lookup_pragma_entry
5c43d212 114 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
c6f14ce5 115static struct pragma_entry *insert_pragma_entry
5c43d212 116 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
117 pragma_cb));
79bd622b 118static void do_pragma_once PARAMS ((cpp_reader *));
119static void do_pragma_poison PARAMS ((cpp_reader *));
120static void do_pragma_system_header PARAMS ((cpp_reader *));
121static void do_pragma_dependency PARAMS ((cpp_reader *));
25266990 122static void do_linemarker PARAMS ((cpp_reader *));
f9b5f742 123static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
124static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
1e0ef2fd 125static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
79bd622b 126static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
127static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
128 int));
129static struct answer ** find_answer PARAMS ((cpp_hashnode *,
130 const struct answer *));
90dc47e0 131static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
a4b4a940 132
4bf559f2 133/* This is the table of directive handlers. It is ordered by
134 frequency of occurrence; the numbers at the end are directive
135 counts from all the source code I have lying around (egcs and libc
136 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
79bd622b 137 pcmcia-cs-3.0.9). This is no longer important as directive lookup
138 is now O(1). All extensions other than #warning and #include_next
139 are deprecated. The name is where the extension appears to have
140 come from. */
4bf559f2 141
79bd622b 142#define DIRECTIVE_TABLE \
143D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
145D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
148D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
25266990 151D(line, T_LINE, KANDR, 0) /* 2465 */ \
81ada207 152D(elif, T_ELIF, STDC89, COND) /* 610 */ \
79bd622b 153D(error, T_ERROR, STDC89, 0) /* 475 */ \
154D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
157D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
159D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161SCCS_ENTRY /* 0 SVR4? */
4bf559f2 162
ef33b55c 163/* #sccs is not always recognized. */
164#ifdef SCCS_DIRECTIVE
f80e83a9 165# define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
ef33b55c 166#else
167# define SCCS_ENTRY /* nothing */
168#endif
169
4bf559f2 170/* Use the table to generate a series of prototypes, an enum for the
171 directive names, and an array of directive handlers. */
172
2c0e001b 173/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
69461e0d 174#define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
4bf559f2 175DIRECTIVE_TABLE
176#undef D
177
f80e83a9 178#define D(n, tag, o, f) tag,
4bf559f2 179enum
180{
181 DIRECTIVE_TABLE
182 N_DIRECTIVES
1d6fa33f 183};
4bf559f2 184#undef D
185
2c0e001b 186/* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
f80e83a9 187#define D(name, t, origin, flags) \
b6d18b0a 188{ CONCAT2(do_,name), (const uchar *) STRINGX(name), \
f80e83a9 189 sizeof STRINGX(name) - 1, origin, flags },
79bd622b 190static const directive dtable[] =
4bf559f2 191{
192DIRECTIVE_TABLE
193};
194#undef D
195#undef DIRECTIVE_TABLE
1d6fa33f 196
25266990 197/* Wrapper struct directive for linemarkers.
198 The origin is more or less true - the original K+R cpp
199 did use this notation in its preprocessed output. */
200static const directive linemarker_dir =
201{
202 do_linemarker, U"#", 1, KANDR, IN_I
203};
204
fb5ab82c 205#define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
1ea7ed21 206
79bd622b 207/* Skip any remaining tokens in a directive. */
208static void
209skip_rest_of_line (pfile)
f80e83a9 210 cpp_reader *pfile;
6060326b 211{
79bd622b 212 /* Discard all stacked contexts. */
213 while (pfile->context != &pfile->base_context)
214 _cpp_pop_context (pfile);
215
920b5d41 216 /* Sweep up all tokens remaining on the line. */
c00e481c 217 if (! SEEN_EOL ())
218 while (_cpp_lex_token (pfile)->type != CPP_EOF)
219 ;
79bd622b 220}
6060326b 221
79bd622b 222/* Ensure there are no stray tokens at the end of a directive. */
223static void
224check_eol (pfile)
225 cpp_reader *pfile;
226{
c00e481c 227 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
73328dce 228 cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
229 pfile->directive->name);
79bd622b 230}
231
9677c711 232/* Called when entering a directive, _Pragma or command-line directive. */
233static void
234start_directive (pfile)
79bd622b 235 cpp_reader *pfile;
79bd622b 236{
343fd982 237 /* Setup in-directive state. */
238 pfile->state.in_directive = 1;
239 pfile->state.save_comments = 0;
240
79bd622b 241 /* Some handlers need the position of the # for diagnostics. */
f8d396cd 242 pfile->directive_line = pfile->line;
9677c711 243}
244
245/* Called when leaving a directive, _Pragma or command-line directive. */
246static void
247end_directive (pfile, skip_line)
248 cpp_reader *pfile;
249 int skip_line;
250{
9677c711 251 /* We don't skip for an assembler #. */
252 if (skip_line)
1ea7ed21 253 {
254 skip_rest_of_line (pfile);
fb5ab82c 255 if (!pfile->keep_tokens)
256 {
257 pfile->cur_run = &pfile->base_run;
258 pfile->cur_token = pfile->base_run.base;
259 }
1ea7ed21 260 }
9677c711 261
262 /* Restore state. */
9677c711 263 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
264 pfile->state.in_directive = 0;
265 pfile->state.angled_headers = 0;
266 pfile->directive = 0;
267}
268
d0d481d7 269/* Output diagnostics for a directive DIR. INDENTED is non-zero if
270 the '#' was indented. */
d0d481d7 271static void
272directive_diagnostics (pfile, dir, indented)
273 cpp_reader *pfile;
274 const directive *dir;
275 int indented;
276{
25266990 277 /* Issue -pedantic warnings for extensions. */
278 if (CPP_PEDANTIC (pfile)
279 && ! pfile->state.skipping
280 && dir->origin == EXTENSION)
73328dce 281 cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
25266990 282
283 /* Traditionally, a directive is ignored unless its # is in
284 column 1. Therefore in code intended to work with K+R
285 compilers, directives added by C89 must have their #
286 indented, and directives present in traditional C must not.
287 This is true even of directives in skipped conditional
288 blocks. #elif cannot be used at all. */
289 if (CPP_WTRADITIONAL (pfile))
d0d481d7 290 {
25266990 291 if (dir == &dtable[T_ELIF])
73328dce 292 cpp_error (pfile, DL_WARNING,
293 "suggest not using #elif in traditional C");
25266990 294 else if (indented && dir->origin == KANDR)
73328dce 295 cpp_error (pfile, DL_WARNING,
296 "traditional C ignores #%s with the # indented",
297 dir->name);
25266990 298 else if (!indented && dir->origin != KANDR)
73328dce 299 cpp_error (pfile, DL_WARNING,
300 "suggest hiding #%s from traditional C with an indented #",
301 dir->name);
d0d481d7 302 }
303}
304
305/* Check if we have a known directive. INDENTED is non-zero if the
306 '#' of the directive was indented. This function is in this file
307 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
308 non-zero if the line of tokens has been handled, zero if we should
309 continue processing the line. */
9677c711 310int
311_cpp_handle_directive (pfile, indented)
312 cpp_reader *pfile;
313 int indented;
314{
9677c711 315 const directive *dir = 0;
c00e481c 316 const cpp_token *dname;
d6af0368 317 bool was_parsing_args = pfile->state.parsing_args;
9677c711 318 int skip = 1;
319
d6af0368 320 if (was_parsing_args)
321 {
322 if (CPP_OPTION (pfile, pedantic))
73328dce 323 cpp_error (pfile, DL_PEDWARN,
d6af0368 324 "embedding a directive within macro arguments is not portable");
325 pfile->state.parsing_args = 0;
326 pfile->state.prevent_expansion = 0;
327 }
9677c711 328 start_directive (pfile);
c00e481c 329 dname = _cpp_lex_token (pfile);
338fa5f7 330
c00e481c 331 if (dname->type == CPP_NAME)
338fa5f7 332 {
c00e481c 333 if (dname->val.node->directive_index)
334 dir = &dtable[dname->val.node->directive_index - 1];
338fa5f7 335 }
d0d481d7 336 /* We do not recognise the # followed by a number extension in
337 assembler code. */
c00e481c 338 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
338fa5f7 339 {
25266990 340 dir = &linemarker_dir;
341 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
342 && ! pfile->state.skipping)
73328dce 343 cpp_error (pfile, DL_PEDWARN,
344 "style of line directive is a GCC extension");
79bd622b 345 }
338fa5f7 346
79bd622b 347 if (dir)
348 {
d0d481d7 349 /* If we have a directive that is not an opening conditional,
350 invalidate any control macro. */
351 if (! (dir->flags & IF_COND))
352 pfile->mi_valid = false;
353
354 /* Kluge alert. In order to be sure that code like this
355
356 #define HASH #
357 HASH define foo bar
358
359 does not cause '#define foo bar' to get executed when
360 compiled with -save-temps, we recognize directives in
361 -fpreprocessed mode only if the # is in column 1. cppmacro.c
3fb1e43b 362 puts a space in front of any '#' at the start of a macro. */
d0d481d7 363 if (CPP_OPTION (pfile, preprocessed)
364 && (indented || !(dir->flags & IN_I)))
946ce1b7 365 {
d0d481d7 366 skip = 0;
367 dir = 0;
946ce1b7 368 }
369 else
79bd622b 370 {
d0d481d7 371 /* In failed conditional groups, all non-conditional
372 directives are ignored. Before doing that, whether
373 skipping or not, we should lex angle-bracketed headers
374 correctly, and maybe output some diagnostics. */
375 pfile->state.angled_headers = dir->flags & INCL;
376 if (! CPP_OPTION (pfile, preprocessed))
377 directive_diagnostics (pfile, dir, indented);
378 if (pfile->state.skipping && !(dir->flags & COND))
379 dir = 0;
79bd622b 380 }
381 }
c00e481c 382 else if (dname->type == CPP_EOF)
d0d481d7 383 ; /* CPP_EOF is the "null directive". */
384 else
79bd622b 385 {
386 /* An unknown directive. Don't complain about it in assembly
387 source: we don't know where the comments are, and # may
388 introduce assembler pseudo-ops. Don't complain about invalid
389 directives in skipped conditional groups (6.10 p4). */
5db5d057 390 if (CPP_OPTION (pfile, lang) == CLK_ASM)
d0d481d7 391 skip = 0;
392 else if (!pfile->state.skipping)
73328dce 393 cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
c00e481c 394 cpp_token_as_text (pfile, dname));
338fa5f7 395 }
396
d0d481d7 397 if (dir)
398 {
d3f7919d 399 /* If we are processing a `#define' directive and we have been
400 requested to expand comments into macros, then re-enable
401 saving of comments. */
402 if (dir == &dtable[T_DEFINE])
b1a9ff83 403 pfile->state.save_comments =
404 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
d3f7919d 405
d0d481d7 406 pfile->directive = dir;
407 (*pfile->directive->handler) (pfile);
408 }
409 else if (skip == 0)
410 _cpp_backup_tokens (pfile, 1);
411
412 end_directive (pfile, skip);
d6af0368 413 if (was_parsing_args)
414 {
415 /* Restore state when within macro args. */
416 pfile->state.parsing_args = 2;
417 pfile->state.prevent_expansion = 1;
418 pfile->buffer->saved_flags |= PREV_WHITE;
419 }
9677c711 420 return skip;
f80e83a9 421}
1d6fa33f 422
79bd622b 423/* Directive handler wrapper used by the command line option
424 processor. */
425static void
c808d026 426run_directive (pfile, dir_no, buf, count)
1d6fa33f 427 cpp_reader *pfile;
79bd622b 428 int dir_no;
429 const char *buf;
430 size_t count;
e3630c4b 431{
b6d18b0a 432 cpp_push_buffer (pfile, (const uchar *) buf, count,
c808d026 433 /* from_stage3 */ true, 1);
6cee4464 434 start_directive (pfile);
d0d481d7 435 /* We don't want a leading # to be interpreted as a directive. */
436 pfile->buffer->saved_flags = 0;
7b5cc295 437 pfile->directive = &dtable[dir_no];
438 (void) (*pfile->directive->handler) (pfile);
6cee4464 439 end_directive (pfile, 1);
4dfe8b74 440 _cpp_pop_buffer (pfile);
79bd622b 441}
442
443/* Checks for validity the macro name in #define, #undef, #ifdef and
444 #ifndef directives. */
f80e83a9 445static cpp_hashnode *
79bd622b 446lex_macro_node (pfile)
f80e83a9 447 cpp_reader *pfile;
448{
f17c0828 449 cpp_hashnode *node;
c00e481c 450 const cpp_token *token = _cpp_lex_token (pfile);
c4357c92 451
31674461 452 /* The token immediately after #define must be an identifier. That
f17c0828 453 identifier may not be "defined", per C99 6.10.8p4.
454 In C++, it may not be any of the "named operators" either,
455 per C++98 [lex.digraph], [lex.key].
456 Finally, the identifier may not have been poisoned. (In that case
d3f7919d 457 the lexer has issued the error message for us.)
458
459 Note that if we're copying comments into macro expansions, we
460 could encounter comment tokens here, so eat them all up first. */
461
462 if (! CPP_OPTION (pfile, discard_comments_in_macro_exp))
463 {
464 while (token->type == CPP_COMMENT)
465 token = _cpp_lex_token (pfile);
466 }
31674461 467
c00e481c 468 if (token->type != CPP_NAME)
1d6fa33f 469 {
c00e481c 470 if (token->type == CPP_EOF)
73328dce 471 cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
79bd622b 472 pfile->directive->name);
c00e481c 473 else if (token->flags & NAMED_OP)
73328dce 474 cpp_error (pfile, DL_ERROR,
f17c0828 475 "\"%s\" cannot be used as a macro name as it is an operator in C++",
c00e481c 476 NODE_NAME (token->val.node));
31674461 477 else
73328dce 478 cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
f17c0828 479
480 return 0;
71aa9da4 481 }
f17c0828 482
c00e481c 483 node = token->val.node;
f17c0828 484 if (node->flags & NODE_POISONED)
485 return 0;
486
487 if (node == pfile->spec_nodes.n_defined)
ef33b55c 488 {
73328dce 489 cpp_error (pfile, DL_ERROR, "\"%s\" cannot be used as a macro name",
f17c0828 490 NODE_NAME (node));
491 return 0;
ef33b55c 492 }
493
f17c0828 494 return node;
1d6fa33f 495}
1d6fa33f 496
79bd622b 497/* Process a #define directive. Most work is done in cppmacro.c. */
69461e0d 498static void
4bf559f2 499do_define (pfile)
1d6fa33f 500 cpp_reader *pfile;
1d6fa33f 501{
79bd622b 502 cpp_hashnode *node = lex_macro_node (pfile);
c030ed73 503
79bd622b 504 if (node)
505 {
79bd622b 506 if (_cpp_create_definition (pfile, node))
507 if (pfile->cb.define)
f8d396cd 508 (*pfile->cb.define) (pfile, pfile->directive_line, node);
79bd622b 509 }
f80e83a9 510}
511
e484a1cc 512/* Handle #undef. Mark the identifier NT_VOID in the hash table. */
69461e0d 513static void
f80e83a9 514do_undef (pfile)
515 cpp_reader *pfile;
516{
b1a9ff83 517 cpp_hashnode *node = lex_macro_node (pfile);
2c63d6c8 518
f80e83a9 519 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
520 is not currently defined as a macro name. */
79bd622b 521 if (node && node->type == NT_MACRO)
2c63d6c8 522 {
6cae2504 523 if (pfile->cb.undef)
f8d396cd 524 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
2c63d6c8 525
31ca26b1 526 if (node->flags & NODE_WARN)
73328dce 527 cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
2c63d6c8 528
f80e83a9 529 _cpp_free_definition (node);
2c63d6c8 530 }
79bd622b 531 check_eol (pfile);
1d6fa33f 532}
533
79bd622b 534/* Helper routine used by parse_include. Reinterpret the current line
535 as an h-char-sequence (< ... >); we are looking at the first token
f9b5f742 536 after the <. Returns the header as a token, or NULL on failure. */
537static const cpp_token *
538glue_header_name (pfile)
79bd622b 539 cpp_reader *pfile;
79bd622b 540{
f9b5f742 541 cpp_token *header = NULL;
542 const cpp_token *token;
92b1c81a 543 unsigned char *buffer;
544 size_t len, total_len = 0, capacity = 1024;
79bd622b 545
546 /* To avoid lexed tokens overwriting our glued name, we can only
547 allocate from the string pool once we've lexed everything. */
92b1c81a 548 buffer = (unsigned char *) xmalloc (capacity);
79bd622b 549 for (;;)
550 {
f9b5f742 551 token = cpp_get_token (pfile);
79bd622b 552
f9b5f742 553 if (token->type == CPP_GREATER || token->type == CPP_EOF)
79bd622b 554 break;
555
92b1c81a 556 len = cpp_token_len (token);
557 if (total_len + len > capacity)
79bd622b 558 {
92b1c81a 559 capacity = (capacity + len) * 2;
560 buffer = (unsigned char *) xrealloc (buffer, capacity);
79bd622b 561 }
562
f9b5f742 563 if (token->flags & PREV_WHITE)
92b1c81a 564 buffer[total_len++] = ' ';
79bd622b 565
92b1c81a 566 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
79bd622b 567 }
568
f9b5f742 569 if (token->type == CPP_EOF)
73328dce 570 cpp_error (pfile, DL_ERROR, "missing terminating > character");
79bd622b 571 else
572 {
92b1c81a 573 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
574 memcpy (token_mem, buffer, total_len);
575 token_mem[total_len] = '\0';
576
f9b5f742 577 header = _cpp_temp_token (pfile);
79bd622b 578 header->type = CPP_HEADER_NAME;
1fdf6039 579 header->flags = 0;
92b1c81a 580 header->val.str.len = total_len;
581 header->val.str.text = token_mem;
79bd622b 582 }
f80e83a9 583
92b1c81a 584 free ((PTR) buffer);
f9b5f742 585 return header;
79bd622b 586}
1d6fa33f 587
f9b5f742 588/* Returns the header string of #include, #include_next, #import and
589 #pragma dependency. Returns NULL on error. */
590static const cpp_token *
591parse_include (pfile)
1d6fa33f 592 cpp_reader *pfile;
593{
79bd622b 594 const unsigned char *dir;
f9b5f742 595 const cpp_token *header;
1d6fa33f 596
968e3f9d 597 if (pfile->directive == &dtable[T_PRAGMA])
79bd622b 598 dir = U"pragma dependency";
599 else
600 dir = pfile->directive->name;
601
602 /* Allow macro expansion. */
f9b5f742 603 header = cpp_get_token (pfile);
79bd622b 604 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
1d6fa33f 605 {
79bd622b 606 if (header->type != CPP_LESS)
f80e83a9 607 {
73328dce 608 cpp_error (pfile, DL_ERROR,
609 "#%s expects \"FILENAME\" or <FILENAME>", dir);
f9b5f742 610 return NULL;
f80e83a9 611 }
f9b5f742 612
613 header = glue_header_name (pfile);
614 if (header == NULL)
615 return header;
1d6fa33f 616 }
79bd622b 617
618 if (header->val.str.len == 0)
1d6fa33f 619 {
73328dce 620 cpp_error (pfile, DL_ERROR, "empty file name in #%s", dir);
f9b5f742 621 return NULL;
1d6fa33f 622 }
1d6fa33f 623
f9b5f742 624 return header;
4bf559f2 625}
d453a374 626
0d6d8dc0 627/* Handle #include, #include_next and #import. */
69461e0d 628static void
0d6d8dc0 629do_include_common (pfile, type)
4bf559f2 630 cpp_reader *pfile;
0d6d8dc0 631 enum include_type type;
4bf559f2 632{
f9b5f742 633 const cpp_token *header;
1d6fa33f 634
968e3f9d 635 /* For #include_next, if this is the primary source file, warn and
636 use the normal search logic. */
637 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
638 {
73328dce 639 cpp_error (pfile, DL_WARNING, "#include_next in primary source file");
968e3f9d 640 type = IT_INCLUDE;
641 }
642 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
643 {
644 CPP_OPTION (pfile, warn_import) = 0;
73328dce 645 cpp_error (pfile, DL_WARNING,
968e3f9d 646 "#import is obsolete, use an #ifndef wrapper in the header file");
647 }
648
f9b5f742 649 header = parse_include (pfile);
650 if (header)
0d6d8dc0 651 {
652 /* Prevent #include recursion. */
4087823d 653 if (pfile->line_maps.depth >= CPP_STACK_MAX)
b9093358 654 cpp_error (pfile, DL_ERROR, "#include nested too deeply");
0d6d8dc0 655 else
656 {
968e3f9d 657 check_eol (pfile);
658 /* Get out of macro context, if we are. */
fb5ab82c 659 skip_rest_of_line (pfile);
968e3f9d 660 if (pfile->cb.include)
f8d396cd 661 (*pfile->cb.include) (pfile, pfile->directive_line,
f9b5f742 662 pfile->directive->name, header);
0d6d8dc0 663
f9b5f742 664 _cpp_execute_include (pfile, header, type);
0d6d8dc0 665 }
666 }
4bf559f2 667}
c4e0ec82 668
69461e0d 669static void
0d6d8dc0 670do_include (pfile)
4bf559f2 671 cpp_reader *pfile;
672{
0d6d8dc0 673 do_include_common (pfile, IT_INCLUDE);
674}
4bf559f2 675
0d6d8dc0 676static void
677do_import (pfile)
678 cpp_reader *pfile;
679{
0d6d8dc0 680 do_include_common (pfile, IT_IMPORT);
4bf559f2 681}
1d6fa33f 682
69461e0d 683static void
4bf559f2 684do_include_next (pfile)
685 cpp_reader *pfile;
686{
0d6d8dc0 687 do_include_common (pfile, IT_INCLUDE_NEXT);
1d6fa33f 688}
1d6fa33f 689
25266990 690/* Subroutine of do_linemarker. Read possible flags after file name.
691 LAST is the last flag seen; 0 if this is the first flag. Return the
692 flag if it is valid, 0 at the end of the directive. Otherwise
693 complain. */
2fd3b10b 694static unsigned int
53c052ca 695read_flag (pfile, last)
314ab6d5 696 cpp_reader *pfile;
53c052ca 697 unsigned int last;
314ab6d5 698{
c00e481c 699 const cpp_token *token = _cpp_lex_token (pfile);
314ab6d5 700
c00e481c 701 if (token->type == CPP_NUMBER && token->val.str.len == 1)
314ab6d5 702 {
c00e481c 703 unsigned int flag = token->val.str.text[0] - '0';
53c052ca 704
705 if (flag > last && flag <= 4
706 && (flag != 4 || last == 3)
707 && (flag != 2 || last == 0))
708 return flag;
314ab6d5 709 }
79bd622b 710
c00e481c 711 if (token->type != CPP_EOF)
73328dce 712 cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
c00e481c 713 cpp_token_as_text (pfile, token));
79bd622b 714 return 0;
314ab6d5 715}
716
25266990 717/* Subroutine of do_line and do_linemarker. Returns a version of STR
718 which has a NUL terminator and all escape sequences converted to
719 their equivalents. Temporary, hopefully. */
b6d18b0a 720static uchar *
25266990 721dequote_string (pfile, str, len)
722 cpp_reader *pfile;
b6d18b0a 723 const uchar *str;
25266990 724 unsigned int len;
725{
b6d18b0a 726 uchar *result = _cpp_unaligned_alloc (pfile, len + 1);
727 uchar *dst = result;
728 const uchar *limit = str + len;
13c457e1 729 cppchar_t c;
25266990 730
25266990 731 while (str < limit)
732 {
733 c = *str++;
734 if (c != '\\')
735 *dst++ = c;
736 else
13c457e1 737 *dst++ = cpp_parse_escape (pfile, &str, limit, 0);
25266990 738 }
739 *dst++ = '\0';
740 return result;
741}
742
743/* Subroutine of do_line and do_linemarker. Convert a number in STR,
744 of length LEN, to binary; store it in NUMP, and return 0 if the
745 number was well-formed, 1 if not. Temporary, hopefully. */
f80e83a9 746static int
747strtoul_for_line (str, len, nump)
b6d18b0a 748 const uchar *str;
f80e83a9 749 unsigned int len;
750 unsigned long *nump;
751{
752 unsigned long reg = 0;
b6d18b0a 753 uchar c;
f80e83a9 754 while (len--)
755 {
756 c = *str++;
757 if (!ISDIGIT (c))
758 return 1;
759 reg *= 10;
760 reg += c - '0';
761 }
762 *nump = reg;
763 return 0;
764}
765
6d71dc85 766/* Interpret #line command.
25266990 767 Note that the filename string (if any) is a true string constant
768 (escapes are interpreted), unlike in #line. */
69461e0d 769static void
4bf559f2 770do_line (pfile)
1d6fa33f 771 cpp_reader *pfile;
1d6fa33f 772{
f9b5f742 773 const cpp_token *token;
5a9f87bb 774 const char *new_file = pfile->map->to_file;
775 unsigned long new_lineno;
79bd622b 776
d7503801 777 /* C99 raised the minimum limit on #line numbers. */
25266990 778 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
d0d481d7 779
79bd622b 780 /* #line commands expand macros. */
f9b5f742 781 token = cpp_get_token (pfile);
782 if (token->type != CPP_NUMBER
783 || strtoul_for_line (token->val.str.text, token->val.str.len,
784 &new_lineno))
1d6fa33f 785 {
73328dce 786 cpp_error (pfile, DL_ERROR,
787 "\"%s\" after #line is not a positive integer",
f9b5f742 788 cpp_token_as_text (pfile, token));
0653b94e 789 return;
b1a9ff83 790 }
1d6fa33f 791
25266990 792 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
73328dce 793 cpp_error (pfile, DL_PEDWARN, "line number out of range");
1d6fa33f 794
f9b5f742 795 token = cpp_get_token (pfile);
796 if (token->type == CPP_STRING)
a852e3b1 797 {
25266990 798 new_file = (const char *) dequote_string (pfile, token->val.str.text,
799 token->val.str.len);
800 check_eol (pfile);
801 }
802 else if (token->type != CPP_EOF)
803 {
73328dce 804 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
25266990 805 cpp_token_as_text (pfile, token));
806 return;
807 }
808
809 skip_rest_of_line (pfile);
810 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
811 pfile->map->sysp);
812}
813
814/* Interpret the # 44 "file" [flags] notation, which has slightly
815 different syntax and semantics from #line: Flags are allowed,
816 and we never complain about the line number being too big. */
817static void
818do_linemarker (pfile)
819 cpp_reader *pfile;
820{
821 const cpp_token *token;
822 const char *new_file = pfile->map->to_file;
823 unsigned long new_lineno;
824 unsigned int new_sysp = pfile->map->sysp;
825 enum lc_reason reason = LC_RENAME;
826 int flag;
827
828 /* Back up so we can get the number again. Putting this in
829 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
830 some circumstances, which can segfault. */
831 _cpp_backup_tokens (pfile, 1);
832
833 /* #line commands expand macros. */
834 token = cpp_get_token (pfile);
835 if (token->type != CPP_NUMBER
836 || strtoul_for_line (token->val.str.text, token->val.str.len,
837 &new_lineno))
838 {
73328dce 839 cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
25266990 840 cpp_token_as_text (pfile, token));
841 return;
b1a9ff83 842 }
616814f8 843
25266990 844 token = cpp_get_token (pfile);
845 if (token->type == CPP_STRING)
846 {
847 new_file = (const char *) dequote_string (pfile, token->val.str.text,
848 token->val.str.len);
849 new_sysp = 0;
850 flag = read_flag (pfile, 0);
851 if (flag == 1)
852 {
853 reason = LC_ENTER;
854 /* Fake an include for cpp_included (). */
855 _cpp_fake_include (pfile, new_file);
856 flag = read_flag (pfile, flag);
857 }
858 else if (flag == 2)
859 {
860 reason = LC_LEAVE;
861 flag = read_flag (pfile, flag);
862 }
863 if (flag == 3)
79bd622b 864 {
25266990 865 new_sysp = 1;
866 flag = read_flag (pfile, flag);
867 if (flag == 4)
868 new_sysp = 2;
79bd622b 869 }
25266990 870
bd507c05 871 check_eol (pfile);
f80e83a9 872 }
f9b5f742 873 else if (token->type != CPP_EOF)
d7503801 874 {
73328dce 875 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
f9b5f742 876 cpp_token_as_text (pfile, token));
d7503801 877 return;
878 }
1d6fa33f 879
fb5ab82c 880 skip_rest_of_line (pfile);
5a9f87bb 881 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
d7503801 882}
883
1ea7ed21 884/* Arrange the file_change callback. pfile->line has changed to
f85fcf2b 885 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
3fb1e43b 886 header, 2 for a system header that needs to be extern "C" protected,
f85fcf2b 887 and zero otherwise. */
4087613d 888void
f85fcf2b 889_cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
d7503801 890 cpp_reader *pfile;
38692459 891 enum lc_reason reason;
f85fcf2b 892 const char *to_file;
1ea7ed21 893 unsigned int file_line;
f85fcf2b 894 unsigned int sysp;
d7503801 895{
f85fcf2b 896 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
897 pfile->line, to_file, file_line);
38692459 898
4087613d 899 if (pfile->cb.file_change)
f85fcf2b 900 (*pfile->cb.file_change) (pfile, pfile->map);
1d6fa33f 901}
616814f8 902
e484a1cc 903/* Report a warning or error detected by the program we are
904 processing. Use the directive's tokens in the error message. */
69461e0d 905static void
def71b06 906do_diagnostic (pfile, code, print_dir)
1d6fa33f 907 cpp_reader *pfile;
73328dce 908 int code;
def71b06 909 int print_dir;
1d6fa33f 910{
73328dce 911 if (_cpp_begin_message (pfile, code,
912 pfile->cur_token[-1].line,
913 pfile->cur_token[-1].col))
6cae2504 914 {
def71b06 915 if (print_dir)
916 fprintf (stderr, "#%s ", pfile->directive->name);
79bd622b 917 pfile->state.prevent_expansion++;
918 cpp_output_line (pfile, stderr);
919 pfile->state.prevent_expansion--;
6cae2504 920 }
1d6fa33f 921}
922
f3b83e9b 923static void
924do_error (pfile)
925 cpp_reader *pfile;
926{
73328dce 927 do_diagnostic (pfile, DL_ERROR, 1);
f3b83e9b 928}
1d6fa33f 929
69461e0d 930static void
4bf559f2 931do_warning (pfile)
1d6fa33f 932 cpp_reader *pfile;
1d6fa33f 933{
2ef5e3d3 934 /* We want #warning diagnostics to be emitted in system headers too. */
73328dce 935 do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
1d6fa33f 936}
937
4e29fcb2 938/* Report program identification. */
69461e0d 939static void
4bf559f2 940do_ident (pfile)
1d6fa33f 941 cpp_reader *pfile;
1d6fa33f 942{
f9b5f742 943 const cpp_token *str = cpp_get_token (pfile);
6cae2504 944
f9b5f742 945 if (str->type != CPP_STRING)
73328dce 946 cpp_error (pfile, DL_ERROR, "invalid #ident directive");
79bd622b 947 else if (pfile->cb.ident)
f9b5f742 948 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
4e29fcb2 949
79bd622b 950 check_eol (pfile);
1d6fa33f 951}
952
c6f14ce5 953/* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
954 matching entry, or NULL if none is found. The returned entry could
955 be the start of a namespace chain, or a pragma. */
956static struct pragma_entry *
957lookup_pragma_entry (chain, pragma)
958 struct pragma_entry *chain;
5c43d212 959 const cpp_hashnode *pragma;
3f075661 960{
5c43d212 961 while (chain && chain->pragma != pragma)
962 chain = chain->next;
c6f14ce5 963
964 return chain;
965}
966
967/* Create and insert a pragma entry for NAME at the beginning of a
968 singly-linked CHAIN. If handler is NULL, it is a namespace,
969 otherwise it is a pragma and its handler. */
970static struct pragma_entry *
5c43d212 971insert_pragma_entry (pfile, chain, pragma, handler)
6cae2504 972 cpp_reader *pfile;
c6f14ce5 973 struct pragma_entry **chain;
5c43d212 974 const cpp_hashnode *pragma;
38692459 975 pragma_cb handler;
3f075661 976{
c6f14ce5 977 struct pragma_entry *new;
6cae2504 978
162cee98 979 new = (struct pragma_entry *)
e6a5f963 980 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
5c43d212 981 new->pragma = pragma;
c6f14ce5 982 if (handler)
983 {
984 new->is_nspace = 0;
985 new->u.handler = handler;
986 }
987 else
988 {
989 new->is_nspace = 1;
990 new->u.space = NULL;
991 }
6cae2504 992
c6f14ce5 993 new->next = *chain;
994 *chain = new;
995 return new;
6cae2504 996}
3f075661 997
c6f14ce5 998/* Register a pragma NAME in namespace SPACE. If SPACE is null, it
999 goes in the global namespace. HANDLER is the handler it will call,
1000 which must be non-NULL. */
6cae2504 1001void
c6f14ce5 1002cpp_register_pragma (pfile, space, name, handler)
3f075661 1003 cpp_reader *pfile;
6cae2504 1004 const char *space;
c6f14ce5 1005 const char *name;
1006 pragma_cb handler;
3f075661 1007{
c6f14ce5 1008 struct pragma_entry **chain = &pfile->pragmas;
1009 struct pragma_entry *entry;
5c43d212 1010 const cpp_hashnode *node;
c6f14ce5 1011
1012 if (!handler)
1013 abort ();
6cae2504 1014
c6f14ce5 1015 if (space)
6cae2504 1016 {
5c43d212 1017 node = cpp_lookup (pfile, U space, strlen (space));
1018 entry = lookup_pragma_entry (*chain, node);
c6f14ce5 1019 if (!entry)
5c43d212 1020 entry = insert_pragma_entry (pfile, chain, node, NULL);
c6f14ce5 1021 else if (!entry->is_nspace)
1022 goto clash;
1023 chain = &entry->u.space;
6cae2504 1024 }
1025
c6f14ce5 1026 /* Check for duplicates. */
5c43d212 1027 node = cpp_lookup (pfile, U name, strlen (name));
1028 entry = lookup_pragma_entry (*chain, node);
c6f14ce5 1029 if (entry)
1030 {
1031 if (entry->is_nspace)
1032 clash:
73328dce 1033 cpp_error (pfile, DL_ICE,
c6f14ce5 1034 "registering \"%s\" as both a pragma and a pragma namespace",
5c43d212 1035 NODE_NAME (node));
c6f14ce5 1036 else if (space)
73328dce 1037 cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1038 space, name);
c6f14ce5 1039 else
73328dce 1040 cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
c6f14ce5 1041 }
1042 else
5c43d212 1043 insert_pragma_entry (pfile, chain, node, handler);
6cae2504 1044}
c6f14ce5 1045
1046/* Register the pragmas the preprocessor itself handles. */
6cae2504 1047void
1048_cpp_init_internal_pragmas (pfile)
1049 cpp_reader *pfile;
1050{
c6f14ce5 1051 /* Pragmas in the global namespace. */
6cae2504 1052 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1053
c6f14ce5 1054 /* New GCC-specific pragmas should be put in the GCC namespace. */
6cae2504 1055 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1056 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1057 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
3f075661 1058}
1d6fa33f 1059
c6f14ce5 1060/* Pragmata handling. We handle some, and pass the rest on to the
1061 front end. C99 defines three pragmas and says that no macro
1062 expansion is to be performed on them; whether or not macro
1063 expansion happens for other pragmas is implementation defined.
1064 This implementation never macro-expands the text after #pragma. */
69461e0d 1065static void
4bf559f2 1066do_pragma (pfile)
1d6fa33f 1067 cpp_reader *pfile;
1d6fa33f 1068{
c6f14ce5 1069 const struct pragma_entry *p = NULL;
f9b5f742 1070 const cpp_token *token;
c6f14ce5 1071 unsigned int count = 1;
2e398cc7 1072
79bd622b 1073 pfile->state.prevent_expansion++;
6cae2504 1074
f9b5f742 1075 token = cpp_get_token (pfile);
1076 if (token->type == CPP_NAME)
c0b823af 1077 {
5c43d212 1078 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
c6f14ce5 1079 if (p && p->is_nspace)
6cae2504 1080 {
c6f14ce5 1081 count = 2;
1082 token = cpp_get_token (pfile);
1083 if (token->type == CPP_NAME)
5c43d212 1084 p = lookup_pragma_entry (p->u.space, token->val.node);
c6f14ce5 1085 else
1086 p = NULL;
6cae2504 1087 }
6cae2504 1088 }
f80e83a9 1089
5621a364 1090 /* FIXME. This is an awful kludge to get the front ends to update
1091 their notion of line number for diagnostic purposes. The line
1092 number should be passed to the handler and they should do it
1093 themselves. Stand-alone CPP must ignore us, otherwise it will
1094 prefix the directive with spaces, hence the 1. Ugh. */
1095 if (pfile->cb.line_change)
f9b5f742 1096 (*pfile->cb.line_change)(pfile, token, 1);
5621a364 1097
c6f14ce5 1098 if (p)
1099 (*p->u.handler) (pfile);
38692459 1100 else if (pfile->cb.def_pragma)
fb5ab82c 1101 {
1102 _cpp_backup_tokens (pfile, count);
1103 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1104 }
c6f14ce5 1105
5621a364 1106 pfile->state.prevent_expansion--;
3f075661 1107}
1108
e484a1cc 1109/* Handle #pragma once. */
6cae2504 1110static void
4e29fcb2 1111do_pragma_once (pfile)
1112 cpp_reader *pfile;
1113{
73328dce 1114 cpp_error (pfile, DL_WARNING, "#pragma once is obsolete");
b1a9ff83 1115
2fd3b10b 1116 if (pfile->buffer->prev == NULL)
73328dce 1117 cpp_error (pfile, DL_WARNING, "#pragma once in main file");
4e29fcb2 1118 else
2fd3b10b 1119 _cpp_never_reread (pfile->buffer->inc);
79bd622b 1120
1121 check_eol (pfile);
4e29fcb2 1122}
c9d838e1 1123
8e1db61c 1124/* Handle #pragma GCC poison, to poison one or more identifiers so
1125 that the lexer produces a hard error for each subsequent usage. */
6cae2504 1126static void
4e29fcb2 1127do_pragma_poison (pfile)
1128 cpp_reader *pfile;
1129{
c00e481c 1130 const cpp_token *tok;
c4abf88d 1131 cpp_hashnode *hp;
4e29fcb2 1132
79bd622b 1133 pfile->state.poisoned_ok = 1;
4e29fcb2 1134 for (;;)
1135 {
c00e481c 1136 tok = _cpp_lex_token (pfile);
1137 if (tok->type == CPP_EOF)
4e29fcb2 1138 break;
c00e481c 1139 if (tok->type != CPP_NAME)
c9d838e1 1140 {
73328dce 1141 cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
79bd622b 1142 break;
c9d838e1 1143 }
1144
c00e481c 1145 hp = tok->val.node;
79bd622b 1146 if (hp->flags & NODE_POISONED)
1147 continue;
1148
1149 if (hp->type == NT_MACRO)
73328dce 1150 cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1151 NODE_NAME (hp));
79bd622b 1152 _cpp_free_definition (hp);
1153 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
c9d838e1 1154 }
79bd622b 1155 pfile->state.poisoned_ok = 0;
1d6fa33f 1156}
e35f919d 1157
1158/* Mark the current header as a system header. This will suppress
1159 some categories of warnings (notably those from -pedantic). It is
1160 intended for use in system libraries that cannot be implemented in
1161 conforming C, but cannot be certain that their headers appear in a
1162 system include directory. To prevent abuse, it is rejected in the
1163 primary source file. */
6cae2504 1164static void
e35f919d 1165do_pragma_system_header (pfile)
1166 cpp_reader *pfile;
1167{
fd944c82 1168 cpp_buffer *buffer = pfile->buffer;
1169
1170 if (buffer->prev == 0)
73328dce 1171 cpp_error (pfile, DL_WARNING,
1172 "#pragma system_header ignored outside include file");
e35f919d 1173 else
38692459 1174 {
1175 check_eol (pfile);
fb5ab82c 1176 skip_rest_of_line (pfile);
38692459 1177 cpp_make_system_header (pfile, 1, 0);
1178 }
e35f919d 1179}
e485814b 1180
1181/* Check the modified date of the current include file against a specified
1182 file. Issue a diagnostic, if the specified file is newer. We use this to
1183 determine if a fixed header should be refixed. */
6cae2504 1184static void
e485814b 1185do_pragma_dependency (pfile)
1186 cpp_reader *pfile;
1187{
f9b5f742 1188 const cpp_token *header;
79bd622b 1189 int ordering;
b1a9ff83 1190
f9b5f742 1191 header = parse_include (pfile);
1192 if (!header)
6cae2504 1193 return;
f80e83a9 1194
f9b5f742 1195 ordering = _cpp_compare_file_date (pfile, header);
e485814b 1196 if (ordering < 0)
73328dce 1197 cpp_error (pfile, DL_WARNING, "cannot find source %s",
1198 cpp_token_as_text (pfile, header));
e485814b 1199 else if (ordering > 0)
1200 {
73328dce 1201 cpp_error (pfile, DL_WARNING, "current file is older than %s",
1202 cpp_token_as_text (pfile, header));
f9b5f742 1203 if (cpp_get_token (pfile)->type != CPP_EOF)
fb5ab82c 1204 {
1205 _cpp_backup_tokens (pfile, 1);
73328dce 1206 do_diagnostic (pfile, DL_WARNING, 0);
fb5ab82c 1207 }
e485814b 1208 }
e485814b 1209}
1210
f9b5f742 1211/* Get a token but skip padding. */
1212static const cpp_token *
1213get_token_no_padding (pfile)
396ffa86 1214 cpp_reader *pfile;
396ffa86 1215{
f9b5f742 1216 for (;;)
1217 {
1218 const cpp_token *result = cpp_get_token (pfile);
1219 if (result->type != CPP_PADDING)
1220 return result;
1221 }
1222}
396ffa86 1223
f9b5f742 1224/* Check syntax is "(string-literal)". Returns the string on success,
1225 or NULL on failure. */
1226static const cpp_token *
1227get__Pragma_string (pfile)
1228 cpp_reader *pfile;
1229{
1230 const cpp_token *string;
396ffa86 1231
f9b5f742 1232 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1233 return NULL;
1234
1235 string = get_token_no_padding (pfile);
396ffa86 1236 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
f9b5f742 1237 return NULL;
1238
1239 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1240 return NULL;
396ffa86 1241
f9b5f742 1242 return string;
396ffa86 1243}
1244
1e0ef2fd 1245/* Destringize IN into a temporary buffer, by removing the first \ of
1246 \" and \\ sequences, and process the result as a #pragma directive. */
1247static void
1248destringize_and_run (pfile, in)
1249 cpp_reader *pfile;
396ffa86 1250 const cpp_string *in;
396ffa86 1251{
1252 const unsigned char *src, *limit;
1e0ef2fd 1253 char *dest, *result;
396ffa86 1254
435fb09b 1255 dest = result = alloca (in->len + 1);
396ffa86 1256 for (src = in->text, limit = src + in->len; src < limit;)
1257 {
1258 /* We know there is a character following the backslash. */
1259 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1260 src++;
1261 *dest++ = *src++;
1262 }
435fb09b 1263 *dest = '\0';
396ffa86 1264
1e0ef2fd 1265 run_directive (pfile, T_PRAGMA, result, dest - result);
396ffa86 1266}
1267
1e0ef2fd 1268/* Handle the _Pragma operator. */
396ffa86 1269void
1270_cpp_do__Pragma (pfile)
1271 cpp_reader *pfile;
1272{
f9b5f742 1273 const cpp_token *string = get__Pragma_string (pfile);
396ffa86 1274
f9b5f742 1275 if (!string)
73328dce 1276 cpp_error (pfile, DL_ERROR,
1277 "_Pragma takes a parenthesized string literal");
1ea7ed21 1278 else
396ffa86 1279 {
729d2022 1280 /* Ideally, we'd like
1281 token1 _Pragma ("foo") token2
1282 to be output as
1283 token1
1284 # 7 "file.c"
1285 #pragma foo
1286 # 7 "file.c"
1287 token2
1288 Getting these correct line markers is a little tricky. */
1289
1290 unsigned int orig_line = pfile->line;
1e0ef2fd 1291 destringize_and_run (pfile, &string->val.str);
729d2022 1292 pfile->line = orig_line;
1293 pfile->buffer->saved_flags = BOL;
396ffa86 1294 }
396ffa86 1295}
1296
1d6fa33f 1297/* Just ignore #sccs, on systems where we define it at all. */
ef33b55c 1298#ifdef SCCS_DIRECTIVE
69461e0d 1299static void
4bf559f2 1300do_sccs (pfile)
f80e83a9 1301 cpp_reader *pfile ATTRIBUTE_UNUSED;
1d6fa33f 1302{
1d6fa33f 1303}
ef33b55c 1304#endif
20534e99 1305
e484a1cc 1306/* Handle #ifdef. */
69461e0d 1307static void
4bf559f2 1308do_ifdef (pfile)
1309 cpp_reader *pfile;
1310{
79bd622b 1311 int skip = 1;
f80e83a9 1312
5e878517 1313 if (! pfile->state.skipping)
79bd622b 1314 {
1315 const cpp_hashnode *node = lex_macro_node (pfile);
f80e83a9 1316
79bd622b 1317 if (node)
1318 skip = node->type != NT_MACRO;
f99f369d 1319
1320 if (node)
1321 check_eol (pfile);
79bd622b 1322 }
4bf559f2 1323
79bd622b 1324 push_conditional (pfile, skip, T_IFDEF, 0);
1325}
4bf559f2 1326
e484a1cc 1327/* Handle #ifndef. */
69461e0d 1328static void
4bf559f2 1329do_ifndef (pfile)
1330 cpp_reader *pfile;
1331{
79bd622b 1332 int skip = 1;
76faa4c0 1333 const cpp_hashnode *node = 0;
4bf559f2 1334
5e878517 1335 if (! pfile->state.skipping)
b970ec42 1336 {
79bd622b 1337 node = lex_macro_node (pfile);
1338 if (node)
1339 skip = node->type == NT_MACRO;
f99f369d 1340
1341 if (node)
1342 check_eol (pfile);
b970ec42 1343 }
f80e83a9 1344
79bd622b 1345 push_conditional (pfile, skip, T_IFNDEF, node);
1d6fa33f 1346}
1347
fa233610 1348/* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1349 pfile->mi_ind_cmacro so we can handle multiple-include
1350 optimisations. If macro expansion occurs in the expression, we
1351 cannot treat it as a controlling conditional, since the expansion
1352 could change in the future. That is handled by cpp_get_token. */
69461e0d 1353static void
c4357c92 1354do_if (pfile)
1d6fa33f 1355 cpp_reader *pfile;
1d6fa33f 1356{
79bd622b 1357 int skip = 1;
1d6fa33f 1358
5e878517 1359 if (! pfile->state.skipping)
5bbf045f 1360 skip = _cpp_parse_expr (pfile) == false;
79bd622b 1361
fa233610 1362 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1d6fa33f 1363}
1364
920b5d41 1365/* Flip skipping state if appropriate and continue without changing
c4357c92 1366 if_stack; this is so that the error message for missing #endif's
1367 etc. will point to the original #if. */
69461e0d 1368static void
c4357c92 1369do_else (pfile)
1370 cpp_reader *pfile;
8e33b875 1371{
920b5d41 1372 cpp_buffer *buffer = pfile->buffer;
1373 struct if_stack *ifs = buffer->if_stack;
c030ed73 1374
c4357c92 1375 if (ifs == NULL)
73328dce 1376 cpp_error (pfile, DL_ERROR, "#else without #if");
79bd622b 1377 else
c030ed73 1378 {
79bd622b 1379 if (ifs->type == T_ELSE)
1380 {
73328dce 1381 cpp_error (pfile, DL_ERROR, "#else after #else");
1382 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
79bd622b 1383 "the conditional began here");
1384 }
920b5d41 1385 ifs->type = T_ELSE;
1386
5e878517 1387 /* Skip any future (erroneous) #elses or #elifs. */
1388 pfile->state.skipping = ifs->skip_elses;
1389 ifs->skip_elses = true;
1d6fa33f 1390
79bd622b 1391 /* Invalidate any controlling macro. */
1392 ifs->mi_cmacro = 0;
79bd622b 1393
5e878517 1394 /* Only check EOL if was not originally skipping. */
923acdd5 1395 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
5e878517 1396 check_eol (pfile);
1397 }
1d6fa33f 1398}
1399
e484a1cc 1400/* Handle a #elif directive by not changing if_stack either. See the
79bd622b 1401 comment above do_else. */
69461e0d 1402static void
c4357c92 1403do_elif (pfile)
1d6fa33f 1404 cpp_reader *pfile;
1d6fa33f 1405{
920b5d41 1406 cpp_buffer *buffer = pfile->buffer;
1407 struct if_stack *ifs = buffer->if_stack;
1d6fa33f 1408
c4357c92 1409 if (ifs == NULL)
73328dce 1410 cpp_error (pfile, DL_ERROR, "#elif without #if");
920b5d41 1411 else
0b60628c 1412 {
920b5d41 1413 if (ifs->type == T_ELSE)
1414 {
73328dce 1415 cpp_error (pfile, DL_ERROR, "#elif after #else");
1416 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
920b5d41 1417 "the conditional began here");
1418 }
1419 ifs->type = T_ELIF;
79bd622b 1420
5e878517 1421 /* Only evaluate this if we aren't skipping elses. During
1422 evaluation, set skipping to false to get lexer warnings. */
1423 if (ifs->skip_elses)
1424 pfile->state.skipping = 1;
1425 else
920b5d41 1426 {
5e878517 1427 pfile->state.skipping = 0;
1428 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1429 ifs->skip_elses = ! pfile->state.skipping;
920b5d41 1430 }
5e878517 1431
1432 /* Invalidate any controlling macro. */
1433 ifs->mi_cmacro = 0;
0b60628c 1434 }
1d6fa33f 1435}
1436
5e878517 1437/* #endif pops the if stack and resets pfile->state.skipping. */
69461e0d 1438static void
4bf559f2 1439do_endif (pfile)
1d6fa33f 1440 cpp_reader *pfile;
1d6fa33f 1441{
920b5d41 1442 cpp_buffer *buffer = pfile->buffer;
1443 struct if_stack *ifs = buffer->if_stack;
c4357c92 1444
c4357c92 1445 if (ifs == NULL)
73328dce 1446 cpp_error (pfile, DL_ERROR, "#endif without #if");
1d6fa33f 1447 else
1448 {
5e878517 1449 /* Only check EOL if was not originally skipping. */
923acdd5 1450 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
5e878517 1451 check_eol (pfile);
1452
79bd622b 1453 /* If potential control macro, we go back outside again. */
1454 if (ifs->next == 0 && ifs->mi_cmacro)
1455 {
fa233610 1456 pfile->mi_valid = true;
79bd622b 1457 pfile->mi_cmacro = ifs->mi_cmacro;
1458 }
1459
920b5d41 1460 buffer->if_stack = ifs->next;
5e878517 1461 pfile->state.skipping = ifs->was_skipping;
0d086e18 1462 obstack_free (&pfile->buffer_ob, ifs);
1d6fa33f 1463 }
79bd622b 1464}
f80e83a9 1465
e484a1cc 1466/* Push an if_stack entry for a preprocessor conditional, and set
1467 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1468 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1469 we need to check here that we are at the top of the file. */
c4357c92 1470static void
1471push_conditional (pfile, skip, type, cmacro)
1472 cpp_reader *pfile;
1473 int skip;
1474 int type;
1475 const cpp_hashnode *cmacro;
1476{
1477 struct if_stack *ifs;
920b5d41 1478 cpp_buffer *buffer = pfile->buffer;
c4357c92 1479
0d086e18 1480 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
729d2022 1481 ifs->line = pfile->directive_line;
920b5d41 1482 ifs->next = buffer->if_stack;
5e878517 1483 ifs->skip_elses = pfile->state.skipping || !skip;
1484 ifs->was_skipping = pfile->state.skipping;
c4357c92 1485 ifs->type = type;
fa233610 1486 /* This condition is effectively a test for top-of-file. */
1487 if (pfile->mi_valid && pfile->mi_cmacro == 0)
79bd622b 1488 ifs->mi_cmacro = cmacro;
1489 else
1490 ifs->mi_cmacro = 0;
c4357c92 1491
5e878517 1492 pfile->state.skipping = skip;
920b5d41 1493 buffer->if_stack = ifs;
4896abfd 1494}
69bcbfc6 1495
e484a1cc 1496/* Read the tokens of the answer into the macro pool, in a directive
1497 of type TYPE. Only commit the memory if we intend it as permanent
1498 storage, i.e. the #assert case. Returns 0 on success, and sets
1499 ANSWERP to point to the answer. */
79bd622b 1500static int
1501parse_answer (pfile, answerp, type)
1d6fa33f 1502 cpp_reader *pfile;
f80e83a9 1503 struct answer **answerp;
79bd622b 1504 int type;
1d6fa33f 1505{
f9b5f742 1506 const cpp_token *paren;
79bd622b 1507 struct answer *answer;
e6a5f963 1508 unsigned int acount;
79bd622b 1509
1510 /* In a conditional, it is legal to not have an open paren. We
1511 should save the following token in this case. */
f9b5f742 1512 paren = cpp_get_token (pfile);
79bd622b 1513
1514 /* If not a paren, see if we're OK. */
f9b5f742 1515 if (paren->type != CPP_OPEN_PAREN)
f80e83a9 1516 {
79bd622b 1517 /* In a conditional no answer is a test for any answer. It
1518 could be followed by any token. */
1519 if (type == T_IF)
fb5ab82c 1520 {
1521 _cpp_backup_tokens (pfile, 1);
1522 return 0;
1523 }
79bd622b 1524
1525 /* #unassert with no answer is valid - it removes all answers. */
f9b5f742 1526 if (type == T_UNASSERT && paren->type == CPP_EOF)
79bd622b 1527 return 0;
bce8e0c0 1528
73328dce 1529 cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
79bd622b 1530 return 1;
f80e83a9 1531 }
69bcbfc6 1532
e6a5f963 1533 for (acount = 0;; acount++)
f80e83a9 1534 {
e6a5f963 1535 size_t room_needed;
1536 const cpp_token *token = cpp_get_token (pfile);
1537 cpp_token *dest;
79bd622b 1538
f80e83a9 1539 if (token->type == CPP_CLOSE_PAREN)
1540 break;
3ba8b497 1541
79bd622b 1542 if (token->type == CPP_EOF)
f80e83a9 1543 {
73328dce 1544 cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
79bd622b 1545 return 1;
f80e83a9 1546 }
e6a5f963 1547
1548 /* struct answer includes the space for one token. */
1549 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1550
1551 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1552 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1553
1554 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1555 *dest = *token;
1556
1557 /* Drop whitespace at start, for answer equivalence purposes. */
1558 if (acount == 0)
1559 dest->flags &= ~PREV_WHITE;
f80e83a9 1560 }
bce8e0c0 1561
e6a5f963 1562 if (acount == 0)
69bcbfc6 1563 {
73328dce 1564 cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
79bd622b 1565 return 1;
1d6fa33f 1566 }
f80e83a9 1567
e6a5f963 1568 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1569 answer->count = acount;
1570 answer->next = NULL;
79bd622b 1571 *answerp = answer;
f80e83a9 1572
79bd622b 1573 return 0;
1574}
f80e83a9 1575
e484a1cc 1576/* Parses an assertion directive of type TYPE, returning a pointer to
1577 the hash node of the predicate, or 0 on error. If an answer was
1578 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
79bd622b 1579static cpp_hashnode *
1580parse_assertion (pfile, answerp, type)
1581 cpp_reader *pfile;
1582 struct answer **answerp;
1583 int type;
1584{
1585 cpp_hashnode *result = 0;
f9b5f742 1586 const cpp_token *predicate;
79bd622b 1587
1588 /* We don't expand predicates or answers. */
1589 pfile->state.prevent_expansion++;
1590
79bd622b 1591 *answerp = 0;
f9b5f742 1592 predicate = cpp_get_token (pfile);
1593 if (predicate->type == CPP_EOF)
73328dce 1594 cpp_error (pfile, DL_ERROR, "assertion without predicate");
f9b5f742 1595 else if (predicate->type != CPP_NAME)
73328dce 1596 cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
79bd622b 1597 else if (parse_answer (pfile, answerp, type) == 0)
1598 {
f9b5f742 1599 unsigned int len = NODE_LEN (predicate->val.node);
79bd622b 1600 unsigned char *sym = alloca (len + 1);
f80e83a9 1601
79bd622b 1602 /* Prefix '#' to get it out of macro namespace. */
1603 sym[0] = '#';
f9b5f742 1604 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
79bd622b 1605 result = cpp_lookup (pfile, sym, len + 1);
1606 }
69bcbfc6 1607
79bd622b 1608 pfile->state.prevent_expansion--;
1609 return result;
1d6fa33f 1610}
69bcbfc6 1611
e484a1cc 1612/* Returns a pointer to the pointer to CANDIDATE in the answer chain,
f80e83a9 1613 or a pointer to NULL if the answer is not in the chain. */
79bd622b 1614static struct answer **
1615find_answer (node, candidate)
f80e83a9 1616 cpp_hashnode *node;
79bd622b 1617 const struct answer *candidate;
1d6fa33f 1618{
79bd622b 1619 unsigned int i;
f80e83a9 1620 struct answer **result;
1d6fa33f 1621
f80e83a9 1622 for (result = &node->value.answers; *result; result = &(*result)->next)
79bd622b 1623 {
1624 struct answer *answer = *result;
1625
1626 if (answer->count == candidate->count)
1627 {
1628 for (i = 0; i < answer->count; i++)
1629 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1630 break;
1631
1632 if (i == answer->count)
1633 break;
1634 }
1635 }
c030ed73 1636
f80e83a9 1637 return result;
1638}
bce8e0c0 1639
79bd622b 1640/* Test an assertion within a preprocessor conditional. Returns
1641 non-zero on failure, zero on success. On success, the result of
1642 the test is written into VALUE. */
1643int
1644_cpp_test_assertion (pfile, value)
1645 cpp_reader *pfile;
93bfa70b 1646 unsigned int *value;
79bd622b 1647{
1648 struct answer *answer;
1649 cpp_hashnode *node;
1650
1651 node = parse_assertion (pfile, &answer, T_IF);
1652 if (node)
1653 *value = (node->type == NT_ASSERTION &&
1654 (answer == 0 || *find_answer (node, answer) != 0));
1655
1656 /* We don't commit the memory for the answer - it's temporary only. */
1657 return node == 0;
1658}
1659
e484a1cc 1660/* Handle #assert. */
69461e0d 1661static void
f80e83a9 1662do_assert (pfile)
1663 cpp_reader *pfile;
1664{
1665 struct answer *new_answer;
1666 cpp_hashnode *node;
b1a9ff83 1667
79bd622b 1668 node = parse_assertion (pfile, &new_answer, T_ASSERT);
f80e83a9 1669 if (node)
c030ed73 1670 {
79bd622b 1671 /* Place the new answer in the answer list. First check there
1672 is not a duplicate. */
f80e83a9 1673 new_answer->next = 0;
79bd622b 1674 if (node->type == NT_ASSERTION)
f80e83a9 1675 {
79bd622b 1676 if (*find_answer (node, new_answer))
1677 {
73328dce 1678 cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1679 NODE_NAME (node) + 1);
79bd622b 1680 return;
1681 }
f80e83a9 1682 new_answer->next = node->value.answers;
1683 }
e6a5f963 1684
79bd622b 1685 node->type = NT_ASSERTION;
f80e83a9 1686 node->value.answers = new_answer;
e6a5f963 1687 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1688 + (new_answer->count - 1)
1689 * sizeof (cpp_token));
1690 check_eol (pfile);
c030ed73 1691 }
f80e83a9 1692}
bce8e0c0 1693
e484a1cc 1694/* Handle #unassert. */
69461e0d 1695static void
f80e83a9 1696do_unassert (pfile)
1697 cpp_reader *pfile;
1698{
1699 cpp_hashnode *node;
79bd622b 1700 struct answer *answer;
b1a9ff83 1701
79bd622b 1702 node = parse_assertion (pfile, &answer, T_UNASSERT);
1703 /* It isn't an error to #unassert something that isn't asserted. */
1704 if (node && node->type == NT_ASSERTION)
69bcbfc6 1705 {
79bd622b 1706 if (answer)
bce8e0c0 1707 {
79bd622b 1708 struct answer **p = find_answer (node, answer), *temp;
f80e83a9 1709
79bd622b 1710 /* Remove the answer from the list. */
1711 temp = *p;
1712 if (temp)
1713 *p = temp->next;
3ba8b497 1714
79bd622b 1715 /* Did we free the last answer? */
1716 if (node->value.answers == 0)
1717 node->type = NT_VOID;
e6a5f963 1718
1719 check_eol (pfile);
79bd622b 1720 }
1721 else
1722 _cpp_free_definition (node);
f80e83a9 1723 }
79bd622b 1724
1725 /* We don't commit the memory for the answer - it's temporary only. */
1d6fa33f 1726}
1d6fa33f 1727
0578f103 1728/* These are for -D, -U, -A. */
1729
1730/* Process the string STR as if it appeared as the body of a #define.
1731 If STR is just an identifier, define it with value 1.
1732 If STR has anything after the identifier, then it should
2c0e001b 1733 be identifier=definition. */
9fa36617 1734void
0578f103 1735cpp_define (pfile, str)
9fa36617 1736 cpp_reader *pfile;
8d7a2585 1737 const char *str;
9fa36617 1738{
0578f103 1739 char *buf, *p;
1740 size_t count;
1741
b1a9ff83 1742 /* Copy the entire option so we can modify it.
0578f103 1743 Change the first "=" in the string to a space. If there is none,
90dc47e0 1744 tack " 1" on the end. */
1745
90dc47e0 1746 count = strlen (str);
435fb09b 1747 buf = (char *) alloca (count + 3);
90dc47e0 1748 memcpy (buf, str, count);
1749
1750 p = strchr (str, '=');
0578f103 1751 if (p)
90dc47e0 1752 buf[p - str] = ' ';
0578f103 1753 else
1754 {
90dc47e0 1755 buf[count++] = ' ';
1756 buf[count++] = '1';
9fa36617 1757 }
435fb09b 1758 buf[count] = '\0';
eb3948d1 1759
c808d026 1760 run_directive (pfile, T_DEFINE, buf, count);
a336277c 1761}
1762
f0c20935 1763/* Slight variant of the above for use by initialize_builtins. */
a336277c 1764void
1765_cpp_define_builtin (pfile, str)
1766 cpp_reader *pfile;
1767 const char *str;
1768{
c808d026 1769 run_directive (pfile, T_DEFINE, str, strlen (str));
0578f103 1770}
a92771b8 1771
0578f103 1772/* Process MACRO as if it appeared as the body of an #undef. */
1773void
1774cpp_undef (pfile, macro)
1d6fa33f 1775 cpp_reader *pfile;
0578f103 1776 const char *macro;
1d6fa33f 1777{
c808d026 1778 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1d6fa33f 1779}
1780
2c0e001b 1781/* Process the string STR as if it appeared as the body of a #assert. */
0578f103 1782void
1783cpp_assert (pfile, str)
1784 cpp_reader *pfile;
1785 const char *str;
1786{
90dc47e0 1787 handle_assertion (pfile, str, T_ASSERT);
0578f103 1788}
1d6fa33f 1789
2c0e001b 1790/* Process STR as if it appeared as the body of an #unassert. */
0578f103 1791void
1792cpp_unassert (pfile, str)
1d6fa33f 1793 cpp_reader *pfile;
0578f103 1794 const char *str;
1d6fa33f 1795{
90dc47e0 1796 handle_assertion (pfile, str, T_UNASSERT);
b1a9ff83 1797}
e3630c4b 1798
90dc47e0 1799/* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1800static void
1801handle_assertion (pfile, str, type)
1802 cpp_reader *pfile;
1803 const char *str;
1804 int type;
1805{
1806 size_t count = strlen (str);
1807 const char *p = strchr (str, '=');
1808
1809 if (p)
1810 {
1811 /* Copy the entire option so we can modify it. Change the first
1812 "=" in the string to a '(', and tack a ')' on the end. */
435fb09b 1813 char *buf = (char *) alloca (count + 2);
90dc47e0 1814
1815 memcpy (buf, str, count);
1816 buf[p - str] = '(';
1817 buf[count++] = ')';
435fb09b 1818 buf[count] = '\0';
90dc47e0 1819 str = buf;
1820 }
1821
c808d026 1822 run_directive (pfile, type, str, count);
90dc47e0 1823}
1824
72b04ca4 1825/* The number of errors for a given reader. */
1826unsigned int
1827cpp_errors (pfile)
1828 cpp_reader *pfile;
1829{
1830 return pfile->errors;
1831}
1832
1833/* The options structure. */
1834cpp_options *
1835cpp_get_options (pfile)
1836 cpp_reader *pfile;
1837{
1838 return &pfile->opts;
1839}
1840
1841/* The callbacks structure. */
1842cpp_callbacks *
1843cpp_get_callbacks (pfile)
1844 cpp_reader *pfile;
1845{
1846 return &pfile->cb;
1847}
1848
38692459 1849/* The line map set. */
f85fcf2b 1850const struct line_maps *
38692459 1851cpp_get_line_maps (pfile)
1852 cpp_reader *pfile;
1853{
1854 return &pfile->line_maps;
1855}
1856
72b04ca4 1857/* Copy the given callbacks structure to our own. */
1858void
1859cpp_set_callbacks (pfile, cb)
1860 cpp_reader *pfile;
1861 cpp_callbacks *cb;
1862{
1863 pfile->cb = *cb;
1864}
1865
4087613d 1866/* Push a new buffer on the buffer stack. Returns the new buffer; it
1867 doesn't fail. It does not generate a file change call back; that
1868 is the responsibility of the caller. */
f51c2148 1869cpp_buffer *
c808d026 1870cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
f51c2148 1871 cpp_reader *pfile;
b6d18b0a 1872 const uchar *buffer;
c4a0fee9 1873 size_t len;
c808d026 1874 int from_stage3;
4dfe8b74 1875 int return_at_eof;
f51c2148 1876{
0d086e18 1877 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
79bd622b 1878
bd507c05 1879 /* Clears, amongst other things, if_stack and mi_cmacro. */
1880 memset (new, 0, sizeof (cpp_buffer));
1881
1882 new->line_base = new->buf = new->cur = buffer;
1883 new->rlimit = buffer + len;
ca52ef60 1884 new->from_stage3 = from_stage3 || CPP_OPTION (pfile, traditional);
c4a0fee9 1885 new->prev = pfile->buffer;
4dfe8b74 1886 new->return_at_eof = return_at_eof;
fb5ab82c 1887 new->saved_flags = BOL;
6cee4464 1888
c4a0fee9 1889 pfile->buffer = new;
6cee4464 1890
f51c2148 1891 return new;
1892}
1893
02516fb9 1894/* Pops a single buffer, with a file change call-back if appropriate.
1895 Then pushes the next -include file, if any remain. */
4dfe8b74 1896void
1897_cpp_pop_buffer (pfile)
f51c2148 1898 cpp_reader *pfile;
1899{
bd507c05 1900 cpp_buffer *buffer = pfile->buffer;
02516fb9 1901 struct include_file *inc = buffer->inc;
f0c20935 1902 struct if_stack *ifs;
f51c2148 1903
bd507c05 1904 /* Walk back up the conditional stack till we reach its level at
1905 entry to this file, issuing error messages. */
1906 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
73328dce 1907 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
bd507c05 1908 "unterminated #%s", dtable[ifs->type].name);
4087613d 1909
5621a364 1910 /* In case of a missing #endif. */
1ea7ed21 1911 pfile->state.skipping = 0;
c808d026 1912
02516fb9 1913 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
c808d026 1914 pfile->buffer = buffer->prev;
1915
02516fb9 1916 /* Free the buffer object now; we may want to push a new buffer
1917 in _cpp_push_next_include_file. */
1918 obstack_free (&pfile->buffer_ob, buffer);
c808d026 1919
02516fb9 1920 if (inc)
1921 {
1922 _cpp_pop_file_buffer (pfile, inc);
1923
1924 /* Don't generate a callback for popping the main file. */
1925 if (pfile->buffer)
1926 {
1927 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1928
1929 /* If this is the main file, there may be some -include
1930 files left to push. */
1931 if (!pfile->buffer->prev)
1932 _cpp_maybe_push_include_file (pfile);
1933 }
1934 }
f51c2148 1935}
1936
e484a1cc 1937/* Enter all recognised directives in the hash table. */
f51c2148 1938void
0d086e18 1939_cpp_init_directives (pfile)
f51c2148 1940 cpp_reader *pfile;
1941{
87f5a8f0 1942 unsigned int i;
79bd622b 1943 cpp_hashnode *node;
76faa4c0 1944
ee6c4e4b 1945 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
79bd622b 1946 {
87f5a8f0 1947 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1948 node->directive_index = i + 1;
79bd622b 1949 }
f51c2148 1950}