OSDN Git Service

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