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. */
32 /* `struct directive' defines one #-directive, including how to handle it. */
36 directive_handler func; /* Function to handle directive. */
37 const char *name; /* Name of directive. */
38 unsigned short length; /* Length of name. */
39 unsigned short flags; /* Flags describing this directive. */
42 /* Stack of conditionals currently in progress
43 (including both successful and failing conditionals). */
47 struct if_stack *next;
48 int lineno; /* line number where condition started */
49 int if_succeeded; /* truth of last condition in this group */
50 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
51 int type; /* type of last directive seen in this group */
53 typedef struct if_stack IF_STACK;
55 /* Forward declarations. */
57 static void validate_else PARAMS ((cpp_reader *, const char *));
58 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
59 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
60 static int conditional_skip PARAMS ((cpp_reader *, int, int,
62 static int skip_if_group PARAMS ((cpp_reader *));
63 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
65 static int read_line_number PARAMS ((cpp_reader *, int *));
66 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
67 static int consider_directive_while_skipping
68 PARAMS ((cpp_reader *, IF_STACK *));
70 /* Values for the flags field of the table below. KANDR and COND
71 directives come from traditional (K&R) C. The difference is, if we
72 care about it while skipping a failed conditional block, its origin
73 is COND. STDC89 directives come from the 1989 C standard.
74 EXTENSION directives are extensions, with origins noted below. */
82 #define ORIGIN(f) ((f) & ORIGIN_MASK)
83 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
85 /* This is the table of directive handlers. It is ordered by
86 frequency of occurrence; the numbers at the end are directive
87 counts from all the source code I have lying around (egcs and libc
88 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
91 The entries with a dash and a name after the count are extensions,
92 of which all but #warning and #include_next are deprecated. The name
93 is where the extension appears to have come from. */
95 /* #sccs is not always recognized. */
97 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
99 # define SCCS_ENTRY /* nothing */
102 #define DIRECTIVE_TABLE \
103 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
104 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
105 D(endif, T_ENDIF, COND) /* 45855 */ \
106 D(ifdef, T_IFDEF, COND) /* 22000 */ \
107 D(if, T_IF, COND) /* 18162 */ \
108 D(else, T_ELSE, COND) /* 9863 */ \
109 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
110 D(undef, T_UNDEF, KANDR) /* 4837 */ \
111 D(line, T_LINE, KANDR) /* 2465 */ \
112 D(elif, T_ELIF, COND) /* 610 */ \
113 D(error, T_ERROR, STDC89) /* 475 */ \
114 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
115 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
116 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
117 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
118 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
119 D(assert, T_ASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
120 D(unassert, T_UNASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
123 /* Use the table to generate a series of prototypes, an enum for the
124 directive names, and an array of directive handlers. */
126 /* The directive-processing functions are declared to return int
127 instead of void, because some old compilers have trouble with
128 pointers to functions returning void. */
130 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
131 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
135 #define D(n, tag, f) tag,
143 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
144 #define D(name, t, flags) \
145 { CONCAT2(do_,name), STRINGX(name), sizeof STRINGX(name) - 1, flags },
146 static const struct directive dtable[] =
151 #undef DIRECTIVE_TABLE
153 /* Handle a possible # directive.
154 '#' has already been read. */
157 _cpp_handle_directive (pfile)
164 long old_written = CPP_WRITTEN (pfile);
167 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
169 cpp_ice (pfile, "handle_directive called on macro buffer");
173 /* -traditional directives are recognized only with the # in column 1. */
174 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
176 /* Scan the next token, then pretend we didn't. */
177 CPP_SET_MARK (pfile);
178 pfile->no_macro_expand++;
179 tok = _cpp_get_directive_token (pfile);
180 pfile->no_macro_expand--;
182 ident = pfile->token_buffer + old_written;
183 len = CPP_PWRITTEN (pfile) - ident;
184 CPP_SET_WRITTEN (pfile, old_written);
185 CPP_GOTO_MARK (pfile);
187 /* # followed by a number is equivalent to #line. Do not recognize
188 this form in assembly language source files. Complain about this
189 form if we're being pedantic, but not if this is regurgitated
190 input (preprocessed or fed back in by the C++ frontend). */
191 if (tok == CPP_NUMBER)
193 if (CPP_OPTION (pfile, lang_asm))
196 if (CPP_PEDANTIC (pfile)
197 && ! CPP_OPTION (pfile, preprocessed)
198 && ! CPP_BUFFER (pfile)->manual_pop)
199 cpp_pedwarn (pfile, "# followed by integer");
204 /* If we are rescanning preprocessed input, don't obey any directives
206 else if (CPP_OPTION (pfile, preprocessed))
209 /* A line of just # becomes blank. */
210 else if (tok == CPP_VSPACE)
213 /* A NAME token might in fact be a directive! */
214 else if (tok == CPP_NAME)
216 for (i = 0; i < N_DIRECTIVES; i++)
218 if (dtable[i].length == len
219 && !strncmp (dtable[i].name, ident, len))
222 /* Don't complain about invalid directives in assembly source,
223 we don't know where the comments are, and # may introduce
224 assembler pseudo-ops. */
225 if (!CPP_OPTION (pfile, lang_asm))
226 cpp_error (pfile, "invalid preprocessing directive #%s", ident);
229 /* And anything else means the # wasn't a directive marker. */
235 /* In -traditional mode, a directive is ignored unless its # is in
237 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
239 if (CPP_WTRADITIONAL (pfile))
240 cpp_warning (pfile, "ignoring #%s because of its indented #",
245 /* no_directives is set when we are parsing macro arguments. Directives
246 in macro arguments are undefined behavior (C99 6.10.3.11); this
247 implementation chooses to make them hard errors. */
248 if (pfile->no_directives)
250 cpp_error (pfile, "#%s may not be used inside a macro argument",
252 _cpp_skip_rest_of_line (pfile);
256 /* Issue -pedantic warnings for extended directives. */
257 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
258 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
260 /* -Wtraditional gives warnings about directives with inappropriate
262 if (CPP_WTRADITIONAL (pfile))
264 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
265 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
267 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
269 "suggest hiding #%s from traditional C with an indented #",
273 /* Unfortunately, it's necessary to scan the directive name again,
274 now we know we're going to consume it. FIXME. */
276 pfile->no_macro_expand++;
277 _cpp_get_directive_token (pfile);
278 pfile->no_macro_expand--;
279 CPP_SET_WRITTEN (pfile, old_written);
281 /* Some directives (e.g. #if) may return a request to execute
282 another directive handler immediately. No directive ever
283 requests that #define be executed immediately, so it is safe for
284 the loop to terminate when some function returns 0 (== T_DEFINE). */
285 while ((i = dtable[i].func (pfile)));
289 /* Pass a directive through to the output file.
290 BUF points to the contents of the directive, as a contiguous string.
291 LEN is the length of the string pointed to by BUF.
292 KEYWORD is the keyword-table entry for the directive. */
295 pass_thru_directive (buf, len, pfile, keyword)
301 const struct directive *kt = &dtable[keyword];
302 register unsigned klen = kt->length;
304 CPP_RESERVE (pfile, 1 + klen + len);
305 CPP_PUTC_Q (pfile, '#');
306 CPP_PUTS_Q (pfile, kt->name, klen);
307 if (len != 0 && buf[0] != ' ')
308 CPP_PUTC_Q (pfile, ' ');
309 CPP_PUTS_Q (pfile, buf, len);
312 /* Process a #define command. */
322 int funlike = 0, empty = 0;
324 cpp_toklist *list = &pfile->directbuf;
326 pfile->no_macro_expand++;
327 pfile->parsing_define_directive++;
328 CPP_OPTION (pfile, discard_comments)++;
330 _cpp_scan_line (pfile, list);
332 /* First token on the line must be a NAME. There must be at least
333 one token (the VSPACE at the end). */
334 if (list->tokens[0].type != CPP_NAME)
336 cpp_error_with_line (pfile, list->line, list->tokens[0].col,
337 "#define must be followed by an identifier");
341 sym = list->namebuf + list->tokens[0].val.name.offset;
342 len = list->tokens[0].val.name.len;
344 /* That NAME is not allowed to be "defined". (Not clear if the
345 standard requires this.) */
346 if (len == 7 && !strncmp (sym, "defined", 7))
348 cpp_error_with_line (pfile, list->line, list->tokens[0].col,
349 "\"defined\" is not a legal macro name");
354 if (list->tokens_used == 2 && list->tokens[1].type == CPP_VSPACE)
355 empty = 1; /* Empty definition of object-like macro. */
357 /* If the next character, with no intervening whitespace, is '(',
358 then this is a function-like macro. Otherwise it is an object-
359 like macro, and C99 requires whitespace after the name
361 else if (!(list->tokens[1].flags & HSPACE_BEFORE))
363 if (list->tokens[1].type == CPP_OPEN_PAREN)
367 "The C standard requires whitespace after #define %.*s",
373 def = _cpp_create_definition (pfile, list, funlike);
378 slot = _cpp_lookup_slot (pfile, sym, len, INSERT, &hash);
382 HASHNODE *hp = *slot;
384 /* Redefining a macro is ok if the definitions are the same. */
385 if (hp->type == T_MACRO)
386 ok = ! empty && ! _cpp_compare_defs (pfile, def, hp->value.defn);
387 else if (hp->type == T_EMPTY)
389 /* Redefining a constant is ok with -D. */
390 else if (hp->type == T_CONST || hp->type == T_STDC)
391 ok = ! pfile->done_initializing;
392 /* Otherwise it's not ok. */
395 /* Print the warning or error if it's not ok. */
398 if (hp->type == T_POISON)
399 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
401 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
402 if (hp->type == T_MACRO && pfile->done_initializing)
404 DEFINITION *d = hp->value.defn;
405 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
406 "this is the location of the previous definition");
409 if (hp->type != T_POISON)
411 /* Replace the old definition. */
412 if (hp->type == T_MACRO)
413 _cpp_free_definition (hp->value.defn);
422 hp->value.defn = def;
428 HASHNODE *hp = _cpp_make_hashnode (sym, len, empty ? T_EMPTY : T_MACRO,
430 hp->value.defn = def;
434 if (CPP_OPTION (pfile, debug_output)
435 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
436 _cpp_dump_definition (pfile, sym, len, def);
437 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
438 pass_thru_directive (sym, len, pfile, T_DEFINE);
441 pfile->no_macro_expand--;
442 pfile->parsing_define_directive--;
443 CPP_OPTION (pfile, discard_comments)--;
447 /* Handle #include and #import. */
450 parse_include (pfile, name)
454 long old_written = CPP_WRITTEN (pfile);
455 enum cpp_ttype token;
458 pfile->parsing_include_directive++;
459 token = _cpp_get_directive_token (pfile);
460 pfile->parsing_include_directive--;
462 len = CPP_WRITTEN (pfile) - old_written;
464 if (token == CPP_STRING)
465 ; /* No special treatment required. */
467 else if (token == CPP_NAME)
469 /* Support '#include xyz' like VAX-C. It is taken as
470 '#include <xyz.h>' and generates a warning. */
471 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
474 /* Rewrite the token to <xyz.h>. */
475 CPP_RESERVE (pfile, 4);
477 memmove (pfile->token_buffer + old_written + 1,
478 pfile->token_buffer + old_written,
479 CPP_WRITTEN (pfile) - old_written);
480 pfile->token_buffer[old_written] = '<';
481 CPP_PUTS_Q (pfile, ".h>", 2);
486 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
487 CPP_SET_WRITTEN (pfile, old_written);
488 _cpp_skip_rest_of_line (pfile);
492 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
494 cpp_error (pfile, "junk at end of `#%s'", name);
495 _cpp_skip_rest_of_line (pfile);
498 CPP_SET_WRITTEN (pfile, old_written);
501 cpp_error (pfile, "empty file name in `#%s'", name);
513 len = parse_include (pfile, dtable[T_INCLUDE].name);
516 token = alloca (len + 1);
517 memcpy (token, CPP_PWRITTEN (pfile), len);
520 if (CPP_OPTION (pfile, dump_includes))
521 pass_thru_directive (token, len, pfile, T_INCLUDE);
523 _cpp_execute_include (pfile, token, len, 0, 0);
534 if (CPP_OPTION (pfile, warn_import)
535 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
537 pfile->import_warning = 1;
539 "#import is obsolete, use an #ifndef wrapper in the header file");
542 len = parse_include (pfile, dtable[T_IMPORT].name);
545 token = alloca (len + 1);
546 memcpy (token, CPP_PWRITTEN (pfile), len);
549 if (CPP_OPTION (pfile, dump_includes))
550 pass_thru_directive (token, len, pfile, T_IMPORT);
552 _cpp_execute_include (pfile, token, len, 1, 0);
557 do_include_next (pfile)
562 struct file_name_list *search_start = 0;
564 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
567 token = alloca (len + 1);
568 memcpy (token, CPP_PWRITTEN (pfile), len);
571 if (CPP_OPTION (pfile, dump_includes))
572 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
574 /* For #include_next, skip in the search path past the dir in which the
575 containing file was found. Treat files specified using an absolute path
576 as if there are no more directories to search. Treat the primary source
577 file like any other included source, but generate a warning. */
578 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
580 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
581 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
584 cpp_warning (pfile, "#include_next in primary source file");
586 _cpp_execute_include (pfile, token, len, 0, search_start);
590 /* Subroutine of do_line. Read next token from PFILE without adding it to
591 the output buffer. If it is a number between 1 and 4, store it in *NUM
592 and return 1; otherwise, return 0 and complain if we aren't at the end
596 read_line_number (pfile, num)
600 long save_written = CPP_WRITTEN (pfile);
602 enum cpp_ttype token = _cpp_get_directive_token (pfile);
603 p = pfile->token_buffer + save_written;
605 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
606 && p[0] >= '1' && p[0] <= '4')
609 CPP_SET_WRITTEN (pfile, save_written);
614 if (token != CPP_VSPACE && token != CPP_EOF)
615 cpp_error (pfile, "invalid format `#line' command");
616 CPP_SET_WRITTEN (pfile, save_written);
621 /* Interpret #line command.
622 Note that the filename string (if any) is treated as if it were an
623 include filename. That means no escape handling. */
629 cpp_buffer *ip = CPP_BUFFER (pfile);
630 unsigned int new_lineno;
631 long old_written = CPP_WRITTEN (pfile);
632 enum cpp_ttype token;
635 token = _cpp_get_directive_token (pfile);
637 if (token != CPP_NUMBER)
639 cpp_error (pfile, "token after `#line' is not an integer");
640 goto bad_line_directive;
643 CPP_PUTC (pfile, '\0'); /* not terminated for us */
644 new_lineno = strtoul (pfile->token_buffer + old_written, &x, 10);
647 cpp_error (pfile, "token after `#line' is not an integer");
648 goto bad_line_directive;
650 CPP_SET_WRITTEN (pfile, old_written);
652 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
653 cpp_pedwarn (pfile, "line number out of range in `#line' command");
655 token = _cpp_get_directive_token (pfile);
657 if (token == CPP_STRING)
659 U_CHAR *fname = pfile->token_buffer + old_written + 1;
660 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
661 int action_number = 0;
663 if (read_line_number (pfile, &action_number))
665 if (CPP_PEDANTIC (pfile))
666 cpp_pedwarn (pfile, "garbage at end of `#line' command");
668 /* This is somewhat questionable: change the buffer stack
669 depth so that output_line_command thinks we've stacked
671 if (action_number == 1)
673 pfile->buffer_stack_depth++;
674 ip->system_header_p = 0;
675 read_line_number (pfile, &action_number);
677 else if (action_number == 2)
679 pfile->buffer_stack_depth--;
680 ip->system_header_p = 0;
681 read_line_number (pfile, &action_number);
683 if (action_number == 3)
685 ip->system_header_p = 1;
686 read_line_number (pfile, &action_number);
688 if (action_number == 4)
690 ip->system_header_p = 2;
691 read_line_number (pfile, &action_number);
697 if (strcmp (fname, ip->nominal_fname))
699 if (!strcmp (fname, ip->ihash->name))
700 ip->nominal_fname = ip->ihash->name;
702 ip->nominal_fname = _cpp_fake_ihash (pfile, fname);
705 else if (token != CPP_VSPACE && token != CPP_EOF)
707 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
708 goto bad_line_directive;
711 /* The Newline at the end of this line remains to be processed.
712 To put the next line at the specified line number,
713 we must store a line number now that is one less. */
714 ip->lineno = new_lineno - 1;
715 CPP_SET_WRITTEN (pfile, old_written);
719 _cpp_skip_rest_of_line (pfile);
720 CPP_SET_WRITTEN (pfile, old_written);
724 /* Remove the definition of a symbol from the symbol table.
725 According to the C standard, it is not an error to undef
726 something that has no definitions. */
734 long here = CPP_WRITTEN (pfile);
735 enum cpp_ttype token;
737 pfile->no_macro_expand++;
738 token = _cpp_get_directive_token (pfile);
739 pfile->no_macro_expand--;
741 if (token != CPP_NAME)
743 cpp_error (pfile, "token after #undef is not an identifier");
744 _cpp_skip_rest_of_line (pfile);
747 len = CPP_WRITTEN (pfile) - here;
749 token = _cpp_get_directive_token (pfile);
750 if (token != CPP_VSPACE)
752 cpp_pedwarn (pfile, "junk on line after #undef");
753 _cpp_skip_rest_of_line (pfile);
756 name = pfile->token_buffer + here;
757 CPP_SET_WRITTEN (pfile, here);
759 slot = _cpp_lookup_slot (pfile, name, len, NO_INSERT, 0);
762 HASHNODE *hp = *slot;
763 if (hp->type == T_POISON)
764 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
767 /* If we are generating additional info for debugging (with -g) we
768 need to pass through all effective #undef commands. */
769 if (CPP_OPTION (pfile, debug_output))
770 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
772 if (hp->type != T_MACRO && hp->type != T_EMPTY)
773 cpp_warning (pfile, "undefining `%s'", hp->name);
775 htab_clear_slot (pfile->hashtab, (void **)slot);
783 * Report an error detected by the program we are processing.
784 * Use the text of the line in the error message.
785 * (We use error because it prints the filename & line#.)
792 const U_CHAR *text, *limit;
794 _cpp_skip_hspace (pfile);
795 text = CPP_BUFFER (pfile)->cur;
796 _cpp_skip_rest_of_line (pfile);
797 limit = CPP_BUFFER (pfile)->cur;
799 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
804 * Report a warning detected by the program we are processing.
805 * Use the text of the line in the warning message, then continue.
812 const U_CHAR *text, *limit;
814 _cpp_skip_hspace (pfile);
815 text = CPP_BUFFER (pfile)->cur;
816 _cpp_skip_rest_of_line (pfile);
817 limit = CPP_BUFFER (pfile)->cur;
819 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
823 /* Report program identification. */
829 long old_written = CPP_WRITTEN (pfile);
831 CPP_PUTS (pfile, "#ident ", 7);
833 /* Next token should be a string constant. */
834 if (_cpp_get_directive_token (pfile) == CPP_STRING)
835 /* And then a newline. */
836 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
837 /* Good - ship it. */
840 cpp_error (pfile, "invalid #ident");
841 _cpp_skip_rest_of_line (pfile);
842 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
847 /* Pragmata handling. We handle some of these, and pass the rest on
848 to the front end. C99 defines three pragmas and says that no macro
849 expansion is to be performed on them; whether or not macro
850 expansion happens for other pragmas is implementation defined.
851 This implementation never macro-expands the text after #pragma.
853 We currently do not support the _Pragma operator. Support for that
854 has to be coordinated with the front end. Proposed implementation:
855 both #pragma blah blah and _Pragma("blah blah") become
856 __builtin_pragma(blah blah) and we teach the parser about that. */
858 /* Sub-handlers for the pragmas needing treatment here.
859 They return 1 if the token buffer is to be popped, 0 if not. */
860 static int do_pragma_once PARAMS ((cpp_reader *));
861 static int do_pragma_implementation PARAMS ((cpp_reader *));
862 static int do_pragma_poison PARAMS ((cpp_reader *));
863 static int do_pragma_default PARAMS ((cpp_reader *));
872 enum cpp_ttype token;
874 here = CPP_WRITTEN (pfile);
875 CPP_PUTS (pfile, "#pragma ", 8);
877 key = CPP_WRITTEN (pfile);
878 pfile->no_macro_expand++;
879 token = _cpp_get_directive_token (pfile);
880 if (token != CPP_NAME)
882 if (token == CPP_VSPACE)
888 buf = pfile->token_buffer + key;
889 CPP_PUTC (pfile, ' ');
891 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
893 pop = do_pragma_once (pfile);
894 else if (tokis ("implementation"))
895 pop = do_pragma_implementation (pfile);
896 else if (tokis ("poison"))
897 pop = do_pragma_poison (pfile);
899 pop = do_pragma_default (pfile);
902 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
906 CPP_SET_WRITTEN (pfile, here);
907 pfile->no_macro_expand--;
911 cpp_error (pfile, "malformed #pragma directive");
912 _cpp_skip_rest_of_line (pfile);
914 CPP_SET_WRITTEN (pfile, here);
915 pfile->no_macro_expand--;
920 do_pragma_default (pfile)
923 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
924 CPP_PUTC (pfile, ' ');
929 do_pragma_once (pfile)
932 cpp_buffer *ip = CPP_BUFFER (pfile);
934 /* Allow #pragma once in system headers, since that's not the user's
936 if (!ip->system_header_p)
937 cpp_warning (pfile, "`#pragma once' is obsolete");
939 if (CPP_PREV_BUFFER (ip) == NULL)
940 cpp_warning (pfile, "`#pragma once' outside include file");
942 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
948 do_pragma_implementation (pfile)
951 /* Be quiet about `#pragma implementation' for a file only if it hasn't
952 been included yet. */
953 enum cpp_ttype token;
954 long written = CPP_WRITTEN (pfile);
959 token = _cpp_get_directive_token (pfile);
960 if (token == CPP_VSPACE)
962 else if (token != CPP_STRING)
964 cpp_error (pfile, "malformed #pragma implementation");
968 /* Trim the leading and trailing quote marks from the string. */
969 name = pfile->token_buffer + written + 1;
970 len = CPP_PWRITTEN (pfile) - name;
971 copy = (U_CHAR *) alloca (len);
972 memcpy (copy, name, len - 1);
973 copy[len - 1] = '\0';
975 if (cpp_included (pfile, copy))
977 "`#pragma implementation' for `%s' appears after file is included",
983 do_pragma_poison (pfile)
986 /* Poison these symbols so that all subsequent usage produces an
992 enum cpp_ttype token;
996 /* As a rule, don't include #pragma poison commands in output,
997 unless the user asks for them. */
998 writeit = (CPP_OPTION (pfile, debug_output)
999 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1000 || CPP_OPTION (pfile, dump_macros) == dump_names);
1004 written = CPP_WRITTEN (pfile);
1005 token = _cpp_get_directive_token (pfile);
1006 if (token == CPP_VSPACE)
1008 if (token != CPP_NAME)
1010 cpp_error (pfile, "invalid #pragma poison directive");
1011 _cpp_skip_rest_of_line (pfile);
1015 p = pfile->token_buffer + written;
1016 len = CPP_PWRITTEN (pfile) - p;
1017 slot = _cpp_lookup_slot (pfile, p, len, INSERT, &hash);
1020 HASHNODE *hp = *slot;
1021 if (hp->type != T_POISON)
1023 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
1024 if (hp->type == T_MACRO)
1025 _cpp_free_definition (hp->value.defn);
1027 hp->type = T_POISON;
1032 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1033 hp->value.cpval = 0;
1037 CPP_PUTC (pfile, ' ');
1042 /* Just ignore #sccs, on systems where we define it at all. */
1043 #ifdef SCCS_DIRECTIVE
1048 _cpp_skip_rest_of_line (pfile);
1053 /* We've found an `#if' directive. If the only thing before it in
1054 this file is white space, and if it is of the form
1055 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1056 for inclusion of this file. (See redundant_include_p in cppfiles.c
1057 for an explanation of controlling macros.) If so, return a
1058 malloced copy of SYMBOL. Otherwise, return NULL. */
1061 detect_if_not_defined (pfile)
1064 U_CHAR *control_macro = 0;
1065 enum cpp_ttype token;
1066 unsigned int base_offset;
1067 unsigned int token_offset;
1068 unsigned int need_rparen = 0;
1069 unsigned int token_len;
1071 if (pfile->only_seen_white != 2)
1074 /* Save state required for restore. */
1075 pfile->no_macro_expand++;
1076 CPP_SET_MARK (pfile);
1077 base_offset = CPP_WRITTEN (pfile);
1080 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1081 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1082 || CPP_PWRITTEN (pfile)[-1] != '!')
1085 /* ...then `defined', */
1086 token_offset = CPP_WRITTEN (pfile);
1087 token = _cpp_get_directive_token (pfile);
1088 if (token != CPP_NAME)
1090 if (strncmp (pfile->token_buffer + token_offset, "defined", 7))
1093 /* ...then an optional '(' and the name, */
1094 token_offset = CPP_WRITTEN (pfile);
1095 token = _cpp_get_directive_token (pfile);
1096 if (token == CPP_OPEN_PAREN)
1098 token_offset = CPP_WRITTEN (pfile);
1100 token = _cpp_get_directive_token (pfile);
1102 if (token != CPP_NAME)
1105 token_len = CPP_WRITTEN (pfile) - token_offset;
1107 /* ...then the ')', if necessary, */
1108 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1111 /* ...and make sure there's nothing else on the line. */
1112 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1115 /* We have a legitimate controlling macro for this header. */
1116 control_macro = (U_CHAR *) xmalloc (token_len + 1);
1117 memcpy (control_macro, pfile->token_buffer + token_offset, token_len);
1118 control_macro[token_len] = '\0';
1121 CPP_SET_WRITTEN (pfile, base_offset);
1122 pfile->no_macro_expand--;
1123 CPP_GOTO_MARK (pfile);
1125 return control_macro;
1129 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1130 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1137 U_CHAR *control_macro = detect_if_not_defined (pfile);
1138 int value = _cpp_parse_expr (pfile);
1139 return conditional_skip (pfile, value == 0, T_IF, control_macro);
1143 * handle a #elif directive by not changing if_stack either.
1144 * see the comment above do_else.
1151 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1153 cpp_error (pfile, "`#elif' not within a conditional");
1158 if (pfile->if_stack->type == T_ELSE)
1160 cpp_error (pfile, "`#elif' after `#else'");
1161 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1162 "the conditional began here");
1164 pfile->if_stack->type = T_ELIF;
1167 if (pfile->if_stack->if_succeeded)
1169 _cpp_skip_rest_of_line (pfile);
1170 return skip_if_group (pfile);
1172 if (_cpp_parse_expr (pfile) == 0)
1173 return skip_if_group (pfile);
1175 ++pfile->if_stack->if_succeeded; /* continue processing input */
1179 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1180 not defined; the macro tested is left in the token buffer (but
1184 parse_ifdef (pfile, name)
1190 enum cpp_ttype token;
1191 long old_written = CPP_WRITTEN (pfile);
1194 pfile->no_macro_expand++;
1195 token = _cpp_get_directive_token (pfile);
1196 pfile->no_macro_expand--;
1198 ident = pfile->token_buffer + old_written;
1199 len = CPP_WRITTEN (pfile) - old_written;
1201 if (token == CPP_VSPACE)
1203 if (! CPP_TRADITIONAL (pfile))
1204 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1208 else if (token == CPP_NAME)
1210 defined = cpp_defined (pfile, ident, len);
1211 CPP_PUTC (pfile, '\0'); /* so it can be copied with xstrdup */
1216 if (! CPP_TRADITIONAL (pfile))
1217 cpp_error (pfile, "`#%s' with invalid argument", name);
1220 if (!CPP_TRADITIONAL (pfile))
1222 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1225 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1227 _cpp_skip_rest_of_line (pfile);
1230 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1234 /* #ifdef is dead simple. */
1240 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1241 return conditional_skip (pfile, skip, T_IFDEF, 0);
1244 /* #ifndef is a tad more complex, because we need to check for a
1245 no-reinclusion wrapper. */
1251 int start_of_file, skip;
1252 U_CHAR *control_macro = 0;
1254 start_of_file = pfile->only_seen_white == 2;
1255 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1257 if (start_of_file && !skip)
1258 control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1260 return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1263 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1264 If this is a #ifndef starting at the beginning of a file,
1265 CONTROL_MACRO is the macro name tested by the #ifndef.
1266 Otherwise, CONTROL_MACRO is 0. */
1269 conditional_skip (pfile, skip, type, control_macro)
1273 U_CHAR *control_macro;
1277 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1278 temp->lineno = CPP_BUFFER (pfile)->lineno;
1279 temp->next = pfile->if_stack;
1280 temp->control_macro = control_macro;
1281 pfile->if_stack = temp;
1283 pfile->if_stack->type = type;
1286 return skip_if_group (pfile);
1288 ++pfile->if_stack->if_succeeded;
1292 /* Subroutine of skip_if_group. Examine one preprocessing directive
1293 and return 0 if skipping should continue, or the directive number
1294 of the directive that ends the block if it should halt.
1296 Also adjusts the if_stack as appropriate. The `#' has been read,
1297 but not the identifier. */
1300 consider_directive_while_skipping (pfile, stack)
1309 /* -traditional directives are recognized only with the # in column 1. */
1310 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1312 ident = CPP_WRITTEN (pfile);
1313 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1315 len = CPP_WRITTEN (pfile) - ident;
1317 for (i = 0; i < N_DIRECTIVES; i++)
1319 if (dtable[i].length == len
1320 && !strncmp (dtable[i].name, pfile->token_buffer + ident, len))
1321 goto real_directive;
1327 /* If it's not a directive of interest to us, return now. */
1328 if (ORIGIN (dtable[i].flags) != COND)
1331 /* First, deal with -traditional and -Wtraditional.
1332 All COND directives are from K+R. */
1336 if (CPP_TRADITIONAL (pfile))
1338 if (CPP_WTRADITIONAL (pfile))
1339 cpp_warning (pfile, "ignoring #%s because of its indented #",
1343 if (CPP_WTRADITIONAL (pfile))
1344 cpp_warning (pfile, "traditional C ignores %s with the # indented",
1351 cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1357 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1358 temp->lineno = CPP_BUFFER (pfile)->lineno;
1359 temp->next = pfile->if_stack;
1361 pfile->if_stack = temp;
1365 if (pfile->if_stack != stack)
1366 validate_else (pfile, dtable[i].name);
1369 if (pfile->if_stack == stack)
1372 pfile->if_stack->type = i;
1376 if (pfile->if_stack != stack)
1377 validate_else (pfile, dtable[i].name);
1379 if (pfile->if_stack == stack)
1382 temp = pfile->if_stack;
1383 pfile->if_stack = temp->next;
1389 /* Skip to #endif, #else, or #elif. Consumes the directive that
1390 causes it to stop, but not its argument. Returns the number of
1391 that directive, which must be passed back up to
1392 _cpp_handle_directive, which will execute it. */
1394 skip_if_group (pfile)
1397 enum cpp_ttype token;
1398 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1402 /* We are no longer at the start of the file. */
1403 pfile->only_seen_white = 0;
1405 old_written = CPP_WRITTEN (pfile);
1406 pfile->no_macro_expand++;
1409 /* We are at the end of a line. Only cpp_get_token knows how to
1410 advance the line number correctly. */
1411 token = cpp_get_token (pfile);
1412 if (token == CPP_POP)
1413 break; /* Caller will issue error. */
1414 else if (token != CPP_VSPACE)
1415 cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1416 CPP_SET_WRITTEN (pfile, old_written);
1418 token = _cpp_get_directive_token (pfile);
1420 if (token == CPP_DIRECTIVE)
1422 ret = consider_directive_while_skipping (pfile, save_if_stack);
1427 if (token != CPP_VSPACE)
1428 _cpp_skip_rest_of_line (pfile);
1430 CPP_SET_WRITTEN (pfile, old_written);
1431 pfile->no_macro_expand--;
1436 * handle a #else directive. Do this by just continuing processing
1437 * without changing if_stack ; this is so that the error message
1438 * for missing #endif's etc. will point to the original #if. It
1439 * is possible that something different would be better.
1446 validate_else (pfile, dtable[T_ELSE].name);
1447 _cpp_skip_rest_of_line (pfile);
1449 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1451 cpp_error (pfile, "`#else' not within a conditional");
1456 /* #ifndef can't have its special treatment for containing the whole file
1457 if it has a #else clause. */
1458 pfile->if_stack->control_macro = 0;
1460 if (pfile->if_stack->type == T_ELSE)
1462 cpp_error (pfile, "`#else' after `#else'");
1463 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1464 "the conditional began here");
1466 pfile->if_stack->type = T_ELSE;
1469 if (pfile->if_stack->if_succeeded)
1470 return skip_if_group (pfile);
1472 ++pfile->if_stack->if_succeeded; /* continue processing input */
1477 * unstack after #endif command
1484 validate_else (pfile, dtable[T_ENDIF].name);
1485 _cpp_skip_rest_of_line (pfile);
1487 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1488 cpp_error (pfile, "`#endif' not within a conditional");
1491 IF_STACK *temp = pfile->if_stack;
1492 pfile->if_stack = temp->next;
1493 if (temp->control_macro != 0)
1494 pfile->potential_control_macro = temp->control_macro;
1500 /* Issue -pedantic warning for text which is not a comment following
1501 an #else or #endif. Do not warn in system headers, as this is harmless
1502 and very common on old systems. */
1505 validate_else (pfile, directive)
1507 const char *directive;
1510 if (! CPP_PEDANTIC (pfile))
1513 old_written = CPP_WRITTEN (pfile);
1514 pfile->no_macro_expand++;
1515 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1517 "text following `#%s' violates ANSI standard", directive);
1518 CPP_SET_WRITTEN (pfile, old_written);
1519 pfile->no_macro_expand--;
1523 _cpp_handle_eof (pfile)
1526 struct if_stack *ifs, *nifs;
1528 /* Unwind the conditional stack and generate error messages. */
1529 for (ifs = pfile->if_stack;
1530 ifs != CPP_BUFFER (pfile)->if_stack;
1533 cpp_error_with_line (pfile, ifs->lineno, 0,
1534 "unterminated `#%s' conditional",
1535 dtable[ifs->type].name);
1540 pfile->if_stack = ifs;
1541 CPP_BUFFER (pfile)->seen_eof = 1;
1551 HASHNODE *base, *this;
1552 HASHNODE **bslot, **tslot;
1554 unsigned long bhash, thash;
1556 old_written = CPP_WRITTEN (pfile); /* remember where it starts */
1557 ret = _cpp_parse_assertion (pfile);
1562 cpp_error (pfile, "missing token-sequence in #assert");
1565 tlen = CPP_WRITTEN (pfile) - old_written;
1567 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1569 cpp_error (pfile, "junk at end of #assert");
1573 sym = pfile->token_buffer + old_written;
1574 blen = (U_CHAR *) strchr (sym, '(') - sym;
1575 tslot = _cpp_lookup_slot (pfile, sym, tlen, INSERT, &thash);
1578 cpp_warning (pfile, "%s re-asserted", sym);
1582 bslot = _cpp_lookup_slot (pfile, sym, blen, INSERT, &bhash);
1585 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1586 base->value.aschain = 0;
1591 if (base->type != T_ASSERT)
1593 /* Token clash - but with what?! */
1594 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1598 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1599 this->value.aschain = base->value.aschain;
1600 base->value.aschain = this;
1603 _cpp_skip_rest_of_line (pfile);
1604 CPP_SET_WRITTEN (pfile, old_written);
1615 long baselen, thislen;
1616 HASHNODE *base, *this, *next;
1618 old_written = CPP_WRITTEN (pfile);
1619 ret = _cpp_parse_assertion (pfile);
1622 thislen = CPP_WRITTEN (pfile) - old_written;
1624 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1626 cpp_error (pfile, "junk at end of #unassert");
1629 sym = pfile->token_buffer + old_written;
1630 CPP_SET_WRITTEN (pfile, old_written);
1634 base = _cpp_lookup (pfile, sym, thislen);
1636 goto error; /* It isn't an error to #undef what isn't #defined,
1637 so it isn't an error to #unassert what isn't
1638 #asserted either. */
1640 for (this = base->value.aschain; this; this = next)
1642 next = this->value.aschain;
1643 htab_remove_elt (pfile->hashtab, this);
1645 htab_remove_elt (pfile->hashtab, base);
1649 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1650 base = _cpp_lookup (pfile, sym, baselen);
1651 if (! base) goto error;
1652 this = _cpp_lookup (pfile, sym, thislen);
1653 if (! this) goto error;
1656 while (next->value.aschain != this)
1657 next = next->value.aschain;
1659 next->value.aschain = this->value.aschain;
1660 htab_remove_elt (pfile->hashtab, this);
1662 if (base->value.aschain == NULL)
1663 /* Last answer for this predicate deleted. */
1664 htab_remove_elt (pfile->hashtab, base);
1669 _cpp_skip_rest_of_line (pfile);
1670 CPP_SET_WRITTEN (pfile, old_written);
1674 /* These are for -D, -U, -A. */
1676 /* Process the string STR as if it appeared as the body of a #define.
1677 If STR is just an identifier, define it with value 1.
1678 If STR has anything after the identifier, then it should
1679 be identifier=definition. */
1682 cpp_define (pfile, str)
1689 p = strchr (str, '=');
1690 /* Copy the entire option so we can modify it.
1691 Change the first "=" in the string to a space. If there is none,
1692 tack " 1" on the end. Then add a newline and a NUL. */
1696 count = strlen (str) + 2;
1697 buf = (char *) alloca (count);
1698 memcpy (buf, str, count - 2);
1700 buf[count - 2] = '\n';
1701 buf[count - 1] = '\0';
1705 count = strlen (str) + 4;
1706 buf = (char *) alloca (count);
1707 memcpy (buf, str, count - 4);
1708 strcpy (&buf[count-4], " 1\n");
1711 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1714 cpp_pop_buffer (pfile);
1718 /* Process MACRO as if it appeared as the body of an #undef. */
1720 cpp_undef (pfile, macro)
1724 /* Copy the string so we can append a newline. */
1725 size_t len = strlen (macro);
1726 char *buf = (char *) alloca (len + 2);
1727 memcpy (buf, macro, len);
1729 buf[len + 1] = '\0';
1730 if (cpp_push_buffer (pfile, buf, len + 1))
1733 cpp_pop_buffer (pfile);
1737 /* Process the string STR as if it appeared as the body of a #assert. */
1739 cpp_assert (pfile, str)
1743 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1746 cpp_pop_buffer (pfile);
1750 /* Process STR as if it appeared as the body of an #unassert. */
1752 cpp_unassert (pfile, str)
1756 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1758 do_unassert (pfile);
1759 cpp_pop_buffer (pfile);
1763 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1765 cpp_defined (pfile, id, len)
1770 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1771 if (hp && hp->type == T_POISON)
1773 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1776 return (hp != NULL);