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 /* `struct directive' defines one #-directive, including how to handle it. */
35 directive_handler func; /* Function to handle directive. */
36 const char *name; /* Name of directive. */
37 unsigned short length; /* Length of name. */
38 unsigned short flags; /* Flags describing this directive. */
41 /* Stack of conditionals currently in progress
42 (including both successful and failing conditionals). */
46 struct if_stack *next;
47 int lineno; /* line number where condition started */
48 int if_succeeded; /* truth of last condition in this group */
49 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
50 int type; /* type of last directive seen in this group */
52 typedef struct if_stack IF_STACK;
54 /* Forward declarations. */
56 static void validate_else PARAMS ((cpp_reader *, const char *));
57 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
58 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
59 static int conditional_skip PARAMS ((cpp_reader *, int, int,
61 static int skip_if_group PARAMS ((cpp_reader *));
62 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
64 static int read_line_number PARAMS ((cpp_reader *, int *));
65 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
66 static int consider_directive_while_skipping
67 PARAMS ((cpp_reader *, IF_STACK *));
68 static int get_macro_name PARAMS ((cpp_reader *));
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 /* Subroutine of do_define: determine the name of the macro to be
316 get_macro_name (pfile)
321 here = CPP_WRITTEN (pfile);
322 if (_cpp_get_directive_token (pfile) != CPP_NAME)
324 cpp_error (pfile, "`#define' must be followed by an identifier");
328 len = CPP_WRITTEN (pfile) - here;
329 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
331 cpp_error (pfile, "`defined' is not a legal macro name");
338 _cpp_skip_rest_of_line (pfile);
342 /* Process a #define command. */
353 int funlike = 0, empty = 0;
355 enum cpp_ttype token;
357 pfile->no_macro_expand++;
358 pfile->parsing_define_directive++;
359 CPP_OPTION (pfile, discard_comments)++;
361 here = CPP_WRITTEN (pfile);
362 len = get_macro_name (pfile);
366 /* Copy out the name so we can pop the token buffer. */
367 len = CPP_WRITTEN (pfile) - here;
368 sym = (U_CHAR *) alloca (len + 1);
369 memcpy (sym, pfile->token_buffer + here, len);
372 /* If the next character, with no intervening whitespace, is '(',
373 then this is a function-like macro.
374 XXX Layering violation. */
375 CPP_SET_MARK (pfile);
376 token = _cpp_get_directive_token (pfile);
377 if (token == CPP_VSPACE)
378 empty = 0; /* Empty definition of object like macro. */
379 else if (token == CPP_LPAREN && ADJACENT_TO_MARK (pfile))
381 else if (ADJACENT_TO_MARK (pfile))
382 /* If this is an object-like macro, C99 requires white space after
384 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
385 CPP_GOTO_MARK (pfile);
386 CPP_SET_WRITTEN (pfile, here);
390 def = _cpp_create_definition (pfile, funlike);
395 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
399 HASHNODE *hp = *slot;
401 /* Redefining a macro is ok if the definitions are the same. */
402 if (hp->type == T_MACRO)
403 ok = ! empty && ! _cpp_compare_defs (pfile, def, hp->value.defn);
404 else if (hp->type == T_EMPTY)
406 /* Redefining a constant is ok with -D. */
407 else if (hp->type == T_CONST || hp->type == T_STDC)
408 ok = ! pfile->done_initializing;
409 /* Otherwise it's not ok. */
412 /* Print the warning or error if it's not ok. */
415 if (hp->type == T_POISON)
416 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
418 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
419 if (hp->type == T_MACRO && pfile->done_initializing)
421 DEFINITION *d = hp->value.defn;
422 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
423 "this is the location of the previous definition");
426 if (hp->type != T_POISON)
428 /* Replace the old definition. */
429 if (hp->type == T_MACRO)
430 _cpp_free_definition (hp->value.defn);
439 hp->value.defn = def;
445 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
446 hp->value.defn = def;
450 if (CPP_OPTION (pfile, debug_output)
451 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
452 _cpp_dump_definition (pfile, sym, len, def);
453 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
454 pass_thru_directive (sym, len, pfile, T_DEFINE);
457 pfile->no_macro_expand--;
458 pfile->parsing_define_directive--;
459 CPP_OPTION (pfile, discard_comments)--;
463 /* Handle #include and #import. */
466 parse_include (pfile, name)
470 long old_written = CPP_WRITTEN (pfile);
471 enum cpp_ttype token;
474 pfile->parsing_include_directive++;
475 token = _cpp_get_directive_token (pfile);
476 pfile->parsing_include_directive--;
478 len = CPP_WRITTEN (pfile) - old_written;
480 if (token == CPP_STRING)
481 ; /* No special treatment required. */
483 else if (token == CPP_NAME)
485 /* Support '#include xyz' like VAX-C. It is taken as
486 '#include <xyz.h>' and generates a warning. */
487 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
490 /* Rewrite the token to <xyz.h>. */
491 CPP_RESERVE (pfile, 4);
493 memmove (pfile->token_buffer + old_written + 1,
494 pfile->token_buffer + old_written,
495 CPP_WRITTEN (pfile) - old_written);
496 pfile->token_buffer[old_written] = '<';
497 CPP_PUTS_Q (pfile, ".h>", 2);
502 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
503 CPP_SET_WRITTEN (pfile, old_written);
504 _cpp_skip_rest_of_line (pfile);
508 CPP_NUL_TERMINATE (pfile);
509 CPP_ADJUST_WRITTEN (pfile, 1);
511 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
513 cpp_error (pfile, "junk at end of `#%s'", name);
514 _cpp_skip_rest_of_line (pfile);
517 CPP_SET_WRITTEN (pfile, old_written);
520 cpp_error (pfile, "empty file name in `#%s'", name);
532 len = parse_include (pfile, dtable[T_INCLUDE].name);
535 token = alloca (len + 1);
536 strcpy (token, CPP_PWRITTEN (pfile));
538 if (CPP_OPTION (pfile, dump_includes))
539 pass_thru_directive (token, len, pfile, T_INCLUDE);
541 _cpp_execute_include (pfile, token, len, 0, 0);
552 if (CPP_OPTION (pfile, warn_import)
553 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
555 pfile->import_warning = 1;
557 "#import is obsolete, use an #ifndef wrapper in the header file");
560 len = parse_include (pfile, dtable[T_IMPORT].name);
563 token = alloca (len + 1);
564 strcpy (token, CPP_PWRITTEN (pfile));
566 if (CPP_OPTION (pfile, dump_includes))
567 pass_thru_directive (token, len, pfile, T_IMPORT);
569 _cpp_execute_include (pfile, token, len, 1, 0);
574 do_include_next (pfile)
579 struct file_name_list *search_start = 0;
581 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
584 token = alloca (len + 1);
585 strcpy (token, CPP_PWRITTEN (pfile));
587 if (CPP_OPTION (pfile, dump_includes))
588 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
590 /* For #include_next, skip in the search path past the dir in which the
591 containing file was found. Treat files specified using an absolute path
592 as if there are no more directories to search. Treat the primary source
593 file like any other included source, but generate a warning. */
594 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
596 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
597 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
600 cpp_warning (pfile, "#include_next in primary source file");
602 _cpp_execute_include (pfile, token, len, 0, search_start);
606 /* Subroutine of do_line. Read next token from PFILE without adding it to
607 the output buffer. If it is a number between 1 and 4, store it in *NUM
608 and return 1; otherwise, return 0 and complain if we aren't at the end
612 read_line_number (pfile, num)
616 long save_written = CPP_WRITTEN (pfile);
618 enum cpp_ttype token = _cpp_get_directive_token (pfile);
619 CPP_SET_WRITTEN (pfile, save_written);
620 p = pfile->token_buffer + save_written;
622 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
629 if (token != CPP_VSPACE && token != CPP_EOF)
630 cpp_error (pfile, "invalid format `#line' command");
635 /* Interpret #line command.
636 Note that the filename string (if any) is treated as if it were an
637 include filename. That means no escape handling. */
643 cpp_buffer *ip = CPP_BUFFER (pfile);
644 unsigned int new_lineno;
645 long old_written = CPP_WRITTEN (pfile);
646 enum cpp_ttype token;
649 token = _cpp_get_directive_token (pfile);
651 if (token != CPP_NUMBER)
653 cpp_error (pfile, "token after `#line' is not an integer");
654 goto bad_line_directive;
657 new_lineno = strtoul (pfile->token_buffer + old_written, &x, 10);
660 cpp_error (pfile, "token after `#line' is not an integer");
661 goto bad_line_directive;
663 CPP_SET_WRITTEN (pfile, old_written);
665 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
666 cpp_pedwarn (pfile, "line number out of range in `#line' command");
668 token = _cpp_get_directive_token (pfile);
670 if (token == CPP_STRING)
672 U_CHAR *fname = pfile->token_buffer + old_written + 1;
673 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
674 int action_number = 0;
676 if (read_line_number (pfile, &action_number))
678 if (CPP_PEDANTIC (pfile))
679 cpp_pedwarn (pfile, "garbage at end of `#line' command");
681 /* This is somewhat questionable: change the buffer stack
682 depth so that output_line_command thinks we've stacked
684 if (action_number == 1)
686 pfile->buffer_stack_depth++;
687 read_line_number (pfile, &action_number);
689 else if (action_number == 2)
691 pfile->buffer_stack_depth--;
692 read_line_number (pfile, &action_number);
694 if (action_number == 3)
696 ip->system_header_p = 1;
697 read_line_number (pfile, &action_number);
699 if (action_number == 4)
701 ip->system_header_p = 2;
702 read_line_number (pfile, &action_number);
708 if (strcmp (fname, ip->nominal_fname))
710 if (!strcmp (fname, ip->ihash->name))
711 ip->nominal_fname = ip->ihash->name;
713 ip->nominal_fname = _cpp_fake_ihash (pfile, fname);
716 else if (token != CPP_VSPACE && token != CPP_EOF)
718 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
719 goto bad_line_directive;
722 /* The Newline at the end of this line remains to be processed.
723 To put the next line at the specified line number,
724 we must store a line number now that is one less. */
725 ip->lineno = new_lineno - 1;
726 CPP_SET_WRITTEN (pfile, old_written);
730 _cpp_skip_rest_of_line (pfile);
731 CPP_SET_WRITTEN (pfile, old_written);
735 /* Remove the definition of a symbol from the symbol table.
736 According to the C standard, it is not an error to undef
737 something that has no definitions. */
745 long here = CPP_WRITTEN (pfile);
746 enum cpp_ttype token;
748 pfile->no_macro_expand++;
749 token = _cpp_get_directive_token (pfile);
750 pfile->no_macro_expand--;
752 if (token != CPP_NAME)
754 cpp_error (pfile, "token after #undef is not an identifier");
755 _cpp_skip_rest_of_line (pfile);
758 len = CPP_WRITTEN (pfile) - here;
760 token = _cpp_get_directive_token (pfile);
761 if (token != CPP_VSPACE)
763 cpp_pedwarn (pfile, "junk on line after #undef");
764 _cpp_skip_rest_of_line (pfile);
767 name = pfile->token_buffer + here;
768 CPP_SET_WRITTEN (pfile, here);
770 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
773 HASHNODE *hp = *slot;
774 if (hp->type == T_POISON)
775 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
778 /* If we are generating additional info for debugging (with -g) we
779 need to pass through all effective #undef commands. */
780 if (CPP_OPTION (pfile, debug_output))
781 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
783 if (hp->type != T_MACRO)
784 cpp_warning (pfile, "undefining `%s'", hp->name);
786 htab_clear_slot (pfile->hashtab, (void **)slot);
794 * Report an error detected by the program we are processing.
795 * Use the text of the line in the error message.
796 * (We use error because it prints the filename & line#.)
803 const U_CHAR *text, *limit;
805 _cpp_skip_hspace (pfile);
806 text = CPP_BUFFER (pfile)->cur;
807 _cpp_skip_rest_of_line (pfile);
808 limit = CPP_BUFFER (pfile)->cur;
810 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
815 * Report a warning detected by the program we are processing.
816 * Use the text of the line in the warning message, then continue.
823 const U_CHAR *text, *limit;
825 _cpp_skip_hspace (pfile);
826 text = CPP_BUFFER (pfile)->cur;
827 _cpp_skip_rest_of_line (pfile);
828 limit = CPP_BUFFER (pfile)->cur;
830 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
834 /* Report program identification. */
840 long old_written = CPP_WRITTEN (pfile);
842 CPP_PUTS (pfile, "#ident ", 7);
844 /* Next token should be a string constant. */
845 if (_cpp_get_directive_token (pfile) == CPP_STRING)
846 /* And then a newline. */
847 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
848 /* Good - ship it. */
851 cpp_error (pfile, "invalid #ident");
852 _cpp_skip_rest_of_line (pfile);
853 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
858 /* Pragmata handling. We handle some of these, and pass the rest on
859 to the front end. C99 defines three pragmas and says that no macro
860 expansion is to be performed on them; whether or not macro
861 expansion happens for other pragmas is implementation defined.
862 This implementation never macro-expands the text after #pragma.
864 We currently do not support the _Pragma operator. Support for that
865 has to be coordinated with the front end. Proposed implementation:
866 both #pragma blah blah and _Pragma("blah blah") become
867 __builtin_pragma(blah blah) and we teach the parser about that. */
869 /* Sub-handlers for the pragmas needing treatment here.
870 They return 1 if the token buffer is to be popped, 0 if not. */
871 static int do_pragma_once PARAMS ((cpp_reader *));
872 static int do_pragma_implementation PARAMS ((cpp_reader *));
873 static int do_pragma_poison PARAMS ((cpp_reader *));
874 static int do_pragma_default PARAMS ((cpp_reader *));
883 enum cpp_ttype token;
885 here = CPP_WRITTEN (pfile);
886 CPP_PUTS (pfile, "#pragma ", 8);
888 key = CPP_WRITTEN (pfile);
889 pfile->no_macro_expand++;
890 token = _cpp_get_directive_token (pfile);
891 if (token != CPP_NAME)
893 if (token == CPP_VSPACE)
899 buf = pfile->token_buffer + key;
900 CPP_PUTC (pfile, ' ');
902 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
904 pop = do_pragma_once (pfile);
905 else if (tokis ("implementation"))
906 pop = do_pragma_implementation (pfile);
907 else if (tokis ("poison"))
908 pop = do_pragma_poison (pfile);
910 pop = do_pragma_default (pfile);
913 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
917 CPP_SET_WRITTEN (pfile, here);
918 pfile->no_macro_expand--;
922 cpp_error (pfile, "malformed #pragma directive");
923 _cpp_skip_rest_of_line (pfile);
925 CPP_SET_WRITTEN (pfile, here);
926 pfile->no_macro_expand--;
931 do_pragma_default (pfile)
934 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
935 CPP_PUTC (pfile, ' ');
940 do_pragma_once (pfile)
943 cpp_buffer *ip = CPP_BUFFER (pfile);
945 /* Allow #pragma once in system headers, since that's not the user's
947 if (!ip->system_header_p)
948 cpp_warning (pfile, "`#pragma once' is obsolete");
950 if (CPP_PREV_BUFFER (ip) == NULL)
951 cpp_warning (pfile, "`#pragma once' outside include file");
953 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
959 do_pragma_implementation (pfile)
962 /* Be quiet about `#pragma implementation' for a file only if it hasn't
963 been included yet. */
964 enum cpp_ttype token;
965 long written = CPP_WRITTEN (pfile);
970 token = _cpp_get_directive_token (pfile);
971 if (token == CPP_VSPACE)
973 else if (token != CPP_STRING)
975 cpp_error (pfile, "malformed #pragma implementation");
979 /* Trim the leading and trailing quote marks from the string. */
980 name = pfile->token_buffer + written + 1;
981 len = CPP_PWRITTEN (pfile) - name;
982 copy = (U_CHAR *) alloca (len);
983 memcpy (copy, name, len - 1);
984 copy[len - 1] = '\0';
986 if (cpp_included (pfile, copy))
988 "`#pragma implementation' for `%s' appears after file is included",
994 do_pragma_poison (pfile)
997 /* Poison these symbols so that all subsequent usage produces an
1003 enum cpp_ttype token;
1007 /* As a rule, don't include #pragma poison commands in output,
1008 unless the user asks for them. */
1009 writeit = (CPP_OPTION (pfile, debug_output)
1010 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1011 || CPP_OPTION (pfile, dump_macros) == dump_names);
1015 written = CPP_WRITTEN (pfile);
1016 token = _cpp_get_directive_token (pfile);
1017 if (token == CPP_VSPACE)
1019 if (token != CPP_NAME)
1021 cpp_error (pfile, "invalid #pragma poison directive");
1022 _cpp_skip_rest_of_line (pfile);
1026 p = pfile->token_buffer + written;
1028 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1031 HASHNODE *hp = *slot;
1032 if (hp->type != T_POISON)
1034 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1035 if (hp->type == T_MACRO)
1036 _cpp_free_definition (hp->value.defn);
1038 hp->type = T_POISON;
1043 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1044 hp->value.cpval = 0;
1048 CPP_PUTC (pfile, ' ');
1053 /* Just ignore #sccs, on systems where we define it at all. */
1054 #ifdef SCCS_DIRECTIVE
1059 _cpp_skip_rest_of_line (pfile);
1064 /* We've found an `#if' directive. If the only thing before it in
1065 this file is white space, and if it is of the form
1066 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1067 for inclusion of this file. (See redundant_include_p in cppfiles.c
1068 for an explanation of controlling macros.) If so, return a
1069 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1072 detect_if_not_defined (pfile)
1075 U_CHAR *control_macro = 0;
1077 if (pfile->only_seen_white == 2)
1080 enum cpp_ttype token;
1083 int need_rparen = 0;
1085 /* Save state required for restore. */
1086 pfile->no_macro_expand++;
1087 CPP_SET_MARK (pfile);
1088 base_offset = CPP_WRITTEN (pfile);
1091 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1092 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1093 || CPP_PWRITTEN (pfile)[-1] != '!')
1096 /* ...then `defined', */
1097 token_offset = CPP_WRITTEN (pfile);
1098 token = _cpp_get_directive_token (pfile);
1099 if (token != CPP_NAME)
1101 ident = pfile->token_buffer + token_offset;
1102 CPP_NUL_TERMINATE (pfile);
1103 if (strcmp (ident, "defined"))
1106 /* ...then an optional '(' and the name, */
1107 token_offset = CPP_WRITTEN (pfile);
1108 token = _cpp_get_directive_token (pfile);
1109 if (token == CPP_LPAREN)
1111 token_offset = CPP_WRITTEN (pfile);
1112 token = _cpp_get_directive_token (pfile);
1113 if (token != CPP_NAME)
1117 else if (token != CPP_NAME)
1120 ident = pfile->token_buffer + token_offset;
1121 CPP_NUL_TERMINATE (pfile);
1123 /* ...then the ')', if necessary, */
1124 if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
1125 /* ...and make sure there's nothing else on the line. */
1126 && _cpp_get_directive_token (pfile) == CPP_VSPACE)
1127 control_macro = (U_CHAR *) xstrdup (ident);
1130 CPP_SET_WRITTEN (pfile, base_offset);
1131 pfile->no_macro_expand--;
1132 CPP_GOTO_MARK (pfile);
1135 return control_macro;
1139 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1140 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1147 U_CHAR *control_macro = detect_if_not_defined (pfile);
1148 int value = _cpp_parse_expr (pfile);
1149 return conditional_skip (pfile, value == 0, T_IF, control_macro);
1153 * handle a #elif directive by not changing if_stack either.
1154 * see the comment above do_else.
1161 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1163 cpp_error (pfile, "`#elif' not within a conditional");
1168 if (pfile->if_stack->type == T_ELSE)
1170 cpp_error (pfile, "`#elif' after `#else'");
1171 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1172 "the conditional began here");
1174 pfile->if_stack->type = T_ELIF;
1177 if (pfile->if_stack->if_succeeded)
1179 _cpp_skip_rest_of_line (pfile);
1180 return skip_if_group (pfile);
1182 if (_cpp_parse_expr (pfile) == 0)
1183 return skip_if_group (pfile);
1185 ++pfile->if_stack->if_succeeded; /* continue processing input */
1189 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1190 not defined; the macro tested is left in the token buffer (but
1194 parse_ifdef (pfile, name)
1200 enum cpp_ttype token;
1201 long old_written = CPP_WRITTEN (pfile);
1204 pfile->no_macro_expand++;
1205 token = _cpp_get_directive_token (pfile);
1206 pfile->no_macro_expand--;
1208 ident = pfile->token_buffer + old_written;
1209 len = CPP_WRITTEN (pfile) - old_written;
1211 if (token == CPP_VSPACE)
1213 if (! CPP_TRADITIONAL (pfile))
1214 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1218 else if (token == CPP_NAME)
1220 defined = cpp_defined (pfile, ident, len);
1221 CPP_NUL_TERMINATE (pfile);
1222 CPP_ADJUST_WRITTEN (pfile, 1);
1227 if (! CPP_TRADITIONAL (pfile))
1228 cpp_error (pfile, "`#%s' with invalid argument", name);
1231 if (!CPP_TRADITIONAL (pfile))
1233 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1236 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1238 _cpp_skip_rest_of_line (pfile);
1241 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1245 /* #ifdef is dead simple. */
1251 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1252 return conditional_skip (pfile, skip, T_IFDEF, 0);
1255 /* #ifndef is a tad more complex, because we need to check for a
1256 no-reinclusion wrapper. */
1262 int start_of_file, skip;
1263 U_CHAR *control_macro = 0;
1265 start_of_file = pfile->only_seen_white == 2;
1266 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1268 if (start_of_file && !skip)
1269 control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1271 return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1274 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1275 If this is a #ifndef starting at the beginning of a file,
1276 CONTROL_MACRO is the macro name tested by the #ifndef.
1277 Otherwise, CONTROL_MACRO is 0. */
1280 conditional_skip (pfile, skip, type, control_macro)
1284 U_CHAR *control_macro;
1288 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1289 temp->lineno = CPP_BUFFER (pfile)->lineno;
1290 temp->next = pfile->if_stack;
1291 temp->control_macro = control_macro;
1292 pfile->if_stack = temp;
1294 pfile->if_stack->type = type;
1297 return skip_if_group (pfile);
1299 ++pfile->if_stack->if_succeeded;
1303 /* Subroutine of skip_if_group. Examine one preprocessing directive
1304 and return 0 if skipping should continue, or the directive number
1305 of the directive that ends the block if it should halt.
1307 Also adjusts the if_stack as appropriate. The `#' has been read,
1308 but not the identifier. */
1311 consider_directive_while_skipping (pfile, stack)
1320 /* -traditional directives are recognized only with the # in column 1. */
1321 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1323 ident = CPP_WRITTEN (pfile);
1324 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1326 len = CPP_WRITTEN (pfile) - ident;
1328 for (i = 0; i < N_DIRECTIVES; i++)
1330 if (dtable[i].length == len
1331 && !strncmp (dtable[i].name, pfile->token_buffer + ident, len))
1332 goto real_directive;
1338 /* If it's not a directive of interest to us, return now. */
1339 if (ORIGIN (dtable[i].flags) != COND)
1342 /* First, deal with -traditional and -Wtraditional.
1343 All COND directives are from K+R. */
1347 if (CPP_TRADITIONAL (pfile))
1349 if (CPP_WTRADITIONAL (pfile))
1350 cpp_warning (pfile, "ignoring #%s because of its indented #",
1354 if (CPP_WTRADITIONAL (pfile))
1355 cpp_warning (pfile, "traditional C ignores %s with the # indented",
1362 cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1368 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1369 temp->lineno = CPP_BUFFER (pfile)->lineno;
1370 temp->next = pfile->if_stack;
1372 pfile->if_stack = temp;
1376 if (pfile->if_stack != stack)
1377 validate_else (pfile, dtable[i].name);
1380 if (pfile->if_stack == stack)
1383 pfile->if_stack->type = i;
1387 if (pfile->if_stack != stack)
1388 validate_else (pfile, dtable[i].name);
1390 if (pfile->if_stack == stack)
1393 temp = pfile->if_stack;
1394 pfile->if_stack = temp->next;
1400 /* Skip to #endif, #else, or #elif. Consumes the directive that
1401 causes it to stop, but not its argument. Returns the number of
1402 that directive, which must be passed back up to
1403 _cpp_handle_directive, which will execute it. */
1405 skip_if_group (pfile)
1408 enum cpp_ttype token;
1409 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1413 /* We are no longer at the start of the file. */
1414 pfile->only_seen_white = 0;
1416 old_written = CPP_WRITTEN (pfile);
1417 pfile->no_macro_expand++;
1420 /* We are at the end of a line. Only cpp_get_token knows how to
1421 advance the line number correctly. */
1422 token = cpp_get_token (pfile);
1423 if (token == CPP_POP)
1424 break; /* Caller will issue error. */
1425 else if (token != CPP_VSPACE)
1426 cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1427 CPP_SET_WRITTEN (pfile, old_written);
1429 token = _cpp_get_directive_token (pfile);
1431 if (token == CPP_DIRECTIVE)
1433 ret = consider_directive_while_skipping (pfile, save_if_stack);
1438 if (token != CPP_VSPACE)
1439 _cpp_skip_rest_of_line (pfile);
1441 CPP_SET_WRITTEN (pfile, old_written);
1442 pfile->no_macro_expand--;
1447 * handle a #else directive. Do this by just continuing processing
1448 * without changing if_stack ; this is so that the error message
1449 * for missing #endif's etc. will point to the original #if. It
1450 * is possible that something different would be better.
1457 validate_else (pfile, dtable[T_ELSE].name);
1458 _cpp_skip_rest_of_line (pfile);
1460 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1462 cpp_error (pfile, "`#else' not within a conditional");
1467 /* #ifndef can't have its special treatment for containing the whole file
1468 if it has a #else clause. */
1469 pfile->if_stack->control_macro = 0;
1471 if (pfile->if_stack->type == T_ELSE)
1473 cpp_error (pfile, "`#else' after `#else'");
1474 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1475 "the conditional began here");
1477 pfile->if_stack->type = T_ELSE;
1480 if (pfile->if_stack->if_succeeded)
1481 return skip_if_group (pfile);
1483 ++pfile->if_stack->if_succeeded; /* continue processing input */
1488 * unstack after #endif command
1495 validate_else (pfile, dtable[T_ENDIF].name);
1496 _cpp_skip_rest_of_line (pfile);
1498 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1499 cpp_error (pfile, "`#endif' not within a conditional");
1502 IF_STACK *temp = pfile->if_stack;
1503 pfile->if_stack = temp->next;
1504 if (temp->control_macro != 0)
1505 pfile->potential_control_macro = temp->control_macro;
1511 /* Issue -pedantic warning for text which is not a comment following
1512 an #else or #endif. Do not warn in system headers, as this is harmless
1513 and very common on old systems. */
1516 validate_else (pfile, directive)
1518 const char *directive;
1521 if (! CPP_PEDANTIC (pfile))
1524 old_written = CPP_WRITTEN (pfile);
1525 pfile->no_macro_expand++;
1526 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1528 "text following `#%s' violates ANSI standard", directive);
1529 CPP_SET_WRITTEN (pfile, old_written);
1530 pfile->no_macro_expand--;
1534 _cpp_handle_eof (pfile)
1537 struct if_stack *ifs, *nifs;
1539 /* Unwind the conditional stack and generate error messages. */
1540 for (ifs = pfile->if_stack;
1541 ifs != CPP_BUFFER (pfile)->if_stack;
1544 cpp_error_with_line (pfile, ifs->lineno, 0,
1545 "unterminated `#%s' conditional",
1546 dtable[ifs->type].name);
1551 pfile->if_stack = ifs;
1552 CPP_BUFFER (pfile)->seen_eof = 1;
1562 HASHNODE *base, *this;
1563 HASHNODE **bslot, **tslot;
1565 unsigned long bhash, thash;
1567 old_written = CPP_WRITTEN (pfile); /* remember where it starts */
1568 ret = _cpp_parse_assertion (pfile);
1573 cpp_error (pfile, "missing token-sequence in #assert");
1576 tlen = CPP_WRITTEN (pfile) - old_written;
1578 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1580 cpp_error (pfile, "junk at end of #assert");
1584 sym = pfile->token_buffer + old_written;
1585 blen = (U_CHAR *) strchr (sym, '(') - sym;
1586 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1589 cpp_warning (pfile, "%s re-asserted", sym);
1593 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1596 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1597 base->value.aschain = 0;
1602 if (base->type != T_ASSERT)
1604 /* Token clash - but with what?! */
1605 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1609 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1610 this->value.aschain = base->value.aschain;
1611 base->value.aschain = this;
1614 _cpp_skip_rest_of_line (pfile);
1615 CPP_SET_WRITTEN (pfile, old_written);
1626 long baselen, thislen;
1627 HASHNODE *base, *this, *next;
1629 old_written = CPP_WRITTEN (pfile);
1630 ret = _cpp_parse_assertion (pfile);
1633 thislen = CPP_WRITTEN (pfile) - old_written;
1635 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1637 cpp_error (pfile, "junk at end of #unassert");
1640 sym = pfile->token_buffer + old_written;
1641 CPP_SET_WRITTEN (pfile, old_written);
1645 base = _cpp_lookup (pfile, sym, thislen);
1647 goto error; /* It isn't an error to #undef what isn't #defined,
1648 so it isn't an error to #unassert what isn't
1649 #asserted either. */
1651 for (this = base->value.aschain; this; this = next)
1653 next = this->value.aschain;
1654 htab_remove_elt (pfile->hashtab, this);
1656 htab_remove_elt (pfile->hashtab, base);
1660 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1661 base = _cpp_lookup (pfile, sym, baselen);
1662 if (! base) goto error;
1663 this = _cpp_lookup (pfile, sym, thislen);
1664 if (! this) goto error;
1667 while (next->value.aschain != this)
1668 next = next->value.aschain;
1670 next->value.aschain = this->value.aschain;
1671 htab_remove_elt (pfile->hashtab, this);
1673 if (base->value.aschain == NULL)
1674 /* Last answer for this predicate deleted. */
1675 htab_remove_elt (pfile->hashtab, base);
1680 _cpp_skip_rest_of_line (pfile);
1681 CPP_SET_WRITTEN (pfile, old_written);
1685 /* These are for -D, -U, -A. */
1687 /* Process the string STR as if it appeared as the body of a #define.
1688 If STR is just an identifier, define it with value 1.
1689 If STR has anything after the identifier, then it should
1690 be identifier=definition. */
1693 cpp_define (pfile, str)
1700 p = strchr (str, '=');
1701 /* Copy the entire option so we can modify it.
1702 Change the first "=" in the string to a space. If there is none,
1703 tack " 1" on the end. Then add a newline and a NUL. */
1707 count = strlen (str) + 2;
1708 buf = alloca (count);
1709 memcpy (buf, str, count - 2);
1711 buf[count - 2] = '\n';
1712 buf[count - 1] = '\0';
1716 count = strlen (str) + 4;
1717 buf = alloca (count);
1718 memcpy (buf, str, count - 4);
1719 strcpy (&buf[count-4], " 1\n");
1722 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1725 cpp_pop_buffer (pfile);
1729 /* Process MACRO as if it appeared as the body of an #undef. */
1731 cpp_undef (pfile, macro)
1735 /* Copy the string so we can append a newline. */
1736 size_t len = strlen (macro);
1737 char *buf = alloca (len + 2);
1738 memcpy (buf, macro, len);
1740 buf[len + 1] = '\0';
1741 if (cpp_push_buffer (pfile, buf, len + 1))
1744 cpp_pop_buffer (pfile);
1748 /* Process the string STR as if it appeared as the body of a #assert. */
1750 cpp_assert (pfile, str)
1754 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1757 cpp_pop_buffer (pfile);
1761 /* Process STR as if it appeared as the body of an #unassert. */
1763 cpp_unassert (pfile, str)
1767 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1769 do_unassert (pfile);
1770 cpp_pop_buffer (pfile);
1774 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1776 cpp_defined (pfile, id, len)
1781 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1782 if (hp && hp->type == T_POISON)
1784 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1787 return (hp != NULL);