OSDN Git Service

2000-06-06 Jakub Jelinek <jakub@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cpplib.c
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
7
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.
12
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.
17
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.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "cpplib.h"
26 #include "cpphash.h"
27 #include "intl.h"
28 #include "symcat.h"
29
30 /* `struct directive' defines one #-directive, including how to handle it.  */
31
32 struct directive
33 {
34   directive_handler func;       /* Function to handle directive.  */
35   const U_CHAR *name;           /* Name of directive.  */
36   unsigned short length;        /* Length of name.  */
37   unsigned short flags;         /* Flags describing this directive.  */
38 };
39
40 /* Stack of conditionals currently in progress
41    (including both successful and failing conditionals).  */
42
43 struct if_stack
44 {
45   struct if_stack *next;
46   int lineno;                   /* line number where condition started */
47   int was_skipping;             /* value of pfile->skipping before this if */
48   const cpp_hashnode *cmacro;   /* macro name for #ifndef around entire file */
49   int type;                     /* type of last directive seen in this group */
50 };
51
52 /* Forward declarations.  */
53
54 static void validate_else               PARAMS ((cpp_reader *, const U_CHAR *));
55 static unsigned int parse_include       PARAMS ((cpp_reader *, const U_CHAR *));
56 static void push_conditional            PARAMS ((cpp_reader *, int, int,
57                                                  const cpp_hashnode *));
58 static void pass_thru_directive         PARAMS ((const U_CHAR *, size_t,
59                                                  cpp_reader *, int));
60 static int read_line_number             PARAMS ((cpp_reader *, int *));
61 static const cpp_hashnode *parse_ifdef  PARAMS ((cpp_reader *, const U_CHAR *));
62 static const cpp_hashnode *detect_if_not_defined PARAMS ((cpp_reader *));
63
64 /* Values for the flags field of the table below.  KANDR and COND
65    directives come from traditional (K&R) C.  The difference is, if we
66    care about it while skipping a failed conditional block, its origin
67    is COND.  STDC89 directives come from the 1989 C standard.
68    EXTENSION directives are extensions, with origins noted below.  */
69
70 #define KANDR       0
71 #define COND        1
72 #define STDC89      2
73 #define EXTENSION   3
74
75 #define ORIGIN_MASK 3
76 #define ORIGIN(f) ((f) & ORIGIN_MASK)
77 #define TRAD_DIRECT_P(f) (ORIGIN (f) == KANDR || ORIGIN (f) == COND)
78
79 /* This is the table of directive handlers.  It is ordered by
80    frequency of occurrence; the numbers at the end are directive
81    counts from all the source code I have lying around (egcs and libc
82    CVS as of 1999-05-18, plus grub-0.5.91, linux-2.2.9, and
83    pcmcia-cs-3.0.9).
84
85    The entries with a dash and a name after the count are extensions,
86    of which all but #warning and #include_next are deprecated.  The name
87    is where the extension appears to have come from.  */
88
89 /* #sccs is not always recognized.  */
90 #ifdef SCCS_DIRECTIVE
91 # define SCCS_ENTRY D(sccs, T_SCCS, EXTENSION)          /*     0 - SVR2? */
92 #else
93 # define SCCS_ENTRY /* nothing */
94 #endif
95
96 #define DIRECTIVE_TABLE                                                  \
97 D(define,       T_DEFINE = 0,   KANDR)                     /* 270554 */ \
98 D(include,      T_INCLUDE,      KANDR | SYNTAX_INCLUDE)    /*  52262 */ \
99 D(endif,        T_ENDIF,        COND)                      /*  45855 */ \
100 D(ifdef,        T_IFDEF,        COND)                      /*  22000 */ \
101 D(if,           T_IF,           COND)                      /*  18162 */ \
102 D(else,         T_ELSE,         COND)                       /*  9863 */ \
103 D(ifndef,       T_IFNDEF,       COND)                       /*  9675 */ \
104 D(undef,        T_UNDEF,        KANDR)                      /*  4837 */ \
105 D(line,         T_LINE,         KANDR)                      /*  2465 */ \
106 D(elif,         T_ELIF,         COND)                       /*   610 */ \
107 D(error,        T_ERROR,        STDC89)                     /*   475 */ \
108 D(pragma,       T_PRAGMA,       STDC89)                     /*   195 */ \
109 D(warning,      T_WARNING,      EXTENSION)                  /*    22 GNU */ \
110 D(include_next, T_INCLUDE_NEXT, EXTENSION | SYNTAX_INCLUDE) /*    19 GNU */ \
111 D(ident,        T_IDENT,        EXTENSION)                  /*    11 SVR4 */ \
112 D(import,       T_IMPORT,       EXTENSION | SYNTAX_INCLUDE) /*     0 ObjC */ \
113 D(assert,       T_ASSERT,       EXTENSION)                  /*     0 SVR4 */ \
114 D(unassert,     T_UNASSERT,     EXTENSION)                  /*     0 SVR4 */ \
115 SCCS_ENTRY
116
117 /* Use the table to generate a series of prototypes, an enum for the
118    directive names, and an array of directive handlers.  */
119
120 /* The directive-processing functions are declared to return int
121    instead of void, because some old compilers have trouble with
122    pointers to functions returning void.  */
123
124 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
125 #define D(name, t, f) static int CONCAT2(do_,name) PARAMS ((cpp_reader *));
126 DIRECTIVE_TABLE
127 #undef D
128
129 #define D(n, tag, f) tag,
130 enum
131 {
132   DIRECTIVE_TABLE
133   N_DIRECTIVES
134 };
135 #undef D
136
137 /* Don't invoke CONCAT2 with any whitespace or K&R cc will fail. */
138 #define D(name, t, flags) \
139 { CONCAT2(do_,name), (const U_CHAR *) STRINGX(name), \
140   sizeof STRINGX(name) - 1, flags },
141 static const struct directive dtable[] =
142 {
143 DIRECTIVE_TABLE
144 };
145 #undef D
146 #undef DIRECTIVE_TABLE
147
148 /* Check if a token's name matches that of a known directive.  Put in
149    this file to save exporting dtable and other unneeded information.  */
150 void
151 _cpp_check_directive (list, token)
152      cpp_toklist *list;
153      cpp_token *token;
154 {
155   const U_CHAR *name = token->val.name.text;
156   size_t len = token->val.name.len;
157   unsigned int i;
158
159   list->dirno = -1;
160   list->flags &= ~SYNTAX_INCLUDE;
161
162   for (i = 0; i < N_DIRECTIVES; i++)
163     if (dtable[i].length == len && !ustrncmp (dtable[i].name, name, len)) 
164       {
165         list->dirno = i;
166         if (dtable[i].flags & SYNTAX_INCLUDE)
167           list->flags |= SYNTAX_INCLUDE;
168         break;
169       }
170 }
171
172 /* Handle a possible # directive.
173    '#' has already been read.  */
174
175 int
176 _cpp_handle_directive (pfile)
177      cpp_reader *pfile;
178 {
179   int i;
180   int hash_at_bol;
181   unsigned int len;
182   U_CHAR *ident;
183   long old_written = CPP_WRITTEN (pfile);
184   enum cpp_ttype tok;
185
186   if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
187     {
188       cpp_ice (pfile, "handle_directive called on macro buffer");
189       return 0;
190     }
191
192   /* -traditional directives are recognized only with the # in column 1.  */
193   hash_at_bol = CPP_IN_COLUMN_1 (pfile);
194
195   /* Scan the next token, then pretend we didn't.  */
196   CPP_SET_MARK (pfile);
197   pfile->no_macro_expand++;
198   tok = _cpp_get_directive_token (pfile);
199   pfile->no_macro_expand--;
200
201   ident = pfile->token_buffer + old_written;
202   len = CPP_PWRITTEN (pfile) - ident;
203   CPP_SET_WRITTEN (pfile, old_written);
204   CPP_GOTO_MARK (pfile);
205
206   /* # followed by a number is equivalent to #line.  Do not recognize
207      this form in assembly language source files or skipped
208      conditional groups.  Complain about this form if we're being
209      pedantic, but not if this is regurgitated input (preprocessed or
210      fed back in by the C++ frontend).  */
211   if (tok == CPP_NUMBER)
212     {
213       if (pfile->skipping || CPP_OPTION (pfile, lang_asm))
214         return 0;
215
216       if (CPP_PEDANTIC (pfile)
217           && CPP_BUFFER (pfile)->ihash
218           && ! CPP_OPTION (pfile, preprocessed))
219         cpp_pedwarn (pfile, "# followed by integer");
220       i = T_LINE;
221       goto process_directive;
222     }
223
224   /* If we are rescanning preprocessed input, don't obey any directives
225      other than # nnn.  */
226   else if (CPP_OPTION (pfile, preprocessed))
227     return 0;
228
229   /* A line of just # becomes blank.  */
230   else if (tok == CPP_VSPACE)
231     return 1;
232
233   /* A NAME token might in fact be a directive!  */
234   else if (tok == CPP_NAME)
235     {
236       for (i = 0; i < N_DIRECTIVES; i++)
237         {
238           if (dtable[i].length == len
239               && !ustrncmp (dtable[i].name, ident, len)) 
240             goto real_directive;
241         }
242       /* Don't complain about invalid directives in assembly source,
243          we don't know where the comments are, and # may introduce
244          assembler pseudo-ops.  Don't complain about invalid directives
245          in skipped conditional groups (6.10 p4). */
246       if (!pfile->skipping && !CPP_OPTION (pfile, lang_asm))
247         cpp_error (pfile, "invalid preprocessing directive #%s", ident);
248       return 0;
249     }
250   /* And anything else means the # wasn't a directive marker.   */
251   else
252     return 0;
253
254  real_directive:
255
256   /* If we are skipping a failed conditional group, all non-conditional
257      directives are ignored.  */
258   if (pfile->skipping && ORIGIN (dtable[i].flags) != COND)
259     return 0;
260
261   /* In -traditional mode, a directive is ignored unless its # is in
262      column 1.  */
263   if (CPP_TRADITIONAL (pfile) && !hash_at_bol)
264     {
265       if (CPP_WTRADITIONAL (pfile))
266         cpp_warning (pfile, "ignoring #%s because of its indented #",
267                      dtable[i].name);
268       return 0;
269     }
270
271   /* no_directives is set when we are parsing macro arguments.  Directives
272      in macro arguments are undefined behavior (C99 6.10.3.11); this
273      implementation chooses to make them hard errors.  */
274   if (pfile->no_directives)
275     {
276       cpp_error (pfile, "#%s may not be used inside a macro argument",
277                  dtable[i].name);
278       _cpp_skip_rest_of_line (pfile);
279       return 1;
280     }
281
282   /* Issue -pedantic warnings for extended directives.   */
283   if (CPP_PEDANTIC (pfile) && ORIGIN (dtable[i].flags) == EXTENSION)
284     cpp_pedwarn (pfile, "ISO C does not allow #%s", dtable[i].name);
285
286   /* -Wtraditional gives warnings about directives with inappropriate
287      indentation of #.  */
288   if (CPP_WTRADITIONAL (pfile))
289     {
290       if (!hash_at_bol && TRAD_DIRECT_P (dtable[i].flags))
291         cpp_warning (pfile, "traditional C ignores #%s with the # indented",
292                      dtable[i].name);
293       else if (hash_at_bol && ! TRAD_DIRECT_P (dtable[i].flags))
294         cpp_warning (pfile,
295                 "suggest hiding #%s from traditional C with an indented #",
296                      dtable[i].name);
297     }
298
299   /* Unfortunately, it's necessary to scan the directive name again,
300      now we know we're going to consume it.  FIXME.  */
301
302   pfile->no_macro_expand++;
303   _cpp_get_directive_token (pfile);
304   pfile->no_macro_expand--;
305   CPP_SET_WRITTEN (pfile, old_written);
306
307  process_directive:
308   (void) (*dtable[i].func) (pfile);
309   return 1;
310 }
311
312 /* Pass a directive through to the output file.
313    BUF points to the contents of the directive, as a contiguous string.
314    LEN is the length of the string pointed to by BUF.
315    KEYWORD is the keyword-table entry for the directive.  */
316
317 static void
318 pass_thru_directive (buf, len, pfile, keyword)
319      const U_CHAR *buf;
320      size_t len;
321      cpp_reader *pfile;
322      int keyword;
323 {
324   const struct directive *kt = &dtable[keyword];
325   register unsigned klen = kt->length;
326
327   CPP_RESERVE (pfile, 1 + klen + len);
328   CPP_PUTC_Q (pfile, '#');
329   CPP_PUTS_Q (pfile, kt->name, klen);
330   if (len != 0 && buf[0] != ' ')
331     CPP_PUTC_Q (pfile, ' ');
332   CPP_PUTS_Q (pfile, buf, len);
333 }
334
335 /* Process a #define command.  */
336
337 static int
338 do_define (pfile)
339      cpp_reader *pfile;
340 {
341   cpp_hashnode *node;
342   int len;
343   const U_CHAR *sym;
344   cpp_toklist *list = &pfile->directbuf;
345
346   pfile->no_macro_expand++;
347   CPP_OPTION (pfile, discard_comments)++;
348
349   _cpp_scan_until (pfile, list, CPP_VSPACE);
350
351   /* First token on the line must be a NAME.  There may not be any
352      tokens in the list (if we had #define all by itself on a line).  */
353   if (list->tokens_used == 0
354       || TOK_TYPE (list, 0) != CPP_NAME)
355     {
356       cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
357                            "#define must be followed by an identifier");
358       goto out;
359     }
360
361   sym = TOK_NAME (list, 0);
362   len = TOK_LEN (list, 0);
363
364   /* That NAME is not allowed to be "defined".  (Not clear if the
365      standard requires this.)  */
366   if (len == 7 && !ustrncmp (sym, U"defined", 7))
367     {
368       cpp_error_with_line (pfile, list->line, TOK_COL (list, 0),
369                            "\"defined\" is not a legal macro name");
370       goto out;
371     }
372
373   node = cpp_lookup (pfile, sym, len);
374   /* Check for poisoned identifiers now.  All other checks
375      are done in cpphash.c.  */
376   if (node->type == T_POISON)
377     {
378       cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
379       goto out;
380     }
381     
382   if (_cpp_create_definition (pfile, list, node) == 0)
383     goto out;
384
385   if (CPP_OPTION (pfile, debug_output)
386       || CPP_OPTION (pfile, dump_macros) == dump_definitions)
387     _cpp_dump_definition (pfile, node);
388   else if (CPP_OPTION (pfile, dump_macros) == dump_names)
389     pass_thru_directive (sym, len, pfile, T_DEFINE);
390
391  out:
392   pfile->no_macro_expand--;
393   CPP_OPTION (pfile, discard_comments)--;
394   return 0;
395 }
396
397 /* Handle #include and #import.  */
398
399 static unsigned int
400 parse_include (pfile, name)
401      cpp_reader *pfile;
402      const U_CHAR *name;
403 {
404   long old_written = CPP_WRITTEN (pfile);
405   enum cpp_ttype token;
406   int len;
407
408   pfile->parsing_include_directive++;
409   token = _cpp_get_directive_token (pfile);
410   pfile->parsing_include_directive--;
411
412   len = CPP_WRITTEN (pfile) - old_written;
413
414   if (token != CPP_STRING)
415     {
416       cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", name);
417       CPP_SET_WRITTEN (pfile, old_written);
418       _cpp_skip_rest_of_line (pfile);
419       return 0;
420     }
421
422   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
423     {
424       cpp_error (pfile, "junk at end of #%s", name);
425       _cpp_skip_rest_of_line (pfile);
426     }
427
428   CPP_SET_WRITTEN (pfile, old_written);
429
430   if (len == 0)
431     cpp_error (pfile, "empty file name in #%s", name);
432
433   return len;
434 }
435
436 static int
437 do_include (pfile)
438      cpp_reader *pfile;
439 {
440   unsigned int len;
441   U_CHAR *token;
442
443   len = parse_include (pfile, dtable[T_INCLUDE].name);
444   if (len == 0)
445     return 0;
446   token = (U_CHAR *) alloca (len + 1);
447   memcpy (token, CPP_PWRITTEN (pfile), len);
448   token[len] = '\0';
449   
450   if (CPP_OPTION (pfile, dump_includes))
451     pass_thru_directive (token, len, pfile, T_INCLUDE);
452
453   _cpp_execute_include (pfile, token, len, 0, 0);
454   return 0;
455 }
456
457 static int
458 do_import (pfile)
459      cpp_reader *pfile;
460 {
461   unsigned int len;
462   U_CHAR *token;
463
464   if (CPP_OPTION (pfile, warn_import)
465       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
466     {
467       pfile->import_warning = 1;
468       cpp_warning (pfile,
469            "#import is obsolete, use an #ifndef wrapper in the header file");
470     }
471
472   len = parse_include (pfile, dtable[T_IMPORT].name);
473   if (len == 0)
474     return 0;
475   token = (U_CHAR *) alloca (len + 1);
476   memcpy (token, CPP_PWRITTEN (pfile), len);
477   token[len] = '\0';
478   
479   if (CPP_OPTION (pfile, dump_includes))
480     pass_thru_directive (token, len, pfile, T_IMPORT);
481
482   _cpp_execute_include (pfile, token, len, 1, 0);
483   return 0;
484 }
485
486 static int
487 do_include_next (pfile)
488      cpp_reader *pfile;
489 {
490   unsigned int len;
491   U_CHAR *token;
492   struct file_name_list *search_start = 0;
493
494   len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
495   if (len == 0)
496     return 0;
497   token = (U_CHAR *) alloca (len + 1);
498   memcpy (token, CPP_PWRITTEN (pfile), len);
499   token[len] = '\0';
500   
501   if (CPP_OPTION (pfile, dump_includes))
502     pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
503
504   /* For #include_next, skip in the search path past the dir in which the
505      containing file was found.  Treat files specified using an absolute path
506      as if there are no more directories to search.  Treat the primary source
507      file like any other included source, but generate a warning.  */
508   if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
509     {
510       if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
511         search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
512     }
513   else
514     cpp_warning (pfile, "#include_next in primary source file");
515
516   _cpp_execute_include (pfile, token, len, 0, search_start);
517   return 0;
518 }
519
520 /* Subroutine of do_line.  Read next token from PFILE without adding it to
521    the output buffer.  If it is a number between 1 and 4, store it in *NUM
522    and return 1; otherwise, return 0 and complain if we aren't at the end
523    of the directive.  */
524
525 static int
526 read_line_number (pfile, num)
527      cpp_reader *pfile;
528      int *num;
529 {
530   long save_written = CPP_WRITTEN (pfile);
531   U_CHAR *p;
532   enum cpp_ttype token = _cpp_get_directive_token (pfile);
533   p = pfile->token_buffer + save_written;
534
535   if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
536       && p[0] >= '1' && p[0] <= '4')
537     {
538       *num = p[0] - '0';
539       CPP_SET_WRITTEN (pfile, save_written);
540       return 1;
541     }
542   else
543     {
544       if (token != CPP_VSPACE && token != CPP_EOF)
545         cpp_error (pfile, "invalid format #line");
546       CPP_SET_WRITTEN (pfile, save_written);
547       return 0;
548     }
549 }
550
551 /* Interpret #line command.
552    Note that the filename string (if any) is treated as if it were an
553    include filename.  That means no escape handling.  */
554
555 static int
556 do_line (pfile)
557      cpp_reader *pfile;
558 {
559   cpp_buffer *ip = CPP_BUFFER (pfile);
560   unsigned int new_lineno;
561   long old_written = CPP_WRITTEN (pfile);
562   enum cpp_ttype token;
563   char *x;
564
565   token = _cpp_get_directive_token (pfile);
566
567   if (token != CPP_NUMBER)
568     {
569       cpp_error (pfile, "token after #line is not an integer");
570       goto bad_line_directive;
571     }
572
573   CPP_PUTC (pfile, '\0');  /* not terminated for us */
574   new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
575                         &x, 10);
576   if (x[0] != '\0')
577     {
578       cpp_error (pfile, "token after #line is not an integer");
579       goto bad_line_directive;
580     }      
581   CPP_SET_WRITTEN (pfile, old_written);
582
583   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
584     cpp_pedwarn (pfile, "line number out of range in #line");
585
586   token = _cpp_get_directive_token (pfile);
587
588   if (token == CPP_STRING)
589     {
590       U_CHAR *fname = pfile->token_buffer + old_written + 1;
591       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
592       int action_number = 0;
593
594       if (read_line_number (pfile, &action_number))
595         {
596           if (CPP_PEDANTIC (pfile))
597             cpp_pedwarn (pfile, "garbage at end of #line");
598
599           /* This is somewhat questionable: change the buffer stack
600              depth so that output_line_command thinks we've stacked
601              another buffer. */
602           if (action_number == 1)
603             {
604               pfile->buffer_stack_depth++;
605               ip->system_header_p = 0;
606               read_line_number (pfile, &action_number);
607             }
608           else if (action_number == 2)
609             {
610               pfile->buffer_stack_depth--;
611               ip->system_header_p = 0;
612               read_line_number (pfile, &action_number);
613             }
614           if (action_number == 3)
615             {
616               ip->system_header_p = 1;
617               read_line_number (pfile, &action_number);
618             }
619           if (action_number == 4)
620             {
621               ip->system_header_p = 2;
622               read_line_number (pfile, &action_number);
623             }
624         }
625       
626       *end_name = '\0';
627       
628       if (strcmp ((const char *)fname, ip->nominal_fname))
629         {
630           if (!strcmp ((const char *)fname, ip->ihash->name))
631             ip->nominal_fname = ip->ihash->name;
632           else
633             ip->nominal_fname = _cpp_fake_ihash (pfile, (const char *)fname);
634         }
635     }
636   else if (token != CPP_VSPACE && token != CPP_EOF)
637     {
638       cpp_error (pfile, "second token after #line is not a string");
639       goto bad_line_directive;
640     }
641
642   /* The Newline at the end of this line remains to be processed.
643      To put the next line at the specified line number,
644      we must store a line number now that is one less.  */
645   ip->lineno = new_lineno - 1;
646   CPP_SET_WRITTEN (pfile, old_written);
647   return 0;
648
649  bad_line_directive:
650   _cpp_skip_rest_of_line (pfile);
651   CPP_SET_WRITTEN (pfile, old_written);
652   return 0;
653 }
654
655 /* Remove the definition of a symbol from the symbol table.
656    According to the C standard, it is not an error to undef
657    something that has no definitions. */
658 static int
659 do_undef (pfile)
660      cpp_reader *pfile;
661 {
662   int len;
663   cpp_hashnode *hp;
664   U_CHAR *name;
665   long here = CPP_WRITTEN (pfile);
666   enum cpp_ttype token;
667
668   pfile->no_macro_expand++;
669   token = _cpp_get_directive_token (pfile);
670   pfile->no_macro_expand--;
671
672   if (token != CPP_NAME)
673     {
674       cpp_error (pfile, "token after #undef is not an identifier");
675       _cpp_skip_rest_of_line (pfile);
676       return 0;
677     }
678   len = CPP_WRITTEN (pfile) - here;
679
680   token = _cpp_get_directive_token (pfile);
681   if (token != CPP_VSPACE)
682   {
683       cpp_pedwarn (pfile, "junk on line after #undef");
684       _cpp_skip_rest_of_line (pfile);
685   }
686
687   name = pfile->token_buffer + here;
688   CPP_SET_WRITTEN (pfile, here);
689
690   hp = cpp_lookup (pfile, name, len);
691   if (hp->type == T_VOID)
692     ; /* Not defined in the first place - do nothing.  */
693   else if (hp->type == T_POISON)
694     cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
695   else
696     {
697       /* If we are generating additional info for debugging (with -g) we
698          need to pass through all effective #undef commands.  */
699       if (CPP_OPTION (pfile, debug_output))
700         pass_thru_directive (hp->name, len, pfile, T_UNDEF);
701
702       if (hp->type != T_MACRO && hp->type != T_FMACRO
703           && hp->type != T_EMPTY && hp->type != T_IDENTITY)
704         cpp_warning (pfile, "undefining `%s'", hp->name);
705
706       _cpp_free_definition (hp);
707       hp->type = T_VOID;
708     }
709
710   return 0;
711 }
712
713 /*
714  * Report an error detected by the program we are processing.
715  * Use the text of the line in the error message.
716  * (We use error because it prints the filename & line#.)
717  */
718
719 static int
720 do_error (pfile)
721      cpp_reader *pfile;
722 {
723   const U_CHAR *text, *limit;
724
725   _cpp_skip_hspace (pfile);
726   text = CPP_BUFFER (pfile)->cur;
727   _cpp_skip_rest_of_line (pfile);
728   limit = CPP_BUFFER (pfile)->cur;
729
730   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
731   return 0;
732 }
733
734 /*
735  * Report a warning detected by the program we are processing.
736  * Use the text of the line in the warning message, then continue.
737  */
738
739 static int
740 do_warning (pfile)
741      cpp_reader *pfile;
742 {
743   const U_CHAR *text, *limit;
744
745   _cpp_skip_hspace (pfile);
746   text = CPP_BUFFER (pfile)->cur;
747   _cpp_skip_rest_of_line (pfile);
748   limit = CPP_BUFFER (pfile)->cur;
749
750   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
751   return 0;
752 }
753
754 /* Report program identification.  */
755
756 static int
757 do_ident (pfile)
758      cpp_reader *pfile;
759 {
760   long old_written = CPP_WRITTEN (pfile);
761
762   CPP_PUTS (pfile, "#ident ", 7);
763
764   /* Next token should be a string constant.  */
765   if (_cpp_get_directive_token (pfile) == CPP_STRING)
766     /* And then a newline.  */
767     if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
768       /* Good - ship it.  */
769       return 0;
770
771   cpp_error (pfile, "invalid #ident");
772   _cpp_skip_rest_of_line (pfile);
773   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
774
775   return 0;
776 }
777
778 /* Pragmata handling.  We handle some of these, and pass the rest on
779    to the front end.  C99 defines three pragmas and says that no macro
780    expansion is to be performed on them; whether or not macro
781    expansion happens for other pragmas is implementation defined.
782    This implementation never macro-expands the text after #pragma.
783
784    We currently do not support the _Pragma operator.  Support for that
785    has to be coordinated with the front end.  Proposed implementation:
786    both #pragma blah blah and _Pragma("blah blah") become
787    __builtin_pragma(blah blah) and we teach the parser about that.  */
788
789 /* Sub-handlers for the pragmas needing treatment here.
790    They return 1 if the token buffer is to be popped, 0 if not. */
791 static int do_pragma_once               PARAMS ((cpp_reader *));
792 static int do_pragma_implementation     PARAMS ((cpp_reader *));
793 static int do_pragma_poison             PARAMS ((cpp_reader *));
794 static int do_pragma_system_header      PARAMS ((cpp_reader *));
795 static int do_pragma_default            PARAMS ((cpp_reader *));
796
797 static int
798 do_pragma (pfile)
799      cpp_reader *pfile;
800 {
801   long here, key;
802   U_CHAR *buf;
803   int pop;
804   enum cpp_ttype token;
805
806   here = CPP_WRITTEN (pfile);
807   CPP_PUTS (pfile, "#pragma ", 8);
808
809   key = CPP_WRITTEN (pfile);
810   pfile->no_macro_expand++;
811   token = _cpp_get_directive_token (pfile);
812   if (token != CPP_NAME)
813     {
814       if (token == CPP_VSPACE)
815         goto empty;
816       else
817         goto skip;
818     }
819
820   buf = pfile->token_buffer + key;
821   CPP_PUTC (pfile, ' ');
822
823 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
824   if (tokis ("once"))
825     pop = do_pragma_once (pfile);
826   else if (tokis ("implementation"))
827     pop = do_pragma_implementation (pfile);
828   else if (tokis ("poison"))
829     pop = do_pragma_poison (pfile);
830   else if (tokis ("system_header"))
831     pop = do_pragma_system_header (pfile);
832   else
833     pop = do_pragma_default (pfile);
834 #undef tokis
835
836   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
837     goto skip;
838
839   if (pop)
840     CPP_SET_WRITTEN (pfile, here);
841   pfile->no_macro_expand--;
842   return 0;
843
844  skip:
845   cpp_error (pfile, "malformed #pragma directive");
846   _cpp_skip_rest_of_line (pfile);
847  empty:
848   CPP_SET_WRITTEN (pfile, here);
849   pfile->no_macro_expand--;
850   return 0;
851 }
852
853 static int
854 do_pragma_default (pfile)
855      cpp_reader *pfile;
856 {
857   while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
858     CPP_PUTC (pfile, ' ');
859   return 0;
860 }
861
862 static int
863 do_pragma_once (pfile)
864      cpp_reader *pfile;
865 {
866   cpp_buffer *ip = CPP_BUFFER (pfile);
867
868   /* Allow #pragma once in system headers, since that's not the user's
869      fault.  */
870   if (!ip->system_header_p)
871     cpp_warning (pfile, "#pragma once is obsolete");
872       
873   if (CPP_PREV_BUFFER (ip) == NULL)
874     cpp_warning (pfile, "#pragma once outside include file");
875   else
876     ip->ihash->cmacro = NEVER_REINCLUDE;
877
878   return 1;
879 }
880
881 static int
882 do_pragma_implementation (pfile)
883      cpp_reader *pfile;
884 {
885   /* Be quiet about `#pragma implementation' for a file only if it hasn't
886      been included yet.  */
887   enum cpp_ttype token;
888   long written = CPP_WRITTEN (pfile);
889   U_CHAR *name;
890   char *copy;
891   size_t len;
892
893   token = _cpp_get_directive_token (pfile);
894   if (token == CPP_VSPACE)
895     return 0;
896   else if (token != CPP_STRING)
897     {
898       cpp_error (pfile, "malformed #pragma implementation");
899       return 1;
900     }
901
902   /* Trim the leading and trailing quote marks from the string.  */
903   name = pfile->token_buffer + written + 1;
904   len = CPP_PWRITTEN (pfile) - name;
905   copy = alloca (len);
906   memcpy (copy, name, len - 1);
907   copy[len - 1] = '\0';
908   
909   if (cpp_included (pfile, copy))
910     cpp_warning (pfile,
911          "#pragma implementation for %s appears after file is included",
912                  copy);
913   return 0;
914 }
915
916 static int
917 do_pragma_poison (pfile)
918      cpp_reader *pfile;
919 {
920   /* Poison these symbols so that all subsequent usage produces an
921      error message.  */
922   U_CHAR *p;
923   cpp_hashnode *hp;
924   long written;
925   size_t len;
926   enum cpp_ttype token;
927   int writeit;
928
929   /* As a rule, don't include #pragma poison commands in output,  
930      unless the user asks for them.  */
931   writeit = (CPP_OPTION (pfile, debug_output)
932              || CPP_OPTION (pfile, dump_macros) == dump_definitions
933              || CPP_OPTION (pfile, dump_macros) == dump_names);
934
935   for (;;)
936     {
937       written = CPP_WRITTEN (pfile);
938       token = _cpp_get_directive_token (pfile);
939       if (token == CPP_VSPACE)
940         break;
941       if (token != CPP_NAME)
942         {
943           cpp_error (pfile, "invalid #pragma poison directive");
944           _cpp_skip_rest_of_line (pfile);
945           return 1;
946         }
947
948       p = pfile->token_buffer + written;
949       len = CPP_PWRITTEN (pfile) - p;
950       hp = cpp_lookup (pfile, p, len);
951       if (hp->type == T_POISON)
952         ;  /* It is allowed to poison the same identifier twice.  */
953       else
954         {
955           if (hp->type != T_VOID)
956             cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
957           _cpp_free_definition (hp);
958           hp->type = T_POISON;
959         }
960       if (writeit)
961         CPP_PUTC (pfile, ' ');
962     }
963   return !writeit;
964 }
965
966 /* Mark the current header as a system header.  This will suppress
967    some categories of warnings (notably those from -pedantic).  It is
968    intended for use in system libraries that cannot be implemented in
969    conforming C, but cannot be certain that their headers appear in a
970    system include directory.  To prevent abuse, it is rejected in the
971    primary source file.  */
972 static int
973 do_pragma_system_header (pfile)
974      cpp_reader *pfile;
975 {
976   cpp_buffer *ip = cpp_file_buffer (pfile);
977   if (CPP_PREV_BUFFER (ip) == NULL)
978     cpp_warning (pfile, "#pragma system_header outside include file");
979   else
980     ip->system_header_p = 1;
981
982   return 1;
983 }
984  
985 /* Just ignore #sccs, on systems where we define it at all.  */
986 #ifdef SCCS_DIRECTIVE
987 static int
988 do_sccs (pfile)
989      cpp_reader *pfile;
990 {
991   _cpp_skip_rest_of_line (pfile);
992   return 0;
993 }
994 #endif
995
996 /* We've found an `#if' directive.  If the only thing before it in
997    this file is white space, and if it is of the form
998    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
999    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1000    for an explanation of controlling macros.)  If so, return the
1001    hash node for SYMBOL.  Otherwise, return NULL.  */
1002
1003 static const cpp_hashnode *
1004 detect_if_not_defined (pfile)
1005      cpp_reader *pfile;
1006 {
1007   const cpp_hashnode *cmacro = 0;
1008   enum cpp_ttype token;
1009   unsigned int base_offset;
1010   unsigned int token_offset;
1011   unsigned int need_rparen = 0;
1012   unsigned int token_len;
1013
1014   if (pfile->skipping || pfile->only_seen_white != 2)
1015     return NULL;
1016
1017   /* Save state required for restore.  */
1018   pfile->no_macro_expand++;
1019   CPP_SET_MARK (pfile);
1020   base_offset = CPP_WRITTEN (pfile);
1021
1022   /* Look for `!', */
1023   if (_cpp_get_directive_token (pfile) != CPP_OTHER
1024       || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1025       || CPP_PWRITTEN (pfile)[-1] != '!')
1026     goto restore;
1027
1028   /* ...then `defined', */
1029   token_offset = CPP_WRITTEN (pfile);
1030   token = _cpp_get_directive_token (pfile);
1031   if (token != CPP_NAME)
1032     goto restore;
1033   if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1034     goto restore;
1035
1036   /* ...then an optional '(' and the name, */
1037   token_offset = CPP_WRITTEN (pfile);
1038   token = _cpp_get_directive_token (pfile);
1039   if (token == CPP_OPEN_PAREN)
1040     {
1041       token_offset = CPP_WRITTEN (pfile);
1042       need_rparen = 1;
1043       token = _cpp_get_directive_token (pfile);
1044     }
1045   if (token != CPP_NAME)
1046     goto restore;
1047
1048   token_len = CPP_WRITTEN (pfile) - token_offset;
1049
1050   /* ...then the ')', if necessary, */
1051   if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1052     goto restore;
1053
1054   /* ...and make sure there's nothing else on the line.  */
1055   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1056     goto restore;
1057
1058   /* We have a legitimate controlling macro for this header.  */
1059   cmacro = cpp_lookup (pfile, pfile->token_buffer + token_offset, token_len);
1060
1061  restore:
1062   CPP_SET_WRITTEN (pfile, base_offset);
1063   pfile->no_macro_expand--;
1064   CPP_GOTO_MARK (pfile);
1065
1066   return cmacro;
1067 }
1068
1069 /* Parse an #ifdef or #ifndef directive.  Returns 1 for defined, 0 for
1070    not defined; the macro tested is left in the token buffer (but
1071    popped).  */
1072
1073 static const cpp_hashnode *
1074 parse_ifdef (pfile, name)
1075      cpp_reader *pfile;
1076      const U_CHAR *name;
1077 {
1078   U_CHAR *ident;
1079   unsigned int len;
1080   enum cpp_ttype token;
1081   long old_written = CPP_WRITTEN (pfile);
1082   const cpp_hashnode *node = 0;
1083
1084   pfile->no_macro_expand++;
1085   token = _cpp_get_directive_token (pfile);
1086   pfile->no_macro_expand--;
1087
1088   ident = pfile->token_buffer + old_written;
1089   len = CPP_WRITTEN (pfile) - old_written;
1090
1091   if (token == CPP_VSPACE)
1092     {
1093       if (! CPP_TRADITIONAL (pfile))
1094         cpp_pedwarn (pfile, "#%s with no argument", name);
1095       goto done;
1096     }
1097   else if (token == CPP_NAME)
1098     {
1099       node = cpp_lookup (pfile, ident, len);
1100     }
1101   else
1102     {
1103       if (! CPP_TRADITIONAL (pfile))
1104         cpp_error (pfile, "#%s with invalid argument", name);
1105     }
1106
1107   if (!CPP_TRADITIONAL (pfile))
1108     {
1109       if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1110         goto done;
1111       
1112       cpp_pedwarn (pfile, "garbage at end of #%s", name);
1113     }
1114   _cpp_skip_rest_of_line (pfile);
1115   
1116  done:
1117   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1118   return node;
1119 }
1120
1121 /* #ifdef is dead simple.  */
1122
1123 static int
1124 do_ifdef (pfile)
1125      cpp_reader *pfile;
1126 {
1127   int def = 0;
1128   const cpp_hashnode *node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1129   if (node)
1130     {
1131       if (node->type == T_POISON)
1132         cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1133       else
1134         def = (node->type != T_VOID);
1135     }
1136   push_conditional (pfile, !def, T_IFDEF, 0);
1137   return 0;
1138 }
1139
1140 /* #ifndef is a tad more complex, because we need to check for a
1141    no-reinclusion wrapper.  */
1142
1143 static int
1144 do_ifndef (pfile)
1145      cpp_reader *pfile;
1146 {
1147   int start_of_file;
1148   int def = 0;
1149   const cpp_hashnode *cmacro;
1150
1151   start_of_file = pfile->only_seen_white == 2;
1152   cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1153   if (cmacro)
1154     {
1155       if (cmacro->type == T_POISON)
1156         cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1157       else
1158         def = (cmacro->type != T_VOID);
1159     }
1160   push_conditional (pfile, def, T_IFNDEF,
1161                     start_of_file ? cmacro : 0);
1162   return 0;
1163 }
1164
1165 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1166    Also, check for a reinclude preventer of the form #if !defined (MACRO).  */
1167
1168 static int
1169 do_if (pfile)
1170      cpp_reader *pfile;
1171 {
1172   const cpp_hashnode *cmacro = 0;
1173   int value = 0;
1174
1175   if (! pfile->skipping)
1176     {
1177       cmacro = detect_if_not_defined (pfile);  
1178       value = _cpp_parse_expr (pfile);
1179     }
1180   push_conditional (pfile, value == 0, T_IF, cmacro);
1181   return 0;
1182 }
1183
1184 /* #else flips pfile->skipping and continues without changing
1185    if_stack; this is so that the error message for missing #endif's
1186    etc. will point to the original #if.  */
1187
1188 static int
1189 do_else (pfile)
1190      cpp_reader *pfile;
1191 {
1192   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1193
1194   validate_else (pfile, dtable[T_ELSE].name);
1195
1196   if (ifs == NULL)
1197     {
1198       cpp_error (pfile, "#else without #if");
1199       return 0;
1200     }
1201   if (ifs->type == T_ELSE)
1202     {
1203       cpp_error (pfile, "#else after #else");
1204       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1205     }
1206
1207   /* #ifndef can't have its special treatment for containing the whole file
1208      if it has a #else clause.  */
1209   ifs->cmacro = 0;
1210
1211   ifs->type = T_ELSE;
1212   if (! ifs->was_skipping)
1213     {
1214       /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1215          succeeded, so we mustn't do the else block.  */
1216       if (pfile->skipping < 2)
1217         pfile->skipping = ! pfile->skipping;
1218     }
1219   return 0;
1220 }
1221
1222 /*
1223  * handle a #elif directive by not changing if_stack either.
1224  * see the comment above do_else.
1225  */
1226
1227 static int
1228 do_elif (pfile)
1229      cpp_reader *pfile;
1230 {
1231   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1232
1233   if (ifs == NULL)
1234     {
1235       cpp_error (pfile, "#elif without #if");
1236       return 0;
1237     }
1238   if (ifs->type == T_ELSE)
1239     {
1240       cpp_error (pfile, "#elif after #else");
1241       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1242     }
1243
1244   ifs->type = T_ELIF;
1245   if (ifs->was_skipping)
1246     _cpp_skip_rest_of_line (pfile);
1247   else if (pfile->skipping != 1)
1248     {
1249       _cpp_skip_rest_of_line (pfile);
1250       pfile->skipping = 2;  /* one block succeeded, so don't do any others */
1251     }
1252   else
1253     pfile->skipping = ! _cpp_parse_expr (pfile);
1254
1255   return 0;
1256 }
1257
1258
1259 /* #endif pops the if stack and resets pfile->skipping.  */
1260
1261 static int
1262 do_endif (pfile)
1263      cpp_reader *pfile;
1264 {
1265   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1266
1267   validate_else (pfile, dtable[T_ENDIF].name);
1268
1269   if (ifs == NULL)
1270     cpp_error (pfile, "#endif without #if");
1271   else
1272     {
1273       CPP_BUFFER (pfile)->if_stack = ifs->next;
1274       pfile->skipping = ifs->was_skipping;
1275       pfile->potential_control_macro = ifs->cmacro;
1276       free (ifs);
1277     }
1278   return 0;
1279 }
1280
1281 /* Push an if_stack entry and set pfile->skipping accordingly.
1282    If this is a #ifndef starting at the beginning of a file,
1283    CMACRO is the macro name tested by the #ifndef.  */
1284
1285 static void
1286 push_conditional (pfile, skip, type, cmacro)
1287      cpp_reader *pfile;
1288      int skip;
1289      int type;
1290      const cpp_hashnode *cmacro;
1291 {
1292   struct if_stack *ifs;
1293
1294   ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1295   ifs->lineno = CPP_BUFFER (pfile)->lineno;
1296   ifs->next = CPP_BUFFER (pfile)->if_stack;
1297   ifs->cmacro = cmacro;
1298   ifs->was_skipping = pfile->skipping;
1299   ifs->type = type;
1300
1301   if (!pfile->skipping)
1302     pfile->skipping = skip;
1303
1304   CPP_BUFFER (pfile)->if_stack = ifs;
1305 }
1306
1307 /* Issue -pedantic warning for text which is not a comment following
1308    an #else or #endif.  */
1309
1310 static void
1311 validate_else (pfile, directive)
1312      cpp_reader *pfile;
1313      const U_CHAR *directive;
1314 {
1315   if (CPP_PEDANTIC (pfile))
1316     {
1317       long old_written = CPP_WRITTEN (pfile);
1318       pfile->no_macro_expand++;
1319       if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1320         cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1321       CPP_SET_WRITTEN (pfile, old_written);
1322       pfile->no_macro_expand--;
1323     }
1324   _cpp_skip_rest_of_line (pfile);
1325 }
1326
1327 /* Called when we reach the end of a macro buffer.  Walk back up the
1328    conditional stack till we reach its level at entry to this file,
1329    issuing error messages.  Then force skipping off.  */
1330 void
1331 _cpp_unwind_if_stack (pfile, pbuf)
1332      cpp_reader *pfile;
1333      cpp_buffer *pbuf;
1334 {
1335   struct if_stack *ifs, *nifs;
1336
1337   for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1338     {
1339       cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1340                            dtable[ifs->type].name);
1341       nifs = ifs->next;
1342       free (ifs);
1343     }
1344   pfile->skipping = 0;
1345 }
1346
1347 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1348 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1349 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1350 static int
1351 do_assert (pfile)
1352      cpp_reader *pfile;
1353 {
1354   long old_written;
1355   U_CHAR *sym;
1356   size_t len;
1357   cpp_hashnode *hp;
1358   struct predicate *pred = 0;
1359   enum cpp_ttype type;
1360
1361   old_written = CPP_WRITTEN (pfile);
1362   pfile->no_macro_expand++;
1363
1364   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1365   type = _cpp_get_directive_token (pfile);
1366   if (type == CPP_VSPACE)
1367     ERROR ("#assert without predicate");
1368   else if (type != CPP_NAME)
1369     ERROR ("assertion predicate is not an identifier");
1370
1371   sym = pfile->token_buffer + old_written;
1372   len = CPP_WRITTEN (pfile) - old_written;
1373   hp = cpp_lookup (pfile, sym, len);
1374
1375   if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1376     ERROR ("missing token-sequence in #assert");
1377
1378   pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1379   _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1380
1381   if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1382       != CPP_CLOSE_PAREN)
1383     ERROR ("missing close paren in #assert");
1384
1385   if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1386     ICE ("impossible token, expecting ) in do_assert");
1387
1388   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1389     ERROR ("junk at end of #assert");
1390
1391   if (hp->type == T_ASSERTION)
1392     {
1393       /* Check for reassertion.  */
1394       const struct predicate *old;
1395
1396       for (old = hp->value.pred; old; old = old->next)
1397         if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1398           /* We used to warn about this, but SVR4 cc doesn't, so let's
1399              match that (also consistent with #define).  goto error will
1400              clean up.  */
1401           goto error;
1402       pred->next = hp->value.pred;
1403     }
1404   else
1405     {
1406       hp->type = T_ASSERTION;
1407       pred->next = 0;
1408     }
1409   
1410   _cpp_squeeze_toklist (&pred->answer);
1411   hp->value.pred = pred;
1412   pfile->no_macro_expand--;
1413   CPP_SET_WRITTEN (pfile, old_written);
1414   return 0;
1415
1416  error:
1417   _cpp_skip_rest_of_line (pfile);
1418   pfile->no_macro_expand--;
1419   CPP_SET_WRITTEN (pfile, old_written);
1420   if (pred)
1421     {
1422       _cpp_free_toklist (&pred->answer);
1423       free (pred);
1424     }
1425   return 0;
1426 }
1427
1428 static int
1429 do_unassert (pfile)
1430      cpp_reader *pfile;
1431 {
1432   long old_written;
1433   U_CHAR *sym;
1434   size_t len;
1435   cpp_hashnode *hp;
1436   cpp_toklist ans;
1437   enum cpp_ttype type;
1438   int specific = 0;
1439
1440   old_written = CPP_WRITTEN (pfile);
1441   pfile->no_macro_expand++;
1442
1443   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1444   if (_cpp_get_directive_token (pfile) != CPP_NAME)
1445     ERROR ("#unassert must be followed by an identifier");
1446
1447   sym = pfile->token_buffer + old_written;
1448   len = CPP_WRITTEN (pfile) - old_written;
1449   hp = cpp_lookup (pfile, sym, len);
1450
1451   type = _cpp_get_directive_token (pfile);
1452   if (type == CPP_OPEN_PAREN)
1453     {
1454       specific = 1;
1455       _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1456
1457       if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1458           != CPP_CLOSE_PAREN)
1459         ERROR ("missing close paren in #unassert");
1460
1461       if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1462         ICE ("impossible token, expecting ) in do_unassert");
1463
1464       type = _cpp_get_directive_token (pfile);
1465     }
1466
1467   if (type != CPP_VSPACE)
1468     ERROR ("junk at end of #unassert");
1469
1470   if (hp->type != T_ASSERTION)
1471     /* Not an error to #unassert something that isn't asserted.
1472        goto error to clean up.  */
1473     goto error;
1474
1475   if (specific)
1476     {
1477       /* Find this specific answer and remove it.  */
1478       struct predicate *o, *p;
1479
1480       for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1481         if (_cpp_equiv_toklists (&ans, &o->answer))
1482           {
1483             if (p)
1484               p->next = o->next;
1485             else
1486               hp->value.pred = o->next;
1487
1488             _cpp_free_toklist (&o->answer);
1489             free (o);
1490             break;
1491           }
1492     }
1493   else
1494     {
1495       struct predicate *o, *p;
1496       for (o = hp->value.pred; o; o = p)
1497         {
1498           p = o->next;
1499           _cpp_free_toklist ((cpp_toklist *) &o->answer);
1500           free (o);
1501         }
1502       hp->value.pred = NULL;
1503     }
1504
1505   if (hp->value.pred == NULL)
1506     hp->type = T_VOID;  /* Last answer for this predicate deleted.  */
1507
1508  error:
1509   _cpp_skip_rest_of_line (pfile);
1510   pfile->no_macro_expand--;
1511   CPP_SET_WRITTEN (pfile, old_written);
1512   if (specific)
1513     _cpp_free_toklist (&ans);
1514   return 0;
1515 }
1516
1517 /* These are for -D, -U, -A.  */
1518
1519 /* Process the string STR as if it appeared as the body of a #define.
1520    If STR is just an identifier, define it with value 1.
1521    If STR has anything after the identifier, then it should
1522    be identifier=definition. */
1523
1524 void
1525 cpp_define (pfile, str)
1526      cpp_reader *pfile;
1527      const char *str;
1528 {
1529   char *buf, *p;
1530   size_t count;
1531
1532   p = strchr (str, '=');
1533   /* Copy the entire option so we can modify it. 
1534      Change the first "=" in the string to a space.  If there is none,
1535      tack " 1" on the end.  Then add a newline and a NUL.  */
1536   
1537   if (p)
1538     {
1539       count = strlen (str) + 2;
1540       buf = (char *) alloca (count);
1541       memcpy (buf, str, count - 2);
1542       buf[p - str] = ' ';
1543       buf[count - 2] = '\n';
1544       buf[count - 1] = '\0';
1545     }
1546   else
1547     {
1548       count = strlen (str) + 4;
1549       buf = (char *) alloca (count);
1550       memcpy (buf, str, count - 4);
1551       strcpy (&buf[count-4], " 1\n");
1552     }
1553
1554   if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1555     {
1556       do_define (pfile);
1557       cpp_pop_buffer (pfile);
1558     }
1559 }
1560
1561 /* Process MACRO as if it appeared as the body of an #undef.  */
1562 void
1563 cpp_undef (pfile, macro)
1564      cpp_reader *pfile;
1565      const char *macro;
1566 {
1567   /* Copy the string so we can append a newline.  */
1568   size_t len = strlen (macro);
1569   char *buf = (char *) alloca (len + 2);
1570   memcpy (buf, macro, len);
1571   buf[len]     = '\n';
1572   buf[len + 1] = '\0';
1573   if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1574     {
1575       do_undef (pfile);
1576       cpp_pop_buffer (pfile);
1577     }
1578 }
1579
1580 /* Process the string STR as if it appeared as the body of a #assert. */
1581 void
1582 cpp_assert (pfile, str)
1583      cpp_reader *pfile;
1584      const char *str;
1585 {
1586   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1587     {
1588       do_assert (pfile);
1589       cpp_pop_buffer (pfile);
1590     }
1591 }
1592
1593 /* Process STR as if it appeared as the body of an #unassert. */
1594 void
1595 cpp_unassert (pfile, str)
1596      cpp_reader *pfile;
1597      const char *str;
1598 {
1599   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1600     {
1601       do_unassert (pfile);
1602       cpp_pop_buffer (pfile);
1603     }
1604 }  
1605
1606 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1607 int
1608 cpp_defined (pfile, id, len)
1609      cpp_reader *pfile;
1610      const U_CHAR *id;
1611      int len;
1612 {
1613   cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1614   if (hp->type == T_POISON)
1615     {
1616       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1617       return 0;
1618     }
1619   return (hp->type != T_VOID);
1620 }