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 void pass_thru_directive PARAMS ((cpp_reader *));
53 static int read_line_number PARAMS ((cpp_reader *, int *));
54 static int strtoul_for_line PARAMS ((const U_CHAR *, unsigned int,
57 static const cpp_hashnode *
58 parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
59 static const cpp_hashnode *
60 detect_if_not_defined PARAMS ((cpp_reader *));
62 get_define_node PARAMS ((cpp_reader *));
63 static void dump_macro_name PARAMS ((cpp_reader *, cpp_hashnode *));
64 static void unwind_if_stack PARAMS ((cpp_reader *, cpp_buffer *));
67 #define str_match(sym, len, str) \
68 ((len) == (sizeof (str) - 1) && !ustrncmp ((sym), U(str), sizeof (str) - 1))
70 /* This is the table of directive handlers. It is ordered by
71 frequency of occurrence; the numbers at the end are directive
72 counts from all the source code I have lying around (egcs and libc
73 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
76 The entries with a dash and a name after the count are extensions,
77 of which all but #warning and #include_next are deprecated. The name
78 is where the extension appears to have come from. */
80 /* #sccs is not always recognized. */
82 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION, 0)
84 # define SCCS_ENTRY /* nothing */
87 #define DIRECTIVE_TABLE \
88 D(define, T_DEFINE = 0, KANDR, COMMENTS) /* 270554 */ \
89 D(include, T_INCLUDE, KANDR, EXPAND | INCL) /* 52262 */ \
90 D(endif, T_ENDIF, KANDR, COND) /* 45855 */ \
91 D(ifdef, T_IFDEF, KANDR, COND) /* 22000 */ \
92 D(if, T_IF, KANDR, COND | EXPAND) /* 18162 */ \
93 D(else, T_ELSE, KANDR, COND) /* 9863 */ \
94 D(ifndef, T_IFNDEF, KANDR, COND) /* 9675 */ \
95 D(undef, T_UNDEF, KANDR, 0) /* 4837 */ \
96 D(line, T_LINE, KANDR, EXPAND) /* 2465 */ \
97 D(elif, T_ELIF, KANDR, COND | EXPAND) /* 610 */ \
98 D(error, T_ERROR, STDC89, 0) /* 475 */ \
99 D(pragma, T_PRAGMA, STDC89, 0) /* 195 */ \
100 D(warning, T_WARNING, EXTENSION, 0) /* 22 GNU */ \
101 D(include_next, T_INCLUDE_NEXT, EXTENSION, EXPAND | INCL) /* 19 GNU */ \
102 D(ident, T_IDENT, EXTENSION, 0) /* 11 SVR4 */ \
103 D(import, T_IMPORT, EXTENSION, EXPAND | INCL) /* 0 ObjC */ \
104 D(assert, T_ASSERT, EXTENSION, 0) /* 0 SVR4 */ \
105 D(unassert, T_UNASSERT, EXTENSION, 0) /* 0 SVR4 */ \
106 SCCS_ENTRY /* 0 SVR2? */
108 /* Use the table to generate a series of prototypes, an enum for the
109 directive names, and an array of directive handlers. */
111 /* The directive-processing functions are declared to return int
112 instead of void, because some old compilers have trouble with
113 pointers to functions returning void. */
115 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
116 #define D(name, t, o, f) static void CONCAT2(do_,name) PARAMS ((cpp_reader *));
120 #define D(n, tag, o, f) tag,
128 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
129 #define D(name, t, origin, flags) \
130 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
131 sizeof STRINGX(name) - 1, origin, flags },
132 static const struct directive dtable[] =
137 #undef DIRECTIVE_TABLE
139 /* Check if a token's name matches that of a known directive. Put in
140 this file to save exporting dtable and other unneeded information. */
141 const struct directive *
142 _cpp_check_directive (pfile, token, bol)
144 const cpp_token *token;
149 /* If we are rescanning preprocessed input, don't obey any directives
151 if (CPP_OPTION (pfile, preprocessed))
154 for (i = 0; i < N_DIRECTIVES; i++)
155 if (pfile->spec_nodes->dirs[i] == token->val.node)
157 /* If we are skipping a failed conditional group, all non-conditional
158 directives are ignored. */
159 if (pfile->skipping && !(dtable[i].flags & COND))
162 /* In -traditional mode, a directive is ignored unless its #
164 if (!bol && dtable[i].origin == KANDR && CPP_WTRADITIONAL (pfile))
165 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
168 /* Issue -pedantic warnings for extended directives. */
169 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
170 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
172 /* -Wtraditional gives warnings about directives with inappropriate
174 if (bol && dtable[i].origin != KANDR && CPP_WTRADITIONAL (pfile))
176 "suggest hiding #%s from traditional C with an indented #",
185 const struct directive *
186 _cpp_check_linemarker (pfile, token, bol)
188 const cpp_token *token ATTRIBUTE_UNUSED;
191 /* # followed by a number is equivalent to #line. Do not recognize
192 this form in assembly language source files or skipped
193 conditional groups. Complain about this form if we're being
194 pedantic, but not if this is regurgitated input (preprocessed or
195 fed back in by the C++ frontend). */
196 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
199 if (CPP_PEDANTIC (pfile) && CPP_BUFFER (pfile)->inc
200 && ! CPP_OPTION (pfile, preprocessed))
201 cpp_pedwarn (pfile, "# followed by integer");
203 /* In -traditional mode, a directive is ignored unless its #
205 if (!bol && CPP_WTRADITIONAL (pfile))
206 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
207 dtable[T_LINE].name);
209 return &dtable[T_LINE];
213 dump_macro_name (pfile, node)
217 CPP_PUTS (pfile, "#define ", sizeof "#define " - 1);
218 CPP_PUTS (pfile, node->name, node->length);
221 /* Pass the current directive through to the output file. */
223 pass_thru_directive (pfile)
226 /* XXX This output may be genuinely needed even when there is no
228 if (! pfile->printer)
230 /* Flush first (temporary). */
231 cpp_output_tokens (pfile, pfile->printer, pfile->token_list.line);
232 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 1);
235 static cpp_hashnode *
236 get_define_node (pfile)
239 const cpp_token *token;
241 /* Skip any -C comments. */
242 while ((token = _cpp_get_token (pfile))->type == CPP_COMMENT)
245 /* The token immediately after #define must be an identifier. That
246 identifier is not allowed to be "defined". See predefined macro
247 names (6.10.8.4). In C++, it is not allowed to be any of the
248 <iso646.h> macro names (which are keywords in C++) either. */
250 if (token->type != CPP_NAME)
252 if (token->type == CPP_DEFINED)
253 cpp_error_with_line (pfile, token->line, token->col,
254 "\"defined\" cannot be used as a macro name");
255 else if (token->flags & NAMED_OP)
256 cpp_error_with_line (pfile, token->line, token->col,
257 "\"%s\" cannot be used as a macro name in C++",
258 token->val.node->name);
260 cpp_error_with_line (pfile, token->line, token->col,
261 "macro names must be identifiers");
265 /* Check for poisoned identifiers now. */
266 if (token->val.node->type == T_POISON)
268 cpp_error_with_line (pfile, token->line, token->col,
269 "attempt to use poisoned \"%s\"",
270 token->val.node->name);
274 return token->val.node;
277 /* Process a #define command. */
284 if ((node = get_define_node (pfile)))
285 if (_cpp_create_definition (pfile, node))
287 if (CPP_OPTION (pfile, debug_output)
288 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
289 _cpp_dump_definition (pfile, node);
290 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
291 dump_macro_name (pfile, node);
295 /* Remove the definition of a symbol from the symbol table. */
300 cpp_hashnode *node = get_define_node (pfile);
302 if (_cpp_get_token (pfile)->type != CPP_EOF)
303 cpp_pedwarn (pfile, "junk on line after #undef");
305 /* 6.10.3.5 paragraph 2: [#undef] is ignored if the specified identifier
306 is not currently defined as a macro name. */
307 if (node && node->type != T_VOID)
309 /* If we are generating additional info for debugging (with -g) we
310 need to pass through all effective #undef commands. */
311 if (CPP_OPTION (pfile, debug_output)
312 || CPP_OPTION (pfile, dump_macros) == dump_definitions
313 || CPP_OPTION (pfile, dump_macros) == dump_names)
314 pass_thru_directive (pfile);
316 if (node->type != T_MACRO)
317 cpp_warning (pfile, "undefining \"%s\"", node->name);
319 _cpp_free_definition (node);
325 /* Handle #include and #import. */
328 parse_include (pfile, dir, trail, strp, lenp, abp)
336 const cpp_token *name = _cpp_get_token (pfile);
338 if (name->type != CPP_STRING && name->type != CPP_HEADER_NAME)
340 if (name->type == CPP_LESS)
341 name = _cpp_glue_header_name (pfile);
344 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", dir);
348 if (name->val.str.len == 0)
350 cpp_error (pfile, "empty file name in #%s", dir);
354 if (!trail && _cpp_get_token (pfile)->type != CPP_EOF)
355 cpp_error (pfile, "junk at end of #%s", dir);
357 *lenp = name->val.str.len;
358 *strp = name->val.str.text;
359 *abp = (name->type == CPP_HEADER_NAME);
371 if (parse_include (pfile, dtable[T_INCLUDE].name, 0, &str, &len, &ab))
374 _cpp_execute_include (pfile, str, len, 0, 0, ab);
375 if (CPP_OPTION (pfile, dump_includes))
376 pass_thru_directive (pfile);
387 if (CPP_OPTION (pfile, warn_import)
388 && !CPP_IN_SYSTEM_HEADER (pfile) && !pfile->import_warning)
390 pfile->import_warning = 1;
392 "#import is obsolete, use an #ifndef wrapper in the header file");
395 if (parse_include (pfile, dtable[T_IMPORT].name, 0, &str, &len, &ab))
398 _cpp_execute_include (pfile, str, len, 1, 0, ab);
399 if (CPP_OPTION (pfile, dump_includes))
400 pass_thru_directive (pfile);
404 do_include_next (pfile)
409 struct file_name_list *search_start = 0;
412 if (parse_include (pfile, dtable[T_INCLUDE_NEXT].name, 0, &str, &len, &ab))
415 /* For #include_next, skip in the search path past the dir in which
416 the current file was found. If this is the last directory in the
417 search path, don't include anything. If the current file was
418 specified with an absolute path, use the normal search logic. If
419 this is the primary source file, use the normal search logic and
420 generate a warning. */
421 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
423 if (CPP_BUFFER (pfile)->inc->foundhere)
425 search_start = CPP_BUFFER (pfile)->inc->foundhere->next;
431 cpp_warning (pfile, "#include_next in primary source file");
433 _cpp_execute_include (pfile, str, len, 0, search_start, ab);
434 if (CPP_OPTION (pfile, dump_includes))
435 pass_thru_directive (pfile);
438 /* Subroutine of do_line. Read next token from PFILE without adding it to
439 the output buffer. If it is a number between 1 and 4, store it in *NUM
440 and return 1; otherwise, return 0 and complain if we aren't at the end
444 read_line_number (pfile, num)
448 const cpp_token *tok = _cpp_get_token (pfile);
449 enum cpp_ttype type = tok->type;
450 const U_CHAR *p = tok->val.str.text;
451 unsigned int len = tok->val.str.len;
453 if (type == CPP_NUMBER && len == 1 && p[0] >= '1' && p[0] <= '4')
461 cpp_error (pfile, "invalid format #line");
466 /* Another subroutine of do_line. Convert a number in STR, of length
467 LEN, to binary; store it in NUMP, and return 0 if the number was
468 legal, 1 if not. Temporary, hopefully. */
470 strtoul_for_line (str, len, nump)
475 unsigned long reg = 0;
489 /* Interpret #line command.
490 Note that the filename string (if any) is treated as if it were an
491 include filename. That means no escape handling. */
497 cpp_buffer *ip = CPP_BUFFER (pfile);
498 unsigned long new_lineno, old_lineno;
499 /* C99 raised the minimum limit on #line numbers. */
500 unsigned int cap = CPP_OPTION (pfile, c99) ? 2147483647 : 32767;
501 int action_number = 0;
506 const cpp_token *tok;
508 tok = _cpp_get_token (pfile);
510 str = tok->val.str.text;
511 len = tok->val.str.len;
513 if (type != CPP_NUMBER || strtoul_for_line (str, len, &new_lineno))
515 cpp_error (pfile, "token after #line is not a positive integer");
519 if (CPP_PEDANTIC (pfile) && (new_lineno == 0 || new_lineno > cap))
520 cpp_pedwarn (pfile, "line number out of range");
522 old_lineno = ip->lineno;
523 ip->lineno = new_lineno;
524 tok = _cpp_get_token (pfile);
526 str = tok->val.str.text;
527 len = tok->val.str.len;
531 else if (type != CPP_STRING)
533 cpp_error (pfile, "second token after #line is not a string");
534 ip->lineno = old_lineno; /* malformed #line should have no effect */
538 fname = alloca (len + 1);
539 memcpy (fname, str, len);
542 if (strcmp (fname, ip->nominal_fname))
544 if (!strcmp (fname, ip->inc->name))
545 ip->nominal_fname = ip->inc->name;
547 ip->nominal_fname = _cpp_fake_include (pfile, fname);
550 if (read_line_number (pfile, &action_number) == 0)
553 if (CPP_PEDANTIC (pfile))
554 cpp_pedwarn (pfile, "garbage at end of #line");
556 /* This is somewhat questionable: change the buffer stack
557 depth so that output_line_command thinks we've stacked
559 if (action_number == 1)
561 pfile->buffer_stack_depth++;
562 cpp_make_system_header (pfile, ip, 0);
563 read_line_number (pfile, &action_number);
565 else if (action_number == 2)
567 pfile->buffer_stack_depth--;
568 cpp_make_system_header (pfile, ip, 0);
569 read_line_number (pfile, &action_number);
571 if (action_number == 3)
573 cpp_make_system_header (pfile, ip, 1);
574 read_line_number (pfile, &action_number);
576 if (action_number == 4)
578 cpp_make_system_header (pfile, ip, 2);
579 read_line_number (pfile, &action_number);
587 * Report an error detected by the program we are processing.
588 * Use the text of the line in the error message.
589 * (We use error because it prints the filename & line#.)
596 U_CHAR *text, *limit;
599 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
600 limit = pfile->limit;
602 cpp_error (pfile, "%.*s", (int)(limit - text), text);
606 * Report a warning detected by the program we are processing.
607 * Use the text of the line in the warning message, then continue.
614 U_CHAR *text, *limit;
617 _cpp_dump_list (pfile, &pfile->token_list, pfile->first_directive_token, 0);
618 limit = pfile->limit;
620 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
623 /* Report program identification. */
629 /* Next token should be a string constant. */
630 if (_cpp_get_token (pfile)->type == CPP_STRING)
631 /* And then a newline. */
632 if (_cpp_get_token (pfile)->type == CPP_EOF)
634 /* Good - ship it. */
635 pass_thru_directive (pfile);
639 cpp_error (pfile, "invalid #ident");
642 /* Pragmata handling. We handle some of these, and pass the rest on
643 to the front end. C99 defines three pragmas and says that no macro
644 expansion is to be performed on them; whether or not macro
645 expansion happens for other pragmas is implementation defined.
646 This implementation never macro-expands the text after #pragma.
648 We currently do not support the _Pragma operator. Support for that
649 has to be coordinated with the front end. Proposed implementation:
650 both #pragma blah blah and _Pragma("blah blah") become
651 __builtin_pragma(blah blah) and we teach the parser about that. */
653 /* Sub-handlers for the pragmas needing treatment here.
654 They return 1 if the token buffer is to be popped, 0 if not. */
658 int (*handler) PARAMS ((cpp_reader *));
661 static int pragma_dispatch
662 PARAMS ((cpp_reader *, const struct pragma_entry *, const cpp_hashnode *));
663 static int do_pragma_once PARAMS ((cpp_reader *));
664 static int do_pragma_implementation PARAMS ((cpp_reader *));
665 static int do_pragma_poison PARAMS ((cpp_reader *));
666 static int do_pragma_system_header PARAMS ((cpp_reader *));
667 static int do_pragma_gcc PARAMS ((cpp_reader *));
668 static int do_pragma_dependency PARAMS ((cpp_reader *));
670 static const struct pragma_entry top_pragmas[] =
672 {"once", do_pragma_once},
673 {"implementation", do_pragma_implementation},
674 {"poison", do_pragma_poison},
675 {"GCC", do_pragma_gcc},
679 static const struct pragma_entry gcc_pragmas[] =
681 {"implementation", do_pragma_implementation},
682 {"poison", do_pragma_poison},
683 {"system_header", do_pragma_system_header},
684 {"dependency", do_pragma_dependency},
688 static int pragma_dispatch (pfile, table, node)
690 const struct pragma_entry *table;
691 const cpp_hashnode *node;
693 const U_CHAR *p = node->name;
694 size_t len = node->length;
696 for (; table->name; table++)
697 if (strlen (table->name) == len && !memcmp (p, table->name, len))
698 return (*table->handler) (pfile);
706 const cpp_token *tok;
709 tok = _cpp_get_token (pfile);
710 if (tok->type == CPP_EOF)
712 else if (tok->type != CPP_NAME)
714 cpp_error (pfile, "malformed #pragma directive");
718 pop = pragma_dispatch (pfile, top_pragmas, tok->val.node);
720 pass_thru_directive (pfile);
724 do_pragma_gcc (pfile)
727 const cpp_token *tok;
729 tok = _cpp_get_token (pfile);
730 if (tok->type == CPP_EOF)
732 else if (tok->type != CPP_NAME)
735 return pragma_dispatch (pfile, gcc_pragmas, tok->val.node);
739 do_pragma_once (pfile)
742 cpp_buffer *ip = CPP_BUFFER (pfile);
744 /* Allow #pragma once in system headers, since that's not the user's
746 if (!CPP_IN_SYSTEM_HEADER (pfile))
747 cpp_warning (pfile, "#pragma once is obsolete");
749 if (CPP_PREV_BUFFER (ip) == NULL)
750 cpp_warning (pfile, "#pragma once outside include file");
752 ip->inc->cmacro = NEVER_REREAD;
758 do_pragma_implementation (pfile)
761 /* Be quiet about `#pragma implementation' for a file only if it hasn't
762 been included yet. */
763 const cpp_token *tok = _cpp_get_token (pfile);
766 if (tok->type == CPP_EOF)
768 else if (tok->type != CPP_STRING
769 || _cpp_get_token (pfile)->type != CPP_EOF)
771 cpp_error (pfile, "malformed #pragma implementation");
775 /* Make a NUL-terminated copy of the string. */
776 copy = alloca (tok->val.str.len + 1);
777 memcpy (copy, tok->val.str.text, tok->val.str.len);
778 copy[tok->val.str.len] = '\0';
780 if (cpp_included (pfile, copy))
782 "#pragma implementation for %s appears after file is included",
788 do_pragma_poison (pfile)
791 /* Poison these symbols so that all subsequent usage produces an
793 const cpp_token *tok;
797 /* As a rule, don't include #pragma poison commands in output,
798 unless the user asks for them. */
799 writeit = (CPP_OPTION (pfile, debug_output)
800 || CPP_OPTION (pfile, dump_macros) == dump_definitions
801 || CPP_OPTION (pfile, dump_macros) == dump_names);
805 tok = _cpp_get_token (pfile);
806 if (tok->type == CPP_EOF)
808 if (tok->type != CPP_NAME)
810 cpp_error (pfile, "invalid #pragma poison directive");
815 if (hp->type == T_POISON)
816 ; /* It is allowed to poison the same identifier twice. */
819 if (hp->type != T_VOID)
820 cpp_warning (pfile, "poisoning existing macro \"%s\"", hp->name);
821 _cpp_free_definition (hp);
828 /* Mark the current header as a system header. This will suppress
829 some categories of warnings (notably those from -pedantic). It is
830 intended for use in system libraries that cannot be implemented in
831 conforming C, but cannot be certain that their headers appear in a
832 system include directory. To prevent abuse, it is rejected in the
833 primary source file. */
835 do_pragma_system_header (pfile)
838 cpp_buffer *ip = CPP_BUFFER (pfile);
839 if (CPP_PREV_BUFFER (ip) == NULL)
840 cpp_warning (pfile, "#pragma system_header outside include file");
842 cpp_make_system_header (pfile, ip, 1);
847 /* Check the modified date of the current include file against a specified
848 file. Issue a diagnostic, if the specified file is newer. We use this to
849 determine if a fixed header should be refixed. */
851 do_pragma_dependency (pfile)
859 if (parse_include (pfile, U"pragma dependency", 1, &name, &len, &ab))
862 left = ab ? '<' : '"';
863 right = ab ? '>' : '"';
865 ordering = _cpp_compare_file_date (pfile, name, len, ab);
867 cpp_warning (pfile, "cannot find source %c%s%c", left, name, right);
868 else if (ordering > 0)
870 const cpp_token *msg = _cpp_get_token (pfile);
872 cpp_warning (pfile, "current file is older than %c%.*s%c",
873 left, (int)len, name, right);
874 if (msg->type != CPP_EOF)
876 U_CHAR *text, *limit;
879 _cpp_dump_list (pfile, &pfile->token_list, msg, 0);
880 limit = pfile->limit;
882 /* There must be something non-whitespace after. */
885 cpp_warning (pfile, "%.*s", (int)(limit - text), text);
891 /* Just ignore #sccs, on systems where we define it at all. */
892 #ifdef SCCS_DIRECTIVE
895 cpp_reader *pfile ATTRIBUTE_UNUSED;
900 /* We've found an `#if' directive. If the only thing before it in
901 this file is white space, and if it is of the form
902 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
903 for inclusion of this file. (See redundant_include_p in cppfiles.c
904 for an explanation of controlling macros.) If so, return the
905 hash node for SYMBOL. Otherwise, return NULL. */
907 static const cpp_hashnode *
908 detect_if_not_defined (pfile)
911 const cpp_token *token;
912 cpp_hashnode *cmacro = 0;
914 /* We are guaranteed that tokens are consecutive and end in CPP_EOF. */
915 token = pfile->first_directive_token + 2;
917 if (token->type != CPP_NOT)
921 if (token->type != CPP_DEFINED)
925 if (token->type == CPP_OPEN_PAREN)
928 if (token->type != CPP_NAME)
931 cmacro = token->val.node;
933 if (token[-1].type == CPP_OPEN_PAREN)
936 if (token->type != CPP_CLOSE_PAREN)
941 if (token->type != CPP_EOF)
947 /* Parse an #ifdef or #ifndef directive. Returns the hash node of the
948 macro being tested, and issues various error messages. */
950 static const cpp_hashnode *
951 parse_ifdef (pfile, name)
956 const cpp_hashnode *node = 0;
958 const cpp_token *token = _cpp_get_token (pfile);
962 cpp_pedwarn (pfile, "#%s with no argument", name);
963 else if (type != CPP_NAME)
964 cpp_pedwarn (pfile, "#%s with invalid argument", name);
965 else if (_cpp_get_token (pfile)->type != CPP_EOF)
966 cpp_pedwarn (pfile, "garbage at end of #%s", name);
968 if (type == CPP_NAME)
969 node = token->val.node;
970 if (node && node->type == T_POISON)
972 cpp_error (pfile, "attempt to use poisoned identifier \"%s\"",
980 /* #ifdef is dead simple. */
986 const cpp_hashnode *node = 0;
988 if (! pfile->skipping)
989 node = parse_ifdef (pfile, dtable[T_IFDEF].name);
991 push_conditional (pfile, !(node && node->type != T_VOID), T_IFDEF, 0);
994 /* #ifndef is a tad more complex, because we need to check for a
995 no-reinclusion wrapper. */
1001 int start_of_file = 0;
1002 const cpp_hashnode *node = 0;
1004 if (! pfile->skipping)
1006 start_of_file = (pfile->token_list.flags & BEG_OF_FILE);
1007 node = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1010 push_conditional (pfile, node && node->type != T_VOID,
1011 T_IFNDEF, start_of_file ? node : 0);
1014 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1015 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1021 const cpp_hashnode *cmacro = 0;
1024 if (! pfile->skipping)
1026 if (pfile->token_list.flags & BEG_OF_FILE)
1027 cmacro = detect_if_not_defined (pfile);
1028 value = _cpp_parse_expr (pfile);
1030 push_conditional (pfile, value == 0, T_IF, cmacro);
1033 /* #else flips pfile->skipping and continues without changing
1034 if_stack; this is so that the error message for missing #endif's
1035 etc. will point to the original #if. */
1041 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1042 validate_else (pfile, dtable[T_ELSE].name);
1046 cpp_error (pfile, "#else without #if");
1049 if (ifs->type == T_ELSE)
1051 cpp_error (pfile, "#else after #else");
1052 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1053 "the conditional began here");
1056 /* #ifndef can't have its special treatment for containing the whole file
1057 if it has a #else clause. */
1060 if (! ifs->was_skipping)
1062 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1063 succeeded, so we mustn't do the else block. */
1064 if (pfile->skipping < 2)
1065 pfile->skipping = ! pfile->skipping;
1070 * handle a #elif directive by not changing if_stack either.
1071 * see the comment above do_else.
1078 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1082 cpp_error (pfile, "#elif without #if");
1085 if (ifs->type == T_ELSE)
1087 cpp_error (pfile, "#elif after #else");
1088 cpp_error_with_line (pfile, ifs->lineno, ifs->colno,
1089 "the conditional began here");
1093 if (ifs->was_skipping)
1094 return; /* Don't evaluate a nested #if */
1096 if (pfile->skipping != 1)
1098 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1102 pfile->skipping = ! _cpp_parse_expr (pfile);
1105 /* #endif pops the if stack and resets pfile->skipping. */
1111 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1113 validate_else (pfile, dtable[T_ENDIF].name);
1116 cpp_error (pfile, "#endif without #if");
1119 CPP_BUFFER (pfile)->if_stack = ifs->next;
1120 pfile->skipping = ifs->was_skipping;
1121 pfile->potential_control_macro = ifs->cmacro;
1122 obstack_free (pfile->buffer_ob, ifs);
1127 /* Push an if_stack entry and set pfile->skipping accordingly.
1128 If this is a #ifndef starting at the beginning of a file,
1129 CMACRO is the macro name tested by the #ifndef. */
1132 push_conditional (pfile, skip, type, cmacro)
1136 const cpp_hashnode *cmacro;
1138 struct if_stack *ifs;
1140 ifs = xobnew (pfile->buffer_ob, struct if_stack);
1141 ifs->lineno = _cpp_get_line (pfile, &ifs->colno);
1142 ifs->next = CPP_BUFFER (pfile)->if_stack;
1143 ifs->cmacro = cmacro;
1144 ifs->was_skipping = pfile->skipping;
1147 if (!pfile->skipping)
1148 pfile->skipping = skip;
1150 CPP_BUFFER (pfile)->if_stack = ifs;
1153 /* Issue -pedantic warning for text which is not a comment following
1154 an #else or #endif. */
1157 validate_else (pfile, directive)
1159 const U_CHAR *directive;
1161 if (CPP_PEDANTIC (pfile) && _cpp_get_token (pfile)->type != CPP_EOF)
1162 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1165 /* Called when we reach the end of a file. Walk back up the
1166 conditional stack till we reach its level at entry to this file,
1167 issuing error messages. Then force skipping off. */
1169 unwind_if_stack (pfile, pbuf)
1173 struct if_stack *ifs, *nifs;
1175 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1177 cpp_error_with_line (pfile, ifs->lineno, ifs->colno, "unterminated #%s",
1178 dtable[ifs->type].name);
1180 /* No need to free - they'll all go away with the buffer. */
1182 pfile->skipping = 0;
1185 /* Parses an assertion, returning a pointer to the hash node of the
1186 predicate, or 0 on error. If an answer was supplied, it is
1187 allocated and placed in ANSWERP, otherwise it is set to 0. We use
1188 _cpp_get_raw_token, since we cannot assume tokens are consecutive
1189 in a #if statement (we may be in a macro), and we don't want to
1192 _cpp_parse_assertion (pfile, answerp)
1194 struct answer **answerp;
1196 struct answer *answer = 0;
1199 const cpp_token *token, *predicate;
1200 const struct directive *d = pfile->token_list.directive;
1201 unsigned int len = 0;
1203 predicate = _cpp_get_raw_token (pfile);
1204 if (predicate->type == CPP_EOF)
1206 cpp_error (pfile, "assertion without predicate");
1209 else if (predicate->type != CPP_NAME)
1211 cpp_error (pfile, "predicate must be an identifier");
1215 token = _cpp_get_raw_token (pfile);
1216 if (token->type != CPP_OPEN_PAREN)
1218 /* #unassert and #if are OK without predicate. */
1219 if (d == &dtable[T_UNASSERT])
1221 if (token->type == CPP_EOF)
1224 else if (d != &dtable[T_ASSERT])
1226 _cpp_push_token (pfile, token);
1229 cpp_error (pfile, "missing '(' after predicate");
1233 /* Allocate a struct answer, and copy the answer to it. */
1234 answer = (struct answer *) xmalloc (sizeof (struct answer));
1235 list = &answer->list;
1236 _cpp_init_toklist (list, NO_DUMMY_TOKEN);
1242 token = _cpp_get_raw_token (pfile);
1244 if (token->type == CPP_EOF)
1246 cpp_error (pfile, "missing ')' to complete answer");
1249 if (token->type == CPP_CLOSE_PAREN)
1252 /* Copy the token. */
1253 _cpp_expand_token_space (list, 1);
1254 dest = &list->tokens[list->tokens_used++];
1257 if (TOKEN_SPELL (token) == SPELL_STRING)
1259 _cpp_expand_name_space (list, token->val.str.len);
1260 dest->val.str.text = list->namebuf + list->name_used;
1261 memcpy (list->namebuf + list->name_used,
1262 token->val.str.text, token->val.str.len);
1263 list->name_used += token->val.str.len;
1267 if (list->tokens_used == 0)
1269 cpp_error (pfile, "predicate's answer is empty");
1273 /* Drop whitespace at start. */
1274 list->tokens[0].flags &= ~PREV_WHITE;
1276 if ((d == &dtable[T_ASSERT] || d == &dtable[T_UNASSERT])
1277 && token[1].type != CPP_EOF)
1279 cpp_error (pfile, "junk at end of assertion");
1285 len = predicate->val.node->length;
1286 sym = alloca (len + 1);
1288 /* Prefix '#' to get it out of macro namespace. */
1290 memcpy (sym + 1, predicate->val.node->name, len);
1291 return cpp_lookup (pfile, sym, len + 1);
1294 FREE_ANSWER (answer);
1298 /* Returns a pointer to the pointer to the answer in the answer chain,
1299 or a pointer to NULL if the answer is not in the chain. */
1301 _cpp_find_answer (node, candidate)
1303 const cpp_toklist *candidate;
1305 struct answer **result;
1307 for (result = &node->value.answers; *result; result = &(*result)->next)
1308 if (_cpp_equiv_toklists (&(*result)->list, candidate))
1314 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1315 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1316 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1321 struct answer *new_answer;
1324 node = _cpp_parse_assertion (pfile, &new_answer);
1327 new_answer->next = 0;
1328 new_answer->list.line = pfile->token_list.line;
1329 new_answer->list.file = pfile->token_list.file;
1331 if (node->type == T_ASSERTION)
1333 if (*_cpp_find_answer (node, &new_answer->list))
1335 new_answer->next = node->value.answers;
1337 node->type = T_ASSERTION;
1338 node->value.answers = new_answer;
1343 cpp_warning (pfile, "\"%s\" re-asserted", node->name + 1);
1344 FREE_ANSWER (new_answer);
1352 struct answer *answer, *temp, *next;
1354 node = _cpp_parse_assertion (pfile, &answer);
1357 /* It isn't an error to #unassert something that isn't asserted. */
1358 if (node->type == T_ASSERTION)
1362 struct answer **p = _cpp_find_answer (node, &answer->list);
1370 if (node->value.answers == 0)
1371 node->type = T_VOID;
1375 for (temp = node->value.answers; temp; temp = next)
1380 node->type = T_VOID;
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);
1429 /* Process MACRO as if it appeared as the body of an #undef. */
1431 cpp_undef (pfile, macro)
1435 _cpp_run_directive (pfile, &dtable[T_UNDEF], macro, strlen (macro));
1438 /* Process the string STR as if it appeared as the body of a #assert. */
1440 cpp_assert (pfile, str)
1444 _cpp_run_directive (pfile, &dtable[T_ASSERT], str, strlen (str));
1447 /* Process STR as if it appeared as the body of an #unassert. */
1449 cpp_unassert (pfile, str)
1453 _cpp_run_directive (pfile, &dtable[T_UNASSERT], str, strlen (str));
1456 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1458 cpp_defined (pfile, id, len)
1463 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1464 if (hp->type == T_POISON)
1466 cpp_error (pfile, "attempt to use poisoned \"%s\"", hp->name);
1469 return (hp->type != T_VOID);
1472 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1473 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1474 as the new input buffer.
1475 Return the new buffer, or NULL on failure. */
1478 cpp_push_buffer (pfile, buffer, length)
1480 const U_CHAR *buffer;
1483 cpp_buffer *buf = CPP_BUFFER (pfile);
1485 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
1487 cpp_fatal (pfile, "#include nested too deep");
1490 if (pfile->cur_context > 0)
1492 cpp_ice (pfile, "buffer pushed with contexts stacked");
1493 _cpp_skip_rest_of_line (pfile);
1496 new = xobnew (pfile->buffer_ob, cpp_buffer);
1497 memset (new, 0, sizeof (cpp_buffer));
1499 new->line_base = new->buf = new->cur = buffer;
1500 new->rlimit = buffer + length;
1503 CPP_BUFFER (pfile) = new;
1508 cpp_pop_buffer (pfile)
1511 cpp_buffer *buf = CPP_BUFFER (pfile);
1513 unwind_if_stack (pfile, buf);
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--;
1520 return CPP_BUFFER (pfile);
1523 #define obstack_chunk_alloc xmalloc
1524 #define obstack_chunk_free free
1525 #define DSC(x) U x, sizeof x - 1
1527 _cpp_init_stacks (pfile)
1531 struct spec_nodes *s;
1533 pfile->buffer_ob = xnew (struct obstack);
1534 obstack_init (pfile->buffer_ob);
1536 /* Perhaps not the ideal place to put this. */
1537 pfile->spec_nodes = s = xnew (struct spec_nodes);
1538 s->n_L = cpp_lookup (pfile, DSC("L"));
1539 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
1540 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
1541 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
1542 for (i = 0; i < N_DIRECTIVES; i++)
1543 s->dirs[i] = cpp_lookup (pfile, dtable[i].name, dtable[i].length);
1547 _cpp_cleanup_stacks (pfile)
1550 obstack_free (pfile->buffer_ob, 0);
1551 free (pfile->buffer_ob);