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