cpplib.h (enum file_change_code): Added rename_file.
[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 && file_change != rename_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       file_change = rename_file;
1362
1363       if (read_line_number (pfile, &action_number))
1364         {
1365           if (CPP_PEDANTIC (pfile))
1366             cpp_pedwarn (pfile, "garbage at end of `#line' command");
1367
1368           if (action_number == 1)
1369             {
1370               file_change = enter_file;
1371               read_line_number (pfile, &action_number);
1372             }
1373           else if (action_number == 2)
1374             {
1375               file_change = leave_file;
1376               read_line_number (pfile, &action_number);
1377             }
1378           if (action_number == 3)
1379             {
1380               ip->system_header_p = 1;
1381               read_line_number (pfile, &action_number);
1382             }
1383           if (action_number == 4)
1384             {
1385               ip->system_header_p = 2;
1386               read_line_number (pfile, &action_number);
1387             }
1388         }
1389       
1390       *end_name = '\0';
1391       
1392       if (strcmp (fname, ip->nominal_fname))
1393         {
1394           const char *newname, *oldname;
1395           if (!strcmp (fname, ip->fname))
1396             newname = ip->fname;
1397           else if (ip->last_nominal_fname
1398                    && !strcmp (fname, ip->last_nominal_fname))
1399             newname = ip->last_nominal_fname;
1400           else
1401             newname = xstrdup (fname);
1402
1403           oldname = ip->nominal_fname;
1404           ip->nominal_fname = newname;
1405
1406           if (ip->last_nominal_fname
1407               && ip->last_nominal_fname != oldname
1408               && ip->last_nominal_fname != newname
1409               && ip->last_nominal_fname != ip->fname)
1410             free ((void *) ip->last_nominal_fname);
1411
1412           if (newname == ip->fname)
1413             ip->last_nominal_fname = NULL;
1414           else
1415             ip->last_nominal_fname = oldname;
1416         } 
1417     }
1418   else if (token != CPP_VSPACE && token != CPP_EOF)
1419     {
1420       cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1421       goto bad_line_directive;
1422     }
1423
1424   /* The Newline at the end of this line remains to be processed.
1425      To put the next line at the specified line number,
1426      we must store a line number now that is one less.  */
1427   ip->lineno = new_lineno - 1;
1428   CPP_SET_WRITTEN (pfile, old_written);
1429   output_line_command (pfile, file_change);
1430   return 0;
1431
1432  bad_line_directive:
1433   skip_rest_of_line (pfile);
1434   CPP_SET_WRITTEN (pfile, old_written);
1435   return 0;
1436 }
1437
1438 /* Remove the definition of a symbol from the symbol table.
1439    According to the C standard, it is not an error to undef
1440    something that has no definitions. */
1441 static int
1442 do_undef (pfile, keyword)
1443      cpp_reader *pfile;
1444      const struct directive *keyword;
1445 {
1446   int len;
1447   HASHNODE *hp;
1448   U_CHAR *buf, *name, *limit;
1449   int c;
1450   long here = CPP_WRITTEN (pfile);
1451   enum cpp_token token;
1452
1453   cpp_skip_hspace (pfile);
1454   c = GETC();
1455   if (! is_idstart(c))
1456   {
1457       cpp_error (pfile, "token after #undef is not an identifier");
1458       skip_rest_of_line (pfile);
1459       return 1;
1460   }
1461
1462   parse_name (pfile, c);
1463   buf = pfile->token_buffer + here;
1464   limit = CPP_PWRITTEN(pfile);
1465
1466   /* Copy out the token so we can pop the token buffer. */
1467   len = limit - buf;
1468   name = (U_CHAR *) alloca (len + 1);
1469   memcpy (name, buf, len);
1470   name[limit - buf] = '\0';
1471
1472   token = get_directive_token (pfile);
1473   if (token != CPP_VSPACE && token != CPP_POP)
1474   {
1475       cpp_pedwarn (pfile, "junk on line after #undef");
1476       skip_rest_of_line (pfile);
1477   }
1478
1479   CPP_SET_WRITTEN (pfile, here);
1480
1481   while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1482     {
1483       /* If we are generating additional info for debugging (with -g) we
1484          need to pass through all effective #undef commands.  */
1485       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1486         pass_thru_directive (name, len, pfile, keyword);
1487       if (hp->type == T_POISON)
1488         cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1489       else 
1490         {
1491           if (hp->type != T_MACRO)
1492             cpp_warning (pfile, "undefining `%s'", hp->name);
1493           delete_macro (hp);
1494         }
1495     }
1496
1497   return 0;
1498 }
1499
1500 /* Wrap do_undef for -U processing. */
1501 void
1502 cpp_undef (pfile, macro)
1503      cpp_reader *pfile;
1504      U_CHAR *macro;
1505 {
1506   /* Copy the string so we can append a newline.  */
1507   size_t len = strlen (macro);
1508   U_CHAR *buf = alloca (len + 2);
1509   memcpy (buf, macro, len);
1510   buf[len]     = '\n';
1511   buf[len + 1] = '\0';
1512   if (cpp_push_buffer (pfile, buf, len + 1))
1513     {
1514       do_undef (pfile, NULL);
1515       cpp_pop_buffer (pfile);
1516     }
1517 }
1518
1519 /*
1520  * Report an error detected by the program we are processing.
1521  * Use the text of the line in the error message.
1522  * (We use error because it prints the filename & line#.)
1523  */
1524
1525 static int
1526 do_error (pfile, keyword)
1527      cpp_reader *pfile;
1528      const struct directive *keyword ATTRIBUTE_UNUSED;
1529 {
1530   U_CHAR *text, *limit;
1531
1532   cpp_skip_hspace (pfile);
1533   text = CPP_BUFFER (pfile)->cur;
1534   skip_rest_of_line (pfile);
1535   limit = CPP_BUFFER (pfile)->cur;
1536
1537   cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1538   return 0;
1539 }
1540
1541 /*
1542  * Report a warning detected by the program we are processing.
1543  * Use the text of the line in the warning message, then continue.
1544  */
1545
1546 static int
1547 do_warning (pfile, keyword)
1548      cpp_reader *pfile;
1549      const struct directive *keyword ATTRIBUTE_UNUSED;
1550 {
1551   U_CHAR *text, *limit;
1552
1553   cpp_skip_hspace (pfile);
1554   text = CPP_BUFFER (pfile)->cur;
1555   skip_rest_of_line (pfile);
1556   limit = CPP_BUFFER (pfile)->cur;
1557
1558   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1559     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1560
1561   cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1562   return 0;
1563 }
1564
1565 /* Report program identification.  */
1566
1567 static int
1568 do_ident (pfile, keyword)
1569      cpp_reader *pfile;
1570      const struct directive *keyword ATTRIBUTE_UNUSED;
1571 {
1572   long old_written = CPP_WRITTEN (pfile);
1573
1574   /* Allow #ident in system headers, since that's not user's fault.  */
1575   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1576     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1577
1578   CPP_PUTS (pfile, "#ident ", 7);
1579
1580   /* Next token should be a string constant.  */
1581   if (get_directive_token (pfile) == CPP_STRING)
1582     /* And then a newline.  */
1583     if (get_directive_token (pfile) == CPP_VSPACE)
1584       /* Good - ship it.  */
1585       return 0;
1586
1587   cpp_error (pfile, "invalid #ident");
1588   skip_rest_of_line (pfile);
1589   CPP_SET_WRITTEN (pfile, old_written);  /* discard directive */
1590
1591   return 0;
1592 }
1593
1594 /* Pragmata handling.  We handle some of these, and pass the rest on
1595    to the front end.  C99 defines three pragmas and says that no macro
1596    expansion is to be performed on them; whether or not macro
1597    expansion happens for other pragmas is implementation defined.
1598    This implementation never macro-expands the text after #pragma.
1599
1600    We currently do not support the _Pragma operator.  Support for that
1601    has to be coordinated with the front end.  Proposed implementation:
1602    both #pragma blah blah and _Pragma("blah blah") become
1603    __builtin_pragma(blah blah) and we teach the parser about that.  */
1604
1605 /* Sub-handlers for the pragmas needing treatment here.
1606    They return 1 if the token buffer is to be popped, 0 if not. */
1607 static int do_pragma_once               PARAMS ((cpp_reader *));
1608 static int do_pragma_implementation     PARAMS ((cpp_reader *));
1609 static int do_pragma_poison             PARAMS ((cpp_reader *));
1610 static int do_pragma_default            PARAMS ((cpp_reader *));
1611
1612 static int
1613 do_pragma (pfile, keyword)
1614      cpp_reader *pfile;
1615      const struct directive *keyword ATTRIBUTE_UNUSED;
1616 {
1617   long here, key;
1618   U_CHAR *buf;
1619   int pop;
1620   enum cpp_token token;
1621
1622   here = CPP_WRITTEN (pfile);
1623   CPP_PUTS (pfile, "#pragma ", 8);
1624
1625   key = CPP_WRITTEN (pfile);
1626   pfile->no_macro_expand++;
1627   token = get_directive_token (pfile);
1628   if (token != CPP_NAME)
1629     {
1630       if (token == CPP_VSPACE)
1631         goto empty;
1632       else
1633         goto skip;
1634     }
1635
1636   buf = pfile->token_buffer + key;
1637   CPP_PUTC (pfile, ' ');
1638
1639 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1640   if (tokis ("once"))
1641     pop = do_pragma_once (pfile);
1642   else if (tokis ("implementation"))
1643     pop = do_pragma_implementation (pfile);
1644   else if (tokis ("poison"))
1645     pop = do_pragma_poison (pfile);
1646   else
1647     pop = do_pragma_default (pfile);
1648 #undef tokis
1649
1650   if (get_directive_token (pfile) != CPP_VSPACE)
1651     goto skip;
1652
1653   if (pop)
1654     CPP_SET_WRITTEN (pfile, here);
1655   pfile->no_macro_expand--;
1656   return 0;
1657
1658  skip:
1659   cpp_error (pfile, "malformed #pragma directive");
1660   skip_rest_of_line (pfile);
1661  empty:
1662   CPP_SET_WRITTEN (pfile, here);
1663   pfile->no_macro_expand--;
1664   return 0;
1665 }
1666
1667 static int
1668 do_pragma_default (pfile)
1669      cpp_reader *pfile;
1670 {
1671   while (get_directive_token (pfile) != CPP_VSPACE)
1672     CPP_PUTC (pfile, ' ');
1673   return 0;
1674 }
1675
1676 static int
1677 do_pragma_once (pfile)
1678      cpp_reader *pfile;
1679 {
1680   cpp_buffer *ip = CPP_BUFFER (pfile);
1681
1682   if (ip->fname == NULL)
1683     {
1684       cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1685       return 1;
1686     }
1687   
1688   /* Allow #pragma once in system headers, since that's not the user's
1689      fault.  */
1690   if (!ip->system_header_p)
1691     cpp_warning (pfile, "`#pragma once' is obsolete");
1692       
1693   if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1694     cpp_warning (pfile, "`#pragma once' outside include file");
1695   else
1696     ip->ihash->control_macro = "";  /* never repeat */
1697
1698   return 1;
1699 }
1700
1701 static int
1702 do_pragma_implementation (pfile)
1703      cpp_reader *pfile;
1704 {
1705   /* Be quiet about `#pragma implementation' for a file only if it hasn't
1706      been included yet.  */
1707   struct include_hash *ptr;
1708   enum cpp_token token;
1709   long written = CPP_WRITTEN (pfile);
1710   U_CHAR *name;
1711   U_CHAR *copy;
1712
1713   token = get_directive_token (pfile);
1714   if (token == CPP_VSPACE)
1715     return 0;
1716   else if (token != CPP_STRING)
1717     {
1718       cpp_error (pfile, "malformed #pragma implementation");
1719       return 1;
1720     }
1721
1722   name = pfile->token_buffer + written + 1;
1723   copy = xstrdup (name);
1724   copy[strlen(copy)] = '\0';  /* trim trailing quote */
1725   
1726   ptr = include_hash (pfile, copy, 0);
1727   if (ptr)
1728     cpp_warning (pfile,
1729          "`#pragma implementation' for `%s' appears after file is included",
1730                  copy);
1731   free (copy);
1732   return 0;
1733 }
1734
1735 static int
1736 do_pragma_poison (pfile)
1737      cpp_reader *pfile;
1738 {
1739   /* Poison these symbols so that all subsequent usage produces an
1740      error message.  */
1741   U_CHAR *p;
1742   HASHNODE *hp;
1743   long written;
1744   size_t len;
1745   enum cpp_token token;
1746   int writeit;
1747   /* As a rule, don't include #pragma poison commands in output,  
1748      unless the user asks for them.  */
1749   writeit = (CPP_OPTIONS (pfile)->debug_output
1750              || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1751              || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1752
1753   for (;;)
1754     {
1755       written = CPP_WRITTEN (pfile);
1756       token = get_directive_token (pfile);
1757       if (token == CPP_VSPACE)
1758         break;
1759       if (token != CPP_NAME)
1760         {
1761           cpp_error (pfile, "invalid #pragma poison directive");
1762           skip_rest_of_line (pfile);
1763           return 1;
1764         }
1765
1766       p = pfile->token_buffer + written;
1767       len = strlen (p);
1768       if ((hp = cpp_lookup (pfile, p, len)))
1769         {
1770           if (hp->type != T_POISON)
1771             {
1772               cpp_warning (pfile, "poisoning existing macro `%s'", p);
1773               free_definition (hp->value.defn);
1774               hp->value.defn = 0;
1775               hp->type = T_POISON;
1776             }
1777         }
1778       else
1779         cpp_install (pfile, p, len, T_POISON, 0);
1780       if (writeit)
1781         CPP_PUTC (pfile, ' ');
1782     }
1783   return !writeit;
1784 }
1785  
1786 #ifdef SCCS_DIRECTIVE
1787 /* Just ignore #sccs, on systems where we define it at all.  */
1788
1789 static int
1790 do_sccs (pfile, keyword)
1791      cpp_reader *pfile;
1792      const struct directive *keyword ATTRIBUTE_UNUSED;
1793 {
1794   if (CPP_PEDANTIC (pfile))
1795     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1796   skip_rest_of_line (pfile);
1797   return 0;
1798 }
1799 #endif
1800
1801 /* We've found an `#if' directive.  If the only thing before it in
1802    this file is white space, and if it is of the form
1803    `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1804    for inclusion of this file.  (See redundant_include_p in cppfiles.c
1805    for an explanation of controlling macros.)  If so, return a
1806    malloc'd copy of SYMBOL.  Otherwise, return NULL.  */
1807
1808 static U_CHAR *
1809 detect_if_not_defined (pfile)
1810      cpp_reader *pfile;
1811 {
1812   U_CHAR *control_macro = 0;
1813
1814   if (pfile->only_seen_white == 2)
1815     {
1816       char *ident;
1817       enum cpp_token token;
1818       int base_offset;
1819       int token_offset;
1820       int need_rparen = 0;
1821
1822       /* Save state required for restore.  */
1823       pfile->no_macro_expand++;
1824       parse_set_mark (pfile);
1825       base_offset = CPP_WRITTEN (pfile);
1826
1827       /* Look for `!', */
1828       if (get_directive_token (pfile) != CPP_OTHER
1829           || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1830           || CPP_PWRITTEN (pfile)[-1] != '!')
1831         goto restore;
1832
1833       /* ...then `defined', */
1834       token_offset = CPP_WRITTEN (pfile);
1835       token = get_directive_token (pfile);
1836       if (token != CPP_NAME)
1837         goto restore;
1838       ident = pfile->token_buffer + token_offset;
1839       CPP_NUL_TERMINATE (pfile);
1840       if (strcmp (ident, "defined"))
1841         goto restore;
1842
1843       /* ...then an optional '(' and the name, */
1844       token_offset = CPP_WRITTEN (pfile);
1845       token = get_directive_token (pfile);
1846       if (token == CPP_LPAREN)
1847         {
1848           token_offset = CPP_WRITTEN (pfile);
1849           token = get_directive_token (pfile);
1850           if (token != CPP_NAME)
1851             goto restore;
1852           need_rparen = 1;
1853         }
1854       else if (token != CPP_NAME)
1855         goto restore;
1856
1857       ident = pfile->token_buffer + token_offset;
1858       CPP_NUL_TERMINATE (pfile);
1859
1860       /* ...then the ')', if necessary, */
1861       if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1862           /* ...and make sure there's nothing else on the line.  */
1863           && get_directive_token (pfile) == CPP_VSPACE)
1864         control_macro = xstrdup (ident);
1865
1866     restore:
1867       CPP_SET_WRITTEN (pfile, base_offset);
1868       pfile->no_macro_expand--;
1869       parse_goto_mark (pfile);
1870     }
1871
1872   return control_macro;
1873 }
1874
1875 /*
1876  * handle #if command by
1877  *   1) inserting special `defined' keyword into the hash table
1878  *      that gets turned into 0 or 1 by special_symbol (thus,
1879  *      if the luser has a symbol called `defined' already, it won't
1880  *      work inside the #if command)
1881  *   2) rescan the input into a temporary output buffer
1882  *   3) pass the output buffer to the yacc parser and collect a value
1883  *   4) clean up the mess left from steps 1 and 2.
1884  *   5) call conditional_skip to skip til the next #endif (etc.),
1885  *      or not, depending on the value from step 3.
1886  */
1887
1888 static int
1889 do_if (pfile, keyword)
1890      cpp_reader *pfile;
1891      const struct directive *keyword ATTRIBUTE_UNUSED;
1892 {
1893   U_CHAR *control_macro = detect_if_not_defined (pfile);
1894   HOST_WIDEST_INT value = eval_if_expression (pfile);
1895   conditional_skip (pfile, value == 0, T_IF, control_macro);
1896   return 0;
1897 }
1898
1899 /*
1900  * handle a #elif directive by not changing  if_stack  either.
1901  * see the comment above do_else.
1902  */
1903
1904 static int
1905 do_elif (pfile, keyword)
1906      cpp_reader *pfile;
1907      const struct directive *keyword ATTRIBUTE_UNUSED;
1908 {
1909   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1910     {
1911       cpp_error (pfile, "`#elif' not within a conditional");
1912       return 0;
1913     }
1914   else
1915     {
1916       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1917         {
1918           cpp_error (pfile, "`#elif' after `#else'");
1919           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1920                                "the conditional began here");
1921         }
1922       pfile->if_stack->type = T_ELIF;
1923     }
1924
1925   if (pfile->if_stack->if_succeeded)
1926     skip_if_group (pfile);
1927   else
1928     {
1929       HOST_WIDEST_INT value = eval_if_expression (pfile);
1930       if (value == 0)
1931         skip_if_group (pfile);
1932       else
1933         {
1934           ++pfile->if_stack->if_succeeded;      /* continue processing input */
1935           output_line_command (pfile, same_file);
1936         }
1937     }
1938   return 0;
1939 }
1940
1941 /*
1942  * evaluate a #if expression in BUF, of length LENGTH,
1943  * then parse the result as a C expression and return the value as an int.
1944  */
1945
1946 static HOST_WIDEST_INT
1947 eval_if_expression (pfile)
1948      cpp_reader *pfile;
1949 {
1950   HOST_WIDEST_INT value;
1951   long old_written = CPP_WRITTEN (pfile);
1952
1953   /* Work around bug in cpp_get_token where it may mistake an
1954      assertion for a directive.  */
1955   pfile->only_seen_white = 0;
1956
1957   value = cpp_parse_expr (pfile);
1958
1959   skip_rest_of_line (pfile);
1960   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1961
1962   return value;
1963 }
1964
1965 /*
1966  * routine to handle ifdef/ifndef.  Try to look up the symbol,
1967  * then do or don't skip to the #endif/#else/#elif depending
1968  * on what directive is actually being processed.
1969  */
1970
1971 static int
1972 do_xifdef (pfile, keyword)
1973      cpp_reader *pfile;
1974      const struct directive *keyword;
1975 {
1976   int skip;
1977   cpp_buffer *ip = CPP_BUFFER (pfile);
1978   U_CHAR *ident;
1979   int ident_length;
1980   enum cpp_token token;
1981   int start_of_file = 0;
1982   U_CHAR *control_macro = 0;
1983   int old_written = CPP_WRITTEN (pfile);
1984
1985   /* Detect a #ifndef at start of file (not counting comments).  */
1986   if (ip->fname != 0 && keyword->type == T_IFNDEF)
1987     start_of_file = pfile->only_seen_white == 2;
1988
1989   pfile->no_macro_expand++;
1990   token = get_directive_token (pfile);
1991   pfile->no_macro_expand--;
1992
1993   ident = pfile->token_buffer + old_written;
1994   ident_length = CPP_WRITTEN (pfile) - old_written;
1995   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1996
1997   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1998     {
1999       skip = (keyword->type == T_IFDEF);
2000       if (! CPP_TRADITIONAL (pfile))
2001         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2002     }
2003   else if (token == CPP_NAME)
2004     {
2005       skip = cpp_defined (pfile, ident, ident_length);
2006       if (keyword->type == T_IFDEF)
2007         skip = !skip;
2008
2009       if (start_of_file && !skip)
2010         {
2011           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2012           bcopy (ident, control_macro, ident_length + 1);
2013         }
2014     }
2015   else
2016     {
2017       skip = (keyword->type == T_IFDEF);
2018       if (! CPP_TRADITIONAL (pfile))
2019         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2020     }
2021
2022   if (!CPP_TRADITIONAL (pfile))
2023     { int c;
2024       cpp_skip_hspace (pfile);
2025       c = PEEKC ();
2026       if (c != EOF && c != '\n')
2027         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2028     }
2029   skip_rest_of_line (pfile);
2030
2031   conditional_skip (pfile, skip, T_IF, control_macro);
2032   return 0;
2033 }
2034
2035 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2036    If this is a #ifndef starting at the beginning of a file,
2037    CONTROL_MACRO is the macro name tested by the #ifndef.
2038    Otherwise, CONTROL_MACRO is 0.  */
2039
2040 static void
2041 conditional_skip (pfile, skip, type, control_macro)
2042      cpp_reader *pfile;
2043      int skip;
2044      enum node_type type;
2045      U_CHAR *control_macro;
2046 {
2047   IF_STACK_FRAME *temp;
2048
2049   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2050   temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2051   temp->lineno = CPP_BUFFER (pfile)->lineno;
2052   temp->next = pfile->if_stack;
2053   temp->control_macro = control_macro;
2054   pfile->if_stack = temp;
2055
2056   pfile->if_stack->type = type;
2057
2058   if (skip != 0) {
2059     skip_if_group (pfile);
2060     return;
2061   } else {
2062     ++pfile->if_stack->if_succeeded;
2063     output_line_command (pfile, same_file);
2064   }
2065 }
2066
2067 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
2068    return 0 if skipping should continue, 1 if it should halt.  Also
2069    adjusts the if_stack as appropriate.
2070    The `#' has been read, but not the identifier. */
2071
2072 static int
2073 consider_directive_while_skipping (pfile, stack)
2074     cpp_reader *pfile;
2075     IF_STACK_FRAME *stack; 
2076 {
2077   long ident_len, ident;
2078   const struct directive *kt;
2079   IF_STACK_FRAME *temp;
2080     
2081   cpp_skip_hspace (pfile);
2082
2083   ident = CPP_WRITTEN (pfile);
2084   parse_name (pfile, GETC());
2085   ident_len = CPP_WRITTEN (pfile) - ident;
2086
2087   CPP_SET_WRITTEN (pfile, ident);
2088
2089   for (kt = directive_table; kt->length >= 0; kt++)
2090     if (kt->length == ident_len
2091         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2092       switch (kt->type)
2093         {
2094         case T_IF:
2095         case T_IFDEF:
2096         case T_IFNDEF:
2097             temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2098             temp->next = pfile->if_stack;
2099             pfile->if_stack = temp;
2100             temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2101             temp->type = kt->type;
2102             return 0;
2103
2104         case T_ELSE:
2105             if (pfile->if_stack != stack)
2106               validate_else (pfile, "#else");
2107             /* fall through */
2108         case T_ELIF:
2109             if (pfile->if_stack == stack)
2110               return 1;
2111             else
2112               {
2113                 pfile->if_stack->type = kt->type;
2114                 return 0;
2115               }
2116
2117             case T_ENDIF:
2118                 if (pfile->if_stack != stack)
2119                   validate_else (pfile, "#endif");
2120
2121                 if (pfile->if_stack == stack)
2122                   return 1;
2123                     
2124                 temp = pfile->if_stack;
2125                 pfile->if_stack = temp->next;
2126                 free (temp);
2127                 return 0;
2128
2129             default:
2130                 return 0;
2131             }
2132
2133     /* Don't let erroneous code go by.  */
2134     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2135         cpp_pedwarn (pfile, "invalid preprocessor directive name");
2136     return 0;
2137 }
2138
2139 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
2140  * leaves input ptr at the sharp sign found.
2141  */
2142 static void
2143 skip_if_group (pfile)
2144     cpp_reader *pfile;
2145 {
2146   int c;
2147   IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2148   U_CHAR *beg_of_line;
2149   long old_written;
2150
2151   if (CPP_OPTIONS (pfile)->output_conditionals)
2152     {
2153       CPP_PUTS (pfile, "#failed\n", 8);
2154       pfile->lineno++;
2155       output_line_command (pfile, same_file);
2156     }
2157
2158   old_written = CPP_WRITTEN (pfile);
2159   
2160   for (;;)
2161     {
2162       beg_of_line = CPP_BUFFER (pfile)->cur;
2163
2164       if (! CPP_TRADITIONAL (pfile))
2165         cpp_skip_hspace (pfile);
2166       c = GETC();
2167       if (c == '\n')
2168         {
2169           if (CPP_OPTIONS (pfile)->output_conditionals)
2170             CPP_PUTC (pfile, c);
2171           CPP_BUMP_LINE (pfile);
2172           continue;
2173         }
2174       else if (c == '#')
2175         {
2176           if (consider_directive_while_skipping (pfile, save_if_stack))
2177             break;
2178         }
2179       else if (c == EOF)
2180         return;  /* Caller will issue error. */
2181
2182       FORWARD(-1);
2183       if (CPP_OPTIONS (pfile)->output_conditionals)
2184         {
2185           CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2186           copy_rest_of_line (pfile);
2187         }
2188       else
2189         {
2190           copy_rest_of_line (pfile);
2191           CPP_SET_WRITTEN (pfile, old_written);  /* discard it */
2192         }
2193
2194       c = GETC();
2195       if (c == EOF)
2196         return;  /* Caller will issue error. */
2197       else
2198         {
2199           /* \n */
2200           if (CPP_OPTIONS (pfile)->output_conditionals)
2201             {
2202               CPP_PUTC (pfile, c);
2203               pfile->lineno++;
2204             }
2205           CPP_BUMP_LINE (pfile);
2206         }
2207     }     
2208
2209   /* Back up to the beginning of this line.  Caller will process the
2210      directive. */
2211   CPP_BUFFER (pfile)->cur = beg_of_line;
2212   pfile->only_seen_white = 1;
2213   if (CPP_OPTIONS (pfile)->output_conditionals)
2214     {
2215       CPP_PUTS (pfile, "#endfailed\n", 11);
2216       pfile->lineno++;
2217     }
2218 }
2219
2220 /*
2221  * handle a #else directive.  Do this by just continuing processing
2222  * without changing  if_stack ;  this is so that the error message
2223  * for missing #endif's etc. will point to the original #if.  It
2224  * is possible that something different would be better.
2225  */
2226
2227 static int
2228 do_else (pfile, keyword)
2229      cpp_reader *pfile;
2230      const struct directive *keyword ATTRIBUTE_UNUSED;
2231 {
2232   validate_else (pfile, "#else");
2233   skip_rest_of_line (pfile);
2234
2235   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2236     {
2237       cpp_error (pfile, "`#else' not within a conditional");
2238       return 0;
2239     }
2240   else
2241     {
2242       /* #ifndef can't have its special treatment for containing the whole file
2243          if it has a #else clause.  */
2244       pfile->if_stack->control_macro = 0;
2245
2246       if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2247         {
2248           cpp_error (pfile, "`#else' after `#else'");
2249           cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2250                                "the conditional began here");
2251         }
2252       pfile->if_stack->type = T_ELSE;
2253     }
2254
2255   if (pfile->if_stack->if_succeeded)
2256     skip_if_group (pfile);
2257   else
2258     {
2259       ++pfile->if_stack->if_succeeded;  /* continue processing input */
2260       output_line_command (pfile, same_file);
2261     }
2262   return 0;
2263 }
2264
2265 /*
2266  * unstack after #endif command
2267  */
2268
2269 static int
2270 do_endif (pfile, keyword)
2271      cpp_reader *pfile;
2272      const struct directive *keyword ATTRIBUTE_UNUSED;
2273 {
2274   validate_else (pfile, "#endif");
2275   skip_rest_of_line (pfile);
2276
2277   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2278     cpp_error (pfile, "`#endif' not within a conditional");
2279   else
2280     {
2281       IF_STACK_FRAME *temp = pfile->if_stack;
2282       pfile->if_stack = temp->next;
2283       if (temp->control_macro != 0)
2284         {
2285           /* This #endif matched a #ifndef at the start of the file.
2286              See if it is at the end of the file.  */
2287           int c;
2288
2289           parse_set_mark (pfile);
2290
2291           for (;;)
2292             {
2293               cpp_skip_hspace (pfile);
2294               c = GETC ();
2295               if (c != '\n')
2296                 break;
2297             }
2298           parse_goto_mark (pfile);
2299
2300           if (c == EOF)
2301             {
2302               /* This #endif ends a #ifndef
2303                  that contains all of the file (aside from whitespace).
2304                  Arrange not to include the file again
2305                  if the macro that was tested is defined. */
2306               struct cpp_buffer *ip;
2307               for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2308                 if (ip->fname != NULL)
2309                   break;
2310               ip->ihash->control_macro = (char *) temp->control_macro;
2311             }
2312         }
2313       free (temp);
2314       output_line_command (pfile, same_file);
2315     }
2316   return 0;
2317 }
2318
2319 /* Issue -pedantic warning for text which is not a comment following
2320    an #else or #endif.  Do not warn in system headers, as this is harmless
2321    and very common on old systems.  */
2322
2323 static void
2324 validate_else (pfile, directive)
2325      cpp_reader *pfile;
2326      const char *directive;
2327 {
2328   if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2329     return;
2330
2331   cpp_skip_hspace (pfile);
2332   if (PEEKC () != '\n')
2333     cpp_pedwarn (pfile,
2334                  "text following `%s' violates ANSI standard", directive);
2335 }
2336
2337 /* Convert T_IF, etc. to a string.   Used in error messages.  */
2338 static const char *
2339 if_directive_name (pfile, ifs)
2340      cpp_reader *pfile;
2341      struct if_stack *ifs;
2342 {
2343   switch (ifs->type)
2344     {
2345     case T_IF:      return "#if";
2346     case T_IFDEF:   return "#ifdef";
2347     case T_IFNDEF:  return "#ifndef";
2348     case T_ELIF:    return "#elif";
2349     case T_ELSE:    return "#else";
2350     default:
2351       cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2352       return "unknown";
2353     }
2354 }
2355
2356 /* Get the next token, and add it to the text in pfile->token_buffer.
2357    Return the kind of token we got.  */
2358
2359 enum cpp_token
2360 cpp_get_token (pfile)
2361      cpp_reader *pfile;
2362 {
2363   register int c, c2, c3;
2364   enum cpp_token token;
2365   struct cpp_options *opts = CPP_OPTIONS (pfile);
2366
2367  get_next:
2368   c = GETC();
2369   if (c == EOF)
2370     {
2371       if (CPP_BUFFER (pfile)->manual_pop)
2372         /* If we've been reading from redirected input, the
2373            frontend will pop the buffer.  */
2374         return CPP_EOF;
2375       else if (CPP_BUFFER (pfile)->seen_eof)
2376         {
2377           if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2378             return CPP_EOF;
2379
2380           cpp_pop_buffer (pfile);
2381           goto get_next;
2382         }
2383       else
2384         {
2385           cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2386           struct if_stack *ifs, *nifs;
2387
2388           /* Unwind the conditional stack and generate error messages.  */
2389           for (ifs = pfile->if_stack;
2390                ifs != CPP_BUFFER (pfile)->if_stack;
2391                ifs = nifs)
2392             {
2393               cpp_error_with_line (pfile, ifs->lineno, -1,
2394                                    "unterminated `%s' conditional",
2395                                    if_directive_name (pfile, ifs));
2396
2397               nifs = ifs->next;
2398               free (ifs);
2399             }
2400           pfile->if_stack = ifs;
2401
2402           if (CPP_BUFFER (pfile)->nominal_fname
2403               && next_buf != CPP_NULL_BUFFER (pfile))
2404             {
2405               /* We're about to return from an #include file.
2406                  Emit #line information now (as part of the CPP_POP) result.
2407                  But the #line refers to the file we will pop to.  */
2408               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2409               CPP_BUFFER (pfile) = next_buf;
2410               pfile->input_stack_listing_current = 0;
2411               output_line_command (pfile, leave_file);
2412               CPP_BUFFER (pfile) = cur_buffer;
2413             }
2414
2415           CPP_BUFFER (pfile)->seen_eof = 1;
2416           return CPP_POP;
2417         }
2418     }
2419   else
2420     {
2421       switch (c)
2422         {
2423         case '/':
2424           if (PEEKC () == '=')
2425             goto op2;
2426
2427         comment:
2428           if (opts->discard_comments)
2429             c = skip_comment (pfile, c);
2430           else
2431             c = copy_comment (pfile, c);
2432           if (c != ' ')
2433             goto randomchar;
2434           
2435           /* Comments are equivalent to spaces.
2436              For -traditional, a comment is equivalent to nothing.  */
2437           if (opts->traditional || !opts->discard_comments)
2438             return CPP_COMMENT;
2439           else
2440             {
2441               CPP_PUTC (pfile, c);
2442               return CPP_HSPACE;
2443             }
2444
2445         case '#':
2446           if (!pfile->only_seen_white)
2447             goto randomchar;
2448           /* -traditional directives are recognized only with the # in
2449              column 1.
2450              XXX Layering violation.  */
2451           if (CPP_TRADITIONAL (pfile)
2452               && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2453             goto randomchar;
2454           if (handle_directive (pfile))
2455             return CPP_DIRECTIVE;
2456           pfile->only_seen_white = 0;
2457           goto randomchar;
2458
2459         case '\"':
2460         case '\'':
2461           parse_string (pfile, c);
2462           pfile->only_seen_white = 0;
2463           return c == '\'' ? CPP_CHAR : CPP_STRING;
2464
2465         case '$':
2466           if (!opts->dollars_in_ident)
2467             goto randomchar;
2468           goto letter;
2469
2470         case ':':
2471           if (opts->cplusplus && PEEKC () == ':')
2472             goto op2;
2473           goto randomchar;
2474
2475         case '&':
2476         case '+':
2477         case '|':
2478           c2 = PEEKC ();
2479           if (c2 == c || c2 == '=')
2480             goto op2;
2481           goto randomchar;
2482
2483         case '*':
2484         case '!':
2485         case '%':
2486         case '=':
2487         case '^':
2488           if (PEEKC () == '=')
2489             goto op2;
2490           goto randomchar;
2491
2492         case '-':
2493           c2 = PEEKC ();
2494           if (c2 == '-' && opts->chill)
2495             goto comment;  /* Chill style comment */
2496           if (c2 == '-' || c2 == '=')
2497             goto op2;
2498           if (c2 == '>')
2499             {
2500               if (opts->cplusplus && PEEKN (1) == '*')
2501                 {
2502                   /* In C++, there's a ->* operator.  */
2503                   token = CPP_OTHER;
2504                   pfile->only_seen_white = 0;
2505                   CPP_RESERVE (pfile, 4);
2506                   CPP_PUTC_Q (pfile, c);
2507                   CPP_PUTC_Q (pfile, GETC ());
2508                   CPP_PUTC_Q (pfile, GETC ());
2509                   CPP_NUL_TERMINATE_Q (pfile);
2510                   return token;
2511                 }
2512               goto op2;
2513             }
2514           goto randomchar;
2515
2516         case '<':
2517           if (pfile->parsing_include_directive)
2518             {
2519               for (;;)
2520                 {
2521                   CPP_PUTC (pfile, c);
2522                   if (c == '>')
2523                     break;
2524                   c = GETC ();
2525                   if (c == '\n' || c == EOF)
2526                     {
2527                       cpp_error (pfile,
2528                                  "missing '>' in `#include <FILENAME>'");
2529                       break;
2530                     }
2531                   else if (c == '\r')
2532                     {
2533                       if (!CPP_BUFFER (pfile)->has_escapes)
2534                         {
2535                           /* Backslash newline is replaced by nothing. */
2536                           CPP_ADJUST_WRITTEN (pfile, -1);
2537                           CPP_BUMP_LINE (pfile);
2538                         }
2539                       else
2540                         {
2541                           /* We might conceivably get \r- or \r<space> in
2542                              here.  Just delete 'em. */
2543                           int d = GETC();
2544                           if (d != '-' && d != ' ')
2545                             cpp_ice (pfile, "unrecognized escape \\r%c", d);
2546                           CPP_ADJUST_WRITTEN (pfile, -1);
2547                         }                         
2548                     }
2549                 }
2550               return CPP_STRING;
2551             }
2552           /* else fall through */
2553         case '>':
2554           c2 = PEEKC ();
2555           if (c2 == '=')
2556             goto op2;
2557           /* GNU C++ supports MIN and MAX operators <? and >?.  */
2558           if (c2 != c && (!opts->cplusplus || c2 != '?'))
2559             goto randomchar;
2560           FORWARD(1);
2561           CPP_RESERVE (pfile, 4);
2562           CPP_PUTC (pfile, c);
2563           CPP_PUTC (pfile, c2);
2564           c3 = PEEKC ();
2565           if (c3 == '=')
2566             CPP_PUTC_Q (pfile, GETC ());
2567           CPP_NUL_TERMINATE_Q (pfile);
2568           pfile->only_seen_white = 0;
2569           return CPP_OTHER;
2570
2571         case '.':
2572           c2 = PEEKC ();
2573           if (ISDIGIT(c2))
2574             {
2575               CPP_RESERVE(pfile, 2);
2576               CPP_PUTC_Q (pfile, '.');
2577               c = GETC ();
2578               goto number;
2579             }
2580
2581           /* In C++ there's a .* operator.  */
2582           if (opts->cplusplus && c2 == '*')
2583             goto op2;
2584
2585           if (c2 == '.' && PEEKN(1) == '.')
2586             {
2587               CPP_RESERVE(pfile, 4);
2588               CPP_PUTC_Q (pfile, '.');
2589               CPP_PUTC_Q (pfile, '.');
2590               CPP_PUTC_Q (pfile, '.');
2591               FORWARD (2);
2592               CPP_NUL_TERMINATE_Q (pfile);
2593               pfile->only_seen_white = 0;
2594               return CPP_3DOTS;
2595             }
2596           goto randomchar;
2597
2598         op2:
2599           token = CPP_OTHER;
2600           pfile->only_seen_white = 0;
2601           CPP_RESERVE(pfile, 3);
2602           CPP_PUTC_Q (pfile, c);
2603           CPP_PUTC_Q (pfile, GETC ());
2604           CPP_NUL_TERMINATE_Q (pfile);
2605           return token;
2606
2607         case 'L':
2608           c2 = PEEKC ();
2609           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2610             {
2611               CPP_PUTC (pfile, c);
2612               c = GETC ();
2613               parse_string (pfile, c);
2614               pfile->only_seen_white = 0;
2615               return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2616             }
2617           goto letter;
2618
2619         case '0': case '1': case '2': case '3': case '4':
2620         case '5': case '6': case '7': case '8': case '9':
2621         number:
2622           c2  = '.';
2623           for (;;)
2624             {
2625               CPP_RESERVE (pfile, 2);
2626               CPP_PUTC_Q (pfile, c);
2627               c = PEEKC ();
2628               if (c == EOF)
2629                 break;
2630               if (!is_idchar(c) && c != '.'
2631                   && ((c2 != 'e' && c2 != 'E'
2632                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2633                       || (c != '+' && c != '-')))
2634                 break;
2635               FORWARD(1);
2636               c2= c;
2637             }
2638           CPP_NUL_TERMINATE_Q (pfile);
2639           pfile->only_seen_white = 0;
2640           return CPP_NUMBER;
2641         case 'b': case 'c': case 'd': case 'h': case 'o':
2642         case 'B': case 'C': case 'D': case 'H': case 'O':
2643           if (opts->chill && PEEKC () == '\'')
2644             {
2645               pfile->only_seen_white = 0;
2646               CPP_RESERVE (pfile, 2);
2647               CPP_PUTC_Q (pfile, c);
2648               CPP_PUTC_Q (pfile, '\'');
2649               FORWARD(1);
2650               for (;;)
2651                 {
2652                   c = GETC();
2653                   if (c == EOF)
2654                     goto chill_number_eof;
2655                   if (!is_idchar(c))
2656                     break;
2657                   CPP_PUTC (pfile, c);
2658                 }
2659               if (c == '\'')
2660                 {
2661                   CPP_RESERVE (pfile, 2);
2662                   CPP_PUTC_Q (pfile, c);
2663                   CPP_NUL_TERMINATE_Q (pfile);
2664                   return CPP_STRING;
2665                 }
2666               else
2667                 {
2668                   FORWARD(-1);
2669                 chill_number_eof:
2670                   CPP_NUL_TERMINATE (pfile);
2671                   return CPP_NUMBER;
2672                 }
2673             }
2674           else
2675             goto letter;
2676         case '_':
2677         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2678         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2679         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2680         case 'x': case 'y': case 'z':
2681         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2682         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2683         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2684         case 'Y': case 'Z':
2685         letter:
2686           {
2687             HASHNODE *hp;
2688             unsigned char *ident;
2689             int before_name_written = CPP_WRITTEN (pfile);
2690             int ident_len;
2691             parse_name (pfile, c);
2692             pfile->only_seen_white = 0;
2693             if (pfile->no_macro_expand)
2694               return CPP_NAME;
2695             ident = pfile->token_buffer + before_name_written;
2696             ident_len = CPP_PWRITTEN (pfile) - ident;
2697             hp = cpp_lookup (pfile, ident, ident_len);
2698             if (!hp)
2699               return CPP_NAME;
2700             if (hp->type == T_DISABLED)
2701               {
2702                 if (pfile->output_escapes)
2703                   { /* Return "\r-IDENT", followed by '\0'.  */
2704                     int i;
2705                     CPP_RESERVE (pfile, 3);
2706                     ident = pfile->token_buffer + before_name_written;
2707                     CPP_ADJUST_WRITTEN (pfile, 2);
2708                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2709                     ident[0] = '\r';
2710                     ident[1] = '-';
2711                   }
2712                 return CPP_NAME;
2713               }
2714
2715             /* If macro wants an arglist, verify that a '(' follows.  */
2716             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2717             {
2718               int macbuf_whitespace = 0;
2719
2720               while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2721                 {
2722                   U_CHAR *point = CPP_BUFFER (pfile)->cur;
2723                   for (;;)
2724                     {
2725                       cpp_skip_hspace (pfile);
2726                       c = PEEKC ();
2727                       if (c == '\n')
2728                         FORWARD(1);
2729                       else
2730                         break;
2731                     }
2732                   if (point != CPP_BUFFER (pfile)->cur)
2733                     macbuf_whitespace = 1;
2734                   if (c == '(')
2735                     goto is_macro_call;
2736                   else if (c != EOF)
2737                     goto not_macro_call;
2738                   cpp_pop_buffer (pfile);
2739                 }
2740
2741               parse_set_mark (pfile);
2742               for (;;)
2743                 {
2744                   cpp_skip_hspace (pfile);
2745                   c = PEEKC ();
2746                   if (c == '\n')
2747                     FORWARD(1);
2748                   else
2749                     break;
2750                 }
2751               parse_goto_mark (pfile);
2752
2753               if (c == '(')
2754                 goto is_macro_call;
2755
2756             not_macro_call:
2757               if (macbuf_whitespace)
2758                 CPP_PUTC (pfile, ' ');
2759               return CPP_NAME;
2760             }
2761           is_macro_call:
2762             /* This is now known to be a macro call.
2763                Expand the macro, reading arguments as needed,
2764                and push the expansion on the input stack.  */
2765             macroexpand (pfile, hp);
2766             CPP_SET_WRITTEN (pfile, before_name_written);
2767           }
2768           goto get_next;
2769
2770         case ' ':  case '\t':  case '\v':
2771           for (;;)
2772             {
2773               CPP_PUTC (pfile, c);
2774               c = PEEKC ();
2775               if (c == EOF || !is_hspace(c))
2776                 break;
2777               FORWARD(1);
2778             }
2779           return CPP_HSPACE;
2780
2781         case '\r':
2782           if (CPP_BUFFER (pfile)->has_escapes)
2783             {
2784               c = GETC ();
2785               if (c == '-')
2786                 {
2787                   if (pfile->output_escapes)
2788                     CPP_PUTS (pfile, "\r-", 2);
2789                   parse_name (pfile, GETC ());
2790                   return CPP_NAME;
2791                 }
2792               else if (c == ' ')
2793                 {
2794                   CPP_RESERVE (pfile, 2);
2795                   if (pfile->output_escapes)
2796                     CPP_PUTC_Q (pfile, '\r');
2797                   CPP_PUTC_Q (pfile, c);
2798                   return CPP_HSPACE;
2799                 }
2800               else
2801                 {
2802                   cpp_ice (pfile, "unrecognized escape \\r%c", c);
2803                   goto get_next;
2804                 }
2805             }
2806           else
2807             {
2808               /* Backslash newline is ignored. */
2809               CPP_BUMP_LINE (pfile);
2810               goto get_next;
2811             }
2812
2813         case '\n':
2814           CPP_PUTC (pfile, c);
2815           if (pfile->only_seen_white == 0)
2816             pfile->only_seen_white = 1;
2817           CPP_BUMP_LINE (pfile);
2818           if (! CPP_OPTIONS (pfile)->no_line_commands)
2819             {
2820               pfile->lineno++;
2821               if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2822                 output_line_command (pfile, same_file);
2823             }
2824           return CPP_VSPACE;
2825
2826         case '(': token = CPP_LPAREN;    goto char1;
2827         case ')': token = CPP_RPAREN;    goto char1;
2828         case '{': token = CPP_LBRACE;    goto char1;
2829         case '}': token = CPP_RBRACE;    goto char1;
2830         case ',': token = CPP_COMMA;     goto char1;
2831         case ';': token = CPP_SEMICOLON; goto char1;
2832
2833         randomchar:
2834         default:
2835           token = CPP_OTHER;
2836         char1:
2837           pfile->only_seen_white = 0;
2838           CPP_PUTC (pfile, c);
2839           return token;
2840         }
2841     }
2842 }
2843
2844 /* Like cpp_get_token, but skip spaces and comments.  */
2845
2846 enum cpp_token
2847 cpp_get_non_space_token (pfile)
2848      cpp_reader *pfile;
2849 {
2850   int old_written = CPP_WRITTEN (pfile);
2851   for (;;)
2852     {
2853       enum cpp_token token = cpp_get_token (pfile);
2854       if (token != CPP_COMMENT && token != CPP_POP
2855           && token != CPP_HSPACE && token != CPP_VSPACE)
2856         return token;
2857       CPP_SET_WRITTEN (pfile, old_written);
2858     }
2859 }
2860
2861 /* Parse an identifier starting with C.  */
2862
2863 static void
2864 parse_name (pfile, c)
2865      cpp_reader *pfile;
2866      int c;
2867 {
2868   for (;;)
2869   {
2870       if (! is_idchar(c))
2871       {
2872           FORWARD (-1);
2873           break;
2874       }
2875
2876       if (c == '$' && CPP_PEDANTIC (pfile))
2877         cpp_pedwarn (pfile, "`$' in identifier");
2878
2879       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
2880       CPP_PUTC_Q (pfile, c);
2881       c = GETC();
2882       if (c == EOF)
2883         break;
2884   }
2885   CPP_NUL_TERMINATE_Q (pfile);
2886   return;
2887 }
2888
2889 /* Parse a string starting with C.  A single quoted string is treated
2890    like a double -- some programs (e.g., troff) are perverse this way.
2891    (However, a single quoted string is not allowed to extend over
2892    multiple lines.)  */
2893 static void
2894 parse_string (pfile, c)
2895      cpp_reader *pfile;
2896      int c;
2897 {
2898   long start_line, start_column;
2899   
2900   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2901
2902   CPP_PUTC (pfile, c);
2903   while (1)
2904     {
2905       int cc = GETC();
2906       if (cc == EOF)
2907         {
2908           if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2909             {
2910               /* try harder: this string crosses a macro expansion
2911                  boundary.  This can happen naturally if -traditional.
2912                  Otherwise, only -D can make a macro with an unmatched
2913                  quote.  */
2914               cpp_pop_buffer (pfile);
2915               continue;
2916             }
2917
2918           cpp_error_with_line (pfile, start_line, start_column,
2919                                "unterminated string or character constant");
2920           if (pfile->multiline_string_line != start_line
2921               && pfile->multiline_string_line != 0)
2922             cpp_error_with_line (pfile,
2923                                  pfile->multiline_string_line, -1,
2924                          "possible real start of unterminated constant");
2925           pfile->multiline_string_line = 0;
2926           break;
2927         }
2928       CPP_PUTC (pfile, cc);
2929       switch (cc)
2930         {
2931         case '\n':
2932           CPP_BUMP_LINE (pfile);
2933           pfile->lineno++;
2934
2935           /* In Fortran and assembly language, silently terminate
2936              strings of either variety at end of line.  This is a
2937              kludge around not knowing where comments are in these
2938              languages.  */
2939           if (CPP_OPTIONS (pfile)->lang_fortran
2940               || CPP_OPTIONS (pfile)->lang_asm)
2941             return;
2942           /* Character constants may not extend over multiple lines.
2943              In Standard C, neither may strings.  We accept multiline
2944              strings as an extension.  */
2945           if (c == '\'')
2946             {
2947               cpp_error_with_line (pfile, start_line, start_column,
2948                                    "unterminated character constant");
2949               return;
2950             }
2951           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2952             cpp_pedwarn_with_line (pfile, start_line, start_column,
2953                                    "string constant runs past end of line");
2954           if (pfile->multiline_string_line == 0)
2955             pfile->multiline_string_line = start_line;
2956           break;
2957
2958         case '\r':
2959           CPP_ADJUST_WRITTEN (pfile, -1);
2960           if (CPP_BUFFER (pfile)->has_escapes)
2961             {
2962               cpp_ice (pfile, "\\r escape inside string constant");
2963               FORWARD(1);
2964             }
2965           else
2966             /* Backslash newline is replaced by nothing at all.  */
2967             CPP_BUMP_LINE (pfile);
2968           break;
2969
2970         case '\\':
2971           cc = GETC();
2972           if (cc != EOF)
2973             CPP_PUTC (pfile, cc);
2974           break;
2975
2976         case '\"':
2977         case '\'':
2978           if (cc == c)
2979             return;
2980           break;
2981         }
2982     }
2983 }
2984
2985 /* Read an assertion into the token buffer, converting to
2986    canonical form: `#predicate(a n swe r)'  The next non-whitespace
2987    character to read should be the first letter of the predicate.
2988    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2989    with answer (see callers for why). In case of 0, an error has been
2990    printed. */
2991 static int
2992 parse_assertion (pfile)
2993      cpp_reader *pfile;
2994 {
2995   int c, dropwhite;
2996   cpp_skip_hspace (pfile);
2997   c = PEEKC();
2998   if (! is_idstart(c))
2999     {
3000       cpp_error (pfile, "assertion predicate is not an identifier");
3001       return 0;
3002     }
3003   CPP_PUTC(pfile, '#');
3004   FORWARD(1);
3005   parse_name(pfile, c);
3006
3007   c = PEEKC();
3008   if (c != '(')
3009     {
3010       if (is_hspace(c) || c == '\r')
3011         cpp_skip_hspace (pfile);
3012       c = PEEKC();
3013     }
3014   if (c != '(')
3015     return 1;
3016
3017   CPP_PUTC(pfile, '(');
3018   FORWARD(1);
3019   dropwhite = 1;
3020   while ((c = GETC()) != ')')
3021     {
3022       if (is_space(c))
3023         {
3024           if (! dropwhite)
3025             {
3026               CPP_PUTC(pfile, ' ');
3027               dropwhite = 1;
3028             }
3029         }
3030       else if (c == '\n' || c == EOF)
3031         {
3032           if (c == '\n') FORWARD(-1);
3033           cpp_error (pfile, "un-terminated assertion answer");
3034           return 0;
3035         }
3036       else if (c == '\r')
3037         /* \r cannot be a macro escape here. */
3038         CPP_BUMP_LINE (pfile);
3039       else
3040         {
3041           CPP_PUTC (pfile, c);
3042           dropwhite = 0;
3043         }
3044     }
3045
3046   if (pfile->limit[-1] == ' ')
3047     pfile->limit[-1] = ')';
3048   else if (pfile->limit[-1] == '(')
3049     {
3050       cpp_error (pfile, "empty token sequence in assertion");
3051       return 0;
3052     }
3053   else
3054     CPP_PUTC (pfile, ')');
3055
3056   CPP_NUL_TERMINATE (pfile);
3057   return 2;
3058 }
3059
3060 static int
3061 do_assert (pfile, keyword)
3062      cpp_reader *pfile;
3063      const struct directive *keyword ATTRIBUTE_UNUSED;
3064 {
3065   char *sym;
3066   int ret, c;
3067   HASHNODE *base, *this;
3068   int baselen, thislen;
3069
3070   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3071       && !CPP_BUFFER (pfile)->system_header_p)
3072     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3073
3074   cpp_skip_hspace (pfile);
3075   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3076   ret = parse_assertion (pfile);
3077   if (ret == 0)
3078     goto error;
3079   else if (ret == 1)
3080     {
3081       cpp_error (pfile, "missing token-sequence in `#assert'");
3082       goto error;
3083     }
3084
3085   cpp_skip_hspace (pfile);
3086   c = PEEKC();
3087   if (c != EOF && c != '\n')
3088     {
3089       cpp_error (pfile, "junk at end of `#assert'");
3090       goto error;
3091     }
3092
3093   thislen = strlen (sym);
3094   baselen = index (sym, '(') - sym;
3095   this = cpp_lookup (pfile, sym, thislen);
3096   if (this)
3097     {
3098       cpp_warning (pfile, "`%s' re-asserted", sym);
3099       goto error;
3100     }
3101
3102   base = cpp_lookup (pfile, sym, baselen);
3103   if (! base)
3104     base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3105   else if (base->type != T_ASSERT)
3106   {
3107     /* Token clash - but with what?! */
3108     cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3109     goto error;
3110   }
3111
3112   this = cpp_install (pfile, sym, thislen, T_ASSERT,
3113                       (char *)base->value.aschain);
3114   base->value.aschain = this;
3115   
3116   pfile->limit = (unsigned char *) sym; /* Pop */
3117   return 0;
3118
3119  error:
3120   skip_rest_of_line (pfile);
3121   pfile->limit = (unsigned char *) sym; /* Pop */
3122   return 0;
3123 }
3124
3125 static int
3126 do_unassert (pfile, keyword)
3127      cpp_reader *pfile;
3128      const struct directive *keyword ATTRIBUTE_UNUSED;
3129 {
3130   int c, ret;
3131   char *sym;
3132   long baselen, thislen;
3133   HASHNODE *base, *this, *next;
3134   
3135   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3136       && !CPP_BUFFER (pfile)->system_header_p)
3137     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3138
3139   cpp_skip_hspace (pfile);
3140
3141   sym = (char *) CPP_PWRITTEN (pfile);  /* remember where it starts */
3142   ret = parse_assertion (pfile);
3143   if (ret == 0)
3144     goto error;
3145   
3146   cpp_skip_hspace (pfile);
3147   c = PEEKC ();
3148   if (c != EOF && c != '\n')
3149       cpp_error (pfile, "junk at end of `#unassert'");
3150
3151   thislen = strlen (sym);
3152   if (ret == 1)
3153     {
3154       base = cpp_lookup (pfile, sym, thislen);
3155       if (! base)
3156         goto error;  /* It isn't an error to #undef what isn't #defined,
3157                         so it isn't an error to #unassert what isn't
3158                         #asserted either. */
3159       
3160       for (this = base->value.aschain; this; this = next)
3161         {
3162           next = this->value.aschain;
3163           delete_macro (this);
3164         }
3165       delete_macro (base);
3166     }
3167   else
3168     {
3169       baselen = index (sym, '(') - sym;
3170       base = cpp_lookup (pfile, sym, baselen);
3171       if (! base) goto error;
3172       this = cpp_lookup (pfile, sym, thislen);
3173       if (! this) goto error;
3174
3175       next = base;
3176       while (next->value.aschain != this)
3177         next = next->value.aschain;
3178
3179       next->value.aschain = this->value.aschain;
3180       delete_macro (this);
3181
3182       if (base->value.aschain == NULL)
3183         delete_macro (base);  /* Last answer for this predicate deleted. */
3184     }
3185   
3186   pfile->limit = (unsigned char *) sym; /* Pop */
3187   return 0;
3188  error:
3189   skip_rest_of_line (pfile);
3190   pfile->limit = (unsigned char *) sym; /* Pop */
3191   return 0;
3192 }
3193
3194 /* Process STR as if it appeared as the body of an #unassert. */
3195 void
3196 cpp_unassert (pfile, str)
3197      cpp_reader *pfile;
3198      unsigned char *str;
3199 {
3200   if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3201     {
3202       do_assert (pfile, NULL);
3203       cpp_pop_buffer (pfile);
3204     }
3205 }  
3206
3207 int
3208 cpp_read_check_assertion (pfile)
3209      cpp_reader *pfile;
3210 {
3211   U_CHAR *name;
3212   int result;
3213   long written = CPP_WRITTEN (pfile);
3214   
3215   FORWARD (1);  /* Skip '#' */
3216   cpp_skip_hspace (pfile);
3217   if (! parse_assertion (pfile))
3218     result = 0;
3219   else
3220     {
3221       name = pfile->token_buffer + written;
3222       result = cpp_defined (pfile, name, CPP_PWRITTEN (pfile) - name);
3223     }
3224
3225   CPP_SET_WRITTEN (pfile, written);
3226   return result;
3227 }
3228
3229 /* Remember the current position of PFILE so it may be returned to
3230    after looking ahead a bit.
3231
3232    Note that when you set a mark, you _must_ return to that mark.  You
3233    may not forget about it and continue parsing.  You may not pop a
3234    buffer with an active mark.  You may not call CPP_BUMP_LINE while a
3235    mark is active.  */
3236
3237 static void
3238 parse_set_mark (pfile)
3239      cpp_reader *pfile;
3240 {
3241   cpp_buffer *ip = CPP_BUFFER (pfile);
3242   if (ACTIVE_MARK_P())
3243       cpp_ice (pfile, "mark active in parse_set_mark");
3244
3245   ip->mark = ip->cur - ip->buf;
3246 }
3247
3248 /* Backup the current position of PFILE to that saved in its mark,
3249    and clear the mark.  */
3250
3251 static void
3252 parse_goto_mark (pfile)
3253      cpp_reader *pfile;
3254 {
3255   cpp_buffer *ip = CPP_BUFFER (pfile);
3256   if (!ACTIVE_MARK_P())
3257       cpp_ice (pfile, "mark not active in parse_goto_mark");
3258
3259   ip->cur = ip->buf + ip->mark;
3260   ip->mark = -1;
3261 }