2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 /* Chained list of answers to an assertion. */
39 /* Stack of conditionals currently in progress
40 (including both successful and failing conditionals). */
44 struct if_stack *next;
45 cpp_lexer_pos pos; /* line and column where condition started */
46 const cpp_hashnode *mi_cmacro;/* macro name for #ifndef around entire file */
47 int was_skipping; /* value of pfile->skipping before this if */
48 int type; /* type of last directive seen in this group */
51 /* Values for the origin field of struct directive. KANDR directives
52 come from traditional (K&R) C. STDC89 directives come from the
53 1989 C standard. EXTENSION directives are extensions. */
58 /* Values for the flags field of struct directive. COND indicates a
59 conditional; IF_COND an opening conditional. INCL means to treat
60 "..." and <...> as q-char and h-char sequences respectively. IN_I
61 means this directive should be handled even if -fpreprocessed is in
62 effect (these are the directives with callback hooks). */
64 #define IF_COND (1 << 1)
68 /* Defines one #-directive, including how to handle it. */
69 typedef void (*directive_handler) PARAMS ((cpp_reader *));
70 typedef struct directive directive;
73 directive_handler handler; /* Function to handle directive. */
74 const U_CHAR *name; /* Name of directive. */
75 unsigned short length; /* Length of name. */
76 unsigned char origin; /* Origin of directive. */
77 unsigned char flags; /* Flags describing this directive. */
80 /* Forward declarations. */
82 static void skip_rest_of_line PARAMS ((cpp_reader *));
83 static void check_eol PARAMS ((cpp_reader *));
84 static void run_directive PARAMS ((cpp_reader *, int,
87 static int glue_header_name PARAMS ((cpp_reader *, cpp_token *));
88 static int parse_include PARAMS ((cpp_reader *, cpp_token *));
89 static void push_conditional PARAMS ((cpp_reader *, int, int,
90 const cpp_hashnode *));
91 static int read_line_number PARAMS ((cpp_reader *, int *));
92 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
94 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type));
96 lex_macro_node PARAMS ((cpp_reader *));
97 static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
98 static void do_pragma_once PARAMS ((cpp_reader *));
99 static void do_pragma_poison PARAMS ((cpp_reader *));
100 static void do_pragma_system_header PARAMS ((cpp_reader *));
101 static void do_pragma_dependency PARAMS ((cpp_reader *));
102 static int parse_answer PARAMS ((cpp_reader *, struct answer **, int));
103 static cpp_hashnode *parse_assertion PARAMS ((cpp_reader *, struct answer **,
105 static struct answer ** find_answer PARAMS ((cpp_hashnode *,
106 const struct answer *));
108 /* This is the table of directive handlers. It is ordered by
109 frequency of occurrence; the numbers at the end are directive
110 counts from all the source code I have lying around (egcs and libc
111 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
112 pcmcia-cs-3.0.9). This is no longer important as directive lookup
113 is now O(1). All extensions other than #warning and #include_next
114 are deprecated. The name is where the extension appears to have
117 #define DIRECTIVE_TABLE \
118 D(define, T_DEFINE = 0, KANDR, IN_I) /* 270554 */ \
119 D(include, T_INCLUDE, KANDR, INCL) /* 52262 */ \
120 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
121 D(ifdef, T_IFDEF, KANDR, COND | IF_COND) /* 22000 */ \
122 D(if, T_IF, KANDR, COND | IF_COND) /* 18162 */ \
123 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
124 D(ifndef, T_IFNDEF, KANDR, COND | IF_COND) /* 9675 */ \
125 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
126 D(line, T_LINE, KANDR, IN_I) /* 2465 */ \
127 D(elif, T_ELIF, KANDR, COND) /* 610 */ \
128 D(error, T_ERROR, STDC89, 0) /* 475 */ \
129 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
130 D(warning, T_WARNING, EXTENSION, 0) /* 22 */ \
131 D(include_next, T_INCLUDE_NEXT, EXTENSION, INCL) /* 19 */ \
132 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 */ \
133 D(import, T_IMPORT, EXTENSION, INCL) /* 0 ObjC */ \
134 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
135 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
136 SCCS_ENTRY /* 0 SVR4? */
138 /* #sccs is not always recognized. */
139 #ifdef SCCS_DIRECTIVE
140 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
142 # define SCCS_ENTRY /* nothing */
145 /* Use the table to generate a series of prototypes, an enum for the
146 directive names, and an array of directive handlers. */
148 /* The directive-processing functions are declared to return int
149 instead of void, because some old compilers have trouble with
150 pointers to functions returning void. */
152 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
153 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
157 #define D(n, tag, o, f) tag,
166 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
167 #define D(name, t, origin, flags) \
168 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
169 sizeof STRINGX(name) - 1, origin, flags },
170 static const directive dtable[] =
175 #undef DIRECTIVE_TABLE
177 /* Skip any remaining tokens in a directive. */
179 skip_rest_of_line (pfile)
184 /* Discard all stacked contexts. */
185 while (pfile->context != &pfile->base_context)
186 _cpp_pop_context (pfile);
188 /* Sweep up all tokens remaining on the line. We need to read
189 tokens from lookahead, but cannot just drop the lookahead buffers
190 because they may be saving tokens prior to this directive for an
191 external client. So we use cpp_get_token, with macros disabled. */
192 pfile->state.prevent_expansion++;
193 while (!pfile->state.skip_newlines)
194 _cpp_lex_token (pfile, &token);
195 pfile->state.prevent_expansion--;
198 /* Ensure there are no stray tokens at the end of a directive. */
203 if (!pfile->state.skip_newlines)
207 _cpp_lex_token (pfile, &token);
208 if (token.type != CPP_EOF)
209 cpp_pedwarn (pfile, "extra tokens at end of #%s directive",
210 pfile->directive->name);
214 /* Check if a token's name matches that of a known directive. Put in
215 this file to save exporting dtable and other unneeded information. */
217 _cpp_handle_directive (pfile, indented)
221 const directive *dir = 0;
225 /* Some handlers need the position of the # for diagnostics. */
226 pfile->directive_pos = pfile->lexer_pos;
228 /* We're now in a directive. This ensures we get pedantic warnings
229 about /v and /f in whitespace. */
230 pfile->state.in_directive = 1;
231 pfile->state.save_comments = 0;
233 /* Lex the directive name directly. */
234 _cpp_lex_token (pfile, &dname);
236 if (dname.type == CPP_NAME)
238 unsigned int index = dname.val.node->directive_index;
240 dir = &dtable[index - 1];
242 else if (dname.type == CPP_NUMBER)
244 /* # followed by a number is equivalent to #line. Do not
245 recognize this form in assembly language source files or
246 skipped conditional groups. Complain about this form if
247 we're being pedantic, but not if this is regurgitated input
248 (preprocessed or fed back in by the C++ frontend). */
249 if (!pfile->skipping && !CPP_OPTION (pfile, lang_asm))
251 dir = &dtable[T_LINE];
252 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
253 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
254 && ! CPP_OPTION (pfile, preprocessed))
255 cpp_pedwarn (pfile, "# followed by integer");
259 pfile->directive = dir;
262 /* Make sure we lex headers correctly, whether skipping or not. */
263 pfile->state.angled_headers = dir->flags & INCL;
265 /* If we are rescanning preprocessed input, only directives tagged
266 with IN_I are honored, and the warnings below are suppressed. */
267 if (! CPP_OPTION (pfile, preprocessed) || dir->flags & IN_I)
269 /* Traditionally, a directive is ignored unless its # is in
270 column 1. Therefore in code intended to work with K+R
271 compilers, directives added by C89 must have their #
272 indented, and directives present in traditional C must
273 not. This is true even of directives in skipped
274 conditional blocks. */
275 if (CPP_WTRADITIONAL (pfile))
277 if (indented && dir->origin == KANDR)
279 "traditional C ignores #%s with the # indented",
281 else if (!indented && dir->origin != KANDR)
283 "suggest hiding #%s from traditional C with an indented #",
287 /* If we are skipping a failed conditional group, all
288 non-conditional directives are ignored. */
289 if (!pfile->skipping || (dir->flags & COND))
291 /* Issue -pedantic warnings for extensions. */
292 if (CPP_PEDANTIC (pfile) && dir->origin == EXTENSION)
293 cpp_pedwarn (pfile, "#%s is a GCC extension", dir->name);
295 /* If we have a directive that is not an opening
296 conditional, invalidate any control macro. */
297 if (! (dir->flags & IF_COND))
298 pfile->mi_state = MI_FAILED;
300 (*dir->handler) (pfile);
304 else if (dname.type == CPP_EOF)
306 /* The null directive. */
307 if (indented && CPP_WTRADITIONAL (pfile))
308 cpp_warning (pfile, "traditional C ignores #\\n with the # indented");
312 /* An unknown directive. Don't complain about it in assembly
313 source: we don't know where the comments are, and # may
314 introduce assembler pseudo-ops. Don't complain about invalid
315 directives in skipped conditional groups (6.10 p4). */
316 if (CPP_OPTION (pfile, lang_asm))
318 /* Output the # and lookahead token for the assembler. */
320 _cpp_push_token (pfile, &dname, &pfile->directive_pos);
322 else if (!pfile->skipping)
323 cpp_error (pfile, "invalid preprocessing directive #%s",
324 cpp_token_as_text (pfile, &dname));
327 /* Save the lookahead token for assembler. */
329 skip_rest_of_line (pfile);
330 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
331 pfile->state.in_directive = 0;
332 pfile->state.angled_headers = 0;
333 pfile->directive = 0;
338 /* Directive handler wrapper used by the command line option
341 run_directive (pfile, dir_no, buf, count, name)
348 if (cpp_push_buffer (pfile, (const U_CHAR *)buf, count) != NULL)
350 const struct directive *dir = &dtable[dir_no];
353 CPP_BUFFER (pfile)->nominal_fname = name;
355 CPP_BUFFER (pfile)->nominal_fname = _("<command line>");
356 CPP_BUFFER (pfile)->lineno = (unsigned int)-1;
358 pfile->state.in_directive = 1;
359 pfile->directive = dir;
360 (void) (*dir->handler) (pfile);
361 pfile->directive = 0;
362 pfile->state.in_directive = 0;
364 skip_rest_of_line (pfile);
365 cpp_pop_buffer (pfile);
369 /* Checks for validity the macro name in #define, #undef, #ifdef and
370 #ifndef directives. */
371 static cpp_hashnode *
372 lex_macro_node (pfile)
377 /* Lex the macro name directly. */
378 _cpp_lex_token (pfile, &token);
380 /* The token immediately after #define must be an identifier. That
381 identifier is not allowed to be "defined". See predefined macro
382 names (6.10.8.4). In C++, it is not allowed to be any of the
383 <iso646.h> macro names (which are keywords in C++) either. */
385 if (token.type != CPP_NAME)
387 if (token.type == CPP_EOF)
388 cpp_error (pfile, "no macro name given in #%s directive",
389 pfile->directive->name);
390 else if (token.flags & NAMED_OP)
392 "\"%s\" cannot be used as a macro name as it is an operator in C++",
393 token.val.node->name);
395 cpp_error (pfile, "macro names must be identifiers");
399 cpp_hashnode *node = token.val.node;
401 /* In Objective C, some keywords begin with '@', but general
402 identifiers do not, and you're not allowed to #define them. */
403 if (node == pfile->spec_nodes.n_defined || node->name[0] == '@')
404 cpp_error (pfile, "\"%s\" cannot be used as a macro name", node->name);
405 else if (!(node->flags & NODE_POISONED))
412 /* Process a #define directive. Most work is done in cppmacro.c. */
417 cpp_hashnode *node = lex_macro_node (pfile);
421 /* Use the permanent pool for storage. */
422 pfile->string_pool = &pfile->ident_pool;
424 if (_cpp_create_definition (pfile, node))
425 if (pfile->cb.define)
426 (*pfile->cb.define) (pfile, node);
428 /* Revert to the temporary pool. */
429 pfile->string_pool = &pfile->temp_string_pool;
433 /* Handle #undef. Marks the identifier NT_VOID in the hash table. */
438 cpp_hashnode *node = lex_macro_node (pfile);
440 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
441 is not currently defined as a macro name. */
442 if (node && node->type == NT_MACRO)
445 (*pfile->cb.undef) (pfile, node);
447 if (node->flags & NODE_BUILTIN)
448 cpp_warning (pfile, "undefining \"%s\"", node->name);
450 _cpp_free_definition (node);
455 /* Helper routine used by parse_include. Reinterpret the current line
456 as an h-char-sequence (< ... >); we are looking at the first token
457 after the <. Returns zero on success. */
459 glue_header_name (pfile, header)
464 unsigned char *buffer, *token_mem;
465 size_t len, total_len = 0, capacity = 1024;
467 /* To avoid lexed tokens overwriting our glued name, we can only
468 allocate from the string pool once we've lexed everything. */
470 buffer = (unsigned char *) xmalloc (capacity);
473 _cpp_get_token (pfile, &token);
475 if (token.type == CPP_GREATER || token.type == CPP_EOF)
478 len = cpp_token_len (&token);
479 if (total_len + len > capacity)
481 capacity = (capacity + len) * 2;
482 buffer = (unsigned char *) realloc (buffer, capacity);
485 if (token.flags & PREV_WHITE)
486 buffer[total_len++] = ' ';
488 total_len = cpp_spell_token (pfile, &token, &buffer[total_len]) - buffer;
491 if (token.type == CPP_EOF)
492 cpp_error (pfile, "missing terminating > character");
495 token_mem = _cpp_pool_alloc (pfile->string_pool, total_len);
496 memcpy (token_mem, buffer, total_len);
498 header->type = CPP_HEADER_NAME;
499 header->flags &= ~PREV_WHITE;
500 header->val.str.len = total_len;
501 header->val.str.text = token_mem;
505 return token.type == CPP_EOF;
508 /* Parse the header name of #include, #include_next, #import and
509 #pragma dependency. Returns zero on success. */
511 parse_include (pfile, header)
515 int is_pragma = pfile->directive == &dtable[T_PRAGMA];
516 const unsigned char *dir;
519 dir = U"pragma dependency";
521 dir = pfile->directive->name;
523 /* Allow macro expansion. */
524 cpp_get_token (pfile, header);
525 if (header->type != CPP_STRING && header->type != CPP_HEADER_NAME)
527 if (header->type != CPP_LESS)
529 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
532 if (glue_header_name (pfile, header))
536 if (header->val.str.len == 0)
538 cpp_error (pfile, "empty file name in #%s", dir);
545 /* Get out of macro context, if we are. */
546 skip_rest_of_line (pfile);
547 if (pfile->cb.include)
548 (*pfile->cb.include) (pfile, dir, header);
560 if (!parse_include (pfile, &header))
561 _cpp_execute_include (pfile, &header, 0, 0);
570 if (!pfile->import_warning && CPP_OPTION (pfile, warn_import))
572 pfile->import_warning = 1;
574 "#import is obsolete, use an #ifndef wrapper in the header file");
577 if (!parse_include (pfile, &header))
578 _cpp_execute_include (pfile, &header, 1, 0);
582 do_include_next (pfile)
586 struct file_name_list *search_start = 0;
588 if (parse_include (pfile, &header))
591 /* For #include_next, skip in the search path past the dir in which
592 the current file was found. If this is the last directory in the
593 search path, don't include anything. If the current file was
594 specified with an absolute path, use the normal search logic. If
595 this is the primary source file, use the normal search logic and
596 generate a warning. */
597 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
599 if (CPP_BUFFER (pfile)->inc->foundhere)
601 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
607 cpp_warning (pfile, "#include_next in primary source file");
609 _cpp_execute_include (pfile, &header, 0, search_start);
612 /* Subroutine of do_line. Read next token from PFILE without adding it to
613 the output buffer. If it is a number between 1 and 4, store it in *NUM
614 and return 1; otherwise, return 0 and complain if we aren't at the end
618 read_line_number (pfile, num)
625 _cpp_lex_token (pfile, &token);
626 if (token.type == CPP_NUMBER && token.val.str.len == 1)
628 val = token.val.str.text[0] - '1';
636 if (token.type != CPP_EOF)
637 cpp_error (pfile, "invalid format #line");
641 /* Another subroutine of do_line. Convert a number in STR, of length
642 LEN, to binary; store it in NUMP, and return 0 if the number was
643 well-formed, 1 if not. Temporary, hopefully. */
645 strtoul_for_line (str, len, nump)
650 unsigned long reg = 0;
664 /* Interpret #line command.
665 Note that the filename string (if any) is treated as if it were an
666 include filename. That means no escape handling. */
672 cpp_buffer *ip = CPP_BUFFER (pfile);
673 unsigned long new_lineno;
674 /* C99 raised the minimum limit on #line numbers. */
675 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
676 int enter = 0, leave = 0, rename = 0;
679 /* #line commands expand macros. */
680 _cpp_get_token (pfile, &token);
681 if (token.type != CPP_NUMBER
682 || strtoul_for_line (token.val.str.text, token.val.str.len, &new_lineno))
684 cpp_error (pfile, "\"%s\" after #line is not a positive integer",
685 cpp_token_as_text (pfile, &token));
689 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
690 cpp_pedwarn (pfile, "line number out of range");
692 _cpp_get_token (pfile, &token);
694 if (token.type != CPP_EOF)
698 int action_number = 0;
700 if (token.type != CPP_STRING)
702 cpp_error (pfile, "\"%s\" is not a valid filename",
703 cpp_token_as_text (pfile, &token));
707 len = token.val.str.len;
708 fname = alloca (len + 1);
709 memcpy (fname, token.val.str.text, len);
712 if (strcmp (fname, ip->nominal_fname))
715 if (!strcmp (fname, ip->inc->name))
716 ip->nominal_fname = ip->inc->name;
718 ip->nominal_fname = _cpp_fake_include (pfile, fname);
721 if (read_line_number (pfile, &action_number) != 0)
723 if (CPP_PEDANTIC (pfile))
724 cpp_pedwarn (pfile, "extra tokens at end of #line directive");
726 if (action_number == 1)
729 cpp_make_system_header (pfile, ip, 0);
730 read_line_number (pfile, &action_number);
732 else if (action_number == 2)
735 cpp_make_system_header (pfile, ip, 0);
736 read_line_number (pfile, &action_number);
738 if (action_number == 3)
740 cpp_make_system_header (pfile, ip, 1);
741 read_line_number (pfile, &action_number);
743 if (action_number == 4)
745 cpp_make_system_header (pfile, ip, 2);
746 read_line_number (pfile, &action_number);
752 /* Our line number is incremented after the directive is processed. */
753 ip->lineno = new_lineno - 1;
754 pfile->lexer_pos.output_line = ip->lineno;
755 if (enter && pfile->cb.enter_file)
756 (*pfile->cb.enter_file) (pfile);
757 if (leave && pfile->cb.leave_file)
758 (*pfile->cb.leave_file) (pfile);
759 if (rename && pfile->cb.rename_file)
760 (*pfile->cb.rename_file) (pfile);
764 * Report a warning or error detected by the program we are
765 * processing. Use the directive's tokens in the error message.
769 do_diagnostic (pfile, code)
771 enum error_type code;
773 if (_cpp_begin_message (pfile, code, NULL, 0))
775 fprintf (stderr, "#%s ", pfile->directive->name);
776 pfile->state.prevent_expansion++;
777 cpp_output_line (pfile, stderr);
778 pfile->state.prevent_expansion--;
786 do_diagnostic (pfile, ERROR);
793 do_diagnostic (pfile, WARNING);
796 /* Report program identification. */
804 _cpp_get_token (pfile, &str);
805 if (str.type != CPP_STRING)
806 cpp_error (pfile, "invalid #ident");
807 else if (pfile->cb.ident)
808 (*pfile->cb.ident) (pfile, &str.val.str);
813 /* Pragmata handling. We handle some of these, and pass the rest on
814 to the front end. C99 defines three pragmas and says that no macro
815 expansion is to be performed on them; whether or not macro
816 expansion happens for other pragmas is implementation defined.
817 This implementation never macro-expands the text after #pragma.
819 We currently do not support the _Pragma operator. Support for that
820 has to be coordinated with the front end. Proposed implementation:
821 both #pragma blah blah and _Pragma("blah blah") become
822 __builtin_pragma(blah blah) and we teach the parser about that. */
824 /* Sub-handlers for the pragmas needing treatment here.
825 They return 1 if the token buffer is to be popped, 0 if not. */
828 struct pragma_entry *next;
833 void (*handler) PARAMS ((cpp_reader *));
834 struct pragma_entry *space;
839 cpp_register_pragma (pfile, space, name, handler)
843 void (*handler) PARAMS ((cpp_reader *));
845 struct pragma_entry **x, *new;
851 struct pragma_entry *p = pfile->pragmas;
852 len = strlen (space);
855 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
862 cpp_ice (pfile, "unknown #pragma namespace %s", space);
867 new = xnew (struct pragma_entry);
869 new->len = strlen (name);
871 new->u.handler = handler;
878 cpp_register_pragma_space (pfile, space)
882 struct pragma_entry *new;
883 const struct pragma_entry *p = pfile->pragmas;
884 size_t len = strlen (space);
888 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
889 /* Multiple different callers are allowed to register the same
895 new = xnew (struct pragma_entry);
901 new->next = pfile->pragmas;
902 pfile->pragmas = new;
906 _cpp_init_internal_pragmas (pfile)
910 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
911 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
914 cpp_register_pragma_space (pfile, "GCC");
916 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
917 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
918 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
925 const struct pragma_entry *p;
927 const cpp_hashnode *node;
933 pfile->state.prevent_expansion++;
934 cpp_start_lookahead (pfile);
937 cpp_get_token (pfile, &tok);
938 if (tok.type == CPP_NAME)
945 if (strlen (p->name) == len && !memcmp (p->name, name, len))
954 (*p->u.handler) (pfile);
963 cpp_stop_lookahead (pfile, drop);
964 pfile->state.prevent_expansion--;
966 if (!drop && pfile->cb.def_pragma)
967 (*pfile->cb.def_pragma) (pfile);
971 do_pragma_once (pfile)
974 cpp_buffer *ip = CPP_BUFFER (pfile);
976 cpp_warning (pfile, "#pragma once is obsolete");
978 if (CPP_PREV_BUFFER (ip) == NULL)
979 cpp_warning (pfile, "#pragma once in main file");
981 ip->inc->cmacro = NEVER_REREAD;
987 do_pragma_poison (pfile)
990 /* Poison these symbols so that all subsequent usage produces an
995 pfile->state.poisoned_ok = 1;
998 _cpp_lex_token (pfile, &tok);
999 if (tok.type == CPP_EOF)
1001 if (tok.type != CPP_NAME)
1003 cpp_error (pfile, "invalid #pragma GCC poison directive");
1008 if (hp->flags & NODE_POISONED)
1011 if (hp->type == NT_MACRO)
1012 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
1013 _cpp_free_definition (hp);
1014 hp->flags |= NODE_POISONED | NODE_DIAGNOSTIC;
1016 pfile->state.poisoned_ok = 0;
1018 #if 0 /* Doesn't quite work yet. */
1019 if (tok.type == CPP_EOF && pfile->cb.poison)
1020 (*pfile->cb.poison) (pfile);
1024 /* Mark the current header as a system header. This will suppress
1025 some categories of warnings (notably those from -pedantic). It is
1026 intended for use in system libraries that cannot be implemented in
1027 conforming C, but cannot be certain that their headers appear in a
1028 system include directory. To prevent abuse, it is rejected in the
1029 primary source file. */
1031 do_pragma_system_header (pfile)
1034 cpp_buffer *ip = CPP_BUFFER (pfile);
1035 if (CPP_PREV_BUFFER (ip) == NULL)
1036 cpp_warning (pfile, "#pragma system_header outside include file");
1038 cpp_make_system_header (pfile, ip, 1);
1043 /* Check the modified date of the current include file against a specified
1044 file. Issue a diagnostic, if the specified file is newer. We use this to
1045 determine if a fixed header should be refixed. */
1047 do_pragma_dependency (pfile)
1050 cpp_token header, msg;
1053 if (parse_include (pfile, &header))
1056 ordering = _cpp_compare_file_date (pfile, &header);
1058 cpp_warning (pfile, "cannot find source %s",
1059 cpp_token_as_text (pfile, &header));
1060 else if (ordering > 0)
1062 cpp_warning (pfile, "current file is older than %s",
1063 cpp_token_as_text (pfile, &header));
1064 cpp_start_lookahead (pfile);
1065 cpp_get_token (pfile, &msg);
1066 cpp_stop_lookahead (pfile, msg.type == CPP_EOF);
1067 if (msg.type != CPP_EOF && _cpp_begin_message (pfile, WARNING, NULL, 0))
1068 cpp_output_line (pfile, stderr);
1072 /* Just ignore #sccs, on systems where we define it at all. */
1073 #ifdef SCCS_DIRECTIVE
1076 cpp_reader *pfile ATTRIBUTE_UNUSED;
1087 if (! pfile->skipping)
1089 const cpp_hashnode *node = lex_macro_node (pfile);
1092 skip = node->type != NT_MACRO;
1095 push_conditional (pfile, skip, T_IFDEF, 0);
1103 const cpp_hashnode *node = 0;
1105 if (! pfile->skipping)
1107 node = lex_macro_node (pfile);
1109 skip = node->type == NT_MACRO;
1112 push_conditional (pfile, skip, T_IFNDEF, node);
1115 /* #if cooperates with parse_defined to handle multiple-include
1116 optimisations. If macro expansions or identifiers appear in the
1117 expression, we cannot treat it as a controlling conditional, since
1118 their values could change in the future. */
1125 const cpp_hashnode *cmacro = 0;
1127 if (!pfile->skipping)
1129 /* Controlling macro of #if ! defined () */
1130 pfile->mi_ind_cmacro = 0;
1131 skip = _cpp_parse_expr (pfile) == 0;
1132 cmacro = pfile->mi_ind_cmacro;
1135 push_conditional (pfile, skip, T_IF, cmacro);
1138 /* #else flips pfile->skipping and continues without changing
1139 if_stack; this is so that the error message for missing #endif's
1140 etc. will point to the original #if. */
1146 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1149 cpp_error (pfile, "#else without #if");
1152 if (ifs->type == T_ELSE)
1154 cpp_error (pfile, "#else after #else");
1155 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1156 "the conditional began here");
1159 /* Invalidate any controlling macro. */
1163 if (! ifs->was_skipping)
1165 /* If pfile->skipping is 2, one of the blocks in an #if
1166 #elif ... chain succeeded, so we skip the else block. */
1167 if (pfile->skipping < 2)
1168 pfile->skipping = ! pfile->skipping;
1175 /* handle a #elif directive by not changing if_stack either. see the
1176 comment above do_else. */
1182 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1186 cpp_error (pfile, "#elif without #if");
1190 if (ifs->type == T_ELSE)
1192 cpp_error (pfile, "#elif after #else");
1193 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1194 "the conditional began here");
1197 /* Invalidate any controlling macro. */
1201 if (ifs->was_skipping)
1202 return; /* Don't evaluate a nested #if */
1204 if (pfile->skipping != 1)
1206 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1210 pfile->skipping = ! _cpp_parse_expr (pfile);
1213 /* #endif pops the if stack and resets pfile->skipping. */
1219 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1222 cpp_error (pfile, "#endif without #if");
1225 /* If potential control macro, we go back outside again. */
1226 if (ifs->next == 0 && ifs->mi_cmacro)
1228 pfile->mi_state = MI_OUTSIDE;
1229 pfile->mi_cmacro = ifs->mi_cmacro;
1232 CPP_BUFFER (pfile)->if_stack = ifs->next;
1233 pfile->skipping = ifs->was_skipping;
1234 obstack_free (pfile->buffer_ob, ifs);
1240 /* Push an if_stack entry and set pfile->skipping accordingly.
1241 If this is a #ifndef starting at the beginning of a file,
1242 CMACRO is the macro name tested by the #ifndef. */
1245 push_conditional (pfile, skip, type, cmacro)
1249 const cpp_hashnode *cmacro;
1251 struct if_stack *ifs;
1253 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1254 ifs->pos = pfile->directive_pos;
1255 ifs->next = CPP_BUFFER (pfile)->if_stack;
1256 ifs->was_skipping = pfile->skipping;
1258 if (pfile->mi_state == MI_OUTSIDE && pfile->mi_cmacro == 0)
1259 ifs->mi_cmacro = cmacro;
1263 if (!pfile->skipping)
1264 pfile->skipping = skip;
1266 CPP_BUFFER (pfile)->if_stack = ifs;
1269 /* Called when we reach the end of a file. Walk back up the
1270 conditional stack till we reach its level at entry to this file,
1271 issuing error messages. Then force skipping off. */
1273 unwind_if_stack (pfile, pbuf)
1277 struct if_stack *ifs;
1279 /* No need to free stack - they'll all go away with the buffer. */
1280 for (ifs = pbuf->if_stack; ifs; ifs = ifs->next)
1281 cpp_error_with_line (pfile, ifs->pos.line, ifs->pos.col,
1282 "unterminated #%s", dtable[ifs->type].name);
1284 pfile->skipping = 0;
1287 /* Read the tokens of the answer into the macro pool. Only commit the
1288 memory if we intend it as permanent storage, i.e. the #assert case.
1289 Returns 0 on success. */
1292 parse_answer (pfile, answerp, type)
1294 struct answer **answerp;
1297 cpp_token paren, *token;
1298 struct answer *answer;
1300 if (POOL_FRONT (&pfile->macro_pool) + sizeof (struct answer) >
1301 POOL_LIMIT (&pfile->macro_pool))
1302 _cpp_next_chunk (&pfile->macro_pool, sizeof (struct answer), 0);
1303 answer = (struct answer *) POOL_FRONT (&pfile->macro_pool);
1306 /* In a conditional, it is legal to not have an open paren. We
1307 should save the following token in this case. */
1309 cpp_start_lookahead (pfile);
1310 cpp_get_token (pfile, &paren);
1312 cpp_stop_lookahead (pfile, paren.type == CPP_OPEN_PAREN);
1314 /* If not a paren, see if we're OK. */
1315 if (paren.type != CPP_OPEN_PAREN)
1317 /* In a conditional no answer is a test for any answer. It
1318 could be followed by any token. */
1322 /* #unassert with no answer is valid - it removes all answers. */
1323 if (type == T_UNASSERT && paren.type == CPP_EOF)
1326 cpp_error (pfile, "missing '(' after predicate");
1332 token = &answer->first[answer->count];
1333 /* Check we have room for the token. */
1334 if ((unsigned char *) (token + 1) >= POOL_LIMIT (&pfile->macro_pool))
1336 _cpp_next_chunk (&pfile->macro_pool, sizeof (cpp_token),
1337 (unsigned char **) &answer);
1338 token = &answer->first[answer->count];
1341 _cpp_get_token (pfile, token);
1342 if (token->type == CPP_CLOSE_PAREN)
1345 if (token->type == CPP_EOF)
1347 cpp_error (pfile, "missing ')' to complete answer");
1353 if (answer->count == 0)
1355 cpp_error (pfile, "predicate's answer is empty");
1359 /* Drop whitespace at start. */
1360 answer->first->flags &= ~PREV_WHITE;
1363 if (type == T_ASSERT || type == T_UNASSERT)
1368 /* Parses an assertion, returning a pointer to the hash node of the
1369 predicate, or 0 on error. If an answer was supplied, it is placed
1370 in ANSWERP, otherwise it is set to 0. We use _cpp_get_raw_token,
1371 since we cannot assume tokens are consecutive in a #if statement
1372 (we may be in a macro), and we don't want to macro expand. */
1373 static cpp_hashnode *
1374 parse_assertion (pfile, answerp, type)
1376 struct answer **answerp;
1379 cpp_hashnode *result = 0;
1380 cpp_token predicate;
1382 /* We don't expand predicates or answers. */
1383 pfile->state.prevent_expansion++;
1385 /* Use the permanent pool for storage (for the answers). */
1386 pfile->string_pool = &pfile->ident_pool;
1389 _cpp_get_token (pfile, &predicate);
1390 if (predicate.type == CPP_EOF)
1391 cpp_error (pfile, "assertion without predicate");
1392 else if (predicate.type != CPP_NAME)
1393 cpp_error (pfile, "predicate must be an identifier");
1394 else if (parse_answer (pfile, answerp, type) == 0)
1396 unsigned int len = predicate.val.node->length;
1397 unsigned char *sym = alloca (len + 1);
1399 /* Prefix '#' to get it out of macro namespace. */
1401 memcpy (sym + 1, predicate.val.node->name, len);
1402 result = cpp_lookup (pfile, sym, len + 1);
1405 pfile->string_pool = &pfile->temp_string_pool;
1406 pfile->state.prevent_expansion--;
1410 /* Returns a pointer to the pointer to the answer in the answer chain,
1411 or a pointer to NULL if the answer is not in the chain. */
1412 static struct answer **
1413 find_answer (node, candidate)
1415 const struct answer *candidate;
1418 struct answer **result;
1420 for (result = &node->value.answers; *result; result = &(*result)->next)
1422 struct answer *answer = *result;
1424 if (answer->count == candidate->count)
1426 for (i = 0; i < answer->count; i++)
1427 if (! _cpp_equiv_tokens (&answer->first[i], &candidate->first[i]))
1430 if (i == answer->count)
1438 /* Test an assertion within a preprocessor conditional. Returns
1439 non-zero on failure, zero on success. On success, the result of
1440 the test is written into VALUE. */
1442 _cpp_test_assertion (pfile, value)
1446 struct answer *answer;
1449 node = parse_assertion (pfile, &answer, T_IF);
1451 *value = (node->type == NT_ASSERTION &&
1452 (answer == 0 || *find_answer (node, answer) != 0));
1454 /* We don't commit the memory for the answer - it's temporary only. */
1462 struct answer *new_answer;
1465 node = parse_assertion (pfile, &new_answer, T_ASSERT);
1468 /* Place the new answer in the answer list. First check there
1469 is not a duplicate. */
1470 new_answer->next = 0;
1471 if (node->type == NT_ASSERTION)
1473 if (*find_answer (node, new_answer))
1475 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1478 new_answer->next = node->value.answers;
1480 node->type = NT_ASSERTION;
1481 node->value.answers = new_answer;
1482 POOL_COMMIT (&pfile->macro_pool, (sizeof (struct answer)
1483 + (new_answer->count - 1)
1484 * sizeof (cpp_token)));
1493 struct answer *answer;
1495 node = parse_assertion (pfile, &answer, T_UNASSERT);
1496 /* It isn't an error to #unassert something that isn't asserted. */
1497 if (node && node->type == NT_ASSERTION)
1501 struct answer **p = find_answer (node, answer), *temp;
1503 /* Remove the answer from the list. */
1508 /* Did we free the last answer? */
1509 if (node->value.answers == 0)
1510 node->type = NT_VOID;
1513 _cpp_free_definition (node);
1516 /* We don't commit the memory for the answer - it's temporary only. */
1519 /* These are for -D, -U, -A. */
1521 /* Process the string STR as if it appeared as the body of a #define.
1522 If STR is just an identifier, define it with value 1.
1523 If STR has anything after the identifier, then it should
1524 be identifier=definition. */
1527 cpp_define (pfile, str)
1534 p = strchr (str, '=');
1535 /* Copy the entire option so we can modify it.
1536 Change the first "=" in the string to a space. If there is none,
1537 tack " 1" on the end. Then add a newline and a NUL. */
1541 count = strlen (str) + 2;
1542 buf = (char *) alloca (count);
1543 memcpy (buf, str, count - 2);
1545 buf[count - 2] = '\n';
1546 buf[count - 1] = '\0';
1550 count = strlen (str) + 4;
1551 buf = (char *) alloca (count);
1552 memcpy (buf, str, count - 4);
1553 strcpy (&buf[count-4], " 1\n");
1556 run_directive (pfile, T_DEFINE, buf, count - 1, 0);
1559 /* Slight variant of the above for use by initialize_builtins, which (a)
1560 knows how to set up the buffer itself, (b) needs a different "filename"
1563 _cpp_define_builtin (pfile, str)
1567 run_directive (pfile, T_DEFINE, str, strlen (str), _("<builtin>"));
1570 /* Process MACRO as if it appeared as the body of an #undef. */
1572 cpp_undef (pfile, macro)
1576 run_directive (pfile, T_UNDEF, macro, strlen (macro), 0);
1579 /* Process the string STR as if it appeared as the body of a #assert. */
1581 cpp_assert (pfile, str)
1585 run_directive (pfile, T_ASSERT, str, strlen (str), 0);
1588 /* Process STR as if it appeared as the body of an #unassert. */
1590 cpp_unassert (pfile, str)
1594 run_directive (pfile, T_UNASSERT, str, strlen (str), 0);
1597 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1599 cpp_defined (pfile, id, len)
1604 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1606 /* If it's of type NT_MACRO, it cannot be poisoned. */
1607 return hp->type == NT_MACRO;
1610 /* Allocate a new cpp_buffer for PFILE, and push it on the input
1611 buffer stack. If BUFFER != NULL, then use the LENGTH characters in
1612 BUFFER as the new input buffer. Return the new buffer, or NULL on
1616 cpp_push_buffer (pfile, buffer, length)
1618 const U_CHAR *buffer;
1621 cpp_buffer *buf = CPP_BUFFER (pfile);
1623 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1625 cpp_fatal (pfile, "#include nested too deeply");
1629 if (pfile->context->prev)
1631 cpp_ice (pfile, "buffer pushed with contexts stacked");
1632 skip_rest_of_line (pfile);
1635 new = xobnew (pfile->buffer_ob, cpp_buffer);
1636 /* Clears, amongst other things, if_stack and mi_cmacro. */
1637 memset (new, 0, sizeof (cpp_buffer));
1639 pfile->lexer_pos.output_line = 1;
1640 new->line_base = new->buf = new->cur = buffer;
1641 new->rlimit = buffer + length;
1644 /* No read ahead or extra char initially. */
1645 new->read_ahead = EOF;
1646 new->extra_char = EOF;
1647 pfile->state.skip_newlines = 1;
1649 CPP_BUFFER (pfile) = new;
1654 cpp_pop_buffer (pfile)
1658 cpp_buffer *buf = CPP_BUFFER (pfile);
1660 unwind_if_stack (pfile, buf);
1661 wfb = (buf->inc != 0);
1663 _cpp_pop_file_buffer (pfile, buf);
1665 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1666 obstack_free (pfile->buffer_ob, buf);
1667 pfile->buffer_stack_depth--;
1669 if (CPP_BUFFER (pfile) && wfb && pfile->cb.leave_file)
1670 (*pfile->cb.leave_file) (pfile);
1672 return CPP_BUFFER (pfile);
1675 #define obstack_chunk_alloc xmalloc
1676 #define obstack_chunk_free free
1678 _cpp_init_stacks (pfile)
1684 pfile->buffer_ob = xnew (struct obstack);
1685 obstack_init (pfile->buffer_ob);
1687 /* Register the directives. */
1688 for (i = 1; i < N_DIRECTIVES; i++)
1690 node = cpp_lookup (pfile, dtable[i - 1].name, dtable[i - 1].length);
1691 node->directive_index = i;
1696 _cpp_cleanup_stacks (pfile)
1699 obstack_free (pfile->buffer_ob, 0);
1700 free (pfile->buffer_ob);