OSDN Git Service

* cpplex.c (_cpp_read_and_prescan): Enlarge len by setting it
[pf3gnuchains/gcc-fork.git] / gcc / cpplex.c
1 /* CPP Library - lexical analysis.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Contributed by Per Bothner, 1994-95.
4    Based on CCCP program by Paul Rubin, June 1986
5    Adapted to ANSI C, Richard Stallman, Jan 1987
6    Broken out to separate file, Zack Weinberg, Mar 2000
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "intl.h"
25 #include "cpplib.h"
26 #include "cpphash.h"
27
28 #define PEEKBUF(BUFFER, N) \
29   ((BUFFER)->rlimit - (BUFFER)->cur > (N) ? (BUFFER)->cur[N] : EOF)
30 #define GETBUF(BUFFER) \
31   ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
32 #define FORWARDBUF(BUFFER, N) ((BUFFER)->cur += (N))
33
34 #define PEEKN(N) PEEKBUF (CPP_BUFFER (pfile), N)
35 #define FORWARD(N) FORWARDBUF (CPP_BUFFER (pfile), (N))
36 #define GETC() GETBUF (CPP_BUFFER (pfile))
37 #define PEEKC() PEEKBUF (CPP_BUFFER (pfile), 0)
38
39 static void skip_block_comment  PARAMS ((cpp_reader *));
40 static void skip_line_comment   PARAMS ((cpp_reader *));
41 static int maybe_macroexpand    PARAMS ((cpp_reader *, long));
42 static int skip_comment         PARAMS ((cpp_reader *, int));
43 static int copy_comment         PARAMS ((cpp_reader *, int));
44 static void skip_string         PARAMS ((cpp_reader *, int));
45 static void parse_string        PARAMS ((cpp_reader *, int));
46 static U_CHAR *find_position    PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
47 static int null_cleanup         PARAMS ((cpp_buffer *, cpp_reader *));
48 static void null_warning        PARAMS ((cpp_reader *, unsigned int));
49
50 static void safe_fwrite         PARAMS ((cpp_reader *, const U_CHAR *,
51                                          size_t, FILE *));
52 static void output_line_command PARAMS ((cpp_reader *, cpp_printer *));
53
54 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
55
56 void
57 _cpp_grow_token_buffer (pfile, n)
58      cpp_reader *pfile;
59      long n;
60 {
61   long old_written = CPP_WRITTEN (pfile);
62   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
63   pfile->token_buffer = (U_CHAR *)
64     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
65   CPP_SET_WRITTEN (pfile, old_written);
66 }
67
68 static int
69 null_cleanup (pbuf, pfile)
70      cpp_buffer *pbuf ATTRIBUTE_UNUSED;
71      cpp_reader *pfile ATTRIBUTE_UNUSED;
72 {
73   return 0;
74 }
75
76 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
77    If BUFFER != NULL, then use the LENGTH characters in BUFFER
78    as the new input buffer.
79    Return the new buffer, or NULL on failure.  */
80
81 cpp_buffer *
82 cpp_push_buffer (pfile, buffer, length)
83      cpp_reader *pfile;
84      const U_CHAR *buffer;
85      long length;
86 {
87   cpp_buffer *buf = CPP_BUFFER (pfile);
88   cpp_buffer *new;
89   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
90     {
91       cpp_fatal (pfile, "macro or `#include' recursion too deep");
92       return NULL;
93     }
94
95   new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
96
97   new->if_stack = pfile->if_stack;
98   new->cleanup = null_cleanup;
99   new->buf = new->cur = buffer;
100   new->rlimit = buffer + length;
101   new->prev = buf;
102   new->mark = NULL;
103   new->line_base = NULL;
104
105   CPP_BUFFER (pfile) = new;
106   return new;
107 }
108
109 cpp_buffer *
110 cpp_pop_buffer (pfile)
111      cpp_reader *pfile;
112 {
113   cpp_buffer *buf = CPP_BUFFER (pfile);
114   if (ACTIVE_MARK_P (pfile))
115     cpp_ice (pfile, "mark active in cpp_pop_buffer");
116   (*buf->cleanup) (buf, pfile);
117   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
118   free (buf);
119   pfile->buffer_stack_depth--;
120   return CPP_BUFFER (pfile);
121 }
122
123 /* Deal with the annoying semantics of fwrite.  */
124 static void
125 safe_fwrite (pfile, buf, len, fp)
126      cpp_reader *pfile;
127      const U_CHAR *buf;
128      size_t len;
129      FILE *fp;
130 {
131   size_t count;
132
133   while (len)
134     {
135       count = fwrite (buf, 1, len, fp);
136       if (count == 0)
137         goto error;
138       len -= count;
139       buf += count;
140     }
141   return;
142
143  error:
144   cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
145 }
146
147 /* Notify the compiler proper that the current line number has jumped,
148    or the current file name has changed.  */
149
150 static void
151 output_line_command (pfile, print)
152      cpp_reader *pfile;
153      cpp_printer *print;
154 {
155   unsigned int line;
156   cpp_buffer *ip;
157   enum { same = 0, enter, leave, rname } change;
158   static const char * const codes[] = { "", " 1", " 2", "" };
159
160   if (CPP_OPTION (pfile, no_line_commands))
161     return;
162
163   ip = cpp_file_buffer (pfile);
164   if (ip == NULL)
165     return;
166   line = CPP_BUF_LINE (ip);
167
168   /* Determine whether the current filename has changed, and if so,
169      how.  'nominal_fname' values are unique, so they can be compared
170      by comparing pointers.  */
171   if (ip->nominal_fname == print->last_fname)
172     change = same;
173   else
174     {
175       if (pfile->buffer_stack_depth == print->last_bsd)
176         change = rname;
177       else
178         {
179           if (pfile->buffer_stack_depth > print->last_bsd)
180             change = enter;
181           else
182             change = leave;
183           print->last_bsd = pfile->buffer_stack_depth;
184         }
185       print->last_fname = ip->nominal_fname;
186     }
187   /* If the current file has not changed, we can output a few newlines
188      instead if we want to increase the line number by a small amount.
189      We cannot do this if print->lineno is zero, because that means we
190      haven't output any line commands yet.  (The very first line
191      command output is a `same_file' command.)  */
192   if (change == same && print->lineno != 0
193       && line >= print->lineno && line < print->lineno + 8)
194     {
195       while (line > print->lineno)
196         {
197           putc ('\n', print->outf);
198           print->lineno++;
199         }
200       return;
201     }
202
203 #ifndef NO_IMPLICIT_EXTERN_C
204   if (CPP_OPTION (pfile, cplusplus))
205     fprintf (print->outf, "# %u \"%s\"%s%s%s\n", line, ip->nominal_fname,
206              codes[change],
207              ip->system_header_p ? " 3" : "",
208              (ip->system_header_p == 2) ? " 4" : "");
209   else
210 #endif
211     fprintf (print->outf, "# %u \"%s\"%s%s\n", line, ip->nominal_fname,
212              codes[change],
213              ip->system_header_p ? " 3" : "");
214   print->lineno = line;
215 }
216
217 /* Write the contents of the token_buffer to the output stream, and
218    clear the token_buffer.  Also handles generating line commands and
219    keeping track of file transitions.  */
220
221 void
222 cpp_output_tokens (pfile, print)
223      cpp_reader *pfile;
224      cpp_printer *print;
225 {
226   if (CPP_WRITTEN (pfile) - print->written)
227     {
228       if (CPP_PWRITTEN (pfile)[-1] == '\n' && print->lineno)
229         print->lineno++;
230       safe_fwrite (pfile, pfile->token_buffer,
231                    CPP_WRITTEN (pfile) - print->written, print->outf);
232     }
233   output_line_command (pfile, print);
234   CPP_SET_WRITTEN (pfile, print->written);
235 }
236
237 /* Scan a string (which may have escape marks), perform macro expansion,
238    and write the result to the token_buffer.  */
239
240 void
241 _cpp_expand_to_buffer (pfile, buf, length)
242      cpp_reader *pfile;
243      const U_CHAR *buf;
244      int length;
245 {
246   cpp_buffer *ip;
247   enum cpp_ttype token;
248   U_CHAR *buf1;
249
250   if (length < 0)
251     {
252       cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
253       return;
254     }
255
256   /* Copy the buffer, because it might be in an unsafe place - for
257      example, a sequence on the token_buffer, where the pointers will
258      be invalidated if we enlarge the token_buffer.  */
259   buf1 = alloca (length);
260   memcpy (buf1, buf, length);
261
262   /* Set up the input on the input stack.  */
263   ip = cpp_push_buffer (pfile, buf1, length);
264   if (ip == NULL)
265     return;
266   ip->has_escapes = 1;
267
268   /* Scan the input, create the output.  */
269   for (;;)
270     {
271       token = cpp_get_token (pfile);
272       if (token == CPP_EOF)
273         break;
274       if (token == CPP_POP && CPP_BUFFER (pfile) == ip)
275         {
276           cpp_pop_buffer (pfile);
277           break;
278         }
279     }
280   CPP_NUL_TERMINATE (pfile);
281 }
282
283 /* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output.
284    Then pop the buffer.  */
285
286 void
287 cpp_scan_buffer_nooutput (pfile)
288      cpp_reader *pfile;
289 {
290   cpp_buffer *buffer = CPP_BUFFER (pfile);
291   enum cpp_ttype token;
292   unsigned int old_written = CPP_WRITTEN (pfile);
293   /* In no-output mode, we can ignore everything but directives.  */
294   for (;;)
295     {
296       if (! pfile->only_seen_white)
297         _cpp_skip_rest_of_line (pfile);
298       token = cpp_get_token (pfile);
299       if (token == CPP_EOF)
300         break;
301       if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
302         {
303           cpp_pop_buffer (pfile);
304           break;
305         }
306     }
307   CPP_SET_WRITTEN (pfile, old_written);
308 }
309
310 /* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT.
311    Then pop the buffer.  */
312
313 void
314 cpp_scan_buffer (pfile, print)
315      cpp_reader *pfile;
316      cpp_printer *print;
317 {
318   cpp_buffer *buffer = CPP_BUFFER (pfile);
319   enum cpp_ttype token;
320
321   for (;;)
322     {
323       token = cpp_get_token (pfile);
324       if ((token == CPP_POP && !CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
325           || token == CPP_EOF || token == CPP_VSPACE
326           /* XXX Temporary kluge - force flush after #include only */
327           || (token == CPP_DIRECTIVE
328               && CPP_BUFFER (pfile)->nominal_fname != print->last_fname))
329         {
330           cpp_output_tokens (pfile, print);
331           if (token == CPP_EOF)
332             return;
333           if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
334             {
335               cpp_pop_buffer (pfile);
336               return;
337             }
338         }
339     }
340 }
341
342 /* Return the topmost cpp_buffer that corresponds to a file (not a macro).  */
343
344 cpp_buffer *
345 cpp_file_buffer (pfile)
346      cpp_reader *pfile;
347 {
348   cpp_buffer *ip;
349
350   for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
351     if (ip->ihash != NULL)
352       return ip;
353   return NULL;
354 }
355
356 /* Skip a C-style block comment.  We know it's a comment, and point is
357    at the second character of the starter.  */
358 static void
359 skip_block_comment (pfile)
360      cpp_reader *pfile;
361 {
362   unsigned int line, col;
363   const U_CHAR *limit, *cur;
364
365   FORWARD(1);
366   line = CPP_BUF_LINE (CPP_BUFFER (pfile));
367   col = CPP_BUF_COL (CPP_BUFFER (pfile));
368   limit = CPP_BUFFER (pfile)->rlimit;
369   cur = CPP_BUFFER (pfile)->cur;
370
371   while (cur < limit)
372     {
373       char c = *cur++;
374       if (c == '\n' || c == '\r')
375         {
376           /* \r cannot be a macro escape marker here. */
377           if (!ACTIVE_MARK_P (pfile))
378             CPP_BUMP_LINE_CUR (pfile, cur);
379         }
380       else if (c == '*')
381         {
382           /* Check for teminator.  */
383           if (cur < limit && *cur == '/')
384             goto out;
385
386           /* Warn about comment starter embedded in comment.  */
387           if (cur[-2] == '/' && CPP_OPTION (pfile, warn_comments))
388             cpp_warning_with_line (pfile, CPP_BUFFER (pfile)->lineno,
389                                    cur - CPP_BUFFER (pfile)->line_base,
390                                    "'/*' within comment");
391         }
392     }
393
394   cpp_error_with_line (pfile, line, col, "unterminated comment");
395   cur--;
396  out:
397   CPP_BUFFER (pfile)->cur = cur + 1;
398 }
399
400 /* Skip a C++/Chill line comment.  We know it's a comment, and point
401    is at the second character of the initiator.  */
402 static void
403 skip_line_comment (pfile)
404      cpp_reader *pfile;
405 {
406   FORWARD(1);
407   for (;;)
408     {
409       int c = GETC ();
410
411       /* We don't have to worry about EOF in here.  */
412       if (c == '\n')
413         {
414           /* Don't consider final '\n' to be part of comment.  */
415           FORWARD(-1);
416           return;
417         }
418       else if (c == '\r')
419         {
420           /* \r cannot be a macro escape marker here. */
421           if (!ACTIVE_MARK_P (pfile))
422             CPP_BUMP_LINE (pfile);
423           if (CPP_OPTION (pfile, warn_comments))
424             cpp_warning (pfile, "backslash-newline within line comment");
425         }
426     }
427 }
428
429 /* Skip a comment - C, C++, or Chill style.  M is the first character
430    of the comment marker.  If this really is a comment, skip to its
431    end and return ' '.  If this is not a comment, return M (which will
432    be '/' or '-').  */
433
434 static int
435 skip_comment (pfile, m)
436      cpp_reader *pfile;
437      int m;
438 {
439   if (m == '/' && PEEKC() == '*')
440     {
441       skip_block_comment (pfile);
442       return ' ';
443     }
444   else if (m == '/' && PEEKC() == '/')
445     {
446       if (CPP_BUFFER (pfile)->system_header_p)
447         {
448           /* We silently allow C++ comments in system headers, irrespective
449              of conformance mode, because lots of busted systems do that
450              and trying to clean it up in fixincludes is a nightmare.  */
451           skip_line_comment (pfile);
452           return ' ';
453         }
454       else if (CPP_OPTION (pfile, cplusplus_comments))
455         {
456           if (CPP_OPTION (pfile, c89)
457               && CPP_PEDANTIC (pfile)
458               && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
459             {
460               cpp_pedwarn (pfile,
461                            "C++ style comments are not allowed in ISO C89");
462               cpp_pedwarn (pfile,
463                            "(this will be reported only once per input file)");
464               CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
465             }
466           skip_line_comment (pfile);
467           return ' ';
468         }
469       else
470         return m;
471     }
472   else if (m == '-' && PEEKC() == '-'
473            && CPP_OPTION (pfile, chill))
474     {
475       skip_line_comment (pfile);
476       return ' ';
477     }
478   else
479     return m;
480 }
481
482 /* Identical to skip_comment except that it copies the comment into the
483    token_buffer.  This is used if !discard_comments.  */
484 static int
485 copy_comment (pfile, m)
486      cpp_reader *pfile;
487      int m;
488 {
489   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
490   const U_CHAR *limit;
491
492   if (skip_comment (pfile, m) == m)
493     return m;
494
495   limit = CPP_BUFFER (pfile)->cur;
496   CPP_RESERVE (pfile, limit - start + 2);
497   CPP_PUTC_Q (pfile, m);
498   for (; start <= limit; start++)
499     if (*start != '\r')
500       CPP_PUTC_Q (pfile, *start);
501
502   return ' ';
503 }
504
505 static void
506 null_warning (pfile, count)
507      cpp_reader *pfile;
508      unsigned int count;
509 {
510   if (count == 1)
511     cpp_warning (pfile, "embedded null character ignored");
512   else
513     cpp_warning (pfile, "embedded null characters ignored");
514 }
515
516 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
517
518 void
519 _cpp_skip_hspace (pfile)
520      cpp_reader *pfile;
521 {
522   unsigned int null_count = 0;
523   int c;
524
525   while (1)
526     {
527       c = GETC();
528       if (c == EOF)
529         goto out;
530       else if (is_hspace(c))
531         {
532           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
533             cpp_pedwarn (pfile, "%s in preprocessing directive",
534                          c == '\f' ? "formfeed" : "vertical tab");
535           else if (c == '\0')
536             null_count++;
537         }
538       else if (c == '\r')
539         {
540           /* \r is a backslash-newline marker if !has_escapes, and
541              a deletable-whitespace or no-reexpansion marker otherwise. */
542           if (CPP_BUFFER (pfile)->has_escapes)
543             {
544               if (PEEKC() == ' ')
545                 FORWARD(1);
546               else
547                 break;
548             }
549           else
550             CPP_BUMP_LINE (pfile);
551         }
552       else if (c == '/' || c == '-')
553         {
554           c = skip_comment (pfile, c);
555           if (c  != ' ')
556             break;
557         }
558       else
559         break;
560     }
561   FORWARD(-1);
562  out:
563   if (null_count)
564     null_warning (pfile, null_count);
565 }
566
567 /* Read and discard the rest of the current line.  */
568
569 void
570 _cpp_skip_rest_of_line (pfile)
571      cpp_reader *pfile;
572 {
573   for (;;)
574     {
575       int c = GETC();
576       switch (c)
577         {
578         case '\n':
579           FORWARD(-1);
580         case EOF:
581           return;
582
583         case '\r':
584           if (! CPP_BUFFER (pfile)->has_escapes)
585             CPP_BUMP_LINE (pfile);
586           break;
587           
588         case '\'':
589         case '\"':
590           skip_string (pfile, c);
591           break;
592
593         case '/':
594         case '-':
595           skip_comment (pfile, c);
596           break;
597
598         case '\f':
599         case '\v':
600           if (CPP_PEDANTIC (pfile))
601             cpp_pedwarn (pfile, "%s in preprocessing directive",
602                          c == '\f' ? "formfeed" : "vertical tab");
603           break;
604
605         }
606     }
607 }
608
609 /* Parse an identifier starting with C.  */
610
611 void
612 _cpp_parse_name (pfile, c)
613      cpp_reader *pfile;
614      int c;
615 {
616   for (;;)
617   {
618       if (! is_idchar(c))
619       {
620           FORWARD (-1);
621           break;
622       }
623
624       if (c == '$' && CPP_PEDANTIC (pfile))
625         cpp_pedwarn (pfile, "`$' in identifier");
626
627       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
628       CPP_PUTC_Q (pfile, c);
629       c = GETC();
630       if (c == EOF)
631         break;
632   }
633   CPP_NUL_TERMINATE_Q (pfile);
634   return;
635 }
636
637 /* Parse and skip over a string starting with C.  A single quoted
638    string is treated like a double -- some programs (e.g., troff) are
639    perverse this way.  (However, a single quoted string is not allowed
640    to extend over multiple lines.)  */
641 static void
642 skip_string (pfile, c)
643      cpp_reader *pfile;
644      int c;
645 {
646   unsigned int start_line, start_column;
647   unsigned int null_count = 0;
648
649   start_line = CPP_BUF_LINE (CPP_BUFFER (pfile));
650   start_column = CPP_BUF_COL (CPP_BUFFER (pfile));
651   while (1)
652     {
653       int cc = GETC();
654       switch (cc)
655         {
656         case EOF:
657           cpp_error_with_line (pfile, start_line, start_column,
658                                "unterminated string or character constant");
659           if (pfile->multiline_string_line != start_line
660               && pfile->multiline_string_line != 0)
661             cpp_error_with_line (pfile,
662                                  pfile->multiline_string_line, -1,
663                          "possible real start of unterminated constant");
664           pfile->multiline_string_line = 0;
665           goto out;
666
667         case '\0':
668           null_count++;
669           break;
670           
671         case '\n':
672           CPP_BUMP_LINE (pfile);
673           /* In Fortran and assembly language, silently terminate
674              strings of either variety at end of line.  This is a
675              kludge around not knowing where comments are in these
676              languages.  */
677           if (CPP_OPTION (pfile, lang_fortran)
678               || CPP_OPTION (pfile, lang_asm))
679             {
680               FORWARD(-1);
681               goto out;
682             }
683           /* Character constants may not extend over multiple lines.
684              In Standard C, neither may strings.  We accept multiline
685              strings as an extension.  */
686           if (c == '\'')
687             {
688               cpp_error_with_line (pfile, start_line, start_column,
689                                    "unterminated character constant");
690               FORWARD(-1);
691               goto out;
692             }
693           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
694             cpp_pedwarn_with_line (pfile, start_line, start_column,
695                                    "string constant runs past end of line");
696           if (pfile->multiline_string_line == 0)
697             pfile->multiline_string_line = start_line;
698           break;
699
700         case '\r':
701           if (CPP_BUFFER (pfile)->has_escapes)
702             {
703               cpp_ice (pfile, "\\r escape inside string constant");
704               FORWARD(1);
705             }
706           else
707             /* Backslash newline is replaced by nothing at all.  */
708             CPP_BUMP_LINE (pfile);
709           break;
710
711         case '\\':
712           FORWARD(1);
713           break;
714
715         case '\"':
716         case '\'':
717           if (cc == c)
718             goto out;
719           break;
720         }
721     }
722
723  out:
724   if (null_count == 1)
725     cpp_warning (pfile, "null character in string or character constant");
726   else if (null_count > 1)
727     cpp_warning (pfile, "null characters in string or character constant");
728 }
729
730 /* Parse a string and copy it to the output.  */
731
732 static void
733 parse_string (pfile, c)
734      cpp_reader *pfile;
735      int c;
736 {
737   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
738   const U_CHAR *limit;
739
740   skip_string (pfile, c);
741
742   limit = CPP_BUFFER (pfile)->cur;
743   CPP_RESERVE (pfile, limit - start + 2);
744   CPP_PUTC_Q (pfile, c);
745   for (; start < limit; start++)
746     if (*start != '\r')
747       CPP_PUTC_Q (pfile, *start);
748 }
749
750 /* Read an assertion into the token buffer, converting to
751    canonical form: `#predicate(a n swe r)'  The next non-whitespace
752    character to read should be the first letter of the predicate.
753    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
754    with answer (see callers for why). In case of 0, an error has been
755    printed. */
756 int
757 _cpp_parse_assertion (pfile)
758      cpp_reader *pfile;
759 {
760   int c, dropwhite;
761   _cpp_skip_hspace (pfile);
762   c = PEEKC();
763   if (c == '\n')
764     {
765       cpp_error (pfile, "assertion without predicate");
766       return 0;
767     }
768   else if (! is_idstart(c))
769     {
770       cpp_error (pfile, "assertion predicate is not an identifier");
771       return 0;
772     }
773   CPP_PUTC(pfile, '#');
774   FORWARD(1);
775   _cpp_parse_name (pfile, c);
776
777   c = PEEKC();
778   if (c != '(')
779     {
780       if (is_hspace(c) || c == '\r')
781         _cpp_skip_hspace (pfile);
782       c = PEEKC();
783     }
784   if (c != '(')
785     return 1;
786
787   CPP_PUTC(pfile, '(');
788   FORWARD(1);
789   dropwhite = 1;
790   while ((c = GETC()) != ')')
791     {
792       if (is_space(c))
793         {
794           if (! dropwhite)
795             {
796               CPP_PUTC(pfile, ' ');
797               dropwhite = 1;
798             }
799         }
800       else if (c == '\n' || c == EOF)
801         {
802           if (c == '\n') FORWARD(-1);
803           cpp_error (pfile, "un-terminated assertion answer");
804           return 0;
805         }
806       else if (c == '\r')
807         /* \r cannot be a macro escape here. */
808         CPP_BUMP_LINE (pfile);
809       else
810         {
811           CPP_PUTC (pfile, c);
812           dropwhite = 0;
813         }
814     }
815
816   if (pfile->limit[-1] == ' ')
817     pfile->limit[-1] = ')';
818   else if (pfile->limit[-1] == '(')
819     {
820       cpp_error (pfile, "empty token sequence in assertion");
821       return 0;
822     }
823   else
824     CPP_PUTC (pfile, ')');
825
826   return 2;
827 }
828
829 /* Get the next token, and add it to the text in pfile->token_buffer.
830    Return the kind of token we got.  */
831
832 enum cpp_ttype
833 _cpp_lex_token (pfile)
834      cpp_reader *pfile;
835 {
836   register int c, c2;
837   enum cpp_ttype token;
838
839   if (CPP_BUFFER (pfile) == NULL)
840     return CPP_EOF;
841
842  get_next:
843   c = GETC();
844   switch (c)
845     {
846     case EOF:
847       return CPP_EOF;
848
849     case '/':
850       if (PEEKC () == '=')
851         goto op2;
852
853     comment:
854       if (CPP_OPTION (pfile, discard_comments))
855         c = skip_comment (pfile, c);
856       else
857         c = copy_comment (pfile, c);
858       if (c != ' ')
859         goto randomchar;
860           
861       /* Comments are equivalent to spaces.
862          For -traditional, a comment is equivalent to nothing.  */
863       if (!CPP_OPTION (pfile, discard_comments))
864         return CPP_COMMENT;
865       else if (CPP_TRADITIONAL (pfile)
866                && ! is_space (PEEKC ()))
867         {
868           if (pfile->parsing_define_directive)
869             return CPP_COMMENT;
870           else
871             goto get_next;
872         }
873       else
874         {
875           CPP_PUTC (pfile, c);
876           return CPP_HSPACE;
877         }
878
879     case '#':
880       CPP_PUTC (pfile, c);
881
882     hash:
883       if (pfile->parsing_if_directive)
884         {
885           CPP_ADJUST_WRITTEN (pfile, -1);
886           if (_cpp_parse_assertion (pfile))
887             return CPP_ASSERTION;
888           return CPP_OTHER;
889         }
890
891       if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
892         {
893           c2 = PEEKC ();
894           if (c2 == '#')
895             {
896               FORWARD (1);
897               CPP_PUTC (pfile, c2);
898             }
899           else if (c2 == '%' && PEEKN (1) == ':')
900             {
901               /* Digraph: "%:" == "#".  */
902               FORWARD (1);
903               CPP_RESERVE (pfile, 2);
904               CPP_PUTC_Q (pfile, c2);
905               CPP_PUTC_Q (pfile, GETC ());
906             }
907           else
908             return CPP_STRINGIZE;
909
910           return CPP_TOKPASTE;
911         }
912
913       if (!pfile->only_seen_white)
914         return CPP_OTHER;
915
916       /* Remove the "#" or "%:" from the token buffer.  */
917       CPP_ADJUST_WRITTEN (pfile, (c == '#' ? -1 : -2));
918       return CPP_DIRECTIVE;
919
920     case '\"':
921     case '\'':
922       parse_string (pfile, c);
923       return c == '\'' ? CPP_CHAR : CPP_STRING;
924
925     case '$':
926       if (!CPP_OPTION (pfile, dollars_in_ident))
927         goto randomchar;
928       goto letter;
929
930     case ':':
931       c2 = PEEKC ();
932       /* Digraph: ":>" == "]".  */
933       if (c2 == '>'
934           || (c2 == ':' && CPP_OPTION (pfile, cplusplus)))
935         goto op2;
936       goto randomchar;
937
938     case '&':
939     case '+':
940     case '|':
941       c2 = PEEKC ();
942       if (c2 == c || c2 == '=')
943         goto op2;
944       goto randomchar;
945
946     case '%':
947       /* Digraphs: "%:" == "#", "%>" == "}".  */
948       c2 = PEEKC ();
949       if (c2 == ':')
950         {
951           FORWARD (1);
952           CPP_RESERVE (pfile, 2);
953           CPP_PUTC_Q (pfile, c);
954           CPP_PUTC_Q (pfile, c2);
955           goto hash;
956         }
957       else if (c2 == '>')
958         {
959           FORWARD (1);
960           CPP_RESERVE (pfile, 2);
961           CPP_PUTC_Q (pfile, c);
962           CPP_PUTC_Q (pfile, c2);
963           return CPP_RBRACE;
964         }
965       /* else fall through */
966
967     case '*':
968     case '!':
969     case '=':
970     case '^':
971       if (PEEKC () == '=')
972         goto op2;
973       goto randomchar;
974
975     case '-':
976       c2 = PEEKC ();
977       if (c2 == '-')
978         {
979           if (CPP_OPTION (pfile, chill))
980             goto comment;  /* Chill style comment */
981           else
982             goto op2;
983         }
984       else if (c2 == '=')
985         goto op2;
986       else if (c2 == '>')
987         {
988           if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
989             {
990               /* In C++, there's a ->* operator.  */
991               token = CPP_OTHER;
992               CPP_RESERVE (pfile, 4);
993               CPP_PUTC_Q (pfile, c);
994               CPP_PUTC_Q (pfile, GETC ());
995               CPP_PUTC_Q (pfile, GETC ());
996               return token;
997             }
998           goto op2;
999         }
1000       goto randomchar;
1001
1002     case '<':
1003       if (pfile->parsing_include_directive)
1004         {
1005           for (;;)
1006             {
1007               CPP_PUTC (pfile, c);
1008               if (c == '>')
1009                 break;
1010               c = GETC ();
1011               if (c == '\n' || c == EOF)
1012                 {
1013                   cpp_error (pfile,
1014                              "missing '>' in `#include <FILENAME>'");
1015                   break;
1016                 }
1017               else if (c == '\r')
1018                 {
1019                   if (!CPP_BUFFER (pfile)->has_escapes)
1020                     {
1021                       /* Backslash newline is replaced by nothing. */
1022                       CPP_ADJUST_WRITTEN (pfile, -1);
1023                       CPP_BUMP_LINE (pfile);
1024                     }
1025                   else
1026                     {
1027                       /* We might conceivably get \r- or \r<space> in
1028                          here.  Just delete 'em. */
1029                       int d = GETC();
1030                       if (d != '-' && d != ' ')
1031                         cpp_ice (pfile, "unrecognized escape \\r%c", d);
1032                       CPP_ADJUST_WRITTEN (pfile, -1);
1033                     }                     
1034                 }
1035             }
1036           return CPP_STRING;
1037         }
1038       /* Digraphs: "<%" == "{", "<:" == "[".  */
1039       c2 = PEEKC ();
1040       if (c2 == '%')
1041         {
1042           FORWARD (1);
1043           CPP_RESERVE (pfile, 2);
1044           CPP_PUTC_Q (pfile, c);
1045           CPP_PUTC_Q (pfile, c2);
1046           return CPP_LBRACE;
1047         }
1048       else if (c2 == ':')
1049         goto op2;
1050       /* else fall through */
1051     case '>':
1052       c2 = PEEKC ();
1053       if (c2 == '=')
1054         goto op2;
1055       /* GNU C++ supports MIN and MAX operators <? and >?.  */
1056       if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
1057         goto randomchar;
1058       FORWARD(1);
1059       CPP_RESERVE (pfile, 3);
1060       CPP_PUTC_Q (pfile, c);
1061       CPP_PUTC_Q (pfile, c2);
1062       if (PEEKC () == '=')
1063         CPP_PUTC_Q (pfile, GETC ());
1064       return CPP_OTHER;
1065
1066     case '.':
1067       c2 = PEEKC ();
1068       if (ISDIGIT (c2))
1069         {
1070           CPP_PUTC (pfile, c);
1071           c = GETC ();
1072           goto number;
1073         }
1074
1075       /* In C++ there's a .* operator.  */
1076       if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
1077         goto op2;
1078
1079       if (c2 == '.' && PEEKN(1) == '.')
1080         {
1081           CPP_RESERVE (pfile, 3);
1082           CPP_PUTC_Q (pfile, '.');
1083           CPP_PUTC_Q (pfile, '.');
1084           CPP_PUTC_Q (pfile, '.');
1085           FORWARD (2);
1086           return CPP_3DOTS;
1087         }
1088       goto randomchar;
1089
1090     op2:
1091       CPP_RESERVE (pfile, 2);
1092       CPP_PUTC_Q (pfile, c);
1093       CPP_PUTC_Q (pfile, GETC ());
1094       return CPP_OTHER;
1095
1096     case 'L':
1097       c2 = PEEKC ();
1098       if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
1099         {
1100           CPP_PUTC (pfile, c);
1101           c = GETC ();
1102           parse_string (pfile, c);
1103           return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
1104         }
1105       goto letter;
1106
1107     case '0': case '1': case '2': case '3': case '4':
1108     case '5': case '6': case '7': case '8': case '9':
1109     number:
1110     c2  = '.';
1111     for (;;)
1112       {
1113         CPP_RESERVE (pfile, 2);
1114         CPP_PUTC_Q (pfile, c);
1115         c = PEEKC ();
1116         if (c == EOF)
1117           break;
1118         if (!is_numchar(c) && c != '.'
1119             && ((c2 != 'e' && c2 != 'E'
1120                  && ((c2 != 'p' && c2 != 'P')
1121                      || CPP_OPTION (pfile, c89)))
1122                 || (c != '+' && c != '-')))
1123           break;
1124         FORWARD(1);
1125         c2= c;
1126       }
1127     CPP_NUL_TERMINATE_Q (pfile);
1128     return CPP_NUMBER;
1129     case 'b': case 'c': case 'd': case 'h': case 'o':
1130     case 'B': case 'C': case 'D': case 'H': case 'O':
1131       if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
1132         {
1133           CPP_RESERVE (pfile, 2);
1134           CPP_PUTC_Q (pfile, c);
1135           CPP_PUTC_Q (pfile, '\'');
1136           FORWARD(1);
1137           for (;;)
1138             {
1139               c = GETC();
1140               if (c == EOF)
1141                 goto chill_number_eof;
1142               if (!is_numchar(c))
1143                 break;
1144               CPP_PUTC (pfile, c);
1145             }
1146           if (c == '\'')
1147             {
1148               CPP_RESERVE (pfile, 2);
1149               CPP_PUTC_Q (pfile, c);
1150               CPP_NUL_TERMINATE_Q (pfile);
1151               return CPP_STRING;
1152             }
1153           else
1154             {
1155               FORWARD(-1);
1156             chill_number_eof:
1157               CPP_NUL_TERMINATE (pfile);
1158               return CPP_NUMBER;
1159             }
1160         }
1161       else
1162         goto letter;
1163     case '_':
1164     case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
1165     case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
1166     case 'r': case 's': case 't': case 'u': case 'v': case 'w':
1167     case 'x': case 'y': case 'z':
1168     case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
1169     case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
1170     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1171     case 'Y': case 'Z':
1172     letter:
1173     _cpp_parse_name (pfile, c);
1174     return CPP_MACRO;
1175
1176     case ' ':  case '\t':  case '\v': case '\f': case '\0':
1177       {
1178         int null_count = 0;
1179
1180         for (;;)
1181           {
1182             if (c == '\0')
1183               null_count++;
1184             else
1185               CPP_PUTC (pfile, c);
1186             c = PEEKC ();
1187             if (c == EOF || !is_hspace(c))
1188               break;
1189             FORWARD(1);
1190           }
1191         if (null_count)
1192           null_warning (pfile, null_count);
1193         return CPP_HSPACE;
1194       }
1195
1196     case '\r':
1197       if (CPP_BUFFER (pfile)->has_escapes)
1198         {
1199           c = GETC ();
1200           if (c == '-')
1201             {
1202               if (pfile->output_escapes)
1203                 CPP_PUTS (pfile, "\r-", 2);
1204               _cpp_parse_name (pfile, GETC ());
1205               return CPP_NAME;
1206             }
1207           else if (c == ' ')
1208             {
1209               /* "\r " means a space, but only if necessary to prevent
1210                  accidental token concatenation.  */
1211               CPP_RESERVE (pfile, 2);
1212               if (pfile->output_escapes)
1213                 CPP_PUTC_Q (pfile, '\r');
1214               CPP_PUTC_Q (pfile, c);
1215               return CPP_HSPACE;
1216             }
1217           else
1218             {
1219               cpp_ice (pfile, "unrecognized escape \\r%c", c);
1220               goto get_next;
1221             }
1222         }
1223       else
1224         {
1225           /* Backslash newline is ignored. */
1226           CPP_BUMP_LINE (pfile);
1227           goto get_next;
1228         }
1229
1230     case '\n':
1231       CPP_PUTC (pfile, c);
1232       return CPP_VSPACE;
1233
1234     case '(': token = CPP_LPAREN;    goto char1;
1235     case ')': token = CPP_RPAREN;    goto char1;
1236     case '{': token = CPP_LBRACE;    goto char1;
1237     case '}': token = CPP_RBRACE;    goto char1;
1238     case ',': token = CPP_COMMA;     goto char1;
1239     case ';': token = CPP_SEMICOLON; goto char1;
1240
1241     randomchar:
1242     default:
1243       token = CPP_OTHER;
1244     char1:
1245       CPP_PUTC (pfile, c);
1246       return token;
1247     }
1248 }
1249
1250 /* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
1251    Caller is expected to have checked no_macro_expand.  */
1252 static int
1253 maybe_macroexpand (pfile, written)
1254      cpp_reader *pfile;
1255      long written;
1256 {
1257   U_CHAR *macro = pfile->token_buffer + written;
1258   size_t len = CPP_WRITTEN (pfile) - written;
1259   HASHNODE *hp = _cpp_lookup (pfile, macro, len);
1260
1261   if (!hp)
1262     return 0;
1263   if (hp->type == T_DISABLED)
1264     {
1265       if (pfile->output_escapes)
1266         {
1267           /* Insert a no-reexpand marker before IDENT.  */
1268           CPP_RESERVE (pfile, 2);
1269           CPP_ADJUST_WRITTEN (pfile, 2);
1270           macro = pfile->token_buffer + written;
1271
1272           memmove (macro + 2, macro, len);
1273           macro[0] = '\r';
1274           macro[1] = '-';
1275         }
1276       return 0;
1277     }
1278   if (hp->type == T_EMPTY)
1279     {
1280       /* Special case optimization: macro expands to nothing.  */
1281       CPP_SET_WRITTEN (pfile, written);
1282       CPP_PUTC_Q (pfile, ' ');
1283       return 1;
1284     }
1285
1286   /* If macro wants an arglist, verify that a '(' follows.  */
1287   if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1288     {
1289       int macbuf_whitespace = 0;
1290       int c;
1291
1292       while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1293         {
1294           const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1295           for (;;)
1296             {
1297               _cpp_skip_hspace (pfile);
1298               c = PEEKC ();
1299               if (c == '\n')
1300                 FORWARD(1);
1301               else
1302                 break;
1303             }
1304           if (point != CPP_BUFFER (pfile)->cur)
1305             macbuf_whitespace = 1;
1306           if (c == '(')
1307             goto is_macro_call;
1308           else if (c != EOF)
1309             goto not_macro_call;
1310           cpp_pop_buffer (pfile);
1311         }
1312
1313       CPP_SET_MARK (pfile);
1314       for (;;)
1315         {
1316           _cpp_skip_hspace (pfile);
1317           c = PEEKC ();
1318           if (c == '\n')
1319             FORWARD(1);
1320           else
1321             break;
1322         }
1323       CPP_GOTO_MARK (pfile);
1324
1325       if (c != '(')
1326         {
1327         not_macro_call:
1328           if (macbuf_whitespace)
1329             CPP_PUTC (pfile, ' ');
1330           return 0;
1331         }
1332     }
1333
1334  is_macro_call:
1335   /* This is now known to be a macro call.
1336      Expand the macro, reading arguments as needed,
1337      and push the expansion on the input stack.  */
1338   _cpp_macroexpand (pfile, hp);
1339   CPP_SET_WRITTEN (pfile, written);
1340   return 1;
1341 }
1342
1343 enum cpp_ttype
1344 cpp_get_token (pfile)
1345      cpp_reader *pfile;
1346 {
1347   enum cpp_ttype token;
1348   long written = CPP_WRITTEN (pfile);
1349
1350  get_next:
1351   token = _cpp_lex_token (pfile);
1352
1353   switch (token)
1354     {
1355     default:
1356       pfile->potential_control_macro = 0;
1357       pfile->only_seen_white = 0;
1358       return token;
1359
1360     case CPP_VSPACE:
1361       if (pfile->only_seen_white == 0)
1362         pfile->only_seen_white = 1;
1363       CPP_BUMP_LINE (pfile);
1364       return token;
1365
1366     case CPP_HSPACE:
1367     case CPP_COMMENT:
1368       return token;
1369
1370     case CPP_DIRECTIVE:
1371       pfile->potential_control_macro = 0;
1372       if (_cpp_handle_directive (pfile))
1373         return CPP_DIRECTIVE;
1374       pfile->only_seen_white = 0;
1375       CPP_PUTC (pfile, '#');
1376       return CPP_OTHER;
1377
1378     case CPP_MACRO:
1379       pfile->potential_control_macro = 0;
1380       pfile->only_seen_white = 0;
1381       if (! pfile->no_macro_expand
1382           && maybe_macroexpand (pfile, written))
1383         goto get_next;
1384       return CPP_NAME;
1385
1386     case CPP_EOF:
1387       if (CPP_BUFFER (pfile) == NULL)
1388         return CPP_EOF;
1389       if (CPP_BUFFER (pfile)->manual_pop)
1390         /* If we've been reading from redirected input, the
1391            frontend will pop the buffer.  */
1392         return CPP_EOF;
1393
1394       if (CPP_BUFFER (pfile)->seen_eof)
1395         {
1396           cpp_pop_buffer (pfile);
1397           goto get_next;
1398         }
1399       else
1400         {
1401           _cpp_handle_eof (pfile);
1402           return CPP_POP;
1403         }
1404     }
1405 }
1406
1407 /* Like cpp_get_token, but skip spaces and comments.  */
1408
1409 enum cpp_ttype
1410 cpp_get_non_space_token (pfile)
1411      cpp_reader *pfile;
1412 {
1413   int old_written = CPP_WRITTEN (pfile);
1414   for (;;)
1415     {
1416       enum cpp_ttype token = cpp_get_token (pfile);
1417       if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
1418         return token;
1419       CPP_SET_WRITTEN (pfile, old_written);
1420     }
1421 }
1422
1423 /* Like cpp_get_token, except that it does not execute directives,
1424    does not consume vertical space, and automatically pops off macro
1425    buffers.
1426
1427    XXX This function will exist only till collect_expansion doesn't
1428    need to see whitespace anymore, then it'll be merged with
1429    _cpp_get_directive_token (below).  */
1430 enum cpp_ttype
1431 _cpp_get_define_token (pfile)
1432      cpp_reader *pfile;
1433 {
1434   long old_written;
1435   enum cpp_ttype token;
1436
1437  get_next:
1438   old_written = CPP_WRITTEN (pfile);
1439   token = _cpp_lex_token (pfile);
1440   switch (token)
1441     {
1442     default:
1443       return token;
1444
1445     case CPP_VSPACE:
1446       /* Put it back and return VSPACE.  */
1447       FORWARD(-1);
1448       CPP_ADJUST_WRITTEN (pfile, -1);
1449       return CPP_VSPACE;
1450
1451     case CPP_HSPACE:
1452       if (CPP_PEDANTIC (pfile))
1453         {
1454           U_CHAR *p, *limit;
1455           p = pfile->token_buffer + old_written;
1456           limit = CPP_PWRITTEN (pfile);
1457           while (p < limit)
1458             {
1459               if (*p == '\v' || *p == '\f')
1460                 cpp_pedwarn (pfile, "%s in preprocessing directive",
1461                              *p == '\f' ? "formfeed" : "vertical tab");
1462               p++;
1463             }
1464         }
1465       return CPP_HSPACE;
1466
1467     case CPP_DIRECTIVE:
1468       /* Don't execute the directive, but don't smash it to OTHER either.  */
1469       CPP_PUTC (pfile, '#');
1470       return CPP_DIRECTIVE;
1471
1472     case CPP_MACRO:
1473       if (! pfile->no_macro_expand
1474           && maybe_macroexpand (pfile, old_written))
1475         goto get_next;
1476       return CPP_NAME;
1477
1478     case CPP_EOF:
1479       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1480         {
1481           cpp_pop_buffer (pfile);
1482           goto get_next;
1483         }
1484       else
1485         /* This can happen for files that don't end with a newline,
1486            and for cpp_define and friends.  Pretend they do, so
1487            callers don't have to deal.  A warning will be issued by
1488            someone else, if necessary.  */
1489         return CPP_VSPACE;
1490     }
1491 }
1492
1493 /* Just like _cpp_get_define_token except that it discards horizontal
1494    whitespace.  */
1495
1496 enum cpp_ttype
1497 _cpp_get_directive_token (pfile)
1498      cpp_reader *pfile;
1499 {
1500   int old_written = CPP_WRITTEN (pfile);
1501   for (;;)
1502     {
1503       enum cpp_ttype token = _cpp_get_define_token (pfile);
1504       if (token != CPP_COMMENT && token != CPP_HSPACE)
1505         return token;
1506       CPP_SET_WRITTEN (pfile, old_written);
1507     }
1508 }
1509
1510 /* Determine the current line and column.  Used only by read_and_prescan. */
1511 static U_CHAR *
1512 find_position (start, limit, linep)
1513      U_CHAR *start;
1514      U_CHAR *limit;
1515      unsigned long *linep;
1516 {
1517   unsigned long line = *linep;
1518   U_CHAR *lbase = start;
1519   while (start < limit)
1520     {
1521       U_CHAR ch = *start++;
1522       if (ch == '\n' || ch == '\r')
1523         {
1524           line++;
1525           lbase = start;
1526         }
1527     }
1528   *linep = line;
1529   return lbase;
1530 }
1531
1532 /* The following table is used by _cpp_read_and_prescan.  If we have
1533    designated initializers, it can be constant data; otherwise, it is
1534    set up at runtime by _cpp_init_input_buffer.  */
1535
1536 #ifndef UCHAR_MAX
1537 #define UCHAR_MAX 255   /* assume 8-bit bytes */
1538 #endif
1539
1540 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
1541 #define init_chartab()  /* nothing */
1542 #define CHARTAB static const unsigned char chartab[UCHAR_MAX + 1] = {
1543 #define END };
1544 #define s(p, v) [p] = v,
1545 #else
1546 #define CHARTAB static unsigned char chartab[UCHAR_MAX + 1] = { 0 }; \
1547  static void init_chartab PARAMS ((void)) { \
1548  unsigned char *x = chartab;
1549 #define END }
1550 #define s(p, v) x[p] = v;
1551 #endif
1552
1553 /* Table of characters that can't be handled in the inner loop.
1554    Also contains the mapping between trigraph third characters and their
1555    replacements.  */
1556 #define SPECCASE_CR        1
1557 #define SPECCASE_BACKSLASH 2
1558 #define SPECCASE_QUESTION  3
1559  
1560 CHARTAB
1561   s('\r', SPECCASE_CR)
1562   s('\\', SPECCASE_BACKSLASH)
1563   s('?',  SPECCASE_QUESTION)
1564
1565   s('=', '#')   s(')', ']')     s('!', '|')
1566   s('(', '[')   s('\'', '^')    s('>', '}')
1567   s('/', '\\')  s('<', '{')     s('-', '~')
1568 END
1569
1570 #undef CHARTAB
1571 #undef END
1572 #undef s
1573
1574 #define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
1575 #define NONTRI(c) ((c) <= SPECCASE_QUESTION)
1576
1577 /* Read the entire contents of file DESC into buffer BUF.  LEN is how
1578    much memory to allocate initially; more will be allocated if
1579    necessary.  Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1580    canonical form (\n).  If enabled, convert and/or warn about
1581    trigraphs.  Convert backslash-newline to a one-character escape
1582    (\r) and remove it from "embarrassing" places (i.e. the middle of a
1583    token).  If there is no newline at the end of the file, add one and
1584    warn.  Returns -1 on failure, or the actual length of the data to
1585    be scanned.
1586
1587    This function does a lot of work, and can be a serious performance
1588    bottleneck.  It has been tuned heavily; make sure you understand it
1589    before hacking.  The common case - no trigraphs, Unix style line
1590    breaks, backslash-newline set off by whitespace, newline at EOF -
1591    has been optimized at the expense of the others.  The performance
1592    penalty for DOS style line breaks (\r\n) is about 15%.
1593    
1594    Warnings lose particularly heavily since we have to determine the
1595    line number, which involves scanning from the beginning of the file
1596    or from the last warning.  The penalty for the absence of a newline
1597    at the end of reload1.c is about 60%.  (reload1.c is 329k.)
1598
1599    If your file has more than one kind of end-of-line marker, you
1600    will get messed-up line numbering.
1601    
1602    So that the cases of the switch statement do not have to concern
1603    themselves with the complications of reading beyond the end of the
1604    buffer, the buffer is guaranteed to have at least 3 characters in
1605    it (or however many are left in the file, if less) on entry to the
1606    switch.  This is enough to handle trigraphs and the "\\\n\r" and
1607    "\\\r\n" cases.
1608    
1609    The end of the buffer is marked by a '\\', which, being a special
1610    character, guarantees we will exit the fast-scan loops and perform
1611    a refill. */
1612  
1613 long
1614 _cpp_read_and_prescan (pfile, fp, desc, len)
1615      cpp_reader *pfile;
1616      cpp_buffer *fp;
1617      int desc;
1618      size_t len;
1619 {
1620   U_CHAR *buf = (U_CHAR *) xmalloc (len);
1621   U_CHAR *ip, *op, *line_base;
1622   U_CHAR *ibase;
1623   unsigned long line;
1624   unsigned int deferred_newlines;
1625   size_t offset;
1626   int count = 0;
1627
1628   offset = 0;
1629   deferred_newlines = 0;
1630   op = buf;
1631   line_base = buf;
1632   line = 1;
1633   ibase = pfile->input_buffer + 3;
1634   ip = ibase;
1635   ip[-1] = '\0';  /* Guarantee no match with \n for SPECCASE_CR */
1636
1637   for (;;)
1638     {
1639       U_CHAR *near_buff_end;
1640
1641       count = read (desc, ibase, pfile->input_buffer_len);
1642       if (count < 0)
1643         goto error;
1644       
1645       ibase[count] = '\\';  /* Marks end of buffer */
1646       if (count)
1647         {
1648           near_buff_end = pfile->input_buffer + count;
1649           offset += count;
1650           if (offset > len)
1651             {
1652               size_t delta_op;
1653               size_t delta_line_base;
1654               len = offset * 2;
1655               if (offset > len)
1656                 /* len overflowed.
1657                    This could happen if the file is larger than half the
1658                    maximum address space of the machine. */
1659                 goto too_big;
1660
1661               delta_op = op - buf;
1662               delta_line_base = line_base - buf;
1663               buf = (U_CHAR *) xrealloc (buf, len);
1664               op = buf + delta_op;
1665               line_base = buf + delta_line_base;
1666             }
1667         }
1668       else
1669         {
1670           if (ip == ibase)
1671             break;
1672           /* Allow normal processing of the (at most 2) remaining
1673              characters.  The end-of-buffer marker is still present
1674              and prevents false matches within the switch. */
1675           near_buff_end = ibase - 1;
1676         }
1677
1678       for (;;)
1679         {
1680           unsigned int span;
1681
1682           /* Deal with \-newline, potentially in the middle of a token. */
1683           if (deferred_newlines)
1684             {
1685               if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
1686                 {
1687                   /* Previous was not white space.  Skip to white
1688                      space, if we can, before outputting the \r's */
1689                   span = 0;
1690                   while (ip[span] != ' '
1691                          && ip[span] != '\t'
1692                          && ip[span] != '\n'
1693                          && NORMAL(ip[span]))
1694                     span++;
1695                   memcpy (op, ip, span);
1696                   op += span;
1697                   ip += span;
1698                   if (! NORMAL(ip[0]))
1699                     goto do_speccase;
1700                 }
1701               while (deferred_newlines)
1702                 deferred_newlines--, *op++ = '\r';
1703             }
1704
1705           /* Copy as much as we can without special treatment. */
1706           span = 0;
1707           while (NORMAL (ip[span])) span++;
1708           memcpy (op, ip, span);
1709           op += span;
1710           ip += span;
1711
1712         do_speccase:
1713           if (ip > near_buff_end) /* Do we have enough chars? */
1714             break;
1715           switch (chartab[*ip++])
1716             {
1717             case SPECCASE_CR:  /* \r */
1718               if (ip[-2] != '\n')
1719                 {
1720                   if (*ip == '\n')
1721                     ip++;
1722                   *op++ = '\n';
1723                 }
1724               break;
1725
1726             case SPECCASE_BACKSLASH:  /* \ */
1727               if (*ip == '\n')
1728                 {
1729                   deferred_newlines++;
1730                   ip++;
1731                   if (*ip == '\r') ip++;
1732                 }
1733               else if (*ip == '\r')
1734                 {
1735                   deferred_newlines++;
1736                   ip++;
1737                   if (*ip == '\n') ip++;
1738                 }
1739               else
1740                 *op++ = '\\';
1741               break;
1742
1743             case SPECCASE_QUESTION: /* ? */
1744               {
1745                 unsigned int d, t;
1746
1747                 *op++ = '?'; /* Normal non-trigraph case */
1748                 if (ip[0] != '?')
1749                   break;
1750                     
1751                 d = ip[1];
1752                 t = chartab[d];
1753                 if (NONTRI (t))
1754                   break;
1755
1756                 if (CPP_OPTION (pfile, warn_trigraphs))
1757                   {
1758                     unsigned long col;
1759                     line_base = find_position (line_base, op, &line);
1760                     col = op - line_base + 1;
1761                     if (CPP_OPTION (pfile, trigraphs))
1762                       cpp_warning_with_line (pfile, line, col,
1763                                              "trigraph ??%c converted to %c", d, t);
1764                     else
1765                       cpp_warning_with_line (pfile, line, col,
1766                                              "trigraph ??%c ignored", d);
1767                   }
1768
1769                 ip += 2;
1770                 if (CPP_OPTION (pfile, trigraphs))
1771                   {
1772                     op[-1] = t;     /* Overwrite '?' */
1773                     if (t == '\\')
1774                       {
1775                         op--;
1776                         *--ip = '\\';
1777                         goto do_speccase; /* May need buffer refill */
1778                       }
1779                   }
1780                 else
1781                   {
1782                     *op++ = '?';
1783                     *op++ = d;
1784                   }
1785               }
1786               break;
1787             }
1788         }
1789       /* Copy previous char plus unprocessed (at most 2) chars
1790          to beginning of buffer, refill it with another
1791          read(), and continue processing */
1792       memmove (ip - count - 1, ip - 1, 4 - (ip - near_buff_end));
1793       ip -= count;
1794     }
1795
1796   if (offset == 0)
1797     return 0;
1798
1799   if (op[-1] != '\n')
1800     {
1801       unsigned long col;
1802       line_base = find_position (line_base, op, &line);
1803       col = op - line_base + 1;
1804       cpp_warning_with_line (pfile, line, col, "no newline at end of file");
1805       if (offset + 1 > len)
1806         {
1807           len += 1;
1808           if (offset + 1 > len)
1809             goto too_big;
1810           buf = (U_CHAR *) xrealloc (buf, len);
1811           op = buf + offset;
1812         }
1813       *op++ = '\n';
1814     }
1815
1816   fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1817   return op - buf;
1818
1819  too_big:
1820   cpp_notice (pfile, "%s is too large (>%lu bytes)", fp->ihash->name,
1821               (unsigned long)offset);
1822   free (buf);
1823   return -1;
1824
1825  error:
1826   cpp_error_from_errno (pfile, fp->ihash->name);
1827   free (buf);
1828   return -1;
1829 }
1830
1831 /* Allocate pfile->input_buffer, and initialize chartab[]
1832    if it hasn't happened already.  */
1833  
1834 void
1835 _cpp_init_input_buffer (pfile)
1836      cpp_reader *pfile;
1837 {
1838   U_CHAR *tmp;
1839
1840   init_chartab ();
1841
1842   /* Determine the appropriate size for the input buffer.  Normal C
1843      source files are smaller than eight K.  */
1844   /* 8Kbytes of buffer proper, 1 to detect running off the end without
1845      address arithmetic all the time, and 3 for pushback during buffer
1846      refill, in case there's a potential trigraph or end-of-line
1847      digraph at the end of a block. */
1848
1849   tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
1850   pfile->input_buffer = tmp;
1851   pfile->input_buffer_len = 8192;
1852 }