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
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
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.
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. */
28 #define PEEKBUF(BUFFER, N) \
29 ((BUFFER)->rlimit - (BUFFER)->cur > (N) ? (BUFFER)->cur[N] : EOF)
30 #define GETBUF(BUFFER) \
31 ((BUFFER)->cur < (BUFFER)->rlimit ? *(BUFFER)->cur++ : EOF)
32 #define FORWARDBUF(BUFFER, N) ((BUFFER)->cur += (N))
34 #define PEEKN(N) PEEKBUF (CPP_BUFFER (pfile), N)
35 #define FORWARD(N) FORWARDBUF (CPP_BUFFER (pfile), (N))
36 #define GETC() GETBUF (CPP_BUFFER (pfile))
37 #define PEEKC() PEEKBUF (CPP_BUFFER (pfile), 0)
39 static void skip_block_comment PARAMS ((cpp_reader *));
40 static void skip_line_comment PARAMS ((cpp_reader *));
41 static int maybe_macroexpand PARAMS ((cpp_reader *, long));
42 static int skip_comment PARAMS ((cpp_reader *, int));
43 static int copy_comment PARAMS ((cpp_reader *, int));
44 static void skip_string PARAMS ((cpp_reader *, int));
45 static void parse_string PARAMS ((cpp_reader *, int));
46 static U_CHAR *find_position PARAMS ((U_CHAR *, U_CHAR *, unsigned long *));
47 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
48 static void null_warning PARAMS ((cpp_reader *, unsigned int));
50 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
53 _cpp_grow_token_buffer (pfile, n)
57 long old_written = CPP_WRITTEN (pfile);
58 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
59 pfile->token_buffer = (U_CHAR *)
60 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
61 CPP_SET_WRITTEN (pfile, old_written);
65 null_cleanup (pbuf, pfile)
66 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
67 cpp_reader *pfile ATTRIBUTE_UNUSED;
72 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
73 If BUFFER != NULL, then use the LENGTH characters in BUFFER
74 as the new input buffer.
75 Return the new buffer, or NULL on failure. */
78 cpp_push_buffer (pfile, buffer, length)
83 cpp_buffer *buf = CPP_BUFFER (pfile);
85 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
87 cpp_fatal (pfile, "macro or `#include' recursion too deep");
91 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
93 new->if_stack = pfile->if_stack;
94 new->cleanup = null_cleanup;
95 new->buf = new->cur = buffer;
96 new->rlimit = buffer + length;
99 new->line_base = NULL;
101 CPP_BUFFER (pfile) = new;
106 cpp_pop_buffer (pfile)
109 cpp_buffer *buf = CPP_BUFFER (pfile);
110 if (ACTIVE_MARK_P (pfile))
111 cpp_ice (pfile, "mark active in cpp_pop_buffer");
112 (*buf->cleanup) (buf, pfile);
113 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
115 pfile->buffer_stack_depth--;
116 return CPP_BUFFER (pfile);
119 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
120 Pop the buffer when done. */
123 cpp_scan_buffer (pfile)
126 cpp_buffer *buffer = CPP_BUFFER (pfile);
127 enum cpp_token token;
128 if (CPP_OPTION (pfile, no_output))
130 long old_written = CPP_WRITTEN (pfile);
131 /* In no-output mode, we can ignore everything but directives. */
134 if (! pfile->only_seen_white)
135 _cpp_skip_rest_of_line (pfile);
136 token = cpp_get_token (pfile);
137 if (token == CPP_EOF) /* Should not happen ... */
139 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
141 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
142 cpp_pop_buffer (pfile);
146 CPP_SET_WRITTEN (pfile, old_written);
152 token = cpp_get_token (pfile);
153 if (token == CPP_EOF) /* Should not happen ... */
155 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
157 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
158 cpp_pop_buffer (pfile);
166 * Rescan a string (which may have escape marks) into pfile's buffer.
167 * Place the result in pfile->token_buffer.
169 * The input is copied before it is scanned, so it is safe to pass
170 * it something from the token_buffer that will get overwritten
171 * (because it follows CPP_WRITTEN). This is used by do_include.
175 cpp_expand_to_buffer (pfile, buf, length)
180 register cpp_buffer *ip;
186 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
190 /* Set up the input on the input stack. */
192 buf1 = (U_CHAR *) alloca (length + 1);
193 memcpy (buf1, buf, length);
196 ip = cpp_push_buffer (pfile, buf1, length);
201 /* Scan the input, create the output. */
202 save_no_output = CPP_OPTION (pfile, no_output);
203 CPP_OPTION (pfile, no_output) = 0;
204 CPP_OPTION (pfile, no_line_commands)++;
205 cpp_scan_buffer (pfile);
206 CPP_OPTION (pfile, no_line_commands)--;
207 CPP_OPTION (pfile, no_output) = save_no_output;
209 CPP_NUL_TERMINATE (pfile);
213 cpp_buf_line_and_col (pbuf, linep, colp)
214 register cpp_buffer *pbuf;
219 *linep = pbuf->lineno;
221 *colp = pbuf->cur - pbuf->line_base;
231 /* Return the topmost cpp_buffer that corresponds to a file (not a macro). */
234 cpp_file_buffer (pfile)
239 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
240 if (ip->ihash != NULL)
245 /* Skip a C-style block comment. We know it's a comment, and point is
246 at the second character of the starter. */
248 skip_block_comment (pfile)
252 const U_CHAR *limit, *cur;
255 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
256 limit = CPP_BUFFER (pfile)->rlimit;
257 cur = CPP_BUFFER (pfile)->cur;
262 if (c == '\n' || c == '\r')
264 /* \r cannot be a macro escape marker here. */
265 if (!ACTIVE_MARK_P (pfile))
266 CPP_BUMP_LINE_CUR (pfile, cur);
270 /* Check for teminator. */
271 if (cur < limit && *cur == '/')
274 /* Warn about comment starter embedded in comment. */
275 if (cur[-2] == '/' && CPP_OPTION (pfile, warn_comments))
276 cpp_warning_with_line (pfile, CPP_BUFFER (pfile)->lineno,
277 cur - CPP_BUFFER (pfile)->line_base,
278 "'/*' within comment");
282 cpp_error_with_line (pfile, line, col, "unterminated comment");
285 CPP_BUFFER (pfile)->cur = cur + 1;
288 /* Skip a C++/Chill line comment. We know it's a comment, and point
289 is at the second character of the initiator. */
291 skip_line_comment (pfile)
299 /* We don't have to worry about EOF in here. */
302 /* Don't consider final '\n' to be part of comment. */
308 /* \r cannot be a macro escape marker here. */
309 if (!ACTIVE_MARK_P (pfile))
310 CPP_BUMP_LINE (pfile);
311 if (CPP_OPTION (pfile, warn_comments))
312 cpp_warning (pfile, "backslash-newline within line comment");
317 /* Skip a comment - C, C++, or Chill style. M is the first character
318 of the comment marker. If this really is a comment, skip to its
319 end and return ' '. If this is not a comment, return M (which will
323 skip_comment (pfile, m)
327 if (m == '/' && PEEKC() == '*')
329 skip_block_comment (pfile);
332 else if (m == '/' && PEEKC() == '/')
334 if (CPP_BUFFER (pfile)->system_header_p)
336 /* We silently allow C++ comments in system headers, irrespective
337 of conformance mode, because lots of busted systems do that
338 and trying to clean it up in fixincludes is a nightmare. */
339 skip_line_comment (pfile);
342 else if (CPP_OPTION (pfile, cplusplus_comments))
344 if (CPP_OPTION (pfile, c89)
345 && CPP_PEDANTIC (pfile)
346 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
349 "C++ style comments are not allowed in ISO C89");
351 "(this will be reported only once per input file)");
352 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
354 skip_line_comment (pfile);
360 else if (m == '-' && PEEKC() == '-'
361 && CPP_OPTION (pfile, chill))
363 skip_line_comment (pfile);
370 /* Identical to skip_comment except that it copies the comment into the
371 token_buffer. This is used if !discard_comments. */
373 copy_comment (pfile, m)
377 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
380 if (skip_comment (pfile, m) == m)
383 limit = CPP_BUFFER (pfile)->cur;
384 CPP_RESERVE (pfile, limit - start + 2);
385 CPP_PUTC_Q (pfile, m);
386 for (; start <= limit; start++)
388 CPP_PUTC_Q (pfile, *start);
394 null_warning (pfile, count)
399 cpp_warning (pfile, "embedded null character ignored");
401 cpp_warning (pfile, "embedded null characters ignored");
404 /* Skip whitespace \-newline and comments. Does not macro-expand. */
407 _cpp_skip_hspace (pfile)
410 unsigned int null_count = 0;
418 else if (is_hspace(c))
420 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
421 cpp_pedwarn (pfile, "%s in preprocessing directive",
422 c == '\f' ? "formfeed" : "vertical tab");
428 /* \r is a backslash-newline marker if !has_escapes, and
429 a deletable-whitespace or no-reexpansion marker otherwise. */
430 if (CPP_BUFFER (pfile)->has_escapes)
438 CPP_BUMP_LINE (pfile);
440 else if (c == '/' || c == '-')
442 c = skip_comment (pfile, c);
452 null_warning (pfile, null_count);
455 /* Read and discard the rest of the current line. */
458 _cpp_skip_rest_of_line (pfile)
472 if (! CPP_BUFFER (pfile)->has_escapes)
473 CPP_BUMP_LINE (pfile);
478 skip_string (pfile, c);
483 skip_comment (pfile, c);
488 if (CPP_PEDANTIC (pfile))
489 cpp_pedwarn (pfile, "%s in preprocessing directive",
490 c == '\f' ? "formfeed" : "vertical tab");
497 /* Parse an identifier starting with C. */
500 _cpp_parse_name (pfile, c)
512 if (c == '$' && CPP_PEDANTIC (pfile))
513 cpp_pedwarn (pfile, "`$' in identifier");
515 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
516 CPP_PUTC_Q (pfile, c);
521 CPP_NUL_TERMINATE_Q (pfile);
525 /* Parse and skip over a string starting with C. A single quoted
526 string is treated like a double -- some programs (e.g., troff) are
527 perverse this way. (However, a single quoted string is not allowed
528 to extend over multiple lines.) */
530 skip_string (pfile, c)
534 long start_line, start_column;
535 unsigned int null_count = 0;
537 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
544 cpp_error_with_line (pfile, start_line, start_column,
545 "unterminated string or character constant");
546 if (pfile->multiline_string_line != start_line
547 && pfile->multiline_string_line != 0)
548 cpp_error_with_line (pfile,
549 pfile->multiline_string_line, -1,
550 "possible real start of unterminated constant");
551 pfile->multiline_string_line = 0;
559 CPP_BUMP_LINE (pfile);
560 /* In Fortran and assembly language, silently terminate
561 strings of either variety at end of line. This is a
562 kludge around not knowing where comments are in these
564 if (CPP_OPTION (pfile, lang_fortran)
565 || CPP_OPTION (pfile, lang_asm))
570 /* Character constants may not extend over multiple lines.
571 In Standard C, neither may strings. We accept multiline
572 strings as an extension. */
575 cpp_error_with_line (pfile, start_line, start_column,
576 "unterminated character constant");
580 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
581 cpp_pedwarn_with_line (pfile, start_line, start_column,
582 "string constant runs past end of line");
583 if (pfile->multiline_string_line == 0)
584 pfile->multiline_string_line = start_line;
588 if (CPP_BUFFER (pfile)->has_escapes)
590 cpp_ice (pfile, "\\r escape inside string constant");
594 /* Backslash newline is replaced by nothing at all. */
595 CPP_BUMP_LINE (pfile);
612 cpp_warning (pfile, "null character in string or character constant");
613 else if (null_count > 1)
614 cpp_warning (pfile, "null characters in string or character constant");
617 /* Parse a string and copy it to the output. */
620 parse_string (pfile, c)
624 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
627 skip_string (pfile, c);
629 limit = CPP_BUFFER (pfile)->cur;
630 CPP_RESERVE (pfile, limit - start + 2);
631 CPP_PUTC_Q (pfile, c);
632 for (; start < limit; start++)
634 CPP_PUTC_Q (pfile, *start);
637 /* Read an assertion into the token buffer, converting to
638 canonical form: `#predicate(a n swe r)' The next non-whitespace
639 character to read should be the first letter of the predicate.
640 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
641 with answer (see callers for why). In case of 0, an error has been
644 _cpp_parse_assertion (pfile)
648 _cpp_skip_hspace (pfile);
652 cpp_error (pfile, "assertion without predicate");
655 else if (! is_idstart(c))
657 cpp_error (pfile, "assertion predicate is not an identifier");
660 CPP_PUTC(pfile, '#');
662 _cpp_parse_name (pfile, c);
667 if (is_hspace(c) || c == '\r')
668 _cpp_skip_hspace (pfile);
674 CPP_PUTC(pfile, '(');
677 while ((c = GETC()) != ')')
683 CPP_PUTC(pfile, ' ');
687 else if (c == '\n' || c == EOF)
689 if (c == '\n') FORWARD(-1);
690 cpp_error (pfile, "un-terminated assertion answer");
694 /* \r cannot be a macro escape here. */
695 CPP_BUMP_LINE (pfile);
703 if (pfile->limit[-1] == ' ')
704 pfile->limit[-1] = ')';
705 else if (pfile->limit[-1] == '(')
707 cpp_error (pfile, "empty token sequence in assertion");
711 CPP_PUTC (pfile, ')');
716 /* Get the next token, and add it to the text in pfile->token_buffer.
717 Return the kind of token we got. */
720 _cpp_lex_token (pfile)
723 register int c, c2, c3;
724 enum cpp_token token;
738 if (CPP_OPTION (pfile, discard_comments))
739 c = skip_comment (pfile, c);
741 c = copy_comment (pfile, c);
745 /* Comments are equivalent to spaces.
746 For -traditional, a comment is equivalent to nothing. */
747 if (!CPP_OPTION (pfile, discard_comments))
749 else if (CPP_TRADITIONAL (pfile)
750 && ! is_space (PEEKC ()))
752 if (pfile->parsing_define_directive)
764 if (pfile->parsing_if_directive)
766 if (_cpp_parse_assertion (pfile))
767 return CPP_ASSERTION;
771 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
773 CPP_RESERVE (pfile, 3);
774 CPP_PUTC_Q (pfile, '#');
775 CPP_NUL_TERMINATE_Q (pfile);
777 return CPP_STRINGIZE;
780 CPP_PUTC_Q (pfile, '#');
781 CPP_NUL_TERMINATE_Q (pfile);
785 if (!pfile->only_seen_white)
787 return CPP_DIRECTIVE;
791 parse_string (pfile, c);
792 return c == '\'' ? CPP_CHAR : CPP_STRING;
795 if (!CPP_OPTION (pfile, dollars_in_ident))
800 if (CPP_OPTION (pfile, cplusplus) && PEEKC () == ':')
808 if (c2 == c || c2 == '=')
825 if (CPP_OPTION (pfile, chill))
826 goto comment; /* Chill style comment */
834 if (CPP_OPTION (pfile, cplusplus) && PEEKN (1) == '*')
836 /* In C++, there's a ->* operator. */
838 CPP_RESERVE (pfile, 4);
839 CPP_PUTC_Q (pfile, c);
840 CPP_PUTC_Q (pfile, GETC ());
841 CPP_PUTC_Q (pfile, GETC ());
842 CPP_NUL_TERMINATE_Q (pfile);
850 if (pfile->parsing_include_directive)
858 if (c == '\n' || c == EOF)
861 "missing '>' in `#include <FILENAME>'");
866 if (!CPP_BUFFER (pfile)->has_escapes)
868 /* Backslash newline is replaced by nothing. */
869 CPP_ADJUST_WRITTEN (pfile, -1);
870 CPP_BUMP_LINE (pfile);
874 /* We might conceivably get \r- or \r<space> in
875 here. Just delete 'em. */
877 if (d != '-' && d != ' ')
878 cpp_ice (pfile, "unrecognized escape \\r%c", d);
879 CPP_ADJUST_WRITTEN (pfile, -1);
885 /* else fall through */
890 /* GNU C++ supports MIN and MAX operators <? and >?. */
891 if (c2 != c && (!CPP_OPTION (pfile, cplusplus) || c2 != '?'))
894 CPP_RESERVE (pfile, 4);
896 CPP_PUTC (pfile, c2);
899 CPP_PUTC_Q (pfile, GETC ());
900 CPP_NUL_TERMINATE_Q (pfile);
907 CPP_RESERVE(pfile, 2);
908 CPP_PUTC_Q (pfile, '.');
913 /* In C++ there's a .* operator. */
914 if (CPP_OPTION (pfile, cplusplus) && c2 == '*')
917 if (c2 == '.' && PEEKN(1) == '.')
919 CPP_RESERVE(pfile, 4);
920 CPP_PUTC_Q (pfile, '.');
921 CPP_PUTC_Q (pfile, '.');
922 CPP_PUTC_Q (pfile, '.');
924 CPP_NUL_TERMINATE_Q (pfile);
931 CPP_RESERVE(pfile, 3);
932 CPP_PUTC_Q (pfile, c);
933 CPP_PUTC_Q (pfile, GETC ());
934 CPP_NUL_TERMINATE_Q (pfile);
939 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
943 parse_string (pfile, c);
944 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
948 case '0': case '1': case '2': case '3': case '4':
949 case '5': case '6': case '7': case '8': case '9':
954 CPP_RESERVE (pfile, 2);
955 CPP_PUTC_Q (pfile, c);
959 if (!is_numchar(c) && c != '.'
960 && ((c2 != 'e' && c2 != 'E'
961 && ((c2 != 'p' && c2 != 'P')
962 || CPP_OPTION (pfile, c89)))
963 || (c != '+' && c != '-')))
968 CPP_NUL_TERMINATE_Q (pfile);
970 case 'b': case 'c': case 'd': case 'h': case 'o':
971 case 'B': case 'C': case 'D': case 'H': case 'O':
972 if (CPP_OPTION (pfile, chill) && PEEKC () == '\'')
974 CPP_RESERVE (pfile, 2);
975 CPP_PUTC_Q (pfile, c);
976 CPP_PUTC_Q (pfile, '\'');
982 goto chill_number_eof;
989 CPP_RESERVE (pfile, 2);
990 CPP_PUTC_Q (pfile, c);
991 CPP_NUL_TERMINATE_Q (pfile);
998 CPP_NUL_TERMINATE (pfile);
1005 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
1006 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
1007 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
1008 case 'x': case 'y': case 'z':
1009 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
1010 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
1011 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1014 _cpp_parse_name (pfile, c);
1017 case ' ': case '\t': case '\v': case '\f': case '\0':
1026 CPP_PUTC (pfile, c);
1028 if (c == EOF || !is_hspace(c))
1033 null_warning (pfile, null_count);
1038 if (CPP_BUFFER (pfile)->has_escapes)
1043 if (pfile->output_escapes)
1044 CPP_PUTS (pfile, "\r-", 2);
1045 _cpp_parse_name (pfile, GETC ());
1050 /* "\r " means a space, but only if necessary to prevent
1051 accidental token concatenation. */
1052 CPP_RESERVE (pfile, 2);
1053 if (pfile->output_escapes)
1054 CPP_PUTC_Q (pfile, '\r');
1055 CPP_PUTC_Q (pfile, c);
1060 cpp_ice (pfile, "unrecognized escape \\r%c", c);
1066 /* Backslash newline is ignored. */
1067 CPP_BUMP_LINE (pfile);
1072 CPP_PUTC (pfile, c);
1075 case '(': token = CPP_LPAREN; goto char1;
1076 case ')': token = CPP_RPAREN; goto char1;
1077 case '{': token = CPP_LBRACE; goto char1;
1078 case '}': token = CPP_RBRACE; goto char1;
1079 case ',': token = CPP_COMMA; goto char1;
1080 case ';': token = CPP_SEMICOLON; goto char1;
1086 CPP_PUTC (pfile, c);
1091 /* Check for and expand a macro, which is from WRITTEN to CPP_WRITTEN (pfile).
1092 Caller is expected to have checked no_macro_expand. */
1094 maybe_macroexpand (pfile, written)
1098 U_CHAR *macro = pfile->token_buffer + written;
1099 size_t len = CPP_WRITTEN (pfile) - written;
1100 HASHNODE *hp = _cpp_lookup (pfile, macro, len);
1104 if (hp->type == T_DISABLED)
1106 if (pfile->output_escapes)
1108 /* Insert a no-reexpand marker before IDENT. */
1109 CPP_RESERVE (pfile, 2);
1110 CPP_ADJUST_WRITTEN (pfile, 2);
1111 macro = pfile->token_buffer + written;
1113 memmove (macro + 2, macro, len);
1119 if (hp->type == T_EMPTY)
1121 /* Special case optimization: macro expands to nothing. */
1122 CPP_SET_WRITTEN (pfile, written);
1123 CPP_PUTC_Q (pfile, ' ');
1127 /* If macro wants an arglist, verify that a '(' follows. */
1128 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1130 int macbuf_whitespace = 0;
1133 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1135 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
1138 _cpp_skip_hspace (pfile);
1145 if (point != CPP_BUFFER (pfile)->cur)
1146 macbuf_whitespace = 1;
1150 goto not_macro_call;
1151 cpp_pop_buffer (pfile);
1154 CPP_SET_MARK (pfile);
1157 _cpp_skip_hspace (pfile);
1164 CPP_GOTO_MARK (pfile);
1169 if (macbuf_whitespace)
1170 CPP_PUTC (pfile, ' ');
1176 /* This is now known to be a macro call.
1177 Expand the macro, reading arguments as needed,
1178 and push the expansion on the input stack. */
1179 _cpp_macroexpand (pfile, hp);
1180 CPP_SET_WRITTEN (pfile, written);
1185 cpp_get_token (pfile)
1188 enum cpp_token token;
1189 long written = CPP_WRITTEN (pfile);
1192 token = _cpp_lex_token (pfile);
1197 pfile->potential_control_macro = 0;
1198 pfile->only_seen_white = 0;
1202 if (pfile->only_seen_white == 0)
1203 pfile->only_seen_white = 1;
1204 CPP_BUMP_LINE (pfile);
1205 if (! CPP_OPTION (pfile, no_line_commands))
1208 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
1209 _cpp_output_line_command (pfile, same_file);
1218 pfile->potential_control_macro = 0;
1219 if (_cpp_handle_directive (pfile))
1220 return CPP_DIRECTIVE;
1221 pfile->only_seen_white = 0;
1222 CPP_PUTC (pfile, '#');
1226 pfile->potential_control_macro = 0;
1227 pfile->only_seen_white = 0;
1228 if (! pfile->no_macro_expand
1229 && maybe_macroexpand (pfile, written))
1234 if (CPP_BUFFER (pfile)->manual_pop)
1235 /* If we've been reading from redirected input, the
1236 frontend will pop the buffer. */
1238 else if (CPP_BUFFER (pfile)->seen_eof)
1240 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
1243 cpp_pop_buffer (pfile);
1248 _cpp_handle_eof (pfile);
1254 /* Like cpp_get_token, but skip spaces and comments. */
1257 cpp_get_non_space_token (pfile)
1260 int old_written = CPP_WRITTEN (pfile);
1263 enum cpp_token token = cpp_get_token (pfile);
1264 if (token != CPP_COMMENT && token != CPP_HSPACE && token != CPP_VSPACE)
1266 CPP_SET_WRITTEN (pfile, old_written);
1270 /* Like cpp_get_token, except that it does not execute directives,
1271 does not consume vertical space, and automatically pops off macro
1274 XXX This function will exist only till collect_expansion doesn't
1275 need to see whitespace anymore, then it'll be merged with
1276 _cpp_get_directive_token (below). */
1278 _cpp_get_define_token (pfile)
1282 enum cpp_token token;
1285 old_written = CPP_WRITTEN (pfile);
1286 token = _cpp_lex_token (pfile);
1293 /* Put it back and return VSPACE. */
1295 CPP_ADJUST_WRITTEN (pfile, -1);
1299 if (CPP_PEDANTIC (pfile))
1302 p = pfile->token_buffer + old_written;
1303 limit = CPP_PWRITTEN (pfile);
1306 if (*p == '\v' || *p == '\f')
1307 cpp_pedwarn (pfile, "%s in preprocessing directive",
1308 *p == '\f' ? "formfeed" : "vertical tab");
1315 /* Don't execute the directive, but don't smash it to OTHER either. */
1316 CPP_PUTC (pfile, '#');
1317 return CPP_DIRECTIVE;
1320 if (! pfile->no_macro_expand
1321 && maybe_macroexpand (pfile, old_written))
1326 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1328 cpp_pop_buffer (pfile);
1332 /* This can happen for files that don't end with a newline,
1333 and for cpp_define and friends. Pretend they do, so
1334 callers don't have to deal. A warning will be issued by
1335 someone else, if necessary. */
1340 /* Just like _cpp_get_define_token except that it discards horizontal
1344 _cpp_get_directive_token (pfile)
1347 int old_written = CPP_WRITTEN (pfile);
1350 enum cpp_token token = _cpp_get_define_token (pfile);
1351 if (token != CPP_COMMENT && token != CPP_HSPACE)
1353 CPP_SET_WRITTEN (pfile, old_written);
1357 /* Determine the current line and column. Used only by read_and_prescan. */
1359 find_position (start, limit, linep)
1362 unsigned long *linep;
1364 unsigned long line = *linep;
1365 U_CHAR *lbase = start;
1366 while (start < limit)
1368 U_CHAR ch = *start++;
1369 if (ch == '\n' || ch == '\r')
1379 /* The following table is used by _cpp_read_and_prescan. If we have
1380 designated initializers, it can be constant data; otherwise, it is
1381 set up at runtime by _cpp_init_input_buffer. */
1384 #define UCHAR_MAX 255 /* assume 8-bit bytes */
1387 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
1388 #define init_chartab() /* nothing */
1389 #define CHARTAB static const unsigned char chartab[UCHAR_MAX + 1] = {
1391 #define s(p, v) [p] = v,
1393 #define CHARTAB static unsigned char chartab[UCHAR_MAX + 1] = { 0 }; \
1394 static void init_chartab PARAMS ((void)) { \
1395 unsigned char *x = chartab;
1397 #define s(p, v) x[p] = v;
1400 /* Table of characters that can't be handled in the inner loop.
1401 Also contains the mapping between trigraph third characters and their
1403 #define SPECCASE_CR 1
1404 #define SPECCASE_BACKSLASH 2
1405 #define SPECCASE_QUESTION 3
1408 s('\r', SPECCASE_CR)
1409 s('\\', SPECCASE_BACKSLASH)
1410 s('?', SPECCASE_QUESTION)
1412 s('=', '#') s(')', ']') s('!', '|')
1413 s('(', '[') s('\'', '^') s('>', '}')
1414 s('/', '\\') s('<', '{') s('-', '~')
1421 #define NORMAL(c) ((chartab[c]) == 0 || (chartab[c]) > SPECCASE_QUESTION)
1422 #define NONTRI(c) ((c) <= SPECCASE_QUESTION)
1424 /* Read the entire contents of file DESC into buffer BUF. LEN is how
1425 much memory to allocate initially; more will be allocated if
1426 necessary. Convert end-of-line markers (\n, \r, \r\n, \n\r) to
1427 canonical form (\n). If enabled, convert and/or warn about
1428 trigraphs. Convert backslash-newline to a one-character escape
1429 (\r) and remove it from "embarrassing" places (i.e. the middle of a
1430 token). If there is no newline at the end of the file, add one and
1431 warn. Returns -1 on failure, or the actual length of the data to
1434 This function does a lot of work, and can be a serious performance
1435 bottleneck. It has been tuned heavily; make sure you understand it
1436 before hacking. The common case - no trigraphs, Unix style line
1437 breaks, backslash-newline set off by whitespace, newline at EOF -
1438 has been optimized at the expense of the others. The performance
1439 penalty for DOS style line breaks (\r\n) is about 15%.
1441 Warnings lose particularly heavily since we have to determine the
1442 line number, which involves scanning from the beginning of the file
1443 or from the last warning. The penalty for the absence of a newline
1444 at the end of reload1.c is about 60%. (reload1.c is 329k.)
1446 If your file has more than one kind of end-of-line marker, you
1447 will get messed-up line numbering.
1449 So that the cases of the switch statement do not have to concern
1450 themselves with the complications of reading beyond the end of the
1451 buffer, the buffer is guaranteed to have at least 3 characters in
1452 it (or however many are left in the file, if less) on entry to the
1453 switch. This is enough to handle trigraphs and the "\\\n\r" and
1456 The end of the buffer is marked by a '\\', which, being a special
1457 character, guarantees we will exit the fast-scan loops and perform
1461 _cpp_read_and_prescan (pfile, fp, desc, len)
1467 U_CHAR *buf = (U_CHAR *) xmalloc (len);
1468 U_CHAR *ip, *op, *line_base;
1471 unsigned int deferred_newlines;
1476 deferred_newlines = 0;
1480 ibase = pfile->input_buffer + 3;
1482 ip[-1] = '\0'; /* Guarantee no match with \n for SPECCASE_CR */
1486 U_CHAR *near_buff_end;
1488 /* Copy previous char plus unprocessed (at most 2) chars
1489 to beginning of buffer, refill it with another
1490 read(), and continue processing */
1491 memcpy(ip - count - 1, ip - 1, 3);
1494 count = read (desc, ibase, pfile->input_buffer_len);
1498 ibase[count] = '\\'; /* Marks end of buffer */
1501 near_buff_end = pfile->input_buffer + count;
1506 size_t delta_line_base;
1510 This could happen if the file is larger than half the
1511 maximum address space of the machine. */
1514 delta_op = op - buf;
1515 delta_line_base = line_base - buf;
1516 buf = (U_CHAR *) xrealloc (buf, len);
1517 op = buf + delta_op;
1518 line_base = buf + delta_line_base;
1525 /* Allow normal processing of the (at most 2) remaining
1526 characters. The end-of-buffer marker is still present
1527 and prevents false matches within the switch. */
1528 near_buff_end = ibase - 1;
1535 /* Deal with \-newline, potentially in the middle of a token. */
1536 if (deferred_newlines)
1538 if (op != buf && ! is_space (op[-1]) && op[-1] != '\r')
1540 /* Previous was not white space. Skip to white
1541 space, if we can, before outputting the \r's */
1543 while (ip[span] != ' '
1546 && NORMAL(ip[span]))
1548 memcpy (op, ip, span);
1551 if (! NORMAL(ip[0]))
1554 while (deferred_newlines)
1555 deferred_newlines--, *op++ = '\r';
1558 /* Copy as much as we can without special treatment. */
1560 while (NORMAL (ip[span])) span++;
1561 memcpy (op, ip, span);
1566 if (ip > near_buff_end) /* Do we have enough chars? */
1568 switch (chartab[*ip++])
1570 case SPECCASE_CR: /* \r */
1579 case SPECCASE_BACKSLASH: /* \ */
1582 deferred_newlines++;
1584 if (*ip == '\r') ip++;
1586 else if (*ip == '\r')
1588 deferred_newlines++;
1590 if (*ip == '\n') ip++;
1596 case SPECCASE_QUESTION: /* ? */
1600 *op++ = '?'; /* Normal non-trigraph case */
1609 if (CPP_OPTION (pfile, warn_trigraphs))
1612 line_base = find_position (line_base, op, &line);
1613 col = op - line_base + 1;
1614 if (CPP_OPTION (pfile, trigraphs))
1615 cpp_warning_with_line (pfile, line, col,
1616 "trigraph ??%c converted to %c", d, t);
1618 cpp_warning_with_line (pfile, line, col,
1619 "trigraph ??%c ignored", d);
1623 if (CPP_OPTION (pfile, trigraphs))
1625 op[-1] = t; /* Overwrite '?' */
1630 goto do_speccase; /* May need buffer refill */
1650 line_base = find_position (line_base, op, &line);
1651 col = op - line_base + 1;
1652 cpp_warning_with_line (pfile, line, col, "no newline at end of file\n");
1653 if (offset + 1 > len)
1656 if (offset + 1 > len)
1658 buf = (U_CHAR *) xrealloc (buf, len);
1664 fp->buf = ((len - offset < 20) ? buf : (U_CHAR *)xrealloc (buf, op - buf));
1668 cpp_error (pfile, "file is too large (>%lu bytes)\n", (unsigned long)offset);
1673 cpp_error_from_errno (pfile, fp->ihash->name);
1678 /* Allocate pfile->input_buffer, and initialize chartab[]
1679 if it hasn't happened already. */
1682 _cpp_init_input_buffer (pfile)
1689 /* Determine the appropriate size for the input buffer. Normal C
1690 source files are smaller than eight K. */
1691 /* 8Kbytes of buffer proper, 1 to detect running off the end without
1692 address arithmetic all the time, and 3 for pushback during buffer
1693 refill, in case there's a potential trigraph or end-of-line
1694 digraph at the end of a block. */
1696 tmp = (U_CHAR *) xmalloc (8192 + 1 + 3);
1697 pfile->input_buffer = tmp;
1698 pfile->input_buffer_len = 8192;