2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
30 /* `struct directive' defines one #-directive, including how to handle it. */
34 directive_handler func; /* Function to handle directive. */
35 const U_CHAR *name; /* Name of directive. */
36 unsigned short length; /* Length of name. */
37 unsigned short flags; /* Flags describing this directive. */
40 /* Stack of conditionals currently in progress
41 (including both successful and failing conditionals). */
45 struct if_stack *next;
46 int lineno; /* line number where condition started */
47 int if_succeeded; /* truth of last condition in this group */
48 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
49 int type; /* type of last directive seen in this group */
51 typedef struct if_stack IF_STACK;
53 /* Forward declarations. */
55 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
56 static int parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
57 static unsigned int parse_include PARAMS ((cpp_reader *, const U_CHAR *));
58 static int conditional_skip PARAMS ((cpp_reader *, int, int,
60 static int skip_if_group PARAMS ((cpp_reader *));
61 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
63 static int read_line_number PARAMS ((cpp_reader *, int *));
64 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
65 static int consider_directive_while_skipping
66 PARAMS ((cpp_reader *, IF_STACK *));
68 /* Values for the flags field of the table below. KANDR and COND
69 directives come from traditional (K&R) C. The difference is, if we
70 care about it while skipping a failed conditional block, its origin
71 is COND. STDC89 directives come from the 1989 C standard.
72 EXTENSION directives are extensions, with origins noted below. */
80 #define ORIGIN(f) ((f) & ORIGIN_MASK)
81 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
83 /* This is the table of directive handlers. It is ordered by
84 frequency of occurrence; the numbers at the end are directive
85 counts from all the source code I have lying around (egcs and libc
86 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
89 The entries with a dash and a name after the count are extensions,
90 of which all but #warning and #include_next are deprecated. The name
91 is where the extension appears to have come from. */
93 /* #sccs is not always recognized. */
95 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
97 # define SCCS_ENTRY /* nothing */
100 #define DIRECTIVE_TABLE \
101 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
102 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
103 D(endif, T_ENDIF, COND) /* 45855 */ \
104 D(ifdef, T_IFDEF, COND) /* 22000 */ \
105 D(if, T_IF, COND) /* 18162 */ \
106 D(else, T_ELSE, COND) /* 9863 */ \
107 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
108 D(undef, T_UNDEF, KANDR) /* 4837 */ \
109 D(line, T_LINE, KANDR) /* 2465 */ \
110 D(elif, T_ELIF, COND) /* 610 */ \
111 D(error, T_ERROR, STDC89) /* 475 */ \
112 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
113 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
114 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
115 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
116 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
117 D(assert, T_ASSERT, EXTENSION) /* 0 SVR4 */ \
118 D(unassert, T_UNASSERT, EXTENSION) /* 0 SVR4 */ \
121 /* Use the table to generate a series of prototypes, an enum for the
122 directive names, and an array of directive handlers. */
124 /* The directive-processing functions are declared to return int
125 instead of void, because some old compilers have trouble with
126 pointers to functions returning void. */
128 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
129 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
133 #define D(n, tag, f) tag,
141 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
142 #define D(name, t, flags) \
143 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
144 sizeof STRINGX(name) - 1, flags },
145 static const struct directive dtable[] =
150 #undef DIRECTIVE_TABLE
152 /* Check if a token's name matches that of a known directive. Put in
153 this file to save exporting dtable and other unneeded information. */
155 _cpp_check_directive (list, token)
159 const U_CHAR *name = token->val.name.text;
160 size_t len = token->val.name.len;
164 list->flags &= ~SYNTAX_INCLUDE;
166 for (i = 0; i < N_DIRECTIVES; i++)
167 if (dtable[i].length == len && !ustrncmp (dtable[i].name, name, len))
170 if (dtable[i].flags & SYNTAX_INCLUDE)
171 list->flags |= SYNTAX_INCLUDE;
176 /* Handle a possible # directive.
177 '#' has already been read. */
180 _cpp_handle_directive (pfile)
187 long old_written = CPP_WRITTEN (pfile);
190 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
192 cpp_ice (pfile, "handle_directive called on macro buffer");
196 /* -traditional directives are recognized only with the # in column 1. */
197 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
199 /* Scan the next token, then pretend we didn't. */
200 CPP_SET_MARK (pfile);
201 pfile->no_macro_expand++;
202 tok = _cpp_get_directive_token (pfile);
203 pfile->no_macro_expand--;
205 ident = pfile->token_buffer + old_written;
206 len = CPP_PWRITTEN (pfile) - ident;
207 CPP_SET_WRITTEN (pfile, old_written);
208 CPP_GOTO_MARK (pfile);
210 /* # followed by a number is equivalent to #line. Do not recognize
211 this form in assembly language source files. Complain about this
212 form if we're being pedantic, but not if this is regurgitated
213 input (preprocessed or fed back in by the C++ frontend). */
214 if (tok == CPP_NUMBER)
216 if (CPP_OPTION (pfile, lang_asm))
219 if (CPP_PEDANTIC (pfile)
220 && CPP_BUFFER (pfile)->ihash
221 && ! CPP_OPTION (pfile, preprocessed))
222 cpp_pedwarn (pfile, "# followed by integer");
224 goto process_directive;
227 /* If we are rescanning preprocessed input, don't obey any directives
229 else if (CPP_OPTION (pfile, preprocessed))
232 /* A line of just # becomes blank. */
233 else if (tok == CPP_VSPACE)
236 /* A NAME token might in fact be a directive! */
237 else if (tok == CPP_NAME)
239 for (i = 0; i < N_DIRECTIVES; i++)
241 if (dtable[i].length == len
242 && !ustrncmp (dtable[i].name, ident, len))
245 /* Don't complain about invalid directives in assembly source,
246 we don't know where the comments are, and # may introduce
247 assembler pseudo-ops. */
248 if (!CPP_OPTION (pfile, lang_asm))
249 cpp_error (pfile, "invalid preprocessing directive #%s", ident);
252 /* And anything else means the # wasn't a directive marker. */
258 /* In -traditional mode, a directive is ignored unless its # is in
260 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
262 if (CPP_WTRADITIONAL (pfile))
263 cpp_warning (pfile, "ignoring #%s because of its indented #",
268 /* no_directives is set when we are parsing macro arguments. Directives
269 in macro arguments are undefined behavior (C99 6.10.3.11); this
270 implementation chooses to make them hard errors. */
271 if (pfile->no_directives)
273 cpp_error (pfile, "#%s may not be used inside a macro argument",
275 _cpp_skip_rest_of_line (pfile);
279 /* Issue -pedantic warnings for extended directives. */
280 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
281 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
283 /* -Wtraditional gives warnings about directives with inappropriate
285 if (CPP_WTRADITIONAL (pfile))
287 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
288 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
290 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
292 "suggest hiding #%s from traditional C with an indented #",
296 /* Unfortunately, it's necessary to scan the directive name again,
297 now we know we're going to consume it. FIXME. */
299 pfile->no_macro_expand++;
300 _cpp_get_directive_token (pfile);
301 pfile->no_macro_expand--;
302 CPP_SET_WRITTEN (pfile, old_written);
305 /* Some directives (e.g. #if) may return a request to execute
306 another directive handler immediately. No directive ever
307 requests that #define be executed immediately, so it is safe for
308 the loop to terminate when some function returns 0 (== T_DEFINE). */
309 while ((i = dtable[i].func (pfile)));
313 /* Pass a directive through to the output file.
314 BUF points to the contents of the directive, as a contiguous string.
315 LEN is the length of the string pointed to by BUF.
316 KEYWORD is the keyword-table entry for the directive. */
319 pass_thru_directive (buf, len, pfile, keyword)
325 const struct directive *kt = &dtable[keyword];
326 register unsigned klen = kt->length;
328 CPP_RESERVE (pfile, 1 + klen + len);
329 CPP_PUTC_Q (pfile, '#');
330 CPP_PUTS_Q (pfile, kt->name, klen);
331 if (len != 0 && buf[0] != ' ')
332 CPP_PUTC_Q (pfile, ' ');
333 CPP_PUTS_Q (pfile, buf, len);
336 /* Process a #define command. */
345 cpp_toklist *list = &pfile->directbuf;
347 pfile->no_macro_expand++;
348 CPP_OPTION (pfile, discard_comments)++;
350 _cpp_scan_until (pfile, list, CPP_VSPACE);
352 /* First token on the line must be a NAME. There may not be any
353 tokens in the list (if we had #define all by itself on a line). */
354 if (list->tokens_used == 0
355 || TOK_TYPE (list, 0) != CPP_NAME)
357 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
358 "#define must be followed by an identifier");
362 sym = TOK_NAME (list, 0);
363 len = TOK_LEN (list, 0);
365 /* That NAME is not allowed to be "defined". (Not clear if the
366 standard requires this.) */
367 if (len == 7 && !ustrncmp (sym, U"defined", 7))
369 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
370 "\"defined\" is not a legal macro name");
374 node = _cpp_lookup (pfile, sym, len);
375 /* Check for poisoned identifiers now. All other checks
376 are done in cpphash.c. */
377 if (node->type == T_POISON)
379 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
383 if (_cpp_create_definition (pfile, list, node) == 0)
386 if (CPP_OPTION (pfile, debug_output)
387 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
388 _cpp_dump_definition (pfile, node);
389 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
390 pass_thru_directive (sym, len, pfile, T_DEFINE);
393 pfile->no_macro_expand--;
394 CPP_OPTION (pfile, discard_comments)--;
398 /* Handle #include and #import. */
401 parse_include (pfile, name)
405 long old_written = CPP_WRITTEN (pfile);
406 enum cpp_ttype token;
409 pfile->parsing_include_directive++;
410 token = _cpp_get_directive_token (pfile);
411 pfile->parsing_include_directive--;
413 len = CPP_WRITTEN (pfile) - old_written;
415 if (token == CPP_STRING)
416 ; /* No special treatment required. */
418 else if (token == CPP_NAME)
420 /* Support '#include xyz' like VAX-C. It is taken as
421 '#include <xyz.h>' and generates a warning. */
422 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
425 /* Rewrite the token to <xyz.h>. */
426 CPP_RESERVE (pfile, 4);
428 memmove (pfile->token_buffer + old_written + 1,
429 pfile->token_buffer + old_written,
430 CPP_WRITTEN (pfile) - old_written);
431 pfile->token_buffer[old_written] = '<';
432 CPP_PUTS_Q (pfile, ".h>", 2);
437 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
438 CPP_SET_WRITTEN (pfile, old_written);
439 _cpp_skip_rest_of_line (pfile);
443 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
445 cpp_error (pfile, "junk at end of `#%s'", name);
446 _cpp_skip_rest_of_line (pfile);
449 CPP_SET_WRITTEN (pfile, old_written);
452 cpp_error (pfile, "empty file name in `#%s'", name);
464 len = parse_include (pfile, dtable[T_INCLUDE].name);
467 token = (U_CHAR *) alloca (len + 1);
468 memcpy (token, CPP_PWRITTEN (pfile), len);
471 if (CPP_OPTION (pfile, dump_includes))
472 pass_thru_directive (token, len, pfile, T_INCLUDE);
474 _cpp_execute_include (pfile, token, len, 0, 0);
485 if (CPP_OPTION (pfile, warn_import)
486 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
488 pfile->import_warning = 1;
490 "#import is obsolete, use an #ifndef wrapper in the header file");
493 len = parse_include (pfile, dtable[T_IMPORT].name);
496 token = (U_CHAR *) alloca (len + 1);
497 memcpy (token, CPP_PWRITTEN (pfile), len);
500 if (CPP_OPTION (pfile, dump_includes))
501 pass_thru_directive (token, len, pfile, T_IMPORT);
503 _cpp_execute_include (pfile, token, len, 1, 0);
508 do_include_next (pfile)
513 struct file_name_list *search_start = 0;
515 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
518 token = (U_CHAR *) alloca (len + 1);
519 memcpy (token, CPP_PWRITTEN (pfile), len);
522 if (CPP_OPTION (pfile, dump_includes))
523 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
525 /* For #include_next, skip in the search path past the dir in which the
526 containing file was found. Treat files specified using an absolute path
527 as if there are no more directories to search. Treat the primary source
528 file like any other included source, but generate a warning. */
529 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
531 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
532 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
535 cpp_warning (pfile, "#include_next in primary source file");
537 _cpp_execute_include (pfile, token, len, 0, search_start);
541 /* Subroutine of do_line. Read next token from PFILE without adding it to
542 the output buffer. If it is a number between 1 and 4, store it in *NUM
543 and return 1; otherwise, return 0 and complain if we aren't at the end
547 read_line_number (pfile, num)
551 long save_written = CPP_WRITTEN (pfile);
553 enum cpp_ttype token = _cpp_get_directive_token (pfile);
554 p = pfile->token_buffer + save_written;
556 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
557 && p[0] >= '1' && p[0] <= '4')
560 CPP_SET_WRITTEN (pfile, save_written);
565 if (token != CPP_VSPACE && token != CPP_EOF)
566 cpp_error (pfile, "invalid format `#line' command");
567 CPP_SET_WRITTEN (pfile, save_written);
572 /* Interpret #line command.
573 Note that the filename string (if any) is treated as if it were an
574 include filename. That means no escape handling. */
580 cpp_buffer *ip = CPP_BUFFER (pfile);
581 unsigned int new_lineno;
582 long old_written = CPP_WRITTEN (pfile);
583 enum cpp_ttype token;
586 token = _cpp_get_directive_token (pfile);
588 if (token != CPP_NUMBER)
590 cpp_error (pfile, "token after `#line' is not an integer");
591 goto bad_line_directive;
594 CPP_PUTC (pfile, '\0'); /* not terminated for us */
595 new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
599 cpp_error (pfile, "token after `#line' is not an integer");
600 goto bad_line_directive;
602 CPP_SET_WRITTEN (pfile, old_written);
604 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
605 cpp_pedwarn (pfile, "line number out of range in `#line' command");
607 token = _cpp_get_directive_token (pfile);
609 if (token == CPP_STRING)
611 U_CHAR *fname = pfile->token_buffer + old_written + 1;
612 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
613 int action_number = 0;
615 if (read_line_number (pfile, &action_number))
617 if (CPP_PEDANTIC (pfile))
618 cpp_pedwarn (pfile, "garbage at end of `#line' command");
620 /* This is somewhat questionable: change the buffer stack
621 depth so that output_line_command thinks we've stacked
623 if (action_number == 1)
625 pfile->buffer_stack_depth++;
626 ip->system_header_p = 0;
627 read_line_number (pfile, &action_number);
629 else if (action_number == 2)
631 pfile->buffer_stack_depth--;
632 ip->system_header_p = 0;
633 read_line_number (pfile, &action_number);
635 if (action_number == 3)
637 ip->system_header_p = 1;
638 read_line_number (pfile, &action_number);
640 if (action_number == 4)
642 ip->system_header_p = 2;
643 read_line_number (pfile, &action_number);
649 if (strcmp ((const char *)fname, ip->nominal_fname))
651 if (!strcmp ((const char *)fname, ip->ihash->name))
652 ip->nominal_fname = ip->ihash->name;
654 ip->nominal_fname = _cpp_fake_ihash (pfile, (const char *)fname);
657 else if (token != CPP_VSPACE && token != CPP_EOF)
659 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
660 goto bad_line_directive;
663 /* The Newline at the end of this line remains to be processed.
664 To put the next line at the specified line number,
665 we must store a line number now that is one less. */
666 ip->lineno = new_lineno - 1;
667 CPP_SET_WRITTEN (pfile, old_written);
671 _cpp_skip_rest_of_line (pfile);
672 CPP_SET_WRITTEN (pfile, old_written);
676 /* Remove the definition of a symbol from the symbol table.
677 According to the C standard, it is not an error to undef
678 something that has no definitions. */
686 long here = CPP_WRITTEN (pfile);
687 enum cpp_ttype token;
689 pfile->no_macro_expand++;
690 token = _cpp_get_directive_token (pfile);
691 pfile->no_macro_expand--;
693 if (token != CPP_NAME)
695 cpp_error (pfile, "token after #undef is not an identifier");
696 _cpp_skip_rest_of_line (pfile);
699 len = CPP_WRITTEN (pfile) - here;
701 token = _cpp_get_directive_token (pfile);
702 if (token != CPP_VSPACE)
704 cpp_pedwarn (pfile, "junk on line after #undef");
705 _cpp_skip_rest_of_line (pfile);
708 name = pfile->token_buffer + here;
709 CPP_SET_WRITTEN (pfile, here);
711 hp = _cpp_lookup (pfile, name, len);
712 if (hp->type == T_VOID)
713 ; /* Not defined in the first place - do nothing. */
714 else if (hp->type == T_POISON)
715 cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
718 /* If we are generating additional info for debugging (with -g) we
719 need to pass through all effective #undef commands. */
720 if (CPP_OPTION (pfile, debug_output))
721 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
723 if (hp->type != T_MACRO && hp->type != T_FMACRO
724 && hp->type != T_EMPTY && hp->type != T_IDENTITY)
725 cpp_warning (pfile, "undefining `%s'", hp->name);
727 _cpp_free_definition (hp);
735 * Report an error detected by the program we are processing.
736 * Use the text of the line in the error message.
737 * (We use error because it prints the filename & line#.)
744 const U_CHAR *text, *limit;
746 _cpp_skip_hspace (pfile);
747 text = CPP_BUFFER (pfile)->cur;
748 _cpp_skip_rest_of_line (pfile);
749 limit = CPP_BUFFER (pfile)->cur;
751 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
756 * Report a warning detected by the program we are processing.
757 * Use the text of the line in the warning message, then continue.
764 const U_CHAR *text, *limit;
766 _cpp_skip_hspace (pfile);
767 text = CPP_BUFFER (pfile)->cur;
768 _cpp_skip_rest_of_line (pfile);
769 limit = CPP_BUFFER (pfile)->cur;
771 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
775 /* Report program identification. */
781 long old_written = CPP_WRITTEN (pfile);
783 CPP_PUTS (pfile, "#ident ", 7);
785 /* Next token should be a string constant. */
786 if (_cpp_get_directive_token (pfile) == CPP_STRING)
787 /* And then a newline. */
788 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
789 /* Good - ship it. */
792 cpp_error (pfile, "invalid #ident");
793 _cpp_skip_rest_of_line (pfile);
794 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
799 /* Pragmata handling. We handle some of these, and pass the rest on
800 to the front end. C99 defines three pragmas and says that no macro
801 expansion is to be performed on them; whether or not macro
802 expansion happens for other pragmas is implementation defined.
803 This implementation never macro-expands the text after #pragma.
805 We currently do not support the _Pragma operator. Support for that
806 has to be coordinated with the front end. Proposed implementation:
807 both #pragma blah blah and _Pragma("blah blah") become
808 __builtin_pragma(blah blah) and we teach the parser about that. */
810 /* Sub-handlers for the pragmas needing treatment here.
811 They return 1 if the token buffer is to be popped, 0 if not. */
812 static int do_pragma_once PARAMS ((cpp_reader *));
813 static int do_pragma_implementation PARAMS ((cpp_reader *));
814 static int do_pragma_poison PARAMS ((cpp_reader *));
815 static int do_pragma_system_header PARAMS ((cpp_reader *));
816 static int do_pragma_default PARAMS ((cpp_reader *));
825 enum cpp_ttype token;
827 here = CPP_WRITTEN (pfile);
828 CPP_PUTS (pfile, "#pragma ", 8);
830 key = CPP_WRITTEN (pfile);
831 pfile->no_macro_expand++;
832 token = _cpp_get_directive_token (pfile);
833 if (token != CPP_NAME)
835 if (token == CPP_VSPACE)
841 buf = pfile->token_buffer + key;
842 CPP_PUTC (pfile, ' ');
844 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
846 pop = do_pragma_once (pfile);
847 else if (tokis ("implementation"))
848 pop = do_pragma_implementation (pfile);
849 else if (tokis ("poison"))
850 pop = do_pragma_poison (pfile);
851 else if (tokis ("system_header"))
852 pop = do_pragma_system_header (pfile);
854 pop = do_pragma_default (pfile);
857 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
861 CPP_SET_WRITTEN (pfile, here);
862 pfile->no_macro_expand--;
866 cpp_error (pfile, "malformed #pragma directive");
867 _cpp_skip_rest_of_line (pfile);
869 CPP_SET_WRITTEN (pfile, here);
870 pfile->no_macro_expand--;
875 do_pragma_default (pfile)
878 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
879 CPP_PUTC (pfile, ' ');
884 do_pragma_once (pfile)
887 cpp_buffer *ip = CPP_BUFFER (pfile);
889 /* Allow #pragma once in system headers, since that's not the user's
891 if (!ip->system_header_p)
892 cpp_warning (pfile, "`#pragma once' is obsolete");
894 if (CPP_PREV_BUFFER (ip) == NULL)
895 cpp_warning (pfile, "`#pragma once' outside include file");
897 ip->ihash->control_macro = U""; /* never repeat */
903 do_pragma_implementation (pfile)
906 /* Be quiet about `#pragma implementation' for a file only if it hasn't
907 been included yet. */
908 enum cpp_ttype token;
909 long written = CPP_WRITTEN (pfile);
914 token = _cpp_get_directive_token (pfile);
915 if (token == CPP_VSPACE)
917 else if (token != CPP_STRING)
919 cpp_error (pfile, "malformed #pragma implementation");
923 /* Trim the leading and trailing quote marks from the string. */
924 name = pfile->token_buffer + written + 1;
925 len = CPP_PWRITTEN (pfile) - name;
927 memcpy (copy, name, len - 1);
928 copy[len - 1] = '\0';
930 if (cpp_included (pfile, copy))
932 "`#pragma implementation' for `%s' appears after file is included",
938 do_pragma_poison (pfile)
941 /* Poison these symbols so that all subsequent usage produces an
947 enum cpp_ttype token;
950 /* As a rule, don't include #pragma poison commands in output,
951 unless the user asks for them. */
952 writeit = (CPP_OPTION (pfile, debug_output)
953 || CPP_OPTION (pfile, dump_macros) == dump_definitions
954 || CPP_OPTION (pfile, dump_macros) == dump_names);
958 written = CPP_WRITTEN (pfile);
959 token = _cpp_get_directive_token (pfile);
960 if (token == CPP_VSPACE)
962 if (token != CPP_NAME)
964 cpp_error (pfile, "invalid #pragma poison directive");
965 _cpp_skip_rest_of_line (pfile);
969 p = pfile->token_buffer + written;
970 len = CPP_PWRITTEN (pfile) - p;
971 hp = _cpp_lookup (pfile, p, len);
972 if (hp->type == T_POISON)
973 ; /* It is allowed to poison the same identifier twice. */
976 if (hp->type != T_VOID)
977 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
978 _cpp_free_definition (hp);
982 CPP_PUTC (pfile, ' ');
987 /* Mark the current header as a system header. This will suppress
988 some categories of warnings (notably those from -pedantic). It is
989 intended for use in system libraries that cannot be implemented in
990 conforming C, but cannot be certain that their headers appear in a
991 system include directory. To prevent abuse, it is rejected in the
992 primary source file. */
994 do_pragma_system_header (pfile)
997 cpp_buffer *ip = cpp_file_buffer (pfile);
998 if (CPP_PREV_BUFFER (ip) == NULL)
999 cpp_warning (pfile, "#pragma system_header outside include file");
1001 ip->system_header_p = 1;
1006 /* Just ignore #sccs, on systems where we define it at all. */
1007 #ifdef SCCS_DIRECTIVE
1012 _cpp_skip_rest_of_line (pfile);
1017 /* We've found an `#if' directive. If the only thing before it in
1018 this file is white space, and if it is of the form
1019 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1020 for inclusion of this file. (See redundant_include_p in cppfiles.c
1021 for an explanation of controlling macros.) If so, return a
1022 malloced copy of SYMBOL. Otherwise, return NULL. */
1025 detect_if_not_defined (pfile)
1028 U_CHAR *control_macro = 0;
1029 enum cpp_ttype token;
1030 unsigned int base_offset;
1031 unsigned int token_offset;
1032 unsigned int need_rparen = 0;
1033 unsigned int token_len;
1035 if (pfile->only_seen_white != 2)
1038 /* Save state required for restore. */
1039 pfile->no_macro_expand++;
1040 CPP_SET_MARK (pfile);
1041 base_offset = CPP_WRITTEN (pfile);
1044 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1045 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1046 || CPP_PWRITTEN (pfile)[-1] != '!')
1049 /* ...then `defined', */
1050 token_offset = CPP_WRITTEN (pfile);
1051 token = _cpp_get_directive_token (pfile);
1052 if (token != CPP_NAME)
1054 if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1057 /* ...then an optional '(' and the name, */
1058 token_offset = CPP_WRITTEN (pfile);
1059 token = _cpp_get_directive_token (pfile);
1060 if (token == CPP_OPEN_PAREN)
1062 token_offset = CPP_WRITTEN (pfile);
1064 token = _cpp_get_directive_token (pfile);
1066 if (token != CPP_NAME)
1069 token_len = CPP_WRITTEN (pfile) - token_offset;
1071 /* ...then the ')', if necessary, */
1072 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1075 /* ...and make sure there's nothing else on the line. */
1076 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1079 /* We have a legitimate controlling macro for this header. */
1080 control_macro = (U_CHAR *) xmalloc (token_len + 1);
1081 memcpy (control_macro, pfile->token_buffer + token_offset, token_len);
1082 control_macro[token_len] = '\0';
1085 CPP_SET_WRITTEN (pfile, base_offset);
1086 pfile->no_macro_expand--;
1087 CPP_GOTO_MARK (pfile);
1089 return control_macro;
1093 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1094 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1101 U_CHAR *control_macro;
1103 int save_only_seen_white = pfile->only_seen_white;
1105 control_macro = detect_if_not_defined (pfile);
1107 pfile->only_seen_white = 0;
1108 value = _cpp_parse_expr (pfile);
1109 pfile->only_seen_white = save_only_seen_white;
1111 return conditional_skip (pfile, value == 0, T_IF, control_macro);
1115 * handle a #elif directive by not changing if_stack either.
1116 * see the comment above do_else.
1123 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1125 cpp_error (pfile, "`#elif' not within a conditional");
1130 if (pfile->if_stack->type == T_ELSE)
1132 cpp_error (pfile, "`#elif' after `#else'");
1133 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1134 "the conditional began here");
1136 pfile->if_stack->type = T_ELIF;
1139 if (pfile->if_stack->if_succeeded)
1141 _cpp_skip_rest_of_line (pfile);
1142 return skip_if_group (pfile);
1144 if (_cpp_parse_expr (pfile) == 0)
1145 return skip_if_group (pfile);
1147 ++pfile->if_stack->if_succeeded; /* continue processing input */
1151 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1152 not defined; the macro tested is left in the token buffer (but
1156 parse_ifdef (pfile, name)
1162 enum cpp_ttype token;
1163 long old_written = CPP_WRITTEN (pfile);
1166 pfile->no_macro_expand++;
1167 token = _cpp_get_directive_token (pfile);
1168 pfile->no_macro_expand--;
1170 ident = pfile->token_buffer + old_written;
1171 len = CPP_WRITTEN (pfile) - old_written;
1173 if (token == CPP_VSPACE)
1175 if (! CPP_TRADITIONAL (pfile))
1176 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1180 else if (token == CPP_NAME)
1182 defined = cpp_defined (pfile, ident, len);
1183 CPP_PUTC (pfile, '\0'); /* so it can be copied with xstrdup */
1188 if (! CPP_TRADITIONAL (pfile))
1189 cpp_error (pfile, "`#%s' with invalid argument", name);
1192 if (!CPP_TRADITIONAL (pfile))
1194 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1197 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1199 _cpp_skip_rest_of_line (pfile);
1202 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1206 /* #ifdef is dead simple. */
1212 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1213 return conditional_skip (pfile, skip, T_IFDEF, 0);
1216 /* #ifndef is a tad more complex, because we need to check for a
1217 no-reinclusion wrapper. */
1223 int start_of_file, skip;
1224 U_CHAR *control_macro = 0;
1226 start_of_file = pfile->only_seen_white == 2;
1227 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1229 if (start_of_file && !skip)
1230 control_macro = uxstrdup (CPP_PWRITTEN (pfile));
1232 return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1235 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1236 If this is a #ifndef starting at the beginning of a file,
1237 CONTROL_MACRO is the macro name tested by the #ifndef.
1238 Otherwise, CONTROL_MACRO is 0. */
1241 conditional_skip (pfile, skip, type, control_macro)
1245 U_CHAR *control_macro;
1249 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1250 temp->lineno = CPP_BUFFER (pfile)->lineno;
1251 temp->next = pfile->if_stack;
1252 temp->control_macro = control_macro;
1253 pfile->if_stack = temp;
1255 pfile->if_stack->type = type;
1258 return skip_if_group (pfile);
1260 ++pfile->if_stack->if_succeeded;
1264 /* Subroutine of skip_if_group. Examine one preprocessing directive
1265 and return 0 if skipping should continue, or the directive number
1266 of the directive that ends the block if it should halt.
1268 Also adjusts the if_stack as appropriate. The `#' has been read,
1269 but not the identifier. */
1272 consider_directive_while_skipping (pfile, stack)
1281 /* -traditional directives are recognized only with the # in column 1. */
1282 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1284 ident = CPP_WRITTEN (pfile);
1285 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1287 len = CPP_WRITTEN (pfile) - ident;
1289 for (i = 0; i < N_DIRECTIVES; i++)
1291 if (dtable[i].length == len
1292 && !ustrncmp (dtable[i].name, pfile->token_buffer + ident, len))
1293 goto real_directive;
1299 /* If it's not a directive of interest to us, return now. */
1300 if (ORIGIN (dtable[i].flags) != COND)
1303 /* First, deal with -traditional and -Wtraditional.
1304 All COND directives are from K+R. */
1308 if (CPP_TRADITIONAL (pfile))
1310 if (CPP_WTRADITIONAL (pfile))
1311 cpp_warning (pfile, "ignoring #%s because of its indented #",
1315 if (CPP_WTRADITIONAL (pfile))
1316 cpp_warning (pfile, "traditional C ignores %s with the # indented",
1323 cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1329 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1330 temp->lineno = CPP_BUFFER (pfile)->lineno;
1331 temp->next = pfile->if_stack;
1333 pfile->if_stack = temp;
1337 if (pfile->if_stack != stack)
1338 validate_else (pfile, dtable[i].name);
1341 if (pfile->if_stack == stack)
1344 pfile->if_stack->type = i;
1348 if (pfile->if_stack != stack)
1349 validate_else (pfile, dtable[i].name);
1351 if (pfile->if_stack == stack)
1354 temp = pfile->if_stack;
1355 pfile->if_stack = temp->next;
1361 /* Skip to #endif, #else, or #elif. Consumes the directive that
1362 causes it to stop, but not its argument. Returns the number of
1363 that directive, which must be passed back up to
1364 _cpp_handle_directive, which will execute it. */
1366 skip_if_group (pfile)
1369 enum cpp_ttype token;
1370 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1374 /* We are no longer at the start of the file. */
1375 pfile->only_seen_white = 0;
1377 old_written = CPP_WRITTEN (pfile);
1378 pfile->no_macro_expand++;
1381 /* We are at the end of a line.
1382 XXX Serious layering violation here. */
1383 int c = CPP_BUF_PEEK (CPP_BUFFER (pfile));
1385 break; /* Caller will issue error. */
1387 cpp_ice (pfile, "character %c at end of line in skip_if_group", c);
1388 CPP_BUFFER (pfile)->cur++;
1389 CPP_BUMP_LINE (pfile);
1390 CPP_SET_WRITTEN (pfile, old_written);
1391 pfile->only_seen_white = 1;
1393 token = _cpp_get_directive_token (pfile);
1395 if (token == CPP_HASH)
1397 ret = consider_directive_while_skipping (pfile, save_if_stack);
1402 if (token != CPP_VSPACE)
1403 _cpp_skip_rest_of_line (pfile);
1405 CPP_SET_WRITTEN (pfile, old_written);
1406 pfile->no_macro_expand--;
1411 * handle a #else directive. Do this by just continuing processing
1412 * without changing if_stack ; this is so that the error message
1413 * for missing #endif's etc. will point to the original #if. It
1414 * is possible that something different would be better.
1421 validate_else (pfile, dtable[T_ELSE].name);
1422 _cpp_skip_rest_of_line (pfile);
1424 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1426 cpp_error (pfile, "`#else' not within a conditional");
1431 /* #ifndef can't have its special treatment for containing the whole file
1432 if it has a #else clause. */
1433 pfile->if_stack->control_macro = 0;
1435 if (pfile->if_stack->type == T_ELSE)
1437 cpp_error (pfile, "`#else' after `#else'");
1438 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1439 "the conditional began here");
1441 pfile->if_stack->type = T_ELSE;
1444 if (pfile->if_stack->if_succeeded)
1445 return skip_if_group (pfile);
1447 ++pfile->if_stack->if_succeeded; /* continue processing input */
1452 * unstack after #endif command
1459 validate_else (pfile, dtable[T_ENDIF].name);
1460 _cpp_skip_rest_of_line (pfile);
1462 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1463 cpp_error (pfile, "`#endif' not within a conditional");
1466 IF_STACK *temp = pfile->if_stack;
1467 pfile->if_stack = temp->next;
1468 if (temp->control_macro != 0)
1469 pfile->potential_control_macro = temp->control_macro;
1475 /* Issue -pedantic warning for text which is not a comment following
1476 an #else or #endif. Do not warn in system headers, as this is harmless
1477 and very common on old systems. */
1480 validate_else (pfile, directive)
1482 const U_CHAR *directive;
1485 if (! CPP_PEDANTIC (pfile))
1488 old_written = CPP_WRITTEN (pfile);
1489 pfile->no_macro_expand++;
1490 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1492 "text following `#%s' violates ANSI standard", directive);
1493 CPP_SET_WRITTEN (pfile, old_written);
1494 pfile->no_macro_expand--;
1497 /* Called when we reach the end of a macro buffer. Walk back up the
1498 conditional stack till we reach its level at entry to this file,
1499 issuing error messages. */
1501 _cpp_unwind_if_stack (pfile, pbuf)
1505 struct if_stack *ifs, *nifs;
1507 for (ifs = pfile->if_stack;
1508 ifs != pbuf->if_stack;
1511 cpp_error_with_line (pfile, ifs->lineno, 0,
1512 "unterminated `#%s' conditional",
1513 dtable[ifs->type].name);
1518 pfile->if_stack = ifs;
1521 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1522 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1523 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1532 struct predicate *pred = 0;
1533 enum cpp_ttype type;
1535 old_written = CPP_WRITTEN (pfile);
1536 pfile->no_macro_expand++;
1538 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1539 type = _cpp_get_directive_token (pfile);
1540 if (type == CPP_VSPACE)
1541 ERROR ("#assert without predicate");
1542 else if (type != CPP_NAME)
1543 ERROR ("assertion predicate is not an identifier");
1545 sym = pfile->token_buffer + old_written;
1546 len = CPP_WRITTEN (pfile) - old_written;
1547 hp = _cpp_lookup (pfile, sym, len);
1549 if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1550 ERROR ("missing token-sequence in #assert");
1552 pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1553 _cpp_init_toklist (&pred->answer);
1555 if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1557 ERROR ("missing close paren in #assert");
1559 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1560 ICE ("impossible token, expecting ) in do_assert");
1562 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1563 ERROR ("junk at end of #assert");
1565 if (hp->type == T_ASSERTION)
1567 /* Check for reassertion. */
1568 const struct predicate *old;
1570 for (old = hp->value.pred; old; old = old->next)
1571 if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1572 /* We used to warn about this, but SVR4 cc doesn't, so let's
1573 match that (also consistent with #define). goto error will
1576 pred->next = hp->value.pred;
1580 hp->type = T_ASSERTION;
1584 _cpp_squeeze_toklist (&pred->answer);
1585 hp->value.pred = pred;
1586 pfile->no_macro_expand--;
1587 CPP_SET_WRITTEN (pfile, old_written);
1591 _cpp_skip_rest_of_line (pfile);
1592 pfile->no_macro_expand--;
1593 CPP_SET_WRITTEN (pfile, old_written);
1596 _cpp_free_toklist (&pred->answer);
1610 struct predicate *pred = 0;
1611 enum cpp_ttype type;
1613 old_written = CPP_WRITTEN (pfile);
1614 pfile->no_macro_expand++;
1616 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1617 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1618 ERROR ("#unassert must be followed by an identifier");
1620 sym = pfile->token_buffer + old_written;
1621 len = CPP_WRITTEN (pfile) - old_written;
1622 hp = _cpp_lookup (pfile, sym, len);
1624 type = _cpp_get_directive_token (pfile);
1625 if (type == CPP_OPEN_PAREN)
1627 pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1628 _cpp_init_toklist (&pred->answer);
1630 if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1632 ERROR ("missing close paren in #unassert");
1634 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1635 ICE ("impossible token, expecting ) in do_unassert");
1637 type = _cpp_get_directive_token (pfile);
1640 if (type != CPP_VSPACE)
1641 ERROR ("junk at end of #unassert");
1643 if (hp->type != T_ASSERTION)
1644 /* Not an error to #unassert something that isn't asserted.
1645 goto error to clean up. */
1650 /* Find this specific answer and remove it. */
1651 struct predicate *o, *p;
1653 for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1654 if (_cpp_equiv_toklists (&pred->answer, &o->answer))
1659 hp->value.pred = o->next;
1661 _cpp_free_toklist (&o->answer);
1668 struct predicate *o, *p;
1669 for (o = hp->value.pred; o; o = p)
1672 _cpp_free_toklist ((cpp_toklist *) &o->answer);
1675 hp->value.pred = NULL;
1678 if (hp->value.pred == NULL)
1679 hp->type = T_VOID; /* Last answer for this predicate deleted. */
1682 _cpp_skip_rest_of_line (pfile);
1683 pfile->no_macro_expand--;
1684 CPP_SET_WRITTEN (pfile, old_written);
1687 _cpp_free_toklist (&pred->answer);
1693 /* These are for -D, -U, -A. */
1695 /* Process the string STR as if it appeared as the body of a #define.
1696 If STR is just an identifier, define it with value 1.
1697 If STR has anything after the identifier, then it should
1698 be identifier=definition. */
1701 cpp_define (pfile, str)
1708 p = strchr (str, '=');
1709 /* Copy the entire option so we can modify it.
1710 Change the first "=" in the string to a space. If there is none,
1711 tack " 1" on the end. Then add a newline and a NUL. */
1715 count = strlen (str) + 2;
1716 buf = (char *) alloca (count);
1717 memcpy (buf, str, count - 2);
1719 buf[count - 2] = '\n';
1720 buf[count - 1] = '\0';
1724 count = strlen (str) + 4;
1725 buf = (char *) alloca (count);
1726 memcpy (buf, str, count - 4);
1727 strcpy (&buf[count-4], " 1\n");
1730 if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1733 cpp_pop_buffer (pfile);
1737 /* Process MACRO as if it appeared as the body of an #undef. */
1739 cpp_undef (pfile, macro)
1743 /* Copy the string so we can append a newline. */
1744 size_t len = strlen (macro);
1745 char *buf = (char *) alloca (len + 2);
1746 memcpy (buf, macro, len);
1748 buf[len + 1] = '\0';
1749 if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1752 cpp_pop_buffer (pfile);
1756 /* Process the string STR as if it appeared as the body of a #assert. */
1758 cpp_assert (pfile, str)
1762 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1765 cpp_pop_buffer (pfile);
1769 /* Process STR as if it appeared as the body of an #unassert. */
1771 cpp_unassert (pfile, str)
1775 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1777 do_unassert (pfile);
1778 cpp_pop_buffer (pfile);
1782 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1784 cpp_defined (pfile, id, len)
1789 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1790 if (hp->type == T_POISON)
1792 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1795 return (hp->type != T_VOID);