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