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, 2009 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 3, 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; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
34 struct if_stack *next;
35 linenum_type 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 bool strtolinenum (const uchar *, size_t, linenum_type *, bool *);
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);
128 static void do_pragma_push_macro (cpp_reader *);
129 static void do_pragma_pop_macro (cpp_reader *);
131 /* This is the table of directive handlers. It is ordered by
132 frequency of occurrence; the numbers at the end are directive
133 counts from all the source code I have lying around (egcs and libc
134 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
135 pcmcia-cs-3.0.9). This is no longer important as directive lookup
136 is now O(1). All extensions other than #warning, #include_next,
137 and #import are deprecated. The name is where the extension
138 appears to have come from. */
140 #define DIRECTIVE_TABLE \
141 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
142 D(include, T_INCLUDE, KANDR, INCL | EXPAND) /* 52262 */ \
143 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
144 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
145 D(if, T_IF, KANDR, COND | IF_COND | EXPAND) /* 18162 */ \
146 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
147 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
148 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
149 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
150 D(elif, T_ELIF, STDC89, COND | EXPAND) /* 610 */ \
151 D(error, T_ERROR, STDC89, 0) /* 475 */ \
152 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
153 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
154 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL | EXPAND) /* 19 */ \
155 D(ident, T_IDENT, EXTENSION, IN_I | DEPRECATED) /* 11 */ \
156 D(import, T_IMPORT, EXTENSION, INCL | EXPAND) /* 0 ObjC */ \
157 D(assert, T_ASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
158 D(unassert, T_UNASSERT, EXTENSION, DEPRECATED) /* 0 SVR4 */ \
159 D(sccs, T_SCCS, EXTENSION, IN_I | DEPRECATED) /* 0 SVR4? */
161 /* #sccs is synonymous with #ident. */
162 #define do_sccs do_ident
164 /* Use the table to generate a series of prototypes, an enum for the
165 directive names, and an array of directive handlers. */
167 #define D(name, t, o, f) static void do_##name (cpp_reader *);
171 #define D(n, tag, o, f) tag,
179 #define D(name, t, origin, flags) \
180 { do_##name, (const uchar *) #name, \
181 sizeof #name - 1, origin, flags },
182 static const directive dtable[] =
187 #undef DIRECTIVE_TABLE
189 /* Wrapper struct directive for linemarkers.
190 The origin is more or less true - the original K+R cpp
191 did use this notation in its preprocessed output. */
192 static const directive linemarker_dir =
194 do_linemarker, UC"#", 1, KANDR, IN_I
197 #define SEEN_EOL() (pfile->cur_token[-1].type == CPP_EOF)
199 /* Skip any remaining tokens in a directive. */
201 skip_rest_of_line (cpp_reader *pfile)
203 /* Discard all stacked contexts. */
204 while (pfile->context->prev)
205 _cpp_pop_context (pfile);
207 /* Sweep up all tokens remaining on the line. */
209 while (_cpp_lex_token (pfile)->type != CPP_EOF)
213 /* Ensure there are no stray tokens at the end of a directive. */
215 check_eol (cpp_reader *pfile)
217 if (! SEEN_EOL () && _cpp_lex_token (pfile)->type != CPP_EOF)
218 cpp_error (pfile, CPP_DL_PEDWARN, "extra tokens at end of #%s directive",
219 pfile->directive->name);
222 /* Ensure there are no stray tokens other than comments at the end of
223 a directive, and gather the comments. */
224 static const cpp_token **
225 check_eol_return_comments (cpp_reader *pfile)
229 const cpp_token **buf;
231 buf = XNEWVEC (const cpp_token *, capacity);
237 const cpp_token *tok;
239 tok = _cpp_lex_token (pfile);
240 if (tok->type == CPP_EOF)
242 if (tok->type != CPP_COMMENT)
243 cpp_error (pfile, CPP_DL_PEDWARN,
244 "extra tokens at end of #%s directive",
245 pfile->directive->name);
248 if (c + 1 >= capacity)
251 buf = XRESIZEVEC (const cpp_token *, buf, capacity);
262 /* Called when entering a directive, _Pragma or command-line directive. */
264 start_directive (cpp_reader *pfile)
266 /* Setup in-directive state. */
267 pfile->state.in_directive = 1;
268 pfile->state.save_comments = 0;
269 pfile->directive_result.type = CPP_PADDING;
271 /* Some handlers need the position of the # for diagnostics. */
272 pfile->directive_line = pfile->line_table->highest_line;
275 /* Called when leaving a directive, _Pragma or command-line directive. */
277 end_directive (cpp_reader *pfile, int skip_line)
279 if (pfile->state.in_deferred_pragma)
281 else if (CPP_OPTION (pfile, traditional))
283 /* Revert change of prepare_directive_trad. */
284 pfile->state.prevent_expansion--;
286 if (pfile->directive != &dtable[T_DEFINE])
287 _cpp_remove_overlay (pfile);
289 /* We don't skip for an assembler #. */
292 skip_rest_of_line (pfile);
293 if (!pfile->keep_tokens)
295 pfile->cur_run = &pfile->base_run;
296 pfile->cur_token = pfile->base_run.base;
301 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
302 pfile->state.in_directive = 0;
303 pfile->state.in_expression = 0;
304 pfile->state.angled_headers = 0;
305 pfile->directive = 0;
308 /* Prepare to handle the directive in pfile->directive. */
310 prepare_directive_trad (cpp_reader *pfile)
312 if (pfile->directive != &dtable[T_DEFINE])
314 bool no_expand = (pfile->directive
315 && ! (pfile->directive->flags & EXPAND));
316 bool was_skipping = pfile->state.skipping;
318 pfile->state.in_expression = (pfile->directive == &dtable[T_IF]
319 || pfile->directive == &dtable[T_ELIF]);
320 if (pfile->state.in_expression)
321 pfile->state.skipping = false;
324 pfile->state.prevent_expansion++;
325 _cpp_scan_out_logical_line (pfile, NULL);
327 pfile->state.prevent_expansion--;
329 pfile->state.skipping = was_skipping;
330 _cpp_overlay_buffer (pfile, pfile->out.base,
331 pfile->out.cur - pfile->out.base);
334 /* Stop ISO C from expanding anything. */
335 pfile->state.prevent_expansion++;
338 /* Output diagnostics for a directive DIR. INDENTED is nonzero if
339 the '#' was indented. */
341 directive_diagnostics (cpp_reader *pfile, const directive *dir, int indented)
343 /* Issue -pedantic or deprecated warnings for extensions. We let
344 -pedantic take precedence if both are applicable. */
345 if (! pfile->state.skipping)
347 if (dir->origin == EXTENSION
348 && !(dir == &dtable[T_IMPORT] && CPP_OPTION (pfile, objc))
349 && CPP_PEDANTIC (pfile))
350 cpp_error (pfile, CPP_DL_PEDWARN, "#%s is a GCC extension", dir->name);
351 else if (((dir->flags & DEPRECATED) != 0
352 || (dir == &dtable[T_IMPORT] && !CPP_OPTION (pfile, objc)))
353 && CPP_OPTION (pfile, warn_deprecated))
354 cpp_error (pfile, CPP_DL_WARNING, "#%s is a deprecated GCC extension",
358 /* Traditionally, a directive is ignored unless its # is in
359 column 1. Therefore in code intended to work with K+R
360 compilers, directives added by C89 must have their #
361 indented, and directives present in traditional C must not.
362 This is true even of directives in skipped conditional
363 blocks. #elif cannot be used at all. */
364 if (CPP_WTRADITIONAL (pfile))
366 if (dir == &dtable[T_ELIF])
367 cpp_error (pfile, CPP_DL_WARNING,
368 "suggest not using #elif in traditional C");
369 else if (indented && dir->origin == KANDR)
370 cpp_error (pfile, CPP_DL_WARNING,
371 "traditional C ignores #%s with the # indented",
373 else if (!indented && dir->origin != KANDR)
374 cpp_error (pfile, CPP_DL_WARNING,
375 "suggest hiding #%s from traditional C with an indented #",
380 /* Check if we have a known directive. INDENTED is nonzero if the
381 '#' of the directive was indented. This function is in this file
382 to save unnecessarily exporting dtable etc. to lex.c. Returns
383 nonzero if the line of tokens has been handled, zero if we should
384 continue processing the line. */
386 _cpp_handle_directive (cpp_reader *pfile, int indented)
388 const directive *dir = 0;
389 const cpp_token *dname;
390 bool was_parsing_args = pfile->state.parsing_args;
391 bool was_discarding_output = pfile->state.discarding_output;
394 if (was_discarding_output)
395 pfile->state.prevent_expansion = 0;
397 if (was_parsing_args)
399 if (CPP_OPTION (pfile, pedantic))
400 cpp_error (pfile, CPP_DL_PEDWARN,
401 "embedding a directive within macro arguments is not portable");
402 pfile->state.parsing_args = 0;
403 pfile->state.prevent_expansion = 0;
405 start_directive (pfile);
406 dname = _cpp_lex_token (pfile);
408 if (dname->type == CPP_NAME)
410 if (dname->val.node->is_directive)
411 dir = &dtable[dname->val.node->directive_index];
413 /* We do not recognize the # followed by a number extension in
415 else if (dname->type == CPP_NUMBER && CPP_OPTION (pfile, lang) != CLK_ASM)
417 dir = &linemarker_dir;
418 if (CPP_PEDANTIC (pfile) && ! CPP_OPTION (pfile, preprocessed)
419 && ! pfile->state.skipping)
420 cpp_error (pfile, CPP_DL_PEDWARN,
421 "style of line directive is a GCC extension");
426 /* If we have a directive that is not an opening conditional,
427 invalidate any control macro. */
428 if (! (dir->flags & IF_COND))
429 pfile->mi_valid = false;
431 /* Kluge alert. In order to be sure that code like this
436 does not cause '#define foo bar' to get executed when
437 compiled with -save-temps, we recognize directives in
438 -fpreprocessed mode only if the # is in column 1. macro.c
439 puts a space in front of any '#' at the start of a macro.
441 We exclude the -fdirectives-only case because macro expansion
442 has not been performed yet, and block comments can cause spaces
443 to preceed the directive. */
444 if (CPP_OPTION (pfile, preprocessed)
445 && !CPP_OPTION (pfile, directives_only)
446 && (indented || !(dir->flags & IN_I)))
453 /* In failed conditional groups, all non-conditional
454 directives are ignored. Before doing that, whether
455 skipping or not, we should lex angle-bracketed headers
456 correctly, and maybe output some diagnostics. */
457 pfile->state.angled_headers = dir->flags & INCL;
458 pfile->state.directive_wants_padding = dir->flags & INCL;
459 if (! CPP_OPTION (pfile, preprocessed))
460 directive_diagnostics (pfile, dir, indented);
461 if (pfile->state.skipping && !(dir->flags & COND))
465 else if (dname->type == CPP_EOF)
466 ; /* CPP_EOF is the "null directive". */
469 /* An unknown directive. Don't complain about it in assembly
470 source: we don't know where the comments are, and # may
471 introduce assembler pseudo-ops. Don't complain about invalid
472 directives in skipped conditional groups (6.10 p4). */
473 if (CPP_OPTION (pfile, lang) == CLK_ASM)
475 else if (!pfile->state.skipping)
476 cpp_error (pfile, CPP_DL_ERROR, "invalid preprocessing directive #%s",
477 cpp_token_as_text (pfile, dname));
480 pfile->directive = dir;
481 if (CPP_OPTION (pfile, traditional))
482 prepare_directive_trad (pfile);
485 pfile->directive->handler (pfile);
487 _cpp_backup_tokens (pfile, 1);
489 end_directive (pfile, skip);
490 if (was_parsing_args && !pfile->state.in_deferred_pragma)
492 /* Restore state when within macro args. */
493 pfile->state.parsing_args = 2;
494 pfile->state.prevent_expansion = 1;
496 if (was_discarding_output)
497 pfile->state.prevent_expansion = 1;
501 /* Directive handler wrapper used by the command line option
502 processor. BUF is \n terminated. */
504 run_directive (cpp_reader *pfile, int dir_no, const char *buf, size_t count)
506 cpp_push_buffer (pfile, (const uchar *) buf, count,
507 /* from_stage3 */ true);
508 start_directive (pfile);
510 /* This is a short-term fix to prevent a leading '#' being
511 interpreted as a directive. */
512 _cpp_clean_line (pfile);
514 pfile->directive = &dtable[dir_no];
515 if (CPP_OPTION (pfile, traditional))
516 prepare_directive_trad (pfile);
517 pfile->directive->handler (pfile);
518 end_directive (pfile, 1);
519 _cpp_pop_buffer (pfile);
522 /* Checks for validity the macro name in #define, #undef, #ifdef and
523 #ifndef directives. IS_DEF_OR_UNDEF is true if this call is
524 processing a #define or #undefine directive, and false
526 static cpp_hashnode *
527 lex_macro_node (cpp_reader *pfile, bool is_def_or_undef)
529 const cpp_token *token = _cpp_lex_token (pfile);
531 /* The token immediately after #define must be an identifier. That
532 identifier may not be "defined", per C99 6.10.8p4.
533 In C++, it may not be any of the "named operators" either,
534 per C++98 [lex.digraph], [lex.key].
535 Finally, the identifier may not have been poisoned. (In that case
536 the lexer has issued the error message for us.) */
538 if (token->type == CPP_NAME)
540 cpp_hashnode *node = token->val.node;
542 if (is_def_or_undef && node == pfile->spec_nodes.n_defined)
543 cpp_error (pfile, CPP_DL_ERROR,
544 "\"defined\" cannot be used as a macro name");
545 else if (! (node->flags & NODE_POISONED))
548 else if (token->flags & NAMED_OP)
549 cpp_error (pfile, CPP_DL_ERROR,
550 "\"%s\" cannot be used as a macro name as it is an operator in C++",
551 NODE_NAME (token->val.node));
552 else if (token->type == CPP_EOF)
553 cpp_error (pfile, CPP_DL_ERROR, "no macro name given in #%s directive",
554 pfile->directive->name);
556 cpp_error (pfile, CPP_DL_ERROR, "macro names must be identifiers");
561 /* Process a #define directive. Most work is done in macro.c. */
563 do_define (cpp_reader *pfile)
565 cpp_hashnode *node = lex_macro_node (pfile, true);
569 /* If we have been requested to expand comments into macros,
570 then re-enable saving of comments. */
571 pfile->state.save_comments =
572 ! CPP_OPTION (pfile, discard_comments_in_macro_exp);
574 if (pfile->cb.before_define)
575 pfile->cb.before_define (pfile);
577 if (_cpp_create_definition (pfile, node))
578 if (pfile->cb.define)
579 pfile->cb.define (pfile, pfile->directive_line, node);
581 node->flags &= ~NODE_USED;
585 /* Handle #undef. Mark the identifier NT_VOID in the hash table. */
587 do_undef (cpp_reader *pfile)
589 cpp_hashnode *node = lex_macro_node (pfile, true);
593 if (pfile->cb.before_define)
594 pfile->cb.before_define (pfile);
597 pfile->cb.undef (pfile, pfile->directive_line, node);
599 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified
600 identifier is not currently defined as a macro name. */
601 if (node->type == NT_MACRO)
603 if (node->flags & NODE_WARN)
604 cpp_error (pfile, CPP_DL_WARNING,
605 "undefining \"%s\"", NODE_NAME (node));
607 if (CPP_OPTION (pfile, warn_unused_macros))
608 _cpp_warn_if_unused_macro (pfile, node, NULL);
610 _cpp_free_definition (node);
617 /* Undefine a single macro/assertion/whatever. */
620 undefine_macros (cpp_reader *pfile ATTRIBUTE_UNUSED, cpp_hashnode *h,
621 void *data_p ATTRIBUTE_UNUSED)
623 /* Body of _cpp_free_definition inlined here for speed.
624 Macros and assertions no longer have anything to free. */
626 h->flags &= ~(NODE_POISONED|NODE_BUILTIN|NODE_DISABLED|NODE_USED);
630 /* Undefine all macros and assertions. */
633 cpp_undef_all (cpp_reader *pfile)
635 cpp_forall_identifiers (pfile, undefine_macros, NULL);
639 /* Helper routine used by parse_include. Reinterpret the current line
640 as an h-char-sequence (< ... >); we are looking at the first token
641 after the <. Returns a malloced filename. */
643 glue_header_name (cpp_reader *pfile)
645 const cpp_token *token;
647 size_t len, total_len = 0, capacity = 1024;
649 /* To avoid lexed tokens overwriting our glued name, we can only
650 allocate from the string pool once we've lexed everything. */
651 buffer = XNEWVEC (char, capacity);
654 token = get_token_no_padding (pfile);
656 if (token->type == CPP_GREATER)
658 if (token->type == CPP_EOF)
660 cpp_error (pfile, CPP_DL_ERROR, "missing terminating > character");
664 len = cpp_token_len (token) + 2; /* Leading space, terminating \0. */
665 if (total_len + len > capacity)
667 capacity = (capacity + len) * 2;
668 buffer = XRESIZEVEC (char, buffer, capacity);
671 if (token->flags & PREV_WHITE)
672 buffer[total_len++] = ' ';
674 total_len = (cpp_spell_token (pfile, token, (uchar *) &buffer[total_len],
679 buffer[total_len] = '\0';
683 /* Returns the file name of #include, #include_next, #import and
684 #pragma dependency. The string is malloced and the caller should
685 free it. Returns NULL on error. */
687 parse_include (cpp_reader *pfile, int *pangle_brackets,
688 const cpp_token ***buf)
691 const cpp_token *header;
693 /* Allow macro expansion. */
694 header = get_token_no_padding (pfile);
695 if (header->type == CPP_STRING || header->type == CPP_HEADER_NAME)
697 fname = XNEWVEC (char, header->val.str.len - 1);
698 memcpy (fname, header->val.str.text + 1, header->val.str.len - 2);
699 fname[header->val.str.len - 2] = '\0';
700 *pangle_brackets = header->type == CPP_HEADER_NAME;
702 else if (header->type == CPP_LESS)
704 fname = glue_header_name (pfile);
705 *pangle_brackets = 1;
709 const unsigned char *dir;
711 if (pfile->directive == &dtable[T_PRAGMA])
712 dir = UC"pragma dependency";
714 dir = pfile->directive->name;
715 cpp_error (pfile, CPP_DL_ERROR, "#%s expects \"FILENAME\" or <FILENAME>",
721 if (pfile->directive == &dtable[T_PRAGMA])
723 /* This pragma allows extra tokens after the file name. */
725 else if (buf == NULL || CPP_OPTION (pfile, discard_comments))
729 /* If we are not discarding comments, then gather them while
730 doing the eol check. */
731 *buf = check_eol_return_comments (pfile);
737 /* Handle #include, #include_next and #import. */
739 do_include_common (cpp_reader *pfile, enum include_type type)
743 const cpp_token **buf = NULL;
745 /* Re-enable saving of comments if requested, so that the include
746 callback can dump comments which follow #include. */
747 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
749 fname = parse_include (pfile, &angle_brackets, &buf);
759 cpp_error (pfile, CPP_DL_ERROR, "empty filename in #%s",
760 pfile->directive->name);
767 /* Prevent #include recursion. */
768 if (pfile->line_table->depth >= CPP_STACK_MAX)
769 cpp_error (pfile, CPP_DL_ERROR, "#include nested too deeply");
772 /* Get out of macro context, if we are. */
773 skip_rest_of_line (pfile);
775 if (pfile->cb.include)
776 pfile->cb.include (pfile, pfile->directive_line,
777 pfile->directive->name, fname, angle_brackets,
780 _cpp_stack_include (pfile, fname, angle_brackets, type);
789 do_include (cpp_reader *pfile)
791 do_include_common (pfile, IT_INCLUDE);
795 do_import (cpp_reader *pfile)
797 do_include_common (pfile, IT_IMPORT);
801 do_include_next (cpp_reader *pfile)
803 enum include_type type = IT_INCLUDE_NEXT;
805 /* If this is the primary source file, warn and use the normal
807 if (cpp_in_primary_file (pfile))
809 cpp_error (pfile, CPP_DL_WARNING,
810 "#include_next in primary source file");
813 do_include_common (pfile, type);
816 /* Subroutine of do_linemarker. Read possible flags after file name.
817 LAST is the last flag seen; 0 if this is the first flag. Return the
818 flag if it is valid, 0 at the end of the directive. Otherwise
821 read_flag (cpp_reader *pfile, unsigned int last)
823 const cpp_token *token = _cpp_lex_token (pfile);
825 if (token->type == CPP_NUMBER && token->val.str.len == 1)
827 unsigned int flag = token->val.str.text[0] - '0';
829 if (flag > last && flag <= 4
830 && (flag != 4 || last == 3)
831 && (flag != 2 || last == 0))
835 if (token->type != CPP_EOF)
836 cpp_error (pfile, CPP_DL_ERROR, "invalid flag \"%s\" in line directive",
837 cpp_token_as_text (pfile, token));
841 /* Subroutine of do_line and do_linemarker. Convert a number in STR,
842 of length LEN, to binary; store it in NUMP, and return false if the
843 number was well-formed, true if not. WRAPPED is set to true if the
844 number did not fit into 'unsigned long'. */
846 strtolinenum (const uchar *str, size_t len, linenum_type *nump, bool *wrapped)
848 linenum_type reg = 0;
849 linenum_type reg_prev = 0;
868 /* Interpret #line command.
869 Note that the filename string (if any) is a true string constant
870 (escapes are interpreted), unlike in #line. */
872 do_line (cpp_reader *pfile)
874 const struct line_maps *line_table = pfile->line_table;
875 const struct line_map *map = &line_table->maps[line_table->used - 1];
877 /* skip_rest_of_line() may cause line table to be realloc()ed so note down
880 unsigned char map_sysp = map->sysp;
881 const cpp_token *token;
882 const char *new_file = map->to_file;
883 linenum_type new_lineno;
885 /* C99 raised the minimum limit on #line numbers. */
886 linenum_type cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
889 /* #line commands expand macros. */
890 token = cpp_get_token (pfile);
891 if (token->type != CPP_NUMBER
892 || strtolinenum (token->val.str.text, token->val.str.len,
893 &new_lineno, &wrapped))
895 if (token->type == CPP_EOF)
896 cpp_error (pfile, CPP_DL_ERROR, "unexpected end of file after #line");
898 cpp_error (pfile, CPP_DL_ERROR,
899 "\"%s\" after #line is not a positive integer",
900 cpp_token_as_text (pfile, token));
904 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap || wrapped))
905 cpp_error (pfile, CPP_DL_PEDWARN, "line number out of range");
907 cpp_error (pfile, CPP_DL_WARNING, "line number out of range");
909 token = cpp_get_token (pfile);
910 if (token->type == CPP_STRING)
912 cpp_string s = { 0, 0 };
913 if (cpp_interpret_string_notranslate (pfile, &token->val.str, 1,
915 new_file = (const char *)s.text;
918 else if (token->type != CPP_EOF)
920 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
921 cpp_token_as_text (pfile, token));
925 skip_rest_of_line (pfile);
926 _cpp_do_file_change (pfile, LC_RENAME, new_file, new_lineno,
930 /* Interpret the # 44 "file" [flags] notation, which has slightly
931 different syntax and semantics from #line: Flags are allowed,
932 and we never complain about the line number being too big. */
934 do_linemarker (cpp_reader *pfile)
936 const struct line_maps *line_table = pfile->line_table;
937 const struct line_map *map = &line_table->maps[line_table->used - 1];
938 const cpp_token *token;
939 const char *new_file = map->to_file;
940 linenum_type new_lineno;
941 unsigned int new_sysp = map->sysp;
942 enum lc_reason reason = LC_RENAME;
946 /* Back up so we can get the number again. Putting this in
947 _cpp_handle_directive risks two calls to _cpp_backup_tokens in
948 some circumstances, which can segfault. */
949 _cpp_backup_tokens (pfile, 1);
951 /* #line commands expand macros. */
952 token = cpp_get_token (pfile);
953 if (token->type != CPP_NUMBER
954 || strtolinenum (token->val.str.text, token->val.str.len,
955 &new_lineno, &wrapped))
957 /* Unlike #line, there does not seem to be a way to get an EOF
958 here. So, it should be safe to always spell the token. */
959 cpp_error (pfile, CPP_DL_ERROR,
960 "\"%s\" after # is not a positive integer",
961 cpp_token_as_text (pfile, token));
965 token = cpp_get_token (pfile);
966 if (token->type == CPP_STRING)
968 cpp_string s = { 0, 0 };
969 if (cpp_interpret_string_notranslate (pfile, &token->val.str,
971 new_file = (const char *)s.text;
974 flag = read_flag (pfile, 0);
978 /* Fake an include for cpp_included (). */
979 _cpp_fake_include (pfile, new_file);
980 flag = read_flag (pfile, flag);
985 flag = read_flag (pfile, flag);
990 flag = read_flag (pfile, flag);
994 pfile->buffer->sysp = new_sysp;
998 else if (token->type != CPP_EOF)
1000 cpp_error (pfile, CPP_DL_ERROR, "\"%s\" is not a valid filename",
1001 cpp_token_as_text (pfile, token));
1005 skip_rest_of_line (pfile);
1006 _cpp_do_file_change (pfile, reason, new_file, new_lineno, new_sysp);
1009 /* Arrange the file_change callback. pfile->line has changed to
1010 FILE_LINE of TO_FILE, for reason REASON. SYSP is 1 for a system
1011 header, 2 for a system header that needs to be extern "C" protected,
1012 and zero otherwise. */
1014 _cpp_do_file_change (cpp_reader *pfile, enum lc_reason reason,
1015 const char *to_file, linenum_type file_line,
1018 const struct line_map *map = linemap_add (pfile->line_table, reason, sysp,
1019 to_file, file_line);
1021 linemap_line_start (pfile->line_table, map->to_line, 127);
1023 if (pfile->cb.file_change)
1024 pfile->cb.file_change (pfile, map);
1027 /* Report a warning or error detected by the program we are
1028 processing. Use the directive's tokens in the error message. */
1030 do_diagnostic (cpp_reader *pfile, int code, int print_dir)
1032 const unsigned char *dir_name;
1033 unsigned char *line;
1034 source_location src_loc = pfile->cur_token[-1].src_loc;
1037 dir_name = pfile->directive->name;
1040 pfile->state.prevent_expansion++;
1041 line = cpp_output_line_to_string (pfile, dir_name);
1042 pfile->state.prevent_expansion--;
1044 cpp_error_with_line (pfile, code, src_loc, 0, "%s", line);
1049 do_error (cpp_reader *pfile)
1051 do_diagnostic (pfile, CPP_DL_ERROR, 1);
1055 do_warning (cpp_reader *pfile)
1057 /* We want #warning diagnostics to be emitted in system headers too. */
1058 do_diagnostic (pfile, CPP_DL_WARNING_SYSHDR, 1);
1061 /* Report program identification. */
1063 do_ident (cpp_reader *pfile)
1065 const cpp_token *str = cpp_get_token (pfile);
1067 if (str->type != CPP_STRING)
1068 cpp_error (pfile, CPP_DL_ERROR, "invalid #%s directive",
1069 pfile->directive->name);
1070 else if (pfile->cb.ident)
1071 pfile->cb.ident (pfile, pfile->directive_line, &str->val.str);
1076 /* Lookup a PRAGMA name in a singly-linked CHAIN. Returns the
1077 matching entry, or NULL if none is found. The returned entry could
1078 be the start of a namespace chain, or a pragma. */
1079 static struct pragma_entry *
1080 lookup_pragma_entry (struct pragma_entry *chain, const cpp_hashnode *pragma)
1082 while (chain && chain->pragma != pragma)
1083 chain = chain->next;
1088 /* Create and insert a blank pragma entry at the beginning of a
1089 singly-linked CHAIN. */
1090 static struct pragma_entry *
1091 new_pragma_entry (cpp_reader *pfile, struct pragma_entry **chain)
1093 struct pragma_entry *new_entry;
1095 new_entry = (struct pragma_entry *)
1096 _cpp_aligned_alloc (pfile, sizeof (struct pragma_entry));
1098 memset (new_entry, 0, sizeof (struct pragma_entry));
1099 new_entry->next = *chain;
1105 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1106 goes in the global namespace. */
1107 static struct pragma_entry *
1108 register_pragma_1 (cpp_reader *pfile, const char *space, const char *name,
1109 bool allow_name_expansion)
1111 struct pragma_entry **chain = &pfile->pragmas;
1112 struct pragma_entry *entry;
1113 const cpp_hashnode *node;
1117 node = cpp_lookup (pfile, UC space, strlen (space));
1118 entry = lookup_pragma_entry (*chain, node);
1121 entry = new_pragma_entry (pfile, chain);
1122 entry->pragma = node;
1123 entry->is_nspace = true;
1124 entry->allow_expansion = allow_name_expansion;
1126 else if (!entry->is_nspace)
1128 else if (entry->allow_expansion != allow_name_expansion)
1130 cpp_error (pfile, CPP_DL_ICE,
1131 "registering pragmas in namespace \"%s\" with mismatched "
1132 "name expansion", space);
1135 chain = &entry->u.space;
1137 else if (allow_name_expansion)
1139 cpp_error (pfile, CPP_DL_ICE,
1140 "registering pragma \"%s\" with name expansion "
1141 "and no namespace", name);
1145 /* Check for duplicates. */
1146 node = cpp_lookup (pfile, UC name, strlen (name));
1147 entry = lookup_pragma_entry (*chain, node);
1150 entry = new_pragma_entry (pfile, chain);
1151 entry->pragma = node;
1155 if (entry->is_nspace)
1157 cpp_error (pfile, CPP_DL_ICE,
1158 "registering \"%s\" as both a pragma and a pragma namespace",
1161 cpp_error (pfile, CPP_DL_ICE, "#pragma %s %s is already registered",
1164 cpp_error (pfile, CPP_DL_ICE, "#pragma %s is already registered", name);
1169 /* Register a cpplib internal pragma SPACE NAME with HANDLER. */
1171 register_pragma_internal (cpp_reader *pfile, const char *space,
1172 const char *name, pragma_cb handler)
1174 struct pragma_entry *entry;
1176 entry = register_pragma_1 (pfile, space, name, false);
1177 entry->is_internal = true;
1178 entry->u.handler = handler;
1181 /* Register a pragma NAME in namespace SPACE. If SPACE is null, it
1182 goes in the global namespace. HANDLER is the handler it will call,
1183 which must be non-NULL. If ALLOW_EXPANSION is set, allow macro
1184 expansion while parsing pragma NAME. This function is exported
1187 cpp_register_pragma (cpp_reader *pfile, const char *space, const char *name,
1188 pragma_cb handler, bool allow_expansion)
1190 struct pragma_entry *entry;
1194 cpp_error (pfile, CPP_DL_ICE, "registering pragma with NULL handler");
1198 entry = register_pragma_1 (pfile, space, name, false);
1201 entry->allow_expansion = allow_expansion;
1202 entry->u.handler = handler;
1206 /* Similarly, but create mark the pragma for deferred processing.
1207 When found, a CPP_PRAGMA token will be insertted into the stream
1208 with IDENT in the token->u.pragma slot. */
1210 cpp_register_deferred_pragma (cpp_reader *pfile, const char *space,
1211 const char *name, unsigned int ident,
1212 bool allow_expansion, bool allow_name_expansion)
1214 struct pragma_entry *entry;
1216 entry = register_pragma_1 (pfile, space, name, allow_name_expansion);
1219 entry->is_deferred = true;
1220 entry->allow_expansion = allow_expansion;
1221 entry->u.ident = ident;
1225 /* Register the pragmas the preprocessor itself handles. */
1227 _cpp_init_internal_pragmas (cpp_reader *pfile)
1229 /* Pragmas in the global namespace. */
1230 register_pragma_internal (pfile, 0, "once", do_pragma_once);
1231 register_pragma_internal (pfile, 0, "push_macro", do_pragma_push_macro);
1232 register_pragma_internal (pfile, 0, "pop_macro", do_pragma_pop_macro);
1234 /* New GCC-specific pragmas should be put in the GCC namespace. */
1235 register_pragma_internal (pfile, "GCC", "poison", do_pragma_poison);
1236 register_pragma_internal (pfile, "GCC", "system_header",
1237 do_pragma_system_header);
1238 register_pragma_internal (pfile, "GCC", "dependency", do_pragma_dependency);
1241 /* Return the number of registered pragmas in PE. */
1244 count_registered_pragmas (struct pragma_entry *pe)
1247 for (; pe != NULL; pe = pe->next)
1250 ct += count_registered_pragmas (pe->u.space);
1256 /* Save into SD the names of the registered pragmas referenced by PE,
1257 and return a pointer to the next free space in SD. */
1260 save_registered_pragmas (struct pragma_entry *pe, char **sd)
1262 for (; pe != NULL; pe = pe->next)
1265 sd = save_registered_pragmas (pe->u.space, sd);
1266 *sd++ = (char *) xmemdup (HT_STR (&pe->pragma->ident),
1267 HT_LEN (&pe->pragma->ident),
1268 HT_LEN (&pe->pragma->ident) + 1);
1273 /* Return a newly-allocated array which saves the names of the
1274 registered pragmas. */
1277 _cpp_save_pragma_names (cpp_reader *pfile)
1279 int ct = count_registered_pragmas (pfile->pragmas);
1280 char **result = XNEWVEC (char *, ct);
1281 (void) save_registered_pragmas (pfile->pragmas, result);
1285 /* Restore from SD the names of the registered pragmas referenced by PE,
1286 and return a pointer to the next unused name in SD. */
1289 restore_registered_pragmas (cpp_reader *pfile, struct pragma_entry *pe,
1292 for (; pe != NULL; pe = pe->next)
1295 sd = restore_registered_pragmas (pfile, pe->u.space, sd);
1296 pe->pragma = cpp_lookup (pfile, UC *sd, strlen (*sd));
1303 /* Restore the names of the registered pragmas from SAVED. */
1306 _cpp_restore_pragma_names (cpp_reader *pfile, char **saved)
1308 (void) restore_registered_pragmas (pfile, pfile->pragmas, saved);
1312 /* Pragmata handling. We handle some, and pass the rest on to the
1313 front end. C99 defines three pragmas and says that no macro
1314 expansion is to be performed on them; whether or not macro
1315 expansion happens for other pragmas is implementation defined.
1316 This implementation allows for a mix of both, since GCC did not
1317 traditionally macro expand its (few) pragmas, whereas OpenMP
1318 specifies that macro expansion should happen. */
1320 do_pragma (cpp_reader *pfile)
1322 const struct pragma_entry *p = NULL;
1323 const cpp_token *token, *pragma_token = pfile->cur_token;
1325 unsigned int count = 1;
1327 pfile->state.prevent_expansion++;
1329 token = cpp_get_token (pfile);
1331 if (token->type == CPP_NAME)
1333 p = lookup_pragma_entry (pfile->pragmas, token->val.node);
1334 if (p && p->is_nspace)
1336 bool allow_name_expansion = p->allow_expansion;
1337 if (allow_name_expansion)
1338 pfile->state.prevent_expansion--;
1339 token = cpp_get_token (pfile);
1340 if (token->type == CPP_NAME)
1341 p = lookup_pragma_entry (p->u.space, token->val.node);
1344 if (allow_name_expansion)
1345 pfile->state.prevent_expansion++;
1354 pfile->directive_result.src_loc = pragma_token->src_loc;
1355 pfile->directive_result.type = CPP_PRAGMA;
1356 pfile->directive_result.flags = pragma_token->flags;
1357 pfile->directive_result.val.pragma = p->u.ident;
1358 pfile->state.in_deferred_pragma = true;
1359 pfile->state.pragma_allow_expansion = p->allow_expansion;
1360 if (!p->allow_expansion)
1361 pfile->state.prevent_expansion++;
1365 /* Since the handler below doesn't get the line number, that
1366 it might need for diagnostics, make sure it has the right
1367 numbers in place. */
1368 if (pfile->cb.line_change)
1369 (*pfile->cb.line_change) (pfile, pragma_token, false);
1370 if (p->allow_expansion)
1371 pfile->state.prevent_expansion--;
1372 (*p->u.handler) (pfile);
1373 if (p->allow_expansion)
1374 pfile->state.prevent_expansion++;
1377 else if (pfile->cb.def_pragma)
1379 if (count == 1 || pfile->context->prev == NULL)
1380 _cpp_backup_tokens (pfile, count);
1383 /* Invalid name comes from macro expansion, _cpp_backup_tokens
1384 won't allow backing 2 tokens. */
1385 /* ??? The token buffer is leaked. Perhaps if def_pragma hook
1386 reads both tokens, we could perhaps free it, but if it doesn't,
1387 we don't know the exact lifespan. */
1388 cpp_token *toks = XNEWVEC (cpp_token, 2);
1390 toks[0].flags |= NO_EXPAND;
1392 toks[1].flags |= NO_EXPAND;
1393 _cpp_push_token_context (pfile, NULL, toks, 2);
1395 pfile->cb.def_pragma (pfile, pfile->directive_line);
1398 pfile->state.prevent_expansion--;
1401 /* Handle #pragma once. */
1403 do_pragma_once (cpp_reader *pfile)
1405 if (cpp_in_primary_file (pfile))
1406 cpp_error (pfile, CPP_DL_WARNING, "#pragma once in main file");
1409 _cpp_mark_file_once_only (pfile, pfile->buffer->file);
1412 /* Handle #pragma push_macro(STRING). */
1414 do_pragma_push_macro (cpp_reader *pfile)
1416 char *macroname, *dest;
1417 const char *limit, *src;
1418 const cpp_token *txt;
1419 struct def_pragma_macro *c;
1421 txt = get__Pragma_string (pfile);
1424 source_location src_loc = pfile->cur_token[-1].src_loc;
1425 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1426 "invalid #pragma push_macro directive");
1428 skip_rest_of_line (pfile);
1431 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1432 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1433 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1436 /* We know there is a character following the backslash. */
1437 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1443 skip_rest_of_line (pfile);
1444 c = XNEW (struct def_pragma_macro);
1445 c->name = XNEWVAR (char, strlen (macroname) + 1);
1446 strcpy (c->name, macroname);
1447 c->next = pfile->pushed_macros;
1448 c->value = cpp_push_definition (pfile, c->name);
1449 pfile->pushed_macros = c;
1452 /* Handle #pragma pop_macro(STRING). */
1454 do_pragma_pop_macro (cpp_reader *pfile)
1456 char *macroname, *dest;
1457 const char *limit, *src;
1458 const cpp_token *txt;
1459 struct def_pragma_macro *l = NULL, *c = pfile->pushed_macros;
1460 txt = get__Pragma_string (pfile);
1463 source_location src_loc = pfile->cur_token[-1].src_loc;
1464 cpp_error_with_line (pfile, CPP_DL_ERROR, src_loc, 0,
1465 "invalid #pragma pop_macro directive");
1467 skip_rest_of_line (pfile);
1470 dest = macroname = (char *) alloca (txt->val.str.len + 2);
1471 src = (const char *) (txt->val.str.text + 1 + (txt->val.str.text[0] == 'L'));
1472 limit = (const char *) (txt->val.str.text + txt->val.str.len - 1);
1475 /* We know there is a character following the backslash. */
1476 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1482 skip_rest_of_line (pfile);
1486 if (!strcmp (c->name, macroname))
1489 pfile->pushed_macros = c->next;
1492 cpp_pop_definition (pfile, c->name, c->value);
1502 /* Handle #pragma GCC poison, to poison one or more identifiers so
1503 that the lexer produces a hard error for each subsequent usage. */
1505 do_pragma_poison (cpp_reader *pfile)
1507 const cpp_token *tok;
1510 pfile->state.poisoned_ok = 1;
1513 tok = _cpp_lex_token (pfile);
1514 if (tok->type == CPP_EOF)
1516 if (tok->type != CPP_NAME)
1518 cpp_error (pfile, CPP_DL_ERROR,
1519 "invalid #pragma GCC poison directive");
1524 if (hp->flags & NODE_POISONED)
1527 if (hp->type == NT_MACRO)
1528 cpp_error (pfile, CPP_DL_WARNING, "poisoning existing macro \"%s\"",
1530 _cpp_free_definition (hp);
1531 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1533 pfile->state.poisoned_ok = 0;
1536 /* Mark the current header as a system header. This will suppress
1537 some categories of warnings (notably those from -pedantic). It is
1538 intended for use in system libraries that cannot be implemented in
1539 conforming C, but cannot be certain that their headers appear in a
1540 system include directory. To prevent abuse, it is rejected in the
1541 primary source file. */
1543 do_pragma_system_header (cpp_reader *pfile)
1545 if (cpp_in_primary_file (pfile))
1546 cpp_error (pfile, CPP_DL_WARNING,
1547 "#pragma system_header ignored outside include file");
1551 skip_rest_of_line (pfile);
1552 cpp_make_system_header (pfile, 1, 0);
1556 /* Check the modified date of the current include file against a specified
1557 file. Issue a diagnostic, if the specified file is newer. We use this to
1558 determine if a fixed header should be refixed. */
1560 do_pragma_dependency (cpp_reader *pfile)
1563 int angle_brackets, ordering;
1565 fname = parse_include (pfile, &angle_brackets, NULL);
1569 ordering = _cpp_compare_file_date (pfile, fname, angle_brackets);
1571 cpp_error (pfile, CPP_DL_WARNING, "cannot find source file %s", fname);
1572 else if (ordering > 0)
1574 cpp_error (pfile, CPP_DL_WARNING,
1575 "current file is older than %s", fname);
1576 if (cpp_get_token (pfile)->type != CPP_EOF)
1578 _cpp_backup_tokens (pfile, 1);
1579 do_diagnostic (pfile, CPP_DL_WARNING, 0);
1583 free ((void *) fname);
1586 /* Get a token but skip padding. */
1587 static const cpp_token *
1588 get_token_no_padding (cpp_reader *pfile)
1592 const cpp_token *result = cpp_get_token (pfile);
1593 if (result->type != CPP_PADDING)
1598 /* Check syntax is "(string-literal)". Returns the string on success,
1599 or NULL on failure. */
1600 static const cpp_token *
1601 get__Pragma_string (cpp_reader *pfile)
1603 const cpp_token *string;
1604 const cpp_token *paren;
1606 paren = get_token_no_padding (pfile);
1607 if (paren->type == CPP_EOF)
1608 _cpp_backup_tokens (pfile, 1);
1609 if (paren->type != CPP_OPEN_PAREN)
1612 string = get_token_no_padding (pfile);
1613 if (string->type == CPP_EOF)
1614 _cpp_backup_tokens (pfile, 1);
1615 if (string->type != CPP_STRING && string->type != CPP_WSTRING
1616 && string->type != CPP_STRING32 && string->type != CPP_STRING16)
1619 paren = get_token_no_padding (pfile);
1620 if (paren->type == CPP_EOF)
1621 _cpp_backup_tokens (pfile, 1);
1622 if (paren->type != CPP_CLOSE_PAREN)
1628 /* Destringize IN into a temporary buffer, by removing the first \ of
1629 \" and \\ sequences, and process the result as a #pragma directive. */
1631 destringize_and_run (cpp_reader *pfile, const cpp_string *in)
1633 const unsigned char *src, *limit;
1634 char *dest, *result;
1635 cpp_context *saved_context;
1636 cpp_token *saved_cur_token;
1637 tokenrun *saved_cur_run;
1640 const struct directive *save_directive;
1642 dest = result = (char *) alloca (in->len - 1);
1643 src = in->text + 1 + (in->text[0] == 'L');
1644 limit = in->text + in->len - 1;
1647 /* We know there is a character following the backslash. */
1648 if (*src == '\\' && (src[1] == '\\' || src[1] == '"'))
1654 /* Ugh; an awful kludge. We are really not set up to be lexing
1655 tokens when in the middle of a macro expansion. Use a new
1656 context to force cpp_get_token to lex, and so skip_rest_of_line
1657 doesn't go beyond the end of the text. Also, remember the
1658 current lexing position so we can return to it later.
1660 Something like line-at-a-time lexing should remove the need for
1662 saved_context = pfile->context;
1663 saved_cur_token = pfile->cur_token;
1664 saved_cur_run = pfile->cur_run;
1666 pfile->context = XNEW (cpp_context);
1667 pfile->context->macro = 0;
1668 pfile->context->prev = 0;
1669 pfile->context->next = 0;
1671 /* Inline run_directive, since we need to delay the _cpp_pop_buffer
1672 until we've read all of the tokens that we want. */
1673 cpp_push_buffer (pfile, (const uchar *) result, dest - result,
1674 /* from_stage3 */ true);
1675 /* ??? Antique Disgusting Hack. What does this do? */
1676 if (pfile->buffer->prev)
1677 pfile->buffer->file = pfile->buffer->prev->file;
1679 start_directive (pfile);
1680 _cpp_clean_line (pfile);
1681 save_directive = pfile->directive;
1682 pfile->directive = &dtable[T_PRAGMA];
1684 end_directive (pfile, 1);
1685 pfile->directive = save_directive;
1687 /* We always insert at least one token, the directive result. It'll
1688 either be a CPP_PADDING or a CPP_PRAGMA. In the later case, we
1689 need to insert *all* of the tokens, including the CPP_PRAGMA_EOL. */
1691 /* If we're not handling the pragma internally, read all of the tokens from
1692 the string buffer now, while the string buffer is still installed. */
1693 /* ??? Note that the token buffer allocated here is leaked. It's not clear
1694 to me what the true lifespan of the tokens are. It would appear that
1695 the lifespan is the entire parse of the main input stream, in which case
1696 this may not be wrong. */
1697 if (pfile->directive_result.type == CPP_PRAGMA)
1703 toks = XNEWVEC (cpp_token, maxcount);
1704 toks[0] = pfile->directive_result;
1708 if (count == maxcount)
1710 maxcount = maxcount * 3 / 2;
1711 toks = XRESIZEVEC (cpp_token, toks, maxcount);
1713 toks[count] = *cpp_get_token (pfile);
1714 /* Macros have been already expanded by cpp_get_token
1715 if the pragma allowed expansion. */
1716 toks[count++].flags |= NO_EXPAND;
1718 while (toks[count-1].type != CPP_PRAGMA_EOL);
1723 toks = XNEW (cpp_token);
1724 toks[0] = pfile->directive_result;
1726 /* If we handled the entire pragma internally, make sure we get the
1727 line number correct for the next token. */
1728 if (pfile->cb.line_change)
1729 pfile->cb.line_change (pfile, pfile->cur_token, false);
1732 /* Finish inlining run_directive. */
1733 pfile->buffer->file = NULL;
1734 _cpp_pop_buffer (pfile);
1736 /* Reset the old macro state before ... */
1737 XDELETE (pfile->context);
1738 pfile->context = saved_context;
1739 pfile->cur_token = saved_cur_token;
1740 pfile->cur_run = saved_cur_run;
1742 /* ... inserting the new tokens we collected. */
1743 _cpp_push_token_context (pfile, NULL, toks, count);
1746 /* Handle the _Pragma operator. Return 0 on error, 1 if ok. */
1748 _cpp_do__Pragma (cpp_reader *pfile)
1750 const cpp_token *string = get__Pragma_string (pfile);
1751 pfile->directive_result.type = CPP_PADDING;
1755 destringize_and_run (pfile, &string->val.str);
1758 cpp_error (pfile, CPP_DL_ERROR,
1759 "_Pragma takes a parenthesized string literal");
1763 /* Handle #ifdef. */
1765 do_ifdef (cpp_reader *pfile)
1769 if (! pfile->state.skipping)
1771 cpp_hashnode *node = lex_macro_node (pfile, false);
1775 skip = node->type != NT_MACRO;
1776 _cpp_mark_macro_used (node);
1777 if (!(node->flags & NODE_USED))
1779 node->flags |= NODE_USED;
1780 if (node->type == NT_MACRO)
1782 if (pfile->cb.used_define)
1783 pfile->cb.used_define (pfile, pfile->directive_line, node);
1787 if (pfile->cb.used_undef)
1788 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1795 push_conditional (pfile, skip, T_IFDEF, 0);
1798 /* Handle #ifndef. */
1800 do_ifndef (cpp_reader *pfile)
1803 cpp_hashnode *node = 0;
1805 if (! pfile->state.skipping)
1807 node = lex_macro_node (pfile, false);
1811 skip = node->type == NT_MACRO;
1812 _cpp_mark_macro_used (node);
1813 if (!(node->flags & NODE_USED))
1815 node->flags |= NODE_USED;
1816 if (node->type == NT_MACRO)
1818 if (pfile->cb.used_define)
1819 pfile->cb.used_define (pfile, pfile->directive_line, node);
1823 if (pfile->cb.used_undef)
1824 pfile->cb.used_undef (pfile, pfile->directive_line, node);
1831 push_conditional (pfile, skip, T_IFNDEF, node);
1834 /* _cpp_parse_expr puts a macro in a "#if !defined ()" expression in
1835 pfile->mi_ind_cmacro so we can handle multiple-include
1836 optimizations. If macro expansion occurs in the expression, we
1837 cannot treat it as a controlling conditional, since the expansion
1838 could change in the future. That is handled by cpp_get_token. */
1840 do_if (cpp_reader *pfile)
1844 if (! pfile->state.skipping)
1845 skip = _cpp_parse_expr (pfile, true) == false;
1847 push_conditional (pfile, skip, T_IF, pfile->mi_ind_cmacro);
1850 /* Flip skipping state if appropriate and continue without changing
1851 if_stack; this is so that the error message for missing #endif's
1852 etc. will point to the original #if. */
1854 do_else (cpp_reader *pfile)
1856 cpp_buffer *buffer = pfile->buffer;
1857 struct if_stack *ifs = buffer->if_stack;
1860 cpp_error (pfile, CPP_DL_ERROR, "#else without #if");
1863 if (ifs->type == T_ELSE)
1865 cpp_error (pfile, CPP_DL_ERROR, "#else after #else");
1866 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1867 "the conditional began here");
1871 /* Skip any future (erroneous) #elses or #elifs. */
1872 pfile->state.skipping = ifs->skip_elses;
1873 ifs->skip_elses = true;
1875 /* Invalidate any controlling macro. */
1878 /* Only check EOL if was not originally skipping. */
1879 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1884 /* Handle a #elif directive by not changing if_stack either. See the
1885 comment above do_else. */
1887 do_elif (cpp_reader *pfile)
1889 cpp_buffer *buffer = pfile->buffer;
1890 struct if_stack *ifs = buffer->if_stack;
1893 cpp_error (pfile, CPP_DL_ERROR, "#elif without #if");
1896 if (ifs->type == T_ELSE)
1898 cpp_error (pfile, CPP_DL_ERROR, "#elif after #else");
1899 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
1900 "the conditional began here");
1904 if (! ifs->was_skipping)
1907 /* The standard mandates that the expression be parsed even
1908 if we are skipping elses at this point -- the lexical
1909 restrictions on #elif only apply to skipped groups, but
1910 this group is not being skipped. Temporarily set
1911 skipping to false to get lexer warnings. */
1912 pfile->state.skipping = 0;
1913 value = _cpp_parse_expr (pfile, false);
1914 if (ifs->skip_elses)
1915 pfile->state.skipping = 1;
1918 pfile->state.skipping = ! value;
1919 ifs->skip_elses = value;
1923 /* Invalidate any controlling macro. */
1928 /* #endif pops the if stack and resets pfile->state.skipping. */
1930 do_endif (cpp_reader *pfile)
1932 cpp_buffer *buffer = pfile->buffer;
1933 struct if_stack *ifs = buffer->if_stack;
1936 cpp_error (pfile, CPP_DL_ERROR, "#endif without #if");
1939 /* Only check EOL if was not originally skipping. */
1940 if (!ifs->was_skipping && CPP_OPTION (pfile, warn_endif_labels))
1943 /* If potential control macro, we go back outside again. */
1944 if (ifs->next == 0 && ifs->mi_cmacro)
1946 pfile->mi_valid = true;
1947 pfile->mi_cmacro = ifs->mi_cmacro;
1950 buffer->if_stack = ifs->next;
1951 pfile->state.skipping = ifs->was_skipping;
1952 obstack_free (&pfile->buffer_ob, ifs);
1956 /* Push an if_stack entry for a preprocessor conditional, and set
1957 pfile->state.skipping to SKIP. If TYPE indicates the conditional
1958 is #if or #ifndef, CMACRO is a potentially controlling macro, and
1959 we need to check here that we are at the top of the file. */
1961 push_conditional (cpp_reader *pfile, int skip, int type,
1962 const cpp_hashnode *cmacro)
1964 struct if_stack *ifs;
1965 cpp_buffer *buffer = pfile->buffer;
1967 ifs = XOBNEW (&pfile->buffer_ob, struct if_stack);
1968 ifs->line = pfile->directive_line;
1969 ifs->next = buffer->if_stack;
1970 ifs->skip_elses = pfile->state.skipping || !skip;
1971 ifs->was_skipping = pfile->state.skipping;
1973 /* This condition is effectively a test for top-of-file. */
1974 if (pfile->mi_valid && pfile->mi_cmacro == 0)
1975 ifs->mi_cmacro = cmacro;
1979 pfile->state.skipping = skip;
1980 buffer->if_stack = ifs;
1983 /* Read the tokens of the answer into the macro pool, in a directive
1984 of type TYPE. Only commit the memory if we intend it as permanent
1985 storage, i.e. the #assert case. Returns 0 on success, and sets
1986 ANSWERP to point to the answer. */
1988 parse_answer (cpp_reader *pfile, struct answer **answerp, int type)
1990 const cpp_token *paren;
1991 struct answer *answer;
1992 unsigned int acount;
1994 /* In a conditional, it is legal to not have an open paren. We
1995 should save the following token in this case. */
1996 paren = cpp_get_token (pfile);
1998 /* If not a paren, see if we're OK. */
1999 if (paren->type != CPP_OPEN_PAREN)
2001 /* In a conditional no answer is a test for any answer. It
2002 could be followed by any token. */
2005 _cpp_backup_tokens (pfile, 1);
2009 /* #unassert with no answer is valid - it removes all answers. */
2010 if (type == T_UNASSERT && paren->type == CPP_EOF)
2013 cpp_error (pfile, CPP_DL_ERROR, "missing '(' after predicate");
2017 for (acount = 0;; acount++)
2020 const cpp_token *token = cpp_get_token (pfile);
2023 if (token->type == CPP_CLOSE_PAREN)
2026 if (token->type == CPP_EOF)
2028 cpp_error (pfile, CPP_DL_ERROR, "missing ')' to complete answer");
2032 /* struct answer includes the space for one token. */
2033 room_needed = (sizeof (struct answer) + acount * sizeof (cpp_token));
2035 if (BUFF_ROOM (pfile->a_buff) < room_needed)
2036 _cpp_extend_buff (pfile, &pfile->a_buff, sizeof (struct answer));
2038 dest = &((struct answer *) BUFF_FRONT (pfile->a_buff))->first[acount];
2041 /* Drop whitespace at start, for answer equivalence purposes. */
2043 dest->flags &= ~PREV_WHITE;
2048 cpp_error (pfile, CPP_DL_ERROR, "predicate's answer is empty");
2052 answer = (struct answer *) BUFF_FRONT (pfile->a_buff);
2053 answer->count = acount;
2054 answer->next = NULL;
2060 /* Parses an assertion directive of type TYPE, returning a pointer to
2061 the hash node of the predicate, or 0 on error. If an answer was
2062 supplied, it is placed in ANSWERP, otherwise it is set to 0. */
2063 static cpp_hashnode *
2064 parse_assertion (cpp_reader *pfile, struct answer **answerp, int type)
2066 cpp_hashnode *result = 0;
2067 const cpp_token *predicate;
2069 /* We don't expand predicates or answers. */
2070 pfile->state.prevent_expansion++;
2073 predicate = cpp_get_token (pfile);
2074 if (predicate->type == CPP_EOF)
2075 cpp_error (pfile, CPP_DL_ERROR, "assertion without predicate");
2076 else if (predicate->type != CPP_NAME)
2077 cpp_error (pfile, CPP_DL_ERROR, "predicate must be an identifier");
2078 else if (parse_answer (pfile, answerp, type) == 0)
2080 unsigned int len = NODE_LEN (predicate->val.node);
2081 unsigned char *sym = (unsigned char *) alloca (len + 1);
2083 /* Prefix '#' to get it out of macro namespace. */
2085 memcpy (sym + 1, NODE_NAME (predicate->val.node), len);
2086 result = cpp_lookup (pfile, sym, len + 1);
2089 pfile->state.prevent_expansion--;
2093 /* Returns a pointer to the pointer to CANDIDATE in the answer chain,
2094 or a pointer to NULL if the answer is not in the chain. */
2095 static struct answer **
2096 find_answer (cpp_hashnode *node, const struct answer *candidate)
2099 struct answer **result;
2101 for (result = &node->value.answers; *result; result = &(*result)->next)
2103 struct answer *answer = *result;
2105 if (answer->count == candidate->count)
2107 for (i = 0; i < answer->count; i++)
2108 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
2111 if (i == answer->count)
2119 /* Test an assertion within a preprocessor conditional. Returns
2120 nonzero on failure, zero on success. On success, the result of
2121 the test is written into VALUE, otherwise the value 0. */
2123 _cpp_test_assertion (cpp_reader *pfile, unsigned int *value)
2125 struct answer *answer;
2128 node = parse_assertion (pfile, &answer, T_IF);
2130 /* For recovery, an erroneous assertion expression is handled as a
2131 failing assertion. */
2135 *value = (node->type == NT_ASSERTION &&
2136 (answer == 0 || *find_answer (node, answer) != 0));
2137 else if (pfile->cur_token[-1].type == CPP_EOF)
2138 _cpp_backup_tokens (pfile, 1);
2140 /* We don't commit the memory for the answer - it's temporary only. */
2144 /* Handle #assert. */
2146 do_assert (cpp_reader *pfile)
2148 struct answer *new_answer;
2151 node = parse_assertion (pfile, &new_answer, T_ASSERT);
2156 /* Place the new answer in the answer list. First check there
2157 is not a duplicate. */
2158 new_answer->next = 0;
2159 if (node->type == NT_ASSERTION)
2161 if (*find_answer (node, new_answer))
2163 cpp_error (pfile, CPP_DL_WARNING, "\"%s\" re-asserted",
2164 NODE_NAME (node) + 1);
2167 new_answer->next = node->value.answers;
2170 answer_size = sizeof (struct answer) + ((new_answer->count - 1)
2171 * sizeof (cpp_token));
2172 /* Commit or allocate storage for the object. */
2173 if (pfile->hash_table->alloc_subobject)
2175 struct answer *temp_answer = new_answer;
2176 new_answer = (struct answer *) pfile->hash_table->alloc_subobject
2178 memcpy (new_answer, temp_answer, answer_size);
2181 BUFF_FRONT (pfile->a_buff) += answer_size;
2183 node->type = NT_ASSERTION;
2184 node->value.answers = new_answer;
2189 /* Handle #unassert. */
2191 do_unassert (cpp_reader *pfile)
2194 struct answer *answer;
2196 node = parse_assertion (pfile, &answer, T_UNASSERT);
2197 /* It isn't an error to #unassert something that isn't asserted. */
2198 if (node && node->type == NT_ASSERTION)
2202 struct answer **p = find_answer (node, answer), *temp;
2204 /* Remove the answer from the list. */
2209 /* Did we free the last answer? */
2210 if (node->value.answers == 0)
2211 node->type = NT_VOID;
2216 _cpp_free_definition (node);
2219 /* We don't commit the memory for the answer - it's temporary only. */
2222 /* These are for -D, -U, -A. */
2224 /* Process the string STR as if it appeared as the body of a #define.
2225 If STR is just an identifier, define it with value 1.
2226 If STR has anything after the identifier, then it should
2227 be identifier=definition. */
2229 cpp_define (cpp_reader *pfile, const char *str)
2234 /* Copy the entire option so we can modify it.
2235 Change the first "=" in the string to a space. If there is none,
2236 tack " 1" on the end. */
2238 count = strlen (str);
2239 buf = (char *) alloca (count + 3);
2240 memcpy (buf, str, count);
2242 p = strchr (str, '=');
2252 run_directive (pfile, T_DEFINE, buf, count);
2256 /* Use to build macros to be run through cpp_define() as
2258 Example: cpp_define_formatted (pfile, "MACRO=%d", value); */
2261 cpp_define_formatted (cpp_reader *pfile, const char *fmt, ...)
2267 vasprintf (&ptr, fmt, ap);
2270 cpp_define (pfile, ptr);
2275 /* Slight variant of the above for use by initialize_builtins. */
2277 _cpp_define_builtin (cpp_reader *pfile, const char *str)
2279 size_t len = strlen (str);
2280 char *buf = (char *) alloca (len + 1);
2281 memcpy (buf, str, len);
2283 run_directive (pfile, T_DEFINE, buf, len);
2286 /* Process MACRO as if it appeared as the body of an #undef. */
2288 cpp_undef (cpp_reader *pfile, const char *macro)
2290 size_t len = strlen (macro);
2291 char *buf = (char *) alloca (len + 1);
2292 memcpy (buf, macro, len);
2294 run_directive (pfile, T_UNDEF, buf, len);
2297 /* If STR is a defined macro, return its definition node, else return NULL. */
2299 cpp_push_definition (cpp_reader *pfile, const char *str)
2301 cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
2302 if (node && node->type == NT_MACRO)
2303 return node->value.macro;
2308 /* Replace a previous definition DFN of the macro STR. If DFN is NULL,
2309 then the macro should be undefined. */
2311 cpp_pop_definition (cpp_reader *pfile, const char *str, cpp_macro *dfn)
2313 cpp_hashnode *node = _cpp_lex_identifier (pfile, str);
2317 if (pfile->cb.before_define)
2318 pfile->cb.before_define (pfile);
2320 if (node->type == NT_MACRO)
2322 if (pfile->cb.undef)
2323 pfile->cb.undef (pfile, pfile->directive_line, node);
2324 if (CPP_OPTION (pfile, warn_unused_macros))
2325 _cpp_warn_if_unused_macro (pfile, node, NULL);
2327 if (node->type != NT_VOID)
2328 _cpp_free_definition (node);
2332 node->type = NT_MACRO;
2333 node->value.macro = dfn;
2334 if (! ustrncmp (NODE_NAME (node), DSC ("__STDC_")))
2335 node->flags |= NODE_WARN;
2337 if (pfile->cb.define)
2338 pfile->cb.define (pfile, pfile->directive_line, node);
2342 /* Process the string STR as if it appeared as the body of a #assert. */
2344 cpp_assert (cpp_reader *pfile, const char *str)
2346 handle_assertion (pfile, str, T_ASSERT);
2349 /* Process STR as if it appeared as the body of an #unassert. */
2351 cpp_unassert (cpp_reader *pfile, const char *str)
2353 handle_assertion (pfile, str, T_UNASSERT);
2356 /* Common code for cpp_assert (-A) and cpp_unassert (-A-). */
2358 handle_assertion (cpp_reader *pfile, const char *str, int type)
2360 size_t count = strlen (str);
2361 const char *p = strchr (str, '=');
2363 /* Copy the entire option so we can modify it. Change the first
2364 "=" in the string to a '(', and tack a ')' on the end. */
2365 char *buf = (char *) alloca (count + 2);
2367 memcpy (buf, str, count);
2376 run_directive (pfile, type, str, count);
2379 /* The number of errors for a given reader. */
2381 cpp_errors (cpp_reader *pfile)
2383 return pfile->errors;
2386 /* The options structure. */
2388 cpp_get_options (cpp_reader *pfile)
2390 return &pfile->opts;
2393 /* The callbacks structure. */
2395 cpp_get_callbacks (cpp_reader *pfile)
2400 /* Copy the given callbacks structure to our own. */
2402 cpp_set_callbacks (cpp_reader *pfile, cpp_callbacks *cb)
2407 /* The dependencies structure. (Creates one if it hasn't already been.) */
2409 cpp_get_deps (cpp_reader *pfile)
2412 pfile->deps = deps_init ();
2416 /* Push a new buffer on the buffer stack. Returns the new buffer; it
2417 doesn't fail. It does not generate a file change call back; that
2418 is the responsibility of the caller. */
2420 cpp_push_buffer (cpp_reader *pfile, const uchar *buffer, size_t len,
2423 cpp_buffer *new_buffer = XOBNEW (&pfile->buffer_ob, cpp_buffer);
2425 /* Clears, amongst other things, if_stack and mi_cmacro. */
2426 memset (new_buffer, 0, sizeof (cpp_buffer));
2428 new_buffer->next_line = new_buffer->buf = buffer;
2429 new_buffer->rlimit = buffer + len;
2430 new_buffer->from_stage3 = from_stage3;
2431 new_buffer->prev = pfile->buffer;
2432 new_buffer->need_line = true;
2434 pfile->buffer = new_buffer;
2439 /* Pops a single buffer, with a file change call-back if appropriate.
2440 Then pushes the next -include file, if any remain. */
2442 _cpp_pop_buffer (cpp_reader *pfile)
2444 cpp_buffer *buffer = pfile->buffer;
2445 struct _cpp_file *inc = buffer->file;
2446 struct if_stack *ifs;
2448 /* Walk back up the conditional stack till we reach its level at
2449 entry to this file, issuing error messages. */
2450 for (ifs = buffer->if_stack; ifs; ifs = ifs->next)
2451 cpp_error_with_line (pfile, CPP_DL_ERROR, ifs->line, 0,
2452 "unterminated #%s", dtable[ifs->type].name);
2454 /* In case of a missing #endif. */
2455 pfile->state.skipping = 0;
2457 /* _cpp_do_file_change expects pfile->buffer to be the new one. */
2458 pfile->buffer = buffer->prev;
2460 free (buffer->notes);
2462 /* Free the buffer object now; we may want to push a new buffer
2463 in _cpp_push_next_include_file. */
2464 obstack_free (&pfile->buffer_ob, buffer);
2468 _cpp_pop_file_buffer (pfile, inc);
2470 _cpp_do_file_change (pfile, LC_LEAVE, 0, 0, 0);
2474 /* Enter all recognized directives in the hash table. */
2476 _cpp_init_directives (cpp_reader *pfile)
2481 for (i = 0; i < (unsigned int) N_DIRECTIVES; i++)
2483 node = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
2484 node->is_directive = 1;
2485 node->directive_index = i;