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,
55 static void do_diagnostic PARAMS ((cpp_reader *, enum error_type));
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 *));
64 /* This is the table of directive handlers. It is ordered by
65 frequency of occurrence; the numbers at the end are directive
66 counts from all the source code I have lying around (egcs and libc
67 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
70 The entries with a dash and a name after the count are extensions,
71 of which all but #warning and #include_next are deprecated. The name
72 is where the extension appears to have come from. */
74 /* #sccs is not always recognized. */
76 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
78 # define SCCS_ENTRY /* nothing */
81 #define DIRECTIVE_TABLE \
82 D(define, T_DEFINE = 0, KANDR, COMMENTS | IN_I)/* 270554 */ \
83 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
84 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
85 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
86 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
87 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
88 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
89 D(undef, T_UNDEF, KANDR, IN_I) /* 4837 */ \
90 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
91 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
92 D(error, T_ERROR, STDC89, 0) /* 475 */ \
93 D(pragma, T_PRAGMA, STDC89, IN_I) /* 195 */ \
94 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
95 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
96 D(ident, T_IDENT, EXTENSION, IN_I) /* 11 SVR4 */ \
97 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
98 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
99 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
100 SCCS_ENTRY /* 0 SVR2? */
102 /* Use the table to generate a series of prototypes, an enum for the
103 directive names, and an array of directive handlers. */
105 /* The directive-processing functions are declared to return int
106 instead of void, because some old compilers have trouble with
107 pointers to functions returning void. */
109 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
110 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
114 #define D(n, tag, o, f) tag,
122 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
123 #define D(name, t, origin, flags) \
124 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
125 sizeof STRINGX(name) - 1, origin, flags },
126 static const struct directive dtable[] =
131 #undef DIRECTIVE_TABLE
133 /* Check if a token's name matches that of a known directive. Put in
134 this file to save exporting dtable and other unneeded information. */
135 const struct directive *
136 _cpp_check_directive (pfile, token)
138 const cpp_token *token;
142 if (token->type != CPP_NAME)
144 if (token->type == CPP_EOF && CPP_WTRADITIONAL (pfile)
145 && pfile->state.indented)
146 cpp_warning (pfile, "traditional C ignores #\\n with the # indented");
151 for (i = 0; i < N_DIRECTIVES; i++)
152 if (pfile->spec_nodes->dirs[i] == token->val.node)
155 if (i == N_DIRECTIVES)
158 /* We should lex headers correctly, regardless of whether we're
160 pfile->state.angled_headers = dtable[i].flags & INCL;
162 /* If we are rescanning preprocessed input, only directives tagged
163 with IN_I are honored, and the warnings below are suppressed. */
164 if (CPP_OPTION (pfile, preprocessed))
166 if (!dtable[i].flags & IN_I)
171 /* Traditionally, a directive is ignored unless its # is in
172 column 1. Therefore in code intended to work with K+R
173 compilers, directives added by C89 must have their #
174 indented, and directives present in traditional C must not.
175 This is true even of directives in skipped conditional
177 if (CPP_WTRADITIONAL (pfile))
179 if (pfile->state.indented && dtable[i].origin == KANDR)
181 "traditional C ignores #%s with the # indented",
184 else if (!pfile->state.indented && dtable[i].origin != KANDR)
186 "suggest hiding #%s from traditional C with an indented #",
190 /* If we are skipping a failed conditional group, all non-conditional
191 directives are ignored. */
192 if (pfile->skipping && !(dtable[i].flags & COND))
195 /* Issue -pedantic warnings for extended directives. */
196 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
197 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
200 /* Only flag to save comments if we process the directive. */
201 pfile->state.save_comments = (! CPP_OPTION (pfile, discard_comments)
202 && (dtable[i].flags & COMMENTS));
207 const struct directive *
208 _cpp_check_linemarker (pfile, token)
210 const cpp_token *token ATTRIBUTE_UNUSED;
212 /* # followed by a number is equivalent to #line. Do not recognize
213 this form in assembly language source files or skipped
214 conditional groups. Complain about this form if we're being
215 pedantic, but not if this is regurgitated input (preprocessed or
216 fed back in by the C++ frontend). */
217 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
220 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
221 && ! CPP_OPTION (pfile, preprocessed))
222 cpp_pedwarn (pfile, "# followed by integer");
224 /* In -traditional mode, a directive is ignored unless its #
226 if (pfile->state.indented && CPP_WTRADITIONAL (pfile))
227 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
228 dtable[T_LINE].name);
230 return &dtable[T_LINE];
233 static cpp_hashnode *
234 get_define_node (pfile)
237 const cpp_token *token;
239 /* Skip any -C comments. */
240 while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
243 /* The token immediately after #define must be an identifier. That
244 identifier is not allowed to be "defined". See predefined macro
245 names (6.10.8.4). In C++, it is not allowed to be any of the
246 <iso646.h> macro names (which are keywords in C++) either. */
248 if (token->type != CPP_NAME)
250 if (token->type == CPP_DEFINED)
251 cpp_error_with_line (pfile, token->line, token->col,
252 "\"defined\" cannot be used as a macro name");
253 else if (token->flags & NAMED_OP)
254 cpp_error_with_line (pfile, token->line, token->col,
255 "\"%s\" cannot be used as a macro name in C++",
256 token->val.node->name);
258 cpp_error_with_line (pfile, token->line, token->col,
259 "macro names must be identifiers");
263 /* In Objective C, some keywords begin with '@', but general identifiers
264 do not, and you're not allowed to #define them. */
265 if (token->val.node->name[0] == '@')
267 cpp_error_with_line (pfile, token->line, token->col,
268 "\"%s\" cannot be used as a macro name",
269 token->val.node->name);
273 /* Check for poisoned identifiers now. */
274 if (token->val.node->type == T_POISON)
276 cpp_error_with_line (pfile, token->line, token->col,
277 "attempt to use poisoned \"%s\"",
278 token->val.node->name);
282 return token->val.node;
285 /* Process a #define command. */
292 if ((node = get_define_node (pfile)))
293 if (_cpp_create_definition (pfile, node))
294 if (pfile->cb.define)
295 (*pfile->cb.define) (pfile, node);
298 /* Remove the definition of a symbol from the symbol table. */
303 cpp_hashnode *node = get_define_node (pfile);
305 if (_cpp_get_token (pfile)->type != CPP_EOF)
306 cpp_pedwarn (pfile, "junk on line after #undef");
308 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
309 is not currently defined as a macro name. */
310 if (node && node->type != T_VOID)
313 (*pfile->cb.undef) (pfile, node);
315 if (node->type != T_MACRO)
316 cpp_warning (pfile, "undefining \"%s\"", node->name);
318 _cpp_free_definition (node);
323 /* Handle #include and #import. */
326 parse_include (pfile, dir, trail, strp, lenp, abp)
334 const cpp_token *name = _cpp_get_token (pfile);
336 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
338 if (name->type == CPP_LESS)
339 name = _cpp_glue_header_name (pfile);
342 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
346 if (name->val.str.len == 0)
348 cpp_error (pfile, "empty file name in #%s", dir);
352 if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
353 cpp_error (pfile, "junk at end of #%s", dir);
355 *lenp = name->val.str.len;
356 *strp = name->val.str.text;
357 *abp = (name->type == CPP_HEADER_NAME);
359 if (pfile->cb.include)
360 (*pfile->cb.include) (pfile, dir, *strp, *lenp, *abp);
372 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
375 _cpp_execute_include (pfile, str, len, 0, 0, ab);
386 if (CPP_OPTION (pfile, warn_import)
387 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
389 pfile->import_warning = 1;
391 "#import is obsolete, use an #ifndef wrapper in the header file");
394 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
397 _cpp_execute_include (pfile, str, len, 1, 0, ab);
401 do_include_next (pfile)
406 struct file_name_list *search_start = 0;
409 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
412 /* For #include_next, skip in the search path past the dir in which
413 the current file was found. If this is the last directory in the
414 search path, don't include anything. If the current file was
415 specified with an absolute path, use the normal search logic. If
416 this is the primary source file, use the normal search logic and
417 generate a warning. */
418 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
420 if (CPP_BUFFER (pfile)->inc->foundhere)
422 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
428 cpp_warning (pfile, "#include_next in primary source file");
430 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
433 /* Subroutine of do_line. Read next token from PFILE without adding it to
434 the output buffer. If it is a number between 1 and 4, store it in *NUM
435 and return 1; otherwise, return 0 and complain if we aren't at the end
439 read_line_number (pfile, num)
443 const cpp_token *tok = _cpp_get_token (pfile);
444 enum cpp_ttype type = tok->type;
445 const U_CHAR *p = tok->val.str.text;
446 unsigned int len = tok->val.str.len;
448 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
456 cpp_error (pfile, "invalid format #line");
461 /* Another subroutine of do_line. Convert a number in STR, of length
462 LEN, to binary; store it in NUMP, and return 0 if the number was
463 well-formed, 1 if not. Temporary, hopefully. */
465 strtoul_for_line (str, len, nump)
470 unsigned long reg = 0;
484 /* Interpret #line command.
485 Note that the filename string (if any) is treated as if it were an
486 include filename. That means no escape handling. */
492 cpp_buffer *ip = CPP_BUFFER (pfile);
493 unsigned long new_lineno, old_lineno;
494 /* C99 raised the minimum limit on #line numbers. */
495 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
496 int action_number = 0;
497 int enter = 0, leave = 0, rename = 0;
502 const cpp_token *tok;
504 tok = _cpp_get_token (pfile);
506 str = tok->val.str.text;
507 len = tok->val.str.len;
509 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
511 cpp_error (pfile, "token after #line is not a positive integer");
515 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
516 cpp_pedwarn (pfile, "line number out of range");
518 old_lineno = ip->lineno;
519 ip->lineno = new_lineno;
520 tok = _cpp_get_token (pfile);
522 str = tok->val.str.text;
523 len = tok->val.str.len;
527 else if (type != CPP_STRING)
529 cpp_error (pfile, "second token after #line is not a string");
530 ip->lineno = old_lineno; /* malformed #line should have no effect */
534 fname = alloca (len + 1);
535 memcpy (fname, str, len);
538 if (strcmp (fname, ip->nominal_fname))
541 if (!strcmp (fname, ip->inc->name))
542 ip->nominal_fname = ip->inc->name;
544 ip->nominal_fname = _cpp_fake_include (pfile, fname);
547 if (read_line_number (pfile, &action_number) == 0)
550 if (CPP_PEDANTIC (pfile))
551 cpp_pedwarn (pfile, "garbage at end of #line");
553 if (action_number == 1)
556 cpp_make_system_header (pfile, ip, 0);
557 read_line_number (pfile, &action_number);
559 else if (action_number == 2)
562 cpp_make_system_header (pfile, ip, 0);
563 read_line_number (pfile, &action_number);
565 if (action_number == 3)
567 cpp_make_system_header (pfile, ip, 1);
568 read_line_number (pfile, &action_number);
570 if (action_number == 4)
572 cpp_make_system_header (pfile, ip, 2);
573 read_line_number (pfile, &action_number);
577 if (enter && pfile->cb.enter_file)
578 (*pfile->cb.enter_file) (pfile);
579 if (leave && pfile->cb.leave_file)
580 (*pfile->cb.leave_file) (pfile);
581 if (rename && pfile->cb.rename_file)
582 (*pfile->cb.rename_file) (pfile);
586 * Report a warning or error detected by the program we are
587 * processing. Use the directive's tokens in the error message.
591 do_diagnostic (pfile, code)
593 enum error_type code;
595 if (_cpp_begin_message (pfile, code, NULL, 0, 0))
597 cpp_output_list (pfile, stderr, &pfile->token_list,
598 pfile->first_directive_token);
607 do_diagnostic (pfile, ERROR);
614 do_diagnostic (pfile, WARNING);
617 /* Report program identification. */
623 const cpp_token *str = _cpp_get_token (pfile);
625 if (str->type == CPP_STRING && _cpp_get_token (pfile)->type == CPP_EOF)
628 (*pfile->cb.ident) (pfile, str->val.str.text, str->val.str.len);
632 cpp_error (pfile, "invalid #ident");
635 /* Pragmata handling. We handle some of these, and pass the rest on
636 to the front end. C99 defines three pragmas and says that no macro
637 expansion is to be performed on them; whether or not macro
638 expansion happens for other pragmas is implementation defined.
639 This implementation never macro-expands the text after #pragma.
641 We currently do not support the _Pragma operator. Support for that
642 has to be coordinated with the front end. Proposed implementation:
643 both #pragma blah blah and _Pragma("blah blah") become
644 __builtin_pragma(blah blah) and we teach the parser about that. */
646 /* Sub-handlers for the pragmas needing treatment here.
647 They return 1 if the token buffer is to be popped, 0 if not. */
650 struct pragma_entry *next;
655 void (*handler) PARAMS ((cpp_reader *));
656 struct pragma_entry *space;
661 cpp_register_pragma (pfile, space, name, handler)
665 void (*handler) PARAMS ((cpp_reader *));
667 struct pragma_entry **x, *new;
673 struct pragma_entry *p = pfile->pragmas;
674 len = strlen (space);
677 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
684 cpp_ice (pfile, "unknown #pragma namespace %s", space);
689 new = xnew (struct pragma_entry);
691 new->len = strlen (name);
693 new->u.handler = handler;
700 cpp_register_pragma_space (pfile, space)
704 struct pragma_entry *new;
705 const struct pragma_entry *p = pfile->pragmas;
706 size_t len = strlen (space);
710 if (p->isnspace && p->len == len && !memcmp (p->name, space, len))
711 /* Multiple different callers are allowed to register the same
717 new = xnew (struct pragma_entry);
723 new->next = pfile->pragmas;
724 pfile->pragmas = new;
727 static void do_pragma_once PARAMS ((cpp_reader *));
728 static void do_pragma_poison PARAMS ((cpp_reader *));
729 static void do_pragma_system_header PARAMS ((cpp_reader *));
730 static void do_pragma_dependency PARAMS ((cpp_reader *));
733 _cpp_init_internal_pragmas (pfile)
737 cpp_register_pragma (pfile, 0, "poison", do_pragma_poison);
738 cpp_register_pragma (pfile, 0, "once", do_pragma_once);
741 cpp_register_pragma_space (pfile, "GCC");
743 cpp_register_pragma (pfile, "GCC", "poison", do_pragma_poison);
744 cpp_register_pragma (pfile, "GCC", "system_header", do_pragma_system_header);
745 cpp_register_pragma (pfile, "GCC", "dependency", do_pragma_dependency);
752 const struct pragma_entry *p;
753 const cpp_token *tok;
754 const cpp_hashnode *node;
761 tok = _cpp_get_token (pfile);
762 if (tok->type == CPP_EOF)
765 if (tok->type != CPP_NAME)
767 cpp_error (pfile, "malformed #pragma directive");
771 node = tok->val.node;
776 if (strlen (p->name) == len && !memcmp (p->name, name, len))
785 (*p->u.handler) (pfile);
792 if (pfile->cb.def_pragma)
793 (*pfile->cb.def_pragma) (pfile);
797 do_pragma_once (pfile)
800 cpp_buffer *ip = CPP_BUFFER (pfile);
802 /* Allow #pragma once in system headers, since that's not the user's
804 if (!CPP_IN_SYSTEM_HEADER (pfile))
805 cpp_warning (pfile, "#pragma once is obsolete");
807 if (CPP_PREV_BUFFER (ip) == NULL)
808 cpp_warning (pfile, "#pragma once outside include file");
810 ip->inc->cmacro = NEVER_REREAD;
814 do_pragma_poison (pfile)
817 /* Poison these symbols so that all subsequent usage produces an
819 const cpp_token *tok;
824 tok = _cpp_get_token (pfile);
825 if (tok->type == CPP_EOF)
827 if (tok->type != CPP_NAME)
829 cpp_error (pfile, "invalid #pragma poison directive");
834 if (hp->type == T_POISON)
835 ; /* It is allowed to poison the same identifier twice. */
838 if (hp->type != T_VOID)
839 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
840 _cpp_free_definition (hp);
845 if (pfile->cb.poison)
846 (*pfile->cb.poison) (pfile);
849 /* Mark the current header as a system header. This will suppress
850 some categories of warnings (notably those from -pedantic). It is
851 intended for use in system libraries that cannot be implemented in
852 conforming C, but cannot be certain that their headers appear in a
853 system include directory. To prevent abuse, it is rejected in the
854 primary source file. */
856 do_pragma_system_header (pfile)
859 cpp_buffer *ip = CPP_BUFFER (pfile);
860 if (CPP_PREV_BUFFER (ip) == NULL)
861 cpp_warning (pfile, "#pragma system_header outside include file");
863 cpp_make_system_header (pfile, ip, 1);
866 /* Check the modified date of the current include file against a specified
867 file. Issue a diagnostic, if the specified file is newer. We use this to
868 determine if a fixed header should be refixed. */
870 do_pragma_dependency (pfile)
878 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
881 left = ab ? '<' : '"';
882 right = ab ? '>' : '"';
884 ordering = _cpp_compare_file_date (pfile, name, len, ab);
886 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
887 else if (ordering > 0)
889 const cpp_token *msg = _cpp_get_token (pfile);
891 cpp_warning (pfile, "current file is older than %c%.*s%c",
892 left, (int)len, name, right);
893 if (msg->type != CPP_EOF
894 && _cpp_begin_message (pfile, WARNING, NULL, msg->line, msg->col))
896 cpp_output_list (pfile, stderr, &pfile->token_list, msg);
902 /* Just ignore #sccs, on systems where we define it at all. */
903 #ifdef SCCS_DIRECTIVE
906 cpp_reader *pfile ATTRIBUTE_UNUSED;
911 /* We've found an `#if' directive. If the only thing before it in
912 this file is white space, and if it is of the form
913 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
914 for inclusion of this file. (See redundant_include_p in cppfiles.c
915 for an explanation of controlling macros.) If so, return the
916 hash node for SYMBOL. Otherwise, return NULL. */
918 static const cpp_hashnode *
919 detect_if_not_defined (pfile)
922 const cpp_token *token;
923 cpp_hashnode *cmacro = 0;
925 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
926 token = pfile->first_directive_token + 2;
928 if (token->type != CPP_NOT)
932 if (token->type != CPP_DEFINED)
936 if (token->type == CPP_OPEN_PAREN)
939 if (token->type != CPP_NAME)
942 cmacro = token->val.node;
944 if (token[-1].type == CPP_OPEN_PAREN)
947 if (token->type != CPP_CLOSE_PAREN)
952 if (token->type != CPP_EOF)
958 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
959 macro being tested, and issues various error messages. */
961 static const cpp_hashnode *
962 parse_ifdef (pfile, name)
967 const cpp_hashnode *node = 0;
969 const cpp_token *token = _cpp_get_token (pfile);
973 cpp_pedwarn (pfile, "#%s with no argument", name);
974 else if (type != CPP_NAME)
975 cpp_pedwarn (pfile, "#%s with invalid argument", name);
976 else if (_cpp_get_token (pfile)->type != CPP_EOF)
977 cpp_pedwarn (pfile, "garbage at end of #%s", name);
979 if (type == CPP_NAME)
980 node = token->val.node;
981 if (node && node->type == T_POISON)
983 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
991 /* #ifdef is dead simple. */
997 const cpp_hashnode *node = 0;
999 if (! pfile->skipping)
1000 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1002 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
1005 /* #ifndef is a tad more complex, because we need to check for a
1006 no-reinclusion wrapper. */
1012 int start_of_file = 0;
1013 const cpp_hashnode *node = 0;
1015 if (! pfile->skipping)
1017 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
1018 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1021 push_conditional (pfile, node && node->type != T_VOID,
1022 T_IFNDEF, start_of_file ? node : 0);
1025 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1026 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1032 const cpp_hashnode *cmacro = 0;
1035 if (! pfile->skipping)
1037 if (pfile->token_list.flags & BEG_OF_FILE)
1038 cmacro = detect_if_not_defined (pfile);
1039 value = _cpp_parse_expr (pfile);
1041 push_conditional (pfile, value == 0, T_IF, cmacro);
1044 /* #else flips pfile->skipping and continues without changing
1045 if_stack; this is so that the error message for missing #endif's
1046 etc. will point to the original #if. */
1052 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1053 validate_else (pfile, dtable[T_ELSE].name);
1057 cpp_error (pfile, "#else without #if");
1060 if (ifs->type == T_ELSE)
1062 cpp_error (pfile, "#else after #else");
1063 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1064 "the conditional began here");
1067 /* #ifndef can't have its special treatment for containing the whole file
1068 if it has a #else clause. */
1071 if (! ifs->was_skipping)
1073 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1074 succeeded, so we mustn't do the else block. */
1075 if (pfile->skipping < 2)
1076 pfile->skipping = ! pfile->skipping;
1081 * handle a #elif directive by not changing if_stack either.
1082 * see the comment above do_else.
1089 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1093 cpp_error (pfile, "#elif without #if");
1096 if (ifs->type == T_ELSE)
1098 cpp_error (pfile, "#elif after #else");
1099 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1100 "the conditional began here");
1104 if (ifs->was_skipping)
1105 return; /* Don't evaluate a nested #if */
1107 if (pfile->skipping != 1)
1109 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1113 pfile->skipping = ! _cpp_parse_expr (pfile);
1116 /* #endif pops the if stack and resets pfile->skipping. */
1122 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1124 validate_else (pfile, dtable[T_ENDIF].name);
1127 cpp_error (pfile, "#endif without #if");
1130 CPP_BUFFER (pfile)->if_stack = ifs->next;
1131 pfile->skipping = ifs->was_skipping;
1132 pfile->potential_control_macro = ifs->cmacro;
1133 obstack_free (pfile->buffer_ob, ifs);
1138 /* Push an if_stack entry and set pfile->skipping accordingly.
1139 If this is a #ifndef starting at the beginning of a file,
1140 CMACRO is the macro name tested by the #ifndef. */
1143 push_conditional (pfile, skip, type, cmacro)
1147 const cpp_hashnode *cmacro;
1149 struct if_stack *ifs;
1151 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1152 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1153 ifs->next = CPP_BUFFER (pfile)->if_stack;
1154 ifs->cmacro = cmacro;
1155 ifs->was_skipping = pfile->skipping;
1158 if (!pfile->skipping)
1159 pfile->skipping = skip;
1161 CPP_BUFFER (pfile)->if_stack = ifs;
1164 /* Issue -pedantic warning for text which is not a comment following
1165 an #else or #endif. */
1168 validate_else (pfile, directive)
1170 const U_CHAR *directive;
1172 if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
1173 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1176 /* Called when we reach the end of a file. Walk back up the
1177 conditional stack till we reach its level at entry to this file,
1178 issuing error messages. Then force skipping off. */
1180 unwind_if_stack (pfile, pbuf)
1184 struct if_stack *ifs, *nifs;
1186 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1188 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1189 dtable[ifs->type].name);
1191 /* No need to free - they'll all go away with the buffer. */
1193 pfile->skipping = 0;
1196 /* Parses an assertion, returning a pointer to the hash node of the
1197 predicate, or 0 on error. If an answer was supplied, it is
1198 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1199 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1200 in a #if statement (we may be in a macro), and we don't want to
1203 _cpp_parse_assertion (pfile, answerp)
1205 struct answer **answerp;
1207 struct answer *answer = 0;
1210 const cpp_token *token, *predicate;
1211 const struct directive *d = pfile->token_list.directive;
1212 unsigned int len = 0;
1214 predicate = _cpp_get_raw_token (pfile);
1215 if (predicate->type == CPP_EOF)
1217 cpp_error (pfile, "assertion without predicate");
1220 else if (predicate->type != CPP_NAME)
1222 cpp_error (pfile, "predicate must be an identifier");
1226 token = _cpp_get_raw_token (pfile);
1227 if (token->type != CPP_OPEN_PAREN)
1229 /* #unassert and #if are OK without predicate. */
1230 if (d == &dtable[T_UNASSERT])
1232 if (token->type == CPP_EOF)
1235 else if (d != &dtable[T_ASSERT])
1237 _cpp_push_token (pfile, token);
1240 cpp_error (pfile, "missing '(' after predicate");
1244 /* Allocate a struct answer, and copy the answer to it. */
1245 answer = (struct answer *) xmalloc (sizeof (struct answer));
1246 list = &answer->list;
1247 _cpp_init_toklist (list, 1); /* Empty. */
1253 token = _cpp_get_raw_token (pfile);
1255 if (token->type == CPP_EOF)
1257 cpp_error (pfile, "missing ')' to complete answer");
1260 if (token->type == CPP_CLOSE_PAREN)
1263 /* Copy the token. */
1264 _cpp_expand_token_space (list, 1);
1265 dest = &list->tokens[list->tokens_used++];
1268 if (TOKEN_SPELL (token) == SPELL_STRING)
1270 _cpp_expand_name_space (list, token->val.str.len);
1271 dest->val.str.text = list->namebuf + list->name_used;
1272 memcpy (list->namebuf + list->name_used,
1273 token->val.str.text, token->val.str.len);
1274 list->name_used += token->val.str.len;
1278 if (list->tokens_used == 0)
1280 cpp_error (pfile, "predicate's answer is empty");
1284 /* Drop whitespace at start. */
1285 list->tokens[0].flags &= ~PREV_WHITE;
1287 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1288 && token[1].type != CPP_EOF)
1290 cpp_error (pfile, "junk at end of assertion");
1296 len = predicate->val.node->length;
1297 sym = alloca (len + 1);
1299 /* Prefix '#' to get it out of macro namespace. */
1301 memcpy (sym + 1, predicate->val.node->name, len);
1302 return cpp_lookup (pfile, sym, len + 1);
1305 FREE_ANSWER (answer);
1309 /* Returns a pointer to the pointer to the answer in the answer chain,
1310 or a pointer to NULL if the answer is not in the chain. */
1312 _cpp_find_answer (node, candidate)
1314 const cpp_toklist *candidate;
1316 struct answer **result;
1318 for (result = &node->value.answers; *result; result = &(*result)->next)
1319 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1329 struct answer *new_answer;
1332 node = _cpp_parse_assertion (pfile, &new_answer);
1335 new_answer->next = 0;
1336 new_answer->list.file = pfile->token_list.file;
1338 if (node->type == T_ASSERTION)
1340 if (*_cpp_find_answer (node, &new_answer->list))
1342 new_answer->next = node->value.answers;
1344 node->type = T_ASSERTION;
1345 node->value.answers = new_answer;
1350 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1351 FREE_ANSWER (new_answer);
1359 struct answer *answer, *temp;
1361 node = _cpp_parse_assertion (pfile, &answer);
1364 /* It isn't an error to #unassert something that isn't asserted. */
1365 if (node->type == T_ASSERTION)
1369 struct answer **p = _cpp_find_answer (node, &answer->list);
1377 if (node->value.answers == 0)
1378 node->type = T_VOID;
1381 _cpp_free_definition (node);
1385 FREE_ANSWER (answer);
1389 /* These are for -D, -U, -A. */
1391 /* Process the string STR as if it appeared as the body of a #define.
1392 If STR is just an identifier, define it with value 1.
1393 If STR has anything after the identifier, then it should
1394 be identifier=definition. */
1397 cpp_define (pfile, str)
1404 p = strchr (str, '=');
1405 /* Copy the entire option so we can modify it.
1406 Change the first "=" in the string to a space. If there is none,
1407 tack " 1" on the end. Then add a newline and a NUL. */
1411 count = strlen (str) + 2;
1412 buf = (char *) alloca (count);
1413 memcpy (buf, str, count - 2);
1415 buf[count - 2] = '\n';
1416 buf[count - 1] = '\0';
1420 count = strlen (str) + 4;
1421 buf = (char *) alloca (count);
1422 memcpy (buf, str, count - 4);
1423 strcpy (&buf[count-4], " 1\n");
1426 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1, 0);
1429 /* Slight variant of the above for use by initialize_builtins, which (a)
1430 knows how to set up the buffer itself, (b) needs a different "filename"
1433 _cpp_define_builtin (pfile, str)
1437 _cpp_run_directive (pfile, &dtable[T_DEFINE],
1442 /* Process MACRO as if it appeared as the body of an #undef. */
1444 cpp_undef (pfile, macro)
1448 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro), 0);
1451 /* Process the string STR as if it appeared as the body of a #assert. */
1453 cpp_assert (pfile, str)
1457 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str), 0);
1460 /* Process STR as if it appeared as the body of an #unassert. */
1462 cpp_unassert (pfile, str)
1466 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str), 0);
1469 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1471 cpp_defined (pfile, id, len)
1476 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1477 if (hp->type == T_POISON)
1479 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1482 return (hp->type != T_VOID);
1485 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1486 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1487 as the new input buffer.
1488 Return the new buffer, or NULL on failure. */
1491 cpp_push_buffer (pfile, buffer, length)
1493 const U_CHAR *buffer;
1496 cpp_buffer *buf = CPP_BUFFER (pfile);
1498 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1500 cpp_fatal (pfile, "#include nested too deep");
1503 if (pfile->cur_context > 0)
1505 cpp_ice (pfile, "buffer pushed with contexts stacked");
1506 _cpp_skip_rest_of_line (pfile);
1509 new = xobnew (pfile->buffer_ob, cpp_buffer);
1510 memset (new, 0, sizeof (cpp_buffer));
1512 new->line_base = new->buf = new->cur = buffer;
1513 new->rlimit = buffer + length;
1516 /* No read ahead or extra char initially. */
1517 new->read_ahead = EOF;
1518 new->extra_char = EOF;
1520 CPP_BUFFER (pfile) = new;
1525 cpp_pop_buffer (pfile)
1529 cpp_buffer *buf = CPP_BUFFER (pfile);
1531 unwind_if_stack (pfile, buf);
1532 wfb = (buf->inc != 0);
1534 _cpp_pop_file_buffer (pfile, buf);
1536 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
1537 obstack_free (pfile->buffer_ob, buf);
1538 pfile->buffer_stack_depth--;
1540 if (wfb && pfile->cb.leave_file && CPP_BUFFER (pfile))
1541 (*pfile->cb.leave_file) (pfile);
1543 return CPP_BUFFER (pfile);
1546 #define obstack_chunk_alloc xmalloc
1547 #define obstack_chunk_free free
1548 #define DSC(x) U x, sizeof x - 1
1550 _cpp_init_stacks (pfile)
1554 struct spec_nodes *s;
1556 pfile->buffer_ob = xnew (struct obstack);
1557 obstack_init (pfile->buffer_ob);
1559 /* Perhaps not the ideal place to put this. */
1560 pfile->spec_nodes = s = xnew (struct spec_nodes);
1561 s->n_L = cpp_lookup (pfile, DSC("L"));
1562 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1563 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1564 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1565 for (i = 0; i < N_DIRECTIVES; i++)
1566 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1570 _cpp_cleanup_stacks (pfile)
1573 obstack_free (pfile->buffer_ob, 0);
1574 free (pfile->buffer_ob);