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 was_skipping; /* value of pfile->skipping before this if */
48 const cpp_hashnode *cmacro; /* macro name for #ifndef around entire file */
49 int type; /* type of last directive seen in this group */
52 /* Forward declarations. */
54 static void validate_else PARAMS ((cpp_reader *, const U_CHAR *));
55 static unsigned int parse_include PARAMS ((cpp_reader *, const U_CHAR *));
56 static void push_conditional PARAMS ((cpp_reader *, int, int,
57 const cpp_hashnode *));
58 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
60 static int read_line_number PARAMS ((cpp_reader *, int *));
61 static const cpp_hashnode *parse_ifdef PARAMS ((cpp_reader *, const U_CHAR *));
62 static const cpp_hashnode *detect_if_not_defined PARAMS ((cpp_reader *));
64 /* Values for the flags field of the table below. KANDR and COND
65 directives come from traditional (K&R) C. The difference is, if we
66 care about it while skipping a failed conditional block, its origin
67 is COND. STDC89 directives come from the 1989 C standard.
68 EXTENSION directives are extensions, with origins noted below. */
76 #define ORIGIN(f) ((f) & ORIGIN_MASK)
77 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
79 /* This is the table of directive handlers. It is ordered by
80 frequency of occurrence; the numbers at the end are directive
81 counts from all the source code I have lying around (egcs and libc
82 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
85 The entries with a dash and a name after the count are extensions,
86 of which all but #warning and #include_next are deprecated. The name
87 is where the extension appears to have come from. */
89 /* #sccs is not always recognized. */
91 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
93 # define SCCS_ENTRY /* nothing */
96 #define DIRECTIVE_TABLE \
97 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
98 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
99 D(endif, T_ENDIF, COND) /* 45855 */ \
100 D(ifdef, T_IFDEF, COND) /* 22000 */ \
101 D(if, T_IF, COND) /* 18162 */ \
102 D(else, T_ELSE, COND) /* 9863 */ \
103 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
104 D(undef, T_UNDEF, KANDR) /* 4837 */ \
105 D(line, T_LINE, KANDR) /* 2465 */ \
106 D(elif, T_ELIF, COND) /* 610 */ \
107 D(error, T_ERROR, STDC89) /* 475 */ \
108 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
109 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
110 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
111 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
112 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
113 D(assert, T_ASSERT, EXTENSION) /* 0 SVR4 */ \
114 D(unassert, T_UNASSERT, EXTENSION) /* 0 SVR4 */ \
117 /* Use the table to generate a series of prototypes, an enum for the
118 directive names, and an array of directive handlers. */
120 /* The directive-processing functions are declared to return int
121 instead of void, because some old compilers have trouble with
122 pointers to functions returning void. */
124 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
125 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
129 #define D(n, tag, f) tag,
137 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
138 #define D(name, t, flags) \
139 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
140 sizeof STRINGX(name) - 1, flags },
141 static const struct directive dtable[] =
146 #undef DIRECTIVE_TABLE
148 /* Check if a token's name matches that of a known directive. Put in
149 this file to save exporting dtable and other unneeded information. */
151 _cpp_check_directive (list, token)
155 const U_CHAR *name = token->val.name.text;
156 size_t len = token->val.name.len;
160 list->flags &= ~SYNTAX_INCLUDE;
162 for (i = 0; i < N_DIRECTIVES; i++)
163 if (dtable[i].length == len && !ustrncmp (dtable[i].name, name, len))
166 if (dtable[i].flags & SYNTAX_INCLUDE)
167 list->flags |= SYNTAX_INCLUDE;
172 /* Handle a possible # directive.
173 '#' has already been read. */
176 _cpp_handle_directive (pfile)
183 long old_written = CPP_WRITTEN (pfile);
186 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
188 cpp_ice (pfile, "handle_directive called on macro buffer");
192 /* -traditional directives are recognized only with the # in column 1. */
193 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
195 /* Scan the next token, then pretend we didn't. */
196 CPP_SET_MARK (pfile);
197 pfile->no_macro_expand++;
198 tok = _cpp_get_directive_token (pfile);
199 pfile->no_macro_expand--;
201 ident = pfile->token_buffer + old_written;
202 len = CPP_PWRITTEN (pfile) - ident;
203 CPP_SET_WRITTEN (pfile, old_written);
204 CPP_GOTO_MARK (pfile);
206 /* # followed by a number is equivalent to #line. Do not recognize
207 this form in assembly language source files or skipped
208 conditional groups. Complain about this form if we're being
209 pedantic, but not if this is regurgitated input (preprocessed or
210 fed back in by the C++ frontend). */
211 if (tok == CPP_NUMBER)
213 if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
216 if (CPP_PEDANTIC (pfile)
217 && CPP_BUFFER (pfile)->ihash
218 && ! CPP_OPTION (pfile, preprocessed))
219 cpp_pedwarn (pfile, "# followed by integer");
221 goto process_directive;
224 /* If we are rescanning preprocessed input, don't obey any directives
226 else if (CPP_OPTION (pfile, preprocessed))
229 /* A line of just # becomes blank. */
230 else if (tok == CPP_VSPACE)
233 /* A NAME token might in fact be a directive! */
234 else if (tok == CPP_NAME)
236 for (i = 0; i < N_DIRECTIVES; i++)
238 if (dtable[i].length == len
239 && !ustrncmp (dtable[i].name, ident, len))
242 /* Don't complain about invalid directives in assembly source,
243 we don't know where the comments are, and # may introduce
244 assembler pseudo-ops. Don't complain about invalid directives
245 in skipped conditional groups (6.10 p4). */
246 if (!pfile->skipping && !CPP_OPTION (pfile, lang_asm))
247 cpp_error (pfile, "invalid preprocessing directive #%s", ident);
250 /* And anything else means the # wasn't a directive marker. */
256 /* If we are skipping a failed conditional group, all non-conditional
257 directives are ignored. */
258 if (pfile->skipping && ORIGIN (dtable[i].flags) != COND)
261 /* In -traditional mode, a directive is ignored unless its # is in
263 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
265 if (CPP_WTRADITIONAL (pfile))
266 cpp_warning (pfile, "ignoring #%s because of its indented #",
271 /* no_directives is set when we are parsing macro arguments. Directives
272 in macro arguments are undefined behavior (C99 6.10.3.11); this
273 implementation chooses to make them hard errors. */
274 if (pfile->no_directives)
276 cpp_error (pfile, "#%s may not be used inside a macro argument",
278 _cpp_skip_rest_of_line (pfile);
282 /* Issue -pedantic warnings for extended directives. */
283 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
284 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
286 /* -Wtraditional gives warnings about directives with inappropriate
288 if (CPP_WTRADITIONAL (pfile))
290 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
291 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
293 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
295 "suggest hiding #%s from traditional C with an indented #",
299 /* Unfortunately, it's necessary to scan the directive name again,
300 now we know we're going to consume it. FIXME. */
302 pfile->no_macro_expand++;
303 _cpp_get_directive_token (pfile);
304 pfile->no_macro_expand--;
305 CPP_SET_WRITTEN (pfile, old_written);
308 (void) (*dtable[i].func) (pfile);
312 /* Pass a directive through to the output file.
313 BUF points to the contents of the directive, as a contiguous string.
314 LEN is the length of the string pointed to by BUF.
315 KEYWORD is the keyword-table entry for the directive. */
318 pass_thru_directive (buf, len, pfile, keyword)
324 const struct directive *kt = &dtable[keyword];
325 register unsigned klen = kt->length;
327 CPP_RESERVE (pfile, 1 + klen + len);
328 CPP_PUTC_Q (pfile, '#');
329 CPP_PUTS_Q (pfile, kt->name, klen);
330 if (len != 0 && buf[0] != ' ')
331 CPP_PUTC_Q (pfile, ' ');
332 CPP_PUTS_Q (pfile, buf, len);
335 /* Process a #define command. */
344 cpp_toklist *list = &pfile->directbuf;
346 pfile->no_macro_expand++;
347 CPP_OPTION (pfile, discard_comments)++;
349 _cpp_scan_until (pfile, list, CPP_VSPACE);
351 /* First token on the line must be a NAME. There may not be any
352 tokens in the list (if we had #define all by itself on a line). */
353 if (list->tokens_used == 0
354 || TOK_TYPE (list, 0) != CPP_NAME)
356 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
357 "#define must be followed by an identifier");
361 sym = TOK_NAME (list, 0);
362 len = TOK_LEN (list, 0);
364 /* That NAME is not allowed to be "defined". (Not clear if the
365 standard requires this.) */
366 if (len == 7 && !ustrncmp (sym, U"defined", 7))
368 cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
369 "\"defined\" is not a legal macro name");
373 node = cpp_lookup (pfile, sym, len);
374 /* Check for poisoned identifiers now. All other checks
375 are done in cpphash.c. */
376 if (node->type == T_POISON)
378 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
382 if (_cpp_create_definition (pfile, list, node) == 0)
385 if (CPP_OPTION (pfile, debug_output)
386 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
387 _cpp_dump_definition (pfile, node);
388 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
389 pass_thru_directive (sym, len, pfile, T_DEFINE);
392 pfile->no_macro_expand--;
393 CPP_OPTION (pfile, discard_comments)--;
397 /* Handle #include and #import. */
400 parse_include (pfile, name)
404 long old_written = CPP_WRITTEN (pfile);
405 enum cpp_ttype token;
408 pfile->parsing_include_directive++;
409 token = _cpp_get_directive_token (pfile);
410 pfile->parsing_include_directive--;
412 len = CPP_WRITTEN (pfile) - old_written;
414 if (token == CPP_STRING)
415 ; /* No special treatment required. */
417 else if (token == CPP_NAME)
419 /* Support '#include xyz' like VAX-C. It is taken as
420 '#include <xyz.h>' and generates a warning. */
421 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
424 /* Rewrite the token to <xyz.h>. */
425 CPP_RESERVE (pfile, 4);
427 memmove (pfile->token_buffer + old_written + 1,
428 pfile->token_buffer + old_written,
429 CPP_WRITTEN (pfile) - old_written);
430 pfile->token_buffer[old_written] = '<';
431 CPP_PUTS_Q (pfile, ".h>", 2);
436 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", name);
437 CPP_SET_WRITTEN (pfile, old_written);
438 _cpp_skip_rest_of_line (pfile);
442 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
444 cpp_error (pfile, "junk at end of #%s", name);
445 _cpp_skip_rest_of_line (pfile);
448 CPP_SET_WRITTEN (pfile, old_written);
451 cpp_error (pfile, "empty file name in #%s", name);
463 len = parse_include (pfile, dtable[T_INCLUDE].name);
466 token = (U_CHAR *) alloca (len + 1);
467 memcpy (token, CPP_PWRITTEN (pfile), len);
470 if (CPP_OPTION (pfile, dump_includes))
471 pass_thru_directive (token, len, pfile, T_INCLUDE);
473 _cpp_execute_include (pfile, token, len, 0, 0);
484 if (CPP_OPTION (pfile, warn_import)
485 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
487 pfile->import_warning = 1;
489 "#import is obsolete, use an #ifndef wrapper in the header file");
492 len = parse_include (pfile, dtable[T_IMPORT].name);
495 token = (U_CHAR *) alloca (len + 1);
496 memcpy (token, CPP_PWRITTEN (pfile), len);
499 if (CPP_OPTION (pfile, dump_includes))
500 pass_thru_directive (token, len, pfile, T_IMPORT);
502 _cpp_execute_include (pfile, token, len, 1, 0);
507 do_include_next (pfile)
512 struct file_name_list *search_start = 0;
514 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
517 token = (U_CHAR *) alloca (len + 1);
518 memcpy (token, CPP_PWRITTEN (pfile), len);
521 if (CPP_OPTION (pfile, dump_includes))
522 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
524 /* For #include_next, skip in the search path past the dir in which the
525 containing file was found. Treat files specified using an absolute path
526 as if there are no more directories to search. Treat the primary source
527 file like any other included source, but generate a warning. */
528 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
530 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
531 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
534 cpp_warning (pfile, "#include_next in primary source file");
536 _cpp_execute_include (pfile, token, len, 0, search_start);
540 /* Subroutine of do_line. Read next token from PFILE without adding it to
541 the output buffer. If it is a number between 1 and 4, store it in *NUM
542 and return 1; otherwise, return 0 and complain if we aren't at the end
546 read_line_number (pfile, num)
550 long save_written = CPP_WRITTEN (pfile);
552 enum cpp_ttype token = _cpp_get_directive_token (pfile);
553 p = pfile->token_buffer + save_written;
555 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
556 && p[0] >= '1' && p[0] <= '4')
559 CPP_SET_WRITTEN (pfile, save_written);
564 if (token != CPP_VSPACE && token != CPP_EOF)
565 cpp_error (pfile, "invalid format #line");
566 CPP_SET_WRITTEN (pfile, save_written);
571 /* Interpret #line command.
572 Note that the filename string (if any) is treated as if it were an
573 include filename. That means no escape handling. */
579 cpp_buffer *ip = CPP_BUFFER (pfile);
580 unsigned int new_lineno;
581 long old_written = CPP_WRITTEN (pfile);
582 enum cpp_ttype token;
585 token = _cpp_get_directive_token (pfile);
587 if (token != CPP_NUMBER)
589 cpp_error (pfile, "token after #line is not an integer");
590 goto bad_line_directive;
593 CPP_PUTC (pfile, '\0'); /* not terminated for us */
594 new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
598 cpp_error (pfile, "token after #line is not an integer");
599 goto bad_line_directive;
601 CPP_SET_WRITTEN (pfile, old_written);
603 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
604 cpp_pedwarn (pfile, "line number out of range in #line");
606 token = _cpp_get_directive_token (pfile);
608 if (token == CPP_STRING)
610 U_CHAR *fname = pfile->token_buffer + old_written + 1;
611 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
612 int action_number = 0;
614 if (read_line_number (pfile, &action_number))
616 if (CPP_PEDANTIC (pfile))
617 cpp_pedwarn (pfile, "garbage at end of #line");
619 /* This is somewhat questionable: change the buffer stack
620 depth so that output_line_command thinks we've stacked
622 if (action_number == 1)
624 pfile->buffer_stack_depth++;
625 ip->system_header_p = 0;
626 read_line_number (pfile, &action_number);
628 else if (action_number == 2)
630 pfile->buffer_stack_depth--;
631 ip->system_header_p = 0;
632 read_line_number (pfile, &action_number);
634 if (action_number == 3)
636 ip->system_header_p = 1;
637 read_line_number (pfile, &action_number);
639 if (action_number == 4)
641 ip->system_header_p = 2;
642 read_line_number (pfile, &action_number);
648 if (strcmp ((const char *)fname, ip->nominal_fname))
650 if (!strcmp ((const char *)fname, ip->ihash->name))
651 ip->nominal_fname = ip->ihash->name;
653 ip->nominal_fname = _cpp_fake_ihash (pfile, (const char *)fname);
656 else if (token != CPP_VSPACE && token != CPP_EOF)
658 cpp_error (pfile, "second token after #line is not a string");
659 goto bad_line_directive;
662 /* The Newline at the end of this line remains to be processed.
663 To put the next line at the specified line number,
664 we must store a line number now that is one less. */
665 ip->lineno = new_lineno - 1;
666 CPP_SET_WRITTEN (pfile, old_written);
670 _cpp_skip_rest_of_line (pfile);
671 CPP_SET_WRITTEN (pfile, old_written);
675 /* Remove the definition of a symbol from the symbol table.
676 According to the C standard, it is not an error to undef
677 something that has no definitions. */
685 long here = CPP_WRITTEN (pfile);
686 enum cpp_ttype token;
688 pfile->no_macro_expand++;
689 token = _cpp_get_directive_token (pfile);
690 pfile->no_macro_expand--;
692 if (token != CPP_NAME)
694 cpp_error (pfile, "token after #undef is not an identifier");
695 _cpp_skip_rest_of_line (pfile);
698 len = CPP_WRITTEN (pfile) - here;
700 token = _cpp_get_directive_token (pfile);
701 if (token != CPP_VSPACE)
703 cpp_pedwarn (pfile, "junk on line after #undef");
704 _cpp_skip_rest_of_line (pfile);
707 name = pfile->token_buffer + here;
708 CPP_SET_WRITTEN (pfile, here);
710 hp = cpp_lookup (pfile, name, len);
711 if (hp->type == T_VOID)
712 ; /* Not defined in the first place - do nothing. */
713 else if (hp->type == T_POISON)
714 cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
717 /* If we are generating additional info for debugging (with -g) we
718 need to pass through all effective #undef commands. */
719 if (CPP_OPTION (pfile, debug_output))
720 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
722 if (hp->type != T_MACRO && hp->type != T_FMACRO
723 && hp->type != T_EMPTY && hp->type != T_IDENTITY)
724 cpp_warning (pfile, "undefining `%s'", hp->name);
726 _cpp_free_definition (hp);
734 * Report an error detected by the program we are processing.
735 * Use the text of the line in the error message.
736 * (We use error because it prints the filename & line#.)
743 const U_CHAR *text, *limit;
745 _cpp_skip_hspace (pfile);
746 text = CPP_BUFFER (pfile)->cur;
747 _cpp_skip_rest_of_line (pfile);
748 limit = CPP_BUFFER (pfile)->cur;
750 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
755 * Report a warning detected by the program we are processing.
756 * Use the text of the line in the warning message, then continue.
763 const U_CHAR *text, *limit;
765 _cpp_skip_hspace (pfile);
766 text = CPP_BUFFER (pfile)->cur;
767 _cpp_skip_rest_of_line (pfile);
768 limit = CPP_BUFFER (pfile)->cur;
770 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
774 /* Report program identification. */
780 long old_written = CPP_WRITTEN (pfile);
782 CPP_PUTS (pfile, "#ident ", 7);
784 /* Next token should be a string constant. */
785 if (_cpp_get_directive_token (pfile) == CPP_STRING)
786 /* And then a newline. */
787 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
788 /* Good - ship it. */
791 cpp_error (pfile, "invalid #ident");
792 _cpp_skip_rest_of_line (pfile);
793 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
798 /* Pragmata handling. We handle some of these, and pass the rest on
799 to the front end. C99 defines three pragmas and says that no macro
800 expansion is to be performed on them; whether or not macro
801 expansion happens for other pragmas is implementation defined.
802 This implementation never macro-expands the text after #pragma.
804 We currently do not support the _Pragma operator. Support for that
805 has to be coordinated with the front end. Proposed implementation:
806 both #pragma blah blah and _Pragma("blah blah") become
807 __builtin_pragma(blah blah) and we teach the parser about that. */
809 /* Sub-handlers for the pragmas needing treatment here.
810 They return 1 if the token buffer is to be popped, 0 if not. */
811 static int do_pragma_once PARAMS ((cpp_reader *));
812 static int do_pragma_implementation PARAMS ((cpp_reader *));
813 static int do_pragma_poison PARAMS ((cpp_reader *));
814 static int do_pragma_system_header PARAMS ((cpp_reader *));
815 static int do_pragma_default PARAMS ((cpp_reader *));
824 enum cpp_ttype token;
826 here = CPP_WRITTEN (pfile);
827 CPP_PUTS (pfile, "#pragma ", 8);
829 key = CPP_WRITTEN (pfile);
830 pfile->no_macro_expand++;
831 token = _cpp_get_directive_token (pfile);
832 if (token != CPP_NAME)
834 if (token == CPP_VSPACE)
840 buf = pfile->token_buffer + key;
841 CPP_PUTC (pfile, ' ');
843 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
845 pop = do_pragma_once (pfile);
846 else if (tokis ("implementation"))
847 pop = do_pragma_implementation (pfile);
848 else if (tokis ("poison"))
849 pop = do_pragma_poison (pfile);
850 else if (tokis ("system_header"))
851 pop = do_pragma_system_header (pfile);
853 pop = do_pragma_default (pfile);
856 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
860 CPP_SET_WRITTEN (pfile, here);
861 pfile->no_macro_expand--;
865 cpp_error (pfile, "malformed #pragma directive");
866 _cpp_skip_rest_of_line (pfile);
868 CPP_SET_WRITTEN (pfile, here);
869 pfile->no_macro_expand--;
874 do_pragma_default (pfile)
877 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
878 CPP_PUTC (pfile, ' ');
883 do_pragma_once (pfile)
886 cpp_buffer *ip = CPP_BUFFER (pfile);
888 /* Allow #pragma once in system headers, since that's not the user's
890 if (!ip->system_header_p)
891 cpp_warning (pfile, "#pragma once is obsolete");
893 if (CPP_PREV_BUFFER (ip) == NULL)
894 cpp_warning (pfile, "#pragma once outside include file");
896 ip->ihash->cmacro = NEVER_REINCLUDE;
902 do_pragma_implementation (pfile)
905 /* Be quiet about `#pragma implementation' for a file only if it hasn't
906 been included yet. */
907 enum cpp_ttype token;
908 long written = CPP_WRITTEN (pfile);
913 token = _cpp_get_directive_token (pfile);
914 if (token == CPP_VSPACE)
916 else if (token != CPP_STRING)
918 cpp_error (pfile, "malformed #pragma implementation");
922 /* Trim the leading and trailing quote marks from the string. */
923 name = pfile->token_buffer + written + 1;
924 len = CPP_PWRITTEN (pfile) - name;
926 memcpy (copy, name, len - 1);
927 copy[len - 1] = '\0';
929 if (cpp_included (pfile, copy))
931 "#pragma implementation for %s appears after file is included",
937 do_pragma_poison (pfile)
940 /* Poison these symbols so that all subsequent usage produces an
946 enum cpp_ttype token;
949 /* As a rule, don't include #pragma poison commands in output,
950 unless the user asks for them. */
951 writeit = (CPP_OPTION (pfile, debug_output)
952 || CPP_OPTION (pfile, dump_macros) == dump_definitions
953 || CPP_OPTION (pfile, dump_macros) == dump_names);
957 written = CPP_WRITTEN (pfile);
958 token = _cpp_get_directive_token (pfile);
959 if (token == CPP_VSPACE)
961 if (token != CPP_NAME)
963 cpp_error (pfile, "invalid #pragma poison directive");
964 _cpp_skip_rest_of_line (pfile);
968 p = pfile->token_buffer + written;
969 len = CPP_PWRITTEN (pfile) - p;
970 hp = cpp_lookup (pfile, p, len);
971 if (hp->type == T_POISON)
972 ; /* It is allowed to poison the same identifier twice. */
975 if (hp->type != T_VOID)
976 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
977 _cpp_free_definition (hp);
981 CPP_PUTC (pfile, ' ');
986 /* Mark the current header as a system header. This will suppress
987 some categories of warnings (notably those from -pedantic). It is
988 intended for use in system libraries that cannot be implemented in
989 conforming C, but cannot be certain that their headers appear in a
990 system include directory. To prevent abuse, it is rejected in the
991 primary source file. */
993 do_pragma_system_header (pfile)
996 cpp_buffer *ip = cpp_file_buffer (pfile);
997 if (CPP_PREV_BUFFER (ip) == NULL)
998 cpp_warning (pfile, "#pragma system_header outside include file");
1000 ip->system_header_p = 1;
1005 /* Just ignore #sccs, on systems where we define it at all. */
1006 #ifdef SCCS_DIRECTIVE
1011 _cpp_skip_rest_of_line (pfile);
1016 /* We've found an `#if' directive. If the only thing before it in
1017 this file is white space, and if it is of the form
1018 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1019 for inclusion of this file. (See redundant_include_p in cppfiles.c
1020 for an explanation of controlling macros.) If so, return the
1021 hash node for SYMBOL. Otherwise, return NULL. */
1023 static const cpp_hashnode *
1024 detect_if_not_defined (pfile)
1027 const cpp_hashnode *cmacro = 0;
1028 enum cpp_ttype token;
1029 unsigned int base_offset;
1030 unsigned int token_offset;
1031 unsigned int need_rparen = 0;
1032 unsigned int token_len;
1034 if (pfile->skipping || pfile->only_seen_white != 2)
1037 /* Save state required for restore. */
1038 pfile->no_macro_expand++;
1039 CPP_SET_MARK (pfile);
1040 base_offset = CPP_WRITTEN (pfile);
1043 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1044 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1045 || CPP_PWRITTEN (pfile)[-1] != '!')
1048 /* ...then `defined', */
1049 token_offset = CPP_WRITTEN (pfile);
1050 token = _cpp_get_directive_token (pfile);
1051 if (token != CPP_NAME)
1053 if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1056 /* ...then an optional '(' and the name, */
1057 token_offset = CPP_WRITTEN (pfile);
1058 token = _cpp_get_directive_token (pfile);
1059 if (token == CPP_OPEN_PAREN)
1061 token_offset = CPP_WRITTEN (pfile);
1063 token = _cpp_get_directive_token (pfile);
1065 if (token != CPP_NAME)
1068 token_len = CPP_WRITTEN (pfile) - token_offset;
1070 /* ...then the ')', if necessary, */
1071 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1074 /* ...and make sure there's nothing else on the line. */
1075 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1078 /* We have a legitimate controlling macro for this header. */
1079 cmacro = cpp_lookup (pfile, pfile->token_buffer + token_offset, token_len);
1082 CPP_SET_WRITTEN (pfile, base_offset);
1083 pfile->no_macro_expand--;
1084 CPP_GOTO_MARK (pfile);
1089 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1090 not defined; the macro tested is left in the token buffer (but
1093 static const cpp_hashnode *
1094 parse_ifdef (pfile, name)
1100 enum cpp_ttype token;
1101 long old_written = CPP_WRITTEN (pfile);
1102 const cpp_hashnode *node = 0;
1104 pfile->no_macro_expand++;
1105 token = _cpp_get_directive_token (pfile);
1106 pfile->no_macro_expand--;
1108 ident = pfile->token_buffer + old_written;
1109 len = CPP_WRITTEN (pfile) - old_written;
1111 if (token == CPP_VSPACE)
1113 if (! CPP_TRADITIONAL (pfile))
1114 cpp_pedwarn (pfile, "#%s with no argument", name);
1117 else if (token == CPP_NAME)
1119 node = cpp_lookup (pfile, ident, len);
1123 if (! CPP_TRADITIONAL (pfile))
1124 cpp_error (pfile, "#%s with invalid argument", name);
1127 if (!CPP_TRADITIONAL (pfile))
1129 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1132 cpp_pedwarn (pfile, "garbage at end of #%s", name);
1134 _cpp_skip_rest_of_line (pfile);
1137 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1141 /* #ifdef is dead simple. */
1148 const cpp_hashnode *node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1149 if (node->type == T_POISON)
1150 cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1152 def = (node->type != T_VOID);
1153 push_conditional (pfile, !def, T_IFDEF, 0);
1157 /* #ifndef is a tad more complex, because we need to check for a
1158 no-reinclusion wrapper. */
1166 const cpp_hashnode *cmacro;
1168 start_of_file = pfile->only_seen_white == 2;
1169 cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1170 if (cmacro->type == T_POISON)
1171 cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1173 def = (cmacro->type != T_VOID);
1175 push_conditional (pfile, def, T_IFNDEF,
1176 start_of_file ? cmacro : 0);
1180 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1181 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1187 const cpp_hashnode *cmacro = 0;
1190 if (! pfile->skipping)
1192 cmacro = detect_if_not_defined (pfile);
1193 value = _cpp_parse_expr (pfile);
1195 push_conditional (pfile, value == 0, T_IF, cmacro);
1199 /* #else flips pfile->skipping and continues without changing
1200 if_stack; this is so that the error message for missing #endif's
1201 etc. will point to the original #if. */
1207 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1209 validate_else (pfile, dtable[T_ELSE].name);
1213 cpp_error (pfile, "#else without #if");
1216 if (ifs->type == T_ELSE)
1218 cpp_error (pfile, "#else after #else");
1219 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1222 /* #ifndef can't have its special treatment for containing the whole file
1223 if it has a #else clause. */
1227 if (! ifs->was_skipping)
1229 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1230 succeeded, so we mustn't do the else block. */
1231 if (pfile->skipping < 2)
1232 pfile->skipping = ! pfile->skipping;
1238 * handle a #elif directive by not changing if_stack either.
1239 * see the comment above do_else.
1246 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1250 cpp_error (pfile, "#elif without #if");
1253 if (ifs->type == T_ELSE)
1255 cpp_error (pfile, "#elif after #else");
1256 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1260 if (ifs->was_skipping)
1261 _cpp_skip_rest_of_line (pfile);
1262 else if (pfile->skipping != 1)
1264 _cpp_skip_rest_of_line (pfile);
1265 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1268 pfile->skipping = ! _cpp_parse_expr (pfile);
1274 /* #endif pops the if stack and resets pfile->skipping. */
1280 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1282 validate_else (pfile, dtable[T_ENDIF].name);
1285 cpp_error (pfile, "#endif without #if");
1288 CPP_BUFFER (pfile)->if_stack = ifs->next;
1289 pfile->skipping = ifs->was_skipping;
1290 pfile->potential_control_macro = ifs->cmacro;
1296 /* Push an if_stack entry and set pfile->skipping accordingly.
1297 If this is a #ifndef starting at the beginning of a file,
1298 CMACRO is the macro name tested by the #ifndef. */
1301 push_conditional (pfile, skip, type, cmacro)
1305 const cpp_hashnode *cmacro;
1307 struct if_stack *ifs;
1309 ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1310 ifs->lineno = CPP_BUFFER (pfile)->lineno;
1311 ifs->next = CPP_BUFFER (pfile)->if_stack;
1312 ifs->cmacro = cmacro;
1313 ifs->was_skipping = pfile->skipping;
1316 if (!pfile->skipping)
1317 pfile->skipping = skip;
1319 CPP_BUFFER (pfile)->if_stack = ifs;
1322 /* Issue -pedantic warning for text which is not a comment following
1323 an #else or #endif. */
1326 validate_else (pfile, directive)
1328 const U_CHAR *directive;
1330 if (CPP_PEDANTIC (pfile))
1332 long old_written = CPP_WRITTEN (pfile);
1333 pfile->no_macro_expand++;
1334 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1335 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1336 CPP_SET_WRITTEN (pfile, old_written);
1337 pfile->no_macro_expand--;
1339 _cpp_skip_rest_of_line (pfile);
1342 /* Called when we reach the end of a macro buffer. Walk back up the
1343 conditional stack till we reach its level at entry to this file,
1344 issuing error messages. Then force skipping off. */
1346 _cpp_unwind_if_stack (pfile, pbuf)
1350 struct if_stack *ifs, *nifs;
1352 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1354 cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1355 dtable[ifs->type].name);
1359 pfile->skipping = 0;
1362 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1363 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1364 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1373 struct predicate *pred = 0;
1374 enum cpp_ttype type;
1376 old_written = CPP_WRITTEN (pfile);
1377 pfile->no_macro_expand++;
1379 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1380 type = _cpp_get_directive_token (pfile);
1381 if (type == CPP_VSPACE)
1382 ERROR ("#assert without predicate");
1383 else if (type != CPP_NAME)
1384 ERROR ("assertion predicate is not an identifier");
1386 sym = pfile->token_buffer + old_written;
1387 len = CPP_WRITTEN (pfile) - old_written;
1388 hp = cpp_lookup (pfile, sym, len);
1390 if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1391 ERROR ("missing token-sequence in #assert");
1393 pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1394 _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1396 if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1398 ERROR ("missing close paren in #assert");
1400 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1401 ICE ("impossible token, expecting ) in do_assert");
1403 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1404 ERROR ("junk at end of #assert");
1406 if (hp->type == T_ASSERTION)
1408 /* Check for reassertion. */
1409 const struct predicate *old;
1411 for (old = hp->value.pred; old; old = old->next)
1412 if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1413 /* We used to warn about this, but SVR4 cc doesn't, so let's
1414 match that (also consistent with #define). goto error will
1417 pred->next = hp->value.pred;
1421 hp->type = T_ASSERTION;
1425 _cpp_squeeze_toklist (&pred->answer);
1426 hp->value.pred = pred;
1427 pfile->no_macro_expand--;
1428 CPP_SET_WRITTEN (pfile, old_written);
1432 _cpp_skip_rest_of_line (pfile);
1433 pfile->no_macro_expand--;
1434 CPP_SET_WRITTEN (pfile, old_written);
1437 _cpp_free_toklist (&pred->answer);
1452 enum cpp_ttype type;
1455 old_written = CPP_WRITTEN (pfile);
1456 pfile->no_macro_expand++;
1458 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1459 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1460 ERROR ("#unassert must be followed by an identifier");
1462 sym = pfile->token_buffer + old_written;
1463 len = CPP_WRITTEN (pfile) - old_written;
1464 hp = cpp_lookup (pfile, sym, len);
1466 type = _cpp_get_directive_token (pfile);
1467 if (type == CPP_OPEN_PAREN)
1470 _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1472 if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1474 ERROR ("missing close paren in #unassert");
1476 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1477 ICE ("impossible token, expecting ) in do_unassert");
1479 type = _cpp_get_directive_token (pfile);
1482 if (type != CPP_VSPACE)
1483 ERROR ("junk at end of #unassert");
1485 if (hp->type != T_ASSERTION)
1486 /* Not an error to #unassert something that isn't asserted.
1487 goto error to clean up. */
1492 /* Find this specific answer and remove it. */
1493 struct predicate *o, *p;
1495 for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1496 if (_cpp_equiv_toklists (&ans, &o->answer))
1501 hp->value.pred = o->next;
1503 _cpp_free_toklist (&o->answer);
1510 struct predicate *o, *p;
1511 for (o = hp->value.pred; o; o = p)
1514 _cpp_free_toklist ((cpp_toklist *) &o->answer);
1517 hp->value.pred = NULL;
1520 if (hp->value.pred == NULL)
1521 hp->type = T_VOID; /* Last answer for this predicate deleted. */
1524 _cpp_skip_rest_of_line (pfile);
1525 pfile->no_macro_expand--;
1526 CPP_SET_WRITTEN (pfile, old_written);
1528 _cpp_free_toklist (&ans);
1532 /* These are for -D, -U, -A. */
1534 /* Process the string STR as if it appeared as the body of a #define.
1535 If STR is just an identifier, define it with value 1.
1536 If STR has anything after the identifier, then it should
1537 be identifier=definition. */
1540 cpp_define (pfile, str)
1547 p = strchr (str, '=');
1548 /* Copy the entire option so we can modify it.
1549 Change the first "=" in the string to a space. If there is none,
1550 tack " 1" on the end. Then add a newline and a NUL. */
1554 count = strlen (str) + 2;
1555 buf = (char *) alloca (count);
1556 memcpy (buf, str, count - 2);
1558 buf[count - 2] = '\n';
1559 buf[count - 1] = '\0';
1563 count = strlen (str) + 4;
1564 buf = (char *) alloca (count);
1565 memcpy (buf, str, count - 4);
1566 strcpy (&buf[count-4], " 1\n");
1569 if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1572 cpp_pop_buffer (pfile);
1576 /* Process MACRO as if it appeared as the body of an #undef. */
1578 cpp_undef (pfile, macro)
1582 /* Copy the string so we can append a newline. */
1583 size_t len = strlen (macro);
1584 char *buf = (char *) alloca (len + 2);
1585 memcpy (buf, macro, len);
1587 buf[len + 1] = '\0';
1588 if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1591 cpp_pop_buffer (pfile);
1595 /* Process the string STR as if it appeared as the body of a #assert. */
1597 cpp_assert (pfile, str)
1601 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1604 cpp_pop_buffer (pfile);
1608 /* Process STR as if it appeared as the body of an #unassert. */
1610 cpp_unassert (pfile, str)
1614 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1616 do_unassert (pfile);
1617 cpp_pop_buffer (pfile);
1621 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1623 cpp_defined (pfile, id, len)
1628 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1629 if (hp->type == T_POISON)
1631 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1634 return (hp->type != T_VOID);