2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 /* `struct directive' defines one #-directive, including how to handle it. */
36 directive_handler func; /* Function to handle directive. */
37 const char *name; /* Name of directive. */
38 unsigned short length; /* Length of name. */
39 unsigned short flags; /* Flags describing this directive. */
42 /* Stack of conditionals currently in progress
43 (including both successful and failing conditionals). */
47 struct if_stack *next;
48 int lineno; /* line number where condition started */
49 int if_succeeded; /* truth of last condition in this group */
50 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
51 int type; /* type of last directive seen in this group */
53 typedef struct if_stack IF_STACK;
55 /* Forward declarations. */
57 static void validate_else PARAMS ((cpp_reader *, const char *));
58 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
59 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
60 static int conditional_skip PARAMS ((cpp_reader *, int, int,
62 static int skip_if_group PARAMS ((cpp_reader *));
63 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
65 static int read_line_number PARAMS ((cpp_reader *, int *));
66 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
67 static int consider_directive_while_skipping
68 PARAMS ((cpp_reader *, IF_STACK *));
69 static int get_macro_name PARAMS ((cpp_reader *));
71 /* Values for the flags field of the table below. KANDR and COND
72 directives come from traditional (K&R) C. The difference is, if we
73 care about it while skipping a failed conditional block, its origin
74 is COND. STDC89 directives come from the 1989 C standard.
75 EXTENSION directives are extensions, with origins noted below. */
83 #define ORIGIN(f) ((f) & ORIGIN_MASK)
84 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
86 /* This is the table of directive handlers. It is ordered by
87 frequency of occurrence; the numbers at the end are directive
88 counts from all the source code I have lying around (egcs and libc
89 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
92 The entries with a dash and a name after the count are extensions,
93 of which all but #warning and #include_next are deprecated. The name
94 is where the extension appears to have come from. */
96 /* #sccs is not always recognized. */
98 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
100 # define SCCS_ENTRY /* nothing */
103 #define DIRECTIVE_TABLE \
104 D(define, T_DEFINE = 0, KANDR) /* 270554 */ \
105 D(include, T_INCLUDE, KANDR | SYNTAX_INCLUDE) /* 52262 */ \
106 D(endif, T_ENDIF, COND) /* 45855 */ \
107 D(ifdef, T_IFDEF, COND) /* 22000 */ \
108 D(if, T_IF, COND) /* 18162 */ \
109 D(else, T_ELSE, COND) /* 9863 */ \
110 D(ifndef, T_IFNDEF, COND) /* 9675 */ \
111 D(undef, T_UNDEF, KANDR) /* 4837 */ \
112 D(line, T_LINE, KANDR) /* 2465 */ \
113 D(elif, T_ELIF, COND) /* 610 */ \
114 D(error, T_ERROR, STDC89) /* 475 */ \
115 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
116 D(warning, T_WARNING, EXTENSION) /* 22 GNU */ \
117 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /* 19 GNU */ \
118 D(ident, T_IDENT, EXTENSION) /* 11 SVR4 */ \
119 D(import, T_IMPORT, EXTENSION | SYNTAX_INCLUDE) /* 0 ObjC */ \
120 D(assert, T_ASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
121 D(unassert, T_UNASSERT, EXTENSION | SYNTAX_ASSERT) /* 0 SVR4 */ \
124 /* Use the table to generate a series of prototypes, an enum for the
125 directive names, and an array of directive handlers. */
127 /* The directive-processing functions are declared to return int
128 instead of void, because some old compilers have trouble with
129 pointers to functions returning void. */
131 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
132 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
136 #define D(n, tag, f) tag,
144 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
145 #define D(name, t, flags) \
146 { CONCAT2(do_,name), STRINGX(name), sizeof STRINGX(name) - 1, flags },
147 static const struct directive dtable[] =
152 #undef DIRECTIVE_TABLE
154 /* Handle a possible # directive.
155 '#' has already been read. */
158 _cpp_handle_directive (pfile)
165 long old_written = CPP_WRITTEN (pfile);
168 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
170 cpp_ice (pfile, "handle_directive called on macro buffer");
174 /* -traditional directives are recognized only with the # in column 1. */
175 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
177 /* Scan the next token, then pretend we didn't. */
178 CPP_SET_MARK (pfile);
179 pfile->no_macro_expand++;
180 tok = _cpp_get_directive_token (pfile);
181 pfile->no_macro_expand--;
183 ident = pfile->token_buffer + old_written;
184 len = CPP_PWRITTEN (pfile) - ident;
185 CPP_SET_WRITTEN (pfile, old_written);
186 CPP_GOTO_MARK (pfile);
188 /* # followed by a number is equivalent to #line. Do not recognize
189 this form in assembly language source files. Complain about this
190 form if we're being pedantic, but not if this is regurgitated
191 input (preprocessed or fed back in by the C++ frontend). */
192 if (tok == CPP_NUMBER)
194 if (CPP_OPTION (pfile, lang_asm))
197 if (CPP_PEDANTIC (pfile)
198 && ! CPP_OPTION (pfile, preprocessed)
199 && ! CPP_BUFFER (pfile)->manual_pop)
200 cpp_pedwarn (pfile, "# followed by integer");
205 /* If we are rescanning preprocessed input, don't obey any directives
207 else if (CPP_OPTION (pfile, preprocessed))
210 /* A line of just # becomes blank. */
211 else if (tok == CPP_VSPACE)
214 /* A NAME token might in fact be a directive! */
215 else if (tok == CPP_NAME)
217 for (i = 0; i < N_DIRECTIVES; i++)
219 if (dtable[i].length == len
220 && !strncmp (dtable[i].name, ident, len))
223 /* Don't complain about invalid directives in assembly source,
224 we don't know where the comments are, and # may introduce
225 assembler pseudo-ops. */
226 if (!CPP_OPTION (pfile, lang_asm))
227 cpp_error (pfile, "invalid preprocessing directive #%s", ident);
230 /* And anything else means the # wasn't a directive marker. */
236 /* In -traditional mode, a directive is ignored unless its # is in
238 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
240 if (CPP_WTRADITIONAL (pfile))
241 cpp_warning (pfile, "ignoring #%s because of its indented #",
246 /* no_directives is set when we are parsing macro arguments. Directives
247 in macro arguments are undefined behavior (C99 6.10.3.11); this
248 implementation chooses to make them hard errors. */
249 if (pfile->no_directives)
251 cpp_error (pfile, "#%s may not be used inside a macro argument",
253 _cpp_skip_rest_of_line (pfile);
257 /* Issue -pedantic warnings for extended directives. */
258 if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
259 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
261 /* -Wtraditional gives warnings about directives with inappropriate
263 if (CPP_WTRADITIONAL (pfile))
265 if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
266 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
268 else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
270 "suggest hiding #%s from traditional C with an indented #",
274 /* Unfortunately, it's necessary to scan the directive name again,
275 now we know we're going to consume it. FIXME. */
277 pfile->no_macro_expand++;
278 _cpp_get_directive_token (pfile);
279 pfile->no_macro_expand--;
280 CPP_SET_WRITTEN (pfile, old_written);
282 /* Some directives (e.g. #if) may return a request to execute
283 another directive handler immediately. No directive ever
284 requests that #define be executed immediately, so it is safe for
285 the loop to terminate when some function returns 0 (== T_DEFINE). */
286 while ((i = dtable[i].func (pfile)));
290 /* Pass a directive through to the output file.
291 BUF points to the contents of the directive, as a contiguous string.
292 LEN is the length of the string pointed to by BUF.
293 KEYWORD is the keyword-table entry for the directive. */
296 pass_thru_directive (buf, len, pfile, keyword)
302 const struct directive *kt = &dtable[keyword];
303 register unsigned klen = kt->length;
305 CPP_RESERVE (pfile, 1 + klen + len);
306 CPP_PUTC_Q (pfile, '#');
307 CPP_PUTS_Q (pfile, kt->name, klen);
308 if (len != 0 && buf[0] != ' ')
309 CPP_PUTC_Q (pfile, ' ');
310 CPP_PUTS_Q (pfile, buf, len);
313 /* Subroutine of do_define: determine the name of the macro to be
317 get_macro_name (pfile)
322 here = CPP_WRITTEN (pfile);
323 if (_cpp_get_directive_token (pfile) != CPP_NAME)
325 cpp_error (pfile, "`#define' must be followed by an identifier");
329 len = CPP_WRITTEN (pfile) - here;
330 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
332 cpp_error (pfile, "`defined' is not a legal macro name");
339 _cpp_skip_rest_of_line (pfile);
343 /* Process a #define command. */
354 int funlike = 0, empty = 0;
356 enum cpp_ttype token;
358 pfile->no_macro_expand++;
359 pfile->parsing_define_directive++;
360 CPP_OPTION (pfile, discard_comments)++;
362 here = CPP_WRITTEN (pfile);
363 len = get_macro_name (pfile);
367 /* Copy out the name so we can pop the token buffer. */
368 len = CPP_WRITTEN (pfile) - here;
369 sym = (U_CHAR *) alloca (len + 1);
370 memcpy (sym, pfile->token_buffer + here, len);
373 /* If the next character, with no intervening whitespace, is '(',
374 then this is a function-like macro.
375 XXX Layering violation. */
376 CPP_SET_MARK (pfile);
377 token = _cpp_get_directive_token (pfile);
378 if (token == CPP_VSPACE)
379 empty = 0; /* Empty definition of object like macro. */
380 else if (token == CPP_LPAREN && ADJACENT_TO_MARK (pfile))
382 else if (ADJACENT_TO_MARK (pfile))
383 /* If this is an object-like macro, C99 requires white space after
385 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
386 CPP_GOTO_MARK (pfile);
387 CPP_SET_WRITTEN (pfile, here);
391 def = _cpp_create_definition (pfile, funlike);
396 slot = _cpp_lookup_slot (pfile, sym, len, INSERT, &hash);
400 HASHNODE *hp = *slot;
402 /* Redefining a macro is ok if the definitions are the same. */
403 if (hp->type == T_MACRO)
404 ok = ! empty && ! _cpp_compare_defs (pfile, def, hp->value.defn);
405 else if (hp->type == T_EMPTY)
407 /* Redefining a constant is ok with -D. */
408 else if (hp->type == T_CONST || hp->type == T_STDC)
409 ok = ! pfile->done_initializing;
410 /* Otherwise it's not ok. */
413 /* Print the warning or error if it's not ok. */
416 if (hp->type == T_POISON)
417 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
419 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
420 if (hp->type == T_MACRO && pfile->done_initializing)
422 DEFINITION *d = hp->value.defn;
423 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
424 "this is the location of the previous definition");
427 if (hp->type != T_POISON)
429 /* Replace the old definition. */
430 if (hp->type == T_MACRO)
431 _cpp_free_definition (hp->value.defn);
440 hp->value.defn = def;
446 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
447 hp->value.defn = def;
451 if (CPP_OPTION (pfile, debug_output)
452 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
453 _cpp_dump_definition (pfile, sym, len, def);
454 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
455 pass_thru_directive (sym, len, pfile, T_DEFINE);
458 pfile->no_macro_expand--;
459 pfile->parsing_define_directive--;
460 CPP_OPTION (pfile, discard_comments)--;
464 /* Handle #include and #import. */
467 parse_include (pfile, name)
471 long old_written = CPP_WRITTEN (pfile);
472 enum cpp_ttype token;
475 pfile->parsing_include_directive++;
476 token = _cpp_get_directive_token (pfile);
477 pfile->parsing_include_directive--;
479 len = CPP_WRITTEN (pfile) - old_written;
481 if (token == CPP_STRING)
482 ; /* No special treatment required. */
484 else if (token == CPP_NAME)
486 /* Support '#include xyz' like VAX-C. It is taken as
487 '#include <xyz.h>' and generates a warning. */
488 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
491 /* Rewrite the token to <xyz.h>. */
492 CPP_RESERVE (pfile, 4);
494 memmove (pfile->token_buffer + old_written + 1,
495 pfile->token_buffer + old_written,
496 CPP_WRITTEN (pfile) - old_written);
497 pfile->token_buffer[old_written] = '<';
498 CPP_PUTS_Q (pfile, ".h>", 2);
503 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
504 CPP_SET_WRITTEN (pfile, old_written);
505 _cpp_skip_rest_of_line (pfile);
509 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
511 cpp_error (pfile, "junk at end of `#%s'", name);
512 _cpp_skip_rest_of_line (pfile);
515 CPP_SET_WRITTEN (pfile, old_written);
518 cpp_error (pfile, "empty file name in `#%s'", name);
530 len = parse_include (pfile, dtable[T_INCLUDE].name);
533 token = alloca (len + 1);
534 memcpy (token, CPP_PWRITTEN (pfile), len);
537 if (CPP_OPTION (pfile, dump_includes))
538 pass_thru_directive (token, len, pfile, T_INCLUDE);
540 _cpp_execute_include (pfile, token, len, 0, 0);
551 if (CPP_OPTION (pfile, warn_import)
552 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
554 pfile->import_warning = 1;
556 "#import is obsolete, use an #ifndef wrapper in the header file");
559 len = parse_include (pfile, dtable[T_IMPORT].name);
562 token = alloca (len + 1);
563 memcpy (token, CPP_PWRITTEN (pfile), len);
566 if (CPP_OPTION (pfile, dump_includes))
567 pass_thru_directive (token, len, pfile, T_IMPORT);
569 _cpp_execute_include (pfile, token, len, 1, 0);
574 do_include_next (pfile)
579 struct file_name_list *search_start = 0;
581 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
584 token = alloca (len + 1);
585 memcpy (token, CPP_PWRITTEN (pfile), len);
588 if (CPP_OPTION (pfile, dump_includes))
589 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
591 /* For #include_next, skip in the search path past the dir in which the
592 containing file was found. Treat files specified using an absolute path
593 as if there are no more directories to search. Treat the primary source
594 file like any other included source, but generate a warning. */
595 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
597 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
598 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
601 cpp_warning (pfile, "#include_next in primary source file");
603 _cpp_execute_include (pfile, token, len, 0, search_start);
607 /* Subroutine of do_line. Read next token from PFILE without adding it to
608 the output buffer. If it is a number between 1 and 4, store it in *NUM
609 and return 1; otherwise, return 0 and complain if we aren't at the end
613 read_line_number (pfile, num)
617 long save_written = CPP_WRITTEN (pfile);
619 enum cpp_ttype token = _cpp_get_directive_token (pfile);
620 p = pfile->token_buffer + save_written;
622 if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
623 && p[0] >= '1' && p[0] <= '4')
626 CPP_SET_WRITTEN (pfile, save_written);
631 if (token != CPP_VSPACE && token != CPP_EOF)
632 cpp_error (pfile, "invalid format `#line' command");
633 CPP_SET_WRITTEN (pfile, save_written);
638 /* Interpret #line command.
639 Note that the filename string (if any) is treated as if it were an
640 include filename. That means no escape handling. */
646 cpp_buffer *ip = CPP_BUFFER (pfile);
647 unsigned int new_lineno;
648 long old_written = CPP_WRITTEN (pfile);
649 enum cpp_ttype token;
652 token = _cpp_get_directive_token (pfile);
654 if (token != CPP_NUMBER)
656 cpp_error (pfile, "token after `#line' is not an integer");
657 goto bad_line_directive;
660 CPP_PUTC (pfile, '\0'); /* not terminated for us */
661 new_lineno = strtoul (pfile->token_buffer + old_written, &x, 10);
664 cpp_error (pfile, "token after `#line' is not an integer");
665 goto bad_line_directive;
667 CPP_SET_WRITTEN (pfile, old_written);
669 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
670 cpp_pedwarn (pfile, "line number out of range in `#line' command");
672 token = _cpp_get_directive_token (pfile);
674 if (token == CPP_STRING)
676 U_CHAR *fname = pfile->token_buffer + old_written + 1;
677 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
678 int action_number = 0;
680 if (read_line_number (pfile, &action_number))
682 if (CPP_PEDANTIC (pfile))
683 cpp_pedwarn (pfile, "garbage at end of `#line' command");
685 /* This is somewhat questionable: change the buffer stack
686 depth so that output_line_command thinks we've stacked
688 if (action_number == 1)
690 pfile->buffer_stack_depth++;
691 read_line_number (pfile, &action_number);
693 else if (action_number == 2)
695 pfile->buffer_stack_depth--;
696 read_line_number (pfile, &action_number);
698 if (action_number == 3)
700 ip->system_header_p = 1;
701 read_line_number (pfile, &action_number);
703 if (action_number == 4)
705 ip->system_header_p = 2;
706 read_line_number (pfile, &action_number);
712 if (strcmp (fname, ip->nominal_fname))
714 if (!strcmp (fname, ip->ihash->name))
715 ip->nominal_fname = ip->ihash->name;
717 ip->nominal_fname = _cpp_fake_ihash (pfile, fname);
720 else if (token != CPP_VSPACE && token != CPP_EOF)
722 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
723 goto bad_line_directive;
726 /* The Newline at the end of this line remains to be processed.
727 To put the next line at the specified line number,
728 we must store a line number now that is one less. */
729 ip->lineno = new_lineno - 1;
730 CPP_SET_WRITTEN (pfile, old_written);
734 _cpp_skip_rest_of_line (pfile);
735 CPP_SET_WRITTEN (pfile, old_written);
739 /* Remove the definition of a symbol from the symbol table.
740 According to the C standard, it is not an error to undef
741 something that has no definitions. */
749 long here = CPP_WRITTEN (pfile);
750 enum cpp_ttype token;
752 pfile->no_macro_expand++;
753 token = _cpp_get_directive_token (pfile);
754 pfile->no_macro_expand--;
756 if (token != CPP_NAME)
758 cpp_error (pfile, "token after #undef is not an identifier");
759 _cpp_skip_rest_of_line (pfile);
762 len = CPP_WRITTEN (pfile) - here;
764 token = _cpp_get_directive_token (pfile);
765 if (token != CPP_VSPACE)
767 cpp_pedwarn (pfile, "junk on line after #undef");
768 _cpp_skip_rest_of_line (pfile);
771 name = pfile->token_buffer + here;
772 CPP_SET_WRITTEN (pfile, here);
774 slot = _cpp_lookup_slot (pfile, name, len, NO_INSERT, 0);
777 HASHNODE *hp = *slot;
778 if (hp->type == T_POISON)
779 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
782 /* If we are generating additional info for debugging (with -g) we
783 need to pass through all effective #undef commands. */
784 if (CPP_OPTION (pfile, debug_output))
785 pass_thru_directive (hp->name, len, pfile, T_UNDEF);
787 if (hp->type != T_MACRO)
788 cpp_warning (pfile, "undefining `%s'", hp->name);
790 htab_clear_slot (pfile->hashtab, (void **)slot);
798 * Report an error detected by the program we are processing.
799 * Use the text of the line in the error message.
800 * (We use error because it prints the filename & line#.)
807 const U_CHAR *text, *limit;
809 _cpp_skip_hspace (pfile);
810 text = CPP_BUFFER (pfile)->cur;
811 _cpp_skip_rest_of_line (pfile);
812 limit = CPP_BUFFER (pfile)->cur;
814 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
819 * Report a warning detected by the program we are processing.
820 * Use the text of the line in the warning message, then continue.
827 const U_CHAR *text, *limit;
829 _cpp_skip_hspace (pfile);
830 text = CPP_BUFFER (pfile)->cur;
831 _cpp_skip_rest_of_line (pfile);
832 limit = CPP_BUFFER (pfile)->cur;
834 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
838 /* Report program identification. */
844 long old_written = CPP_WRITTEN (pfile);
846 CPP_PUTS (pfile, "#ident ", 7);
848 /* Next token should be a string constant. */
849 if (_cpp_get_directive_token (pfile) == CPP_STRING)
850 /* And then a newline. */
851 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
852 /* Good - ship it. */
855 cpp_error (pfile, "invalid #ident");
856 _cpp_skip_rest_of_line (pfile);
857 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
862 /* Pragmata handling. We handle some of these, and pass the rest on
863 to the front end. C99 defines three pragmas and says that no macro
864 expansion is to be performed on them; whether or not macro
865 expansion happens for other pragmas is implementation defined.
866 This implementation never macro-expands the text after #pragma.
868 We currently do not support the _Pragma operator. Support for that
869 has to be coordinated with the front end. Proposed implementation:
870 both #pragma blah blah and _Pragma("blah blah") become
871 __builtin_pragma(blah blah) and we teach the parser about that. */
873 /* Sub-handlers for the pragmas needing treatment here.
874 They return 1 if the token buffer is to be popped, 0 if not. */
875 static int do_pragma_once PARAMS ((cpp_reader *));
876 static int do_pragma_implementation PARAMS ((cpp_reader *));
877 static int do_pragma_poison PARAMS ((cpp_reader *));
878 static int do_pragma_default PARAMS ((cpp_reader *));
887 enum cpp_ttype token;
889 here = CPP_WRITTEN (pfile);
890 CPP_PUTS (pfile, "#pragma ", 8);
892 key = CPP_WRITTEN (pfile);
893 pfile->no_macro_expand++;
894 token = _cpp_get_directive_token (pfile);
895 if (token != CPP_NAME)
897 if (token == CPP_VSPACE)
903 buf = pfile->token_buffer + key;
904 CPP_PUTC (pfile, ' ');
906 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
908 pop = do_pragma_once (pfile);
909 else if (tokis ("implementation"))
910 pop = do_pragma_implementation (pfile);
911 else if (tokis ("poison"))
912 pop = do_pragma_poison (pfile);
914 pop = do_pragma_default (pfile);
917 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
921 CPP_SET_WRITTEN (pfile, here);
922 pfile->no_macro_expand--;
926 cpp_error (pfile, "malformed #pragma directive");
927 _cpp_skip_rest_of_line (pfile);
929 CPP_SET_WRITTEN (pfile, here);
930 pfile->no_macro_expand--;
935 do_pragma_default (pfile)
938 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
939 CPP_PUTC (pfile, ' ');
944 do_pragma_once (pfile)
947 cpp_buffer *ip = CPP_BUFFER (pfile);
949 /* Allow #pragma once in system headers, since that's not the user's
951 if (!ip->system_header_p)
952 cpp_warning (pfile, "`#pragma once' is obsolete");
954 if (CPP_PREV_BUFFER (ip) == NULL)
955 cpp_warning (pfile, "`#pragma once' outside include file");
957 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
963 do_pragma_implementation (pfile)
966 /* Be quiet about `#pragma implementation' for a file only if it hasn't
967 been included yet. */
968 enum cpp_ttype token;
969 long written = CPP_WRITTEN (pfile);
974 token = _cpp_get_directive_token (pfile);
975 if (token == CPP_VSPACE)
977 else if (token != CPP_STRING)
979 cpp_error (pfile, "malformed #pragma implementation");
983 /* Trim the leading and trailing quote marks from the string. */
984 name = pfile->token_buffer + written + 1;
985 len = CPP_PWRITTEN (pfile) - name;
986 copy = (U_CHAR *) alloca (len);
987 memcpy (copy, name, len - 1);
988 copy[len - 1] = '\0';
990 if (cpp_included (pfile, copy))
992 "`#pragma implementation' for `%s' appears after file is included",
998 do_pragma_poison (pfile)
1001 /* Poison these symbols so that all subsequent usage produces an
1007 enum cpp_ttype token;
1011 /* As a rule, don't include #pragma poison commands in output,
1012 unless the user asks for them. */
1013 writeit = (CPP_OPTION (pfile, debug_output)
1014 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1015 || CPP_OPTION (pfile, dump_macros) == dump_names);
1019 written = CPP_WRITTEN (pfile);
1020 token = _cpp_get_directive_token (pfile);
1021 if (token == CPP_VSPACE)
1023 if (token != CPP_NAME)
1025 cpp_error (pfile, "invalid #pragma poison directive");
1026 _cpp_skip_rest_of_line (pfile);
1030 p = pfile->token_buffer + written;
1031 len = CPP_PWRITTEN (pfile) - p;
1032 slot = _cpp_lookup_slot (pfile, p, len, INSERT, &hash);
1035 HASHNODE *hp = *slot;
1036 if (hp->type != T_POISON)
1038 cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
1039 if (hp->type == T_MACRO)
1040 _cpp_free_definition (hp->value.defn);
1042 hp->type = T_POISON;
1047 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1048 hp->value.cpval = 0;
1052 CPP_PUTC (pfile, ' ');
1057 /* Just ignore #sccs, on systems where we define it at all. */
1058 #ifdef SCCS_DIRECTIVE
1063 _cpp_skip_rest_of_line (pfile);
1068 /* We've found an `#if' directive. If the only thing before it in
1069 this file is white space, and if it is of the form
1070 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1071 for inclusion of this file. (See redundant_include_p in cppfiles.c
1072 for an explanation of controlling macros.) If so, return a
1073 malloced copy of SYMBOL. Otherwise, return NULL. */
1076 detect_if_not_defined (pfile)
1079 U_CHAR *control_macro = 0;
1080 enum cpp_ttype token;
1081 unsigned int base_offset;
1082 unsigned int token_offset;
1083 unsigned int need_rparen = 0;
1084 unsigned int token_len;
1086 if (pfile->only_seen_white != 2)
1089 /* Save state required for restore. */
1090 pfile->no_macro_expand++;
1091 CPP_SET_MARK (pfile);
1092 base_offset = CPP_WRITTEN (pfile);
1095 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1096 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1097 || CPP_PWRITTEN (pfile)[-1] != '!')
1100 /* ...then `defined', */
1101 token_offset = CPP_WRITTEN (pfile);
1102 token = _cpp_get_directive_token (pfile);
1103 if (token != CPP_NAME)
1105 if (strncmp (pfile->token_buffer + token_offset, "defined", 7))
1108 /* ...then an optional '(' and the name, */
1109 token_offset = CPP_WRITTEN (pfile);
1110 token = _cpp_get_directive_token (pfile);
1111 if (token == CPP_LPAREN)
1113 token_offset = CPP_WRITTEN (pfile);
1115 token = _cpp_get_directive_token (pfile);
1117 if (token != CPP_NAME)
1120 token_len = CPP_WRITTEN (pfile) - token_offset;
1122 /* ...then the ')', if necessary, */
1123 if (need_rparen && _cpp_get_directive_token (pfile) != CPP_RPAREN)
1126 /* ...and make sure there's nothing else on the line. */
1127 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1130 /* We have a legitimate controlling macro for this header. */
1131 control_macro = (U_CHAR *) xmalloc (token_len + 1);
1132 memcpy (control_macro, pfile->token_buffer + token_offset, token_len);
1133 control_macro[token_len] = '\0';
1136 CPP_SET_WRITTEN (pfile, base_offset);
1137 pfile->no_macro_expand--;
1138 CPP_GOTO_MARK (pfile);
1140 return control_macro;
1144 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1145 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1152 U_CHAR *control_macro = detect_if_not_defined (pfile);
1153 int value = _cpp_parse_expr (pfile);
1154 return conditional_skip (pfile, value == 0, T_IF, control_macro);
1158 * handle a #elif directive by not changing if_stack either.
1159 * see the comment above do_else.
1166 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1168 cpp_error (pfile, "`#elif' not within a conditional");
1173 if (pfile->if_stack->type == T_ELSE)
1175 cpp_error (pfile, "`#elif' after `#else'");
1176 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1177 "the conditional began here");
1179 pfile->if_stack->type = T_ELIF;
1182 if (pfile->if_stack->if_succeeded)
1184 _cpp_skip_rest_of_line (pfile);
1185 return skip_if_group (pfile);
1187 if (_cpp_parse_expr (pfile) == 0)
1188 return skip_if_group (pfile);
1190 ++pfile->if_stack->if_succeeded; /* continue processing input */
1194 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1195 not defined; the macro tested is left in the token buffer (but
1199 parse_ifdef (pfile, name)
1205 enum cpp_ttype token;
1206 long old_written = CPP_WRITTEN (pfile);
1209 pfile->no_macro_expand++;
1210 token = _cpp_get_directive_token (pfile);
1211 pfile->no_macro_expand--;
1213 ident = pfile->token_buffer + old_written;
1214 len = CPP_WRITTEN (pfile) - old_written;
1216 if (token == CPP_VSPACE)
1218 if (! CPP_TRADITIONAL (pfile))
1219 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1223 else if (token == CPP_NAME)
1225 defined = cpp_defined (pfile, ident, len);
1226 CPP_PUTC (pfile, '\0'); /* so it can be copied with xstrdup */
1231 if (! CPP_TRADITIONAL (pfile))
1232 cpp_error (pfile, "`#%s' with invalid argument", name);
1235 if (!CPP_TRADITIONAL (pfile))
1237 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1240 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1242 _cpp_skip_rest_of_line (pfile);
1245 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1249 /* #ifdef is dead simple. */
1255 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1256 return conditional_skip (pfile, skip, T_IFDEF, 0);
1259 /* #ifndef is a tad more complex, because we need to check for a
1260 no-reinclusion wrapper. */
1266 int start_of_file, skip;
1267 U_CHAR *control_macro = 0;
1269 start_of_file = pfile->only_seen_white == 2;
1270 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1272 if (start_of_file && !skip)
1273 control_macro = (U_CHAR *) xstrdup (CPP_PWRITTEN (pfile));
1275 return conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1278 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1279 If this is a #ifndef starting at the beginning of a file,
1280 CONTROL_MACRO is the macro name tested by the #ifndef.
1281 Otherwise, CONTROL_MACRO is 0. */
1284 conditional_skip (pfile, skip, type, control_macro)
1288 U_CHAR *control_macro;
1292 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1293 temp->lineno = CPP_BUFFER (pfile)->lineno;
1294 temp->next = pfile->if_stack;
1295 temp->control_macro = control_macro;
1296 pfile->if_stack = temp;
1298 pfile->if_stack->type = type;
1301 return skip_if_group (pfile);
1303 ++pfile->if_stack->if_succeeded;
1307 /* Subroutine of skip_if_group. Examine one preprocessing directive
1308 and return 0 if skipping should continue, or the directive number
1309 of the directive that ends the block if it should halt.
1311 Also adjusts the if_stack as appropriate. The `#' has been read,
1312 but not the identifier. */
1315 consider_directive_while_skipping (pfile, stack)
1324 /* -traditional directives are recognized only with the # in column 1. */
1325 hash_at_bol = CPP_IN_COLUMN_1 (pfile);
1327 ident = CPP_WRITTEN (pfile);
1328 if (_cpp_get_directive_token (pfile) != CPP_NAME)
1330 len = CPP_WRITTEN (pfile) - ident;
1332 for (i = 0; i < N_DIRECTIVES; i++)
1334 if (dtable[i].length == len
1335 && !strncmp (dtable[i].name, pfile->token_buffer + ident, len))
1336 goto real_directive;
1342 /* If it's not a directive of interest to us, return now. */
1343 if (ORIGIN (dtable[i].flags) != COND)
1346 /* First, deal with -traditional and -Wtraditional.
1347 All COND directives are from K+R. */
1351 if (CPP_TRADITIONAL (pfile))
1353 if (CPP_WTRADITIONAL (pfile))
1354 cpp_warning (pfile, "ignoring #%s because of its indented #",
1358 if (CPP_WTRADITIONAL (pfile))
1359 cpp_warning (pfile, "traditional C ignores %s with the # indented",
1366 cpp_ice (pfile, "non COND directive in switch in c_d_w_s");
1372 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1373 temp->lineno = CPP_BUFFER (pfile)->lineno;
1374 temp->next = pfile->if_stack;
1376 pfile->if_stack = temp;
1380 if (pfile->if_stack != stack)
1381 validate_else (pfile, dtable[i].name);
1384 if (pfile->if_stack == stack)
1387 pfile->if_stack->type = i;
1391 if (pfile->if_stack != stack)
1392 validate_else (pfile, dtable[i].name);
1394 if (pfile->if_stack == stack)
1397 temp = pfile->if_stack;
1398 pfile->if_stack = temp->next;
1404 /* Skip to #endif, #else, or #elif. Consumes the directive that
1405 causes it to stop, but not its argument. Returns the number of
1406 that directive, which must be passed back up to
1407 _cpp_handle_directive, which will execute it. */
1409 skip_if_group (pfile)
1412 enum cpp_ttype token;
1413 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1417 /* We are no longer at the start of the file. */
1418 pfile->only_seen_white = 0;
1420 old_written = CPP_WRITTEN (pfile);
1421 pfile->no_macro_expand++;
1424 /* We are at the end of a line. Only cpp_get_token knows how to
1425 advance the line number correctly. */
1426 token = cpp_get_token (pfile);
1427 if (token == CPP_POP)
1428 break; /* Caller will issue error. */
1429 else if (token != CPP_VSPACE)
1430 cpp_ice (pfile, "cpp_get_token returned %d in skip_if_group", token);
1431 CPP_SET_WRITTEN (pfile, old_written);
1433 token = _cpp_get_directive_token (pfile);
1435 if (token == CPP_DIRECTIVE)
1437 ret = consider_directive_while_skipping (pfile, save_if_stack);
1442 if (token != CPP_VSPACE)
1443 _cpp_skip_rest_of_line (pfile);
1445 CPP_SET_WRITTEN (pfile, old_written);
1446 pfile->no_macro_expand--;
1451 * handle a #else directive. Do this by just continuing processing
1452 * without changing if_stack ; this is so that the error message
1453 * for missing #endif's etc. will point to the original #if. It
1454 * is possible that something different would be better.
1461 validate_else (pfile, dtable[T_ELSE].name);
1462 _cpp_skip_rest_of_line (pfile);
1464 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1466 cpp_error (pfile, "`#else' not within a conditional");
1471 /* #ifndef can't have its special treatment for containing the whole file
1472 if it has a #else clause. */
1473 pfile->if_stack->control_macro = 0;
1475 if (pfile->if_stack->type == T_ELSE)
1477 cpp_error (pfile, "`#else' after `#else'");
1478 cpp_error_with_line (pfile, pfile->if_stack->lineno, 0,
1479 "the conditional began here");
1481 pfile->if_stack->type = T_ELSE;
1484 if (pfile->if_stack->if_succeeded)
1485 return skip_if_group (pfile);
1487 ++pfile->if_stack->if_succeeded; /* continue processing input */
1492 * unstack after #endif command
1499 validate_else (pfile, dtable[T_ENDIF].name);
1500 _cpp_skip_rest_of_line (pfile);
1502 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1503 cpp_error (pfile, "`#endif' not within a conditional");
1506 IF_STACK *temp = pfile->if_stack;
1507 pfile->if_stack = temp->next;
1508 if (temp->control_macro != 0)
1509 pfile->potential_control_macro = temp->control_macro;
1515 /* Issue -pedantic warning for text which is not a comment following
1516 an #else or #endif. Do not warn in system headers, as this is harmless
1517 and very common on old systems. */
1520 validate_else (pfile, directive)
1522 const char *directive;
1525 if (! CPP_PEDANTIC (pfile))
1528 old_written = CPP_WRITTEN (pfile);
1529 pfile->no_macro_expand++;
1530 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1532 "text following `#%s' violates ANSI standard", directive);
1533 CPP_SET_WRITTEN (pfile, old_written);
1534 pfile->no_macro_expand--;
1538 _cpp_handle_eof (pfile)
1541 struct if_stack *ifs, *nifs;
1543 /* Unwind the conditional stack and generate error messages. */
1544 for (ifs = pfile->if_stack;
1545 ifs != CPP_BUFFER (pfile)->if_stack;
1548 cpp_error_with_line (pfile, ifs->lineno, 0,
1549 "unterminated `#%s' conditional",
1550 dtable[ifs->type].name);
1555 pfile->if_stack = ifs;
1556 CPP_BUFFER (pfile)->seen_eof = 1;
1566 HASHNODE *base, *this;
1567 HASHNODE **bslot, **tslot;
1569 unsigned long bhash, thash;
1571 old_written = CPP_WRITTEN (pfile); /* remember where it starts */
1572 ret = _cpp_parse_assertion (pfile);
1577 cpp_error (pfile, "missing token-sequence in #assert");
1580 tlen = CPP_WRITTEN (pfile) - old_written;
1582 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1584 cpp_error (pfile, "junk at end of #assert");
1588 sym = pfile->token_buffer + old_written;
1589 blen = (U_CHAR *) strchr (sym, '(') - sym;
1590 tslot = _cpp_lookup_slot (pfile, sym, tlen, INSERT, &thash);
1593 cpp_warning (pfile, "%s re-asserted", sym);
1597 bslot = _cpp_lookup_slot (pfile, sym, blen, INSERT, &bhash);
1600 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1601 base->value.aschain = 0;
1606 if (base->type != T_ASSERT)
1608 /* Token clash - but with what?! */
1609 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1613 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1614 this->value.aschain = base->value.aschain;
1615 base->value.aschain = this;
1618 _cpp_skip_rest_of_line (pfile);
1619 CPP_SET_WRITTEN (pfile, old_written);
1630 long baselen, thislen;
1631 HASHNODE *base, *this, *next;
1633 old_written = CPP_WRITTEN (pfile);
1634 ret = _cpp_parse_assertion (pfile);
1637 thislen = CPP_WRITTEN (pfile) - old_written;
1639 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1641 cpp_error (pfile, "junk at end of #unassert");
1644 sym = pfile->token_buffer + old_written;
1645 CPP_SET_WRITTEN (pfile, old_written);
1649 base = _cpp_lookup (pfile, sym, thislen);
1651 goto error; /* It isn't an error to #undef what isn't #defined,
1652 so it isn't an error to #unassert what isn't
1653 #asserted either. */
1655 for (this = base->value.aschain; this; this = next)
1657 next = this->value.aschain;
1658 htab_remove_elt (pfile->hashtab, this);
1660 htab_remove_elt (pfile->hashtab, base);
1664 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1665 base = _cpp_lookup (pfile, sym, baselen);
1666 if (! base) goto error;
1667 this = _cpp_lookup (pfile, sym, thislen);
1668 if (! this) goto error;
1671 while (next->value.aschain != this)
1672 next = next->value.aschain;
1674 next->value.aschain = this->value.aschain;
1675 htab_remove_elt (pfile->hashtab, this);
1677 if (base->value.aschain == NULL)
1678 /* Last answer for this predicate deleted. */
1679 htab_remove_elt (pfile->hashtab, base);
1684 _cpp_skip_rest_of_line (pfile);
1685 CPP_SET_WRITTEN (pfile, old_written);
1689 /* These are for -D, -U, -A. */
1691 /* Process the string STR as if it appeared as the body of a #define.
1692 If STR is just an identifier, define it with value 1.
1693 If STR has anything after the identifier, then it should
1694 be identifier=definition. */
1697 cpp_define (pfile, str)
1704 p = strchr (str, '=');
1705 /* Copy the entire option so we can modify it.
1706 Change the first "=" in the string to a space. If there is none,
1707 tack " 1" on the end. Then add a newline and a NUL. */
1711 count = strlen (str) + 2;
1712 buf = (char *) alloca (count);
1713 memcpy (buf, str, count - 2);
1715 buf[count - 2] = '\n';
1716 buf[count - 1] = '\0';
1720 count = strlen (str) + 4;
1721 buf = (char *) alloca (count);
1722 memcpy (buf, str, count - 4);
1723 strcpy (&buf[count-4], " 1\n");
1726 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1729 cpp_pop_buffer (pfile);
1733 /* Process MACRO as if it appeared as the body of an #undef. */
1735 cpp_undef (pfile, macro)
1739 /* Copy the string so we can append a newline. */
1740 size_t len = strlen (macro);
1741 char *buf = (char *) alloca (len + 2);
1742 memcpy (buf, macro, len);
1744 buf[len + 1] = '\0';
1745 if (cpp_push_buffer (pfile, buf, len + 1))
1748 cpp_pop_buffer (pfile);
1752 /* Process the string STR as if it appeared as the body of a #assert. */
1754 cpp_assert (pfile, str)
1758 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1761 cpp_pop_buffer (pfile);
1765 /* Process STR as if it appeared as the body of an #unassert. */
1767 cpp_unassert (pfile, str)
1771 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1773 do_unassert (pfile);
1774 cpp_pop_buffer (pfile);
1778 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1780 cpp_defined (pfile, id, len)
1785 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1786 if (hp && hp->type == T_POISON)
1788 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1791 return (hp != NULL);