cpplib.h (CPP_PEDANTIC): Only true if system_header_p is not set for the buffer.
[platform/upstream/gcc.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 ATTRIBUTE_UNUSED;
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 (CPP_OPTIONS (pfile)->debug_output
732       || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
733     dump_definition (pfile, sym, len, def);
734   else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
735     pass_thru_directive (sym, len, pfile, keyword);
736
737   return 0;
738 }
739
740
741 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
742    If BUFFER != NULL, then use the LENGTH characters in BUFFER
743    as the new input buffer.
744    Return the new buffer, or NULL on failure.  */
745
746 cpp_buffer *
747 cpp_push_buffer (pfile, buffer, length)
748      cpp_reader *pfile;
749      U_CHAR *buffer;
750      long length;
751 {
752   cpp_buffer *buf = CPP_BUFFER (pfile);
753   cpp_buffer *new;
754   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
755     {
756       cpp_fatal (pfile, "macro or `#include' recursion too deep");
757       return NULL;
758     }
759
760   new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
761
762   new->if_stack = pfile->if_stack;
763   new->cleanup = null_cleanup;
764   new->underflow = null_underflow;
765   new->buf = new->cur = buffer;
766   new->alimit = new->rlimit = buffer + length;
767   new->prev = buf;
768   new->mark = -1;
769   new->line_base = NULL;
770
771   CPP_BUFFER (pfile) = new;
772   return new;
773 }
774
775 cpp_buffer *
776 cpp_pop_buffer (pfile)
777      cpp_reader *pfile;
778 {
779   cpp_buffer *buf = CPP_BUFFER (pfile);
780   if (ACTIVE_MARK_P())
781     cpp_ice (pfile, "mark active in cpp_pop_buffer");
782   (*buf->cleanup) (buf, pfile);
783   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
784   free (buf);
785   pfile->buffer_stack_depth--;
786   return CPP_BUFFER (pfile);
787 }
788
789 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
790    Pop the buffer when done.  */
791
792 void
793 cpp_scan_buffer (pfile)
794      cpp_reader *pfile;
795 {
796   cpp_buffer *buffer = CPP_BUFFER (pfile);
797   enum cpp_token token;
798   if (CPP_OPTIONS (pfile)->no_output)
799     {
800       long old_written = CPP_WRITTEN (pfile);
801       /* In no-output mode, we can ignore everything but directives.  */
802       for (;;)
803         {
804           if (! pfile->only_seen_white)
805             skip_rest_of_line (pfile);
806           token = cpp_get_token (pfile);
807           if (token == CPP_EOF) /* Should not happen ...  */
808             break;
809           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
810             {
811               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
812                   != CPP_NULL_BUFFER (pfile))
813                 cpp_pop_buffer (pfile);
814               break;
815             }
816         }
817       CPP_SET_WRITTEN (pfile, old_written);
818     }
819   else
820     {
821       for (;;)
822         {
823           token = cpp_get_token (pfile);
824           if (token == CPP_EOF) /* Should not happen ...  */
825             break;
826           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
827             {
828               if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
829                   != CPP_NULL_BUFFER (pfile))
830                 cpp_pop_buffer (pfile);
831               break;
832             }
833         }
834     }
835 }
836
837 /*
838  * Rescan a string (which may have escape marks) into pfile's buffer.
839  * Place the result in pfile->token_buffer.
840  *
841  * The input is copied before it is scanned, so it is safe to pass
842  * it something from the token_buffer that will get overwritten
843  * (because it follows CPP_WRITTEN).  This is used by do_include.
844  */
845
846 void
847 cpp_expand_to_buffer (pfile, buf, length)
848      cpp_reader *pfile;
849      const U_CHAR *buf;
850      int length;
851 {
852   register cpp_buffer *ip;
853   U_CHAR *buf1;
854   int save_no_output;
855
856   if (length < 0)
857     {
858       cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
859       return;
860     }
861
862   /* Set up the input on the input stack.  */
863
864   buf1 = (U_CHAR *) alloca (length + 1);
865   memcpy (buf1, buf, length);
866   buf1[length] = 0;
867
868   ip = cpp_push_buffer (pfile, buf1, length);
869   if (ip == NULL)
870     return;
871   ip->has_escapes = 1;
872
873   /* Scan the input, create the output.  */
874   save_no_output = CPP_OPTIONS (pfile)->no_output;
875   CPP_OPTIONS (pfile)->no_output = 0;
876   CPP_OPTIONS (pfile)->no_line_commands++;
877   cpp_scan_buffer (pfile);
878   CPP_OPTIONS (pfile)->no_line_commands--;
879   CPP_OPTIONS (pfile)->no_output = save_no_output;
880
881   CPP_NUL_TERMINATE (pfile);
882 }
883
884 void
885 cpp_buf_line_and_col (pbuf, linep, colp)
886      register cpp_buffer *pbuf;
887      long *linep, *colp;
888 {
889   if (pbuf)
890     {
891       *linep = pbuf->lineno;
892       if (colp)
893         *colp = pbuf->cur - pbuf->line_base;
894     }
895   else
896     {
897       *linep = 0;
898       if (colp)
899         *colp = 0;
900     }
901 }
902
903 /* Return the cpp_buffer that corresponds to a file (not a macro).  */
904
905 cpp_buffer *
906 cpp_file_buffer (pfile)
907      cpp_reader *pfile;
908 {
909   cpp_buffer *ip = CPP_BUFFER (pfile);
910
911   for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
912     if (ip->fname != NULL)
913       return ip;
914   return NULL;
915 }
916
917 /*
918  * write out a #line command, for instance, after an #include file.
919  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
920  */
921
922 void
923 output_line_command (pfile, file_change)
924      cpp_reader *pfile;
925      enum file_change_code file_change;
926 {
927   long line;
928   cpp_buffer *ip;
929
930   if (CPP_OPTIONS (pfile)->no_line_commands
931       || CPP_OPTIONS (pfile)->no_output)
932     return;
933
934   ip = cpp_file_buffer (pfile);
935   cpp_buf_line_and_col (ip, &line, NULL);
936
937   /* If the current file has not changed, we omit the #line if it would
938      appear to be a no-op, and we output a few newlines instead
939      if we want to increase the line number by a small amount.
940      We cannot do this if pfile->lineno is zero, because that means we
941      haven't output any line commands yet.  (The very first line command
942      output is a `same_file' command.)  */
943   if (file_change == same_file && pfile->lineno != 0)
944     {
945       if (line == pfile->lineno)
946         return;
947
948       /* If the inherited line number is a little too small,
949          output some newlines instead of a #line command.  */
950       if (line > pfile->lineno && line < pfile->lineno + 8)
951         {
952           CPP_RESERVE (pfile, 20);
953           while (line > pfile->lineno)
954             {
955               CPP_PUTC_Q (pfile, '\n');
956               pfile->lineno++;
957             }
958           return;
959         }
960     }
961
962   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
963   CPP_PUTS_Q (pfile, "# ", 2);
964
965   sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
966   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
967
968   quote_string (pfile, ip->nominal_fname); 
969   if (file_change != same_file && file_change != rename_file)
970     {
971       CPP_PUTC_Q (pfile, ' ');
972       CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
973     }
974   /* Tell cc1 if following text comes from a system header file.  */
975   if (ip->system_header_p)
976     {
977       CPP_PUTC_Q (pfile, ' ');
978       CPP_PUTC_Q (pfile, '3');
979     }
980 #ifndef NO_IMPLICIT_EXTERN_C
981   /* Tell cc1plus if following text should be treated as C.  */
982   if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
983     {
984       CPP_PUTC_Q (pfile, ' ');
985       CPP_PUTC_Q (pfile, '4');
986     }
987 #endif
988   CPP_PUTC_Q (pfile, '\n');
989   pfile->lineno = line;
990 }
991
992
993 /* Like cpp_get_token, except that it does not read past end-of-line.
994    Also, horizontal space is skipped, and macros are popped.  */
995
996 enum cpp_token
997 get_directive_token (pfile)
998      cpp_reader *pfile;
999 {
1000   long old_written = CPP_WRITTEN (pfile);
1001   enum cpp_token token;
1002
1003   for (;;)
1004     {
1005       cpp_skip_hspace (pfile);
1006       if (PEEKC () == '\n')
1007         return CPP_VSPACE;
1008
1009       token = cpp_get_token (pfile);
1010       /* token could be hspace at the beginning of a macro.  */
1011       if (token == CPP_HSPACE || token == CPP_COMMENT)
1012         {
1013           CPP_SET_WRITTEN (pfile, old_written);
1014           continue;
1015         }
1016
1017       /* token cannot be vspace, it would have been caught above.  */
1018       if (token == CPP_VSPACE)
1019         {
1020           cpp_ice (pfile, "VSPACE in get_directive_token");
1021           return token;
1022         }
1023
1024       /* token cannot be POP unless the buffer is a macro buffer.  */
1025       if (token != CPP_POP)
1026         return token;
1027
1028       if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1029         {
1030           cpp_ice (pfile, "POP of file buffer in get_directive_token");
1031           return token;
1032         }
1033
1034       /* We must pop the buffer by hand, or else cpp_get_token might
1035          hand us white space or newline on the next invocation.  */
1036       cpp_pop_buffer (pfile);
1037     }
1038 }
1039 \f
1040 /* Handle #include and #import.
1041    This function expects to see "fname" or <fname> on the input.
1042
1043    The input is normally in part of the output_buffer following
1044    CPP_WRITTEN, and will get overwritten by output_line_command.
1045    I.e. in input file specification has been popped by handle_directive.
1046    This is safe.  */
1047
1048 static int
1049 do_include (pfile, keyword)
1050      cpp_reader *pfile;
1051      const struct directive *keyword;
1052 {
1053   int importing = (keyword->type == T_IMPORT);
1054   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1055   int angle_brackets = 0;       /* 0 for "...", 1 for <...> */
1056   int before;  /* included before? */
1057   long flen;
1058   unsigned char *ftok;
1059   cpp_buffer *fp;
1060
1061   enum cpp_token token;
1062
1063   /* Chain of dirs to search */
1064   struct include_hash *ihash;
1065   struct file_name_list *search_start;
1066   
1067   long old_written = CPP_WRITTEN (pfile);
1068
1069   int fd;
1070
1071   if (CPP_PEDANTIC (pfile))
1072     {
1073       if (importing)
1074         cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1075       if (skip_dirs)
1076         cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1077     }
1078
1079   if (importing && CPP_OPTIONS (pfile)->warn_import
1080       && !CPP_OPTIONS (pfile)->inhibit_warnings
1081       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1082     {
1083       pfile->import_warning = 1;
1084       cpp_warning (pfile,
1085            "#import is obsolete, use an #ifndef wrapper in the header file");
1086     }
1087
1088   pfile->parsing_include_directive++;
1089   token = get_directive_token (pfile);
1090   pfile->parsing_include_directive--;
1091
1092   if (token == CPP_STRING)
1093     {
1094       if (pfile->token_buffer[old_written] == '<')
1095         angle_brackets = 1;
1096     }
1097 #ifdef VMS
1098   else if (token == CPP_NAME)
1099     {
1100       /* Support '#include xyz' like VAX-C.  It is taken as
1101          '#include <xyz.h>' and generates a warning.  */
1102       cpp_warning (pfile,
1103                "`#include filename' is obsolete, use `#include <filename.h>'");
1104       angle_brackets = 1;
1105
1106       /* Append the missing `.h' to the name. */
1107       CPP_PUTS (pfile, ".h", 2);
1108     }
1109 #endif
1110   else
1111     {
1112       cpp_error (pfile,
1113                  "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1114       CPP_SET_WRITTEN (pfile, old_written);
1115       skip_rest_of_line (pfile);
1116       return 0;
1117     }
1118
1119   flen = CPP_WRITTEN (pfile) - old_written;
1120   ftok = (unsigned char *) alloca (flen + 1);
1121   memcpy (ftok, pfile->token_buffer + old_written, flen);
1122   ftok[flen] = '\0';
1123
1124   if (get_directive_token (pfile) != CPP_VSPACE)
1125     {
1126       cpp_error (pfile, "junk at end of `#include'");
1127       skip_rest_of_line (pfile);
1128     }
1129
1130   CPP_SET_WRITTEN (pfile, old_written);
1131
1132   if (flen == 0)
1133     {
1134       cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1135       return 0;
1136     }
1137
1138   if (CPP_OPTIONS (pfile)->dump_includes)
1139     pass_thru_directive (ftok,
1140                          flen
1141 #ifdef VMS
1142           - ((token == CPP_NAME) ? 2 : 0)
1143 #endif
1144                          , pfile, keyword);
1145
1146 #ifdef VMS
1147   if (token == CPP_STRING)
1148 #endif
1149     {
1150       ftok++;
1151       flen -= 2;
1152       ftok[flen] = '\0';
1153     }
1154
1155   search_start = 0;
1156
1157   for (fp = CPP_BUFFER (pfile);
1158        fp != CPP_NULL_BUFFER (pfile);
1159        fp = CPP_PREV_BUFFER (fp))
1160     if (fp->fname != NULL)
1161       break;
1162
1163   if (fp == CPP_NULL_BUFFER (pfile))
1164     {
1165       cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1166       return 0;
1167     }
1168   
1169   /* For #include_next, skip in the search path past the dir in which the
1170      containing file was found.  Treat files specified using an absolute path
1171      as if there are no more directories to search.  Treat the primary source
1172      file like any other included source, but generate a warning.  */
1173   if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1174     {
1175       if (fp->ihash->foundhere != ABSOLUTE_PATH)
1176         search_start = fp->ihash->foundhere->next;
1177     }
1178   else
1179     {
1180       if (skip_dirs)
1181         cpp_warning (pfile, "#include_next in primary source file");
1182       
1183       if (angle_brackets)
1184         search_start = CPP_OPTIONS (pfile)->bracket_include;
1185       else
1186         {
1187           if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1188             {
1189               if (fp)
1190                 search_start = fp->actual_dir;
1191             }
1192           else
1193             search_start = CPP_OPTIONS (pfile)->quote_include;
1194         }
1195     }
1196
1197   if (!search_start)
1198     {
1199       cpp_error (pfile, "No include path in which to find %s", ftok);
1200       return 0;
1201     }
1202
1203   fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1204
1205   if (fd == -2)
1206     return 0;
1207   
1208   if (fd == -1)
1209     {
1210       if (CPP_OPTIONS (pfile)->print_deps_missing_files
1211           && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1212                                        (pfile->system_include_depth > 0)))
1213         {
1214           if (!angle_brackets)
1215             deps_output (pfile, ftok, ' ');
1216           else
1217             {
1218               char *p;
1219               struct file_name_list *ptr;
1220               /* If requested as a system header, assume it belongs in
1221                  the first system header directory. */
1222               if (CPP_OPTIONS (pfile)->bracket_include)
1223                 ptr = CPP_OPTIONS (pfile)->bracket_include;
1224               else
1225                 ptr = CPP_OPTIONS (pfile)->quote_include;
1226
1227               p = (char *) alloca (strlen (ptr->name)
1228                                    + strlen (ftok) + 2);
1229               if (*ptr->name != '\0')
1230                 {
1231                   strcpy (p, ptr->name);
1232                   strcat (p, "/");
1233                 }
1234               strcat (p, ftok);
1235               deps_output (pfile, p, ' ');
1236             }
1237         }
1238       /* If -M was specified, and this header file won't be added to
1239          the dependency list, then don't count this as an error,
1240          because we can still produce correct output.  Otherwise, we
1241          can't produce correct output, because there may be
1242          dependencies we need inside the missing file, and we don't
1243          know what directory this missing file exists in. */
1244       else if (CPP_PRINT_DEPS (pfile)
1245                && (CPP_PRINT_DEPS (pfile)
1246                    <= (angle_brackets || (pfile->system_include_depth > 0))))
1247         cpp_warning (pfile, "No include path in which to find %s", ftok);
1248       else
1249         cpp_error_from_errno (pfile, ftok);
1250
1251       return 0;
1252     }
1253
1254   /* For -M, add the file to the dependencies on its first inclusion. */
1255   if (!before && (CPP_PRINT_DEPS (pfile)
1256                   > (angle_brackets || (pfile->system_include_depth > 0))))
1257     deps_output (pfile, ihash->name, ' ');
1258
1259   /* Handle -H option.  */
1260   if (CPP_OPTIONS(pfile)->print_include_names)
1261     {
1262       fp = CPP_BUFFER (pfile);
1263       while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1264         putc ('.', stderr);
1265       fprintf (stderr, " %s\n", ihash->name);
1266     }
1267
1268   /* Actually process the file */
1269
1270   if (importing)
1271     ihash->control_macro = "";
1272   
1273   if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1274     {
1275       close (fd);
1276       return 0;
1277     }
1278   
1279   if (angle_brackets)
1280     pfile->system_include_depth++;   /* Decremented in file_cleanup. */
1281
1282   if (finclude (pfile, fd, ihash))
1283     {
1284       output_line_command (pfile, enter_file);
1285       pfile->only_seen_white = 2;
1286     }
1287
1288   return 0;
1289 }
1290
1291 /* Subroutine of do_line.  Read next token from PFILE without adding it to
1292    the output buffer.  If it is a number between 1 and 4, store it in *NUM
1293    and return 1; otherwise, return 0 and complain if we aren't at the end
1294    of the directive.  */
1295
1296 static int
1297 read_line_number (pfile, num)
1298      cpp_reader *pfile;
1299      int *num;
1300 {
1301   long save_written = CPP_WRITTEN (pfile);
1302   U_CHAR *p = pfile->token_buffer + save_written;
1303   enum cpp_token token = get_directive_token (pfile);
1304   CPP_SET_WRITTEN (pfile, save_written);
1305
1306   if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1307     {
1308       *num = p[0] - '0';
1309       return 1;
1310     }
1311   else
1312     {
1313       if (token != CPP_VSPACE && token != CPP_EOF)
1314         cpp_error (pfile, "invalid format `#line' command");
1315       return 0;
1316     }
1317 }
1318
1319 /* Interpret #line command.
1320    Note that the filename string (if any) is treated as if it were an
1321    include filename.  That means no escape handling.  */
1322
1323 static int
1324 do_line (pfile, keyword)
1325      cpp_reader *pfile;
1326      const struct directive *keyword ATTRIBUTE_UNUSED;
1327 {
1328   cpp_buffer *ip = CPP_BUFFER (pfile);
1329   int new_lineno;
1330   long old_written = CPP_WRITTEN (pfile);
1331   enum file_change_code file_change = same_file;
1332   enum cpp_token token;
1333   char *x;
1334
1335   token = get_directive_token (pfile);
1336
1337   if (token != CPP_NUMBER)
1338     {
1339       cpp_error (pfile, "token after `#line' is not an integer");
1340       goto bad_line_directive;
1341     }
1342
1343   new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1344   if (x[0] != '\0')
1345     {
1346       cpp_error (pfile, "token after `#line' is not an integer");
1347       goto bad_line_directive;
1348     }      
1349   CPP_SET_WRITTEN (pfile, old_written);
1350
1351   if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1352     cpp_pedwarn (pfile, "line number out of range in `#line' command");
1353
1354   token = get_directive_token (pfile);
1355
1356   if (token == CPP_STRING)
1357     {
1358       U_CHAR *fname = pfile->token_buffer + old_written + 1;
1359       U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1360       int action_number = 0;
1361
1362       file_change = rename_file;
1363
1364       if (read_line_number (pfile, &action_number))
1365         {
1366           if (CPP_PEDANTIC (pfile))
1367             cpp_pedwarn (pfile, "garbage at end of `#line' command");
1368
1369           if (action_number == 1)
1370             {
1371               file_change = enter_file;
1372               read_line_number (pfile, &action_number);
1373             }
1374           else if (action_number == 2)
1375             {
1376               file_change = leave_file;
1377               read_line_number (pfile, &action_number);
1378             }
1379           if (action_number == 3)
1380             {
1381               ip->system_header_p = 1;
1382               read_line_number (pfile, &action_number);
1383             }
1384           if (action_number == 4)
1385             {
1386               ip->system_header_p = 2;
1387               read_line_number (pfile, &action_number);
1388             }
1389         }
1390       
1391       *end_name = '\0';
1392       
1393       if (strcmp (fname, ip->nominal_fname))
1394         {
1395           const char *newname, *oldname;
1396           if (!strcmp (fname, ip->fname))
1397             newname = ip->fname;
1398           else if (ip->last_nominal_fname
1399                    && !strcmp (fname, ip->last_nominal_fname))
1400             newname = ip->last_nominal_fname;
1401           else
1402             newname = xstrdup (fname);
1403
1404           oldname = ip->nominal_fname;
1405           ip->nominal_fname = newname;
1406
1407           if (ip->last_nominal_fname
1408               && ip->last_nominal_fname != oldname
1409               && ip->last_nominal_fname != newname
1410               && ip->last_nominal_fname != ip->fname)
1411             free ((void *) ip->last_nominal_fname);
1412
1413           if (newname == ip->fname)
1414             ip->last_nominal_fname = NULL;
1415           else
1416             ip->last_nominal_fname = oldname;
1417         } 
1418     }
1419   else if (token != CPP_VSPACE && token != CPP_EOF)
1420     {
1421       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1422       goto bad_line_directive;
1423     }
1424
1425   /* The Newline at the end of this line remains to be processed.
1426      To put the next line at the specified line number,
1427      we must store a line number now that is one less.  */
1428   ip->lineno = new_lineno - 1;
1429   CPP_SET_WRITTEN (pfile, old_written);
1430   output_line_command (pfile, file_change);
1431   return 0;
1432
1433  bad_line_directive:
1434   skip_rest_of_line (pfile);
1435   CPP_SET_WRITTEN (pfile, old_written);
1436   return 0;
1437 }
1438
1439 /* Remove the definition of a symbol from the symbol table.
1440    According to the C standard, it is not an error to undef
1441    something that has no definitions. */
1442 static int
1443 do_undef (pfile, keyword)
1444      cpp_reader *pfile;
1445      const struct directive *keyword;
1446 {
1447   int len;
1448   HASHNODE *hp;
1449   U_CHAR *buf, *name, *limit;
1450   int c;
1451   long here = CPP_WRITTEN (pfile);
1452   enum cpp_token token;
1453
1454   cpp_skip_hspace (pfile);
1455   c = GETC();
1456   if (! is_idstart(c))
1457   {
1458       cpp_error (pfile, "token after #undef is not an identifier");
1459       skip_rest_of_line (pfile);
1460       return 1;
1461   }
1462
1463   parse_name (pfile, c);
1464   buf = pfile->token_buffer + here;
1465   limit = CPP_PWRITTEN(pfile);
1466
1467   /* Copy out the token so we can pop the token buffer. */
1468   len = limit - buf;
1469   name = (U_CHAR *) alloca (len + 1);
1470   memcpy (name, buf, len);
1471   name[len] = '\0';
1472
1473   token = get_directive_token (pfile);
1474   if (token != CPP_VSPACE)
1475   {
1476       cpp_pedwarn (pfile, "junk on line after #undef");
1477       skip_rest_of_line (pfile);
1478   }
1479   CPP_SET_WRITTEN (pfile, here);
1480
1481   while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1482     {
1483       /* If we are generating additional info for debugging (with -g) we
1484          need to pass through all effective #undef commands.  */
1485       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1486         pass_thru_directive (name, len, pfile, keyword);
1487       if (hp->type == T_POISON)
1488         cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1489       else 
1490         {
1491           if (hp->type != T_MACRO)
1492             cpp_warning (pfile, "undefining `%s'", hp->name);
1493           delete_macro (hp);
1494         }
1495     }
1496
1497   return 0;
1498 }
1499
1500 /* Wrap do_undef for -U processing. */
1501 void
1502 cpp_undef (pfile, macro)
1503      cpp_reader *pfile;
1504      U_CHAR *macro;
1505 {
1506   /* Copy the string so we can append a newline.  */
1507   size_t len = strlen (macro);
1508   U_CHAR *buf = alloca (len + 2);
1509   memcpy (buf, macro, len);
1510   buf[len]     = '\n';
1511   buf[len + 1] = '\0';
1512   if (cpp_push_buffer (pfile, buf, len + 1))
1513     {
1514       do_undef (pfile, NULL);
1515       cpp_pop_buffer (pfile);
1516     }
1517 }
1518
1519 /*
1520  * Report an error detected by the program we are processing.
1521  * Use the text of the line in the error message.
1522  * (We use error because it prints the filename & line#.)
1523  */
1524
1525 static int
1526 do_error (pfile, keyword)
1527      cpp_reader *pfile;
1528      const struct directive *keyword ATTRIBUTE_UNUSED;
1529 {
1530   U_CHAR *text, *limit;
1531
1532   cpp_skip_hspace (pfile);
1533   text = CPP_BUFFER (pfile)->cur;
1534   skip_rest_of_line (pfile);
1535   limit = CPP_BUFFER (pfile)->cur;
1536
1537   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1538   return 0;
1539 }
1540
1541 /*
1542  * Report a warning detected by the program we are processing.
1543  * Use the text of the line in the warning message, then continue.
1544  */
1545
1546 static int
1547 do_warning (pfile, keyword)
1548      cpp_reader *pfile;
1549      const struct directive *keyword ATTRIBUTE_UNUSED;
1550 {
1551   U_CHAR *text, *limit;
1552
1553   cpp_skip_hspace (pfile);
1554   text = CPP_BUFFER (pfile)->cur;
1555   skip_rest_of_line (pfile);
1556   limit = CPP_BUFFER (pfile)->cur;
1557
1558   if (CPP_PEDANTIC (pfile))
1559     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1560
1561   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1562   return 0;
1563 }
1564
1565 /* Report program identification.  */
1566
1567 static int
1568 do_ident (pfile, keyword)
1569      cpp_reader *pfile;
1570      const struct directive *keyword ATTRIBUTE_UNUSED;
1571 {
1572   long old_written = CPP_WRITTEN (pfile);
1573
1574   /* Allow #ident in system headers, since that's not user's fault.  */
1575   if (CPP_PEDANTIC (pfile))
1576     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1577
1578   CPP_PUTS (pfile, "#ident ", 7);
1579
1580   /* Next token should be a string constant.  */
1581   if (get_directive_token (pfile) == CPP_STRING)
1582     /* And then a newline.  */
1583     if (get_directive_token (pfile) == CPP_VSPACE)
1584       /* Good - ship it.  */
1585       return 0;
1586
1587   cpp_error (pfile, "invalid #ident");
1588   skip_rest_of_line (pfile);
1589   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
1590
1591   return 0;
1592 }
1593
1594 /* Pragmata handling.  We handle some of these, and pass the rest on
1595    to the front end.  C99 defines three pragmas and says that no macro
1596    expansion is to be performed on them; whether or not macro
1597    expansion happens for other pragmas is implementation defined.
1598    This implementation never macro-expands the text after #pragma.
1599
1600    We currently do not support the _Pragma operator.  Support for that
1601    has to be coordinated with the front end.  Proposed implementation:
1602    both #pragma blah blah and _Pragma("blah blah") become
1603    __builtin_pragma(blah blah) and we teach the parser about that.  */
1604
1605 /* Sub-handlers for the pragmas needing treatment here.
1606    They return 1 if the token buffer is to be popped, 0 if not. */
1607 static int do_pragma_once               PARAMS ((cpp_reader *));
1608 static int do_pragma_implementation     PARAMS ((cpp_reader *));
1609 static int do_pragma_poison             PARAMS ((cpp_reader *));
1610 static int do_pragma_default            PARAMS ((cpp_reader *));
1611
1612 static int
1613 do_pragma (pfile, keyword)
1614      cpp_reader *pfile;
1615      const struct directive *keyword ATTRIBUTE_UNUSED;
1616 {
1617   long here, key;
1618   U_CHAR *buf;
1619   int pop;
1620   enum cpp_token token;
1621
1622   here = CPP_WRITTEN (pfile);
1623   CPP_PUTS (pfile, "#pragma ", 8);
1624
1625   key = CPP_WRITTEN (pfile);
1626   pfile->no_macro_expand++;
1627   token = get_directive_token (pfile);
1628   if (token != CPP_NAME)
1629     {
1630       if (token == CPP_VSPACE)
1631         goto empty;
1632       else
1633         goto skip;
1634     }
1635
1636   buf = pfile->token_buffer + key;
1637   CPP_PUTC (pfile, ' ');
1638
1639 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1640   if (tokis ("once"))
1641     pop = do_pragma_once (pfile);
1642   else if (tokis ("implementation"))
1643     pop = do_pragma_implementation (pfile);
1644   else if (tokis ("poison"))
1645     pop = do_pragma_poison (pfile);
1646   else
1647     pop = do_pragma_default (pfile);
1648 #undef tokis
1649
1650   if (get_directive_token (pfile) != CPP_VSPACE)
1651     goto skip;
1652
1653   if (pop)
1654     CPP_SET_WRITTEN (pfile, here);
1655   pfile->no_macro_expand--;
1656   return 0;
1657
1658  skip:
1659   cpp_error (pfile, "malformed #pragma directive");
1660   skip_rest_of_line (pfile);
1661  empty:
1662   CPP_SET_WRITTEN (pfile, here);
1663   pfile->no_macro_expand--;
1664   return 0;
1665 }
1666
1667 static int
1668 do_pragma_default (pfile)
1669      cpp_reader *pfile;
1670 {
1671   while (get_directive_token (pfile) != CPP_VSPACE)
1672     CPP_PUTC (pfile, ' ');
1673   return 0;
1674 }
1675
1676 static int
1677 do_pragma_once (pfile)
1678      cpp_reader *pfile;
1679 {
1680   cpp_buffer *ip = CPP_BUFFER (pfile);
1681
1682   if (ip->fname == NULL)
1683     {
1684       cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1685       return 1;
1686     }
1687   
1688   /* Allow #pragma once in system headers, since that's not the user's
1689      fault.  */
1690   if (!ip->system_header_p)
1691     cpp_warning (pfile, "`#pragma once' is obsolete");
1692       
1693   if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1694     cpp_warning (pfile, "`#pragma once' outside include file");
1695   else
1696     ip->ihash->control_macro = "";  /* never repeat */
1697
1698   return 1;
1699 }
1700
1701 static int
1702 do_pragma_implementation (pfile)
1703      cpp_reader *pfile;
1704 {
1705   /* Be quiet about `#pragma implementation' for a file only if it hasn't
1706      been included yet.  */
1707   struct include_hash *ptr;
1708   enum cpp_token token;
1709   long written = CPP_WRITTEN (pfile);
1710   U_CHAR *name;
1711   U_CHAR *copy;
1712
1713   token = get_directive_token (pfile);
1714   if (token == CPP_VSPACE)
1715     return 0;
1716   else if (token != CPP_STRING)
1717     {
1718       cpp_error (pfile, "malformed #pragma implementation");
1719       return 1;
1720     }
1721
1722   name = pfile->token_buffer + written + 1;
1723   copy = xstrdup (name);
1724   copy[strlen(copy)] = '\0';  /* trim trailing quote */
1725   
1726   ptr = include_hash (pfile, copy, 0);
1727   if (ptr)
1728     cpp_warning (pfile,
1729          "`#pragma implementation' for `%s' appears after file is included",
1730                  copy);
1731   free (copy);
1732   return 0;
1733 }
1734
1735 static int
1736 do_pragma_poison (pfile)
1737      cpp_reader *pfile;
1738 {
1739   /* Poison these symbols so that all subsequent usage produces an
1740      error message.  */
1741   U_CHAR *p;
1742   HASHNODE *hp;
1743   long written;
1744   size_t len;
1745   enum cpp_token token;
1746   int writeit;
1747   /* As a rule, don't include #pragma poison commands in output,  
1748      unless the user asks for them.  */
1749   writeit = (CPP_OPTIONS (pfile)->debug_output
1750              || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1751              || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1752
1753   for (;;)
1754     {
1755       written = CPP_WRITTEN (pfile);
1756       token = get_directive_token (pfile);
1757       if (token == CPP_VSPACE)
1758         break;
1759       if (token != CPP_NAME)
1760         {
1761           cpp_error (pfile, "invalid #pragma poison directive");
1762           skip_rest_of_line (pfile);
1763           return 1;
1764         }
1765
1766       p = pfile->token_buffer + written;
1767       len = strlen (p);
1768       if ((hp = cpp_lookup (pfile, p, len)))
1769         {
1770           if (hp->type != T_POISON)
1771             {
1772               cpp_warning (pfile, "poisoning existing macro `%s'", p);
1773               free_definition (hp->value.defn);
1774               hp->value.defn = 0;
1775               hp->type = T_POISON;
1776             }
1777         }
1778       else
1779         cpp_install (pfile, p, len, T_POISON, 0);
1780       if (writeit)
1781         CPP_PUTC (pfile, ' ');
1782     }
1783   return !writeit;
1784 }
1785  
1786 #ifdef SCCS_DIRECTIVE
1787 /* Just ignore #sccs, on systems where we define it at all.  */
1788
1789 static int
1790 do_sccs (pfile, keyword)
1791      cpp_reader *pfile;
1792      const struct directive *keyword ATTRIBUTE_UNUSED;
1793 {
1794   if (CPP_PEDANTIC (pfile))
1795     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1796   skip_rest_of_line (pfile);
1797   return 0;
1798 }
1799 #endif
1800
1801 /* We've found an `#if' directive.  If the only thing before it in
1802    this file is white space, and if it is of the form
1803    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1804    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1805    for an explanation of controlling macros.)  If so, return a
1806    malloc'd copy of SYMBOL.  Otherwise, return NULL.  */
1807
1808 static U_CHAR *
1809 detect_if_not_defined (pfile)
1810      cpp_reader *pfile;
1811 {
1812   U_CHAR *control_macro = 0;
1813
1814   if (pfile->only_seen_white == 2)
1815     {
1816       char *ident;
1817       enum cpp_token token;
1818       int base_offset;
1819       int token_offset;
1820       int need_rparen = 0;
1821
1822       /* Save state required for restore.  */
1823       pfile->no_macro_expand++;
1824       parse_set_mark (pfile);
1825       base_offset = CPP_WRITTEN (pfile);
1826
1827       /* Look for `!', */
1828       if (get_directive_token (pfile) != CPP_OTHER
1829           || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1830           || CPP_PWRITTEN (pfile)[-1] != '!')
1831         goto restore;
1832
1833       /* ...then `defined', */
1834       token_offset = CPP_WRITTEN (pfile);
1835       token = get_directive_token (pfile);
1836       if (token != CPP_NAME)
1837         goto restore;
1838       ident = pfile->token_buffer + token_offset;
1839       CPP_NUL_TERMINATE (pfile);
1840       if (strcmp (ident, "defined"))
1841         goto restore;
1842
1843       /* ...then an optional '(' and the name, */
1844       token_offset = CPP_WRITTEN (pfile);
1845       token = get_directive_token (pfile);
1846       if (token == CPP_LPAREN)
1847         {
1848           token_offset = CPP_WRITTEN (pfile);
1849           token = get_directive_token (pfile);
1850           if (token != CPP_NAME)
1851             goto restore;
1852           need_rparen = 1;
1853         }
1854       else if (token != CPP_NAME)
1855         goto restore;
1856
1857       ident = pfile->token_buffer + token_offset;
1858       CPP_NUL_TERMINATE (pfile);
1859
1860       /* ...then the ')', if necessary, */
1861       if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1862           /* ...and make sure there's nothing else on the line.  */
1863           && get_directive_token (pfile) == CPP_VSPACE)
1864         control_macro = xstrdup (ident);
1865
1866     restore:
1867       CPP_SET_WRITTEN (pfile, base_offset);
1868       pfile->no_macro_expand--;
1869       parse_goto_mark (pfile);
1870     }
1871
1872   return control_macro;
1873 }
1874
1875 /*
1876  * handle #if command by
1877  *   1) inserting special `defined' keyword into the hash table
1878  *      that gets turned into 0 or 1 by special_symbol (thus,
1879  *      if the luser has a symbol called `defined' already, it won't
1880  *      work inside the #if command)
1881  *   2) rescan the input into a temporary output buffer
1882  *   3) pass the output buffer to the yacc parser and collect a value
1883  *   4) clean up the mess left from steps 1 and 2.
1884  *   5) call conditional_skip to skip til the next #endif (etc.),
1885  *      or not, depending on the value from step 3.
1886  */
1887
1888 static int
1889 do_if (pfile, keyword)
1890      cpp_reader *pfile;
1891      const struct directive *keyword ATTRIBUTE_UNUSED;
1892 {
1893   U_CHAR *control_macro = detect_if_not_defined (pfile);
1894   HOST_WIDEST_INT value = eval_if_expression (pfile);
1895   conditional_skip (pfile, value == 0, T_IF, control_macro);
1896   return 0;
1897 }
1898
1899 /*
1900  * handle a #elif directive by not changing  if_stack  either.
1901  * see the comment above do_else.
1902  */
1903
1904 static int
1905 do_elif (pfile, keyword)
1906      cpp_reader *pfile;
1907      const struct directive *keyword ATTRIBUTE_UNUSED;
1908 {
1909   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1910     {
1911       cpp_error (pfile, "`#elif' not within a conditional");
1912       return 0;
1913     }
1914   else
1915     {
1916       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1917         {
1918           cpp_error (pfile, "`#elif' after `#else'");
1919           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1920                                "the conditional began here");
1921         }
1922       pfile->if_stack->type = T_ELIF;
1923     }
1924
1925   if (pfile->if_stack->if_succeeded)
1926     skip_if_group (pfile);
1927   else
1928     {
1929       HOST_WIDEST_INT value = eval_if_expression (pfile);
1930       if (value == 0)
1931         skip_if_group (pfile);
1932       else
1933         {
1934           ++pfile->if_stack->if_succeeded;      /* continue processing input */
1935           output_line_command (pfile, same_file);
1936         }
1937     }
1938   return 0;
1939 }
1940
1941 /*
1942  * evaluate a #if expression in BUF, of length LENGTH,
1943  * then parse the result as a C expression and return the value as an int.
1944  */
1945
1946 static HOST_WIDEST_INT
1947 eval_if_expression (pfile)
1948      cpp_reader *pfile;
1949 {
1950   HOST_WIDEST_INT value;
1951   long old_written = CPP_WRITTEN (pfile);
1952
1953   pfile->parsing_if_directive++;
1954   value = cpp_parse_expr (pfile);
1955   pfile->parsing_if_directive--;
1956
1957   skip_rest_of_line (pfile);
1958   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1959
1960   return value;
1961 }
1962
1963 /*
1964  * routine to handle ifdef/ifndef.  Try to look up the symbol,
1965  * then do or don't skip to the #endif/#else/#elif depending
1966  * on what directive is actually being processed.
1967  */
1968
1969 static int
1970 do_xifdef (pfile, keyword)
1971      cpp_reader *pfile;
1972      const struct directive *keyword;
1973 {
1974   int skip;
1975   cpp_buffer *ip = CPP_BUFFER (pfile);
1976   U_CHAR *ident;
1977   int ident_length;
1978   enum cpp_token token;
1979   int start_of_file = 0;
1980   U_CHAR *control_macro = 0;
1981   int old_written = CPP_WRITTEN (pfile);
1982
1983   /* Detect a #ifndef at start of file (not counting comments).  */
1984   if (ip->fname != 0 && keyword->type == T_IFNDEF)
1985     start_of_file = pfile->only_seen_white == 2;
1986
1987   pfile->no_macro_expand++;
1988   token = get_directive_token (pfile);
1989   pfile->no_macro_expand--;
1990
1991   ident = pfile->token_buffer + old_written;
1992   ident_length = CPP_WRITTEN (pfile) - old_written;
1993   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1994
1995   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1996     {
1997       skip = (keyword->type == T_IFDEF);
1998       if (! CPP_TRADITIONAL (pfile))
1999         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2000     }
2001   else if (token == CPP_NAME)
2002     {
2003       skip = cpp_defined (pfile, ident, ident_length);
2004       if (keyword->type == T_IFDEF)
2005         skip = !skip;
2006
2007       if (start_of_file && !skip)
2008         {
2009           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2010           bcopy (ident, control_macro, ident_length + 1);
2011         }
2012     }
2013   else
2014     {
2015       skip = (keyword->type == T_IFDEF);
2016       if (! CPP_TRADITIONAL (pfile))
2017         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2018     }
2019
2020   if (!CPP_TRADITIONAL (pfile))
2021     { int c;
2022       cpp_skip_hspace (pfile);
2023       c = PEEKC ();
2024       if (c != EOF && c != '\n')
2025         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2026     }
2027   skip_rest_of_line (pfile);
2028
2029   conditional_skip (pfile, skip, T_IF, control_macro);
2030   return 0;
2031 }
2032
2033 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2034    If this is a #ifndef starting at the beginning of a file,
2035    CONTROL_MACRO is the macro name tested by the #ifndef.
2036    Otherwise, CONTROL_MACRO is 0.  */
2037
2038 static void
2039 conditional_skip (pfile, skip, type, control_macro)
2040      cpp_reader *pfile;
2041      int skip;
2042      enum node_type type;
2043      U_CHAR *control_macro;
2044 {
2045   IF_STACK_FRAME *temp;
2046
2047   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2048   temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2049   temp->lineno = CPP_BUFFER (pfile)->lineno;
2050   temp->next = pfile->if_stack;
2051   temp->control_macro = control_macro;
2052   pfile->if_stack = temp;
2053
2054   pfile->if_stack->type = type;
2055
2056   if (skip != 0) {
2057     skip_if_group (pfile);
2058     return;
2059   } else {
2060     ++pfile->if_stack->if_succeeded;
2061     output_line_command (pfile, same_file);
2062   }
2063 }
2064
2065 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
2066    return 0 if skipping should continue, 1 if it should halt.  Also
2067    adjusts the if_stack as appropriate.
2068    The `#' has been read, but not the identifier. */
2069
2070 static int
2071 consider_directive_while_skipping (pfile, stack)
2072     cpp_reader *pfile;
2073     IF_STACK_FRAME *stack; 
2074 {
2075   long ident_len, ident;
2076   const struct directive *kt;
2077   IF_STACK_FRAME *temp;
2078     
2079   cpp_skip_hspace (pfile);
2080
2081   ident = CPP_WRITTEN (pfile);
2082   parse_name (pfile, GETC());
2083   ident_len = CPP_WRITTEN (pfile) - ident;
2084
2085   CPP_SET_WRITTEN (pfile, ident);
2086
2087   for (kt = directive_table; kt->length >= 0; kt++)
2088     if (kt->length == ident_len
2089         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2090       switch (kt->type)
2091         {
2092         case T_IF:
2093         case T_IFDEF:
2094         case T_IFNDEF:
2095             temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2096             temp->next = pfile->if_stack;
2097             pfile->if_stack = temp;
2098             temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2099             temp->type = kt->type;
2100             return 0;
2101
2102         case T_ELSE:
2103             if (pfile->if_stack != stack)
2104               validate_else (pfile, "#else");
2105             /* fall through */
2106         case T_ELIF:
2107             if (pfile->if_stack == stack)
2108               return 1;
2109             else
2110               {
2111                 pfile->if_stack->type = kt->type;
2112                 return 0;
2113               }
2114
2115             case T_ENDIF:
2116                 if (pfile->if_stack != stack)
2117                   validate_else (pfile, "#endif");
2118
2119                 if (pfile->if_stack == stack)
2120                   return 1;
2121                     
2122                 temp = pfile->if_stack;
2123                 pfile->if_stack = temp->next;
2124                 free (temp);
2125                 return 0;
2126
2127             default:
2128                 return 0;
2129             }
2130
2131     /* Don't let erroneous code go by.  */
2132     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2133         cpp_pedwarn (pfile, "invalid preprocessor directive name");
2134     return 0;
2135 }
2136
2137 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
2138  * leaves input ptr at the sharp sign found.
2139  */
2140 static void
2141 skip_if_group (pfile)
2142     cpp_reader *pfile;
2143 {
2144   int c;
2145   IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2146   U_CHAR *beg_of_line;
2147   long old_written;
2148
2149   old_written = CPP_WRITTEN (pfile);
2150   
2151   for (;;)
2152     {
2153       beg_of_line = CPP_BUFFER (pfile)->cur;
2154
2155       if (! CPP_TRADITIONAL (pfile))
2156         cpp_skip_hspace (pfile);
2157       c = GETC();
2158       if (c == '\n')
2159         {
2160           CPP_BUMP_LINE (pfile);
2161           continue;
2162         }
2163       else if (c == '#')
2164         {
2165           if (consider_directive_while_skipping (pfile, save_if_stack))
2166             break;
2167         }
2168       else if (c == EOF)
2169         return;  /* Caller will issue error. */
2170
2171       FORWARD(-1);
2172       skip_rest_of_line (pfile);
2173
2174       c = GETC();
2175       if (c == EOF)
2176         return;  /* Caller will issue error. */
2177       else
2178         CPP_BUMP_LINE (pfile);
2179     }     
2180
2181   /* Back up to the beginning of this line.  Caller will process the
2182      directive. */
2183   CPP_BUFFER (pfile)->cur = beg_of_line;
2184   pfile->only_seen_white = 1;
2185 }
2186
2187 /*
2188  * handle a #else directive.  Do this by just continuing processing
2189  * without changing  if_stack ;  this is so that the error message
2190  * for missing #endif's etc. will point to the original #if.  It
2191  * is possible that something different would be better.
2192  */
2193
2194 static int
2195 do_else (pfile, keyword)
2196      cpp_reader *pfile;
2197      const struct directive *keyword ATTRIBUTE_UNUSED;
2198 {
2199   validate_else (pfile, "#else");
2200   skip_rest_of_line (pfile);
2201
2202   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2203     {
2204       cpp_error (pfile, "`#else' not within a conditional");
2205       return 0;
2206     }
2207   else
2208     {
2209       /* #ifndef can't have its special treatment for containing the whole file
2210          if it has a #else clause.  */
2211       pfile->if_stack->control_macro = 0;
2212
2213       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2214         {
2215           cpp_error (pfile, "`#else' after `#else'");
2216           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2217                                "the conditional began here");
2218         }
2219       pfile->if_stack->type = T_ELSE;
2220     }
2221
2222   if (pfile->if_stack->if_succeeded)
2223     skip_if_group (pfile);
2224   else
2225     {
2226       ++pfile->if_stack->if_succeeded;  /* continue processing input */
2227       output_line_command (pfile, same_file);
2228     }
2229   return 0;
2230 }
2231
2232 /*
2233  * unstack after #endif command
2234  */
2235
2236 static int
2237 do_endif (pfile, keyword)
2238      cpp_reader *pfile;
2239      const struct directive *keyword ATTRIBUTE_UNUSED;
2240 {
2241   validate_else (pfile, "#endif");
2242   skip_rest_of_line (pfile);
2243
2244   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2245     cpp_error (pfile, "`#endif' not within a conditional");
2246   else
2247     {
2248       IF_STACK_FRAME *temp = pfile->if_stack;
2249       pfile->if_stack = temp->next;
2250       if (temp->control_macro != 0)
2251         {
2252           /* This #endif matched a #ifndef at the start of the file.
2253              See if it is at the end of the file.  */
2254           int c;
2255
2256           parse_set_mark (pfile);
2257
2258           for (;;)
2259             {
2260               cpp_skip_hspace (pfile);
2261               c = GETC ();
2262               if (c != '\n')
2263                 break;
2264             }
2265           parse_goto_mark (pfile);
2266
2267           if (c == EOF)
2268             {
2269               /* This #endif ends a #ifndef
2270                  that contains all of the file (aside from whitespace).
2271                  Arrange not to include the file again
2272                  if the macro that was tested is defined. */
2273               struct cpp_buffer *ip;
2274               for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2275                 if (ip->fname != NULL)
2276                   break;
2277               ip->ihash->control_macro = (char *) temp->control_macro;
2278             }
2279         }
2280       free (temp);
2281       output_line_command (pfile, same_file);
2282     }
2283   return 0;
2284 }
2285
2286 /* Issue -pedantic warning for text which is not a comment following
2287    an #else or #endif.  Do not warn in system headers, as this is harmless
2288    and very common on old systems.  */
2289
2290 static void
2291 validate_else (pfile, directive)
2292      cpp_reader *pfile;
2293      const char *directive;
2294 {
2295   if (! CPP_PEDANTIC (pfile))
2296     return;
2297
2298   cpp_skip_hspace (pfile);
2299   if (PEEKC () != '\n')
2300     cpp_pedwarn (pfile,
2301                  "text following `%s' violates ANSI standard", directive);
2302 }
2303
2304 /* Convert T_IF, etc. to a string.   Used in error messages.  */
2305 static const char *
2306 if_directive_name (pfile, ifs)
2307      cpp_reader *pfile;
2308      struct if_stack *ifs;
2309 {
2310   switch (ifs->type)
2311     {
2312     case T_IF:      return "#if";
2313     case T_IFDEF:   return "#ifdef";
2314     case T_IFNDEF:  return "#ifndef";
2315     case T_ELIF:    return "#elif";
2316     case T_ELSE:    return "#else";
2317     default:
2318       cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2319       return "unknown";
2320     }
2321 }
2322
2323 /* Get the next token, and add it to the text in pfile->token_buffer.
2324    Return the kind of token we got.  */
2325
2326 enum cpp_token
2327 cpp_get_token (pfile)
2328      cpp_reader *pfile;
2329 {
2330   register int c, c2, c3;
2331   enum cpp_token token;
2332   struct cpp_options *opts = CPP_OPTIONS (pfile);
2333
2334  get_next:
2335   c = GETC();
2336   if (c == EOF)
2337     {
2338       if (CPP_BUFFER (pfile)->manual_pop)
2339         /* If we've been reading from redirected input, the
2340            frontend will pop the buffer.  */
2341         return CPP_EOF;
2342       else if (CPP_BUFFER (pfile)->seen_eof)
2343         {
2344           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2345             return CPP_EOF;
2346
2347           cpp_pop_buffer (pfile);
2348           goto get_next;
2349         }
2350       else
2351         {
2352           cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2353           struct if_stack *ifs, *nifs;
2354
2355           /* Unwind the conditional stack and generate error messages.  */
2356           for (ifs = pfile->if_stack;
2357                ifs != CPP_BUFFER (pfile)->if_stack;
2358                ifs = nifs)
2359             {
2360               cpp_error_with_line (pfile, ifs->lineno, -1,
2361                                    "unterminated `%s' conditional",
2362                                    if_directive_name (pfile, ifs));
2363
2364               nifs = ifs->next;
2365               free (ifs);
2366             }
2367           pfile->if_stack = ifs;
2368
2369           if (CPP_BUFFER (pfile)->nominal_fname
2370               && next_buf != CPP_NULL_BUFFER (pfile))
2371             {
2372               /* We're about to return from an #include file.
2373                  Emit #line information now (as part of the CPP_POP) result.
2374                  But the #line refers to the file we will pop to.  */
2375               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2376               CPP_BUFFER (pfile) = next_buf;
2377               pfile->input_stack_listing_current = 0;
2378               output_line_command (pfile, leave_file);
2379               CPP_BUFFER (pfile) = cur_buffer;
2380             }
2381
2382           CPP_BUFFER (pfile)->seen_eof = 1;
2383           return CPP_POP;
2384         }
2385     }
2386   else
2387     {
2388       switch (c)
2389         {
2390         case '/':
2391           if (PEEKC () == '=')
2392             goto op2;
2393
2394         comment:
2395           if (opts->discard_comments)
2396             c = skip_comment (pfile, c);
2397           else
2398             c = copy_comment (pfile, c);
2399           if (c != ' ')
2400             goto randomchar;
2401           
2402           /* Comments are equivalent to spaces.
2403              For -traditional, a comment is equivalent to nothing.  */
2404           if (opts->traditional || !opts->discard_comments)
2405             return CPP_COMMENT;
2406           else
2407             {
2408               CPP_PUTC (pfile, c);
2409               return CPP_HSPACE;
2410             }
2411
2412         case '#':
2413           if (pfile->parsing_if_directive)
2414             {
2415               cpp_skip_hspace (pfile);
2416               parse_assertion (pfile);
2417               return CPP_ASSERTION;
2418             }
2419
2420           if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2421             {
2422               CPP_RESERVE (pfile, 3);
2423               CPP_PUTC_Q (pfile, '#');
2424               CPP_NUL_TERMINATE_Q (pfile);
2425               if (PEEKC () != '#')
2426                 return CPP_STRINGIZE;
2427               
2428               FORWARD (1);
2429               CPP_PUTC_Q (pfile, '#');
2430               CPP_NUL_TERMINATE_Q (pfile);
2431               return CPP_TOKPASTE;
2432             }
2433
2434           if (!pfile->only_seen_white)
2435             goto randomchar;
2436           /* -traditional directives are recognized only with the # in
2437              column 1.
2438              XXX Layering violation.  */
2439           if (CPP_TRADITIONAL (pfile)
2440               && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2441             goto randomchar;
2442           if (handle_directive (pfile))
2443             return CPP_DIRECTIVE;
2444           pfile->only_seen_white = 0;
2445           goto randomchar;
2446
2447         case '\"':
2448         case '\'':
2449           parse_string (pfile, c);
2450           pfile->only_seen_white = 0;
2451           return c == '\'' ? CPP_CHAR : CPP_STRING;
2452
2453         case '$':
2454           if (!opts->dollars_in_ident)
2455             goto randomchar;
2456           goto letter;
2457
2458         case ':':
2459           if (opts->cplusplus && PEEKC () == ':')
2460             goto op2;
2461           goto randomchar;
2462
2463         case '&':
2464         case '+':
2465         case '|':
2466           c2 = PEEKC ();
2467           if (c2 == c || c2 == '=')
2468             goto op2;
2469           goto randomchar;
2470
2471         case '*':
2472         case '!':
2473         case '%':
2474         case '=':
2475         case '^':
2476           if (PEEKC () == '=')
2477             goto op2;
2478           goto randomchar;
2479
2480         case '-':
2481           c2 = PEEKC ();
2482           if (c2 == '-' && opts->chill)
2483             goto comment;  /* Chill style comment */
2484           if (c2 == '-' || c2 == '=')
2485             goto op2;
2486           if (c2 == '>')
2487             {
2488               if (opts->cplusplus && PEEKN (1) == '*')
2489                 {
2490                   /* In C++, there's a ->* operator.  */
2491                   token = CPP_OTHER;
2492                   pfile->only_seen_white = 0;
2493                   CPP_RESERVE (pfile, 4);
2494                   CPP_PUTC_Q (pfile, c);
2495                   CPP_PUTC_Q (pfile, GETC ());
2496                   CPP_PUTC_Q (pfile, GETC ());
2497                   CPP_NUL_TERMINATE_Q (pfile);
2498                   return token;
2499                 }
2500               goto op2;
2501             }
2502           goto randomchar;
2503
2504         case '<':
2505           if (pfile->parsing_include_directive)
2506             {
2507               for (;;)
2508                 {
2509                   CPP_PUTC (pfile, c);
2510                   if (c == '>')
2511                     break;
2512                   c = GETC ();
2513                   if (c == '\n' || c == EOF)
2514                     {
2515                       cpp_error (pfile,
2516                                  "missing '>' in `#include <FILENAME>'");
2517                       break;
2518                     }
2519                   else if (c == '\r')
2520                     {
2521                       if (!CPP_BUFFER (pfile)->has_escapes)
2522                         {
2523                           /* Backslash newline is replaced by nothing. */
2524                           CPP_ADJUST_WRITTEN (pfile, -1);
2525                           CPP_BUMP_LINE (pfile);
2526                         }
2527                       else
2528                         {
2529                           /* We might conceivably get \r- or \r<space> in
2530                              here.  Just delete 'em. */
2531                           int d = GETC();
2532                           if (d != '-' && d != ' ')
2533                             cpp_ice (pfile, "unrecognized escape \\r%c", d);
2534                           CPP_ADJUST_WRITTEN (pfile, -1);
2535                         }                         
2536                     }
2537                 }
2538               return CPP_STRING;
2539             }
2540           /* else fall through */
2541         case '>':
2542           c2 = PEEKC ();
2543           if (c2 == '=')
2544             goto op2;
2545           /* GNU C++ supports MIN and MAX operators <? and >?.  */
2546           if (c2 != c && (!opts->cplusplus || c2 != '?'))
2547             goto randomchar;
2548           FORWARD(1);
2549           CPP_RESERVE (pfile, 4);
2550           CPP_PUTC (pfile, c);
2551           CPP_PUTC (pfile, c2);
2552           c3 = PEEKC ();
2553           if (c3 == '=')
2554             CPP_PUTC_Q (pfile, GETC ());
2555           CPP_NUL_TERMINATE_Q (pfile);
2556           pfile->only_seen_white = 0;
2557           return CPP_OTHER;
2558
2559         case '.':
2560           c2 = PEEKC ();
2561           if (ISDIGIT(c2))
2562             {
2563               CPP_RESERVE(pfile, 2);
2564               CPP_PUTC_Q (pfile, '.');
2565               c = GETC ();
2566               goto number;
2567             }
2568
2569           /* In C++ there's a .* operator.  */
2570           if (opts->cplusplus && c2 == '*')
2571             goto op2;
2572
2573           if (c2 == '.' && PEEKN(1) == '.')
2574             {
2575               CPP_RESERVE(pfile, 4);
2576               CPP_PUTC_Q (pfile, '.');
2577               CPP_PUTC_Q (pfile, '.');
2578               CPP_PUTC_Q (pfile, '.');
2579               FORWARD (2);
2580               CPP_NUL_TERMINATE_Q (pfile);
2581               pfile->only_seen_white = 0;
2582               return CPP_3DOTS;
2583             }
2584           goto randomchar;
2585
2586         op2:
2587           token = CPP_OTHER;
2588           pfile->only_seen_white = 0;
2589           CPP_RESERVE(pfile, 3);
2590           CPP_PUTC_Q (pfile, c);
2591           CPP_PUTC_Q (pfile, GETC ());
2592           CPP_NUL_TERMINATE_Q (pfile);
2593           return token;
2594
2595         case 'L':
2596           c2 = PEEKC ();
2597           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2598             {
2599               CPP_PUTC (pfile, c);
2600               c = GETC ();
2601               parse_string (pfile, c);
2602               pfile->only_seen_white = 0;
2603               return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2604             }
2605           goto letter;
2606
2607         case '0': case '1': case '2': case '3': case '4':
2608         case '5': case '6': case '7': case '8': case '9':
2609         number:
2610           c2  = '.';
2611           for (;;)
2612             {
2613               CPP_RESERVE (pfile, 2);
2614               CPP_PUTC_Q (pfile, c);
2615               c = PEEKC ();
2616               if (c == EOF)
2617                 break;
2618               if (!is_idchar(c) && c != '.'
2619                   && ((c2 != 'e' && c2 != 'E'
2620                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2621                       || (c != '+' && c != '-')))
2622                 break;
2623               FORWARD(1);
2624               c2= c;
2625             }
2626           CPP_NUL_TERMINATE_Q (pfile);
2627           pfile->only_seen_white = 0;
2628           return CPP_NUMBER;
2629         case 'b': case 'c': case 'd': case 'h': case 'o':
2630         case 'B': case 'C': case 'D': case 'H': case 'O':
2631           if (opts->chill && PEEKC () == '\'')
2632             {
2633               pfile->only_seen_white = 0;
2634               CPP_RESERVE (pfile, 2);
2635               CPP_PUTC_Q (pfile, c);
2636               CPP_PUTC_Q (pfile, '\'');
2637               FORWARD(1);
2638               for (;;)
2639                 {
2640                   c = GETC();
2641                   if (c == EOF)
2642                     goto chill_number_eof;
2643                   if (!is_idchar(c))
2644                     break;
2645                   CPP_PUTC (pfile, c);
2646                 }
2647               if (c == '\'')
2648                 {
2649                   CPP_RESERVE (pfile, 2);
2650                   CPP_PUTC_Q (pfile, c);
2651                   CPP_NUL_TERMINATE_Q (pfile);
2652                   return CPP_STRING;
2653                 }
2654               else
2655                 {
2656                   FORWARD(-1);
2657                 chill_number_eof:
2658                   CPP_NUL_TERMINATE (pfile);
2659                   return CPP_NUMBER;
2660                 }
2661             }
2662           else
2663             goto letter;
2664         case '_':
2665         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2666         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2667         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2668         case 'x': case 'y': case 'z':
2669         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2670         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2671         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2672         case 'Y': case 'Z':
2673         letter:
2674           {
2675             HASHNODE *hp;
2676             unsigned char *ident;
2677             int before_name_written = CPP_WRITTEN (pfile);
2678             int ident_len;
2679             parse_name (pfile, c);
2680             pfile->only_seen_white = 0;
2681             if (pfile->no_macro_expand)
2682               return CPP_NAME;
2683             ident = pfile->token_buffer + before_name_written;
2684             ident_len = CPP_PWRITTEN (pfile) - ident;
2685             hp = cpp_lookup (pfile, ident, ident_len);
2686             if (!hp)
2687               return CPP_NAME;
2688             if (hp->type == T_DISABLED)
2689               {
2690                 if (pfile->output_escapes)
2691                   { /* Return "\r-IDENT", followed by '\0'.  */
2692                     int i;
2693                     CPP_RESERVE (pfile, 3);
2694                     ident = pfile->token_buffer + before_name_written;
2695                     CPP_ADJUST_WRITTEN (pfile, 2);
2696                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2697                     ident[0] = '\r';
2698                     ident[1] = '-';
2699                   }
2700                 return CPP_NAME;
2701               }
2702
2703             /* If macro wants an arglist, verify that a '(' follows.  */
2704             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2705             {
2706               int macbuf_whitespace = 0;
2707
2708               while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2709                 {
2710                   U_CHAR *point = CPP_BUFFER (pfile)->cur;
2711                   for (;;)
2712                     {
2713                       cpp_skip_hspace (pfile);
2714                       c = PEEKC ();
2715                       if (c == '\n')
2716                         FORWARD(1);
2717                       else
2718                         break;
2719                     }
2720                   if (point != CPP_BUFFER (pfile)->cur)
2721                     macbuf_whitespace = 1;
2722                   if (c == '(')
2723                     goto is_macro_call;
2724                   else if (c != EOF)
2725                     goto not_macro_call;
2726                   cpp_pop_buffer (pfile);
2727                 }
2728
2729               parse_set_mark (pfile);
2730               for (;;)
2731                 {
2732                   cpp_skip_hspace (pfile);
2733                   c = PEEKC ();
2734                   if (c == '\n')
2735                     FORWARD(1);
2736                   else
2737                     break;
2738                 }
2739               parse_goto_mark (pfile);
2740
2741               if (c == '(')
2742                 goto is_macro_call;
2743
2744             not_macro_call:
2745               if (macbuf_whitespace)
2746                 CPP_PUTC (pfile, ' ');
2747               return CPP_NAME;
2748             }
2749           is_macro_call:
2750             /* This is now known to be a macro call.
2751                Expand the macro, reading arguments as needed,
2752                and push the expansion on the input stack.  */
2753             macroexpand (pfile, hp);
2754             CPP_SET_WRITTEN (pfile, before_name_written);
2755           }
2756           goto get_next;
2757
2758         case ' ':  case '\t':  case '\v':
2759           for (;;)
2760             {
2761               CPP_PUTC (pfile, c);
2762               c = PEEKC ();
2763               if (c == EOF || !is_hspace(c))
2764                 break;
2765               FORWARD(1);
2766             }
2767           return CPP_HSPACE;
2768
2769         case '\r':
2770           if (CPP_BUFFER (pfile)->has_escapes)
2771             {
2772               c = GETC ();
2773               if (c == '-')
2774                 {
2775                   if (pfile->output_escapes)
2776                     CPP_PUTS (pfile, "\r-", 2);
2777                   parse_name (pfile, GETC ());
2778                   return CPP_NAME;
2779                 }
2780               else if (c == ' ')
2781                 {
2782                   CPP_RESERVE (pfile, 2);
2783                   if (pfile->output_escapes)
2784                     CPP_PUTC_Q (pfile, '\r');
2785                   CPP_PUTC_Q (pfile, c);
2786                   return CPP_HSPACE;
2787                 }
2788               else
2789                 {
2790                   cpp_ice (pfile, "unrecognized escape \\r%c", c);
2791                   goto get_next;
2792                 }
2793             }
2794           else
2795             {
2796               /* Backslash newline is ignored. */
2797               CPP_BUMP_LINE (pfile);
2798               goto get_next;
2799             }
2800
2801         case '\n':
2802           CPP_PUTC (pfile, c);
2803           if (pfile->only_seen_white == 0)
2804             pfile->only_seen_white = 1;
2805           CPP_BUMP_LINE (pfile);
2806           if (! CPP_OPTIONS (pfile)->no_line_commands)
2807             {
2808               pfile->lineno++;
2809               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2810                 output_line_command (pfile, same_file);
2811             }
2812           return CPP_VSPACE;
2813
2814         case '(': token = CPP_LPAREN;    goto char1;
2815         case ')': token = CPP_RPAREN;    goto char1;
2816         case '{': token = CPP_LBRACE;    goto char1;
2817         case '}': token = CPP_RBRACE;    goto char1;
2818         case ',': token = CPP_COMMA;     goto char1;
2819         case ';': token = CPP_SEMICOLON; goto char1;
2820
2821         randomchar:
2822         default:
2823           token = CPP_OTHER;
2824         char1:
2825           pfile->only_seen_white = 0;
2826           CPP_PUTC (pfile, c);
2827           return token;
2828         }
2829     }
2830 }
2831
2832 /* Like cpp_get_token, but skip spaces and comments.  */
2833
2834 enum cpp_token
2835 cpp_get_non_space_token (pfile)
2836      cpp_reader *pfile;
2837 {
2838   int old_written = CPP_WRITTEN (pfile);
2839   for (;;)
2840     {
2841       enum cpp_token token = cpp_get_token (pfile);
2842       if (token != CPP_COMMENT && token != CPP_POP
2843           && token != CPP_HSPACE && token != CPP_VSPACE)
2844         return token;
2845       CPP_SET_WRITTEN (pfile, old_written);
2846     }
2847 }
2848
2849 /* Parse an identifier starting with C.  */
2850
2851 static void
2852 parse_name (pfile, c)
2853      cpp_reader *pfile;
2854      int c;
2855 {
2856   for (;;)
2857   {
2858       if (! is_idchar(c))
2859       {
2860           FORWARD (-1);
2861           break;
2862       }
2863
2864       if (c == '$' && CPP_PEDANTIC (pfile))
2865         cpp_pedwarn (pfile, "`$' in identifier");
2866
2867       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2868       CPP_PUTC_Q (pfile, c);
2869       c = GETC();
2870       if (c == EOF)
2871         break;
2872   }
2873   CPP_NUL_TERMINATE_Q (pfile);
2874   return;
2875 }
2876
2877 /* Parse and skip over a string starting with C.  A single quoted
2878    string is treated like a double -- some programs (e.g., troff) are
2879    perverse this way.  (However, a single quoted string is not allowed
2880    to extend over multiple lines.)  */
2881 static void
2882 skip_string (pfile, c)
2883      cpp_reader *pfile;
2884      int c;
2885 {
2886   long start_line, start_column;
2887   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2888
2889   while (1)
2890     {
2891       int cc = GETC();
2892       switch (cc)
2893         {
2894         case EOF:
2895           cpp_error_with_line (pfile, start_line, start_column,
2896                                "unterminated string or character constant");
2897           if (pfile->multiline_string_line != start_line
2898               && pfile->multiline_string_line != 0)
2899             cpp_error_with_line (pfile,
2900                                  pfile->multiline_string_line, -1,
2901                          "possible real start of unterminated constant");
2902           pfile->multiline_string_line = 0;
2903           return;
2904
2905         case '\n':
2906           CPP_BUMP_LINE (pfile);
2907           /* In Fortran and assembly language, silently terminate
2908              strings of either variety at end of line.  This is a
2909              kludge around not knowing where comments are in these
2910              languages.  */
2911           if (CPP_OPTIONS (pfile)->lang_fortran
2912               || CPP_OPTIONS (pfile)->lang_asm)
2913             {
2914               FORWARD(-1);
2915               return;
2916             }
2917           /* Character constants may not extend over multiple lines.
2918              In Standard C, neither may strings.  We accept multiline
2919              strings as an extension.  */
2920           if (c == '\'')
2921             {
2922               cpp_error_with_line (pfile, start_line, start_column,
2923                                    "unterminated character constant");
2924               FORWARD(-1);
2925               return;
2926             }
2927           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2928             cpp_pedwarn_with_line (pfile, start_line, start_column,
2929                                    "string constant runs past end of line");
2930           if (pfile->multiline_string_line == 0)
2931             pfile->multiline_string_line = start_line;
2932           break;
2933
2934         case '\r':
2935           if (CPP_BUFFER (pfile)->has_escapes)
2936             {
2937               cpp_ice (pfile, "\\r escape inside string constant");
2938               FORWARD(1);
2939             }
2940           else
2941             /* Backslash newline is replaced by nothing at all.  */
2942             CPP_BUMP_LINE (pfile);
2943           break;
2944
2945         case '\\':
2946           FORWARD(1);
2947           break;
2948
2949         case '\"':
2950         case '\'':
2951           if (cc == c)
2952             return;
2953           break;
2954         }
2955     }
2956 }
2957
2958 /* Parse a string and copy it to the output.  */
2959
2960 static void
2961 parse_string (pfile, c)
2962      cpp_reader *pfile;
2963      int c;
2964 {
2965   U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
2966   U_CHAR *limit;
2967
2968   skip_string (pfile, c);
2969
2970   limit = CPP_BUFFER (pfile)->cur;
2971   CPP_RESERVE (pfile, limit - start + 2);
2972   CPP_PUTC_Q (pfile, c);
2973   for (; start < limit; start++)
2974     if (*start != '\r')
2975       CPP_PUTC_Q (pfile, *start);
2976 }
2977
2978 /* Read an assertion into the token buffer, converting to
2979    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2980    character to read should be the first letter of the predicate.
2981    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2982    with answer (see callers for why). In case of 0, an error has been
2983    printed. */
2984 static int
2985 parse_assertion (pfile)
2986      cpp_reader *pfile;
2987 {
2988   int c, dropwhite;
2989   cpp_skip_hspace (pfile);
2990   c = PEEKC();
2991   if (! is_idstart(c))
2992     {
2993       cpp_error (pfile, "assertion predicate is not an identifier");
2994       return 0;
2995     }
2996   CPP_PUTC(pfile, '#');
2997   FORWARD(1);
2998   parse_name(pfile, c);
2999
3000   c = PEEKC();
3001   if (c != '(')
3002     {
3003       if (is_hspace(c) || c == '\r')
3004         cpp_skip_hspace (pfile);
3005       c = PEEKC();
3006     }
3007   if (c != '(')
3008     return 1;
3009
3010   CPP_PUTC(pfile, '(');
3011   FORWARD(1);
3012   dropwhite = 1;
3013   while ((c = GETC()) != ')')
3014     {
3015       if (is_space(c))
3016         {
3017           if (! dropwhite)
3018             {
3019               CPP_PUTC(pfile, ' ');
3020               dropwhite = 1;
3021             }
3022         }
3023       else if (c == '\n' || c == EOF)
3024         {
3025           if (c == '\n') FORWARD(-1);
3026           cpp_error (pfile, "un-terminated assertion answer");
3027           return 0;
3028         }
3029       else if (c == '\r')
3030         /* \r cannot be a macro escape here. */
3031         CPP_BUMP_LINE (pfile);
3032       else
3033         {
3034           CPP_PUTC (pfile, c);
3035           dropwhite = 0;
3036         }
3037     }
3038
3039   if (pfile->limit[-1] == ' ')
3040     pfile->limit[-1] = ')';
3041   else if (pfile->limit[-1] == '(')
3042     {
3043       cpp_error (pfile, "empty token sequence in assertion");
3044       return 0;
3045     }
3046   else
3047     CPP_PUTC (pfile, ')');
3048
3049   CPP_NUL_TERMINATE (pfile);
3050   return 2;
3051 }
3052
3053 static int
3054 do_assert (pfile, keyword)
3055      cpp_reader *pfile;
3056      const struct directive *keyword ATTRIBUTE_UNUSED;
3057 {
3058   char *sym;
3059   int ret, c;
3060   HASHNODE *base, *this;
3061   int baselen, thislen;
3062
3063   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3064     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3065
3066   cpp_skip_hspace (pfile);
3067   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3068   ret = parse_assertion (pfile);
3069   if (ret == 0)
3070     goto error;
3071   else if (ret == 1)
3072     {
3073       cpp_error (pfile, "missing token-sequence in `#assert'");
3074       goto error;
3075     }
3076
3077   cpp_skip_hspace (pfile);
3078   c = PEEKC();
3079   if (c != EOF && c != '\n')
3080     {
3081       cpp_error (pfile, "junk at end of `#assert'");
3082       goto error;
3083     }
3084
3085   thislen = strlen (sym);
3086   baselen = index (sym, '(') - sym;
3087   this = cpp_lookup (pfile, sym, thislen);
3088   if (this)
3089     {
3090       cpp_warning (pfile, "`%s' re-asserted", sym);
3091       goto error;
3092     }
3093
3094   base = cpp_lookup (pfile, sym, baselen);
3095   if (! base)
3096     base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3097   else if (base->type != T_ASSERT)
3098   {
3099     /* Token clash - but with what?! */
3100     cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3101     goto error;
3102   }
3103
3104   this = cpp_install (pfile, sym, thislen, T_ASSERT,
3105                       (char *)base->value.aschain);
3106   base->value.aschain = this;
3107   
3108   pfile->limit = (unsigned char *) sym; /* Pop */
3109   return 0;
3110
3111  error:
3112   skip_rest_of_line (pfile);
3113   pfile->limit = (unsigned char *) sym; /* Pop */
3114   return 0;
3115 }
3116
3117 static int
3118 do_unassert (pfile, keyword)
3119      cpp_reader *pfile;
3120      const struct directive *keyword ATTRIBUTE_UNUSED;
3121 {
3122   int c, ret;
3123   char *sym;
3124   long baselen, thislen;
3125   HASHNODE *base, *this, *next;
3126   
3127   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3128     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3129
3130   cpp_skip_hspace (pfile);
3131
3132   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3133   ret = parse_assertion (pfile);
3134   if (ret == 0)
3135     goto error;
3136   
3137   cpp_skip_hspace (pfile);
3138   c = PEEKC ();
3139   if (c != EOF && c != '\n')
3140       cpp_error (pfile, "junk at end of `#unassert'");
3141
3142   thislen = strlen (sym);
3143   if (ret == 1)
3144     {
3145       base = cpp_lookup (pfile, sym, thislen);
3146       if (! base)
3147         goto error;  /* It isn't an error to #undef what isn't #defined,
3148                         so it isn't an error to #unassert what isn't
3149                         #asserted either. */
3150       
3151       for (this = base->value.aschain; this; this = next)
3152         {
3153           next = this->value.aschain;
3154           delete_macro (this);
3155         }
3156       delete_macro (base);
3157     }
3158   else
3159     {
3160       baselen = index (sym, '(') - sym;
3161       base = cpp_lookup (pfile, sym, baselen);
3162       if (! base) goto error;
3163       this = cpp_lookup (pfile, sym, thislen);
3164       if (! this) goto error;
3165
3166       next = base;
3167       while (next->value.aschain != this)
3168         next = next->value.aschain;
3169
3170       next->value.aschain = this->value.aschain;
3171       delete_macro (this);
3172
3173       if (base->value.aschain == NULL)
3174         delete_macro (base);  /* Last answer for this predicate deleted. */
3175     }
3176   
3177   pfile->limit = (unsigned char *) sym; /* Pop */
3178   return 0;
3179  error:
3180   skip_rest_of_line (pfile);
3181   pfile->limit = (unsigned char *) sym; /* Pop */
3182   return 0;
3183 }
3184
3185 /* Process STR as if it appeared as the body of an #unassert. */
3186 void
3187 cpp_unassert (pfile, str)
3188      cpp_reader *pfile;
3189      unsigned char *str;
3190 {
3191   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3192     {
3193       do_unassert (pfile, NULL);
3194       cpp_pop_buffer (pfile);
3195     }
3196 }  
3197
3198 /* Remember the current position of PFILE so it may be returned to
3199    after looking ahead a bit.
3200
3201    Note that when you set a mark, you _must_ return to that mark.  You
3202    may not forget about it and continue parsing.  You may not pop a
3203    buffer with an active mark.  You may not call CPP_BUMP_LINE while a
3204    mark is active.  */
3205
3206 static void
3207 parse_set_mark (pfile)
3208      cpp_reader *pfile;
3209 {
3210   cpp_buffer *ip = CPP_BUFFER (pfile);
3211   if (ACTIVE_MARK_P())
3212       cpp_ice (pfile, "mark active in parse_set_mark");
3213
3214   ip->mark = ip->cur - ip->buf;
3215 }
3216
3217 /* Backup the current position of PFILE to that saved in its mark,
3218    and clear the mark.  */
3219
3220 static void
3221 parse_goto_mark (pfile)
3222      cpp_reader *pfile;
3223 {
3224   cpp_buffer *ip = CPP_BUFFER (pfile);
3225   if (!ACTIVE_MARK_P())
3226       cpp_ice (pfile, "mark not active in parse_goto_mark");
3227
3228   ip->cur = ip->buf + ip->mark;
3229   ip->mark = -1;
3230 }