OSDN Git Service

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