OSDN Git Service

* sh.h (CPP_SPEC): Add -D__NOMACSAVE__ for -mnomacsave.
[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->type == T_POISON)
1130     cpp_error (pfile, "attempt to use poisoned `%s'", node->name);
1131   else
1132     def = (node->type != T_VOID);
1133   push_conditional (pfile, !def, T_IFDEF, 0);
1134   return 0;
1135 }
1136
1137 /* #ifndef is a tad more complex, because we need to check for a
1138    no-reinclusion wrapper.  */
1139
1140 static int
1141 do_ifndef (pfile)
1142      cpp_reader *pfile;
1143 {
1144   int start_of_file;
1145   int def = 0;
1146   const cpp_hashnode *cmacro;
1147
1148   start_of_file = pfile->only_seen_white == 2;
1149   cmacro = parse_ifdef (pfile, dtable[T_IFNDEF].name);
1150   if (cmacro->type == T_POISON)
1151     cpp_error (pfile, "attempt to use poisoned `%s'", cmacro->name);
1152   else
1153     def = (cmacro->type != T_VOID);
1154
1155   push_conditional (pfile, def, T_IFNDEF,
1156                     start_of_file ? cmacro : 0);
1157   return 0;
1158 }
1159
1160 /* #if is straightforward; just call _cpp_parse_expr, then conditional_skip.
1161    Also, check for a reinclude preventer of the form #if !defined (MACRO).  */
1162
1163 static int
1164 do_if (pfile)
1165      cpp_reader *pfile;
1166 {
1167   const cpp_hashnode *cmacro = 0;
1168   int value = 0;
1169
1170   if (! pfile->skipping)
1171     {
1172       cmacro = detect_if_not_defined (pfile);  
1173       value = _cpp_parse_expr (pfile);
1174     }
1175   push_conditional (pfile, value == 0, T_IF, cmacro);
1176   return 0;
1177 }
1178
1179 /* #else flips pfile->skipping and continues without changing
1180    if_stack; this is so that the error message for missing #endif's
1181    etc. will point to the original #if.  */
1182
1183 static int
1184 do_else (pfile)
1185      cpp_reader *pfile;
1186 {
1187   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1188
1189   validate_else (pfile, dtable[T_ELSE].name);
1190
1191   if (ifs == NULL)
1192     {
1193       cpp_error (pfile, "#else without #if");
1194       return 0;
1195     }
1196   if (ifs->type == T_ELSE)
1197     {
1198       cpp_error (pfile, "#else after #else");
1199       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1200     }
1201
1202   /* #ifndef can't have its special treatment for containing the whole file
1203      if it has a #else clause.  */
1204   ifs->cmacro = 0;
1205
1206   ifs->type = T_ELSE;
1207   if (! ifs->was_skipping)
1208     {
1209       /* If pfile->skipping is 2, one of the blocks in an #if/#elif/... chain
1210          succeeded, so we mustn't do the else block.  */
1211       if (pfile->skipping < 2)
1212         pfile->skipping = ! pfile->skipping;
1213     }
1214   return 0;
1215 }
1216
1217 /*
1218  * handle a #elif directive by not changing if_stack either.
1219  * see the comment above do_else.
1220  */
1221
1222 static int
1223 do_elif (pfile)
1224      cpp_reader *pfile;
1225 {
1226   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1227
1228   if (ifs == NULL)
1229     {
1230       cpp_error (pfile, "#elif without #if");
1231       return 0;
1232     }
1233   if (ifs->type == T_ELSE)
1234     {
1235       cpp_error (pfile, "#elif after #else");
1236       cpp_error_with_line (pfile, ifs->lineno, 1, "the conditional began here");
1237     }
1238
1239   ifs->type = T_ELIF;
1240   if (ifs->was_skipping)
1241     _cpp_skip_rest_of_line (pfile);
1242   else if (pfile->skipping != 1)
1243     {
1244       _cpp_skip_rest_of_line (pfile);
1245       pfile->skipping = 2;  /* one block succeeded, so don't do any others */
1246     }
1247   else
1248     pfile->skipping = ! _cpp_parse_expr (pfile);
1249
1250   return 0;
1251 }
1252
1253
1254 /* #endif pops the if stack and resets pfile->skipping.  */
1255
1256 static int
1257 do_endif (pfile)
1258      cpp_reader *pfile;
1259 {
1260   struct if_stack *ifs = CPP_BUFFER (pfile)->if_stack;
1261
1262   validate_else (pfile, dtable[T_ENDIF].name);
1263
1264   if (ifs == NULL)
1265     cpp_error (pfile, "#endif without #if");
1266   else
1267     {
1268       CPP_BUFFER (pfile)->if_stack = ifs->next;
1269       pfile->skipping = ifs->was_skipping;
1270       pfile->potential_control_macro = ifs->cmacro;
1271       free (ifs);
1272     }
1273   return 0;
1274 }
1275
1276 /* Push an if_stack entry and set pfile->skipping accordingly.
1277    If this is a #ifndef starting at the beginning of a file,
1278    CMACRO is the macro name tested by the #ifndef.  */
1279
1280 static void
1281 push_conditional (pfile, skip, type, cmacro)
1282      cpp_reader *pfile;
1283      int skip;
1284      int type;
1285      const cpp_hashnode *cmacro;
1286 {
1287   struct if_stack *ifs;
1288
1289   ifs = (struct if_stack *) xmalloc (sizeof (struct if_stack));
1290   ifs->lineno = CPP_BUFFER (pfile)->lineno;
1291   ifs->next = CPP_BUFFER (pfile)->if_stack;
1292   ifs->cmacro = cmacro;
1293   ifs->was_skipping = pfile->skipping;
1294   ifs->type = type;
1295
1296   if (!pfile->skipping)
1297     pfile->skipping = skip;
1298
1299   CPP_BUFFER (pfile)->if_stack = ifs;
1300 }
1301
1302 /* Issue -pedantic warning for text which is not a comment following
1303    an #else or #endif.  */
1304
1305 static void
1306 validate_else (pfile, directive)
1307      cpp_reader *pfile;
1308      const U_CHAR *directive;
1309 {
1310   if (CPP_PEDANTIC (pfile))
1311     {
1312       long old_written = CPP_WRITTEN (pfile);
1313       pfile->no_macro_expand++;
1314       if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1315         cpp_pedwarn (pfile, "ISO C forbids text after #%s", directive);
1316       CPP_SET_WRITTEN (pfile, old_written);
1317       pfile->no_macro_expand--;
1318     }
1319   _cpp_skip_rest_of_line (pfile);
1320 }
1321
1322 /* Called when we reach the end of a macro buffer.  Walk back up the
1323    conditional stack till we reach its level at entry to this file,
1324    issuing error messages.  Then force skipping off.  */
1325 void
1326 _cpp_unwind_if_stack (pfile, pbuf)
1327      cpp_reader *pfile;
1328      cpp_buffer *pbuf;
1329 {
1330   struct if_stack *ifs, *nifs;
1331
1332   for (ifs = pbuf->if_stack; ifs; ifs = nifs)
1333     {
1334       cpp_error_with_line (pfile, ifs->lineno, 1, "unterminated #%s",
1335                            dtable[ifs->type].name);
1336       nifs = ifs->next;
1337       free (ifs);
1338     }
1339   pfile->skipping = 0;
1340 }
1341
1342 #define WARNING(msgid) do { cpp_warning(pfile, msgid); goto error; } while (0)
1343 #define ERROR(msgid) do { cpp_error(pfile, msgid); goto error; } while (0)
1344 #define ICE(msgid) do { cpp_ice(pfile, msgid); goto error; } while (0)
1345 static int
1346 do_assert (pfile)
1347      cpp_reader *pfile;
1348 {
1349   long old_written;
1350   U_CHAR *sym;
1351   size_t len;
1352   cpp_hashnode *hp;
1353   struct predicate *pred = 0;
1354   enum cpp_ttype type;
1355
1356   old_written = CPP_WRITTEN (pfile);
1357   pfile->no_macro_expand++;
1358
1359   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1360   type = _cpp_get_directive_token (pfile);
1361   if (type == CPP_VSPACE)
1362     ERROR ("#assert without predicate");
1363   else if (type != CPP_NAME)
1364     ERROR ("assertion predicate is not an identifier");
1365
1366   sym = pfile->token_buffer + old_written;
1367   len = CPP_WRITTEN (pfile) - old_written;
1368   hp = cpp_lookup (pfile, sym, len);
1369
1370   if (_cpp_get_directive_token (pfile) != CPP_OPEN_PAREN)
1371     ERROR ("missing token-sequence in #assert");
1372
1373   pred = (struct predicate *) xmalloc (sizeof (struct predicate));
1374   _cpp_init_toklist (&pred->answer, NO_DUMMY_TOKEN);
1375
1376   if (_cpp_scan_until (pfile, &pred->answer, CPP_CLOSE_PAREN)
1377       != CPP_CLOSE_PAREN)
1378     ERROR ("missing close paren in #assert");
1379
1380   if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1381     ICE ("impossible token, expecting ) in do_assert");
1382
1383   if (_cpp_get_directive_token (pfile) != CPP_VSPACE)
1384     ERROR ("junk at end of #assert");
1385
1386   if (hp->type == T_ASSERTION)
1387     {
1388       /* Check for reassertion.  */
1389       const struct predicate *old;
1390
1391       for (old = hp->value.pred; old; old = old->next)
1392         if (_cpp_equiv_toklists (&pred->answer, &old->answer))
1393           /* We used to warn about this, but SVR4 cc doesn't, so let's
1394              match that (also consistent with #define).  goto error will
1395              clean up.  */
1396           goto error;
1397       pred->next = hp->value.pred;
1398     }
1399   else
1400     {
1401       hp->type = T_ASSERTION;
1402       pred->next = 0;
1403     }
1404   
1405   _cpp_squeeze_toklist (&pred->answer);
1406   hp->value.pred = pred;
1407   pfile->no_macro_expand--;
1408   CPP_SET_WRITTEN (pfile, old_written);
1409   return 0;
1410
1411  error:
1412   _cpp_skip_rest_of_line (pfile);
1413   pfile->no_macro_expand--;
1414   CPP_SET_WRITTEN (pfile, old_written);
1415   if (pred)
1416     {
1417       _cpp_free_toklist (&pred->answer);
1418       free (pred);
1419     }
1420   return 0;
1421 }
1422
1423 static int
1424 do_unassert (pfile)
1425      cpp_reader *pfile;
1426 {
1427   long old_written;
1428   U_CHAR *sym;
1429   size_t len;
1430   cpp_hashnode *hp;
1431   cpp_toklist ans;
1432   enum cpp_ttype type;
1433   int specific = 0;
1434
1435   old_written = CPP_WRITTEN (pfile);
1436   pfile->no_macro_expand++;
1437
1438   CPP_PUTC (pfile, '#');        /* force token out of macro namespace */
1439   if (_cpp_get_directive_token (pfile) != CPP_NAME)
1440     ERROR ("#unassert must be followed by an identifier");
1441
1442   sym = pfile->token_buffer + old_written;
1443   len = CPP_WRITTEN (pfile) - old_written;
1444   hp = cpp_lookup (pfile, sym, len);
1445
1446   type = _cpp_get_directive_token (pfile);
1447   if (type == CPP_OPEN_PAREN)
1448     {
1449       specific = 1;
1450       _cpp_init_toklist (&ans, NO_DUMMY_TOKEN);
1451
1452       if (_cpp_scan_until (pfile, &ans, CPP_CLOSE_PAREN)
1453           != CPP_CLOSE_PAREN)
1454         ERROR ("missing close paren in #unassert");
1455
1456       if (_cpp_get_directive_token (pfile) != CPP_CLOSE_PAREN)
1457         ICE ("impossible token, expecting ) in do_unassert");
1458
1459       type = _cpp_get_directive_token (pfile);
1460     }
1461
1462   if (type != CPP_VSPACE)
1463     ERROR ("junk at end of #unassert");
1464
1465   if (hp->type != T_ASSERTION)
1466     /* Not an error to #unassert something that isn't asserted.
1467        goto error to clean up.  */
1468     goto error;
1469
1470   if (specific)
1471     {
1472       /* Find this specific answer and remove it.  */
1473       struct predicate *o, *p;
1474
1475       for (p = NULL, o = hp->value.pred; o; p = o, o = o->next)
1476         if (_cpp_equiv_toklists (&ans, &o->answer))
1477           {
1478             if (p)
1479               p->next = o->next;
1480             else
1481               hp->value.pred = o->next;
1482
1483             _cpp_free_toklist (&o->answer);
1484             free (o);
1485             break;
1486           }
1487     }
1488   else
1489     {
1490       struct predicate *o, *p;
1491       for (o = hp->value.pred; o; o = p)
1492         {
1493           p = o->next;
1494           _cpp_free_toklist ((cpp_toklist *) &o->answer);
1495           free (o);
1496         }
1497       hp->value.pred = NULL;
1498     }
1499
1500   if (hp->value.pred == NULL)
1501     hp->type = T_VOID;  /* Last answer for this predicate deleted.  */
1502
1503  error:
1504   _cpp_skip_rest_of_line (pfile);
1505   pfile->no_macro_expand--;
1506   CPP_SET_WRITTEN (pfile, old_written);
1507   if (specific)
1508     _cpp_free_toklist (&ans);
1509   return 0;
1510 }
1511
1512 /* These are for -D, -U, -A.  */
1513
1514 /* Process the string STR as if it appeared as the body of a #define.
1515    If STR is just an identifier, define it with value 1.
1516    If STR has anything after the identifier, then it should
1517    be identifier=definition. */
1518
1519 void
1520 cpp_define (pfile, str)
1521      cpp_reader *pfile;
1522      const char *str;
1523 {
1524   char *buf, *p;
1525   size_t count;
1526
1527   p = strchr (str, '=');
1528   /* Copy the entire option so we can modify it. 
1529      Change the first "=" in the string to a space.  If there is none,
1530      tack " 1" on the end.  Then add a newline and a NUL.  */
1531   
1532   if (p)
1533     {
1534       count = strlen (str) + 2;
1535       buf = (char *) alloca (count);
1536       memcpy (buf, str, count - 2);
1537       buf[p - str] = ' ';
1538       buf[count - 2] = '\n';
1539       buf[count - 1] = '\0';
1540     }
1541   else
1542     {
1543       count = strlen (str) + 4;
1544       buf = (char *) alloca (count);
1545       memcpy (buf, str, count - 4);
1546       strcpy (&buf[count-4], " 1\n");
1547     }
1548
1549   if (cpp_push_buffer (pfile, (U_CHAR *)buf, count - 1) != NULL)
1550     {
1551       do_define (pfile);
1552       cpp_pop_buffer (pfile);
1553     }
1554 }
1555
1556 /* Process MACRO as if it appeared as the body of an #undef.  */
1557 void
1558 cpp_undef (pfile, macro)
1559      cpp_reader *pfile;
1560      const char *macro;
1561 {
1562   /* Copy the string so we can append a newline.  */
1563   size_t len = strlen (macro);
1564   char *buf = (char *) alloca (len + 2);
1565   memcpy (buf, macro, len);
1566   buf[len]     = '\n';
1567   buf[len + 1] = '\0';
1568   if (cpp_push_buffer (pfile, (U_CHAR *)buf, len + 1) != NULL)
1569     {
1570       do_undef (pfile);
1571       cpp_pop_buffer (pfile);
1572     }
1573 }
1574
1575 /* Process the string STR as if it appeared as the body of a #assert. */
1576 void
1577 cpp_assert (pfile, str)
1578      cpp_reader *pfile;
1579      const char *str;
1580 {
1581   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1582     {
1583       do_assert (pfile);
1584       cpp_pop_buffer (pfile);
1585     }
1586 }
1587
1588 /* Process STR as if it appeared as the body of an #unassert. */
1589 void
1590 cpp_unassert (pfile, str)
1591      cpp_reader *pfile;
1592      const char *str;
1593 {
1594   if (cpp_push_buffer (pfile, (const U_CHAR *)str, strlen (str)) != NULL)
1595     {
1596       do_unassert (pfile);
1597       cpp_pop_buffer (pfile);
1598     }
1599 }  
1600
1601 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
1602 int
1603 cpp_defined (pfile, id, len)
1604      cpp_reader *pfile;
1605      const U_CHAR *id;
1606      int len;
1607 {
1608   cpp_hashnode *hp = cpp_lookup (pfile, id, len);
1609   if (hp->type == T_POISON)
1610     {
1611       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1612       return 0;
1613     }
1614   return (hp->type != T_VOID);
1615 }