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