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. */
30 /* Stack of conditionals currently in progress
31 (including both successful and failing conditionals). */
35 struct if_stack *next;
36 unsigned int lineno; /* line number where condition started */
37 unsigned int colno; /* and column */
38 int was_skipping; /* value of pfile->skipping before this if */
39 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
40 int type; /* type of last directive seen in this group */
43 /* Forward declarations. */
45 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
46 static int parse_include PARAMS ((cpp_reader *, const U_CHAR *, int,
47 const U_CHAR **, unsigned int *,
49 static void push_conditional PARAMS ((cpp_reader *, int, int,
50 const cpp_hashnode *));
51 static void pass_thru_directive PARAMS ((cpp_reader *));
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 *parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
57 static const cpp_hashnode *detect_if_not_defined
58 PARAMS ((cpp_reader *));
59 static cpp_hashnode *get_define_node PARAMS ((cpp_reader *));
60 static void dump_macro_name PARAMS ((cpp_reader *, cpp_hashnode *));
63 #define str_match(sym, len, str) \
64 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
66 /* This is the table of directive handlers. It is ordered by
67 frequency of occurrence; the numbers at the end are directive
68 counts from all the source code I have lying around (egcs and libc
69 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
72 The entries with a dash and a name after the count are extensions,
73 of which all but #warning and #include_next are deprecated. The name
74 is where the extension appears to have come from. */
76 /* #sccs is not always recognized. */
78 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
80 # define SCCS_ENTRY /* nothing */
83 #define DIRECTIVE_TABLE \
84 D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
85 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
86 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
87 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
88 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
89 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
90 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
91 D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
92 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
93 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
94 D(error, T_ERROR, STDC89, 0) /* 475 */ \
95 D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
96 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
97 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
98 D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
99 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
100 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
101 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
102 SCCS_ENTRY /* 0 SVR2? */
104 /* Use the table to generate a series of prototypes, an enum for the
105 directive names, and an array of directive handlers. */
107 /* The directive-processing functions are declared to return int
108 instead of void, because some old compilers have trouble with
109 pointers to functions returning void. */
111 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
112 #define D(name, t, o, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
116 #define D(n, tag, o, f) tag,
124 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
125 #define D(name, t, origin, flags) \
126 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
127 sizeof STRINGX(name) - 1, origin, flags },
128 static const struct directive dtable[] =
133 #undef DIRECTIVE_TABLE
135 /* Check if a token's name matches that of a known directive. Put in
136 this file to save exporting dtable and other unneeded information. */
137 const struct directive *
138 _cpp_check_directive (pfile, token, bol)
140 const cpp_token *token;
143 const U_CHAR *name = token->val.name.text;
144 size_t len = token->val.name.len;
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 (dtable[i].length == len && !memcmp (dtable[i].name, name, len))
155 /* If we are skipping a failed conditional group, all non-conditional
156 directives are ignored. */
157 if (pfile->skipping && !(dtable[i].flags & COND))
160 /* In -traditional mode, a directive is ignored unless its #
162 if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile))
163 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
166 if (!bol && CPP_TRADITIONAL (pfile))
169 /* Issue -pedantic warnings for extended directives. */
170 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
171 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
173 /* -Wtraditional gives warnings about directives with inappropriate
175 if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile))
177 "suggest hiding #%s from traditional C with an indented #",
186 const struct directive *
187 _cpp_check_linemarker (pfile, token, bol)
189 const cpp_token *token ATTRIBUTE_UNUSED;
192 /* # followed by a number is equivalent to #line. Do not recognize
193 this form in assembly language source files or skipped
194 conditional groups. Complain about this form if we're being
195 pedantic, but not if this is regurgitated input (preprocessed or
196 fed back in by the C++ frontend). */
197 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
200 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
201 && ! CPP_OPTION (pfile, preprocessed))
202 cpp_pedwarn (pfile, "# followed by integer");
204 /* In -traditional mode, a directive is ignored unless its #
206 if (!bol && CPP_WTRADITIONAL (pfile))
207 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
208 dtable[T_LINE].name);
210 if (!bol && CPP_TRADITIONAL (pfile))
213 return &dtable[T_LINE];
217 dump_macro_name (pfile, node)
221 CPP_PUTS (pfile, "#define ", sizeof "#define " - 1);
222 CPP_PUTS (pfile, node->name, node->length);
225 /* Pass the current directive through to the output file. */
227 pass_thru_directive (pfile)
230 /* XXX This output may be genuinely needed even when there is no
232 if (! pfile->printer)
234 /* Flush first (temporary). */
235 cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line);
236 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1);
239 static cpp_hashnode *
240 get_define_node (pfile)
244 const cpp_token *token;
248 /* Skip any -C comments. */
249 while ((token = cpp_get_token (pfile))->type == CPP_COMMENT)
252 if (token->type != CPP_NAME)
254 cpp_error_with_line (pfile, pfile->token_list.line, token->col,
255 "macro names must be identifiers");
259 /* That identifier is not allowed to be "defined". See predefined
260 macro names (6.10.8.4). */
261 len = token->val.name.len;
262 sym = token->val.name.text;
263 if (str_match (sym, len, "defined"))
265 cpp_error_with_line (pfile, pfile->token_list.line, token->col,
266 "\"defined\" is not a legal macro name");
270 node = cpp_lookup (pfile, sym, len);
271 /* Check for poisoned identifiers now. */
272 if (node->type == T_POISON)
274 cpp_error (pfile, "attempt to use poisoned \"%.*s\"", (int) len, sym);
281 /* Process a #define command. */
288 if ((node = get_define_node (pfile)))
289 if (_cpp_create_definition (pfile, node))
291 if (CPP_OPTION (pfile, debug_output)
292 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
293 _cpp_dump_definition (pfile, node);
294 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
295 dump_macro_name (pfile, node);
300 /* Remove the definition of a symbol from the symbol table. */
305 cpp_hashnode *node = get_define_node (pfile);
307 if (cpp_get_token (pfile)->type != CPP_EOF)
308 cpp_pedwarn (pfile, "junk on line after #undef");
310 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
311 is not currently defined as a macro name. */
312 if (node && node->type != T_VOID)
314 /* If we are generating additional info for debugging (with -g) we
315 need to pass through all effective #undef commands. */
316 if (CPP_OPTION (pfile, debug_output)
317 || CPP_OPTION (pfile, dump_macros) == dump_definitions
318 || CPP_OPTION (pfile, dump_macros) == dump_names)
319 pass_thru_directive (pfile);
321 if (node->type != T_MACRO)
322 cpp_warning (pfile, "undefining \"%s\"", node->name);
324 _cpp_free_definition (node);
332 /* Handle #include and #import. */
335 parse_include (pfile, dir, trail, strp, lenp, abp)
343 const cpp_token *name = cpp_get_token (pfile);
345 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
347 if (name->type == CPP_LESS)
348 name = _cpp_glue_header_name (pfile);
351 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
355 if (name->val.name.len == 0)
357 cpp_error (pfile, "empty file name in #%s", dir);
361 if (!trail && cpp_get_token (pfile)->type != CPP_EOF)
362 cpp_error (pfile, "junk at end of #%s", dir);
364 *lenp = name->val.name.len;
365 *strp = name->val.name.text;
366 *abp = (name->type == CPP_HEADER_NAME);
378 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
381 _cpp_execute_include (pfile, str, len, 0, 0, ab);
382 if (CPP_OPTION (pfile, dump_includes))
383 pass_thru_directive (pfile);
395 if (CPP_OPTION (pfile, warn_import)
396 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
398 pfile->import_warning = 1;
400 "#import is obsolete, use an #ifndef wrapper in the header file");
403 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
406 _cpp_execute_include (pfile, str, len, 1, 0, ab);
407 if (CPP_OPTION (pfile, dump_includes))
408 pass_thru_directive (pfile);
413 do_include_next (pfile)
418 struct file_name_list *search_start = 0;
421 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
424 /* For #include_next, skip in the search path past the dir in which
425 the current file was found. If this is the last directory in the
426 search path, don't include anything. If the current file was
427 specified with an absolute path, use the normal search logic. If
428 this is the primary source file, use the normal search logic and
429 generate a warning. */
430 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
432 if (CPP_BUFFER (pfile)->inc->foundhere)
434 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
440 cpp_warning (pfile, "#include_next in primary source file");
442 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
443 if (CPP_OPTION (pfile, dump_includes))
444 pass_thru_directive (pfile);
449 /* Subroutine of do_line. Read next token from PFILE without adding it to
450 the output buffer. If it is a number between 1 and 4, store it in *NUM
451 and return 1; otherwise, return 0 and complain if we aren't at the end
455 read_line_number (pfile, num)
459 const cpp_token *tok = cpp_get_token (pfile);
460 enum cpp_ttype type = tok->type;
461 const U_CHAR *p = tok->val.name.text;
462 unsigned int len = tok->val.name.len;
464 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
471 if (type != CPP_VSPACE && type != CPP_EOF)
472 cpp_error (pfile, "invalid format #line");
477 /* Another subroutine of do_line. Convert a number in STR, of length
478 LEN, to binary; store it in NUMP, and return 0 if the number was
479 legal, 1 if not. Temporary, hopefully. */
481 strtoul_for_line (str, len, nump)
486 unsigned long reg = 0;
500 /* Interpret #line command.
501 Note that the filename string (if any) is treated as if it were an
502 include filename. That means no escape handling. */
508 cpp_buffer *ip = CPP_BUFFER (pfile);
509 unsigned long new_lineno, old_lineno;
510 /* C99 raised the minimum limit on #line numbers. */
511 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
512 int action_number = 0;
517 const cpp_token *tok;
519 tok = cpp_get_token (pfile);
521 str = tok->val.name.text;
522 len = tok->val.name.len;
524 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
526 cpp_error (pfile, "token after #line is not a positive integer");
530 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
531 cpp_pedwarn (pfile, "line number out of range");
533 old_lineno = ip->lineno;
534 ip->lineno = new_lineno;
535 tok = cpp_get_token (pfile);
537 str = tok->val.name.text;
538 len = tok->val.name.len;
540 if (type == CPP_VSPACE || type == CPP_EOF)
542 else if (type != CPP_STRING)
544 cpp_error (pfile, "second token after #line is not a string");
545 ip->lineno = old_lineno; /* malformed #line should have no effect */
549 fname = alloca (len + 1);
550 memcpy (fname, str, len);
553 if (strcmp (fname, ip->nominal_fname))
555 if (!strcmp (fname, ip->inc->name))
556 ip->nominal_fname = ip->inc->name;
558 ip->nominal_fname = _cpp_fake_include (pfile, fname);
561 if (read_line_number (pfile, &action_number) == 0)
564 if (CPP_PEDANTIC (pfile))
565 cpp_pedwarn (pfile, "garbage at end of #line");
567 /* This is somewhat questionable: change the buffer stack
568 depth so that output_line_command thinks we've stacked
570 if (action_number == 1)
572 pfile->buffer_stack_depth++;
573 cpp_make_system_header (pfile, ip, 0);
574 read_line_number (pfile, &action_number);
576 else if (action_number == 2)
578 pfile->buffer_stack_depth--;
579 cpp_make_system_header (pfile, ip, 0);
580 read_line_number (pfile, &action_number);
582 if (action_number == 3)
584 cpp_make_system_header (pfile, ip, 1);
585 read_line_number (pfile, &action_number);
587 if (action_number == 4)
589 cpp_make_system_header (pfile, ip, 2);
590 read_line_number (pfile, &action_number);
599 * Report an error detected by the program we are processing.
600 * Use the text of the line in the error message.
601 * (We use error because it prints the filename & line#.)
608 U_CHAR *text, *limit;
611 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
612 limit = pfile->limit;
614 cpp_error (pfile, "%.*s", (int)(limit - text), text);
620 * Report a warning detected by the program we are processing.
621 * Use the text of the line in the warning message, then continue.
628 U_CHAR *text, *limit;
631 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
632 limit = pfile->limit;
634 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
638 /* Report program identification. */
644 /* Next token should be a string constant. */
645 if (cpp_get_token (pfile)->type == CPP_STRING)
646 /* And then a newline. */
647 if (cpp_get_token (pfile)->type == CPP_VSPACE)
649 /* Good - ship it. */
650 pass_thru_directive (pfile);
654 cpp_error (pfile, "invalid #ident");
658 /* Pragmata handling. We handle some of these, and pass the rest on
659 to the front end. C99 defines three pragmas and says that no macro
660 expansion is to be performed on them; whether or not macro
661 expansion happens for other pragmas is implementation defined.
662 This implementation never macro-expands the text after #pragma.
664 We currently do not support the _Pragma operator. Support for that
665 has to be coordinated with the front end. Proposed implementation:
666 both #pragma blah blah and _Pragma("blah blah") become
667 __builtin_pragma(blah blah) and we teach the parser about that. */
669 /* Sub-handlers for the pragmas needing treatment here.
670 They return 1 if the token buffer is to be popped, 0 if not. */
674 int (*handler) PARAMS ((cpp_reader *));
677 static int pragma_dispatch
678 PARAMS ((cpp_reader *, const struct pragma_entry *,
679 const U_CHAR *, size_t));
680 static int do_pragma_once PARAMS ((cpp_reader *));
681 static int do_pragma_implementation PARAMS ((cpp_reader *));
682 static int do_pragma_poison PARAMS ((cpp_reader *));
683 static int do_pragma_system_header PARAMS ((cpp_reader *));
684 static int do_pragma_gcc PARAMS ((cpp_reader *));
685 static int do_pragma_dependency PARAMS ((cpp_reader *));
687 static const struct pragma_entry top_pragmas[] =
689 {"once", do_pragma_once},
690 {"implementation", do_pragma_implementation},
691 {"poison", do_pragma_poison},
692 {"GCC", do_pragma_gcc},
696 static const struct pragma_entry gcc_pragmas[] =
698 {"implementation", do_pragma_implementation},
699 {"poison", do_pragma_poison},
700 {"system_header", do_pragma_system_header},
701 {"dependency", do_pragma_dependency},
705 static int pragma_dispatch (pfile, table, p, len)
707 const struct pragma_entry *table;
711 for (; table->name; table++)
712 if (strlen (table->name) == len && !memcmp (p, table->name, len))
713 return (*table->handler) (pfile);
721 const cpp_token *tok;
724 tok = cpp_get_token (pfile);
725 if (tok->type == CPP_EOF)
727 else if (tok->type != CPP_NAME)
729 cpp_error (pfile, "malformed #pragma directive");
733 pop = pragma_dispatch (pfile, top_pragmas,
734 tok->val.name.text, tok->val.name.len);
736 pass_thru_directive (pfile);
741 do_pragma_gcc (pfile)
744 const cpp_token *tok;
746 tok = cpp_get_token (pfile);
747 if (tok->type == CPP_EOF)
749 else if (tok->type != CPP_NAME)
752 return pragma_dispatch (pfile, gcc_pragmas,
753 tok->val.name.text, tok->val.name.len);
757 do_pragma_once (pfile)
760 cpp_buffer *ip = CPP_BUFFER (pfile);
762 /* Allow #pragma once in system headers, since that's not the user's
764 if (!CPP_IN_SYSTEM_HEADER (pfile))
765 cpp_warning (pfile, "#pragma once is obsolete");
767 if (CPP_PREV_BUFFER (ip) == NULL)
768 cpp_warning (pfile, "#pragma once outside include file");
770 ip->inc->cmacro = NEVER_REREAD;
776 do_pragma_implementation (pfile)
779 /* Be quiet about `#pragma implementation' for a file only if it hasn't
780 been included yet. */
781 const cpp_token *tok = cpp_get_token (pfile);
784 if (tok->type == CPP_EOF)
786 else if (tok->type != CPP_STRING
787 || cpp_get_token (pfile)->type != CPP_EOF)
789 cpp_error (pfile, "malformed #pragma implementation");
793 /* Make a NUL-terminated copy of the string. */
794 copy = alloca (tok->val.name.len + 1);
795 memcpy (copy, tok->val.name.text, tok->val.name.len);
796 copy[tok->val.name.len] = '\0';
798 if (cpp_included (pfile, copy))
800 "#pragma implementation for %s appears after file is included",
806 do_pragma_poison (pfile)
809 /* Poison these symbols so that all subsequent usage produces an
811 const cpp_token *tok;
815 /* As a rule, don't include #pragma poison commands in output,
816 unless the user asks for them. */
817 writeit = (CPP_OPTION (pfile, debug_output)
818 || CPP_OPTION (pfile, dump_macros) == dump_definitions
819 || CPP_OPTION (pfile, dump_macros) == dump_names);
823 tok = cpp_get_token (pfile);
824 if (tok->type == CPP_EOF)
826 if (tok->type != CPP_NAME)
828 cpp_error (pfile, "invalid #pragma poison directive");
832 hp = cpp_lookup (pfile, tok->val.name.text, tok->val.name.len);
833 if (hp->type == T_POISON)
834 ; /* It is allowed to poison the same identifier twice. */
837 if (hp->type != T_VOID)
838 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
839 _cpp_free_definition (hp);
846 /* Mark the current header as a system header. This will suppress
847 some categories of warnings (notably those from -pedantic). It is
848 intended for use in system libraries that cannot be implemented in
849 conforming C, but cannot be certain that their headers appear in a
850 system include directory. To prevent abuse, it is rejected in the
851 primary source file. */
853 do_pragma_system_header (pfile)
856 cpp_buffer *ip = CPP_BUFFER (pfile);
857 if (CPP_PREV_BUFFER (ip) == NULL)
858 cpp_warning (pfile, "#pragma system_header outside include file");
860 cpp_make_system_header (pfile, ip, 1);
865 /* Check the modified date of the current include file against a specified
866 file. Issue a diagnostic, if the specified file is newer. We use this to
867 determine if a fixed header should be refixed. */
869 do_pragma_dependency (pfile)
877 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
880 left = ab ? '<' : '"';
881 right = ab ? '>' : '"';
883 ordering = _cpp_compare_file_date (pfile, name, len, ab);
885 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
886 else if (ordering > 0)
888 const cpp_token *msg = cpp_get_token (pfile);
890 cpp_warning (pfile, "current file is older than %c%s%c",
892 if (msg->type != CPP_EOF)
894 U_CHAR *text, *limit;
897 _cpp_dump_list (pfile, &pfile->token_list, msg, 0);
898 limit = pfile->limit;
900 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
906 /* Just ignore #sccs, on systems where we define it at all. */
907 #ifdef SCCS_DIRECTIVE
910 cpp_reader *pfile ATTRIBUTE_UNUSED;
916 /* We've found an `#if' directive. If the only thing before it in
917 this file is white space, and if it is of the form
918 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
919 for inclusion of this file. (See redundant_include_p in cppfiles.c
920 for an explanation of controlling macros.) If so, return the
921 hash node for SYMBOL. Otherwise, return NULL. */
923 static const cpp_hashnode *
924 detect_if_not_defined (pfile)
927 const cpp_token *token;
928 cpp_hashnode *cmacro = 0;
930 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
931 token = pfile->first_directive_token + 2;
933 if (token->type != CPP_NOT)
937 if (token->type != CPP_NAME
938 || !str_match (token->val.name.text, token->val.name.len, "defined"))
942 if (token->type == CPP_OPEN_PAREN)
945 if (token->type != CPP_NAME)
948 cmacro = cpp_lookup (pfile, token->val.name.text, token->val.name.len);
950 if (token[-1].type == CPP_OPEN_PAREN)
953 if (token->type != CPP_CLOSE_PAREN)
958 if (token->type != CPP_EOF)
964 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
965 macro being tested, and issues various error messages. */
967 static const cpp_hashnode *
968 parse_ifdef (pfile, name)
975 const cpp_hashnode *node = 0;
977 const cpp_token *token = cpp_get_token (pfile);
979 ident = token->val.name.text;
980 len = token->val.name.len;
982 if (!CPP_TRADITIONAL (pfile))
985 cpp_pedwarn (pfile, "#%s with no argument", name);
986 else if (type != CPP_NAME)
987 cpp_pedwarn (pfile, "#%s with invalid argument", name);
988 else if (cpp_get_token (pfile)->type != CPP_EOF)
989 cpp_pedwarn (pfile, "garbage at end of #%s", name);
992 if (type == CPP_NAME)
993 node = cpp_lookup (pfile, ident, len);
994 if (node && node->type == T_POISON)
995 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"", node->name);
1000 /* #ifdef is dead simple. */
1007 const cpp_hashnode *node = 0;
1009 if (! pfile->skipping)
1011 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1013 def = (node->type != T_VOID && node->type != T_POISON);
1016 push_conditional (pfile, !def, T_IFDEF, 0);
1020 /* #ifndef is a tad more complex, because we need to check for a
1021 no-reinclusion wrapper. */
1027 int start_of_file = 0;
1029 const cpp_hashnode *cmacro = 0;
1031 if (! pfile->skipping)
1033 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
1034 cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1036 def = cmacro->type != T_VOID;
1039 push_conditional (pfile, def, T_IFNDEF, start_of_file ? cmacro : 0);
1043 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1044 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1050 const cpp_hashnode *cmacro = 0;
1053 if (! pfile->skipping)
1055 cmacro = detect_if_not_defined (pfile);
1056 value = _cpp_parse_expr (pfile);
1058 push_conditional (pfile, value == 0, T_IF, cmacro);
1062 /* #else flips pfile->skipping and continues without changing
1063 if_stack; this is so that the error message for missing #endif's
1064 etc. will point to the original #if. */
1070 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1071 validate_else (pfile, dtable[T_ELSE].name);
1075 cpp_error (pfile, "#else without #if");
1078 if (ifs->type == T_ELSE)
1080 cpp_error (pfile, "#else after #else");
1081 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1082 "the conditional began here");
1085 /* #ifndef can't have its special treatment for containing the whole file
1086 if it has a #else clause. */
1089 if (! ifs->was_skipping)
1091 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1092 succeeded, so we mustn't do the else block. */
1093 if (pfile->skipping < 2)
1094 pfile->skipping = ! pfile->skipping;
1100 * handle a #elif directive by not changing if_stack either.
1101 * see the comment above do_else.
1108 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1112 cpp_error (pfile, "#elif without #if");
1115 if (ifs->type == T_ELSE)
1117 cpp_error (pfile, "#elif after #else");
1118 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1119 "the conditional began here");
1123 if (ifs->was_skipping)
1124 return 0; /* Don't evaluate a nested #if */
1126 if (pfile->skipping != 1)
1128 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1132 pfile->skipping = ! _cpp_parse_expr (pfile);
1136 /* #endif pops the if stack and resets pfile->skipping. */
1142 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1144 validate_else (pfile, dtable[T_ENDIF].name);
1147 cpp_error (pfile, "#endif without #if");
1150 CPP_BUFFER (pfile)->if_stack = ifs->next;
1151 pfile->skipping = ifs->was_skipping;
1152 pfile->potential_control_macro = ifs->cmacro;
1159 /* Push an if_stack entry and set pfile->skipping accordingly.
1160 If this is a #ifndef starting at the beginning of a file,
1161 CMACRO is the macro name tested by the #ifndef. */
1164 push_conditional (pfile, skip, type, cmacro)
1168 const cpp_hashnode *cmacro;
1170 struct if_stack *ifs;
1172 ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1173 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1174 ifs->next = CPP_BUFFER (pfile)->if_stack;
1175 ifs->cmacro = cmacro;
1176 ifs->was_skipping = pfile->skipping;
1179 if (!pfile->skipping)
1180 pfile->skipping = skip;
1182 CPP_BUFFER (pfile)->if_stack = ifs;
1185 /* Issue -pedantic warning for text which is not a comment following
1186 an #else or #endif. */
1189 validate_else (pfile, directive)
1191 const U_CHAR *directive;
1193 if (CPP_PEDANTIC (pfile) && cpp_get_token (pfile)->type != CPP_EOF)
1194 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1197 /* Called when we reach the end of a file. Walk back up the
1198 conditional stack till we reach its level at entry to this file,
1199 issuing error messages. Then force skipping off. */
1201 _cpp_unwind_if_stack (pfile, pbuf)
1205 struct if_stack *ifs, *nifs;
1207 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1209 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1210 dtable[ifs->type].name);
1214 pfile->skipping = 0;
1217 /* Parses an assertion, returning a pointer to the hash node of the
1218 predicate, or 0 on error. If an answer was supplied, it is
1219 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1220 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1221 in a #if statement (we may be in a macro), and we don't want to
1224 _cpp_parse_assertion (pfile, answerp)
1226 struct answer **answerp;
1228 struct answer *answer = 0;
1231 const cpp_token *token, *predicate;
1232 const struct directive *d = pfile->token_list.directive;
1233 unsigned int len = 0;
1235 predicate = _cpp_get_raw_token (pfile);
1236 if (predicate->type == CPP_EOF)
1238 cpp_error (pfile, "assertion without predicate");
1241 else if (predicate->type != CPP_NAME)
1243 cpp_error (pfile, "predicate must be an identifier");
1247 token = _cpp_get_raw_token (pfile);
1248 if (token->type != CPP_OPEN_PAREN)
1250 /* #unassert and #if are OK without predicate. */
1251 if (d == &dtable[T_UNASSERT])
1253 if (token->type == CPP_EOF)
1256 else if (d != &dtable[T_ASSERT])
1258 _cpp_push_token (pfile, token);
1261 cpp_error (pfile, "missing '(' after predicate");
1265 /* Allocate a struct answer, and copy the answer to it. */
1266 answer = (struct answer *) xmalloc (sizeof (struct answer));
1267 list = &answer->list;
1268 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1274 token = _cpp_get_raw_token (pfile);
1276 if (token->type == CPP_EOF)
1278 cpp_error (pfile, "missing ')' to complete answer");
1281 if (token->type == CPP_CLOSE_PAREN)
1284 /* Copy the token. */
1285 _cpp_expand_token_space (list, 1);
1286 dest = &list->tokens[list->tokens_used++];
1289 if (token_spellings[token->type].type > SPELL_NONE)
1291 _cpp_expand_name_space (list, token->val.name.len);
1292 dest->val.name.text = list->namebuf + list->name_used;
1293 memcpy (list->namebuf + list->name_used,
1294 token->val.name.text, token->val.name.len);
1295 list->name_used += token->val.name.len;
1299 if (list->tokens_used == 0)
1301 cpp_error (pfile, "predicate's answer is empty");
1305 /* Drop whitespace at start. */
1306 list->tokens[0].flags &= ~PREV_WHITE;
1308 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1309 && token[1].type != CPP_EOF)
1311 cpp_error (pfile, "junk at end of assertion");
1317 len = predicate->val.name.len + 1;
1320 /* Prefix '#' to get it out of macro namespace. */
1322 memcpy (sym + 1, predicate->val.name.text, len);
1323 return cpp_lookup (pfile, sym, len);
1326 FREE_ANSWER (answer);
1330 /* Returns a pointer to the pointer to the answer in the answer chain,
1331 or a pointer to NULL if the answer is not in the chain. */
1333 find_answer (node, candidate)
1335 const cpp_toklist *candidate;
1337 struct answer **result;
1339 for (result = &node->value.answers; *result; result = &(*result)->next)
1340 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1346 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1347 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1348 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1353 struct answer *new_answer;
1356 node = _cpp_parse_assertion (pfile, &new_answer);
1359 new_answer->next = 0;
1360 new_answer->list.line = pfile->token_list.line;
1361 new_answer->list.file = pfile->token_list.file;
1363 if (node->type == T_ASSERTION)
1365 if (*find_answer (node, &new_answer->list))
1367 new_answer->next = node->value.answers;
1369 node->type = T_ASSERTION;
1370 node->value.answers = new_answer;
1375 cpp_warning (pfile, "\"%.*s\" re-asserted",
1376 node->length - 1, node->name + 1);
1377 FREE_ANSWER (new_answer);
1386 struct answer *answer, *temp, *next;
1388 node = _cpp_parse_assertion (pfile, &answer);
1391 /* It isn't an error to #unassert something that isn't asserted. */
1392 if (node->type == T_ASSERTION)
1396 struct answer **p = find_answer (node, &answer->list);
1404 if (node->value.answers == 0)
1405 node->type = T_VOID;
1409 for (temp = node->value.answers; temp; temp = next)
1414 node->type = T_VOID;
1419 FREE_ANSWER (answer);
1424 /* These are for -D, -U, -A. */
1426 /* Process the string STR as if it appeared as the body of a #define.
1427 If STR is just an identifier, define it with value 1.
1428 If STR has anything after the identifier, then it should
1429 be identifier=definition. */
1432 cpp_define (pfile, str)
1439 p = strchr (str, '=');
1440 /* Copy the entire option so we can modify it.
1441 Change the first "=" in the string to a space. If there is none,
1442 tack " 1" on the end. Then add a newline and a NUL. */
1446 count = strlen (str) + 2;
1447 buf = (char *) alloca (count);
1448 memcpy (buf, str, count - 2);
1450 buf[count - 2] = '\n';
1451 buf[count - 1] = '\0';
1455 count = strlen (str) + 4;
1456 buf = (char *) alloca (count);
1457 memcpy (buf, str, count - 4);
1458 strcpy (&buf[count-4], " 1\n");
1461 _cpp_run_directive (pfile, &dtable[T_DEFINE], buf, count - 1);
1464 /* Process MACRO as if it appeared as the body of an #undef. */
1466 cpp_undef (pfile, macro)
1470 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
1473 /* Process the string STR as if it appeared as the body of a #assert. */
1475 cpp_assert (pfile, str)
1479 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
1482 /* Process STR as if it appeared as the body of an #unassert. */
1484 cpp_unassert (pfile, str)
1488 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
1491 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1493 cpp_defined (pfile, id, len)
1498 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1499 if (hp->type == T_POISON)
1501 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1504 return (hp->type != T_VOID);