OSDN Git Service

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