OSDN Git Service

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