OSDN Git Service

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