* builtins.c (expand_builtin): Handle bcmp.
[official-gcc.git] / gcc / cpplib.c
blobdac186d9904e547e7a96f24ad2ac6641308bd89a
1 /* CPP Library.
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
11 later version.
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. */
22 #include "config.h"
23 #include "system.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "hashtab.h"
28 #include "intl.h"
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) \
31 ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
36 /* `struct directive' defines one #-directive, including how to handle it. */
38 struct directive
40 unsigned int length; /* Length of name */
41 const char *name; /* Name of directive */
42 int (*func) /* Function to handle directive */
43 PARAMS ((cpp_reader *));
46 /* Stack of conditionals currently in progress
47 (including both successful and failing conditionals). */
49 struct if_stack
51 struct if_stack *next;
52 int lineno; /* line number where condition started */
53 int if_succeeded; /* truth of last condition in this group */
54 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
55 int type; /* type of last directive seen in this group */
57 typedef struct if_stack IF_STACK;
59 /* Forward declarations. */
61 static void validate_else PARAMS ((cpp_reader *, const char *));
62 static int parse_ifdef PARAMS ((cpp_reader *, const char *));
63 static unsigned int parse_include PARAMS ((cpp_reader *, const char *));
64 static void conditional_skip PARAMS ((cpp_reader *, int, int,
65 U_CHAR *));
66 static void skip_if_group PARAMS ((cpp_reader *));
67 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
68 cpp_reader *, int));
69 static int read_line_number PARAMS ((cpp_reader *, int *));
70 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
71 static int consider_directive_while_skipping
72 PARAMS ((cpp_reader *, IF_STACK *));
73 static int get_macro_name PARAMS ((cpp_reader *));
75 /* This is the table of directive handlers. It is ordered by
76 frequency of occurrence; the numbers at the end are directive
77 counts from all the source code I have lying around (egcs and libc
78 CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
79 pcmcia-cs-3.0.9).
81 The entries with a dash and a name after the count are extensions,
82 of which all but #warning and #include_next are deprecated. The name
83 is where the extension appears to have come from. */
85 #define DIRECTIVE_TABLE \
86 D("define", do_define, T_DEFINE) /* 270554 */ \
87 D("include", do_include, T_INCLUDE) /* 52262 */ \
88 D("endif", do_endif, T_ENDIF) /* 45855 */ \
89 D("ifdef", do_ifdef, T_IFDEF) /* 22000 */ \
90 D("if", do_if, T_IF) /* 18162 */ \
91 D("else", do_else, T_ELSE) /* 9863 */ \
92 D("ifndef", do_ifndef, T_IFNDEF) /* 9675 */ \
93 D("undef", do_undef, T_UNDEF) /* 4837 */ \
94 D("line", do_line, T_LINE) /* 2465 */ \
95 D("elif", do_elif, T_ELIF) /* 610 */ \
96 D("error", do_error, T_ERROR) /* 475 */ \
97 D("pragma", do_pragma, T_PRAGMA) /* 195 */ \
98 D("warning", do_warning, T_WARNING) /* 22 - GNU */ \
99 D("include_next", do_include_next, T_INCLUDE_NEXT) /* 19 - GNU */ \
100 D("ident", do_ident, T_IDENT) /* 11 - SVR4 */ \
101 D("import", do_import, T_IMPORT) /* 0 - ObjC */ \
102 D("assert", do_assert, T_ASSERT) /* 0 - SVR4 */ \
103 D("unassert", do_unassert, T_UNASSERT) /* 0 - SVR4 */ \
104 D("sccs", do_sccs, T_SCCS) /* 0 - SVR2? */
106 /* Use the table to generate a series of prototypes, an enum for the
107 directive names, and an array of directive handlers. */
109 /* The directive-processing functions are declared to return int
110 instead of void, because some old compilers have trouble with
111 pointers to functions returning void. */
113 #define D(name, fun, tag) static int fun PARAMS ((cpp_reader *));
114 DIRECTIVE_TABLE
115 #undef D
117 #define D(name, fun, tag) tag,
118 enum
120 DIRECTIVE_TABLE
121 N_DIRECTIVES
123 #undef D
125 #define D(name, fun, tag) { sizeof name - 1, name, fun },
126 static const struct directive dtable[] =
128 DIRECTIVE_TABLE
130 #undef D
131 #undef DIRECTIVE_TABLE
133 /* Handle a possible # directive.
134 '#' has already been read. */
137 _cpp_handle_directive (pfile)
138 cpp_reader *pfile;
140 int c, i;
141 unsigned int len;
142 U_CHAR *ident;
143 long old_written = CPP_WRITTEN (pfile);
145 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
147 cpp_ice (pfile, "handle_directive called on macro buffer");
148 return 0;
151 _cpp_skip_hspace (pfile);
153 c = PEEKC ();
154 /* # followed by a number is equivalent to #line. Do not recognize
155 this form in assembly language source files. Complain about this
156 form if we're being pedantic, but not if this is regurgitated
157 input (preprocessed or fed back in by the C++ frontend). */
158 if (c >= '0' && c <= '9')
160 if (CPP_OPTIONS (pfile)->lang_asm)
161 return 0;
163 if (CPP_PEDANTIC (pfile)
164 && ! CPP_OPTIONS (pfile)->preprocessed
165 && ! CPP_BUFFER (pfile)->manual_pop)
166 cpp_pedwarn (pfile, "`#' followed by integer");
167 do_line (pfile);
168 return 1;
171 /* If we are rescanning preprocessed input, don't obey any directives
172 other than # nnn. */
173 if (CPP_OPTIONS (pfile)->preprocessed)
174 return 0;
176 /* Now find the directive name. */
177 CPP_PUTC (pfile, '#');
178 _cpp_parse_name (pfile, GETC());
179 ident = pfile->token_buffer + old_written + 1;
180 len = CPP_PWRITTEN (pfile) - ident;
181 if (len == 0)
183 /* A line of just `#' becomes blank. A line with something
184 other than an identifier after the # is reparsed as a non-
185 directive line. */
186 CPP_SET_WRITTEN (pfile, old_written);
187 return (PEEKC() == '\n');
190 /* Decode the keyword and call the appropriate expansion routine. */
191 for (i = 0; i < N_DIRECTIVES; i++)
193 if (dtable[i].length == len
194 && !strncmp (dtable[i].name, ident, len))
195 break;
197 if (i == N_DIRECTIVES)
198 /* # identifier, but not a legit directive. Pass onward as a
199 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
200 return 1;
202 CPP_SET_WRITTEN (pfile, old_written);
204 if (pfile->no_directives)
206 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
207 dtable[i].name);
208 _cpp_skip_rest_of_line (pfile);
210 else
211 (*dtable[i].func) (pfile);
213 return 1;
216 /* Pass a directive through to the output file.
217 BUF points to the contents of the directive, as a contiguous string.
218 LEN is the length of the string pointed to by BUF.
219 KEYWORD is the keyword-table entry for the directive. */
221 static void
222 pass_thru_directive (buf, len, pfile, keyword)
223 const U_CHAR *buf;
224 size_t len;
225 cpp_reader *pfile;
226 int keyword;
228 const struct directive *kt = &dtable[keyword];
229 register unsigned klen = kt->length;
231 CPP_RESERVE (pfile, 1 + klen + len);
232 CPP_PUTC_Q (pfile, '#');
233 CPP_PUTS_Q (pfile, kt->name, klen);
234 if (len != 0 && buf[0] != ' ')
235 CPP_PUTC_Q (pfile, ' ');
236 CPP_PUTS_Q (pfile, buf, len);
239 /* Subroutine of do_define: determine the name of the macro to be
240 defined. */
242 static int
243 get_macro_name (pfile)
244 cpp_reader *pfile;
246 long here, len;
248 here = CPP_WRITTEN (pfile);
249 pfile->no_macro_expand++;
250 if (_cpp_get_directive_token (pfile) != CPP_NAME)
252 cpp_error (pfile, "`#define' must be followed by an identifier");
253 goto invalid;
256 len = CPP_WRITTEN (pfile) - here;
257 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
259 cpp_error (pfile, "`defined' is not a legal macro name");
260 goto invalid;
263 pfile->no_macro_expand--;
264 return len;
266 invalid:
267 _cpp_skip_rest_of_line (pfile);
268 pfile->no_macro_expand--;
269 return 0;
272 /* Process a #define command. */
274 static int
275 do_define (pfile)
276 cpp_reader *pfile;
278 HASHNODE **slot;
279 DEFINITION *def;
280 long here;
281 unsigned long hash;
282 int len, c;
283 int funlike = 0;
284 U_CHAR *sym;
286 here = CPP_WRITTEN (pfile);
287 len = get_macro_name (pfile);
288 if (len == 0)
289 return 0;
291 /* Copy out the name so we can pop the token buffer. */
292 len = CPP_WRITTEN (pfile) - here;
293 sym = (U_CHAR *) alloca (len + 1);
294 memcpy (sym, pfile->token_buffer + here, len);
295 sym[len] = '\0';
296 CPP_SET_WRITTEN (pfile, here);
298 /* If the next character, with no intervening whitespace, is '(',
299 then this is a function-like macro. */
300 c = PEEKC ();
301 if (c == '(')
302 funlike = 1;
303 else if (c != '\n' && !is_hspace (c))
304 /* Otherwise, C99 requires white space after the name. We treat it
305 as an object-like macro if this happens, with a warning. */
306 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
308 def = _cpp_create_definition (pfile, funlike);
309 if (def == 0)
310 return 0;
312 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
313 if (*slot)
315 int ok;
316 HASHNODE *hp = *slot;
318 /* Redefining a macro is ok if the definitions are the same. */
319 if (hp->type == T_MACRO)
320 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
321 /* Redefining a constant is ok with -D. */
322 else if (hp->type == T_CONST || hp->type == T_STDC)
323 ok = ! CPP_OPTIONS (pfile)->done_initializing;
324 /* Otherwise it's not ok. */
325 else
326 ok = 0;
327 /* Print the warning or error if it's not ok. */
328 if (! ok)
330 if (hp->type == T_POISON)
331 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
332 else
333 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
334 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
336 DEFINITION *d = hp->value.defn;
337 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
338 "this is the location of the previous definition");
341 if (hp->type != T_POISON)
343 /* Replace the old definition. */
344 if (hp->type == T_MACRO)
345 _cpp_free_definition (hp->value.defn);
346 hp->type = T_MACRO;
347 hp->value.defn = def;
350 else
352 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
353 hp->value.defn = def;
354 *slot = hp;
357 if (CPP_OPTIONS (pfile)->debug_output
358 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
359 _cpp_dump_definition (pfile, sym, len, def);
360 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
361 pass_thru_directive (sym, len, pfile, T_DEFINE);
363 return 0;
367 * write out a #line command, for instance, after an #include file.
368 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
371 void
372 _cpp_output_line_command (pfile, file_change)
373 cpp_reader *pfile;
374 enum file_change_code file_change;
376 long line;
377 cpp_buffer *ip;
379 if (CPP_OPTIONS (pfile)->no_line_commands
380 || CPP_OPTIONS (pfile)->no_output)
381 return;
383 ip = cpp_file_buffer (pfile);
384 cpp_buf_line_and_col (ip, &line, NULL);
386 /* If the current file has not changed, we omit the #line if it would
387 appear to be a no-op, and we output a few newlines instead
388 if we want to increase the line number by a small amount.
389 We cannot do this if pfile->lineno is zero, because that means we
390 haven't output any line commands yet. (The very first line command
391 output is a `same_file' command.) */
392 if (file_change == same_file && pfile->lineno != 0)
394 if (line == pfile->lineno)
395 return;
397 /* If the inherited line number is a little too small,
398 output some newlines instead of a #line command. */
399 if (line > pfile->lineno && line < pfile->lineno + 8)
401 CPP_RESERVE (pfile, 20);
402 while (line > pfile->lineno)
404 CPP_PUTC_Q (pfile, '\n');
405 pfile->lineno++;
407 return;
411 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
412 CPP_PUTS_Q (pfile, "# ", 2);
414 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
415 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
417 _cpp_quote_string (pfile, ip->nominal_fname);
418 if (file_change != same_file && file_change != rename_file)
420 CPP_PUTC_Q (pfile, ' ');
421 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
423 /* Tell cc1 if following text comes from a system header file. */
424 if (ip->system_header_p)
426 CPP_PUTC_Q (pfile, ' ');
427 CPP_PUTC_Q (pfile, '3');
429 #ifndef NO_IMPLICIT_EXTERN_C
430 /* Tell cc1plus if following text should be treated as C. */
431 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
433 CPP_PUTC_Q (pfile, ' ');
434 CPP_PUTC_Q (pfile, '4');
436 #endif
437 CPP_PUTC_Q (pfile, '\n');
438 pfile->lineno = line;
441 /* Handle #include and #import. */
443 static unsigned int
444 parse_include (pfile, name)
445 cpp_reader *pfile;
446 const char *name;
448 long old_written = CPP_WRITTEN (pfile);
449 enum cpp_token token;
450 int len;
452 pfile->parsing_include_directive++;
453 token = _cpp_get_directive_token (pfile);
454 pfile->parsing_include_directive--;
456 len = CPP_WRITTEN (pfile) - old_written;
458 if (token == CPP_STRING)
459 ; /* No special treatment required. */
460 #ifdef VMS
461 else if (token == CPP_NAME)
463 /* Support '#include xyz' like VAX-C. It is taken as
464 '#include <xyz.h>' and generates a warning. */
465 cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
466 name, name);
468 /* Rewrite the token to <xyz.h>. */
469 CPP_RESERVE (pfile, 4);
470 len += 4;
471 memmove (pfile->token_buffer + old_written + 1,
472 pfile->token_buffer + old_written,
473 CPP_WRITTEN (pfile) - old_written);
474 pfile->token_buffer[old_written] = '<';
475 CPP_PUTS_Q (pfile, ".h>", 2);
477 #endif
478 else
480 cpp_error (pfile, "`#%s' expects \"FILENAME\" or <FILENAME>", name);
481 CPP_SET_WRITTEN (pfile, old_written);
482 _cpp_skip_rest_of_line (pfile);
483 return 0;
486 CPP_NUL_TERMINATE (pfile);
487 CPP_ADJUST_WRITTEN (pfile, 1);
489 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
491 cpp_error (pfile, "junk at end of `#%s'", name);
492 _cpp_skip_rest_of_line (pfile);
495 CPP_SET_WRITTEN (pfile, old_written);
497 if (len == 0)
498 cpp_error (pfile, "empty file name in `#%s'", name);
500 return len;
503 static int
504 do_include (pfile)
505 cpp_reader *pfile;
507 unsigned int len;
508 char *token;
510 len = parse_include (pfile, dtable[T_INCLUDE].name);
511 token = alloca (len + 1);
512 strcpy (token, CPP_PWRITTEN (pfile));
514 if (CPP_OPTIONS (pfile)->dump_includes)
515 pass_thru_directive (token, len, pfile, T_INCLUDE);
517 _cpp_execute_include (pfile, token, len, 0, 0);
518 return 0;
521 static int
522 do_import (pfile)
523 cpp_reader *pfile;
525 unsigned int len;
526 char *token;
528 if (CPP_PEDANTIC (pfile))
529 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
531 if (CPP_OPTIONS (pfile)->warn_import
532 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
534 pfile->import_warning = 1;
535 cpp_warning (pfile,
536 "#import is obsolete, use an #ifndef wrapper in the header file");
539 len = parse_include (pfile, dtable[T_IMPORT].name);
540 token = alloca (len + 1);
541 strcpy (token, CPP_PWRITTEN (pfile));
543 if (CPP_OPTIONS (pfile)->dump_includes)
544 pass_thru_directive (token, len, pfile, T_IMPORT);
546 _cpp_execute_include (pfile, token, len, 1, 0);
547 return 0;
550 static int
551 do_include_next (pfile)
552 cpp_reader *pfile;
554 unsigned int len;
555 char *token;
556 struct file_name_list *search_start = 0;
558 if (CPP_PEDANTIC (pfile))
559 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
561 len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
563 token = alloca (len + 1);
564 strcpy (token, CPP_PWRITTEN (pfile));
566 if (CPP_OPTIONS (pfile)->dump_includes)
567 pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
569 /* For #include_next, skip in the search path past the dir in which the
570 containing file was found. Treat files specified using an absolute path
571 as if there are no more directories to search. Treat the primary source
572 file like any other included source, but generate a warning. */
573 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
575 if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
576 search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
578 else
579 cpp_warning (pfile, "#include_next in primary source file");
581 _cpp_execute_include (pfile, token, len, 0, search_start);
582 return 0;
585 /* Subroutine of do_line. Read next token from PFILE without adding it to
586 the output buffer. If it is a number between 1 and 4, store it in *NUM
587 and return 1; otherwise, return 0 and complain if we aren't at the end
588 of the directive. */
590 static int
591 read_line_number (pfile, num)
592 cpp_reader *pfile;
593 int *num;
595 long save_written = CPP_WRITTEN (pfile);
596 U_CHAR *p;
597 enum cpp_token token = _cpp_get_directive_token (pfile);
598 CPP_SET_WRITTEN (pfile, save_written);
599 p = pfile->token_buffer + save_written;
601 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
603 *num = p[0] - '0';
604 return 1;
606 else
608 if (token != CPP_VSPACE && token != CPP_EOF)
609 cpp_error (pfile, "invalid format `#line' command");
610 return 0;
614 /* Interpret #line command.
615 Note that the filename string (if any) is treated as if it were an
616 include filename. That means no escape handling. */
618 static int
619 do_line (pfile)
620 cpp_reader *pfile;
622 cpp_buffer *ip = CPP_BUFFER (pfile);
623 int new_lineno;
624 long old_written = CPP_WRITTEN (pfile);
625 enum file_change_code file_change = same_file;
626 enum cpp_token token;
627 char *x;
629 token = _cpp_get_directive_token (pfile);
631 if (token != CPP_NUMBER)
633 cpp_error (pfile, "token after `#line' is not an integer");
634 goto bad_line_directive;
637 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
638 if (x[0] != '\0')
640 cpp_error (pfile, "token after `#line' is not an integer");
641 goto bad_line_directive;
643 CPP_SET_WRITTEN (pfile, old_written);
645 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
646 cpp_pedwarn (pfile, "line number out of range in `#line' command");
648 token = _cpp_get_directive_token (pfile);
650 if (token == CPP_STRING)
652 U_CHAR *fname = pfile->token_buffer + old_written + 1;
653 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
654 int action_number = 0;
656 file_change = rename_file;
658 if (read_line_number (pfile, &action_number))
660 if (CPP_PEDANTIC (pfile))
661 cpp_pedwarn (pfile, "garbage at end of `#line' command");
663 if (action_number == 1)
665 file_change = enter_file;
666 read_line_number (pfile, &action_number);
668 else if (action_number == 2)
670 file_change = leave_file;
671 read_line_number (pfile, &action_number);
673 if (action_number == 3)
675 ip->system_header_p = 1;
676 read_line_number (pfile, &action_number);
678 if (action_number == 4)
680 ip->system_header_p = 2;
681 read_line_number (pfile, &action_number);
685 *end_name = '\0';
687 if (strcmp (fname, ip->nominal_fname))
689 const char *newname, *oldname;
690 if (!strcmp (fname, ip->ihash->name))
691 newname = ip->ihash->name;
692 else if (ip->last_nominal_fname
693 && !strcmp (fname, ip->last_nominal_fname))
694 newname = ip->last_nominal_fname;
695 else
696 newname = xstrdup (fname);
698 oldname = ip->nominal_fname;
699 ip->nominal_fname = newname;
701 if (ip->last_nominal_fname
702 && ip->last_nominal_fname != oldname
703 && ip->last_nominal_fname != newname
704 && ip->last_nominal_fname != ip->ihash->name)
705 free ((void *) ip->last_nominal_fname);
707 if (newname == ip->ihash->name)
708 ip->last_nominal_fname = NULL;
709 else
710 ip->last_nominal_fname = oldname;
713 else if (token != CPP_VSPACE && token != CPP_EOF)
715 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
716 goto bad_line_directive;
719 /* The Newline at the end of this line remains to be processed.
720 To put the next line at the specified line number,
721 we must store a line number now that is one less. */
722 ip->lineno = new_lineno - 1;
723 CPP_SET_WRITTEN (pfile, old_written);
724 _cpp_output_line_command (pfile, file_change);
725 return 0;
727 bad_line_directive:
728 _cpp_skip_rest_of_line (pfile);
729 CPP_SET_WRITTEN (pfile, old_written);
730 return 0;
733 /* Remove the definition of a symbol from the symbol table.
734 According to the C standard, it is not an error to undef
735 something that has no definitions. */
736 static int
737 do_undef (pfile)
738 cpp_reader *pfile;
740 int len;
741 HASHNODE **slot;
742 U_CHAR *buf, *name, *limit;
743 int c;
744 long here = CPP_WRITTEN (pfile);
745 enum cpp_token token;
747 _cpp_skip_hspace (pfile);
748 c = GETC();
749 if (! is_idstart(c))
751 cpp_error (pfile, "token after #undef is not an identifier");
752 _cpp_skip_rest_of_line (pfile);
753 return 1;
756 _cpp_parse_name (pfile, c);
757 buf = pfile->token_buffer + here;
758 limit = CPP_PWRITTEN(pfile);
760 /* Copy out the token so we can pop the token buffer. */
761 len = limit - buf;
762 name = (U_CHAR *) alloca (len + 1);
763 memcpy (name, buf, len);
764 name[len] = '\0';
766 token = _cpp_get_directive_token (pfile);
767 if (token != CPP_VSPACE)
769 cpp_pedwarn (pfile, "junk on line after #undef");
770 _cpp_skip_rest_of_line (pfile);
772 CPP_SET_WRITTEN (pfile, here);
774 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
775 if (slot)
777 HASHNODE *hp = *slot;
778 /* If we are generating additional info for debugging (with -g) we
779 need to pass through all effective #undef commands. */
780 if (CPP_OPTIONS (pfile)->debug_output)
781 pass_thru_directive (name, len, pfile, T_UNDEF);
782 if (hp->type == T_POISON)
783 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
784 else
786 if (hp->type != T_MACRO)
787 cpp_warning (pfile, "undefining `%s'", hp->name);
789 htab_clear_slot (pfile->hashtab, (void **)slot);
793 return 0;
797 * Report an error detected by the program we are processing.
798 * Use the text of the line in the error message.
799 * (We use error because it prints the filename & line#.)
802 static int
803 do_error (pfile)
804 cpp_reader *pfile;
806 const U_CHAR *text, *limit;
808 _cpp_skip_hspace (pfile);
809 text = CPP_BUFFER (pfile)->cur;
810 _cpp_skip_rest_of_line (pfile);
811 limit = CPP_BUFFER (pfile)->cur;
813 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
814 return 0;
818 * Report a warning detected by the program we are processing.
819 * Use the text of the line in the warning message, then continue.
822 static int
823 do_warning (pfile)
824 cpp_reader *pfile;
826 const U_CHAR *text, *limit;
828 _cpp_skip_hspace (pfile);
829 text = CPP_BUFFER (pfile)->cur;
830 _cpp_skip_rest_of_line (pfile);
831 limit = CPP_BUFFER (pfile)->cur;
833 if (CPP_PEDANTIC (pfile))
834 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
836 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
837 return 0;
840 /* Report program identification. */
842 static int
843 do_ident (pfile)
844 cpp_reader *pfile;
846 long old_written = CPP_WRITTEN (pfile);
848 /* Allow #ident in system headers, since that's not user's fault. */
849 if (CPP_PEDANTIC (pfile))
850 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
852 CPP_PUTS (pfile, "#ident ", 7);
854 /* Next token should be a string constant. */
855 if (_cpp_get_directive_token (pfile) == CPP_STRING)
856 /* And then a newline. */
857 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
858 /* Good - ship it. */
859 return 0;
861 cpp_error (pfile, "invalid #ident");
862 _cpp_skip_rest_of_line (pfile);
863 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
865 return 0;
868 /* Pragmata handling. We handle some of these, and pass the rest on
869 to the front end. C99 defines three pragmas and says that no macro
870 expansion is to be performed on them; whether or not macro
871 expansion happens for other pragmas is implementation defined.
872 This implementation never macro-expands the text after #pragma.
874 We currently do not support the _Pragma operator. Support for that
875 has to be coordinated with the front end. Proposed implementation:
876 both #pragma blah blah and _Pragma("blah blah") become
877 __builtin_pragma(blah blah) and we teach the parser about that. */
879 /* Sub-handlers for the pragmas needing treatment here.
880 They return 1 if the token buffer is to be popped, 0 if not. */
881 static int do_pragma_once PARAMS ((cpp_reader *));
882 static int do_pragma_implementation PARAMS ((cpp_reader *));
883 static int do_pragma_poison PARAMS ((cpp_reader *));
884 static int do_pragma_default PARAMS ((cpp_reader *));
886 static int
887 do_pragma (pfile)
888 cpp_reader *pfile;
890 long here, key;
891 U_CHAR *buf;
892 int pop;
893 enum cpp_token token;
895 here = CPP_WRITTEN (pfile);
896 CPP_PUTS (pfile, "#pragma ", 8);
898 key = CPP_WRITTEN (pfile);
899 pfile->no_macro_expand++;
900 token = _cpp_get_directive_token (pfile);
901 if (token != CPP_NAME)
903 if (token == CPP_VSPACE)
904 goto empty;
905 else
906 goto skip;
909 buf = pfile->token_buffer + key;
910 CPP_PUTC (pfile, ' ');
912 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
913 if (tokis ("once"))
914 pop = do_pragma_once (pfile);
915 else if (tokis ("implementation"))
916 pop = do_pragma_implementation (pfile);
917 else if (tokis ("poison"))
918 pop = do_pragma_poison (pfile);
919 else
920 pop = do_pragma_default (pfile);
921 #undef tokis
923 if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
924 goto skip;
926 if (pop)
927 CPP_SET_WRITTEN (pfile, here);
928 pfile->no_macro_expand--;
929 return 0;
931 skip:
932 cpp_error (pfile, "malformed #pragma directive");
933 _cpp_skip_rest_of_line (pfile);
934 empty:
935 CPP_SET_WRITTEN (pfile, here);
936 pfile->no_macro_expand--;
937 return 0;
940 static int
941 do_pragma_default (pfile)
942 cpp_reader *pfile;
944 while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
945 CPP_PUTC (pfile, ' ');
946 return 0;
949 static int
950 do_pragma_once (pfile)
951 cpp_reader *pfile;
953 cpp_buffer *ip = CPP_BUFFER (pfile);
955 /* Allow #pragma once in system headers, since that's not the user's
956 fault. */
957 if (!ip->system_header_p)
958 cpp_warning (pfile, "`#pragma once' is obsolete");
960 if (CPP_PREV_BUFFER (ip) == NULL)
961 cpp_warning (pfile, "`#pragma once' outside include file");
962 else
963 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
965 return 1;
968 static int
969 do_pragma_implementation (pfile)
970 cpp_reader *pfile;
972 /* Be quiet about `#pragma implementation' for a file only if it hasn't
973 been included yet. */
974 enum cpp_token token;
975 long written = CPP_WRITTEN (pfile);
976 U_CHAR *name;
977 U_CHAR *copy;
978 size_t len;
980 token = _cpp_get_directive_token (pfile);
981 if (token == CPP_VSPACE)
982 return 0;
983 else if (token != CPP_STRING)
985 cpp_error (pfile, "malformed #pragma implementation");
986 return 1;
989 /* Trim the leading and trailing quote marks from the string. */
990 name = pfile->token_buffer + written + 1;
991 len = CPP_PWRITTEN (pfile) - name;
992 copy = (U_CHAR *) alloca (len);
993 memcpy (copy, name, len - 1);
994 copy[len - 1] = '\0';
996 if (cpp_included (pfile, copy))
997 cpp_warning (pfile,
998 "`#pragma implementation' for `%s' appears after file is included",
999 copy);
1000 return 0;
1003 static int
1004 do_pragma_poison (pfile)
1005 cpp_reader *pfile;
1007 /* Poison these symbols so that all subsequent usage produces an
1008 error message. */
1009 U_CHAR *p;
1010 HASHNODE **slot;
1011 long written;
1012 size_t len;
1013 enum cpp_token token;
1014 int writeit;
1015 unsigned long hash;
1017 /* As a rule, don't include #pragma poison commands in output,
1018 unless the user asks for them. */
1019 writeit = (CPP_OPTIONS (pfile)->debug_output
1020 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1021 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1023 for (;;)
1025 written = CPP_WRITTEN (pfile);
1026 token = _cpp_get_directive_token (pfile);
1027 if (token == CPP_VSPACE)
1028 break;
1029 if (token != CPP_NAME)
1031 cpp_error (pfile, "invalid #pragma poison directive");
1032 _cpp_skip_rest_of_line (pfile);
1033 return 1;
1036 p = pfile->token_buffer + written;
1037 len = strlen (p);
1038 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1039 if (*slot)
1041 HASHNODE *hp = *slot;
1042 if (hp->type != T_POISON)
1044 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1045 if (hp->type == T_MACRO)
1046 _cpp_free_definition (hp->value.defn);
1047 hp->value.defn = 0;
1048 hp->type = T_POISON;
1051 else
1053 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1054 hp->value.cpval = 0;
1055 *slot = hp;
1057 if (writeit)
1058 CPP_PUTC (pfile, ' ');
1060 return !writeit;
1063 /* Just ignore #sccs, on systems where we define it at all. */
1064 static int
1065 do_sccs (pfile)
1066 cpp_reader *pfile;
1068 #ifdef SCCS_DIRECTIVE
1069 if (CPP_PEDANTIC (pfile))
1070 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1071 #else
1072 cpp_error (pfile, "undefined or invalid # directive `sccs'");
1073 #endif
1074 _cpp_skip_rest_of_line (pfile);
1075 return 0;
1079 /* We've found an `#if' directive. If the only thing before it in
1080 this file is white space, and if it is of the form
1081 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1082 for inclusion of this file. (See redundant_include_p in cppfiles.c
1083 for an explanation of controlling macros.) If so, return a
1084 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1086 static U_CHAR *
1087 detect_if_not_defined (pfile)
1088 cpp_reader *pfile;
1090 U_CHAR *control_macro = 0;
1092 if (pfile->only_seen_white == 2)
1094 U_CHAR *ident;
1095 enum cpp_token token;
1096 int base_offset;
1097 int token_offset;
1098 int need_rparen = 0;
1100 /* Save state required for restore. */
1101 pfile->no_macro_expand++;
1102 CPP_SET_MARK (pfile);
1103 base_offset = CPP_WRITTEN (pfile);
1105 /* Look for `!', */
1106 if (_cpp_get_directive_token (pfile) != CPP_OTHER
1107 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1108 || CPP_PWRITTEN (pfile)[-1] != '!')
1109 goto restore;
1111 /* ...then `defined', */
1112 token_offset = CPP_WRITTEN (pfile);
1113 token = _cpp_get_directive_token (pfile);
1114 if (token != CPP_NAME)
1115 goto restore;
1116 ident = pfile->token_buffer + token_offset;
1117 CPP_NUL_TERMINATE (pfile);
1118 if (strcmp (ident, "defined"))
1119 goto restore;
1121 /* ...then an optional '(' and the name, */
1122 token_offset = CPP_WRITTEN (pfile);
1123 token = _cpp_get_directive_token (pfile);
1124 if (token == CPP_LPAREN)
1126 token_offset = CPP_WRITTEN (pfile);
1127 token = _cpp_get_directive_token (pfile);
1128 if (token != CPP_NAME)
1129 goto restore;
1130 need_rparen = 1;
1132 else if (token != CPP_NAME)
1133 goto restore;
1135 ident = pfile->token_buffer + token_offset;
1136 CPP_NUL_TERMINATE (pfile);
1138 /* ...then the ')', if necessary, */
1139 if ((!need_rparen || _cpp_get_directive_token (pfile) == CPP_RPAREN)
1140 /* ...and make sure there's nothing else on the line. */
1141 && _cpp_get_directive_token (pfile) == CPP_VSPACE)
1142 control_macro = (U_CHAR *) xstrdup (ident);
1144 restore:
1145 CPP_SET_WRITTEN (pfile, base_offset);
1146 pfile->no_macro_expand--;
1147 CPP_GOTO_MARK (pfile);
1150 return control_macro;
1154 * #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1155 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1158 static int
1159 do_if (pfile)
1160 cpp_reader *pfile;
1162 U_CHAR *control_macro = detect_if_not_defined (pfile);
1163 int value = _cpp_parse_expr (pfile);
1164 conditional_skip (pfile, value == 0, T_IF, control_macro);
1165 return 0;
1169 * handle a #elif directive by not changing if_stack either.
1170 * see the comment above do_else.
1173 static int
1174 do_elif (pfile)
1175 cpp_reader *pfile;
1177 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1179 cpp_error (pfile, "`#elif' not within a conditional");
1180 return 0;
1182 else
1184 if (pfile->if_stack->type == T_ELSE)
1186 cpp_error (pfile, "`#elif' after `#else'");
1187 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1188 "the conditional began here");
1190 pfile->if_stack->type = T_ELIF;
1193 if (pfile->if_stack->if_succeeded)
1194 skip_if_group (pfile);
1195 else
1197 if (_cpp_parse_expr (pfile) == 0)
1198 skip_if_group (pfile);
1199 else
1201 ++pfile->if_stack->if_succeeded; /* continue processing input */
1202 _cpp_output_line_command (pfile, same_file);
1205 return 0;
1208 /* Parse an #ifdef or #ifndef directive. Returns 1 for defined, 0 for
1209 not defined; the macro tested is left in the token buffer (but
1210 popped). */
1212 static int
1213 parse_ifdef (pfile, name)
1214 cpp_reader *pfile;
1215 const char *name;
1217 U_CHAR *ident;
1218 unsigned int len;
1219 enum cpp_token token;
1220 long old_written = CPP_WRITTEN (pfile);
1221 int defined;
1223 pfile->no_macro_expand++;
1224 token = _cpp_get_directive_token (pfile);
1225 pfile->no_macro_expand--;
1227 ident = pfile->token_buffer + old_written;
1228 len = CPP_WRITTEN (pfile) - old_written;
1230 if (token == CPP_VSPACE)
1232 if (! CPP_TRADITIONAL (pfile))
1233 cpp_pedwarn (pfile, "`#%s' with no argument", name);
1234 defined = 0;
1235 goto done;
1237 else if (token == CPP_NAME)
1239 defined = cpp_defined (pfile, ident, len);
1240 CPP_NUL_TERMINATE (pfile);
1241 CPP_ADJUST_WRITTEN (pfile, 1);
1243 else
1245 defined = 0;
1246 if (! CPP_TRADITIONAL (pfile))
1247 cpp_error (pfile, "`#%s' with invalid argument", name);
1250 if (!CPP_TRADITIONAL (pfile))
1252 if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1253 goto done;
1255 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", name);
1257 _cpp_skip_rest_of_line (pfile);
1259 done:
1260 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1261 return defined;
1264 /* #ifdef is dead simple. */
1266 static int
1267 do_ifdef (pfile)
1268 cpp_reader *pfile;
1270 int skip = ! parse_ifdef (pfile, dtable[T_IFDEF].name);
1271 conditional_skip (pfile, skip, T_IFDEF, 0);
1272 return 0;
1275 /* #ifndef is a tad more complex, because we need to check for a
1276 no-reinclusion wrapper. */
1278 static int
1279 do_ifndef (pfile)
1280 cpp_reader *pfile;
1282 int start_of_file, skip;
1283 U_CHAR *control_macro = 0;
1285 start_of_file = pfile->only_seen_white == 2;
1286 skip = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1288 if (start_of_file && !skip)
1289 control_macro = xstrdup (CPP_PWRITTEN (pfile));
1291 conditional_skip (pfile, skip, T_IFNDEF, control_macro);
1292 return 0;
1295 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1296 If this is a #ifndef starting at the beginning of a file,
1297 CONTROL_MACRO is the macro name tested by the #ifndef.
1298 Otherwise, CONTROL_MACRO is 0. */
1300 static void
1301 conditional_skip (pfile, skip, type, control_macro)
1302 cpp_reader *pfile;
1303 int skip;
1304 int type;
1305 U_CHAR *control_macro;
1307 IF_STACK *temp;
1309 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
1310 temp->lineno = CPP_BUFFER (pfile)->lineno;
1311 temp->next = pfile->if_stack;
1312 temp->control_macro = control_macro;
1313 pfile->if_stack = temp;
1315 pfile->if_stack->type = type;
1317 if (skip != 0) {
1318 skip_if_group (pfile);
1319 return;
1320 } else {
1321 ++pfile->if_stack->if_succeeded;
1322 _cpp_output_line_command (pfile, same_file);
1326 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1327 return 0 if skipping should continue, 1 if it should halt. Also
1328 adjusts the if_stack as appropriate.
1329 The `#' has been read, but not the identifier. */
1331 static int
1332 consider_directive_while_skipping (pfile, stack)
1333 cpp_reader *pfile;
1334 IF_STACK *stack;
1336 long ident;
1337 const struct directive *kt;
1338 int i;
1339 unsigned int len;
1340 IF_STACK *temp;
1342 _cpp_skip_hspace (pfile);
1344 ident = CPP_WRITTEN (pfile);
1345 _cpp_parse_name (pfile, GETC());
1346 len = CPP_WRITTEN (pfile) - ident;
1348 CPP_SET_WRITTEN (pfile, ident);
1350 for (i = 0; i < N_DIRECTIVES; i++)
1352 kt = &dtable[i];
1353 if (kt->length == len
1354 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1355 switch (i)
1357 case T_IF:
1358 case T_IFDEF:
1359 case T_IFNDEF:
1360 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
1361 temp->next = pfile->if_stack;
1362 pfile->if_stack = temp;
1363 temp->type = i;
1364 return 0;
1366 case T_ELSE:
1367 if (pfile->if_stack != stack)
1368 validate_else (pfile, dtable[i].name);
1369 /* fall through */
1370 case T_ELIF:
1371 if (pfile->if_stack == stack)
1372 return 1;
1373 else
1375 pfile->if_stack->type = i;
1376 return 0;
1379 case T_ENDIF:
1380 if (pfile->if_stack != stack)
1381 validate_else (pfile, dtable[i].name);
1383 if (pfile->if_stack == stack)
1384 return 1;
1386 temp = pfile->if_stack;
1387 pfile->if_stack = temp->next;
1388 free (temp);
1389 return 0;
1391 default:
1392 return 0;
1396 /* Don't let erroneous code go by. */
1397 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
1398 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1399 return 0;
1402 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1403 * leaves input ptr at the sharp sign found.
1405 static void
1406 skip_if_group (pfile)
1407 cpp_reader *pfile;
1409 int c;
1410 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
1411 const U_CHAR *beg_of_line;
1412 long old_written;
1414 old_written = CPP_WRITTEN (pfile);
1416 for (;;)
1418 beg_of_line = CPP_BUFFER (pfile)->cur;
1420 if (! CPP_TRADITIONAL (pfile))
1421 _cpp_skip_hspace (pfile);
1422 c = GETC();
1423 if (c == '\n')
1425 CPP_BUMP_LINE (pfile);
1426 continue;
1428 else if (c == '#')
1430 if (consider_directive_while_skipping (pfile, save_if_stack))
1431 break;
1433 else if (c == EOF)
1434 return; /* Caller will issue error. */
1436 FORWARD(-1);
1437 _cpp_skip_rest_of_line (pfile);
1439 c = GETC();
1440 if (c == EOF)
1441 return; /* Caller will issue error. */
1442 else
1443 CPP_BUMP_LINE (pfile);
1446 /* Back up to the beginning of this line. Caller will process the
1447 directive. */
1448 CPP_BUFFER (pfile)->cur = beg_of_line;
1449 pfile->only_seen_white = 1;
1453 * handle a #else directive. Do this by just continuing processing
1454 * without changing if_stack ; this is so that the error message
1455 * for missing #endif's etc. will point to the original #if. It
1456 * is possible that something different would be better.
1459 static int
1460 do_else (pfile)
1461 cpp_reader *pfile;
1463 validate_else (pfile, dtable[T_ELSE].name);
1464 _cpp_skip_rest_of_line (pfile);
1466 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1468 cpp_error (pfile, "`#else' not within a conditional");
1469 return 0;
1471 else
1473 /* #ifndef can't have its special treatment for containing the whole file
1474 if it has a #else clause. */
1475 pfile->if_stack->control_macro = 0;
1477 if (pfile->if_stack->type == T_ELSE)
1479 cpp_error (pfile, "`#else' after `#else'");
1480 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1481 "the conditional began here");
1483 pfile->if_stack->type = T_ELSE;
1486 if (pfile->if_stack->if_succeeded)
1487 skip_if_group (pfile);
1488 else
1490 ++pfile->if_stack->if_succeeded; /* continue processing input */
1491 _cpp_output_line_command (pfile, same_file);
1493 return 0;
1497 * unstack after #endif command
1500 static int
1501 do_endif (pfile)
1502 cpp_reader *pfile;
1504 validate_else (pfile, dtable[T_ENDIF].name);
1505 _cpp_skip_rest_of_line (pfile);
1507 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1508 cpp_error (pfile, "`#endif' not within a conditional");
1509 else
1511 IF_STACK *temp = pfile->if_stack;
1512 pfile->if_stack = temp->next;
1513 if (temp->control_macro != 0)
1515 /* This #endif matched a #ifndef at the start of the file.
1516 See if it is at the end of the file. */
1517 int c;
1519 CPP_SET_MARK (pfile);
1521 for (;;)
1523 _cpp_skip_hspace (pfile);
1524 c = GETC ();
1525 if (c != '\n')
1526 break;
1528 CPP_GOTO_MARK (pfile);
1530 if (c == EOF)
1532 /* This #endif ends a #ifndef
1533 that contains all of the file (aside from whitespace).
1534 Arrange not to include the file again
1535 if the macro that was tested is defined. */
1536 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
1539 free (temp);
1540 _cpp_output_line_command (pfile, same_file);
1542 return 0;
1545 /* Issue -pedantic warning for text which is not a comment following
1546 an #else or #endif. Do not warn in system headers, as this is harmless
1547 and very common on old systems. */
1549 static void
1550 validate_else (pfile, directive)
1551 cpp_reader *pfile;
1552 const char *directive;
1554 if (! CPP_PEDANTIC (pfile))
1555 return;
1557 _cpp_skip_hspace (pfile);
1558 if (PEEKC () != '\n')
1559 cpp_pedwarn (pfile,
1560 "text following `#%s' violates ANSI standard", directive);
1563 void
1564 _cpp_handle_eof (pfile)
1565 cpp_reader *pfile;
1567 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
1568 struct if_stack *ifs, *nifs;
1570 /* Unwind the conditional stack and generate error messages. */
1571 for (ifs = pfile->if_stack;
1572 ifs != CPP_BUFFER (pfile)->if_stack;
1573 ifs = nifs)
1575 cpp_error_with_line (pfile, ifs->lineno, -1,
1576 "unterminated `#%s' conditional",
1577 dtable[ifs->type].name);
1579 nifs = ifs->next;
1580 free (ifs);
1582 pfile->if_stack = ifs;
1584 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
1586 /* We're about to return from an #include file.
1587 Emit #line information now (as part of the CPP_POP) result.
1588 But the #line refers to the file we will pop to. */
1589 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
1590 CPP_BUFFER (pfile) = next_buf;
1591 pfile->input_stack_listing_current = 0;
1592 _cpp_output_line_command (pfile, leave_file);
1593 CPP_BUFFER (pfile) = cur_buffer;
1596 CPP_BUFFER (pfile)->seen_eof = 1;
1599 static int
1600 do_assert (pfile)
1601 cpp_reader *pfile;
1603 U_CHAR *sym;
1604 int ret, c;
1605 HASHNODE *base, *this;
1606 HASHNODE **bslot, **tslot;
1607 size_t blen, tlen;
1608 unsigned long bhash, thash;
1610 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
1611 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
1613 _cpp_skip_hspace (pfile);
1614 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1615 ret = _cpp_parse_assertion (pfile);
1616 if (ret == 0)
1617 goto error;
1618 else if (ret == 1)
1620 cpp_error (pfile, "missing token-sequence in `#assert'");
1621 goto error;
1624 _cpp_skip_hspace (pfile);
1625 c = PEEKC();
1626 if (c != EOF && c != '\n')
1628 cpp_error (pfile, "junk at end of `#assert'");
1629 goto error;
1632 tlen = strlen (sym);
1633 blen = (U_CHAR *) strchr (sym, '(') - sym;
1634 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
1635 if (*tslot)
1637 cpp_warning (pfile, "`%s' re-asserted", sym);
1638 goto error;
1641 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
1642 if (! *bslot)
1643 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
1644 else
1646 base = *bslot;
1647 if (base->type != T_ASSERT)
1649 /* Token clash - but with what?! */
1650 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
1651 goto error;
1654 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
1655 this->value.aschain = base->value.aschain;
1656 base->value.aschain = this;
1658 pfile->limit = sym; /* Pop */
1659 return 0;
1661 error:
1662 _cpp_skip_rest_of_line (pfile);
1663 pfile->limit = sym; /* Pop */
1664 return 0;
1667 static int
1668 do_unassert (pfile)
1669 cpp_reader *pfile;
1671 int c, ret;
1672 U_CHAR *sym;
1673 long baselen, thislen;
1674 HASHNODE *base, *this, *next;
1676 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
1677 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
1679 _cpp_skip_hspace (pfile);
1681 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
1682 ret = _cpp_parse_assertion (pfile);
1683 if (ret == 0)
1684 goto error;
1686 _cpp_skip_hspace (pfile);
1687 c = PEEKC ();
1688 if (c != EOF && c != '\n')
1689 cpp_error (pfile, "junk at end of `#unassert'");
1691 thislen = strlen (sym);
1692 if (ret == 1)
1694 base = _cpp_lookup (pfile, sym, thislen);
1695 if (! base)
1696 goto error; /* It isn't an error to #undef what isn't #defined,
1697 so it isn't an error to #unassert what isn't
1698 #asserted either. */
1700 for (this = base->value.aschain; this; this = next)
1702 next = this->value.aschain;
1703 htab_remove_elt (pfile->hashtab, this);
1705 htab_remove_elt (pfile->hashtab, base);
1707 else
1709 baselen = (U_CHAR *) strchr (sym, '(') - sym;
1710 base = _cpp_lookup (pfile, sym, baselen);
1711 if (! base) goto error;
1712 this = _cpp_lookup (pfile, sym, thislen);
1713 if (! this) goto error;
1715 next = base;
1716 while (next->value.aschain != this)
1717 next = next->value.aschain;
1719 next->value.aschain = this->value.aschain;
1720 htab_remove_elt (pfile->hashtab, this);
1722 if (base->value.aschain == NULL)
1723 /* Last answer for this predicate deleted. */
1724 htab_remove_elt (pfile->hashtab, base);
1727 pfile->limit = sym; /* Pop */
1728 return 0;
1729 error:
1730 _cpp_skip_rest_of_line (pfile);
1731 pfile->limit = sym; /* Pop */
1732 return 0;
1735 /* These are for -D, -U, -A. */
1737 /* Process the string STR as if it appeared as the body of a #define.
1738 If STR is just an identifier, define it with value 1.
1739 If STR has anything after the identifier, then it should
1740 be identifier=definition. */
1742 void
1743 cpp_define (pfile, str)
1744 cpp_reader *pfile;
1745 const char *str;
1747 char *buf, *p;
1748 size_t count;
1750 p = strchr (str, '=');
1751 /* Copy the entire option so we can modify it.
1752 Change the first "=" in the string to a space. If there is none,
1753 tack " 1" on the end. Then add a newline and a NUL. */
1755 if (p)
1757 count = strlen (str) + 2;
1758 buf = alloca (count);
1759 memcpy (buf, str, count - 2);
1760 buf[p - str] = ' ';
1761 buf[count - 2] = '\n';
1762 buf[count - 1] = '\0';
1764 else
1766 count = strlen (str) + 4;
1767 buf = alloca (count);
1768 memcpy (buf, str, count - 4);
1769 strcpy (&buf[count-4], " 1\n");
1772 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
1774 do_define (pfile);
1775 cpp_pop_buffer (pfile);
1779 /* Process MACRO as if it appeared as the body of an #undef. */
1780 void
1781 cpp_undef (pfile, macro)
1782 cpp_reader *pfile;
1783 const char *macro;
1785 /* Copy the string so we can append a newline. */
1786 size_t len = strlen (macro);
1787 char *buf = alloca (len + 2);
1788 memcpy (buf, macro, len);
1789 buf[len] = '\n';
1790 buf[len + 1] = '\0';
1791 if (cpp_push_buffer (pfile, buf, len + 1))
1793 do_undef (pfile);
1794 cpp_pop_buffer (pfile);
1798 /* Process the string STR as if it appeared as the body of a #assert. */
1799 void
1800 cpp_assert (pfile, str)
1801 cpp_reader *pfile;
1802 const char *str;
1804 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1806 do_assert (pfile);
1807 cpp_pop_buffer (pfile);
1811 /* Process STR as if it appeared as the body of an #unassert. */
1812 void
1813 cpp_unassert (pfile, str)
1814 cpp_reader *pfile;
1815 const char *str;
1817 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
1819 do_unassert (pfile);
1820 cpp_pop_buffer (pfile);
1824 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
1826 cpp_defined (pfile, id, len)
1827 cpp_reader *pfile;
1828 const U_CHAR *id;
1829 int len;
1831 HASHNODE *hp = _cpp_lookup (pfile, id, len);
1832 if (hp && hp->type == T_POISON)
1834 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1835 return 0;
1837 return (hp != NULL);