OSDN Git Service

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