* rtl.h (struct rtx_def): Update comments.
[official-gcc.git] / gcc / cpplib.c
blobc90224c0b0a8f626bb3ade236a0d001128ae9b5e
1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "obstack.h"
29 /* Chained list of answers to an assertion. */
30 struct answer
32 struct answer *next;
33 unsigned int count;
34 cpp_token first[1];
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
39 struct if_stack
41 struct if_stack *next;
42 unsigned int line; /* Line where condition started. */
43 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
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. */
49 /* Contains a registered pragma or pragma namespace. */
50 typedef void (*pragma_cb) PARAMS ((cpp_reader *));
51 struct pragma_entry
53 struct pragma_entry *next;
54 const cpp_hashnode *pragma; /* Name and length. */
55 int is_nspace;
56 union {
57 pragma_cb handler;
58 struct pragma_entry *space;
59 } u;
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
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)
79 /* Defines one #-directive, including how to handle it. */
80 typedef void (*directive_handler) PARAMS ((cpp_reader *));
81 typedef struct directive directive;
82 struct directive
84 directive_handler handler; /* Function to handle directive. */
85 const uchar *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. */
91 /* Forward declarations. */
93 static void skip_rest_of_line PARAMS ((cpp_reader *));
94 static void check_eol PARAMS ((cpp_reader *));
95 static void start_directive PARAMS ((cpp_reader *));
96 static void end_directive PARAMS ((cpp_reader *, int));
97 static void directive_diagnostics
98 PARAMS ((cpp_reader *, const directive *, int));
99 static void run_directive PARAMS ((cpp_reader *, int,
100 const char *, size_t));
101 static const cpp_token *glue_header_name PARAMS ((cpp_reader *));
102 static const cpp_token *parse_include PARAMS ((cpp_reader *));
103 static void push_conditional PARAMS ((cpp_reader *, int, int,
104 const cpp_hashnode *));
105 static unsigned int read_flag PARAMS ((cpp_reader *, unsigned int));
106 static uchar *dequote_string PARAMS ((cpp_reader *, const uchar *,
107 unsigned int));
108 static int strtoul_for_line PARAMS ((const uchar *, unsigned int,
109 unsigned long *));
110 static void do_diagnostic PARAMS ((cpp_reader *, int, int));
111 static cpp_hashnode *lex_macro_node PARAMS ((cpp_reader *));
112 static void do_include_common PARAMS ((cpp_reader *, enum include_type));
113 static struct pragma_entry *lookup_pragma_entry
114 PARAMS ((struct pragma_entry *, const cpp_hashnode *pragma));
115 static struct pragma_entry *insert_pragma_entry
116 PARAMS ((cpp_reader *, struct pragma_entry **, const cpp_hashnode *,
117 pragma_cb));
118 static void do_pragma_once PARAMS ((cpp_reader *));
119 static void do_pragma_poison PARAMS ((cpp_reader *));
120 static void do_pragma_system_header PARAMS ((cpp_reader *));
121 static void do_pragma_dependency PARAMS ((cpp_reader *));
122 static void do_linemarker PARAMS ((cpp_reader *));
123 static const cpp_token *get_token_no_padding PARAMS ((cpp_reader *));
124 static const cpp_token *get__Pragma_string PARAMS ((cpp_reader *));
125 static void destringize_and_run PARAMS ((cpp_reader *, const cpp_string *));
126 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
127 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
128 int));
129 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
130 const struct answer *));
131 static void handle_assertion PARAMS ((cpp_reader *, const char *, int));
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
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. */
142 #define DIRECTIVE_TABLE \
143 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
144 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
145 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
146 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
147 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
148 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
149 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
150 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
151 D(line, T_LINE, KANDR, 0) /* 2465 */ \
152 D(elif, T_ELIF, STDC89, COND) /* 610 */ \
153 D(error, T_ERROR, STDC89, 0) /* 475 */ \
154 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
155 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
156 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
157 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
158 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
159 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
160 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
161 SCCS_ENTRY /* 0 SVR4? */
163 /* #sccs is not always recognized. */
164 #ifdef SCCS_DIRECTIVE
165 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
166 #else
167 # define SCCS_ENTRY /* nothing */
168 #endif
170 /* Use the table to generate a series of prototypes, an enum for the
171 directive names, and an array of directive handlers. */
173 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
174 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
175 DIRECTIVE_TABLE
176 #undef D
178 #define D(n, tag, o, f) tag,
179 enum
181 DIRECTIVE_TABLE
182 N_DIRECTIVES
184 #undef D
186 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
187 #define D(name, t, origin, flags) \
188 { CONCAT2(do_,name), (const uchar *) STRINGX(name), \
189 sizeof STRINGX(name) - 1, origin, flags },
190 static const directive dtable[] =
192 DIRECTIVE_TABLE
194 #undef D
195 #undef DIRECTIVE_TABLE
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. */
200 static const directive linemarker_dir =
202 do_linemarker, U"#", 1, KANDR, IN_I
205 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
207 /* Skip any remaining tokens in a directive. */
208 static void
209 skip_rest_of_line (pfile)
210 cpp_reader *pfile;
212 /* Discard all stacked contexts. */
213 while (pfile->context != &pfile->base_context)
214 _cpp_pop_context (pfile);
216 /* Sweep up all tokens remaining on the line. */
217 if (! SEEN_EOL ())
218 while (_cpp_lex_token (pfile)->type != CPP_EOF)
222 /* Ensure there are no stray tokens at the end of a directive. */
223 static void
224 check_eol (pfile)
225 cpp_reader *pfile;
227 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
228 cpp_error (pfile, DL_PEDWARN, "extra tokens at end of #%s directive",
229 pfile->directive->name);
232 /* Called when entering a directive, _Pragma or command-line directive. */
233 static void
234 start_directive (pfile)
235 cpp_reader *pfile;
237 /* Setup in-directive state. */
238 pfile->state.in_directive = 1;
239 pfile->state.save_comments = 0;
241 /* Some handlers need the position of the # for diagnostics. */
242 pfile->directive_line = pfile->line;
245 /* Called when leaving a directive, _Pragma or command-line directive. */
246 static void
247 end_directive (pfile, skip_line)
248 cpp_reader *pfile;
249 int skip_line;
251 /* We don't skip for an assembler #. */
252 if (skip_line)
254 skip_rest_of_line (pfile);
255 if (!pfile->keep_tokens)
257 pfile->cur_run = &pfile->base_run;
258 pfile->cur_token = pfile->base_run.base;
262 /* Restore state. */
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;
269 /* Output diagnostics for a directive DIR. INDENTED is non-zero if
270 the '#' was indented. */
271 static void
272 directive_diagnostics (pfile, dir, indented)
273 cpp_reader *pfile;
274 const directive *dir;
275 int indented;
277 /* Issue -pedantic warnings for extensions. */
278 if (CPP_PEDANTIC (pfile)
279 && ! pfile->state.skipping
280 && dir->origin == EXTENSION)
281 cpp_error (pfile, DL_PEDWARN, "#%s is a GCC extension", dir->name);
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))
291 if (dir == &dtable[T_ELIF])
292 cpp_error (pfile, DL_WARNING,
293 "suggest not using #elif in traditional C");
294 else if (indented && dir->origin == KANDR)
295 cpp_error (pfile, DL_WARNING,
296 "traditional C ignores #%s with the # indented",
297 dir->name);
298 else if (!indented && dir->origin != KANDR)
299 cpp_error (pfile, DL_WARNING,
300 "suggest hiding #%s from traditional C with an indented #",
301 dir->name);
305 /* Check if we have a known directive. INDENTED is non-zero if the
306 '#' of the directive was indented. This function is in this file
307 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
308 non-zero if the line of tokens has been handled, zero if we should
309 continue processing the line. */
311 _cpp_handle_directive (pfile, indented)
312 cpp_reader *pfile;
313 int indented;
315 const directive *dir = 0;
316 const cpp_token *dname;
317 bool was_parsing_args = pfile->state.parsing_args;
318 int skip = 1;
320 if (was_parsing_args)
322 if (CPP_OPTION (pfile, pedantic))
323 cpp_error (pfile, DL_PEDWARN,
324 "embedding a directive within macro arguments is not portable");
325 pfile->state.parsing_args = 0;
326 pfile->state.prevent_expansion = 0;
328 start_directive (pfile);
329 dname = _cpp_lex_token (pfile);
331 if (dname->type == CPP_NAME)
333 if (dname->val.node->directive_index)
334 dir = &dtable[dname->val.node->directive_index - 1];
336 /* We do not recognise the # followed by a number extension in
337 assembler code. */
338 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
340 dir = &linemarker_dir;
341 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
342 && ! pfile->state.skipping)
343 cpp_error (pfile, DL_PEDWARN,
344 "style of line directive is a GCC extension");
347 if (dir)
349 /* If we have a directive that is not an opening conditional,
350 invalidate any control macro. */
351 if (! (dir->flags & IF_COND))
352 pfile->mi_valid = false;
354 /* Kluge alert. In order to be sure that code like this
356 #define HASH #
357 HASH define foo bar
359 does not cause '#define foo bar' to get executed when
360 compiled with -save-temps, we recognize directives in
361 -fpreprocessed mode only if the # is in column 1. cppmacro.c
362 puts a space in front of any '#' at the start of a macro. */
363 if (CPP_OPTION (pfile, preprocessed)
364 && (indented || !(dir->flags & IN_I)))
366 skip = 0;
367 dir = 0;
369 else
371 /* In failed conditional groups, all non-conditional
372 directives are ignored. Before doing that, whether
373 skipping or not, we should lex angle-bracketed headers
374 correctly, and maybe output some diagnostics. */
375 pfile->state.angled_headers = dir->flags & INCL;
376 if (! CPP_OPTION (pfile, preprocessed))
377 directive_diagnostics (pfile, dir, indented);
378 if (pfile->state.skipping && !(dir->flags & COND))
379 dir = 0;
382 else if (dname->type == CPP_EOF)
383 ; /* CPP_EOF is the "null directive". */
384 else
386 /* An unknown directive. Don't complain about it in assembly
387 source: we don't know where the comments are, and # may
388 introduce assembler pseudo-ops. Don't complain about invalid
389 directives in skipped conditional groups (6.10 p4). */
390 if (CPP_OPTION (pfile, lang) == CLK_ASM)
391 skip = 0;
392 else if (!pfile->state.skipping)
393 cpp_error (pfile, DL_ERROR, "invalid preprocessing directive #%s",
394 cpp_token_as_text (pfile, dname));
397 if (dir)
399 /* If we are processing a `#define' directive and we have been
400 requested to expand comments into macros, then re-enable
401 saving of comments. */
402 if (dir == &dtable[T_DEFINE])
403 pfile->state.save_comments =
404 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
406 pfile->directive = dir;
407 (*pfile->directive->handler) (pfile);
409 else if (skip == 0)
410 _cpp_backup_tokens (pfile, 1);
412 end_directive (pfile, skip);
413 if (was_parsing_args)
415 /* Restore state when within macro args. */
416 pfile->state.parsing_args = 2;
417 pfile->state.prevent_expansion = 1;
418 pfile->buffer->saved_flags |= PREV_WHITE;
420 return skip;
423 /* Directive handler wrapper used by the command line option
424 processor. */
425 static void
426 run_directive (pfile, dir_no, buf, count)
427 cpp_reader *pfile;
428 int dir_no;
429 const char *buf;
430 size_t count;
432 cpp_push_buffer (pfile, (const uchar *) buf, count,
433 /* from_stage3 */ true, 1);
434 start_directive (pfile);
435 /* We don't want a leading # to be interpreted as a directive. */
436 pfile->buffer->saved_flags = 0;
437 pfile->directive = &dtable[dir_no];
438 (void) (*pfile->directive->handler) (pfile);
439 end_directive (pfile, 1);
440 _cpp_pop_buffer (pfile);
443 /* Checks for validity the macro name in #define, #undef, #ifdef and
444 #ifndef directives. */
445 static cpp_hashnode *
446 lex_macro_node (pfile)
447 cpp_reader *pfile;
449 cpp_hashnode *node;
450 const cpp_token *token = _cpp_lex_token (pfile);
452 /* The token immediately after #define must be an identifier. That
453 identifier may not be "defined", per C99 6.10.8p4.
454 In C++, it may not be any of the "named operators" either,
455 per C++98 [lex.digraph], [lex.key].
456 Finally, the identifier may not have been poisoned. (In that case
457 the lexer has issued the error message for us.)
459 Note that if we're copying comments into macro expansions, we
460 could encounter comment tokens here, so eat them all up first. */
462 if (! CPP_OPTION (pfile, discard_comments_in_macro_exp))
464 while (token->type == CPP_COMMENT)
465 token = _cpp_lex_token (pfile);
468 if (token->type != CPP_NAME)
470 if (token->type == CPP_EOF)
471 cpp_error (pfile, DL_ERROR, "no macro name given in #%s directive",
472 pfile->directive->name);
473 else if (token->flags & NAMED_OP)
474 cpp_error (pfile, DL_ERROR,
475 "\"%s\" cannot be used as a macro name as it is an operator in C++",
476 NODE_NAME (token->val.node));
477 else
478 cpp_error (pfile, DL_ERROR, "macro names must be identifiers");
480 return 0;
483 node = token->val.node;
484 if (node->flags & NODE_POISONED)
485 return 0;
487 if (node == pfile->spec_nodes.n_defined)
489 cpp_error (pfile, DL_ERROR, "\"%s\" cannot be used as a macro name",
490 NODE_NAME (node));
491 return 0;
494 return node;
497 /* Process a #define directive. Most work is done in cppmacro.c. */
498 static void
499 do_define (pfile)
500 cpp_reader *pfile;
502 cpp_hashnode *node = lex_macro_node (pfile);
504 if (node)
506 if (_cpp_create_definition (pfile, node))
507 if (pfile->cb.define)
508 (*pfile->cb.define) (pfile, pfile->directive_line, node);
512 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
513 static void
514 do_undef (pfile)
515 cpp_reader *pfile;
517 cpp_hashnode *node = lex_macro_node (pfile);
519 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
520 is not currently defined as a macro name. */
521 if (node && node->type == NT_MACRO)
523 if (pfile->cb.undef)
524 (*pfile->cb.undef) (pfile, pfile->directive_line, node);
526 if (node->flags & NODE_WARN)
527 cpp_error (pfile, DL_WARNING, "undefining \"%s\"", NODE_NAME (node));
529 _cpp_free_definition (node);
531 check_eol (pfile);
534 /* Helper routine used by parse_include. Reinterpret the current line
535 as an h-char-sequence (< ... >); we are looking at the first token
536 after the <. Returns the header as a token, or NULL on failure. */
537 static const cpp_token *
538 glue_header_name (pfile)
539 cpp_reader *pfile;
541 cpp_token *header = NULL;
542 const cpp_token *token;
543 unsigned char *buffer;
544 size_t len, total_len = 0, capacity = 1024;
546 /* To avoid lexed tokens overwriting our glued name, we can only
547 allocate from the string pool once we've lexed everything. */
548 buffer = (unsigned char *) xmalloc (capacity);
549 for (;;)
551 token = cpp_get_token (pfile);
553 if (token->type == CPP_GREATER || token->type == CPP_EOF)
554 break;
556 len = cpp_token_len (token);
557 if (total_len + len > capacity)
559 capacity = (capacity + len) * 2;
560 buffer = (unsigned char *) xrealloc (buffer, capacity);
563 if (token->flags & PREV_WHITE)
564 buffer[total_len++] = ' ';
566 total_len = cpp_spell_token (pfile, token, &buffer[total_len]) - buffer;
569 if (token->type == CPP_EOF)
570 cpp_error (pfile, DL_ERROR, "missing terminating > character");
571 else
573 unsigned char *token_mem = _cpp_unaligned_alloc (pfile, total_len + 1);
574 memcpy (token_mem, buffer, total_len);
575 token_mem[total_len] = '\0';
577 header = _cpp_temp_token (pfile);
578 header->type = CPP_HEADER_NAME;
579 header->flags = 0;
580 header->val.str.len = total_len;
581 header->val.str.text = token_mem;
584 free ((PTR) buffer);
585 return header;
588 /* Returns the header string of #include, #include_next, #import and
589 #pragma dependency. Returns NULL on error. */
590 static const cpp_token *
591 parse_include (pfile)
592 cpp_reader *pfile;
594 const unsigned char *dir;
595 const cpp_token *header;
597 if (pfile->directive == &dtable[T_PRAGMA])
598 dir = U"pragma dependency";
599 else
600 dir = pfile->directive->name;
602 /* Allow macro expansion. */
603 header = cpp_get_token (pfile);
604 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
606 if (header->type != CPP_LESS)
608 cpp_error (pfile, DL_ERROR,
609 "#%s expects \"FILENAME\" or <FILENAME>", dir);
610 return NULL;
613 header = glue_header_name (pfile);
614 if (header == NULL)
615 return header;
618 if (header->val.str.len == 0)
620 cpp_error (pfile, DL_ERROR, "empty file name in #%s", dir);
621 return NULL;
624 return header;
627 /* Handle #include, #include_next and #import. */
628 static void
629 do_include_common (pfile, type)
630 cpp_reader *pfile;
631 enum include_type type;
633 const cpp_token *header;
635 /* For #include_next, if this is the primary source file, warn and
636 use the normal search logic. */
637 if (type == IT_INCLUDE_NEXT && ! pfile->buffer->prev)
639 cpp_error (pfile, DL_WARNING, "#include_next in primary source file");
640 type = IT_INCLUDE;
642 else if (type == IT_IMPORT && CPP_OPTION (pfile, warn_import))
644 CPP_OPTION (pfile, warn_import) = 0;
645 cpp_error (pfile, DL_WARNING,
646 "#import is obsolete, use an #ifndef wrapper in the header file");
649 header = parse_include (pfile);
650 if (header)
652 /* Prevent #include recursion. */
653 if (pfile->line_maps.depth >= CPP_STACK_MAX)
654 cpp_error (pfile, DL_FATAL, "#include nested too deeply");
655 else
657 check_eol (pfile);
658 /* Get out of macro context, if we are. */
659 skip_rest_of_line (pfile);
660 if (pfile->cb.include)
661 (*pfile->cb.include) (pfile, pfile->directive_line,
662 pfile->directive->name, header);
664 _cpp_execute_include (pfile, header, type);
669 static void
670 do_include (pfile)
671 cpp_reader *pfile;
673 do_include_common (pfile, IT_INCLUDE);
676 static void
677 do_import (pfile)
678 cpp_reader *pfile;
680 do_include_common (pfile, IT_IMPORT);
683 static void
684 do_include_next (pfile)
685 cpp_reader *pfile;
687 do_include_common (pfile, IT_INCLUDE_NEXT);
690 /* Subroutine of do_linemarker. Read possible flags after file name.
691 LAST is the last flag seen; 0 if this is the first flag. Return the
692 flag if it is valid, 0 at the end of the directive. Otherwise
693 complain. */
694 static unsigned int
695 read_flag (pfile, last)
696 cpp_reader *pfile;
697 unsigned int last;
699 const cpp_token *token = _cpp_lex_token (pfile);
701 if (token->type == CPP_NUMBER && token->val.str.len == 1)
703 unsigned int flag = token->val.str.text[0] - '0';
705 if (flag > last && flag <= 4
706 && (flag != 4 || last == 3)
707 && (flag != 2 || last == 0))
708 return flag;
711 if (token->type != CPP_EOF)
712 cpp_error (pfile, DL_ERROR, "invalid flag \"%s\" in line directive",
713 cpp_token_as_text (pfile, token));
714 return 0;
717 /* Subroutine of do_line and do_linemarker. Returns a version of STR
718 which has a NUL terminator and all escape sequences converted to
719 their equivalents. Temporary, hopefully. */
720 static uchar *
721 dequote_string (pfile, str, len)
722 cpp_reader *pfile;
723 const uchar *str;
724 unsigned int len;
726 uchar *result = _cpp_unaligned_alloc (pfile, len + 1);
727 uchar *dst = result;
728 const uchar *limit = str + len;
729 cppchar_t c;
731 while (str < limit)
733 c = *str++;
734 if (c != '\\')
735 *dst++ = c;
736 else
737 *dst++ = cpp_parse_escape (pfile, &str, limit, 0);
739 *dst++ = '\0';
740 return result;
743 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
744 of length LEN, to binary; store it in NUMP, and return 0 if the
745 number was well-formed, 1 if not. Temporary, hopefully. */
746 static int
747 strtoul_for_line (str, len, nump)
748 const uchar *str;
749 unsigned int len;
750 unsigned long *nump;
752 unsigned long reg = 0;
753 uchar c;
754 while (len--)
756 c = *str++;
757 if (!ISDIGIT (c))
758 return 1;
759 reg *= 10;
760 reg += c - '0';
762 *nump = reg;
763 return 0;
766 /* Interpret #line command.
767 Note that the filename string (if any) is a true string constant
768 (escapes are interpreted), unlike in #line. */
769 static void
770 do_line (pfile)
771 cpp_reader *pfile;
773 const cpp_token *token;
774 const char *new_file = pfile->map->to_file;
775 unsigned long new_lineno;
777 /* C99 raised the minimum limit on #line numbers. */
778 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
780 /* #line commands expand macros. */
781 token = cpp_get_token (pfile);
782 if (token->type != CPP_NUMBER
783 || strtoul_for_line (token->val.str.text, token->val.str.len,
784 &new_lineno))
786 cpp_error (pfile, DL_ERROR,
787 "\"%s\" after #line is not a positive integer",
788 cpp_token_as_text (pfile, token));
789 return;
792 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
793 cpp_error (pfile, DL_PEDWARN, "line number out of range");
795 token = cpp_get_token (pfile);
796 if (token->type == CPP_STRING)
798 new_file = (const char *) dequote_string (pfile, token->val.str.text,
799 token->val.str.len);
800 check_eol (pfile);
802 else if (token->type != CPP_EOF)
804 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
805 cpp_token_as_text (pfile, token));
806 return;
809 skip_rest_of_line (pfile);
810 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
811 pfile->map->sysp);
814 /* Interpret the # 44 "file" [flags] notation, which has slightly
815 different syntax and semantics from #line: Flags are allowed,
816 and we never complain about the line number being too big. */
817 static void
818 do_linemarker (pfile)
819 cpp_reader *pfile;
821 const cpp_token *token;
822 const char *new_file = pfile->map->to_file;
823 unsigned long new_lineno;
824 unsigned int new_sysp = pfile->map->sysp;
825 enum lc_reason reason = LC_RENAME;
826 int flag;
828 /* Back up so we can get the number again. Putting this in
829 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
830 some circumstances, which can segfault. */
831 _cpp_backup_tokens (pfile, 1);
833 /* #line commands expand macros. */
834 token = cpp_get_token (pfile);
835 if (token->type != CPP_NUMBER
836 || strtoul_for_line (token->val.str.text, token->val.str.len,
837 &new_lineno))
839 cpp_error (pfile, DL_ERROR, "\"%s\" after # is not a positive integer",
840 cpp_token_as_text (pfile, token));
841 return;
844 token = cpp_get_token (pfile);
845 if (token->type == CPP_STRING)
847 new_file = (const char *) dequote_string (pfile, token->val.str.text,
848 token->val.str.len);
849 new_sysp = 0;
850 flag = read_flag (pfile, 0);
851 if (flag == 1)
853 reason = LC_ENTER;
854 /* Fake an include for cpp_included (). */
855 _cpp_fake_include (pfile, new_file);
856 flag = read_flag (pfile, flag);
858 else if (flag == 2)
860 reason = LC_LEAVE;
861 flag = read_flag (pfile, flag);
863 if (flag == 3)
865 new_sysp = 1;
866 flag = read_flag (pfile, flag);
867 if (flag == 4)
868 new_sysp = 2;
871 check_eol (pfile);
873 else if (token->type != CPP_EOF)
875 cpp_error (pfile, DL_ERROR, "\"%s\" is not a valid filename",
876 cpp_token_as_text (pfile, token));
877 return;
880 skip_rest_of_line (pfile);
881 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
884 /* Arrange the file_change callback. pfile->line has changed to
885 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
886 header, 2 for a system header that needs to be extern "C" protected,
887 and zero otherwise. */
888 void
889 _cpp_do_file_change (pfile, reason, to_file, file_line, sysp)
890 cpp_reader *pfile;
891 enum lc_reason reason;
892 const char *to_file;
893 unsigned int file_line;
894 unsigned int sysp;
896 pfile->map = add_line_map (&pfile->line_maps, reason, sysp,
897 pfile->line, to_file, file_line);
899 if (pfile->cb.file_change)
900 (*pfile->cb.file_change) (pfile, pfile->map);
903 /* Report a warning or error detected by the program we are
904 processing. Use the directive's tokens in the error message. */
905 static void
906 do_diagnostic (pfile, code, print_dir)
907 cpp_reader *pfile;
908 int code;
909 int print_dir;
911 if (_cpp_begin_message (pfile, code,
912 pfile->cur_token[-1].line,
913 pfile->cur_token[-1].col))
915 if (print_dir)
916 fprintf (stderr, "#%s ", pfile->directive->name);
917 pfile->state.prevent_expansion++;
918 cpp_output_line (pfile, stderr);
919 pfile->state.prevent_expansion--;
923 static void
924 do_error (pfile)
925 cpp_reader *pfile;
927 do_diagnostic (pfile, DL_ERROR, 1);
930 static void
931 do_warning (pfile)
932 cpp_reader *pfile;
934 /* We want #warning diagnostics to be emitted in system headers too. */
935 do_diagnostic (pfile, DL_WARNING_SYSHDR, 1);
938 /* Report program identification. */
939 static void
940 do_ident (pfile)
941 cpp_reader *pfile;
943 const cpp_token *str = cpp_get_token (pfile);
945 if (str->type != CPP_STRING)
946 cpp_error (pfile, DL_ERROR, "invalid #ident directive");
947 else if (pfile->cb.ident)
948 (*pfile->cb.ident) (pfile, pfile->directive_line, &str->val.str);
950 check_eol (pfile);
953 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
954 matching entry, or NULL if none is found. The returned entry could
955 be the start of a namespace chain, or a pragma. */
956 static struct pragma_entry *
957 lookup_pragma_entry (chain, pragma)
958 struct pragma_entry *chain;
959 const cpp_hashnode *pragma;
961 while (chain && chain->pragma != pragma)
962 chain = chain->next;
964 return chain;
967 /* Create and insert a pragma entry for NAME at the beginning of a
968 singly-linked CHAIN. If handler is NULL, it is a namespace,
969 otherwise it is a pragma and its handler. */
970 static struct pragma_entry *
971 insert_pragma_entry (pfile, chain, pragma, handler)
972 cpp_reader *pfile;
973 struct pragma_entry **chain;
974 const cpp_hashnode *pragma;
975 pragma_cb handler;
977 struct pragma_entry *new;
979 new = (struct pragma_entry *)
980 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
981 new->pragma = pragma;
982 if (handler)
984 new->is_nspace = 0;
985 new->u.handler = handler;
987 else
989 new->is_nspace = 1;
990 new->u.space = NULL;
993 new->next = *chain;
994 *chain = new;
995 return new;
998 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
999 goes in the global namespace. HANDLER is the handler it will call,
1000 which must be non-NULL. */
1001 void
1002 cpp_register_pragma (pfile, space, name, handler)
1003 cpp_reader *pfile;
1004 const char *space;
1005 const char *name;
1006 pragma_cb handler;
1008 struct pragma_entry **chain = &pfile->pragmas;
1009 struct pragma_entry *entry;
1010 const cpp_hashnode *node;
1012 if (!handler)
1013 abort ();
1015 if (space)
1017 node = cpp_lookup (pfile, U space, strlen (space));
1018 entry = lookup_pragma_entry (*chain, node);
1019 if (!entry)
1020 entry = insert_pragma_entry (pfile, chain, node, NULL);
1021 else if (!entry->is_nspace)
1022 goto clash;
1023 chain = &entry->u.space;
1026 /* Check for duplicates. */
1027 node = cpp_lookup (pfile, U name, strlen (name));
1028 entry = lookup_pragma_entry (*chain, node);
1029 if (entry)
1031 if (entry->is_nspace)
1032 clash:
1033 cpp_error (pfile, DL_ICE,
1034 "registering \"%s\" as both a pragma and a pragma namespace",
1035 NODE_NAME (node));
1036 else if (space)
1037 cpp_error (pfile, DL_ICE, "#pragma %s %s is already registered",
1038 space, name);
1039 else
1040 cpp_error (pfile, DL_ICE, "#pragma %s is already registered", name);
1042 else
1043 insert_pragma_entry (pfile, chain, node, handler);
1046 /* Register the pragmas the preprocessor itself handles. */
1047 void
1048 _cpp_init_internal_pragmas (pfile)
1049 cpp_reader *pfile;
1051 /* Pragmas in the global namespace. */
1052 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
1053 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1055 /* New GCC-specific pragmas should be put in the GCC namespace. */
1056 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1057 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1058 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1061 /* Pragmata handling. We handle some, and pass the rest on to the
1062 front end. C99 defines three pragmas and says that no macro
1063 expansion is to be performed on them; whether or not macro
1064 expansion happens for other pragmas is implementation defined.
1065 This implementation never macro-expands the text after #pragma. */
1066 static void
1067 do_pragma (pfile)
1068 cpp_reader *pfile;
1070 const struct pragma_entry *p = NULL;
1071 const cpp_token *token;
1072 unsigned int count = 1;
1074 pfile->state.prevent_expansion++;
1076 token = cpp_get_token (pfile);
1077 if (token->type == CPP_NAME)
1079 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1080 if (p && p->is_nspace)
1082 count = 2;
1083 token = cpp_get_token (pfile);
1084 if (token->type == CPP_NAME)
1085 p = lookup_pragma_entry (p->u.space, token->val.node);
1086 else
1087 p = NULL;
1091 /* FIXME. This is an awful kludge to get the front ends to update
1092 their notion of line number for diagnostic purposes. The line
1093 number should be passed to the handler and they should do it
1094 themselves. Stand-alone CPP must ignore us, otherwise it will
1095 prefix the directive with spaces, hence the 1. Ugh. */
1096 if (pfile->cb.line_change)
1097 (*pfile->cb.line_change)(pfile, token, 1);
1099 if (p)
1100 (*p->u.handler) (pfile);
1101 else if (pfile->cb.def_pragma)
1103 _cpp_backup_tokens (pfile, count);
1104 (*pfile->cb.def_pragma) (pfile, pfile->directive_line);
1107 pfile->state.prevent_expansion--;
1110 /* Handle #pragma once. */
1111 static void
1112 do_pragma_once (pfile)
1113 cpp_reader *pfile;
1115 cpp_error (pfile, DL_WARNING, "#pragma once is obsolete");
1117 if (pfile->buffer->prev == NULL)
1118 cpp_error (pfile, DL_WARNING, "#pragma once in main file");
1119 else
1120 _cpp_never_reread (pfile->buffer->inc);
1122 check_eol (pfile);
1125 /* Handle #pragma poison, to poison one or more identifiers so that
1126 the lexer produces a hard error for each subsequent usage. */
1127 static void
1128 do_pragma_poison (pfile)
1129 cpp_reader *pfile;
1131 const cpp_token *tok;
1132 cpp_hashnode *hp;
1134 pfile->state.poisoned_ok = 1;
1135 for (;;)
1137 tok = _cpp_lex_token (pfile);
1138 if (tok->type == CPP_EOF)
1139 break;
1140 if (tok->type != CPP_NAME)
1142 cpp_error (pfile, DL_ERROR, "invalid #pragma GCC poison directive");
1143 break;
1146 hp = tok->val.node;
1147 if (hp->flags & NODE_POISONED)
1148 continue;
1150 if (hp->type == NT_MACRO)
1151 cpp_error (pfile, DL_WARNING, "poisoning existing macro \"%s\"",
1152 NODE_NAME (hp));
1153 _cpp_free_definition (hp);
1154 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1156 pfile->state.poisoned_ok = 0;
1159 /* Mark the current header as a system header. This will suppress
1160 some categories of warnings (notably those from -pedantic). It is
1161 intended for use in system libraries that cannot be implemented in
1162 conforming C, but cannot be certain that their headers appear in a
1163 system include directory. To prevent abuse, it is rejected in the
1164 primary source file. */
1165 static void
1166 do_pragma_system_header (pfile)
1167 cpp_reader *pfile;
1169 cpp_buffer *buffer = pfile->buffer;
1171 if (buffer->prev == 0)
1172 cpp_error (pfile, DL_WARNING,
1173 "#pragma system_header ignored outside include file");
1174 else
1176 check_eol (pfile);
1177 skip_rest_of_line (pfile);
1178 cpp_make_system_header (pfile, 1, 0);
1182 /* Check the modified date of the current include file against a specified
1183 file. Issue a diagnostic, if the specified file is newer. We use this to
1184 determine if a fixed header should be refixed. */
1185 static void
1186 do_pragma_dependency (pfile)
1187 cpp_reader *pfile;
1189 const cpp_token *header;
1190 int ordering;
1192 header = parse_include (pfile);
1193 if (!header)
1194 return;
1196 ordering = _cpp_compare_file_date (pfile, header);
1197 if (ordering < 0)
1198 cpp_error (pfile, DL_WARNING, "cannot find source %s",
1199 cpp_token_as_text (pfile, header));
1200 else if (ordering > 0)
1202 cpp_error (pfile, DL_WARNING, "current file is older than %s",
1203 cpp_token_as_text (pfile, header));
1204 if (cpp_get_token (pfile)->type != CPP_EOF)
1206 _cpp_backup_tokens (pfile, 1);
1207 do_diagnostic (pfile, DL_WARNING, 0);
1212 /* Get a token but skip padding. */
1213 static const cpp_token *
1214 get_token_no_padding (pfile)
1215 cpp_reader *pfile;
1217 for (;;)
1219 const cpp_token *result = cpp_get_token (pfile);
1220 if (result->type != CPP_PADDING)
1221 return result;
1225 /* Check syntax is "(string-literal)". Returns the string on success,
1226 or NULL on failure. */
1227 static const cpp_token *
1228 get__Pragma_string (pfile)
1229 cpp_reader *pfile;
1231 const cpp_token *string;
1233 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1234 return NULL;
1236 string = get_token_no_padding (pfile);
1237 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1238 return NULL;
1240 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1241 return NULL;
1243 return string;
1246 /* Destringize IN into a temporary buffer, by removing the first \ of
1247 \" and \\ sequences, and process the result as a #pragma directive. */
1248 static void
1249 destringize_and_run (pfile, in)
1250 cpp_reader *pfile;
1251 const cpp_string *in;
1253 const unsigned char *src, *limit;
1254 char *dest, *result;
1256 dest = result = alloca (in->len + 1);
1257 for (src = in->text, limit = src + in->len; src < limit;)
1259 /* We know there is a character following the backslash. */
1260 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1261 src++;
1262 *dest++ = *src++;
1264 *dest = '\0';
1266 run_directive (pfile, T_PRAGMA, result, dest - result);
1269 /* Handle the _Pragma operator. */
1270 void
1271 _cpp_do__Pragma (pfile)
1272 cpp_reader *pfile;
1274 const cpp_token *string = get__Pragma_string (pfile);
1276 if (!string)
1277 cpp_error (pfile, DL_ERROR,
1278 "_Pragma takes a parenthesized string literal");
1279 else
1281 /* Ideally, we'd like
1282 token1 _Pragma ("foo") token2
1283 to be output as
1284 token1
1285 # 7 "file.c"
1286 #pragma foo
1287 # 7 "file.c"
1288 token2
1289 Getting these correct line markers is a little tricky. */
1291 unsigned int orig_line = pfile->line;
1292 destringize_and_run (pfile, &string->val.str);
1293 pfile->line = orig_line;
1294 pfile->buffer->saved_flags = BOL;
1298 /* Just ignore #sccs, on systems where we define it at all. */
1299 #ifdef SCCS_DIRECTIVE
1300 static void
1301 do_sccs (pfile)
1302 cpp_reader *pfile ATTRIBUTE_UNUSED;
1305 #endif
1307 /* Handle #ifdef. */
1308 static void
1309 do_ifdef (pfile)
1310 cpp_reader *pfile;
1312 int skip = 1;
1314 if (! pfile->state.skipping)
1316 const cpp_hashnode *node = lex_macro_node (pfile);
1318 if (node)
1319 skip = node->type != NT_MACRO;
1321 if (node)
1322 check_eol (pfile);
1325 push_conditional (pfile, skip, T_IFDEF, 0);
1328 /* Handle #ifndef. */
1329 static void
1330 do_ifndef (pfile)
1331 cpp_reader *pfile;
1333 int skip = 1;
1334 const cpp_hashnode *node = 0;
1336 if (! pfile->state.skipping)
1338 node = lex_macro_node (pfile);
1339 if (node)
1340 skip = node->type == NT_MACRO;
1342 if (node)
1343 check_eol (pfile);
1346 push_conditional (pfile, skip, T_IFNDEF, node);
1349 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1350 pfile->mi_ind_cmacro so we can handle multiple-include
1351 optimisations. If macro expansion occurs in the expression, we
1352 cannot treat it as a controlling conditional, since the expansion
1353 could change in the future. That is handled by cpp_get_token. */
1354 static void
1355 do_if (pfile)
1356 cpp_reader *pfile;
1358 int skip = 1;
1360 if (! pfile->state.skipping)
1361 skip = _cpp_parse_expr (pfile) == false;
1363 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1366 /* Flip skipping state if appropriate and continue without changing
1367 if_stack; this is so that the error message for missing #endif's
1368 etc. will point to the original #if. */
1369 static void
1370 do_else (pfile)
1371 cpp_reader *pfile;
1373 cpp_buffer *buffer = pfile->buffer;
1374 struct if_stack *ifs = buffer->if_stack;
1376 if (ifs == NULL)
1377 cpp_error (pfile, DL_ERROR, "#else without #if");
1378 else
1380 if (ifs->type == T_ELSE)
1382 cpp_error (pfile, DL_ERROR, "#else after #else");
1383 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1384 "the conditional began here");
1386 ifs->type = T_ELSE;
1388 /* Skip any future (erroneous) #elses or #elifs. */
1389 pfile->state.skipping = ifs->skip_elses;
1390 ifs->skip_elses = true;
1392 /* Invalidate any controlling macro. */
1393 ifs->mi_cmacro = 0;
1395 /* Only check EOL if was not originally skipping. */
1396 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1397 check_eol (pfile);
1401 /* Handle a #elif directive by not changing if_stack either. See the
1402 comment above do_else. */
1403 static void
1404 do_elif (pfile)
1405 cpp_reader *pfile;
1407 cpp_buffer *buffer = pfile->buffer;
1408 struct if_stack *ifs = buffer->if_stack;
1410 if (ifs == NULL)
1411 cpp_error (pfile, DL_ERROR, "#elif without #if");
1412 else
1414 if (ifs->type == T_ELSE)
1416 cpp_error (pfile, DL_ERROR, "#elif after #else");
1417 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1418 "the conditional began here");
1420 ifs->type = T_ELIF;
1422 /* Only evaluate this if we aren't skipping elses. During
1423 evaluation, set skipping to false to get lexer warnings. */
1424 if (ifs->skip_elses)
1425 pfile->state.skipping = 1;
1426 else
1428 pfile->state.skipping = 0;
1429 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1430 ifs->skip_elses = ! pfile->state.skipping;
1433 /* Invalidate any controlling macro. */
1434 ifs->mi_cmacro = 0;
1438 /* #endif pops the if stack and resets pfile->state.skipping. */
1439 static void
1440 do_endif (pfile)
1441 cpp_reader *pfile;
1443 cpp_buffer *buffer = pfile->buffer;
1444 struct if_stack *ifs = buffer->if_stack;
1446 if (ifs == NULL)
1447 cpp_error (pfile, DL_ERROR, "#endif without #if");
1448 else
1450 /* Only check EOL if was not originally skipping. */
1451 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1452 check_eol (pfile);
1454 /* If potential control macro, we go back outside again. */
1455 if (ifs->next == 0 && ifs->mi_cmacro)
1457 pfile->mi_valid = true;
1458 pfile->mi_cmacro = ifs->mi_cmacro;
1461 buffer->if_stack = ifs->next;
1462 pfile->state.skipping = ifs->was_skipping;
1463 obstack_free (&pfile->buffer_ob, ifs);
1467 /* Push an if_stack entry for a preprocessor conditional, and set
1468 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1469 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1470 we need to check here that we are at the top of the file. */
1471 static void
1472 push_conditional (pfile, skip, type, cmacro)
1473 cpp_reader *pfile;
1474 int skip;
1475 int type;
1476 const cpp_hashnode *cmacro;
1478 struct if_stack *ifs;
1479 cpp_buffer *buffer = pfile->buffer;
1481 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1482 ifs->line = pfile->directive_line;
1483 ifs->next = buffer->if_stack;
1484 ifs->skip_elses = pfile->state.skipping || !skip;
1485 ifs->was_skipping = pfile->state.skipping;
1486 ifs->type = type;
1487 /* This condition is effectively a test for top-of-file. */
1488 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1489 ifs->mi_cmacro = cmacro;
1490 else
1491 ifs->mi_cmacro = 0;
1493 pfile->state.skipping = skip;
1494 buffer->if_stack = ifs;
1497 /* Read the tokens of the answer into the macro pool, in a directive
1498 of type TYPE. Only commit the memory if we intend it as permanent
1499 storage, i.e. the #assert case. Returns 0 on success, and sets
1500 ANSWERP to point to the answer. */
1501 static int
1502 parse_answer (pfile, answerp, type)
1503 cpp_reader *pfile;
1504 struct answer **answerp;
1505 int type;
1507 const cpp_token *paren;
1508 struct answer *answer;
1509 unsigned int acount;
1511 /* In a conditional, it is legal to not have an open paren. We
1512 should save the following token in this case. */
1513 paren = cpp_get_token (pfile);
1515 /* If not a paren, see if we're OK. */
1516 if (paren->type != CPP_OPEN_PAREN)
1518 /* In a conditional no answer is a test for any answer. It
1519 could be followed by any token. */
1520 if (type == T_IF)
1522 _cpp_backup_tokens (pfile, 1);
1523 return 0;
1526 /* #unassert with no answer is valid - it removes all answers. */
1527 if (type == T_UNASSERT && paren->type == CPP_EOF)
1528 return 0;
1530 cpp_error (pfile, DL_ERROR, "missing '(' after predicate");
1531 return 1;
1534 for (acount = 0;; acount++)
1536 size_t room_needed;
1537 const cpp_token *token = cpp_get_token (pfile);
1538 cpp_token *dest;
1540 if (token->type == CPP_CLOSE_PAREN)
1541 break;
1543 if (token->type == CPP_EOF)
1545 cpp_error (pfile, DL_ERROR, "missing ')' to complete answer");
1546 return 1;
1549 /* struct answer includes the space for one token. */
1550 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1552 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1553 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1555 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1556 *dest = *token;
1558 /* Drop whitespace at start, for answer equivalence purposes. */
1559 if (acount == 0)
1560 dest->flags &= ~PREV_WHITE;
1563 if (acount == 0)
1565 cpp_error (pfile, DL_ERROR, "predicate's answer is empty");
1566 return 1;
1569 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1570 answer->count = acount;
1571 answer->next = NULL;
1572 *answerp = answer;
1574 return 0;
1577 /* Parses an assertion directive of type TYPE, returning a pointer to
1578 the hash node of the predicate, or 0 on error. If an answer was
1579 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1580 static cpp_hashnode *
1581 parse_assertion (pfile, answerp, type)
1582 cpp_reader *pfile;
1583 struct answer **answerp;
1584 int type;
1586 cpp_hashnode *result = 0;
1587 const cpp_token *predicate;
1589 /* We don't expand predicates or answers. */
1590 pfile->state.prevent_expansion++;
1592 *answerp = 0;
1593 predicate = cpp_get_token (pfile);
1594 if (predicate->type == CPP_EOF)
1595 cpp_error (pfile, DL_ERROR, "assertion without predicate");
1596 else if (predicate->type != CPP_NAME)
1597 cpp_error (pfile, DL_ERROR, "predicate must be an identifier");
1598 else if (parse_answer (pfile, answerp, type) == 0)
1600 unsigned int len = NODE_LEN (predicate->val.node);
1601 unsigned char *sym = alloca (len + 1);
1603 /* Prefix '#' to get it out of macro namespace. */
1604 sym[0] = '#';
1605 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1606 result = cpp_lookup (pfile, sym, len + 1);
1609 pfile->state.prevent_expansion--;
1610 return result;
1613 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1614 or a pointer to NULL if the answer is not in the chain. */
1615 static struct answer **
1616 find_answer (node, candidate)
1617 cpp_hashnode *node;
1618 const struct answer *candidate;
1620 unsigned int i;
1621 struct answer **result;
1623 for (result = &node->value.answers; *result; result = &(*result)->next)
1625 struct answer *answer = *result;
1627 if (answer->count == candidate->count)
1629 for (i = 0; i < answer->count; i++)
1630 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1631 break;
1633 if (i == answer->count)
1634 break;
1638 return result;
1641 /* Test an assertion within a preprocessor conditional. Returns
1642 non-zero on failure, zero on success. On success, the result of
1643 the test is written into VALUE. */
1645 _cpp_test_assertion (pfile, value)
1646 cpp_reader *pfile;
1647 unsigned int *value;
1649 struct answer *answer;
1650 cpp_hashnode *node;
1652 node = parse_assertion (pfile, &answer, T_IF);
1653 if (node)
1654 *value = (node->type == NT_ASSERTION &&
1655 (answer == 0 || *find_answer (node, answer) != 0));
1657 /* We don't commit the memory for the answer - it's temporary only. */
1658 return node == 0;
1661 /* Handle #assert. */
1662 static void
1663 do_assert (pfile)
1664 cpp_reader *pfile;
1666 struct answer *new_answer;
1667 cpp_hashnode *node;
1669 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1670 if (node)
1672 /* Place the new answer in the answer list. First check there
1673 is not a duplicate. */
1674 new_answer->next = 0;
1675 if (node->type == NT_ASSERTION)
1677 if (*find_answer (node, new_answer))
1679 cpp_error (pfile, DL_WARNING, "\"%s\" re-asserted",
1680 NODE_NAME (node) + 1);
1681 return;
1683 new_answer->next = node->value.answers;
1686 node->type = NT_ASSERTION;
1687 node->value.answers = new_answer;
1688 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1689 + (new_answer->count - 1)
1690 * sizeof (cpp_token));
1691 check_eol (pfile);
1695 /* Handle #unassert. */
1696 static void
1697 do_unassert (pfile)
1698 cpp_reader *pfile;
1700 cpp_hashnode *node;
1701 struct answer *answer;
1703 node = parse_assertion (pfile, &answer, T_UNASSERT);
1704 /* It isn't an error to #unassert something that isn't asserted. */
1705 if (node && node->type == NT_ASSERTION)
1707 if (answer)
1709 struct answer **p = find_answer (node, answer), *temp;
1711 /* Remove the answer from the list. */
1712 temp = *p;
1713 if (temp)
1714 *p = temp->next;
1716 /* Did we free the last answer? */
1717 if (node->value.answers == 0)
1718 node->type = NT_VOID;
1720 check_eol (pfile);
1722 else
1723 _cpp_free_definition (node);
1726 /* We don't commit the memory for the answer - it's temporary only. */
1729 /* These are for -D, -U, -A. */
1731 /* Process the string STR as if it appeared as the body of a #define.
1732 If STR is just an identifier, define it with value 1.
1733 If STR has anything after the identifier, then it should
1734 be identifier=definition. */
1735 void
1736 cpp_define (pfile, str)
1737 cpp_reader *pfile;
1738 const char *str;
1740 char *buf, *p;
1741 size_t count;
1743 /* Copy the entire option so we can modify it.
1744 Change the first "=" in the string to a space. If there is none,
1745 tack " 1" on the end. */
1747 count = strlen (str);
1748 buf = (char *) alloca (count + 3);
1749 memcpy (buf, str, count);
1751 p = strchr (str, '=');
1752 if (p)
1753 buf[p - str] = ' ';
1754 else
1756 buf[count++] = ' ';
1757 buf[count++] = '1';
1759 buf[count] = '\0';
1761 run_directive (pfile, T_DEFINE, buf, count);
1764 /* Slight variant of the above for use by initialize_builtins. */
1765 void
1766 _cpp_define_builtin (pfile, str)
1767 cpp_reader *pfile;
1768 const char *str;
1770 run_directive (pfile, T_DEFINE, str, strlen (str));
1773 /* Process MACRO as if it appeared as the body of an #undef. */
1774 void
1775 cpp_undef (pfile, macro)
1776 cpp_reader *pfile;
1777 const char *macro;
1779 run_directive (pfile, T_UNDEF, macro, strlen (macro));
1782 /* Process the string STR as if it appeared as the body of a #assert. */
1783 void
1784 cpp_assert (pfile, str)
1785 cpp_reader *pfile;
1786 const char *str;
1788 handle_assertion (pfile, str, T_ASSERT);
1791 /* Process STR as if it appeared as the body of an #unassert. */
1792 void
1793 cpp_unassert (pfile, str)
1794 cpp_reader *pfile;
1795 const char *str;
1797 handle_assertion (pfile, str, T_UNASSERT);
1800 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1801 static void
1802 handle_assertion (pfile, str, type)
1803 cpp_reader *pfile;
1804 const char *str;
1805 int type;
1807 size_t count = strlen (str);
1808 const char *p = strchr (str, '=');
1810 if (p)
1812 /* Copy the entire option so we can modify it. Change the first
1813 "=" in the string to a '(', and tack a ')' on the end. */
1814 char *buf = (char *) alloca (count + 2);
1816 memcpy (buf, str, count);
1817 buf[p - str] = '(';
1818 buf[count++] = ')';
1819 buf[count] = '\0';
1820 str = buf;
1823 run_directive (pfile, type, str, count);
1826 /* The number of errors for a given reader. */
1827 unsigned int
1828 cpp_errors (pfile)
1829 cpp_reader *pfile;
1831 return pfile->errors;
1834 /* The options structure. */
1835 cpp_options *
1836 cpp_get_options (pfile)
1837 cpp_reader *pfile;
1839 return &pfile->opts;
1842 /* The callbacks structure. */
1843 cpp_callbacks *
1844 cpp_get_callbacks (pfile)
1845 cpp_reader *pfile;
1847 return &pfile->cb;
1850 /* The line map set. */
1851 const struct line_maps *
1852 cpp_get_line_maps (pfile)
1853 cpp_reader *pfile;
1855 return &pfile->line_maps;
1858 /* Copy the given callbacks structure to our own. */
1859 void
1860 cpp_set_callbacks (pfile, cb)
1861 cpp_reader *pfile;
1862 cpp_callbacks *cb;
1864 pfile->cb = *cb;
1867 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1868 doesn't fail. It does not generate a file change call back; that
1869 is the responsibility of the caller. */
1870 cpp_buffer *
1871 cpp_push_buffer (pfile, buffer, len, from_stage3, return_at_eof)
1872 cpp_reader *pfile;
1873 const uchar *buffer;
1874 size_t len;
1875 int from_stage3;
1876 int return_at_eof;
1878 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1880 /* Clears, amongst other things, if_stack and mi_cmacro. */
1881 memset (new, 0, sizeof (cpp_buffer));
1883 new->line_base = new->buf = new->cur = buffer;
1884 new->rlimit = buffer + len;
1885 new->from_stage3 = from_stage3;
1886 new->prev = pfile->buffer;
1887 new->return_at_eof = return_at_eof;
1888 new->saved_flags = BOL;
1890 pfile->buffer = new;
1892 return new;
1895 /* Pops a single buffer, with a file change call-back if appropriate.
1896 Then pushes the next -include file, if any remain. */
1897 void
1898 _cpp_pop_buffer (pfile)
1899 cpp_reader *pfile;
1901 cpp_buffer *buffer = pfile->buffer;
1902 struct include_file *inc = buffer->inc;
1903 struct if_stack *ifs;
1905 /* Walk back up the conditional stack till we reach its level at
1906 entry to this file, issuing error messages. */
1907 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1908 cpp_error_with_line (pfile, DL_ERROR, ifs->line, 0,
1909 "unterminated #%s", dtable[ifs->type].name);
1911 /* In case of a missing #endif. */
1912 pfile->state.skipping = 0;
1914 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1915 pfile->buffer = buffer->prev;
1917 /* Free the buffer object now; we may want to push a new buffer
1918 in _cpp_push_next_include_file. */
1919 obstack_free (&pfile->buffer_ob, buffer);
1921 if (inc)
1923 _cpp_pop_file_buffer (pfile, inc);
1925 /* Don't generate a callback for popping the main file. */
1926 if (pfile->buffer)
1928 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1930 /* If this is the main file, there may be some -include
1931 files left to push. */
1932 if (!pfile->buffer->prev)
1933 _cpp_maybe_push_include_file (pfile);
1938 /* Enter all recognised directives in the hash table. */
1939 void
1940 _cpp_init_directives (pfile)
1941 cpp_reader *pfile;
1943 unsigned int i;
1944 cpp_hashnode *node;
1946 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1948 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1949 node->directive_index = i + 1;