OSDN Git Service

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