OSDN Git Service

* cpplib.h (CPP_ASSERTION, CPP_STRINGIZE, CPP_TOKPASTE): New
[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
29 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
30
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? 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 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
36    (Note that it is false while we're expanding macro *arguments*.) */
37 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38
39 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
40    case CPP_BUMP_LINE must not be called.  */
41 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
42
43 /* External declarations.  */
44
45 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
46
47 /* `struct directive' defines one #-directive, including how to handle it.  */
48
49 struct directive
50 {
51   int length;                   /* Length of name */
52   int (*func)                   /* Function to handle directive */
53     PARAMS ((cpp_reader *, const struct directive *));
54   const char *name;             /* Name of directive */
55   enum node_type type;          /* Code which describes which directive.  */
56 };
57
58 /* These functions are declared to return int instead of void since they
59    are going to be placed in a table and some old compilers have trouble with
60    pointers to functions returning void.  */
61
62 static int do_define PARAMS ((cpp_reader *, const struct directive *));
63 static int do_line PARAMS ((cpp_reader *, const struct directive *));
64 static int do_include PARAMS ((cpp_reader *, const struct directive *));
65 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
66 static int do_error PARAMS ((cpp_reader *, const struct directive *));
67 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
68 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
69 static int do_if PARAMS ((cpp_reader *, const struct directive *));
70 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
71 static int do_else PARAMS ((cpp_reader *, const struct directive *));
72 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
73 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
74 #ifdef SCCS_DIRECTIVE
75 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
76 #endif
77 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
78 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
79 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
80
81 /* Forward declarations.  */
82
83 static void validate_else               PARAMS ((cpp_reader *, const char *));
84 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
85 static void conditional_skip            PARAMS ((cpp_reader *, int,
86                                                 enum node_type, U_CHAR *));
87 static void skip_if_group               PARAMS ((cpp_reader *));
88 static void parse_name                  PARAMS ((cpp_reader *, int));
89 static void parse_string                PARAMS ((cpp_reader *, int));
90 static int parse_assertion              PARAMS ((cpp_reader *));
91 static const char *if_directive_name    PARAMS ((cpp_reader *,
92                                                  struct if_stack *));
93 static enum cpp_token null_underflow    PARAMS ((cpp_reader *));
94 static int null_cleanup                 PARAMS ((cpp_buffer *, cpp_reader *));
95 static int skip_comment                 PARAMS ((cpp_reader *, int));
96 static int copy_comment                 PARAMS ((cpp_reader *, int));
97 static void skip_string                 PARAMS ((cpp_reader *, int));
98 static void skip_rest_of_line           PARAMS ((cpp_reader *));
99 static void cpp_skip_hspace             PARAMS ((cpp_reader *));
100 static int handle_directive             PARAMS ((cpp_reader *));
101 static void pass_thru_directive         PARAMS ((const U_CHAR *, size_t,
102                                                  cpp_reader *,
103                                                  const struct directive *));
104 static int read_line_number             PARAMS ((cpp_reader *, int *));
105 static U_CHAR *detect_if_not_defined    PARAMS ((cpp_reader *));
106 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
107                                                       IF_STACK_FRAME *));
108 static void skip_block_comment          PARAMS ((cpp_reader *));
109 static void skip_line_comment           PARAMS ((cpp_reader *));
110 static void parse_set_mark              PARAMS ((cpp_reader *));
111 static void parse_goto_mark             PARAMS ((cpp_reader *));
112 static int get_macro_name               PARAMS ((cpp_reader *));
113
114 /* Here is the actual list of #-directives.
115    This table is ordered by frequency of occurrence; the numbers
116    at the end are directive counts from all the source code I have
117    lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
118    linux-2.2.9, and pcmcia-cs-3.0.9).  */
119
120 static const struct directive directive_table[] = {
121   /* In C89 */
122   {  6, do_define,   "define",       T_DEFINE },        /* 270554 */
123   {  7, do_include,  "include",      T_INCLUDE },       /*  52262 */
124   {  5, do_endif,    "endif",        T_ENDIF },         /*  45855 */
125   {  5, do_xifdef,   "ifdef",        T_IFDEF },         /*  22000 */
126   {  2, do_if,       "if",           T_IF },            /*  18162 */
127   {  4, do_else,     "else",         T_ELSE },          /*   9863 */
128   {  6, do_xifdef,   "ifndef",       T_IFNDEF },        /*   9675 */
129   {  5, do_undef,    "undef",        T_UNDEF },         /*   4837 */
130   {  4, do_line,     "line",         T_LINE },          /*   2465 */
131   {  4, do_elif,     "elif",         T_ELIF },          /*    610 */
132   {  5, do_error,    "error",        T_ERROR },         /*    475 */
133   {  6, do_pragma,   "pragma",       T_PRAGMA },        /*    195 */
134
135   /* Extensions.  All deprecated except #warning and #include_next.  */
136   {  7, do_warning,  "warning",      T_WARNING },       /*     22 - GNU   */
137   { 12, do_include,  "include_next", T_INCLUDE_NEXT },  /*     19 - GNU   */
138   {  5, do_ident,    "ident",        T_IDENT },         /*     11 - SVR4  */
139   {  6, do_include,  "import",       T_IMPORT },        /*      0 - ObjC  */
140   {  6, do_assert,   "assert",       T_ASSERT },        /*      0 - SVR4  */
141   {  8, do_unassert, "unassert",     T_UNASSERT },      /*      0 - SVR4  */
142 #ifdef SCCS_DIRECTIVE
143   {  4, do_sccs,     "sccs",         T_SCCS },          /*      0 - SVR2? */
144 #endif
145   {  -1, 0, "", T_UNUSED }
146 };
147
148 /* Place into PFILE a quoted string representing the string SRC.
149    Caller must reserve enough space in pfile->token_buffer.  */
150
151 void
152 quote_string (pfile, src)
153      cpp_reader *pfile;
154      const char *src;
155 {
156   U_CHAR c;
157
158   CPP_PUTC_Q (pfile, '\"');
159   for (;;)
160     switch ((c = *src++))
161       {
162       default:
163         if (ISPRINT (c))
164           CPP_PUTC_Q (pfile, c);
165         else
166           {
167             sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
168             CPP_ADJUST_WRITTEN (pfile, 4);
169           }
170         break;
171
172       case '\"':
173       case '\\':
174         CPP_PUTC_Q (pfile, '\\');
175         CPP_PUTC_Q (pfile, c);
176         break;
177       
178       case '\0':
179         CPP_PUTC_Q (pfile, '\"');
180         CPP_NUL_TERMINATE_Q (pfile);
181         return;
182       }
183 }
184
185 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
186
187 void
188 cpp_grow_buffer (pfile, n)
189      cpp_reader *pfile;
190      long n;
191 {
192   long old_written = CPP_WRITTEN (pfile);
193   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
194   pfile->token_buffer = (U_CHAR *)
195     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
196   CPP_SET_WRITTEN (pfile, old_written);
197 }
198
199 /* Process the string STR as if it appeared as the body of a #define.
200    If STR is just an identifier, define it with value 1.
201    If STR has anything after the identifier, then it should
202    be identifier=definition. */
203
204 void
205 cpp_define (pfile, str)
206      cpp_reader *pfile;
207      U_CHAR *str;
208 {
209   U_CHAR *buf, *p;
210   size_t count;
211
212   p = strchr (str, '=');
213   /* Copy the entire option so we can modify it. 
214      Change the first "=" in the string to a space.  If there is none,
215      tack " 1" on the end.  Then add a newline and a NUL.  */
216   
217   if (p)
218     {
219       count = strlen (str) + 2;
220       buf = (U_CHAR *) alloca (count);
221       memcpy (buf, str, count - 2);
222       buf[p - str] = ' ';
223       buf[count - 2] = '\n';
224       buf[count - 1] = '\0';
225     }
226   else
227     {
228       count = strlen (str) + 4;
229       buf = (U_CHAR *) alloca (count);
230       memcpy (buf, str, count - 4);
231       strcpy (&buf[count-4], " 1\n");
232     }
233
234   if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
235     {
236       do_define (pfile, NULL);
237       cpp_pop_buffer (pfile);
238     }
239 }
240
241 /* Process the string STR as if it appeared as the body of a #assert. */
242 void
243 cpp_assert (pfile, str)
244      cpp_reader *pfile;
245      U_CHAR *str;
246 {
247   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
248     {
249       do_assert (pfile, NULL);
250       cpp_pop_buffer (pfile);
251     }
252 }
253
254 /* Determine whether the identifier ID, of length LEN, is a defined macro.  */
255 int
256 cpp_defined (pfile, id, len)
257      cpp_reader *pfile;
258      const U_CHAR *id;
259      int len;
260 {
261   HASHNODE *hp = cpp_lookup (pfile, id, len);
262   if (hp && hp->type == T_POISON)
263     {
264       cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
265       return 0;
266     }
267   return (hp != NULL);
268 }
269
270 static enum cpp_token
271 null_underflow (pfile)
272      cpp_reader *pfile ATTRIBUTE_UNUSED;
273 {
274   return CPP_EOF;
275 }
276
277 static int
278 null_cleanup (pbuf, pfile)
279      cpp_buffer *pbuf ATTRIBUTE_UNUSED;
280      cpp_reader *pfile ATTRIBUTE_UNUSED;
281 {
282   return 0;
283 }
284
285 /* Skip a C-style block comment.  We know it's a comment, and point is
286    at the second character of the starter.  */
287 static void
288 skip_block_comment (pfile)
289      cpp_reader *pfile;
290 {
291   int c, prev_c = -1;
292   long line, col;
293
294   FORWARD(1);
295   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
296   for (;;)
297     {
298       c = GETC ();
299       if (c == EOF)
300         {
301           cpp_error_with_line (pfile, line, col, "unterminated comment");
302           return;
303         }
304       else if (c == '\n' || c == '\r')
305         {
306           /* \r cannot be a macro escape marker here. */
307           if (!ACTIVE_MARK_P())
308             CPP_BUMP_LINE (pfile);
309         }
310       else if (c == '/' && prev_c == '*')
311         return;
312       else if (c == '*' && prev_c == '/'
313                && CPP_OPTIONS (pfile)->warn_comments)
314         cpp_warning (pfile, "`/*' within comment");
315
316       prev_c = c;
317     }
318 }
319
320 /* Skip a C++/Chill line comment.  We know it's a comment, and point
321    is at the second character of the initiator.  */
322 static void
323 skip_line_comment (pfile)
324      cpp_reader *pfile;
325 {
326   FORWARD(1);
327   for (;;)
328     {
329       int c = GETC ();
330
331       /* We don't have to worry about EOF in here.  */
332       if (c == '\n')
333         {
334           /* Don't consider final '\n' to be part of comment.  */
335           FORWARD(-1);
336           return;
337         }
338       else if (c == '\r')
339         {
340           /* \r cannot be a macro escape marker here. */
341           if (!ACTIVE_MARK_P())
342             CPP_BUMP_LINE (pfile);
343           if (CPP_OPTIONS (pfile)->warn_comments)
344             cpp_warning (pfile, "backslash-newline within line comment");
345         }
346     }
347 }
348
349 /* Skip a comment - C, C++, or Chill style.  M is the first character
350    of the comment marker.  If this really is a comment, skip to its
351    end and return ' '.  If this is not a comment, return M (which will
352    be '/' or '-').  */
353
354 static int
355 skip_comment (pfile, m)
356      cpp_reader *pfile;
357      int m;
358 {
359   if (m == '/' && PEEKC() == '*')
360     {
361       skip_block_comment (pfile);
362       return ' ';
363     }
364   else if (m == '/' && PEEKC() == '/')
365     {
366       if (CPP_BUFFER (pfile)->system_header_p)
367         {
368           /* We silently allow C++ comments in system headers, irrespective
369              of conformance mode, because lots of busted systems do that
370              and trying to clean it up in fixincludes is a nightmare.  */
371           skip_line_comment (pfile);
372           return ' ';
373         }
374       else if (CPP_OPTIONS (pfile)->cplusplus_comments)
375         {
376           if (CPP_OPTIONS (pfile)->c89
377               && CPP_PEDANTIC (pfile)
378               && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
379             {
380               cpp_pedwarn (pfile,
381                            "C++ style comments are not allowed in ISO C89");
382               cpp_pedwarn (pfile,
383                            "(this will be reported only once per input file)");
384               CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
385             }
386           skip_line_comment (pfile);
387           return ' ';
388         }
389       else
390         return m;
391     }
392   else if (m == '-' && PEEKC() == '-'
393            && CPP_OPTIONS (pfile)->chill)
394     {
395       skip_line_comment (pfile);
396       return ' ';
397     }
398   else
399     return m;
400 }
401
402 /* Identical to skip_comment except that it copies the comment into the
403    token_buffer.  This is used if !discard_comments.  */
404 static int
405 copy_comment (pfile, m)
406      cpp_reader *pfile;
407      int m;
408 {
409   U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
410   U_CHAR *limit;
411
412   if (skip_comment (pfile, m) == m)
413     return m;
414
415   limit = CPP_BUFFER (pfile)->cur;
416   CPP_RESERVE (pfile, limit - start + 2);
417   CPP_PUTC_Q (pfile, m);
418   for (; start <= limit; start++)
419     if (*start != '\r')
420       CPP_PUTC_Q (pfile, *start);
421
422   return ' ';
423 }
424
425 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
426
427 static void
428 cpp_skip_hspace (pfile)
429      cpp_reader *pfile;
430 {
431   int c;
432   while (1)
433     {
434       c = GETC();
435       if (c == EOF)
436         return;
437       else if (is_hspace(c))
438         {
439           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
440             cpp_pedwarn (pfile, "%s in preprocessing directive",
441                          c == '\f' ? "formfeed" : "vertical tab");
442         }
443       else if (c == '\r')
444         {
445           /* \r is a backslash-newline marker if !has_escapes, and
446              a deletable-whitespace or no-reexpansion marker otherwise. */
447           if (CPP_BUFFER (pfile)->has_escapes)
448             {
449               if (PEEKC() == ' ')
450                 FORWARD(1);
451               else
452                 break;
453             }
454           else
455             CPP_BUMP_LINE (pfile);
456         }
457       else if (c == '/' || c == '-')
458         {
459           c = skip_comment (pfile, c);
460           if (c  != ' ')
461             break;
462         }
463       else
464         break;
465     }
466   FORWARD(-1);
467 }
468
469 /* Read and discard the rest of the current line.  */
470
471 static void
472 skip_rest_of_line (pfile)
473      cpp_reader *pfile;
474 {
475   for (;;)
476     {
477       int c = GETC();
478       switch (c)
479         {
480         case '\n':
481           FORWARD(-1);
482         case EOF:
483           return;
484
485         case '\r':
486           if (! CPP_BUFFER (pfile)->has_escapes)
487             CPP_BUMP_LINE (pfile);
488           break;
489           
490         case '\'':
491         case '\"':
492           skip_string (pfile, c);
493           break;
494
495         case '/':
496         case '-':
497           skip_comment (pfile, c);
498           break;
499
500         case '\f':
501         case '\v':
502           if (CPP_PEDANTIC (pfile))
503             cpp_pedwarn (pfile, "%s in preprocessing directive",
504                          c == '\f' ? "formfeed" : "vertical tab");
505           break;
506
507         }
508     }
509 }
510
511 /* Handle a possible # directive.
512    '#' has already been read.  */
513
514 static int
515 handle_directive (pfile)
516      cpp_reader *pfile;
517 {
518   int c;
519   register const struct directive *kt;
520   int ident_length;
521   U_CHAR *ident;
522   long old_written = CPP_WRITTEN (pfile);
523
524   if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
525     {
526       cpp_ice (pfile, "handle_directive called on macro buffer");
527       return 0;
528     }
529
530   cpp_skip_hspace (pfile);
531
532   c = PEEKC ();
533   /* # followed by a number is equivalent to #line.  Do not recognize
534      this form in assembly language source files.  Complain about this
535      form if we're being pedantic, but not if this is regurgitated
536      input (preprocessed or fed back in by the C++ frontend).  */
537   if (c >= '0' && c <= '9')
538     {
539       if (CPP_OPTIONS (pfile)->lang_asm)
540         return 0;
541
542       if (CPP_PEDANTIC (pfile)
543           && ! CPP_PREPROCESSED (pfile)
544           && ! CPP_BUFFER (pfile)->manual_pop)
545         cpp_pedwarn (pfile, "`#' followed by integer");
546       do_line (pfile, NULL);
547       return 1;
548     }
549
550   /* If we are rescanning preprocessed input, don't obey any directives
551      other than # nnn.  */
552   if (CPP_PREPROCESSED (pfile))
553     return 0;
554
555   /* Now find the directive name.  */
556   CPP_PUTC (pfile, '#');
557   parse_name (pfile, GETC());
558   ident = pfile->token_buffer + old_written + 1;
559   ident_length = CPP_PWRITTEN (pfile) - ident;
560   if (ident_length == 0)
561     {
562       /* A line of just `#' becomes blank.  A line with something
563          other than an identifier after the # is reparsed as a non-
564          directive line.  */
565       CPP_SET_WRITTEN (pfile, old_written);
566       return (PEEKC() == '\n');
567     }
568
569   /* Decode the keyword and call the appropriate expansion routine.  */
570   for (kt = directive_table; ; kt++)
571     {
572       if (kt->length <= 0)
573         /* # identifier, but not a legit directive.  Pass onward as a
574            CPP_DIRECTIVE token anyway - let the consumer worry about it.  */
575         return 1;
576       if (kt->length == ident_length
577           && !strncmp (kt->name, ident, ident_length)) 
578         break;
579     }
580
581   CPP_SET_WRITTEN (pfile, old_written);
582
583   if (pfile->no_directives)
584     {
585       cpp_error (pfile, "`#%s' may not be used inside a macro argument",
586                  kt->name);
587       skip_rest_of_line (pfile);
588     }
589   else
590     (*kt->func) (pfile, kt);
591
592   return 1;
593 }
594
595 /* Pass a directive through to the output file.
596    BUF points to the contents of the directive, as a contiguous string.
597    LEN is the length of the string pointed to by BUF.
598    KEYWORD is the keyword-table entry for the directive.  */
599
600 static void
601 pass_thru_directive (buf, len, pfile, keyword)
602      const U_CHAR *buf;
603      size_t len;
604      cpp_reader *pfile;
605      const struct directive *keyword;
606 {
607   register unsigned keyword_length = keyword->length;
608
609   CPP_RESERVE (pfile, 1 + keyword_length + len);
610   CPP_PUTC_Q (pfile, '#');
611   CPP_PUTS_Q (pfile, keyword->name, keyword_length);
612   if (len != 0 && buf[0] != ' ')
613     CPP_PUTC_Q (pfile, ' ');
614   CPP_PUTS_Q (pfile, buf, len);
615 }
616
617 /* Subroutine of do_define: determine the name of the macro to be
618    defined.  */
619
620 static int
621 get_macro_name (pfile)
622      cpp_reader *pfile;
623 {
624   long here, len;
625
626   here = CPP_WRITTEN (pfile);
627   pfile->no_macro_expand++;
628   if (get_directive_token (pfile) != CPP_NAME)
629     {
630       cpp_error (pfile, "`#define' must be followed by an identifier");
631       goto invalid;
632     }
633
634   len = CPP_WRITTEN (pfile) - here;
635   if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
636     {
637       cpp_error (pfile, "`defined' is not a legal macro name");
638       goto invalid;
639     }
640
641   pfile->no_macro_expand--;
642   return len;
643
644  invalid:
645   skip_rest_of_line (pfile);
646   pfile->no_macro_expand--;
647   return 0;
648 }
649
650 /* Process a #define command.
651    KEYWORD is the keyword-table entry for #define,
652    or NULL for a "predefined" macro.  */
653
654 static int
655 do_define (pfile, keyword)
656      cpp_reader *pfile;
657      const struct directive *keyword;
658 {
659   HASHNODE *hp;
660   DEFINITION *def;
661   long here;
662   int len, c;
663   int funlike = 0;
664   U_CHAR *sym;
665
666   here = CPP_WRITTEN (pfile);
667   len = get_macro_name (pfile);
668   if (len == 0)
669     return 0;
670
671   /* Copy out the name so we can pop the token buffer.  */
672   len = CPP_WRITTEN (pfile) - here;
673   sym = (U_CHAR *) alloca (len + 1);
674   memcpy (sym, pfile->token_buffer + here, len);
675   sym[len] = '\0';
676   CPP_SET_WRITTEN (pfile, here);
677
678   /* If the next character, with no intervening whitespace, is '(',
679      then this is a function-like macro.  */
680   c = PEEKC ();
681   if (c == '(')
682     funlike = 1;
683   else if (c != '\n' && !is_hspace (c))
684     /* Otherwise, C99 requires white space after the name.  We treat it
685        as an object-like macro if this happens, with a warning.  */
686     cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
687
688   def = create_definition (pfile, funlike);
689   if (def == 0)
690     return 0;
691
692   if ((hp = cpp_lookup (pfile, sym, len)) != NULL)
693     {
694       int ok;
695
696       /* Redefining a macro is ok if the definitions are the same.  */
697       if (hp->type == T_MACRO)
698         ok = ! compare_defs (pfile, def, hp->value.defn);
699       /* Redefining a constant is ok with -D.  */
700       else if (hp->type == T_CONST || hp->type == T_STDC)
701         ok = ! CPP_OPTIONS (pfile)->done_initializing;
702       /* Otherwise it's not ok.  */
703       else
704         ok = 0;
705       /* Print the warning or error if it's not ok.  */
706       if (! ok)
707         {
708           if (hp->type == T_POISON)
709             cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
710           else
711             cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
712           if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
713             {
714               DEFINITION *d = hp->value.defn;
715               cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
716                         "this is the location of the previous definition");
717             }
718         }
719       if (hp->type != T_POISON)
720         {
721           /* Replace the old definition.  */
722           if (hp->type == T_MACRO)
723             free_definition (hp->value.defn);
724           hp->type = T_MACRO;
725           hp->value.defn = def;
726         }
727     }
728   else
729     cpp_install (pfile, sym, len, T_MACRO, (char *) def);
730
731   if (keyword != NULL && keyword->type == T_DEFINE)
732     {
733       if (CPP_OPTIONS (pfile)->debug_output
734           || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
735         dump_definition (pfile, sym, len, def);
736       else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
737         pass_thru_directive (sym, len, pfile, keyword);
738     }
739
740   return 0;
741 }
742
743
744 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
745    If BUFFER != NULL, then use the LENGTH characters in BUFFER
746    as the new input buffer.
747    Return the new buffer, or NULL on failure.  */
748
749 cpp_buffer *
750 cpp_push_buffer (pfile, buffer, length)
751      cpp_reader *pfile;
752      U_CHAR *buffer;
753      long length;
754 {
755   cpp_buffer *buf = CPP_BUFFER (pfile);
756   cpp_buffer *new;
757   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
758     {
759       cpp_fatal (pfile, "macro or `#include' recursion too deep");
760       return NULL;
761     }
762
763   new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
764
765   new->if_stack = pfile->if_stack;
766   new->cleanup = null_cleanup;
767   new->underflow = null_underflow;
768   new->buf = new->cur = buffer;
769   new->alimit = new->rlimit = buffer + length;
770   new->prev = buf;
771   new->mark = -1;
772   new->line_base = NULL;
773
774   CPP_BUFFER (pfile) = new;
775   return new;
776 }
777
778 cpp_buffer *
779 cpp_pop_buffer (pfile)
780      cpp_reader *pfile;
781 {
782   cpp_buffer *buf = CPP_BUFFER (pfile);
783   if (ACTIVE_MARK_P())
784     cpp_ice (pfile, "mark active in cpp_pop_buffer");
785   (*buf->cleanup) (buf, pfile);
786   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
787   free (buf);
788   pfile->buffer_stack_depth--;
789   return CPP_BUFFER (pfile);
790 }
791
792 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
793    Pop the buffer when done.  */
794
795 void
796 cpp_scan_buffer (pfile)
797      cpp_reader *pfile;
798 {
799   cpp_buffer *buffer = CPP_BUFFER (pfile);
800   enum cpp_token token;
801   if (CPP_OPTIONS (pfile)->no_output)
802     {
803       long old_written = CPP_WRITTEN (pfile);
804       /* In no-output mode, we can ignore everything but directives.  */
805       for (;;)
806         {
807           if (! pfile->only_seen_white)
808             skip_rest_of_line (pfile);
809           token = cpp_get_token (pfile);
810           if (token == CPP_EOF) /* Should not happen ...  */
811             break;
812           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
813             {
814               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
815                   != CPP_NULL_BUFFER (pfile))
816                 cpp_pop_buffer (pfile);
817               break;
818             }
819         }
820       CPP_SET_WRITTEN (pfile, old_written);
821     }
822   else
823     {
824       for (;;)
825         {
826           token = cpp_get_token (pfile);
827           if (token == CPP_EOF) /* Should not happen ...  */
828             break;
829           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
830             {
831               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
832                   != CPP_NULL_BUFFER (pfile))
833                 cpp_pop_buffer (pfile);
834               break;
835             }
836         }
837     }
838 }
839
840 /*
841  * Rescan a string (which may have escape marks) into pfile's buffer.
842  * Place the result in pfile->token_buffer.
843  *
844  * The input is copied before it is scanned, so it is safe to pass
845  * it something from the token_buffer that will get overwritten
846  * (because it follows CPP_WRITTEN).  This is used by do_include.
847  */
848
849 void
850 cpp_expand_to_buffer (pfile, buf, length)
851      cpp_reader *pfile;
852      const U_CHAR *buf;
853      int length;
854 {
855   register cpp_buffer *ip;
856   U_CHAR *buf1;
857   int save_no_output;
858
859   if (length < 0)
860     {
861       cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
862       return;
863     }
864
865   /* Set up the input on the input stack.  */
866
867   buf1 = (U_CHAR *) alloca (length + 1);
868   memcpy (buf1, buf, length);
869   buf1[length] = 0;
870
871   ip = cpp_push_buffer (pfile, buf1, length);
872   if (ip == NULL)
873     return;
874   ip->has_escapes = 1;
875
876   /* Scan the input, create the output.  */
877   save_no_output = CPP_OPTIONS (pfile)->no_output;
878   CPP_OPTIONS (pfile)->no_output = 0;
879   cpp_scan_buffer (pfile);
880   CPP_OPTIONS (pfile)->no_output = save_no_output;
881
882   CPP_NUL_TERMINATE (pfile);
883 }
884
885 void
886 cpp_buf_line_and_col (pbuf, linep, colp)
887      register cpp_buffer *pbuf;
888      long *linep, *colp;
889 {
890   if (pbuf)
891     {
892       *linep = pbuf->lineno;
893       if (colp)
894         *colp = pbuf->cur - pbuf->line_base;
895     }
896   else
897     {
898       *linep = 0;
899       if (colp)
900         *colp = 0;
901     }
902 }
903
904 /* Return the cpp_buffer that corresponds to a file (not a macro).  */
905
906 cpp_buffer *
907 cpp_file_buffer (pfile)
908      cpp_reader *pfile;
909 {
910   cpp_buffer *ip = CPP_BUFFER (pfile);
911
912   for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
913     if (ip->fname != NULL)
914       return ip;
915   return NULL;
916 }
917
918 /*
919  * write out a #line command, for instance, after an #include file.
920  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
921  */
922
923 void
924 output_line_command (pfile, file_change)
925      cpp_reader *pfile;
926      enum file_change_code file_change;
927 {
928   long line;
929   cpp_buffer *ip = CPP_BUFFER (pfile);
930
931   if (ip->fname == NULL)
932     return;
933
934   if (CPP_OPTIONS (pfile)->no_line_commands
935       || CPP_OPTIONS (pfile)->no_output)
936     return;
937
938   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
939
940   /* If the current file has not changed, we omit the #line if it would
941      appear to be a no-op, and we output a few newlines instead
942      if we want to increase the line number by a small amount.
943      We cannot do this if pfile->lineno is zero, because that means we
944      haven't output any line commands yet.  (The very first line command
945      output is a `same_file' command.)  */
946   if (file_change == same_file && pfile->lineno != 0)
947     {
948       if (line == pfile->lineno)
949         return;
950
951       /* If the inherited line number is a little too small,
952          output some newlines instead of a #line command.  */
953       if (line > pfile->lineno && line < pfile->lineno + 8)
954         {
955           CPP_RESERVE (pfile, 20);
956           while (line > pfile->lineno)
957             {
958               CPP_PUTC_Q (pfile, '\n');
959               pfile->lineno++;
960             }
961           return;
962         }
963     }
964
965   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
966   CPP_PUTS_Q (pfile, "# ", 2);
967
968   sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
969   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
970
971   quote_string (pfile, ip->nominal_fname); 
972   if (file_change != same_file && file_change != rename_file)
973     {
974       CPP_PUTC_Q (pfile, ' ');
975       CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
976     }
977   /* Tell cc1 if following text comes from a system header file.  */
978   if (ip->system_header_p)
979     {
980       CPP_PUTC_Q (pfile, ' ');
981       CPP_PUTC_Q (pfile, '3');
982     }
983 #ifndef NO_IMPLICIT_EXTERN_C
984   /* Tell cc1plus if following text should be treated as C.  */
985   if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
986     {
987       CPP_PUTC_Q (pfile, ' ');
988       CPP_PUTC_Q (pfile, '4');
989     }
990 #endif
991   CPP_PUTC_Q (pfile, '\n');
992   pfile->lineno = line;
993 }
994
995
996 /* Like cpp_get_token, except that it does not read past end-of-line.
997    Also, horizontal space is skipped, and macros are popped.  */
998
999 enum cpp_token
1000 get_directive_token (pfile)
1001      cpp_reader *pfile;
1002 {
1003   long old_written = CPP_WRITTEN (pfile);
1004   enum cpp_token token;
1005
1006   for (;;)
1007     {
1008       cpp_skip_hspace (pfile);
1009       if (PEEKC () == '\n')
1010         return CPP_VSPACE;
1011
1012       token = cpp_get_token (pfile);
1013       /* token could be hspace at the beginning of a macro.  */
1014       if (token == CPP_HSPACE || token == CPP_COMMENT)
1015         {
1016           CPP_SET_WRITTEN (pfile, old_written);
1017           continue;
1018         }
1019
1020       /* token cannot be vspace, it would have been caught above.  */
1021       if (token == CPP_VSPACE)
1022         {
1023           cpp_ice (pfile, "VSPACE in get_directive_token");
1024           return token;
1025         }
1026
1027       /* token cannot be POP unless the buffer is a macro buffer.  */
1028       if (token != CPP_POP)
1029         return token;
1030
1031       if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1032         {
1033           cpp_ice (pfile, "POP of file buffer in get_directive_token");
1034           return token;
1035         }
1036
1037       /* We must pop the buffer by hand, or else cpp_get_token might
1038          hand us white space or newline on the next invocation.  */
1039       cpp_pop_buffer (pfile);
1040     }
1041 }
1042 \f
1043 /* Handle #include and #import.
1044    This function expects to see "fname" or <fname> on the input.
1045
1046    The input is normally in part of the output_buffer following
1047    CPP_WRITTEN, and will get overwritten by output_line_command.
1048    I.e. in input file specification has been popped by handle_directive.
1049    This is safe.  */
1050
1051 static int
1052 do_include (pfile, keyword)
1053      cpp_reader *pfile;
1054      const struct directive *keyword;
1055 {
1056   int importing = (keyword->type == T_IMPORT);
1057   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1058   int angle_brackets = 0;       /* 0 for "...", 1 for <...> */
1059   int before;  /* included before? */
1060   long flen;
1061   unsigned char *ftok;
1062   cpp_buffer *fp;
1063
1064   enum cpp_token token;
1065
1066   /* Chain of dirs to search */
1067   struct include_hash *ihash;
1068   struct file_name_list *search_start;
1069   
1070   long old_written = CPP_WRITTEN (pfile);
1071
1072   int fd;
1073
1074   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1075     {
1076       if (importing)
1077         cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1078       if (skip_dirs)
1079         cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1080     }
1081
1082   if (importing && CPP_OPTIONS (pfile)->warn_import
1083       && !CPP_OPTIONS (pfile)->inhibit_warnings
1084       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1085     {
1086       pfile->import_warning = 1;
1087       cpp_warning (pfile,
1088            "#import is obsolete, use an #ifndef wrapper in the header file");
1089     }
1090
1091   pfile->parsing_include_directive++;
1092   token = get_directive_token (pfile);
1093   pfile->parsing_include_directive--;
1094
1095   if (token == CPP_STRING)
1096     {
1097       if (pfile->token_buffer[old_written] == '<')
1098         angle_brackets = 1;
1099     }
1100 #ifdef VMS
1101   else if (token == CPP_NAME)
1102     {
1103       /* Support '#include xyz' like VAX-C.  It is taken as
1104          '#include <xyz.h>' and generates a warning.  */
1105       cpp_warning (pfile,
1106                "`#include filename' is obsolete, use `#include <filename.h>'");
1107       angle_brackets = 1;
1108
1109       /* Append the missing `.h' to the name. */
1110       CPP_PUTS (pfile, ".h", 2);
1111     }
1112 #endif
1113   else
1114     {
1115       cpp_error (pfile,
1116                  "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1117       CPP_SET_WRITTEN (pfile, old_written);
1118       skip_rest_of_line (pfile);
1119       return 0;
1120     }
1121
1122   flen = CPP_WRITTEN (pfile) - old_written;
1123   ftok = (unsigned char *) alloca (flen + 1);
1124   memcpy (ftok, pfile->token_buffer + old_written, flen);
1125   ftok[flen] = '\0';
1126
1127   if (get_directive_token (pfile) != CPP_VSPACE)
1128     {
1129       cpp_error (pfile, "junk at end of `#include'");
1130       skip_rest_of_line (pfile);
1131     }
1132
1133   CPP_SET_WRITTEN (pfile, old_written);
1134
1135   if (flen == 0)
1136     {
1137       cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1138       return 0;
1139     }
1140
1141   if (CPP_OPTIONS (pfile)->dump_includes)
1142     pass_thru_directive (ftok,
1143                          flen
1144 #ifdef VMS
1145           - ((token == CPP_NAME) ? 2 : 0)
1146 #endif
1147                          , pfile, keyword);
1148
1149 #ifdef VMS
1150   if (token == CPP_STRING)
1151 #endif
1152     {
1153       ftok++;
1154       flen -= 2;
1155       ftok[flen] = '\0';
1156     }
1157
1158   search_start = 0;
1159
1160   for (fp = CPP_BUFFER (pfile);
1161        fp != CPP_NULL_BUFFER (pfile);
1162        fp = CPP_PREV_BUFFER (fp))
1163     if (fp->fname != NULL)
1164       break;
1165
1166   if (fp == CPP_NULL_BUFFER (pfile))
1167     {
1168       cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1169       return 0;
1170     }
1171   
1172   /* For #include_next, skip in the search path past the dir in which the
1173      containing file was found.  Treat files specified using an absolute path
1174      as if there are no more directories to search.  Treat the primary source
1175      file like any other included source, but generate a warning.  */
1176   if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1177     {
1178       if (fp->ihash->foundhere != ABSOLUTE_PATH)
1179         search_start = fp->ihash->foundhere->next;
1180     }
1181   else
1182     {
1183       if (skip_dirs)
1184         cpp_warning (pfile, "#include_next in primary source file");
1185       
1186       if (angle_brackets)
1187         search_start = CPP_OPTIONS (pfile)->bracket_include;
1188       else
1189         {
1190           if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1191             {
1192               if (fp)
1193                 search_start = fp->actual_dir;
1194             }
1195           else
1196             search_start = CPP_OPTIONS (pfile)->quote_include;
1197         }
1198     }
1199
1200   if (!search_start)
1201     {
1202       cpp_error (pfile, "No include path in which to find %s", ftok);
1203       return 0;
1204     }
1205
1206   fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1207
1208   if (fd == -2)
1209     return 0;
1210   
1211   if (fd == -1)
1212     {
1213       if (CPP_OPTIONS (pfile)->print_deps_missing_files
1214           && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1215                                        (pfile->system_include_depth > 0)))
1216         {
1217           if (!angle_brackets)
1218             deps_output (pfile, ftok, ' ');
1219           else
1220             {
1221               char *p;
1222               struct file_name_list *ptr;
1223               /* If requested as a system header, assume it belongs in
1224                  the first system header directory. */
1225               if (CPP_OPTIONS (pfile)->bracket_include)
1226                 ptr = CPP_OPTIONS (pfile)->bracket_include;
1227               else
1228                 ptr = CPP_OPTIONS (pfile)->quote_include;
1229
1230               p = (char *) alloca (strlen (ptr->name)
1231                                    + strlen (ftok) + 2);
1232               if (*ptr->name != '\0')
1233                 {
1234                   strcpy (p, ptr->name);
1235                   strcat (p, "/");
1236                 }
1237               strcat (p, ftok);
1238               deps_output (pfile, p, ' ');
1239             }
1240         }
1241       /* If -M was specified, and this header file won't be added to
1242          the dependency list, then don't count this as an error,
1243          because we can still produce correct output.  Otherwise, we
1244          can't produce correct output, because there may be
1245          dependencies we need inside the missing file, and we don't
1246          know what directory this missing file exists in. */
1247       else if (CPP_PRINT_DEPS (pfile)
1248                && (CPP_PRINT_DEPS (pfile)
1249                    <= (angle_brackets || (pfile->system_include_depth > 0))))
1250         cpp_warning (pfile, "No include path in which to find %s", ftok);
1251       else
1252         cpp_error_from_errno (pfile, ftok);
1253
1254       return 0;
1255     }
1256
1257   /* For -M, add the file to the dependencies on its first inclusion. */
1258   if (!before && (CPP_PRINT_DEPS (pfile)
1259                   > (angle_brackets || (pfile->system_include_depth > 0))))
1260     deps_output (pfile, ihash->name, ' ');
1261
1262   /* Handle -H option.  */
1263   if (CPP_OPTIONS(pfile)->print_include_names)
1264     {
1265       fp = CPP_BUFFER (pfile);
1266       while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1267         putc ('.', stderr);
1268       fprintf (stderr, " %s\n", ihash->name);
1269     }
1270
1271   /* Actually process the file */
1272
1273   if (importing)
1274     ihash->control_macro = "";
1275   
1276   if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1277     {
1278       close (fd);
1279       return 0;
1280     }
1281   
1282   if (angle_brackets)
1283     pfile->system_include_depth++;   /* Decremented in file_cleanup. */
1284
1285   if (finclude (pfile, fd, ihash))
1286     {
1287       output_line_command (pfile, enter_file);
1288       pfile->only_seen_white = 2;
1289     }
1290
1291   return 0;
1292 }
1293
1294 /* Subroutine of do_line.  Read next token from PFILE without adding it to
1295    the output buffer.  If it is a number between 1 and 4, store it in *NUM
1296    and return 1; otherwise, return 0 and complain if we aren't at the end
1297    of the directive.  */
1298
1299 static int
1300 read_line_number (pfile, num)
1301      cpp_reader *pfile;
1302      int *num;
1303 {
1304   long save_written = CPP_WRITTEN (pfile);
1305   U_CHAR *p = pfile->token_buffer + save_written;
1306   enum cpp_token token = get_directive_token (pfile);
1307   CPP_SET_WRITTEN (pfile, save_written);
1308
1309   if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1310     {
1311       *num = p[0] - '0';
1312       return 1;
1313     }
1314   else
1315     {
1316       if (token != CPP_VSPACE && token != CPP_EOF)
1317         cpp_error (pfile, "invalid format `#line' command");
1318       return 0;
1319     }
1320 }
1321
1322 /* Interpret #line command.
1323    Note that the filename string (if any) is treated as if it were an
1324    include filename.  That means no escape handling.  */
1325
1326 static int
1327 do_line (pfile, keyword)
1328      cpp_reader *pfile;
1329      const struct directive *keyword ATTRIBUTE_UNUSED;
1330 {
1331   cpp_buffer *ip = CPP_BUFFER (pfile);
1332   int new_lineno;
1333   long old_written = CPP_WRITTEN (pfile);
1334   enum file_change_code file_change = same_file;
1335   enum cpp_token token;
1336   char *x;
1337
1338   token = get_directive_token (pfile);
1339
1340   if (token != CPP_NUMBER)
1341     {
1342       cpp_error (pfile, "token after `#line' is not an integer");
1343       goto bad_line_directive;
1344     }
1345
1346   new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1347   if (x[0] != '\0')
1348     {
1349       cpp_error (pfile, "token after `#line' is not an integer");
1350       goto bad_line_directive;
1351     }      
1352   CPP_SET_WRITTEN (pfile, old_written);
1353
1354   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1355     cpp_pedwarn (pfile, "line number out of range in `#line' command");
1356
1357   token = get_directive_token (pfile);
1358
1359   if (token == CPP_STRING)
1360     {
1361       U_CHAR *fname = pfile->token_buffer + old_written + 1;
1362       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1363       int action_number = 0;
1364
1365       file_change = rename_file;
1366
1367       if (read_line_number (pfile, &action_number))
1368         {
1369           if (CPP_PEDANTIC (pfile))
1370             cpp_pedwarn (pfile, "garbage at end of `#line' command");
1371
1372           if (action_number == 1)
1373             {
1374               file_change = enter_file;
1375               read_line_number (pfile, &action_number);
1376             }
1377           else if (action_number == 2)
1378             {
1379               file_change = leave_file;
1380               read_line_number (pfile, &action_number);
1381             }
1382           if (action_number == 3)
1383             {
1384               ip->system_header_p = 1;
1385               read_line_number (pfile, &action_number);
1386             }
1387           if (action_number == 4)
1388             {
1389               ip->system_header_p = 2;
1390               read_line_number (pfile, &action_number);
1391             }
1392         }
1393       
1394       *end_name = '\0';
1395       
1396       if (strcmp (fname, ip->nominal_fname))
1397         {
1398           const char *newname, *oldname;
1399           if (!strcmp (fname, ip->fname))
1400             newname = ip->fname;
1401           else if (ip->last_nominal_fname
1402                    && !strcmp (fname, ip->last_nominal_fname))
1403             newname = ip->last_nominal_fname;
1404           else
1405             newname = xstrdup (fname);
1406
1407           oldname = ip->nominal_fname;
1408           ip->nominal_fname = newname;
1409
1410           if (ip->last_nominal_fname
1411               && ip->last_nominal_fname != oldname
1412               && ip->last_nominal_fname != newname
1413               && ip->last_nominal_fname != ip->fname)
1414             free ((void *) ip->last_nominal_fname);
1415
1416           if (newname == ip->fname)
1417             ip->last_nominal_fname = NULL;
1418           else
1419             ip->last_nominal_fname = oldname;
1420         } 
1421     }
1422   else if (token != CPP_VSPACE && token != CPP_EOF)
1423     {
1424       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1425       goto bad_line_directive;
1426     }
1427
1428   /* The Newline at the end of this line remains to be processed.
1429      To put the next line at the specified line number,
1430      we must store a line number now that is one less.  */
1431   ip->lineno = new_lineno - 1;
1432   CPP_SET_WRITTEN (pfile, old_written);
1433   output_line_command (pfile, file_change);
1434   return 0;
1435
1436  bad_line_directive:
1437   skip_rest_of_line (pfile);
1438   CPP_SET_WRITTEN (pfile, old_written);
1439   return 0;
1440 }
1441
1442 /* Remove the definition of a symbol from the symbol table.
1443    According to the C standard, it is not an error to undef
1444    something that has no definitions. */
1445 static int
1446 do_undef (pfile, keyword)
1447      cpp_reader *pfile;
1448      const struct directive *keyword;
1449 {
1450   int len;
1451   HASHNODE *hp;
1452   U_CHAR *buf, *name, *limit;
1453   int c;
1454   long here = CPP_WRITTEN (pfile);
1455   enum cpp_token token;
1456
1457   cpp_skip_hspace (pfile);
1458   c = GETC();
1459   if (! is_idstart(c))
1460   {
1461       cpp_error (pfile, "token after #undef is not an identifier");
1462       skip_rest_of_line (pfile);
1463       return 1;
1464   }
1465
1466   parse_name (pfile, c);
1467   buf = pfile->token_buffer + here;
1468   limit = CPP_PWRITTEN(pfile);
1469
1470   /* Copy out the token so we can pop the token buffer. */
1471   len = limit - buf;
1472   name = (U_CHAR *) alloca (len + 1);
1473   memcpy (name, buf, len);
1474   name[len] = '\0';
1475
1476   token = get_directive_token (pfile);
1477   if (token != CPP_VSPACE)
1478   {
1479       cpp_pedwarn (pfile, "junk on line after #undef");
1480       skip_rest_of_line (pfile);
1481   }
1482   CPP_SET_WRITTEN (pfile, here);
1483
1484   while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1485     {
1486       /* If we are generating additional info for debugging (with -g) we
1487          need to pass through all effective #undef commands.  */
1488       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1489         pass_thru_directive (name, len, pfile, keyword);
1490       if (hp->type == T_POISON)
1491         cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1492       else 
1493         {
1494           if (hp->type != T_MACRO)
1495             cpp_warning (pfile, "undefining `%s'", hp->name);
1496           delete_macro (hp);
1497         }
1498     }
1499
1500   return 0;
1501 }
1502
1503 /* Wrap do_undef for -U processing. */
1504 void
1505 cpp_undef (pfile, macro)
1506      cpp_reader *pfile;
1507      U_CHAR *macro;
1508 {
1509   /* Copy the string so we can append a newline.  */
1510   size_t len = strlen (macro);
1511   U_CHAR *buf = alloca (len + 2);
1512   memcpy (buf, macro, len);
1513   buf[len]     = '\n';
1514   buf[len + 1] = '\0';
1515   if (cpp_push_buffer (pfile, buf, len + 1))
1516     {
1517       do_undef (pfile, NULL);
1518       cpp_pop_buffer (pfile);
1519     }
1520 }
1521
1522 /*
1523  * Report an error detected by the program we are processing.
1524  * Use the text of the line in the error message.
1525  * (We use error because it prints the filename & line#.)
1526  */
1527
1528 static int
1529 do_error (pfile, keyword)
1530      cpp_reader *pfile;
1531      const struct directive *keyword ATTRIBUTE_UNUSED;
1532 {
1533   U_CHAR *text, *limit;
1534
1535   cpp_skip_hspace (pfile);
1536   text = CPP_BUFFER (pfile)->cur;
1537   skip_rest_of_line (pfile);
1538   limit = CPP_BUFFER (pfile)->cur;
1539
1540   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1541   return 0;
1542 }
1543
1544 /*
1545  * Report a warning detected by the program we are processing.
1546  * Use the text of the line in the warning message, then continue.
1547  */
1548
1549 static int
1550 do_warning (pfile, keyword)
1551      cpp_reader *pfile;
1552      const struct directive *keyword ATTRIBUTE_UNUSED;
1553 {
1554   U_CHAR *text, *limit;
1555
1556   cpp_skip_hspace (pfile);
1557   text = CPP_BUFFER (pfile)->cur;
1558   skip_rest_of_line (pfile);
1559   limit = CPP_BUFFER (pfile)->cur;
1560
1561   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1562     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1563
1564   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1565   return 0;
1566 }
1567
1568 /* Report program identification.  */
1569
1570 static int
1571 do_ident (pfile, keyword)
1572      cpp_reader *pfile;
1573      const struct directive *keyword ATTRIBUTE_UNUSED;
1574 {
1575   long old_written = CPP_WRITTEN (pfile);
1576
1577   /* Allow #ident in system headers, since that's not user's fault.  */
1578   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1579     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1580
1581   CPP_PUTS (pfile, "#ident ", 7);
1582
1583   /* Next token should be a string constant.  */
1584   if (get_directive_token (pfile) == CPP_STRING)
1585     /* And then a newline.  */
1586     if (get_directive_token (pfile) == CPP_VSPACE)
1587       /* Good - ship it.  */
1588       return 0;
1589
1590   cpp_error (pfile, "invalid #ident");
1591   skip_rest_of_line (pfile);
1592   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
1593
1594   return 0;
1595 }
1596
1597 /* Pragmata handling.  We handle some of these, and pass the rest on
1598    to the front end.  C99 defines three pragmas and says that no macro
1599    expansion is to be performed on them; whether or not macro
1600    expansion happens for other pragmas is implementation defined.
1601    This implementation never macro-expands the text after #pragma.
1602
1603    We currently do not support the _Pragma operator.  Support for that
1604    has to be coordinated with the front end.  Proposed implementation:
1605    both #pragma blah blah and _Pragma("blah blah") become
1606    __builtin_pragma(blah blah) and we teach the parser about that.  */
1607
1608 /* Sub-handlers for the pragmas needing treatment here.
1609    They return 1 if the token buffer is to be popped, 0 if not. */
1610 static int do_pragma_once               PARAMS ((cpp_reader *));
1611 static int do_pragma_implementation     PARAMS ((cpp_reader *));
1612 static int do_pragma_poison             PARAMS ((cpp_reader *));
1613 static int do_pragma_default            PARAMS ((cpp_reader *));
1614
1615 static int
1616 do_pragma (pfile, keyword)
1617      cpp_reader *pfile;
1618      const struct directive *keyword ATTRIBUTE_UNUSED;
1619 {
1620   long here, key;
1621   U_CHAR *buf;
1622   int pop;
1623   enum cpp_token token;
1624
1625   here = CPP_WRITTEN (pfile);
1626   CPP_PUTS (pfile, "#pragma ", 8);
1627
1628   key = CPP_WRITTEN (pfile);
1629   pfile->no_macro_expand++;
1630   token = get_directive_token (pfile);
1631   if (token != CPP_NAME)
1632     {
1633       if (token == CPP_VSPACE)
1634         goto empty;
1635       else
1636         goto skip;
1637     }
1638
1639   buf = pfile->token_buffer + key;
1640   CPP_PUTC (pfile, ' ');
1641
1642 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1643   if (tokis ("once"))
1644     pop = do_pragma_once (pfile);
1645   else if (tokis ("implementation"))
1646     pop = do_pragma_implementation (pfile);
1647   else if (tokis ("poison"))
1648     pop = do_pragma_poison (pfile);
1649   else
1650     pop = do_pragma_default (pfile);
1651 #undef tokis
1652
1653   if (get_directive_token (pfile) != CPP_VSPACE)
1654     goto skip;
1655
1656   if (pop)
1657     CPP_SET_WRITTEN (pfile, here);
1658   pfile->no_macro_expand--;
1659   return 0;
1660
1661  skip:
1662   cpp_error (pfile, "malformed #pragma directive");
1663   skip_rest_of_line (pfile);
1664  empty:
1665   CPP_SET_WRITTEN (pfile, here);
1666   pfile->no_macro_expand--;
1667   return 0;
1668 }
1669
1670 static int
1671 do_pragma_default (pfile)
1672      cpp_reader *pfile;
1673 {
1674   while (get_directive_token (pfile) != CPP_VSPACE)
1675     CPP_PUTC (pfile, ' ');
1676   return 0;
1677 }
1678
1679 static int
1680 do_pragma_once (pfile)
1681      cpp_reader *pfile;
1682 {
1683   cpp_buffer *ip = CPP_BUFFER (pfile);
1684
1685   if (ip->fname == NULL)
1686     {
1687       cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1688       return 1;
1689     }
1690   
1691   /* Allow #pragma once in system headers, since that's not the user's
1692      fault.  */
1693   if (!ip->system_header_p)
1694     cpp_warning (pfile, "`#pragma once' is obsolete");
1695       
1696   if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1697     cpp_warning (pfile, "`#pragma once' outside include file");
1698   else
1699     ip->ihash->control_macro = "";  /* never repeat */
1700
1701   return 1;
1702 }
1703
1704 static int
1705 do_pragma_implementation (pfile)
1706      cpp_reader *pfile;
1707 {
1708   /* Be quiet about `#pragma implementation' for a file only if it hasn't
1709      been included yet.  */
1710   struct include_hash *ptr;
1711   enum cpp_token token;
1712   long written = CPP_WRITTEN (pfile);
1713   U_CHAR *name;
1714   U_CHAR *copy;
1715
1716   token = get_directive_token (pfile);
1717   if (token == CPP_VSPACE)
1718     return 0;
1719   else if (token != CPP_STRING)
1720     {
1721       cpp_error (pfile, "malformed #pragma implementation");
1722       return 1;
1723     }
1724
1725   name = pfile->token_buffer + written + 1;
1726   copy = xstrdup (name);
1727   copy[strlen(copy)] = '\0';  /* trim trailing quote */
1728   
1729   ptr = include_hash (pfile, copy, 0);
1730   if (ptr)
1731     cpp_warning (pfile,
1732          "`#pragma implementation' for `%s' appears after file is included",
1733                  copy);
1734   free (copy);
1735   return 0;
1736 }
1737
1738 static int
1739 do_pragma_poison (pfile)
1740      cpp_reader *pfile;
1741 {
1742   /* Poison these symbols so that all subsequent usage produces an
1743      error message.  */
1744   U_CHAR *p;
1745   HASHNODE *hp;
1746   long written;
1747   size_t len;
1748   enum cpp_token token;
1749   int writeit;
1750   /* As a rule, don't include #pragma poison commands in output,  
1751      unless the user asks for them.  */
1752   writeit = (CPP_OPTIONS (pfile)->debug_output
1753              || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1754              || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1755
1756   for (;;)
1757     {
1758       written = CPP_WRITTEN (pfile);
1759       token = get_directive_token (pfile);
1760       if (token == CPP_VSPACE)
1761         break;
1762       if (token != CPP_NAME)
1763         {
1764           cpp_error (pfile, "invalid #pragma poison directive");
1765           skip_rest_of_line (pfile);
1766           return 1;
1767         }
1768
1769       p = pfile->token_buffer + written;
1770       len = strlen (p);
1771       if ((hp = cpp_lookup (pfile, p, len)))
1772         {
1773           if (hp->type != T_POISON)
1774             {
1775               cpp_warning (pfile, "poisoning existing macro `%s'", p);
1776               free_definition (hp->value.defn);
1777               hp->value.defn = 0;
1778               hp->type = T_POISON;
1779             }
1780         }
1781       else
1782         cpp_install (pfile, p, len, T_POISON, 0);
1783       if (writeit)
1784         CPP_PUTC (pfile, ' ');
1785     }
1786   return !writeit;
1787 }
1788  
1789 #ifdef SCCS_DIRECTIVE
1790 /* Just ignore #sccs, on systems where we define it at all.  */
1791
1792 static int
1793 do_sccs (pfile, keyword)
1794      cpp_reader *pfile;
1795      const struct directive *keyword ATTRIBUTE_UNUSED;
1796 {
1797   if (CPP_PEDANTIC (pfile))
1798     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1799   skip_rest_of_line (pfile);
1800   return 0;
1801 }
1802 #endif
1803
1804 /* We've found an `#if' directive.  If the only thing before it in
1805    this file is white space, and if it is of the form
1806    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1807    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1808    for an explanation of controlling macros.)  If so, return a
1809    malloc'd copy of SYMBOL.  Otherwise, return NULL.  */
1810
1811 static U_CHAR *
1812 detect_if_not_defined (pfile)
1813      cpp_reader *pfile;
1814 {
1815   U_CHAR *control_macro = 0;
1816
1817   if (pfile->only_seen_white == 2)
1818     {
1819       char *ident;
1820       enum cpp_token token;
1821       int base_offset;
1822       int token_offset;
1823       int need_rparen = 0;
1824
1825       /* Save state required for restore.  */
1826       pfile->no_macro_expand++;
1827       parse_set_mark (pfile);
1828       base_offset = CPP_WRITTEN (pfile);
1829
1830       /* Look for `!', */
1831       if (get_directive_token (pfile) != CPP_OTHER
1832           || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1833           || CPP_PWRITTEN (pfile)[-1] != '!')
1834         goto restore;
1835
1836       /* ...then `defined', */
1837       token_offset = CPP_WRITTEN (pfile);
1838       token = get_directive_token (pfile);
1839       if (token != CPP_NAME)
1840         goto restore;
1841       ident = pfile->token_buffer + token_offset;
1842       CPP_NUL_TERMINATE (pfile);
1843       if (strcmp (ident, "defined"))
1844         goto restore;
1845
1846       /* ...then an optional '(' and the name, */
1847       token_offset = CPP_WRITTEN (pfile);
1848       token = get_directive_token (pfile);
1849       if (token == CPP_LPAREN)
1850         {
1851           token_offset = CPP_WRITTEN (pfile);
1852           token = get_directive_token (pfile);
1853           if (token != CPP_NAME)
1854             goto restore;
1855           need_rparen = 1;
1856         }
1857       else if (token != CPP_NAME)
1858         goto restore;
1859
1860       ident = pfile->token_buffer + token_offset;
1861       CPP_NUL_TERMINATE (pfile);
1862
1863       /* ...then the ')', if necessary, */
1864       if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1865           /* ...and make sure there's nothing else on the line.  */
1866           && get_directive_token (pfile) == CPP_VSPACE)
1867         control_macro = xstrdup (ident);
1868
1869     restore:
1870       CPP_SET_WRITTEN (pfile, base_offset);
1871       pfile->no_macro_expand--;
1872       parse_goto_mark (pfile);
1873     }
1874
1875   return control_macro;
1876 }
1877
1878 /*
1879  * handle #if command by
1880  *   1) inserting special `defined' keyword into the hash table
1881  *      that gets turned into 0 or 1 by special_symbol (thus,
1882  *      if the luser has a symbol called `defined' already, it won't
1883  *      work inside the #if command)
1884  *   2) rescan the input into a temporary output buffer
1885  *   3) pass the output buffer to the yacc parser and collect a value
1886  *   4) clean up the mess left from steps 1 and 2.
1887  *   5) call conditional_skip to skip til the next #endif (etc.),
1888  *      or not, depending on the value from step 3.
1889  */
1890
1891 static int
1892 do_if (pfile, keyword)
1893      cpp_reader *pfile;
1894      const struct directive *keyword ATTRIBUTE_UNUSED;
1895 {
1896   U_CHAR *control_macro = detect_if_not_defined (pfile);
1897   HOST_WIDEST_INT value = eval_if_expression (pfile);
1898   conditional_skip (pfile, value == 0, T_IF, control_macro);
1899   return 0;
1900 }
1901
1902 /*
1903  * handle a #elif directive by not changing  if_stack  either.
1904  * see the comment above do_else.
1905  */
1906
1907 static int
1908 do_elif (pfile, keyword)
1909      cpp_reader *pfile;
1910      const struct directive *keyword ATTRIBUTE_UNUSED;
1911 {
1912   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1913     {
1914       cpp_error (pfile, "`#elif' not within a conditional");
1915       return 0;
1916     }
1917   else
1918     {
1919       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1920         {
1921           cpp_error (pfile, "`#elif' after `#else'");
1922           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1923                                "the conditional began here");
1924         }
1925       pfile->if_stack->type = T_ELIF;
1926     }
1927
1928   if (pfile->if_stack->if_succeeded)
1929     skip_if_group (pfile);
1930   else
1931     {
1932       HOST_WIDEST_INT value = eval_if_expression (pfile);
1933       if (value == 0)
1934         skip_if_group (pfile);
1935       else
1936         {
1937           ++pfile->if_stack->if_succeeded;      /* continue processing input */
1938           output_line_command (pfile, same_file);
1939         }
1940     }
1941   return 0;
1942 }
1943
1944 /*
1945  * evaluate a #if expression in BUF, of length LENGTH,
1946  * then parse the result as a C expression and return the value as an int.
1947  */
1948
1949 static HOST_WIDEST_INT
1950 eval_if_expression (pfile)
1951      cpp_reader *pfile;
1952 {
1953   HOST_WIDEST_INT value;
1954   long old_written = CPP_WRITTEN (pfile);
1955
1956   pfile->parsing_if_directive++;
1957   value = cpp_parse_expr (pfile);
1958   pfile->parsing_if_directive--;
1959
1960   skip_rest_of_line (pfile);
1961   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1962
1963   return value;
1964 }
1965
1966 /*
1967  * routine to handle ifdef/ifndef.  Try to look up the symbol,
1968  * then do or don't skip to the #endif/#else/#elif depending
1969  * on what directive is actually being processed.
1970  */
1971
1972 static int
1973 do_xifdef (pfile, keyword)
1974      cpp_reader *pfile;
1975      const struct directive *keyword;
1976 {
1977   int skip;
1978   cpp_buffer *ip = CPP_BUFFER (pfile);
1979   U_CHAR *ident;
1980   int ident_length;
1981   enum cpp_token token;
1982   int start_of_file = 0;
1983   U_CHAR *control_macro = 0;
1984   int old_written = CPP_WRITTEN (pfile);
1985
1986   /* Detect a #ifndef at start of file (not counting comments).  */
1987   if (ip->fname != 0 && keyword->type == T_IFNDEF)
1988     start_of_file = pfile->only_seen_white == 2;
1989
1990   pfile->no_macro_expand++;
1991   token = get_directive_token (pfile);
1992   pfile->no_macro_expand--;
1993
1994   ident = pfile->token_buffer + old_written;
1995   ident_length = CPP_WRITTEN (pfile) - old_written;
1996   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1997
1998   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1999     {
2000       skip = (keyword->type == T_IFDEF);
2001       if (! CPP_TRADITIONAL (pfile))
2002         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2003     }
2004   else if (token == CPP_NAME)
2005     {
2006       skip = cpp_defined (pfile, ident, ident_length);
2007       if (keyword->type == T_IFDEF)
2008         skip = !skip;
2009
2010       if (start_of_file && !skip)
2011         {
2012           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2013           bcopy (ident, control_macro, ident_length + 1);
2014         }
2015     }
2016   else
2017     {
2018       skip = (keyword->type == T_IFDEF);
2019       if (! CPP_TRADITIONAL (pfile))
2020         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2021     }
2022
2023   if (!CPP_TRADITIONAL (pfile))
2024     { int c;
2025       cpp_skip_hspace (pfile);
2026       c = PEEKC ();
2027       if (c != EOF && c != '\n')
2028         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2029     }
2030   skip_rest_of_line (pfile);
2031
2032   conditional_skip (pfile, skip, T_IF, control_macro);
2033   return 0;
2034 }
2035
2036 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2037    If this is a #ifndef starting at the beginning of a file,
2038    CONTROL_MACRO is the macro name tested by the #ifndef.
2039    Otherwise, CONTROL_MACRO is 0.  */
2040
2041 static void
2042 conditional_skip (pfile, skip, type, control_macro)
2043      cpp_reader *pfile;
2044      int skip;
2045      enum node_type type;
2046      U_CHAR *control_macro;
2047 {
2048   IF_STACK_FRAME *temp;
2049
2050   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2051   temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2052   temp->lineno = CPP_BUFFER (pfile)->lineno;
2053   temp->next = pfile->if_stack;
2054   temp->control_macro = control_macro;
2055   pfile->if_stack = temp;
2056
2057   pfile->if_stack->type = type;
2058
2059   if (skip != 0) {
2060     skip_if_group (pfile);
2061     return;
2062   } else {
2063     ++pfile->if_stack->if_succeeded;
2064     output_line_command (pfile, same_file);
2065   }
2066 }
2067
2068 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
2069    return 0 if skipping should continue, 1 if it should halt.  Also
2070    adjusts the if_stack as appropriate.
2071    The `#' has been read, but not the identifier. */
2072
2073 static int
2074 consider_directive_while_skipping (pfile, stack)
2075     cpp_reader *pfile;
2076     IF_STACK_FRAME *stack; 
2077 {
2078   long ident_len, ident;
2079   const struct directive *kt;
2080   IF_STACK_FRAME *temp;
2081     
2082   cpp_skip_hspace (pfile);
2083
2084   ident = CPP_WRITTEN (pfile);
2085   parse_name (pfile, GETC());
2086   ident_len = CPP_WRITTEN (pfile) - ident;
2087
2088   CPP_SET_WRITTEN (pfile, ident);
2089
2090   for (kt = directive_table; kt->length >= 0; kt++)
2091     if (kt->length == ident_len
2092         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2093       switch (kt->type)
2094         {
2095         case T_IF:
2096         case T_IFDEF:
2097         case T_IFNDEF:
2098             temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2099             temp->next = pfile->if_stack;
2100             pfile->if_stack = temp;
2101             temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2102             temp->type = kt->type;
2103             return 0;
2104
2105         case T_ELSE:
2106             if (pfile->if_stack != stack)
2107               validate_else (pfile, "#else");
2108             /* fall through */
2109         case T_ELIF:
2110             if (pfile->if_stack == stack)
2111               return 1;
2112             else
2113               {
2114                 pfile->if_stack->type = kt->type;
2115                 return 0;
2116               }
2117
2118             case T_ENDIF:
2119                 if (pfile->if_stack != stack)
2120                   validate_else (pfile, "#endif");
2121
2122                 if (pfile->if_stack == stack)
2123                   return 1;
2124                     
2125                 temp = pfile->if_stack;
2126                 pfile->if_stack = temp->next;
2127                 free (temp);
2128                 return 0;
2129
2130             default:
2131                 return 0;
2132             }
2133
2134     /* Don't let erroneous code go by.  */
2135     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2136         cpp_pedwarn (pfile, "invalid preprocessor directive name");
2137     return 0;
2138 }
2139
2140 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
2141  * leaves input ptr at the sharp sign found.
2142  */
2143 static void
2144 skip_if_group (pfile)
2145     cpp_reader *pfile;
2146 {
2147   int c;
2148   IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2149   U_CHAR *beg_of_line;
2150   long old_written;
2151
2152   old_written = CPP_WRITTEN (pfile);
2153   
2154   for (;;)
2155     {
2156       beg_of_line = CPP_BUFFER (pfile)->cur;
2157
2158       if (! CPP_TRADITIONAL (pfile))
2159         cpp_skip_hspace (pfile);
2160       c = GETC();
2161       if (c == '\n')
2162         {
2163           CPP_BUMP_LINE (pfile);
2164           continue;
2165         }
2166       else if (c == '#')
2167         {
2168           if (consider_directive_while_skipping (pfile, save_if_stack))
2169             break;
2170         }
2171       else if (c == EOF)
2172         return;  /* Caller will issue error. */
2173
2174       FORWARD(-1);
2175       skip_rest_of_line (pfile);
2176
2177       c = GETC();
2178       if (c == EOF)
2179         return;  /* Caller will issue error. */
2180       else
2181         CPP_BUMP_LINE (pfile);
2182     }     
2183
2184   /* Back up to the beginning of this line.  Caller will process the
2185      directive. */
2186   CPP_BUFFER (pfile)->cur = beg_of_line;
2187   pfile->only_seen_white = 1;
2188 }
2189
2190 /*
2191  * handle a #else directive.  Do this by just continuing processing
2192  * without changing  if_stack ;  this is so that the error message
2193  * for missing #endif's etc. will point to the original #if.  It
2194  * is possible that something different would be better.
2195  */
2196
2197 static int
2198 do_else (pfile, keyword)
2199      cpp_reader *pfile;
2200      const struct directive *keyword ATTRIBUTE_UNUSED;
2201 {
2202   validate_else (pfile, "#else");
2203   skip_rest_of_line (pfile);
2204
2205   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2206     {
2207       cpp_error (pfile, "`#else' not within a conditional");
2208       return 0;
2209     }
2210   else
2211     {
2212       /* #ifndef can't have its special treatment for containing the whole file
2213          if it has a #else clause.  */
2214       pfile->if_stack->control_macro = 0;
2215
2216       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2217         {
2218           cpp_error (pfile, "`#else' after `#else'");
2219           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2220                                "the conditional began here");
2221         }
2222       pfile->if_stack->type = T_ELSE;
2223     }
2224
2225   if (pfile->if_stack->if_succeeded)
2226     skip_if_group (pfile);
2227   else
2228     {
2229       ++pfile->if_stack->if_succeeded;  /* continue processing input */
2230       output_line_command (pfile, same_file);
2231     }
2232   return 0;
2233 }
2234
2235 /*
2236  * unstack after #endif command
2237  */
2238
2239 static int
2240 do_endif (pfile, keyword)
2241      cpp_reader *pfile;
2242      const struct directive *keyword ATTRIBUTE_UNUSED;
2243 {
2244   validate_else (pfile, "#endif");
2245   skip_rest_of_line (pfile);
2246
2247   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2248     cpp_error (pfile, "`#endif' not within a conditional");
2249   else
2250     {
2251       IF_STACK_FRAME *temp = pfile->if_stack;
2252       pfile->if_stack = temp->next;
2253       if (temp->control_macro != 0)
2254         {
2255           /* This #endif matched a #ifndef at the start of the file.
2256              See if it is at the end of the file.  */
2257           int c;
2258
2259           parse_set_mark (pfile);
2260
2261           for (;;)
2262             {
2263               cpp_skip_hspace (pfile);
2264               c = GETC ();
2265               if (c != '\n')
2266                 break;
2267             }
2268           parse_goto_mark (pfile);
2269
2270           if (c == EOF)
2271             {
2272               /* This #endif ends a #ifndef
2273                  that contains all of the file (aside from whitespace).
2274                  Arrange not to include the file again
2275                  if the macro that was tested is defined. */
2276               struct cpp_buffer *ip;
2277               for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2278                 if (ip->fname != NULL)
2279                   break;
2280               ip->ihash->control_macro = (char *) temp->control_macro;
2281             }
2282         }
2283       free (temp);
2284       output_line_command (pfile, same_file);
2285     }
2286   return 0;
2287 }
2288
2289 /* Issue -pedantic warning for text which is not a comment following
2290    an #else or #endif.  Do not warn in system headers, as this is harmless
2291    and very common on old systems.  */
2292
2293 static void
2294 validate_else (pfile, directive)
2295      cpp_reader *pfile;
2296      const char *directive;
2297 {
2298   if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2299     return;
2300
2301   cpp_skip_hspace (pfile);
2302   if (PEEKC () != '\n')
2303     cpp_pedwarn (pfile,
2304                  "text following `%s' violates ANSI standard", directive);
2305 }
2306
2307 /* Convert T_IF, etc. to a string.   Used in error messages.  */
2308 static const char *
2309 if_directive_name (pfile, ifs)
2310      cpp_reader *pfile;
2311      struct if_stack *ifs;
2312 {
2313   switch (ifs->type)
2314     {
2315     case T_IF:      return "#if";
2316     case T_IFDEF:   return "#ifdef";
2317     case T_IFNDEF:  return "#ifndef";
2318     case T_ELIF:    return "#elif";
2319     case T_ELSE:    return "#else";
2320     default:
2321       cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2322       return "unknown";
2323     }
2324 }
2325
2326 /* Get the next token, and add it to the text in pfile->token_buffer.
2327    Return the kind of token we got.  */
2328
2329 enum cpp_token
2330 cpp_get_token (pfile)
2331      cpp_reader *pfile;
2332 {
2333   register int c, c2, c3;
2334   enum cpp_token token;
2335   struct cpp_options *opts = CPP_OPTIONS (pfile);
2336
2337  get_next:
2338   c = GETC();
2339   if (c == EOF)
2340     {
2341       if (CPP_BUFFER (pfile)->manual_pop)
2342         /* If we've been reading from redirected input, the
2343            frontend will pop the buffer.  */
2344         return CPP_EOF;
2345       else if (CPP_BUFFER (pfile)->seen_eof)
2346         {
2347           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2348             return CPP_EOF;
2349
2350           cpp_pop_buffer (pfile);
2351           goto get_next;
2352         }
2353       else
2354         {
2355           cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2356           struct if_stack *ifs, *nifs;
2357
2358           /* Unwind the conditional stack and generate error messages.  */
2359           for (ifs = pfile->if_stack;
2360                ifs != CPP_BUFFER (pfile)->if_stack;
2361                ifs = nifs)
2362             {
2363               cpp_error_with_line (pfile, ifs->lineno, -1,
2364                                    "unterminated `%s' conditional",
2365                                    if_directive_name (pfile, ifs));
2366
2367               nifs = ifs->next;
2368               free (ifs);
2369             }
2370           pfile->if_stack = ifs;
2371
2372           if (CPP_BUFFER (pfile)->nominal_fname
2373               && next_buf != CPP_NULL_BUFFER (pfile))
2374             {
2375               /* We're about to return from an #include file.
2376                  Emit #line information now (as part of the CPP_POP) result.
2377                  But the #line refers to the file we will pop to.  */
2378               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2379               CPP_BUFFER (pfile) = next_buf;
2380               pfile->input_stack_listing_current = 0;
2381               output_line_command (pfile, leave_file);
2382               CPP_BUFFER (pfile) = cur_buffer;
2383             }
2384
2385           CPP_BUFFER (pfile)->seen_eof = 1;
2386           return CPP_POP;
2387         }
2388     }
2389   else
2390     {
2391       switch (c)
2392         {
2393         case '/':
2394           if (PEEKC () == '=')
2395             goto op2;
2396
2397         comment:
2398           if (opts->discard_comments)
2399             c = skip_comment (pfile, c);
2400           else
2401             c = copy_comment (pfile, c);
2402           if (c != ' ')
2403             goto randomchar;
2404           
2405           /* Comments are equivalent to spaces.
2406              For -traditional, a comment is equivalent to nothing.  */
2407           if (opts->traditional || !opts->discard_comments)
2408             return CPP_COMMENT;
2409           else
2410             {
2411               CPP_PUTC (pfile, c);
2412               return CPP_HSPACE;
2413             }
2414
2415         case '#':
2416           if (pfile->parsing_if_directive)
2417             {
2418               cpp_skip_hspace (pfile);
2419               parse_assertion (pfile);
2420               return CPP_ASSERTION;
2421             }
2422
2423           if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2424             {
2425               CPP_RESERVE (pfile, 3);
2426               CPP_PUTC_Q (pfile, '#');
2427               CPP_NUL_TERMINATE_Q (pfile);
2428               if (PEEKC () != '#')
2429                 return CPP_STRINGIZE;
2430               
2431               FORWARD (1);
2432               CPP_PUTC_Q (pfile, '#');
2433               CPP_NUL_TERMINATE_Q (pfile);
2434               return CPP_TOKPASTE;
2435             }
2436
2437           if (!pfile->only_seen_white)
2438             goto randomchar;
2439           /* -traditional directives are recognized only with the # in
2440              column 1.
2441              XXX Layering violation.  */
2442           if (CPP_TRADITIONAL (pfile)
2443               && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2444             goto randomchar;
2445           if (handle_directive (pfile))
2446             return CPP_DIRECTIVE;
2447           pfile->only_seen_white = 0;
2448           goto randomchar;
2449
2450         case '\"':
2451         case '\'':
2452           parse_string (pfile, c);
2453           pfile->only_seen_white = 0;
2454           return c == '\'' ? CPP_CHAR : CPP_STRING;
2455
2456         case '$':
2457           if (!opts->dollars_in_ident)
2458             goto randomchar;
2459           goto letter;
2460
2461         case ':':
2462           if (opts->cplusplus && PEEKC () == ':')
2463             goto op2;
2464           goto randomchar;
2465
2466         case '&':
2467         case '+':
2468         case '|':
2469           c2 = PEEKC ();
2470           if (c2 == c || c2 == '=')
2471             goto op2;
2472           goto randomchar;
2473
2474         case '*':
2475         case '!':
2476         case '%':
2477         case '=':
2478         case '^':
2479           if (PEEKC () == '=')
2480             goto op2;
2481           goto randomchar;
2482
2483         case '-':
2484           c2 = PEEKC ();
2485           if (c2 == '-' && opts->chill)
2486             goto comment;  /* Chill style comment */
2487           if (c2 == '-' || c2 == '=')
2488             goto op2;
2489           if (c2 == '>')
2490             {
2491               if (opts->cplusplus && PEEKN (1) == '*')
2492                 {
2493                   /* In C++, there's a ->* operator.  */
2494                   token = CPP_OTHER;
2495                   pfile->only_seen_white = 0;
2496                   CPP_RESERVE (pfile, 4);
2497                   CPP_PUTC_Q (pfile, c);
2498                   CPP_PUTC_Q (pfile, GETC ());
2499                   CPP_PUTC_Q (pfile, GETC ());
2500                   CPP_NUL_TERMINATE_Q (pfile);
2501                   return token;
2502                 }
2503               goto op2;
2504             }
2505           goto randomchar;
2506
2507         case '<':
2508           if (pfile->parsing_include_directive)
2509             {
2510               for (;;)
2511                 {
2512                   CPP_PUTC (pfile, c);
2513                   if (c == '>')
2514                     break;
2515                   c = GETC ();
2516                   if (c == '\n' || c == EOF)
2517                     {
2518                       cpp_error (pfile,
2519                                  "missing '>' in `#include <FILENAME>'");
2520                       break;
2521                     }
2522                   else if (c == '\r')
2523                     {
2524                       if (!CPP_BUFFER (pfile)->has_escapes)
2525                         {
2526                           /* Backslash newline is replaced by nothing. */
2527                           CPP_ADJUST_WRITTEN (pfile, -1);
2528                           CPP_BUMP_LINE (pfile);
2529                         }
2530                       else
2531                         {
2532                           /* We might conceivably get \r- or \r<space> in
2533                              here.  Just delete 'em. */
2534                           int d = GETC();
2535                           if (d != '-' && d != ' ')
2536                             cpp_ice (pfile, "unrecognized escape \\r%c", d);
2537                           CPP_ADJUST_WRITTEN (pfile, -1);
2538                         }                         
2539                     }
2540                 }
2541               return CPP_STRING;
2542             }
2543           /* else fall through */
2544         case '>':
2545           c2 = PEEKC ();
2546           if (c2 == '=')
2547             goto op2;
2548           /* GNU C++ supports MIN and MAX operators <? and >?.  */
2549           if (c2 != c && (!opts->cplusplus || c2 != '?'))
2550             goto randomchar;
2551           FORWARD(1);
2552           CPP_RESERVE (pfile, 4);
2553           CPP_PUTC (pfile, c);
2554           CPP_PUTC (pfile, c2);
2555           c3 = PEEKC ();
2556           if (c3 == '=')
2557             CPP_PUTC_Q (pfile, GETC ());
2558           CPP_NUL_TERMINATE_Q (pfile);
2559           pfile->only_seen_white = 0;
2560           return CPP_OTHER;
2561
2562         case '.':
2563           c2 = PEEKC ();
2564           if (ISDIGIT(c2))
2565             {
2566               CPP_RESERVE(pfile, 2);
2567               CPP_PUTC_Q (pfile, '.');
2568               c = GETC ();
2569               goto number;
2570             }
2571
2572           /* In C++ there's a .* operator.  */
2573           if (opts->cplusplus && c2 == '*')
2574             goto op2;
2575
2576           if (c2 == '.' && PEEKN(1) == '.')
2577             {
2578               CPP_RESERVE(pfile, 4);
2579               CPP_PUTC_Q (pfile, '.');
2580               CPP_PUTC_Q (pfile, '.');
2581               CPP_PUTC_Q (pfile, '.');
2582               FORWARD (2);
2583               CPP_NUL_TERMINATE_Q (pfile);
2584               pfile->only_seen_white = 0;
2585               return CPP_3DOTS;
2586             }
2587           goto randomchar;
2588
2589         op2:
2590           token = CPP_OTHER;
2591           pfile->only_seen_white = 0;
2592           CPP_RESERVE(pfile, 3);
2593           CPP_PUTC_Q (pfile, c);
2594           CPP_PUTC_Q (pfile, GETC ());
2595           CPP_NUL_TERMINATE_Q (pfile);
2596           return token;
2597
2598         case 'L':
2599           c2 = PEEKC ();
2600           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2601             {
2602               CPP_PUTC (pfile, c);
2603               c = GETC ();
2604               parse_string (pfile, c);
2605               pfile->only_seen_white = 0;
2606               return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2607             }
2608           goto letter;
2609
2610         case '0': case '1': case '2': case '3': case '4':
2611         case '5': case '6': case '7': case '8': case '9':
2612         number:
2613           c2  = '.';
2614           for (;;)
2615             {
2616               CPP_RESERVE (pfile, 2);
2617               CPP_PUTC_Q (pfile, c);
2618               c = PEEKC ();
2619               if (c == EOF)
2620                 break;
2621               if (!is_idchar(c) && c != '.'
2622                   && ((c2 != 'e' && c2 != 'E'
2623                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2624                       || (c != '+' && c != '-')))
2625                 break;
2626               FORWARD(1);
2627               c2= c;
2628             }
2629           CPP_NUL_TERMINATE_Q (pfile);
2630           pfile->only_seen_white = 0;
2631           return CPP_NUMBER;
2632         case 'b': case 'c': case 'd': case 'h': case 'o':
2633         case 'B': case 'C': case 'D': case 'H': case 'O':
2634           if (opts->chill && PEEKC () == '\'')
2635             {
2636               pfile->only_seen_white = 0;
2637               CPP_RESERVE (pfile, 2);
2638               CPP_PUTC_Q (pfile, c);
2639               CPP_PUTC_Q (pfile, '\'');
2640               FORWARD(1);
2641               for (;;)
2642                 {
2643                   c = GETC();
2644                   if (c == EOF)
2645                     goto chill_number_eof;
2646                   if (!is_idchar(c))
2647                     break;
2648                   CPP_PUTC (pfile, c);
2649                 }
2650               if (c == '\'')
2651                 {
2652                   CPP_RESERVE (pfile, 2);
2653                   CPP_PUTC_Q (pfile, c);
2654                   CPP_NUL_TERMINATE_Q (pfile);
2655                   return CPP_STRING;
2656                 }
2657               else
2658                 {
2659                   FORWARD(-1);
2660                 chill_number_eof:
2661                   CPP_NUL_TERMINATE (pfile);
2662                   return CPP_NUMBER;
2663                 }
2664             }
2665           else
2666             goto letter;
2667         case '_':
2668         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2669         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2670         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2671         case 'x': case 'y': case 'z':
2672         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2673         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2674         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2675         case 'Y': case 'Z':
2676         letter:
2677           {
2678             HASHNODE *hp;
2679             unsigned char *ident;
2680             int before_name_written = CPP_WRITTEN (pfile);
2681             int ident_len;
2682             parse_name (pfile, c);
2683             pfile->only_seen_white = 0;
2684             if (pfile->no_macro_expand)
2685               return CPP_NAME;
2686             ident = pfile->token_buffer + before_name_written;
2687             ident_len = CPP_PWRITTEN (pfile) - ident;
2688             hp = cpp_lookup (pfile, ident, ident_len);
2689             if (!hp)
2690               return CPP_NAME;
2691             if (hp->type == T_DISABLED)
2692               {
2693                 if (pfile->output_escapes)
2694                   { /* Return "\r-IDENT", followed by '\0'.  */
2695                     int i;
2696                     CPP_RESERVE (pfile, 3);
2697                     ident = pfile->token_buffer + before_name_written;
2698                     CPP_ADJUST_WRITTEN (pfile, 2);
2699                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2700                     ident[0] = '\r';
2701                     ident[1] = '-';
2702                   }
2703                 return CPP_NAME;
2704               }
2705
2706             /* If macro wants an arglist, verify that a '(' follows.  */
2707             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2708             {
2709               int macbuf_whitespace = 0;
2710
2711               while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2712                 {
2713                   U_CHAR *point = CPP_BUFFER (pfile)->cur;
2714                   for (;;)
2715                     {
2716                       cpp_skip_hspace (pfile);
2717                       c = PEEKC ();
2718                       if (c == '\n')
2719                         FORWARD(1);
2720                       else
2721                         break;
2722                     }
2723                   if (point != CPP_BUFFER (pfile)->cur)
2724                     macbuf_whitespace = 1;
2725                   if (c == '(')
2726                     goto is_macro_call;
2727                   else if (c != EOF)
2728                     goto not_macro_call;
2729                   cpp_pop_buffer (pfile);
2730                 }
2731
2732               parse_set_mark (pfile);
2733               for (;;)
2734                 {
2735                   cpp_skip_hspace (pfile);
2736                   c = PEEKC ();
2737                   if (c == '\n')
2738                     FORWARD(1);
2739                   else
2740                     break;
2741                 }
2742               parse_goto_mark (pfile);
2743
2744               if (c == '(')
2745                 goto is_macro_call;
2746
2747             not_macro_call:
2748               if (macbuf_whitespace)
2749                 CPP_PUTC (pfile, ' ');
2750               return CPP_NAME;
2751             }
2752           is_macro_call:
2753             /* This is now known to be a macro call.
2754                Expand the macro, reading arguments as needed,
2755                and push the expansion on the input stack.  */
2756             macroexpand (pfile, hp);
2757             CPP_SET_WRITTEN (pfile, before_name_written);
2758           }
2759           goto get_next;
2760
2761         case ' ':  case '\t':  case '\v':
2762           for (;;)
2763             {
2764               CPP_PUTC (pfile, c);
2765               c = PEEKC ();
2766               if (c == EOF || !is_hspace(c))
2767                 break;
2768               FORWARD(1);
2769             }
2770           return CPP_HSPACE;
2771
2772         case '\r':
2773           if (CPP_BUFFER (pfile)->has_escapes)
2774             {
2775               c = GETC ();
2776               if (c == '-')
2777                 {
2778                   if (pfile->output_escapes)
2779                     CPP_PUTS (pfile, "\r-", 2);
2780                   parse_name (pfile, GETC ());
2781                   return CPP_NAME;
2782                 }
2783               else if (c == ' ')
2784                 {
2785                   CPP_RESERVE (pfile, 2);
2786                   if (pfile->output_escapes)
2787                     CPP_PUTC_Q (pfile, '\r');
2788                   CPP_PUTC_Q (pfile, c);
2789                   return CPP_HSPACE;
2790                 }
2791               else
2792                 {
2793                   cpp_ice (pfile, "unrecognized escape \\r%c", c);
2794                   goto get_next;
2795                 }
2796             }
2797           else
2798             {
2799               /* Backslash newline is ignored. */
2800               CPP_BUMP_LINE (pfile);
2801               goto get_next;
2802             }
2803
2804         case '\n':
2805           CPP_PUTC (pfile, c);
2806           if (pfile->only_seen_white == 0)
2807             pfile->only_seen_white = 1;
2808           CPP_BUMP_LINE (pfile);
2809           if (! CPP_OPTIONS (pfile)->no_line_commands)
2810             {
2811               pfile->lineno++;
2812               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2813                 output_line_command (pfile, same_file);
2814             }
2815           return CPP_VSPACE;
2816
2817         case '(': token = CPP_LPAREN;    goto char1;
2818         case ')': token = CPP_RPAREN;    goto char1;
2819         case '{': token = CPP_LBRACE;    goto char1;
2820         case '}': token = CPP_RBRACE;    goto char1;
2821         case ',': token = CPP_COMMA;     goto char1;
2822         case ';': token = CPP_SEMICOLON; goto char1;
2823
2824         randomchar:
2825         default:
2826           token = CPP_OTHER;
2827         char1:
2828           pfile->only_seen_white = 0;
2829           CPP_PUTC (pfile, c);
2830           return token;
2831         }
2832     }
2833 }
2834
2835 /* Like cpp_get_token, but skip spaces and comments.  */
2836
2837 enum cpp_token
2838 cpp_get_non_space_token (pfile)
2839      cpp_reader *pfile;
2840 {
2841   int old_written = CPP_WRITTEN (pfile);
2842   for (;;)
2843     {
2844       enum cpp_token token = cpp_get_token (pfile);
2845       if (token != CPP_COMMENT && token != CPP_POP
2846           && token != CPP_HSPACE && token != CPP_VSPACE)
2847         return token;
2848       CPP_SET_WRITTEN (pfile, old_written);
2849     }
2850 }
2851
2852 /* Parse an identifier starting with C.  */
2853
2854 static void
2855 parse_name (pfile, c)
2856      cpp_reader *pfile;
2857      int c;
2858 {
2859   for (;;)
2860   {
2861       if (! is_idchar(c))
2862       {
2863           FORWARD (-1);
2864           break;
2865       }
2866
2867       if (c == '$' && CPP_PEDANTIC (pfile))
2868         cpp_pedwarn (pfile, "`$' in identifier");
2869
2870       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2871       CPP_PUTC_Q (pfile, c);
2872       c = GETC();
2873       if (c == EOF)
2874         break;
2875   }
2876   CPP_NUL_TERMINATE_Q (pfile);
2877   return;
2878 }
2879
2880 /* Parse and skip over a string starting with C.  A single quoted
2881    string is treated like a double -- some programs (e.g., troff) are
2882    perverse this way.  (However, a single quoted string is not allowed
2883    to extend over multiple lines.)  */
2884 static void
2885 skip_string (pfile, c)
2886      cpp_reader *pfile;
2887      int c;
2888 {
2889   long start_line, start_column;
2890   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2891
2892   while (1)
2893     {
2894       int cc = GETC();
2895       switch (cc)
2896         {
2897         case EOF:
2898           cpp_error_with_line (pfile, start_line, start_column,
2899                                "unterminated string or character constant");
2900           if (pfile->multiline_string_line != start_line
2901               && pfile->multiline_string_line != 0)
2902             cpp_error_with_line (pfile,
2903                                  pfile->multiline_string_line, -1,
2904                          "possible real start of unterminated constant");
2905           pfile->multiline_string_line = 0;
2906           return;
2907
2908         case '\n':
2909           CPP_BUMP_LINE (pfile);
2910           /* In Fortran and assembly language, silently terminate
2911              strings of either variety at end of line.  This is a
2912              kludge around not knowing where comments are in these
2913              languages.  */
2914           if (CPP_OPTIONS (pfile)->lang_fortran
2915               || CPP_OPTIONS (pfile)->lang_asm)
2916             {
2917               FORWARD(-1);
2918               return;
2919             }
2920           /* Character constants may not extend over multiple lines.
2921              In Standard C, neither may strings.  We accept multiline
2922              strings as an extension.  */
2923           if (c == '\'')
2924             {
2925               cpp_error_with_line (pfile, start_line, start_column,
2926                                    "unterminated character constant");
2927               FORWARD(-1);
2928               return;
2929             }
2930           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2931             cpp_pedwarn_with_line (pfile, start_line, start_column,
2932                                    "string constant runs past end of line");
2933           if (pfile->multiline_string_line == 0)
2934             pfile->multiline_string_line = start_line;
2935           break;
2936
2937         case '\r':
2938           if (CPP_BUFFER (pfile)->has_escapes)
2939             {
2940               cpp_ice (pfile, "\\r escape inside string constant");
2941               FORWARD(1);
2942             }
2943           else
2944             /* Backslash newline is replaced by nothing at all.  */
2945             CPP_BUMP_LINE (pfile);
2946           break;
2947
2948         case '\\':
2949           FORWARD(1);
2950           break;
2951
2952         case '\"':
2953         case '\'':
2954           if (cc == c)
2955             return;
2956           break;
2957         }
2958     }
2959 }
2960
2961 /* Parse a string and copy it to the output.  */
2962
2963 static void
2964 parse_string (pfile, c)
2965      cpp_reader *pfile;
2966      int c;
2967 {
2968   U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
2969   U_CHAR *limit;
2970
2971   skip_string (pfile, c);
2972
2973   limit = CPP_BUFFER (pfile)->cur;
2974   CPP_RESERVE (pfile, limit - start + 2);
2975   CPP_PUTC_Q (pfile, c);
2976   for (; start < limit; start++)
2977     if (*start != '\r')
2978       CPP_PUTC_Q (pfile, *start);
2979 }
2980
2981 /* Read an assertion into the token buffer, converting to
2982    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2983    character to read should be the first letter of the predicate.
2984    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2985    with answer (see callers for why). In case of 0, an error has been
2986    printed. */
2987 static int
2988 parse_assertion (pfile)
2989      cpp_reader *pfile;
2990 {
2991   int c, dropwhite;
2992   cpp_skip_hspace (pfile);
2993   c = PEEKC();
2994   if (! is_idstart(c))
2995     {
2996       cpp_error (pfile, "assertion predicate is not an identifier");
2997       return 0;
2998     }
2999   CPP_PUTC(pfile, '#');
3000   FORWARD(1);
3001   parse_name(pfile, c);
3002
3003   c = PEEKC();
3004   if (c != '(')
3005     {
3006       if (is_hspace(c) || c == '\r')
3007         cpp_skip_hspace (pfile);
3008       c = PEEKC();
3009     }
3010   if (c != '(')
3011     return 1;
3012
3013   CPP_PUTC(pfile, '(');
3014   FORWARD(1);
3015   dropwhite = 1;
3016   while ((c = GETC()) != ')')
3017     {
3018       if (is_space(c))
3019         {
3020           if (! dropwhite)
3021             {
3022               CPP_PUTC(pfile, ' ');
3023               dropwhite = 1;
3024             }
3025         }
3026       else if (c == '\n' || c == EOF)
3027         {
3028           if (c == '\n') FORWARD(-1);
3029           cpp_error (pfile, "un-terminated assertion answer");
3030           return 0;
3031         }
3032       else if (c == '\r')
3033         /* \r cannot be a macro escape here. */
3034         CPP_BUMP_LINE (pfile);
3035       else
3036         {
3037           CPP_PUTC (pfile, c);
3038           dropwhite = 0;
3039         }
3040     }
3041
3042   if (pfile->limit[-1] == ' ')
3043     pfile->limit[-1] = ')';
3044   else if (pfile->limit[-1] == '(')
3045     {
3046       cpp_error (pfile, "empty token sequence in assertion");
3047       return 0;
3048     }
3049   else
3050     CPP_PUTC (pfile, ')');
3051
3052   CPP_NUL_TERMINATE (pfile);
3053   return 2;
3054 }
3055
3056 static int
3057 do_assert (pfile, keyword)
3058      cpp_reader *pfile;
3059      const struct directive *keyword ATTRIBUTE_UNUSED;
3060 {
3061   char *sym;
3062   int ret, c;
3063   HASHNODE *base, *this;
3064   int baselen, thislen;
3065
3066   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3067       && !CPP_BUFFER (pfile)->system_header_p)
3068     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3069
3070   cpp_skip_hspace (pfile);
3071   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3072   ret = parse_assertion (pfile);
3073   if (ret == 0)
3074     goto error;
3075   else if (ret == 1)
3076     {
3077       cpp_error (pfile, "missing token-sequence in `#assert'");
3078       goto error;
3079     }
3080
3081   cpp_skip_hspace (pfile);
3082   c = PEEKC();
3083   if (c != EOF && c != '\n')
3084     {
3085       cpp_error (pfile, "junk at end of `#assert'");
3086       goto error;
3087     }
3088
3089   thislen = strlen (sym);
3090   baselen = index (sym, '(') - sym;
3091   this = cpp_lookup (pfile, sym, thislen);
3092   if (this)
3093     {
3094       cpp_warning (pfile, "`%s' re-asserted", sym);
3095       goto error;
3096     }
3097
3098   base = cpp_lookup (pfile, sym, baselen);
3099   if (! base)
3100     base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3101   else if (base->type != T_ASSERT)
3102   {
3103     /* Token clash - but with what?! */
3104     cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3105     goto error;
3106   }
3107
3108   this = cpp_install (pfile, sym, thislen, T_ASSERT,
3109                       (char *)base->value.aschain);
3110   base->value.aschain = this;
3111   
3112   pfile->limit = (unsigned char *) sym; /* Pop */
3113   return 0;
3114
3115  error:
3116   skip_rest_of_line (pfile);
3117   pfile->limit = (unsigned char *) sym; /* Pop */
3118   return 0;
3119 }
3120
3121 static int
3122 do_unassert (pfile, keyword)
3123      cpp_reader *pfile;
3124      const struct directive *keyword ATTRIBUTE_UNUSED;
3125 {
3126   int c, ret;
3127   char *sym;
3128   long baselen, thislen;
3129   HASHNODE *base, *this, *next;
3130   
3131   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3132       && !CPP_BUFFER (pfile)->system_header_p)
3133     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3134
3135   cpp_skip_hspace (pfile);
3136
3137   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3138   ret = parse_assertion (pfile);
3139   if (ret == 0)
3140     goto error;
3141   
3142   cpp_skip_hspace (pfile);
3143   c = PEEKC ();
3144   if (c != EOF && c != '\n')
3145       cpp_error (pfile, "junk at end of `#unassert'");
3146
3147   thislen = strlen (sym);
3148   if (ret == 1)
3149     {
3150       base = cpp_lookup (pfile, sym, thislen);
3151       if (! base)
3152         goto error;  /* It isn't an error to #undef what isn't #defined,
3153                         so it isn't an error to #unassert what isn't
3154                         #asserted either. */
3155       
3156       for (this = base->value.aschain; this; this = next)
3157         {
3158           next = this->value.aschain;
3159           delete_macro (this);
3160         }
3161       delete_macro (base);
3162     }
3163   else
3164     {
3165       baselen = index (sym, '(') - sym;
3166       base = cpp_lookup (pfile, sym, baselen);
3167       if (! base) goto error;
3168       this = cpp_lookup (pfile, sym, thislen);
3169       if (! this) goto error;
3170
3171       next = base;
3172       while (next->value.aschain != this)
3173         next = next->value.aschain;
3174
3175       next->value.aschain = this->value.aschain;
3176       delete_macro (this);
3177
3178       if (base->value.aschain == NULL)
3179         delete_macro (base);  /* Last answer for this predicate deleted. */
3180     }
3181   
3182   pfile->limit = (unsigned char *) sym; /* Pop */
3183   return 0;
3184  error:
3185   skip_rest_of_line (pfile);
3186   pfile->limit = (unsigned char *) sym; /* Pop */
3187   return 0;
3188 }
3189
3190 /* Process STR as if it appeared as the body of an #unassert. */
3191 void
3192 cpp_unassert (pfile, str)
3193      cpp_reader *pfile;
3194      unsigned char *str;
3195 {
3196   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3197     {
3198       do_unassert (pfile, NULL);
3199       cpp_pop_buffer (pfile);
3200     }
3201 }  
3202
3203 /* Remember the current position of PFILE so it may be returned to
3204    after looking ahead a bit.
3205
3206    Note that when you set a mark, you _must_ return to that mark.  You
3207    may not forget about it and continue parsing.  You may not pop a
3208    buffer with an active mark.  You may not call CPP_BUMP_LINE while a
3209    mark is active.  */
3210
3211 static void
3212 parse_set_mark (pfile)
3213      cpp_reader *pfile;
3214 {
3215   cpp_buffer *ip = CPP_BUFFER (pfile);
3216   if (ACTIVE_MARK_P())
3217       cpp_ice (pfile, "mark active in parse_set_mark");
3218
3219   ip->mark = ip->cur - ip->buf;
3220 }
3221
3222 /* Backup the current position of PFILE to that saved in its mark,
3223    and clear the mark.  */
3224
3225 static void
3226 parse_goto_mark (pfile)
3227      cpp_reader *pfile;
3228 {
3229   cpp_buffer *ip = CPP_BUFFER (pfile);
3230   if (!ACTIVE_MARK_P())
3231       cpp_ice (pfile, "mark not active in parse_goto_mark");
3232
3233   ip->cur = ip->buf + ip->mark;
3234   ip->mark = -1;
3235 }