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