OSDN Git Service

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