OSDN Git Service

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