OSDN Git Service

* cpplib.c (do_if): Don't save and restore only_seen_white here.
[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     ; /* No special treatment required.  */
416 #ifdef VMS
417   else if (token == CPP_NAME)
418     {
419       /* Support '#include xyz' like VAX-C.  It is taken as
420          '#include <xyz.h>' and generates a warning.  */
421       cpp_warning (pfile, "#%s filename is obsolete, use #%s <filename.h>",
422                    name, name);
423
424       /* Rewrite the token to <xyz.h>.  */
425       CPP_RESERVE (pfile, 4);
426       len += 4;
427       memmove (pfile->token_buffer + old_written + 1,
428                pfile->token_buffer + old_written,
429                CPP_WRITTEN (pfile) - old_written);
430       pfile->token_buffer[old_written] = '<';
431       CPP_PUTS_Q (pfile, ".h>", 2);
432     }
433 #endif
434   else
435     {
436       cpp_error (pfile, "#%s expects \"FILENAME\" or <FILENAME>", name);
437       CPP_SET_WRITTEN (pfile, old_written);
438       _cpp_skip_rest_of_line (pfile);
439       return 0;
440     }
441
442   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
443     {
444       cpp_error (pfile, "junk at end of #%s", name);
445       _cpp_skip_rest_of_line (pfile);
446     }
447
448   CPP_SET_WRITTEN (pfile, old_written);
449
450   if (len == 0)
451     cpp_error (pfile, "empty file name in #%s", name);
452
453   return len;
454 }
455
456 static int
457 do_include (pfile)
458      cpp_reader *pfile;
459 {
460   unsigned int len;
461   U_CHAR *token;
462
463   len = parse_include (pfile, dtable[T_INCLUDE].name);
464   if (len == 0)
465     return 0;
466   token = (U_CHAR *) alloca (len + 1);
467   memcpy (token, CPP_PWRITTEN (pfile), len);
468   token[len] = '\0';
469   
470   if (CPP_OPTION (pfile, dump_includes))
471     pass_thru_directive (token, len, pfile, T_INCLUDE);
472
473   _cpp_execute_include (pfile, token, len, 0, 0);
474   return 0;
475 }
476
477 static int
478 do_import (pfile)
479      cpp_reader *pfile;
480 {
481   unsigned int len;
482   U_CHAR *token;
483
484   if (CPP_OPTION (pfile, warn_import)
485       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
486     {
487       pfile->import_warning = 1;
488       cpp_warning (pfile,
489            "#import is obsolete, use an #ifndef wrapper in the header file");
490     }
491
492   len = parse_include (pfile, dtable[T_IMPORT].name);
493   if (len == 0)
494     return 0;
495   token = (U_CHAR *) alloca (len + 1);
496   memcpy (token, CPP_PWRITTEN (pfile), len);
497   token[len] = '\0';
498   
499   if (CPP_OPTION (pfile, dump_includes))
500     pass_thru_directive (token, len, pfile, T_IMPORT);
501
502   _cpp_execute_include (pfile, token, len, 1, 0);
503   return 0;
504 }
505
506 static int
507 do_include_next (pfile)
508      cpp_reader *pfile;
509 {
510   unsigned int len;
511   U_CHAR *token;
512   struct file_name_list *search_start = 0;
513
514   len = parse_include (pfile, dtable[T_INCLUDE_NEXT].name);
515   if (len == 0)
516     return 0;
517   token = (U_CHAR *) alloca (len + 1);
518   memcpy (token, CPP_PWRITTEN (pfile), len);
519   token[len] = '\0';
520   
521   if (CPP_OPTION (pfile, dump_includes))
522     pass_thru_directive (token, len, pfile, T_INCLUDE_NEXT);
523
524   /* For #include_next, skip in the search path past the dir in which the
525      containing file was found.  Treat files specified using an absolute path
526      as if there are no more directories to search.  Treat the primary source
527      file like any other included source, but generate a warning.  */
528   if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)))
529     {
530       if (CPP_BUFFER (pfile)->ihash->foundhere != ABSOLUTE_PATH)
531         search_start = CPP_BUFFER (pfile)->ihash->foundhere->next;
532     }
533   else
534     cpp_warning (pfile, "#include_next in primary source file");
535
536   _cpp_execute_include (pfile, token, len, 0, search_start);
537   return 0;
538 }
539
540 /* Subroutine of do_line.  Read next token from PFILE without adding it to
541    the output buffer.  If it is a number between 1 and 4, store it in *NUM
542    and return 1; otherwise, return 0 and complain if we aren't at the end
543    of the directive.  */
544
545 static int
546 read_line_number (pfile, num)
547      cpp_reader *pfile;
548      int *num;
549 {
550   long save_written = CPP_WRITTEN (pfile);
551   U_CHAR *p;
552   enum cpp_ttype token = _cpp_get_directive_token (pfile);
553   p = pfile->token_buffer + save_written;
554
555   if (token == CPP_NUMBER && p + 1 == CPP_PWRITTEN (pfile)
556       && p[0] >= '1' && p[0] <= '4')
557     {
558       *num = p[0] - '0';
559       CPP_SET_WRITTEN (pfile, save_written);
560       return 1;
561     }
562   else
563     {
564       if (token != CPP_VSPACE && token != CPP_EOF)
565         cpp_error (pfile, "invalid format #line");
566       CPP_SET_WRITTEN (pfile, save_written);
567       return 0;
568     }
569 }
570
571 /* Interpret #line command.
572    Note that the filename string (if any) is treated as if it were an
573    include filename.  That means no escape handling.  */
574
575 static int
576 do_line (pfile)
577      cpp_reader *pfile;
578 {
579   cpp_buffer *ip = CPP_BUFFER (pfile);
580   unsigned int new_lineno;
581   long old_written = CPP_WRITTEN (pfile);
582   enum cpp_ttype token;
583   char *x;
584
585   token = _cpp_get_directive_token (pfile);
586
587   if (token != CPP_NUMBER)
588     {
589       cpp_error (pfile, "token after #line is not an integer");
590       goto bad_line_directive;
591     }
592
593   CPP_PUTC (pfile, '\0');  /* not terminated for us */
594   new_lineno = strtoul ((const char *) (pfile->token_buffer + old_written),
595                         &x, 10);
596   if (x[0] != '\0')
597     {
598       cpp_error (pfile, "token after #line is not an integer");
599       goto bad_line_directive;
600     }      
601   CPP_SET_WRITTEN (pfile, old_written);
602
603   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
604     cpp_pedwarn (pfile, "line number out of range in #line");
605
606   token = _cpp_get_directive_token (pfile);
607
608   if (token == CPP_STRING)
609     {
610       U_CHAR *fname = pfile->token_buffer + old_written + 1;
611       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
612       int action_number = 0;
613
614       if (read_line_number (pfile, &action_number))
615         {
616           if (CPP_PEDANTIC (pfile))
617             cpp_pedwarn (pfile, "garbage at end of #line");
618
619           /* This is somewhat questionable: change the buffer stack
620              depth so that output_line_command thinks we've stacked
621              another buffer. */
622           if (action_number == 1)
623             {
624               pfile->buffer_stack_depth++;
625               ip->system_header_p = 0;
626               read_line_number (pfile, &action_number);
627             }
628           else if (action_number == 2)
629             {
630               pfile->buffer_stack_depth--;
631               ip->system_header_p = 0;
632               read_line_number (pfile, &action_number);
633             }
634           if (action_number == 3)
635             {
636               ip->system_header_p = 1;
637               read_line_number (pfile, &action_number);
638             }
639           if (action_number == 4)
640             {
641               ip->system_header_p = 2;
642               read_line_number (pfile, &action_number);
643             }
644         }
645       
646       *end_name = '\0';
647       
648       if (strcmp ((const char *)fname, ip->nominal_fname))
649         {
650           if (!strcmp ((const char *)fname, ip->ihash->name))
651             ip->nominal_fname = ip->ihash->name;
652           else
653             ip->nominal_fname = _cpp_fake_ihash (pfile, (const char *)fname);
654         }
655     }
656   else if (token != CPP_VSPACE && token != CPP_EOF)
657     {
658       cpp_error (pfile, "second token after #line is not a string");
659       goto bad_line_directive;
660     }
661
662   /* The Newline at the end of this line remains to be processed.
663      To put the next line at the specified line number,
664      we must store a line number now that is one less.  */
665   ip->lineno = new_lineno - 1;
666   CPP_SET_WRITTEN (pfile, old_written);
667   return 0;
668
669  bad_line_directive:
670   _cpp_skip_rest_of_line (pfile);
671   CPP_SET_WRITTEN (pfile, old_written);
672   return 0;
673 }
674
675 /* Remove the definition of a symbol from the symbol table.
676    According to the C standard, it is not an error to undef
677    something that has no definitions. */
678 static int
679 do_undef (pfile)
680      cpp_reader *pfile;
681 {
682   int len;
683   cpp_hashnode *hp;
684   U_CHAR *name;
685   long here = CPP_WRITTEN (pfile);
686   enum cpp_ttype token;
687
688   pfile->no_macro_expand++;
689   token = _cpp_get_directive_token (pfile);
690   pfile->no_macro_expand--;
691
692   if (token != CPP_NAME)
693     {
694       cpp_error (pfile, "token after #undef is not an identifier");
695       _cpp_skip_rest_of_line (pfile);
696       return 0;
697     }
698   len = CPP_WRITTEN (pfile) - here;
699
700   token = _cpp_get_directive_token (pfile);
701   if (token != CPP_VSPACE)
702   {
703       cpp_pedwarn (pfile, "junk on line after #undef");
704       _cpp_skip_rest_of_line (pfile);
705   }
706
707   name = pfile->token_buffer + here;
708   CPP_SET_WRITTEN (pfile, here);
709
710   hp = cpp_lookup (pfile, name, len);
711   if (hp->type == T_VOID)
712     ; /* Not defined in the first place - do nothing.  */
713   else if (hp->type == T_POISON)
714     cpp_error (pfile, "cannot undefine poisoned \"%s\"", hp->name);
715   else
716     {
717       /* If we are generating additional info for debugging (with -g) we
718          need to pass through all effective #undef commands.  */
719       if (CPP_OPTION (pfile, debug_output))
720         pass_thru_directive (hp->name, len, pfile, T_UNDEF);
721
722       if (hp->type != T_MACRO && hp->type != T_FMACRO
723           && hp->type != T_EMPTY && hp->type != T_IDENTITY)
724         cpp_warning (pfile, "undefining `%s'", hp->name);
725
726       _cpp_free_definition (hp);
727       hp->type = T_VOID;
728     }
729
730   return 0;
731 }
732
733 /*
734  * Report an error detected by the program we are processing.
735  * Use the text of the line in the error message.
736  * (We use error because it prints the filename & line#.)
737  */
738
739 static int
740 do_error (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_error (pfile, "#error %.*s", (int)(limit - text), text);
751   return 0;
752 }
753
754 /*
755  * Report a warning detected by the program we are processing.
756  * Use the text of the line in the warning message, then continue.
757  */
758
759 static int
760 do_warning (pfile)
761      cpp_reader *pfile;
762 {
763   const U_CHAR *text, *limit;
764
765   _cpp_skip_hspace (pfile);
766   text = CPP_BUFFER (pfile)->cur;
767   _cpp_skip_rest_of_line (pfile);
768   limit = CPP_BUFFER (pfile)->cur;
769
770   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
771   return 0;
772 }
773
774 /* Report program identification.  */
775
776 static int
777 do_ident (pfile)
778      cpp_reader *pfile;
779 {
780   long old_written = CPP_WRITTEN (pfile);
781
782   CPP_PUTS (pfile, "#ident ", 7);
783
784   /* Next token should be a string constant.  */
785   if (_cpp_get_directive_token (pfile) == CPP_STRING)
786     /* And then a newline.  */
787     if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
788       /* Good - ship it.  */
789       return 0;
790
791   cpp_error (pfile, "invalid #ident");
792   _cpp_skip_rest_of_line (pfile);
793   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
794
795   return 0;
796 }
797
798 /* Pragmata handling.  We handle some of these, and pass the rest on
799    to the front end.  C99 defines three pragmas and says that no macro
800    expansion is to be performed on them; whether or not macro
801    expansion happens for other pragmas is implementation defined.
802    This implementation never macro-expands the text after #pragma.
803
804    We currently do not support the _Pragma operator.  Support for that
805    has to be coordinated with the front end.  Proposed implementation:
806    both #pragma blah blah and _Pragma("blah blah") become
807    __builtin_pragma(blah blah) and we teach the parser about that.  */
808
809 /* Sub-handlers for the pragmas needing treatment here.
810    They return 1 if the token buffer is to be popped, 0 if not. */
811 static int do_pragma_once               PARAMS ((cpp_reader *));
812 static int do_pragma_implementation     PARAMS ((cpp_reader *));
813 static int do_pragma_poison             PARAMS ((cpp_reader *));
814 static int do_pragma_system_header      PARAMS ((cpp_reader *));
815 static int do_pragma_default            PARAMS ((cpp_reader *));
816
817 static int
818 do_pragma (pfile)
819      cpp_reader *pfile;
820 {
821   long here, key;
822   U_CHAR *buf;
823   int pop;
824   enum cpp_ttype token;
825
826   here = CPP_WRITTEN (pfile);
827   CPP_PUTS (pfile, "#pragma ", 8);
828
829   key = CPP_WRITTEN (pfile);
830   pfile->no_macro_expand++;
831   token = _cpp_get_directive_token (pfile);
832   if (token != CPP_NAME)
833     {
834       if (token == CPP_VSPACE)
835         goto empty;
836       else
837         goto skip;
838     }
839
840   buf = pfile->token_buffer + key;
841   CPP_PUTC (pfile, ' ');
842
843 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
844   if (tokis ("once"))
845     pop = do_pragma_once (pfile);
846   else if (tokis ("implementation"))
847     pop = do_pragma_implementation (pfile);
848   else if (tokis ("poison"))
849     pop = do_pragma_poison (pfile);
850   else if (tokis ("system_header"))
851     pop = do_pragma_system_header (pfile);
852   else
853     pop = do_pragma_default (pfile);
854 #undef tokis
855
856   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
857     goto skip;
858
859   if (pop)
860     CPP_SET_WRITTEN (pfile, here);
861   pfile->no_macro_expand--;
862   return 0;
863
864  skip:
865   cpp_error (pfile, "malformed #pragma directive");
866   _cpp_skip_rest_of_line (pfile);
867  empty:
868   CPP_SET_WRITTEN (pfile, here);
869   pfile->no_macro_expand--;
870   return 0;
871 }
872
873 static int
874 do_pragma_default (pfile)
875      cpp_reader *pfile;
876 {
877   while (_cpp_get_directive_token (pfile) != CPP_VSPACE)
878     CPP_PUTC (pfile, ' ');
879   return 0;
880 }
881
882 static int
883 do_pragma_once (pfile)
884      cpp_reader *pfile;
885 {
886   cpp_buffer *ip = CPP_BUFFER (pfile);
887
888   /* Allow #pragma once in system headers, since that's not the user's
889      fault.  */
890   if (!ip->system_header_p)
891     cpp_warning (pfile, "#pragma once is obsolete");
892       
893   if (CPP_PREV_BUFFER (ip) == NULL)
894     cpp_warning (pfile, "#pragma once outside include file");
895   else
896     ip->ihash->cmacro = NEVER_REINCLUDE;
897
898   return 1;
899 }
900
901 static int
902 do_pragma_implementation (pfile)
903      cpp_reader *pfile;
904 {
905   /* Be quiet about `#pragma implementation' for a file only if it hasn't
906      been included yet.  */
907   enum cpp_ttype token;
908   long written = CPP_WRITTEN (pfile);
909   U_CHAR *name;
910   char *copy;
911   size_t len;
912
913   token = _cpp_get_directive_token (pfile);
914   if (token == CPP_VSPACE)
915     return 0;
916   else if (token != CPP_STRING)
917     {
918       cpp_error (pfile, "malformed #pragma implementation");
919       return 1;
920     }
921
922   /* Trim the leading and trailing quote marks from the string.  */
923   name = pfile->token_buffer + written + 1;
924   len = CPP_PWRITTEN (pfile) - name;
925   copy = alloca (len);
926   memcpy (copy, name, len - 1);
927   copy[len - 1] = '\0';
928   
929   if (cpp_included (pfile, copy))
930     cpp_warning (pfile,
931          "#pragma implementation for %s appears after file is included",
932                  copy);
933   return 0;
934 }
935
936 static int
937 do_pragma_poison (pfile)
938      cpp_reader *pfile;
939 {
940   /* Poison these symbols so that all subsequent usage produces an
941      error message.  */
942   U_CHAR *p;
943   cpp_hashnode *hp;
944   long written;
945   size_t len;
946   enum cpp_ttype token;
947   int writeit;
948
949   /* As a rule, don't include #pragma poison commands in output,  
950      unless the user asks for them.  */
951   writeit = (CPP_OPTION (pfile, debug_output)
952              || CPP_OPTION (pfile, dump_macros) == dump_definitions
953              || CPP_OPTION (pfile, dump_macros) == dump_names);
954
955   for (;;)
956     {
957       written = CPP_WRITTEN (pfile);
958       token = _cpp_get_directive_token (pfile);
959       if (token == CPP_VSPACE)
960         break;
961       if (token != CPP_NAME)
962         {
963           cpp_error (pfile, "invalid #pragma poison directive");
964           _cpp_skip_rest_of_line (pfile);
965           return 1;
966         }
967
968       p = pfile->token_buffer + written;
969       len = CPP_PWRITTEN (pfile) - p;
970       hp = cpp_lookup (pfile, p, len);
971       if (hp->type == T_POISON)
972         ;  /* It is allowed to poison the same identifier twice.  */
973       else
974         {
975           if (hp->type != T_VOID)
976             cpp_warning (pfile, "poisoning existing macro `%s'", hp->name);
977           _cpp_free_definition (hp);
978           hp->type = T_POISON;
979         }
980       if (writeit)
981         CPP_PUTC (pfile, ' ');
982     }
983   return !writeit;
984 }
985
986 /* Mark the current header as a system header.  This will suppress
987    some categories of warnings (notably those from -pedantic).  It is
988    intended for use in system libraries that cannot be implemented in
989    conforming C, but cannot be certain that their headers appear in a
990    system include directory.  To prevent abuse, it is rejected in the
991    primary source file.  */
992 static int
993 do_pragma_system_header (pfile)
994      cpp_reader *pfile;
995 {
996   cpp_buffer *ip = cpp_file_buffer (pfile);
997   if (CPP_PREV_BUFFER (ip) == NULL)
998     cpp_warning (pfile, "#pragma system_header outside include file");
999   else
1000     ip->system_header_p = 1;
1001
1002   return 1;
1003 }
1004  
1005 /* Just ignore #sccs, on systems where we define it at all.  */
1006 #ifdef SCCS_DIRECTIVE
1007 static int
1008 do_sccs (pfile)
1009      cpp_reader *pfile;
1010 {
1011   _cpp_skip_rest_of_line (pfile);
1012   return 0;
1013 }
1014 #endif
1015
1016 /* We've found an `#if' directive.  If the only thing before it in
1017    this file is white space, and if it is of the form
1018    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1019    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1020    for an explanation of controlling macros.)  If so, return the
1021    hash node for SYMBOL.  Otherwise, return NULL.  */
1022
1023 static const cpp_hashnode *
1024 detect_if_not_defined (pfile)
1025      cpp_reader *pfile;
1026 {
1027   const cpp_hashnode *cmacro = 0;
1028   enum cpp_ttype token;
1029   unsigned int base_offset;
1030   unsigned int token_offset;
1031   unsigned int need_rparen = 0;
1032   unsigned int token_len;
1033
1034   if (pfile->skipping || pfile->only_seen_white != 2)
1035     return NULL;
1036
1037   /* Save state required for restore.  */
1038   pfile->no_macro_expand++;
1039   CPP_SET_MARK (pfile);
1040   base_offset = CPP_WRITTEN (pfile);
1041
1042   /* Look for `!', */
1043   if (_cpp_get_directive_token (pfile) != CPP_OTHER
1044       || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1045       || CPP_PWRITTEN (pfile)[-1] != '!')
1046     goto restore;
1047
1048   /* ...then `defined', */
1049   token_offset = CPP_WRITTEN (pfile);
1050   token = _cpp_get_directive_token (pfile);
1051   if (token != CPP_NAME)
1052     goto restore;
1053   if (ustrncmp (pfile->token_buffer + token_offset, U"defined", 7))
1054     goto restore;
1055
1056   /* ...then an optional '(' and the name, */
1057   token_offset = CPP_WRITTEN (pfile);
1058   token = _cpp_get_directive_token (pfile);
1059   if (token == CPP_OPEN_PAREN)
1060     {
1061       token_offset = CPP_WRITTEN (pfile);
1062       need_rparen = 1;
1063       token = _cpp_get_directive_token (pfile);
1064     }
1065   if (token != CPP_NAME)
1066     goto restore;
1067
1068   token_len = CPP_WRITTEN (pfile) - token_offset;
1069
1070   /* ...then the ')', if necessary, */
1071   if (need_rparen && _cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1072     goto restore;
1073
1074   /* ...and make sure there's nothing else on the line.  */
1075   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1076     goto restore;
1077
1078   /* We have a legitimate controlling macro for this header.  */
1079   cmacro = cpp_lookup (pfile, pfile->token_buffer + token_offset, token_len);
1080
1081  restore:
1082   CPP_SET_WRITTEN (pfile, base_offset);
1083   pfile->no_macro_expand--;
1084   CPP_GOTO_MARK (pfile);
1085
1086   return cmacro;
1087 }
1088
1089 /* Parse an #ifdef or #ifndef directive.  Returns 1 for defined, 0 for
1090    not defined; the macro tested is left in the token buffer (but
1091    popped).  */
1092
1093 static const cpp_hashnode *
1094 parse_ifdef (pfile, name)
1095      cpp_reader *pfile;
1096      const U_CHAR *name;
1097 {
1098   U_CHAR *ident;
1099   unsigned int len;
1100   enum cpp_ttype token;
1101   long old_written = CPP_WRITTEN (pfile);
1102   const cpp_hashnode *node = 0;
1103
1104   pfile->no_macro_expand++;
1105   token = _cpp_get_directive_token (pfile);
1106   pfile->no_macro_expand--;
1107
1108   ident = pfile->token_buffer + old_written;
1109   len = CPP_WRITTEN (pfile) - old_written;
1110
1111   if (token == CPP_VSPACE)
1112     {
1113       if (! CPP_TRADITIONAL (pfile))
1114         cpp_pedwarn (pfile, "#%s with no argument", name);
1115       goto done;
1116     }
1117   else if (token == CPP_NAME)
1118     {
1119       node = cpp_lookup (pfile, ident, len);
1120     }
1121   else
1122     {
1123       if (! CPP_TRADITIONAL (pfile))
1124         cpp_error (pfile, "#%s with invalid argument", name);
1125     }
1126
1127   if (!CPP_TRADITIONAL (pfile))
1128     {
1129       if (_cpp_get_directive_token (pfile) == CPP_VSPACE)
1130         goto done;
1131       
1132       cpp_pedwarn (pfile, "garbage at end of #%s", name);
1133     }
1134   _cpp_skip_rest_of_line (pfile);
1135   
1136  done:
1137   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1138   return node;
1139 }
1140
1141 /* #ifdef is dead simple.  */
1142
1143 static int
1144 do_ifdef (pfile)
1145      cpp_reader *pfile;
1146 {
1147   int def = 0;
1148   const cpp_hashnode *node = parse_ifdef (pfile, dtable[T_IFDEF].name);
1149   if (node->type == T_POISON)
1150     cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1151   else
1152     def = (node->type != T_VOID);
1153   push_conditional (pfile, !def, T_IFDEF, 0);
1154   return 0;
1155 }
1156
1157 /* #ifndef is a tad more complex, because we need to check for a
1158    no-reinclusion wrapper.  */
1159
1160 static int
1161 do_ifndef (pfile)
1162      cpp_reader *pfile;
1163 {
1164   int start_of_file;
1165   int def = 0;
1166   const cpp_hashnode *cmacro;
1167
1168   start_of_file = pfile->only_seen_white == 2;
1169   cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1170   if (cmacro->type == T_POISON)
1171     cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1172   else
1173     def = (cmacro->type != T_VOID);
1174
1175   push_conditional (pfile, def, T_IFNDEF,
1176                     start_of_file ? cmacro : 0);
1177   return 0;
1178 }
1179
1180 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1181    Also, check for a reinclude preventer of the form #if !defined (MACRO).  */
1182
1183 static int
1184 do_if (pfile)
1185      cpp_reader *pfile;
1186 {
1187   const cpp_hashnode *cmacro = 0;
1188   int value = 0;
1189
1190   if (! pfile->skipping)
1191     {
1192       cmacro = detect_if_not_defined (pfile);  
1193       value = _cpp_parse_expr (pfile);
1194     }
1195   push_conditional (pfile, value == 0, T_IF, cmacro);
1196   return 0;
1197 }
1198
1199 /* #else flips pfile->skipping and continues without changing
1200    if_stack; this is so that the error message for missing #endif's
1201    etc. will point to the original #if.  */
1202
1203 static int
1204 do_else (pfile)
1205      cpp_reader *pfile;
1206 {
1207   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1208
1209   validate_else (pfile, dtable[T_ELSE].name);
1210
1211   if (ifs == NULL)
1212     {
1213       cpp_error (pfile, "#else without #if");
1214       return 0;
1215     }
1216   if (ifs->type == T_ELSE)
1217     {
1218       cpp_error (pfile, "#else after #else");
1219       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1220     }
1221
1222   /* #ifndef can't have its special treatment for containing the whole file
1223      if it has a #else clause.  */
1224   ifs->cmacro = 0;
1225
1226   ifs->type = T_ELSE;
1227   if (! ifs->was_skipping)
1228     {
1229       /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1230          succeeded, so we mustn't do the else block.  */
1231       if (pfile->skipping < 2)
1232         pfile->skipping = ! pfile->skipping;
1233     }
1234   return 0;
1235 }
1236
1237 /*
1238  * handle a #elif directive by not changing if_stack either.
1239  * see the comment above do_else.
1240  */
1241
1242 static int
1243 do_elif (pfile)
1244      cpp_reader *pfile;
1245 {
1246   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1247
1248   if (ifs == NULL)
1249     {
1250       cpp_error (pfile, "#elif without #if");
1251       return 0;
1252     }
1253   if (ifs->type == T_ELSE)
1254     {
1255       cpp_error (pfile, "#elif after #else");
1256       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1257     }
1258
1259   ifs->type = T_ELIF;
1260   if (ifs->was_skipping)
1261     _cpp_skip_rest_of_line (pfile);
1262   else if (pfile->skipping != 1)
1263     {
1264       _cpp_skip_rest_of_line (pfile);
1265       pfile->skipping = 2;  /* one block succeeded, so don't do any others */
1266     }
1267   else
1268     pfile->skipping = ! _cpp_parse_expr (pfile);
1269
1270   return 0;
1271 }
1272
1273
1274 /* #endif pops the if stack and resets pfile->skipping.  */
1275
1276 static int
1277 do_endif (pfile)
1278      cpp_reader *pfile;
1279 {
1280   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1281
1282   validate_else (pfile, dtable[T_ENDIF].name);
1283
1284   if (ifs == NULL)
1285     cpp_error (pfile, "#endif without #if");
1286   else
1287     {
1288       CPP_BUFFER (pfile)->if_stack = ifs->next;
1289       pfile->skipping = ifs->was_skipping;
1290       pfile->potential_control_macro = ifs->cmacro;
1291       free (ifs);
1292     }
1293   return 0;
1294 }
1295
1296 /* Push an if_stack entry and set pfile->skipping accordingly.
1297    If this is a #ifndef starting at the beginning of a file,
1298    CMACRO is the macro name tested by the #ifndef.  */
1299
1300 static void
1301 push_conditional (pfile, skip, type, cmacro)
1302      cpp_reader *pfile;
1303      int skip;
1304      int type;
1305      const cpp_hashnode *cmacro;
1306 {
1307   struct if_stack *ifs;
1308
1309   ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1310   ifs->lineno = CPP_BUFFER (pfile)->lineno;
1311   ifs->next = CPP_BUFFER (pfile)->if_stack;
1312   ifs->cmacro = cmacro;
1313   ifs->was_skipping = pfile->skipping;
1314   ifs->type = type;
1315
1316   if (!pfile->skipping)
1317     pfile->skipping = skip;
1318
1319   CPP_BUFFER (pfile)->if_stack = ifs;
1320 }
1321
1322 /* Issue -pedantic warning for text which is not a comment following
1323    an #else or #endif.  */
1324
1325 static void
1326 validate_else (pfile, directive)
1327      cpp_reader *pfile;
1328      const U_CHAR *directive;
1329 {
1330   if (CPP_PEDANTIC (pfile))
1331     {
1332       long old_written = CPP_WRITTEN (pfile);
1333       pfile->no_macro_expand++;
1334       if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1335         cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1336       CPP_SET_WRITTEN (pfile, old_written);
1337       pfile->no_macro_expand--;
1338     }
1339   _cpp_skip_rest_of_line (pfile);
1340 }
1341
1342 /* Called when we reach the end of a macro buffer.  Walk back up the
1343    conditional stack till we reach its level at entry to this file,
1344    issuing error messages.  Then force skipping off.  */
1345 void
1346 _cpp_unwind_if_stack (pfile, pbuf)
1347      cpp_reader *pfile;
1348      cpp_buffer *pbuf;
1349 {
1350   struct if_stack *ifs, *nifs;
1351
1352   for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1353     {
1354       cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1355                            dtable[ifs->type].name);
1356       nifs = ifs->next;
1357       free (ifs);
1358     }
1359   pfile->skipping = 0;
1360 }
1361
1362 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1363 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1364 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1365 static int
1366 do_assert (pfile)
1367      cpp_reader *pfile;
1368 {
1369   long old_written;
1370   U_CHAR *sym;
1371   size_t len;
1372   cpp_hashnode *hp;
1373   struct predicate *pred = 0;
1374   enum cpp_ttype type;
1375
1376   old_written = CPP_WRITTEN (pfile);
1377   pfile->no_macro_expand++;
1378
1379   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1380   type = _cpp_get_directive_token (pfile);
1381   if (type == CPP_VSPACE)
1382     ERROR ("#assert without predicate");
1383   else if (type != CPP_NAME)
1384     ERROR ("assertion predicate is not an identifier");
1385
1386   sym = pfile->token_buffer + old_written;
1387   len = CPP_WRITTEN (pfile) - old_written;
1388   hp = cpp_lookup (pfile, sym, len);
1389
1390   if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1391     ERROR ("missing token-sequence in #assert");
1392
1393   pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1394   _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1395
1396   if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1397       != CPP_CLOSE_PAREN)
1398     ERROR ("missing close paren in #assert");
1399
1400   if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1401     ICE ("impossible token, expecting ) in do_assert");
1402
1403   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1404     ERROR ("junk at end of #assert");
1405
1406   if (hp->type == T_ASSERTION)
1407     {
1408       /* Check for reassertion.  */
1409       const struct predicate *old;
1410
1411       for (old = hp->value.pred; old; old = old->next)
1412         if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1413           /* We used to warn about this, but SVR4 cc doesn't, so let's
1414              match that (also consistent with #define).  goto error will
1415              clean up.  */
1416           goto error;
1417       pred->next = hp->value.pred;
1418     }
1419   else
1420     {
1421       hp->type = T_ASSERTION;
1422       pred->next = 0;
1423     }
1424   
1425   _cpp_squeeze_toklist (&pred->answer);
1426   hp->value.pred = pred;
1427   pfile->no_macro_expand--;
1428   CPP_SET_WRITTEN (pfile, old_written);
1429   return 0;
1430
1431  error:
1432   _cpp_skip_rest_of_line (pfile);
1433   pfile->no_macro_expand--;
1434   CPP_SET_WRITTEN (pfile, old_written);
1435   if (pred)
1436     {
1437       _cpp_free_toklist (&pred->answer);
1438       free (pred);
1439     }
1440   return 0;
1441 }
1442
1443 static int
1444 do_unassert (pfile)
1445      cpp_reader *pfile;
1446 {
1447   long old_written;
1448   U_CHAR *sym;
1449   size_t len;
1450   cpp_hashnode *hp;
1451   cpp_toklist ans;
1452   enum cpp_ttype type;
1453   int specific = 0;
1454
1455   old_written = CPP_WRITTEN (pfile);
1456   pfile->no_macro_expand++;
1457
1458   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1459   if (_cpp_get_directive_token (pfile) != CPP_NAME)
1460     ERROR ("#unassert must be followed by an identifier");
1461
1462   sym = pfile->token_buffer + old_written;
1463   len = CPP_WRITTEN (pfile) - old_written;
1464   hp = cpp_lookup (pfile, sym, len);
1465
1466   type = _cpp_get_directive_token (pfile);
1467   if (type == CPP_OPEN_PAREN)
1468     {
1469       specific = 1;
1470       _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1471
1472       if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1473           != CPP_CLOSE_PAREN)
1474         ERROR ("missing close paren in #unassert");
1475
1476       if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1477         ICE ("impossible token, expecting ) in do_unassert");
1478
1479       type = _cpp_get_directive_token (pfile);
1480     }
1481
1482   if (type != CPP_VSPACE)
1483     ERROR ("junk at end of #unassert");
1484
1485   if (hp->type != T_ASSERTION)
1486     /* Not an error to #unassert something that isn't asserted.
1487        goto error to clean up.  */
1488     goto error;
1489
1490   if (specific)
1491     {
1492       /* Find this specific answer and remove it.  */
1493       struct predicate *o, *p;
1494
1495       for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1496         if (_cpp_equiv_toklists (&ans, &o->answer))
1497           {
1498             if (p)
1499               p->next = o->next;
1500             else
1501               hp->value.pred = o->next;
1502
1503             _cpp_free_toklist (&o->answer);
1504             free (o);
1505             break;
1506           }
1507     }
1508   else
1509     {
1510       struct predicate *o, *p;
1511       for (o = hp->value.pred; o; o = p)
1512         {
1513           p = o->next;
1514           _cpp_free_toklist ((cpp_toklist *) &o->answer);
1515           free (o);
1516         }
1517       hp->value.pred = NULL;
1518     }
1519
1520   if (hp->value.pred == NULL)
1521     hp->type = T_VOID;  /* Last answer for this predicate deleted.  */
1522
1523  error:
1524   _cpp_skip_rest_of_line (pfile);
1525   pfile->no_macro_expand--;
1526   CPP_SET_WRITTEN (pfile, old_written);
1527   if (specific)
1528     _cpp_free_toklist (&ans);
1529   return 0;
1530 }
1531
1532 /* These are for -D, -U, -A.  */
1533
1534 /* Process the string STR as if it appeared as the body of a #define.
1535    If STR is just an identifier, define it with value 1.
1536    If STR has anything after the identifier, then it should
1537    be identifier=definition. */
1538
1539 void
1540 cpp_define (pfile, str)
1541      cpp_reader *pfile;
1542      const char *str;
1543 {
1544   char *buf, *p;
1545   size_t count;
1546
1547   p = strchr (str, '=');
1548   /* Copy the entire option so we can modify it. 
1549      Change the first "=" in the string to a space.  If there is none,
1550      tack " 1" on the end.  Then add a newline and a NUL.  */
1551   
1552   if (p)
1553     {
1554       count = strlen (str) + 2;
1555       buf = (char *) alloca (count);
1556       memcpy (buf, str, count - 2);
1557       buf[p - str] = ' ';
1558       buf[count - 2] = '\n';
1559       buf[count - 1] = '\0';
1560     }
1561   else
1562     {
1563       count = strlen (str) + 4;
1564       buf = (char *) alloca (count);
1565       memcpy (buf, str, count - 4);
1566       strcpy (&buf[count-4], " 1\n");
1567     }
1568
1569   if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1570     {
1571       do_define (pfile);
1572       cpp_pop_buffer (pfile);
1573     }
1574 }
1575
1576 /* Process MACRO as if it appeared as the body of an #undef.  */
1577 void
1578 cpp_undef (pfile, macro)
1579      cpp_reader *pfile;
1580      const char *macro;
1581 {
1582   /* Copy the string so we can append a newline.  */
1583   size_t len = strlen (macro);
1584   char *buf = (char *) alloca (len + 2);
1585   memcpy (buf, macro, len);
1586   buf[len]     = '\n';
1587   buf[len + 1] = '\0';
1588   if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1589     {
1590       do_undef (pfile);
1591       cpp_pop_buffer (pfile);
1592     }
1593 }
1594
1595 /* Process the string STR as if it appeared as the body of a #assert. */
1596 void
1597 cpp_assert (pfile, str)
1598      cpp_reader *pfile;
1599      const char *str;
1600 {
1601   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1602     {
1603       do_assert (pfile);
1604       cpp_pop_buffer (pfile);
1605     }
1606 }
1607
1608 /* Process STR as if it appeared as the body of an #unassert. */
1609 void
1610 cpp_unassert (pfile, str)
1611      cpp_reader *pfile;
1612      const char *str;
1613 {
1614   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1615     {
1616       do_unassert (pfile);
1617       cpp_pop_buffer (pfile);
1618     }
1619 }  
1620
1621 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1622 int
1623 cpp_defined (pfile, id, len)
1624      cpp_reader *pfile;
1625      const U_CHAR *id;
1626      int len;
1627 {
1628   cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1629   if (hp->type == T_POISON)
1630     {
1631       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1632       return 0;
1633     }
1634   return (hp->type != T_VOID);
1635 }