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,
4 2007, 2008 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. */
54 struct pragma_entry *space;
59 /* Values for the origin field of struct directive. KANDR directives
60 come from traditional (K&R) C. STDC89 directives come from the
61 1989 C standard. EXTENSION directives are extensions. */
66 /* Values for the flags field of struct directive. COND indicates a
67 conditional; IF_COND an opening conditional. INCL means to treat
68 "..." and <...> as q-char and h-char sequences respectively. IN_I
69 means this directive should be handled even if -fpreprocessed is in
70 effect (these are the directives with callback hooks).
72 EXPAND is set on directives that are always macro-expanded. */
74 #define IF_COND (1 << 1)
77 #define EXPAND (1 << 4)
78 #define DEPRECATED (1 << 5)
80 /* Defines one #-directive, including how to handle it. */
81 typedef void (*directive_handler) (cpp_reader *);
82 typedef struct directive directive;
85 directive_handler handler; /* Function to handle directive. */
86 const uchar *name; /* Name of directive. */
87 unsigned short length; /* Length of name. */
88 unsigned char origin; /* Origin of directive. */
89 unsigned char flags; /* Flags describing this directive. */
92 /* Forward declarations. */
94 static void skip_rest_of_line (cpp_reader *);
95 static void check_eol (cpp_reader *);
96 static void start_directive (cpp_reader *);
97 static void prepare_directive_trad (cpp_reader *);
98 static void end_directive (cpp_reader *, int);
99 static void directive_diagnostics (cpp_reader *, const directive *, int);
100 static void run_directive (cpp_reader *, int, const char *, size_t);
101 static char *glue_header_name (cpp_reader *);
102 static const char *parse_include (cpp_reader *, int *, const cpp_token ***);
103 static void push_conditional (cpp_reader *, int, int, const cpp_hashnode *);
104 static unsigned int read_flag (cpp_reader *, unsigned int);
105 static int strtoul_for_line (const uchar *, unsigned int, unsigned long *);
106 static void do_diagnostic (cpp_reader *, int, int);
107 static cpp_hashnode *lex_macro_node (cpp_reader *, bool);
108 static int undefine_macros (cpp_reader *, cpp_hashnode *, void *);
109 static void do_include_common (cpp_reader *, enum include_type);
110 static struct pragma_entry *lookup_pragma_entry (struct pragma_entry *,
111 const cpp_hashnode *);
112 static int count_registered_pragmas (struct pragma_entry *);
113 static char ** save_registered_pragmas (struct pragma_entry *, char **);
114 static char ** restore_registered_pragmas (cpp_reader *, struct pragma_entry *,
116 static void do_pragma_once (cpp_reader *);
117 static void do_pragma_poison (cpp_reader *);
118 static void do_pragma_system_header (cpp_reader *);
119 static void do_pragma_dependency (cpp_reader *);
120 static void do_linemarker (cpp_reader *);
121 static const cpp_token *get_token_no_padding (cpp_reader *);
122 static const cpp_token *get__Pragma_string (cpp_reader *);
123 static void destringize_and_run (cpp_reader *, const cpp_string *);
124 static int parse_answer (cpp_reader *, struct answer **, int);
125 static cpp_hashnode *parse_assertion (cpp_reader *, struct answer **, int);
126 static struct answer ** find_answer (cpp_hashnode *, const struct answer *);
127 static void handle_assertion (cpp_reader *, const char *, int);
129 /* This is the table of directive handlers. It is ordered by
130 frequency of occurrence; the numbers at the end are directive
131 counts from all the source code I have lying around (egcs and libc
132 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
133 pcmcia-cs-3.0.9). This is no longer important as directive lookup
134 is now O(1). All extensions other than #warning, #include_next,
135 and #import are deprecated. The name is where the extension
136 appears to have come from. */
138 #define DIRECTIVE_TABLE \
139 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
140 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
141 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
142 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
143 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
144 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
145 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
146 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
147 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
148 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
149 D(error, T_ERROR, STDC89, 0) /* 475 */ \
150 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
151 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
152 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
153 D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
154 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
155 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
156 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
157 D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
159 /* #sccs is synonymous with #ident. */
160 #define do_sccs do_ident
162 /* Use the table to generate a series of prototypes, an enum for the
163 directive names, and an array of directive handlers. */
165 #define D(name, t, o, f) static void do_##name (cpp_reader *);
169 #define D(n, tag, o, f) tag,
177 #define D(name, t, origin, flags) \
178 { do_##name, (const uchar *) #name, \
179 sizeof #name - 1, origin, flags },
180 static const directive dtable[] =
185 #undef DIRECTIVE_TABLE
187 /* Wrapper struct directive for linemarkers.
188 The origin is more or less true - the original K+R cpp
189 did use this notation in its preprocessed output. */
190 static const directive linemarker_dir =
192 do_linemarker, UC"#", 1, KANDR, IN_I
195 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
197 /* Skip any remaining tokens in a directive. */
199 skip_rest_of_line (cpp_reader *pfile)
201 /* Discard all stacked contexts. */
202 while (pfile->context->prev)
203 _cpp_pop_context (pfile);
205 /* Sweep up all tokens remaining on the line. */
207 while (_cpp_lex_token (pfile)->type != CPP_EOF)
211 /* Ensure there are no stray tokens at the end of a directive. */
213 check_eol (cpp_reader *pfile)
215 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
216 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
217 pfile->directive->name);
220 /* Ensure there are no stray tokens other than comments at the end of
221 a directive, and gather the comments. */
222 static const cpp_token **
223 check_eol_return_comments (cpp_reader *pfile)
227 const cpp_token **buf;
229 buf = XNEWVEC (const cpp_token *, capacity);
235 const cpp_token *tok;
237 tok = _cpp_lex_token (pfile);
238 if (tok->type == CPP_EOF)
240 if (tok->type != CPP_COMMENT)
241 cpp_error (pfile, CPP_DL_PEDWARN,
242 "extra tokens at end of #%s directive",
243 pfile->directive->name);
246 if (c + 1 >= capacity)
249 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
260 /* Called when entering a directive, _Pragma or command-line directive. */
262 start_directive (cpp_reader *pfile)
264 /* Setup in-directive state. */
265 pfile->state.in_directive = 1;
266 pfile->state.save_comments = 0;
267 pfile->directive_result.type = CPP_PADDING;
269 /* Some handlers need the position of the # for diagnostics. */
270 pfile->directive_line = pfile->line_table->highest_line;
273 /* Called when leaving a directive, _Pragma or command-line directive. */
275 end_directive (cpp_reader *pfile, int skip_line)
277 if (pfile->state.in_deferred_pragma)
279 else if (CPP_OPTION (pfile, traditional))
281 /* Revert change of prepare_directive_trad. */
282 pfile->state.prevent_expansion--;
284 if (pfile->directive != &dtable[T_DEFINE])
285 _cpp_remove_overlay (pfile);
287 /* We don't skip for an assembler #. */
290 skip_rest_of_line (pfile);
291 if (!pfile->keep_tokens)
293 pfile->cur_run = &pfile->base_run;
294 pfile->cur_token = pfile->base_run.base;
299 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
300 pfile->state.in_directive = 0;
301 pfile->state.in_expression = 0;
302 pfile->state.angled_headers = 0;
303 pfile->directive = 0;
306 /* Prepare to handle the directive in pfile->directive. */
308 prepare_directive_trad (cpp_reader *pfile)
310 if (pfile->directive != &dtable[T_DEFINE])
312 bool no_expand = (pfile->directive
313 && ! (pfile->directive->flags & EXPAND));
314 bool was_skipping = pfile->state.skipping;
316 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
317 || pfile->directive == &dtable[T_ELIF]);
318 if (pfile->state.in_expression)
319 pfile->state.skipping = false;
322 pfile->state.prevent_expansion++;
323 _cpp_scan_out_logical_line (pfile, NULL);
325 pfile->state.prevent_expansion--;
327 pfile->state.skipping = was_skipping;
328 _cpp_overlay_buffer (pfile, pfile->out.base,
329 pfile->out.cur - pfile->out.base);
332 /* Stop ISO C from expanding anything. */
333 pfile->state.prevent_expansion++;
336 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
337 the '#' was indented. */
339 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
341 /* Issue -pedantic or deprecated warnings for extensions. We let
342 -pedantic take precedence if both are applicable. */
343 if (! pfile->state.skipping)
345 if (dir->origin == EXTENSION
346 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
347 && CPP_PEDANTIC (pfile))
348 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
349 else if (((dir->flags & DEPRECATED) != 0
350 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
351 && CPP_OPTION (pfile, warn_deprecated))
352 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
356 /* Traditionally, a directive is ignored unless its # is in
357 column 1. Therefore in code intended to work with K+R
358 compilers, directives added by C89 must have their #
359 indented, and directives present in traditional C must not.
360 This is true even of directives in skipped conditional
361 blocks. #elif cannot be used at all. */
362 if (CPP_WTRADITIONAL (pfile))
364 if (dir == &dtable[T_ELIF])
365 cpp_error (pfile, CPP_DL_WARNING,
366 "suggest not using #elif in traditional C");
367 else if (indented && dir->origin == KANDR)
368 cpp_error (pfile, CPP_DL_WARNING,
369 "traditional C ignores #%s with the # indented",
371 else if (!indented && dir->origin != KANDR)
372 cpp_error (pfile, CPP_DL_WARNING,
373 "suggest hiding #%s from traditional C with an indented #",
378 /* Check if we have a known directive. INDENTED is nonzero if the
379 '#' of the directive was indented. This function is in this file
380 to save unnecessarily exporting dtable etc. to lex.c. Returns
381 nonzero if the line of tokens has been handled, zero if we should
382 continue processing the line. */
384 _cpp_handle_directive (cpp_reader *pfile, int indented)
386 const directive *dir = 0;
387 const cpp_token *dname;
388 bool was_parsing_args = pfile->state.parsing_args;
389 bool was_discarding_output = pfile->state.discarding_output;
392 if (was_discarding_output)
393 pfile->state.prevent_expansion = 0;
395 if (was_parsing_args)
397 if (CPP_OPTION (pfile, pedantic))
398 cpp_error (pfile, CPP_DL_PEDWARN,
399 "embedding a directive within macro arguments is not portable");
400 pfile->state.parsing_args = 0;
401 pfile->state.prevent_expansion = 0;
403 start_directive (pfile);
404 dname = _cpp_lex_token (pfile);
406 if (dname->type == CPP_NAME)
408 if (dname->val.node->is_directive)
409 dir = &dtable[dname->val.node->directive_index];
411 /* We do not recognize the # followed by a number extension in
413 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
415 dir = &linemarker_dir;
416 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
417 && ! pfile->state.skipping)
418 cpp_error (pfile, CPP_DL_PEDWARN,
419 "style of line directive is a GCC extension");
424 /* If we have a directive that is not an opening conditional,
425 invalidate any control macro. */
426 if (! (dir->flags & IF_COND))
427 pfile->mi_valid = false;
429 /* Kluge alert. In order to be sure that code like this
434 does not cause '#define foo bar' to get executed when
435 compiled with -save-temps, we recognize directives in
436 -fpreprocessed mode only if the # is in column 1. macro.c
437 puts a space in front of any '#' at the start of a macro.
439 We exclude the -fdirectives-only case because macro expansion
440 has not been performed yet, and block comments can cause spaces
441 to preceed the directive. */
442 if (CPP_OPTION (pfile, preprocessed)
443 && !CPP_OPTION (pfile, directives_only)
444 && (indented || !(dir->flags & IN_I)))
451 /* In failed conditional groups, all non-conditional
452 directives are ignored. Before doing that, whether
453 skipping or not, we should lex angle-bracketed headers
454 correctly, and maybe output some diagnostics. */
455 pfile->state.angled_headers = dir->flags & INCL;
456 pfile->state.directive_wants_padding = dir->flags & INCL;
457 if (! CPP_OPTION (pfile, preprocessed))
458 directive_diagnostics (pfile, dir, indented);
459 if (pfile->state.skipping && !(dir->flags & COND))
463 else if (dname->type == CPP_EOF)
464 ; /* CPP_EOF is the "null directive". */
467 /* An unknown directive. Don't complain about it in assembly
468 source: we don't know where the comments are, and # may
469 introduce assembler pseudo-ops. Don't complain about invalid
470 directives in skipped conditional groups (6.10 p4). */
471 if (CPP_OPTION (pfile, lang) == CLK_ASM)
473 else if (!pfile->state.skipping)
474 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
475 cpp_token_as_text (pfile, dname));
478 pfile->directive = dir;
479 if (CPP_OPTION (pfile, traditional))
480 prepare_directive_trad (pfile);
483 pfile->directive->handler (pfile);
485 _cpp_backup_tokens (pfile, 1);
487 end_directive (pfile, skip);
488 if (was_parsing_args && !pfile->state.in_deferred_pragma)
490 /* Restore state when within macro args. */
491 pfile->state.parsing_args = 2;
492 pfile->state.prevent_expansion = 1;
494 if (was_discarding_output)
495 pfile->state.prevent_expansion = 1;
499 /* Directive handler wrapper used by the command line option
500 processor. BUF is \n terminated. */
502 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
504 cpp_push_buffer (pfile, (const uchar *) buf, count,
505 /* from_stage3 */ true);
506 start_directive (pfile);
508 /* This is a short-term fix to prevent a leading '#' being
509 interpreted as a directive. */
510 _cpp_clean_line (pfile);
512 pfile->directive = &dtable[dir_no];
513 if (CPP_OPTION (pfile, traditional))
514 prepare_directive_trad (pfile);
515 pfile->directive->handler (pfile);
516 end_directive (pfile, 1);
517 _cpp_pop_buffer (pfile);
520 /* Checks for validity the macro name in #define, #undef, #ifdef and
521 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
522 processing a #define or #undefine directive, and false
524 static cpp_hashnode *
525 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
527 const cpp_token *token = _cpp_lex_token (pfile);
529 /* The token immediately after #define must be an identifier. That
530 identifier may not be "defined", per C99 6.10.8p4.
531 In C++, it may not be any of the "named operators" either,
532 per C++98 [lex.digraph], [lex.key].
533 Finally, the identifier may not have been poisoned. (In that case
534 the lexer has issued the error message for us.) */
536 if (token->type == CPP_NAME)
538 cpp_hashnode *node = token->val.node;
540 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
541 cpp_error (pfile, CPP_DL_ERROR,
542 "\"defined\" cannot be used as a macro name");
543 else if (! (node->flags & NODE_POISONED))
546 else if (token->flags & NAMED_OP)
547 cpp_error (pfile, CPP_DL_ERROR,
548 "\"%s\" cannot be used as a macro name as it is an operator in C++",
549 NODE_NAME (token->val.node));
550 else if (token->type == CPP_EOF)
551 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
552 pfile->directive->name);
554 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
559 /* Process a #define directive. Most work is done in macro.c. */
561 do_define (cpp_reader *pfile)
563 cpp_hashnode *node = lex_macro_node (pfile, true);
567 /* If we have been requested to expand comments into macros,
568 then re-enable saving of comments. */
569 pfile->state.save_comments =
570 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
572 if (pfile->cb.before_define)
573 pfile->cb.before_define (pfile);
575 if (_cpp_create_definition (pfile, node))
576 if (pfile->cb.define)
577 pfile->cb.define (pfile, pfile->directive_line, node);
579 node->flags &= ~NODE_USED;
583 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
585 do_undef (cpp_reader *pfile)
587 cpp_hashnode *node = lex_macro_node (pfile, true);
591 if (pfile->cb.before_define)
592 pfile->cb.before_define (pfile);
595 pfile->cb.undef (pfile, pfile->directive_line, node);
597 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
598 identifier is not currently defined as a macro name. */
599 if (node->type == NT_MACRO)
601 if (node->flags & NODE_WARN)
602 cpp_error (pfile, CPP_DL_WARNING,
603 "undefining \"%s\"", NODE_NAME (node));
605 if (CPP_OPTION (pfile, warn_unused_macros))
606 _cpp_warn_if_unused_macro (pfile, node, NULL);
608 _cpp_free_definition (node);
615 /* Undefine a single macro/assertion/whatever. */
618 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
619 void *data_p ATTRIBUTE_UNUSED)
621 /* Body of _cpp_free_definition inlined here for speed.
622 Macros and assertions no longer have anything to free. */
624 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
628 /* Undefine all macros and assertions. */
631 cpp_undef_all (cpp_reader *pfile)
633 cpp_forall_identifiers (pfile, undefine_macros, NULL);
637 /* Helper routine used by parse_include. Reinterpret the current line
638 as an h-char-sequence (< ... >); we are looking at the first token
639 after the <. Returns a malloced filename. */
641 glue_header_name (cpp_reader *pfile)
643 const cpp_token *token;
645 size_t len, total_len = 0, capacity = 1024;
647 /* To avoid lexed tokens overwriting our glued name, we can only
648 allocate from the string pool once we've lexed everything. */
649 buffer = XNEWVEC (char, capacity);
652 token = get_token_no_padding (pfile);
654 if (token->type == CPP_GREATER)
656 if (token->type == CPP_EOF)
658 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
662 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
663 if (total_len + len > capacity)
665 capacity = (capacity + len) * 2;
666 buffer = XRESIZEVEC (char, buffer, capacity);
669 if (token->flags & PREV_WHITE)
670 buffer[total_len++] = ' ';
672 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
677 buffer[total_len] = '\0';
681 /* Returns the file name of #include, #include_next, #import and
682 #pragma dependency. The string is malloced and the caller should
683 free it. Returns NULL on error. */
685 parse_include (cpp_reader *pfile, int *pangle_brackets,
686 const cpp_token ***buf)
689 const cpp_token *header;
691 /* Allow macro expansion. */
692 header = get_token_no_padding (pfile);
693 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
695 fname = XNEWVEC (char, header->val.str.len - 1);
696 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
697 fname[header->val.str.len - 2] = '\0';
698 *pangle_brackets = header->type == CPP_HEADER_NAME;
700 else if (header->type == CPP_LESS)
702 fname = glue_header_name (pfile);
703 *pangle_brackets = 1;
707 const unsigned char *dir;
709 if (pfile->directive == &dtable[T_PRAGMA])
710 dir = UC"pragma dependency";
712 dir = pfile->directive->name;
713 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
719 if (pfile->directive == &dtable[T_PRAGMA])
721 /* This pragma allows extra tokens after the file name. */
723 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
727 /* If we are not discarding comments, then gather them while
728 doing the eol check. */
729 *buf = check_eol_return_comments (pfile);
735 /* Handle #include, #include_next and #import. */
737 do_include_common (cpp_reader *pfile, enum include_type type)
741 const cpp_token **buf = NULL;
743 /* Re-enable saving of comments if requested, so that the include
744 callback can dump comments which follow #include. */
745 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
747 fname = parse_include (pfile, &angle_brackets, &buf);
757 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
758 pfile->directive->name);
765 /* Prevent #include recursion. */
766 if (pfile->line_table->depth >= CPP_STACK_MAX)
767 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
770 /* Get out of macro context, if we are. */
771 skip_rest_of_line (pfile);
773 if (pfile->cb.include)
774 pfile->cb.include (pfile, pfile->directive_line,
775 pfile->directive->name, fname, angle_brackets,
778 _cpp_stack_include (pfile, fname, angle_brackets, type);
787 do_include (cpp_reader *pfile)
789 do_include_common (pfile, IT_INCLUDE);
793 do_import (cpp_reader *pfile)
795 do_include_common (pfile, IT_IMPORT);
799 do_include_next (cpp_reader *pfile)
801 enum include_type type = IT_INCLUDE_NEXT;
803 /* If this is the primary source file, warn and use the normal
805 if (cpp_in_primary_file (pfile))
807 cpp_error (pfile, CPP_DL_WARNING,
808 "#include_next in primary source file");
811 do_include_common (pfile, type);
814 /* Subroutine of do_linemarker. Read possible flags after file name.
815 LAST is the last flag seen; 0 if this is the first flag. Return the
816 flag if it is valid, 0 at the end of the directive. Otherwise
819 read_flag (cpp_reader *pfile, unsigned int last)
821 const cpp_token *token = _cpp_lex_token (pfile);
823 if (token->type == CPP_NUMBER && token->val.str.len == 1)
825 unsigned int flag = token->val.str.text[0] - '0';
827 if (flag > last && flag <= 4
828 && (flag != 4 || last == 3)
829 && (flag != 2 || last == 0))
833 if (token->type != CPP_EOF)
834 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
835 cpp_token_as_text (pfile, token));
839 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
840 of length LEN, to binary; store it in NUMP, and return 0 if the
841 number was well-formed, 1 if not. Temporary, hopefully. */
843 strtoul_for_line (const uchar *str, unsigned int len, long unsigned int *nump)
845 unsigned long reg = 0;
859 /* Interpret #line command.
860 Note that the filename string (if any) is a true string constant
861 (escapes are interpreted), unlike in #line. */
863 do_line (cpp_reader *pfile)
865 const struct line_maps *line_table = pfile->line_table;
866 const struct line_map *map = &line_table->maps[line_table->used - 1];
868 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
871 unsigned char map_sysp = map->sysp;
872 const cpp_token *token;
873 const char *new_file = map->to_file;
874 unsigned long new_lineno;
876 /* C99 raised the minimum limit on #line numbers. */
877 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
879 /* #line commands expand macros. */
880 token = cpp_get_token (pfile);
881 if (token->type != CPP_NUMBER
882 || strtoul_for_line (token->val.str.text, token->val.str.len,
885 if (token->type == CPP_EOF)
886 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
888 cpp_error (pfile, CPP_DL_ERROR,
889 "\"%s\" after #line is not a positive integer",
890 cpp_token_as_text (pfile, token));
894 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
895 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
897 token = cpp_get_token (pfile);
898 if (token->type == CPP_STRING)
900 cpp_string s = { 0, 0 };
901 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
903 new_file = (const char *)s.text;
906 else if (token->type != CPP_EOF)
908 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
909 cpp_token_as_text (pfile, token));
913 skip_rest_of_line (pfile);
914 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
918 /* Interpret the # 44 "file" [flags] notation, which has slightly
919 different syntax and semantics from #line: Flags are allowed,
920 and we never complain about the line number being too big. */
922 do_linemarker (cpp_reader *pfile)
924 const struct line_maps *line_table = pfile->line_table;
925 const struct line_map *map = &line_table->maps[line_table->used - 1];
926 const cpp_token *token;
927 const char *new_file = map->to_file;
928 unsigned long new_lineno;
929 unsigned int new_sysp = map->sysp;
930 enum lc_reason reason = LC_RENAME;
933 /* Back up so we can get the number again. Putting this in
934 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
935 some circumstances, which can segfault. */
936 _cpp_backup_tokens (pfile, 1);
938 /* #line commands expand macros. */
939 token = cpp_get_token (pfile);
940 if (token->type != CPP_NUMBER
941 || strtoul_for_line (token->val.str.text, token->val.str.len,
944 /* Unlike #line, there does not seem to be a way to get an EOF
945 here. So, it should be safe to always spell the token. */
946 cpp_error (pfile, CPP_DL_ERROR,
947 "\"%s\" after # is not a positive integer",
948 cpp_token_as_text (pfile, token));
952 token = cpp_get_token (pfile);
953 if (token->type == CPP_STRING)
955 cpp_string s = { 0, 0 };
956 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
958 new_file = (const char *)s.text;
961 flag = read_flag (pfile, 0);
965 /* Fake an include for cpp_included (). */
966 _cpp_fake_include (pfile, new_file);
967 flag = read_flag (pfile, flag);
972 flag = read_flag (pfile, flag);
977 flag = read_flag (pfile, flag);
981 pfile->buffer->sysp = new_sysp;
985 else if (token->type != CPP_EOF)
987 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
988 cpp_token_as_text (pfile, token));
992 skip_rest_of_line (pfile);
993 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
996 /* Arrange the file_change callback. pfile->line has changed to
997 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
998 header, 2 for a system header that needs to be extern "C" protected,
999 and zero otherwise. */
1001 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1002 const char *to_file, unsigned int file_line,
1005 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1006 to_file, file_line);
1008 linemap_line_start (pfile->line_table, map->to_line, 127);
1010 if (pfile->cb.file_change)
1011 pfile->cb.file_change (pfile, map);
1014 /* Report a warning or error detected by the program we are
1015 processing. Use the directive's tokens in the error message. */
1017 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
1019 if (_cpp_begin_message (pfile, code, pfile->cur_token[-1].src_loc, 0))
1022 fprintf (stderr, "#%s ", pfile->directive->name);
1023 pfile->state.prevent_expansion++;
1024 cpp_output_line (pfile, stderr);
1025 pfile->state.prevent_expansion--;
1030 do_error (cpp_reader *pfile)
1032 do_diagnostic (pfile, CPP_DL_ERROR, 1);
1036 do_warning (cpp_reader *pfile)
1038 /* We want #warning diagnostics to be emitted in system headers too. */
1039 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1042 /* Report program identification. */
1044 do_ident (cpp_reader *pfile)
1046 const cpp_token *str = cpp_get_token (pfile);
1048 if (str->type != CPP_STRING)
1049 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1050 pfile->directive->name);
1051 else if (pfile->cb.ident)
1052 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1057 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1058 matching entry, or NULL if none is found. The returned entry could
1059 be the start of a namespace chain, or a pragma. */
1060 static struct pragma_entry *
1061 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1063 while (chain && chain->pragma != pragma)
1064 chain = chain->next;
1069 /* Create and insert a blank pragma entry at the beginning of a
1070 singly-linked CHAIN. */
1071 static struct pragma_entry *
1072 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1074 struct pragma_entry *new_entry;
1076 new_entry = (struct pragma_entry *)
1077 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1079 memset (new_entry, 0, sizeof (struct pragma_entry));
1080 new_entry->next = *chain;
1086 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1087 goes in the global namespace. */
1088 static struct pragma_entry *
1089 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1090 bool allow_name_expansion)
1092 struct pragma_entry **chain = &pfile->pragmas;
1093 struct pragma_entry *entry;
1094 const cpp_hashnode *node;
1098 node = cpp_lookup (pfile, UC space, strlen (space));
1099 entry = lookup_pragma_entry (*chain, node);
1102 entry = new_pragma_entry (pfile, chain);
1103 entry->pragma = node;
1104 entry->is_nspace = true;
1105 entry->allow_expansion = allow_name_expansion;
1107 else if (!entry->is_nspace)
1109 else if (entry->allow_expansion != allow_name_expansion)
1111 cpp_error (pfile, CPP_DL_ICE,
1112 "registering pragmas in namespace \"%s\" with mismatched "
1113 "name expansion", space);
1116 chain = &entry->u.space;
1118 else if (allow_name_expansion)
1120 cpp_error (pfile, CPP_DL_ICE,
1121 "registering pragma \"%s\" with name expansion "
1122 "and no namespace", name);
1126 /* Check for duplicates. */
1127 node = cpp_lookup (pfile, UC name, strlen (name));
1128 entry = lookup_pragma_entry (*chain, node);
1131 entry = new_pragma_entry (pfile, chain);
1132 entry->pragma = node;
1136 if (entry->is_nspace)
1138 cpp_error (pfile, CPP_DL_ICE,
1139 "registering \"%s\" as both a pragma and a pragma namespace",
1142 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1145 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1150 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1152 register_pragma_internal (cpp_reader *pfile, const char *space,
1153 const char *name, pragma_cb handler)
1155 struct pragma_entry *entry;
1157 entry = register_pragma_1 (pfile, space, name, false);
1158 entry->is_internal = true;
1159 entry->u.handler = handler;
1162 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1163 goes in the global namespace. HANDLER is the handler it will call,
1164 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1165 expansion while parsing pragma NAME. This function is exported
1168 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1169 pragma_cb handler, bool allow_expansion)
1171 struct pragma_entry *entry;
1175 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1179 entry = register_pragma_1 (pfile, space, name, false);
1182 entry->allow_expansion = allow_expansion;
1183 entry->u.handler = handler;
1187 /* Similarly, but create mark the pragma for deferred processing.
1188 When found, a CPP_PRAGMA token will be insertted into the stream
1189 with IDENT in the token->u.pragma slot. */
1191 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1192 const char *name, unsigned int ident,
1193 bool allow_expansion, bool allow_name_expansion)
1195 struct pragma_entry *entry;
1197 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1200 entry->is_deferred = true;
1201 entry->allow_expansion = allow_expansion;
1202 entry->u.ident = ident;
1206 /* Register the pragmas the preprocessor itself handles. */
1208 _cpp_init_internal_pragmas (cpp_reader *pfile)
1210 /* Pragmas in the global namespace. */
1211 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1213 /* New GCC-specific pragmas should be put in the GCC namespace. */
1214 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1215 register_pragma_internal (pfile, "GCC", "system_header",
1216 do_pragma_system_header);
1217 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1220 /* Return the number of registered pragmas in PE. */
1223 count_registered_pragmas (struct pragma_entry *pe)
1226 for (; pe != NULL; pe = pe->next)
1229 ct += count_registered_pragmas (pe->u.space);
1235 /* Save into SD the names of the registered pragmas referenced by PE,
1236 and return a pointer to the next free space in SD. */
1239 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1241 for (; pe != NULL; pe = pe->next)
1244 sd = save_registered_pragmas (pe->u.space, sd);
1245 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1246 HT_LEN (&pe->pragma->ident),
1247 HT_LEN (&pe->pragma->ident) + 1);
1252 /* Return a newly-allocated array which saves the names of the
1253 registered pragmas. */
1256 _cpp_save_pragma_names (cpp_reader *pfile)
1258 int ct = count_registered_pragmas (pfile->pragmas);
1259 char **result = XNEWVEC (char *, ct);
1260 (void) save_registered_pragmas (pfile->pragmas, result);
1264 /* Restore from SD the names of the registered pragmas referenced by PE,
1265 and return a pointer to the next unused name in SD. */
1268 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1271 for (; pe != NULL; pe = pe->next)
1274 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1275 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1282 /* Restore the names of the registered pragmas from SAVED. */
1285 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1287 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1291 /* Pragmata handling. We handle some, and pass the rest on to the
1292 front end. C99 defines three pragmas and says that no macro
1293 expansion is to be performed on them; whether or not macro
1294 expansion happens for other pragmas is implementation defined.
1295 This implementation allows for a mix of both, since GCC did not
1296 traditionally macro expand its (few) pragmas, whereas OpenMP
1297 specifies that macro expansion should happen. */
1299 do_pragma (cpp_reader *pfile)
1301 const struct pragma_entry *p = NULL;
1302 const cpp_token *token, *pragma_token = pfile->cur_token;
1304 unsigned int count = 1;
1306 pfile->state.prevent_expansion++;
1308 token = cpp_get_token (pfile);
1310 if (token->type == CPP_NAME)
1312 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1313 if (p && p->is_nspace)
1315 bool allow_name_expansion = p->allow_expansion;
1316 if (allow_name_expansion)
1317 pfile->state.prevent_expansion--;
1318 token = cpp_get_token (pfile);
1319 if (token->type == CPP_NAME)
1320 p = lookup_pragma_entry (p->u.space, token->val.node);
1323 if (allow_name_expansion)
1324 pfile->state.prevent_expansion++;
1333 pfile->directive_result.src_loc = pragma_token->src_loc;
1334 pfile->directive_result.type = CPP_PRAGMA;
1335 pfile->directive_result.flags = pragma_token->flags;
1336 pfile->directive_result.val.pragma = p->u.ident;
1337 pfile->state.in_deferred_pragma = true;
1338 pfile->state.pragma_allow_expansion = p->allow_expansion;
1339 if (!p->allow_expansion)
1340 pfile->state.prevent_expansion++;
1344 /* Since the handler below doesn't get the line number, that
1345 it might need for diagnostics, make sure it has the right
1346 numbers in place. */
1347 if (pfile->cb.line_change)
1348 (*pfile->cb.line_change) (pfile, pragma_token, false);
1349 if (p->allow_expansion)
1350 pfile->state.prevent_expansion--;
1351 (*p->u.handler) (pfile);
1352 if (p->allow_expansion)
1353 pfile->state.prevent_expansion++;
1356 else if (pfile->cb.def_pragma)
1358 if (count == 1 || pfile->context->prev == NULL)
1359 _cpp_backup_tokens (pfile, count);
1362 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1363 won't allow backing 2 tokens. */
1364 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1365 reads both tokens, we could perhaps free it, but if it doesn't,
1366 we don't know the exact lifespan. */
1367 cpp_token *toks = XNEWVEC (cpp_token, 2);
1369 toks[0].flags |= NO_EXPAND;
1371 toks[1].flags |= NO_EXPAND;
1372 _cpp_push_token_context (pfile, NULL, toks, 2);
1374 pfile->cb.def_pragma (pfile, pfile->directive_line);
1377 pfile->state.prevent_expansion--;
1380 /* Handle #pragma once. */
1382 do_pragma_once (cpp_reader *pfile)
1384 if (cpp_in_primary_file (pfile))
1385 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1388 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1391 /* Handle #pragma GCC poison, to poison one or more identifiers so
1392 that the lexer produces a hard error for each subsequent usage. */
1394 do_pragma_poison (cpp_reader *pfile)
1396 const cpp_token *tok;
1399 pfile->state.poisoned_ok = 1;
1402 tok = _cpp_lex_token (pfile);
1403 if (tok->type == CPP_EOF)
1405 if (tok->type != CPP_NAME)
1407 cpp_error (pfile, CPP_DL_ERROR,
1408 "invalid #pragma GCC poison directive");
1413 if (hp->flags & NODE_POISONED)
1416 if (hp->type == NT_MACRO)
1417 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1419 _cpp_free_definition (hp);
1420 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1422 pfile->state.poisoned_ok = 0;
1425 /* Mark the current header as a system header. This will suppress
1426 some categories of warnings (notably those from -pedantic). It is
1427 intended for use in system libraries that cannot be implemented in
1428 conforming C, but cannot be certain that their headers appear in a
1429 system include directory. To prevent abuse, it is rejected in the
1430 primary source file. */
1432 do_pragma_system_header (cpp_reader *pfile)
1434 if (cpp_in_primary_file (pfile))
1435 cpp_error (pfile, CPP_DL_WARNING,
1436 "#pragma system_header ignored outside include file");
1440 skip_rest_of_line (pfile);
1441 cpp_make_system_header (pfile, 1, 0);
1445 /* Check the modified date of the current include file against a specified
1446 file. Issue a diagnostic, if the specified file is newer. We use this to
1447 determine if a fixed header should be refixed. */
1449 do_pragma_dependency (cpp_reader *pfile)
1452 int angle_brackets, ordering;
1454 fname = parse_include (pfile, &angle_brackets, NULL);
1458 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1460 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1461 else if (ordering > 0)
1463 cpp_error (pfile, CPP_DL_WARNING,
1464 "current file is older than %s", fname);
1465 if (cpp_get_token (pfile)->type != CPP_EOF)
1467 _cpp_backup_tokens (pfile, 1);
1468 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1472 free ((void *) fname);
1475 /* Get a token but skip padding. */
1476 static const cpp_token *
1477 get_token_no_padding (cpp_reader *pfile)
1481 const cpp_token *result = cpp_get_token (pfile);
1482 if (result->type != CPP_PADDING)
1487 /* Check syntax is "(string-literal)". Returns the string on success,
1488 or NULL on failure. */
1489 static const cpp_token *
1490 get__Pragma_string (cpp_reader *pfile)
1492 const cpp_token *string;
1493 const cpp_token *paren;
1495 paren = get_token_no_padding (pfile);
1496 if (paren->type == CPP_EOF)
1497 _cpp_backup_tokens (pfile, 1);
1498 if (paren->type != CPP_OPEN_PAREN)
1501 string = get_token_no_padding (pfile);
1502 if (string->type == CPP_EOF)
1503 _cpp_backup_tokens (pfile, 1);
1504 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1505 && string->type != CPP_STRING32 && string->type != CPP_STRING16)
1508 paren = get_token_no_padding (pfile);
1509 if (paren->type == CPP_EOF)
1510 _cpp_backup_tokens (pfile, 1);
1511 if (paren->type != CPP_CLOSE_PAREN)
1517 /* Destringize IN into a temporary buffer, by removing the first \ of
1518 \" and \\ sequences, and process the result as a #pragma directive. */
1520 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1522 const unsigned char *src, *limit;
1523 char *dest, *result;
1524 cpp_context *saved_context;
1525 cpp_token *saved_cur_token;
1526 tokenrun *saved_cur_run;
1529 const struct directive *save_directive;
1531 dest = result = (char *) alloca (in->len - 1);
1532 src = in->text + 1 + (in->text[0] == 'L');
1533 limit = in->text + in->len - 1;
1536 /* We know there is a character following the backslash. */
1537 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1543 /* Ugh; an awful kludge. We are really not set up to be lexing
1544 tokens when in the middle of a macro expansion. Use a new
1545 context to force cpp_get_token to lex, and so skip_rest_of_line
1546 doesn't go beyond the end of the text. Also, remember the
1547 current lexing position so we can return to it later.
1549 Something like line-at-a-time lexing should remove the need for
1551 saved_context = pfile->context;
1552 saved_cur_token = pfile->cur_token;
1553 saved_cur_run = pfile->cur_run;
1555 pfile->context = XNEW (cpp_context);
1556 pfile->context->macro = 0;
1557 pfile->context->prev = 0;
1558 pfile->context->next = 0;
1560 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1561 until we've read all of the tokens that we want. */
1562 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1563 /* from_stage3 */ true);
1564 /* ??? Antique Disgusting Hack. What does this do? */
1565 if (pfile->buffer->prev)
1566 pfile->buffer->file = pfile->buffer->prev->file;
1568 start_directive (pfile);
1569 _cpp_clean_line (pfile);
1570 save_directive = pfile->directive;
1571 pfile->directive = &dtable[T_PRAGMA];
1573 end_directive (pfile, 1);
1574 pfile->directive = save_directive;
1576 /* We always insert at least one token, the directive result. It'll
1577 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1578 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1580 /* If we're not handling the pragma internally, read all of the tokens from
1581 the string buffer now, while the string buffer is still installed. */
1582 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1583 to me what the true lifespan of the tokens are. It would appear that
1584 the lifespan is the entire parse of the main input stream, in which case
1585 this may not be wrong. */
1586 if (pfile->directive_result.type == CPP_PRAGMA)
1592 toks = XNEWVEC (cpp_token, maxcount);
1593 toks[0] = pfile->directive_result;
1597 if (count == maxcount)
1599 maxcount = maxcount * 3 / 2;
1600 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1602 toks[count] = *cpp_get_token (pfile);
1603 /* Macros have been already expanded by cpp_get_token
1604 if the pragma allowed expansion. */
1605 toks[count++].flags |= NO_EXPAND;
1607 while (toks[count-1].type != CPP_PRAGMA_EOL);
1612 toks = XNEW (cpp_token);
1613 toks[0] = pfile->directive_result;
1615 /* If we handled the entire pragma internally, make sure we get the
1616 line number correct for the next token. */
1617 if (pfile->cb.line_change)
1618 pfile->cb.line_change (pfile, pfile->cur_token, false);
1621 /* Finish inlining run_directive. */
1622 pfile->buffer->file = NULL;
1623 _cpp_pop_buffer (pfile);
1625 /* Reset the old macro state before ... */
1626 XDELETE (pfile->context);
1627 pfile->context = saved_context;
1628 pfile->cur_token = saved_cur_token;
1629 pfile->cur_run = saved_cur_run;
1631 /* ... inserting the new tokens we collected. */
1632 _cpp_push_token_context (pfile, NULL, toks, count);
1635 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1637 _cpp_do__Pragma (cpp_reader *pfile)
1639 const cpp_token *string = get__Pragma_string (pfile);
1640 pfile->directive_result.type = CPP_PADDING;
1644 destringize_and_run (pfile, &string->val.str);
1647 cpp_error (pfile, CPP_DL_ERROR,
1648 "_Pragma takes a parenthesized string literal");
1652 /* Handle #ifdef. */
1654 do_ifdef (cpp_reader *pfile)
1658 if (! pfile->state.skipping)
1660 cpp_hashnode *node = lex_macro_node (pfile, false);
1664 skip = node->type != NT_MACRO;
1665 _cpp_mark_macro_used (node);
1666 if (!(node->flags & NODE_USED))
1668 node->flags |= NODE_USED;
1669 if (node->type == NT_MACRO)
1671 if (pfile->cb.used_define)
1672 pfile->cb.used_define (pfile, pfile->directive_line, node);
1676 if (pfile->cb.used_undef)
1677 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1684 push_conditional (pfile, skip, T_IFDEF, 0);
1687 /* Handle #ifndef. */
1689 do_ifndef (cpp_reader *pfile)
1692 cpp_hashnode *node = 0;
1694 if (! pfile->state.skipping)
1696 node = lex_macro_node (pfile, false);
1700 skip = node->type == NT_MACRO;
1701 _cpp_mark_macro_used (node);
1702 if (!(node->flags & NODE_USED))
1704 node->flags |= NODE_USED;
1705 if (node->type == NT_MACRO)
1707 if (pfile->cb.used_define)
1708 pfile->cb.used_define (pfile, pfile->directive_line, node);
1712 if (pfile->cb.used_undef)
1713 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1720 push_conditional (pfile, skip, T_IFNDEF, node);
1723 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1724 pfile->mi_ind_cmacro so we can handle multiple-include
1725 optimizations. If macro expansion occurs in the expression, we
1726 cannot treat it as a controlling conditional, since the expansion
1727 could change in the future. That is handled by cpp_get_token. */
1729 do_if (cpp_reader *pfile)
1733 if (! pfile->state.skipping)
1734 skip = _cpp_parse_expr (pfile) == false;
1736 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1739 /* Flip skipping state if appropriate and continue without changing
1740 if_stack; this is so that the error message for missing #endif's
1741 etc. will point to the original #if. */
1743 do_else (cpp_reader *pfile)
1745 cpp_buffer *buffer = pfile->buffer;
1746 struct if_stack *ifs = buffer->if_stack;
1749 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1752 if (ifs->type == T_ELSE)
1754 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1755 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1756 "the conditional began here");
1760 /* Skip any future (erroneous) #elses or #elifs. */
1761 pfile->state.skipping = ifs->skip_elses;
1762 ifs->skip_elses = true;
1764 /* Invalidate any controlling macro. */
1767 /* Only check EOL if was not originally skipping. */
1768 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1773 /* Handle a #elif directive by not changing if_stack either. See the
1774 comment above do_else. */
1776 do_elif (cpp_reader *pfile)
1778 cpp_buffer *buffer = pfile->buffer;
1779 struct if_stack *ifs = buffer->if_stack;
1782 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1785 if (ifs->type == T_ELSE)
1787 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1788 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1789 "the conditional began here");
1793 /* Only evaluate this if we aren't skipping elses. During
1794 evaluation, set skipping to false to get lexer warnings. */
1795 if (ifs->skip_elses)
1796 pfile->state.skipping = 1;
1799 pfile->state.skipping = 0;
1800 pfile->state.skipping = ! _cpp_parse_expr (pfile);
1801 ifs->skip_elses = ! pfile->state.skipping;
1804 /* Invalidate any controlling macro. */
1809 /* #endif pops the if stack and resets pfile->state.skipping. */
1811 do_endif (cpp_reader *pfile)
1813 cpp_buffer *buffer = pfile->buffer;
1814 struct if_stack *ifs = buffer->if_stack;
1817 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1820 /* Only check EOL if was not originally skipping. */
1821 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1824 /* If potential control macro, we go back outside again. */
1825 if (ifs->next == 0 && ifs->mi_cmacro)
1827 pfile->mi_valid = true;
1828 pfile->mi_cmacro = ifs->mi_cmacro;
1831 buffer->if_stack = ifs->next;
1832 pfile->state.skipping = ifs->was_skipping;
1833 obstack_free (&pfile->buffer_ob, ifs);
1837 /* Push an if_stack entry for a preprocessor conditional, and set
1838 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1839 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1840 we need to check here that we are at the top of the file. */
1842 push_conditional (cpp_reader *pfile, int skip, int type,
1843 const cpp_hashnode *cmacro)
1845 struct if_stack *ifs;
1846 cpp_buffer *buffer = pfile->buffer;
1848 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1849 ifs->line = pfile->directive_line;
1850 ifs->next = buffer->if_stack;
1851 ifs->skip_elses = pfile->state.skipping || !skip;
1852 ifs->was_skipping = pfile->state.skipping;
1854 /* This condition is effectively a test for top-of-file. */
1855 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1856 ifs->mi_cmacro = cmacro;
1860 pfile->state.skipping = skip;
1861 buffer->if_stack = ifs;
1864 /* Read the tokens of the answer into the macro pool, in a directive
1865 of type TYPE. Only commit the memory if we intend it as permanent
1866 storage, i.e. the #assert case. Returns 0 on success, and sets
1867 ANSWERP to point to the answer. */
1869 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1871 const cpp_token *paren;
1872 struct answer *answer;
1873 unsigned int acount;
1875 /* In a conditional, it is legal to not have an open paren. We
1876 should save the following token in this case. */
1877 paren = cpp_get_token (pfile);
1879 /* If not a paren, see if we're OK. */
1880 if (paren->type != CPP_OPEN_PAREN)
1882 /* In a conditional no answer is a test for any answer. It
1883 could be followed by any token. */
1886 _cpp_backup_tokens (pfile, 1);
1890 /* #unassert with no answer is valid - it removes all answers. */
1891 if (type == T_UNASSERT && paren->type == CPP_EOF)
1894 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
1898 for (acount = 0;; acount++)
1901 const cpp_token *token = cpp_get_token (pfile);
1904 if (token->type == CPP_CLOSE_PAREN)
1907 if (token->type == CPP_EOF)
1909 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
1913 /* struct answer includes the space for one token. */
1914 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
1916 if (BUFF_ROOM (pfile->a_buff) < room_needed)
1917 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
1919 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
1922 /* Drop whitespace at start, for answer equivalence purposes. */
1924 dest->flags &= ~PREV_WHITE;
1929 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
1933 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
1934 answer->count = acount;
1935 answer->next = NULL;
1941 /* Parses an assertion directive of type TYPE, returning a pointer to
1942 the hash node of the predicate, or 0 on error. If an answer was
1943 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
1944 static cpp_hashnode *
1945 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
1947 cpp_hashnode *result = 0;
1948 const cpp_token *predicate;
1950 /* We don't expand predicates or answers. */
1951 pfile->state.prevent_expansion++;
1954 predicate = cpp_get_token (pfile);
1955 if (predicate->type == CPP_EOF)
1956 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
1957 else if (predicate->type != CPP_NAME)
1958 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
1959 else if (parse_answer (pfile, answerp, type) == 0)
1961 unsigned int len = NODE_LEN (predicate->val.node);
1962 unsigned char *sym = (unsigned char *) alloca (len + 1);
1964 /* Prefix '#' to get it out of macro namespace. */
1966 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
1967 result = cpp_lookup (pfile, sym, len + 1);
1970 pfile->state.prevent_expansion--;
1974 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
1975 or a pointer to NULL if the answer is not in the chain. */
1976 static struct answer **
1977 find_answer (cpp_hashnode *node, const struct answer *candidate)
1980 struct answer **result;
1982 for (result = &node->value.answers; *result; result = &(*result)->next)
1984 struct answer *answer = *result;
1986 if (answer->count == candidate->count)
1988 for (i = 0; i < answer->count; i++)
1989 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1992 if (i == answer->count)
2000 /* Test an assertion within a preprocessor conditional. Returns
2001 nonzero on failure, zero on success. On success, the result of
2002 the test is written into VALUE, otherwise the value 0. */
2004 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2006 struct answer *answer;
2009 node = parse_assertion (pfile, &answer, T_IF);
2011 /* For recovery, an erroneous assertion expression is handled as a
2012 failing assertion. */
2016 *value = (node->type == NT_ASSERTION &&
2017 (answer == 0 || *find_answer (node, answer) != 0));
2018 else if (pfile->cur_token[-1].type == CPP_EOF)
2019 _cpp_backup_tokens (pfile, 1);
2021 /* We don't commit the memory for the answer - it's temporary only. */
2025 /* Handle #assert. */
2027 do_assert (cpp_reader *pfile)
2029 struct answer *new_answer;
2032 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2037 /* Place the new answer in the answer list. First check there
2038 is not a duplicate. */
2039 new_answer->next = 0;
2040 if (node->type == NT_ASSERTION)
2042 if (*find_answer (node, new_answer))
2044 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2045 NODE_NAME (node) + 1);
2048 new_answer->next = node->value.answers;
2051 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2052 * sizeof (cpp_token));
2053 /* Commit or allocate storage for the object. */
2054 if (pfile->hash_table->alloc_subobject)
2056 struct answer *temp_answer = new_answer;
2057 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2059 memcpy (new_answer, temp_answer, answer_size);
2062 BUFF_FRONT (pfile->a_buff) += answer_size;
2064 node->type = NT_ASSERTION;
2065 node->value.answers = new_answer;
2070 /* Handle #unassert. */
2072 do_unassert (cpp_reader *pfile)
2075 struct answer *answer;
2077 node = parse_assertion (pfile, &answer, T_UNASSERT);
2078 /* It isn't an error to #unassert something that isn't asserted. */
2079 if (node && node->type == NT_ASSERTION)
2083 struct answer **p = find_answer (node, answer), *temp;
2085 /* Remove the answer from the list. */
2090 /* Did we free the last answer? */
2091 if (node->value.answers == 0)
2092 node->type = NT_VOID;
2097 _cpp_free_definition (node);
2100 /* We don't commit the memory for the answer - it's temporary only. */
2103 /* These are for -D, -U, -A. */
2105 /* Process the string STR as if it appeared as the body of a #define.
2106 If STR is just an identifier, define it with value 1.
2107 If STR has anything after the identifier, then it should
2108 be identifier=definition. */
2110 cpp_define (cpp_reader *pfile, const char *str)
2115 /* Copy the entire option so we can modify it.
2116 Change the first "=" in the string to a space. If there is none,
2117 tack " 1" on the end. */
2119 count = strlen (str);
2120 buf = (char *) alloca (count + 3);
2121 memcpy (buf, str, count);
2123 p = strchr (str, '=');
2133 run_directive (pfile, T_DEFINE, buf, count);
2137 /* Use to build macros to be run through cpp_define() as
2139 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2142 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2148 vasprintf (&ptr, fmt, ap);
2151 cpp_define (pfile, ptr);
2156 /* Slight variant of the above for use by initialize_builtins. */
2158 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2160 size_t len = strlen (str);
2161 char *buf = (char *) alloca (len + 1);
2162 memcpy (buf, str, len);
2164 run_directive (pfile, T_DEFINE, buf, len);
2167 /* Process MACRO as if it appeared as the body of an #undef. */
2169 cpp_undef (cpp_reader *pfile, const char *macro)
2171 size_t len = strlen (macro);
2172 char *buf = (char *) alloca (len + 1);
2173 memcpy (buf, macro, len);
2175 run_directive (pfile, T_UNDEF, buf, len);
2178 /* Like lex_macro_node, but read the input from STR. */
2179 static cpp_hashnode *
2180 lex_macro_node_from_str (cpp_reader *pfile, const char *str)
2182 size_t len = strlen (str);
2183 uchar *buf = (uchar *) alloca (len + 1);
2186 memcpy (buf, str, len);
2188 cpp_push_buffer (pfile, buf, len, true);
2189 node = lex_macro_node (pfile, true);
2190 _cpp_pop_buffer (pfile);
2195 /* If STR is a defined macro, return its definition node, else return NULL. */
2197 cpp_push_definition (cpp_reader *pfile, const char *str)
2199 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2200 if (node && node->type == NT_MACRO)
2201 return node->value.macro;
2206 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2207 then the macro should be undefined. */
2209 cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2211 cpp_hashnode *node = lex_macro_node_from_str (pfile, str);
2215 if (pfile->cb.before_define)
2216 pfile->cb.before_define (pfile);
2218 if (node->type == NT_MACRO)
2220 if (pfile->cb.undef)
2221 pfile->cb.undef (pfile, pfile->directive_line, node);
2222 if (CPP_OPTION (pfile, warn_unused_macros))
2223 _cpp_warn_if_unused_macro (pfile, node, NULL);
2225 if (node->type != NT_VOID)
2226 _cpp_free_definition (node);
2230 node->type = NT_MACRO;
2231 node->value.macro = dfn;
2232 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2233 node->flags |= NODE_WARN;
2235 if (pfile->cb.define)
2236 pfile->cb.define (pfile, pfile->directive_line, node);
2240 /* Process the string STR as if it appeared as the body of a #assert. */
2242 cpp_assert (cpp_reader *pfile, const char *str)
2244 handle_assertion (pfile, str, T_ASSERT);
2247 /* Process STR as if it appeared as the body of an #unassert. */
2249 cpp_unassert (cpp_reader *pfile, const char *str)
2251 handle_assertion (pfile, str, T_UNASSERT);
2254 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2256 handle_assertion (cpp_reader *pfile, const char *str, int type)
2258 size_t count = strlen (str);
2259 const char *p = strchr (str, '=');
2261 /* Copy the entire option so we can modify it. Change the first
2262 "=" in the string to a '(', and tack a ')' on the end. */
2263 char *buf = (char *) alloca (count + 2);
2265 memcpy (buf, str, count);
2274 run_directive (pfile, type, str, count);
2277 /* The number of errors for a given reader. */
2279 cpp_errors (cpp_reader *pfile)
2281 return pfile->errors;
2284 /* The options structure. */
2286 cpp_get_options (cpp_reader *pfile)
2288 return &pfile->opts;
2291 /* The callbacks structure. */
2293 cpp_get_callbacks (cpp_reader *pfile)
2298 /* Copy the given callbacks structure to our own. */
2300 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2305 /* The dependencies structure. (Creates one if it hasn't already been.) */
2307 cpp_get_deps (cpp_reader *pfile)
2310 pfile->deps = deps_init ();
2314 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2315 doesn't fail. It does not generate a file change call back; that
2316 is the responsibility of the caller. */
2318 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2321 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2323 /* Clears, amongst other things, if_stack and mi_cmacro. */
2324 memset (new_buffer, 0, sizeof (cpp_buffer));
2326 new_buffer->next_line = new_buffer->buf = buffer;
2327 new_buffer->rlimit = buffer + len;
2328 new_buffer->from_stage3 = from_stage3;
2329 new_buffer->prev = pfile->buffer;
2330 new_buffer->need_line = true;
2332 pfile->buffer = new_buffer;
2337 /* Pops a single buffer, with a file change call-back if appropriate.
2338 Then pushes the next -include file, if any remain. */
2340 _cpp_pop_buffer (cpp_reader *pfile)
2342 cpp_buffer *buffer = pfile->buffer;
2343 struct _cpp_file *inc = buffer->file;
2344 struct if_stack *ifs;
2346 /* Walk back up the conditional stack till we reach its level at
2347 entry to this file, issuing error messages. */
2348 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2349 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2350 "unterminated #%s", dtable[ifs->type].name);
2352 /* In case of a missing #endif. */
2353 pfile->state.skipping = 0;
2355 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2356 pfile->buffer = buffer->prev;
2358 free (buffer->notes);
2360 /* Free the buffer object now; we may want to push a new buffer
2361 in _cpp_push_next_include_file. */
2362 obstack_free (&pfile->buffer_ob, buffer);
2366 _cpp_pop_file_buffer (pfile, inc);
2368 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2372 /* Enter all recognized directives in the hash table. */
2374 _cpp_init_directives (cpp_reader *pfile)
2379 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2381 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2382 node->is_directive = 1;
2383 node->directive_index = i;