OSDN Git Service

* cpphash.c (trad_stringify, warn_trad_stringify,
[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    Single-pass line tokenization by Neil Booth, April 2000
8
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "intl.h"
26 #include "cpplib.h"
27 #include "cpphash.h"
28
29 #define PEEKBUF(BUFFER, N) \
30   ((BUFFER)->rlimit - (BUFFER)->cur > (N) ? (BUFFER)->cur[N] : EOF)
31 #define GETBUF(BUFFER) \
32   ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
33 #define FORWARDBUF(BUFFER, N) ((BUFFER)->cur += (N))
34
35 #define PEEKN(N) PEEKBUF (CPP_BUFFER (pfile), N)
36 #define FORWARD(N) FORWARDBUF (CPP_BUFFER (pfile), (N))
37 #define GETC() GETBUF (CPP_BUFFER (pfile))
38 #define PEEKC() PEEKBUF (CPP_BUFFER (pfile), 0)
39
40 static void skip_block_comment  PARAMS ((cpp_reader *));
41 static void skip_line_comment   PARAMS ((cpp_reader *));
42 static int maybe_macroexpand    PARAMS ((cpp_reader *, long));
43 static int skip_comment         PARAMS ((cpp_reader *, int));
44 static int copy_comment         PARAMS ((cpp_reader *, int));
45 static void skip_string         PARAMS ((cpp_reader *, int));
46 static void parse_string        PARAMS ((cpp_reader *, int));
47 static U_CHAR *find_position    PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
48 static void null_warning        PARAMS ((cpp_reader *, unsigned int));
49
50 static void safe_fwrite         PARAMS ((cpp_reader *, const U_CHAR *,
51                                          size_t, FILE *));
52 static void output_line_command PARAMS ((cpp_reader *, cpp_printer *,
53                                          unsigned int));
54 static void bump_column         PARAMS ((cpp_printer *, unsigned int,
55                                          unsigned int));
56 static void expand_name_space   PARAMS ((cpp_toklist *, unsigned int));
57 static void expand_token_space  PARAMS ((cpp_toklist *));
58 static void init_token_list     PARAMS ((cpp_reader *, cpp_toklist *, int));
59 static void pedantic_whitespace PARAMS ((cpp_reader *, U_CHAR *,
60                                          unsigned int));
61
62 #define auto_expand_name_space(list) \
63     expand_name_space ((list), 1 + (list)->name_cap / 2)
64
65 #ifdef NEW_LEXER
66
67 static void expand_comment_space PARAMS ((cpp_toklist *));
68 void init_trigraph_map PARAMS ((void));
69 static unsigned char* trigraph_replace PARAMS ((cpp_reader *, unsigned char *,
70                                                 unsigned char *));
71 static const unsigned char *backslash_start PARAMS ((cpp_reader *,
72                                                      const unsigned char *));
73 static int skip_block_comment2 PARAMS ((cpp_reader *));
74 static int skip_line_comment2 PARAMS ((cpp_reader *));
75 static void skip_whitespace PARAMS ((cpp_reader *, int));
76 static void parse_name PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *));
77 static void parse_number PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *));
78 static void parse_string2 PARAMS ((cpp_reader *, cpp_toklist *, cpp_name *,
79                                   unsigned int));
80 static int trigraph_ok PARAMS ((cpp_reader *, const unsigned char *));
81 static void save_comment PARAMS ((cpp_toklist *, const unsigned char *,
82                                   unsigned int, unsigned int, unsigned int));
83 void _cpp_lex_line PARAMS ((cpp_reader *, cpp_toklist *));
84
85 static void _cpp_output_list PARAMS ((cpp_reader *, cpp_toklist *));
86
87 static unsigned char * spell_token PARAMS ((cpp_reader *, cpp_token *,
88                                             unsigned char *, int));
89
90 typedef unsigned int (* speller) PARAMS ((unsigned char *, cpp_toklist *,
91                                           cpp_token *));
92
93 /* Macros on a cpp_name.  */
94 #define INIT_NAME(list, name) \
95   do {(name).len = 0; \
96       (name).text = (list)->namebuf + (list)->name_used;} while (0)
97
98 #define IS_DIRECTIVE(list) (TOK_TYPE (list, 0) == CPP_HASH)
99 #define COLUMN(cur) ((cur) - buffer->line_base)
100
101 /* Maybe put these in the ISTABLE eventually.  */
102 #define IS_HSPACE(c) ((c) == ' ' || (c) == '\t')
103 #define IS_NEWLINE(c) ((c) == '\n' || (c) == '\r')
104
105 /* Handle LF, CR, CR-LF and LF-CR style newlines.  Assumes next
106    character, if any, is in buffer.  */
107 #define handle_newline(cur, limit, c) \
108   do {\
109   if ((cur) < (limit) && *(cur) == '\r' + '\n' - c) \
110     (cur)++; \
111   CPP_BUMP_LINE_CUR (pfile, (cur)); \
112   } while (0)
113
114 #define IMMED_TOKEN() (!(cur_token->flags & PREV_WHITESPACE))
115 #define PREV_TOKEN_TYPE (cur_token[-1].type)
116
117 #define PUSH_TOKEN(ttype) cur_token++->type = ttype
118 #define REVISE_TOKEN(ttype) cur_token[-1].type = ttype
119 #define BACKUP_TOKEN(ttype) (--cur_token)->type = ttype
120 #define BACKUP_DIGRAPH(ttype) do { \
121   BACKUP_TOKEN(ttype); cur_token->flags |= DIGRAPH;} while (0)
122
123 /* An upper bound on the number of bytes needed to spell a token,
124    including preceding whitespace.  */
125 #define TOKEN_LEN(token) (5 + (token_spellings[token->type].type > \
126                                SPELL_NONE ? token->val.name.len: 0))
127
128 #endif
129
130 /* Order here matters.  Those beyond SPELL_NONE store their spelling
131    in the token list, and it's length in the token->val.name.len.  */
132 #define SPELL_OPERATOR 0
133 #define SPELL_CHAR     2        /* FIXME: revert order after transition. */
134 #define SPELL_NONE     1
135 #define SPELL_IDENT    3
136 #define SPELL_STRING   4
137
138 #define T(e, s) {SPELL_OPERATOR, (const U_CHAR *) s},
139 #define I(e, s) {SPELL_IDENT, s},
140 #define S(e, s) {SPELL_STRING, s},
141 #define C(e, s) {SPELL_CHAR, s},
142 #define N(e, s) {SPELL_NONE, s},
143
144 static const struct token_spelling
145 {
146   U_CHAR type;
147   const U_CHAR *spelling;
148 } token_spellings [N_TTYPES + 1] = {TTYPE_TABLE {0, 0} };
149
150 #undef T
151 #undef I
152 #undef S
153 #undef C
154 #undef N
155
156 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
157
158 void
159 _cpp_grow_token_buffer (pfile, n)
160      cpp_reader *pfile;
161      long n;
162 {
163   long old_written = CPP_WRITTEN (pfile);
164   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
165   pfile->token_buffer = (U_CHAR *)
166     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
167   CPP_SET_WRITTEN (pfile, old_written);
168 }
169
170 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
171    If BUFFER != NULL, then use the LENGTH characters in BUFFER
172    as the new input buffer.
173    Return the new buffer, or NULL on failure.  */
174
175 cpp_buffer *
176 cpp_push_buffer (pfile, buffer, length)
177      cpp_reader *pfile;
178      const U_CHAR *buffer;
179      long length;
180 {
181   cpp_buffer *buf = CPP_BUFFER (pfile);
182   cpp_buffer *new;
183   if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
184     {
185       cpp_fatal (pfile, "macro or `#include' recursion too deep");
186       return NULL;
187     }
188
189   new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
190
191   new->if_stack = pfile->if_stack;
192   new->buf = new->cur = buffer;
193   new->rlimit = buffer + length;
194   new->prev = buf;
195   new->mark = NULL;
196   new->line_base = NULL;
197
198   CPP_BUFFER (pfile) = new;
199   return new;
200 }
201
202 cpp_buffer *
203 cpp_pop_buffer (pfile)
204      cpp_reader *pfile;
205 {
206   cpp_buffer *buf = CPP_BUFFER (pfile);
207   if (ACTIVE_MARK_P (pfile))
208     cpp_ice (pfile, "mark active in cpp_pop_buffer");
209
210   if (buf->ihash)
211     {
212       _cpp_unwind_if_stack (pfile, buf);
213       if (buf->buf)
214         free ((PTR) buf->buf);
215       if (pfile->system_include_depth)
216         pfile->system_include_depth--;
217       if (pfile->potential_control_macro)
218         {
219           buf->ihash->control_macro = pfile->potential_control_macro;
220           pfile->potential_control_macro = 0;
221         }
222       pfile->input_stack_listing_current = 0;
223     }
224   else if (buf->macro)
225     {
226       HASHNODE *m = buf->macro;
227   
228       m->disabled = 0;
229       if ((m->type == T_FMACRO && buf->mapped)
230           || m->type == T_SPECLINE || m->type == T_FILE
231           || m->type == T_BASE_FILE || m->type == T_INCLUDE_LEVEL
232           || m->type == T_STDC)
233         free ((PTR) buf->buf);
234     }
235   CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
236   free (buf);
237   pfile->buffer_stack_depth--;
238   return CPP_BUFFER (pfile);
239 }
240
241 /* Deal with the annoying semantics of fwrite.  */
242 static void
243 safe_fwrite (pfile, buf, len, fp)
244      cpp_reader *pfile;
245      const U_CHAR *buf;
246      size_t len;
247      FILE *fp;
248 {
249   size_t count;
250
251   while (len)
252     {
253       count = fwrite (buf, 1, len, fp);
254       if (count == 0)
255         goto error;
256       len -= count;
257       buf += count;
258     }
259   return;
260
261  error:
262   cpp_notice_from_errno (pfile, CPP_OPTION (pfile, out_fname));
263 }
264
265 /* Notify the compiler proper that the current line number has jumped,
266    or the current file name has changed.  */
267
268 static void
269 output_line_command (pfile, print, line)
270      cpp_reader *pfile;
271      cpp_printer *print;
272      unsigned int line;
273 {
274   cpp_buffer *ip = cpp_file_buffer (pfile);
275   enum { same = 0, enter, leave, rname } change;
276   static const char * const codes[] = { "", " 1", " 2", "" };
277
278   if (CPP_OPTION (pfile, no_line_commands))
279     return;
280
281   /* Determine whether the current filename has changed, and if so,
282      how.  'nominal_fname' values are unique, so they can be compared
283      by comparing pointers.  */
284   if (ip->nominal_fname == print->last_fname)
285     change = same;
286   else
287     {
288       if (pfile->buffer_stack_depth == print->last_bsd)
289         change = rname;
290       else
291         {
292           if (pfile->buffer_stack_depth > print->last_bsd)
293             change = enter;
294           else
295             change = leave;
296           print->last_bsd = pfile->buffer_stack_depth;
297         }
298       print->last_fname = ip->nominal_fname;
299     }
300   /* If the current file has not changed, we can output a few newlines
301      instead if we want to increase the line number by a small amount.
302      We cannot do this if print->lineno is zero, because that means we
303      haven't output any line commands yet.  (The very first line
304      command output is a `same_file' command.)  */
305   if (change == same && print->lineno != 0
306       && line >= print->lineno && line < print->lineno + 8)
307     {
308       while (line > print->lineno)
309         {
310           putc ('\n', print->outf);
311           print->lineno++;
312         }
313       return;
314     }
315
316 #ifndef NO_IMPLICIT_EXTERN_C
317   if (CPP_OPTION (pfile, cplusplus))
318     fprintf (print->outf, "# %u \"%s\"%s%s%s\n", line, ip->nominal_fname,
319              codes[change],
320              ip->system_header_p ? " 3" : "",
321              (ip->system_header_p == 2) ? " 4" : "");
322   else
323 #endif
324     fprintf (print->outf, "# %u \"%s\"%s%s\n", line, ip->nominal_fname,
325              codes[change],
326              ip->system_header_p ? " 3" : "");
327   print->lineno = line;
328 }
329
330 /* Write the contents of the token_buffer to the output stream, and
331    clear the token_buffer.  Also handles generating line commands and
332    keeping track of file transitions.  */
333
334 void
335 cpp_output_tokens (pfile, print)
336      cpp_reader *pfile;
337      cpp_printer *print;
338 {
339   cpp_buffer *ip;
340
341   if (CPP_WRITTEN (pfile) - print->written)
342     {
343       if (CPP_PWRITTEN (pfile)[-1] == '\n' && print->lineno)
344         print->lineno++;
345       safe_fwrite (pfile, pfile->token_buffer,
346                    CPP_WRITTEN (pfile) - print->written, print->outf);
347     }
348
349   ip = cpp_file_buffer (pfile);
350   if (ip)
351     output_line_command (pfile, print, CPP_BUF_LINE (ip));
352
353   CPP_SET_WRITTEN (pfile, print->written);
354 }
355
356 /* Helper for cpp_output_list - increases the column number to match
357    what we expect it to be.  */
358
359 static void
360 bump_column (print, from, to)
361      cpp_printer *print;
362      unsigned int from, to;
363 {
364   unsigned int tabs, spcs;
365   unsigned int delta = to - from;
366
367   /* Only if FROM is 0, advance by tabs.  */
368   if (from == 0)
369     tabs = delta / 8, spcs = delta % 8;
370   else
371     tabs = 0, spcs = delta;
372
373   while (tabs--) putc ('\t', print->outf);
374   while (spcs--) putc (' ', print->outf);
375 }
376
377 /* Write out the list L onto pfile->token_buffer.  This function is
378    incomplete:
379
380    1) pfile->token_buffer is not going to continue to exist.
381    2) At the moment, tokens don't carry the information described
382    in cpplib.h; they are all strings.
383    3) The list has to be a complete line, and has to be written starting
384    at the beginning of a line.  */
385
386 void
387 cpp_output_list (pfile, print, list)
388      cpp_reader *pfile;
389      cpp_printer *print;
390      const cpp_toklist *list;
391 {
392   unsigned int i;
393   unsigned int curcol = 1;
394
395   /* XXX Probably does not do what is intended.  */
396   if (print->lineno != list->line)
397     output_line_command (pfile, print, list->line);
398   
399   for (i = 0; i < list->tokens_used; i++)
400     {
401       if (TOK_TYPE (list, i) == CPP_VSPACE)
402         {
403           output_line_command (pfile, print, list->tokens[i].aux);
404           continue;
405         }
406           
407       if (curcol < TOK_COL (list, i))
408         {
409           /* Insert space to bring the column to what it should be.  */
410           bump_column (print, curcol - 1, TOK_COL (list, i));
411           curcol = TOK_COL (list, i);
412         }
413       /* XXX We may have to insert space to prevent an accidental
414          token paste.  */
415       safe_fwrite (pfile, TOK_NAME (list, i), TOK_LEN (list, i), print->outf);
416       curcol += TOK_LEN (list, i);
417     }
418 }
419
420 /* Scan a string (which may have escape marks), perform macro expansion,
421    and write the result to the token_buffer.  */
422
423 void
424 _cpp_expand_to_buffer (pfile, buf, length)
425      cpp_reader *pfile;
426      const U_CHAR *buf;
427      int length;
428 {
429   cpp_buffer *stop;
430   enum cpp_ttype token;
431   U_CHAR *buf1;
432
433   if (length < 0)
434     {
435       cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
436       return;
437     }
438
439   /* Copy the buffer, because it might be in an unsafe place - for
440      example, a sequence on the token_buffer, where the pointers will
441      be invalidated if we enlarge the token_buffer.  */
442   buf1 = alloca (length);
443   memcpy (buf1, buf, length);
444
445   /* Set up the input on the input stack.  */
446   stop = CPP_BUFFER (pfile);
447   if (cpp_push_buffer (pfile, buf1, length) == NULL)
448     return;
449   CPP_BUFFER (pfile)->has_escapes = 1;
450
451   /* Scan the input, create the output.  */
452   for (;;)
453     {
454       token = cpp_get_token (pfile);
455       if (token == CPP_EOF && CPP_BUFFER (pfile) == stop)
456         break;
457     }
458 }
459
460 /* Scan until CPP_BUFFER (PFILE) is exhausted, discarding output.  */
461
462 void
463 cpp_scan_buffer_nooutput (pfile)
464      cpp_reader *pfile;
465 {
466   cpp_buffer *stop = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
467   enum cpp_ttype token;
468   unsigned int old_written = CPP_WRITTEN (pfile);
469   /* In no-output mode, we can ignore everything but directives.  */
470   for (;;)
471     {
472       if (! pfile->only_seen_white)
473         _cpp_skip_rest_of_line (pfile);
474       token = cpp_get_token (pfile);
475       if (token == CPP_EOF && CPP_BUFFER (pfile) == stop)
476         break;
477     }
478   CPP_SET_WRITTEN (pfile, old_written);
479 }
480
481 /* Scan until CPP_BUFFER (pfile) is exhausted, writing output to PRINT.  */
482
483 void
484 cpp_scan_buffer (pfile, print)
485      cpp_reader *pfile;
486      cpp_printer *print;
487 {
488   cpp_buffer *stop = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
489   enum cpp_ttype token;
490
491   for (;;)
492     {
493       token = cpp_get_token (pfile);
494       if (token == CPP_EOF || token == CPP_VSPACE
495           /* XXX Temporary kluge - force flush after #include only */
496           || (token == CPP_DIRECTIVE
497               && CPP_BUFFER (pfile)->nominal_fname != print->last_fname))
498         {
499           cpp_output_tokens (pfile, print);
500           if (token == CPP_EOF && CPP_BUFFER (pfile) == stop)
501             return;
502         }
503     }
504 }
505
506 /* Return the topmost cpp_buffer that corresponds to a file (not a macro).  */
507
508 cpp_buffer *
509 cpp_file_buffer (pfile)
510      cpp_reader *pfile;
511 {
512   cpp_buffer *ip;
513
514   for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
515     if (ip->ihash != NULL)
516       return ip;
517   return NULL;
518 }
519
520 /* Token-buffer helper functions.  */
521
522 /* Expand a token list's string space.  */
523 static void
524 expand_name_space (list, len)
525      cpp_toklist *list;
526      unsigned int len;
527 {
528   const U_CHAR *old_namebuf;
529   ptrdiff_t delta;
530
531   old_namebuf = list->namebuf;
532   list->name_cap += len;
533   list->namebuf = (unsigned char *) xrealloc (list->namebuf, list->name_cap);
534
535   /* Fix up token text pointers.  */
536   delta = list->namebuf - old_namebuf;
537   if (delta)
538     {
539       unsigned int i;
540
541       for (i = 0; i < list->tokens_used; i++)
542         if (token_spellings[list->tokens[i].type].type > SPELL_NONE)
543           list->tokens[i].val.name.text += delta;
544     }
545 }
546
547 /* Expand the number of tokens in a list.  */
548 static void
549 expand_token_space (list)
550      cpp_toklist *list;
551 {
552   list->tokens_cap *= 2;
553   list->tokens = (cpp_token *)
554     xrealloc (list->tokens - 1, (list->tokens_cap + 1) * sizeof (cpp_token));
555   list->tokens++;               /* Skip the dummy.  */
556 }
557
558 /* Initialize a token list.  We allocate an extra token in front of
559    the token list, as this allows us to always peek at the previous
560    token without worrying about underflowing the list.  */
561 static void
562 init_token_list (pfile, list, recycle)
563      cpp_reader *pfile;
564      cpp_toklist *list;
565      int recycle;
566 {
567   /* Recycling a used list saves 3 free-malloc pairs.  */
568   if (!recycle)
569     {
570       /* Initialize token space.  Put a dummy token before the start
571          that will fail matches.  */
572       list->tokens_cap = 256;   /* 4K's worth.  */
573       list->tokens = (cpp_token *)
574         xmalloc ((list->tokens_cap + 1) * sizeof (cpp_token));
575       list->tokens[0].type = CPP_EOF;
576       list->tokens++;
577
578       /* Initialize name space.  */
579       list->name_cap = 1024;
580       list->namebuf = (unsigned char *) xmalloc (list->name_cap);
581
582       /* Only create a comment space on demand.  */
583       list->comments_cap = 0;
584       list->comments = 0;
585     }
586
587   list->tokens_used = 0;
588   list->name_used = 0;
589   list->comments_used = 0;
590   if (pfile->buffer)
591     list->line = pfile->buffer->lineno;
592   list->dir_handler = 0;
593   list->dir_flags = 0;
594 }
595
596 /* Scan an entire line and create a token list for it.  Does not
597    macro-expand or execute directives.  */
598
599 void
600 _cpp_scan_line (pfile, list)
601      cpp_reader *pfile;
602      cpp_toklist *list;
603 {
604   int i, col;
605   long written, len;
606   enum cpp_ttype type;
607   int space_before;
608
609   init_token_list (pfile, list, 1);
610
611   written = CPP_WRITTEN (pfile);
612   i = 0;
613   space_before = 0;
614   for (;;)
615     {
616       col = CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base;
617       type = _cpp_lex_token (pfile);
618       len = CPP_WRITTEN (pfile) - written;
619       CPP_SET_WRITTEN (pfile, written);
620       if (type == CPP_HSPACE)
621         {
622           if (CPP_PEDANTIC (pfile))
623             pedantic_whitespace (pfile, pfile->token_buffer + written, len);
624           space_before = 1;
625           continue;
626         }
627       else if (type == CPP_COMMENT)
628         /* Only happens when processing -traditional macro definitions.
629            Do not give this a token entry, but do not change space_before
630            either.  */
631         continue;
632
633       if (list->tokens_used >= list->tokens_cap)
634         expand_token_space (list);
635       if (list->name_used + len >= list->name_cap)
636         expand_name_space (list, list->name_used + len + 1 - list->name_cap);
637
638       if (type == CPP_MACRO)
639         type = CPP_NAME;
640
641       list->tokens_used++;
642       TOK_TYPE  (list, i) = type;
643       TOK_COL   (list, i) = col;
644       TOK_FLAGS (list, i) = space_before ? PREV_WHITESPACE : 0;
645       
646       if (type == CPP_VSPACE)
647         break;
648
649       TOK_LEN (list, i) = len;
650       if (token_spellings[type].type > SPELL_NONE)
651         {
652           memcpy (list->namebuf + list->name_used, CPP_PWRITTEN (pfile), len);
653           TOK_NAME (list, i) = list->namebuf + list->name_used;
654           list->name_used += len;
655         }
656       else
657         TOK_NAME (list, i) = token_spellings[type].spelling;
658       i++;
659       space_before = 0;
660     }
661   TOK_AUX (list, i) = CPP_BUFFER (pfile)->lineno + 1;
662
663   /* XXX Temporary kluge: put back the newline.  */
664   FORWARD(-1);
665 }
666
667
668 /* Skip a C-style block comment.  We know it's a comment, and point is
669    at the second character of the starter.  */
670 static void
671 skip_block_comment (pfile)
672      cpp_reader *pfile;
673 {
674   unsigned int line, col;
675   const U_CHAR *limit, *cur;
676
677   FORWARD(1);
678   line = CPP_BUF_LINE (CPP_BUFFER (pfile));
679   col = CPP_BUF_COL (CPP_BUFFER (pfile));
680   limit = CPP_BUFFER (pfile)->rlimit;
681   cur = CPP_BUFFER (pfile)->cur;
682
683   while (cur < limit)
684     {
685       char c = *cur++;
686       if (c == '\n' || c == '\r')
687         {
688           /* \r cannot be a macro escape marker here. */
689           if (!ACTIVE_MARK_P (pfile))
690             CPP_BUMP_LINE_CUR (pfile, cur);
691         }
692       else if (c == '*')
693         {
694           /* Check for teminator.  */
695           if (cur < limit && *cur == '/')
696             goto out;
697
698           /* Warn about comment starter embedded in comment.  */
699           if (cur[-2] == '/' && CPP_OPTION (pfile, warn_comments))
700             cpp_warning_with_line (pfile, CPP_BUFFER (pfile)->lineno,
701                                    cur - CPP_BUFFER (pfile)->line_base,
702                                    "'/*' within comment");
703         }
704     }
705
706   cpp_error_with_line (pfile, line, col, "unterminated comment");
707   cur--;
708  out:
709   CPP_BUFFER (pfile)->cur = cur + 1;
710 }
711
712 /* Skip a C++/Chill line comment.  We know it's a comment, and point
713    is at the second character of the initiator.  */
714 static void
715 skip_line_comment (pfile)
716      cpp_reader *pfile;
717 {
718   FORWARD(1);
719   for (;;)
720     {
721       int c = GETC ();
722
723       /* We don't have to worry about EOF in here.  */
724       if (c == '\n')
725         {
726           /* Don't consider final '\n' to be part of comment.  */
727           FORWARD(-1);
728           return;
729         }
730       else if (c == '\r')
731         {
732           /* \r cannot be a macro escape marker here. */
733           if (!ACTIVE_MARK_P (pfile))
734             CPP_BUMP_LINE (pfile);
735           if (CPP_OPTION (pfile, warn_comments))
736             cpp_warning (pfile, "backslash-newline within line comment");
737         }
738     }
739 }
740
741 /* Skip a comment - C, C++, or Chill style.  M is the first character
742    of the comment marker.  If this really is a comment, skip to its
743    end and return ' '.  If this is not a comment, return M (which will
744    be '/' or '-').  */
745
746 static int
747 skip_comment (pfile, m)
748      cpp_reader *pfile;
749      int m;
750 {
751   if (m == '/' && PEEKC() == '*')
752     {
753       skip_block_comment (pfile);
754       return ' ';
755     }
756   else if (m == '/' && PEEKC() == '/')
757     {
758       if (CPP_BUFFER (pfile)->system_header_p)
759         {
760           /* We silently allow C++ comments in system headers, irrespective
761              of conformance mode, because lots of busted systems do that
762              and trying to clean it up in fixincludes is a nightmare.  */
763           skip_line_comment (pfile);
764           return ' ';
765         }
766       else if (CPP_OPTION (pfile, cplusplus_comments))
767         {
768           if (! CPP_BUFFER (pfile)->warned_cplusplus_comments)
769             {
770               if (CPP_WTRADITIONAL (pfile))
771                 cpp_pedwarn (pfile,
772                         "C++ style comments are not allowed in traditional C");
773               else if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile))
774                 cpp_pedwarn (pfile,
775                         "C++ style comments are not allowed in ISO C89");
776               if (CPP_WTRADITIONAL (pfile)
777                   || (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)))
778                 cpp_pedwarn (pfile,
779                            "(this will be reported only once per input file)");
780               CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
781             }
782           skip_line_comment (pfile);
783           return ' ';
784         }
785       else
786         return m;
787     }
788   else if (m == '-' && PEEKC() == '-'
789            && CPP_OPTION (pfile, chill))
790     {
791       skip_line_comment (pfile);
792       return ' ';
793     }
794   else
795     return m;
796 }
797
798 /* Identical to skip_comment except that it copies the comment into the
799    token_buffer.  This is used if !discard_comments.  */
800 static int
801 copy_comment (pfile, m)
802      cpp_reader *pfile;
803      int m;
804 {
805   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
806   const U_CHAR *limit;
807
808   if (skip_comment (pfile, m) == m)
809     return m;
810
811   limit = CPP_BUFFER (pfile)->cur;
812   CPP_RESERVE (pfile, limit - start + 2);
813   CPP_PUTC_Q (pfile, m);
814   for (; start <= limit; start++)
815     if (*start != '\r')
816       CPP_PUTC_Q (pfile, *start);
817
818   return ' ';
819 }
820
821 static void
822 null_warning (pfile, count)
823      cpp_reader *pfile;
824      unsigned int count;
825 {
826   if (count == 1)
827     cpp_warning (pfile, "embedded null character ignored");
828   else
829     cpp_warning (pfile, "embedded null characters ignored");
830 }
831
832 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
833
834 void
835 _cpp_skip_hspace (pfile)
836      cpp_reader *pfile;
837 {
838   unsigned int null_count = 0;
839   int c;
840
841   while (1)
842     {
843       c = GETC();
844       if (c == EOF)
845         goto out;
846       else if (is_hspace(c))
847         {
848           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
849             cpp_pedwarn (pfile, "%s in preprocessing directive",
850                          c == '\f' ? "formfeed" : "vertical tab");
851           else if (c == '\0')
852             null_count++;
853         }
854       else if (c == '\r')
855         {
856           /* \r is a backslash-newline marker if !has_escapes, and
857              a deletable-whitespace or no-reexpansion marker otherwise. */
858           if (CPP_BUFFER (pfile)->has_escapes)
859             {
860               if (PEEKC() == ' ')
861                 FORWARD(1);
862               else
863                 break;
864             }
865           else
866             CPP_BUMP_LINE (pfile);
867         }
868       else if (c == '/' || c == '-')
869         {
870           c = skip_comment (pfile, c);
871           if (c  != ' ')
872             break;
873         }
874       else
875         break;
876     }
877   FORWARD(-1);
878  out:
879   if (null_count)
880     null_warning (pfile, null_count);
881 }
882
883 /* Read and discard the rest of the current line.  */
884
885 void
886 _cpp_skip_rest_of_line (pfile)
887      cpp_reader *pfile;
888 {
889   for (;;)
890     {
891       int c = GETC();
892       switch (c)
893         {
894         case '\n':
895           FORWARD(-1);
896         case EOF:
897           return;
898
899         case '\r':
900           if (! CPP_BUFFER (pfile)->has_escapes)
901             CPP_BUMP_LINE (pfile);
902           break;
903           
904         case '\'':
905         case '\"':
906           skip_string (pfile, c);
907           break;
908
909         case '/':
910         case '-':
911           skip_comment (pfile, c);
912           break;
913
914         case '\f':
915         case '\v':
916           if (CPP_PEDANTIC (pfile))
917             cpp_pedwarn (pfile, "%s in preprocessing directive",
918                          c == '\f' ? "formfeed" : "vertical tab");
919           break;
920
921         }
922     }
923 }
924
925 /* Parse an identifier starting with C.  */
926
927 void
928 _cpp_parse_name (pfile, c)
929      cpp_reader *pfile;
930      int c;
931 {
932   for (;;)
933   {
934       if (! is_idchar(c))
935       {
936           FORWARD (-1);
937           break;
938       }
939
940       if (c == '$' && CPP_PEDANTIC (pfile))
941         cpp_pedwarn (pfile, "`$' in identifier");
942
943       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
944       CPP_PUTC_Q (pfile, c);
945       c = GETC();
946       if (c == EOF)
947         break;
948   }
949   return;
950 }
951
952 /* Parse and skip over a string starting with C.  A single quoted
953    string is treated like a double -- some programs (e.g., troff) are
954    perverse this way.  (However, a single quoted string is not allowed
955    to extend over multiple lines.)  */
956 static void
957 skip_string (pfile, c)
958      cpp_reader *pfile;
959      int c;
960 {
961   unsigned int start_line, start_column;
962   unsigned int null_count = 0;
963
964   start_line = CPP_BUF_LINE (CPP_BUFFER (pfile));
965   start_column = CPP_BUF_COL (CPP_BUFFER (pfile));
966   while (1)
967     {
968       int cc = GETC();
969       switch (cc)
970         {
971         case EOF:
972           cpp_error_with_line (pfile, start_line, start_column,
973                                "unterminated string or character constant");
974           if (pfile->multiline_string_line != start_line
975               && pfile->multiline_string_line != 0)
976             cpp_error_with_line (pfile,
977                                  pfile->multiline_string_line, -1,
978                          "possible real start of unterminated constant");
979           pfile->multiline_string_line = 0;
980           goto out;
981
982         case '\0':
983           null_count++;
984           break;
985           
986         case '\n':
987           CPP_BUMP_LINE (pfile);
988           /* In Fortran and assembly language, silently terminate
989              strings of either variety at end of line.  This is a
990              kludge around not knowing where comments are in these
991              languages.  */
992           if (CPP_OPTION (pfile, lang_fortran)
993               || CPP_OPTION (pfile, lang_asm))
994             {
995               FORWARD(-1);
996               goto out;
997             }
998           /* Character constants may not extend over multiple lines.
999              In Standard C, neither may strings.  We accept multiline
1000              strings as an extension.  */
1001           if (c == '\'')
1002             {
1003               cpp_error_with_line (pfile, start_line, start_column,
1004                                    "unterminated character constant");
1005               FORWARD(-1);
1006               goto out;
1007             }
1008           if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
1009             cpp_pedwarn_with_line (pfile, start_line, start_column,
1010                                    "string constant runs past end of line");
1011           if (pfile->multiline_string_line == 0)
1012             pfile->multiline_string_line = start_line;
1013           break;
1014
1015         case '\r':
1016           if (CPP_BUFFER (pfile)->has_escapes)
1017             {
1018               cpp_ice (pfile, "\\r escape inside string constant");
1019               FORWARD(1);
1020             }
1021           else
1022             /* Backslash newline is replaced by nothing at all.  */
1023             CPP_BUMP_LINE (pfile);
1024           break;
1025
1026         case '\\':
1027           FORWARD(1);
1028           break;
1029
1030         case '\"':
1031         case '\'':
1032           if (cc == c)
1033             goto out;
1034           break;
1035         }
1036     }
1037
1038  out:
1039   if (null_count == 1)
1040     cpp_warning (pfile, "null character in string or character constant");
1041   else if (null_count > 1)
1042     cpp_warning (pfile, "null characters in string or character constant");
1043 }
1044
1045 /* Parse a string and copy it to the output.  */
1046
1047 static void
1048 parse_string (pfile, c)
1049      cpp_reader *pfile;
1050      int c;
1051 {
1052   const U_CHAR *start = CPP_BUFFER (pfile)->cur;  /* XXX Layering violation */
1053   const U_CHAR *limit;
1054
1055   skip_string (pfile, c);
1056
1057   limit = CPP_BUFFER (pfile)->cur;
1058   CPP_RESERVE (pfile, limit - start + 2);
1059   CPP_PUTC_Q (pfile, c);
1060   for (; start < limit; start++)
1061     if (*start != '\r')
1062       CPP_PUTC_Q (pfile, *start);
1063 }
1064
1065 /* Read an assertion into the token buffer, converting to
1066    canonical form: `#predicate(a n swe r)'  The next non-whitespace
1067    character to read should be the first letter of the predicate.
1068    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
1069    with answer (see callers for why). In case of 0, an error has been
1070    printed. */
1071 int
1072 _cpp_parse_assertion (pfile)
1073      cpp_reader *pfile;
1074 {
1075   int c, dropwhite;
1076   _cpp_skip_hspace (pfile);
1077   c = PEEKC();
1078   if (c == '\n')
1079     {
1080       cpp_error (pfile, "assertion without predicate");
1081       return 0;
1082     }
1083   else if (! is_idstart(c))
1084     {
1085       cpp_error (pfile, "assertion predicate is not an identifier");
1086       return 0;
1087     }
1088   CPP_PUTC(pfile, '#');
1089   FORWARD(1);
1090   _cpp_parse_name (pfile, c);
1091
1092   c = PEEKC();
1093   if (c != '(')
1094     {
1095       if (is_hspace(c) || c == '\r')
1096         _cpp_skip_hspace (pfile);
1097       c = PEEKC();
1098     }
1099   if (c != '(')
1100     return 1;
1101
1102   CPP_PUTC(pfile, '(');
1103   FORWARD(1);
1104   dropwhite = 1;
1105   while ((c = GETC()) != ')')
1106     {
1107       if (is_space(c))
1108         {
1109           if (! dropwhite)
1110             {
1111               CPP_PUTC(pfile, ' ');
1112               dropwhite = 1;
1113             }
1114         }
1115       else if (c == '\n' || c == EOF)
1116         {
1117           if (c == '\n') FORWARD(-1);
1118           cpp_error (pfile, "un-terminated assertion answer");
1119           return 0;
1120         }
1121       else if (c == '\r')
1122         /* \r cannot be a macro escape here. */
1123         CPP_BUMP_LINE (pfile);
1124       else
1125         {
1126           CPP_PUTC (pfile, c);
1127           dropwhite = 0;
1128         }
1129     }
1130
1131   if (pfile->limit[-1] == ' ')
1132     pfile->limit[-1] = ')';
1133   else if (pfile->limit[-1] == '(')
1134     {
1135       cpp_error (pfile, "empty token sequence in assertion");
1136       return 0;
1137     }
1138   else
1139     CPP_PUTC (pfile, ')');
1140
1141   return 2;
1142 }
1143
1144 /* Get the next token, and add it to the text in pfile->token_buffer.
1145    Return the kind of token we got.  */
1146
1147 enum cpp_ttype
1148 _cpp_lex_token (pfile)
1149      cpp_reader *pfile;
1150 {
1151   register int c, c2;
1152   enum cpp_ttype token;
1153
1154   if (CPP_BUFFER (pfile) == NULL)
1155     return CPP_EOF;
1156
1157  get_next:
1158   c = GETC();
1159   switch (c)
1160     {
1161     case EOF:
1162       return CPP_EOF;
1163
1164     case '/':
1165       if (PEEKC () == '=')
1166         goto op2;
1167
1168     comment:
1169       if (CPP_OPTION (pfile, discard_comments))
1170         c = skip_comment (pfile, c);
1171       else
1172         c = copy_comment (pfile, c);
1173       if (c != ' ')
1174         goto randomchar;
1175           
1176       /* Comments are equivalent to spaces.
1177          For -traditional, a comment is equivalent to nothing.  */
1178       if (!CPP_OPTION (pfile, discard_comments))
1179         return CPP_COMMENT;
1180       else if (CPP_TRADITIONAL (pfile))
1181         {
1182           if (pfile->parsing_define_directive)
1183             return CPP_COMMENT;
1184           goto get_next;
1185         }
1186       else
1187         {
1188           CPP_PUTC (pfile, c);
1189           return CPP_HSPACE;
1190         }
1191
1192     case '#':
1193       CPP_PUTC (pfile, c);
1194
1195     hash:
1196       if (pfile->parsing_if_directive)
1197         {
1198           CPP_ADJUST_WRITTEN (pfile, -1);
1199           if (_cpp_parse_assertion (pfile))
1200             return CPP_ASSERTION;
1201           return CPP_OTHER;
1202         }
1203
1204       if (pfile->parsing_define_directive)
1205         {
1206           c2 = PEEKC ();
1207           if (c2 == '#')
1208             {
1209               FORWARD (1);
1210               CPP_PUTC (pfile, c2);
1211             }
1212           else if (c2 == '%' && PEEKN (1) == ':')
1213             {
1214               /* Digraph: "%:" == "#".  */
1215               FORWARD (1);
1216               CPP_RESERVE (pfile, 2);
1217               CPP_PUTC_Q (pfile, c2);
1218               CPP_PUTC_Q (pfile, GETC ());
1219             }
1220           else
1221             return CPP_HASH;
1222
1223           return CPP_PASTE;
1224         }
1225
1226       if (!pfile->only_seen_white)
1227         return CPP_OTHER;
1228
1229       /* Remove the "#" or "%:" from the token buffer.  */
1230       CPP_ADJUST_WRITTEN (pfile, (c == '#' ? -1 : -2));
1231       return CPP_DIRECTIVE;
1232
1233     case '\"':
1234     case '\'':
1235       parse_string (pfile, c);
1236       return c == '\'' ? CPP_CHAR : CPP_STRING;
1237
1238     case '$':
1239       if (!CPP_OPTION (pfile, dollars_in_ident))
1240         goto randomchar;
1241       goto letter;
1242
1243     case ':':
1244       c2 = PEEKC ();
1245       /* Digraph: ":>" == "]".  */
1246       if (c2 == '>'
1247           || (c2 == ':' && CPP_OPTION (pfile, cplusplus)))
1248         goto op2;
1249       goto randomchar;
1250
1251     case '&':
1252     case '+':
1253     case '|':
1254       c2 = PEEKC ();
1255       if (c2 == c || c2 == '=')
1256         goto op2;
1257       goto randomchar;
1258
1259     case '%':
1260       /* Digraphs: "%:" == "#", "%>" == "}".  */
1261       c2 = PEEKC ();
1262       if (c2 == ':')
1263         {
1264           FORWARD (1);
1265           CPP_RESERVE (pfile, 2);
1266           CPP_PUTC_Q (pfile, c);
1267           CPP_PUTC_Q (pfile, c2);
1268           goto hash;
1269         }
1270       else if (c2 == '>')
1271         {
1272           FORWARD (1);
1273           CPP_RESERVE (pfile, 2);
1274           CPP_PUTC_Q (pfile, c);
1275           CPP_PUTC_Q (pfile, c2);
1276           return CPP_OPEN_BRACE;
1277         }
1278       /* else fall through */
1279
1280     case '*':
1281     case '!':
1282     case '=':
1283     case '^':
1284       if (PEEKC () == '=')
1285         goto op2;
1286       goto randomchar;
1287
1288     case '-':
1289       c2 = PEEKC ();
1290       if (c2 == '-')
1291         {
1292           if (CPP_OPTION (pfile, chill))
1293             goto comment;  /* Chill style comment */
1294           else
1295             goto op2;
1296         }
1297       else if (c2 == '=')
1298         goto op2;
1299       else if (c2 == '>')
1300         {
1301           if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
1302             {
1303               /* In C++, there's a ->* operator.  */
1304               token = CPP_OTHER;
1305               CPP_RESERVE (pfile, 4);
1306               CPP_PUTC_Q (pfile, c);
1307               CPP_PUTC_Q (pfile, GETC ());
1308               CPP_PUTC_Q (pfile, GETC ());
1309               return token;
1310             }
1311           goto op2;
1312         }
1313       goto randomchar;
1314
1315     case '<':
1316       if (pfile->parsing_include_directive)
1317         {
1318           for (;;)
1319             {
1320               CPP_PUTC (pfile, c);
1321               if (c == '>')
1322                 break;
1323               c = GETC ();
1324               if (c == '\n' || c == EOF)
1325                 {
1326                   cpp_error (pfile,
1327                              "missing '>' in `#include <FILENAME>'");
1328                   break;
1329                 }
1330               else if (c == '\r')
1331                 {
1332                   if (!CPP_BUFFER (pfile)->has_escapes)
1333                     {
1334                       /* Backslash newline is replaced by nothing. */
1335                       CPP_ADJUST_WRITTEN (pfile, -1);
1336                       CPP_BUMP_LINE (pfile);
1337                     }
1338                   else
1339                     {
1340                       /* We might conceivably get \r- or \r<space> in
1341                          here.  Just delete 'em. */
1342                       int d = GETC();
1343                       if (d != '-' && d != ' ')
1344                         cpp_ice (pfile, "unrecognized escape \\r%c", d);
1345                       CPP_ADJUST_WRITTEN (pfile, -1);
1346                     }                     
1347                 }
1348             }
1349           return CPP_STRING;
1350         }
1351       /* Digraphs: "<%" == "{", "<:" == "[".  */
1352       c2 = PEEKC ();
1353       if (c2 == '%')
1354         {
1355           FORWARD (1);
1356           CPP_RESERVE (pfile, 2);
1357           CPP_PUTC_Q (pfile, c);
1358           CPP_PUTC_Q (pfile, c2);
1359           return CPP_CLOSE_BRACE;
1360         }
1361       else if (c2 == ':')
1362         goto op2;
1363       /* else fall through */
1364     case '>':
1365       c2 = PEEKC ();
1366       if (c2 == '=')
1367         goto op2;
1368       /* GNU C++ supports MIN and MAX operators <? and >?.  */
1369       if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
1370         goto randomchar;
1371       FORWARD(1);
1372       CPP_RESERVE (pfile, 3);
1373       CPP_PUTC_Q (pfile, c);
1374       CPP_PUTC_Q (pfile, c2);
1375       if (PEEKC () == '=')
1376         CPP_PUTC_Q (pfile, GETC ());
1377       return CPP_OTHER;
1378
1379     case '.':
1380       c2 = PEEKC ();
1381       if (ISDIGIT (c2))
1382         {
1383           CPP_PUTC (pfile, c);
1384           c = GETC ();
1385           goto number;
1386         }
1387
1388       /* In C++ there's a .* operator.  */
1389       if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
1390         goto op2;
1391
1392       if (c2 == '.' && PEEKN(1) == '.')
1393         {
1394           CPP_RESERVE (pfile, 3);
1395           CPP_PUTC_Q (pfile, '.');
1396           CPP_PUTC_Q (pfile, '.');
1397           CPP_PUTC_Q (pfile, '.');
1398           FORWARD (2);
1399           return CPP_ELLIPSIS;
1400         }
1401       goto randomchar;
1402
1403     op2:
1404       CPP_RESERVE (pfile, 2);
1405       CPP_PUTC_Q (pfile, c);
1406       CPP_PUTC_Q (pfile, GETC ());
1407       return CPP_OTHER;
1408
1409     case 'L':
1410       c2 = PEEKC ();
1411       if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
1412         {
1413           CPP_PUTC (pfile, c);
1414           c = GETC ();
1415           parse_string (pfile, c);
1416           return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
1417         }
1418       goto letter;
1419
1420     case '0': case '1': case '2': case '3': case '4':
1421     case '5': case '6': case '7': case '8': case '9':
1422     number:
1423     c2  = '.';
1424     for (;;)
1425       {
1426         CPP_RESERVE (pfile, 2);
1427         CPP_PUTC_Q (pfile, c);
1428         c = PEEKC ();
1429         if (c == EOF)
1430           break;
1431         if (!is_numchar(c) && c != '.'
1432             && ((c2 != 'e' && c2 != 'E'
1433                  && ((c2 != 'p' && c2 != 'P')
1434                      || CPP_OPTION (pfile, c89)))
1435                 || (c != '+' && c != '-')))
1436           break;
1437         FORWARD(1);
1438         c2= c;
1439       }
1440     return CPP_NUMBER;
1441     case 'b': case 'c': case 'd': case 'h': case 'o':
1442     case 'B': case 'C': case 'D': case 'H': case 'O':
1443       if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
1444         {
1445           CPP_RESERVE (pfile, 2);
1446           CPP_PUTC_Q (pfile, c);
1447           CPP_PUTC_Q (pfile, '\'');
1448           FORWARD(1);
1449           for (;;)
1450             {
1451               c = GETC();
1452               if (c == EOF)
1453                 goto chill_number_eof;
1454               if (!is_numchar(c))
1455                 break;
1456               CPP_PUTC (pfile, c);
1457             }
1458           if (c == '\'')
1459             {
1460               CPP_RESERVE (pfile, 2);
1461               CPP_PUTC_Q (pfile, c);
1462               return CPP_STRING;
1463             }
1464           else
1465             {
1466               FORWARD(-1);
1467             chill_number_eof:
1468               return CPP_NUMBER;
1469             }
1470         }
1471       else
1472         goto letter;
1473     case '_':
1474     case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
1475     case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
1476     case 'r': case 's': case 't': case 'u': case 'v': case 'w':
1477     case 'x': case 'y': case 'z':
1478     case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
1479     case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
1480     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1481     case 'Y': case 'Z':
1482     letter:
1483     _cpp_parse_name (pfile, c);
1484     return CPP_MACRO;
1485
1486     case ' ':  case '\t':  case '\v': case '\f': case '\0':
1487       {
1488         int null_count = 0;
1489
1490         for (;;)
1491           {
1492             if (c == '\0')
1493               null_count++;
1494             else
1495               CPP_PUTC (pfile, c);
1496             c = PEEKC ();
1497             if (c == EOF || !is_hspace(c))
1498               break;
1499             FORWARD(1);
1500           }
1501         if (null_count)
1502           null_warning (pfile, null_count);
1503         return CPP_HSPACE;
1504       }
1505
1506     case '\r':
1507       if (CPP_BUFFER (pfile)->has_escapes)
1508         {
1509           c = GETC ();
1510           if (c == '-')
1511             {
1512               if (pfile->output_escapes)
1513                 CPP_PUTS (pfile, "\r-", 2);
1514               _cpp_parse_name (pfile, GETC ());
1515               return CPP_NAME;
1516             }
1517           else if (c == ' ')
1518             {
1519               /* "\r " means a space, but only if necessary to prevent
1520                  accidental token concatenation.  */
1521               CPP_RESERVE (pfile, 2);
1522               if (pfile->output_escapes)
1523                 CPP_PUTC_Q (pfile, '\r');
1524               CPP_PUTC_Q (pfile, c);
1525               return CPP_HSPACE;
1526             }
1527           else
1528             {
1529               cpp_ice (pfile, "unrecognized escape \\r%c", c);
1530               goto get_next;
1531             }
1532         }
1533       else
1534         {
1535           /* Backslash newline is ignored. */
1536           if (!ACTIVE_MARK_P (pfile))
1537             CPP_BUMP_LINE (pfile);
1538           goto get_next;
1539         }
1540
1541     case '\n':
1542       CPP_PUTC (pfile, c);
1543       return CPP_VSPACE;
1544
1545     case '(': token = CPP_OPEN_PAREN;  goto char1;
1546     case ')': token = CPP_CLOSE_PAREN; goto char1;
1547     case '{': token = CPP_OPEN_BRACE;  goto char1;
1548     case '}': token = CPP_CLOSE_BRACE; goto char1;
1549     case ',': token = CPP_COMMA;       goto char1;
1550     case ';': token = CPP_SEMICOLON;   goto char1;
1551
1552     randomchar:
1553     default:
1554       token = CPP_OTHER;
1555     char1:
1556       CPP_PUTC (pfile, c);
1557       return token;
1558     }
1559 }
1560
1561 /* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
1562    Caller is expected to have checked no_macro_expand.  */
1563 static int
1564 maybe_macroexpand (pfile, written)
1565      cpp_reader *pfile;
1566      long written;
1567 {
1568   U_CHAR *macro = pfile->token_buffer + written;
1569   size_t len = CPP_WRITTEN (pfile) - written;
1570   HASHNODE *hp = _cpp_lookup (pfile, macro, len);
1571
1572   /* _cpp_lookup never returns null.  */
1573   if (hp->type == T_VOID)
1574     return 0;
1575   if (hp->disabled || hp->type == T_IDENTITY)
1576     {
1577       if (pfile->output_escapes)
1578         {
1579           /* Insert a no-reexpand marker before IDENT.  */
1580           CPP_RESERVE (pfile, 2);
1581           CPP_ADJUST_WRITTEN (pfile, 2);
1582           macro = pfile->token_buffer + written;
1583
1584           memmove (macro + 2, macro, len);
1585           macro[0] = '\r';
1586           macro[1] = '-';
1587         }
1588       return 0;
1589     }
1590   if (hp->type == T_EMPTY)
1591     {
1592       /* Special case optimization: macro expands to nothing.  */
1593       CPP_SET_WRITTEN (pfile, written);
1594       CPP_PUTC_Q (pfile, ' ');
1595       return 1;
1596     }
1597
1598   /* If macro wants an arglist, verify that a '(' follows.  */
1599   if (hp->type == T_FMACRO)
1600     {
1601       int macbuf_whitespace = 0;
1602       int c;
1603
1604       while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1605         {
1606           const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1607           for (;;)
1608             {
1609               _cpp_skip_hspace (pfile);
1610               c = PEEKC ();
1611               if (c == '\n')
1612                 FORWARD(1);
1613               else
1614                 break;
1615             }
1616           if (point != CPP_BUFFER (pfile)->cur)
1617             macbuf_whitespace = 1;
1618           if (c == '(')
1619             goto is_macro_call;
1620           else if (c != EOF)
1621             goto not_macro_call;
1622           cpp_pop_buffer (pfile);
1623         }
1624
1625       CPP_SET_MARK (pfile);
1626       for (;;)
1627         {
1628           _cpp_skip_hspace (pfile);
1629           c = PEEKC ();
1630           if (c == '\n')
1631             FORWARD(1);
1632           else
1633             break;
1634         }
1635       CPP_GOTO_MARK (pfile);
1636
1637       if (c != '(')
1638         {
1639         not_macro_call:
1640           if (macbuf_whitespace)
1641             CPP_PUTC (pfile, ' ');
1642           return 0;
1643         }
1644     }
1645
1646  is_macro_call:
1647   /* This is now known to be a macro call.
1648      Expand the macro, reading arguments as needed,
1649      and push the expansion on the input stack.  */
1650   _cpp_macroexpand (pfile, hp);
1651   CPP_SET_WRITTEN (pfile, written);
1652   return 1;
1653 }
1654
1655 /* Complain about \v or \f in a preprocessing directive (constraint
1656    violation, C99 6.10 para 5).  Caller has checked CPP_PEDANTIC.  */
1657 static void
1658 pedantic_whitespace (pfile, p, len)
1659      cpp_reader *pfile;
1660      U_CHAR *p;
1661      unsigned int len;
1662 {
1663   while (len)
1664     {
1665       if (*p == '\v')
1666         cpp_pedwarn (pfile, "vertical tab in preprocessing directive");
1667       else if (*p == '\f')
1668         cpp_pedwarn (pfile, "form feed in preprocessing directive");
1669       p++;
1670       len--;
1671     }
1672 }
1673
1674
1675 enum cpp_ttype
1676 cpp_get_token (pfile)
1677      cpp_reader *pfile;
1678 {
1679   enum cpp_ttype token;
1680   long written = CPP_WRITTEN (pfile);
1681
1682  get_next:
1683   token = _cpp_lex_token (pfile);
1684
1685   switch (token)
1686     {
1687     default:
1688       pfile->potential_control_macro = 0;
1689       pfile->only_seen_white = 0;
1690       return token;
1691
1692     case CPP_VSPACE:
1693       if (pfile->only_seen_white == 0)
1694         pfile->only_seen_white = 1;
1695       CPP_BUMP_LINE (pfile);
1696       return token;
1697
1698     case CPP_HSPACE:
1699     case CPP_COMMENT:
1700       return token;
1701
1702     case CPP_DIRECTIVE:
1703       pfile->potential_control_macro = 0;
1704       if (_cpp_handle_directive (pfile))
1705         return CPP_DIRECTIVE;
1706       pfile->only_seen_white = 0;
1707       CPP_PUTC (pfile, '#');
1708       return CPP_OTHER;
1709
1710     case CPP_MACRO:
1711       pfile->potential_control_macro = 0;
1712       pfile->only_seen_white = 0;
1713       if (! pfile->no_macro_expand
1714           && maybe_macroexpand (pfile, written))
1715         goto get_next;
1716       return CPP_NAME;
1717
1718     case CPP_EOF:
1719       if (CPP_BUFFER (pfile) == NULL)
1720         return CPP_EOF;
1721       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1722         {
1723           cpp_pop_buffer (pfile);
1724           goto get_next;
1725         }
1726       cpp_pop_buffer (pfile);
1727       return CPP_EOF;
1728     }
1729 }
1730
1731 /* Like cpp_get_token, but skip spaces and comments.  */
1732
1733 enum cpp_ttype
1734 cpp_get_non_space_token (pfile)
1735      cpp_reader *pfile;
1736 {
1737   int old_written = CPP_WRITTEN (pfile);
1738   for (;;)
1739     {
1740       enum cpp_ttype token = cpp_get_token (pfile);
1741       if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
1742         return token;
1743       CPP_SET_WRITTEN (pfile, old_written);
1744     }
1745 }
1746
1747 /* Like cpp_get_token, except that it does not execute directives,
1748    does not consume vertical space, and discards horizontal space.  */
1749 enum cpp_ttype
1750 _cpp_get_directive_token (pfile)
1751      cpp_reader *pfile;
1752 {
1753   long old_written;
1754   enum cpp_ttype token;
1755   int at_bol;
1756
1757  get_next:
1758   at_bol = (CPP_BUFFER (pfile)->cur == CPP_BUFFER (pfile)->line_base);
1759   old_written = CPP_WRITTEN (pfile);
1760   token = _cpp_lex_token (pfile);
1761   switch (token)
1762     {
1763     default:
1764       return token;
1765
1766     case CPP_VSPACE:
1767       /* Put it back and return VSPACE.  */
1768       FORWARD(-1);
1769       CPP_ADJUST_WRITTEN (pfile, -1);
1770       return CPP_VSPACE;
1771
1772     case CPP_HSPACE:
1773       /* The purpose of this rather strange check is to prevent pedantic
1774          warnings for ^L in an #ifdefed out block.  */
1775       if (CPP_PEDANTIC (pfile) && ! at_bol)
1776         pedantic_whitespace (pfile, pfile->token_buffer + old_written,
1777                              CPP_WRITTEN (pfile) - old_written);
1778       CPP_SET_WRITTEN (pfile, old_written);
1779       goto get_next;
1780       return CPP_HSPACE;
1781
1782     case CPP_DIRECTIVE:
1783       /* Don't execute the directive, but don't smash it to OTHER either.  */
1784       CPP_PUTC (pfile, '#');
1785       return CPP_DIRECTIVE;
1786
1787     case CPP_MACRO:
1788       if (! pfile->no_macro_expand
1789           && maybe_macroexpand (pfile, old_written))
1790         goto get_next;
1791       return CPP_NAME;
1792
1793     case CPP_EOF:
1794       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1795         {
1796           cpp_pop_buffer (pfile);
1797           goto get_next;
1798         }
1799       else
1800         /* This can happen for files that don't end with a newline,
1801            and for cpp_define and friends.  Pretend they do, so
1802            callers don't have to deal.  A warning will be issued by
1803            someone else, if necessary.  */
1804         return CPP_VSPACE;
1805     }
1806 }
1807
1808 /* Determine the current line and column.  Used only by read_and_prescan. */
1809 static U_CHAR *
1810 find_position (start, limit, linep)
1811      U_CHAR *start;
1812      U_CHAR *limit;
1813      unsigned long *linep;
1814 {
1815   unsigned long line = *linep;
1816   U_CHAR *lbase = start;
1817   while (start < limit)
1818     {
1819       U_CHAR ch = *start++;
1820       if (ch == '\n' || ch == '\r')
1821         {
1822           line++;
1823           lbase = start;
1824         }
1825     }
1826   *linep = line;
1827   return lbase;
1828 }
1829
1830 /* The following table is used by _cpp_read_and_prescan.  If we have
1831    designated initializers, it can be constant data; otherwise, it is
1832    set up at runtime by _cpp_init_input_buffer.  */
1833
1834 #ifndef UCHAR_MAX
1835 #define UCHAR_MAX 255   /* assume 8-bit bytes */
1836 #endif
1837
1838 #if (GCC_VERSION >= 2007)
1839 #define init_chartab()  /* nothing */
1840 #define CHARTAB __extension__ static const U_CHAR chartab[UCHAR_MAX + 1] = {
1841 #define END };
1842 #define s(p, v) [p] = v,
1843 #else
1844 #define CHARTAB static U_CHAR chartab[UCHAR_MAX + 1] = { 0 }; \
1845  static void init_chartab PARAMS ((void)) { \
1846  unsigned char *x = chartab;
1847 #define END }
1848 #define s(p, v) x[p] = v;
1849 #endif
1850
1851 /* Table of characters that can't be handled in the inner loop.
1852    Also contains the mapping between trigraph third characters and their
1853    replacements.  */
1854 #define SPECCASE_CR        1
1855 #define SPECCASE_BACKSLASH 2
1856 #define SPECCASE_QUESTION  3
1857  
1858 CHARTAB
1859   s('\r', SPECCASE_CR)
1860   s('\\', SPECCASE_BACKSLASH)
1861   s('?',  SPECCASE_QUESTION)
1862
1863   s('=', '#')   s(')', ']')     s('!', '|')
1864   s('(', '[')   s('\'', '^')    s('>', '}')
1865   s('/', '\\')  s('<', '{')     s('-', '~')
1866 END
1867
1868 #undef CHARTAB
1869 #undef END
1870 #undef s
1871
1872 #define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
1873 #define NONTRI(c) ((c) <= SPECCASE_QUESTION)
1874
1875 /* Read the entire contents of file DESC into buffer BUF.  LEN is how
1876    much memory to allocate initially; more will be allocated if
1877    necessary.  Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1878    canonical form (\n).  If enabled, convert and/or warn about
1879    trigraphs.  Convert backslash-newline to a one-character escape
1880    (\r) and remove it from "embarrassing" places (i.e. the middle of a
1881    token).  If there is no newline at the end of the file, add one and
1882    warn.  Returns -1 on failure, or the actual length of the data to
1883    be scanned.
1884
1885    This function does a lot of work, and can be a serious performance
1886    bottleneck.  It has been tuned heavily; make sure you understand it
1887    before hacking.  The common case - no trigraphs, Unix style line
1888    breaks, backslash-newline set off by whitespace, newline at EOF -
1889    has been optimized at the expense of the others.  The performance
1890    penalty for DOS style line breaks (\r\n) is about 15%.
1891    
1892    Warnings lose particularly heavily since we have to determine the
1893    line number, which involves scanning from the beginning of the file
1894    or from the last warning.  The penalty for the absence of a newline
1895    at the end of reload1.c is about 60%.  (reload1.c is 329k.)
1896
1897    If your file has more than one kind of end-of-line marker, you
1898    will get messed-up line numbering.
1899    
1900    So that the cases of the switch statement do not have to concern
1901    themselves with the complications of reading beyond the end of the
1902    buffer, the buffer is guaranteed to have at least 3 characters in
1903    it (or however many are left in the file, if less) on entry to the
1904    switch.  This is enough to handle trigraphs and the "\\\n\r" and
1905    "\\\r\n" cases.
1906    
1907    The end of the buffer is marked by a '\\', which, being a special
1908    character, guarantees we will exit the fast-scan loops and perform
1909    a refill. */
1910  
1911 long
1912 _cpp_read_and_prescan (pfile, fp, desc, len)
1913      cpp_reader *pfile;
1914      cpp_buffer *fp;
1915      int desc;
1916      size_t len;
1917 {
1918   U_CHAR *buf = (U_CHAR *) xmalloc (len);
1919   U_CHAR *ip, *op, *line_base;
1920   U_CHAR *ibase;
1921   unsigned long line;
1922   unsigned int deferred_newlines;
1923   size_t offset;
1924   int count = 0;
1925
1926   offset = 0;
1927   deferred_newlines = 0;
1928   op = buf;
1929   line_base = buf;
1930   line = 1;
1931   ibase = pfile->input_buffer + 3;
1932   ip = ibase;
1933   ip[-1] = '\0';  /* Guarantee no match with \n for SPECCASE_CR */
1934
1935   for (;;)
1936     {
1937       U_CHAR *near_buff_end;
1938
1939       count = read (desc, ibase, pfile->input_buffer_len);
1940       if (count < 0)
1941         goto error;
1942       
1943       ibase[count] = '\\';  /* Marks end of buffer */
1944       if (count)
1945         {
1946           near_buff_end = pfile->input_buffer + count;
1947           offset += count;
1948           if (offset > len)
1949             {
1950               size_t delta_op;
1951               size_t delta_line_base;
1952               len = offset * 2;
1953               if (offset > len)
1954                 /* len overflowed.
1955                    This could happen if the file is larger than half the
1956                    maximum address space of the machine. */
1957                 goto too_big;
1958
1959               delta_op = op - buf;
1960               delta_line_base = line_base - buf;
1961               buf = (U_CHAR *) xrealloc (buf, len);
1962               op = buf + delta_op;
1963               line_base = buf + delta_line_base;
1964             }
1965         }
1966       else
1967         {
1968           if (ip == ibase)
1969             break;
1970           /* Allow normal processing of the (at most 2) remaining
1971              characters.  The end-of-buffer marker is still present
1972              and prevents false matches within the switch. */
1973           near_buff_end = ibase - 1;
1974         }
1975
1976       for (;;)
1977         {
1978           unsigned int span;
1979
1980           /* Deal with \-newline, potentially in the middle of a token. */
1981           if (deferred_newlines)
1982             {
1983               if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
1984                 {
1985                   /* Previous was not white space.  Skip to white
1986                      space, if we can, before outputting the \r's */
1987                   span = 0;
1988                   while (ip[span] != ' '
1989                          && ip[span] != '\t'
1990                          && ip[span] != '\n'
1991                          && NORMAL(ip[span]))
1992                     span++;
1993                   memcpy (op, ip, span);
1994                   op += span;
1995                   ip += span;
1996                   if (! NORMAL(ip[0]))
1997                     goto do_speccase;
1998                 }
1999               while (deferred_newlines)
2000                 deferred_newlines--, *op++ = '\r';
2001             }
2002
2003           /* Copy as much as we can without special treatment. */
2004           span = 0;
2005           while (NORMAL (ip[span])) span++;
2006           memcpy (op, ip, span);
2007           op += span;
2008           ip += span;
2009
2010         do_speccase:
2011           if (ip > near_buff_end) /* Do we have enough chars? */
2012             break;
2013           switch (chartab[*ip++])
2014             {
2015             case SPECCASE_CR:  /* \r */
2016               if (ip[-2] != '\n')
2017                 {
2018                   if (*ip == '\n')
2019                     ip++;
2020                   *op++ = '\n';
2021                 }
2022               break;
2023
2024             case SPECCASE_BACKSLASH:  /* \ */
2025               if (*ip == '\n')
2026                 {
2027                   deferred_newlines++;
2028                   ip++;
2029                   if (*ip == '\r') ip++;
2030                 }
2031               else if (*ip == '\r')
2032                 {
2033                   deferred_newlines++;
2034                   ip++;
2035                   if (*ip == '\n') ip++;
2036                 }
2037               else
2038                 *op++ = '\\';
2039               break;
2040
2041             case SPECCASE_QUESTION: /* ? */
2042               {
2043                 unsigned int d, t;
2044
2045                 *op++ = '?'; /* Normal non-trigraph case */
2046                 if (ip[0] != '?')
2047                   break;
2048                     
2049                 d = ip[1];
2050                 t = chartab[d];
2051                 if (NONTRI (t))
2052                   break;
2053
2054                 if (CPP_OPTION (pfile, warn_trigraphs))
2055                   {
2056                     unsigned long col;
2057                     line_base = find_position (line_base, op, &line);
2058                     col = op - line_base + 1;
2059                     if (CPP_OPTION (pfile, trigraphs))
2060                       cpp_warning_with_line (pfile, line, col,
2061                                              "trigraph ??%c converted to %c", d, t);
2062                     else
2063                       cpp_warning_with_line (pfile, line, col,
2064                                              "trigraph ??%c ignored", d);
2065                   }
2066
2067                 ip += 2;
2068                 if (CPP_OPTION (pfile, trigraphs))
2069                   {
2070                     op[-1] = t;     /* Overwrite '?' */
2071                     if (t == '\\')
2072                       {
2073                         op--;
2074                         *--ip = '\\';
2075                         goto do_speccase; /* May need buffer refill */
2076                       }
2077                   }
2078                 else
2079                   {
2080                     *op++ = '?';
2081                     *op++ = d;
2082                   }
2083               }
2084               break;
2085             }
2086         }
2087       /* Copy previous char plus unprocessed (at most 2) chars
2088          to beginning of buffer, refill it with another
2089          read(), and continue processing */
2090       memmove (ip - count - 1, ip - 1, 4 - (ip - near_buff_end));
2091       ip -= count;
2092     }
2093
2094   if (offset == 0)
2095     return 0;
2096
2097   if (op[-1] != '\n')
2098     {
2099       unsigned long col;
2100       line_base = find_position (line_base, op, &line);
2101       col = op - line_base + 1;
2102       cpp_warning_with_line (pfile, line, col, "no newline at end of file");
2103       if (offset + 1 > len)
2104         {
2105           len += 1;
2106           if (offset + 1 > len)
2107             goto too_big;
2108           buf = (U_CHAR *) xrealloc (buf, len);
2109           op = buf + offset;
2110         }
2111       *op++ = '\n';
2112     }
2113
2114   fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
2115   return op - buf;
2116
2117  too_big:
2118   cpp_notice (pfile, "%s is too large (>%lu bytes)", fp->ihash->name,
2119               (unsigned long)offset);
2120   free (buf);
2121   return -1;
2122
2123  error:
2124   cpp_error_from_errno (pfile, fp->ihash->name);
2125   free (buf);
2126   return -1;
2127 }
2128
2129 /* Allocate pfile->input_buffer, and initialize chartab[]
2130    if it hasn't happened already.  */
2131  
2132 void
2133 _cpp_init_input_buffer (pfile)
2134      cpp_reader *pfile;
2135 {
2136   U_CHAR *tmp;
2137
2138   init_chartab ();
2139   init_token_list (pfile, &pfile->directbuf, 0);
2140
2141   /* Determine the appropriate size for the input buffer.  Normal C
2142      source files are smaller than eight K.  */
2143   /* 8Kbytes of buffer proper, 1 to detect running off the end without
2144      address arithmetic all the time, and 3 for pushback during buffer
2145      refill, in case there's a potential trigraph or end-of-line
2146      digraph at the end of a block. */
2147
2148   tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
2149   pfile->input_buffer = tmp;
2150   pfile->input_buffer_len = 8192;
2151 }
2152
2153 /* Utility routine:
2154    Compares, in the manner of strcmp(3), the token beginning at TOKEN
2155    and extending for LEN characters to the NUL-terminated string
2156    STRING.  Typical usage:
2157
2158    if (! cpp_idcmp (pfile->token_buffer + here, CPP_WRITTEN (pfile) - here,
2159                  "inline"))
2160      { ... }
2161  */
2162
2163 int
2164 cpp_idcmp (token, len, string)
2165      const U_CHAR *token;
2166      size_t len;
2167      const char *string;
2168 {
2169   size_t len2 = strlen (string);
2170   int r;
2171
2172   if ((r = memcmp (token, string, MIN (len, len2))))
2173     return r;
2174
2175   /* The longer of the two strings sorts after the shorter.  */
2176   if (len == len2)
2177     return 0;
2178   else if (len < len2)
2179     return -1;
2180   else
2181     return 1;
2182 }
2183
2184 #ifdef NEW_LEXER
2185
2186 /* Lexing algorithm.
2187
2188  The original lexer in cpplib was made up of two passes: a first pass
2189  that replaced trigraphs and deleted esacped newlines, and a second
2190  pass that tokenized the result of the first pass.  Tokenisation was
2191  performed by peeking at the next character in the input stream.  For
2192  example, if the input stream contained "!=", the handler for the !
2193  character would peek at the next character, and if it were a '='
2194  would skip over it, and return a "!=" token, otherwise it would
2195  return just the "!" token.
2196
2197  To implement a single-pass lexer, this peeking ahead is unworkable.
2198  An arbitrary number of escaped newlines, and trigraphs (in particular
2199  ??/ which translates to the escape \), could separate the '!' and '='
2200  in the input stream, yet the next token is still a "!=".
2201
2202  Suppose instead that we lex by one logical line at a time, producing
2203  a token list or stack for each logical line, and when seeing the '!'
2204  push a CPP_NOT token on the list.  Then if the '!' is part of a
2205  longer token ("!=") we know we must see the remainder of the token by
2206  the time we reach the end of the logical line.  Thus we can have the
2207  '=' handler look at the previous token (at the end of the list / top
2208  of the stack) and see if it is a "!" token, and if so, instead of
2209  pushing a "=" token revise the existing token to be a "!=" token.
2210
2211  This works in the presence of escaped newlines, because the '\' would
2212  have been pushed on the top of the stack as a CPP_BACKSLASH.  The
2213  newline ('\n' or '\r') handler looks at the token at the top of the
2214  stack to see if it is a CPP_BACKSLASH, and if so discards both.
2215  Otherwise it pushes the newline (CPP_VSPACE) token as normal.  Hence
2216  the '=' handler would never see any intervening escaped newlines.
2217
2218  To make trigraphs work in this context, as in precedence trigraphs
2219  are highest and converted before anything else, the '?' handler does
2220  lookahead to see if it is a trigraph, and if so skips the trigraph
2221  and pushes the token it represents onto the top of the stack.  This
2222  also works in the particular case of a CPP_BACKSLASH trigraph.
2223
2224  To the preprocessor, whitespace is only significant to the point of
2225  knowing whether whitespace precedes a particular token.  For example,
2226  the '=' handler needs to know whether there was whitespace between it
2227  and a "!" token on the top of the stack, to make the token conversion
2228  decision correctly.  So each token has a PREV_WHITESPACE flag to
2229  indicate this - the standard permits consecutive whitespace to be
2230  regarded as a single space.  The compiler front ends are not
2231  interested in whitespace at all; they just require a token stream.
2232  Another place where whitespace is significant to the preprocessor is
2233  a #define statment - if there is whitespace between the macro name
2234  and an initial "(" token the macro is "object-like", otherwise it is
2235  a function-like macro that takes arguments.
2236
2237  However, all is not rosy.  Parsing of identifiers, numbers, comments
2238  and strings becomes trickier because of the possibility of raw
2239  trigraphs and escaped newlines in the input stream.
2240
2241  The trigraphs are three consecutive characters beginning with two
2242  question marks.  A question mark is not valid as part of a number or
2243  identifier, so parsing of a number or identifier terminates normally
2244  upon reaching it, returning to the mainloop which handles the
2245  trigraph just like it would in any other position.  Similarly for the
2246  backslash of a backslash-newline combination.  So we just need the
2247  escaped-newline dropper in the mainloop to check if the token on the
2248  top of the stack after dropping the escaped newline is a number or
2249  identifier, and if so to continue the processing it as if nothing had
2250  happened.
2251
2252  For strings, we replace trigraphs whenever we reach a quote or
2253  newline, because there might be a backslash trigraph escaping them.
2254  We need to be careful that we start trigraph replacing from where we
2255  left off previously, because it is possible for a first scan to leave
2256  "fake" trigraphs that a second scan would pick up as real (e.g. the
2257  sequence "????/\n=" would find a fake ??= trigraph after removing the
2258  escaped newline.)
2259
2260  For line comments, on reaching a newline we scan the previous
2261  character(s) to see if it escaped, and continue if it is.  Block
2262  comments ignore everything and just focus on finding the comment
2263  termination mark.  The only difficult thing, and it is surprisingly
2264  tricky, is checking if an asterisk precedes the final slash since
2265  they could be separated by escaped newlines.  If the preprocessor is
2266  invoked with the output comments option, we don't bother removing
2267  escaped newlines and replacing trigraphs for output.
2268
2269  Finally, numbers can begin with a period, which is pushed initially
2270  as a CPP_DOT token in its own right.  The digit handler checks if the
2271  previous token was a CPP_DOT not separated by whitespace, and if so
2272  pops it off the stack and pushes a period into the number's buffer
2273  before calling the number parser.
2274
2275 */
2276
2277 static const unsigned char *digraph_spellings [] = {U"%:", U"%:%:", U"<:",
2278                                                     U":>", U"<%", U"%>"};
2279 static unsigned char trigraph_map[256];
2280
2281 static void
2282 expand_comment_space (list)
2283      cpp_toklist *list;
2284 {
2285   if (list->comments_cap == 0)
2286     {
2287       list->comments_cap = 10;
2288       list->comments = (cpp_token *)
2289         xmalloc (list->comments_cap * sizeof (cpp_token));
2290     }
2291   else
2292     {
2293       list->comments_cap *= 2;
2294       list->comments = (cpp_token *)
2295         xrealloc (list->comments, list->comments_cap);
2296     }
2297 }
2298
2299 void
2300 cpp_free_token_list (list)
2301      cpp_toklist *list;
2302 {
2303   if (list->comments)
2304     free (list->comments);
2305   free (list->tokens - 1);      /* Backup over dummy token.  */
2306   free (list->namebuf);
2307   free (list);
2308 }
2309
2310 void
2311 init_trigraph_map ()
2312 {
2313   trigraph_map['='] = '#';
2314   trigraph_map['('] = '[';
2315   trigraph_map[')'] = ']';
2316   trigraph_map['/'] = '\\';
2317   trigraph_map['\''] = '^';
2318   trigraph_map['<'] = '{';
2319   trigraph_map['>'] = '}';
2320   trigraph_map['!'] = '|';
2321   trigraph_map['-'] = '~';
2322 }
2323
2324 /* Call when a trigraph is encountered.  It warns if necessary, and
2325    returns true if the trigraph should be honoured.  END is the third
2326    character of a trigraph in the input stream.  */
2327 static int
2328 trigraph_ok (pfile, end)
2329      cpp_reader *pfile;
2330      const unsigned char *end;
2331 {
2332   int accept = CPP_OPTION (pfile, trigraphs);
2333   
2334   if (CPP_OPTION (pfile, warn_trigraphs))
2335     {
2336       unsigned int col = end - 1 - pfile->buffer->line_base;
2337       if (accept)
2338         cpp_warning_with_line (pfile, pfile->buffer->lineno, col, 
2339                                "trigraph ??%c converted to %c",
2340                                (int) *end, (int) trigraph_map[*end]);
2341       else
2342         cpp_warning_with_line (pfile, pfile->buffer->lineno, col,
2343                                "trigraph ??%c ignored", (int) *end);
2344     }
2345   return accept;
2346 }
2347
2348 /* Scan a string for trigraphs, warning or replacing them inline as
2349    appropriate.  When parsing a string, we must call this routine
2350    before processing a newline character (if trigraphs are enabled),
2351    since the newline might be escaped by a preceding backslash
2352    trigraph sequence.  Returns a pointer to the end of the name after
2353    replacement.  */
2354
2355 static unsigned char*
2356 trigraph_replace (pfile, src, limit)
2357      cpp_reader *pfile;
2358      unsigned char *src;
2359      unsigned char* limit;
2360 {
2361   unsigned char *dest;
2362
2363   /* Starting with src[1], find two consecutive '?'.  The case of no
2364      trigraphs is streamlined.  */
2365   
2366   for (; src + 1 < limit; src += 2)
2367     {
2368       if (src[0] != '?')
2369         continue;
2370
2371       /* Make src point to the 1st (NOT 2nd) of two consecutive '?'s.  */
2372       if (src[-1] == '?')
2373         src--;
2374       else if (src + 2 == limit || src[1] != '?')
2375         continue;
2376
2377       /* Check if it really is a trigraph.  */
2378       if (trigraph_map[src[2]] == 0)
2379         continue;
2380
2381       dest = src;
2382       goto trigraph_found;
2383     }
2384   return limit;
2385
2386   /* Now we have a trigraph, we need to scan the remaining buffer, and
2387      copy-shifting its contents left if replacement is enabled.  */
2388   for (; src + 2 < limit; dest++, src++)
2389     if ((*dest = *src) == '?' && src[1] == '?' && trigraph_map[src[2]])
2390       {
2391       trigraph_found:
2392         src += 2;
2393         if (trigraph_ok (pfile, pfile->buffer->cur - (limit - src)))
2394           *dest = trigraph_map[*src];
2395       }
2396   
2397   /* Copy remaining (at most 2) characters.  */
2398   while (src < limit)
2399     *dest++ = *src++;
2400   return dest;
2401 }
2402
2403 /* If CUR is a backslash or the end of a trigraphed backslash, return
2404    a pointer to its beginning, otherwise NULL.  We don't read beyond
2405    the buffer start, because there is the start of the comment in the
2406    buffer.  */
2407 static const unsigned char *
2408 backslash_start (pfile, cur)
2409      cpp_reader *pfile;
2410      const unsigned char *cur;
2411 {
2412   if (cur[0] == '\\')
2413     return cur;
2414   if (cur[0] == '/' && cur[-1] == '?' && cur[-2] == '?'
2415       && trigraph_ok (pfile, cur))
2416     return cur - 2;
2417   return 0;
2418 }
2419
2420 /* Skip a C-style block comment.  This is probably the trickiest
2421    handler.  We find the end of the comment by seeing if an asterisk
2422    is before every '/' we encounter.  The nasty complication is that a
2423    previous asterisk may be separated by one or more escaped newlines.
2424    Returns non-zero if comment terminated by EOF, zero otherwise.  */
2425 static int
2426 skip_block_comment2 (pfile)
2427      cpp_reader *pfile;
2428 {
2429   cpp_buffer *buffer = pfile->buffer;
2430   const unsigned char *char_after_star = 0;
2431   register const unsigned char *cur = buffer->cur;
2432   int seen_eof = 0;
2433   
2434   /* Inner loop would think the comment has ended if the first comment
2435      character is a '/'.  Avoid this and keep the inner loop clean by
2436      skipping such a character.  */
2437   if (cur < buffer->rlimit && cur[0] == '/')
2438     cur++;
2439
2440   for (; cur < buffer->rlimit; )
2441     {
2442       unsigned char c = *cur++;
2443
2444       /* People like decorating comments with '*', so check for
2445          '/' instead for efficiency.  */
2446       if (c == '/')
2447         {
2448           if (cur[-2] == '*' || cur - 1 == char_after_star)
2449             goto out;
2450
2451           /* Warn about potential nested comments, but not when
2452              the final character inside the comment is a '/'.
2453              Don't bother to get it right across escaped newlines.  */
2454           if (CPP_OPTION (pfile, warn_comments) && cur + 1 < buffer->rlimit
2455               && cur[0] == '*' && cur[1] != '/') 
2456             {
2457               buffer->cur = cur;
2458               cpp_warning (pfile, "'/*' within comment");
2459             }
2460         }
2461       else if (IS_NEWLINE(c))
2462         {
2463           const unsigned char* bslash = backslash_start (pfile, cur - 2);
2464
2465           handle_newline (cur, buffer->rlimit, c);
2466           /* Work correctly if there is an asterisk before an
2467              arbirtrarily long sequence of escaped newlines.  */
2468           if (bslash && (bslash[-1] == '*' || bslash == char_after_star))
2469             char_after_star = cur;
2470           else
2471             char_after_star = 0;
2472         }
2473     }
2474   seen_eof = 1;
2475
2476  out:
2477   buffer->cur = cur;
2478   return seen_eof;
2479 }
2480
2481 /* Skip a C++ or Chill line comment.  Handles escaped newlines.
2482    Returns non-zero if a multiline comment.  */
2483 static int
2484 skip_line_comment2 (pfile)
2485      cpp_reader *pfile;
2486 {
2487   cpp_buffer *buffer = pfile->buffer;
2488   register const unsigned char *cur = buffer->cur;
2489   int multiline = 0;
2490
2491   for (; cur < buffer->rlimit; )
2492     {
2493       unsigned char c = *cur++;
2494
2495       if (IS_NEWLINE (c))
2496         {
2497           /* Check for a (trigaph?) backslash escaping the newline.  */
2498           if (!backslash_start (pfile, cur - 2))
2499             goto out;
2500           multiline = 1;
2501           handle_newline (cur, buffer->rlimit, c);
2502         }
2503     }
2504   cur++;
2505
2506  out:
2507   buffer->cur = cur - 1;        /* Leave newline for caller.  */
2508   return multiline;
2509 }
2510
2511 /* Skips whitespace, stopping at next non-whitespace character.  */
2512 static void
2513 skip_whitespace (pfile, in_directive)
2514      cpp_reader *pfile;
2515      int in_directive;
2516 {
2517   cpp_buffer *buffer = pfile->buffer;
2518   register const unsigned char *cur = buffer->cur;
2519   unsigned short null_count = 0;
2520
2521   for (; cur < buffer->rlimit; )
2522     {
2523       unsigned char c = *cur++;
2524
2525       if (IS_HSPACE(c))         /* FIXME: Fix ISTABLE.  */
2526         continue;
2527       if (!is_space(c) || IS_NEWLINE (c)) /* Main loop handles newlines.  */
2528         goto out;
2529       if (c == '\0')
2530         null_count++;
2531       /* Mut be '\f' or '\v' */
2532       else if (in_directive && CPP_PEDANTIC (pfile))
2533         cpp_pedwarn (pfile, "%s in preprocessing directive",
2534                      c == '\f' ? "formfeed" : "vertical tab");
2535     }
2536   cur++;
2537
2538  out:
2539   buffer->cur = cur - 1;
2540   if (null_count)
2541     cpp_warning (pfile, null_count > 1 ? "embedded null characters ignored"
2542                  : "embedded null character ignored");
2543 }
2544
2545 /* Parse (append) an identifier.  */
2546 static void
2547 parse_name (pfile, list, name)
2548      cpp_reader *pfile;
2549      cpp_toklist *list;
2550      cpp_name *name;
2551 {
2552   const unsigned char *name_limit;
2553   unsigned char *namebuf;
2554   cpp_buffer *buffer = pfile->buffer;
2555   register const unsigned char *cur = buffer->cur;
2556
2557  expanded:
2558   name_limit = list->namebuf + list->name_cap;
2559   namebuf = list->namebuf + list->name_used;
2560
2561   for (; cur < buffer->rlimit && namebuf < name_limit; )
2562     {
2563       unsigned char c = *namebuf = *cur; /* Copy a single char.  */
2564
2565       if (! is_idchar(c))
2566         goto out;
2567       namebuf++;
2568       cur++;
2569       if (c == '$' && CPP_PEDANTIC (pfile))
2570         {
2571           buffer->cur = cur;
2572           cpp_pedwarn (pfile, "'$' character in identifier");
2573         }
2574     }
2575
2576   /* Run out of name space?  */
2577   if (cur < buffer->rlimit)
2578     {
2579       list->name_used = namebuf - list->namebuf;
2580       auto_expand_name_space (list);
2581       goto expanded;
2582     }
2583
2584  out:
2585   buffer->cur = cur;
2586   name->len = namebuf - name->text;
2587   list->name_used = namebuf - list->namebuf;
2588 }
2589
2590 /* Parse (append) a number.  */
2591
2592 #define VALID_SIGN(c, prevc) \
2593   (((c) == '+' || (c) == '-') && \
2594    ((prevc) == 'e' || (prevc) == 'E' \
2595     || (((prevc) == 'p' || (prevc) == 'P') && !CPP_OPTION (pfile, c89))))
2596
2597 static void
2598 parse_number (pfile, list, name)
2599      cpp_reader *pfile;
2600      cpp_toklist *list;
2601      cpp_name *name;
2602 {
2603   const unsigned char *name_limit;
2604   unsigned char *namebuf;
2605   cpp_buffer *buffer = pfile->buffer;
2606   register const unsigned char *cur = buffer->cur;
2607
2608  expanded:
2609   name_limit = list->namebuf + list->name_cap;
2610   namebuf = list->namebuf + list->name_used;
2611
2612   for (; cur < buffer->rlimit && namebuf < name_limit; )
2613     {
2614       unsigned char c = *namebuf = *cur; /* Copy a single char.  */
2615
2616       /* Perhaps we should accept '$' here if we accept it for
2617          identifiers.  We know namebuf[-1] is safe, because for c to
2618          be a sign we must have pushed at least one character.  */
2619       if (!is_numchar (c) && c != '.' && ! VALID_SIGN (c, namebuf[-1]))
2620         goto out;
2621
2622       namebuf++;
2623       cur++;
2624     }
2625
2626   /* Run out of name space?  */
2627   if (cur < buffer->rlimit)
2628     {
2629       list->name_used = namebuf - list->namebuf;
2630       auto_expand_name_space (list);
2631       goto expanded;
2632     }
2633   
2634  out:
2635   buffer->cur = cur;
2636   name->len = namebuf - name->text;
2637   list->name_used = namebuf - list->namebuf;
2638 }
2639
2640 /* Places a string terminated by an unescaped TERMINATOR into a
2641    cpp_name, which should be expandable and thus at the top of the
2642    list's stack.  Handles embedded trigraphs, if necessary, and
2643    escaped newlines.
2644
2645    Can be used for character constants (terminator = '\''), string
2646    constants ('"'), angled headers ('>') and assertions (')').  */
2647
2648 static void
2649 parse_string2 (pfile, list, name, terminator)
2650      cpp_reader *pfile;
2651      cpp_toklist *list;
2652      cpp_name *name;
2653      unsigned int terminator;
2654 {
2655   cpp_buffer *buffer = pfile->buffer;
2656   register const unsigned char *cur = buffer->cur;
2657   const unsigned char *name_limit;
2658   unsigned char *namebuf;
2659   unsigned int null_count = 0;
2660   int trigraphed_len = 0;
2661
2662  expanded:
2663   name_limit = list->namebuf + list->name_cap;
2664   namebuf = list->namebuf + list->name_used;
2665
2666   for (; cur < buffer->rlimit && namebuf < name_limit; )
2667     {
2668       unsigned int c = *namebuf++ = *cur++; /* Copy a single char.  */
2669
2670       if (c == '\0')
2671         null_count++;
2672       else if (c == terminator || IS_NEWLINE (c))
2673         {
2674           /* Needed for trigraph_replace and multiline string warning.  */
2675           buffer->cur = cur;
2676
2677           /* Scan for trigraphs before checking if backslash-escaped.  */
2678           if (CPP_OPTION (pfile, trigraphs)
2679               || CPP_OPTION (pfile, warn_trigraphs))
2680             {
2681               namebuf = trigraph_replace (pfile, name->text + trigraphed_len,
2682                                             namebuf);
2683               trigraphed_len = namebuf - 2 - (name->text + trigraphed_len);
2684               if (trigraphed_len < 0)
2685                 trigraphed_len = 0;
2686             }
2687
2688           namebuf--;     /* Drop the newline / terminator from the name.  */
2689           if (IS_NEWLINE (c))
2690             {
2691               /* Drop a backslash newline, and continue. */
2692               if (namebuf[-1] == '\\')
2693                 {
2694                   handle_newline (cur, buffer->rlimit, c);
2695                   namebuf--;
2696                   continue;
2697                 }
2698
2699               cur--;
2700
2701               /* In Fortran and assembly language, silently terminate
2702                  strings of either variety at end of line.  This is a
2703                  kludge around not knowing where comments are in these
2704                  languages.  */
2705               if (CPP_OPTION (pfile, lang_fortran)
2706                   || CPP_OPTION (pfile, lang_asm))
2707                 goto out;
2708
2709               /* Character constants, headers and asserts may not
2710                  extend over multiple lines.  In Standard C, neither
2711                  may strings.  We accept multiline strings as an
2712                  extension, but not in directives.  */
2713               if (terminator != '"' || IS_DIRECTIVE (list))
2714                 goto unterminated;
2715                 
2716               cur++;  /* Move forwards again.  */
2717
2718               if (pfile->multiline_string_line == 0)
2719                 {
2720                   pfile->multiline_string_line = list->line;
2721                   if (CPP_PEDANTIC (pfile))
2722                     cpp_pedwarn (pfile, "multi-line string constant");
2723                 }
2724
2725               *namebuf++ = '\n';
2726               handle_newline (cur, buffer->rlimit, c);
2727             }
2728           else
2729             {
2730               unsigned char *temp;
2731
2732               /* An odd number of consecutive backslashes represents
2733                  an escaped terminator.  */
2734               temp = namebuf - 1;
2735               while (temp >= name->text && *temp == '\\')
2736                 temp--;
2737
2738               if ((namebuf - temp) & 1)
2739                 goto out;
2740               namebuf++;
2741             }
2742         }
2743     }
2744
2745   /* Run out of name space?  */
2746   if (cur < buffer->rlimit)
2747     {
2748       list->name_used = namebuf - list->namebuf;
2749       auto_expand_name_space (list);
2750       goto expanded;
2751     }
2752
2753   /* We may not have trigraph-replaced the input for this code path,
2754      but as the input is in error by being unterminated we don't
2755      bother.  Prevent warnings about no newlines at EOF.  */
2756   if (IS_NEWLINE(cur[-1]))
2757     cur--;
2758
2759  unterminated:
2760   cpp_error (pfile, "missing terminating %c character", (int) terminator);
2761
2762   if (terminator == '\"' && pfile->multiline_string_line != list->line
2763       && pfile->multiline_string_line != 0)
2764     {
2765       cpp_error_with_line (pfile, pfile->multiline_string_line, -1,
2766                            "possible start of unterminated string literal");
2767       pfile->multiline_string_line = 0;
2768     }
2769   
2770  out:
2771   buffer->cur = cur;
2772   name->len = namebuf - name->text;
2773   list->name_used = namebuf - list->namebuf;
2774
2775   if (null_count > 0)
2776     cpp_warning (pfile, (null_count > 1 ? "null characters preserved"
2777                          : "null character preserved"));
2778 }
2779
2780 /* The character TYPE helps us distinguish comment types: '*' = C
2781    style, '-' = Chill-style and '/' = C++ style.  For code simplicity,
2782    the stored comment includes the comment start and any terminator.  */
2783
2784 #define COMMENT_START_LEN 2
2785 static void
2786 save_comment (list, from, len, tok_no, type)
2787      cpp_toklist *list;
2788      const unsigned char *from;
2789      unsigned int len;
2790      unsigned int tok_no;
2791      unsigned int type;
2792 {
2793   cpp_token *comment;
2794   unsigned char *buffer;
2795   
2796   len += COMMENT_START_LEN;
2797
2798   if (list->comments_used == list->comments_cap)
2799     expand_comment_space (list);
2800
2801   if (list->name_used + len > list->name_cap)
2802     expand_name_space (list, len);
2803
2804   buffer = list->namebuf + list->name_used;
2805
2806   comment = &list->comments[list->comments_used++];
2807   comment->type = CPP_COMMENT;
2808   comment->aux = tok_no;
2809   comment->val.name.len = len;
2810   comment->val.name.text = buffer;
2811
2812   if (type == '*')
2813     {
2814       *buffer++ = '/';
2815       *buffer++ = '*';
2816     }
2817   else
2818     {
2819       *buffer++ = type;
2820       *buffer++ = type;
2821     }
2822
2823   memcpy (buffer, from, len - COMMENT_START_LEN);
2824   list->name_used += len;
2825 }
2826
2827 /*
2828  *  The tokenizer's main loop.  Returns a token list, representing a
2829  *  logical line in the input file, terminated with a CPP_VSPACE
2830  *  token.  On EOF, a token list containing the single CPP_EOF token
2831  *  is returned.
2832  *
2833  *  Implementation relies almost entirely on lookback, rather than
2834  *  looking forwards.  This means that tokenization requires just
2835  *  a single pass of the file, even in the presence of trigraphs and
2836  *  escaped newlines, providing significant performance benefits.
2837  *  Trigraph overhead is negligible if they are disabled, and low
2838  *  even when enabled.
2839  */
2840
2841 void
2842 _cpp_lex_line (pfile, list)
2843      cpp_reader *pfile;
2844      cpp_toklist *list;
2845 {
2846   cpp_token *cur_token, *token_limit;
2847   cpp_buffer *buffer = pfile->buffer;
2848   register const unsigned char *cur = buffer->cur;
2849   unsigned char flags = 0;
2850
2851  expanded:
2852   token_limit = list->tokens + list->tokens_cap;
2853   cur_token = list->tokens + list->tokens_used;
2854
2855   for (; cur < buffer->rlimit && cur_token < token_limit;)
2856     {
2857       unsigned char c = *cur++;
2858
2859       /* Optimize whitespace skipping, in particular the case of a
2860          single whitespace character, as every other token is probably
2861          whitespace. (' ' '\t' '\v' '\f' '\0').  */
2862       if (is_hspace ((unsigned int) c))
2863         {
2864           if (c == '\0' || (cur < buffer->rlimit && is_hspace (*cur)))
2865             {
2866               buffer->cur = cur - (c == '\0');  /* Get the null warning.  */
2867               skip_whitespace (pfile, IS_DIRECTIVE (list));
2868               cur = buffer->cur;
2869             }
2870           flags = PREV_WHITESPACE;
2871           if (cur == buffer->rlimit)
2872             break;
2873           c = *cur++;
2874         }
2875
2876       /* Initialize current token.  Its type is set in the switch.  */
2877       cur_token->col = COLUMN (cur);
2878       cur_token->flags = flags;
2879       flags = 0;
2880
2881       switch (c)
2882         {
2883         case '0': case '1': case '2': case '3': case '4':
2884         case '5': case '6': case '7': case '8': case '9':
2885           cur--;                /* Backup character.  */
2886           if (PREV_TOKEN_TYPE == CPP_DOT && IMMED_TOKEN ())
2887             {
2888               /* Prepend an immediately previous CPP_DOT token.  */
2889               cur_token--;
2890               if (list->name_cap == list->name_used)
2891                 auto_expand_name_space (list);
2892
2893               cur_token->val.name.len = 1;
2894               cur_token->val.name.text = list->namebuf + list->name_used;
2895               list->namebuf[list->name_used++] = '.';
2896             }
2897           else
2898             INIT_NAME (list, cur_token->val.name);
2899
2900         continue_number:
2901           buffer->cur = cur;
2902           parse_number (pfile, list, &cur_token->val.name);
2903           cur = buffer->cur;
2904
2905           PUSH_TOKEN (CPP_NUMBER); /* Number not yet interpreted.  */
2906           break;
2907
2908         letter:
2909         case '_':
2910         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
2911         case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2912         case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
2913         case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2914         case 'y': case 'z':
2915         case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
2916         case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
2917         case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
2918         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2919         case 'Y': case 'Z':
2920           cur--;                     /* Backup character.  */
2921           INIT_NAME (list, cur_token->val.name);
2922           cur_token->type = CPP_NAME; /* Identifier, macro etc.  */
2923
2924         continue_name:
2925           buffer->cur = cur;
2926           parse_name (pfile, list, &cur_token->val.name);
2927           cur = buffer->cur;
2928
2929           /* Find handler for newly created / extended directive.  */
2930           if (IS_DIRECTIVE (list) && cur_token == &list->tokens[1])
2931             _cpp_check_directive (list, cur_token);
2932           cur_token++;
2933           break;
2934
2935         case '\'':
2936           /* Fall through.  */
2937         case '\"':
2938           cur_token->type = c == '\'' ? CPP_CHAR : CPP_STRING;
2939           /* Do we have a wide string?  */
2940           if (cur_token[-1].type == CPP_NAME && IMMED_TOKEN ()
2941               && cur_token[-1].val.name.len == 1
2942               && cur_token[-1].val.name.text[0] == 'L'
2943               && !CPP_TRADITIONAL (pfile))
2944             {
2945               /* No need for 'L' any more.  */
2946               list->name_used--;
2947               (--cur_token)->type = (c == '\'' ? CPP_WCHAR : CPP_WSTRING);
2948             }
2949
2950         do_parse_string:
2951           /* Here c is one of ' " > or ).  */
2952           INIT_NAME (list, cur_token->val.name);
2953           buffer->cur = cur;
2954           parse_string2 (pfile, list, &cur_token->val.name, c);
2955           cur = buffer->cur;
2956           cur_token++;
2957           break;
2958
2959         case '/':
2960           cur_token->type = CPP_DIV;
2961           if (IMMED_TOKEN ())
2962             {
2963               if (PREV_TOKEN_TYPE == CPP_DIV)
2964                 {
2965                   /* We silently allow C++ comments in system headers,
2966                      irrespective of conformance mode, because lots of
2967                      broken systems do that and trying to clean it up
2968                      in fixincludes is a nightmare.  */
2969                   if (buffer->system_header_p)
2970                     goto do_line_comment;
2971                   else if (CPP_OPTION (pfile, cplusplus_comments))
2972                     {
2973                       if (CPP_OPTION (pfile, c89) && CPP_PEDANTIC (pfile)
2974                           && ! buffer->warned_cplusplus_comments)
2975                         {
2976                           buffer->cur = cur;
2977                           cpp_pedwarn (pfile,
2978                              "C++ style comments are not allowed in ISO C89");
2979                           cpp_pedwarn (pfile,
2980                           "(this will be reported only once per input file)");
2981                           buffer->warned_cplusplus_comments = 1;
2982                         }
2983                     do_line_comment:
2984                       buffer->cur = cur;
2985                       if (cur[-2] != c)
2986                         cpp_warning (pfile,
2987                                      "comment start split across lines");
2988                       if (skip_line_comment2 (pfile))
2989                         cpp_error_with_line (pfile, list->line,
2990                                              cur_token[-1].col,
2991                                              "multi-line comment");
2992                       if (!CPP_OPTION (pfile, discard_comments))
2993                         save_comment (list, cur, buffer->cur - cur,
2994                                       cur_token - 1 - list->tokens, c);
2995                       cur = buffer->cur;
2996
2997                       /* Back-up to first '-' or '/'.  */
2998                       cur_token -= 2;
2999                       if (!CPP_OPTION (pfile, traditional))
3000                         flags = PREV_WHITESPACE;
3001                     }
3002                 }
3003             }
3004           cur_token++;
3005           break;
3006                       
3007         case '*':
3008           cur_token->type = CPP_MULT;
3009           if (IMMED_TOKEN ())
3010             {
3011               if (PREV_TOKEN_TYPE == CPP_DIV)
3012                 {
3013                   buffer->cur = cur;
3014                   if (cur[-2] != '/')
3015                     cpp_warning (pfile,
3016                                  "comment start '/*' split across lines");
3017                   if (skip_block_comment2 (pfile))
3018                     cpp_error_with_line (pfile, list->line, cur_token[-1].col,
3019                                          "unterminated comment");
3020                   else if (buffer->cur[-2] != '*')
3021                     cpp_warning (pfile,
3022                                  "comment end '*/' split across lines");
3023                   if (!CPP_OPTION (pfile, discard_comments))
3024                     save_comment (list, cur, buffer->cur - cur,
3025                                  cur_token - 1 - list->tokens, c);
3026                   cur = buffer->cur;
3027
3028                   cur_token--;
3029                   if (!CPP_OPTION (pfile, traditional))
3030                     flags = PREV_WHITESPACE;
3031                   break;
3032                 }
3033               else if (CPP_OPTION (pfile, cplusplus))
3034                 {
3035                   /* In C++, there are .* and ->* operators.  */
3036                   if (PREV_TOKEN_TYPE == CPP_DEREF)
3037                     BACKUP_TOKEN (CPP_DEREF_STAR);
3038                   else if (PREV_TOKEN_TYPE == CPP_DOT)
3039                     BACKUP_TOKEN (CPP_DOT_STAR);
3040                 }
3041             }
3042           cur_token++;
3043           break;
3044
3045         case '\n':
3046         case '\r':
3047           handle_newline (cur, buffer->rlimit, c);
3048           if (PREV_TOKEN_TYPE == CPP_BACKSLASH && IMMED_TOKEN ())
3049             {
3050               /* Remove the escaped newline.  Then continue to process
3051                  any interrupted name or number.  */
3052               cur_token--;
3053               if (IMMED_TOKEN ())
3054                 {
3055                   cur_token--;
3056                   if (cur_token->type == CPP_NAME)
3057                     goto continue_name;
3058                   else if (cur_token->type == CPP_NUMBER)
3059                     goto continue_number;
3060                   cur_token++;
3061                 }
3062               /* Remember whitespace setting.  */
3063               flags = cur_token->flags;
3064               break;
3065             }
3066           if (PREV_TOKEN_TYPE == CPP_BACKSLASH)
3067             {
3068               buffer->cur = cur;
3069               cpp_warning (pfile, "backslash and newline separated by space");
3070             }
3071           PUSH_TOKEN (CPP_VSPACE);
3072           goto out;
3073
3074         case '-':
3075           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_MINUS)
3076             {
3077               if (CPP_OPTION (pfile, chill))
3078                 goto do_line_comment;
3079               REVISE_TOKEN (CPP_MINUS_MINUS);
3080             }
3081           else
3082             PUSH_TOKEN (CPP_MINUS);
3083           break;
3084
3085           /* The digraph flag checking ensures that ## and %:%:
3086              are interpreted as CPP_PASTE, but #%: and %:# are not.  */
3087         make_hash:
3088         case '#':
3089           if (PREV_TOKEN_TYPE == CPP_HASH && IMMED_TOKEN ()
3090               && ((cur_token->flags ^ cur_token[-1].flags) & DIGRAPH) == 0)
3091             REVISE_TOKEN (CPP_PASTE);
3092           else
3093             PUSH_TOKEN (CPP_HASH);
3094           break;
3095
3096         case ':':
3097           cur_token->type = CPP_COLON;
3098           if (IMMED_TOKEN ())
3099             {
3100               if (PREV_TOKEN_TYPE == CPP_COLON
3101                   && CPP_OPTION (pfile, cplusplus))
3102                 BACKUP_TOKEN (CPP_SCOPE);
3103               /* Digraph: "<:" is a '['  */
3104               else if (PREV_TOKEN_TYPE == CPP_LESS)
3105                 BACKUP_DIGRAPH (CPP_OPEN_SQUARE);
3106               /* Digraph: "%:" is a '#'  */
3107               else if (PREV_TOKEN_TYPE == CPP_MOD)
3108                 {
3109                   (--cur_token)->flags |= DIGRAPH;
3110                   goto make_hash;
3111                 }
3112             }
3113           cur_token++;
3114           break;
3115
3116         case '&':
3117           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_AND)
3118             REVISE_TOKEN (CPP_AND_AND);
3119           else
3120             PUSH_TOKEN (CPP_AND);
3121           break;
3122
3123         make_or:
3124         case '|':
3125           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_OR)
3126             REVISE_TOKEN (CPP_OR_OR);
3127           else
3128             PUSH_TOKEN (CPP_OR);
3129           break;
3130
3131         case '+':
3132           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_PLUS)
3133             REVISE_TOKEN (CPP_PLUS_PLUS);
3134           else
3135             PUSH_TOKEN (CPP_PLUS);
3136           break;
3137
3138         case '=':
3139             /* This relies on equidistance of "?=" and "?" tokens.  */
3140           if (IMMED_TOKEN () && PREV_TOKEN_TYPE <= CPP_LAST_EQ)
3141             REVISE_TOKEN (PREV_TOKEN_TYPE + (CPP_EQ_EQ - CPP_EQ));
3142           else
3143             PUSH_TOKEN (CPP_EQ);
3144           break;
3145
3146         case '>':
3147           cur_token->type = CPP_GREATER;
3148           if (IMMED_TOKEN ())
3149             {
3150               if (PREV_TOKEN_TYPE == CPP_GREATER)
3151                 BACKUP_TOKEN (CPP_RSHIFT);
3152               else if (PREV_TOKEN_TYPE == CPP_MINUS)
3153                 BACKUP_TOKEN (CPP_DEREF);
3154               /* Digraph: ":>" is a ']'  */
3155               else if (PREV_TOKEN_TYPE == CPP_COLON)
3156                 BACKUP_DIGRAPH (CPP_CLOSE_SQUARE);
3157               /* Digraph: "%>" is a '}'  */
3158               else if (PREV_TOKEN_TYPE == CPP_MOD)
3159                 BACKUP_DIGRAPH (CPP_CLOSE_BRACE);
3160             }
3161           cur_token++;
3162           break;
3163           
3164         case '<':
3165           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS)
3166             {
3167               REVISE_TOKEN (CPP_LSHIFT);
3168               break;
3169             }
3170           /* Is this the beginning of a header name?  */
3171           if (list->dir_flags & SYNTAX_INCLUDE)
3172             {
3173               c = '>';  /* Terminator.  */
3174               cur_token->type = CPP_HEADER_NAME;
3175               goto do_parse_string;
3176             }
3177           PUSH_TOKEN (CPP_LESS);
3178           break;
3179
3180         case '%':
3181           /* Digraph: "<%" is a '{'  */
3182           cur_token->type = CPP_MOD;
3183           if (IMMED_TOKEN () && PREV_TOKEN_TYPE == CPP_LESS)
3184             BACKUP_DIGRAPH (CPP_OPEN_BRACE);
3185           cur_token++;
3186           break;
3187
3188         case '(':
3189           /* Is this the beginning of an assertion string?  */
3190           if (list->dir_flags & SYNTAX_ASSERT)
3191             {
3192               c = ')';  /* Terminator.  */
3193               cur_token->type = CPP_ASSERTION;
3194               goto do_parse_string;
3195             }
3196           PUSH_TOKEN (CPP_OPEN_PAREN);
3197           break;
3198
3199         case '?':
3200           if (cur + 1 < buffer->rlimit && *cur == '?'
3201               && trigraph_map[cur[1]] && trigraph_ok (pfile, cur + 1))
3202             {
3203               /* Handle trigraph.  */
3204               cur++;
3205               switch (*cur++)
3206                 {
3207                 case '(': goto make_open_square;
3208                 case ')': goto make_close_square;
3209                 case '<': goto make_open_brace;
3210                 case '>': goto make_close_brace;
3211                 case '=': goto make_hash;
3212                 case '!': goto make_or;
3213                 case '-': goto make_complement;
3214                 case '/': goto make_backslash;
3215                 case '\'': goto make_xor;
3216                 }
3217             }
3218           if (IMMED_TOKEN () && CPP_OPTION (pfile, cplusplus))
3219             {
3220               /* GNU C++ defines <? and >? operators.  */
3221               if (PREV_TOKEN_TYPE == CPP_LESS)
3222                 {
3223                   REVISE_TOKEN (CPP_MIN);
3224                   break;
3225                 }
3226               else if (PREV_TOKEN_TYPE == CPP_GREATER)
3227                 {
3228                   REVISE_TOKEN (CPP_MAX);
3229                   break;
3230                 }
3231             }
3232           PUSH_TOKEN (CPP_QUERY);
3233           break;
3234
3235         case '.':
3236           if (PREV_TOKEN_TYPE == CPP_DOT && cur_token[-2].type == CPP_DOT
3237               && IMMED_TOKEN ()
3238               && !(cur_token[-1].flags & PREV_WHITESPACE))
3239             {
3240               cur_token -= 2;
3241               PUSH_TOKEN (CPP_ELLIPSIS);
3242             }
3243           else
3244             PUSH_TOKEN (CPP_DOT);
3245           break;
3246
3247         make_complement:
3248         case '~': PUSH_TOKEN (CPP_COMPL); break;
3249         make_xor:
3250         case '^': PUSH_TOKEN (CPP_XOR); break;
3251         make_open_brace:
3252         case '{': PUSH_TOKEN (CPP_OPEN_BRACE); break;
3253         make_close_brace:
3254         case '}': PUSH_TOKEN (CPP_CLOSE_BRACE); break;
3255         make_open_square:
3256         case '[': PUSH_TOKEN (CPP_OPEN_SQUARE); break;
3257         make_close_square:
3258         case ']': PUSH_TOKEN (CPP_CLOSE_SQUARE); break;
3259         make_backslash:
3260         case '\\': PUSH_TOKEN (CPP_BACKSLASH); break;
3261         case '!': PUSH_TOKEN (CPP_NOT); break;
3262         case ',': PUSH_TOKEN (CPP_COMMA); break;
3263         case ';': PUSH_TOKEN (CPP_SEMICOLON); break;
3264         case ')': PUSH_TOKEN (CPP_CLOSE_PAREN); break;
3265
3266         case '$':
3267           if (CPP_OPTION (pfile, dollars_in_ident))
3268             goto letter;
3269           /* Fall through */
3270         default:
3271           cur_token->aux = c;
3272           cur_token->val.name.len = 0; /* FIXME: needed for transition only */
3273           PUSH_TOKEN (CPP_OTHER);
3274           break;
3275         }
3276     }
3277
3278   /* Run out of token space?  */
3279   if (cur_token == token_limit)
3280     {
3281       list->tokens_used = cur_token - list->tokens;
3282       expand_token_space (list);
3283       goto expanded;
3284     }
3285
3286   cur_token->type = CPP_EOF;
3287   cur_token->flags = flags;
3288
3289   if (cur_token != &list->tokens[0])
3290     {
3291       /* Next call back will get just a CPP_EOF.  */
3292       buffer->cur = cur;
3293       cpp_warning (pfile, "no newline at end of file");
3294       PUSH_TOKEN (CPP_VSPACE);
3295     }
3296
3297  out:
3298   buffer->cur = cur;
3299
3300   list->tokens_used = cur_token - list->tokens;
3301
3302   /* FIXME:  take this check out and put it in the caller.
3303      list->directive == 0 indicates an unknown directive (but null
3304      directive is OK).  This is the first time we can be sure the
3305      directive is invalid, and thus warn about it, because it might
3306      have been split by escaped newlines.  Also, don't complain about
3307      invalid directives in assembly source, we don't know where the
3308      comments are, and # may introduce assembler pseudo-ops.  */
3309
3310   if (IS_DIRECTIVE (list) && list->dir_handler == 0
3311       && list->tokens[1].type != CPP_VSPACE
3312       && !CPP_OPTION (pfile, lang_asm))
3313     cpp_error_with_line (pfile, list->line, list->tokens[1].col,
3314                          "invalid preprocessing directive");
3315 }
3316
3317 /* Write the spelling of a token TOKEN to BUFFER.  The buffer must
3318    already contain the enough space to hold the token's spelling.  If
3319    WHITESPACE is true, and the token was preceded by whitespace,
3320    output a single space before the token proper.  Returns a pointer
3321    to the character after the last character written.  */
3322
3323 static unsigned char *
3324 spell_token (pfile, token, buffer, whitespace)
3325      cpp_reader *pfile;         /* Would be nice to be rid of this...  */
3326      cpp_token *token;
3327      unsigned char *buffer;
3328      int whitespace;
3329 {
3330   /* Whitespace will not be wanted by handlers of the # and ##
3331      operators calling this function, but will be wanted by the
3332      function that writes out the preprocessed file.  */
3333   if (whitespace && token->flags & PREV_WHITESPACE)
3334     *buffer++ = ' ';
3335
3336   switch (token_spellings[token->type].type)
3337     {
3338     case SPELL_OPERATOR:
3339       {
3340         const unsigned char *spelling;
3341         unsigned char c;
3342
3343         if (token->flags & DIGRAPH)
3344           spelling = digraph_spellings[token->type - CPP_FIRST_DIGRAPH];
3345         else
3346           spelling = token_spellings[token->type].spelling;
3347         
3348         while ((c = *spelling++) != '\0')
3349           *buffer++ = c;
3350       }
3351       break;
3352
3353     case SPELL_IDENT:
3354       memcpy (buffer, token->val.name.text, token->val.name.len);
3355       buffer += token->val.name.len;
3356       break;
3357
3358     case SPELL_STRING:
3359       {
3360         unsigned char c;
3361
3362         if (token->type == CPP_WSTRING || token->type == CPP_WCHAR)
3363           *buffer++ = 'L';
3364         c = '\'';
3365         if (token->type == CPP_STRING || token->type == CPP_WSTRING)
3366           c = '"';
3367         *buffer++ = c;
3368         memcpy (buffer, token->val.name.text, token->val.name.len);
3369         buffer += token->val.name.len;
3370         *buffer++ = c;
3371       }
3372       break;
3373
3374     case SPELL_CHAR:
3375       *buffer++ = token->aux;
3376       break;
3377
3378     case SPELL_NONE:
3379       cpp_ice (pfile, "Unspellable token");
3380       break;
3381     }
3382
3383   return buffer;
3384 }
3385
3386 /* Temporary function for illustrative purposes.  */
3387 void
3388 _cpp_lex_file (pfile)
3389      cpp_reader* pfile;
3390 {
3391   int recycle;
3392   cpp_toklist* list;
3393
3394   init_trigraph_map ();
3395   list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
3396
3397   for (recycle = 0; ;)
3398     {
3399       init_token_list (pfile, list, recycle);
3400       recycle = 1;
3401
3402       _cpp_lex_line (pfile, list);
3403       if (list->tokens[0].type == CPP_EOF)
3404         break;
3405
3406       if (list->dir_handler)
3407         {
3408           if (list->dir_handler (pfile))
3409             {
3410               list = (cpp_toklist *) xmalloc (sizeof (cpp_toklist));
3411               recycle = 0;
3412             }
3413         }
3414       else
3415         _cpp_output_list (pfile, list);
3416     }
3417 }
3418
3419 /* Temporary function for illustrative purposes.  */
3420 static void
3421 _cpp_output_list (pfile, list)
3422      cpp_reader *pfile;
3423      cpp_toklist *list;
3424 {
3425   cpp_token *token, *comment, *comment_before = 0;
3426
3427   if (list->comments_used > 0)
3428     {
3429       comment = &list->comments[0];
3430       comment_before = &list->tokens[comment->aux];
3431     }
3432
3433   token = &list->tokens[0];
3434   do
3435     {
3436       /* Output comments if -C.  */
3437       while (token == comment_before)
3438         {
3439           /* Make space for the comment, and copy it out.  */
3440           CPP_RESERVE (pfile, TOKEN_LEN (comment));
3441           pfile->limit = spell_token (pfile, comment, pfile->limit, 0);
3442
3443           /* Stop if no comments left, or no more comments appear
3444              before the current token.  */
3445           comment++;
3446           if (comment == list->comments + list->comments_used)
3447             break;
3448           comment_before = &list->tokens[comment->aux];
3449         }
3450
3451       CPP_RESERVE (pfile, TOKEN_LEN (token));
3452       pfile->limit = spell_token (pfile, token, pfile->limit, 1);
3453     }
3454   while (token++->type != CPP_VSPACE);
3455 }
3456
3457 #endif