1 /* CPP Library. (Directive handling.)
2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 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
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. */
29 /* Chained list of answers to an assertion. */
37 /* Stack of conditionals currently in progress
38 (including both successful and failing conditionals). */
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) (cpp_reader *);
53 struct pragma_entry *next;
54 const cpp_hashnode *pragma; /* Name and length. */
58 struct pragma_entry *space;
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. */
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).
75 EXPAND is set on directives that are always macro-expanded. */
77 #define IF_COND (1 << 1)
80 #define EXPAND (1 << 4)
82 /* Defines one #-directive, including how to handle it. */
83 typedef void (*directive_handler) (cpp_reader *);
84 typedef struct directive directive;
87 directive_handler handler; /* Function to handle directive. */
88 const uchar *name; /* Name of directive. */
89 unsigned short length; /* Length of name. */
90 unsigned char origin; /* Origin of directive. */
91 unsigned char flags; /* Flags describing this directive. */
94 /* Forward declarations. */
96 static void skip_rest_of_line (cpp_reader *);
97 static void check_eol (cpp_reader *);
98 static void start_directive (cpp_reader *);
99 static void prepare_directive_trad (cpp_reader *);
100 static void end_directive (cpp_reader *, int);
101 static void directive_diagnostics (cpp_reader *, const directive *, int);
102 static void run_directive (cpp_reader *, int, const char *, size_t);
103 static char *glue_header_name (cpp_reader *);
104 static const char *parse_include (cpp_reader *, int *);
105 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
106 static unsigned int read_flag (cpp_reader *, unsigned int);
107 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
108 static void do_diagnostic (cpp_reader *, int, int);
109 static cpp_hashnode *lex_macro_node (cpp_reader *);
110 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
111 static void do_include_common (cpp_reader *, enum include_type);
112 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
113 const cpp_hashnode *);
114 static struct pragma_entry *insert_pragma_entry (cpp_reader *,
115 struct pragma_entry **,
116 const cpp_hashnode *,
118 static int count_registered_pragmas (struct pragma_entry *);
119 static char ** save_registered_pragmas (struct pragma_entry *, char **);
120 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
122 static void do_pragma_once (cpp_reader *);
123 static void do_pragma_poison (cpp_reader *);
124 static void do_pragma_system_header (cpp_reader *);
125 static void do_pragma_dependency (cpp_reader *);
126 static void do_linemarker (cpp_reader *);
127 static const cpp_token *get_token_no_padding (cpp_reader *);
128 static const cpp_token *get__Pragma_string (cpp_reader *);
129 static void destringize_and_run (cpp_reader *, const cpp_string *);
130 static int parse_answer (cpp_reader *, struct answer **, int);
131 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
132 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
133 static void handle_assertion (cpp_reader *, const char *, int);
135 /* This is the table of directive handlers. It is ordered by
136 frequency of occurrence; the numbers at the end are directive
137 counts from all the source code I have lying around (egcs and libc
138 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
139 pcmcia-cs-3.0.9). This is no longer important as directive lookup
140 is now O(1). All extensions other than #warning and #include_next
141 are deprecated. The name is where the extension appears to have
144 #define DIRECTIVE_TABLE \
145 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
146 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
147 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
148 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
149 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
150 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
151 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
152 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
153 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
154 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
155 D(error, T_ERROR, STDC89, 0) /* 475 */ \
156 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
157 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
158 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
159 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
160 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
161 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
162 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
163 D(sccs, T_SCCS, EXTENSION, 0) /* 0 SVR4? */
165 /* Use the table to generate a series of prototypes, an enum for the
166 directive names, and an array of directive handlers. */
168 #define D(name, t, o, f) static void do_##name (cpp_reader *);
172 #define D(n, tag, o, f) tag,
180 #define D(name, t, origin, flags) \
181 { do_##name, (const uchar *) #name, \
182 sizeof #name - 1, origin, flags },
183 static const directive dtable[] =
188 #undef DIRECTIVE_TABLE
190 /* Wrapper struct directive for linemarkers.
191 The origin is more or less true - the original K+R cpp
192 did use this notation in its preprocessed output. */
193 static const directive linemarker_dir =
195 do_linemarker, U"#", 1, KANDR, IN_I
198 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
200 /* Skip any remaining tokens in a directive. */
202 skip_rest_of_line (cpp_reader *pfile)
204 /* Discard all stacked contexts. */
205 while (pfile->context->prev)
206 _cpp_pop_context (pfile);
208 /* Sweep up all tokens remaining on the line. */
210 while (_cpp_lex_token (pfile)->type != CPP_EOF)
214 /* Ensure there are no stray tokens at the end of a directive. */
216 check_eol (cpp_reader *pfile)
218 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
219 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
220 pfile->directive->name);
223 /* Called when entering a directive, _Pragma or command-line directive. */
225 start_directive (cpp_reader *pfile)
227 /* Setup in-directive state. */
228 pfile->state.in_directive = 1;
229 pfile->state.save_comments = 0;
231 /* Some handlers need the position of the # for diagnostics. */
232 pfile->directive_line = pfile->line_table->highest_line;
235 /* Called when leaving a directive, _Pragma or command-line directive. */
237 end_directive (cpp_reader *pfile, int skip_line)
239 if (CPP_OPTION (pfile, traditional))
241 /* Revert change of prepare_directive_trad. */
242 pfile->state.prevent_expansion--;
244 if (pfile->directive != &dtable[T_DEFINE])
245 _cpp_remove_overlay (pfile);
247 /* We don't skip for an assembler #. */
250 skip_rest_of_line (pfile);
251 if (!pfile->keep_tokens)
253 pfile->cur_run = &pfile->base_run;
254 pfile->cur_token = pfile->base_run.base;
259 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
260 pfile->state.in_directive = 0;
261 pfile->state.in_expression = 0;
262 pfile->state.angled_headers = 0;
263 pfile->directive = 0;
266 /* Prepare to handle the directive in pfile->directive. */
268 prepare_directive_trad (cpp_reader *pfile)
270 if (pfile->directive != &dtable[T_DEFINE])
272 bool no_expand = (pfile->directive
273 && ! (pfile->directive->flags & EXPAND));
274 bool was_skipping = pfile->state.skipping;
276 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
277 || pfile->directive == &dtable[T_ELIF]);
278 if (pfile->state.in_expression)
279 pfile->state.skipping = false;
282 pfile->state.prevent_expansion++;
283 _cpp_scan_out_logical_line (pfile, NULL);
285 pfile->state.prevent_expansion--;
287 pfile->state.skipping = was_skipping;
288 _cpp_overlay_buffer (pfile, pfile->out.base,
289 pfile->out.cur - pfile->out.base);
292 /* Stop ISO C from expanding anything. */
293 pfile->state.prevent_expansion++;
296 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
297 the '#' was indented. */
299 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
301 /* Issue -pedantic warnings for extensions. */
302 if (CPP_PEDANTIC (pfile)
303 && ! pfile->state.skipping
304 && dir->origin == EXTENSION)
305 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
307 /* Traditionally, a directive is ignored unless its # is in
308 column 1. Therefore in code intended to work with K+R
309 compilers, directives added by C89 must have their #
310 indented, and directives present in traditional C must not.
311 This is true even of directives in skipped conditional
312 blocks. #elif cannot be used at all. */
313 if (CPP_WTRADITIONAL (pfile))
315 if (dir == &dtable[T_ELIF])
316 cpp_error (pfile, CPP_DL_WARNING,
317 "suggest not using #elif in traditional C");
318 else if (indented && dir->origin == KANDR)
319 cpp_error (pfile, CPP_DL_WARNING,
320 "traditional C ignores #%s with the # indented",
322 else if (!indented && dir->origin != KANDR)
323 cpp_error (pfile, CPP_DL_WARNING,
324 "suggest hiding #%s from traditional C with an indented #",
329 /* Check if we have a known directive. INDENTED is nonzero if the
330 '#' of the directive was indented. This function is in this file
331 to save unnecessarily exporting dtable etc. to cpplex.c. Returns
332 nonzero if the line of tokens has been handled, zero if we should
333 continue processing the line. */
335 _cpp_handle_directive (cpp_reader *pfile, int indented)
337 const directive *dir = 0;
338 const cpp_token *dname;
339 bool was_parsing_args = pfile->state.parsing_args;
340 bool was_discarding_output = pfile->state.discarding_output;
343 if (was_discarding_output)
344 pfile->state.prevent_expansion = 0;
346 if (was_parsing_args)
348 if (CPP_OPTION (pfile, pedantic))
349 cpp_error (pfile, CPP_DL_PEDWARN,
350 "embedding a directive within macro arguments is not portable");
351 pfile->state.parsing_args = 0;
352 pfile->state.prevent_expansion = 0;
354 start_directive (pfile);
355 dname = _cpp_lex_token (pfile);
357 if (dname->type == CPP_NAME)
359 if (dname->val.node->is_directive)
360 dir = &dtable[dname->val.node->directive_index];
362 /* We do not recognize the # followed by a number extension in
364 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
366 dir = &linemarker_dir;
367 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
368 && ! pfile->state.skipping)
369 cpp_error (pfile, CPP_DL_PEDWARN,
370 "style of line directive is a GCC extension");
375 /* If we have a directive that is not an opening conditional,
376 invalidate any control macro. */
377 if (! (dir->flags & IF_COND))
378 pfile->mi_valid = false;
380 /* Kluge alert. In order to be sure that code like this
385 does not cause '#define foo bar' to get executed when
386 compiled with -save-temps, we recognize directives in
387 -fpreprocessed mode only if the # is in column 1. cppmacro.c
388 puts a space in front of any '#' at the start of a macro. */
389 if (CPP_OPTION (pfile, preprocessed)
390 && (indented || !(dir->flags & IN_I)))
397 /* In failed conditional groups, all non-conditional
398 directives are ignored. Before doing that, whether
399 skipping or not, we should lex angle-bracketed headers
400 correctly, and maybe output some diagnostics. */
401 pfile->state.angled_headers = dir->flags & INCL;
402 pfile->state.directive_wants_padding = dir->flags & INCL;
403 if (! CPP_OPTION (pfile, preprocessed))
404 directive_diagnostics (pfile, dir, indented);
405 if (pfile->state.skipping && !(dir->flags & COND))
409 else if (dname->type == CPP_EOF)
410 ; /* CPP_EOF is the "null directive". */
413 /* An unknown directive. Don't complain about it in assembly
414 source: we don't know where the comments are, and # may
415 introduce assembler pseudo-ops. Don't complain about invalid
416 directives in skipped conditional groups (6.10 p4). */
417 if (CPP_OPTION (pfile, lang) == CLK_ASM)
419 else if (!pfile->state.skipping)
420 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
421 cpp_token_as_text (pfile, dname));
424 pfile->directive = dir;
425 if (CPP_OPTION (pfile, traditional))
426 prepare_directive_trad (pfile);
429 pfile->directive->handler (pfile);
431 _cpp_backup_tokens (pfile, 1);
433 end_directive (pfile, skip);
434 if (was_parsing_args)
436 /* Restore state when within macro args. */
437 pfile->state.parsing_args = 2;
438 pfile->state.prevent_expansion = 1;
440 if (was_discarding_output)
441 pfile->state.prevent_expansion = 1;
445 /* Directive handler wrapper used by the command line option
446 processor. BUF is \n terminated. */
448 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
450 cpp_push_buffer (pfile, (const uchar *) buf, count,
451 /* from_stage3 */ true);
452 /* Disgusting hack. */
453 if (dir_no == T_PRAGMA)
454 pfile->buffer->file = pfile->buffer->prev->file;
455 start_directive (pfile);
457 /* This is a short-term fix to prevent a leading '#' being
458 interpreted as a directive. */
459 _cpp_clean_line (pfile);
461 pfile->directive = &dtable[dir_no];
462 if (CPP_OPTION (pfile, traditional))
463 prepare_directive_trad (pfile);
464 pfile->directive->handler (pfile);
465 end_directive (pfile, 1);
466 if (dir_no == T_PRAGMA)
467 pfile->buffer->file = NULL;
468 _cpp_pop_buffer (pfile);
471 /* Checks for validity the macro name in #define, #undef, #ifdef and
472 #ifndef directives. */
473 static cpp_hashnode *
474 lex_macro_node (cpp_reader *pfile)
476 const cpp_token *token = _cpp_lex_token (pfile);
478 /* The token immediately after #define must be an identifier. That
479 identifier may not be "defined", per C99 6.10.8p4.
480 In C++, it may not be any of the "named operators" either,
481 per C++98 [lex.digraph], [lex.key].
482 Finally, the identifier may not have been poisoned. (In that case
483 the lexer has issued the error message for us.) */
485 if (token->type == CPP_NAME)
487 cpp_hashnode *node = token->val.node;
489 if (node == pfile->spec_nodes.n_defined)
490 cpp_error (pfile, CPP_DL_ERROR,
491 "\"defined\" cannot be used as a macro name");
492 else if (! (node->flags & NODE_POISONED))
495 else if (token->flags & NAMED_OP)
496 cpp_error (pfile, CPP_DL_ERROR,
497 "\"%s\" cannot be used as a macro name as it is an operator in C++",
498 NODE_NAME (token->val.node));
499 else if (token->type == CPP_EOF)
500 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
501 pfile->directive->name);
503 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
508 /* Process a #define directive. Most work is done in cppmacro.c. */
510 do_define (cpp_reader *pfile)
512 cpp_hashnode *node = lex_macro_node (pfile);
516 /* If we have been requested to expand comments into macros,
517 then re-enable saving of comments. */
518 pfile->state.save_comments =
519 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
521 if (_cpp_create_definition (pfile, node))
522 if (pfile->cb.define)
523 pfile->cb.define (pfile, pfile->directive_line, node);
527 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
529 do_undef (cpp_reader *pfile)
531 cpp_hashnode *node = lex_macro_node (pfile);
536 pfile->cb.undef (pfile, pfile->directive_line, node);
538 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
539 identifier is not currently defined as a macro name. */
540 if (node->type == NT_MACRO)
542 if (node->flags & NODE_WARN)
543 cpp_error (pfile, CPP_DL_WARNING,
544 "undefining \"%s\"", NODE_NAME (node));
546 if (CPP_OPTION (pfile, warn_unused_macros))
547 _cpp_warn_if_unused_macro (pfile, node, NULL);
549 _cpp_free_definition (node);
556 /* Undefine a single macro/assertion/whatever. */
559 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
560 void *data_p ATTRIBUTE_UNUSED)
562 /* Body of _cpp_free_definition inlined here for speed.
563 Macros and assertions no longer have anything to free. */
565 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
569 /* Undefine all macros and assertions. */
572 cpp_undef_all (cpp_reader *pfile)
574 cpp_forall_identifiers (pfile, undefine_macros, NULL);
578 /* Helper routine used by parse_include. Reinterpret the current line
579 as an h-char-sequence (< ... >); we are looking at the first token
580 after the <. Returns a malloced filename. */
582 glue_header_name (cpp_reader *pfile)
584 const cpp_token *token;
586 size_t len, total_len = 0, capacity = 1024;
588 /* To avoid lexed tokens overwriting our glued name, we can only
589 allocate from the string pool once we've lexed everything. */
590 buffer = xmalloc (capacity);
593 token = get_token_no_padding (pfile);
595 if (token->type == CPP_GREATER)
597 if (token->type == CPP_EOF)
599 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
603 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
604 if (total_len + len > capacity)
606 capacity = (capacity + len) * 2;
607 buffer = xrealloc (buffer, capacity);
610 if (token->flags & PREV_WHITE)
611 buffer[total_len++] = ' ';
613 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len])
617 buffer[total_len] = '\0';
621 /* Returns the file name of #include, #include_next, #import and
622 #pragma dependency. The string is malloced and the caller should
623 free it. Returns NULL on error. */
625 parse_include (cpp_reader *pfile, int *pangle_brackets)
628 const cpp_token *header;
630 /* Allow macro expansion. */
631 header = get_token_no_padding (pfile);
632 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
634 fname = xmalloc (header->val.str.len - 1);
635 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
636 fname[header->val.str.len - 2] = '\0';
637 *pangle_brackets = header->type == CPP_HEADER_NAME;
639 else if (header->type == CPP_LESS)
641 fname = glue_header_name (pfile);
642 *pangle_brackets = 1;
646 const unsigned char *dir;
648 if (pfile->directive == &dtable[T_PRAGMA])
649 dir = U"pragma dependency";
651 dir = pfile->directive->name;
652 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
662 /* Handle #include, #include_next and #import. */
664 do_include_common (cpp_reader *pfile, enum include_type type)
669 fname = parse_include (pfile, &angle_brackets);
673 /* Prevent #include recursion. */
674 if (pfile->line_table->depth >= CPP_STACK_MAX)
675 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
678 /* Get out of macro context, if we are. */
679 skip_rest_of_line (pfile);
681 if (pfile->cb.include)
682 pfile->cb.include (pfile, pfile->directive_line,
683 pfile->directive->name, fname, angle_brackets);
685 _cpp_stack_include (pfile, fname, angle_brackets, type);
688 free ((void *) fname);
692 do_include (cpp_reader *pfile)
694 do_include_common (pfile, IT_INCLUDE);
698 do_import (cpp_reader *pfile)
700 do_include_common (pfile, IT_IMPORT);
704 do_include_next (cpp_reader *pfile)
706 enum include_type type = IT_INCLUDE_NEXT;
708 /* If this is the primary source file, warn and use the normal
710 if (! pfile->buffer->prev)
712 cpp_error (pfile, CPP_DL_WARNING,
713 "#include_next in primary source file");
716 do_include_common (pfile, type);
719 /* Subroutine of do_linemarker. Read possible flags after file name.
720 LAST is the last flag seen; 0 if this is the first flag. Return the
721 flag if it is valid, 0 at the end of the directive. Otherwise
724 read_flag (cpp_reader *pfile, unsigned int last)
726 const cpp_token *token = _cpp_lex_token (pfile);
728 if (token->type == CPP_NUMBER && token->val.str.len == 1)
730 unsigned int flag = token->val.str.text[0] - '0';
732 if (flag > last && flag <= 4
733 && (flag != 4 || last == 3)
734 && (flag != 2 || last == 0))
738 if (token->type != CPP_EOF)
739 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
740 cpp_token_as_text (pfile, token));
744 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
745 of length LEN, to binary; store it in NUMP, and return 0 if the
746 number was well-formed, 1 if not. Temporary, hopefully. */
748 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
750 unsigned long reg = 0;
764 /* Interpret #line command.
765 Note that the filename string (if any) is a true string constant
766 (escapes are interpreted), unlike in #line. */
768 do_line (cpp_reader *pfile)
770 const struct line_maps *line_table = pfile->line_table;
771 const struct line_map *map = &line_table->maps[line_table->used - 1];
772 const cpp_token *token;
773 const char *new_file = map->to_file;
774 unsigned long new_lineno;
776 /* C99 raised the minimum limit on #line numbers. */
777 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
779 /* #line commands expand macros. */
780 token = cpp_get_token (pfile);
781 if (token->type != CPP_NUMBER
782 || strtoul_for_line (token->val.str.text, token->val.str.len,
785 cpp_error (pfile, CPP_DL_ERROR,
786 "\"%s\" after #line is not a positive integer",
787 cpp_token_as_text (pfile, token));
791 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
792 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
794 token = cpp_get_token (pfile);
795 if (token->type == CPP_STRING)
797 cpp_string s = { 0, 0 };
798 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
800 new_file = (const char *)s.text;
803 else if (token->type != CPP_EOF)
805 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
806 cpp_token_as_text (pfile, token));
810 skip_rest_of_line (pfile);
811 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
815 /* Interpret the # 44 "file" [flags] notation, which has slightly
816 different syntax and semantics from #line: Flags are allowed,
817 and we never complain about the line number being too big. */
819 do_linemarker (cpp_reader *pfile)
821 const struct line_maps *line_table = pfile->line_table;
822 const struct line_map *map = &line_table->maps[line_table->used - 1];
823 const cpp_token *token;
824 const char *new_file = map->to_file;
825 unsigned long new_lineno;
826 unsigned int new_sysp = map->sysp;
827 enum lc_reason reason = LC_RENAME;
830 /* Back up so we can get the number again. Putting this in
831 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
832 some circumstances, which can segfault. */
833 _cpp_backup_tokens (pfile, 1);
835 /* #line commands expand macros. */
836 token = cpp_get_token (pfile);
837 if (token->type != CPP_NUMBER
838 || strtoul_for_line (token->val.str.text, token->val.str.len,
841 cpp_error (pfile, CPP_DL_ERROR,
842 "\"%s\" after # is not a positive integer",
843 cpp_token_as_text (pfile, token));
847 token = cpp_get_token (pfile);
848 if (token->type == CPP_STRING)
850 cpp_string s = { 0, 0 };
851 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
853 new_file = (const char *)s.text;
856 flag = read_flag (pfile, 0);
860 /* Fake an include for cpp_included (). */
861 _cpp_fake_include (pfile, new_file);
862 flag = read_flag (pfile, flag);
867 flag = read_flag (pfile, flag);
872 flag = read_flag (pfile, flag);
875 pfile->buffer->sysp = new_sysp;
880 else if (token->type != CPP_EOF)
882 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
883 cpp_token_as_text (pfile, token));
887 skip_rest_of_line (pfile);
888 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
891 /* Arrange the file_change callback. pfile->line has changed to
892 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
893 header, 2 for a system header that needs to be extern "C" protected,
894 and zero otherwise. */
896 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
897 const char *to_file, unsigned int file_line,
900 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
903 linemap_line_start (pfile->line_table, map->to_line, 127);
905 if (pfile->cb.file_change)
906 pfile->cb.file_change (pfile, map);
909 /* Report a warning or error detected by the program we are
910 processing. Use the directive's tokens in the error message. */
912 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
914 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
917 fprintf (stderr, "#%s ", pfile->directive->name);
918 pfile->state.prevent_expansion++;
919 cpp_output_line (pfile, stderr);
920 pfile->state.prevent_expansion--;
925 do_error (cpp_reader *pfile)
927 do_diagnostic (pfile, CPP_DL_ERROR, 1);
931 do_warning (cpp_reader *pfile)
933 /* We want #warning diagnostics to be emitted in system headers too. */
934 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
937 /* Report program identification. */
939 do_ident (cpp_reader *pfile)
941 const cpp_token *str = cpp_get_token (pfile);
943 if (str->type != CPP_STRING)
944 cpp_error (pfile, CPP_DL_ERROR, "invalid #ident directive");
945 else if (pfile->cb.ident)
946 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
951 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
952 matching entry, or NULL if none is found. The returned entry could
953 be the start of a namespace chain, or a pragma. */
954 static struct pragma_entry *
955 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
957 while (chain && chain->pragma != pragma)
963 /* Create and insert a pragma entry for NAME at the beginning of a
964 singly-linked CHAIN. If handler is NULL, it is a namespace,
965 otherwise it is a pragma and its handler. */
966 static struct pragma_entry *
967 insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
968 const cpp_hashnode *pragma, pragma_cb handler)
970 struct pragma_entry *new;
972 new = (struct pragma_entry *)
973 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
974 new->pragma = pragma;
978 new->u.handler = handler;
991 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
992 goes in the global namespace. HANDLER is the handler it will call,
993 which must be non-NULL. */
995 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
998 struct pragma_entry **chain = &pfile->pragmas;
999 struct pragma_entry *entry;
1000 const cpp_hashnode *node;
1007 node = cpp_lookup (pfile, U space, strlen (space));
1008 entry = lookup_pragma_entry (*chain, node);
1010 entry = insert_pragma_entry (pfile, chain, node, NULL);
1011 else if (!entry->is_nspace)
1013 chain = &entry->u.space;
1016 /* Check for duplicates. */
1017 node = cpp_lookup (pfile, U name, strlen (name));
1018 entry = lookup_pragma_entry (*chain, node);
1021 if (entry->is_nspace)
1023 cpp_error (pfile, CPP_DL_ICE,
1024 "registering \"%s\" as both a pragma and a pragma namespace",
1027 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1030 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1033 insert_pragma_entry (pfile, chain, node, handler);
1036 /* Register the pragmas the preprocessor itself handles. */
1038 _cpp_init_internal_pragmas (cpp_reader *pfile)
1040 /* Pragmas in the global namespace. */
1041 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
1043 /* New GCC-specific pragmas should be put in the GCC namespace. */
1044 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
1045 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
1046 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
1049 /* Return the number of registered pragmas in PE. */
1052 count_registered_pragmas (struct pragma_entry *pe)
1055 for (; pe != NULL; pe = pe->next)
1058 ct += count_registered_pragmas (pe->u.space);
1064 /* Save into SD the names of the registered pragmas referenced by PE,
1065 and return a pointer to the next free space in SD. */
1068 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1070 for (; pe != NULL; pe = pe->next)
1073 sd = save_registered_pragmas (pe->u.space, sd);
1074 *sd++ = xmemdup (HT_STR (&pe->pragma->ident),
1075 HT_LEN (&pe->pragma->ident),
1076 HT_LEN (&pe->pragma->ident) + 1);
1081 /* Return a newly-allocated array which saves the names of the
1082 registered pragmas. */
1085 _cpp_save_pragma_names (cpp_reader *pfile)
1087 int ct = count_registered_pragmas (pfile->pragmas);
1088 char **result = xnewvec (char *, ct);
1089 (void) save_registered_pragmas (pfile->pragmas, result);
1093 /* Restore from SD the names of the registered pragmas referenced by PE,
1094 and return a pointer to the next unused name in SD. */
1097 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1100 for (; pe != NULL; pe = pe->next)
1103 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1104 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1111 /* Restore the names of the registered pragmas from SAVED. */
1114 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1116 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1120 /* Pragmata handling. We handle some, and pass the rest on to the
1121 front end. C99 defines three pragmas and says that no macro
1122 expansion is to be performed on them; whether or not macro
1123 expansion happens for other pragmas is implementation defined.
1124 This implementation never macro-expands the text after #pragma. */
1126 do_pragma (cpp_reader *pfile)
1128 const struct pragma_entry *p = NULL;
1129 const cpp_token *token, *pragma_token = pfile->cur_token;
1130 unsigned int count = 1;
1132 pfile->state.prevent_expansion++;
1134 token = cpp_get_token (pfile);
1135 if (token->type == CPP_NAME)
1137 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1138 if (p && p->is_nspace)
1141 token = cpp_get_token (pfile);
1142 if (token->type == CPP_NAME)
1143 p = lookup_pragma_entry (p->u.space, token->val.node);
1151 /* Since the handler below doesn't get the line number, that it
1152 might need for diagnostics, make sure it has the right
1153 numbers in place. */
1154 if (pfile->cb.line_change)
1155 (*pfile->cb.line_change) (pfile, pragma_token, false);
1156 (*p->u.handler) (pfile);
1158 else if (pfile->cb.def_pragma)
1160 _cpp_backup_tokens (pfile, count);
1161 pfile->cb.def_pragma (pfile, pfile->directive_line);
1164 pfile->state.prevent_expansion--;
1167 /* Handle #pragma once. */
1169 do_pragma_once (cpp_reader *pfile)
1171 if (pfile->buffer->prev == NULL)
1172 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1175 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1178 /* Handle #pragma GCC poison, to poison one or more identifiers so
1179 that the lexer produces a hard error for each subsequent usage. */
1181 do_pragma_poison (cpp_reader *pfile)
1183 const cpp_token *tok;
1186 pfile->state.poisoned_ok = 1;
1189 tok = _cpp_lex_token (pfile);
1190 if (tok->type == CPP_EOF)
1192 if (tok->type != CPP_NAME)
1194 cpp_error (pfile, CPP_DL_ERROR,
1195 "invalid #pragma GCC poison directive");
1200 if (hp->flags & NODE_POISONED)
1203 if (hp->type == NT_MACRO)
1204 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1206 _cpp_free_definition (hp);
1207 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1209 pfile->state.poisoned_ok = 0;
1212 /* Mark the current header as a system header. This will suppress
1213 some categories of warnings (notably those from -pedantic). It is
1214 intended for use in system libraries that cannot be implemented in
1215 conforming C, but cannot be certain that their headers appear in a
1216 system include directory. To prevent abuse, it is rejected in the
1217 primary source file. */
1219 do_pragma_system_header (cpp_reader *pfile)
1221 cpp_buffer *buffer = pfile->buffer;
1223 if (buffer->prev == 0)
1224 cpp_error (pfile, CPP_DL_WARNING,
1225 "#pragma system_header ignored outside include file");
1229 skip_rest_of_line (pfile);
1230 cpp_make_system_header (pfile, 1, 0);
1234 /* Check the modified date of the current include file against a specified
1235 file. Issue a diagnostic, if the specified file is newer. We use this to
1236 determine if a fixed header should be refixed. */
1238 do_pragma_dependency (cpp_reader *pfile)
1241 int angle_brackets, ordering;
1243 fname = parse_include (pfile, &angle_brackets);
1247 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1249 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1250 else if (ordering > 0)
1252 cpp_error (pfile, CPP_DL_WARNING,
1253 "current file is older than %s", fname);
1254 if (cpp_get_token (pfile)->type != CPP_EOF)
1256 _cpp_backup_tokens (pfile, 1);
1257 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1261 free ((void *) fname);
1264 /* Get a token but skip padding. */
1265 static const cpp_token *
1266 get_token_no_padding (cpp_reader *pfile)
1270 const cpp_token *result = cpp_get_token (pfile);
1271 if (result->type != CPP_PADDING)
1276 /* Check syntax is "(string-literal)". Returns the string on success,
1277 or NULL on failure. */
1278 static const cpp_token *
1279 get__Pragma_string (cpp_reader *pfile)
1281 const cpp_token *string;
1283 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1286 string = get_token_no_padding (pfile);
1287 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1290 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1296 /* Destringize IN into a temporary buffer, by removing the first \ of
1297 \" and \\ sequences, and process the result as a #pragma directive. */
1299 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1301 const unsigned char *src, *limit;
1302 char *dest, *result;
1304 dest = result = alloca (in->len - 1);
1305 src = in->text + 1 + (in->text[0] == 'L');
1306 limit = in->text + in->len - 1;
1309 /* We know there is a character following the backslash. */
1310 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1316 /* Ugh; an awful kludge. We are really not set up to be lexing
1317 tokens when in the middle of a macro expansion. Use a new
1318 context to force cpp_get_token to lex, and so skip_rest_of_line
1319 doesn't go beyond the end of the text. Also, remember the
1320 current lexing position so we can return to it later.
1322 Something like line-at-a-time lexing should remove the need for
1325 cpp_context *saved_context = pfile->context;
1326 cpp_token *saved_cur_token = pfile->cur_token;
1327 tokenrun *saved_cur_run = pfile->cur_run;
1329 pfile->context = xnew (cpp_context);
1330 pfile->context->macro = 0;
1331 pfile->context->prev = 0;
1332 run_directive (pfile, T_PRAGMA, result, dest - result);
1333 free (pfile->context);
1334 pfile->context = saved_context;
1335 pfile->cur_token = saved_cur_token;
1336 pfile->cur_run = saved_cur_run;
1339 /* See above comment. For the moment, we'd like
1341 token1 _Pragma ("foo") token2
1351 Getting the line markers is a little tricky. */
1352 if (pfile->cb.line_change)
1353 pfile->cb.line_change (pfile, pfile->cur_token, false);
1356 /* Handle the _Pragma operator. */
1358 _cpp_do__Pragma (cpp_reader *pfile)
1360 const cpp_token *string = get__Pragma_string (pfile);
1363 destringize_and_run (pfile, &string->val.str);
1365 cpp_error (pfile, CPP_DL_ERROR,
1366 "_Pragma takes a parenthesized string literal");
1369 /* Ignore #sccs on all systems. */
1371 do_sccs (cpp_reader *pfile ATTRIBUTE_UNUSED)
1375 /* Handle #ifdef. */
1377 do_ifdef (cpp_reader *pfile)
1381 if (! pfile->state.skipping)
1383 const cpp_hashnode *node = lex_macro_node (pfile);
1387 skip = node->type != NT_MACRO;
1388 _cpp_mark_macro_used (node);
1393 push_conditional (pfile, skip, T_IFDEF, 0);
1396 /* Handle #ifndef. */
1398 do_ifndef (cpp_reader *pfile)
1401 const cpp_hashnode *node = 0;
1403 if (! pfile->state.skipping)
1405 node = lex_macro_node (pfile);
1409 skip = node->type == NT_MACRO;
1410 _cpp_mark_macro_used (node);
1415 push_conditional (pfile, skip, T_IFNDEF, node);
1418 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1419 pfile->mi_ind_cmacro so we can handle multiple-include
1420 optimizations. If macro expansion occurs in the expression, we
1421 cannot treat it as a controlling conditional, since the expansion
1422 could change in the future. That is handled by cpp_get_token. */
1424 do_if (cpp_reader *pfile)
1428 if (! pfile->state.skipping)
1429 skip = _cpp_parse_expr (pfile) == false;
1431 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1434 /* Flip skipping state if appropriate and continue without changing
1435 if_stack; this is so that the error message for missing #endif's
1436 etc. will point to the original #if. */
1438 do_else (cpp_reader *pfile)
1440 cpp_buffer *buffer = pfile->buffer;
1441 struct if_stack *ifs = buffer->if_stack;
1444 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1447 if (ifs->type == T_ELSE)
1449 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1450 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1451 "the conditional began here");
1455 /* Skip any future (erroneous) #elses or #elifs. */
1456 pfile->state.skipping = ifs->skip_elses;
1457 ifs->skip_elses = true;
1459 /* Invalidate any controlling macro. */
1462 /* Only check EOL if was not originally skipping. */
1463 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1468 /* Handle a #elif directive by not changing if_stack either. See the
1469 comment above do_else. */
1471 do_elif (cpp_reader *pfile)
1473 cpp_buffer *buffer = pfile->buffer;
1474 struct if_stack *ifs = buffer->if_stack;
1477 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1480 if (ifs->type == T_ELSE)
1482 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1483 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1484 "the conditional began here");
1488 /* Only evaluate this if we aren't skipping elses. During
1489 evaluation, set skipping to false to get lexer warnings. */
1490 if (ifs->skip_elses)
1491 pfile->state.skipping = 1;
1494 pfile->state.skipping = 0;
1495 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1496 ifs->skip_elses = ! pfile->state.skipping;
1499 /* Invalidate any controlling macro. */
1504 /* #endif pops the if stack and resets pfile->state.skipping. */
1506 do_endif (cpp_reader *pfile)
1508 cpp_buffer *buffer = pfile->buffer;
1509 struct if_stack *ifs = buffer->if_stack;
1512 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1515 /* Only check EOL if was not originally skipping. */
1516 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1519 /* If potential control macro, we go back outside again. */
1520 if (ifs->next == 0 && ifs->mi_cmacro)
1522 pfile->mi_valid = true;
1523 pfile->mi_cmacro = ifs->mi_cmacro;
1526 buffer->if_stack = ifs->next;
1527 pfile->state.skipping = ifs->was_skipping;
1528 obstack_free (&pfile->buffer_ob, ifs);
1532 /* Push an if_stack entry for a preprocessor conditional, and set
1533 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1534 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1535 we need to check here that we are at the top of the file. */
1537 push_conditional (cpp_reader *pfile, int skip, int type,
1538 const cpp_hashnode *cmacro)
1540 struct if_stack *ifs;
1541 cpp_buffer *buffer = pfile->buffer;
1543 ifs = xobnew (&pfile->buffer_ob, struct if_stack);
1544 ifs->line = pfile->directive_line;
1545 ifs->next = buffer->if_stack;
1546 ifs->skip_elses = pfile->state.skipping || !skip;
1547 ifs->was_skipping = pfile->state.skipping;
1549 /* This condition is effectively a test for top-of-file. */
1550 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1551 ifs->mi_cmacro = cmacro;
1555 pfile->state.skipping = skip;
1556 buffer->if_stack = ifs;
1559 /* Read the tokens of the answer into the macro pool, in a directive
1560 of type TYPE. Only commit the memory if we intend it as permanent
1561 storage, i.e. the #assert case. Returns 0 on success, and sets
1562 ANSWERP to point to the answer. */
1564 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1566 const cpp_token *paren;
1567 struct answer *answer;
1568 unsigned int acount;
1570 /* In a conditional, it is legal to not have an open paren. We
1571 should save the following token in this case. */
1572 paren = cpp_get_token (pfile);
1574 /* If not a paren, see if we're OK. */
1575 if (paren->type != CPP_OPEN_PAREN)
1577 /* In a conditional no answer is a test for any answer. It
1578 could be followed by any token. */
1581 _cpp_backup_tokens (pfile, 1);
1585 /* #unassert with no answer is valid - it removes all answers. */
1586 if (type == T_UNASSERT && paren->type == CPP_EOF)
1589 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1593 for (acount = 0;; acount++)
1596 const cpp_token *token = cpp_get_token (pfile);
1599 if (token->type == CPP_CLOSE_PAREN)
1602 if (token->type == CPP_EOF)
1604 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1608 /* struct answer includes the space for one token. */
1609 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1611 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1612 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1614 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1617 /* Drop whitespace at start, for answer equivalence purposes. */
1619 dest->flags &= ~PREV_WHITE;
1624 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1628 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1629 answer->count = acount;
1630 answer->next = NULL;
1636 /* Parses an assertion directive of type TYPE, returning a pointer to
1637 the hash node of the predicate, or 0 on error. If an answer was
1638 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1639 static cpp_hashnode *
1640 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1642 cpp_hashnode *result = 0;
1643 const cpp_token *predicate;
1645 /* We don't expand predicates or answers. */
1646 pfile->state.prevent_expansion++;
1649 predicate = cpp_get_token (pfile);
1650 if (predicate->type == CPP_EOF)
1651 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1652 else if (predicate->type != CPP_NAME)
1653 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1654 else if (parse_answer (pfile, answerp, type) == 0)
1656 unsigned int len = NODE_LEN (predicate->val.node);
1657 unsigned char *sym = alloca (len + 1);
1659 /* Prefix '#' to get it out of macro namespace. */
1661 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1662 result = cpp_lookup (pfile, sym, len + 1);
1665 pfile->state.prevent_expansion--;
1669 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1670 or a pointer to NULL if the answer is not in the chain. */
1671 static struct answer **
1672 find_answer (cpp_hashnode *node, const struct answer *candidate)
1675 struct answer **result;
1677 for (result = &node->value.answers; *result; result = &(*result)->next)
1679 struct answer *answer = *result;
1681 if (answer->count == candidate->count)
1683 for (i = 0; i < answer->count; i++)
1684 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1687 if (i == answer->count)
1695 /* Test an assertion within a preprocessor conditional. Returns
1696 nonzero on failure, zero on success. On success, the result of
1697 the test is written into VALUE, otherwise the value 0. */
1699 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1701 struct answer *answer;
1704 node = parse_assertion (pfile, &answer, T_IF);
1706 /* For recovery, an erroneous assertion expression is handled as a
1707 failing assertion. */
1711 *value = (node->type == NT_ASSERTION &&
1712 (answer == 0 || *find_answer (node, answer) != 0));
1713 else if (pfile->cur_token[-1].type == CPP_EOF)
1714 _cpp_backup_tokens (pfile, 1);
1716 /* We don't commit the memory for the answer - it's temporary only. */
1720 /* Handle #assert. */
1722 do_assert (cpp_reader *pfile)
1724 struct answer *new_answer;
1727 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1730 /* Place the new answer in the answer list. First check there
1731 is not a duplicate. */
1732 new_answer->next = 0;
1733 if (node->type == NT_ASSERTION)
1735 if (*find_answer (node, new_answer))
1737 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1738 NODE_NAME (node) + 1);
1741 new_answer->next = node->value.answers;
1744 node->type = NT_ASSERTION;
1745 node->value.answers = new_answer;
1746 BUFF_FRONT (pfile->a_buff) += (sizeof (struct answer)
1747 + (new_answer->count - 1)
1748 * sizeof (cpp_token));
1753 /* Handle #unassert. */
1755 do_unassert (cpp_reader *pfile)
1758 struct answer *answer;
1760 node = parse_assertion (pfile, &answer, T_UNASSERT);
1761 /* It isn't an error to #unassert something that isn't asserted. */
1762 if (node && node->type == NT_ASSERTION)
1766 struct answer **p = find_answer (node, answer), *temp;
1768 /* Remove the answer from the list. */
1773 /* Did we free the last answer? */
1774 if (node->value.answers == 0)
1775 node->type = NT_VOID;
1780 _cpp_free_definition (node);
1783 /* We don't commit the memory for the answer - it's temporary only. */
1786 /* These are for -D, -U, -A. */
1788 /* Process the string STR as if it appeared as the body of a #define.
1789 If STR is just an identifier, define it with value 1.
1790 If STR has anything after the identifier, then it should
1791 be identifier=definition. */
1793 cpp_define (cpp_reader *pfile, const char *str)
1798 /* Copy the entire option so we can modify it.
1799 Change the first "=" in the string to a space. If there is none,
1800 tack " 1" on the end. */
1802 count = strlen (str);
1803 buf = alloca (count + 3);
1804 memcpy (buf, str, count);
1806 p = strchr (str, '=');
1816 run_directive (pfile, T_DEFINE, buf, count);
1819 /* Slight variant of the above for use by initialize_builtins. */
1821 _cpp_define_builtin (cpp_reader *pfile, const char *str)
1823 size_t len = strlen (str);
1824 char *buf = alloca (len + 1);
1825 memcpy (buf, str, len);
1827 run_directive (pfile, T_DEFINE, buf, len);
1830 /* Process MACRO as if it appeared as the body of an #undef. */
1832 cpp_undef (cpp_reader *pfile, const char *macro)
1834 size_t len = strlen (macro);
1835 char *buf = alloca (len + 1);
1836 memcpy (buf, macro, len);
1838 run_directive (pfile, T_UNDEF, buf, len);
1841 /* Process the string STR as if it appeared as the body of a #assert. */
1843 cpp_assert (cpp_reader *pfile, const char *str)
1845 handle_assertion (pfile, str, T_ASSERT);
1848 /* Process STR as if it appeared as the body of an #unassert. */
1850 cpp_unassert (cpp_reader *pfile, const char *str)
1852 handle_assertion (pfile, str, T_UNASSERT);
1855 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
1857 handle_assertion (cpp_reader *pfile, const char *str, int type)
1859 size_t count = strlen (str);
1860 const char *p = strchr (str, '=');
1862 /* Copy the entire option so we can modify it. Change the first
1863 "=" in the string to a '(', and tack a ')' on the end. */
1864 char *buf = alloca (count + 2);
1866 memcpy (buf, str, count);
1875 run_directive (pfile, type, str, count);
1878 /* The number of errors for a given reader. */
1880 cpp_errors (cpp_reader *pfile)
1882 return pfile->errors;
1885 /* The options structure. */
1887 cpp_get_options (cpp_reader *pfile)
1889 return &pfile->opts;
1892 /* The callbacks structure. */
1894 cpp_get_callbacks (cpp_reader *pfile)
1899 /* Copy the given callbacks structure to our own. */
1901 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
1906 /* The dependencies structure. (Creates one if it hasn't already been.) */
1908 cpp_get_deps (cpp_reader *pfile)
1911 pfile->deps = deps_init ();
1915 /* Push a new buffer on the buffer stack. Returns the new buffer; it
1916 doesn't fail. It does not generate a file change call back; that
1917 is the responsibility of the caller. */
1919 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
1922 cpp_buffer *new = xobnew (&pfile->buffer_ob, cpp_buffer);
1924 /* Clears, amongst other things, if_stack and mi_cmacro. */
1925 memset (new, 0, sizeof (cpp_buffer));
1927 new->next_line = new->buf = buffer;
1928 new->rlimit = buffer + len;
1929 new->from_stage3 = from_stage3;
1930 new->prev = pfile->buffer;
1931 new->need_line = true;
1933 pfile->buffer = new;
1938 /* Pops a single buffer, with a file change call-back if appropriate.
1939 Then pushes the next -include file, if any remain. */
1941 _cpp_pop_buffer (cpp_reader *pfile)
1943 cpp_buffer *buffer = pfile->buffer;
1944 struct _cpp_file *inc = buffer->file;
1945 struct if_stack *ifs;
1947 /* Walk back up the conditional stack till we reach its level at
1948 entry to this file, issuing error messages. */
1949 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
1950 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1951 "unterminated #%s", dtable[ifs->type].name);
1953 /* In case of a missing #endif. */
1954 pfile->state.skipping = 0;
1956 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
1957 pfile->buffer = buffer->prev;
1959 free (buffer->notes);
1961 /* Free the buffer object now; we may want to push a new buffer
1962 in _cpp_push_next_include_file. */
1963 obstack_free (&pfile->buffer_ob, buffer);
1967 _cpp_pop_file_buffer (pfile, inc);
1969 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
1973 /* Enter all recognized directives in the hash table. */
1975 _cpp_init_directives (cpp_reader *pfile)
1980 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
1982 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1983 node->is_directive = 1;
1984 node->directive_index = i;