OSDN Git Service

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