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