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, 2005, 2006
4 Free Software Foundation, Inc.
5 Contributed by Per Bothner, 1994-95.
6 Based on CCCP program by Paul Rubin, June 1986
7 Adapted to ANSI C, Richard Stallman, Jan 1987
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
34 struct if_stack *next;
35 unsigned int line; /* Line where condition started. */
36 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
37 bool skip_elses; /* Can future #else / #elif be skipped? */
38 bool was_skipping; /* If were skipping on entry. */
39 int type; /* Most recent conditional for diagnostics. */
42 /* Contains a registered pragma or pragma namespace. */
43 typedef void (*pragma_cb) (cpp_reader *);
46 struct pragma_entry *next;
47 const cpp_hashnode *pragma; /* Name and length. */
53 struct pragma_entry *space;
57 /* Values for the origin field of struct directive. KANDR directives
58 come from traditional (K&R) C. STDC89 directives come from the
59 1989 C standard. EXTENSION directives are extensions. */
64 /* Values for the flags field of struct directive. COND indicates a
65 conditional; IF_COND an opening conditional. INCL means to treat
66 "..." and <...> as q-char and h-char sequences respectively. IN_I
67 means this directive should be handled even if -fpreprocessed is in
68 effect (these are the directives with callback hooks).
70 EXPAND is set on directives that are always macro-expanded. */
72 #define IF_COND (1 << 1)
75 #define EXPAND (1 << 4)
77 /* Defines one #-directive, including how to handle it. */
78 typedef void (*directive_handler) (cpp_reader *);
79 typedef struct directive directive;
82 directive_handler handler; /* Function to handle directive. */
83 const uchar *name; /* Name of directive. */
84 unsigned short length; /* Length of name. */
85 unsigned char origin; /* Origin of directive. */
86 unsigned char flags; /* Flags describing this directive. */
89 /* Forward declarations. */
91 static void skip_rest_of_line (cpp_reader *);
92 static void check_eol (cpp_reader *);
93 static void start_directive (cpp_reader *);
94 static void prepare_directive_trad (cpp_reader *);
95 static void end_directive (cpp_reader *, int);
96 static void directive_diagnostics (cpp_reader *, const directive *, int);
97 static void run_directive (cpp_reader *, int, const char *, size_t);
98 static char *glue_header_name (cpp_reader *);
99 static const char *parse_include (cpp_reader *, int *, const cpp_token ***);
100 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
101 static unsigned int read_flag (cpp_reader *, unsigned int);
102 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
103 static void do_diagnostic (cpp_reader *, int, int);
104 static cpp_hashnode *lex_macro_node (cpp_reader *);
105 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
106 static void do_include_common (cpp_reader *, enum include_type);
107 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
108 const cpp_hashnode *);
109 static struct pragma_entry *insert_pragma_entry (cpp_reader *,
110 struct pragma_entry **,
111 const cpp_hashnode *,
114 static void register_pragma (cpp_reader *, const char *, const char *,
115 pragma_cb, bool, bool);
116 static int count_registered_pragmas (struct pragma_entry *);
117 static char ** save_registered_pragmas (struct pragma_entry *, char **);
118 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
120 static void do_pragma_once (cpp_reader *);
121 static void do_pragma_poison (cpp_reader *);
122 static void do_pragma_system_header (cpp_reader *);
123 static void do_pragma_dependency (cpp_reader *);
124 static void do_pragma_sdcc_hash (cpp_reader *pfile);
125 static void do_pragma_preproc_asm (cpp_reader *pfile);
126 static void do_pragma_pedantic_parse_number (cpp_reader *pfile);
127 static void do_linemarker (cpp_reader *);
128 static const cpp_token *get_token_no_padding (cpp_reader *);
129 static const cpp_token *get__Pragma_string (cpp_reader *);
130 static void destringize_and_run (cpp_reader *, const cpp_string *);
131 static int parse_answer (cpp_reader *, struct answer **, int);
132 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
133 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
134 static void handle_assertion (cpp_reader *, const char *, int);
136 /* This is the table of directive handlers. It is ordered by
137 frequency of occurrence; the numbers at the end are directive
138 counts from all the source code I have lying around (egcs and libc
139 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
140 pcmcia-cs-3.0.9). This is no longer important as directive lookup
141 is now O(1). All extensions other than #warning and #include_next
142 are deprecated. The name is where the extension appears to have
145 #define DIRECTIVE_TABLE \
146 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
147 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
148 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
149 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
150 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
151 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
152 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
153 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
154 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
155 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
156 D(error, T_ERROR, STDC89, 0) /* 475 */ \
157 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
158 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
159 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
160 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
161 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
162 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
163 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
164 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
166 /* #sccs is synonymous with #ident. */
167 #define do_sccs do_ident
169 /* Use the table to generate a series of prototypes, an enum for the
170 directive names, and an array of directive handlers. */
172 #define D(name, t, o, f) static void do_##name (cpp_reader *);
176 #define D(n, tag, o, f) tag,
184 #define D(name, t, origin, flags) \
185 { do_##name, (const uchar *) #name, \
186 sizeof #name - 1, origin, flags },
187 static const directive dtable[] =
192 #undef DIRECTIVE_TABLE
194 /* Wrapper struct directive for linemarkers.
195 The origin is more or less true - the original K+R cpp
196 did use this notation in its preprocessed output. */
197 static const directive linemarker_dir =
199 do_linemarker, U"#", 1, KANDR, IN_I
202 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
204 /* Skip any remaining tokens in a directive. */
206 skip_rest_of_line (cpp_reader *pfile)
208 /* Discard all stacked contexts. */
209 while (pfile->context->prev)
210 _cpp_pop_context (pfile);
212 /* Sweep up all tokens remaining on the line. */
214 while (_cpp_lex_token (pfile)->type != CPP_EOF)
218 /* Ensure there are no stray tokens at the end of a directive. */
220 check_eol (cpp_reader *pfile)
222 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
223 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
224 pfile->directive->name);
227 /* Ensure there are no stray tokens other than comments at the end of
228 a directive, and gather the comments. */
229 static const cpp_token **
230 check_eol_return_comments (cpp_reader *pfile)
234 const cpp_token **buf;
236 buf = XNEWVEC (const cpp_token *, capacity);
242 const cpp_token *tok;
244 tok = _cpp_lex_token (pfile);
245 if (tok->type == CPP_EOF)
247 if (tok->type != CPP_COMMENT)
248 cpp_error (pfile, CPP_DL_PEDWARN,
249 "extra tokens at end of #%s directive",
250 pfile->directive->name);
253 if (c + 1 >= capacity)
256 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
267 /* Called when entering a directive, _Pragma or command-line directive. */
269 start_directive (cpp_reader *pfile)
271 /* Setup in-directive state. */
272 pfile->state.in_directive = 1;
273 pfile->state.save_comments = 0;
274 pfile->directive_result.type = CPP_PADDING;
276 /* Some handlers need the position of the # for diagnostics. */
277 pfile->directive_line = pfile->line_table->highest_line;
280 /* Called when leaving a directive, _Pragma or command-line directive. */
282 end_directive (cpp_reader *pfile, int skip_line)
284 if (CPP_OPTION (pfile, traditional))
286 /* Revert change of prepare_directive_trad. */
287 pfile->state.prevent_expansion--;
289 if (pfile->directive != &dtable[T_DEFINE])
290 _cpp_remove_overlay (pfile);
292 /* We don't skip for an assembler #. */
295 skip_rest_of_line (pfile);
296 if (!pfile->keep_tokens)
298 pfile->cur_run = &pfile->base_run;
299 pfile->cur_token = pfile->base_run.base;
304 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
305 pfile->state.in_directive = 0;
306 pfile->state.in_expression = 0;
307 pfile->state.angled_headers = 0;
308 pfile->directive = 0;
311 /* Prepare to handle the directive in pfile->directive. */
313 prepare_directive_trad (cpp_reader *pfile)
315 if (pfile->directive != &dtable[T_DEFINE])
317 bool no_expand = (pfile->directive
318 && ! (pfile->directive->flags & EXPAND));
319 bool was_skipping = pfile->state.skipping;
321 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
322 || pfile->directive == &dtable[T_ELIF]);
323 if (pfile->state.in_expression)
324 pfile->state.skipping = false;
327 pfile->state.prevent_expansion++;
328 _cpp_scan_out_logical_line (pfile, NULL);
330 pfile->state.prevent_expansion--;
332 pfile->state.skipping = was_skipping;
333 _cpp_overlay_buffer (pfile, pfile->out.base,
334 pfile->out.cur - pfile->out.base);
337 /* Stop ISO C from expanding anything. */
338 pfile->state.prevent_expansion++;
341 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
342 the '#' was indented. */
344 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
346 /* Issue -pedantic warnings for extensions. */
347 if (CPP_PEDANTIC (pfile)
348 && ! pfile->state.skipping
349 && dir->origin == EXTENSION)
350 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
352 /* Traditionally, a directive is ignored unless its # is in
353 column 1. Therefore in code intended to work with K+R
354 compilers, directives added by C89 must have their #
355 indented, and directives present in traditional C must not.
356 This is true even of directives in skipped conditional
357 blocks. #elif cannot be used at all. */
358 if (CPP_WTRADITIONAL (pfile))
360 if (dir == &dtable[T_ELIF])
361 cpp_error (pfile, CPP_DL_WARNING,
362 "suggest not using #elif in traditional C");
363 else if (indented && dir->origin == KANDR)
364 cpp_error (pfile, CPP_DL_WARNING,
365 "traditional C ignores #%s with the # indented",
367 else if (!indented && dir->origin != KANDR)
368 cpp_error (pfile, CPP_DL_WARNING,
369 "suggest hiding #%s from traditional C with an indented #",
374 /* Check if we have a known directive. INDENTED is nonzero if the
375 '#' of the directive was indented. This function is in this file
376 to save unnecessarily exporting dtable etc. to lex.c. Returns
377 nonzero if the line of tokens has been handled, zero if we should
378 continue processing the line. */
380 _cpp_handle_directive (cpp_reader *pfile, int indented)
382 const directive *dir = 0;
383 const cpp_token *dname;
384 bool was_parsing_args = pfile->state.parsing_args;
385 bool was_discarding_output = pfile->state.discarding_output;
388 if (was_discarding_output)
389 pfile->state.prevent_expansion = 0;
391 if (was_parsing_args)
393 if (CPP_OPTION (pfile, pedantic))
394 cpp_error (pfile, CPP_DL_PEDWARN,
395 "embedding a directive within macro arguments is not portable");
396 pfile->state.parsing_args = 0;
397 pfile->state.prevent_expansion = 0;
399 start_directive (pfile);
400 dname = _cpp_lex_token (pfile);
402 if (dname->type == CPP_NAME)
404 if (dname->val.node->is_directive)
405 dir = &dtable[dname->val.node->directive_index];
407 /* We do not recognize the # followed by a number extension in
409 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
411 dir = &linemarker_dir;
412 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
413 && ! pfile->state.skipping)
414 cpp_error (pfile, CPP_DL_PEDWARN,
415 "style of line directive is a GCC extension");
420 /* If we have a directive that is not an opening conditional,
421 invalidate any control macro. */
422 if (! (dir->flags & IF_COND))
423 pfile->mi_valid = false;
425 /* Kluge alert. In order to be sure that code like this
430 does not cause '#define foo bar' to get executed when
431 compiled with -save-temps, we recognize directives in
432 -fpreprocessed mode only if the # is in column 1. macro.c
433 puts a space in front of any '#' at the start of a macro. */
434 if (CPP_OPTION (pfile, preprocessed)
435 && (indented || !(dir->flags & IN_I)))
442 /* In failed conditional groups, all non-conditional
443 directives are ignored. Before doing that, whether
444 skipping or not, we should lex angle-bracketed headers
445 correctly, and maybe output some diagnostics. */
446 pfile->state.angled_headers = dir->flags & INCL;
447 pfile->state.directive_wants_padding = dir->flags & INCL;
448 if (! CPP_OPTION (pfile, preprocessed))
449 directive_diagnostics (pfile, dir, indented);
450 if (pfile->state.skipping && !(dir->flags & COND))
454 else if (dname->type == CPP_EOF)
455 ; /* CPP_EOF is the "null directive". */
458 /* An unknown directive. Don't complain about it in assembly
459 source: we don't know where the comments are, and # may
460 introduce assembler pseudo-ops. Don't complain about invalid
461 directives in skipped conditional groups (6.10 p4). */
462 if (CPP_OPTION (pfile, lang) == CLK_ASM)
464 else if (!pfile->state.skipping)
465 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
466 cpp_token_as_text (pfile, dname));
469 pfile->directive = dir;
470 if (CPP_OPTION (pfile, traditional))
471 prepare_directive_trad (pfile);
474 pfile->directive->handler (pfile);
476 _cpp_backup_tokens (pfile, 1);
478 end_directive (pfile, skip);
479 if (was_parsing_args)
481 /* Restore state when within macro args. */
482 pfile->state.parsing_args = 2;
483 pfile->state.prevent_expansion = 1;
485 if (was_discarding_output)
486 pfile->state.prevent_expansion = 1;
490 /* Directive handler wrapper used by the command line option
491 processor. BUF is \n terminated. */
493 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
495 cpp_push_buffer (pfile, (const uchar *) buf, count,
496 /* from_stage3 */ true);
497 /* Disgusting hack. */
498 if (dir_no == T_PRAGMA && pfile->buffer->prev)
499 pfile->buffer->file = pfile->buffer->prev->file;
500 start_directive (pfile);
502 /* This is a short-term fix to prevent a leading '#' being
503 interpreted as a directive. */
504 _cpp_clean_line (pfile);
506 pfile->directive = &dtable[dir_no];
507 if (CPP_OPTION (pfile, traditional))
508 prepare_directive_trad (pfile);
509 pfile->directive->handler (pfile);
510 end_directive (pfile, 1);
511 if (dir_no == T_PRAGMA)
512 pfile->buffer->file = NULL;
513 _cpp_pop_buffer (pfile);
516 /* Checks for validity the macro name in #define, #undef, #ifdef and
517 #ifndef directives. */
518 static cpp_hashnode *
519 lex_macro_node (cpp_reader *pfile)
521 const cpp_token *token = _cpp_lex_token (pfile);
523 /* The token immediately after #define must be an identifier. That
524 identifier may not be "defined", per C99 6.10.8p4.
525 In C++, it may not be any of the "named operators" either,
526 per C++98 [lex.digraph], [lex.key].
527 Finally, the identifier may not have been poisoned. (In that case
528 the lexer has issued the error message for us.) */
530 if (token->type == CPP_NAME)
532 cpp_hashnode *node = token->val.node;
534 if (node == pfile->spec_nodes.n_defined)
535 cpp_error (pfile, CPP_DL_ERROR,
536 "\"defined\" cannot be used as a macro name");
537 else if (! (node->flags & NODE_POISONED))
540 else if (token->flags & NAMED_OP)
541 cpp_error (pfile, CPP_DL_ERROR,
542 "\"%s\" cannot be used as a macro name as it is an operator in C++",
543 NODE_NAME (token->val.node));
544 else if (token->type == CPP_EOF)
545 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
546 pfile->directive->name);
548 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
553 /* Process a #define directive. Most work is done in macro.c. */
555 do_define (cpp_reader *pfile)
557 cpp_hashnode *node = lex_macro_node (pfile);
561 /* If we have been requested to expand comments into macros,
562 then re-enable saving of comments. */
563 pfile->state.save_comments =
564 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
566 if (_cpp_create_definition (pfile, node))
567 if (pfile->cb.define)
568 pfile->cb.define (pfile, pfile->directive_line, node);
572 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
574 do_undef (cpp_reader *pfile)
576 cpp_hashnode *node = lex_macro_node (pfile);
581 pfile->cb.undef (pfile, pfile->directive_line, node);
583 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
584 identifier is not currently defined as a macro name. */
585 if (node->type == NT_MACRO)
587 if (node->flags & NODE_WARN)
588 cpp_error (pfile, CPP_DL_WARNING,
589 "undefining \"%s\"", NODE_NAME (node));
591 if (CPP_OPTION (pfile, warn_unused_macros))
592 _cpp_warn_if_unused_macro (pfile, node, NULL);
594 _cpp_free_definition (node);
601 /* Undefine a single macro/assertion/whatever. */
604 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
605 void *data_p ATTRIBUTE_UNUSED)
607 /* Body of _cpp_free_definition inlined here for speed.
608 Macros and assertions no longer have anything to free. */
610 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
614 /* Undefine all macros and assertions. */
617 cpp_undef_all (cpp_reader *pfile)
619 cpp_forall_identifiers (pfile, undefine_macros, NULL);
623 /* Helper routine used by parse_include. Reinterpret the current line
624 as an h-char-sequence (< ... >); we are looking at the first token
625 after the <. Returns a malloced filename. */
627 glue_header_name (cpp_reader *pfile)
629 const cpp_token *token;
631 size_t len, total_len = 0, capacity = 1024;
633 /* To avoid lexed tokens overwriting our glued name, we can only
634 allocate from the string pool once we've lexed everything. */
635 buffer = XNEWVEC (char, capacity);
638 token = get_token_no_padding (pfile);
640 if (token->type == CPP_GREATER)
642 if (token->type == CPP_EOF)
644 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
648 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
649 if (total_len + len > capacity)
651 capacity = (capacity + len) * 2;
652 buffer = XRESIZEVEC (char, buffer, capacity);
655 if (token->flags & PREV_WHITE)
656 buffer[total_len++] = ' ';
658 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
663 buffer[total_len] = '\0';
667 /* Returns the file name of #include, #include_next, #import and
668 #pragma dependency. The string is malloced and the caller should
669 free it. Returns NULL on error. */
671 parse_include (cpp_reader *pfile, int *pangle_brackets,
672 const cpp_token ***buf)
675 const cpp_token *header;
677 /* Allow macro expansion. */
678 header = get_token_no_padding (pfile);
679 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
681 fname = XNEWVEC (char, header->val.str.len - 1);
682 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
683 fname[header->val.str.len - 2] = '\0';
684 *pangle_brackets = header->type == CPP_HEADER_NAME;
686 else if (header->type == CPP_LESS)
688 fname = glue_header_name (pfile);
689 *pangle_brackets = 1;
693 const unsigned char *dir;
695 if (pfile->directive == &dtable[T_PRAGMA])
696 dir = U"pragma dependency";
698 dir = pfile->directive->name;
699 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
705 if (buf == NULL || CPP_OPTION (pfile, discard_comments))
709 /* If we are not discarding comments, then gather them while
710 doing the eol check. */
711 *buf = check_eol_return_comments (pfile);
717 /* Handle #include, #include_next and #import. */
719 do_include_common (cpp_reader *pfile, enum include_type type)
723 const cpp_token **buf = NULL;
725 /* Re-enable saving of comments if requested, so that the include
726 callback can dump comments which follow #include. */
727 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
729 fname = parse_include (pfile, &angle_brackets, &buf);
739 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
740 pfile->directive->name);
747 /* Prevent #include recursion. */
748 if (pfile->line_table->depth >= CPP_STACK_MAX)
749 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
752 /* Get out of macro context, if we are. */
753 skip_rest_of_line (pfile);
755 if (pfile->cb.include)
756 pfile->cb.include (pfile, pfile->directive_line,
757 pfile->directive->name, fname, angle_brackets,
760 _cpp_stack_include (pfile, fname, angle_brackets, type);
769 do_include (cpp_reader *pfile)
771 do_include_common (pfile, IT_INCLUDE);
775 do_import (cpp_reader *pfile)
777 do_include_common (pfile, IT_IMPORT);
781 do_include_next (cpp_reader *pfile)
783 enum include_type type = IT_INCLUDE_NEXT;
785 /* If this is the primary source file, warn and use the normal
787 if (! pfile->buffer->prev)
789 cpp_error (pfile, CPP_DL_WARNING,
790 "#include_next in primary source file");
793 do_include_common (pfile, type);
796 /* Subroutine of do_linemarker. Read possible flags after file name.
797 LAST is the last flag seen; 0 if this is the first flag. Return the
798 flag if it is valid, 0 at the end of the directive. Otherwise
801 read_flag (cpp_reader *pfile, unsigned int last)
803 const cpp_token *token = _cpp_lex_token (pfile);
805 if (token->type == CPP_NUMBER && token->val.str.len == 1)
807 unsigned int flag = token->val.str.text[0] - '0';
809 if (flag > last && flag <= 4
810 && (flag != 4 || last == 3)
811 && (flag != 2 || last == 0))
815 if (token->type != CPP_EOF)
816 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
817 cpp_token_as_text (pfile, token));
821 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
822 of length LEN, to binary; store it in NUMP, and return 0 if the
823 number was well-formed, 1 if not. Temporary, hopefully. */
825 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
827 unsigned long reg = 0;
841 /* Interpret #line command.
842 Note that the filename string (if any) is a true string constant
843 (escapes are interpreted), unlike in #line. */
845 do_line (cpp_reader *pfile)
847 const struct line_maps *line_table = pfile->line_table;
848 const struct line_map *map = &line_table->maps[line_table->used - 1];
850 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
853 unsigned char map_sysp = map->sysp;
854 const cpp_token *token;
855 const char *new_file = map->to_file;
856 unsigned long new_lineno;
858 /* C99 raised the minimum limit on #line numbers. */
859 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
861 /* #line commands expand macros. */
862 token = cpp_get_token (pfile);
863 if (token->type != CPP_NUMBER
864 || strtoul_for_line (token->val.str.text, token->val.str.len,
867 cpp_error (pfile, CPP_DL_ERROR,
868 "\"%s\" after #line is not a positive integer",
869 cpp_token_as_text (pfile, token));
873 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
874 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
876 token = cpp_get_token (pfile);
877 if (token->type == CPP_STRING)
879 cpp_string s = { 0, 0 };
880 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
882 new_file = (const char *)s.text;
885 else if (token->type != CPP_EOF)
887 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
888 cpp_token_as_text (pfile, token));
892 skip_rest_of_line (pfile);
893 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
897 /* Interpret the # 44 "file" [flags] notation, which has slightly
898 different syntax and semantics from #line: Flags are allowed,
899 and we never complain about the line number being too big. */
901 do_linemarker (cpp_reader *pfile)
903 const struct line_maps *line_table = pfile->line_table;
904 const struct line_map *map = &line_table->maps[line_table->used - 1];
905 const cpp_token *token;
906 const char *new_file = map->to_file;
907 unsigned long new_lineno;
908 unsigned int new_sysp = map->sysp;
909 enum lc_reason reason = LC_RENAME;
912 /* Back up so we can get the number again. Putting this in
913 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
914 some circumstances, which can segfault. */
915 _cpp_backup_tokens (pfile, 1);
917 /* #line commands expand macros. */
918 token = cpp_get_token (pfile);
919 if (token->type != CPP_NUMBER
920 || strtoul_for_line (token->val.str.text, token->val.str.len,
923 cpp_error (pfile, CPP_DL_ERROR,
924 "\"%s\" after # is not a positive integer",
925 cpp_token_as_text (pfile, token));
929 token = cpp_get_token (pfile);
930 if (token->type == CPP_STRING)
932 cpp_string s = { 0, 0 };
933 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
935 new_file = (const char *)s.text;
938 flag = read_flag (pfile, 0);
942 /* Fake an include for cpp_included (). */
943 _cpp_fake_include (pfile, new_file);
944 flag = read_flag (pfile, flag);
949 flag = read_flag (pfile, flag);
954 flag = read_flag (pfile, flag);
957 pfile->buffer->sysp = new_sysp;
962 else if (token->type != CPP_EOF)
964 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
965 cpp_token_as_text (pfile, token));
969 skip_rest_of_line (pfile);
970 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
973 /* Arrange the file_change callback. pfile->line has changed to
974 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
975 header, 2 for a system header that needs to be extern "C" protected,
976 and zero otherwise. */
978 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
979 const char *to_file, unsigned int file_line,
982 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
985 linemap_line_start (pfile->line_table, map->to_line, 127);
987 if (pfile->cb.file_change)
988 pfile->cb.file_change (pfile, map);
991 /* Report a warning or error detected by the program we are
992 processing. Use the directive's tokens in the error message. */
994 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
996 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
999 fprintf (stderr, "#%s ", pfile->directive->name);
1000 pfile->state.prevent_expansion++;
1001 cpp_output_line (pfile, stderr);
1002 pfile->state.prevent_expansion--;
1007 do_error (cpp_reader *pfile)
1009 do_diagnostic (pfile, CPP_DL_ERROR, 1);
1013 do_warning (cpp_reader *pfile)
1015 /* We want #warning diagnostics to be emitted in system headers too. */
1016 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1019 /* Report program identification. */
1021 do_ident (cpp_reader *pfile)
1023 const cpp_token *str = cpp_get_token (pfile);
1025 if (str->type != CPP_STRING)
1026 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1027 pfile->directive->name);
1028 else if (pfile->cb.ident)
1029 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1034 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1035 matching entry, or NULL if none is found. The returned entry could
1036 be the start of a namespace chain, or a pragma. */
1037 static struct pragma_entry *
1038 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1040 while (chain && chain->pragma != pragma)
1041 chain = chain->next;
1046 /* Create and insert a pragma entry for NAME at the beginning of a
1047 singly-linked CHAIN. If handler is NULL, it is a namespace,
1048 otherwise it is a pragma and its handler. If INTERNAL is true
1049 this pragma is being inserted by libcpp itself. */
1050 static struct pragma_entry *
1051 insert_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain,
1052 const cpp_hashnode *pragma, pragma_cb handler,
1053 bool allow_expansion, bool internal)
1055 struct pragma_entry *new_entry;
1057 new_entry = (struct pragma_entry *)
1058 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1059 new_entry->pragma = pragma;
1062 new_entry->is_nspace = 0;
1063 new_entry->u.handler = handler;
1067 new_entry->is_nspace = 1;
1068 new_entry->u.space = NULL;
1071 new_entry->allow_expansion = allow_expansion;
1072 new_entry->is_internal = internal;
1073 new_entry->next = *chain;
1078 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1079 goes in the global namespace. HANDLER is the handler it will call,
1080 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1081 expansion while parsing pragma NAME. INTERNAL is true if this is a
1082 pragma registered by cpplib itself, false if it is registered via
1083 cpp_register_pragma */
1085 register_pragma (cpp_reader *pfile, const char *space, const char *name,
1086 pragma_cb handler, bool allow_expansion, bool internal)
1088 struct pragma_entry **chain = &pfile->pragmas;
1089 struct pragma_entry *entry;
1090 const cpp_hashnode *node;
1097 node = cpp_lookup (pfile, U space, strlen (space));
1098 entry = lookup_pragma_entry (*chain, node);
1100 entry = insert_pragma_entry (pfile, chain, node, NULL,
1101 allow_expansion, internal);
1102 else if (!entry->is_nspace)
1104 chain = &entry->u.space;
1107 /* Check for duplicates. */
1108 node = cpp_lookup (pfile, U name, strlen (name));
1109 entry = lookup_pragma_entry (*chain, node);
1112 if (entry->is_nspace)
1114 cpp_error (pfile, CPP_DL_ICE,
1115 "registering \"%s\" as both a pragma and a pragma namespace",
1118 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1121 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1124 insert_pragma_entry (pfile, chain, node, handler, allow_expansion,
1128 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1129 goes in the global namespace. HANDLER is the handler it will call,
1130 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1131 expansion while parsing pragma NAME. This function is exported
1134 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1135 pragma_cb handler, bool allow_expansion)
1137 register_pragma (pfile, space, name, handler, allow_expansion, false);
1140 /* Register the pragmas the preprocessor itself handles. */
1142 _cpp_init_internal_pragmas (cpp_reader *pfile)
1144 /* Pragmas in the global namespace. */
1145 register_pragma (pfile, 0, "once", do_pragma_once, false, true);
1147 /* New GCC-specific pragmas should be put in the GCC namespace. */
1148 register_pragma (pfile, "GCC", "poison", do_pragma_poison, false, true);
1149 register_pragma (pfile, "GCC", "system_header", do_pragma_system_header,
1151 register_pragma (pfile, "GCC", "dependency", do_pragma_dependency,
1154 /* Kevin abuse for SDCC. */
1155 cpp_register_pragma(pfile, 0, "sdcc_hash", do_pragma_sdcc_hash, false);
1156 /* SDCC _asm specific */
1157 cpp_register_pragma(pfile, 0, "preproc_asm", do_pragma_preproc_asm, false);
1159 cpp_register_pragma(pfile, 0, "pedantic_parse_number", do_pragma_pedantic_parse_number, false);
1162 /* Return the number of registered pragmas in PE. */
1165 count_registered_pragmas (struct pragma_entry *pe)
1168 for (; pe != NULL; pe = pe->next)
1171 ct += count_registered_pragmas (pe->u.space);
1177 /* Save into SD the names of the registered pragmas referenced by PE,
1178 and return a pointer to the next free space in SD. */
1181 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1183 for (; pe != NULL; pe = pe->next)
1186 sd = save_registered_pragmas (pe->u.space, sd);
1187 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1188 HT_LEN (&pe->pragma->ident),
1189 HT_LEN (&pe->pragma->ident) + 1);
1194 /* Return a newly-allocated array which saves the names of the
1195 registered pragmas. */
1198 _cpp_save_pragma_names (cpp_reader *pfile)
1200 int ct = count_registered_pragmas (pfile->pragmas);
1201 char **result = XNEWVEC (char *, ct);
1202 (void) save_registered_pragmas (pfile->pragmas, result);
1206 /* Restore from SD the names of the registered pragmas referenced by PE,
1207 and return a pointer to the next unused name in SD. */
1210 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1213 for (; pe != NULL; pe = pe->next)
1216 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1217 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1224 /* Restore the names of the registered pragmas from SAVED. */
1227 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1229 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1233 /* Pragmata handling. We handle some, and pass the rest on to the
1234 front end. C99 defines three pragmas and says that no macro
1235 expansion is to be performed on them; whether or not macro
1236 expansion happens for other pragmas is implementation defined.
1237 This implementation never macro-expands the text after #pragma.
1239 The library user has the option of deferring execution of
1240 #pragmas not handled by cpplib, in which case they are converted
1241 to CPP_PRAGMA tokens and inserted into the output stream. */
1243 do_pragma (cpp_reader *pfile)
1245 const struct pragma_entry *p = NULL;
1246 const cpp_token *token, *pragma_token = pfile->cur_token;
1247 unsigned int count = 1;
1249 /* Save the current position so that defer_pragmas mode can
1250 copy the entire current line to a string. It will not work
1251 to use _cpp_backup_tokens as that does not reverse buffer->cur. */
1252 const uchar *line_start = CPP_BUFFER (pfile)->cur;
1254 pfile->state.prevent_expansion++;
1256 token = cpp_get_token (pfile);
1257 if (token->type == CPP_NAME)
1259 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1260 if (p && p->is_nspace)
1263 token = cpp_get_token (pfile);
1264 if (token->type == CPP_NAME)
1265 p = lookup_pragma_entry (p->u.space, token->val.node);
1273 if (p->is_internal || !CPP_OPTION (pfile, defer_pragmas))
1275 /* Since the handler below doesn't get the line number, that it
1276 might need for diagnostics, make sure it has the right
1277 numbers in place. */
1278 if (pfile->cb.line_change)
1279 (*pfile->cb.line_change) (pfile, pragma_token, false);
1280 /* Never expand macros if handling a deferred pragma, since
1281 the macro definitions now applicable may be different
1282 from those at the point the pragma appeared. */
1283 if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1284 pfile->state.prevent_expansion--;
1285 (*p->u.handler) (pfile);
1286 if (p->allow_expansion && !pfile->state.in_deferred_pragma)
1287 pfile->state.prevent_expansion++;
1291 /* Squirrel away the pragma text. Pragmas are
1292 newline-terminated. */
1293 const uchar *line_end;
1298 for (line_end = line_start; (c = *line_end) != '\n'; line_end++)
1299 if (c == '"' || c == '\'')
1301 /* Skip over string literal. */
1305 if (cc == '\\' && line_end[1] != '\n')
1307 else if (cc == '\n')
1317 if (line_end[1] == '*')
1319 /* Skip over C block comment, unless it is multi-line.
1320 When encountering multi-line block comment, terminate
1321 the pragma token right before that block comment. */
1322 const uchar *le = line_end + 2;
1324 if (*le++ == '*' && *le == '/')
1332 else if (line_end[1] == '/'
1333 && (CPP_OPTION (pfile, cplusplus_comments)
1334 || cpp_in_system_header (pfile)))
1337 while (*line_end != '\n')
1343 body.len = (line_end - line_start) + 1;
1344 s = _cpp_unaligned_alloc (pfile, body.len + 1);
1345 memcpy (s, line_start, body.len - 1);
1346 s[body.len - 1] = '\n';
1350 /* Create a CPP_PRAGMA token. */
1351 ptok = &pfile->directive_result;
1352 ptok->src_loc = pragma_token->src_loc;
1353 ptok->type = CPP_PRAGMA;
1354 ptok->flags = pragma_token->flags | NO_EXPAND;
1355 ptok->val.str = body;
1358 else if (pfile->cb.def_pragma)
1360 _cpp_backup_tokens (pfile, count);
1361 pfile->cb.def_pragma (pfile, pfile->directive_line);
1364 pfile->state.prevent_expansion--;
1367 /* Handle #pragma once. */
1369 do_pragma_once (cpp_reader *pfile)
1371 if (pfile->buffer->prev == NULL)
1372 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1375 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1378 /* Handle #pragma GCC poison, to poison one or more identifiers so
1379 that the lexer produces a hard error for each subsequent usage. */
1381 do_pragma_poison (cpp_reader *pfile)
1383 const cpp_token *tok;
1386 pfile->state.poisoned_ok = 1;
1389 tok = _cpp_lex_token (pfile);
1390 if (tok->type == CPP_EOF)
1392 if (tok->type != CPP_NAME)
1394 cpp_error (pfile, CPP_DL_ERROR,
1395 "invalid #pragma GCC poison directive");
1400 if (hp->flags & NODE_POISONED)
1403 if (hp->type == NT_MACRO)
1404 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1406 _cpp_free_definition (hp);
1407 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1409 pfile->state.poisoned_ok = 0;
1415 do_pragma_sdcc_hash (cpp_reader *pfile)
1417 const cpp_token *tok = _cpp_lex_token (pfile);
1419 if (tok->type == CPP_PLUS)
1421 CPP_OPTION(pfile, allow_naked_hash)++;
1423 else if (tok->type == CPP_MINUS)
1425 CPP_OPTION(pfile, allow_naked_hash)--;
1429 cpp_error (pfile, CPP_DL_ERROR,
1430 "invalid #pragma sdcc_hash directive, need '+' or '-'");
1435 pedantic_parse_number pragma */
1437 do_pragma_pedantic_parse_number (cpp_reader *pfile)
1439 const cpp_token *tok = _cpp_lex_token (pfile);
1441 if (tok->type == CPP_PLUS)
1443 CPP_OPTION(pfile, pedantic_parse_number)++;
1445 else if (tok->type == CPP_MINUS)
1447 CPP_OPTION(pfile, pedantic_parse_number)--;
1451 cpp_error (pfile, CPP_DL_ERROR,
1452 "invalid #pragma pedantic_parse_number directive, need '+' or '-'");
1456 /* SDCC _asm specific
1457 switch _asm block preprocessing on / off */
1459 do_pragma_preproc_asm (cpp_reader *pfile)
1461 const cpp_token *tok = _cpp_lex_token (pfile);
1463 if (tok->type == CPP_PLUS)
1465 CPP_OPTION(pfile, preproc_asm)++;
1467 else if (tok->type == CPP_MINUS)
1469 CPP_OPTION(pfile, preproc_asm)--;
1473 cpp_error (pfile, CPP_DL_ERROR,
1474 "invalid #pragma preproc_asm directive, need '+' or '-'");
1478 /* Mark the current header as a system header. This will suppress
1479 some categories of warnings (notably those from -pedantic). It is
1480 intended for use in system libraries that cannot be implemented in
1481 conforming C, but cannot be certain that their headers appear in a
1482 system include directory. To prevent abuse, it is rejected in the
1483 primary source file. */
1485 do_pragma_system_header (cpp_reader *pfile)
1487 cpp_buffer *buffer = pfile->buffer;
1489 if (buffer->prev == 0)
1490 cpp_error (pfile, CPP_DL_WARNING,
1491 "#pragma system_header ignored outside include file");
1495 skip_rest_of_line (pfile);
1496 cpp_make_system_header (pfile, 1, 0);
1500 /* Check the modified date of the current include file against a specified
1501 file. Issue a diagnostic, if the specified file is newer. We use this to
1502 determine if a fixed header should be refixed. */
1504 do_pragma_dependency (cpp_reader *pfile)
1507 int angle_brackets, ordering;
1509 fname = parse_include (pfile, &angle_brackets, NULL);
1513 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1515 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1516 else if (ordering > 0)
1518 cpp_error (pfile, CPP_DL_WARNING,
1519 "current file is older than %s", fname);
1520 if (cpp_get_token (pfile)->type != CPP_EOF)
1522 _cpp_backup_tokens (pfile, 1);
1523 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1527 free ((void *) fname);
1530 /* Get a token but skip padding. */
1531 static const cpp_token *
1532 get_token_no_padding (cpp_reader *pfile)
1536 const cpp_token *result = cpp_get_token (pfile);
1537 if (result->type != CPP_PADDING)
1542 /* Check syntax is "(string-literal)". Returns the string on success,
1543 or NULL on failure. */
1544 static const cpp_token *
1545 get__Pragma_string (cpp_reader *pfile)
1547 const cpp_token *string;
1549 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1552 string = get_token_no_padding (pfile);
1553 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1556 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1562 /* Destringize IN into a temporary buffer, by removing the first \ of
1563 \" and \\ sequences, and process the result as a #pragma directive. */
1565 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1567 const unsigned char *src, *limit;
1568 char *dest, *result;
1570 dest = result = (char *) alloca (in->len - 1);
1571 src = in->text + 1 + (in->text[0] == 'L');
1572 limit = in->text + in->len - 1;
1575 /* We know there is a character following the backslash. */
1576 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1582 /* Ugh; an awful kludge. We are really not set up to be lexing
1583 tokens when in the middle of a macro expansion. Use a new
1584 context to force cpp_get_token to lex, and so skip_rest_of_line
1585 doesn't go beyond the end of the text. Also, remember the
1586 current lexing position so we can return to it later.
1588 Something like line-at-a-time lexing should remove the need for
1591 cpp_context *saved_context = pfile->context;
1592 cpp_token *saved_cur_token = pfile->cur_token;
1593 tokenrun *saved_cur_run = pfile->cur_run;
1595 pfile->context = XNEW (cpp_context);
1596 pfile->context->macro = 0;
1597 pfile->context->prev = 0;
1598 run_directive (pfile, T_PRAGMA, result, dest - result);
1599 XDELETE (pfile->context);
1600 pfile->context = saved_context;
1601 pfile->cur_token = saved_cur_token;
1602 pfile->cur_run = saved_cur_run;
1605 /* See above comment. For the moment, we'd like
1607 token1 _Pragma ("foo") token2
1617 Getting the line markers is a little tricky. */
1618 if (pfile->cb.line_change)
1619 pfile->cb.line_change (pfile, pfile->cur_token, false);
1622 /* Handle the _Pragma operator. */
1624 _cpp_do__Pragma (cpp_reader *pfile)
1626 const cpp_token *string = get__Pragma_string (pfile);
1627 pfile->directive_result.type = CPP_PADDING;
1630 destringize_and_run (pfile, &string->val.str);
1632 cpp_error (pfile, CPP_DL_ERROR,
1633 "_Pragma takes a parenthesized string literal");
1636 /* Handle a pragma that the front end deferred until now. */
1638 cpp_handle_deferred_pragma (cpp_reader *pfile, const cpp_string *s)
1640 cpp_context *saved_context = pfile->context;
1641 cpp_token *saved_cur_token = pfile->cur_token;
1642 tokenrun *saved_cur_run = pfile->cur_run;
1643 bool saved_defer_pragmas = CPP_OPTION (pfile, defer_pragmas);
1644 void (*saved_line_change) (cpp_reader *, const cpp_token *, int)
1645 = pfile->cb.line_change;
1647 pfile->context = XNEW (cpp_context);
1648 pfile->context->macro = 0;
1649 pfile->context->prev = 0;
1650 pfile->cb.line_change = NULL;
1651 pfile->state.in_deferred_pragma = true;
1652 CPP_OPTION (pfile, defer_pragmas) = false;
1654 run_directive (pfile, T_PRAGMA, (const char *)s->text, s->len);
1656 XDELETE (pfile->context);
1657 pfile->context = saved_context;
1658 pfile->cur_token = saved_cur_token;
1659 pfile->cur_run = saved_cur_run;
1660 pfile->cb.line_change = saved_line_change;
1661 pfile->state.in_deferred_pragma = false;
1662 CPP_OPTION (pfile, defer_pragmas) = saved_defer_pragmas;
1665 /* Handle #ifdef. */
1667 do_ifdef (cpp_reader *pfile)
1671 if (! pfile->state.skipping)
1673 const cpp_hashnode *node = lex_macro_node (pfile);
1677 skip = node->type != NT_MACRO;
1678 _cpp_mark_macro_used (node);
1683 push_conditional (pfile, skip, T_IFDEF, 0);
1686 /* Handle #ifndef. */
1688 do_ifndef (cpp_reader *pfile)
1691 const cpp_hashnode *node = 0;
1693 if (! pfile->state.skipping)
1695 node = lex_macro_node (pfile);
1699 skip = node->type == NT_MACRO;
1700 _cpp_mark_macro_used (node);
1705 push_conditional (pfile, skip, T_IFNDEF, node);
1708 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1709 pfile->mi_ind_cmacro so we can handle multiple-include
1710 optimizations. If macro expansion occurs in the expression, we
1711 cannot treat it as a controlling conditional, since the expansion
1712 could change in the future. That is handled by cpp_get_token. */
1714 do_if (cpp_reader *pfile)
1718 if (! pfile->state.skipping)
1719 skip = _cpp_parse_expr (pfile) == false;
1721 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1724 /* Flip skipping state if appropriate and continue without changing
1725 if_stack; this is so that the error message for missing #endif's
1726 etc. will point to the original #if. */
1728 do_else (cpp_reader *pfile)
1730 cpp_buffer *buffer = pfile->buffer;
1731 struct if_stack *ifs = buffer->if_stack;
1734 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1737 if (ifs->type == T_ELSE)
1739 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1740 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1741 "the conditional began here");
1745 /* Skip any future (erroneous) #elses or #elifs. */
1746 pfile->state.skipping = ifs->skip_elses;
1747 ifs->skip_elses = true;
1749 /* Invalidate any controlling macro. */
1752 /* Only check EOL if was not originally skipping. */
1753 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1758 /* Handle a #elif directive by not changing if_stack either. See the
1759 comment above do_else. */
1761 do_elif (cpp_reader *pfile)
1763 cpp_buffer *buffer = pfile->buffer;
1764 struct if_stack *ifs = buffer->if_stack;
1767 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1770 if (ifs->type == T_ELSE)
1772 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1773 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1774 "the conditional began here");
1778 /* Only evaluate this if we aren't skipping elses. During
1779 evaluation, set skipping to false to get lexer warnings. */
1780 if (ifs->skip_elses)
1781 pfile->state.skipping = 1;
1784 pfile->state.skipping = 0;
1785 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1786 ifs->skip_elses = ! pfile->state.skipping;
1789 /* Invalidate any controlling macro. */
1794 /* #endif pops the if stack and resets pfile->state.skipping. */
1796 do_endif (cpp_reader *pfile)
1798 cpp_buffer *buffer = pfile->buffer;
1799 struct if_stack *ifs = buffer->if_stack;
1802 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1805 /* Only check EOL if was not originally skipping. */
1806 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1809 /* If potential control macro, we go back outside again. */
1810 if (ifs->next == 0 && ifs->mi_cmacro)
1812 pfile->mi_valid = true;
1813 pfile->mi_cmacro = ifs->mi_cmacro;
1816 buffer->if_stack = ifs->next;
1817 pfile->state.skipping = ifs->was_skipping;
1818 obstack_free (&pfile->buffer_ob, ifs);
1822 /* Push an if_stack entry for a preprocessor conditional, and set
1823 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1824 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1825 we need to check here that we are at the top of the file. */
1827 push_conditional (cpp_reader *pfile, int skip, int type,
1828 const cpp_hashnode *cmacro)
1830 struct if_stack *ifs;
1831 cpp_buffer *buffer = pfile->buffer;
1833 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1834 ifs->line = pfile->directive_line;
1835 ifs->next = buffer->if_stack;
1836 ifs->skip_elses = pfile->state.skipping || !skip;
1837 ifs->was_skipping = pfile->state.skipping;
1839 /* This condition is effectively a test for top-of-file. */
1840 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1841 ifs->mi_cmacro = cmacro;
1845 pfile->state.skipping = skip;
1846 buffer->if_stack = ifs;
1849 /* Read the tokens of the answer into the macro pool, in a directive
1850 of type TYPE. Only commit the memory if we intend it as permanent
1851 storage, i.e. the #assert case. Returns 0 on success, and sets
1852 ANSWERP to point to the answer. */
1854 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1856 const cpp_token *paren;
1857 struct answer *answer;
1858 unsigned int acount;
1860 /* In a conditional, it is legal to not have an open paren. We
1861 should save the following token in this case. */
1862 paren = cpp_get_token (pfile);
1864 /* If not a paren, see if we're OK. */
1865 if (paren->type != CPP_OPEN_PAREN)
1867 /* In a conditional no answer is a test for any answer. It
1868 could be followed by any token. */
1871 _cpp_backup_tokens (pfile, 1);
1875 /* #unassert with no answer is valid - it removes all answers. */
1876 if (type == T_UNASSERT && paren->type == CPP_EOF)
1879 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1883 for (acount = 0;; acount++)
1886 const cpp_token *token = cpp_get_token (pfile);
1889 if (token->type == CPP_CLOSE_PAREN)
1892 if (token->type == CPP_EOF)
1894 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1898 /* struct answer includes the space for one token. */
1899 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1901 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1902 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1904 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1907 /* Drop whitespace at start, for answer equivalence purposes. */
1909 dest->flags &= ~PREV_WHITE;
1914 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1918 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1919 answer->count = acount;
1920 answer->next = NULL;
1926 /* Parses an assertion directive of type TYPE, returning a pointer to
1927 the hash node of the predicate, or 0 on error. If an answer was
1928 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1929 static cpp_hashnode *
1930 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1932 cpp_hashnode *result = 0;
1933 const cpp_token *predicate;
1935 /* We don't expand predicates or answers. */
1936 pfile->state.prevent_expansion++;
1939 predicate = cpp_get_token (pfile);
1940 if (predicate->type == CPP_EOF)
1941 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1942 else if (predicate->type != CPP_NAME)
1943 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1944 else if (parse_answer (pfile, answerp, type) == 0)
1946 unsigned int len = NODE_LEN (predicate->val.node);
1947 unsigned char *sym = (unsigned char *) alloca (len + 1);
1949 /* Prefix '#' to get it out of macro namespace. */
1951 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1952 result = cpp_lookup (pfile, sym, len + 1);
1955 pfile->state.prevent_expansion--;
1959 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1960 or a pointer to NULL if the answer is not in the chain. */
1961 static struct answer **
1962 find_answer (cpp_hashnode *node, const struct answer *candidate)
1965 struct answer **result;
1967 for (result = &node->value.answers; *result; result = &(*result)->next)
1969 struct answer *answer = *result;
1971 if (answer->count == candidate->count)
1973 for (i = 0; i < answer->count; i++)
1974 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1977 if (i == answer->count)
1985 /* Test an assertion within a preprocessor conditional. Returns
1986 nonzero on failure, zero on success. On success, the result of
1987 the test is written into VALUE, otherwise the value 0. */
1989 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1991 struct answer *answer;
1994 node = parse_assertion (pfile, &answer, T_IF);
1996 /* For recovery, an erroneous assertion expression is handled as a
1997 failing assertion. */
2001 *value = (node->type == NT_ASSERTION &&
2002 (answer == 0 || *find_answer (node, answer) != 0));
2003 else if (pfile->cur_token[-1].type == CPP_EOF)
2004 _cpp_backup_tokens (pfile, 1);
2006 /* We don't commit the memory for the answer - it's temporary only. */
2010 /* Handle #assert. */
2012 do_assert (cpp_reader *pfile)
2014 struct answer *new_answer;
2017 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2022 /* Place the new answer in the answer list. First check there
2023 is not a duplicate. */
2024 new_answer->next = 0;
2025 if (node->type == NT_ASSERTION)
2027 if (*find_answer (node, new_answer))
2029 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2030 NODE_NAME (node) + 1);
2033 new_answer->next = node->value.answers;
2036 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2037 * sizeof (cpp_token));
2038 /* Commit or allocate storage for the object. */
2039 if (pfile->hash_table->alloc_subobject)
2041 struct answer *temp_answer = new_answer;
2042 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2044 memcpy (new_answer, temp_answer, answer_size);
2047 BUFF_FRONT (pfile->a_buff) += answer_size;
2049 node->type = NT_ASSERTION;
2050 node->value.answers = new_answer;
2055 /* Handle #unassert. */
2057 do_unassert (cpp_reader *pfile)
2060 struct answer *answer;
2062 node = parse_assertion (pfile, &answer, T_UNASSERT);
2063 /* It isn't an error to #unassert something that isn't asserted. */
2064 if (node && node->type == NT_ASSERTION)
2068 struct answer **p = find_answer (node, answer), *temp;
2070 /* Remove the answer from the list. */
2075 /* Did we free the last answer? */
2076 if (node->value.answers == 0)
2077 node->type = NT_VOID;
2082 _cpp_free_definition (node);
2085 /* We don't commit the memory for the answer - it's temporary only. */
2088 /* These are for -D, -U, -A. */
2090 /* Process the string STR as if it appeared as the body of a #define.
2091 If STR is just an identifier, define it with value 1.
2092 If STR has anything after the identifier, then it should
2093 be identifier=definition. */
2095 cpp_define (cpp_reader *pfile, const char *str)
2100 /* Copy the entire option so we can modify it.
2101 Change the first "=" in the string to a space. If there is none,
2102 tack " 1" on the end. */
2104 count = strlen (str);
2105 buf = (char *) alloca (count + 3);
2106 memcpy (buf, str, count);
2108 p = strchr (str, '=');
2118 run_directive (pfile, T_DEFINE, buf, count);
2121 /* Slight variant of the above for use by initialize_builtins. */
2123 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2125 size_t len = strlen (str);
2126 char *buf = (char *) alloca (len + 1);
2127 memcpy (buf, str, len);
2129 run_directive (pfile, T_DEFINE, buf, len);
2132 /* Process MACRO as if it appeared as the body of an #undef. */
2134 cpp_undef (cpp_reader *pfile, const char *macro)
2136 size_t len = strlen (macro);
2137 char *buf = (char *) alloca (len + 1);
2138 memcpy (buf, macro, len);
2140 run_directive (pfile, T_UNDEF, buf, len);
2143 /* Process the string STR as if it appeared as the body of a #assert. */
2145 cpp_assert (cpp_reader *pfile, const char *str)
2147 handle_assertion (pfile, str, T_ASSERT);
2150 /* Process STR as if it appeared as the body of an #unassert. */
2152 cpp_unassert (cpp_reader *pfile, const char *str)
2154 handle_assertion (pfile, str, T_UNASSERT);
2157 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2159 handle_assertion (cpp_reader *pfile, const char *str, int type)
2161 size_t count = strlen (str);
2162 const char *p = strchr (str, '=');
2164 /* Copy the entire option so we can modify it. Change the first
2165 "=" in the string to a '(', and tack a ')' on the end. */
2166 char *buf = (char *) alloca (count + 2);
2168 memcpy (buf, str, count);
2177 run_directive (pfile, type, str, count);
2180 /* The number of errors for a given reader. */
2182 cpp_errors (cpp_reader *pfile)
2184 return pfile->errors;
2187 /* The options structure. */
2189 cpp_get_options (cpp_reader *pfile)
2191 return &pfile->opts;
2194 /* The callbacks structure. */
2196 cpp_get_callbacks (cpp_reader *pfile)
2201 /* Copy the given callbacks structure to our own. */
2203 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2208 /* The dependencies structure. (Creates one if it hasn't already been.) */
2210 cpp_get_deps (cpp_reader *pfile)
2213 pfile->deps = deps_init ();
2217 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2218 doesn't fail. It does not generate a file change call back; that
2219 is the responsibility of the caller. */
2221 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2224 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2226 /* Clears, amongst other things, if_stack and mi_cmacro. */
2227 memset (new_buffer, 0, sizeof (cpp_buffer));
2229 new_buffer->next_line = new_buffer->buf = buffer;
2230 new_buffer->rlimit = buffer + len;
2231 new_buffer->from_stage3 = from_stage3;
2232 new_buffer->prev = pfile->buffer;
2233 new_buffer->need_line = true;
2235 pfile->buffer = new_buffer;
2240 /* Pops a single buffer, with a file change call-back if appropriate.
2241 Then pushes the next -include file, if any remain. */
2243 _cpp_pop_buffer (cpp_reader *pfile)
2245 cpp_buffer *buffer = pfile->buffer;
2246 struct _cpp_file *inc = buffer->file;
2247 struct if_stack *ifs;
2249 /* Walk back up the conditional stack till we reach its level at
2250 entry to this file, issuing error messages. */
2251 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2252 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2253 "unterminated #%s", dtable[ifs->type].name);
2255 /* In case of a missing #endif. */
2256 pfile->state.skipping = 0;
2258 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2259 pfile->buffer = buffer->prev;
2261 free (buffer->notes);
2263 /* Free the buffer object now; we may want to push a new buffer
2264 in _cpp_push_next_include_file. */
2265 obstack_free (&pfile->buffer_ob, buffer);
2269 _cpp_pop_file_buffer (pfile, inc);
2271 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2275 /* Enter all recognized directives in the hash table. */
2277 _cpp_init_directives (cpp_reader *pfile)
2282 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2284 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2285 node->is_directive = 1;
2286 node->directive_index = i;