2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \
32 ? CPP_BUFFER (pfile)->cur[N] : EOF)
33 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
34 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
35 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
37 /* `struct directive' defines one #-directive, including how to handle it. */
41 int (*func) /* Function to handle directive */
42 PARAMS ((cpp_reader *));
43 const char *name; /* Name of directive */
44 unsigned short length; /* Length of name */
45 unsigned short origin; /* Origin of this directive */
48 /* Stack of conditionals currently in progress
49 (including both successful and failing conditionals). */
53 struct if_stack *next;
54 int lineno; /* line number where condition started */
55 int if_succeeded; /* truth of last condition in this group */
56 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
57 int type; /* type of last directive seen in this group */
59 typedef struct if_stack IF_STACK;
61 /* Forward declarations. */
63 static void validate_else PARAMS ((cpp_reader *, const char *));
64 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
65 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
66 static void conditional_skip PARAMS ((cpp_reader *, int, int,
68 static void skip_if_group PARAMS ((cpp_reader *));
69 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
71 static int read_line_number PARAMS ((cpp_reader *, int *));
72 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
73 static int consider_directive_while_skipping
74 PARAMS ((cpp_reader *, IF_STACK *));
75 static int get_macro_name PARAMS ((cpp_reader *));
77 /* Values for the "origin" field of the table below. */
78 enum { KANDR = 0, STDC89, EXTENSION };
80 /* This is the table of directive handlers. It is ordered by
81 frequency of occurrence; the numbers at the end are directive
82 counts from all the source code I have lying around (egcs and libc
83 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
86 The entries with a dash and a name after the count are extensions,
87 of which all but #warning and #include_next are deprecated. The name
88 is where the extension appears to have come from. */
90 /* #sccs is not always recognized. */
92 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION) /* 0 - SVR2? */
94 # define SCCS_ENTRY /* nothing */
97 #define DIRECTIVE_TABLE \
98 D(define, T_DEFINE, KANDR) /* 270554 */ \
99 D(include, T_INCLUDE, KANDR) /* 52262 */ \
100 D(endif, T_ENDIF, KANDR) /* 45855 */ \
101 D(ifdef, T_IFDEF, KANDR) /* 22000 */ \
102 D(if, T_IF, KANDR) /* 18162 */ \
103 D(else, T_ELSE, KANDR) /* 9863 */ \
104 D(ifndef, T_IFNDEF, KANDR) /* 9675 */ \
105 D(undef, T_UNDEF, KANDR) /* 4837 */ \
106 D(line, T_LINE, KANDR) /* 2465 */ \
107 D(elif, T_ELIF, KANDR) /* 610 */ \
108 D(error, T_ERROR, STDC89) /* 475 */ \
109 D(pragma, T_PRAGMA, STDC89) /* 195 */ \
110 D(warning, T_WARNING, EXTENSION) /* 22 - GNU */ \
111 D(include_next, T_INCLUDE_NEXT, EXTENSION) /* 19 - GNU */ \
112 D(ident, T_IDENT, EXTENSION) /* 11 - SVR4 */ \
113 D(import, T_IMPORT, EXTENSION) /* 0 - ObjC */ \
114 D(assert, T_ASSERT, EXTENSION) /* 0 - SVR4 */ \
115 D(unassert, T_UNASSERT, EXTENSION) /* 0 - SVR4 */ \
118 /* Use the table to generate a series of prototypes, an enum for the
119 directive names, and an array of directive handlers. */
121 /* The directive-processing functions are declared to return int
122 instead of void, because some old compilers have trouble with
123 pointers to functions returning void. */
125 #define D(name, t, o) static int CONCAT2(do_, name) PARAMS ((cpp_reader *));
129 #define D(n, tag, o) tag,
137 #define D(name, t, origin) \
138 { CONCAT2(do_, name), STRINGX(name), sizeof STRINGX(name) - 1, origin },
139 static const struct directive dtable[] =
144 #undef DIRECTIVE_TABLE
146 /* Handle a possible # directive.
147 '#' has already been read. */
150 _cpp_handle_directive (pfile)
157 long old_written = CPP_WRITTEN (pfile);
159 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
161 cpp_ice (pfile, "handle_directive called on macro buffer");
165 /* -traditional directives are recognized only with the # in column 1.
166 XXX Layering violation. */
167 hash_at_bol = (CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base == 1);
169 _cpp_skip_hspace (pfile);
172 /* # followed by a number is equivalent to #line. Do not recognize
173 this form in assembly language source files. Complain about this
174 form if we're being pedantic, but not if this is regurgitated
175 input (preprocessed or fed back in by the C++ frontend). */
176 if (c >= '0' && c <= '9')
178 if (CPP_OPTION (pfile, lang_asm))
181 if (CPP_PEDANTIC (pfile)
182 && ! CPP_OPTION (pfile, preprocessed)
183 && ! CPP_BUFFER (pfile)->manual_pop)
184 cpp_pedwarn (pfile, "# followed by integer");
189 /* If we are rescanning preprocessed input, don't obey any directives
191 if (CPP_OPTION (pfile, preprocessed))
194 /* Now find the directive name. */
195 CPP_PUTC (pfile, '#');
196 _cpp_parse_name (pfile, GETC());
197 ident = pfile->token_buffer + old_written + 1;
198 len = CPP_PWRITTEN (pfile) - ident;
201 /* A line of just # becomes blank. A line with something
202 other than an identifier after the # is reparsed as a non-
204 CPP_SET_WRITTEN (pfile, old_written);
205 return (PEEKC() == '\n');
208 /* Decode the keyword and call the appropriate expansion routine. */
209 for (i = 0; i < N_DIRECTIVES; i++)
211 if (dtable[i].length == len
212 && !strncmp (dtable[i].name, ident, len))
215 if (i == N_DIRECTIVES)
216 /* # identifier, but not a legit directive. Pass onward as a
217 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
220 CPP_SET_WRITTEN (pfile, old_written);
222 if (pfile->no_directives)
224 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
226 _cpp_skip_rest_of_line (pfile);
230 if (CPP_PEDANTIC (pfile) && dtable[i].origin == EXTENSION)
231 cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
232 if (CPP_WTRADITIONAL (pfile))
234 if (!hash_at_bol && dtable[i].origin == KANDR)
235 cpp_warning (pfile, "traditional C ignores #%s with the # indented",
237 else if (hash_at_bol && dtable[i].origin != KANDR)
239 "traditional C rejects #%s unless the # is indented",
243 if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
246 (*dtable[i].func) (pfile);
250 /* Pass a directive through to the output file.
251 BUF points to the contents of the directive, as a contiguous string.
252 LEN is the length of the string pointed to by BUF.
253 KEYWORD is the keyword-table entry for the directive. */
256 pass_thru_directive (buf, len, pfile, keyword)
262 const struct directive *kt = &dtable[keyword];
263 register unsigned klen = kt->length;
265 CPP_RESERVE (pfile, 1 + klen + len);
266 CPP_PUTC_Q (pfile, '#');
267 CPP_PUTS_Q (pfile, kt->name, klen);
268 if (len != 0 && buf[0] != ' ')
269 CPP_PUTC_Q (pfile, ' ');
270 CPP_PUTS_Q (pfile, buf, len);
273 /* Subroutine of do_define: determine the name of the macro to be
277 get_macro_name (pfile)
282 here = CPP_WRITTEN (pfile);
283 pfile->no_macro_expand++;
284 if (_cpp_get_directive_token (pfile) != CPP_NAME)
286 cpp_error (pfile, "`#define' must be followed by an identifier");
290 len = CPP_WRITTEN (pfile) - here;
291 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
293 cpp_error (pfile, "`defined' is not a legal macro name");
297 pfile->no_macro_expand--;
301 _cpp_skip_rest_of_line (pfile);
302 pfile->no_macro_expand--;
306 /* Process a #define command. */
320 here = CPP_WRITTEN (pfile);
321 len = get_macro_name (pfile);
325 /* Copy out the name so we can pop the token buffer. */
326 len = CPP_WRITTEN (pfile) - here;
327 sym = (U_CHAR *) alloca (len + 1);
328 memcpy (sym, pfile->token_buffer + here, len);
330 CPP_SET_WRITTEN (pfile, here);
332 /* If the next character, with no intervening whitespace, is '(',
333 then this is a function-like macro. */
337 else if (c != '\n' && !is_hspace (c))
338 /* Otherwise, C99 requires white space after the name. We treat it
339 as an object-like macro if this happens, with a warning. */
340 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
342 def = _cpp_create_definition (pfile, funlike);
346 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
350 HASHNODE *hp = *slot;
352 /* Redefining a macro is ok if the definitions are the same. */
353 if (hp->type == T_MACRO)
354 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
355 /* Redefining a constant is ok with -D. */
356 else if (hp->type == T_CONST || hp->type == T_STDC)
357 ok = ! pfile->done_initializing;
358 /* Otherwise it's not ok. */
361 /* Print the warning or error if it's not ok. */
364 if (hp->type == T_POISON)
365 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
367 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
368 if (hp->type == T_MACRO && pfile->done_initializing)
370 DEFINITION *d = hp->value.defn;
371 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
372 "this is the location of the previous definition");
375 if (hp->type != T_POISON)
377 /* Replace the old definition. */
378 if (hp->type == T_MACRO)
379 _cpp_free_definition (hp->value.defn);
381 hp->value.defn = def;
386 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
387 hp->value.defn = def;
391 if (CPP_OPTION (pfile, debug_output)
392 || CPP_OPTION (pfile, dump_macros) == dump_definitions)
393 _cpp_dump_definition (pfile, sym, len, def);
394 else if (CPP_OPTION (pfile, dump_macros) == dump_names)
395 pass_thru_directive (sym, len, pfile, T_DEFINE);
401 * write out a #line command, for instance, after an #include file.
402 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
406 _cpp_output_line_command (pfile, file_change)
408 enum file_change_code file_change;
413 if (CPP_OPTION (pfile, no_line_commands)
414 || CPP_OPTION (pfile, no_output))
417 ip = cpp_file_buffer (pfile);
418 cpp_buf_line_and_col (ip, &line, NULL);
420 /* If the current file has not changed, we omit the #line if it would
421 appear to be a no-op, and we output a few newlines instead
422 if we want to increase the line number by a small amount.
423 We cannot do this if pfile->lineno is zero, because that means we
424 haven't output any line commands yet. (The very first line command
425 output is a `same_file' command.) */
426 if (file_change == same_file && pfile->lineno != 0)
428 if (line == pfile->lineno)
431 /* If the inherited line number is a little too small,
432 output some newlines instead of a #line command. */
433 if (line > pfile->lineno && line < pfile->lineno + 8)
435 CPP_RESERVE (pfile, 20);
436 while (line > pfile->lineno)
438 CPP_PUTC_Q (pfile, '\n');
445 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
446 CPP_PUTS_Q (pfile, "# ", 2);
448 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
449 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
451 _cpp_quote_string (pfile, ip->nominal_fname);
452 if (file_change != same_file && file_change != rename_file)
454 CPP_PUTC_Q (pfile, ' ');
455 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
457 /* Tell cc1 if following text comes from a system header file. */
458 if (ip->system_header_p)
460 CPP_PUTC_Q (pfile, ' ');
461 CPP_PUTC_Q (pfile, '3');
463 #ifndef NO_IMPLICIT_EXTERN_C
464 /* Tell cc1plus if following text should be treated as C. */
465 if (ip->system_header_p == 2 && CPP_OPTION (pfile, cplusplus))
467 CPP_PUTC_Q (pfile, ' ');
468 CPP_PUTC_Q (pfile, '4');
471 CPP_PUTC_Q (pfile, '\n');
472 pfile->lineno = line;
475 /* Handle #include and #import. */
478 parse_include (pfile, name)
482 long old_written = CPP_WRITTEN (pfile);
483 enum cpp_token token;
486 pfile->parsing_include_directive++;
487 token = _cpp_get_directive_token (pfile);
488 pfile->parsing_include_directive--;
490 len = CPP_WRITTEN (pfile) - old_written;
492 if (token == CPP_STRING)
493 ; /* No special treatment required. */
495 else if (token == CPP_NAME)
497 /* Support '#include xyz' like VAX-C. It is taken as
498 '#include <xyz.h>' and generates a warning. */
499 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
502 /* Rewrite the token to <xyz.h>. */
503 CPP_RESERVE (pfile, 4);
505 memmove (pfile->token_buffer + old_written + 1,
506 pfile->token_buffer + old_written,
507 CPP_WRITTEN (pfile) - old_written);
508 pfile->token_buffer[old_written] = '<';
509 CPP_PUTS_Q (pfile, ".h>", 2);
514 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
515 CPP_SET_WRITTEN (pfile, old_written);
516 _cpp_skip_rest_of_line (pfile);
520 CPP_NUL_TERMINATE (pfile);
521 CPP_ADJUST_WRITTEN (pfile, 1);
523 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
525 cpp_error (pfile, "junk at end of `#%s'", name);
526 _cpp_skip_rest_of_line (pfile);
529 CPP_SET_WRITTEN (pfile, old_written);
532 cpp_error (pfile, "empty file name in `#%s'", name);
544 len = parse_include (pfile, dtable[T_INCLUDE].name);
547 token = alloca (len + 1);
548 strcpy (token, CPP_PWRITTEN (pfile));
550 if (CPP_OPTION (pfile, dump_includes))
551 pass_thru_directive (token, len, pfile, T_INCLUDE);
553 _cpp_execute_include (pfile, token, len, 0, 0);
564 if (CPP_OPTION (pfile, warn_import)
565 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
567 pfile->import_warning = 1;
569 "#import is obsolete, use an #ifndef wrapper in the header file");
572 len = parse_include (pfile, dtable[T_IMPORT].name);
575 token = alloca (len + 1);
576 strcpy (token, CPP_PWRITTEN (pfile));
578 if (CPP_OPTION (pfile, dump_includes))
579 pass_thru_directive (token, len, pfile, T_IMPORT);
581 _cpp_execute_include (pfile, token, len, 1, 0);
586 do_include_next (pfile)
591 struct file_name_list *search_start = 0;
593 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
596 token = alloca (len + 1);
597 strcpy (token, CPP_PWRITTEN (pfile));
599 if (CPP_OPTION (pfile, dump_includes))
600 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
602 /* For #include_next, skip in the search path past the dir in which the
603 containing file was found. Treat files specified using an absolute path
604 as if there are no more directories to search. Treat the primary source
605 file like any other included source, but generate a warning. */
606 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
608 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
609 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
612 cpp_warning (pfile, "#include_next in primary source file");
614 _cpp_execute_include (pfile, token, len, 0, search_start);
618 /* Subroutine of do_line. Read next token from PFILE without adding it to
619 the output buffer. If it is a number between 1 and 4, store it in *NUM
620 and return 1; otherwise, return 0 and complain if we aren't at the end
624 read_line_number (pfile, num)
628 long save_written = CPP_WRITTEN (pfile);
630 enum cpp_token token = _cpp_get_directive_token (pfile);
631 CPP_SET_WRITTEN (pfile, save_written);
632 p = pfile->token_buffer + save_written;
634 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
641 if (token != CPP_VSPACE && token != CPP_EOF)
642 cpp_error (pfile, "invalid format `#line' command");
647 /* Interpret #line command.
648 Note that the filename string (if any) is treated as if it were an
649 include filename. That means no escape handling. */
655 cpp_buffer *ip = CPP_BUFFER (pfile);
657 long old_written = CPP_WRITTEN (pfile);
658 enum file_change_code file_change = same_file;
659 enum cpp_token token;
662 token = _cpp_get_directive_token (pfile);
664 if (token != CPP_NUMBER)
666 cpp_error (pfile, "token after `#line' is not an integer");
667 goto bad_line_directive;
670 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
673 cpp_error (pfile, "token after `#line' is not an integer");
674 goto bad_line_directive;
676 CPP_SET_WRITTEN (pfile, old_written);
678 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
679 cpp_pedwarn (pfile, "line number out of range in `#line' command");
681 token = _cpp_get_directive_token (pfile);
683 if (token == CPP_STRING)
685 U_CHAR *fname = pfile->token_buffer + old_written + 1;
686 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
687 int action_number = 0;
689 file_change = rename_file;
691 if (read_line_number (pfile, &action_number))
693 if (CPP_PEDANTIC (pfile))
694 cpp_pedwarn (pfile, "garbage at end of `#line' command");
696 if (action_number == 1)
698 file_change = enter_file;
699 read_line_number (pfile, &action_number);
701 else if (action_number == 2)
703 file_change = leave_file;
704 read_line_number (pfile, &action_number);
706 if (action_number == 3)
708 ip->system_header_p = 1;
709 read_line_number (pfile, &action_number);
711 if (action_number == 4)
713 ip->system_header_p = 2;
714 read_line_number (pfile, &action_number);
720 if (strcmp (fname, ip->nominal_fname))
722 const char *newname, *oldname;
723 if (!strcmp (fname, ip->ihash->name))
724 newname = ip->ihash->name;
725 else if (ip->last_nominal_fname
726 && !strcmp (fname, ip->last_nominal_fname))
727 newname = ip->last_nominal_fname;
729 newname = xstrdup (fname);
731 oldname = ip->nominal_fname;
732 ip->nominal_fname = newname;
734 if (ip->last_nominal_fname
735 && ip->last_nominal_fname != oldname
736 && ip->last_nominal_fname != newname
737 && ip->last_nominal_fname != ip->ihash->name)
738 free ((void *) ip->last_nominal_fname);
740 if (newname == ip->ihash->name)
741 ip->last_nominal_fname = NULL;
743 ip->last_nominal_fname = oldname;
746 else if (token != CPP_VSPACE && token != CPP_EOF)
748 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
749 goto bad_line_directive;
752 /* The Newline at the end of this line remains to be processed.
753 To put the next line at the specified line number,
754 we must store a line number now that is one less. */
755 ip->lineno = new_lineno - 1;
756 CPP_SET_WRITTEN (pfile, old_written);
757 _cpp_output_line_command (pfile, file_change);
761 _cpp_skip_rest_of_line (pfile);
762 CPP_SET_WRITTEN (pfile, old_written);
766 /* Remove the definition of a symbol from the symbol table.
767 According to the C standard, it is not an error to undef
768 something that has no definitions. */
775 U_CHAR *buf, *name, *limit;
777 long here = CPP_WRITTEN (pfile);
778 enum cpp_token token;
780 _cpp_skip_hspace (pfile);
784 cpp_error (pfile, "token after #undef is not an identifier");
785 _cpp_skip_rest_of_line (pfile);
789 _cpp_parse_name (pfile, c);
790 buf = pfile->token_buffer + here;
791 limit = CPP_PWRITTEN(pfile);
793 /* Copy out the token so we can pop the token buffer. */
795 name = (U_CHAR *) alloca (len + 1);
796 memcpy (name, buf, len);
799 token = _cpp_get_directive_token (pfile);
800 if (token != CPP_VSPACE)
802 cpp_pedwarn (pfile, "junk on line after #undef");
803 _cpp_skip_rest_of_line (pfile);
805 CPP_SET_WRITTEN (pfile, here);
807 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
810 HASHNODE *hp = *slot;
811 /* If we are generating additional info for debugging (with -g) we
812 need to pass through all effective #undef commands. */
813 if (CPP_OPTION (pfile, debug_output))
814 pass_thru_directive (name, len, pfile, T_UNDEF);
815 if (hp->type == T_POISON)
816 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
819 if (hp->type != T_MACRO)
820 cpp_warning (pfile, "undefining `%s'", hp->name);
822 htab_clear_slot (pfile->hashtab, (void **)slot);
830 * Report an error detected by the program we are processing.
831 * Use the text of the line in the error message.
832 * (We use error because it prints the filename & line#.)
839 const U_CHAR *text, *limit;
841 _cpp_skip_hspace (pfile);
842 text = CPP_BUFFER (pfile)->cur;
843 _cpp_skip_rest_of_line (pfile);
844 limit = CPP_BUFFER (pfile)->cur;
846 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
851 * Report a warning detected by the program we are processing.
852 * Use the text of the line in the warning message, then continue.
859 const U_CHAR *text, *limit;
861 _cpp_skip_hspace (pfile);
862 text = CPP_BUFFER (pfile)->cur;
863 _cpp_skip_rest_of_line (pfile);
864 limit = CPP_BUFFER (pfile)->cur;
866 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
870 /* Report program identification. */
876 long old_written = CPP_WRITTEN (pfile);
878 CPP_PUTS (pfile, "#ident ", 7);
880 /* Next token should be a string constant. */
881 if (_cpp_get_directive_token (pfile) == CPP_STRING)
882 /* And then a newline. */
883 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
884 /* Good - ship it. */
887 cpp_error (pfile, "invalid #ident");
888 _cpp_skip_rest_of_line (pfile);
889 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
894 /* Pragmata handling. We handle some of these, and pass the rest on
895 to the front end. C99 defines three pragmas and says that no macro
896 expansion is to be performed on them; whether or not macro
897 expansion happens for other pragmas is implementation defined.
898 This implementation never macro-expands the text after #pragma.
900 We currently do not support the _Pragma operator. Support for that
901 has to be coordinated with the front end. Proposed implementation:
902 both #pragma blah blah and _Pragma("blah blah") become
903 __builtin_pragma(blah blah) and we teach the parser about that. */
905 /* Sub-handlers for the pragmas needing treatment here.
906 They return 1 if the token buffer is to be popped, 0 if not. */
907 static int do_pragma_once PARAMS ((cpp_reader *));
908 static int do_pragma_implementation PARAMS ((cpp_reader *));
909 static int do_pragma_poison PARAMS ((cpp_reader *));
910 static int do_pragma_default PARAMS ((cpp_reader *));
919 enum cpp_token token;
921 here = CPP_WRITTEN (pfile);
922 CPP_PUTS (pfile, "#pragma ", 8);
924 key = CPP_WRITTEN (pfile);
925 pfile->no_macro_expand++;
926 token = _cpp_get_directive_token (pfile);
927 if (token != CPP_NAME)
929 if (token == CPP_VSPACE)
935 buf = pfile->token_buffer + key;
936 CPP_PUTC (pfile, ' ');
938 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
940 pop = do_pragma_once (pfile);
941 else if (tokis ("implementation"))
942 pop = do_pragma_implementation (pfile);
943 else if (tokis ("poison"))
944 pop = do_pragma_poison (pfile);
946 pop = do_pragma_default (pfile);
949 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
953 CPP_SET_WRITTEN (pfile, here);
954 pfile->no_macro_expand--;
958 cpp_error (pfile, "malformed #pragma directive");
959 _cpp_skip_rest_of_line (pfile);
961 CPP_SET_WRITTEN (pfile, here);
962 pfile->no_macro_expand--;
967 do_pragma_default (pfile)
970 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
971 CPP_PUTC (pfile, ' ');
976 do_pragma_once (pfile)
979 cpp_buffer *ip = CPP_BUFFER (pfile);
981 /* Allow #pragma once in system headers, since that's not the user's
983 if (!ip->system_header_p)
984 cpp_warning (pfile, "`#pragma once' is obsolete");
986 if (CPP_PREV_BUFFER (ip) == NULL)
987 cpp_warning (pfile, "`#pragma once' outside include file");
989 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
995 do_pragma_implementation (pfile)
998 /* Be quiet about `#pragma implementation' for a file only if it hasn't
999 been included yet. */
1000 enum cpp_token token;
1001 long written = CPP_WRITTEN (pfile);
1006 token = _cpp_get_directive_token (pfile);
1007 if (token == CPP_VSPACE)
1009 else if (token != CPP_STRING)
1011 cpp_error (pfile, "malformed #pragma implementation");
1015 /* Trim the leading and trailing quote marks from the string. */
1016 name = pfile->token_buffer + written + 1;
1017 len = CPP_PWRITTEN (pfile) - name;
1018 copy = (U_CHAR *) alloca (len);
1019 memcpy (copy, name, len - 1);
1020 copy[len - 1] = '\0';
1022 if (cpp_included (pfile, copy))
1024 "`#pragma implementation' for `%s' appears after file is included",
1030 do_pragma_poison (pfile)
1033 /* Poison these symbols so that all subsequent usage produces an
1039 enum cpp_token token;
1043 /* As a rule, don't include #pragma poison commands in output,
1044 unless the user asks for them. */
1045 writeit = (CPP_OPTION (pfile, debug_output)
1046 || CPP_OPTION (pfile, dump_macros) == dump_definitions
1047 || CPP_OPTION (pfile, dump_macros) == dump_names);
1051 written = CPP_WRITTEN (pfile);
1052 token = _cpp_get_directive_token (pfile);
1053 if (token == CPP_VSPACE)
1055 if (token != CPP_NAME)
1057 cpp_error (pfile, "invalid #pragma poison directive");
1058 _cpp_skip_rest_of_line (pfile);
1062 p = pfile->token_buffer + written;
1064 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1067 HASHNODE *hp = *slot;
1068 if (hp->type != T_POISON)
1070 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1071 if (hp->type == T_MACRO)
1072 _cpp_free_definition (hp->value.defn);
1074 hp->type = T_POISON;
1079 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1080 hp->value.cpval = 0;
1084 CPP_PUTC (pfile, ' ');
1089 /* Just ignore #sccs, on systems where we define it at all. */
1090 #ifdef SCCS_DIRECTIVE
1095 _cpp_skip_rest_of_line (pfile);
1100 /* We've found an `#if' directive. If the only thing before it in
1101 this file is white space, and if it is of the form
1102 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1103 for inclusion of this file. (See redundant_include_p in cppfiles.c
1104 for an explanation of controlling macros.) If so, return a
1105 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1108 detect_if_not_defined (pfile)
1111 U_CHAR *control_macro = 0;
1113 if (pfile->only_seen_white == 2)
1116 enum cpp_token token;
1119 int need_rparen = 0;
1121 /* Save state required for restore. */
1122 pfile->no_macro_expand++;
1123 CPP_SET_MARK (pfile);
1124 base_offset = CPP_WRITTEN (pfile);
1127 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1128 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1129 || CPP_PWRITTEN (pfile)[-1] != '!')
1132 /* ...then `defined', */
1133 token_offset = CPP_WRITTEN (pfile);
1134 token = _cpp_get_directive_token (pfile);
1135 if (token != CPP_NAME)
1137 ident = pfile->token_buffer + token_offset;
1138 CPP_NUL_TERMINATE (pfile);
1139 if (strcmp (ident, "defined"))
1142 /* ...then an optional '(' and the name, */
1143 token_offset = CPP_WRITTEN (pfile);
1144 token = _cpp_get_directive_token (pfile);
1145 if (token == CPP_LPAREN)
1147 token_offset = CPP_WRITTEN (pfile);
1148 token = _cpp_get_directive_token (pfile);
1149 if (token != CPP_NAME)
1153 else if (token != CPP_NAME)
1156 ident = pfile->token_buffer + token_offset;
1157 CPP_NUL_TERMINATE (pfile);
1159 /* ...then the ')', if necessary, */
1160 if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
1161 /* ...and make sure there's nothing else on the line. */
1162 && _cpp_get_directive_token (pfile) == CPP_VSPACE)
1163 control_macro = (U_CHAR *) xstrdup (ident);
1166 CPP_SET_WRITTEN (pfile, base_offset);
1167 pfile->no_macro_expand--;
1168 CPP_GOTO_MARK (pfile);
1171 return control_macro;
1175 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1176 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1183 U_CHAR *control_macro = detect_if_not_defined (pfile);
1184 int value = _cpp_parse_expr (pfile);
1185 conditional_skip (pfile, value == 0, T_IF, control_macro);
1190 * handle a #elif directive by not changing if_stack either.
1191 * see the comment above do_else.
1198 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1200 cpp_error (pfile, "`#elif' not within a conditional");
1205 if (pfile->if_stack->type == T_ELSE)
1207 cpp_error (pfile, "`#elif' after `#else'");
1208 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1209 "the conditional began here");
1211 pfile->if_stack->type = T_ELIF;
1214 if (pfile->if_stack->if_succeeded)
1215 skip_if_group (pfile);
1218 if (_cpp_parse_expr (pfile) == 0)
1219 skip_if_group (pfile);
1222 ++pfile->if_stack->if_succeeded; /* continue processing input */
1223 _cpp_output_line_command (pfile, same_file);
1229 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1230 not defined; the macro tested is left in the token buffer (but
1234 parse_ifdef (pfile, name)
1240 enum cpp_token token;
1241 long old_written = CPP_WRITTEN (pfile);
1244 pfile->no_macro_expand++;
1245 token = _cpp_get_directive_token (pfile);
1246 pfile->no_macro_expand--;
1248 ident = pfile->token_buffer + old_written;
1249 len = CPP_WRITTEN (pfile) - old_written;
1251 if (token == CPP_VSPACE)
1253 if (! CPP_TRADITIONAL (pfile))
1254 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1258 else if (token == CPP_NAME)
1260 defined = cpp_defined (pfile, ident, len);
1261 CPP_NUL_TERMINATE (pfile);
1262 CPP_ADJUST_WRITTEN (pfile, 1);
1267 if (! CPP_TRADITIONAL (pfile))
1268 cpp_error (pfile, "`#%s' with invalid argument", name);
1271 if (!CPP_TRADITIONAL (pfile))
1273 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1276 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1278 _cpp_skip_rest_of_line (pfile);
1281 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1285 /* #ifdef is dead simple. */
1291 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1292 conditional_skip (pfile, skip, T_IFDEF, 0);
1296 /* #ifndef is a tad more complex, because we need to check for a
1297 no-reinclusion wrapper. */
1303 int start_of_file, skip;
1304 U_CHAR *control_macro = 0;
1306 start_of_file = pfile->only_seen_white == 2;
1307 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1309 if (start_of_file && !skip)
1310 control_macro = xstrdup (CPP_PWRITTEN (pfile));
1312 conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1316 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1317 If this is a #ifndef starting at the beginning of a file,
1318 CONTROL_MACRO is the macro name tested by the #ifndef.
1319 Otherwise, CONTROL_MACRO is 0. */
1322 conditional_skip (pfile, skip, type, control_macro)
1326 U_CHAR *control_macro;
1330 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1331 temp->lineno = CPP_BUFFER (pfile)->lineno;
1332 temp->next = pfile->if_stack;
1333 temp->control_macro = control_macro;
1334 pfile->if_stack = temp;
1336 pfile->if_stack->type = type;
1339 skip_if_group (pfile);
1342 ++pfile->if_stack->if_succeeded;
1343 _cpp_output_line_command (pfile, same_file);
1347 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1348 return 0 if skipping should continue, 1 if it should halt. Also
1349 adjusts the if_stack as appropriate.
1350 The `#' has been read, but not the identifier. */
1353 consider_directive_while_skipping (pfile, stack)
1358 const struct directive *kt;
1363 _cpp_skip_hspace (pfile);
1365 ident = CPP_WRITTEN (pfile);
1366 _cpp_parse_name (pfile, GETC());
1367 len = CPP_WRITTEN (pfile) - ident;
1369 CPP_SET_WRITTEN (pfile, ident);
1371 for (i = 0; i < N_DIRECTIVES; i++)
1374 if (kt->length == len
1375 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1381 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
1382 temp->next = pfile->if_stack;
1383 pfile->if_stack = temp;
1388 if (pfile->if_stack != stack)
1389 validate_else (pfile, dtable[i].name);
1392 if (pfile->if_stack == stack)
1396 pfile->if_stack->type = i;
1401 if (pfile->if_stack != stack)
1402 validate_else (pfile, dtable[i].name);
1404 if (pfile->if_stack == stack)
1407 temp = pfile->if_stack;
1408 pfile->if_stack = temp->next;
1417 /* Don't let erroneous code go by. */
1418 if (!CPP_OPTION (pfile, lang_asm) && CPP_PEDANTIC (pfile))
1419 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1423 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1424 * leaves input ptr at the sharp sign found.
1427 skip_if_group (pfile)
1431 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1432 const U_CHAR *beg_of_line;
1435 old_written = CPP_WRITTEN (pfile);
1439 beg_of_line = CPP_BUFFER (pfile)->cur;
1441 if (! CPP_TRADITIONAL (pfile))
1442 _cpp_skip_hspace (pfile);
1446 CPP_BUMP_LINE (pfile);
1451 if (consider_directive_while_skipping (pfile, save_if_stack))
1455 return; /* Caller will issue error. */
1458 _cpp_skip_rest_of_line (pfile);
1462 return; /* Caller will issue error. */
1464 CPP_BUMP_LINE (pfile);
1467 /* Back up to the beginning of this line. Caller will process the
1469 CPP_BUFFER (pfile)->cur = beg_of_line;
1470 pfile->only_seen_white = 1;
1474 * handle a #else directive. Do this by just continuing processing
1475 * without changing if_stack ; this is so that the error message
1476 * for missing #endif's etc. will point to the original #if. It
1477 * is possible that something different would be better.
1484 validate_else (pfile, dtable[T_ELSE].name);
1485 _cpp_skip_rest_of_line (pfile);
1487 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1489 cpp_error (pfile, "`#else' not within a conditional");
1494 /* #ifndef can't have its special treatment for containing the whole file
1495 if it has a #else clause. */
1496 pfile->if_stack->control_macro = 0;
1498 if (pfile->if_stack->type == T_ELSE)
1500 cpp_error (pfile, "`#else' after `#else'");
1501 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1502 "the conditional began here");
1504 pfile->if_stack->type = T_ELSE;
1507 if (pfile->if_stack->if_succeeded)
1508 skip_if_group (pfile);
1511 ++pfile->if_stack->if_succeeded; /* continue processing input */
1512 _cpp_output_line_command (pfile, same_file);
1518 * unstack after #endif command
1525 validate_else (pfile, dtable[T_ENDIF].name);
1526 _cpp_skip_rest_of_line (pfile);
1528 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1529 cpp_error (pfile, "`#endif' not within a conditional");
1532 IF_STACK *temp = pfile->if_stack;
1533 pfile->if_stack = temp->next;
1534 if (temp->control_macro != 0)
1536 /* This #endif matched a #ifndef at the start of the file.
1537 See if it is at the end of the file. */
1540 CPP_SET_MARK (pfile);
1544 _cpp_skip_hspace (pfile);
1549 CPP_GOTO_MARK (pfile);
1553 /* This #endif ends a #ifndef
1554 that contains all of the file (aside from whitespace).
1555 Arrange not to include the file again
1556 if the macro that was tested is defined. */
1557 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
1561 _cpp_output_line_command (pfile, same_file);
1566 /* Issue -pedantic warning for text which is not a comment following
1567 an #else or #endif. Do not warn in system headers, as this is harmless
1568 and very common on old systems. */
1571 validate_else (pfile, directive)
1573 const char *directive;
1575 if (! CPP_PEDANTIC (pfile))
1578 _cpp_skip_hspace (pfile);
1579 if (PEEKC () != '\n')
1581 "text following `#%s' violates ANSI standard", directive);
1585 _cpp_handle_eof (pfile)
1588 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1589 struct if_stack *ifs, *nifs;
1591 /* Unwind the conditional stack and generate error messages. */
1592 for (ifs = pfile->if_stack;
1593 ifs != CPP_BUFFER (pfile)->if_stack;
1596 cpp_error_with_line (pfile, ifs->lineno, -1,
1597 "unterminated `#%s' conditional",
1598 dtable[ifs->type].name);
1603 pfile->if_stack = ifs;
1605 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
1607 /* We're about to return from an #include file.
1608 Emit #line information now (as part of the CPP_POP) result.
1609 But the #line refers to the file we will pop to. */
1610 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1611 CPP_BUFFER (pfile) = next_buf;
1612 pfile->input_stack_listing_current = 0;
1613 _cpp_output_line_command (pfile, leave_file);
1614 CPP_BUFFER (pfile) = cur_buffer;
1617 CPP_BUFFER (pfile)->seen_eof = 1;
1626 HASHNODE *base, *this;
1627 HASHNODE **bslot, **tslot;
1629 unsigned long bhash, thash;
1631 _cpp_skip_hspace (pfile);
1632 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1633 ret = _cpp_parse_assertion (pfile);
1638 cpp_error (pfile, "missing token-sequence in `#assert'");
1642 _cpp_skip_hspace (pfile);
1644 if (c != EOF && c != '\n')
1646 cpp_error (pfile, "junk at end of `#assert'");
1650 tlen = strlen (sym);
1651 blen = (U_CHAR *) strchr (sym, '(') - sym;
1652 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1655 cpp_warning (pfile, "`%s' re-asserted", sym);
1659 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1662 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1663 base->value.aschain = 0;
1668 if (base->type != T_ASSERT)
1670 /* Token clash - but with what?! */
1671 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1675 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1676 this->value.aschain = base->value.aschain;
1677 base->value.aschain = this;
1679 pfile->limit = sym; /* Pop */
1683 _cpp_skip_rest_of_line (pfile);
1684 pfile->limit = sym; /* Pop */
1694 long baselen, thislen;
1695 HASHNODE *base, *this, *next;
1697 _cpp_skip_hspace (pfile);
1699 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1700 ret = _cpp_parse_assertion (pfile);
1704 _cpp_skip_hspace (pfile);
1706 if (c != EOF && c != '\n')
1707 cpp_error (pfile, "junk at end of `#unassert'");
1709 thislen = strlen (sym);
1712 base = _cpp_lookup (pfile, sym, thislen);
1714 goto error; /* It isn't an error to #undef what isn't #defined,
1715 so it isn't an error to #unassert what isn't
1716 #asserted either. */
1718 for (this = base->value.aschain; this; this = next)
1720 next = this->value.aschain;
1721 htab_remove_elt (pfile->hashtab, this);
1723 htab_remove_elt (pfile->hashtab, base);
1727 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1728 base = _cpp_lookup (pfile, sym, baselen);
1729 if (! base) goto error;
1730 this = _cpp_lookup (pfile, sym, thislen);
1731 if (! this) goto error;
1734 while (next->value.aschain != this)
1735 next = next->value.aschain;
1737 next->value.aschain = this->value.aschain;
1738 htab_remove_elt (pfile->hashtab, this);
1740 if (base->value.aschain == NULL)
1741 /* Last answer for this predicate deleted. */
1742 htab_remove_elt (pfile->hashtab, base);
1745 pfile->limit = sym; /* Pop */
1748 _cpp_skip_rest_of_line (pfile);
1749 pfile->limit = sym; /* Pop */
1753 /* These are for -D, -U, -A. */
1755 /* Process the string STR as if it appeared as the body of a #define.
1756 If STR is just an identifier, define it with value 1.
1757 If STR has anything after the identifier, then it should
1758 be identifier=definition. */
1761 cpp_define (pfile, str)
1768 p = strchr (str, '=');
1769 /* Copy the entire option so we can modify it.
1770 Change the first "=" in the string to a space. If there is none,
1771 tack " 1" on the end. Then add a newline and a NUL. */
1775 count = strlen (str) + 2;
1776 buf = alloca (count);
1777 memcpy (buf, str, count - 2);
1779 buf[count - 2] = '\n';
1780 buf[count - 1] = '\0';
1784 count = strlen (str) + 4;
1785 buf = alloca (count);
1786 memcpy (buf, str, count - 4);
1787 strcpy (&buf[count-4], " 1\n");
1790 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1793 cpp_pop_buffer (pfile);
1797 /* Process MACRO as if it appeared as the body of an #undef. */
1799 cpp_undef (pfile, macro)
1803 /* Copy the string so we can append a newline. */
1804 size_t len = strlen (macro);
1805 char *buf = alloca (len + 2);
1806 memcpy (buf, macro, len);
1808 buf[len + 1] = '\0';
1809 if (cpp_push_buffer (pfile, buf, len + 1))
1812 cpp_pop_buffer (pfile);
1816 /* Process the string STR as if it appeared as the body of a #assert. */
1818 cpp_assert (pfile, str)
1822 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1825 cpp_pop_buffer (pfile);
1829 /* Process STR as if it appeared as the body of an #unassert. */
1831 cpp_unassert (pfile, str)
1835 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1837 do_unassert (pfile);
1838 cpp_pop_buffer (pfile);
1842 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1844 cpp_defined (pfile, id, len)
1849 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1850 if (hp && hp->type == T_POISON)
1852 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1855 return (hp != NULL);