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)
416 cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", name);
417 CPP_SET_WRITTEN (pfile, old_written);
418 _cpp_skip_rest_of_line (pfile);
422 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
424 cpp_error (pfile, "junk at end of #%s", name);
425 _cpp_skip_rest_of_line (pfile);
428 CPP_SET_WRITTEN (pfile, old_written);
431 cpp_error (pfile, "empty file name in #%s", name);
443 len = parse_include (pfile, dtable[T_INCLUDE].name);
446 token = (U_CHAR *) alloca (len + 1);
447 memcpy (token, CPP_PWRITTEN (pfile), len);
450 if (CPP_OPTION (pfile, dump_includes))
451 pass_thru_directive (token, len, pfile, T_INCLUDE);
453 _cpp_execute_include (pfile, token, len, 0, 0);
464 if (CPP_OPTION (pfile, warn_import)
465 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
467 pfile->import_warning = 1;
469 "#import is obsolete, use an #ifndef wrapper in the header file");
472 len = parse_include (pfile, dtable[T_IMPORT].name);
475 token = (U_CHAR *) alloca (len + 1);
476 memcpy (token, CPP_PWRITTEN (pfile), len);
479 if (CPP_OPTION (pfile, dump_includes))
480 pass_thru_directive (token, len, pfile, T_IMPORT);
482 _cpp_execute_include (pfile, token, len, 1, 0);
487 do_include_next (pfile)
492 struct file_name_list *search_start = 0;
494 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
497 token = (U_CHAR *) alloca (len + 1);
498 memcpy (token, CPP_PWRITTEN (pfile), len);
501 if (CPP_OPTION (pfile, dump_includes))
502 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
504 /* For #include_next, skip in the search path past the dir in which the
505 containing file was found. Treat files specified using an absolute path
506 as if there are no more directories to search. Treat the primary source
507 file like any other included source, but generate a warning. */
508 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
510 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
511 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
514 cpp_warning (pfile, "#include_next in primary source file");
516 _cpp_execute_include (pfile, token, len, 0, search_start);
520 /* Subroutine of do_line. Read next token from PFILE without adding it to
521 the output buffer. If it is a number between 1 and 4, store it in *NUM
522 and return 1; otherwise, return 0 and complain if we aren't at the end
526 read_line_number (pfile, num)
530 long save_written = CPP_WRITTEN (pfile);
532 enum cpp_ttype token = _cpp_get_directive_token (pfile);
533 p = pfile->token_buffer + save_written;
535 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
536 && p[0] >= '1' && p[0] <= '4')
539 CPP_SET_WRITTEN (pfile, save_written);
544 if (token != CPP_VSPACE && token != CPP_EOF)
545 cpp_error (pfile, "invalid format #line");
546 CPP_SET_WRITTEN (pfile, save_written);
551 /* Interpret #line command.
552 Note that the filename string (if any) is treated as if it were an
553 include filename. That means no escape handling. */
559 cpp_buffer *ip = CPP_BUFFER (pfile);
560 unsigned int new_lineno;
561 long old_written = CPP_WRITTEN (pfile);
562 enum cpp_ttype token;
565 token = _cpp_get_directive_token (pfile);
567 if (token != CPP_NUMBER)
569 cpp_error (pfile, "token after #line is not an integer");
570 goto bad_line_directive;
573 CPP_PUTC (pfile, '\0'); /* not terminated for us */
574 new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
578 cpp_error (pfile, "token after #line is not an integer");
579 goto bad_line_directive;
581 CPP_SET_WRITTEN (pfile, old_written);
583 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
584 cpp_pedwarn (pfile, "line number out of range in #line");
586 token = _cpp_get_directive_token (pfile);
588 if (token == CPP_STRING)
590 U_CHAR *fname = pfile->token_buffer + old_written + 1;
591 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
592 int action_number = 0;
594 if (read_line_number (pfile, &action_number))
596 if (CPP_PEDANTIC (pfile))
597 cpp_pedwarn (pfile, "garbage at end of #line");
599 /* This is somewhat questionable: change the buffer stack
600 depth so that output_line_command thinks we've stacked
602 if (action_number == 1)
604 pfile->buffer_stack_depth++;
605 ip->system_header_p = 0;
606 read_line_number (pfile, &action_number);
608 else if (action_number == 2)
610 pfile->buffer_stack_depth--;
611 ip->system_header_p = 0;
612 read_line_number (pfile, &action_number);
614 if (action_number == 3)
616 ip->system_header_p = 1;
617 read_line_number (pfile, &action_number);
619 if (action_number == 4)
621 ip->system_header_p = 2;
622 read_line_number (pfile, &action_number);
628 if (strcmp ((const char *)fname, ip->nominal_fname))
630 if (!strcmp ((const char *)fname, ip->ihash->name))
631 ip->nominal_fname = ip->ihash->name;
633 ip->nominal_fname = _cpp_fake_ihash (pfile, (const char *)fname);
636 else if (token != CPP_VSPACE && token != CPP_EOF)
638 cpp_error (pfile, "second token after #line is not a string");
639 goto bad_line_directive;
642 /* The Newline at the end of this line remains to be processed.
643 To put the next line at the specified line number,
644 we must store a line number now that is one less. */
645 ip->lineno = new_lineno - 1;
646 CPP_SET_WRITTEN (pfile, old_written);
650 _cpp_skip_rest_of_line (pfile);
651 CPP_SET_WRITTEN (pfile, old_written);
655 /* Remove the definition of a symbol from the symbol table.
656 According to the C standard, it is not an error to undef
657 something that has no definitions. */
665 long here = CPP_WRITTEN (pfile);
666 enum cpp_ttype token;
668 pfile->no_macro_expand++;
669 token = _cpp_get_directive_token (pfile);
670 pfile->no_macro_expand--;
672 if (token != CPP_NAME)
674 cpp_error (pfile, "token after #undef is not an identifier");
675 _cpp_skip_rest_of_line (pfile);
678 len = CPP_WRITTEN (pfile) - here;
680 token = _cpp_get_directive_token (pfile);
681 if (token != CPP_VSPACE)
683 cpp_pedwarn (pfile, "junk on line after #undef");
684 _cpp_skip_rest_of_line (pfile);
687 name = pfile->token_buffer + here;
688 CPP_SET_WRITTEN (pfile, here);
690 hp = cpp_lookup (pfile, name, len);
691 if (hp->type == T_VOID)
692 ; /* Not defined in the first place - do nothing. */
693 else if (hp->type == T_POISON)
694 cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
697 /* If we are generating additional info for debugging (with -g) we
698 need to pass through all effective #undef commands. */
699 if (CPP_OPTION (pfile, debug_output))
700 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
702 if (hp->type != T_MACRO && hp->type != T_FMACRO
703 && hp->type != T_EMPTY && hp->type != T_IDENTITY)
704 cpp_warning (pfile, "undefining `%s'", hp->name);
706 _cpp_free_definition (hp);
714 * Report an error detected by the program we are processing.
715 * Use the text of the line in the error message.
716 * (We use error because it prints the filename & line#.)
723 const U_CHAR *text, *limit;
725 _cpp_skip_hspace (pfile);
726 text = CPP_BUFFER (pfile)->cur;
727 _cpp_skip_rest_of_line (pfile);
728 limit = CPP_BUFFER (pfile)->cur;
730 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
735 * Report a warning detected by the program we are processing.
736 * Use the text of the line in the warning message, then continue.
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_warning (pfile, "#warning %.*s", (int)(limit - text), text);
754 /* Report program identification. */
760 long old_written = CPP_WRITTEN (pfile);
762 CPP_PUTS (pfile, "#ident ", 7);
764 /* Next token should be a string constant. */
765 if (_cpp_get_directive_token (pfile) == CPP_STRING)
766 /* And then a newline. */
767 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
768 /* Good - ship it. */
771 cpp_error (pfile, "invalid #ident");
772 _cpp_skip_rest_of_line (pfile);
773 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
778 /* Pragmata handling. We handle some of these, and pass the rest on
779 to the front end. C99 defines three pragmas and says that no macro
780 expansion is to be performed on them; whether or not macro
781 expansion happens for other pragmas is implementation defined.
782 This implementation never macro-expands the text after #pragma.
784 We currently do not support the _Pragma operator. Support for that
785 has to be coordinated with the front end. Proposed implementation:
786 both #pragma blah blah and _Pragma("blah blah") become
787 __builtin_pragma(blah blah) and we teach the parser about that. */
789 /* Sub-handlers for the pragmas needing treatment here.
790 They return 1 if the token buffer is to be popped, 0 if not. */
791 static int do_pragma_once PARAMS ((cpp_reader *));
792 static int do_pragma_implementation PARAMS ((cpp_reader *));
793 static int do_pragma_poison PARAMS ((cpp_reader *));
794 static int do_pragma_system_header PARAMS ((cpp_reader *));
795 static int do_pragma_default PARAMS ((cpp_reader *));
804 enum cpp_ttype token;
806 here = CPP_WRITTEN (pfile);
807 CPP_PUTS (pfile, "#pragma ", 8);
809 key = CPP_WRITTEN (pfile);
810 pfile->no_macro_expand++;
811 token = _cpp_get_directive_token (pfile);
812 if (token != CPP_NAME)
814 if (token == CPP_VSPACE)
820 buf = pfile->token_buffer + key;
821 CPP_PUTC (pfile, ' ');
823 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
825 pop = do_pragma_once (pfile);
826 else if (tokis ("implementation"))
827 pop = do_pragma_implementation (pfile);
828 else if (tokis ("poison"))
829 pop = do_pragma_poison (pfile);
830 else if (tokis ("system_header"))
831 pop = do_pragma_system_header (pfile);
833 pop = do_pragma_default (pfile);
836 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
840 CPP_SET_WRITTEN (pfile, here);
841 pfile->no_macro_expand--;
845 cpp_error (pfile, "malformed #pragma directive");
846 _cpp_skip_rest_of_line (pfile);
848 CPP_SET_WRITTEN (pfile, here);
849 pfile->no_macro_expand--;
854 do_pragma_default (pfile)
857 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
858 CPP_PUTC (pfile, ' ');
863 do_pragma_once (pfile)
866 cpp_buffer *ip = CPP_BUFFER (pfile);
868 /* Allow #pragma once in system headers, since that's not the user's
870 if (!ip->system_header_p)
871 cpp_warning (pfile, "#pragma once is obsolete");
873 if (CPP_PREV_BUFFER (ip) == NULL)
874 cpp_warning (pfile, "#pragma once outside include file");
876 ip->ihash->cmacro = NEVER_REINCLUDE;
882 do_pragma_implementation (pfile)
885 /* Be quiet about `#pragma implementation' for a file only if it hasn't
886 been included yet. */
887 enum cpp_ttype token;
888 long written = CPP_WRITTEN (pfile);
893 token = _cpp_get_directive_token (pfile);
894 if (token == CPP_VSPACE)
896 else if (token != CPP_STRING)
898 cpp_error (pfile, "malformed #pragma implementation");
902 /* Trim the leading and trailing quote marks from the string. */
903 name = pfile->token_buffer + written + 1;
904 len = CPP_PWRITTEN (pfile) - name;
906 memcpy (copy, name, len - 1);
907 copy[len - 1] = '\0';
909 if (cpp_included (pfile, copy))
911 "#pragma implementation for %s appears after file is included",
917 do_pragma_poison (pfile)
920 /* Poison these symbols so that all subsequent usage produces an
926 enum cpp_ttype token;
929 /* As a rule, don't include #pragma poison commands in output,
930 unless the user asks for them. */
931 writeit = (CPP_OPTION (pfile, debug_output)
932 || CPP_OPTION (pfile, dump_macros) == dump_definitions
933 || CPP_OPTION (pfile, dump_macros) == dump_names);
937 written = CPP_WRITTEN (pfile);
938 token = _cpp_get_directive_token (pfile);
939 if (token == CPP_VSPACE)
941 if (token != CPP_NAME)
943 cpp_error (pfile, "invalid #pragma poison directive");
944 _cpp_skip_rest_of_line (pfile);
948 p = pfile->token_buffer + written;
949 len = CPP_PWRITTEN (pfile) - p;
950 hp = cpp_lookup (pfile, p, len);
951 if (hp->type == T_POISON)
952 ; /* It is allowed to poison the same identifier twice. */
955 if (hp->type != T_VOID)
956 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
957 _cpp_free_definition (hp);
961 CPP_PUTC (pfile, ' ');
966 /* Mark the current header as a system header. This will suppress
967 some categories of warnings (notably those from -pedantic). It is
968 intended for use in system libraries that cannot be implemented in
969 conforming C, but cannot be certain that their headers appear in a
970 system include directory. To prevent abuse, it is rejected in the
971 primary source file. */
973 do_pragma_system_header (pfile)
976 cpp_buffer *ip = cpp_file_buffer (pfile);
977 if (CPP_PREV_BUFFER (ip) == NULL)
978 cpp_warning (pfile, "#pragma system_header outside include file");
980 ip->system_header_p = 1;
985 /* Just ignore #sccs, on systems where we define it at all. */
986 #ifdef SCCS_DIRECTIVE
991 _cpp_skip_rest_of_line (pfile);
996 /* We've found an `#if' directive. If the only thing before it in
997 this file is white space, and if it is of the form
998 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
999 for inclusion of this file. (See redundant_include_p in cppfiles.c
1000 for an explanation of controlling macros.) If so, return the
1001 hash node for SYMBOL. Otherwise, return NULL. */
1003 static const cpp_hashnode *
1004 detect_if_not_defined (pfile)
1007 const cpp_hashnode *cmacro = 0;
1008 enum cpp_ttype token;
1009 unsigned int base_offset;
1010 unsigned int token_offset;
1011 unsigned int need_rparen = 0;
1012 unsigned int token_len;
1014 if (pfile->skipping || pfile->only_seen_white != 2)
1017 /* Save state required for restore. */
1018 pfile->no_macro_expand++;
1019 CPP_SET_MARK (pfile);
1020 base_offset = CPP_WRITTEN (pfile);
1023 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1024 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1025 || CPP_PWRITTEN (pfile)[-1] != '!')
1028 /* ...then `defined', */
1029 token_offset = CPP_WRITTEN (pfile);
1030 token = _cpp_get_directive_token (pfile);
1031 if (token != CPP_NAME)
1033 if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1036 /* ...then an optional '(' and the name, */
1037 token_offset = CPP_WRITTEN (pfile);
1038 token = _cpp_get_directive_token (pfile);
1039 if (token == CPP_OPEN_PAREN)
1041 token_offset = CPP_WRITTEN (pfile);
1043 token = _cpp_get_directive_token (pfile);
1045 if (token != CPP_NAME)
1048 token_len = CPP_WRITTEN (pfile) - token_offset;
1050 /* ...then the ')', if necessary, */
1051 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1054 /* ...and make sure there's nothing else on the line. */
1055 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1058 /* We have a legitimate controlling macro for this header. */
1059 cmacro = cpp_lookup (pfile, pfile->token_buffer + token_offset, token_len);
1062 CPP_SET_WRITTEN (pfile, base_offset);
1063 pfile->no_macro_expand--;
1064 CPP_GOTO_MARK (pfile);
1069 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1070 not defined; the macro tested is left in the token buffer (but
1073 static const cpp_hashnode *
1074 parse_ifdef (pfile, name)
1080 enum cpp_ttype token;
1081 long old_written = CPP_WRITTEN (pfile);
1082 const cpp_hashnode *node = 0;
1084 pfile->no_macro_expand++;
1085 token = _cpp_get_directive_token (pfile);
1086 pfile->no_macro_expand--;
1088 ident = pfile->token_buffer + old_written;
1089 len = CPP_WRITTEN (pfile) - old_written;
1091 if (token == CPP_VSPACE)
1093 if (! CPP_TRADITIONAL (pfile))
1094 cpp_pedwarn (pfile, "#%s with no argument", name);
1097 else if (token == CPP_NAME)
1099 node = cpp_lookup (pfile, ident, len);
1103 if (! CPP_TRADITIONAL (pfile))
1104 cpp_error (pfile, "#%s with invalid argument", name);
1107 if (!CPP_TRADITIONAL (pfile))
1109 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1112 cpp_pedwarn (pfile, "garbage at end of #%s", name);
1114 _cpp_skip_rest_of_line (pfile);
1117 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1121 /* #ifdef is dead simple. */
1128 const cpp_hashnode *node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1131 if (node->type == T_POISON)
1132 cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1134 def = (node->type != T_VOID);
1136 push_conditional (pfile, !def, T_IFDEF, 0);
1140 /* #ifndef is a tad more complex, because we need to check for a
1141 no-reinclusion wrapper. */
1149 const cpp_hashnode *cmacro;
1151 start_of_file = pfile->only_seen_white == 2;
1152 cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1155 if (cmacro->type == T_POISON)
1156 cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1158 def = (cmacro->type != T_VOID);
1160 push_conditional (pfile, def, T_IFNDEF,
1161 start_of_file ? cmacro : 0);
1165 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1166 Also, check for a reinclude preventer of the form #if !defined (MACRO). */
1172 const cpp_hashnode *cmacro = 0;
1175 if (! pfile->skipping)
1177 cmacro = detect_if_not_defined (pfile);
1178 value = _cpp_parse_expr (pfile);
1180 push_conditional (pfile, value == 0, T_IF, cmacro);
1184 /* #else flips pfile->skipping and continues without changing
1185 if_stack; this is so that the error message for missing #endif's
1186 etc. will point to the original #if. */
1192 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1194 validate_else (pfile, dtable[T_ELSE].name);
1198 cpp_error (pfile, "#else without #if");
1201 if (ifs->type == T_ELSE)
1203 cpp_error (pfile, "#else after #else");
1204 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1207 /* #ifndef can't have its special treatment for containing the whole file
1208 if it has a #else clause. */
1212 if (! ifs->was_skipping)
1214 /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1215 succeeded, so we mustn't do the else block. */
1216 if (pfile->skipping < 2)
1217 pfile->skipping = ! pfile->skipping;
1223 * handle a #elif directive by not changing if_stack either.
1224 * see the comment above do_else.
1231 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1235 cpp_error (pfile, "#elif without #if");
1238 if (ifs->type == T_ELSE)
1240 cpp_error (pfile, "#elif after #else");
1241 cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1245 if (ifs->was_skipping)
1246 _cpp_skip_rest_of_line (pfile);
1247 else if (pfile->skipping != 1)
1249 _cpp_skip_rest_of_line (pfile);
1250 pfile->skipping = 2; /* one block succeeded, so don't do any others */
1253 pfile->skipping = ! _cpp_parse_expr (pfile);
1259 /* #endif pops the if stack and resets pfile->skipping. */
1265 struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1267 validate_else (pfile, dtable[T_ENDIF].name);
1270 cpp_error (pfile, "#endif without #if");
1273 CPP_BUFFER (pfile)->if_stack = ifs->next;
1274 pfile->skipping = ifs->was_skipping;
1275 pfile->potential_control_macro = ifs->cmacro;
1281 /* Push an if_stack entry and set pfile->skipping accordingly.
1282 If this is a #ifndef starting at the beginning of a file,
1283 CMACRO is the macro name tested by the #ifndef. */
1286 push_conditional (pfile, skip, type, cmacro)
1290 const cpp_hashnode *cmacro;
1292 struct if_stack *ifs;
1294 ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1295 ifs->lineno = CPP_BUFFER (pfile)->lineno;
1296 ifs->next = CPP_BUFFER (pfile)->if_stack;
1297 ifs->cmacro = cmacro;
1298 ifs->was_skipping = pfile->skipping;
1301 if (!pfile->skipping)
1302 pfile->skipping = skip;
1304 CPP_BUFFER (pfile)->if_stack = ifs;
1307 /* Issue -pedantic warning for text which is not a comment following
1308 an #else or #endif. */
1311 validate_else (pfile, directive)
1313 const U_CHAR *directive;
1315 if (CPP_PEDANTIC (pfile))
1317 long old_written = CPP_WRITTEN (pfile);
1318 pfile->no_macro_expand++;
1319 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1320 cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1321 CPP_SET_WRITTEN (pfile, old_written);
1322 pfile->no_macro_expand--;
1324 _cpp_skip_rest_of_line (pfile);
1327 /* Called when we reach the end of a macro buffer. Walk back up the
1328 conditional stack till we reach its level at entry to this file,
1329 issuing error messages. Then force skipping off. */
1331 _cpp_unwind_if_stack (pfile, pbuf)
1335 struct if_stack *ifs, *nifs;
1337 for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1339 cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1340 dtable[ifs->type].name);
1344 pfile->skipping = 0;
1347 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1348 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1349 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1358 struct predicate *pred = 0;
1359 enum cpp_ttype type;
1361 old_written = CPP_WRITTEN (pfile);
1362 pfile->no_macro_expand++;
1364 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1365 type = _cpp_get_directive_token (pfile);
1366 if (type == CPP_VSPACE)
1367 ERROR ("#assert without predicate");
1368 else if (type != CPP_NAME)
1369 ERROR ("assertion predicate is not an identifier");
1371 sym = pfile->token_buffer + old_written;
1372 len = CPP_WRITTEN (pfile) - old_written;
1373 hp = cpp_lookup (pfile, sym, len);
1375 if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1376 ERROR ("missing token-sequence in #assert");
1378 pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1379 _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1381 if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1383 ERROR ("missing close paren in #assert");
1385 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1386 ICE ("impossible token, expecting ) in do_assert");
1388 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1389 ERROR ("junk at end of #assert");
1391 if (hp->type == T_ASSERTION)
1393 /* Check for reassertion. */
1394 const struct predicate *old;
1396 for (old = hp->value.pred; old; old = old->next)
1397 if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1398 /* We used to warn about this, but SVR4 cc doesn't, so let's
1399 match that (also consistent with #define). goto error will
1402 pred->next = hp->value.pred;
1406 hp->type = T_ASSERTION;
1410 _cpp_squeeze_toklist (&pred->answer);
1411 hp->value.pred = pred;
1412 pfile->no_macro_expand--;
1413 CPP_SET_WRITTEN (pfile, old_written);
1417 _cpp_skip_rest_of_line (pfile);
1418 pfile->no_macro_expand--;
1419 CPP_SET_WRITTEN (pfile, old_written);
1422 _cpp_free_toklist (&pred->answer);
1437 enum cpp_ttype type;
1440 old_written = CPP_WRITTEN (pfile);
1441 pfile->no_macro_expand++;
1443 CPP_PUTC (pfile, '#'); /* force token out of macro namespace */
1444 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1445 ERROR ("#unassert must be followed by an identifier");
1447 sym = pfile->token_buffer + old_written;
1448 len = CPP_WRITTEN (pfile) - old_written;
1449 hp = cpp_lookup (pfile, sym, len);
1451 type = _cpp_get_directive_token (pfile);
1452 if (type == CPP_OPEN_PAREN)
1455 _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1457 if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1459 ERROR ("missing close paren in #unassert");
1461 if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1462 ICE ("impossible token, expecting ) in do_unassert");
1464 type = _cpp_get_directive_token (pfile);
1467 if (type != CPP_VSPACE)
1468 ERROR ("junk at end of #unassert");
1470 if (hp->type != T_ASSERTION)
1471 /* Not an error to #unassert something that isn't asserted.
1472 goto error to clean up. */
1477 /* Find this specific answer and remove it. */
1478 struct predicate *o, *p;
1480 for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1481 if (_cpp_equiv_toklists (&ans, &o->answer))
1486 hp->value.pred = o->next;
1488 _cpp_free_toklist (&o->answer);
1495 struct predicate *o, *p;
1496 for (o = hp->value.pred; o; o = p)
1499 _cpp_free_toklist ((cpp_toklist *) &o->answer);
1502 hp->value.pred = NULL;
1505 if (hp->value.pred == NULL)
1506 hp->type = T_VOID; /* Last answer for this predicate deleted. */
1509 _cpp_skip_rest_of_line (pfile);
1510 pfile->no_macro_expand--;
1511 CPP_SET_WRITTEN (pfile, old_written);
1513 _cpp_free_toklist (&ans);
1517 /* These are for -D, -U, -A. */
1519 /* Process the string STR as if it appeared as the body of a #define.
1520 If STR is just an identifier, define it with value 1.
1521 If STR has anything after the identifier, then it should
1522 be identifier=definition. */
1525 cpp_define (pfile, str)
1532 p = strchr (str, '=');
1533 /* Copy the entire option so we can modify it.
1534 Change the first "=" in the string to a space. If there is none,
1535 tack " 1" on the end. Then add a newline and a NUL. */
1539 count = strlen (str) + 2;
1540 buf = (char *) alloca (count);
1541 memcpy (buf, str, count - 2);
1543 buf[count - 2] = '\n';
1544 buf[count - 1] = '\0';
1548 count = strlen (str) + 4;
1549 buf = (char *) alloca (count);
1550 memcpy (buf, str, count - 4);
1551 strcpy (&buf[count-4], " 1\n");
1554 if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1557 cpp_pop_buffer (pfile);
1561 /* Process MACRO as if it appeared as the body of an #undef. */
1563 cpp_undef (pfile, macro)
1567 /* Copy the string so we can append a newline. */
1568 size_t len = strlen (macro);
1569 char *buf = (char *) alloca (len + 2);
1570 memcpy (buf, macro, len);
1572 buf[len + 1] = '\0';
1573 if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1576 cpp_pop_buffer (pfile);
1580 /* Process the string STR as if it appeared as the body of a #assert. */
1582 cpp_assert (pfile, str)
1586 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1589 cpp_pop_buffer (pfile);
1593 /* Process STR as if it appeared as the body of an #unassert. */
1595 cpp_unassert (pfile, str)
1599 if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1601 do_unassert (pfile);
1602 cpp_pop_buffer (pfile);
1606 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1608 cpp_defined (pfile, id, len)
1613 cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1614 if (hp->type == T_POISON)
1616 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1619 return (hp->type != T_VOID);