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