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