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 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 /* Stack of conditionals currently in progress
30 (including both successful and failing conditionals). */
33 struct if_stack *next;
34 unsigned int line; /* Line where condition started. */
35 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
36 bool skip_elses; /* Can future #else / #elif be skipped? */
37 bool was_skipping; /* If were skipping on entry. */
38 int type; /* Most recent conditional for diagnostics. */
41 /* Contains a registered pragma or pragma namespace. */
42 typedef void (*pragma_cb) (cpp_reader *);
45 struct pragma_entry *next;
46 const cpp_hashnode *pragma; /* Name and length. */
53 struct pragma_entry *space;
58 /* Values for the origin field of struct directive. KANDR directives
59 come from traditional (K&R) C. STDC89 directives come from the
60 1989 C standard. EXTENSION directives are extensions. */
65 /* Values for the flags field of struct directive. COND indicates a
66 conditional; IF_COND an opening conditional. INCL means to treat
67 "..." and <...> as q-char and h-char sequences respectively. IN_I
68 means this directive should be handled even if -fpreprocessed is in
69 effect (these are the directives with callback hooks).
71 EXPAND is set on directives that are always macro-expanded. */
73 #define IF_COND (1 << 1)
76 #define EXPAND (1 << 4)
78 /* Defines one #-directive, including how to handle it. */
79 typedef void (*directive_handler) (cpp_reader *);
80 typedef struct directive directive;
83 directive_handler handler; /* Function to handle directive. */
84 const uchar *name; /* Name of directive. */
85 unsigned short length; /* Length of name. */
86 unsigned char origin; /* Origin of directive. */
87 unsigned char flags; /* Flags describing this directive. */
90 /* Forward declarations. */
92 static void skip_rest_of_line (cpp_reader *);
93 static void check_eol (cpp_reader *);
94 static void start_directive (cpp_reader *);
95 static void prepare_directive_trad (cpp_reader *);
96 static void end_directive (cpp_reader *, int);
97 static void directive_diagnostics (cpp_reader *, const directive *, int);
98 static void run_directive (cpp_reader *, int, const char *, size_t);
99 static char *glue_header_name (cpp_reader *);
100 static const char *parse_include (cpp_reader *, int *, const cpp_token ***);
101 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
102 static unsigned int read_flag (cpp_reader *, unsigned int);
103 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
104 static void do_diagnostic (cpp_reader *, int, int);
105 static cpp_hashnode *lex_macro_node (cpp_reader *);
106 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
107 static void do_include_common (cpp_reader *, enum include_type);
108 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
109 const cpp_hashnode *);
110 static int count_registered_pragmas (struct pragma_entry *);
111 static char ** save_registered_pragmas (struct pragma_entry *, char **);
112 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
114 static void do_pragma_once (cpp_reader *);
115 static void do_pragma_poison (cpp_reader *);
116 static void do_pragma_system_header (cpp_reader *);
117 static void do_pragma_dependency (cpp_reader *);
118 static void do_linemarker (cpp_reader *);
119 static const cpp_token *get_token_no_padding (cpp_reader *);
120 static const cpp_token *get__Pragma_string (cpp_reader *);
121 static void destringize_and_run (cpp_reader *, const cpp_string *);
122 static int parse_answer (cpp_reader *, struct answer **, int);
123 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
124 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
125 static void handle_assertion (cpp_reader *, const char *, int);
127 /* This is the table of directive handlers. It is ordered by
128 frequency of occurrence; the numbers at the end are directive
129 counts from all the source code I have lying around (egcs and libc
130 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
131 pcmcia-cs-3.0.9). This is no longer important as directive lookup
132 is now O(1). All extensions other than #warning and #include_next
133 are deprecated. The name is where the extension appears to have
136 #define DIRECTIVE_TABLE \
137 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
138 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
139 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
140 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
141 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
142 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
143 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
144 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
145 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
146 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
147 D(error, T_ERROR, STDC89, 0) /* 475 */ \
148 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
149 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
150 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
151 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
152 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
153 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
154 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
155 D(sccs, T_SCCS, EXTENSION, IN_I) /* 0 SVR4? */
157 /* #sccs is synonymous with #ident. */
158 #define do_sccs do_ident
160 /* Use the table to generate a series of prototypes, an enum for the
161 directive names, and an array of directive handlers. */
163 #define D(name, t, o, f) static void do_##name (cpp_reader *);
167 #define D(n, tag, o, f) tag,
175 #define D(name, t, origin, flags) \
176 { do_##name, (const uchar *) #name, \
177 sizeof #name - 1, origin, flags },
178 static const directive dtable[] =
183 #undef DIRECTIVE_TABLE
185 /* Wrapper struct directive for linemarkers.
186 The origin is more or less true - the original K+R cpp
187 did use this notation in its preprocessed output. */
188 static const directive linemarker_dir =
190 do_linemarker, U"#", 1, KANDR, IN_I
193 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
195 /* Skip any remaining tokens in a directive. */
197 skip_rest_of_line (cpp_reader *pfile)
199 /* Discard all stacked contexts. */
200 while (pfile->context->prev)
201 _cpp_pop_context (pfile);
203 /* Sweep up all tokens remaining on the line. */
205 while (_cpp_lex_token (pfile)->type != CPP_EOF)
209 /* Ensure there are no stray tokens at the end of a directive. */
211 check_eol (cpp_reader *pfile)
213 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
214 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
215 pfile->directive->name);
218 /* Ensure there are no stray tokens other than comments at the end of
219 a directive, and gather the comments. */
220 static const cpp_token **
221 check_eol_return_comments (cpp_reader *pfile)
225 const cpp_token **buf;
227 buf = XNEWVEC (const cpp_token *, capacity);
233 const cpp_token *tok;
235 tok = _cpp_lex_token (pfile);
236 if (tok->type == CPP_EOF)
238 if (tok->type != CPP_COMMENT)
239 cpp_error (pfile, CPP_DL_PEDWARN,
240 "extra tokens at end of #%s directive",
241 pfile->directive->name);
244 if (c + 1 >= capacity)
247 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
258 /* Called when entering a directive, _Pragma or command-line directive. */
260 start_directive (cpp_reader *pfile)
262 /* Setup in-directive state. */
263 pfile->state.in_directive = 1;
264 pfile->state.save_comments = 0;
265 pfile->directive_result.type = CPP_PADDING;
267 /* Some handlers need the position of the # for diagnostics. */
268 pfile->directive_line = pfile->line_table->highest_line;
271 /* Called when leaving a directive, _Pragma or command-line directive. */
273 end_directive (cpp_reader *pfile, int skip_line)
275 if (pfile->state.in_deferred_pragma)
277 else if (CPP_OPTION (pfile, traditional))
279 /* Revert change of prepare_directive_trad. */
280 pfile->state.prevent_expansion--;
282 if (pfile->directive != &dtable[T_DEFINE])
283 _cpp_remove_overlay (pfile);
285 /* We don't skip for an assembler #. */
288 skip_rest_of_line (pfile);
289 if (!pfile->keep_tokens)
291 pfile->cur_run = &pfile->base_run;
292 pfile->cur_token = pfile->base_run.base;
297 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
298 pfile->state.in_directive = 0;
299 pfile->state.in_expression = 0;
300 pfile->state.angled_headers = 0;
301 pfile->directive = 0;
304 /* Prepare to handle the directive in pfile->directive. */
306 prepare_directive_trad (cpp_reader *pfile)
308 if (pfile->directive != &dtable[T_DEFINE])
310 bool no_expand = (pfile->directive
311 && ! (pfile->directive->flags & EXPAND));
312 bool was_skipping = pfile->state.skipping;
314 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
315 || pfile->directive == &dtable[T_ELIF]);
316 if (pfile->state.in_expression)
317 pfile->state.skipping = false;
320 pfile->state.prevent_expansion++;
321 _cpp_scan_out_logical_line (pfile, NULL);
323 pfile->state.prevent_expansion--;
325 pfile->state.skipping = was_skipping;
326 _cpp_overlay_buffer (pfile, pfile->out.base,
327 pfile->out.cur - pfile->out.base);
330 /* Stop ISO C from expanding anything. */
331 pfile->state.prevent_expansion++;
334 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
335 the '#' was indented. */
337 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
339 /* Issue -pedantic warnings for extensions. */
340 if (CPP_PEDANTIC (pfile)
341 && ! pfile->state.skipping
342 && dir->origin == EXTENSION)
343 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
345 /* Traditionally, a directive is ignored unless its # is in
346 column 1. Therefore in code intended to work with K+R
347 compilers, directives added by C89 must have their #
348 indented, and directives present in traditional C must not.
349 This is true even of directives in skipped conditional
350 blocks. #elif cannot be used at all. */
351 if (CPP_WTRADITIONAL (pfile))
353 if (dir == &dtable[T_ELIF])
354 cpp_error (pfile, CPP_DL_WARNING,
355 "suggest not using #elif in traditional C");
356 else if (indented && dir->origin == KANDR)
357 cpp_error (pfile, CPP_DL_WARNING,
358 "traditional C ignores #%s with the # indented",
360 else if (!indented && dir->origin != KANDR)
361 cpp_error (pfile, CPP_DL_WARNING,
362 "suggest hiding #%s from traditional C with an indented #",
367 /* Check if we have a known directive. INDENTED is nonzero if the
368 '#' of the directive was indented. This function is in this file
369 to save unnecessarily exporting dtable etc. to lex.c. Returns
370 nonzero if the line of tokens has been handled, zero if we should
371 continue processing the line. */
373 _cpp_handle_directive (cpp_reader *pfile, int indented)
375 const directive *dir = 0;
376 const cpp_token *dname;
377 bool was_parsing_args = pfile->state.parsing_args;
378 bool was_discarding_output = pfile->state.discarding_output;
381 if (was_discarding_output)
382 pfile->state.prevent_expansion = 0;
384 if (was_parsing_args)
386 if (CPP_OPTION (pfile, pedantic))
387 cpp_error (pfile, CPP_DL_PEDWARN,
388 "embedding a directive within macro arguments is not portable");
389 pfile->state.parsing_args = 0;
390 pfile->state.prevent_expansion = 0;
392 start_directive (pfile);
393 dname = _cpp_lex_token (pfile);
395 if (dname->type == CPP_NAME)
397 if (dname->val.node->is_directive)
398 dir = &dtable[dname->val.node->directive_index];
400 /* We do not recognize the # followed by a number extension in
402 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
404 dir = &linemarker_dir;
405 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
406 && ! pfile->state.skipping)
407 cpp_error (pfile, CPP_DL_PEDWARN,
408 "style of line directive is a GCC extension");
413 /* If we have a directive that is not an opening conditional,
414 invalidate any control macro. */
415 if (! (dir->flags & IF_COND))
416 pfile->mi_valid = false;
418 /* Kluge alert. In order to be sure that code like this
423 does not cause '#define foo bar' to get executed when
424 compiled with -save-temps, we recognize directives in
425 -fpreprocessed mode only if the # is in column 1. macro.c
426 puts a space in front of any '#' at the start of a macro.
428 We exclude the -fdirectives-only case because macro expansion
429 has not been performed yet, and block comments can cause spaces
430 to preceed the directive. */
431 if (CPP_OPTION (pfile, preprocessed)
432 && !CPP_OPTION (pfile, directives_only)
433 && (indented || !(dir->flags & IN_I)))
440 /* In failed conditional groups, all non-conditional
441 directives are ignored. Before doing that, whether
442 skipping or not, we should lex angle-bracketed headers
443 correctly, and maybe output some diagnostics. */
444 pfile->state.angled_headers = dir->flags & INCL;
445 pfile->state.directive_wants_padding = dir->flags & INCL;
446 if (! CPP_OPTION (pfile, preprocessed))
447 directive_diagnostics (pfile, dir, indented);
448 if (pfile->state.skipping && !(dir->flags & COND))
452 else if (dname->type == CPP_EOF)
453 ; /* CPP_EOF is the "null directive". */
456 /* An unknown directive. Don't complain about it in assembly
457 source: we don't know where the comments are, and # may
458 introduce assembler pseudo-ops. Don't complain about invalid
459 directives in skipped conditional groups (6.10 p4). */
460 if (CPP_OPTION (pfile, lang) == CLK_ASM)
462 else if (!pfile->state.skipping)
463 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
464 cpp_token_as_text (pfile, dname));
467 pfile->directive = dir;
468 if (CPP_OPTION (pfile, traditional))
469 prepare_directive_trad (pfile);
472 pfile->directive->handler (pfile);
474 _cpp_backup_tokens (pfile, 1);
476 end_directive (pfile, skip);
477 if (was_parsing_args)
479 /* Restore state when within macro args. */
480 pfile->state.parsing_args = 2;
481 pfile->state.prevent_expansion = 1;
483 if (was_discarding_output)
484 pfile->state.prevent_expansion = 1;
488 /* Directive handler wrapper used by the command line option
489 processor. BUF is \n terminated. */
491 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
493 cpp_push_buffer (pfile, (const uchar *) buf, count,
494 /* from_stage3 */ true);
495 start_directive (pfile);
497 /* This is a short-term fix to prevent a leading '#' being
498 interpreted as a directive. */
499 _cpp_clean_line (pfile);
501 pfile->directive = &dtable[dir_no];
502 if (CPP_OPTION (pfile, traditional))
503 prepare_directive_trad (pfile);
504 pfile->directive->handler (pfile);
505 end_directive (pfile, 1);
506 _cpp_pop_buffer (pfile);
509 /* Checks for validity the macro name in #define, #undef, #ifdef and
510 #ifndef directives. */
511 static cpp_hashnode *
512 lex_macro_node (cpp_reader *pfile)
514 const cpp_token *token = _cpp_lex_token (pfile);
516 /* The token immediately after #define must be an identifier. That
517 identifier may not be "defined", per C99 6.10.8p4.
518 In C++, it may not be any of the "named operators" either,
519 per C++98 [lex.digraph], [lex.key].
520 Finally, the identifier may not have been poisoned. (In that case
521 the lexer has issued the error message for us.) */
523 if (token->type == CPP_NAME)
525 cpp_hashnode *node = token->val.node;
527 if (node == pfile->spec_nodes.n_defined)
528 cpp_error (pfile, CPP_DL_ERROR,
529 "\"defined\" cannot be used as a macro name");
530 else if (! (node->flags & NODE_POISONED))
533 else if (token->flags & NAMED_OP)
534 cpp_error (pfile, CPP_DL_ERROR,
535 "\"%s\" cannot be used as a macro name as it is an operator in C++",
536 NODE_NAME (token->val.node));
537 else if (token->type == CPP_EOF)
538 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
539 pfile->directive->name);
541 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
546 /* Process a #define directive. Most work is done in macro.c. */
548 do_define (cpp_reader *pfile)
550 cpp_hashnode *node = lex_macro_node (pfile);
554 /* If we have been requested to expand comments into macros,
555 then re-enable saving of comments. */
556 pfile->state.save_comments =
557 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
559 if (_cpp_create_definition (pfile, node))
560 if (pfile->cb.define)
561 pfile->cb.define (pfile, pfile->directive_line, node);
565 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
567 do_undef (cpp_reader *pfile)
569 cpp_hashnode *node = lex_macro_node (pfile);
574 pfile->cb.undef (pfile, pfile->directive_line, node);
576 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
577 identifier is not currently defined as a macro name. */
578 if (node->type == NT_MACRO)
580 if (node->flags & NODE_WARN)
581 cpp_error (pfile, CPP_DL_WARNING,
582 "undefining \"%s\"", NODE_NAME (node));
584 if (CPP_OPTION (pfile, warn_unused_macros))
585 _cpp_warn_if_unused_macro (pfile, node, NULL);
587 _cpp_free_definition (node);
594 /* Undefine a single macro/assertion/whatever. */
597 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
598 void *data_p ATTRIBUTE_UNUSED)
600 /* Body of _cpp_free_definition inlined here for speed.
601 Macros and assertions no longer have anything to free. */
603 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED);
607 /* Undefine all macros and assertions. */
610 cpp_undef_all (cpp_reader *pfile)
612 cpp_forall_identifiers (pfile, undefine_macros, NULL);
616 /* Helper routine used by parse_include. Reinterpret the current line
617 as an h-char-sequence (< ... >); we are looking at the first token
618 after the <. Returns a malloced filename. */
620 glue_header_name (cpp_reader *pfile)
622 const cpp_token *token;
624 size_t len, total_len = 0, capacity = 1024;
626 /* To avoid lexed tokens overwriting our glued name, we can only
627 allocate from the string pool once we've lexed everything. */
628 buffer = XNEWVEC (char, capacity);
631 token = get_token_no_padding (pfile);
633 if (token->type == CPP_GREATER)
635 if (token->type == CPP_EOF)
637 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
641 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
642 if (total_len + len > capacity)
644 capacity = (capacity + len) * 2;
645 buffer = XRESIZEVEC (char, buffer, capacity);
648 if (token->flags & PREV_WHITE)
649 buffer[total_len++] = ' ';
651 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
656 buffer[total_len] = '\0';
660 /* Returns the file name of #include, #include_next, #import and
661 #pragma dependency. The string is malloced and the caller should
662 free it. Returns NULL on error. */
664 parse_include (cpp_reader *pfile, int *pangle_brackets,
665 const cpp_token ***buf)
668 const cpp_token *header;
670 /* Allow macro expansion. */
671 header = get_token_no_padding (pfile);
672 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
674 fname = XNEWVEC (char, header->val.str.len - 1);
675 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
676 fname[header->val.str.len - 2] = '\0';
677 *pangle_brackets = header->type == CPP_HEADER_NAME;
679 else if (header->type == CPP_LESS)
681 fname = glue_header_name (pfile);
682 *pangle_brackets = 1;
686 const unsigned char *dir;
688 if (pfile->directive == &dtable[T_PRAGMA])
689 dir = U"pragma dependency";
691 dir = pfile->directive->name;
692 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
698 if (buf == NULL || CPP_OPTION (pfile, discard_comments))
702 /* If we are not discarding comments, then gather them while
703 doing the eol check. */
704 *buf = check_eol_return_comments (pfile);
710 /* Handle #include, #include_next and #import. */
712 do_include_common (cpp_reader *pfile, enum include_type type)
716 const cpp_token **buf = NULL;
718 /* Re-enable saving of comments if requested, so that the include
719 callback can dump comments which follow #include. */
720 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
722 fname = parse_include (pfile, &angle_brackets, &buf);
732 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
733 pfile->directive->name);
740 /* Prevent #include recursion. */
741 if (pfile->line_table->depth >= CPP_STACK_MAX)
742 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
745 /* Get out of macro context, if we are. */
746 skip_rest_of_line (pfile);
748 if (pfile->cb.include)
749 pfile->cb.include (pfile, pfile->directive_line,
750 pfile->directive->name, fname, angle_brackets,
753 _cpp_stack_include (pfile, fname, angle_brackets, type);
762 do_include (cpp_reader *pfile)
764 do_include_common (pfile, IT_INCLUDE);
768 do_import (cpp_reader *pfile)
770 do_include_common (pfile, IT_IMPORT);
774 do_include_next (cpp_reader *pfile)
776 enum include_type type = IT_INCLUDE_NEXT;
778 /* If this is the primary source file, warn and use the normal
780 if (! pfile->buffer->prev)
782 cpp_error (pfile, CPP_DL_WARNING,
783 "#include_next in primary source file");
786 do_include_common (pfile, type);
789 /* Subroutine of do_linemarker. Read possible flags after file name.
790 LAST is the last flag seen; 0 if this is the first flag. Return the
791 flag if it is valid, 0 at the end of the directive. Otherwise
794 read_flag (cpp_reader *pfile, unsigned int last)
796 const cpp_token *token = _cpp_lex_token (pfile);
798 if (token->type == CPP_NUMBER && token->val.str.len == 1)
800 unsigned int flag = token->val.str.text[0] - '0';
802 if (flag > last && flag <= 4
803 && (flag != 4 || last == 3)
804 && (flag != 2 || last == 0))
808 if (token->type != CPP_EOF)
809 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
810 cpp_token_as_text (pfile, token));
814 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
815 of length LEN, to binary; store it in NUMP, and return 0 if the
816 number was well-formed, 1 if not. Temporary, hopefully. */
818 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
820 unsigned long reg = 0;
834 /* Interpret #line command.
835 Note that the filename string (if any) is a true string constant
836 (escapes are interpreted), unlike in #line. */
838 do_line (cpp_reader *pfile)
840 const struct line_maps *line_table = pfile->line_table;
841 const struct line_map *map = &line_table->maps[line_table->used - 1];
843 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
846 unsigned char map_sysp = map->sysp;
847 const cpp_token *token;
848 const char *new_file = map->to_file;
849 unsigned long new_lineno;
851 /* C99 raised the minimum limit on #line numbers. */
852 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
854 /* #line commands expand macros. */
855 token = cpp_get_token (pfile);
856 if (token->type != CPP_NUMBER
857 || strtoul_for_line (token->val.str.text, token->val.str.len,
860 cpp_error (pfile, CPP_DL_ERROR,
861 "\"%s\" after #line is not a positive integer",
862 cpp_token_as_text (pfile, token));
866 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
867 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
869 token = cpp_get_token (pfile);
870 if (token->type == CPP_STRING)
872 cpp_string s = { 0, 0 };
873 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
875 new_file = (const char *)s.text;
878 else if (token->type != CPP_EOF)
880 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
881 cpp_token_as_text (pfile, token));
885 skip_rest_of_line (pfile);
886 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
890 /* Interpret the # 44 "file" [flags] notation, which has slightly
891 different syntax and semantics from #line: Flags are allowed,
892 and we never complain about the line number being too big. */
894 do_linemarker (cpp_reader *pfile)
896 const struct line_maps *line_table = pfile->line_table;
897 const struct line_map *map = &line_table->maps[line_table->used - 1];
898 const cpp_token *token;
899 const char *new_file = map->to_file;
900 unsigned long new_lineno;
901 unsigned int new_sysp = map->sysp;
902 enum lc_reason reason = LC_RENAME;
905 /* Back up so we can get the number again. Putting this in
906 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
907 some circumstances, which can segfault. */
908 _cpp_backup_tokens (pfile, 1);
910 /* #line commands expand macros. */
911 token = cpp_get_token (pfile);
912 if (token->type != CPP_NUMBER
913 || strtoul_for_line (token->val.str.text, token->val.str.len,
916 cpp_error (pfile, CPP_DL_ERROR,
917 "\"%s\" after # is not a positive integer",
918 cpp_token_as_text (pfile, token));
922 token = cpp_get_token (pfile);
923 if (token->type == CPP_STRING)
925 cpp_string s = { 0, 0 };
926 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
928 new_file = (const char *)s.text;
931 flag = read_flag (pfile, 0);
935 /* Fake an include for cpp_included (). */
936 _cpp_fake_include (pfile, new_file);
937 flag = read_flag (pfile, flag);
942 flag = read_flag (pfile, flag);
947 flag = read_flag (pfile, flag);
951 pfile->buffer->sysp = new_sysp;
955 else if (token->type != CPP_EOF)
957 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
958 cpp_token_as_text (pfile, token));
962 skip_rest_of_line (pfile);
963 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
966 /* Arrange the file_change callback. pfile->line has changed to
967 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
968 header, 2 for a system header that needs to be extern "C" protected,
969 and zero otherwise. */
971 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
972 const char *to_file, unsigned int file_line,
975 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
978 linemap_line_start (pfile->line_table, map->to_line, 127);
980 if (pfile->cb.file_change)
981 pfile->cb.file_change (pfile, map);
984 /* Report a warning or error detected by the program we are
985 processing. Use the directive's tokens in the error message. */
987 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
989 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
992 fprintf (stderr, "#%s ", pfile->directive->name);
993 pfile->state.prevent_expansion++;
994 /* APPLE LOCAL #error with unmatched quotes 5607574 */
995 pfile->state.in_diagnostic++;
996 cpp_output_line (pfile, stderr);
997 /* APPLE LOCAL #error with unmatched quotes 5607574 */
998 pfile->state.in_diagnostic--;
999 pfile->state.prevent_expansion--;
1004 do_error (cpp_reader *pfile)
1006 do_diagnostic (pfile, CPP_DL_ERROR, 1);
1010 do_warning (cpp_reader *pfile)
1012 /* We want #warning diagnostics to be emitted in system headers too. */
1013 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1016 /* Report program identification. */
1018 do_ident (cpp_reader *pfile)
1020 const cpp_token *str = cpp_get_token (pfile);
1022 if (str->type != CPP_STRING)
1023 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1024 pfile->directive->name);
1025 else if (pfile->cb.ident)
1026 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1031 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1032 matching entry, or NULL if none is found. The returned entry could
1033 be the start of a namespace chain, or a pragma. */
1034 static struct pragma_entry *
1035 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1037 while (chain && chain->pragma != pragma)
1038 chain = chain->next;
1043 /* Create and insert a blank pragma entry at the beginning of a
1044 singly-linked CHAIN. */
1045 static struct pragma_entry *
1046 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1048 struct pragma_entry *new_entry;
1050 new_entry = (struct pragma_entry *)
1051 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1053 memset (new_entry, 0, sizeof (struct pragma_entry));
1054 new_entry->next = *chain;
1060 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1061 goes in the global namespace. */
1062 static struct pragma_entry *
1063 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1064 bool allow_name_expansion)
1066 struct pragma_entry **chain = &pfile->pragmas;
1067 struct pragma_entry *entry;
1068 const cpp_hashnode *node;
1072 node = cpp_lookup (pfile, U space, strlen (space));
1073 entry = lookup_pragma_entry (*chain, node);
1076 entry = new_pragma_entry (pfile, chain);
1077 entry->pragma = node;
1078 entry->is_nspace = true;
1079 entry->allow_expansion = allow_name_expansion;
1081 else if (!entry->is_nspace)
1083 else if (entry->allow_expansion != allow_name_expansion)
1085 cpp_error (pfile, CPP_DL_ICE,
1086 "registering pragmas in namespace \"%s\" with mismatched "
1087 "name expansion", space);
1090 chain = &entry->u.space;
1092 else if (allow_name_expansion)
1094 cpp_error (pfile, CPP_DL_ICE,
1095 "registering pragma \"%s\" with name expansion "
1096 "and no namespace", name);
1100 /* Check for duplicates. */
1101 node = cpp_lookup (pfile, U name, strlen (name));
1102 entry = lookup_pragma_entry (*chain, node);
1105 entry = new_pragma_entry (pfile, chain);
1106 entry->pragma = node;
1110 if (entry->is_nspace)
1112 cpp_error (pfile, CPP_DL_ICE,
1113 "registering \"%s\" as both a pragma and a pragma namespace",
1116 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1119 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1124 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1126 register_pragma_internal (cpp_reader *pfile, const char *space,
1127 const char *name, pragma_cb handler)
1129 struct pragma_entry *entry;
1131 entry = register_pragma_1 (pfile, space, name, false);
1132 entry->is_internal = true;
1133 entry->u.handler = handler;
1136 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1137 goes in the global namespace. HANDLER is the handler it will call,
1138 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1139 expansion while parsing pragma NAME. This function is exported
1142 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1143 pragma_cb handler, bool allow_expansion)
1145 struct pragma_entry *entry;
1149 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1153 entry = register_pragma_1 (pfile, space, name, false);
1156 entry->allow_expansion = allow_expansion;
1157 entry->u.handler = handler;
1161 /* Similarly, but create mark the pragma for deferred processing.
1162 When found, a CPP_PRAGMA token will be insertted into the stream
1163 with IDENT in the token->u.pragma slot. */
1165 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1166 const char *name, unsigned int ident,
1167 bool allow_expansion, bool allow_name_expansion)
1169 struct pragma_entry *entry;
1171 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1174 entry->is_deferred = true;
1175 entry->allow_expansion = allow_expansion;
1176 entry->u.ident = ident;
1180 /* APPLE LOCAL begin pragma mark 5614511 */
1181 /* Handle #pragma mark. */
1183 do_pragma_mark (cpp_reader *pfile)
1185 ++pfile->state.skipping;
1186 skip_rest_of_line (pfile);
1187 --pfile->state.skipping;
1189 /* APPLE LOCAL end pragma mark 5614511 */
1191 /* Register the pragmas the preprocessor itself handles. */
1193 _cpp_init_internal_pragmas (cpp_reader *pfile)
1195 /* Pragmas in the global namespace. */
1196 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1197 /* APPLE LOCAL pragma mark 5614511 */
1198 register_pragma_internal (pfile, 0, "mark", do_pragma_mark);
1200 /* New GCC-specific pragmas should be put in the GCC namespace. */
1201 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1202 register_pragma_internal (pfile, "GCC", "system_header",
1203 do_pragma_system_header);
1204 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1207 /* Return the number of registered pragmas in PE. */
1210 count_registered_pragmas (struct pragma_entry *pe)
1213 for (; pe != NULL; pe = pe->next)
1216 ct += count_registered_pragmas (pe->u.space);
1222 /* Save into SD the names of the registered pragmas referenced by PE,
1223 and return a pointer to the next free space in SD. */
1226 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1228 for (; pe != NULL; pe = pe->next)
1231 sd = save_registered_pragmas (pe->u.space, sd);
1232 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1233 HT_LEN (&pe->pragma->ident),
1234 HT_LEN (&pe->pragma->ident) + 1);
1239 /* Return a newly-allocated array which saves the names of the
1240 registered pragmas. */
1243 _cpp_save_pragma_names (cpp_reader *pfile)
1245 int ct = count_registered_pragmas (pfile->pragmas);
1246 char **result = XNEWVEC (char *, ct);
1247 (void) save_registered_pragmas (pfile->pragmas, result);
1251 /* Restore from SD the names of the registered pragmas referenced by PE,
1252 and return a pointer to the next unused name in SD. */
1255 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1258 for (; pe != NULL; pe = pe->next)
1261 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1262 pe->pragma = cpp_lookup (pfile, U *sd, strlen (*sd));
1269 /* Restore the names of the registered pragmas from SAVED. */
1272 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1274 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1278 /* Pragmata handling. We handle some, and pass the rest on to the
1279 front end. C99 defines three pragmas and says that no macro
1280 expansion is to be performed on them; whether or not macro
1281 expansion happens for other pragmas is implementation defined.
1282 This implementation allows for a mix of both, since GCC did not
1283 traditionally macro expand its (few) pragmas, whereas OpenMP
1284 specifies that macro expansion should happen. */
1286 do_pragma (cpp_reader *pfile)
1288 const struct pragma_entry *p = NULL;
1289 const cpp_token *token, *pragma_token = pfile->cur_token;
1291 unsigned int count = 1;
1293 pfile->state.prevent_expansion++;
1295 token = cpp_get_token (pfile);
1297 if (token->type == CPP_NAME)
1299 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1300 if (p && p->is_nspace)
1302 bool allow_name_expansion = p->allow_expansion;
1303 if (allow_name_expansion)
1304 pfile->state.prevent_expansion--;
1305 token = cpp_get_token (pfile);
1306 if (token->type == CPP_NAME)
1307 p = lookup_pragma_entry (p->u.space, token->val.node);
1310 if (allow_name_expansion)
1311 pfile->state.prevent_expansion++;
1320 pfile->directive_result.src_loc = pragma_token->src_loc;
1321 pfile->directive_result.type = CPP_PRAGMA;
1322 pfile->directive_result.flags = pragma_token->flags;
1323 pfile->directive_result.val.pragma = p->u.ident;
1324 pfile->state.in_deferred_pragma = true;
1325 pfile->state.pragma_allow_expansion = p->allow_expansion;
1326 if (!p->allow_expansion)
1327 pfile->state.prevent_expansion++;
1331 /* Since the handler below doesn't get the line number, that
1332 it might need for diagnostics, make sure it has the right
1333 numbers in place. */
1334 if (pfile->cb.line_change)
1335 (*pfile->cb.line_change) (pfile, pragma_token, false);
1336 if (p->allow_expansion)
1337 pfile->state.prevent_expansion--;
1338 (*p->u.handler) (pfile);
1339 if (p->allow_expansion)
1340 pfile->state.prevent_expansion++;
1343 else if (pfile->cb.def_pragma)
1345 if (count == 1 || pfile->context->prev == NULL)
1346 _cpp_backup_tokens (pfile, count);
1349 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1350 won't allow backing 2 tokens. */
1351 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1352 reads both tokens, we could perhaps free it, but if it doesn't,
1353 we don't know the exact lifespan. */
1354 cpp_token *toks = XNEWVEC (cpp_token, 2);
1356 toks[0].flags |= NO_EXPAND;
1358 toks[1].flags |= NO_EXPAND;
1359 _cpp_push_token_context (pfile, NULL, toks, 2);
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;
1412 /* Mark the current header as a system header. This will suppress
1413 some categories of warnings (notably those from -pedantic). It is
1414 intended for use in system libraries that cannot be implemented in
1415 conforming C, but cannot be certain that their headers appear in a
1416 system include directory. To prevent abuse, it is rejected in the
1417 primary source file. */
1419 do_pragma_system_header (cpp_reader *pfile)
1421 cpp_buffer *buffer = pfile->buffer;
1423 if (buffer->prev == 0)
1424 cpp_error (pfile, CPP_DL_WARNING,
1425 "#pragma system_header ignored outside include file");
1429 skip_rest_of_line (pfile);
1430 cpp_make_system_header (pfile, 1, 0);
1434 /* Check the modified date of the current include file against a specified
1435 file. Issue a diagnostic, if the specified file is newer. We use this to
1436 determine if a fixed header should be refixed. */
1438 do_pragma_dependency (cpp_reader *pfile)
1441 int angle_brackets, ordering;
1443 fname = parse_include (pfile, &angle_brackets, NULL);
1447 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1449 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1450 else if (ordering > 0)
1452 cpp_error (pfile, CPP_DL_WARNING,
1453 "current file is older than %s", fname);
1454 if (cpp_get_token (pfile)->type != CPP_EOF)
1456 _cpp_backup_tokens (pfile, 1);
1457 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1461 free ((void *) fname);
1464 /* Get a token but skip padding. */
1465 static const cpp_token *
1466 get_token_no_padding (cpp_reader *pfile)
1470 const cpp_token *result = cpp_get_token (pfile);
1471 if (result->type != CPP_PADDING)
1476 /* Check syntax is "(string-literal)". Returns the string on success,
1477 or NULL on failure. */
1478 static const cpp_token *
1479 get__Pragma_string (cpp_reader *pfile)
1481 const cpp_token *string;
1483 if (get_token_no_padding (pfile)->type != CPP_OPEN_PAREN)
1486 string = get_token_no_padding (pfile);
1487 if (string->type != CPP_STRING && string->type != CPP_WSTRING)
1490 if (get_token_no_padding (pfile)->type != CPP_CLOSE_PAREN)
1496 /* Destringize IN into a temporary buffer, by removing the first \ of
1497 \" and \\ sequences, and process the result as a #pragma directive. */
1499 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1501 const unsigned char *src, *limit;
1502 char *dest, *result;
1503 cpp_context *saved_context;
1504 cpp_token *saved_cur_token;
1505 tokenrun *saved_cur_run;
1509 dest = result = (char *) alloca (in->len - 1);
1510 src = in->text + 1 + (in->text[0] == 'L');
1511 limit = in->text + in->len - 1;
1514 /* We know there is a character following the backslash. */
1515 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1521 /* Ugh; an awful kludge. We are really not set up to be lexing
1522 tokens when in the middle of a macro expansion. Use a new
1523 context to force cpp_get_token to lex, and so skip_rest_of_line
1524 doesn't go beyond the end of the text. Also, remember the
1525 current lexing position so we can return to it later.
1527 Something like line-at-a-time lexing should remove the need for
1529 saved_context = pfile->context;
1530 saved_cur_token = pfile->cur_token;
1531 saved_cur_run = pfile->cur_run;
1533 pfile->context = XNEW (cpp_context);
1534 pfile->context->macro = 0;
1535 pfile->context->prev = 0;
1536 pfile->context->next = 0;
1538 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1539 until we've read all of the tokens that we want. */
1540 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1541 /* from_stage3 */ true);
1542 /* ??? Antique Disgusting Hack. What does this do? */
1543 if (pfile->buffer->prev)
1544 pfile->buffer->file = pfile->buffer->prev->file;
1546 start_directive (pfile);
1547 _cpp_clean_line (pfile);
1549 end_directive (pfile, 1);
1551 /* We always insert at least one token, the directive result. It'll
1552 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1553 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1555 /* If we're not handling the pragma internally, read all of the tokens from
1556 the string buffer now, while the string buffer is still installed. */
1557 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1558 to me what the true lifespan of the tokens are. It would appear that
1559 the lifespan is the entire parse of the main input stream, in which case
1560 this may not be wrong. */
1561 if (pfile->directive_result.type == CPP_PRAGMA)
1567 toks = XNEWVEC (cpp_token, maxcount);
1568 toks[0] = pfile->directive_result;
1572 if (count == maxcount)
1574 maxcount = maxcount * 3 / 2;
1575 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1577 toks[count] = *cpp_get_token (pfile);
1578 /* Macros have been already expanded by cpp_get_token
1579 if the pragma allowed expansion. */
1580 toks[count++].flags |= NO_EXPAND;
1582 while (toks[count-1].type != CPP_PRAGMA_EOL);
1587 toks = XNEW (cpp_token);
1588 toks[0] = pfile->directive_result;
1590 /* If we handled the entire pragma internally, make sure we get the
1591 line number correct for the next token. */
1592 if (pfile->cb.line_change)
1593 pfile->cb.line_change (pfile, pfile->cur_token, false);
1596 /* Finish inlining run_directive. */
1597 pfile->buffer->file = NULL;
1598 _cpp_pop_buffer (pfile);
1600 /* Reset the old macro state before ... */
1601 XDELETE (pfile->context);
1602 pfile->context = saved_context;
1603 pfile->cur_token = saved_cur_token;
1604 pfile->cur_run = saved_cur_run;
1606 /* ... inserting the new tokens we collected. */
1607 _cpp_push_token_context (pfile, NULL, toks, count);
1610 /* Handle the _Pragma operator. */
1612 _cpp_do__Pragma (cpp_reader *pfile)
1614 const cpp_token *string = get__Pragma_string (pfile);
1615 pfile->directive_result.type = CPP_PADDING;
1618 destringize_and_run (pfile, &string->val.str);
1620 cpp_error (pfile, CPP_DL_ERROR,
1621 "_Pragma takes a parenthesized string literal");
1624 /* Handle #ifdef. */
1626 do_ifdef (cpp_reader *pfile)
1630 if (! pfile->state.skipping)
1632 const cpp_hashnode *node = lex_macro_node (pfile);
1636 skip = node->type != NT_MACRO;
1637 _cpp_mark_macro_used (node);
1642 push_conditional (pfile, skip, T_IFDEF, 0);
1645 /* Handle #ifndef. */
1647 do_ifndef (cpp_reader *pfile)
1650 const cpp_hashnode *node = 0;
1652 if (! pfile->state.skipping)
1654 node = lex_macro_node (pfile);
1658 skip = node->type == NT_MACRO;
1659 _cpp_mark_macro_used (node);
1664 push_conditional (pfile, skip, T_IFNDEF, node);
1667 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1668 pfile->mi_ind_cmacro so we can handle multiple-include
1669 optimizations. If macro expansion occurs in the expression, we
1670 cannot treat it as a controlling conditional, since the expansion
1671 could change in the future. That is handled by cpp_get_token. */
1673 do_if (cpp_reader *pfile)
1677 if (! pfile->state.skipping)
1678 skip = _cpp_parse_expr (pfile) == false;
1680 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1683 /* Flip skipping state if appropriate and continue without changing
1684 if_stack; this is so that the error message for missing #endif's
1685 etc. will point to the original #if. */
1687 do_else (cpp_reader *pfile)
1689 cpp_buffer *buffer = pfile->buffer;
1690 struct if_stack *ifs = buffer->if_stack;
1693 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1696 if (ifs->type == T_ELSE)
1698 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1699 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1700 "the conditional began here");
1704 /* Skip any future (erroneous) #elses or #elifs. */
1705 pfile->state.skipping = ifs->skip_elses;
1706 ifs->skip_elses = true;
1708 /* Invalidate any controlling macro. */
1711 /* Only check EOL if was not originally skipping. */
1712 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1717 /* Handle a #elif directive by not changing if_stack either. See the
1718 comment above do_else. */
1720 do_elif (cpp_reader *pfile)
1722 cpp_buffer *buffer = pfile->buffer;
1723 struct if_stack *ifs = buffer->if_stack;
1726 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1729 if (ifs->type == T_ELSE)
1731 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1732 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1733 "the conditional began here");
1737 /* Only evaluate this if we aren't skipping elses. During
1738 evaluation, set skipping to false to get lexer warnings. */
1739 if (ifs->skip_elses)
1740 pfile->state.skipping = 1;
1743 pfile->state.skipping = 0;
1744 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1745 ifs->skip_elses = ! pfile->state.skipping;
1748 /* Invalidate any controlling macro. */
1753 /* #endif pops the if stack and resets pfile->state.skipping. */
1755 do_endif (cpp_reader *pfile)
1757 cpp_buffer *buffer = pfile->buffer;
1758 struct if_stack *ifs = buffer->if_stack;
1761 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1764 /* Only check EOL if was not originally skipping. */
1765 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1768 /* If potential control macro, we go back outside again. */
1769 if (ifs->next == 0 && ifs->mi_cmacro)
1771 pfile->mi_valid = true;
1772 pfile->mi_cmacro = ifs->mi_cmacro;
1775 buffer->if_stack = ifs->next;
1776 pfile->state.skipping = ifs->was_skipping;
1777 obstack_free (&pfile->buffer_ob, ifs);
1781 /* Push an if_stack entry for a preprocessor conditional, and set
1782 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1783 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1784 we need to check here that we are at the top of the file. */
1786 push_conditional (cpp_reader *pfile, int skip, int type,
1787 const cpp_hashnode *cmacro)
1789 struct if_stack *ifs;
1790 cpp_buffer *buffer = pfile->buffer;
1792 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1793 ifs->line = pfile->directive_line;
1794 ifs->next = buffer->if_stack;
1795 ifs->skip_elses = pfile->state.skipping || !skip;
1796 ifs->was_skipping = pfile->state.skipping;
1798 /* This condition is effectively a test for top-of-file. */
1799 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1800 ifs->mi_cmacro = cmacro;
1804 pfile->state.skipping = skip;
1805 buffer->if_stack = ifs;
1808 /* Read the tokens of the answer into the macro pool, in a directive
1809 of type TYPE. Only commit the memory if we intend it as permanent
1810 storage, i.e. the #assert case. Returns 0 on success, and sets
1811 ANSWERP to point to the answer. */
1813 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1815 const cpp_token *paren;
1816 struct answer *answer;
1817 unsigned int acount;
1819 /* In a conditional, it is legal to not have an open paren. We
1820 should save the following token in this case. */
1821 paren = cpp_get_token (pfile);
1823 /* If not a paren, see if we're OK. */
1824 if (paren->type != CPP_OPEN_PAREN)
1826 /* In a conditional no answer is a test for any answer. It
1827 could be followed by any token. */
1830 _cpp_backup_tokens (pfile, 1);
1834 /* #unassert with no answer is valid - it removes all answers. */
1835 if (type == T_UNASSERT && paren->type == CPP_EOF)
1838 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1842 for (acount = 0;; acount++)
1845 const cpp_token *token = cpp_get_token (pfile);
1848 if (token->type == CPP_CLOSE_PAREN)
1851 if (token->type == CPP_EOF)
1853 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1857 /* struct answer includes the space for one token. */
1858 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1860 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1861 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1863 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1866 /* Drop whitespace at start, for answer equivalence purposes. */
1868 dest->flags &= ~PREV_WHITE;
1873 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1877 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1878 answer->count = acount;
1879 answer->next = NULL;
1885 /* Parses an assertion directive of type TYPE, returning a pointer to
1886 the hash node of the predicate, or 0 on error. If an answer was
1887 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1888 static cpp_hashnode *
1889 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1891 cpp_hashnode *result = 0;
1892 const cpp_token *predicate;
1894 /* We don't expand predicates or answers. */
1895 pfile->state.prevent_expansion++;
1898 predicate = cpp_get_token (pfile);
1899 if (predicate->type == CPP_EOF)
1900 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1901 else if (predicate->type != CPP_NAME)
1902 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1903 else if (parse_answer (pfile, answerp, type) == 0)
1905 unsigned int len = NODE_LEN (predicate->val.node);
1906 unsigned char *sym = (unsigned char *) alloca (len + 1);
1908 /* Prefix '#' to get it out of macro namespace. */
1910 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1911 result = cpp_lookup (pfile, sym, len + 1);
1914 pfile->state.prevent_expansion--;
1918 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1919 or a pointer to NULL if the answer is not in the chain. */
1920 static struct answer **
1921 find_answer (cpp_hashnode *node, const struct answer *candidate)
1924 struct answer **result;
1926 for (result = &node->value.answers; *result; result = &(*result)->next)
1928 struct answer *answer = *result;
1930 if (answer->count == candidate->count)
1932 for (i = 0; i < answer->count; i++)
1933 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1936 if (i == answer->count)
1944 /* Test an assertion within a preprocessor conditional. Returns
1945 nonzero on failure, zero on success. On success, the result of
1946 the test is written into VALUE, otherwise the value 0. */
1948 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
1950 struct answer *answer;
1953 node = parse_assertion (pfile, &answer, T_IF);
1955 /* For recovery, an erroneous assertion expression is handled as a
1956 failing assertion. */
1960 *value = (node->type == NT_ASSERTION &&
1961 (answer == 0 || *find_answer (node, answer) != 0));
1962 else if (pfile->cur_token[-1].type == CPP_EOF)
1963 _cpp_backup_tokens (pfile, 1);
1965 /* We don't commit the memory for the answer - it's temporary only. */
1969 /* Handle #assert. */
1971 do_assert (cpp_reader *pfile)
1973 struct answer *new_answer;
1976 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1981 /* Place the new answer in the answer list. First check there
1982 is not a duplicate. */
1983 new_answer->next = 0;
1984 if (node->type == NT_ASSERTION)
1986 if (*find_answer (node, new_answer))
1988 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
1989 NODE_NAME (node) + 1);
1992 new_answer->next = node->value.answers;
1995 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
1996 * sizeof (cpp_token));
1997 /* Commit or allocate storage for the object. */
1998 if (pfile->hash_table->alloc_subobject)
2000 struct answer *temp_answer = new_answer;
2001 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2003 memcpy (new_answer, temp_answer, answer_size);
2006 BUFF_FRONT (pfile->a_buff) += answer_size;
2008 node->type = NT_ASSERTION;
2009 node->value.answers = new_answer;
2014 /* Handle #unassert. */
2016 do_unassert (cpp_reader *pfile)
2019 struct answer *answer;
2021 node = parse_assertion (pfile, &answer, T_UNASSERT);
2022 /* It isn't an error to #unassert something that isn't asserted. */
2023 if (node && node->type == NT_ASSERTION)
2027 struct answer **p = find_answer (node, answer), *temp;
2029 /* Remove the answer from the list. */
2034 /* Did we free the last answer? */
2035 if (node->value.answers == 0)
2036 node->type = NT_VOID;
2041 _cpp_free_definition (node);
2044 /* We don't commit the memory for the answer - it's temporary only. */
2047 /* These are for -D, -U, -A. */
2049 /* Process the string STR as if it appeared as the body of a #define.
2050 If STR is just an identifier, define it with value 1.
2051 If STR has anything after the identifier, then it should
2052 be identifier=definition. */
2054 cpp_define (cpp_reader *pfile, const char *str)
2059 /* Copy the entire option so we can modify it.
2060 Change the first "=" in the string to a space. If there is none,
2061 tack " 1" on the end. */
2063 count = strlen (str);
2064 buf = (char *) alloca (count + 3);
2065 memcpy (buf, str, count);
2067 p = strchr (str, '=');
2077 run_directive (pfile, T_DEFINE, buf, count);
2080 /* Slight variant of the above for use by initialize_builtins. */
2082 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2084 size_t len = strlen (str);
2085 char *buf = (char *) alloca (len + 1);
2086 memcpy (buf, str, len);
2088 run_directive (pfile, T_DEFINE, buf, len);
2091 /* Process MACRO as if it appeared as the body of an #undef. */
2093 cpp_undef (cpp_reader *pfile, const char *macro)
2095 size_t len = strlen (macro);
2096 char *buf = (char *) alloca (len + 1);
2097 memcpy (buf, macro, len);
2099 run_directive (pfile, T_UNDEF, buf, len);
2102 /* Process the string STR as if it appeared as the body of a #assert. */
2104 cpp_assert (cpp_reader *pfile, const char *str)
2106 handle_assertion (pfile, str, T_ASSERT);
2109 /* Process STR as if it appeared as the body of an #unassert. */
2111 cpp_unassert (cpp_reader *pfile, const char *str)
2113 handle_assertion (pfile, str, T_UNASSERT);
2116 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2118 handle_assertion (cpp_reader *pfile, const char *str, int type)
2120 size_t count = strlen (str);
2121 const char *p = strchr (str, '=');
2123 /* Copy the entire option so we can modify it. Change the first
2124 "=" in the string to a '(', and tack a ')' on the end. */
2125 char *buf = (char *) alloca (count + 2);
2127 memcpy (buf, str, count);
2136 run_directive (pfile, type, str, count);
2139 /* The number of errors for a given reader. */
2141 cpp_errors (cpp_reader *pfile)
2143 return pfile->errors;
2146 /* The options structure. */
2148 cpp_get_options (cpp_reader *pfile)
2150 return &pfile->opts;
2153 /* The callbacks structure. */
2155 cpp_get_callbacks (cpp_reader *pfile)
2160 /* Copy the given callbacks structure to our own. */
2162 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2167 /* The dependencies structure. (Creates one if it hasn't already been.) */
2169 cpp_get_deps (cpp_reader *pfile)
2172 pfile->deps = deps_init ();
2176 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2177 doesn't fail. It does not generate a file change call back; that
2178 is the responsibility of the caller. */
2180 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2183 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2185 /* Clears, amongst other things, if_stack and mi_cmacro. */
2186 memset (new_buffer, 0, sizeof (cpp_buffer));
2188 new_buffer->next_line = new_buffer->buf = buffer;
2189 new_buffer->rlimit = buffer + len;
2190 new_buffer->from_stage3 = from_stage3;
2191 new_buffer->prev = pfile->buffer;
2192 new_buffer->need_line = true;
2194 pfile->buffer = new_buffer;
2199 /* Pops a single buffer, with a file change call-back if appropriate.
2200 Then pushes the next -include file, if any remain. */
2202 _cpp_pop_buffer (cpp_reader *pfile)
2204 cpp_buffer *buffer = pfile->buffer;
2205 struct _cpp_file *inc = buffer->file;
2206 struct if_stack *ifs;
2208 /* Walk back up the conditional stack till we reach its level at
2209 entry to this file, issuing error messages. */
2210 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2211 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2212 "unterminated #%s", dtable[ifs->type].name);
2214 /* In case of a missing #endif. */
2215 pfile->state.skipping = 0;
2217 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2218 pfile->buffer = buffer->prev;
2220 free (buffer->notes);
2222 /* Free the buffer object now; we may want to push a new buffer
2223 in _cpp_push_next_include_file. */
2224 obstack_free (&pfile->buffer_ob, buffer);
2228 _cpp_pop_file_buffer (pfile, inc);
2230 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2234 /* Enter all recognized directives in the hash table. */
2236 _cpp_init_directives (cpp_reader *pfile)
2241 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2243 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2244 node->is_directive = 1;
2245 node->directive_index = i;