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 /* Stack of conditionals currently in progress
32 (including both successful and failing conditionals). */
36 struct if_stack *next;
37 unsigned int lineno; /* line number where condition started */
38 unsigned int colno; /* and column */
39 int was_skipping; /* value of pfile->skipping before this if */
40 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
41 int type; /* type of last directive seen in this group */
44 /* Forward declarations. */
46 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
47 static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int,
48 const U_CHAR **, unsigned int *,
50 static void push_conditional PARAMS ((cpp_reader *, int, int,
51 const cpp_hashnode *));
52 static int read_line_number PARAMS ((cpp_reader *, int *));
53 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
56 static const cpp_hashnode *
57 parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
58 static const cpp_hashnode *
59 detect_if_not_defined PARAMS ((cpp_reader *));
61 get_define_node PARAMS ((cpp_reader *));
62 static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
65 #define str_match(sym, len, str) \
66 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
68 /* This is the table of directive handlers. It is ordered by
69 frequency of occurrence; the numbers at the end are directive
70 counts from all the source code I have lying around (egcs and libc
71 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
74 The entries with a dash and a name after the count are extensions,
75 of which all but #warning and #include_next are deprecated. The name
76 is where the extension appears to have come from. */
78 /* #sccs is not always recognized. */
80 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
82 # define SCCS_ENTRY /* nothing */
85 #define DIRECTIVE_TABLE \
86 D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
87 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
88 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
89 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
90 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
91 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
92 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
93 D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
94 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
95 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
96 D(error, T_ERROR, STDC89, 0) /* 475 */ \
97 D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
98 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
99 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
100 D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
101 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
102 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
103 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
104 SCCS_ENTRY /* 0 SVR2? */
106 /* Use the table to generate a series of prototypes, an enum for the
107 directive names, and an array of directive handlers. */
109 /* The directive-processing functions are declared to return int
110 instead of void, because some old compilers have trouble with
111 pointers to functions returning void. */
113 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
114 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
118 #define D(n, tag, o, f) tag,
126 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
127 #define D(name, t, origin, flags) \
128 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
129 sizeof STRINGX(name) - 1, origin, flags },
130 static const struct directive dtable[] =
135 #undef DIRECTIVE_TABLE
137 /* Check if a token's name matches that of a known directive. Put in
138 this file to save exporting dtable and other unneeded information. */
139 const struct directive *
140 _cpp_check_directive (pfile, token, bol)
142 const cpp_token *token;
147 /* If we are rescanning preprocessed input, don't obey any directives
149 if (CPP_OPTION (pfile, preprocessed))
152 for (i = 0; i < N_DIRECTIVES; i++)
153 if (pfile->spec_nodes->dirs[i] == token->val.node)
155 /* In -traditional mode, a directive is ignored unless its #
156 is in column 1. In code intended to work with K+R compilers,
157 therefore, directives added by C89 must have their # indented,
158 and directives present in traditional C must not. This is true
159 even of directives in skipped conditional blocks. */
160 if (CPP_WTRADITIONAL (pfile))
162 if (!bol && dtable[i].origin == KANDR)
164 "traditional C ignores #%s with the # indented",
167 if (bol && dtable[i].origin != KANDR)
169 "suggest hiding #%s from traditional C with an indented #",
173 /* If we are skipping a failed conditional group, all non-conditional
174 directives are ignored. */
175 if (pfile->skipping && !(dtable[i].flags & COND))
178 /* Issue -pedantic warnings for extended directives. */
179 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
180 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
188 const struct directive *
189 _cpp_check_linemarker (pfile, token, bol)
191 const cpp_token *token ATTRIBUTE_UNUSED;
194 /* # followed by a number is equivalent to #line. Do not recognize
195 this form in assembly language source files or skipped
196 conditional groups. Complain about this form if we're being
197 pedantic, but not if this is regurgitated input (preprocessed or
198 fed back in by the C++ frontend). */
199 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
202 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
203 && ! CPP_OPTION (pfile, preprocessed))
204 cpp_pedwarn (pfile, "# followed by integer");
206 /* In -traditional mode, a directive is ignored unless its #
208 if (!bol && CPP_WTRADITIONAL (pfile))
209 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
210 dtable[T_LINE].name);
212 return &dtable[T_LINE];
215 static cpp_hashnode *
216 get_define_node (pfile)
219 const cpp_token *token;
221 /* Skip any -C comments. */
222 while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
225 /* The token immediately after #define must be an identifier. That
226 identifier is not allowed to be "defined". See predefined macro
227 names (6.10.8.4). In C++, it is not allowed to be any of the
228 <iso646.h> macro names (which are keywords in C++) either. */
230 if (token->type != CPP_NAME)
232 if (token->type == CPP_DEFINED)
233 cpp_error_with_line (pfile, token->line, token->col,
234 "\"defined\" cannot be used as a macro name");
235 else if (token->flags & NAMED_OP)
236 cpp_error_with_line (pfile, token->line, token->col,
237 "\"%s\" cannot be used as a macro name in C++",
238 token->val.node->name);
240 cpp_error_with_line (pfile, token->line, token->col,
241 "macro names must be identifiers");
245 /* In Objective C, some keywords begin with '@', but general identifiers
246 do not, and you're not allowed to #define them. */
247 if (token->val.node->name[0] == '@')
249 cpp_error_with_line (pfile, token->line, token->col,
250 "\"%s\" cannot be used as a macro name",
251 token->val.node->name);
255 /* Check for poisoned identifiers now. */
256 if (token->val.node->type == T_POISON)
258 cpp_error_with_line (pfile, token->line, token->col,
259 "attempt to use poisoned \"%s\"",
260 token->val.node->name);
264 return token->val.node;
267 /* Process a #define command. */
274 if ((node = get_define_node (pfile)))
275 if (_cpp_create_definition (pfile, node))
276 if (pfile->cb.define)
277 (*pfile->cb.define) (pfile, node);
280 /* Remove the definition of a symbol from the symbol table. */
285 cpp_hashnode *node = get_define_node (pfile);
287 if (_cpp_get_token (pfile)->type != CPP_EOF)
288 cpp_pedwarn (pfile, "junk on line after #undef");
290 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
291 is not currently defined as a macro name. */
292 if (node && node->type != T_VOID)
295 (*pfile->cb.undef) (pfile, node);
297 if (node->type != T_MACRO)
298 cpp_warning (pfile, "undefining \"%s\"", node->name);
300 _cpp_free_definition (node);
306 /* Handle #include and #import. */
309 parse_include (pfile, dir, trail, strp, lenp, abp)
317 const cpp_token *name = _cpp_get_token (pfile);
319 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
321 if (name->type == CPP_LESS)
322 name = _cpp_glue_header_name (pfile);
325 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
329 if (name->val.str.len == 0)
331 cpp_error (pfile, "empty file name in #%s", dir);
335 if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
336 cpp_error (pfile, "junk at end of #%s", dir);
338 *lenp = name->val.str.len;
339 *strp = name->val.str.text;
340 *abp = (name->type == CPP_HEADER_NAME);
342 if (pfile->cb.include)
343 (*pfile->cb.include) (pfile, dir, *strp, *lenp, *abp);
355 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
358 _cpp_execute_include (pfile, str, len, 0, 0, ab);
369 if (CPP_OPTION (pfile, warn_import)
370 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
372 pfile->import_warning = 1;
374 "#import is obsolete, use an #ifndef wrapper in the header file");
377 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
380 _cpp_execute_include (pfile, str, len, 1, 0, ab);
384 do_include_next (pfile)
389 struct file_name_list *search_start = 0;
392 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
395 /* For #include_next, skip in the search path past the dir in which
396 the current file was found. If this is the last directory in the
397 search path, don't include anything. If the current file was
398 specified with an absolute path, use the normal search logic. If
399 this is the primary source file, use the normal search logic and
400 generate a warning. */
401 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
403 if (CPP_BUFFER (pfile)->inc->foundhere)
405 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
411 cpp_warning (pfile, "#include_next in primary source file");
413 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
416 /* Subroutine of do_line. Read next token from PFILE without adding it to
417 the output buffer. If it is a number between 1 and 4, store it in *NUM
418 and return 1; otherwise, return 0 and complain if we aren't at the end
422 read_line_number (pfile, num)
426 const cpp_token *tok = _cpp_get_token (pfile);
427 enum cpp_ttype type = tok->type;
428 const U_CHAR *p = tok->val.str.text;
429 unsigned int len = tok->val.str.len;
431 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
439 cpp_error (pfile, "invalid format #line");
444 /* Another subroutine of do_line. Convert a number in STR, of length
445 LEN, to binary; store it in NUMP, and return 0 if the number was
446 well-formed, 1 if not. Temporary, hopefully. */
448 strtoul_for_line (str, len, nump)
453 unsigned long reg = 0;
467 /* Interpret #line command.
468 Note that the filename string (if any) is treated as if it were an
469 include filename. That means no escape handling. */
475 cpp_buffer *ip = CPP_BUFFER (pfile);
476 unsigned long new_lineno, old_lineno;
477 /* C99 raised the minimum limit on #line numbers. */
478 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
479 int action_number = 0;
480 int enter = 0, leave = 0, rename = 0;
485 const cpp_token *tok;
487 tok = _cpp_get_token (pfile);
489 str = tok->val.str.text;
490 len = tok->val.str.len;
492 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
494 cpp_error (pfile, "token after #line is not a positive integer");
498 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
499 cpp_pedwarn (pfile, "line number out of range");
501 old_lineno = ip->lineno;
502 ip->lineno = new_lineno;
503 tok = _cpp_get_token (pfile);
505 str = tok->val.str.text;
506 len = tok->val.str.len;
510 else if (type != CPP_STRING)
512 cpp_error (pfile, "second token after #line is not a string");
513 ip->lineno = old_lineno; /* malformed #line should have no effect */
517 fname = alloca (len + 1);
518 memcpy (fname, str, len);
521 if (strcmp (fname, ip->nominal_fname))
524 if (!strcmp (fname, ip->inc->name))
525 ip->nominal_fname = ip->inc->name;
527 ip->nominal_fname = _cpp_fake_include (pfile, fname);
530 if (read_line_number (pfile, &action_number) == 0)
533 if (CPP_PEDANTIC (pfile))
534 cpp_pedwarn (pfile, "garbage at end of #line");
536 if (action_number == 1)
539 cpp_make_system_header (pfile, ip, 0);
540 read_line_number (pfile, &action_number);
542 else if (action_number == 2)
545 cpp_make_system_header (pfile, ip, 0);
546 read_line_number (pfile, &action_number);
548 if (action_number == 3)
550 cpp_make_system_header (pfile, ip, 1);
551 read_line_number (pfile, &action_number);
553 if (action_number == 4)
555 cpp_make_system_header (pfile, ip, 2);
556 read_line_number (pfile, &action_number);
560 if (enter && pfile->cb.enter_file)
561 (*pfile->cb.enter_file) (pfile);
562 if (leave && pfile->cb.leave_file)
563 (*pfile->cb.leave_file) (pfile);
564 if (rename && pfile->cb.rename_file)
565 (*pfile->cb.rename_file) (pfile);
569 * Report an error detected by the program we are processing.
570 * Use the text of the line in the error message.
571 * (We use error because it prints the filename & line#.)
578 if (_cpp_begin_message (pfile, ERROR, NULL, 0, 0))
580 cpp_output_list (pfile, stderr, &pfile->token_list,
581 pfile->first_directive_token);
587 * Report a warning detected by the program we are processing.
588 * Use the text of the line in the warning message, then continue.
595 if (_cpp_begin_message (pfile, WARNING, NULL, 0, 0))
597 cpp_output_list (pfile, stderr, &pfile->token_list,
598 pfile->first_directive_token);
603 /* Report program identification. */
609 const cpp_token *str = _cpp_get_token (pfile);
611 if (str->type == CPP_STRING && _cpp_get_token (pfile)->type == CPP_EOF)
614 (*pfile->cb.ident) (pfile, str->val.str.text, str->val.str.len);
618 cpp_error (pfile, "invalid #ident");
621 /* Pragmata handling. We handle some of these, and pass the rest on
622 to the front end. C99 defines three pragmas and says that no macro
623 expansion is to be performed on them; whether or not macro
624 expansion happens for other pragmas is implementation defined.
625 This implementation never macro-expands the text after #pragma.
627 We currently do not support the _Pragma operator. Support for that
628 has to be coordinated with the front end. Proposed implementation:
629 both #pragma blah blah and _Pragma("blah blah") become
630 __builtin_pragma(blah blah) and we teach the parser about that. */
632 /* Sub-handlers for the pragmas needing treatment here.
633 They return 1 if the token buffer is to be popped, 0 if not. */
636 struct pragma_entry *next;
641 void (*handler) PARAMS ((cpp_reader *));
642 struct pragma_entry *space;
647 cpp_register_pragma (pfile, space, name, handler)
651 void (*handler) PARAMS ((cpp_reader *));
653 struct pragma_entry **x, *new;
659 struct pragma_entry *p = pfile->pragmas;
660 len = strlen (space);
663 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
670 cpp_ice (pfile, "unknown #pragma namespace %s", space);
675 new = xnew (struct pragma_entry);
677 new->len = strlen (name);
679 new->u.handler = handler;
686 cpp_register_pragma_space (pfile, space)
690 struct pragma_entry *new;
691 const struct pragma_entry *p = pfile->pragmas;
692 size_t len = strlen (space);
696 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
698 cpp_ice (pfile, "#pragma namespace %s already registered", space);
704 new = xnew (struct pragma_entry);
710 new->next = pfile->pragmas;
711 pfile->pragmas = new;
714 static void do_pragma_once PARAMS ((cpp_reader *));
715 static void do_pragma_poison PARAMS ((cpp_reader *));
716 static void do_pragma_system_header PARAMS ((cpp_reader *));
717 static void do_pragma_dependency PARAMS ((cpp_reader *));
720 _cpp_init_internal_pragmas (pfile)
724 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
725 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
728 cpp_register_pragma_space (pfile, "GCC");
730 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
731 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
732 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
739 const struct pragma_entry *p;
740 const cpp_token *tok;
741 const cpp_hashnode *node;
748 tok = _cpp_get_token (pfile);
749 if (tok->type == CPP_EOF)
752 if (tok->type != CPP_NAME)
754 cpp_error (pfile, "malformed #pragma directive");
758 node = tok->val.node;
763 if (strlen (p->name) == len && !memcmp (p->name, name, len))
772 (*p->u.handler) (pfile);
779 if (pfile->cb.def_pragma)
780 (*pfile->cb.def_pragma) (pfile);
784 do_pragma_once (pfile)
787 cpp_buffer *ip = CPP_BUFFER (pfile);
789 /* Allow #pragma once in system headers, since that's not the user's
791 if (!CPP_IN_SYSTEM_HEADER (pfile))
792 cpp_warning (pfile, "#pragma once is obsolete");
794 if (CPP_PREV_BUFFER (ip) == NULL)
795 cpp_warning (pfile, "#pragma once outside include file");
797 ip->inc->cmacro = NEVER_REREAD;
801 do_pragma_poison (pfile)
804 /* Poison these symbols so that all subsequent usage produces an
806 const cpp_token *tok;
811 tok = _cpp_get_token (pfile);
812 if (tok->type == CPP_EOF)
814 if (tok->type != CPP_NAME)
816 cpp_error (pfile, "invalid #pragma poison directive");
821 if (hp->type == T_POISON)
822 ; /* It is allowed to poison the same identifier twice. */
825 if (hp->type != T_VOID)
826 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
827 _cpp_free_definition (hp);
832 if (pfile->cb.poison)
833 (*pfile->cb.poison) (pfile);
836 /* Mark the current header as a system header. This will suppress
837 some categories of warnings (notably those from -pedantic). It is
838 intended for use in system libraries that cannot be implemented in
839 conforming C, but cannot be certain that their headers appear in a
840 system include directory. To prevent abuse, it is rejected in the
841 primary source file. */
843 do_pragma_system_header (pfile)
846 cpp_buffer *ip = CPP_BUFFER (pfile);
847 if (CPP_PREV_BUFFER (ip) == NULL)
848 cpp_warning (pfile, "#pragma system_header outside include file");
850 cpp_make_system_header (pfile, ip, 1);
853 /* Check the modified date of the current include file against a specified
854 file. Issue a diagnostic, if the specified file is newer. We use this to
855 determine if a fixed header should be refixed. */
857 do_pragma_dependency (pfile)
865 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
868 left = ab ? '<' : '"';
869 right = ab ? '>' : '"';
871 ordering = _cpp_compare_file_date (pfile, name, len, ab);
873 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
874 else if (ordering > 0)
876 const cpp_token *msg = _cpp_get_token (pfile);
878 cpp_warning (pfile, "current file is older than %c%.*s%c",
879 left, (int)len, name, right);
880 if (msg->type != CPP_EOF
881 && _cpp_begin_message (pfile, WARNING, NULL, msg->line, msg->col))
883 cpp_output_list (pfile, stderr, &pfile->token_list, msg);
889 /* Just ignore #sccs, on systems where we define it at all. */
890 #ifdef SCCS_DIRECTIVE
893 cpp_reader *pfile ATTRIBUTE_UNUSED;
898 /* We've found an `#if' directive. If the only thing before it in
899 this file is white space, and if it is of the form
900 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
901 for inclusion of this file. (See redundant_include_p in cppfiles.c
902 for an explanation of controlling macros.) If so, return the
903 hash node for SYMBOL. Otherwise, return NULL. */
905 static const cpp_hashnode *
906 detect_if_not_defined (pfile)
909 const cpp_token *token;
910 cpp_hashnode *cmacro = 0;
912 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
913 token = pfile->first_directive_token + 2;
915 if (token->type != CPP_NOT)
919 if (token->type != CPP_DEFINED)
923 if (token->type == CPP_OPEN_PAREN)
926 if (token->type != CPP_NAME)
929 cmacro = token->val.node;
931 if (token[-1].type == CPP_OPEN_PAREN)
934 if (token->type != CPP_CLOSE_PAREN)
939 if (token->type != CPP_EOF)
945 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
946 macro being tested, and issues various error messages. */
948 static const cpp_hashnode *
949 parse_ifdef (pfile, name)
954 const cpp_hashnode *node = 0;
956 const cpp_token *token = _cpp_get_token (pfile);
960 cpp_pedwarn (pfile, "#%s with no argument", name);
961 else if (type != CPP_NAME)
962 cpp_pedwarn (pfile, "#%s with invalid argument", name);
963 else if (_cpp_get_token (pfile)->type != CPP_EOF)
964 cpp_pedwarn (pfile, "garbage at end of #%s", name);
966 if (type == CPP_NAME)
967 node = token->val.node;
968 if (node && node->type == T_POISON)
970 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
978 /* #ifdef is dead simple. */
984 const cpp_hashnode *node = 0;
986 if (! pfile->skipping)
987 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
989 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
992 /* #ifndef is a tad more complex, because we need to check for a
993 no-reinclusion wrapper. */
999 int start_of_file = 0;
1000 const cpp_hashnode *node = 0;
1002 if (! pfile->skipping)
1004 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
1005 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1008 push_conditional (pfile, node && node->type != T_VOID,
1009 T_IFNDEF, start_of_file ? node : 0);
1012 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1013 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1019 const cpp_hashnode *cmacro = 0;
1022 if (! pfile->skipping)
1024 if (pfile->token_list.flags & BEG_OF_FILE)
1025 cmacro = detect_if_not_defined (pfile);
1026 value = _cpp_parse_expr (pfile);
1028 push_conditional (pfile, value == 0, T_IF, cmacro);
1031 /* #else flips pfile->skipping and continues without changing
1032 if_stack; this is so that the error message for missing #endif's
1033 etc. will point to the original #if. */
1039 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1040 validate_else (pfile, dtable[T_ELSE].name);
1044 cpp_error (pfile, "#else without #if");
1047 if (ifs->type == T_ELSE)
1049 cpp_error (pfile, "#else after #else");
1050 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1051 "the conditional began here");
1054 /* #ifndef can't have its special treatment for containing the whole file
1055 if it has a #else clause. */
1058 if (! ifs->was_skipping)
1060 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1061 succeeded, so we mustn't do the else block. */
1062 if (pfile->skipping < 2)
1063 pfile->skipping = ! pfile->skipping;
1068 * handle a #elif directive by not changing if_stack either.
1069 * see the comment above do_else.
1076 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1080 cpp_error (pfile, "#elif without #if");
1083 if (ifs->type == T_ELSE)
1085 cpp_error (pfile, "#elif after #else");
1086 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1087 "the conditional began here");
1091 if (ifs->was_skipping)
1092 return; /* Don't evaluate a nested #if */
1094 if (pfile->skipping != 1)
1096 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1100 pfile->skipping = ! _cpp_parse_expr (pfile);
1103 /* #endif pops the if stack and resets pfile->skipping. */
1109 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1111 validate_else (pfile, dtable[T_ENDIF].name);
1114 cpp_error (pfile, "#endif without #if");
1117 CPP_BUFFER (pfile)->if_stack = ifs->next;
1118 pfile->skipping = ifs->was_skipping;
1119 pfile->potential_control_macro = ifs->cmacro;
1120 obstack_free (pfile->buffer_ob, ifs);
1125 /* Push an if_stack entry and set pfile->skipping accordingly.
1126 If this is a #ifndef starting at the beginning of a file,
1127 CMACRO is the macro name tested by the #ifndef. */
1130 push_conditional (pfile, skip, type, cmacro)
1134 const cpp_hashnode *cmacro;
1136 struct if_stack *ifs;
1138 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1139 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1140 ifs->next = CPP_BUFFER (pfile)->if_stack;
1141 ifs->cmacro = cmacro;
1142 ifs->was_skipping = pfile->skipping;
1145 if (!pfile->skipping)
1146 pfile->skipping = skip;
1148 CPP_BUFFER (pfile)->if_stack = ifs;
1151 /* Issue -pedantic warning for text which is not a comment following
1152 an #else or #endif. */
1155 validate_else (pfile, directive)
1157 const U_CHAR *directive;
1159 if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
1160 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1163 /* Called when we reach the end of a file. Walk back up the
1164 conditional stack till we reach its level at entry to this file,
1165 issuing error messages. Then force skipping off. */
1167 unwind_if_stack (pfile, pbuf)
1171 struct if_stack *ifs, *nifs;
1173 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1175 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1176 dtable[ifs->type].name);
1178 /* No need to free - they'll all go away with the buffer. */
1180 pfile->skipping = 0;
1183 /* Parses an assertion, returning a pointer to the hash node of the
1184 predicate, or 0 on error. If an answer was supplied, it is
1185 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1186 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1187 in a #if statement (we may be in a macro), and we don't want to
1190 _cpp_parse_assertion (pfile, answerp)
1192 struct answer **answerp;
1194 struct answer *answer = 0;
1197 const cpp_token *token, *predicate;
1198 const struct directive *d = pfile->token_list.directive;
1199 unsigned int len = 0;
1201 predicate = _cpp_get_raw_token (pfile);
1202 if (predicate->type == CPP_EOF)
1204 cpp_error (pfile, "assertion without predicate");
1207 else if (predicate->type != CPP_NAME)
1209 cpp_error (pfile, "predicate must be an identifier");
1213 token = _cpp_get_raw_token (pfile);
1214 if (token->type != CPP_OPEN_PAREN)
1216 /* #unassert and #if are OK without predicate. */
1217 if (d == &dtable[T_UNASSERT])
1219 if (token->type == CPP_EOF)
1222 else if (d != &dtable[T_ASSERT])
1224 _cpp_push_token (pfile, token);
1227 cpp_error (pfile, "missing '(' after predicate");
1231 /* Allocate a struct answer, and copy the answer to it. */
1232 answer = (struct answer *) xmalloc (sizeof (struct answer));
1233 list = &answer->list;
1234 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1240 token = _cpp_get_raw_token (pfile);
1242 if (token->type == CPP_EOF)
1244 cpp_error (pfile, "missing ')' to complete answer");
1247 if (token->type == CPP_CLOSE_PAREN)
1250 /* Copy the token. */
1251 _cpp_expand_token_space (list, 1);
1252 dest = &list->tokens[list->tokens_used++];
1255 if (TOKEN_SPELL (token) == SPELL_STRING)
1257 _cpp_expand_name_space (list, token->val.str.len);
1258 dest->val.str.text = list->namebuf + list->name_used;
1259 memcpy (list->namebuf + list->name_used,
1260 token->val.str.text, token->val.str.len);
1261 list->name_used += token->val.str.len;
1265 if (list->tokens_used == 0)
1267 cpp_error (pfile, "predicate's answer is empty");
1271 /* Drop whitespace at start. */
1272 list->tokens[0].flags &= ~PREV_WHITE;
1274 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1275 && token[1].type != CPP_EOF)
1277 cpp_error (pfile, "junk at end of assertion");
1283 len = predicate->val.node->length;
1284 sym = alloca (len + 1);
1286 /* Prefix '#' to get it out of macro namespace. */
1288 memcpy (sym + 1, predicate->val.node->name, len);
1289 return cpp_lookup (pfile, sym, len + 1);
1292 FREE_ANSWER (answer);
1296 /* Returns a pointer to the pointer to the answer in the answer chain,
1297 or a pointer to NULL if the answer is not in the chain. */
1299 _cpp_find_answer (node, candidate)
1301 const cpp_toklist *candidate;
1303 struct answer **result;
1305 for (result = &node->value.answers; *result; result = &(*result)->next)
1306 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1312 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1313 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1314 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1319 struct answer *new_answer;
1322 node = _cpp_parse_assertion (pfile, &new_answer);
1325 new_answer->next = 0;
1326 new_answer->list.line = pfile->token_list.line;
1327 new_answer->list.file = pfile->token_list.file;
1329 if (node->type == T_ASSERTION)
1331 if (*_cpp_find_answer (node, &new_answer->list))
1333 new_answer->next = node->value.answers;
1335 node->type = T_ASSERTION;
1336 node->value.answers = new_answer;
1341 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1342 FREE_ANSWER (new_answer);
1350 struct answer *answer, *temp, *next;
1352 node = _cpp_parse_assertion (pfile, &answer);
1355 /* It isn't an error to #unassert something that isn't asserted. */
1356 if (node->type == T_ASSERTION)
1360 struct answer **p = _cpp_find_answer (node, &answer->list);
1368 if (node->value.answers == 0)
1369 node->type = T_VOID;
1373 for (temp = node->value.answers; temp; temp = next)
1378 node->type = T_VOID;
1383 FREE_ANSWER (answer);
1387 /* These are for -D, -U, -A. */
1389 /* Process the string STR as if it appeared as the body of a #define.
1390 If STR is just an identifier, define it with value 1.
1391 If STR has anything after the identifier, then it should
1392 be identifier=definition. */
1395 cpp_define (pfile, str)
1402 p = strchr (str, '=');
1403 /* Copy the entire option so we can modify it.
1404 Change the first "=" in the string to a space. If there is none,
1405 tack " 1" on the end. Then add a newline and a NUL. */
1409 count = strlen (str) + 2;
1410 buf = (char *) alloca (count);
1411 memcpy (buf, str, count - 2);
1413 buf[count - 2] = '\n';
1414 buf[count - 1] = '\0';
1418 count = strlen (str) + 4;
1419 buf = (char *) alloca (count);
1420 memcpy (buf, str, count - 4);
1421 strcpy (&buf[count-4], " 1\n");
1424 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
1427 /* Process MACRO as if it appeared as the body of an #undef. */
1429 cpp_undef (pfile, macro)
1433 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
1436 /* Process the string STR as if it appeared as the body of a #assert. */
1438 cpp_assert (pfile, str)
1442 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
1445 /* Process STR as if it appeared as the body of an #unassert. */
1447 cpp_unassert (pfile, str)
1451 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
1454 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1456 cpp_defined (pfile, id, len)
1461 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1462 if (hp->type == T_POISON)
1464 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1467 return (hp->type != T_VOID);
1470 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1471 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1472 as the new input buffer.
1473 Return the new buffer, or NULL on failure. */
1476 cpp_push_buffer (pfile, buffer, length)
1478 const U_CHAR *buffer;
1481 cpp_buffer *buf = CPP_BUFFER (pfile);
1483 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1485 cpp_fatal (pfile, "#include nested too deep");
1488 if (pfile->cur_context > 0)
1490 cpp_ice (pfile, "buffer pushed with contexts stacked");
1491 _cpp_skip_rest_of_line (pfile);
1494 new = xobnew (pfile->buffer_ob, cpp_buffer);
1495 memset (new, 0, sizeof (cpp_buffer));
1497 new->line_base = new->buf = new->cur = buffer;
1498 new->rlimit = buffer + length;
1501 CPP_BUFFER (pfile) = new;
1506 cpp_pop_buffer (pfile)
1510 cpp_buffer *buf = CPP_BUFFER (pfile);
1512 unwind_if_stack (pfile, buf);
1513 wfb = (buf->inc != 0);
1515 _cpp_pop_file_buffer (pfile, buf);
1517 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1518 obstack_free (pfile->buffer_ob, buf);
1519 pfile->buffer_stack_depth--;
1521 if (wfb && pfile->cb.leave_file && CPP_BUFFER (pfile))
1522 (*pfile->cb.leave_file) (pfile);
1524 return CPP_BUFFER (pfile);
1527 #define obstack_chunk_alloc xmalloc
1528 #define obstack_chunk_free free
1529 #define DSC(x) U x, sizeof x - 1
1531 _cpp_init_stacks (pfile)
1535 struct spec_nodes *s;
1537 pfile->buffer_ob = xnew (struct obstack);
1538 obstack_init (pfile->buffer_ob);
1540 /* Perhaps not the ideal place to put this. */
1541 pfile->spec_nodes = s = xnew (struct spec_nodes);
1542 s->n_L = cpp_lookup (pfile, DSC("L"));
1543 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1544 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1545 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1546 for (i = 0; i < N_DIRECTIVES; i++)
1547 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1551 _cpp_cleanup_stacks (pfile)
1554 obstack_free (pfile->buffer_ob, 0);
1555 free (pfile->buffer_ob);